exc3000.c 11 KB

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