exc3000.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for I2C connected EETI EXC3000 multiple touch controller
  4. *
  5. * Copyright (C) 2017 Ahmet Inan <inan@distec.de>
  6. *
  7. * minimal implementation based on egalax_ts.c and egalax_i2c.c
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/bitops.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/i2c.h>
  15. #include <linux/input.h>
  16. #include <linux/input/mt.h>
  17. #include <linux/input/touchscreen.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/sizes.h>
  23. #include <linux/timer.h>
  24. #include <linux/unaligned.h>
  25. #define EXC3000_NUM_SLOTS 10
  26. #define EXC3000_SLOTS_PER_FRAME 5
  27. #define EXC3000_LEN_FRAME 66
  28. #define EXC3000_LEN_VENDOR_REQUEST 68
  29. #define EXC3000_LEN_POINT 10
  30. #define EXC3000_LEN_MODEL_NAME 16
  31. #define EXC3000_LEN_FW_VERSION 16
  32. #define EXC3000_VENDOR_EVENT 0x03
  33. #define EXC3000_MT1_EVENT 0x06
  34. #define EXC3000_MT2_EVENT 0x18
  35. #define EXC3000_TIMEOUT_MS 100
  36. #define EXC3000_RESET_MS 10
  37. #define EXC3000_READY_MS 100
  38. static const struct i2c_device_id exc3000_id[];
  39. struct eeti_dev_info {
  40. const char *name;
  41. int max_xy;
  42. };
  43. enum eeti_dev_id {
  44. EETI_EXC3000,
  45. EETI_EXC80H60,
  46. EETI_EXC80H84,
  47. EETI_EXC81W32,
  48. };
  49. static struct eeti_dev_info exc3000_info[] = {
  50. [EETI_EXC3000] = {
  51. .name = "EETI EXC3000 Touch Screen",
  52. .max_xy = SZ_4K - 1,
  53. },
  54. [EETI_EXC80H60] = {
  55. .name = "EETI EXC80H60 Touch Screen",
  56. .max_xy = SZ_16K - 1,
  57. },
  58. [EETI_EXC80H84] = {
  59. .name = "EETI EXC80H84 Touch Screen",
  60. .max_xy = SZ_16K - 1,
  61. },
  62. [EETI_EXC81W32] = {
  63. .name = "EETI EXC81W32 Touch Screen",
  64. .max_xy = SZ_16K - 1,
  65. },
  66. };
  67. struct exc3000_data {
  68. struct i2c_client *client;
  69. const struct eeti_dev_info *info;
  70. struct input_dev *input;
  71. struct touchscreen_properties prop;
  72. struct gpio_desc *reset;
  73. struct timer_list timer;
  74. u8 buf[2 * EXC3000_LEN_FRAME];
  75. struct completion wait_event;
  76. struct mutex query_lock;
  77. };
  78. static void exc3000_report_slots(struct input_dev *input,
  79. struct touchscreen_properties *prop,
  80. const u8 *buf, int num)
  81. {
  82. for (; num--; buf += EXC3000_LEN_POINT) {
  83. if (buf[0] & BIT(0)) {
  84. input_mt_slot(input, buf[1]);
  85. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  86. touchscreen_report_pos(input, prop,
  87. get_unaligned_le16(buf + 2),
  88. get_unaligned_le16(buf + 4),
  89. true);
  90. }
  91. }
  92. }
  93. static void exc3000_timer(struct timer_list *t)
  94. {
  95. struct exc3000_data *data = timer_container_of(data, t, timer);
  96. input_mt_sync_frame(data->input);
  97. input_sync(data->input);
  98. }
  99. static inline void exc3000_schedule_timer(struct exc3000_data *data)
  100. {
  101. mod_timer(&data->timer, jiffies + msecs_to_jiffies(EXC3000_TIMEOUT_MS));
  102. }
  103. static void exc3000_shutdown_timer(void *timer)
  104. {
  105. timer_shutdown_sync(timer);
  106. }
  107. static int exc3000_read_frame(struct exc3000_data *data, u8 *buf)
  108. {
  109. struct i2c_client *client = data->client;
  110. int ret;
  111. ret = i2c_master_send(client, "'", 2);
  112. if (ret < 0)
  113. return ret;
  114. if (ret != 2)
  115. return -EIO;
  116. ret = i2c_master_recv(client, buf, EXC3000_LEN_FRAME);
  117. if (ret < 0)
  118. return ret;
  119. if (ret != EXC3000_LEN_FRAME)
  120. return -EIO;
  121. if (get_unaligned_le16(buf) != EXC3000_LEN_FRAME)
  122. return -EINVAL;
  123. return 0;
  124. }
  125. static int exc3000_handle_mt_event(struct exc3000_data *data)
  126. {
  127. struct input_dev *input = data->input;
  128. int ret, total_slots;
  129. u8 *buf = data->buf;
  130. total_slots = buf[3];
  131. if (!total_slots || total_slots > EXC3000_NUM_SLOTS) {
  132. ret = -EINVAL;
  133. goto out_fail;
  134. }
  135. if (total_slots > EXC3000_SLOTS_PER_FRAME) {
  136. /* Read 2nd frame to get the rest of the contacts. */
  137. ret = exc3000_read_frame(data, buf + EXC3000_LEN_FRAME);
  138. if (ret)
  139. goto out_fail;
  140. /* 2nd chunk must have number of contacts set to 0. */
  141. if (buf[EXC3000_LEN_FRAME + 3] != 0) {
  142. ret = -EINVAL;
  143. goto out_fail;
  144. }
  145. }
  146. /*
  147. * We read full state successfully, no contacts will be "stuck".
  148. */
  149. timer_delete_sync(&data->timer);
  150. while (total_slots > 0) {
  151. int slots = min(total_slots, EXC3000_SLOTS_PER_FRAME);
  152. exc3000_report_slots(input, &data->prop, buf + 4, slots);
  153. total_slots -= slots;
  154. buf += EXC3000_LEN_FRAME;
  155. }
  156. input_mt_sync_frame(input);
  157. input_sync(input);
  158. return 0;
  159. out_fail:
  160. /* Schedule a timer to release "stuck" contacts */
  161. exc3000_schedule_timer(data);
  162. return ret;
  163. }
  164. static irqreturn_t exc3000_interrupt(int irq, void *dev_id)
  165. {
  166. struct exc3000_data *data = dev_id;
  167. u8 *buf = data->buf;
  168. int ret;
  169. ret = exc3000_read_frame(data, buf);
  170. if (ret) {
  171. /* Schedule a timer to release "stuck" contacts */
  172. exc3000_schedule_timer(data);
  173. goto out;
  174. }
  175. switch (buf[2]) {
  176. case EXC3000_VENDOR_EVENT:
  177. complete(&data->wait_event);
  178. break;
  179. case EXC3000_MT1_EVENT:
  180. case EXC3000_MT2_EVENT:
  181. exc3000_handle_mt_event(data);
  182. break;
  183. default:
  184. break;
  185. }
  186. out:
  187. return IRQ_HANDLED;
  188. }
  189. static int exc3000_vendor_data_request(struct exc3000_data *data, u8 *request,
  190. u8 request_len, u8 *response, int timeout)
  191. {
  192. u8 buf[EXC3000_LEN_VENDOR_REQUEST] = { 0x67, 0x00, 0x42, 0x00, 0x03 };
  193. int ret;
  194. unsigned long time_left;
  195. mutex_lock(&data->query_lock);
  196. reinit_completion(&data->wait_event);
  197. buf[5] = request_len;
  198. memcpy(&buf[6], request, request_len);
  199. ret = i2c_master_send(data->client, buf, EXC3000_LEN_VENDOR_REQUEST);
  200. if (ret < 0)
  201. goto out_unlock;
  202. if (response) {
  203. time_left = wait_for_completion_timeout(&data->wait_event,
  204. timeout * HZ);
  205. if (time_left == 0) {
  206. ret = -ETIMEDOUT;
  207. goto out_unlock;
  208. }
  209. if (data->buf[3] >= EXC3000_LEN_FRAME) {
  210. ret = -ENOSPC;
  211. goto out_unlock;
  212. }
  213. memcpy(response, &data->buf[4], data->buf[3]);
  214. ret = data->buf[3];
  215. }
  216. out_unlock:
  217. mutex_unlock(&data->query_lock);
  218. return ret;
  219. }
  220. static ssize_t fw_version_show(struct device *dev,
  221. struct device_attribute *attr, char *buf)
  222. {
  223. struct i2c_client *client = to_i2c_client(dev);
  224. struct exc3000_data *data = i2c_get_clientdata(client);
  225. u8 response[EXC3000_LEN_FRAME];
  226. int ret;
  227. /* query bootloader info */
  228. ret = exc3000_vendor_data_request(data,
  229. (u8[]){0x39, 0x02}, 2, response, 1);
  230. if (ret < 0)
  231. return ret;
  232. /*
  233. * If the bootloader version is non-zero then the device is in
  234. * bootloader mode and won't answer a query for the application FW
  235. * version, so we just use the bootloader version info.
  236. */
  237. if (response[2] || response[3])
  238. return sprintf(buf, "%d.%d\n", response[2], response[3]);
  239. ret = exc3000_vendor_data_request(data, (u8[]){'D'}, 1, response, 1);
  240. if (ret < 0)
  241. return ret;
  242. return sprintf(buf, "%s\n", &response[1]);
  243. }
  244. static DEVICE_ATTR_RO(fw_version);
  245. static ssize_t model_show(struct device *dev,
  246. struct device_attribute *attr, char *buf)
  247. {
  248. struct i2c_client *client = to_i2c_client(dev);
  249. struct exc3000_data *data = i2c_get_clientdata(client);
  250. u8 response[EXC3000_LEN_FRAME];
  251. int ret;
  252. ret = exc3000_vendor_data_request(data, (u8[]){'E'}, 1, response, 1);
  253. if (ret < 0)
  254. return ret;
  255. return sprintf(buf, "%s\n", &response[1]);
  256. }
  257. static DEVICE_ATTR_RO(model);
  258. static ssize_t type_show(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. struct i2c_client *client = to_i2c_client(dev);
  262. struct exc3000_data *data = i2c_get_clientdata(client);
  263. u8 response[EXC3000_LEN_FRAME];
  264. int ret;
  265. ret = exc3000_vendor_data_request(data, (u8[]){'F'}, 1, response, 1);
  266. if (ret < 0)
  267. return ret;
  268. return sprintf(buf, "%s\n", &response[1]);
  269. }
  270. static DEVICE_ATTR_RO(type);
  271. static struct attribute *exc3000_attrs[] = {
  272. &dev_attr_fw_version.attr,
  273. &dev_attr_model.attr,
  274. &dev_attr_type.attr,
  275. NULL
  276. };
  277. ATTRIBUTE_GROUPS(exc3000);
  278. static int exc3000_probe(struct i2c_client *client)
  279. {
  280. struct exc3000_data *data;
  281. struct input_dev *input;
  282. int error, max_xy, retry;
  283. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  284. if (!data)
  285. return -ENOMEM;
  286. data->client = client;
  287. data->info = device_get_match_data(&client->dev);
  288. if (!data->info) {
  289. enum eeti_dev_id eeti_dev_id =
  290. i2c_match_id(exc3000_id, client)->driver_data;
  291. data->info = &exc3000_info[eeti_dev_id];
  292. }
  293. timer_setup(&data->timer, exc3000_timer, 0);
  294. init_completion(&data->wait_event);
  295. mutex_init(&data->query_lock);
  296. data->reset = devm_gpiod_get_optional(&client->dev, "reset",
  297. GPIOD_OUT_HIGH);
  298. if (IS_ERR(data->reset))
  299. return PTR_ERR(data->reset);
  300. /* For proper reset sequence, enable power while reset asserted */
  301. error = devm_regulator_get_enable(&client->dev, "vdd");
  302. if (error && error != -ENODEV)
  303. return dev_err_probe(&client->dev, error,
  304. "failed to request vdd regulator\n");
  305. if (data->reset) {
  306. msleep(EXC3000_RESET_MS);
  307. gpiod_set_value_cansleep(data->reset, 0);
  308. msleep(EXC3000_READY_MS);
  309. }
  310. input = devm_input_allocate_device(&client->dev);
  311. if (!input)
  312. return -ENOMEM;
  313. data->input = input;
  314. input_set_drvdata(input, data);
  315. input->name = data->info->name;
  316. input->id.bustype = BUS_I2C;
  317. max_xy = data->info->max_xy;
  318. input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0);
  319. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0);
  320. touchscreen_parse_properties(input, true, &data->prop);
  321. error = input_mt_init_slots(input, EXC3000_NUM_SLOTS,
  322. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  323. if (error)
  324. return error;
  325. error = input_register_device(input);
  326. if (error)
  327. return error;
  328. error = devm_add_action_or_reset(&client->dev, exc3000_shutdown_timer,
  329. &data->timer);
  330. if (error)
  331. return error;
  332. error = devm_request_threaded_irq(&client->dev, client->irq,
  333. NULL, exc3000_interrupt, IRQF_ONESHOT,
  334. client->name, data);
  335. if (error)
  336. return error;
  337. /*
  338. * I²C does not have built-in recovery, so retry on failure. This
  339. * ensures, that the device probe will not fail for temporary issues
  340. * on the bus. This is not needed for the sysfs calls (userspace
  341. * will receive the error code and can start another query) and
  342. * cannot be done for touch events (but that only means loosing one
  343. * or two touch events anyways).
  344. */
  345. for (retry = 0; retry < 3; retry++) {
  346. u8 response[EXC3000_LEN_FRAME];
  347. error = exc3000_vendor_data_request(data, (u8[]){'E'}, 1,
  348. response, 1);
  349. if (error > 0) {
  350. dev_dbg(&client->dev, "TS Model: %s", &response[1]);
  351. error = 0;
  352. break;
  353. }
  354. dev_warn(&client->dev, "Retry %d get EETI EXC3000 model: %d\n",
  355. retry + 1, error);
  356. }
  357. if (error)
  358. return error;
  359. i2c_set_clientdata(client, data);
  360. return 0;
  361. }
  362. static const struct i2c_device_id exc3000_id[] = {
  363. { "exc3000", EETI_EXC3000 },
  364. { "exc80h60", EETI_EXC80H60 },
  365. { "exc80h84", EETI_EXC80H84 },
  366. { "exc81w32", EETI_EXC81W32 },
  367. { }
  368. };
  369. MODULE_DEVICE_TABLE(i2c, exc3000_id);
  370. #ifdef CONFIG_OF
  371. static const struct of_device_id exc3000_of_match[] = {
  372. { .compatible = "eeti,exc3000", .data = &exc3000_info[EETI_EXC3000] },
  373. { .compatible = "eeti,exc80h60", .data = &exc3000_info[EETI_EXC80H60] },
  374. { .compatible = "eeti,exc80h84", .data = &exc3000_info[EETI_EXC80H84] },
  375. { .compatible = "eeti,exc81w32", .data = &exc3000_info[EETI_EXC81W32] },
  376. { }
  377. };
  378. MODULE_DEVICE_TABLE(of, exc3000_of_match);
  379. #endif
  380. #ifdef CONFIG_ACPI
  381. static const struct acpi_device_id exc3000_acpi_match[] = {
  382. { "EGA00001", .driver_data = (kernel_ulong_t)&exc3000_info[EETI_EXC80H60] },
  383. { }
  384. };
  385. MODULE_DEVICE_TABLE(acpi, exc3000_acpi_match);
  386. #endif
  387. static struct i2c_driver exc3000_driver = {
  388. .driver = {
  389. .name = "exc3000",
  390. .dev_groups = exc3000_groups,
  391. .of_match_table = of_match_ptr(exc3000_of_match),
  392. .acpi_match_table = ACPI_PTR(exc3000_acpi_match),
  393. },
  394. .id_table = exc3000_id,
  395. .probe = exc3000_probe,
  396. };
  397. module_i2c_driver(exc3000_driver);
  398. MODULE_AUTHOR("Ahmet Inan <inan@distec.de>");
  399. MODULE_DESCRIPTION("I2C connected EETI EXC3000 multiple touch controller driver");
  400. MODULE_LICENSE("GPL v2");