ads7846.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * ADS7846 based touchscreen and sensor driver
  3. *
  4. * Copyright (c) 2005 David Brownell
  5. *
  6. * Using code from:
  7. * - corgi_ts.c
  8. * Copyright (C) 2004-2005 Richard Purdie
  9. * - omap_ts.[hc], ads7846.h, ts_osk.c
  10. * Copyright (C) 2002 MontaVista Software
  11. * Copyright (C) 2004 Texas Instruments
  12. * Copyright (C) 2005 Dirk Behme
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/input.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/slab.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/ads7846.h>
  26. #ifdef CONFIG_ARM
  27. #include <asm/mach-types.h>
  28. #ifdef CONFIG_ARCH_OMAP
  29. #include <asm/arch/gpio.h>
  30. #endif
  31. #endif
  32. /*
  33. * This code has been lightly tested on an ads7846.
  34. * Support for ads7843 and ads7845 has only been stubbed in.
  35. *
  36. * Not yet done: investigate the values reported. Are x/y/pressure
  37. * event values sane enough for X11? How accurate are the temperature
  38. * and voltage readings? (System-specific calibration should support
  39. * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
  40. *
  41. * app note sbaa036 talks in more detail about accurate sampling...
  42. * that ought to help in situations like LCDs inducing noise (which
  43. * can also be helped by using synch signals) and more generally.
  44. */
  45. #define TS_POLL_PERIOD msecs_to_jiffies(10)
  46. struct ts_event {
  47. /* For portability, we can't read 12 bit values using SPI (which
  48. * would make the controller deliver them as native byteorder u16
  49. * with msbs zeroed). Instead, we read them as two 8-byte values,
  50. * which need byteswapping then range adjustment.
  51. */
  52. __be16 x;
  53. __be16 y;
  54. __be16 z1, z2;
  55. };
  56. struct ads7846 {
  57. struct input_dev input;
  58. char phys[32];
  59. struct spi_device *spi;
  60. u16 model;
  61. u16 vref_delay_usecs;
  62. u16 x_plate_ohms;
  63. struct ts_event tc;
  64. struct spi_transfer xfer[8];
  65. struct spi_message msg;
  66. spinlock_t lock;
  67. struct timer_list timer; /* P: lock */
  68. unsigned pendown:1; /* P: lock */
  69. unsigned pending:1; /* P: lock */
  70. // FIXME remove "irq_disabled"
  71. unsigned irq_disabled:1; /* P: lock */
  72. };
  73. /* leave chip selected when we're done, for quicker re-select? */
  74. #if 0
  75. #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
  76. #else
  77. #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
  78. #endif
  79. /*--------------------------------------------------------------------------*/
  80. /* The ADS7846 has touchscreen and other sensors.
  81. * Earlier ads784x chips are somewhat compatible.
  82. */
  83. #define ADS_START (1 << 7)
  84. #define ADS_A2A1A0_d_y (1 << 4) /* differential */
  85. #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
  86. #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
  87. #define ADS_A2A1A0_d_x (5 << 4) /* differential */
  88. #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
  89. #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
  90. #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
  91. #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
  92. #define ADS_8_BIT (1 << 3)
  93. #define ADS_12_BIT (0 << 3)
  94. #define ADS_SER (1 << 2) /* non-differential */
  95. #define ADS_DFR (0 << 2) /* differential */
  96. #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
  97. #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
  98. #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
  99. #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
  100. #define MAX_12BIT ((1<<12)-1)
  101. /* leave ADC powered up (disables penirq) between differential samples */
  102. #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
  103. | ADS_12_BIT | ADS_DFR)
  104. static const u8 read_y = READ_12BIT_DFR(y) | ADS_PD10_ADC_ON;
  105. static const u8 read_z1 = READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON;
  106. static const u8 read_z2 = READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON;
  107. static const u8 read_x = READ_12BIT_DFR(x) | ADS_PD10_PDOWN; /* LAST */
  108. /* single-ended samples need to first power up reference voltage;
  109. * we leave both ADC and VREF powered
  110. */
  111. #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
  112. | ADS_12_BIT | ADS_SER)
  113. static const u8 ref_on = READ_12BIT_DFR(x) | ADS_PD10_ALL_ON;
  114. static const u8 ref_off = READ_12BIT_DFR(y) | ADS_PD10_PDOWN;
  115. /*--------------------------------------------------------------------------*/
  116. /*
  117. * Non-touchscreen sensors only use single-ended conversions.
  118. */
  119. struct ser_req {
  120. u8 command;
  121. u16 scratch;
  122. __be16 sample;
  123. struct spi_message msg;
  124. struct spi_transfer xfer[6];
  125. };
  126. static int ads7846_read12_ser(struct device *dev, unsigned command)
  127. {
  128. struct spi_device *spi = to_spi_device(dev);
  129. struct ads7846 *ts = dev_get_drvdata(dev);
  130. struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
  131. int status;
  132. int sample;
  133. int i;
  134. if (!req)
  135. return -ENOMEM;
  136. INIT_LIST_HEAD(&req->msg.transfers);
  137. /* activate reference, so it has time to settle; */
  138. req->xfer[0].tx_buf = &ref_on;
  139. req->xfer[0].len = 1;
  140. req->xfer[1].rx_buf = &req->scratch;
  141. req->xfer[1].len = 2;
  142. /*
  143. * for external VREF, 0 usec (and assume it's always on);
  144. * for 1uF, use 800 usec;
  145. * no cap, 100 usec.
  146. */
  147. req->xfer[1].delay_usecs = ts->vref_delay_usecs;
  148. /* take sample */
  149. req->command = (u8) command;
  150. req->xfer[2].tx_buf = &req->command;
  151. req->xfer[2].len = 1;
  152. req->xfer[3].rx_buf = &req->sample;
  153. req->xfer[3].len = 2;
  154. /* REVISIT: take a few more samples, and compare ... */
  155. /* turn off reference */
  156. req->xfer[4].tx_buf = &ref_off;
  157. req->xfer[4].len = 1;
  158. req->xfer[5].rx_buf = &req->scratch;
  159. req->xfer[5].len = 2;
  160. CS_CHANGE(req->xfer[5]);
  161. /* group all the transfers together, so we can't interfere with
  162. * reading touchscreen state; disable penirq while sampling
  163. */
  164. for (i = 0; i < 6; i++)
  165. spi_message_add_tail(&req->xfer[i], &req->msg);
  166. disable_irq(spi->irq);
  167. status = spi_sync(spi, &req->msg);
  168. enable_irq(spi->irq);
  169. if (req->msg.status)
  170. status = req->msg.status;
  171. sample = be16_to_cpu(req->sample);
  172. sample = sample >> 4;
  173. kfree(req);
  174. return status ? status : sample;
  175. }
  176. #define SHOW(name) static ssize_t \
  177. name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
  178. { \
  179. ssize_t v = ads7846_read12_ser(dev, \
  180. READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
  181. if (v < 0) \
  182. return v; \
  183. return sprintf(buf, "%u\n", (unsigned) v); \
  184. } \
  185. static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
  186. SHOW(temp0)
  187. SHOW(temp1)
  188. SHOW(vaux)
  189. SHOW(vbatt)
  190. /*--------------------------------------------------------------------------*/
  191. /*
  192. * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
  193. * to retrieve touchscreen status.
  194. *
  195. * The SPI transfer completion callback does the real work. It reports
  196. * touchscreen events and reactivates the timer (or IRQ) as appropriate.
  197. */
  198. static void ads7846_rx(void *ads)
  199. {
  200. struct ads7846 *ts = ads;
  201. unsigned Rt;
  202. unsigned sync = 0;
  203. u16 x, y, z1, z2;
  204. unsigned long flags;
  205. /* adjust: 12 bit samples (left aligned), built from
  206. * two 8 bit values writen msb-first.
  207. */
  208. x = be16_to_cpu(ts->tc.x) >> 4;
  209. y = be16_to_cpu(ts->tc.y) >> 4;
  210. z1 = be16_to_cpu(ts->tc.z1) >> 4;
  211. z2 = be16_to_cpu(ts->tc.z2) >> 4;
  212. /* range filtering */
  213. if (x == MAX_12BIT)
  214. x = 0;
  215. if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
  216. /* compute touch pressure resistance using equation #2 */
  217. Rt = z2;
  218. Rt -= z1;
  219. Rt *= x;
  220. Rt *= ts->x_plate_ohms;
  221. Rt /= z1;
  222. Rt = (Rt + 2047) >> 12;
  223. } else
  224. Rt = 0;
  225. /* NOTE: "pendown" is inferred from pressure; we don't rely on
  226. * being able to check nPENIRQ status, or "friendly" trigger modes
  227. * (both-edges is much better than just-falling or low-level).
  228. *
  229. * REVISIT: some boards may require reading nPENIRQ; it's
  230. * needed on 7843. and 7845 reads pressure differently...
  231. *
  232. * REVISIT: the touchscreen might not be connected; this code
  233. * won't notice that, even if nPENIRQ never fires ...
  234. */
  235. if (!ts->pendown && Rt != 0) {
  236. input_report_key(&ts->input, BTN_TOUCH, 1);
  237. sync = 1;
  238. } else if (ts->pendown && Rt == 0) {
  239. input_report_key(&ts->input, BTN_TOUCH, 0);
  240. sync = 1;
  241. }
  242. if (Rt) {
  243. input_report_abs(&ts->input, ABS_X, x);
  244. input_report_abs(&ts->input, ABS_Y, y);
  245. input_report_abs(&ts->input, ABS_PRESSURE, Rt);
  246. sync = 1;
  247. }
  248. if (sync)
  249. input_sync(&ts->input);
  250. #ifdef VERBOSE
  251. if (Rt || ts->pendown)
  252. pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
  253. x, y, Rt, Rt ? "" : " UP");
  254. #endif
  255. /* don't retrigger while we're suspended */
  256. spin_lock_irqsave(&ts->lock, flags);
  257. ts->pendown = (Rt != 0);
  258. ts->pending = 0;
  259. if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
  260. if (ts->pendown)
  261. mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
  262. else if (ts->irq_disabled) {
  263. ts->irq_disabled = 0;
  264. enable_irq(ts->spi->irq);
  265. }
  266. }
  267. spin_unlock_irqrestore(&ts->lock, flags);
  268. }
  269. static void ads7846_timer(unsigned long handle)
  270. {
  271. struct ads7846 *ts = (void *)handle;
  272. int status = 0;
  273. unsigned long flags;
  274. spin_lock_irqsave(&ts->lock, flags);
  275. if (!ts->pending) {
  276. ts->pending = 1;
  277. if (!ts->irq_disabled) {
  278. ts->irq_disabled = 1;
  279. disable_irq(ts->spi->irq);
  280. }
  281. status = spi_async(ts->spi, &ts->msg);
  282. if (status)
  283. dev_err(&ts->spi->dev, "spi_async --> %d\n",
  284. status);
  285. }
  286. spin_unlock_irqrestore(&ts->lock, flags);
  287. }
  288. static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
  289. {
  290. ads7846_timer((unsigned long) handle);
  291. return IRQ_HANDLED;
  292. }
  293. /*--------------------------------------------------------------------------*/
  294. static int
  295. ads7846_suspend(struct spi_device *spi, pm_message_t message)
  296. {
  297. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  298. unsigned long flags;
  299. spin_lock_irqsave(&ts->lock, flags);
  300. spi->dev.power.power_state = message;
  301. /* are we waiting for IRQ, or polling? */
  302. if (!ts->pendown) {
  303. if (!ts->irq_disabled) {
  304. ts->irq_disabled = 1;
  305. disable_irq(ts->spi->irq);
  306. }
  307. } else {
  308. /* polling; force a final SPI completion;
  309. * that will clean things up neatly
  310. */
  311. if (!ts->pending)
  312. mod_timer(&ts->timer, jiffies);
  313. while (ts->pendown || ts->pending) {
  314. spin_unlock_irqrestore(&ts->lock, flags);
  315. udelay(10);
  316. spin_lock_irqsave(&ts->lock, flags);
  317. }
  318. }
  319. /* we know the chip's in lowpower mode since we always
  320. * leave it that way after every request
  321. */
  322. spin_unlock_irqrestore(&ts->lock, flags);
  323. return 0;
  324. }
  325. static int ads7846_resume(struct spi_device *spi)
  326. {
  327. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  328. ts->irq_disabled = 0;
  329. enable_irq(ts->spi->irq);
  330. spi->dev.power.power_state = PMSG_ON;
  331. return 0;
  332. }
  333. static int __devinit ads7846_probe(struct spi_device *spi)
  334. {
  335. struct ads7846 *ts;
  336. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  337. struct spi_transfer *x;
  338. int i;
  339. if (!spi->irq) {
  340. dev_dbg(&spi->dev, "no IRQ?\n");
  341. return -ENODEV;
  342. }
  343. if (!pdata) {
  344. dev_dbg(&spi->dev, "no platform data?\n");
  345. return -ENODEV;
  346. }
  347. /* don't exceed max specified sample rate */
  348. if (spi->max_speed_hz > (125000 * 16)) {
  349. dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
  350. (spi->max_speed_hz/16)/1000);
  351. return -EINVAL;
  352. }
  353. /* We'd set the wordsize to 12 bits ... except that some controllers
  354. * will then treat the 8 bit command words as 12 bits (and drop the
  355. * four MSBs of the 12 bit result). Result: inputs must be shifted
  356. * to discard the four garbage LSBs.
  357. */
  358. if (!(ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL)))
  359. return -ENOMEM;
  360. dev_set_drvdata(&spi->dev, ts);
  361. ts->spi = spi;
  362. spi->dev.power.power_state = PMSG_ON;
  363. init_timer(&ts->timer);
  364. ts->timer.data = (unsigned long) ts;
  365. ts->timer.function = ads7846_timer;
  366. ts->model = pdata->model ? : 7846;
  367. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  368. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  369. init_input_dev(&ts->input);
  370. ts->input.dev = &spi->dev;
  371. ts->input.name = "ADS784x Touchscreen";
  372. snprintf(ts->phys, sizeof ts->phys, "%s/input0", spi->dev.bus_id);
  373. ts->input.phys = ts->phys;
  374. ts->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  375. ts->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  376. input_set_abs_params(&ts->input, ABS_X,
  377. pdata->x_min ? : 0,
  378. pdata->x_max ? : MAX_12BIT,
  379. 0, 0);
  380. input_set_abs_params(&ts->input, ABS_Y,
  381. pdata->y_min ? : 0,
  382. pdata->y_max ? : MAX_12BIT,
  383. 0, 0);
  384. input_set_abs_params(&ts->input, ABS_PRESSURE,
  385. pdata->pressure_min, pdata->pressure_max, 0, 0);
  386. input_register_device(&ts->input);
  387. /* set up the transfers to read touchscreen state; this assumes we
  388. * use formula #2 for pressure, not #3.
  389. */
  390. x = ts->xfer;
  391. /* y- still on; turn on only y+ (and ADC) */
  392. x->tx_buf = &read_y;
  393. x->len = 1;
  394. x++;
  395. x->rx_buf = &ts->tc.y;
  396. x->len = 2;
  397. x++;
  398. /* turn y+ off, x- on; we'll use formula #2 */
  399. if (ts->model == 7846) {
  400. x->tx_buf = &read_z1;
  401. x->len = 1;
  402. x++;
  403. x->rx_buf = &ts->tc.z1;
  404. x->len = 2;
  405. x++;
  406. x->tx_buf = &read_z2;
  407. x->len = 1;
  408. x++;
  409. x->rx_buf = &ts->tc.z2;
  410. x->len = 2;
  411. x++;
  412. }
  413. /* turn y- off, x+ on, then leave in lowpower */
  414. x->tx_buf = &read_x;
  415. x->len = 1;
  416. x++;
  417. x->rx_buf = &ts->tc.x;
  418. x->len = 2;
  419. x++;
  420. CS_CHANGE(x[-1]);
  421. for (i = 0; i < x - ts->xfer; i++)
  422. spi_message_add_tail(&ts->xfer[i], &ts->msg);
  423. ts->msg.complete = ads7846_rx;
  424. ts->msg.context = ts;
  425. if (request_irq(spi->irq, ads7846_irq,
  426. SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
  427. spi->dev.bus_id, ts)) {
  428. dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
  429. input_unregister_device(&ts->input);
  430. kfree(ts);
  431. return -EBUSY;
  432. }
  433. dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
  434. /* take a first sample, leaving nPENIRQ active; avoid
  435. * the touchscreen, in case it's not connected.
  436. */
  437. (void) ads7846_read12_ser(&spi->dev,
  438. READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
  439. /* ads7843/7845 don't have temperature sensors, and
  440. * use the other sensors a bit differently too
  441. */
  442. if (ts->model == 7846) {
  443. device_create_file(&spi->dev, &dev_attr_temp0);
  444. device_create_file(&spi->dev, &dev_attr_temp1);
  445. }
  446. if (ts->model != 7845)
  447. device_create_file(&spi->dev, &dev_attr_vbatt);
  448. device_create_file(&spi->dev, &dev_attr_vaux);
  449. return 0;
  450. }
  451. static int __devexit ads7846_remove(struct spi_device *spi)
  452. {
  453. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  454. ads7846_suspend(spi, PMSG_SUSPEND);
  455. free_irq(ts->spi->irq, ts);
  456. if (ts->irq_disabled)
  457. enable_irq(ts->spi->irq);
  458. if (ts->model == 7846) {
  459. device_remove_file(&spi->dev, &dev_attr_temp0);
  460. device_remove_file(&spi->dev, &dev_attr_temp1);
  461. }
  462. if (ts->model != 7845)
  463. device_remove_file(&spi->dev, &dev_attr_vbatt);
  464. device_remove_file(&spi->dev, &dev_attr_vaux);
  465. input_unregister_device(&ts->input);
  466. kfree(ts);
  467. dev_dbg(&spi->dev, "unregistered touchscreen\n");
  468. return 0;
  469. }
  470. static struct spi_driver ads7846_driver = {
  471. .driver = {
  472. .name = "ads7846",
  473. .bus = &spi_bus_type,
  474. .owner = THIS_MODULE,
  475. },
  476. .probe = ads7846_probe,
  477. .remove = __devexit_p(ads7846_remove),
  478. .suspend = ads7846_suspend,
  479. .resume = ads7846_resume,
  480. };
  481. static int __init ads7846_init(void)
  482. {
  483. /* grr, board-specific init should stay out of drivers!! */
  484. #ifdef CONFIG_ARCH_OMAP
  485. if (machine_is_omap_osk()) {
  486. /* GPIO4 = PENIRQ; GPIO6 = BUSY */
  487. omap_request_gpio(4);
  488. omap_set_gpio_direction(4, 1);
  489. omap_request_gpio(6);
  490. omap_set_gpio_direction(6, 1);
  491. }
  492. // also TI 1510 Innovator, bitbanging through FPGA
  493. // also Nokia 770
  494. // also Palm Tungsten T2
  495. #endif
  496. // PXA:
  497. // also Dell Axim X50
  498. // also HP iPaq H191x/H192x/H415x/H435x
  499. // also Intel Lubbock (additional to UCB1400; as temperature sensor)
  500. // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
  501. // Atmel at91sam9261-EK uses ads7843
  502. // also various AMD Au1x00 devel boards
  503. return spi_register_driver(&ads7846_driver);
  504. }
  505. module_init(ads7846_init);
  506. static void __exit ads7846_exit(void)
  507. {
  508. spi_unregister_driver(&ads7846_driver);
  509. #ifdef CONFIG_ARCH_OMAP
  510. if (machine_is_omap_osk()) {
  511. omap_free_gpio(4);
  512. omap_free_gpio(6);
  513. }
  514. #endif
  515. }
  516. module_exit(ads7846_exit);
  517. MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
  518. MODULE_LICENSE("GPL");