tsc2007.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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; /* in jiffies */
  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, ts->poll_period);
  178. }
  179. dev_dbg(&ts->client->dev, "UP\n");
  180. input_report_key(input, BTN_TOUCH, 0);
  181. input_report_abs(input, ABS_PRESSURE, 0);
  182. input_sync(input);
  183. if (ts->clear_penirq)
  184. ts->clear_penirq();
  185. return IRQ_HANDLED;
  186. }
  187. static irqreturn_t tsc2007_hard_irq(int irq, void *handle)
  188. {
  189. struct tsc2007 *ts = handle;
  190. if (tsc2007_is_pen_down(ts))
  191. return IRQ_WAKE_THREAD;
  192. if (ts->clear_penirq)
  193. ts->clear_penirq();
  194. return IRQ_HANDLED;
  195. }
  196. static void tsc2007_stop(struct tsc2007 *ts)
  197. {
  198. ts->stopped = true;
  199. mb();
  200. wake_up(&ts->wait);
  201. disable_irq(ts->irq);
  202. }
  203. static int tsc2007_open(struct input_dev *input_dev)
  204. {
  205. struct tsc2007 *ts = input_get_drvdata(input_dev);
  206. int err;
  207. ts->stopped = false;
  208. mb();
  209. enable_irq(ts->irq);
  210. /* Prepare for touch readings - power down ADC and enable PENIRQ */
  211. err = tsc2007_xfer(ts, PWRDOWN);
  212. if (err < 0) {
  213. tsc2007_stop(ts);
  214. return err;
  215. }
  216. return 0;
  217. }
  218. static void tsc2007_close(struct input_dev *input_dev)
  219. {
  220. struct tsc2007 *ts = input_get_drvdata(input_dev);
  221. tsc2007_stop(ts);
  222. }
  223. #ifdef CONFIG_OF
  224. static int tsc2007_get_pendown_state_gpio(struct device *dev)
  225. {
  226. struct i2c_client *client = to_i2c_client(dev);
  227. struct tsc2007 *ts = i2c_get_clientdata(client);
  228. return !gpio_get_value(ts->gpio);
  229. }
  230. static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
  231. {
  232. struct device_node *np = client->dev.of_node;
  233. u32 val32;
  234. u64 val64;
  235. if (!np) {
  236. dev_err(&client->dev, "missing device tree data\n");
  237. return -EINVAL;
  238. }
  239. if (!of_property_read_u32(np, "ti,max-rt", &val32))
  240. ts->max_rt = val32;
  241. else
  242. ts->max_rt = MAX_12BIT;
  243. if (!of_property_read_u32(np, "ti,fuzzx", &val32))
  244. ts->fuzzx = val32;
  245. if (!of_property_read_u32(np, "ti,fuzzy", &val32))
  246. ts->fuzzy = val32;
  247. if (!of_property_read_u32(np, "ti,fuzzz", &val32))
  248. ts->fuzzz = val32;
  249. if (!of_property_read_u64(np, "ti,poll-period", &val64))
  250. ts->poll_period = msecs_to_jiffies(val64);
  251. else
  252. ts->poll_period = msecs_to_jiffies(1);
  253. if (!of_property_read_u32(np, "ti,x-plate-ohms", &val32)) {
  254. ts->x_plate_ohms = val32;
  255. } else {
  256. dev_err(&client->dev, "missing ti,x-plate-ohms devicetree property.");
  257. return -EINVAL;
  258. }
  259. ts->gpio = of_get_gpio(np, 0);
  260. if (gpio_is_valid(ts->gpio))
  261. ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
  262. else
  263. dev_warn(&client->dev,
  264. "GPIO not specified in DT (of_get_gpio returned %d)\n",
  265. ts->gpio);
  266. return 0;
  267. }
  268. #else
  269. static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
  270. {
  271. dev_err(&client->dev, "platform data is required!\n");
  272. return -EINVAL;
  273. }
  274. #endif
  275. static int tsc2007_probe_pdev(struct i2c_client *client, struct tsc2007 *ts,
  276. const struct tsc2007_platform_data *pdata,
  277. const struct i2c_device_id *id)
  278. {
  279. ts->model = pdata->model;
  280. ts->x_plate_ohms = pdata->x_plate_ohms;
  281. ts->max_rt = pdata->max_rt ? : MAX_12BIT;
  282. ts->poll_period = msecs_to_jiffies(pdata->poll_period ? : 1);
  283. ts->get_pendown_state = pdata->get_pendown_state;
  284. ts->clear_penirq = pdata->clear_penirq;
  285. ts->fuzzx = pdata->fuzzx;
  286. ts->fuzzy = pdata->fuzzy;
  287. ts->fuzzz = pdata->fuzzz;
  288. if (pdata->x_plate_ohms == 0) {
  289. dev_err(&client->dev, "x_plate_ohms is not set up in platform data");
  290. return -EINVAL;
  291. }
  292. return 0;
  293. }
  294. static void tsc2007_call_exit_platform_hw(void *data)
  295. {
  296. struct device *dev = data;
  297. const struct tsc2007_platform_data *pdata = dev_get_platdata(dev);
  298. pdata->exit_platform_hw();
  299. }
  300. static int tsc2007_probe(struct i2c_client *client,
  301. const struct i2c_device_id *id)
  302. {
  303. const struct tsc2007_platform_data *pdata = dev_get_platdata(&client->dev);
  304. struct tsc2007 *ts;
  305. struct input_dev *input_dev;
  306. int err;
  307. if (!i2c_check_functionality(client->adapter,
  308. I2C_FUNC_SMBUS_READ_WORD_DATA))
  309. return -EIO;
  310. ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
  311. if (!ts)
  312. return -ENOMEM;
  313. if (pdata)
  314. err = tsc2007_probe_pdev(client, ts, pdata, id);
  315. else
  316. err = tsc2007_probe_dt(client, ts);
  317. if (err)
  318. return err;
  319. input_dev = devm_input_allocate_device(&client->dev);
  320. if (!input_dev)
  321. return -ENOMEM;
  322. i2c_set_clientdata(client, ts);
  323. ts->client = client;
  324. ts->irq = client->irq;
  325. ts->input = input_dev;
  326. init_waitqueue_head(&ts->wait);
  327. snprintf(ts->phys, sizeof(ts->phys),
  328. "%s/input0", dev_name(&client->dev));
  329. input_dev->name = "TSC2007 Touchscreen";
  330. input_dev->phys = ts->phys;
  331. input_dev->id.bustype = BUS_I2C;
  332. input_dev->open = tsc2007_open;
  333. input_dev->close = tsc2007_close;
  334. input_set_drvdata(input_dev, ts);
  335. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  336. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  337. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
  338. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
  339. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
  340. ts->fuzzz, 0);
  341. if (pdata) {
  342. if (pdata->exit_platform_hw) {
  343. err = devm_add_action(&client->dev,
  344. tsc2007_call_exit_platform_hw,
  345. &client->dev);
  346. if (err) {
  347. dev_err(&client->dev,
  348. "Failed to register exit_platform_hw action, %d\n",
  349. err);
  350. return err;
  351. }
  352. }
  353. if (pdata->init_platform_hw)
  354. pdata->init_platform_hw();
  355. }
  356. err = devm_request_threaded_irq(&client->dev, ts->irq,
  357. tsc2007_hard_irq, tsc2007_soft_irq,
  358. IRQF_ONESHOT,
  359. client->dev.driver->name, ts);
  360. if (err) {
  361. dev_err(&client->dev, "Failed to request irq %d: %d\n",
  362. ts->irq, err);
  363. return err;
  364. }
  365. tsc2007_stop(ts);
  366. /* power down the chip (TSC2007_SETUP does not ACK on I2C) */
  367. err = tsc2007_xfer(ts, PWRDOWN);
  368. if (err < 0) {
  369. dev_err(&client->dev,
  370. "Failed to setup chip: %d\n", err);
  371. return err; /* usually, chip does not respond */
  372. }
  373. err = input_register_device(input_dev);
  374. if (err) {
  375. dev_err(&client->dev,
  376. "Failed to register input device: %d\n", err);
  377. return err;
  378. }
  379. return 0;
  380. }
  381. static const struct i2c_device_id tsc2007_idtable[] = {
  382. { "tsc2007", 0 },
  383. { }
  384. };
  385. MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
  386. #ifdef CONFIG_OF
  387. static const struct of_device_id tsc2007_of_match[] = {
  388. { .compatible = "ti,tsc2007" },
  389. { /* sentinel */ }
  390. };
  391. MODULE_DEVICE_TABLE(of, tsc2007_of_match);
  392. #endif
  393. static struct i2c_driver tsc2007_driver = {
  394. .driver = {
  395. .name = "tsc2007",
  396. .of_match_table = of_match_ptr(tsc2007_of_match),
  397. },
  398. .id_table = tsc2007_idtable,
  399. .probe = tsc2007_probe,
  400. };
  401. module_i2c_driver(tsc2007_driver);
  402. MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
  403. MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
  404. MODULE_LICENSE("GPL");