tsc2007.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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/i2c/tsc2007.h>
  28. #define TS_POLL_DELAY 1 /* ms delay between samples */
  29. #define TS_POLL_PERIOD 1 /* ms delay between samples */
  30. #define TSC2007_MEASURE_TEMP0 (0x0 << 4)
  31. #define TSC2007_MEASURE_AUX (0x2 << 4)
  32. #define TSC2007_MEASURE_TEMP1 (0x4 << 4)
  33. #define TSC2007_ACTIVATE_XN (0x8 << 4)
  34. #define TSC2007_ACTIVATE_YN (0x9 << 4)
  35. #define TSC2007_ACTIVATE_YP_XN (0xa << 4)
  36. #define TSC2007_SETUP (0xb << 4)
  37. #define TSC2007_MEASURE_X (0xc << 4)
  38. #define TSC2007_MEASURE_Y (0xd << 4)
  39. #define TSC2007_MEASURE_Z1 (0xe << 4)
  40. #define TSC2007_MEASURE_Z2 (0xf << 4)
  41. #define TSC2007_POWER_OFF_IRQ_EN (0x0 << 2)
  42. #define TSC2007_ADC_ON_IRQ_DIS0 (0x1 << 2)
  43. #define TSC2007_ADC_OFF_IRQ_EN (0x2 << 2)
  44. #define TSC2007_ADC_ON_IRQ_DIS1 (0x3 << 2)
  45. #define TSC2007_12BIT (0x0 << 1)
  46. #define TSC2007_8BIT (0x1 << 1)
  47. #define MAX_12BIT ((1 << 12) - 1)
  48. #define ADC_ON_12BIT (TSC2007_12BIT | TSC2007_ADC_ON_IRQ_DIS0)
  49. #define READ_Y (ADC_ON_12BIT | TSC2007_MEASURE_Y)
  50. #define READ_Z1 (ADC_ON_12BIT | TSC2007_MEASURE_Z1)
  51. #define READ_Z2 (ADC_ON_12BIT | TSC2007_MEASURE_Z2)
  52. #define READ_X (ADC_ON_12BIT | TSC2007_MEASURE_X)
  53. #define PWRDOWN (TSC2007_12BIT | TSC2007_POWER_OFF_IRQ_EN)
  54. struct ts_event {
  55. u16 x;
  56. u16 y;
  57. u16 z1, z2;
  58. };
  59. struct tsc2007 {
  60. struct input_dev *input;
  61. char phys[32];
  62. struct delayed_work work;
  63. struct i2c_client *client;
  64. u16 model;
  65. u16 x_plate_ohms;
  66. bool pendown;
  67. int irq;
  68. int (*get_pendown_state)(void);
  69. void (*clear_penirq)(void);
  70. };
  71. static inline int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd)
  72. {
  73. s32 data;
  74. u16 val;
  75. data = i2c_smbus_read_word_data(tsc->client, cmd);
  76. if (data < 0) {
  77. dev_err(&tsc->client->dev, "i2c io error: %d\n", data);
  78. return data;
  79. }
  80. /* The protocol and raw data format from i2c interface:
  81. * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
  82. * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit].
  83. */
  84. val = swab16(data) >> 4;
  85. dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val);
  86. return val;
  87. }
  88. static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc)
  89. {
  90. /* y- still on; turn on only y+ (and ADC) */
  91. tc->y = tsc2007_xfer(tsc, READ_Y);
  92. /* turn y- off, x+ on, then leave in lowpower */
  93. tc->x = tsc2007_xfer(tsc, READ_X);
  94. /* turn y+ off, x- on; we'll use formula #1 */
  95. tc->z1 = tsc2007_xfer(tsc, READ_Z1);
  96. tc->z2 = tsc2007_xfer(tsc, READ_Z2);
  97. /* Prepare for next touch reading - power down ADC, enable PENIRQ */
  98. tsc2007_xfer(tsc, PWRDOWN);
  99. }
  100. static u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, struct ts_event *tc)
  101. {
  102. u32 rt = 0;
  103. /* range filtering */
  104. if (tc->x == MAX_12BIT)
  105. tc->x = 0;
  106. if (likely(tc->x && tc->z1)) {
  107. /* compute touch pressure resistance using equation #1 */
  108. rt = tc->z2 - tc->z1;
  109. rt *= tc->x;
  110. rt *= tsc->x_plate_ohms;
  111. rt /= tc->z1;
  112. rt = (rt + 2047) >> 12;
  113. }
  114. return rt;
  115. }
  116. static void tsc2007_send_up_event(struct tsc2007 *tsc)
  117. {
  118. struct input_dev *input = tsc->input;
  119. dev_dbg(&tsc->client->dev, "UP\n");
  120. input_report_key(input, BTN_TOUCH, 0);
  121. input_report_abs(input, ABS_PRESSURE, 0);
  122. input_sync(input);
  123. }
  124. static void tsc2007_work(struct work_struct *work)
  125. {
  126. struct tsc2007 *ts =
  127. container_of(to_delayed_work(work), struct tsc2007, work);
  128. struct ts_event tc;
  129. u32 rt;
  130. /*
  131. * NOTE: We can't rely on the pressure to determine the pen down
  132. * state, even though this controller has a pressure sensor.
  133. * The pressure value can fluctuate for quite a while after
  134. * lifting the pen and in some cases may not even settle at the
  135. * expected value.
  136. *
  137. * The only safe way to check for the pen up condition is in the
  138. * work function by reading the pen signal state (it's a GPIO
  139. * and IRQ). Unfortunately such callback is not always available,
  140. * in that case we have rely on the pressure anyway.
  141. */
  142. if (ts->get_pendown_state) {
  143. if (unlikely(!ts->get_pendown_state())) {
  144. tsc2007_send_up_event(ts);
  145. ts->pendown = false;
  146. goto out;
  147. }
  148. dev_dbg(&ts->client->dev, "pen is still down\n");
  149. }
  150. tsc2007_read_values(ts, &tc);
  151. rt = tsc2007_calculate_pressure(ts, &tc);
  152. if (rt > MAX_12BIT) {
  153. /*
  154. * Sample found inconsistent by debouncing or pressure is
  155. * beyond the maximum. Don't report it to user space,
  156. * repeat at least once more the measurement.
  157. */
  158. dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
  159. goto out;
  160. }
  161. if (rt) {
  162. struct input_dev *input = ts->input;
  163. if (!ts->pendown) {
  164. dev_dbg(&ts->client->dev, "DOWN\n");
  165. input_report_key(input, BTN_TOUCH, 1);
  166. ts->pendown = true;
  167. }
  168. input_report_abs(input, ABS_X, tc.x);
  169. input_report_abs(input, ABS_Y, tc.y);
  170. input_report_abs(input, ABS_PRESSURE, rt);
  171. input_sync(input);
  172. dev_dbg(&ts->client->dev, "point(%4d,%4d), pressure (%4u)\n",
  173. tc.x, tc.y, rt);
  174. } else if (!ts->get_pendown_state && ts->pendown) {
  175. /*
  176. * We don't have callback to check pendown state, so we
  177. * have to assume that since pressure reported is 0 the
  178. * pen was lifted up.
  179. */
  180. tsc2007_send_up_event(ts);
  181. ts->pendown = false;
  182. }
  183. out:
  184. if (ts->pendown)
  185. schedule_delayed_work(&ts->work,
  186. msecs_to_jiffies(TS_POLL_PERIOD));
  187. else
  188. enable_irq(ts->irq);
  189. }
  190. static irqreturn_t tsc2007_irq(int irq, void *handle)
  191. {
  192. struct tsc2007 *ts = handle;
  193. if (!ts->get_pendown_state || likely(ts->get_pendown_state())) {
  194. disable_irq_nosync(ts->irq);
  195. schedule_delayed_work(&ts->work,
  196. msecs_to_jiffies(TS_POLL_DELAY));
  197. }
  198. if (ts->clear_penirq)
  199. ts->clear_penirq();
  200. return IRQ_HANDLED;
  201. }
  202. static void tsc2007_free_irq(struct tsc2007 *ts)
  203. {
  204. free_irq(ts->irq, ts);
  205. if (cancel_delayed_work_sync(&ts->work)) {
  206. /*
  207. * Work was pending, therefore we need to enable
  208. * IRQ here to balance the disable_irq() done in the
  209. * interrupt handler.
  210. */
  211. enable_irq(ts->irq);
  212. }
  213. }
  214. static int __devinit tsc2007_probe(struct i2c_client *client,
  215. const struct i2c_device_id *id)
  216. {
  217. struct tsc2007 *ts;
  218. struct tsc2007_platform_data *pdata = pdata = client->dev.platform_data;
  219. struct input_dev *input_dev;
  220. int err;
  221. if (!pdata) {
  222. dev_err(&client->dev, "platform data is required!\n");
  223. return -EINVAL;
  224. }
  225. if (!i2c_check_functionality(client->adapter,
  226. I2C_FUNC_SMBUS_READ_WORD_DATA))
  227. return -EIO;
  228. ts = kzalloc(sizeof(struct tsc2007), GFP_KERNEL);
  229. input_dev = input_allocate_device();
  230. if (!ts || !input_dev) {
  231. err = -ENOMEM;
  232. goto err_free_mem;
  233. }
  234. ts->client = client;
  235. ts->irq = client->irq;
  236. ts->input = input_dev;
  237. INIT_DELAYED_WORK(&ts->work, tsc2007_work);
  238. ts->model = pdata->model;
  239. ts->x_plate_ohms = pdata->x_plate_ohms;
  240. ts->get_pendown_state = pdata->get_pendown_state;
  241. ts->clear_penirq = pdata->clear_penirq;
  242. snprintf(ts->phys, sizeof(ts->phys),
  243. "%s/input0", dev_name(&client->dev));
  244. input_dev->name = "TSC2007 Touchscreen";
  245. input_dev->phys = ts->phys;
  246. input_dev->id.bustype = BUS_I2C;
  247. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  248. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  249. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  250. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  251. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  252. if (pdata->init_platform_hw)
  253. pdata->init_platform_hw();
  254. err = request_irq(ts->irq, tsc2007_irq, 0,
  255. client->dev.driver->name, ts);
  256. if (err < 0) {
  257. dev_err(&client->dev, "irq %d busy?\n", ts->irq);
  258. goto err_free_mem;
  259. }
  260. /* Prepare for touch readings - power down ADC and enable PENIRQ */
  261. err = tsc2007_xfer(ts, PWRDOWN);
  262. if (err < 0)
  263. goto err_free_irq;
  264. err = input_register_device(input_dev);
  265. if (err)
  266. goto err_free_irq;
  267. i2c_set_clientdata(client, ts);
  268. return 0;
  269. err_free_irq:
  270. tsc2007_free_irq(ts);
  271. if (pdata->exit_platform_hw)
  272. pdata->exit_platform_hw();
  273. err_free_mem:
  274. input_free_device(input_dev);
  275. kfree(ts);
  276. return err;
  277. }
  278. static int __devexit tsc2007_remove(struct i2c_client *client)
  279. {
  280. struct tsc2007 *ts = i2c_get_clientdata(client);
  281. struct tsc2007_platform_data *pdata = client->dev.platform_data;
  282. tsc2007_free_irq(ts);
  283. if (pdata->exit_platform_hw)
  284. pdata->exit_platform_hw();
  285. input_unregister_device(ts->input);
  286. kfree(ts);
  287. return 0;
  288. }
  289. static struct i2c_device_id tsc2007_idtable[] = {
  290. { "tsc2007", 0 },
  291. { }
  292. };
  293. MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
  294. static struct i2c_driver tsc2007_driver = {
  295. .driver = {
  296. .owner = THIS_MODULE,
  297. .name = "tsc2007"
  298. },
  299. .id_table = tsc2007_idtable,
  300. .probe = tsc2007_probe,
  301. .remove = __devexit_p(tsc2007_remove),
  302. };
  303. static int __init tsc2007_init(void)
  304. {
  305. return i2c_add_driver(&tsc2007_driver);
  306. }
  307. static void __exit tsc2007_exit(void)
  308. {
  309. i2c_del_driver(&tsc2007_driver);
  310. }
  311. module_init(tsc2007_init);
  312. module_exit(tsc2007_exit);
  313. MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
  314. MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
  315. MODULE_LICENSE("GPL");