chipone_icn8318.c 6.9 KB

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