novatek-nvt-ts.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Novatek NT11205 i2c touchscreen controller as found
  4. * on the Acer Iconia One 7 B1-750 tablet.
  5. *
  6. * Copyright (c) 2023 Hans de Goede <hdegoede@redhat.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/i2c.h>
  12. #include <linux/input.h>
  13. #include <linux/input/mt.h>
  14. #include <linux/input/touchscreen.h>
  15. #include <linux/module.h>
  16. #include <linux/unaligned.h>
  17. #define NVT_TS_TOUCH_START 0x00
  18. #define NVT_TS_TOUCH_SIZE 6
  19. #define NVT_TS_PARAMETERS_START 0x78
  20. /* These are offsets from NVT_TS_PARAMETERS_START */
  21. #define NVT_TS_PARAMS_WIDTH 0x04
  22. #define NVT_TS_PARAMS_HEIGHT 0x06
  23. #define NVT_TS_PARAMS_MAX_TOUCH 0x09
  24. #define NVT_TS_PARAMS_MAX_BUTTONS 0x0a
  25. #define NVT_TS_PARAMS_IRQ_TYPE 0x0b
  26. #define NVT_TS_PARAMS_WAKE_TYPE 0x0c
  27. #define NVT_TS_PARAMS_CHIP_ID 0x0e
  28. #define NVT_TS_PARAMS_SIZE 0x0f
  29. #define NVT_TS_SUPPORTED_WAKE_TYPE 0x05
  30. #define NVT_TS_SUPPORTED_CHIP_ID 0x05
  31. #define NVT_TS_MAX_TOUCHES 10
  32. #define NVT_TS_MAX_SIZE 4096
  33. #define NVT_TS_TOUCH_INVALID 0xff
  34. #define NVT_TS_TOUCH_SLOT_SHIFT 3
  35. #define NVT_TS_TOUCH_TYPE_MASK GENMASK(2, 0)
  36. #define NVT_TS_TOUCH_NEW 1
  37. #define NVT_TS_TOUCH_UPDATE 2
  38. #define NVT_TS_TOUCH_RELEASE 3
  39. static const int nvt_ts_irq_type[4] = {
  40. IRQF_TRIGGER_RISING,
  41. IRQF_TRIGGER_FALLING,
  42. IRQF_TRIGGER_LOW,
  43. IRQF_TRIGGER_HIGH
  44. };
  45. struct nvt_ts_data {
  46. struct i2c_client *client;
  47. struct input_dev *input;
  48. struct gpio_desc *reset_gpio;
  49. struct touchscreen_properties prop;
  50. int max_touches;
  51. u8 buf[NVT_TS_TOUCH_SIZE * NVT_TS_MAX_TOUCHES];
  52. };
  53. static int nvt_ts_read_data(struct i2c_client *client, u8 reg, u8 *data, int count)
  54. {
  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 = count,
  65. .buf = data,
  66. }
  67. };
  68. int ret;
  69. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  70. if (ret != ARRAY_SIZE(msg)) {
  71. dev_err(&client->dev, "Error reading from 0x%02x: %d\n", reg, ret);
  72. return (ret < 0) ? ret : -EIO;
  73. }
  74. return 0;
  75. }
  76. static irqreturn_t nvt_ts_irq(int irq, void *dev_id)
  77. {
  78. struct nvt_ts_data *data = dev_id;
  79. struct device *dev = &data->client->dev;
  80. int i, error, slot, x, y;
  81. bool active;
  82. u8 *touch;
  83. error = nvt_ts_read_data(data->client, NVT_TS_TOUCH_START, data->buf,
  84. data->max_touches * NVT_TS_TOUCH_SIZE);
  85. if (error)
  86. return IRQ_HANDLED;
  87. for (i = 0; i < data->max_touches; i++) {
  88. touch = &data->buf[i * NVT_TS_TOUCH_SIZE];
  89. if (touch[0] == NVT_TS_TOUCH_INVALID)
  90. continue;
  91. slot = touch[0] >> NVT_TS_TOUCH_SLOT_SHIFT;
  92. if (slot < 1 || slot > data->max_touches) {
  93. dev_warn(dev, "slot %d out of range, ignoring\n", slot);
  94. continue;
  95. }
  96. switch (touch[0] & NVT_TS_TOUCH_TYPE_MASK) {
  97. case NVT_TS_TOUCH_NEW:
  98. case NVT_TS_TOUCH_UPDATE:
  99. active = true;
  100. break;
  101. case NVT_TS_TOUCH_RELEASE:
  102. active = false;
  103. break;
  104. default:
  105. dev_warn(dev, "slot %d unknown state %d\n", slot, touch[0] & 7);
  106. continue;
  107. }
  108. slot--;
  109. x = (touch[1] << 4) | (touch[3] >> 4);
  110. y = (touch[2] << 4) | (touch[3] & 0x0f);
  111. input_mt_slot(data->input, slot);
  112. input_mt_report_slot_state(data->input, MT_TOOL_FINGER, active);
  113. touchscreen_report_pos(data->input, &data->prop, x, y, true);
  114. }
  115. input_mt_sync_frame(data->input);
  116. input_sync(data->input);
  117. return IRQ_HANDLED;
  118. }
  119. static int nvt_ts_start(struct input_dev *dev)
  120. {
  121. struct nvt_ts_data *data = input_get_drvdata(dev);
  122. enable_irq(data->client->irq);
  123. gpiod_set_value_cansleep(data->reset_gpio, 0);
  124. return 0;
  125. }
  126. static void nvt_ts_stop(struct input_dev *dev)
  127. {
  128. struct nvt_ts_data *data = input_get_drvdata(dev);
  129. disable_irq(data->client->irq);
  130. gpiod_set_value_cansleep(data->reset_gpio, 1);
  131. }
  132. static int nvt_ts_suspend(struct device *dev)
  133. {
  134. struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev));
  135. mutex_lock(&data->input->mutex);
  136. if (input_device_enabled(data->input))
  137. nvt_ts_stop(data->input);
  138. mutex_unlock(&data->input->mutex);
  139. return 0;
  140. }
  141. static int nvt_ts_resume(struct device *dev)
  142. {
  143. struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev));
  144. mutex_lock(&data->input->mutex);
  145. if (input_device_enabled(data->input))
  146. nvt_ts_start(data->input);
  147. mutex_unlock(&data->input->mutex);
  148. return 0;
  149. }
  150. static DEFINE_SIMPLE_DEV_PM_OPS(nvt_ts_pm_ops, nvt_ts_suspend, nvt_ts_resume);
  151. static int nvt_ts_probe(struct i2c_client *client)
  152. {
  153. struct device *dev = &client->dev;
  154. int error, width, height, irq_type;
  155. struct nvt_ts_data *data;
  156. struct input_dev *input;
  157. if (!client->irq) {
  158. dev_err(dev, "Error no irq specified\n");
  159. return -EINVAL;
  160. }
  161. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  162. if (!data)
  163. return -ENOMEM;
  164. data->client = client;
  165. i2c_set_clientdata(client, data);
  166. data->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  167. error = PTR_ERR_OR_ZERO(data->reset_gpio);
  168. if (error) {
  169. dev_err(dev, "failed to request reset GPIO: %d\n", error);
  170. return error;
  171. }
  172. /* Wait for controller to come out of reset before params read */
  173. msleep(100);
  174. error = nvt_ts_read_data(data->client, NVT_TS_PARAMETERS_START,
  175. data->buf, NVT_TS_PARAMS_SIZE);
  176. gpiod_set_value_cansleep(data->reset_gpio, 1); /* Put back in reset */
  177. if (error)
  178. return error;
  179. width = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_WIDTH]);
  180. height = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_HEIGHT]);
  181. data->max_touches = data->buf[NVT_TS_PARAMS_MAX_TOUCH];
  182. irq_type = data->buf[NVT_TS_PARAMS_IRQ_TYPE];
  183. if (width > NVT_TS_MAX_SIZE || height >= NVT_TS_MAX_SIZE ||
  184. data->max_touches > NVT_TS_MAX_TOUCHES ||
  185. irq_type >= ARRAY_SIZE(nvt_ts_irq_type) ||
  186. data->buf[NVT_TS_PARAMS_WAKE_TYPE] != NVT_TS_SUPPORTED_WAKE_TYPE ||
  187. data->buf[NVT_TS_PARAMS_CHIP_ID] != NVT_TS_SUPPORTED_CHIP_ID) {
  188. dev_err(dev, "Unsupported touchscreen parameters: %*ph\n",
  189. NVT_TS_PARAMS_SIZE, data->buf);
  190. return -EIO;
  191. }
  192. dev_dbg(dev, "Detected %dx%d touchscreen with %d max touches\n",
  193. width, height, data->max_touches);
  194. if (data->buf[NVT_TS_PARAMS_MAX_BUTTONS])
  195. dev_warn(dev, "Touchscreen buttons are not supported\n");
  196. input = devm_input_allocate_device(dev);
  197. if (!input)
  198. return -ENOMEM;
  199. input->name = client->name;
  200. input->id.bustype = BUS_I2C;
  201. input->open = nvt_ts_start;
  202. input->close = nvt_ts_stop;
  203. input_set_abs_params(input, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
  204. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
  205. touchscreen_parse_properties(input, true, &data->prop);
  206. error = input_mt_init_slots(input, data->max_touches,
  207. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  208. if (error)
  209. return error;
  210. data->input = input;
  211. input_set_drvdata(input, data);
  212. error = devm_request_threaded_irq(dev, client->irq, NULL, nvt_ts_irq,
  213. IRQF_ONESHOT | IRQF_NO_AUTOEN |
  214. nvt_ts_irq_type[irq_type],
  215. client->name, data);
  216. if (error) {
  217. dev_err(dev, "failed to request irq: %d\n", error);
  218. return error;
  219. }
  220. error = input_register_device(input);
  221. if (error) {
  222. dev_err(dev, "failed to register input device: %d\n", error);
  223. return error;
  224. }
  225. return 0;
  226. }
  227. static const struct i2c_device_id nvt_ts_i2c_id[] = {
  228. { "NVT-ts" },
  229. { }
  230. };
  231. MODULE_DEVICE_TABLE(i2c, nvt_ts_i2c_id);
  232. static struct i2c_driver nvt_ts_driver = {
  233. .driver = {
  234. .name = "novatek-nvt-ts",
  235. .pm = pm_sleep_ptr(&nvt_ts_pm_ops),
  236. },
  237. .probe = nvt_ts_probe,
  238. .id_table = nvt_ts_i2c_id,
  239. };
  240. module_i2c_driver(nvt_ts_driver);
  241. MODULE_DESCRIPTION("Novatek NT11205 touchscreen driver");
  242. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  243. MODULE_LICENSE("GPL");