0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/delay.h>
0013 #include <linux/gpio/consumer.h>
0014 #include <linux/i2c.h>
0015 #include <linux/input.h>
0016 #include <linux/input/touchscreen.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/irq.h>
0019 #include <linux/module.h>
0020 #include <linux/regulator/consumer.h>
0021 #include <linux/timer.h>
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 #define BU21029_HWID_REG (0x0E << 3)
0040 #define SUPPORTED_HWID 0x0229
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 #define BU21029_CFR0_REG (0x00 << 3)
0055 #define CFR0_VALUE 0x00
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 #define BU21029_CFR1_REG (0x01 << 3)
0071 #define CFR1_VALUE 0xA6
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 #define BU21029_CFR2_REG (0x02 << 3)
0087 #define CFR2_VALUE 0xC9
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 #define BU21029_CFR3_REG (0x0B << 3)
0108 #define CFR3_VALUE 0x42
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 #define BU21029_LDO_REG (0x0C << 3)
0121 #define LDO_VALUE 0x77
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 #define BU21029_AUTOSCAN 0x80
0138
0139
0140
0141
0142
0143
0144
0145 #define PEN_UP_TIMEOUT_MS 50
0146
0147 #define STOP_DELAY_MIN_US 50
0148 #define STOP_DELAY_MAX_US 1000
0149 #define START_DELAY_MS 2
0150 #define BUF_LEN 8
0151 #define SCALE_12BIT (1 << 12)
0152 #define MAX_12BIT ((1 << 12) - 1)
0153 #define DRIVER_NAME "bu21029"
0154
0155 struct bu21029_ts_data {
0156 struct i2c_client *client;
0157 struct input_dev *in_dev;
0158 struct timer_list timer;
0159 struct regulator *vdd;
0160 struct gpio_desc *reset_gpios;
0161 u32 x_plate_ohms;
0162 struct touchscreen_properties prop;
0163 };
0164
0165 static void bu21029_touch_report(struct bu21029_ts_data *bu21029, const u8 *buf)
0166 {
0167 u16 x, y, z1, z2;
0168 u32 rz;
0169 s32 max_pressure = input_abs_get_max(bu21029->in_dev, ABS_PRESSURE);
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181 x = (buf[0] << 4) | (buf[1] >> 4);
0182 y = (buf[2] << 4) | (buf[3] >> 4);
0183 z1 = (buf[4] << 4) | (buf[5] >> 4);
0184 z2 = (buf[6] << 4) | (buf[7] >> 4);
0185
0186 if (z1 && z2) {
0187
0188
0189
0190
0191
0192
0193
0194 rz = z2 - z1;
0195 rz *= x;
0196 rz *= bu21029->x_plate_ohms;
0197 rz /= z1;
0198 rz = DIV_ROUND_CLOSEST(rz, SCALE_12BIT);
0199 if (rz <= max_pressure) {
0200 touchscreen_report_pos(bu21029->in_dev, &bu21029->prop,
0201 x, y, false);
0202 input_report_abs(bu21029->in_dev, ABS_PRESSURE,
0203 max_pressure - rz);
0204 input_report_key(bu21029->in_dev, BTN_TOUCH, 1);
0205 input_sync(bu21029->in_dev);
0206 }
0207 }
0208 }
0209
0210 static void bu21029_touch_release(struct timer_list *t)
0211 {
0212 struct bu21029_ts_data *bu21029 = from_timer(bu21029, t, timer);
0213
0214 input_report_abs(bu21029->in_dev, ABS_PRESSURE, 0);
0215 input_report_key(bu21029->in_dev, BTN_TOUCH, 0);
0216 input_sync(bu21029->in_dev);
0217 }
0218
0219 static irqreturn_t bu21029_touch_soft_irq(int irq, void *data)
0220 {
0221 struct bu21029_ts_data *bu21029 = data;
0222 u8 buf[BUF_LEN];
0223 int error;
0224
0225
0226
0227
0228
0229 error = i2c_smbus_read_i2c_block_data(bu21029->client, BU21029_AUTOSCAN,
0230 sizeof(buf), buf);
0231 if (error < 0)
0232 goto out;
0233
0234 bu21029_touch_report(bu21029, buf);
0235
0236
0237 mod_timer(&bu21029->timer,
0238 jiffies + msecs_to_jiffies(PEN_UP_TIMEOUT_MS));
0239
0240 out:
0241 return IRQ_HANDLED;
0242 }
0243
0244 static void bu21029_put_chip_in_reset(struct bu21029_ts_data *bu21029)
0245 {
0246 if (bu21029->reset_gpios) {
0247 gpiod_set_value_cansleep(bu21029->reset_gpios, 1);
0248 usleep_range(STOP_DELAY_MIN_US, STOP_DELAY_MAX_US);
0249 }
0250 }
0251
0252 static int bu21029_start_chip(struct input_dev *dev)
0253 {
0254 struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
0255 struct i2c_client *i2c = bu21029->client;
0256 struct {
0257 u8 reg;
0258 u8 value;
0259 } init_table[] = {
0260 {BU21029_CFR0_REG, CFR0_VALUE},
0261 {BU21029_CFR1_REG, CFR1_VALUE},
0262 {BU21029_CFR2_REG, CFR2_VALUE},
0263 {BU21029_CFR3_REG, CFR3_VALUE},
0264 {BU21029_LDO_REG, LDO_VALUE}
0265 };
0266 int error, i;
0267 __be16 hwid;
0268
0269 error = regulator_enable(bu21029->vdd);
0270 if (error) {
0271 dev_err(&i2c->dev, "failed to power up chip: %d", error);
0272 return error;
0273 }
0274
0275
0276 if (bu21029->reset_gpios) {
0277 gpiod_set_value_cansleep(bu21029->reset_gpios, 0);
0278 msleep(START_DELAY_MS);
0279 }
0280
0281 error = i2c_smbus_read_i2c_block_data(i2c, BU21029_HWID_REG,
0282 sizeof(hwid), (u8 *)&hwid);
0283 if (error < 0) {
0284 dev_err(&i2c->dev, "failed to read HW ID\n");
0285 goto err_out;
0286 }
0287
0288 if (be16_to_cpu(hwid) != SUPPORTED_HWID) {
0289 dev_err(&i2c->dev,
0290 "unsupported HW ID 0x%x\n", be16_to_cpu(hwid));
0291 error = -ENODEV;
0292 goto err_out;
0293 }
0294
0295 for (i = 0; i < ARRAY_SIZE(init_table); ++i) {
0296 error = i2c_smbus_write_byte_data(i2c,
0297 init_table[i].reg,
0298 init_table[i].value);
0299 if (error < 0) {
0300 dev_err(&i2c->dev,
0301 "failed to write %#02x to register %#02x: %d\n",
0302 init_table[i].value, init_table[i].reg,
0303 error);
0304 goto err_out;
0305 }
0306 }
0307
0308 error = i2c_smbus_write_byte(i2c, BU21029_AUTOSCAN);
0309 if (error < 0) {
0310 dev_err(&i2c->dev, "failed to start autoscan\n");
0311 goto err_out;
0312 }
0313
0314 enable_irq(bu21029->client->irq);
0315 return 0;
0316
0317 err_out:
0318 bu21029_put_chip_in_reset(bu21029);
0319 regulator_disable(bu21029->vdd);
0320 return error;
0321 }
0322
0323 static void bu21029_stop_chip(struct input_dev *dev)
0324 {
0325 struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
0326
0327 disable_irq(bu21029->client->irq);
0328 del_timer_sync(&bu21029->timer);
0329
0330 bu21029_put_chip_in_reset(bu21029);
0331 regulator_disable(bu21029->vdd);
0332 }
0333
0334 static int bu21029_probe(struct i2c_client *client,
0335 const struct i2c_device_id *id)
0336 {
0337 struct bu21029_ts_data *bu21029;
0338 struct input_dev *in_dev;
0339 int error;
0340
0341 if (!i2c_check_functionality(client->adapter,
0342 I2C_FUNC_SMBUS_WRITE_BYTE |
0343 I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
0344 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
0345 dev_err(&client->dev,
0346 "i2c functionality support is not sufficient\n");
0347 return -EIO;
0348 }
0349
0350 bu21029 = devm_kzalloc(&client->dev, sizeof(*bu21029), GFP_KERNEL);
0351 if (!bu21029)
0352 return -ENOMEM;
0353
0354 error = device_property_read_u32(&client->dev, "rohm,x-plate-ohms",
0355 &bu21029->x_plate_ohms);
0356 if (error) {
0357 dev_err(&client->dev,
0358 "invalid 'x-plate-ohms' supplied: %d\n", error);
0359 return error;
0360 }
0361
0362 bu21029->vdd = devm_regulator_get(&client->dev, "vdd");
0363 if (IS_ERR(bu21029->vdd)) {
0364 error = PTR_ERR(bu21029->vdd);
0365 if (error != -EPROBE_DEFER)
0366 dev_err(&client->dev,
0367 "failed to acquire 'vdd' supply: %d\n", error);
0368 return error;
0369 }
0370
0371 bu21029->reset_gpios = devm_gpiod_get_optional(&client->dev,
0372 "reset", GPIOD_OUT_HIGH);
0373 if (IS_ERR(bu21029->reset_gpios)) {
0374 error = PTR_ERR(bu21029->reset_gpios);
0375 if (error != -EPROBE_DEFER)
0376 dev_err(&client->dev,
0377 "failed to acquire 'reset' gpio: %d\n", error);
0378 return error;
0379 }
0380
0381 in_dev = devm_input_allocate_device(&client->dev);
0382 if (!in_dev) {
0383 dev_err(&client->dev, "unable to allocate input device\n");
0384 return -ENOMEM;
0385 }
0386
0387 bu21029->client = client;
0388 bu21029->in_dev = in_dev;
0389 timer_setup(&bu21029->timer, bu21029_touch_release, 0);
0390
0391 in_dev->name = DRIVER_NAME;
0392 in_dev->id.bustype = BUS_I2C;
0393 in_dev->open = bu21029_start_chip;
0394 in_dev->close = bu21029_stop_chip;
0395
0396 input_set_capability(in_dev, EV_KEY, BTN_TOUCH);
0397 input_set_abs_params(in_dev, ABS_X, 0, MAX_12BIT, 0, 0);
0398 input_set_abs_params(in_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
0399 input_set_abs_params(in_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
0400 touchscreen_parse_properties(in_dev, false, &bu21029->prop);
0401
0402 input_set_drvdata(in_dev, bu21029);
0403
0404 error = devm_request_threaded_irq(&client->dev, client->irq,
0405 NULL, bu21029_touch_soft_irq,
0406 IRQF_ONESHOT | IRQF_NO_AUTOEN,
0407 DRIVER_NAME, bu21029);
0408 if (error) {
0409 dev_err(&client->dev,
0410 "unable to request touch irq: %d\n", error);
0411 return error;
0412 }
0413
0414 error = input_register_device(in_dev);
0415 if (error) {
0416 dev_err(&client->dev,
0417 "unable to register input device: %d\n", error);
0418 return error;
0419 }
0420
0421 i2c_set_clientdata(client, bu21029);
0422
0423 return 0;
0424 }
0425
0426 static int __maybe_unused bu21029_suspend(struct device *dev)
0427 {
0428 struct i2c_client *i2c = to_i2c_client(dev);
0429 struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
0430
0431 if (!device_may_wakeup(dev)) {
0432 mutex_lock(&bu21029->in_dev->mutex);
0433 if (input_device_enabled(bu21029->in_dev))
0434 bu21029_stop_chip(bu21029->in_dev);
0435 mutex_unlock(&bu21029->in_dev->mutex);
0436 }
0437
0438 return 0;
0439 }
0440
0441 static int __maybe_unused bu21029_resume(struct device *dev)
0442 {
0443 struct i2c_client *i2c = to_i2c_client(dev);
0444 struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
0445
0446 if (!device_may_wakeup(dev)) {
0447 mutex_lock(&bu21029->in_dev->mutex);
0448 if (input_device_enabled(bu21029->in_dev))
0449 bu21029_start_chip(bu21029->in_dev);
0450 mutex_unlock(&bu21029->in_dev->mutex);
0451 }
0452
0453 return 0;
0454 }
0455 static SIMPLE_DEV_PM_OPS(bu21029_pm_ops, bu21029_suspend, bu21029_resume);
0456
0457 static const struct i2c_device_id bu21029_ids[] = {
0458 { DRIVER_NAME, 0 },
0459 { }
0460 };
0461 MODULE_DEVICE_TABLE(i2c, bu21029_ids);
0462
0463 #ifdef CONFIG_OF
0464 static const struct of_device_id bu21029_of_ids[] = {
0465 { .compatible = "rohm,bu21029" },
0466 { }
0467 };
0468 MODULE_DEVICE_TABLE(of, bu21029_of_ids);
0469 #endif
0470
0471 static struct i2c_driver bu21029_driver = {
0472 .driver = {
0473 .name = DRIVER_NAME,
0474 .of_match_table = of_match_ptr(bu21029_of_ids),
0475 .pm = &bu21029_pm_ops,
0476 },
0477 .id_table = bu21029_ids,
0478 .probe = bu21029_probe,
0479 };
0480 module_i2c_driver(bu21029_driver);
0481
0482 MODULE_AUTHOR("Zhu Yi <yi.zhu5@cn.bosch.com>");
0483 MODULE_DESCRIPTION("Rohm BU21029 touchscreen controller driver");
0484 MODULE_LICENSE("GPL v2");