tsc2007.c 12 KB

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