tsc2007_core.c 10 KB

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