tsc2007.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/of_device.h>
  29. #include <linux/of.h>
  30. #include <linux/of_gpio.h>
  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 i2c_client *client;
  64. u16 model;
  65. u16 x_plate_ohms;
  66. u16 max_rt;
  67. unsigned long poll_period;
  68. int fuzzx;
  69. int fuzzy;
  70. int fuzzz;
  71. unsigned gpio;
  72. int irq;
  73. wait_queue_head_t wait;
  74. bool stopped;
  75. int (*get_pendown_state)(struct device *);
  76. void (*clear_penirq)(void);
  77. };
  78. static inline int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd)
  79. {
  80. s32 data;
  81. u16 val;
  82. data = i2c_smbus_read_word_data(tsc->client, cmd);
  83. if (data < 0) {
  84. dev_err(&tsc->client->dev, "i2c io error: %d\n", data);
  85. return data;
  86. }
  87. /* The protocol and raw data format from i2c interface:
  88. * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
  89. * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit].
  90. */
  91. val = swab16(data) >> 4;
  92. dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val);
  93. return val;
  94. }
  95. static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc)
  96. {
  97. /* y- still on; turn on only y+ (and ADC) */
  98. tc->y = tsc2007_xfer(tsc, READ_Y);
  99. /* turn y- off, x+ on, then leave in lowpower */
  100. tc->x = tsc2007_xfer(tsc, READ_X);
  101. /* turn y+ off, x- on; we'll use formula #1 */
  102. tc->z1 = tsc2007_xfer(tsc, READ_Z1);
  103. tc->z2 = tsc2007_xfer(tsc, READ_Z2);
  104. /* Prepare for next touch reading - power down ADC, enable PENIRQ */
  105. tsc2007_xfer(tsc, PWRDOWN);
  106. }
  107. static u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, struct ts_event *tc)
  108. {
  109. u32 rt = 0;
  110. /* range filtering */
  111. if (tc->x == MAX_12BIT)
  112. tc->x = 0;
  113. if (likely(tc->x && tc->z1)) {
  114. /* compute touch pressure resistance using equation #1 */
  115. rt = tc->z2 - tc->z1;
  116. rt *= tc->x;
  117. rt *= tsc->x_plate_ohms;
  118. rt /= tc->z1;
  119. rt = (rt + 2047) >> 12;
  120. }
  121. return rt;
  122. }
  123. static bool tsc2007_is_pen_down(struct tsc2007 *ts)
  124. {
  125. /*
  126. * NOTE: We can't rely on the pressure to determine the pen down
  127. * state, even though this controller has a pressure sensor.
  128. * The pressure value can fluctuate for quite a while after
  129. * lifting the pen and in some cases may not even settle at the
  130. * expected value.
  131. *
  132. * The only safe way to check for the pen up condition is in the
  133. * work function by reading the pen signal state (it's a GPIO
  134. * and IRQ). Unfortunately such callback is not always available,
  135. * in that case we assume that the pen is down and expect caller
  136. * to fall back on the pressure reading.
  137. */
  138. if (!ts->get_pendown_state)
  139. return true;
  140. return ts->get_pendown_state(&ts->client->dev);
  141. }
  142. static irqreturn_t tsc2007_soft_irq(int irq, void *handle)
  143. {
  144. struct tsc2007 *ts = handle;
  145. struct input_dev *input = ts->input;
  146. struct ts_event tc;
  147. u32 rt;
  148. while (!ts->stopped && tsc2007_is_pen_down(ts)) {
  149. /* pen is down, continue with the measurement */
  150. tsc2007_read_values(ts, &tc);
  151. rt = tsc2007_calculate_pressure(ts, &tc);
  152. if (!rt && !ts->get_pendown_state) {
  153. /*
  154. * If pressure reported is 0 and we don't have
  155. * callback to check pendown state, we have to
  156. * assume that pen was lifted up.
  157. */
  158. break;
  159. }
  160. if (rt <= ts->max_rt) {
  161. dev_dbg(&ts->client->dev,
  162. "DOWN point(%4d,%4d), pressure (%4u)\n",
  163. tc.x, tc.y, rt);
  164. input_report_key(input, BTN_TOUCH, 1);
  165. input_report_abs(input, ABS_X, tc.x);
  166. input_report_abs(input, ABS_Y, tc.y);
  167. input_report_abs(input, ABS_PRESSURE, rt);
  168. input_sync(input);
  169. } else {
  170. /*
  171. * Sample found inconsistent by debouncing or pressure is
  172. * beyond the maximum. Don't report it to user space,
  173. * repeat at least once more the measurement.
  174. */
  175. dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
  176. }
  177. wait_event_timeout(ts->wait, ts->stopped,
  178. msecs_to_jiffies(ts->poll_period));
  179. }
  180. dev_dbg(&ts->client->dev, "UP\n");
  181. input_report_key(input, BTN_TOUCH, 0);
  182. input_report_abs(input, ABS_PRESSURE, 0);
  183. input_sync(input);
  184. if (ts->clear_penirq)
  185. ts->clear_penirq();
  186. return IRQ_HANDLED;
  187. }
  188. static irqreturn_t tsc2007_hard_irq(int irq, void *handle)
  189. {
  190. struct tsc2007 *ts = handle;
  191. if (tsc2007_is_pen_down(ts))
  192. return IRQ_WAKE_THREAD;
  193. if (ts->clear_penirq)
  194. ts->clear_penirq();
  195. return IRQ_HANDLED;
  196. }
  197. static void tsc2007_stop(struct tsc2007 *ts)
  198. {
  199. ts->stopped = true;
  200. mb();
  201. wake_up(&ts->wait);
  202. disable_irq(ts->irq);
  203. }
  204. static int tsc2007_open(struct input_dev *input_dev)
  205. {
  206. struct tsc2007 *ts = input_get_drvdata(input_dev);
  207. int err;
  208. ts->stopped = false;
  209. mb();
  210. enable_irq(ts->irq);
  211. /* Prepare for touch readings - power down ADC and enable PENIRQ */
  212. err = tsc2007_xfer(ts, PWRDOWN);
  213. if (err < 0) {
  214. tsc2007_stop(ts);
  215. return err;
  216. }
  217. return 0;
  218. }
  219. static void tsc2007_close(struct input_dev *input_dev)
  220. {
  221. struct tsc2007 *ts = input_get_drvdata(input_dev);
  222. tsc2007_stop(ts);
  223. }
  224. #ifdef CONFIG_OF
  225. static int tsc2007_get_pendown_state_gpio(struct device *dev)
  226. {
  227. struct i2c_client *client = to_i2c_client(dev);
  228. struct tsc2007 *ts = i2c_get_clientdata(client);
  229. return !gpio_get_value(ts->gpio);
  230. }
  231. static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
  232. {
  233. struct device_node *np = client->dev.of_node;
  234. u32 val32;
  235. u64 val64;
  236. if (!np) {
  237. dev_err(&client->dev, "missing device tree data\n");
  238. return -EINVAL;
  239. }
  240. if (!of_property_read_u32(np, "ti,max-rt", &val32))
  241. ts->max_rt = val32;
  242. else
  243. ts->max_rt = MAX_12BIT;
  244. if (!of_property_read_u32(np, "ti,fuzzx", &val32))
  245. ts->fuzzx = val32;
  246. if (!of_property_read_u32(np, "ti,fuzzy", &val32))
  247. ts->fuzzy = val32;
  248. if (!of_property_read_u32(np, "ti,fuzzz", &val32))
  249. ts->fuzzz = val32;
  250. if (!of_property_read_u64(np, "ti,poll-period", &val64))
  251. ts->poll_period = val64;
  252. else
  253. ts->poll_period = 1;
  254. if (!of_property_read_u32(np, "ti,x-plate-ohms", &val32)) {
  255. ts->x_plate_ohms = val32;
  256. } else {
  257. dev_err(&client->dev, "missing ti,x-plate-ohms devicetree property.");
  258. return -EINVAL;
  259. }
  260. ts->gpio = of_get_gpio(np, 0);
  261. if (gpio_is_valid(ts->gpio))
  262. ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
  263. else
  264. dev_warn(&client->dev,
  265. "GPIO not specified in DT (of_get_gpio returned %d)\n",
  266. ts->gpio);
  267. return 0;
  268. }
  269. #else
  270. static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
  271. {
  272. dev_err(&client->dev, "platform data is required!\n");
  273. return -EINVAL;
  274. }
  275. #endif
  276. static int tsc2007_probe_pdev(struct i2c_client *client, struct tsc2007 *ts,
  277. const struct tsc2007_platform_data *pdata,
  278. const struct i2c_device_id *id)
  279. {
  280. ts->model = pdata->model;
  281. ts->x_plate_ohms = pdata->x_plate_ohms;
  282. ts->max_rt = pdata->max_rt ? : MAX_12BIT;
  283. ts->poll_period = pdata->poll_period ? : 1;
  284. ts->get_pendown_state = pdata->get_pendown_state;
  285. ts->clear_penirq = pdata->clear_penirq;
  286. ts->fuzzx = pdata->fuzzx;
  287. ts->fuzzy = pdata->fuzzy;
  288. ts->fuzzz = pdata->fuzzz;
  289. if (pdata->x_plate_ohms == 0) {
  290. dev_err(&client->dev, "x_plate_ohms is not set up in platform data");
  291. return -EINVAL;
  292. }
  293. return 0;
  294. }
  295. static void tsc2007_call_exit_platform_hw(void *data)
  296. {
  297. struct device *dev = data;
  298. const struct tsc2007_platform_data *pdata = dev_get_platdata(dev);
  299. pdata->exit_platform_hw();
  300. }
  301. static int tsc2007_probe(struct i2c_client *client,
  302. const struct i2c_device_id *id)
  303. {
  304. const struct tsc2007_platform_data *pdata = dev_get_platdata(&client->dev);
  305. struct tsc2007 *ts;
  306. struct input_dev *input_dev;
  307. int err;
  308. if (!i2c_check_functionality(client->adapter,
  309. I2C_FUNC_SMBUS_READ_WORD_DATA))
  310. return -EIO;
  311. ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
  312. if (!ts)
  313. return -ENOMEM;
  314. if (pdata)
  315. err = tsc2007_probe_pdev(client, ts, pdata, id);
  316. else
  317. err = tsc2007_probe_dt(client, ts);
  318. if (err)
  319. return err;
  320. input_dev = devm_input_allocate_device(&client->dev);
  321. if (!input_dev)
  322. return -ENOMEM;
  323. i2c_set_clientdata(client, ts);
  324. ts->client = client;
  325. ts->irq = client->irq;
  326. ts->input = input_dev;
  327. init_waitqueue_head(&ts->wait);
  328. snprintf(ts->phys, sizeof(ts->phys),
  329. "%s/input0", dev_name(&client->dev));
  330. input_dev->name = "TSC2007 Touchscreen";
  331. input_dev->phys = ts->phys;
  332. input_dev->id.bustype = BUS_I2C;
  333. input_dev->open = tsc2007_open;
  334. input_dev->close = tsc2007_close;
  335. input_set_drvdata(input_dev, ts);
  336. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  337. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  338. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
  339. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
  340. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
  341. ts->fuzzz, 0);
  342. if (pdata) {
  343. if (pdata->exit_platform_hw) {
  344. err = devm_add_action(&client->dev,
  345. tsc2007_call_exit_platform_hw,
  346. &client->dev);
  347. if (err) {
  348. dev_err(&client->dev,
  349. "Failed to register exit_platform_hw action, %d\n",
  350. err);
  351. return err;
  352. }
  353. }
  354. if (pdata->init_platform_hw)
  355. pdata->init_platform_hw();
  356. }
  357. err = devm_request_threaded_irq(&client->dev, ts->irq,
  358. tsc2007_hard_irq, tsc2007_soft_irq,
  359. IRQF_ONESHOT,
  360. client->dev.driver->name, ts);
  361. if (err) {
  362. dev_err(&client->dev, "Failed to request irq %d: %d\n",
  363. ts->irq, err);
  364. return err;
  365. }
  366. tsc2007_stop(ts);
  367. err = input_register_device(input_dev);
  368. if (err) {
  369. dev_err(&client->dev,
  370. "Failed to register input device: %d\n", err);
  371. return err;
  372. }
  373. return 0;
  374. }
  375. static const struct i2c_device_id tsc2007_idtable[] = {
  376. { "tsc2007", 0 },
  377. { }
  378. };
  379. MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
  380. #ifdef CONFIG_OF
  381. static const struct of_device_id tsc2007_of_match[] = {
  382. { .compatible = "ti,tsc2007" },
  383. { /* sentinel */ }
  384. };
  385. MODULE_DEVICE_TABLE(of, tsc2007_of_match);
  386. #endif
  387. static struct i2c_driver tsc2007_driver = {
  388. .driver = {
  389. .owner = THIS_MODULE,
  390. .name = "tsc2007",
  391. .of_match_table = of_match_ptr(tsc2007_of_match),
  392. },
  393. .id_table = tsc2007_idtable,
  394. .probe = tsc2007_probe,
  395. };
  396. module_i2c_driver(tsc2007_driver);
  397. MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
  398. MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
  399. MODULE_LICENSE("GPL");