ads7846.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. if (!req)
  136. return -ENOMEM;
  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. req->msg.transfers = req->xfer;
  165. req->msg.n_transfer = 6;
  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. /* non-empty "extra" is needed before 2.6.14-git5 or so */
  295. #define EXTRA // , u32 level
  296. #define EXTRA2 // , 0
  297. static int
  298. ads7846_suspend(struct device *dev, pm_message_t message EXTRA)
  299. {
  300. struct ads7846 *ts = dev_get_drvdata(dev);
  301. unsigned long flags;
  302. spin_lock_irqsave(&ts->lock, flags);
  303. ts->spi->dev.power.power_state = message;
  304. /* are we waiting for IRQ, or polling? */
  305. if (!ts->pendown) {
  306. if (!ts->irq_disabled) {
  307. ts->irq_disabled = 1;
  308. disable_irq(ts->spi->irq);
  309. }
  310. } else {
  311. /* polling; force a final SPI completion;
  312. * that will clean things up neatly
  313. */
  314. if (!ts->pending)
  315. mod_timer(&ts->timer, jiffies);
  316. while (ts->pendown || ts->pending) {
  317. spin_unlock_irqrestore(&ts->lock, flags);
  318. udelay(10);
  319. spin_lock_irqsave(&ts->lock, flags);
  320. }
  321. }
  322. /* we know the chip's in lowpower mode since we always
  323. * leave it that way after every request
  324. */
  325. spin_unlock_irqrestore(&ts->lock, flags);
  326. return 0;
  327. }
  328. static int ads7846_resume(struct device *dev EXTRA)
  329. {
  330. struct ads7846 *ts = dev_get_drvdata(dev);
  331. ts->irq_disabled = 0;
  332. enable_irq(ts->spi->irq);
  333. dev->power.power_state = PMSG_ON;
  334. return 0;
  335. }
  336. static int __init ads7846_probe(struct device *dev)
  337. {
  338. struct spi_device *spi = to_spi_device(dev);
  339. struct ads7846 *ts;
  340. struct ads7846_platform_data *pdata = dev->platform_data;
  341. struct spi_transfer *x;
  342. if (!spi->irq) {
  343. dev_dbg(dev, "no IRQ?\n");
  344. return -ENODEV;
  345. }
  346. if (!pdata) {
  347. dev_dbg(dev, "no platform data?\n");
  348. return -ENODEV;
  349. }
  350. /* don't exceed max specified sample rate */
  351. if (spi->max_speed_hz > (125000 * 16)) {
  352. dev_dbg(dev, "f(sample) %d KHz?\n",
  353. (spi->max_speed_hz/16)/1000);
  354. return -EINVAL;
  355. }
  356. /* We'd set the wordsize to 12 bits ... except that some controllers
  357. * will then treat the 8 bit command words as 12 bits (and drop the
  358. * four MSBs of the 12 bit result). Result: inputs must be shifted
  359. * to discard the four garbage LSBs.
  360. */
  361. if (!(ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL)))
  362. return -ENOMEM;
  363. dev_set_drvdata(dev, ts);
  364. ts->spi = spi;
  365. spi->dev.power.power_state = PMSG_ON;
  366. init_timer(&ts->timer);
  367. ts->timer.data = (unsigned long) ts;
  368. ts->timer.function = ads7846_timer;
  369. ts->model = pdata->model ? : 7846;
  370. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  371. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  372. init_input_dev(&ts->input);
  373. ts->input.dev = dev;
  374. ts->input.name = "ADS784x Touchscreen";
  375. snprintf(ts->phys, sizeof ts->phys, "%s/input0", dev->bus_id);
  376. ts->input.phys = ts->phys;
  377. ts->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  378. ts->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  379. input_set_abs_params(&ts->input, ABS_X,
  380. pdata->x_min ? : 0,
  381. pdata->x_max ? : MAX_12BIT,
  382. 0, 0);
  383. input_set_abs_params(&ts->input, ABS_Y,
  384. pdata->y_min ? : 0,
  385. pdata->y_max ? : MAX_12BIT,
  386. 0, 0);
  387. input_set_abs_params(&ts->input, ABS_PRESSURE,
  388. pdata->pressure_min, pdata->pressure_max, 0, 0);
  389. input_register_device(&ts->input);
  390. /* set up the transfers to read touchscreen state; this assumes we
  391. * use formula #2 for pressure, not #3.
  392. */
  393. x = ts->xfer;
  394. /* y- still on; turn on only y+ (and ADC) */
  395. x->tx_buf = &read_y;
  396. x->len = 1;
  397. x++;
  398. x->rx_buf = &ts->tc.y;
  399. x->len = 2;
  400. x++;
  401. /* turn y+ off, x- on; we'll use formula #2 */
  402. if (ts->model == 7846) {
  403. x->tx_buf = &read_z1;
  404. x->len = 1;
  405. x++;
  406. x->rx_buf = &ts->tc.z1;
  407. x->len = 2;
  408. x++;
  409. x->tx_buf = &read_z2;
  410. x->len = 1;
  411. x++;
  412. x->rx_buf = &ts->tc.z2;
  413. x->len = 2;
  414. x++;
  415. }
  416. /* turn y- off, x+ on, then leave in lowpower */
  417. x->tx_buf = &read_x;
  418. x->len = 1;
  419. x++;
  420. x->rx_buf = &ts->tc.x;
  421. x->len = 2;
  422. x++;
  423. CS_CHANGE(x[-1]);
  424. ts->msg.transfers = ts->xfer;
  425. ts->msg.n_transfer = x - ts->xfer;
  426. ts->msg.complete = ads7846_rx;
  427. ts->msg.context = ts;
  428. if (request_irq(spi->irq, ads7846_irq, SA_SAMPLE_RANDOM,
  429. dev->bus_id, ts)) {
  430. dev_dbg(dev, "irq %d busy?\n", spi->irq);
  431. input_unregister_device(&ts->input);
  432. kfree(ts);
  433. return -EBUSY;
  434. }
  435. set_irq_type(spi->irq, IRQT_FALLING);
  436. dev_info(dev, "touchscreen, irq %d\n", spi->irq);
  437. /* take a first sample, leaving nPENIRQ active; avoid
  438. * the touchscreen, in case it's not connected.
  439. */
  440. (void) ads7846_read12_ser(dev,
  441. READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
  442. /* ads7843/7845 don't have temperature sensors, and
  443. * use the other sensors a bit differently too
  444. */
  445. if (ts->model == 7846) {
  446. device_create_file(dev, &dev_attr_temp0);
  447. device_create_file(dev, &dev_attr_temp1);
  448. }
  449. if (ts->model != 7845)
  450. device_create_file(dev, &dev_attr_vbatt);
  451. device_create_file(dev, &dev_attr_vaux);
  452. return 0;
  453. }
  454. static int __exit ads7846_remove(struct device *dev)
  455. {
  456. struct ads7846 *ts = dev_get_drvdata(dev);
  457. ads7846_suspend(dev, PMSG_SUSPEND EXTRA2);
  458. free_irq(ts->spi->irq, ts);
  459. if (ts->irq_disabled)
  460. enable_irq(ts->spi->irq);
  461. if (ts->model == 7846) {
  462. device_remove_file(dev, &dev_attr_temp0);
  463. device_remove_file(dev, &dev_attr_temp1);
  464. }
  465. if (ts->model != 7845)
  466. device_remove_file(dev, &dev_attr_vbatt);
  467. device_remove_file(dev, &dev_attr_vaux);
  468. input_unregister_device(&ts->input);
  469. kfree(ts);
  470. dev_dbg(dev, "unregistered touchscreen\n");
  471. return 0;
  472. }
  473. static struct device_driver ads7846_driver = {
  474. .name = "ads7846",
  475. .bus = &spi_bus_type,
  476. .probe = ads7846_probe,
  477. .remove = __exit_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 (alternate to UCB1400)
  500. // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
  501. // also various AMD Au1x00 devel boards
  502. return driver_register(&ads7846_driver);
  503. }
  504. module_init(ads7846_init);
  505. static void __exit ads7846_exit(void)
  506. {
  507. driver_unregister(&ads7846_driver);
  508. #ifdef CONFIG_ARCH_OMAP
  509. if (machine_is_omap_osk()) {
  510. omap_free_gpio(4);
  511. omap_free_gpio(6);
  512. }
  513. #endif
  514. }
  515. module_exit(ads7846_exit);
  516. MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
  517. MODULE_LICENSE("GPL");