tsc2007.c 9.6 KB

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