tsc2007.c 9.0 KB

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