ads7846.c 16 KB

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