hynitron_cstxxx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Hynitron cstxxx Touchscreen
  4. *
  5. * Copyright (c) 2022 Chris Morgan <macromorgan@hotmail.com>
  6. *
  7. * This code is based on hynitron_core.c authored by Hynitron.
  8. * Note that no datasheet was available, so much of these registers
  9. * are undocumented. This is essentially a cleaned-up version of the
  10. * vendor driver with support removed for hardware I cannot test and
  11. * device-specific functions replated with generic functions wherever
  12. * possible.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/i2c.h>
  18. #include <linux/input.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/input/touchscreen.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/module.h>
  23. #include <linux/property.h>
  24. #include <linux/unaligned.h>
  25. /* Per chip data */
  26. struct hynitron_ts_chip_data {
  27. unsigned int max_touch_num;
  28. u32 ic_chkcode;
  29. int (*firmware_info)(struct i2c_client *client);
  30. int (*bootloader_enter)(struct i2c_client *client);
  31. int (*init_input)(struct i2c_client *client);
  32. void (*report_touch)(struct i2c_client *client);
  33. };
  34. /* Data generic to all (supported and non-supported) controllers. */
  35. struct hynitron_ts_data {
  36. const struct hynitron_ts_chip_data *chip;
  37. struct i2c_client *client;
  38. struct input_dev *input_dev;
  39. struct touchscreen_properties prop;
  40. struct gpio_desc *reset_gpio;
  41. };
  42. /*
  43. * Since I have no datasheet, these values are guessed and/or assumed
  44. * based on observation and testing.
  45. */
  46. #define CST3XX_FIRMWARE_INFO_START_CMD 0x01d1
  47. #define CST3XX_FIRMWARE_INFO_END_CMD 0x09d1
  48. #define CST3XX_FIRMWARE_CHK_CODE_REG 0xfcd1
  49. #define CST3XX_FIRMWARE_VERSION_REG 0x08d2
  50. #define CST3XX_FIRMWARE_VER_INVALID_VAL 0xa5a5a5a5
  51. #define CST3XX_BOOTLDR_PROG_CMD 0xaa01a0
  52. #define CST3XX_BOOTLDR_PROG_CHK_REG 0x02a0
  53. #define CST3XX_BOOTLDR_CHK_VAL 0xac
  54. #define CST3XX_TOUCH_DATA_PART_REG 0x00d0
  55. #define CST3XX_TOUCH_DATA_FULL_REG 0x07d0
  56. #define CST3XX_TOUCH_DATA_CHK_VAL 0xab
  57. #define CST3XX_TOUCH_DATA_TOUCH_VAL 0x03
  58. #define CST3XX_TOUCH_DATA_STOP_CMD 0xab00d0
  59. #define CST3XX_TOUCH_COUNT_MASK GENMASK(6, 0)
  60. /*
  61. * Hard coded reset delay value of 20ms not IC dependent in
  62. * vendor driver.
  63. */
  64. static void hyn_reset_proc(struct i2c_client *client, int delay)
  65. {
  66. struct hynitron_ts_data *ts_data = i2c_get_clientdata(client);
  67. gpiod_set_value_cansleep(ts_data->reset_gpio, 1);
  68. msleep(20);
  69. gpiod_set_value_cansleep(ts_data->reset_gpio, 0);
  70. if (delay)
  71. fsleep(delay * 1000);
  72. }
  73. static irqreturn_t hyn_interrupt_handler(int irq, void *dev_id)
  74. {
  75. struct i2c_client *client = dev_id;
  76. struct hynitron_ts_data *ts_data = i2c_get_clientdata(client);
  77. ts_data->chip->report_touch(client);
  78. return IRQ_HANDLED;
  79. }
  80. /*
  81. * The vendor driver would retry twice before failing to read or write
  82. * to the i2c device.
  83. */
  84. static int cst3xx_i2c_write(struct i2c_client *client,
  85. unsigned char *buf, int len)
  86. {
  87. int ret;
  88. int retries = 0;
  89. while (retries < 2) {
  90. ret = i2c_master_send(client, buf, len);
  91. if (ret == len)
  92. return 0;
  93. if (ret <= 0)
  94. retries++;
  95. else
  96. break;
  97. }
  98. return ret < 0 ? ret : -EIO;
  99. }
  100. static int cst3xx_i2c_read_register(struct i2c_client *client, u16 reg,
  101. u8 *val, u16 len)
  102. {
  103. __le16 buf = cpu_to_le16(reg);
  104. struct i2c_msg msgs[] = {
  105. {
  106. .addr = client->addr,
  107. .flags = 0,
  108. .len = 2,
  109. .buf = (u8 *)&buf,
  110. },
  111. {
  112. .addr = client->addr,
  113. .flags = I2C_M_RD,
  114. .len = len,
  115. .buf = val,
  116. }
  117. };
  118. int err;
  119. int ret;
  120. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  121. if (ret == ARRAY_SIZE(msgs))
  122. return 0;
  123. err = ret < 0 ? ret : -EIO;
  124. dev_err(&client->dev, "Error reading %d bytes from 0x%04x: %d (%d)\n",
  125. len, reg, err, ret);
  126. return err;
  127. }
  128. static int cst3xx_firmware_info(struct i2c_client *client)
  129. {
  130. struct hynitron_ts_data *ts_data = i2c_get_clientdata(client);
  131. int err;
  132. u32 tmp;
  133. unsigned char buf[4];
  134. /*
  135. * Tests suggest this command needed to read firmware regs.
  136. */
  137. put_unaligned_le16(CST3XX_FIRMWARE_INFO_START_CMD, buf);
  138. err = cst3xx_i2c_write(client, buf, 2);
  139. if (err)
  140. return err;
  141. usleep_range(10000, 11000);
  142. /*
  143. * Read register for check-code to determine if device detected
  144. * correctly.
  145. */
  146. err = cst3xx_i2c_read_register(client, CST3XX_FIRMWARE_CHK_CODE_REG,
  147. buf, 4);
  148. if (err)
  149. return err;
  150. tmp = get_unaligned_le32(buf);
  151. if ((tmp & 0xffff0000) != ts_data->chip->ic_chkcode) {
  152. dev_err(&client->dev, "%s ic mismatch, chkcode is %u\n",
  153. __func__, tmp);
  154. return -ENODEV;
  155. }
  156. usleep_range(10000, 11000);
  157. /* Read firmware version and test if firmware missing. */
  158. err = cst3xx_i2c_read_register(client, CST3XX_FIRMWARE_VERSION_REG,
  159. buf, 4);
  160. if (err)
  161. return err;
  162. tmp = get_unaligned_le32(buf);
  163. if (tmp == CST3XX_FIRMWARE_VER_INVALID_VAL) {
  164. dev_err(&client->dev, "Device firmware missing\n");
  165. return -ENODEV;
  166. }
  167. /*
  168. * Tests suggest cmd required to exit reading firmware regs.
  169. */
  170. put_unaligned_le16(CST3XX_FIRMWARE_INFO_END_CMD, buf);
  171. err = cst3xx_i2c_write(client, buf, 2);
  172. if (err)
  173. return err;
  174. usleep_range(5000, 6000);
  175. return 0;
  176. }
  177. static int cst3xx_bootloader_enter(struct i2c_client *client)
  178. {
  179. int err;
  180. u8 retry;
  181. u32 tmp = 0;
  182. unsigned char buf[3];
  183. for (retry = 0; retry < 5; retry++) {
  184. hyn_reset_proc(client, (7 + retry));
  185. /* set cmd to enter program mode */
  186. put_unaligned_le24(CST3XX_BOOTLDR_PROG_CMD, buf);
  187. err = cst3xx_i2c_write(client, buf, 3);
  188. if (err)
  189. continue;
  190. usleep_range(2000, 2500);
  191. /* check whether in program mode */
  192. err = cst3xx_i2c_read_register(client,
  193. CST3XX_BOOTLDR_PROG_CHK_REG,
  194. buf, 1);
  195. if (err)
  196. continue;
  197. tmp = get_unaligned(buf);
  198. if (tmp == CST3XX_BOOTLDR_CHK_VAL)
  199. break;
  200. }
  201. if (tmp != CST3XX_BOOTLDR_CHK_VAL) {
  202. dev_err(&client->dev, "%s unable to enter bootloader mode\n",
  203. __func__);
  204. return -ENODEV;
  205. }
  206. hyn_reset_proc(client, 40);
  207. return 0;
  208. }
  209. static void cst3xx_report_contact(struct hynitron_ts_data *ts_data,
  210. u8 id, unsigned int x, unsigned int y, u8 w)
  211. {
  212. input_mt_slot(ts_data->input_dev, id);
  213. input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER, 1);
  214. touchscreen_report_pos(ts_data->input_dev, &ts_data->prop, x, y, true);
  215. input_report_abs(ts_data->input_dev, ABS_MT_TOUCH_MAJOR, w);
  216. }
  217. static int cst3xx_finish_touch_read(struct i2c_client *client)
  218. {
  219. unsigned char buf[3];
  220. int err;
  221. put_unaligned_le24(CST3XX_TOUCH_DATA_STOP_CMD, buf);
  222. err = cst3xx_i2c_write(client, buf, 3);
  223. if (err) {
  224. dev_err(&client->dev,
  225. "send read touch info ending failed: %d\n", err);
  226. return err;
  227. }
  228. return 0;
  229. }
  230. /*
  231. * Handle events from IRQ. Note that for cst3xx it appears that IRQ
  232. * fires continuously while touched, otherwise once every 1500ms
  233. * when not touched (assume touchscreen waking up periodically).
  234. * Note buffer is sized for 5 fingers, if more needed buffer must
  235. * be increased. The buffer contains 5 bytes for each touch point,
  236. * a touch count byte, a check byte, and then a second check byte after
  237. * all other touch points.
  238. *
  239. * For example 1 touch would look like this:
  240. * touch1[5]:touch_count[1]:chk_byte[1]
  241. *
  242. * 3 touches would look like this:
  243. * touch1[5]:touch_count[1]:chk_byte[1]:touch2[5]:touch3[5]:chk_byte[1]
  244. */
  245. static void cst3xx_touch_report(struct i2c_client *client)
  246. {
  247. struct hynitron_ts_data *ts_data = i2c_get_clientdata(client);
  248. u8 buf[28];
  249. u8 finger_id, sw, w;
  250. unsigned int x, y;
  251. unsigned int touch_cnt, end_byte;
  252. unsigned int idx = 0;
  253. unsigned int i;
  254. int err;
  255. /* Read and validate the first bits of input data. */
  256. err = cst3xx_i2c_read_register(client, CST3XX_TOUCH_DATA_PART_REG,
  257. buf, 28);
  258. if (err ||
  259. buf[6] != CST3XX_TOUCH_DATA_CHK_VAL ||
  260. buf[0] == CST3XX_TOUCH_DATA_CHK_VAL) {
  261. dev_err(&client->dev, "cst3xx touch read failure\n");
  262. return;
  263. }
  264. /* Report to the device we're done reading the touch data. */
  265. err = cst3xx_finish_touch_read(client);
  266. if (err)
  267. return;
  268. touch_cnt = buf[5] & CST3XX_TOUCH_COUNT_MASK;
  269. /*
  270. * Check the check bit of the last touch slot. The check bit is
  271. * always present after touch point 1 for valid data, and then
  272. * appears as the last byte after all other touch data.
  273. */
  274. if (touch_cnt > 1) {
  275. end_byte = touch_cnt * 5 + 2;
  276. if (buf[end_byte] != CST3XX_TOUCH_DATA_CHK_VAL) {
  277. dev_err(&client->dev, "cst3xx touch read failure\n");
  278. return;
  279. }
  280. }
  281. /* Parse through the buffer to capture touch data. */
  282. for (i = 0; i < touch_cnt; i++) {
  283. x = ((buf[idx + 1] << 4) | ((buf[idx + 3] >> 4) & 0x0f));
  284. y = ((buf[idx + 2] << 4) | (buf[idx + 3] & 0x0f));
  285. w = (buf[idx + 4] >> 3);
  286. sw = (buf[idx] & 0x0f) >> 1;
  287. finger_id = (buf[idx] >> 4) & 0x0f;
  288. /* Sanity check we don't have more fingers than we expect */
  289. if (ts_data->chip->max_touch_num < finger_id) {
  290. dev_err(&client->dev, "cst3xx touch read failure\n");
  291. break;
  292. }
  293. /* sw value of 0 means no touch, 0x03 means touch */
  294. if (sw == CST3XX_TOUCH_DATA_TOUCH_VAL)
  295. cst3xx_report_contact(ts_data, finger_id, x, y, w);
  296. idx += 5;
  297. /* Skip the 2 bytes between point 1 and point 2 */
  298. if (i == 0)
  299. idx += 2;
  300. }
  301. input_mt_sync_frame(ts_data->input_dev);
  302. input_sync(ts_data->input_dev);
  303. }
  304. static int cst3xx_input_dev_int(struct i2c_client *client)
  305. {
  306. struct hynitron_ts_data *ts_data = i2c_get_clientdata(client);
  307. int err;
  308. ts_data->input_dev = devm_input_allocate_device(&client->dev);
  309. if (!ts_data->input_dev) {
  310. dev_err(&client->dev, "Failed to allocate input device\n");
  311. return -ENOMEM;
  312. }
  313. ts_data->input_dev->name = "Hynitron cst3xx Touchscreen";
  314. ts_data->input_dev->phys = "input/ts";
  315. ts_data->input_dev->id.bustype = BUS_I2C;
  316. input_set_drvdata(ts_data->input_dev, ts_data);
  317. input_set_capability(ts_data->input_dev, EV_ABS, ABS_MT_POSITION_X);
  318. input_set_capability(ts_data->input_dev, EV_ABS, ABS_MT_POSITION_Y);
  319. input_set_abs_params(ts_data->input_dev, ABS_MT_TOUCH_MAJOR,
  320. 0, 255, 0, 0);
  321. touchscreen_parse_properties(ts_data->input_dev, true, &ts_data->prop);
  322. if (!ts_data->prop.max_x || !ts_data->prop.max_y) {
  323. dev_err(&client->dev,
  324. "Invalid x/y (%d, %d), using defaults\n",
  325. ts_data->prop.max_x, ts_data->prop.max_y);
  326. ts_data->prop.max_x = 1152;
  327. ts_data->prop.max_y = 1920;
  328. input_abs_set_max(ts_data->input_dev,
  329. ABS_MT_POSITION_X, ts_data->prop.max_x);
  330. input_abs_set_max(ts_data->input_dev,
  331. ABS_MT_POSITION_Y, ts_data->prop.max_y);
  332. }
  333. err = input_mt_init_slots(ts_data->input_dev,
  334. ts_data->chip->max_touch_num,
  335. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  336. if (err) {
  337. dev_err(&client->dev,
  338. "Failed to initialize input slots: %d\n", err);
  339. return err;
  340. }
  341. err = input_register_device(ts_data->input_dev);
  342. if (err) {
  343. dev_err(&client->dev,
  344. "Input device registration failed: %d\n", err);
  345. return err;
  346. }
  347. return 0;
  348. }
  349. static int hyn_probe(struct i2c_client *client)
  350. {
  351. struct hynitron_ts_data *ts_data;
  352. int err;
  353. ts_data = devm_kzalloc(&client->dev, sizeof(*ts_data), GFP_KERNEL);
  354. if (!ts_data)
  355. return -ENOMEM;
  356. ts_data->client = client;
  357. i2c_set_clientdata(client, ts_data);
  358. ts_data->chip = device_get_match_data(&client->dev);
  359. if (!ts_data->chip)
  360. return -EINVAL;
  361. ts_data->reset_gpio = devm_gpiod_get(&client->dev,
  362. "reset", GPIOD_OUT_LOW);
  363. err = PTR_ERR_OR_ZERO(ts_data->reset_gpio);
  364. if (err) {
  365. dev_err(&client->dev, "request reset gpio failed: %d\n", err);
  366. return err;
  367. }
  368. hyn_reset_proc(client, 60);
  369. err = ts_data->chip->bootloader_enter(client);
  370. if (err < 0)
  371. return err;
  372. err = ts_data->chip->init_input(client);
  373. if (err < 0)
  374. return err;
  375. err = ts_data->chip->firmware_info(client);
  376. if (err < 0)
  377. return err;
  378. err = devm_request_threaded_irq(&client->dev, client->irq,
  379. NULL, hyn_interrupt_handler,
  380. IRQF_ONESHOT,
  381. "Hynitron Touch Int", client);
  382. if (err) {
  383. dev_err(&client->dev, "failed to request IRQ: %d\n", err);
  384. return err;
  385. }
  386. return 0;
  387. }
  388. static const struct hynitron_ts_chip_data cst3xx_data = {
  389. .max_touch_num = 5,
  390. .ic_chkcode = 0xcaca0000,
  391. .firmware_info = &cst3xx_firmware_info,
  392. .bootloader_enter = &cst3xx_bootloader_enter,
  393. .init_input = &cst3xx_input_dev_int,
  394. .report_touch = &cst3xx_touch_report,
  395. };
  396. static const struct i2c_device_id hyn_tpd_id[] = {
  397. { .name = "hynitron_ts" },
  398. { /* sentinel */ },
  399. };
  400. MODULE_DEVICE_TABLE(i2c, hyn_tpd_id);
  401. static const struct of_device_id hyn_dt_match[] = {
  402. { .compatible = "hynitron,cst340", .data = &cst3xx_data },
  403. { /* sentinel */ },
  404. };
  405. MODULE_DEVICE_TABLE(of, hyn_dt_match);
  406. static struct i2c_driver hynitron_i2c_driver = {
  407. .driver = {
  408. .name = "Hynitron-TS",
  409. .of_match_table = hyn_dt_match,
  410. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  411. },
  412. .id_table = hyn_tpd_id,
  413. .probe = hyn_probe,
  414. };
  415. module_i2c_driver(hynitron_i2c_driver);
  416. MODULE_AUTHOR("Chris Morgan");
  417. MODULE_DESCRIPTION("Hynitron Touchscreen Driver");
  418. MODULE_LICENSE("GPL");