himax_hx852x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Himax HX852x(ES) Touchscreen Driver
  4. * Copyright (c) 2020-2024 Stephan Gerhold <stephan@gerhold.net>
  5. * Copyright (c) 2020 Jonathan Albrieux <jonathan.albrieux@gmail.com>
  6. *
  7. * Based on the Himax Android Driver Sample Code Ver 0.3 for HMX852xES chipset:
  8. * Copyright (c) 2014 Himax Corporation.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/i2c.h>
  13. #include <linux/input.h>
  14. #include <linux/input/mt.h>
  15. #include <linux/input/touchscreen.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/unaligned.h>
  23. #define HX852X_COORD_SIZE(fingers) ((fingers) * sizeof(struct hx852x_coord))
  24. #define HX852X_WIDTH_SIZE(fingers) ALIGN(fingers, 4)
  25. #define HX852X_BUF_SIZE(fingers) (HX852X_COORD_SIZE(fingers) + \
  26. HX852X_WIDTH_SIZE(fingers) + \
  27. sizeof(struct hx852x_touch_info))
  28. #define HX852X_MAX_FINGERS 12
  29. #define HX852X_MAX_KEY_COUNT 4
  30. #define HX852X_MAX_BUF_SIZE HX852X_BUF_SIZE(HX852X_MAX_FINGERS)
  31. #define HX852X_TS_SLEEP_IN 0x80
  32. #define HX852X_TS_SLEEP_OUT 0x81
  33. #define HX852X_TS_SENSE_OFF 0x82
  34. #define HX852X_TS_SENSE_ON 0x83
  35. #define HX852X_READ_ONE_EVENT 0x85
  36. #define HX852X_READ_ALL_EVENTS 0x86
  37. #define HX852X_READ_LATEST_EVENT 0x87
  38. #define HX852X_CLEAR_EVENT_STACK 0x88
  39. #define HX852X_REG_SRAM_SWITCH 0x8c
  40. #define HX852X_REG_SRAM_ADDR 0x8b
  41. #define HX852X_REG_FLASH_RPLACE 0x5a
  42. #define HX852X_SRAM_SWITCH_TEST_MODE 0x14
  43. #define HX852X_SRAM_ADDR_CONFIG 0x7000
  44. struct hx852x {
  45. struct i2c_client *client;
  46. struct input_dev *input_dev;
  47. struct touchscreen_properties props;
  48. struct gpio_desc *reset_gpiod;
  49. struct regulator_bulk_data supplies[2];
  50. unsigned int max_fingers;
  51. unsigned int keycount;
  52. unsigned int keycodes[HX852X_MAX_KEY_COUNT];
  53. };
  54. struct hx852x_config {
  55. u8 rx_num;
  56. u8 tx_num;
  57. u8 max_pt;
  58. u8 padding1[3];
  59. __be16 x_res;
  60. __be16 y_res;
  61. u8 padding2[2];
  62. } __packed __aligned(4);
  63. struct hx852x_coord {
  64. __be16 x;
  65. __be16 y;
  66. } __packed __aligned(4);
  67. struct hx852x_touch_info {
  68. u8 finger_num;
  69. __le16 finger_pressed;
  70. u8 padding;
  71. } __packed __aligned(4);
  72. static int hx852x_i2c_read(struct hx852x *hx, u8 cmd, void *data, u16 len)
  73. {
  74. struct i2c_client *client = hx->client;
  75. int error;
  76. int ret;
  77. struct i2c_msg msg[] = {
  78. {
  79. .addr = client->addr,
  80. .flags = 0,
  81. .len = 1,
  82. .buf = &cmd,
  83. },
  84. {
  85. .addr = client->addr,
  86. .flags = I2C_M_RD,
  87. .len = len,
  88. .buf = data,
  89. },
  90. };
  91. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  92. if (ret != ARRAY_SIZE(msg)) {
  93. error = ret < 0 ? ret : -EIO;
  94. dev_err(&client->dev, "failed to read %#x: %d\n", cmd, error);
  95. return error;
  96. }
  97. return 0;
  98. }
  99. static int hx852x_power_on(struct hx852x *hx)
  100. {
  101. struct device *dev = &hx->client->dev;
  102. int error;
  103. error = regulator_bulk_enable(ARRAY_SIZE(hx->supplies), hx->supplies);
  104. if (error) {
  105. dev_err(dev, "failed to enable regulators: %d\n", error);
  106. return error;
  107. }
  108. gpiod_set_value_cansleep(hx->reset_gpiod, 1);
  109. msleep(20);
  110. gpiod_set_value_cansleep(hx->reset_gpiod, 0);
  111. msleep(50);
  112. return 0;
  113. }
  114. static int hx852x_start(struct hx852x *hx)
  115. {
  116. struct device *dev = &hx->client->dev;
  117. int error;
  118. error = i2c_smbus_write_byte(hx->client, HX852X_TS_SLEEP_OUT);
  119. if (error) {
  120. dev_err(dev, "failed to send TS_SLEEP_OUT: %d\n", error);
  121. return error;
  122. }
  123. msleep(30);
  124. error = i2c_smbus_write_byte(hx->client, HX852X_TS_SENSE_ON);
  125. if (error) {
  126. dev_err(dev, "failed to send TS_SENSE_ON: %d\n", error);
  127. return error;
  128. }
  129. msleep(20);
  130. return 0;
  131. }
  132. static int hx852x_stop(struct hx852x *hx)
  133. {
  134. struct device *dev = &hx->client->dev;
  135. int error;
  136. error = i2c_smbus_write_byte(hx->client, HX852X_TS_SENSE_OFF);
  137. if (error) {
  138. dev_err(dev, "failed to send TS_SENSE_OFF: %d\n", error);
  139. return error;
  140. }
  141. msleep(20);
  142. error = i2c_smbus_write_byte(hx->client, HX852X_TS_SLEEP_IN);
  143. if (error) {
  144. dev_err(dev, "failed to send TS_SLEEP_IN: %d\n", error);
  145. return error;
  146. }
  147. msleep(30);
  148. return 0;
  149. }
  150. static int hx852x_power_off(struct hx852x *hx)
  151. {
  152. struct device *dev = &hx->client->dev;
  153. int error;
  154. error = regulator_bulk_disable(ARRAY_SIZE(hx->supplies), hx->supplies);
  155. if (error) {
  156. dev_err(dev, "failed to disable regulators: %d\n", error);
  157. return error;
  158. }
  159. return 0;
  160. }
  161. static int hx852x_read_config(struct hx852x *hx)
  162. {
  163. struct device *dev = &hx->client->dev;
  164. struct hx852x_config conf;
  165. int x_res, y_res;
  166. int error, error2;
  167. error = hx852x_power_on(hx);
  168. if (error)
  169. return error;
  170. /* Sensing must be turned on briefly to load the config */
  171. error = hx852x_start(hx);
  172. if (error)
  173. goto err_power_off;
  174. error = hx852x_stop(hx);
  175. if (error)
  176. goto err_power_off;
  177. error = i2c_smbus_write_byte_data(hx->client, HX852X_REG_SRAM_SWITCH,
  178. HX852X_SRAM_SWITCH_TEST_MODE);
  179. if (error)
  180. goto err_power_off;
  181. error = i2c_smbus_write_word_data(hx->client, HX852X_REG_SRAM_ADDR,
  182. HX852X_SRAM_ADDR_CONFIG);
  183. if (error)
  184. goto err_test_mode;
  185. error = hx852x_i2c_read(hx, HX852X_REG_FLASH_RPLACE, &conf, sizeof(conf));
  186. if (error)
  187. goto err_test_mode;
  188. x_res = be16_to_cpu(conf.x_res);
  189. y_res = be16_to_cpu(conf.y_res);
  190. hx->max_fingers = (conf.max_pt & 0xf0) >> 4;
  191. dev_dbg(dev, "x res: %u, y res: %u, max fingers: %u\n",
  192. x_res, y_res, hx->max_fingers);
  193. if (hx->max_fingers > HX852X_MAX_FINGERS) {
  194. dev_err(dev, "max supported fingers: %u, found: %u\n",
  195. HX852X_MAX_FINGERS, hx->max_fingers);
  196. error = -EINVAL;
  197. goto err_test_mode;
  198. }
  199. if (x_res && y_res) {
  200. input_set_abs_params(hx->input_dev, ABS_MT_POSITION_X, 0, x_res - 1, 0, 0);
  201. input_set_abs_params(hx->input_dev, ABS_MT_POSITION_Y, 0, y_res - 1, 0, 0);
  202. }
  203. err_test_mode:
  204. error2 = i2c_smbus_write_byte_data(hx->client, HX852X_REG_SRAM_SWITCH, 0);
  205. error = error ?: error2;
  206. err_power_off:
  207. error2 = hx852x_power_off(hx);
  208. return error ?: error2;
  209. }
  210. static int hx852x_handle_events(struct hx852x *hx)
  211. {
  212. /*
  213. * The event packets have variable size, depending on the amount of
  214. * supported fingers (hx->max_fingers). They are laid out as follows:
  215. * - struct hx852x_coord[hx->max_fingers]: Coordinates for each finger
  216. * - u8[ALIGN(hx->max_fingers, 4)]: Touch width for each finger
  217. * with padding for 32-bit alignment
  218. * - struct hx852x_touch_info
  219. *
  220. * Load everything into a 32-bit aligned buffer so the coordinates
  221. * can be assigned directly, without using get_unaligned_*().
  222. */
  223. u8 buf[HX852X_MAX_BUF_SIZE] __aligned(4);
  224. struct hx852x_coord *coord = (struct hx852x_coord *)buf;
  225. u8 *width = &buf[HX852X_COORD_SIZE(hx->max_fingers)];
  226. struct hx852x_touch_info *info = (struct hx852x_touch_info *)
  227. &width[HX852X_WIDTH_SIZE(hx->max_fingers)];
  228. unsigned long finger_pressed, key_pressed;
  229. unsigned int i, x, y, w;
  230. int error;
  231. error = hx852x_i2c_read(hx, HX852X_READ_ALL_EVENTS, buf,
  232. HX852X_BUF_SIZE(hx->max_fingers));
  233. if (error)
  234. return error;
  235. finger_pressed = get_unaligned_le16(&info->finger_pressed);
  236. key_pressed = finger_pressed >> HX852X_MAX_FINGERS;
  237. /* All bits are set when no touch is detected */
  238. if (info->finger_num == 0xff || !(info->finger_num & 0x0f))
  239. finger_pressed = 0;
  240. if (key_pressed == 0xf)
  241. key_pressed = 0;
  242. for_each_set_bit(i, &finger_pressed, hx->max_fingers) {
  243. x = be16_to_cpu(coord[i].x);
  244. y = be16_to_cpu(coord[i].y);
  245. w = width[i];
  246. input_mt_slot(hx->input_dev, i);
  247. input_mt_report_slot_state(hx->input_dev, MT_TOOL_FINGER, 1);
  248. touchscreen_report_pos(hx->input_dev, &hx->props, x, y, true);
  249. input_report_abs(hx->input_dev, ABS_MT_TOUCH_MAJOR, w);
  250. }
  251. input_mt_sync_frame(hx->input_dev);
  252. for (i = 0; i < hx->keycount; i++)
  253. input_report_key(hx->input_dev, hx->keycodes[i], key_pressed & BIT(i));
  254. input_sync(hx->input_dev);
  255. return 0;
  256. }
  257. static irqreturn_t hx852x_interrupt(int irq, void *ptr)
  258. {
  259. struct hx852x *hx = ptr;
  260. int error;
  261. error = hx852x_handle_events(hx);
  262. if (error) {
  263. dev_err_ratelimited(&hx->client->dev,
  264. "failed to handle events: %d\n", error);
  265. return IRQ_NONE;
  266. }
  267. return IRQ_HANDLED;
  268. }
  269. static int hx852x_input_open(struct input_dev *dev)
  270. {
  271. struct hx852x *hx = input_get_drvdata(dev);
  272. int error;
  273. error = hx852x_power_on(hx);
  274. if (error)
  275. return error;
  276. error = hx852x_start(hx);
  277. if (error) {
  278. hx852x_power_off(hx);
  279. return error;
  280. }
  281. enable_irq(hx->client->irq);
  282. return 0;
  283. }
  284. static void hx852x_input_close(struct input_dev *dev)
  285. {
  286. struct hx852x *hx = input_get_drvdata(dev);
  287. hx852x_stop(hx);
  288. disable_irq(hx->client->irq);
  289. hx852x_power_off(hx);
  290. }
  291. static int hx852x_parse_properties(struct hx852x *hx)
  292. {
  293. struct device *dev = &hx->client->dev;
  294. int error, count;
  295. count = device_property_count_u32(dev, "linux,keycodes");
  296. if (count == -EINVAL) {
  297. /* Property does not exist, keycodes are optional */
  298. return 0;
  299. } else if (count < 0) {
  300. dev_err(dev, "Failed to read linux,keycodes: %d\n", count);
  301. return count;
  302. } else if (count > HX852X_MAX_KEY_COUNT) {
  303. dev_err(dev, "max supported keys: %u, found: %u\n",
  304. HX852X_MAX_KEY_COUNT, hx->keycount);
  305. return -EINVAL;
  306. }
  307. hx->keycount = count;
  308. error = device_property_read_u32_array(dev, "linux,keycodes",
  309. hx->keycodes, hx->keycount);
  310. if (error) {
  311. dev_err(dev, "failed to read linux,keycodes: %d\n", error);
  312. return error;
  313. }
  314. return 0;
  315. }
  316. static int hx852x_probe(struct i2c_client *client)
  317. {
  318. struct device *dev = &client->dev;
  319. struct hx852x *hx;
  320. int error, i;
  321. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  322. I2C_FUNC_SMBUS_WRITE_BYTE |
  323. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  324. I2C_FUNC_SMBUS_WRITE_WORD_DATA)) {
  325. dev_err(dev, "not all required i2c functionality supported\n");
  326. return -ENXIO;
  327. }
  328. hx = devm_kzalloc(dev, sizeof(*hx), GFP_KERNEL);
  329. if (!hx)
  330. return -ENOMEM;
  331. hx->client = client;
  332. hx->input_dev = devm_input_allocate_device(dev);
  333. if (!hx->input_dev)
  334. return -ENOMEM;
  335. hx->input_dev->name = "Himax HX852x";
  336. hx->input_dev->id.bustype = BUS_I2C;
  337. hx->input_dev->open = hx852x_input_open;
  338. hx->input_dev->close = hx852x_input_close;
  339. i2c_set_clientdata(client, hx);
  340. input_set_drvdata(hx->input_dev, hx);
  341. hx->supplies[0].supply = "vcca";
  342. hx->supplies[1].supply = "vccd";
  343. error = devm_regulator_bulk_get(dev, ARRAY_SIZE(hx->supplies), hx->supplies);
  344. if (error)
  345. return dev_err_probe(dev, error, "failed to get regulators\n");
  346. hx->reset_gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  347. if (IS_ERR(hx->reset_gpiod))
  348. return dev_err_probe(dev, PTR_ERR(hx->reset_gpiod),
  349. "failed to get reset gpio\n");
  350. error = devm_request_threaded_irq(dev, client->irq, NULL, hx852x_interrupt,
  351. IRQF_ONESHOT | IRQF_NO_AUTOEN, NULL, hx);
  352. if (error)
  353. return dev_err_probe(dev, error, "failed to request irq %d", client->irq);
  354. error = hx852x_read_config(hx);
  355. if (error)
  356. return error;
  357. input_set_capability(hx->input_dev, EV_ABS, ABS_MT_POSITION_X);
  358. input_set_capability(hx->input_dev, EV_ABS, ABS_MT_POSITION_Y);
  359. input_set_abs_params(hx->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  360. touchscreen_parse_properties(hx->input_dev, true, &hx->props);
  361. error = hx852x_parse_properties(hx);
  362. if (error)
  363. return error;
  364. hx->input_dev->keycode = hx->keycodes;
  365. hx->input_dev->keycodemax = hx->keycount;
  366. hx->input_dev->keycodesize = sizeof(hx->keycodes[0]);
  367. for (i = 0; i < hx->keycount; i++)
  368. input_set_capability(hx->input_dev, EV_KEY, hx->keycodes[i]);
  369. error = input_mt_init_slots(hx->input_dev, hx->max_fingers,
  370. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  371. if (error)
  372. return dev_err_probe(dev, error, "failed to init MT slots\n");
  373. error = input_register_device(hx->input_dev);
  374. if (error)
  375. return dev_err_probe(dev, error, "failed to register input device\n");
  376. return 0;
  377. }
  378. static int hx852x_suspend(struct device *dev)
  379. {
  380. struct hx852x *hx = dev_get_drvdata(dev);
  381. guard(mutex)(&hx->input_dev->mutex);
  382. if (input_device_enabled(hx->input_dev))
  383. return hx852x_stop(hx);
  384. return 0;
  385. }
  386. static int hx852x_resume(struct device *dev)
  387. {
  388. struct hx852x *hx = dev_get_drvdata(dev);
  389. guard(mutex)(&hx->input_dev->mutex);
  390. if (input_device_enabled(hx->input_dev))
  391. return hx852x_start(hx);
  392. return 0;
  393. }
  394. static DEFINE_SIMPLE_DEV_PM_OPS(hx852x_pm_ops, hx852x_suspend, hx852x_resume);
  395. #ifdef CONFIG_OF
  396. static const struct of_device_id hx852x_of_match[] = {
  397. { .compatible = "himax,hx852es" },
  398. { }
  399. };
  400. MODULE_DEVICE_TABLE(of, hx852x_of_match);
  401. #endif
  402. static struct i2c_driver hx852x_driver = {
  403. .probe = hx852x_probe,
  404. .driver = {
  405. .name = "himax_hx852x",
  406. .pm = pm_sleep_ptr(&hx852x_pm_ops),
  407. .of_match_table = of_match_ptr(hx852x_of_match),
  408. },
  409. };
  410. module_i2c_driver(hx852x_driver);
  411. MODULE_DESCRIPTION("Himax HX852x(ES) Touchscreen Driver");
  412. MODULE_AUTHOR("Jonathan Albrieux <jonathan.albrieux@gmail.com>");
  413. MODULE_AUTHOR("Stephan Gerhold <stephan@gerhold.net>");
  414. MODULE_LICENSE("GPL");