ads7846.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * ADS7846 based touchscreen and sensor driver
  3. *
  4. * Copyright (c) 2005 David Brownell
  5. * Copyright (c) 2006 Nokia Corporation
  6. * Various changes: Imre Deak <imre.deak@nokia.com>
  7. *
  8. * Using code from:
  9. * - corgi_ts.c
  10. * Copyright (C) 2004-2005 Richard Purdie
  11. * - omap_ts.[hc], ads7846.h, ts_osk.c
  12. * Copyright (C) 2002 MontaVista Software
  13. * Copyright (C) 2004 Texas Instruments
  14. * Copyright (C) 2005 Dirk Behme
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. */
  20. #include <linux/device.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/input.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/slab.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/spi/ads7846.h>
  28. #include <asm/irq.h>
  29. #ifdef CONFIG_ARM
  30. #include <asm/mach-types.h>
  31. #ifdef CONFIG_ARCH_OMAP
  32. #include <asm/arch/gpio.h>
  33. #endif
  34. #endif
  35. /*
  36. * This code has been tested on an ads7846 / N770 device.
  37. * Support for ads7843 and ads7845 has only been stubbed in.
  38. *
  39. * Not yet done: How accurate are the temperature and voltage
  40. * readings? (System-specific calibration should support
  41. * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
  42. *
  43. * IRQ handling needs a workaround because of a shortcoming in handling
  44. * edge triggered IRQs on some platforms like the OMAP1/2. These
  45. * platforms don't handle the ARM lazy IRQ disabling properly, thus we
  46. * have to maintain our own SW IRQ disabled status. This should be
  47. * removed as soon as the affected platform's IRQ handling is fixed.
  48. *
  49. * app note sbaa036 talks in more detail about accurate sampling...
  50. * that ought to help in situations like LCDs inducing noise (which
  51. * can also be helped by using synch signals) and more generally.
  52. * This driver tries to utilize the measures described in the app
  53. * note. The strength of filtering can be set in the board-* specific
  54. * files.
  55. */
  56. #define TS_POLL_PERIOD msecs_to_jiffies(10)
  57. /* this driver doesn't aim at the peak continuous sample rate */
  58. #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
  59. struct ts_event {
  60. /* For portability, we can't read 12 bit values using SPI (which
  61. * would make the controller deliver them as native byteorder u16
  62. * with msbs zeroed). Instead, we read them as two 8-bit values,
  63. * which need byteswapping then range adjustment.
  64. */
  65. __be16 x;
  66. __be16 y;
  67. __be16 z1, z2;
  68. };
  69. struct ads7846 {
  70. struct input_dev *input;
  71. char phys[32];
  72. struct spi_device *spi;
  73. u16 model;
  74. u16 vref_delay_usecs;
  75. u16 x_plate_ohms;
  76. u8 read_x, read_y, read_z1, read_z2, pwrdown;
  77. u16 dummy; /* for the pwrdown read */
  78. struct ts_event tc;
  79. struct spi_transfer xfer[10];
  80. struct spi_message msg[5];
  81. int msg_idx;
  82. int read_cnt;
  83. int last_read;
  84. u16 debounce_max;
  85. u16 debounce_tol;
  86. spinlock_t lock;
  87. struct timer_list timer; /* P: lock */
  88. unsigned pendown:1; /* P: lock */
  89. unsigned pending:1; /* P: lock */
  90. // FIXME remove "irq_disabled"
  91. unsigned irq_disabled:1; /* P: lock */
  92. unsigned disabled:1;
  93. };
  94. /* leave chip selected when we're done, for quicker re-select? */
  95. #if 0
  96. #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
  97. #else
  98. #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
  99. #endif
  100. /*--------------------------------------------------------------------------*/
  101. /* The ADS7846 has touchscreen and other sensors.
  102. * Earlier ads784x chips are somewhat compatible.
  103. */
  104. #define ADS_START (1 << 7)
  105. #define ADS_A2A1A0_d_y (1 << 4) /* differential */
  106. #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
  107. #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
  108. #define ADS_A2A1A0_d_x (5 << 4) /* differential */
  109. #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
  110. #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
  111. #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
  112. #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
  113. #define ADS_8_BIT (1 << 3)
  114. #define ADS_12_BIT (0 << 3)
  115. #define ADS_SER (1 << 2) /* non-differential */
  116. #define ADS_DFR (0 << 2) /* differential */
  117. #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
  118. #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
  119. #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
  120. #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
  121. #define MAX_12BIT ((1<<12)-1)
  122. /* leave ADC powered up (disables penirq) between differential samples */
  123. #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
  124. | ADS_12_BIT | ADS_DFR)
  125. #define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
  126. #define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
  127. #define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
  128. #define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON)
  129. #define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */
  130. /* single-ended samples need to first power up reference voltage;
  131. * we leave both ADC and VREF powered
  132. */
  133. #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
  134. | ADS_12_BIT | ADS_SER)
  135. #define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
  136. #define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
  137. /*--------------------------------------------------------------------------*/
  138. /*
  139. * Non-touchscreen sensors only use single-ended conversions.
  140. */
  141. struct ser_req {
  142. u8 ref_on;
  143. u8 command;
  144. u8 ref_off;
  145. u16 scratch;
  146. __be16 sample;
  147. struct spi_message msg;
  148. struct spi_transfer xfer[6];
  149. };
  150. static void ads7846_enable(struct ads7846 *ts);
  151. static void ads7846_disable(struct ads7846 *ts);
  152. static int ads7846_read12_ser(struct device *dev, unsigned command)
  153. {
  154. struct spi_device *spi = to_spi_device(dev);
  155. struct ads7846 *ts = dev_get_drvdata(dev);
  156. struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
  157. int status;
  158. int sample;
  159. int i;
  160. if (!req)
  161. return -ENOMEM;
  162. spi_message_init(&req->msg);
  163. /* activate reference, so it has time to settle; */
  164. req->ref_on = REF_ON;
  165. req->xfer[0].tx_buf = &req->ref_on;
  166. req->xfer[0].len = 1;
  167. req->xfer[1].rx_buf = &req->scratch;
  168. req->xfer[1].len = 2;
  169. /*
  170. * for external VREF, 0 usec (and assume it's always on);
  171. * for 1uF, use 800 usec;
  172. * no cap, 100 usec.
  173. */
  174. req->xfer[1].delay_usecs = ts->vref_delay_usecs;
  175. /* take sample */
  176. req->command = (u8) command;
  177. req->xfer[2].tx_buf = &req->command;
  178. req->xfer[2].len = 1;
  179. req->xfer[3].rx_buf = &req->sample;
  180. req->xfer[3].len = 2;
  181. /* REVISIT: take a few more samples, and compare ... */
  182. /* turn off reference */
  183. req->ref_off = REF_OFF;
  184. req->xfer[4].tx_buf = &req->ref_off;
  185. req->xfer[4].len = 1;
  186. req->xfer[5].rx_buf = &req->scratch;
  187. req->xfer[5].len = 2;
  188. CS_CHANGE(req->xfer[5]);
  189. /* group all the transfers together, so we can't interfere with
  190. * reading touchscreen state; disable penirq while sampling
  191. */
  192. for (i = 0; i < 6; i++)
  193. spi_message_add_tail(&req->xfer[i], &req->msg);
  194. disable_irq(spi->irq);
  195. status = spi_sync(spi, &req->msg);
  196. enable_irq(spi->irq);
  197. if (req->msg.status)
  198. status = req->msg.status;
  199. sample = be16_to_cpu(req->sample);
  200. sample = sample >> 4;
  201. kfree(req);
  202. return status ? status : sample;
  203. }
  204. #define SHOW(name) static ssize_t \
  205. name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
  206. { \
  207. ssize_t v = ads7846_read12_ser(dev, \
  208. READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
  209. if (v < 0) \
  210. return v; \
  211. return sprintf(buf, "%u\n", (unsigned) v); \
  212. } \
  213. static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
  214. SHOW(temp0)
  215. SHOW(temp1)
  216. SHOW(vaux)
  217. SHOW(vbatt)
  218. static int is_pen_down(struct device *dev)
  219. {
  220. struct ads7846 *ts = dev_get_drvdata(dev);
  221. return ts->pendown;
  222. }
  223. static ssize_t ads7846_pen_down_show(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. return sprintf(buf, "%u\n", is_pen_down(dev));
  227. }
  228. static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
  229. static ssize_t ads7846_disable_show(struct device *dev,
  230. struct device_attribute *attr, char *buf)
  231. {
  232. struct ads7846 *ts = dev_get_drvdata(dev);
  233. return sprintf(buf, "%u\n", ts->disabled);
  234. }
  235. static ssize_t ads7846_disable_store(struct device *dev,
  236. struct device_attribute *attr,
  237. const char *buf, size_t count)
  238. {
  239. struct ads7846 *ts = dev_get_drvdata(dev);
  240. char *endp;
  241. int i;
  242. i = simple_strtoul(buf, &endp, 10);
  243. spin_lock_irq(&ts->lock);
  244. if (i)
  245. ads7846_disable(ts);
  246. else
  247. ads7846_enable(ts);
  248. spin_unlock_irq(&ts->lock);
  249. return count;
  250. }
  251. static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
  252. /*--------------------------------------------------------------------------*/
  253. /*
  254. * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
  255. * to retrieve touchscreen status.
  256. *
  257. * The SPI transfer completion callback does the real work. It reports
  258. * touchscreen events and reactivates the timer (or IRQ) as appropriate.
  259. */
  260. static void ads7846_rx(void *ads)
  261. {
  262. struct ads7846 *ts = ads;
  263. struct input_dev *input_dev = ts->input;
  264. unsigned Rt;
  265. unsigned sync = 0;
  266. u16 x, y, z1, z2;
  267. unsigned long flags;
  268. /* adjust: 12 bit samples (left aligned), built from
  269. * two 8 bit values writen msb-first.
  270. */
  271. x = be16_to_cpu(ts->tc.x) >> 4;
  272. y = be16_to_cpu(ts->tc.y) >> 4;
  273. z1 = be16_to_cpu(ts->tc.z1) >> 4;
  274. z2 = be16_to_cpu(ts->tc.z2) >> 4;
  275. /* range filtering */
  276. if (x == MAX_12BIT)
  277. x = 0;
  278. if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
  279. /* compute touch pressure resistance using equation #2 */
  280. Rt = z2;
  281. Rt -= z1;
  282. Rt *= x;
  283. Rt *= ts->x_plate_ohms;
  284. Rt /= z1;
  285. Rt = (Rt + 2047) >> 12;
  286. } else
  287. Rt = 0;
  288. /* NOTE: "pendown" is inferred from pressure; we don't rely on
  289. * being able to check nPENIRQ status, or "friendly" trigger modes
  290. * (both-edges is much better than just-falling or low-level).
  291. *
  292. * REVISIT: some boards may require reading nPENIRQ; it's
  293. * needed on 7843. and 7845 reads pressure differently...
  294. *
  295. * REVISIT: the touchscreen might not be connected; this code
  296. * won't notice that, even if nPENIRQ never fires ...
  297. */
  298. if (!ts->pendown && Rt != 0) {
  299. input_report_key(input_dev, BTN_TOUCH, 1);
  300. sync = 1;
  301. } else if (ts->pendown && Rt == 0) {
  302. input_report_key(input_dev, BTN_TOUCH, 0);
  303. sync = 1;
  304. }
  305. if (Rt) {
  306. input_report_abs(input_dev, ABS_X, x);
  307. input_report_abs(input_dev, ABS_Y, y);
  308. input_report_abs(input_dev, ABS_PRESSURE, Rt);
  309. sync = 1;
  310. }
  311. if (sync)
  312. input_sync(input_dev);
  313. #ifdef VERBOSE
  314. if (Rt || ts->pendown)
  315. pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
  316. x, y, Rt, Rt ? "" : " UP");
  317. #endif
  318. /* don't retrigger while we're suspended */
  319. spin_lock_irqsave(&ts->lock, flags);
  320. ts->pendown = (Rt != 0);
  321. ts->pending = 0;
  322. if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
  323. if (ts->pendown)
  324. mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
  325. else if (ts->irq_disabled) {
  326. ts->irq_disabled = 0;
  327. enable_irq(ts->spi->irq);
  328. }
  329. }
  330. spin_unlock_irqrestore(&ts->lock, flags);
  331. }
  332. static void ads7846_debounce(void *ads)
  333. {
  334. struct ads7846 *ts = ads;
  335. struct spi_message *m;
  336. struct spi_transfer *t;
  337. u16 val;
  338. int status;
  339. m = &ts->msg[ts->msg_idx];
  340. t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
  341. val = (*(u16 *)t->rx_buf) >> 3;
  342. if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol
  343. && ts->read_cnt < ts->debounce_max)) {
  344. /* Repeat it, if this was the first read or the read wasn't
  345. * consistent enough
  346. */
  347. ts->read_cnt++;
  348. ts->last_read = val;
  349. } else {
  350. /* Go for the next read */
  351. ts->msg_idx++;
  352. ts->read_cnt = 0;
  353. m++;
  354. }
  355. status = spi_async(ts->spi, m);
  356. if (status)
  357. dev_err(&ts->spi->dev, "spi_async --> %d\n",
  358. status);
  359. }
  360. static void ads7846_timer(unsigned long handle)
  361. {
  362. struct ads7846 *ts = (void *)handle;
  363. int status = 0;
  364. ts->msg_idx = 0;
  365. status = spi_async(ts->spi, &ts->msg[0]);
  366. if (status)
  367. dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
  368. }
  369. static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
  370. {
  371. struct ads7846 *ts = handle;
  372. unsigned long flags;
  373. spin_lock_irqsave(&ts->lock, flags);
  374. if (likely(!ts->irq_disabled && !ts->disabled)) {
  375. if (!ts->irq_disabled) {
  376. /* REVISIT irq logic for many ARM chips has cloned a
  377. * bug wherein disabling an irq in its handler won't
  378. * work;(it's disabled lazily, and too late to work.
  379. * until all their irq logic is fixed, we must shadow
  380. * that state here.
  381. */
  382. ts->irq_disabled = 1;
  383. disable_irq(ts->spi->irq);
  384. }
  385. if (!ts->pending) {
  386. ts->pending = 1;
  387. mod_timer(&ts->timer, jiffies);
  388. }
  389. }
  390. spin_unlock_irqrestore(&ts->lock, flags);
  391. return IRQ_HANDLED;
  392. }
  393. /*--------------------------------------------------------------------------*/
  394. /* Must be called with ts->lock held */
  395. static void ads7846_disable(struct ads7846 *ts)
  396. {
  397. if (ts->disabled)
  398. return;
  399. /* are we waiting for IRQ, or polling? */
  400. if (!ts->pendown) {
  401. if (!ts->irq_disabled) {
  402. ts->irq_disabled = 1;
  403. disable_irq(ts->spi->irq);
  404. }
  405. } else {
  406. /* polling; force a final SPI completion;
  407. * that will clean things up neatly
  408. */
  409. if (!ts->pending)
  410. mod_timer(&ts->timer, jiffies);
  411. while (ts->pendown || ts->pending) {
  412. spin_unlock_irq(&ts->lock);
  413. msleep(1);
  414. spin_lock_irq(&ts->lock);
  415. }
  416. }
  417. /* we know the chip's in lowpower mode since we always
  418. * leave it that way after every request
  419. */
  420. ts->disabled = 1;
  421. }
  422. /* Must be called with ts->lock held */
  423. static void ads7846_enable(struct ads7846 *ts)
  424. {
  425. if (!ts->disabled)
  426. return;
  427. ts->disabled = 0;
  428. ts->irq_disabled = 0;
  429. enable_irq(ts->spi->irq);
  430. }
  431. static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
  432. {
  433. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  434. spin_lock_irq(&ts->lock);
  435. spi->dev.power.power_state = message;
  436. ads7846_disable(ts);
  437. spin_unlock_irq(&ts->lock);
  438. return 0;
  439. }
  440. static int ads7846_resume(struct spi_device *spi)
  441. {
  442. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  443. spin_lock_irq(&ts->lock);
  444. spi->dev.power.power_state = PMSG_ON;
  445. ads7846_enable(ts);
  446. spin_unlock_irq(&ts->lock);
  447. return 0;
  448. }
  449. static int __devinit ads7846_probe(struct spi_device *spi)
  450. {
  451. struct ads7846 *ts;
  452. struct input_dev *input_dev;
  453. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  454. struct spi_message *m;
  455. struct spi_transfer *x;
  456. int err;
  457. if (!spi->irq) {
  458. dev_dbg(&spi->dev, "no IRQ?\n");
  459. return -ENODEV;
  460. }
  461. if (!pdata) {
  462. dev_dbg(&spi->dev, "no platform data?\n");
  463. return -ENODEV;
  464. }
  465. /* don't exceed max specified sample rate */
  466. if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
  467. dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
  468. (spi->max_speed_hz/SAMPLE_BITS)/1000);
  469. return -EINVAL;
  470. }
  471. /* We'd set the wordsize to 12 bits ... except that some controllers
  472. * will then treat the 8 bit command words as 12 bits (and drop the
  473. * four MSBs of the 12 bit result). Result: inputs must be shifted
  474. * to discard the four garbage LSBs.
  475. */
  476. ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
  477. input_dev = input_allocate_device();
  478. if (!ts || !input_dev) {
  479. err = -ENOMEM;
  480. goto err_free_mem;
  481. }
  482. dev_set_drvdata(&spi->dev, ts);
  483. spi->dev.power.power_state = PMSG_ON;
  484. ts->spi = spi;
  485. ts->input = input_dev;
  486. init_timer(&ts->timer);
  487. ts->timer.data = (unsigned long) ts;
  488. ts->timer.function = ads7846_timer;
  489. spin_lock_init(&ts->lock);
  490. ts->model = pdata->model ? : 7846;
  491. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  492. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  493. ts->debounce_max = pdata->debounce_max ? : 1;
  494. ts->debounce_tol = pdata->debounce_tol ? : 10;
  495. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
  496. input_dev->name = "ADS784x Touchscreen";
  497. input_dev->phys = ts->phys;
  498. input_dev->cdev.dev = &spi->dev;
  499. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  500. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  501. input_set_abs_params(input_dev, ABS_X,
  502. pdata->x_min ? : 0,
  503. pdata->x_max ? : MAX_12BIT,
  504. 0, 0);
  505. input_set_abs_params(input_dev, ABS_Y,
  506. pdata->y_min ? : 0,
  507. pdata->y_max ? : MAX_12BIT,
  508. 0, 0);
  509. input_set_abs_params(input_dev, ABS_PRESSURE,
  510. pdata->pressure_min, pdata->pressure_max, 0, 0);
  511. /* set up the transfers to read touchscreen state; this assumes we
  512. * use formula #2 for pressure, not #3.
  513. */
  514. m = &ts->msg[0];
  515. x = ts->xfer;
  516. spi_message_init(m);
  517. /* y- still on; turn on only y+ (and ADC) */
  518. ts->read_y = READ_Y;
  519. x->tx_buf = &ts->read_y;
  520. x->len = 1;
  521. spi_message_add_tail(x, m);
  522. x++;
  523. x->rx_buf = &ts->tc.y;
  524. x->len = 2;
  525. spi_message_add_tail(x, m);
  526. m->complete = ads7846_debounce;
  527. m->context = ts;
  528. m++;
  529. spi_message_init(m);
  530. /* turn y- off, x+ on, then leave in lowpower */
  531. x++;
  532. ts->read_x = READ_X;
  533. x->tx_buf = &ts->read_x;
  534. x->len = 1;
  535. spi_message_add_tail(x, m);
  536. x++;
  537. x->rx_buf = &ts->tc.x;
  538. x->len = 2;
  539. spi_message_add_tail(x, m);
  540. m->complete = ads7846_debounce;
  541. m->context = ts;
  542. /* turn y+ off, x- on; we'll use formula #2 */
  543. if (ts->model == 7846) {
  544. m++;
  545. spi_message_init(m);
  546. x++;
  547. ts->read_z1 = READ_Z1;
  548. x->tx_buf = &ts->read_z1;
  549. x->len = 1;
  550. spi_message_add_tail(x, m);
  551. x++;
  552. x->rx_buf = &ts->tc.z1;
  553. x->len = 2;
  554. spi_message_add_tail(x, m);
  555. m->complete = ads7846_debounce;
  556. m->context = ts;
  557. m++;
  558. spi_message_init(m);
  559. x++;
  560. ts->read_z2 = READ_Z2;
  561. x->tx_buf = &ts->read_z2;
  562. x->len = 1;
  563. spi_message_add_tail(x, m);
  564. x++;
  565. x->rx_buf = &ts->tc.z2;
  566. x->len = 2;
  567. spi_message_add_tail(x, m);
  568. m->complete = ads7846_debounce;
  569. m->context = ts;
  570. }
  571. /* power down */
  572. m++;
  573. spi_message_init(m);
  574. x++;
  575. ts->pwrdown = PWRDOWN;
  576. x->tx_buf = &ts->pwrdown;
  577. x->len = 1;
  578. spi_message_add_tail(x, m);
  579. x++;
  580. x->rx_buf = &ts->dummy;
  581. x->len = 2;
  582. CS_CHANGE(*x);
  583. spi_message_add_tail(x, m);
  584. m->complete = ads7846_rx;
  585. m->context = ts;
  586. if (request_irq(spi->irq, ads7846_irq,
  587. SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
  588. spi->dev.bus_id, ts)) {
  589. dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
  590. err = -EBUSY;
  591. goto err_free_mem;
  592. }
  593. dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
  594. /* take a first sample, leaving nPENIRQ active; avoid
  595. * the touchscreen, in case it's not connected.
  596. */
  597. (void) ads7846_read12_ser(&spi->dev,
  598. READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
  599. /* ads7843/7845 don't have temperature sensors, and
  600. * use the other sensors a bit differently too
  601. */
  602. if (ts->model == 7846) {
  603. device_create_file(&spi->dev, &dev_attr_temp0);
  604. device_create_file(&spi->dev, &dev_attr_temp1);
  605. }
  606. if (ts->model != 7845)
  607. device_create_file(&spi->dev, &dev_attr_vbatt);
  608. device_create_file(&spi->dev, &dev_attr_vaux);
  609. device_create_file(&spi->dev, &dev_attr_pen_down);
  610. device_create_file(&spi->dev, &dev_attr_disable);
  611. err = input_register_device(input_dev);
  612. if (err)
  613. goto err_remove_attr;
  614. return 0;
  615. err_remove_attr:
  616. device_remove_file(&spi->dev, &dev_attr_disable);
  617. device_remove_file(&spi->dev, &dev_attr_pen_down);
  618. if (ts->model == 7846) {
  619. device_remove_file(&spi->dev, &dev_attr_temp1);
  620. device_remove_file(&spi->dev, &dev_attr_temp0);
  621. }
  622. if (ts->model != 7845)
  623. device_remove_file(&spi->dev, &dev_attr_vbatt);
  624. device_remove_file(&spi->dev, &dev_attr_vaux);
  625. free_irq(spi->irq, ts);
  626. err_free_mem:
  627. input_free_device(input_dev);
  628. kfree(ts);
  629. return err;
  630. }
  631. static int __devexit ads7846_remove(struct spi_device *spi)
  632. {
  633. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  634. input_unregister_device(ts->input);
  635. ads7846_suspend(spi, PMSG_SUSPEND);
  636. device_remove_file(&spi->dev, &dev_attr_disable);
  637. device_remove_file(&spi->dev, &dev_attr_pen_down);
  638. if (ts->model == 7846) {
  639. device_remove_file(&spi->dev, &dev_attr_temp1);
  640. device_remove_file(&spi->dev, &dev_attr_temp0);
  641. }
  642. if (ts->model != 7845)
  643. device_remove_file(&spi->dev, &dev_attr_vbatt);
  644. device_remove_file(&spi->dev, &dev_attr_vaux);
  645. free_irq(ts->spi->irq, ts);
  646. if (ts->irq_disabled)
  647. enable_irq(ts->spi->irq);
  648. kfree(ts);
  649. dev_dbg(&spi->dev, "unregistered touchscreen\n");
  650. return 0;
  651. }
  652. static struct spi_driver ads7846_driver = {
  653. .driver = {
  654. .name = "ads7846",
  655. .bus = &spi_bus_type,
  656. .owner = THIS_MODULE,
  657. },
  658. .probe = ads7846_probe,
  659. .remove = __devexit_p(ads7846_remove),
  660. .suspend = ads7846_suspend,
  661. .resume = ads7846_resume,
  662. };
  663. static int __init ads7846_init(void)
  664. {
  665. /* grr, board-specific init should stay out of drivers!! */
  666. #ifdef CONFIG_ARCH_OMAP
  667. if (machine_is_omap_osk()) {
  668. /* GPIO4 = PENIRQ; GPIO6 = BUSY */
  669. omap_request_gpio(4);
  670. omap_set_gpio_direction(4, 1);
  671. omap_request_gpio(6);
  672. omap_set_gpio_direction(6, 1);
  673. }
  674. // also TI 1510 Innovator, bitbanging through FPGA
  675. // also Nokia 770
  676. // also Palm Tungsten T2
  677. #endif
  678. // PXA:
  679. // also Dell Axim X50
  680. // also HP iPaq H191x/H192x/H415x/H435x
  681. // also Intel Lubbock (additional to UCB1400; as temperature sensor)
  682. // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
  683. // Atmel at91sam9261-EK uses ads7843
  684. // also various AMD Au1x00 devel boards
  685. return spi_register_driver(&ads7846_driver);
  686. }
  687. module_init(ads7846_init);
  688. static void __exit ads7846_exit(void)
  689. {
  690. spi_unregister_driver(&ads7846_driver);
  691. #ifdef CONFIG_ARCH_OMAP
  692. if (machine_is_omap_osk()) {
  693. omap_free_gpio(4);
  694. omap_free_gpio(6);
  695. }
  696. #endif
  697. }
  698. module_exit(ads7846_exit);
  699. MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
  700. MODULE_LICENSE("GPL");