st1232.c 9.4 KB

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