hynitron-cst816x.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for I2C connected Hynitron CST816x Series Touchscreen
  4. *
  5. * Copyright (C) 2025 Oleh Kuzhylnyi <kuzhylol@gmail.com>
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/i2c.h>
  12. #include <linux/input.h>
  13. #include <linux/unaligned.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #define CST816X_RD_REG 0x01
  17. #define CST816X_NUM_KEYS 5
  18. struct cst816x_touch {
  19. u8 gest;
  20. u8 active;
  21. u16 abs_x;
  22. u16 abs_y;
  23. } __packed;
  24. struct cst816x_priv {
  25. struct i2c_client *client;
  26. struct gpio_desc *reset;
  27. struct input_dev *input;
  28. unsigned int keycode[CST816X_NUM_KEYS];
  29. unsigned int keycodemax;
  30. };
  31. static int cst816x_parse_keycodes(struct device *dev, struct cst816x_priv *priv)
  32. {
  33. int count;
  34. int error;
  35. if (device_property_present(dev, "linux,keycodes")) {
  36. count = device_property_count_u32(dev, "linux,keycodes");
  37. if (count < 0) {
  38. error = count;
  39. dev_err(dev, "failed to count keys: %d\n", error);
  40. return error;
  41. } else if (count > ARRAY_SIZE(priv->keycode)) {
  42. dev_err(dev, "too many keys defined: %d\n", count);
  43. return -EINVAL;
  44. }
  45. priv->keycodemax = count;
  46. error = device_property_read_u32_array(dev, "linux,keycodes",
  47. priv->keycode,
  48. priv->keycodemax);
  49. if (error) {
  50. dev_err(dev, "failed to read keycodes: %d\n", error);
  51. return error;
  52. }
  53. }
  54. return 0;
  55. }
  56. static int cst816x_i2c_read_register(struct cst816x_priv *priv, u8 reg,
  57. void *buf, size_t len)
  58. {
  59. struct i2c_msg xfer[] = {
  60. {
  61. .addr = priv->client->addr,
  62. .flags = 0,
  63. .buf = &reg,
  64. .len = sizeof(reg),
  65. },
  66. {
  67. .addr = priv->client->addr,
  68. .flags = I2C_M_RD,
  69. .buf = buf,
  70. .len = len,
  71. },
  72. };
  73. int error;
  74. int ret;
  75. ret = i2c_transfer(priv->client->adapter, xfer, ARRAY_SIZE(xfer));
  76. if (ret != ARRAY_SIZE(xfer)) {
  77. error = ret < 0 ? ret : -EIO;
  78. dev_err(&priv->client->dev, "i2c rx err: %d\n", error);
  79. return error;
  80. }
  81. return 0;
  82. }
  83. static u8 cst816x_gest_idx(u8 gest)
  84. {
  85. u8 index;
  86. switch (gest) {
  87. case 0x01: /* Slide up gesture */
  88. case 0x02: /* Slide down gesture */
  89. case 0x03: /* Slide left gesture */
  90. case 0x04: /* Slide right gesture */
  91. index = gest;
  92. break;
  93. case 0x0c: /* Long press gesture */
  94. default:
  95. index = CST816X_NUM_KEYS;
  96. break;
  97. }
  98. return index - 1;
  99. }
  100. static bool cst816x_process_touch(struct cst816x_priv *priv,
  101. struct cst816x_touch *tch)
  102. {
  103. if (cst816x_i2c_read_register(priv, CST816X_RD_REG, tch, sizeof(*tch)))
  104. return false;
  105. tch->abs_x = get_unaligned_be16(&tch->abs_x) & GENMASK(11, 0);
  106. tch->abs_y = get_unaligned_be16(&tch->abs_y) & GENMASK(11, 0);
  107. dev_dbg(&priv->client->dev, "x: %u, y: %u, t: %u, g: 0x%x\n",
  108. tch->abs_x, tch->abs_y, tch->active, tch->gest);
  109. return true;
  110. }
  111. static int cst816x_register_input(struct cst816x_priv *priv)
  112. {
  113. priv->input = devm_input_allocate_device(&priv->client->dev);
  114. if (!priv->input)
  115. return -ENOMEM;
  116. priv->input->name = "Hynitron CST816x Series Touchscreen";
  117. priv->input->phys = "input/ts";
  118. priv->input->id.bustype = BUS_I2C;
  119. input_set_drvdata(priv->input, priv);
  120. input_set_abs_params(priv->input, ABS_X, 0, 240, 0, 0);
  121. input_set_abs_params(priv->input, ABS_Y, 0, 240, 0, 0);
  122. input_set_capability(priv->input, EV_KEY, BTN_TOUCH);
  123. priv->input->keycode = priv->keycode;
  124. priv->input->keycodesize = sizeof(priv->keycode[0]);
  125. priv->input->keycodemax = priv->keycodemax;
  126. for (int i = 0; i < priv->keycodemax; i++) {
  127. if (priv->keycode[i] == KEY_RESERVED)
  128. continue;
  129. input_set_capability(priv->input, EV_KEY, priv->keycode[i]);
  130. }
  131. return input_register_device(priv->input);
  132. }
  133. static void cst816x_reset(struct cst816x_priv *priv)
  134. {
  135. gpiod_set_value_cansleep(priv->reset, 1);
  136. msleep(50);
  137. gpiod_set_value_cansleep(priv->reset, 0);
  138. msleep(100);
  139. }
  140. static irqreturn_t cst816x_irq_cb(int irq, void *cookie)
  141. {
  142. struct cst816x_priv *priv = cookie;
  143. struct cst816x_touch tch;
  144. if (!cst816x_process_touch(priv, &tch))
  145. return IRQ_HANDLED;
  146. input_report_abs(priv->input, ABS_X, tch.abs_x);
  147. input_report_abs(priv->input, ABS_Y, tch.abs_y);
  148. if (tch.gest)
  149. input_report_key(priv->input,
  150. priv->keycode[cst816x_gest_idx(tch.gest)],
  151. tch.active);
  152. input_report_key(priv->input, BTN_TOUCH, tch.active);
  153. input_sync(priv->input);
  154. return IRQ_HANDLED;
  155. }
  156. static int cst816x_probe(struct i2c_client *client)
  157. {
  158. struct device *dev = &client->dev;
  159. struct cst816x_priv *priv;
  160. int error;
  161. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  162. if (!priv)
  163. return -ENOMEM;
  164. priv->client = client;
  165. priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  166. if (IS_ERR(priv->reset))
  167. return dev_err_probe(dev, PTR_ERR(priv->reset),
  168. "gpio reset request failed\n");
  169. if (priv->reset)
  170. cst816x_reset(priv);
  171. error = cst816x_parse_keycodes(dev, priv);
  172. if (error)
  173. dev_warn(dev, "no gestures found in dt\n");
  174. error = cst816x_register_input(priv);
  175. if (error)
  176. return dev_err_probe(dev, error, "input register failed\n");
  177. error = devm_request_threaded_irq(dev, client->irq,
  178. NULL, cst816x_irq_cb, IRQF_ONESHOT,
  179. dev_driver_string(dev), priv);
  180. if (error)
  181. return dev_err_probe(dev, error, "irq request failed\n");
  182. return 0;
  183. }
  184. static const struct i2c_device_id cst816x_id[] = {
  185. { .name = "cst816s", 0 },
  186. { }
  187. };
  188. MODULE_DEVICE_TABLE(i2c, cst816x_id);
  189. static const struct of_device_id cst816x_of_match[] = {
  190. { .compatible = "hynitron,cst816s", },
  191. { }
  192. };
  193. MODULE_DEVICE_TABLE(of, cst816x_of_match);
  194. static struct i2c_driver cst816x_driver = {
  195. .driver = {
  196. .name = "cst816x",
  197. .of_match_table = cst816x_of_match,
  198. },
  199. .id_table = cst816x_id,
  200. .probe = cst816x_probe,
  201. };
  202. module_i2c_driver(cst816x_driver);
  203. MODULE_AUTHOR("Oleh Kuzhylnyi <kuzhylol@gmail.com>");
  204. MODULE_DESCRIPTION("Hynitron CST816x Series Touchscreen Driver");
  205. MODULE_LICENSE("GPL");