tsc2007_core.c 11 KB

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