novatek-nvt-ts.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_CHIP_ID 0x0e
  27. #define NVT_TS_PARAMS_SIZE 0x0f
  28. #define NVT_TS_MAX_TOUCHES 10
  29. #define NVT_TS_MAX_SIZE 4096
  30. #define NVT_TS_TOUCH_INVALID 0xff
  31. #define NVT_TS_TOUCH_SLOT_SHIFT 3
  32. #define NVT_TS_TOUCH_TYPE_MASK GENMASK(2, 0)
  33. #define NVT_TS_TOUCH_NEW 1
  34. #define NVT_TS_TOUCH_UPDATE 2
  35. #define NVT_TS_TOUCH_RELEASE 3
  36. static const int nvt_ts_irq_type[4] = {
  37. IRQF_TRIGGER_RISING,
  38. IRQF_TRIGGER_FALLING,
  39. IRQF_TRIGGER_LOW,
  40. IRQF_TRIGGER_HIGH
  41. };
  42. struct nvt_ts_i2c_chip_data {
  43. u8 chip_id;
  44. };
  45. struct nvt_ts_data {
  46. struct i2c_client *client;
  47. struct input_dev *input;
  48. struct gpio_desc *reset_gpio;
  49. struct regulator_bulk_data regulators[2];
  50. struct touchscreen_properties prop;
  51. int max_touches;
  52. u8 buf[NVT_TS_TOUCH_SIZE * NVT_TS_MAX_TOUCHES];
  53. };
  54. static int nvt_ts_read_data(struct i2c_client *client, u8 reg, u8 *data, int count)
  55. {
  56. struct i2c_msg msg[2] = {
  57. {
  58. .addr = client->addr,
  59. .len = 1,
  60. .buf = &reg,
  61. },
  62. {
  63. .addr = client->addr,
  64. .flags = I2C_M_RD,
  65. .len = count,
  66. .buf = data,
  67. }
  68. };
  69. int ret;
  70. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  71. if (ret != ARRAY_SIZE(msg)) {
  72. dev_err(&client->dev, "Error reading from 0x%02x: %d\n", reg, ret);
  73. return (ret < 0) ? ret : -EIO;
  74. }
  75. return 0;
  76. }
  77. static irqreturn_t nvt_ts_irq(int irq, void *dev_id)
  78. {
  79. struct nvt_ts_data *data = dev_id;
  80. struct device *dev = &data->client->dev;
  81. int i, error, slot, x, y;
  82. bool active;
  83. u8 *touch;
  84. error = nvt_ts_read_data(data->client, NVT_TS_TOUCH_START, data->buf,
  85. data->max_touches * NVT_TS_TOUCH_SIZE);
  86. if (error)
  87. return IRQ_HANDLED;
  88. for (i = 0; i < data->max_touches; i++) {
  89. touch = &data->buf[i * NVT_TS_TOUCH_SIZE];
  90. if (touch[0] == NVT_TS_TOUCH_INVALID)
  91. continue;
  92. slot = touch[0] >> NVT_TS_TOUCH_SLOT_SHIFT;
  93. if (slot < 1 || slot > data->max_touches) {
  94. dev_warn(dev, "slot %d out of range, ignoring\n", slot);
  95. continue;
  96. }
  97. switch (touch[0] & NVT_TS_TOUCH_TYPE_MASK) {
  98. case NVT_TS_TOUCH_NEW:
  99. case NVT_TS_TOUCH_UPDATE:
  100. active = true;
  101. break;
  102. case NVT_TS_TOUCH_RELEASE:
  103. active = false;
  104. break;
  105. default:
  106. dev_warn(dev, "slot %d unknown state %d\n", slot, touch[0] & 7);
  107. continue;
  108. }
  109. slot--;
  110. x = (touch[1] << 4) | (touch[3] >> 4);
  111. y = (touch[2] << 4) | (touch[3] & 0x0f);
  112. input_mt_slot(data->input, slot);
  113. input_mt_report_slot_state(data->input, MT_TOOL_FINGER, active);
  114. touchscreen_report_pos(data->input, &data->prop, x, y, true);
  115. }
  116. input_mt_sync_frame(data->input);
  117. input_sync(data->input);
  118. return IRQ_HANDLED;
  119. }
  120. static int nvt_ts_start(struct input_dev *dev)
  121. {
  122. struct nvt_ts_data *data = input_get_drvdata(dev);
  123. int error;
  124. error = regulator_bulk_enable(ARRAY_SIZE(data->regulators), data->regulators);
  125. if (error) {
  126. dev_err(&data->client->dev, "failed to enable regulators\n");
  127. return error;
  128. }
  129. enable_irq(data->client->irq);
  130. gpiod_set_value_cansleep(data->reset_gpio, 0);
  131. return 0;
  132. }
  133. static void nvt_ts_stop(struct input_dev *dev)
  134. {
  135. struct nvt_ts_data *data = input_get_drvdata(dev);
  136. disable_irq(data->client->irq);
  137. gpiod_set_value_cansleep(data->reset_gpio, 1);
  138. regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
  139. }
  140. static int nvt_ts_suspend(struct device *dev)
  141. {
  142. struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev));
  143. mutex_lock(&data->input->mutex);
  144. if (input_device_enabled(data->input))
  145. nvt_ts_stop(data->input);
  146. mutex_unlock(&data->input->mutex);
  147. return 0;
  148. }
  149. static int nvt_ts_resume(struct device *dev)
  150. {
  151. struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev));
  152. mutex_lock(&data->input->mutex);
  153. if (input_device_enabled(data->input))
  154. nvt_ts_start(data->input);
  155. mutex_unlock(&data->input->mutex);
  156. return 0;
  157. }
  158. static DEFINE_SIMPLE_DEV_PM_OPS(nvt_ts_pm_ops, nvt_ts_suspend, nvt_ts_resume);
  159. static int nvt_ts_probe(struct i2c_client *client)
  160. {
  161. struct device *dev = &client->dev;
  162. int error, width, height, irq_type;
  163. struct nvt_ts_data *data;
  164. const struct nvt_ts_i2c_chip_data *chip;
  165. struct input_dev *input;
  166. if (!client->irq) {
  167. dev_err(dev, "Error no irq specified\n");
  168. return -EINVAL;
  169. }
  170. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  171. if (!data)
  172. return -ENOMEM;
  173. chip = device_get_match_data(&client->dev);
  174. if (!chip)
  175. return -EINVAL;
  176. data->client = client;
  177. i2c_set_clientdata(client, data);
  178. /*
  179. * VCC is the analog voltage supply
  180. * IOVCC is the digital voltage supply
  181. */
  182. data->regulators[0].supply = "vcc";
  183. data->regulators[1].supply = "iovcc";
  184. error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators), data->regulators);
  185. if (error) {
  186. dev_err(dev, "cannot get regulators: %d\n", error);
  187. return error;
  188. }
  189. error = regulator_bulk_enable(ARRAY_SIZE(data->regulators), data->regulators);
  190. if (error) {
  191. dev_err(dev, "failed to enable regulators: %d\n", error);
  192. return error;
  193. }
  194. data->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  195. error = PTR_ERR_OR_ZERO(data->reset_gpio);
  196. if (error) {
  197. regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
  198. dev_err(dev, "failed to request reset GPIO: %d\n", error);
  199. return error;
  200. }
  201. /* Wait for controller to come out of reset before params read */
  202. msleep(100);
  203. error = nvt_ts_read_data(data->client, NVT_TS_PARAMETERS_START,
  204. data->buf, NVT_TS_PARAMS_SIZE);
  205. gpiod_set_value_cansleep(data->reset_gpio, 1); /* Put back in reset */
  206. regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
  207. if (error)
  208. return error;
  209. width = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_WIDTH]);
  210. height = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_HEIGHT]);
  211. data->max_touches = data->buf[NVT_TS_PARAMS_MAX_TOUCH];
  212. irq_type = data->buf[NVT_TS_PARAMS_IRQ_TYPE];
  213. if (width > NVT_TS_MAX_SIZE || height >= NVT_TS_MAX_SIZE ||
  214. data->max_touches > NVT_TS_MAX_TOUCHES ||
  215. irq_type >= ARRAY_SIZE(nvt_ts_irq_type) ||
  216. data->buf[NVT_TS_PARAMS_CHIP_ID] != chip->chip_id) {
  217. dev_err(dev, "Unsupported touchscreen parameters: %*ph\n",
  218. NVT_TS_PARAMS_SIZE, data->buf);
  219. return -EIO;
  220. }
  221. dev_dbg(dev, "Detected %dx%d touchscreen with %d max touches\n",
  222. width, height, data->max_touches);
  223. if (data->buf[NVT_TS_PARAMS_MAX_BUTTONS])
  224. dev_warn(dev, "Touchscreen buttons are not supported\n");
  225. input = devm_input_allocate_device(dev);
  226. if (!input)
  227. return -ENOMEM;
  228. input->name = client->name;
  229. input->id.bustype = BUS_I2C;
  230. input->open = nvt_ts_start;
  231. input->close = nvt_ts_stop;
  232. input_set_abs_params(input, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
  233. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
  234. touchscreen_parse_properties(input, true, &data->prop);
  235. error = input_mt_init_slots(input, data->max_touches,
  236. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  237. if (error)
  238. return error;
  239. data->input = input;
  240. input_set_drvdata(input, data);
  241. error = devm_request_threaded_irq(dev, client->irq, NULL, nvt_ts_irq,
  242. IRQF_ONESHOT | IRQF_NO_AUTOEN |
  243. nvt_ts_irq_type[irq_type],
  244. client->name, data);
  245. if (error) {
  246. dev_err(dev, "failed to request irq: %d\n", error);
  247. return error;
  248. }
  249. error = input_register_device(input);
  250. if (error) {
  251. dev_err(dev, "failed to register input device: %d\n", error);
  252. return error;
  253. }
  254. return 0;
  255. }
  256. static const struct nvt_ts_i2c_chip_data nvt_nt11205_ts_data = {
  257. .chip_id = 0x05,
  258. };
  259. static const struct nvt_ts_i2c_chip_data nvt_nt36672a_ts_data = {
  260. .chip_id = 0x08,
  261. };
  262. static const struct of_device_id nvt_ts_of_match[] = {
  263. { .compatible = "novatek,nt11205-ts", .data = &nvt_nt11205_ts_data },
  264. { .compatible = "novatek,nt36672a-ts", .data = &nvt_nt36672a_ts_data },
  265. { }
  266. };
  267. MODULE_DEVICE_TABLE(of, nvt_ts_of_match);
  268. static const struct i2c_device_id nvt_ts_i2c_id[] = {
  269. { "nt11205-ts", (unsigned long) &nvt_nt11205_ts_data },
  270. { "nt36672a-ts", (unsigned long) &nvt_nt36672a_ts_data },
  271. { }
  272. };
  273. MODULE_DEVICE_TABLE(i2c, nvt_ts_i2c_id);
  274. static struct i2c_driver nvt_ts_driver = {
  275. .driver = {
  276. .name = "novatek-nvt-ts",
  277. .pm = pm_sleep_ptr(&nvt_ts_pm_ops),
  278. .of_match_table = nvt_ts_of_match,
  279. },
  280. .probe = nvt_ts_probe,
  281. .id_table = nvt_ts_i2c_id,
  282. };
  283. module_i2c_driver(nvt_ts_driver);
  284. MODULE_DESCRIPTION("Novatek NT11205 touchscreen driver");
  285. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  286. MODULE_LICENSE("GPL");