tsc2007.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 ts_event tc;
  64. struct i2c_client *client;
  65. u16 model;
  66. u16 x_plate_ohms;
  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_send_event(void *tsc)
  90. {
  91. struct tsc2007 *ts = tsc;
  92. u32 rt;
  93. u16 x, y, z1, z2;
  94. x = ts->tc.x;
  95. y = ts->tc.y;
  96. z1 = ts->tc.z1;
  97. z2 = ts->tc.z2;
  98. /* range filtering */
  99. if (x == MAX_12BIT)
  100. x = 0;
  101. if (likely(x && z1)) {
  102. /* compute touch pressure resistance using equation #1 */
  103. rt = z2;
  104. rt -= z1;
  105. rt *= x;
  106. rt *= ts->x_plate_ohms;
  107. rt /= z1;
  108. rt = (rt + 2047) >> 12;
  109. } else
  110. rt = 0;
  111. /*
  112. * Sample found inconsistent by debouncing or pressure is beyond
  113. * the maximum. Don't report it to user space, repeat at least
  114. * once more the measurement
  115. */
  116. if (rt > MAX_12BIT) {
  117. dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
  118. return;
  119. }
  120. /*
  121. * NOTE: We can't rely on the pressure to determine the pen down
  122. * state, even this controller has a pressure sensor. The pressure
  123. * value can fluctuate for quite a while after lifting the pen and
  124. * in some cases may not even settle at the expected value.
  125. *
  126. * The only safe way to check for the pen up condition is in the
  127. * work function by reading the pen signal state (it's a GPIO and IRQ).
  128. */
  129. if (rt) {
  130. struct input_dev *input = ts->input;
  131. if (!ts->pendown) {
  132. dev_dbg(&ts->client->dev, "DOWN\n");
  133. input_report_key(input, BTN_TOUCH, 1);
  134. ts->pendown = true;
  135. }
  136. input_report_abs(input, ABS_X, x);
  137. input_report_abs(input, ABS_Y, y);
  138. input_report_abs(input, ABS_PRESSURE, rt);
  139. input_sync(input);
  140. dev_dbg(&ts->client->dev, "point(%4d,%4d), pressure (%4u)\n",
  141. x, y, rt);
  142. }
  143. }
  144. static int tsc2007_read_values(struct tsc2007 *tsc)
  145. {
  146. /* y- still on; turn on only y+ (and ADC) */
  147. tsc->tc.y = tsc2007_xfer(tsc, READ_Y);
  148. /* turn y- off, x+ on, then leave in lowpower */
  149. tsc->tc.x = tsc2007_xfer(tsc, READ_X);
  150. /* turn y+ off, x- on; we'll use formula #1 */
  151. tsc->tc.z1 = tsc2007_xfer(tsc, READ_Z1);
  152. tsc->tc.z2 = tsc2007_xfer(tsc, READ_Z2);
  153. /* power down */
  154. tsc2007_xfer(tsc, PWRDOWN);
  155. return 0;
  156. }
  157. static void tsc2007_work(struct work_struct *work)
  158. {
  159. struct tsc2007 *ts =
  160. container_of(to_delayed_work(work), struct tsc2007, work);
  161. if (unlikely(!ts->get_pendown_state() && ts->pendown)) {
  162. struct input_dev *input = ts->input;
  163. dev_dbg(&ts->client->dev, "UP\n");
  164. input_report_key(input, BTN_TOUCH, 0);
  165. input_report_abs(input, ABS_PRESSURE, 0);
  166. input_sync(input);
  167. ts->pendown = false;
  168. enable_irq(ts->irq);
  169. } else {
  170. /* pen is still down, continue with the measurement */
  171. dev_dbg(&ts->client->dev, "pen is still down\n");
  172. tsc2007_read_values(ts);
  173. tsc2007_send_event(ts);
  174. schedule_delayed_work(&ts->work,
  175. msecs_to_jiffies(TS_POLL_PERIOD));
  176. }
  177. }
  178. static irqreturn_t tsc2007_irq(int irq, void *handle)
  179. {
  180. struct tsc2007 *ts = handle;
  181. if (likely(ts->get_pendown_state())) {
  182. disable_irq_nosync(ts->irq);
  183. schedule_delayed_work(&ts->work,
  184. msecs_to_jiffies(TS_POLL_DELAY));
  185. }
  186. if (ts->clear_penirq)
  187. ts->clear_penirq();
  188. return IRQ_HANDLED;
  189. }
  190. static void tsc2007_free_irq(struct tsc2007 *ts)
  191. {
  192. free_irq(ts->irq, ts);
  193. if (cancel_delayed_work_sync(&ts->work)) {
  194. /*
  195. * Work was pending, therefore we need to enable
  196. * IRQ here to balance the disable_irq() done in the
  197. * interrupt handler.
  198. */
  199. enable_irq(ts->irq);
  200. }
  201. }
  202. static int __devinit tsc2007_probe(struct i2c_client *client,
  203. const struct i2c_device_id *id)
  204. {
  205. struct tsc2007 *ts;
  206. struct tsc2007_platform_data *pdata = pdata = client->dev.platform_data;
  207. struct input_dev *input_dev;
  208. int err;
  209. if (!pdata || !pdata->get_pendown_state) {
  210. dev_err(&client->dev, "platform data is required!\n");
  211. return -EINVAL;
  212. }
  213. if (!i2c_check_functionality(client->adapter,
  214. I2C_FUNC_SMBUS_READ_WORD_DATA))
  215. return -EIO;
  216. ts = kzalloc(sizeof(struct tsc2007), GFP_KERNEL);
  217. input_dev = input_allocate_device();
  218. if (!ts || !input_dev) {
  219. err = -ENOMEM;
  220. goto err_free_mem;
  221. }
  222. ts->client = client;
  223. ts->irq = client->irq;
  224. ts->input = input_dev;
  225. INIT_DELAYED_WORK(&ts->work, tsc2007_work);
  226. ts->model = pdata->model;
  227. ts->x_plate_ohms = pdata->x_plate_ohms;
  228. ts->get_pendown_state = pdata->get_pendown_state;
  229. ts->clear_penirq = pdata->clear_penirq;
  230. snprintf(ts->phys, sizeof(ts->phys),
  231. "%s/input0", dev_name(&client->dev));
  232. input_dev->name = "TSC2007 Touchscreen";
  233. input_dev->phys = ts->phys;
  234. input_dev->id.bustype = BUS_I2C;
  235. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  236. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  237. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  238. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  239. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  240. if (pdata->init_platform_hw)
  241. pdata->init_platform_hw();
  242. err = request_irq(ts->irq, tsc2007_irq, 0,
  243. client->dev.driver->name, ts);
  244. if (err < 0) {
  245. dev_err(&client->dev, "irq %d busy?\n", ts->irq);
  246. goto err_free_mem;
  247. }
  248. err = input_register_device(input_dev);
  249. if (err)
  250. goto err_free_irq;
  251. i2c_set_clientdata(client, ts);
  252. tsc2007_read_values(ts);
  253. return 0;
  254. err_free_irq:
  255. tsc2007_free_irq(ts);
  256. if (pdata->exit_platform_hw)
  257. pdata->exit_platform_hw();
  258. err_free_mem:
  259. input_free_device(input_dev);
  260. kfree(ts);
  261. return err;
  262. }
  263. static int __devexit tsc2007_remove(struct i2c_client *client)
  264. {
  265. struct tsc2007 *ts = i2c_get_clientdata(client);
  266. struct tsc2007_platform_data *pdata = client->dev.platform_data;
  267. tsc2007_free_irq(ts);
  268. if (pdata->exit_platform_hw)
  269. pdata->exit_platform_hw();
  270. input_unregister_device(ts->input);
  271. kfree(ts);
  272. return 0;
  273. }
  274. static struct i2c_device_id tsc2007_idtable[] = {
  275. { "tsc2007", 0 },
  276. { }
  277. };
  278. MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
  279. static struct i2c_driver tsc2007_driver = {
  280. .driver = {
  281. .owner = THIS_MODULE,
  282. .name = "tsc2007"
  283. },
  284. .id_table = tsc2007_idtable,
  285. .probe = tsc2007_probe,
  286. .remove = __devexit_p(tsc2007_remove),
  287. };
  288. static int __init tsc2007_init(void)
  289. {
  290. return i2c_add_driver(&tsc2007_driver);
  291. }
  292. static void __exit tsc2007_exit(void)
  293. {
  294. i2c_del_driver(&tsc2007_driver);
  295. }
  296. module_init(tsc2007_init);
  297. module_exit(tsc2007_exit);
  298. MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
  299. MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
  300. MODULE_LICENSE("GPL");