bu21029_ts.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Rohm BU21029 touchscreen controller driver
  4. *
  5. * Copyright (C) 2015-2018 Bosch Sicherheitssysteme GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/i2c.h>
  14. #include <linux/input.h>
  15. #include <linux/input/touchscreen.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/module.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/timer.h>
  21. /*
  22. * HW_ID1 Register (PAGE=0, ADDR=0x0E, Reset value=0x02, Read only)
  23. * +--------+--------+--------+--------+--------+--------+--------+--------+
  24. * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  25. * +--------+--------+--------+--------+--------+--------+--------+--------+
  26. * | HW_IDH |
  27. * +--------+--------+--------+--------+--------+--------+--------+--------+
  28. * HW_ID2 Register (PAGE=0, ADDR=0x0F, Reset value=0x29, Read only)
  29. * +--------+--------+--------+--------+--------+--------+--------+--------+
  30. * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  31. * +--------+--------+--------+--------+--------+--------+--------+--------+
  32. * | HW_IDL |
  33. * +--------+--------+--------+--------+--------+--------+--------+--------+
  34. * HW_IDH: high 8bits of IC's ID
  35. * HW_IDL: low 8bits of IC's ID
  36. */
  37. #define BU21029_HWID_REG (0x0E << 3)
  38. #define SUPPORTED_HWID 0x0229
  39. /*
  40. * CFR0 Register (PAGE=0, ADDR=0x00, Reset value=0x20)
  41. * +--------+--------+--------+--------+--------+--------+--------+--------+
  42. * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  43. * +--------+--------+--------+--------+--------+--------+--------+--------+
  44. * | 0 | 0 | CALIB | INTRM | 0 | 0 | 0 | 0 |
  45. * +--------+--------+--------+--------+--------+--------+--------+--------+
  46. * CALIB: 0 = not to use calibration result (*)
  47. * 1 = use calibration result
  48. * INTRM: 0 = INT output depend on "pen down" (*)
  49. * 1 = INT output always "0"
  50. */
  51. #define BU21029_CFR0_REG (0x00 << 3)
  52. #define CFR0_VALUE 0x00
  53. /*
  54. * CFR1 Register (PAGE=0, ADDR=0x01, Reset value=0xA6)
  55. * +--------+--------+--------+--------+--------+--------+--------+--------+
  56. * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  57. * +--------+--------+--------+--------+--------+--------+--------+--------+
  58. * | MAV | AVE[2:0] | 0 | SMPL[2:0] |
  59. * +--------+--------+--------+--------+--------+--------+--------+--------+
  60. * MAV: 0 = median average filter off
  61. * 1 = median average filter on (*)
  62. * AVE: AVE+1 = number of average samples for MAV,
  63. * if AVE>SMPL, then AVE=SMPL (=3)
  64. * SMPL: SMPL+1 = number of conversion samples for MAV (=7)
  65. */
  66. #define BU21029_CFR1_REG (0x01 << 3)
  67. #define CFR1_VALUE 0xA6
  68. /*
  69. * CFR2 Register (PAGE=0, ADDR=0x02, Reset value=0x04)
  70. * +--------+--------+--------+--------+--------+--------+--------+--------+
  71. * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  72. * +--------+--------+--------+--------+--------+--------+--------+--------+
  73. * | INTVL_TIME[3:0] | TIME_ST_ADC[3:0] |
  74. * +--------+--------+--------+--------+--------+--------+--------+--------+
  75. * INTVL_TIME: waiting time between completion of conversion
  76. * and start of next conversion, only usable in
  77. * autoscan mode (=20.480ms)
  78. * TIME_ST_ADC: waiting time between application of voltage
  79. * to panel and start of A/D conversion (=100us)
  80. */
  81. #define BU21029_CFR2_REG (0x02 << 3)
  82. #define CFR2_VALUE 0xC9
  83. /*
  84. * CFR3 Register (PAGE=0, ADDR=0x0B, Reset value=0x72)
  85. * +--------+--------+--------+--------+--------+--------+--------+--------+
  86. * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  87. * +--------+--------+--------+--------+--------+--------+--------+--------+
  88. * | RM8 | STRETCH| PU90K | DUAL | PIDAC_OFS[3:0] |
  89. * +--------+--------+--------+--------+--------+--------+--------+--------+
  90. * RM8: 0 = coordinate resolution is 12bit (*)
  91. * 1 = coordinate resolution is 8bit
  92. * STRETCH: 0 = SCL_STRETCH function off
  93. * 1 = SCL_STRETCH function on (*)
  94. * PU90K: 0 = internal pull-up resistance for touch detection is ~50kohms (*)
  95. * 1 = internal pull-up resistance for touch detection is ~90kohms
  96. * DUAL: 0 = dual touch detection off (*)
  97. * 1 = dual touch detection on
  98. * PIDAC_OFS: dual touch detection circuit adjustment, it is not necessary
  99. * to change this from initial value
  100. */
  101. #define BU21029_CFR3_REG (0x0B << 3)
  102. #define CFR3_VALUE 0x42
  103. /*
  104. * LDO Register (PAGE=0, ADDR=0x0C, Reset value=0x00)
  105. * +--------+--------+--------+--------+--------+--------+--------+--------+
  106. * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  107. * +--------+--------+--------+--------+--------+--------+--------+--------+
  108. * | 0 | PVDD[2:0] | 0 | AVDD[2:0] |
  109. * +--------+--------+--------+--------+--------+--------+--------+--------+
  110. * PVDD: output voltage of panel output regulator (=2.000V)
  111. * AVDD: output voltage of analog circuit regulator (=2.000V)
  112. */
  113. #define BU21029_LDO_REG (0x0C << 3)
  114. #define LDO_VALUE 0x77
  115. /*
  116. * Serial Interface Command Byte 1 (CID=1)
  117. * +--------+--------+--------+--------+--------+--------+--------+--------+
  118. * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  119. * +--------+--------+--------+--------+--------+--------+--------+--------+
  120. * | 1 | CF | CMSK | PDM | STP |
  121. * +--------+--------+--------+--------+--------+--------+--------+--------+
  122. * CF: conversion function, see table 3 in datasheet p6 (=0000, automatic scan)
  123. * CMSK: 0 = executes convert function (*)
  124. * 1 = reads the convert result
  125. * PDM: 0 = power down after convert function stops (*)
  126. * 1 = keep power on after convert function stops
  127. * STP: 1 = abort current conversion and power down, set to "0" automatically
  128. */
  129. #define BU21029_AUTOSCAN 0x80
  130. /*
  131. * The timeout value needs to be larger than INTVL_TIME + tConv4 (sample and
  132. * conversion time), where tConv4 is calculated by formula:
  133. * tPON + tDLY1 + (tTIME_ST_ADC + (tADC * tSMPL) * 2 + tDLY2) * 3
  134. * see figure 8 in datasheet p15 for details of each field.
  135. */
  136. #define PEN_UP_TIMEOUT_MS 50
  137. #define STOP_DELAY_MIN_US 50
  138. #define STOP_DELAY_MAX_US 1000
  139. #define START_DELAY_MS 2
  140. #define BUF_LEN 8
  141. #define SCALE_12BIT (1 << 12)
  142. #define MAX_12BIT ((1 << 12) - 1)
  143. #define DRIVER_NAME "bu21029"
  144. struct bu21029_ts_data {
  145. struct i2c_client *client;
  146. struct input_dev *in_dev;
  147. struct timer_list timer;
  148. struct regulator *vdd;
  149. struct gpio_desc *reset_gpios;
  150. u32 x_plate_ohms;
  151. struct touchscreen_properties prop;
  152. };
  153. static void bu21029_touch_report(struct bu21029_ts_data *bu21029, const u8 *buf)
  154. {
  155. u16 x, y, z1, z2;
  156. u32 rz;
  157. s32 max_pressure = input_abs_get_max(bu21029->in_dev, ABS_PRESSURE);
  158. /*
  159. * compose upper 8 and lower 4 bits into a 12bit value:
  160. * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  161. * | ByteH | ByteL |
  162. * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  163. * |b07|b06|b05|b04|b03|b02|b01|b00|b07|b06|b05|b04|b03|b02|b01|b00|
  164. * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  165. * |v11|v10|v09|v08|v07|v06|v05|v04|v03|v02|v01|v00| 0 | 0 | 0 | 0 |
  166. * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  167. */
  168. x = (buf[0] << 4) | (buf[1] >> 4);
  169. y = (buf[2] << 4) | (buf[3] >> 4);
  170. z1 = (buf[4] << 4) | (buf[5] >> 4);
  171. z2 = (buf[6] << 4) | (buf[7] >> 4);
  172. if (z1 && z2) {
  173. /*
  174. * calculate Rz (pressure resistance value) by equation:
  175. * Rz = Rx * (x/Q) * ((z2/z1) - 1), where
  176. * Rx is x-plate resistance,
  177. * Q is the touch screen resolution (8bit = 256, 12bit = 4096)
  178. * x, z1, z2 are the measured positions.
  179. */
  180. rz = z2 - z1;
  181. rz *= x;
  182. rz *= bu21029->x_plate_ohms;
  183. rz /= z1;
  184. rz = DIV_ROUND_CLOSEST(rz, SCALE_12BIT);
  185. if (rz <= max_pressure) {
  186. touchscreen_report_pos(bu21029->in_dev, &bu21029->prop,
  187. x, y, false);
  188. input_report_abs(bu21029->in_dev, ABS_PRESSURE,
  189. max_pressure - rz);
  190. input_report_key(bu21029->in_dev, BTN_TOUCH, 1);
  191. input_sync(bu21029->in_dev);
  192. }
  193. }
  194. }
  195. static void bu21029_touch_release(struct timer_list *t)
  196. {
  197. struct bu21029_ts_data *bu21029 = from_timer(bu21029, t, timer);
  198. input_report_abs(bu21029->in_dev, ABS_PRESSURE, 0);
  199. input_report_key(bu21029->in_dev, BTN_TOUCH, 0);
  200. input_sync(bu21029->in_dev);
  201. }
  202. static irqreturn_t bu21029_touch_soft_irq(int irq, void *data)
  203. {
  204. struct bu21029_ts_data *bu21029 = data;
  205. u8 buf[BUF_LEN];
  206. int error;
  207. /*
  208. * Read touch data and deassert interrupt (will assert again after
  209. * INTVL_TIME + tConv4 for continuous touch)
  210. */
  211. error = i2c_smbus_read_i2c_block_data(bu21029->client, BU21029_AUTOSCAN,
  212. sizeof(buf), buf);
  213. if (error < 0)
  214. goto out;
  215. bu21029_touch_report(bu21029, buf);
  216. /* reset timer for pen up detection */
  217. mod_timer(&bu21029->timer,
  218. jiffies + msecs_to_jiffies(PEN_UP_TIMEOUT_MS));
  219. out:
  220. return IRQ_HANDLED;
  221. }
  222. static void bu21029_put_chip_in_reset(struct bu21029_ts_data *bu21029)
  223. {
  224. if (bu21029->reset_gpios) {
  225. gpiod_set_value_cansleep(bu21029->reset_gpios, 1);
  226. usleep_range(STOP_DELAY_MIN_US, STOP_DELAY_MAX_US);
  227. }
  228. }
  229. static int bu21029_start_chip(struct input_dev *dev)
  230. {
  231. struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
  232. struct i2c_client *i2c = bu21029->client;
  233. struct {
  234. u8 reg;
  235. u8 value;
  236. } init_table[] = {
  237. {BU21029_CFR0_REG, CFR0_VALUE},
  238. {BU21029_CFR1_REG, CFR1_VALUE},
  239. {BU21029_CFR2_REG, CFR2_VALUE},
  240. {BU21029_CFR3_REG, CFR3_VALUE},
  241. {BU21029_LDO_REG, LDO_VALUE}
  242. };
  243. int error, i;
  244. __be16 hwid;
  245. error = regulator_enable(bu21029->vdd);
  246. if (error) {
  247. dev_err(&i2c->dev, "failed to power up chip: %d", error);
  248. return error;
  249. }
  250. /* take chip out of reset */
  251. if (bu21029->reset_gpios) {
  252. gpiod_set_value_cansleep(bu21029->reset_gpios, 0);
  253. msleep(START_DELAY_MS);
  254. }
  255. error = i2c_smbus_read_i2c_block_data(i2c, BU21029_HWID_REG,
  256. sizeof(hwid), (u8 *)&hwid);
  257. if (error < 0) {
  258. dev_err(&i2c->dev, "failed to read HW ID\n");
  259. goto err_out;
  260. }
  261. if (be16_to_cpu(hwid) != SUPPORTED_HWID) {
  262. dev_err(&i2c->dev,
  263. "unsupported HW ID 0x%x\n", be16_to_cpu(hwid));
  264. error = -ENODEV;
  265. goto err_out;
  266. }
  267. for (i = 0; i < ARRAY_SIZE(init_table); ++i) {
  268. error = i2c_smbus_write_byte_data(i2c,
  269. init_table[i].reg,
  270. init_table[i].value);
  271. if (error < 0) {
  272. dev_err(&i2c->dev,
  273. "failed to write %#02x to register %#02x: %d\n",
  274. init_table[i].value, init_table[i].reg,
  275. error);
  276. goto err_out;
  277. }
  278. }
  279. error = i2c_smbus_write_byte(i2c, BU21029_AUTOSCAN);
  280. if (error < 0) {
  281. dev_err(&i2c->dev, "failed to start autoscan\n");
  282. goto err_out;
  283. }
  284. enable_irq(bu21029->client->irq);
  285. return 0;
  286. err_out:
  287. bu21029_put_chip_in_reset(bu21029);
  288. regulator_disable(bu21029->vdd);
  289. return error;
  290. }
  291. static void bu21029_stop_chip(struct input_dev *dev)
  292. {
  293. struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
  294. disable_irq(bu21029->client->irq);
  295. del_timer_sync(&bu21029->timer);
  296. bu21029_put_chip_in_reset(bu21029);
  297. regulator_disable(bu21029->vdd);
  298. }
  299. static int bu21029_probe(struct i2c_client *client,
  300. const struct i2c_device_id *id)
  301. {
  302. struct bu21029_ts_data *bu21029;
  303. struct input_dev *in_dev;
  304. int error;
  305. if (!i2c_check_functionality(client->adapter,
  306. I2C_FUNC_SMBUS_WRITE_BYTE |
  307. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  308. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  309. dev_err(&client->dev,
  310. "i2c functionality support is not sufficient\n");
  311. return -EIO;
  312. }
  313. bu21029 = devm_kzalloc(&client->dev, sizeof(*bu21029), GFP_KERNEL);
  314. if (!bu21029)
  315. return -ENOMEM;
  316. error = device_property_read_u32(&client->dev, "rohm,x-plate-ohms",
  317. &bu21029->x_plate_ohms);
  318. if (error) {
  319. dev_err(&client->dev,
  320. "invalid 'x-plate-ohms' supplied: %d\n", error);
  321. return error;
  322. }
  323. bu21029->vdd = devm_regulator_get(&client->dev, "vdd");
  324. if (IS_ERR(bu21029->vdd)) {
  325. error = PTR_ERR(bu21029->vdd);
  326. if (error != -EPROBE_DEFER)
  327. dev_err(&client->dev,
  328. "failed to acquire 'vdd' supply: %d\n", error);
  329. return error;
  330. }
  331. bu21029->reset_gpios = devm_gpiod_get_optional(&client->dev,
  332. "reset", GPIOD_OUT_HIGH);
  333. if (IS_ERR(bu21029->reset_gpios)) {
  334. error = PTR_ERR(bu21029->reset_gpios);
  335. if (error != -EPROBE_DEFER)
  336. dev_err(&client->dev,
  337. "failed to acquire 'reset' gpio: %d\n", error);
  338. return error;
  339. }
  340. in_dev = devm_input_allocate_device(&client->dev);
  341. if (!in_dev) {
  342. dev_err(&client->dev, "unable to allocate input device\n");
  343. return -ENOMEM;
  344. }
  345. bu21029->client = client;
  346. bu21029->in_dev = in_dev;
  347. timer_setup(&bu21029->timer, bu21029_touch_release, 0);
  348. in_dev->name = DRIVER_NAME;
  349. in_dev->id.bustype = BUS_I2C;
  350. in_dev->open = bu21029_start_chip;
  351. in_dev->close = bu21029_stop_chip;
  352. input_set_capability(in_dev, EV_KEY, BTN_TOUCH);
  353. input_set_abs_params(in_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  354. input_set_abs_params(in_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  355. input_set_abs_params(in_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  356. touchscreen_parse_properties(in_dev, false, &bu21029->prop);
  357. input_set_drvdata(in_dev, bu21029);
  358. irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
  359. error = devm_request_threaded_irq(&client->dev, client->irq,
  360. NULL, bu21029_touch_soft_irq,
  361. IRQF_ONESHOT, DRIVER_NAME, bu21029);
  362. if (error) {
  363. dev_err(&client->dev,
  364. "unable to request touch irq: %d\n", error);
  365. return error;
  366. }
  367. error = input_register_device(in_dev);
  368. if (error) {
  369. dev_err(&client->dev,
  370. "unable to register input device: %d\n", error);
  371. return error;
  372. }
  373. i2c_set_clientdata(client, bu21029);
  374. return 0;
  375. }
  376. static int __maybe_unused bu21029_suspend(struct device *dev)
  377. {
  378. struct i2c_client *i2c = to_i2c_client(dev);
  379. struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
  380. if (!device_may_wakeup(dev)) {
  381. mutex_lock(&bu21029->in_dev->mutex);
  382. if (bu21029->in_dev->users)
  383. bu21029_stop_chip(bu21029->in_dev);
  384. mutex_unlock(&bu21029->in_dev->mutex);
  385. }
  386. return 0;
  387. }
  388. static int __maybe_unused bu21029_resume(struct device *dev)
  389. {
  390. struct i2c_client *i2c = to_i2c_client(dev);
  391. struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
  392. if (!device_may_wakeup(dev)) {
  393. mutex_lock(&bu21029->in_dev->mutex);
  394. if (bu21029->in_dev->users)
  395. bu21029_start_chip(bu21029->in_dev);
  396. mutex_unlock(&bu21029->in_dev->mutex);
  397. }
  398. return 0;
  399. }
  400. static SIMPLE_DEV_PM_OPS(bu21029_pm_ops, bu21029_suspend, bu21029_resume);
  401. static const struct i2c_device_id bu21029_ids[] = {
  402. { DRIVER_NAME, 0 },
  403. { /* sentinel */ }
  404. };
  405. MODULE_DEVICE_TABLE(i2c, bu21029_ids);
  406. #ifdef CONFIG_OF
  407. static const struct of_device_id bu21029_of_ids[] = {
  408. { .compatible = "rohm,bu21029" },
  409. { /* sentinel */ }
  410. };
  411. MODULE_DEVICE_TABLE(of, bu21029_of_ids);
  412. #endif
  413. static struct i2c_driver bu21029_driver = {
  414. .driver = {
  415. .name = DRIVER_NAME,
  416. .of_match_table = of_match_ptr(bu21029_of_ids),
  417. .pm = &bu21029_pm_ops,
  418. },
  419. .id_table = bu21029_ids,
  420. .probe = bu21029_probe,
  421. };
  422. module_i2c_driver(bu21029_driver);
  423. MODULE_AUTHOR("Zhu Yi <yi.zhu5@cn.bosch.com>");
  424. MODULE_DESCRIPTION("Rohm BU21029 touchscreen controller driver");
  425. MODULE_LICENSE("GPL v2");