tsc2007_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/input/touchscreen/tsc2007.c
  4. *
  5. * Copyright (c) 2008 MtekVision Co., Ltd.
  6. * Kwangwoo Lee <kwlee@mtekvision.com>
  7. *
  8. * Using code from:
  9. * - ads7846.c
  10. * Copyright (c) 2005 David Brownell
  11. * Copyright (c) 2006 Nokia Corporation
  12. * - corgi_ts.c
  13. * Copyright (C) 2004-2005 Richard Purdie
  14. * - omap_ts.[hc], ads7846.h, ts_osk.c
  15. * Copyright (C) 2002 MontaVista Software
  16. * Copyright (C) 2004 Texas Instruments
  17. * Copyright (C) 2005 Dirk Behme
  18. */
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/input.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/i2c.h>
  25. #include <linux/math64.h>
  26. #include <linux/mod_devicetable.h>
  27. #include <linux/property.h>
  28. #include <linux/platform_data/tsc2007.h>
  29. #include "tsc2007.h"
  30. int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd)
  31. {
  32. s32 data;
  33. u16 val;
  34. data = i2c_smbus_read_word_data(tsc->client, cmd);
  35. if (data < 0) {
  36. dev_err(&tsc->client->dev, "i2c io error: %d\n", data);
  37. return data;
  38. }
  39. /* The protocol and raw data format from i2c interface:
  40. * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
  41. * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit].
  42. */
  43. val = swab16(data) >> 4;
  44. dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val);
  45. return val;
  46. }
  47. static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc)
  48. {
  49. /* y- still on; turn on only y+ (and ADC) */
  50. tc->y = tsc2007_xfer(tsc, READ_Y);
  51. /* turn y- off, x+ on, then leave in lowpower */
  52. tc->x = tsc2007_xfer(tsc, READ_X);
  53. /* turn y+ off, x- on; we'll use formula #1 */
  54. tc->z1 = tsc2007_xfer(tsc, READ_Z1);
  55. tc->z2 = tsc2007_xfer(tsc, READ_Z2);
  56. /* Prepare for next touch reading - power down ADC, enable PENIRQ */
  57. tsc2007_xfer(tsc, PWRDOWN);
  58. }
  59. u32 tsc2007_calculate_resistance(struct tsc2007 *tsc, struct ts_event *tc)
  60. {
  61. u64 rt = 0;
  62. /* range filtering */
  63. if (tc->x == MAX_12BIT)
  64. tc->x = 0;
  65. if (likely(tc->x && tc->z1)) {
  66. /* compute touch resistance using equation #1 */
  67. rt = tc->z2 - tc->z1;
  68. rt *= tc->x;
  69. rt *= tsc->x_plate_ohms;
  70. rt = div_u64(rt, tc->z1);
  71. rt = (rt + 2047) >> 12;
  72. }
  73. if (rt > U32_MAX)
  74. return U32_MAX;
  75. return (u32) rt;
  76. }
  77. bool tsc2007_is_pen_down(struct tsc2007 *ts)
  78. {
  79. /*
  80. * NOTE: We can't rely on the pressure to determine the pen down
  81. * state, even though this controller has a pressure sensor.
  82. * The pressure value can fluctuate for quite a while after
  83. * lifting the pen and in some cases may not even settle at the
  84. * expected value.
  85. *
  86. * The only safe way to check for the pen up condition is in the
  87. * work function by reading the pen signal state (it's a GPIO
  88. * and IRQ). Unfortunately such callback is not always available,
  89. * in that case we assume that the pen is down and expect caller
  90. * to fall back on the pressure reading.
  91. */
  92. if (!ts->get_pendown_state)
  93. return true;
  94. return ts->get_pendown_state(&ts->client->dev);
  95. }
  96. static irqreturn_t tsc2007_soft_irq(int irq, void *handle)
  97. {
  98. struct tsc2007 *ts = handle;
  99. struct input_dev *input = ts->input;
  100. struct ts_event tc;
  101. u32 rt;
  102. while (!ts->stopped && tsc2007_is_pen_down(ts)) {
  103. /* pen is down, continue with the measurement */
  104. mutex_lock(&ts->mlock);
  105. tsc2007_read_values(ts, &tc);
  106. mutex_unlock(&ts->mlock);
  107. rt = tsc2007_calculate_resistance(ts, &tc);
  108. if (!rt && !ts->get_pendown_state) {
  109. /*
  110. * If pressure reported is 0 and we don't have
  111. * callback to check pendown state, we have to
  112. * assume that pen was lifted up.
  113. */
  114. break;
  115. }
  116. if (rt <= ts->max_rt) {
  117. dev_dbg(&ts->client->dev,
  118. "DOWN point(%4d,%4d), resistance (%4u)\n",
  119. tc.x, tc.y, rt);
  120. rt = ts->max_rt - rt;
  121. input_report_key(input, BTN_TOUCH, 1);
  122. touchscreen_report_pos(input, &ts->prop, tc.x, tc.y, false);
  123. input_report_abs(input, ABS_PRESSURE, rt);
  124. input_sync(input);
  125. } else {
  126. /*
  127. * Sample found inconsistent by debouncing or pressure is
  128. * beyond the maximum. Don't report it to user space,
  129. * repeat at least once more the measurement.
  130. */
  131. dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
  132. }
  133. wait_event_timeout(ts->wait, ts->stopped, ts->poll_period);
  134. }
  135. dev_dbg(&ts->client->dev, "UP\n");
  136. input_report_key(input, BTN_TOUCH, 0);
  137. input_report_abs(input, ABS_PRESSURE, 0);
  138. input_sync(input);
  139. if (ts->clear_penirq)
  140. ts->clear_penirq();
  141. return IRQ_HANDLED;
  142. }
  143. static void tsc2007_stop(struct tsc2007 *ts)
  144. {
  145. ts->stopped = true;
  146. mb();
  147. wake_up(&ts->wait);
  148. if (ts->irq)
  149. disable_irq(ts->irq);
  150. }
  151. static int tsc2007_open(struct input_dev *input_dev)
  152. {
  153. struct tsc2007 *ts = input_get_drvdata(input_dev);
  154. int err;
  155. ts->stopped = false;
  156. mb();
  157. if (ts->irq)
  158. enable_irq(ts->irq);
  159. /* Prepare for touch readings - power down ADC and enable PENIRQ */
  160. err = tsc2007_xfer(ts, PWRDOWN);
  161. if (err < 0) {
  162. tsc2007_stop(ts);
  163. return err;
  164. }
  165. return 0;
  166. }
  167. static void tsc2007_close(struct input_dev *input_dev)
  168. {
  169. struct tsc2007 *ts = input_get_drvdata(input_dev);
  170. tsc2007_stop(ts);
  171. }
  172. static int tsc2007_get_pendown_state_gpio(struct device *dev)
  173. {
  174. struct i2c_client *client = to_i2c_client(dev);
  175. struct tsc2007 *ts = i2c_get_clientdata(client);
  176. return gpiod_get_value_cansleep(ts->gpiod);
  177. }
  178. static int tsc2007_probe_properties(struct device *dev, struct tsc2007 *ts)
  179. {
  180. u32 val32;
  181. u64 val64;
  182. if (!device_property_read_u32(dev, "ti,max-rt", &val32))
  183. ts->max_rt = val32;
  184. else
  185. ts->max_rt = MAX_12BIT;
  186. if (!device_property_read_u32(dev, "ti,fuzzx", &val32))
  187. ts->fuzzx = val32;
  188. if (!device_property_read_u32(dev, "ti,fuzzy", &val32))
  189. ts->fuzzy = val32;
  190. if (!device_property_read_u32(dev, "ti,fuzzz", &val32))
  191. ts->fuzzz = val32;
  192. if (!device_property_read_u64(dev, "ti,poll-period", &val64))
  193. ts->poll_period = msecs_to_jiffies(val64);
  194. else
  195. ts->poll_period = msecs_to_jiffies(1);
  196. if (!device_property_read_u32(dev, "ti,x-plate-ohms", &val32)) {
  197. ts->x_plate_ohms = val32;
  198. } else {
  199. dev_err(dev, "Missing ti,x-plate-ohms device property\n");
  200. return -EINVAL;
  201. }
  202. ts->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
  203. if (IS_ERR(ts->gpiod))
  204. return PTR_ERR(ts->gpiod);
  205. if (ts->gpiod)
  206. ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
  207. else
  208. dev_dbg(dev, "Pen down GPIO is not specified in properties\n");
  209. return 0;
  210. }
  211. static int tsc2007_probe_pdev(struct device *dev, struct tsc2007 *ts,
  212. const struct tsc2007_platform_data *pdata,
  213. const struct i2c_device_id *id)
  214. {
  215. ts->model = pdata->model;
  216. ts->x_plate_ohms = pdata->x_plate_ohms;
  217. ts->max_rt = pdata->max_rt ? : MAX_12BIT;
  218. ts->poll_period = msecs_to_jiffies(pdata->poll_period ? : 1);
  219. ts->get_pendown_state = pdata->get_pendown_state;
  220. ts->clear_penirq = pdata->clear_penirq;
  221. ts->fuzzx = pdata->fuzzx;
  222. ts->fuzzy = pdata->fuzzy;
  223. ts->fuzzz = pdata->fuzzz;
  224. if (pdata->x_plate_ohms == 0) {
  225. dev_err(dev, "x_plate_ohms is not set up in platform data\n");
  226. return -EINVAL;
  227. }
  228. return 0;
  229. }
  230. static void tsc2007_call_exit_platform_hw(void *data)
  231. {
  232. struct device *dev = data;
  233. const struct tsc2007_platform_data *pdata = dev_get_platdata(dev);
  234. pdata->exit_platform_hw();
  235. }
  236. static int tsc2007_probe(struct i2c_client *client)
  237. {
  238. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  239. const struct tsc2007_platform_data *pdata =
  240. dev_get_platdata(&client->dev);
  241. struct tsc2007 *ts;
  242. struct input_dev *input_dev;
  243. int err;
  244. if (!i2c_check_functionality(client->adapter,
  245. I2C_FUNC_SMBUS_READ_WORD_DATA))
  246. return -EIO;
  247. ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
  248. if (!ts)
  249. return -ENOMEM;
  250. if (pdata)
  251. err = tsc2007_probe_pdev(&client->dev, ts, pdata, id);
  252. else
  253. err = tsc2007_probe_properties(&client->dev, ts);
  254. if (err)
  255. return err;
  256. input_dev = devm_input_allocate_device(&client->dev);
  257. if (!input_dev)
  258. return -ENOMEM;
  259. i2c_set_clientdata(client, ts);
  260. ts->client = client;
  261. ts->irq = client->irq;
  262. ts->input = input_dev;
  263. init_waitqueue_head(&ts->wait);
  264. mutex_init(&ts->mlock);
  265. snprintf(ts->phys, sizeof(ts->phys),
  266. "%s/input0", dev_name(&client->dev));
  267. input_dev->name = "TSC2007 Touchscreen";
  268. input_dev->phys = ts->phys;
  269. input_dev->id.bustype = BUS_I2C;
  270. input_dev->open = tsc2007_open;
  271. input_dev->close = tsc2007_close;
  272. input_set_drvdata(input_dev, ts);
  273. input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
  274. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
  275. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
  276. touchscreen_parse_properties(input_dev, false, &ts->prop);
  277. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
  278. ts->fuzzz, 0);
  279. if (pdata) {
  280. if (pdata->exit_platform_hw) {
  281. err = devm_add_action(&client->dev,
  282. tsc2007_call_exit_platform_hw,
  283. &client->dev);
  284. if (err) {
  285. dev_err(&client->dev,
  286. "Failed to register exit_platform_hw action, %d\n",
  287. err);
  288. return err;
  289. }
  290. }
  291. if (pdata->init_platform_hw)
  292. pdata->init_platform_hw();
  293. }
  294. if (ts->irq) {
  295. err = devm_request_threaded_irq(&client->dev, ts->irq,
  296. NULL, tsc2007_soft_irq,
  297. IRQF_ONESHOT,
  298. client->dev.driver->name, ts);
  299. if (err) {
  300. dev_err(&client->dev, "Failed to request irq %d: %d\n",
  301. ts->irq, err);
  302. return err;
  303. }
  304. tsc2007_stop(ts);
  305. }
  306. /* power down the chip (TSC2007_SETUP does not ACK on I2C) */
  307. err = tsc2007_xfer(ts, PWRDOWN);
  308. if (err < 0) {
  309. dev_err(&client->dev,
  310. "Failed to setup chip: %d\n", err);
  311. return err; /* chip does not respond */
  312. }
  313. err = input_register_device(input_dev);
  314. if (err) {
  315. dev_err(&client->dev,
  316. "Failed to register input device: %d\n", err);
  317. return err;
  318. }
  319. err = tsc2007_iio_configure(ts);
  320. if (err) {
  321. dev_err(&client->dev,
  322. "Failed to register with IIO: %d\n", err);
  323. return err;
  324. }
  325. return 0;
  326. }
  327. static const struct i2c_device_id tsc2007_idtable[] = {
  328. { "tsc2007" },
  329. { }
  330. };
  331. MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
  332. static const struct of_device_id tsc2007_of_match[] = {
  333. { .compatible = "ti,tsc2007" },
  334. { /* sentinel */ }
  335. };
  336. MODULE_DEVICE_TABLE(of, tsc2007_of_match);
  337. static struct i2c_driver tsc2007_driver = {
  338. .driver = {
  339. .name = "tsc2007",
  340. .of_match_table = tsc2007_of_match,
  341. },
  342. .id_table = tsc2007_idtable,
  343. .probe = tsc2007_probe,
  344. };
  345. module_i2c_driver(tsc2007_driver);
  346. MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
  347. MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
  348. MODULE_LICENSE("GPL");