tsc2007.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 i2c_client *client;
  61. u16 model;
  62. u16 x_plate_ohms;
  63. u16 max_rt;
  64. unsigned long poll_delay;
  65. unsigned long poll_period;
  66. int irq;
  67. wait_queue_head_t wait;
  68. bool stopped;
  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 bool tsc2007_is_pen_down(struct tsc2007 *ts)
  118. {
  119. /*
  120. * NOTE: We can't rely on the pressure to determine the pen down
  121. * state, even though this controller has a pressure sensor.
  122. * The pressure value can fluctuate for quite a while after
  123. * lifting the pen and in some cases may not even settle at the
  124. * 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
  128. * and IRQ). Unfortunately such callback is not always available,
  129. * in that case we assume that the pen is down and expect caller
  130. * to fall back on the pressure reading.
  131. */
  132. if (!ts->get_pendown_state)
  133. return true;
  134. return ts->get_pendown_state();
  135. }
  136. static irqreturn_t tsc2007_soft_irq(int irq, void *handle)
  137. {
  138. struct tsc2007 *ts = handle;
  139. struct input_dev *input = ts->input;
  140. struct ts_event tc;
  141. u32 rt;
  142. while (!ts->stopped && tsc2007_is_pen_down(ts)) {
  143. /* pen is down, continue with the measurement */
  144. tsc2007_read_values(ts, &tc);
  145. rt = tsc2007_calculate_pressure(ts, &tc);
  146. if (rt == 0 && !ts->get_pendown_state) {
  147. /*
  148. * If pressure reported is 0 and we don't have
  149. * callback to check pendown state, we have to
  150. * assume that pen was lifted up.
  151. */
  152. break;
  153. }
  154. if (rt <= ts->max_rt) {
  155. dev_dbg(&ts->client->dev,
  156. "DOWN point(%4d,%4d), pressure (%4u)\n",
  157. tc.x, tc.y, rt);
  158. input_report_key(input, BTN_TOUCH, 1);
  159. input_report_abs(input, ABS_X, tc.x);
  160. input_report_abs(input, ABS_Y, tc.y);
  161. input_report_abs(input, ABS_PRESSURE, rt);
  162. input_sync(input);
  163. } else {
  164. /*
  165. * Sample found inconsistent by debouncing or pressure is
  166. * beyond the maximum. Don't report it to user space,
  167. * repeat at least once more the measurement.
  168. */
  169. dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
  170. }
  171. wait_event_timeout(ts->wait, ts->stopped,
  172. msecs_to_jiffies(ts->poll_period));
  173. }
  174. dev_dbg(&ts->client->dev, "UP\n");
  175. input_report_key(input, BTN_TOUCH, 0);
  176. input_report_abs(input, ABS_PRESSURE, 0);
  177. input_sync(input);
  178. if (ts->clear_penirq)
  179. ts->clear_penirq();
  180. return IRQ_HANDLED;
  181. }
  182. static irqreturn_t tsc2007_hard_irq(int irq, void *handle)
  183. {
  184. struct tsc2007 *ts = handle;
  185. if (!ts->get_pendown_state || likely(ts->get_pendown_state()))
  186. return IRQ_WAKE_THREAD;
  187. if (ts->clear_penirq)
  188. ts->clear_penirq();
  189. return IRQ_HANDLED;
  190. }
  191. static void tsc2007_stop(struct tsc2007 *ts)
  192. {
  193. ts->stopped = true;
  194. mb();
  195. wake_up(&ts->wait);
  196. disable_irq(ts->irq);
  197. }
  198. static int tsc2007_open(struct input_dev *input_dev)
  199. {
  200. struct tsc2007 *ts = input_get_drvdata(input_dev);
  201. int err;
  202. ts->stopped = false;
  203. mb();
  204. enable_irq(ts->irq);
  205. /* Prepare for touch readings - power down ADC and enable PENIRQ */
  206. err = tsc2007_xfer(ts, PWRDOWN);
  207. if (err < 0) {
  208. tsc2007_stop(ts);
  209. return err;
  210. }
  211. return 0;
  212. }
  213. static void tsc2007_close(struct input_dev *input_dev)
  214. {
  215. struct tsc2007 *ts = input_get_drvdata(input_dev);
  216. tsc2007_stop(ts);
  217. }
  218. static int __devinit tsc2007_probe(struct i2c_client *client,
  219. const struct i2c_device_id *id)
  220. {
  221. struct tsc2007 *ts;
  222. struct tsc2007_platform_data *pdata = client->dev.platform_data;
  223. struct input_dev *input_dev;
  224. int err;
  225. if (!pdata) {
  226. dev_err(&client->dev, "platform data is required!\n");
  227. return -EINVAL;
  228. }
  229. if (!i2c_check_functionality(client->adapter,
  230. I2C_FUNC_SMBUS_READ_WORD_DATA))
  231. return -EIO;
  232. ts = kzalloc(sizeof(struct tsc2007), GFP_KERNEL);
  233. input_dev = input_allocate_device();
  234. if (!ts || !input_dev) {
  235. err = -ENOMEM;
  236. goto err_free_mem;
  237. }
  238. ts->client = client;
  239. ts->irq = client->irq;
  240. ts->input = input_dev;
  241. init_waitqueue_head(&ts->wait);
  242. ts->model = pdata->model;
  243. ts->x_plate_ohms = pdata->x_plate_ohms;
  244. ts->max_rt = pdata->max_rt ? : MAX_12BIT;
  245. ts->poll_delay = pdata->poll_delay ? : 1;
  246. ts->poll_period = pdata->poll_period ? : 1;
  247. ts->get_pendown_state = pdata->get_pendown_state;
  248. ts->clear_penirq = pdata->clear_penirq;
  249. if (pdata->x_plate_ohms == 0) {
  250. dev_err(&client->dev, "x_plate_ohms is not set up in platform data");
  251. err = -EINVAL;
  252. goto err_free_mem;
  253. }
  254. snprintf(ts->phys, sizeof(ts->phys),
  255. "%s/input0", dev_name(&client->dev));
  256. input_dev->name = "TSC2007 Touchscreen";
  257. input_dev->phys = ts->phys;
  258. input_dev->id.bustype = BUS_I2C;
  259. input_dev->open = tsc2007_open;
  260. input_dev->close = tsc2007_close;
  261. input_set_drvdata(input_dev, ts);
  262. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  263. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  264. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, pdata->fuzzx, 0);
  265. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, pdata->fuzzy, 0);
  266. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
  267. pdata->fuzzz, 0);
  268. if (pdata->init_platform_hw)
  269. pdata->init_platform_hw();
  270. err = request_threaded_irq(ts->irq, tsc2007_hard_irq, tsc2007_soft_irq,
  271. IRQF_ONESHOT, client->dev.driver->name, ts);
  272. if (err < 0) {
  273. dev_err(&client->dev, "irq %d busy?\n", ts->irq);
  274. goto err_free_mem;
  275. }
  276. tsc2007_stop(ts);
  277. err = input_register_device(input_dev);
  278. if (err)
  279. goto err_free_irq;
  280. i2c_set_clientdata(client, ts);
  281. return 0;
  282. err_free_irq:
  283. free_irq(ts->irq, ts);
  284. if (pdata->exit_platform_hw)
  285. pdata->exit_platform_hw();
  286. err_free_mem:
  287. input_free_device(input_dev);
  288. kfree(ts);
  289. return err;
  290. }
  291. static int __devexit tsc2007_remove(struct i2c_client *client)
  292. {
  293. struct tsc2007 *ts = i2c_get_clientdata(client);
  294. struct tsc2007_platform_data *pdata = client->dev.platform_data;
  295. free_irq(ts->irq, ts);
  296. if (pdata->exit_platform_hw)
  297. pdata->exit_platform_hw();
  298. input_unregister_device(ts->input);
  299. kfree(ts);
  300. return 0;
  301. }
  302. static const struct i2c_device_id tsc2007_idtable[] = {
  303. { "tsc2007", 0 },
  304. { }
  305. };
  306. MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
  307. static struct i2c_driver tsc2007_driver = {
  308. .driver = {
  309. .owner = THIS_MODULE,
  310. .name = "tsc2007"
  311. },
  312. .id_table = tsc2007_idtable,
  313. .probe = tsc2007_probe,
  314. .remove = __devexit_p(tsc2007_remove),
  315. };
  316. module_i2c_driver(tsc2007_driver);
  317. MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
  318. MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
  319. MODULE_LICENSE("GPL");