chipone_icn8318.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <hdegoede@redhat.com>
  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. static int icn8318_suspend(struct device *dev)
  127. {
  128. struct icn8318_data *data = i2c_get_clientdata(to_i2c_client(dev));
  129. mutex_lock(&data->input->mutex);
  130. if (input_device_enabled(data->input))
  131. icn8318_stop(data->input);
  132. mutex_unlock(&data->input->mutex);
  133. return 0;
  134. }
  135. static int icn8318_resume(struct device *dev)
  136. {
  137. struct icn8318_data *data = i2c_get_clientdata(to_i2c_client(dev));
  138. mutex_lock(&data->input->mutex);
  139. if (input_device_enabled(data->input))
  140. icn8318_start(data->input);
  141. mutex_unlock(&data->input->mutex);
  142. return 0;
  143. }
  144. static DEFINE_SIMPLE_DEV_PM_OPS(icn8318_pm_ops, icn8318_suspend, icn8318_resume);
  145. static int icn8318_probe(struct i2c_client *client)
  146. {
  147. struct device *dev = &client->dev;
  148. struct icn8318_data *data;
  149. struct input_dev *input;
  150. int error;
  151. if (!client->irq) {
  152. dev_err(dev, "Error no irq specified\n");
  153. return -EINVAL;
  154. }
  155. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  156. if (!data)
  157. return -ENOMEM;
  158. data->wake_gpio = devm_gpiod_get(dev, "wake", GPIOD_OUT_LOW);
  159. if (IS_ERR(data->wake_gpio))
  160. return dev_err_probe(dev, PTR_ERR(data->wake_gpio), "Error getting wake gpio\n");
  161. input = devm_input_allocate_device(dev);
  162. if (!input)
  163. return -ENOMEM;
  164. input->name = client->name;
  165. input->id.bustype = BUS_I2C;
  166. input->open = icn8318_start;
  167. input->close = icn8318_stop;
  168. input->dev.parent = dev;
  169. input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
  170. input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
  171. touchscreen_parse_properties(input, true, &data->prop);
  172. if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
  173. !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
  174. dev_err(dev, "Error touchscreen-size-x and/or -y missing\n");
  175. return -EINVAL;
  176. }
  177. error = input_mt_init_slots(input, ICN8318_MAX_TOUCHES,
  178. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  179. if (error)
  180. return error;
  181. data->client = client;
  182. data->input = input;
  183. input_set_drvdata(input, data);
  184. error = devm_request_threaded_irq(dev, client->irq, NULL, icn8318_irq,
  185. IRQF_ONESHOT, client->name, data);
  186. if (error) {
  187. dev_err(dev, "Error requesting irq: %d\n", error);
  188. return error;
  189. }
  190. /* Stop device till opened */
  191. icn8318_stop(data->input);
  192. error = input_register_device(input);
  193. if (error)
  194. return error;
  195. i2c_set_clientdata(client, data);
  196. return 0;
  197. }
  198. static const struct of_device_id icn8318_of_match[] = {
  199. { .compatible = "chipone,icn8318" },
  200. { }
  201. };
  202. MODULE_DEVICE_TABLE(of, icn8318_of_match);
  203. /* This is useless for OF-enabled devices, but it is needed by I2C subsystem */
  204. static const struct i2c_device_id icn8318_i2c_id[] = {
  205. { },
  206. };
  207. MODULE_DEVICE_TABLE(i2c, icn8318_i2c_id);
  208. static struct i2c_driver icn8318_driver = {
  209. .driver = {
  210. .name = "chipone_icn8318",
  211. .pm = pm_sleep_ptr(&icn8318_pm_ops),
  212. .of_match_table = icn8318_of_match,
  213. },
  214. .probe = icn8318_probe,
  215. .id_table = icn8318_i2c_id,
  216. };
  217. module_i2c_driver(icn8318_driver);
  218. MODULE_DESCRIPTION("ChipOne icn8318 I2C Touchscreen Driver");
  219. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  220. MODULE_LICENSE("GPL");