st1232.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ST1232 Touchscreen Controller Driver
  4. *
  5. * Copyright (C) 2010 Renesas Solutions Corp.
  6. * Tony SIM <chinyeow.sim.xt@renesas.com>
  7. *
  8. * Using code from:
  9. * - android.git.kernel.org: projects/kernel/common.git: synaptics_i2c_rmi.c
  10. * Copyright (C) 2007 Google, Inc.
  11. */
  12. #include <linux/delay.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/pm_qos.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/input/touch-overlay.h>
  25. #define ST1232_TS_NAME "st1232-ts"
  26. #define ST1633_TS_NAME "st1633-ts"
  27. #define REG_STATUS 0x01 /* Device Status | Error Code */
  28. #define STATUS_NORMAL 0x00
  29. #define STATUS_INIT 0x01
  30. #define STATUS_ERROR 0x02
  31. #define STATUS_AUTO_TUNING 0x03
  32. #define STATUS_IDLE 0x04
  33. #define STATUS_POWER_DOWN 0x05
  34. #define ERROR_NONE 0x00
  35. #define ERROR_INVALID_ADDRESS 0x10
  36. #define ERROR_INVALID_VALUE 0x20
  37. #define ERROR_INVALID_PLATFORM 0x30
  38. #define REG_XY_RESOLUTION 0x04
  39. #define REG_XY_COORDINATES 0x12
  40. #define ST_TS_MAX_FINGERS 10
  41. struct st_chip_info {
  42. bool have_z;
  43. u16 max_area;
  44. u16 max_fingers;
  45. };
  46. struct st1232_ts_data {
  47. struct i2c_client *client;
  48. struct input_dev *input_dev;
  49. struct touchscreen_properties prop;
  50. struct dev_pm_qos_request low_latency_req;
  51. struct gpio_desc *reset_gpio;
  52. const struct st_chip_info *chip_info;
  53. struct list_head touch_overlay_list;
  54. int read_buf_len;
  55. u8 *read_buf;
  56. };
  57. static int st1232_ts_read_data(struct st1232_ts_data *ts, u8 reg,
  58. unsigned int n)
  59. {
  60. struct i2c_client *client = ts->client;
  61. struct i2c_msg msg[] = {
  62. {
  63. .addr = client->addr,
  64. .len = sizeof(reg),
  65. .buf = &reg,
  66. },
  67. {
  68. .addr = client->addr,
  69. .flags = I2C_M_RD | I2C_M_DMA_SAFE,
  70. .len = n,
  71. .buf = ts->read_buf,
  72. }
  73. };
  74. int ret;
  75. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  76. if (ret != ARRAY_SIZE(msg))
  77. return ret < 0 ? ret : -EIO;
  78. return 0;
  79. }
  80. static int st1232_ts_wait_ready(struct st1232_ts_data *ts)
  81. {
  82. unsigned int retries;
  83. int error;
  84. for (retries = 100; retries; retries--) {
  85. error = st1232_ts_read_data(ts, REG_STATUS, 1);
  86. if (!error) {
  87. switch (ts->read_buf[0]) {
  88. case STATUS_NORMAL | ERROR_NONE:
  89. case STATUS_IDLE | ERROR_NONE:
  90. return 0;
  91. }
  92. }
  93. usleep_range(1000, 2000);
  94. }
  95. return -ENXIO;
  96. }
  97. static int st1232_ts_read_resolution(struct st1232_ts_data *ts, u16 *max_x,
  98. u16 *max_y)
  99. {
  100. u8 *buf;
  101. int error;
  102. /* select resolution register */
  103. error = st1232_ts_read_data(ts, REG_XY_RESOLUTION, 3);
  104. if (error)
  105. return error;
  106. buf = ts->read_buf;
  107. *max_x = (((buf[0] & 0x0070) << 4) | buf[1]) - 1;
  108. *max_y = (((buf[0] & 0x0007) << 8) | buf[2]) - 1;
  109. return 0;
  110. }
  111. static int st1232_ts_parse_and_report(struct st1232_ts_data *ts)
  112. {
  113. struct input_dev *input = ts->input_dev;
  114. struct input_mt_pos pos[ST_TS_MAX_FINGERS];
  115. u8 z[ST_TS_MAX_FINGERS];
  116. int slots[ST_TS_MAX_FINGERS];
  117. int n_contacts = 0;
  118. int i;
  119. for (i = 0; i < ts->chip_info->max_fingers; i++) {
  120. u8 *buf = &ts->read_buf[i * 4];
  121. if (buf[0] & BIT(7)) {
  122. unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
  123. unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
  124. touchscreen_set_mt_pos(&pos[n_contacts],
  125. &ts->prop, x, y);
  126. /* st1232 includes a z-axis / touch strength */
  127. if (ts->chip_info->have_z)
  128. z[n_contacts] = ts->read_buf[i + 6];
  129. n_contacts++;
  130. }
  131. }
  132. input_mt_assign_slots(input, slots, pos, n_contacts, 0);
  133. for (i = 0; i < n_contacts; i++) {
  134. if (touch_overlay_process_contact(&ts->touch_overlay_list,
  135. input, &pos[i], slots[i]))
  136. continue;
  137. input_mt_slot(input, slots[i]);
  138. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  139. input_report_abs(input, ABS_MT_POSITION_X, pos[i].x);
  140. input_report_abs(input, ABS_MT_POSITION_Y, pos[i].y);
  141. if (ts->chip_info->have_z)
  142. input_report_abs(input, ABS_MT_TOUCH_MAJOR, z[i]);
  143. }
  144. touch_overlay_sync_frame(&ts->touch_overlay_list, input);
  145. input_mt_sync_frame(input);
  146. input_sync(input);
  147. return n_contacts;
  148. }
  149. static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
  150. {
  151. struct st1232_ts_data *ts = dev_id;
  152. int count;
  153. int error;
  154. error = st1232_ts_read_data(ts, REG_XY_COORDINATES, ts->read_buf_len);
  155. if (error)
  156. goto out;
  157. count = st1232_ts_parse_and_report(ts);
  158. if (!count) {
  159. if (ts->low_latency_req.dev) {
  160. dev_pm_qos_remove_request(&ts->low_latency_req);
  161. ts->low_latency_req.dev = NULL;
  162. }
  163. } else if (!ts->low_latency_req.dev) {
  164. /* First contact, request 100 us latency. */
  165. dev_pm_qos_add_ancestor_request(&ts->client->dev,
  166. &ts->low_latency_req,
  167. DEV_PM_QOS_RESUME_LATENCY, 100);
  168. }
  169. out:
  170. return IRQ_HANDLED;
  171. }
  172. static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
  173. {
  174. if (ts->reset_gpio)
  175. gpiod_set_value_cansleep(ts->reset_gpio, !poweron);
  176. }
  177. static void st1232_ts_power_off(void *data)
  178. {
  179. st1232_ts_power(data, false);
  180. }
  181. static const struct st_chip_info st1232_chip_info = {
  182. .have_z = true,
  183. .max_area = 0xff,
  184. .max_fingers = 2,
  185. };
  186. static const struct st_chip_info st1633_chip_info = {
  187. .have_z = false,
  188. .max_area = 0x00,
  189. .max_fingers = 5,
  190. };
  191. static int st1232_ts_probe(struct i2c_client *client)
  192. {
  193. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  194. const struct st_chip_info *match;
  195. struct st1232_ts_data *ts;
  196. struct input_dev *input_dev;
  197. u16 max_x, max_y;
  198. int error;
  199. match = device_get_match_data(&client->dev);
  200. if (!match && id)
  201. match = (const void *)id->driver_data;
  202. if (!match) {
  203. dev_err(&client->dev, "unknown device model\n");
  204. return -ENODEV;
  205. }
  206. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  207. dev_err(&client->dev, "need I2C_FUNC_I2C\n");
  208. return -EIO;
  209. }
  210. if (!client->irq) {
  211. dev_err(&client->dev, "no IRQ?\n");
  212. return -EINVAL;
  213. }
  214. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  215. if (!ts)
  216. return -ENOMEM;
  217. ts->chip_info = match;
  218. /* allocate a buffer according to the number of registers to read */
  219. ts->read_buf_len = ts->chip_info->max_fingers * 4;
  220. ts->read_buf = devm_kzalloc(&client->dev, ts->read_buf_len, GFP_KERNEL);
  221. if (!ts->read_buf)
  222. return -ENOMEM;
  223. input_dev = devm_input_allocate_device(&client->dev);
  224. if (!input_dev)
  225. return -ENOMEM;
  226. ts->client = client;
  227. ts->input_dev = input_dev;
  228. ts->reset_gpio = devm_gpiod_get_optional(&client->dev, NULL,
  229. GPIOD_OUT_HIGH);
  230. if (IS_ERR(ts->reset_gpio)) {
  231. error = PTR_ERR(ts->reset_gpio);
  232. dev_err(&client->dev, "Unable to request GPIO pin: %d.\n",
  233. error);
  234. return error;
  235. }
  236. st1232_ts_power(ts, true);
  237. error = devm_add_action_or_reset(&client->dev, st1232_ts_power_off, ts);
  238. if (error) {
  239. dev_err(&client->dev,
  240. "Failed to install power off action: %d\n", error);
  241. return error;
  242. }
  243. input_dev->name = "st1232-touchscreen";
  244. input_dev->id.bustype = BUS_I2C;
  245. /* Wait until device is ready */
  246. error = st1232_ts_wait_ready(ts);
  247. if (error)
  248. return error;
  249. if (ts->chip_info->have_z)
  250. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
  251. ts->chip_info->max_area, 0, 0);
  252. /* map overlay objects if defined in the device tree */
  253. INIT_LIST_HEAD(&ts->touch_overlay_list);
  254. error = touch_overlay_map(&ts->touch_overlay_list, input_dev);
  255. if (error)
  256. return error;
  257. if (touch_overlay_mapped_touchscreen(&ts->touch_overlay_list)) {
  258. /* Read resolution from the overlay touchscreen if defined */
  259. touch_overlay_get_touchscreen_abs(&ts->touch_overlay_list,
  260. &max_x, &max_y);
  261. } else {
  262. /* Read resolution from the chip */
  263. error = st1232_ts_read_resolution(ts, &max_x, &max_y);
  264. if (error) {
  265. dev_err(&client->dev,
  266. "Failed to read resolution: %d\n", error);
  267. return error;
  268. }
  269. }
  270. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  271. 0, max_x, 0, 0);
  272. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  273. 0, max_y, 0, 0);
  274. touchscreen_parse_properties(input_dev, true, &ts->prop);
  275. error = input_mt_init_slots(input_dev, ts->chip_info->max_fingers,
  276. INPUT_MT_DIRECT | INPUT_MT_TRACK |
  277. INPUT_MT_DROP_UNUSED);
  278. if (error) {
  279. dev_err(&client->dev, "failed to initialize MT slots\n");
  280. return error;
  281. }
  282. error = devm_request_threaded_irq(&client->dev, client->irq,
  283. NULL, st1232_ts_irq_handler,
  284. IRQF_ONESHOT,
  285. client->name, ts);
  286. if (error) {
  287. dev_err(&client->dev, "Failed to register interrupt\n");
  288. return error;
  289. }
  290. error = input_register_device(ts->input_dev);
  291. if (error) {
  292. dev_err(&client->dev, "Unable to register %s input device\n",
  293. input_dev->name);
  294. return error;
  295. }
  296. i2c_set_clientdata(client, ts);
  297. return 0;
  298. }
  299. static int st1232_ts_suspend(struct device *dev)
  300. {
  301. struct i2c_client *client = to_i2c_client(dev);
  302. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  303. disable_irq(client->irq);
  304. if (!device_may_wakeup(&client->dev))
  305. st1232_ts_power(ts, false);
  306. return 0;
  307. }
  308. static int st1232_ts_resume(struct device *dev)
  309. {
  310. struct i2c_client *client = to_i2c_client(dev);
  311. struct st1232_ts_data *ts = i2c_get_clientdata(client);
  312. if (!device_may_wakeup(&client->dev))
  313. st1232_ts_power(ts, true);
  314. enable_irq(client->irq);
  315. return 0;
  316. }
  317. static DEFINE_SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
  318. st1232_ts_suspend, st1232_ts_resume);
  319. static const struct i2c_device_id st1232_ts_id[] = {
  320. { ST1232_TS_NAME, (unsigned long)&st1232_chip_info },
  321. { ST1633_TS_NAME, (unsigned long)&st1633_chip_info },
  322. { }
  323. };
  324. MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
  325. static const struct of_device_id st1232_ts_dt_ids[] = {
  326. { .compatible = "sitronix,st1232", .data = &st1232_chip_info },
  327. { .compatible = "sitronix,st1633", .data = &st1633_chip_info },
  328. { }
  329. };
  330. MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
  331. static struct i2c_driver st1232_ts_driver = {
  332. .probe = st1232_ts_probe,
  333. .id_table = st1232_ts_id,
  334. .driver = {
  335. .name = ST1232_TS_NAME,
  336. .of_match_table = st1232_ts_dt_ids,
  337. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  338. .pm = pm_sleep_ptr(&st1232_ts_pm_ops),
  339. },
  340. };
  341. module_i2c_driver(st1232_ts_driver);
  342. MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
  343. MODULE_AUTHOR("Martin Kepplinger <martin.kepplinger@ginzinger.com>");
  344. MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
  345. MODULE_LICENSE("GPL v2");