chipone_icn8318.c 7.8 KB

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