chipone_icn8318.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for ChipOne icn8318 i2c touchscreen controller
  4. *
  5. * Copyright (c) 2015 Red Hat Inc.
  6. *
  7. * Red Hat authors:
  8. * Hans de Goede <[email protected]>
  9. */
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/interrupt.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/module.h>
  17. #include <linux/of.h>
  18. #define ICN8318_REG_POWER 4
  19. #define ICN8318_REG_TOUCHDATA 16
  20. #define ICN8318_POWER_ACTIVE 0
  21. #define ICN8318_POWER_MONITOR 1
  22. #define ICN8318_POWER_HIBERNATE 2
  23. #define ICN8318_MAX_TOUCHES 5
  24. struct icn8318_touch {
  25. __u8 slot;
  26. __be16 x;
  27. __be16 y;
  28. __u8 pressure; /* Seems more like finger width then pressure really */
  29. __u8 event;
  30. /* The difference between 2 and 3 is unclear */
  31. #define ICN8318_EVENT_NO_DATA 1 /* No finger seen yet since wakeup */
  32. #define ICN8318_EVENT_UPDATE1 2 /* New or updated coordinates */
  33. #define ICN8318_EVENT_UPDATE2 3 /* New or updated coordinates */
  34. #define ICN8318_EVENT_END 4 /* Finger lifted */
  35. } __packed;
  36. struct icn8318_touch_data {
  37. __u8 softbutton;
  38. __u8 touch_count;
  39. struct icn8318_touch touches[ICN8318_MAX_TOUCHES];
  40. } __packed;
  41. struct icn8318_data {
  42. struct i2c_client *client;
  43. struct input_dev *input;
  44. struct gpio_desc *wake_gpio;
  45. struct touchscreen_properties prop;
  46. };
  47. static int icn8318_read_touch_data(struct i2c_client *client,
  48. struct icn8318_touch_data *touch_data)
  49. {
  50. u8 reg = ICN8318_REG_TOUCHDATA;
  51. struct i2c_msg msg[2] = {
  52. {
  53. .addr = client->addr,
  54. .len = 1,
  55. .buf = &reg
  56. },
  57. {
  58. .addr = client->addr,
  59. .flags = I2C_M_RD,
  60. .len = sizeof(struct icn8318_touch_data),
  61. .buf = (u8 *)touch_data
  62. }
  63. };
  64. return i2c_transfer(client->adapter, msg, 2);
  65. }
  66. static inline bool icn8318_touch_active(u8 event)
  67. {
  68. return (event == ICN8318_EVENT_UPDATE1) ||
  69. (event == ICN8318_EVENT_UPDATE2);
  70. }
  71. static irqreturn_t icn8318_irq(int irq, void *dev_id)
  72. {
  73. struct icn8318_data *data = dev_id;
  74. struct device *dev = &data->client->dev;
  75. struct icn8318_touch_data touch_data;
  76. int i, ret;
  77. ret = icn8318_read_touch_data(data->client, &touch_data);
  78. if (ret < 0) {
  79. dev_err(dev, "Error reading touch data: %d\n", ret);
  80. return IRQ_HANDLED;
  81. }
  82. if (touch_data.softbutton) {
  83. /*
  84. * Other data is invalid when a softbutton is pressed.
  85. * This needs some extra devicetree bindings to map the icn8318
  86. * softbutton codes to evdev codes. Currently no known devices
  87. * use this.
  88. */
  89. return IRQ_HANDLED;
  90. }
  91. if (touch_data.touch_count > ICN8318_MAX_TOUCHES) {
  92. dev_warn(dev, "Too much touches %d > %d\n",
  93. touch_data.touch_count, ICN8318_MAX_TOUCHES);
  94. touch_data.touch_count = ICN8318_MAX_TOUCHES;
  95. }
  96. for (i = 0; i < touch_data.touch_count; i++) {
  97. struct icn8318_touch *touch = &touch_data.touches[i];
  98. bool act = icn8318_touch_active(touch->event);
  99. input_mt_slot(data->input, touch->slot);
  100. input_mt_report_slot_state(data->input, MT_TOOL_FINGER, act);
  101. if (!act)
  102. continue;
  103. touchscreen_report_pos(data->input, &data->prop,
  104. be16_to_cpu(touch->x),
  105. be16_to_cpu(touch->y), true);
  106. }
  107. input_mt_sync_frame(data->input);
  108. input_sync(data->input);
  109. return IRQ_HANDLED;
  110. }
  111. static int icn8318_start(struct input_dev *dev)
  112. {
  113. struct icn8318_data *data = input_get_drvdata(dev);
  114. enable_irq(data->client->irq);
  115. gpiod_set_value_cansleep(data->wake_gpio, 1);
  116. return 0;
  117. }
  118. static void icn8318_stop(struct input_dev *dev)
  119. {
  120. struct icn8318_data *data = input_get_drvdata(dev);
  121. disable_irq(data->client->irq);
  122. i2c_smbus_write_byte_data(data->client, ICN8318_REG_POWER,
  123. ICN8318_POWER_HIBERNATE);
  124. gpiod_set_value_cansleep(data->wake_gpio, 0);
  125. }
  126. #ifdef CONFIG_PM_SLEEP
  127. static int icn8318_suspend(struct device *dev)
  128. {
  129. struct icn8318_data *data = i2c_get_clientdata(to_i2c_client(dev));
  130. mutex_lock(&data->input->mutex);
  131. if (input_device_enabled(data->input))
  132. icn8318_stop(data->input);
  133. mutex_unlock(&data->input->mutex);
  134. return 0;
  135. }
  136. static int icn8318_resume(struct device *dev)
  137. {
  138. struct icn8318_data *data = i2c_get_clientdata(to_i2c_client(dev));
  139. mutex_lock(&data->input->mutex);
  140. if (input_device_enabled(data->input))
  141. icn8318_start(data->input);
  142. mutex_unlock(&data->input->mutex);
  143. return 0;
  144. }
  145. #endif
  146. static SIMPLE_DEV_PM_OPS(icn8318_pm_ops, icn8318_suspend, icn8318_resume);
  147. static int icn8318_probe(struct i2c_client *client,
  148. const struct i2c_device_id *id)
  149. {
  150. struct device *dev = &client->dev;
  151. struct icn8318_data *data;
  152. struct input_dev *input;
  153. int error;
  154. if (!client->irq) {
  155. dev_err(dev, "Error no irq specified\n");
  156. return -EINVAL;
  157. }
  158. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  159. if (!data)
  160. return -ENOMEM;
  161. data->wake_gpio = devm_gpiod_get(dev, "wake", GPIOD_OUT_LOW);
  162. if (IS_ERR(data->wake_gpio)) {
  163. error = PTR_ERR(data->wake_gpio);
  164. if (error != -EPROBE_DEFER)
  165. dev_err(dev, "Error getting wake gpio: %d\n", error);
  166. return error;
  167. }
  168. input = devm_input_allocate_device(dev);
  169. if (!input)
  170. return -ENOMEM;
  171. input->name = client->name;
  172. input->id.bustype = BUS_I2C;
  173. input->open = icn8318_start;
  174. input->close = icn8318_stop;
  175. input->dev.parent = dev;
  176. input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
  177. input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
  178. touchscreen_parse_properties(input, true, &data->prop);
  179. if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
  180. !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
  181. dev_err(dev, "Error touchscreen-size-x and/or -y missing\n");
  182. return -EINVAL;
  183. }
  184. error = input_mt_init_slots(input, ICN8318_MAX_TOUCHES,
  185. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  186. if (error)
  187. return error;
  188. data->client = client;
  189. data->input = input;
  190. input_set_drvdata(input, data);
  191. error = devm_request_threaded_irq(dev, client->irq, NULL, icn8318_irq,
  192. IRQF_ONESHOT, client->name, data);
  193. if (error) {
  194. dev_err(dev, "Error requesting irq: %d\n", error);
  195. return error;
  196. }
  197. /* Stop device till opened */
  198. icn8318_stop(data->input);
  199. error = input_register_device(input);
  200. if (error)
  201. return error;
  202. i2c_set_clientdata(client, data);
  203. return 0;
  204. }
  205. static const struct of_device_id icn8318_of_match[] = {
  206. { .compatible = "chipone,icn8318" },
  207. { }
  208. };
  209. MODULE_DEVICE_TABLE(of, icn8318_of_match);
  210. /* This is useless for OF-enabled devices, but it is needed by I2C subsystem */
  211. static const struct i2c_device_id icn8318_i2c_id[] = {
  212. { },
  213. };
  214. MODULE_DEVICE_TABLE(i2c, icn8318_i2c_id);
  215. static struct i2c_driver icn8318_driver = {
  216. .driver = {
  217. .name = "chipone_icn8318",
  218. .pm = &icn8318_pm_ops,
  219. .of_match_table = icn8318_of_match,
  220. },
  221. .probe = icn8318_probe,
  222. .id_table = icn8318_i2c_id,
  223. };
  224. module_i2c_driver(icn8318_driver);
  225. MODULE_DESCRIPTION("ChipOne icn8318 I2C Touchscreen Driver");
  226. MODULE_AUTHOR("Hans de Goede <[email protected]>");
  227. MODULE_LICENSE("GPL");