ads7846.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. int (*get_pendown_state)(void);
  94. };
  95. /* leave chip selected when we're done, for quicker re-select? */
  96. #if 0
  97. #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
  98. #else
  99. #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
  100. #endif
  101. /*--------------------------------------------------------------------------*/
  102. /* The ADS7846 has touchscreen and other sensors.
  103. * Earlier ads784x chips are somewhat compatible.
  104. */
  105. #define ADS_START (1 << 7)
  106. #define ADS_A2A1A0_d_y (1 << 4) /* differential */
  107. #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
  108. #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
  109. #define ADS_A2A1A0_d_x (5 << 4) /* differential */
  110. #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
  111. #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
  112. #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
  113. #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
  114. #define ADS_8_BIT (1 << 3)
  115. #define ADS_12_BIT (0 << 3)
  116. #define ADS_SER (1 << 2) /* non-differential */
  117. #define ADS_DFR (0 << 2) /* differential */
  118. #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
  119. #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
  120. #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
  121. #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
  122. #define MAX_12BIT ((1<<12)-1)
  123. /* leave ADC powered up (disables penirq) between differential samples */
  124. #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
  125. | ADS_12_BIT | ADS_DFR)
  126. #define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
  127. #define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
  128. #define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
  129. #define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON)
  130. #define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */
  131. /* single-ended samples need to first power up reference voltage;
  132. * we leave both ADC and VREF powered
  133. */
  134. #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
  135. | ADS_12_BIT | ADS_SER)
  136. #define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
  137. #define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
  138. /*--------------------------------------------------------------------------*/
  139. /*
  140. * Non-touchscreen sensors only use single-ended conversions.
  141. */
  142. struct ser_req {
  143. u8 ref_on;
  144. u8 command;
  145. u8 ref_off;
  146. u16 scratch;
  147. __be16 sample;
  148. struct spi_message msg;
  149. struct spi_transfer xfer[6];
  150. };
  151. static void ads7846_enable(struct ads7846 *ts);
  152. static void ads7846_disable(struct ads7846 *ts);
  153. static int device_suspended(struct device *dev)
  154. {
  155. struct ads7846 *ts = dev_get_drvdata(dev);
  156. return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
  157. }
  158. static int ads7846_read12_ser(struct device *dev, unsigned command)
  159. {
  160. struct spi_device *spi = to_spi_device(dev);
  161. struct ads7846 *ts = dev_get_drvdata(dev);
  162. struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
  163. int status;
  164. int sample;
  165. int i;
  166. if (!req)
  167. return -ENOMEM;
  168. spi_message_init(&req->msg);
  169. /* activate reference, so it has time to settle; */
  170. req->ref_on = REF_ON;
  171. req->xfer[0].tx_buf = &req->ref_on;
  172. req->xfer[0].len = 1;
  173. req->xfer[1].rx_buf = &req->scratch;
  174. req->xfer[1].len = 2;
  175. /*
  176. * for external VREF, 0 usec (and assume it's always on);
  177. * for 1uF, use 800 usec;
  178. * no cap, 100 usec.
  179. */
  180. req->xfer[1].delay_usecs = ts->vref_delay_usecs;
  181. /* take sample */
  182. req->command = (u8) command;
  183. req->xfer[2].tx_buf = &req->command;
  184. req->xfer[2].len = 1;
  185. req->xfer[3].rx_buf = &req->sample;
  186. req->xfer[3].len = 2;
  187. /* REVISIT: take a few more samples, and compare ... */
  188. /* turn off reference */
  189. req->ref_off = REF_OFF;
  190. req->xfer[4].tx_buf = &req->ref_off;
  191. req->xfer[4].len = 1;
  192. req->xfer[5].rx_buf = &req->scratch;
  193. req->xfer[5].len = 2;
  194. CS_CHANGE(req->xfer[5]);
  195. /* group all the transfers together, so we can't interfere with
  196. * reading touchscreen state; disable penirq while sampling
  197. */
  198. for (i = 0; i < 6; i++)
  199. spi_message_add_tail(&req->xfer[i], &req->msg);
  200. ts->irq_disabled = 1;
  201. disable_irq(spi->irq);
  202. status = spi_sync(spi, &req->msg);
  203. ts->irq_disabled = 0;
  204. enable_irq(spi->irq);
  205. if (req->msg.status)
  206. status = req->msg.status;
  207. sample = be16_to_cpu(req->sample);
  208. sample = sample >> 4;
  209. kfree(req);
  210. return status ? status : sample;
  211. }
  212. #define SHOW(name) static ssize_t \
  213. name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
  214. { \
  215. ssize_t v = ads7846_read12_ser(dev, \
  216. READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
  217. if (v < 0) \
  218. return v; \
  219. return sprintf(buf, "%u\n", (unsigned) v); \
  220. } \
  221. static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
  222. SHOW(temp0)
  223. SHOW(temp1)
  224. SHOW(vaux)
  225. SHOW(vbatt)
  226. static int is_pen_down(struct device *dev)
  227. {
  228. struct ads7846 *ts = dev_get_drvdata(dev);
  229. return ts->pendown;
  230. }
  231. static ssize_t ads7846_pen_down_show(struct device *dev,
  232. struct device_attribute *attr, char *buf)
  233. {
  234. return sprintf(buf, "%u\n", is_pen_down(dev));
  235. }
  236. static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
  237. static ssize_t ads7846_disable_show(struct device *dev,
  238. struct device_attribute *attr, char *buf)
  239. {
  240. struct ads7846 *ts = dev_get_drvdata(dev);
  241. return sprintf(buf, "%u\n", ts->disabled);
  242. }
  243. static ssize_t ads7846_disable_store(struct device *dev,
  244. struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. struct ads7846 *ts = dev_get_drvdata(dev);
  248. char *endp;
  249. int i;
  250. i = simple_strtoul(buf, &endp, 10);
  251. spin_lock_irq(&ts->lock);
  252. if (i)
  253. ads7846_disable(ts);
  254. else
  255. ads7846_enable(ts);
  256. spin_unlock_irq(&ts->lock);
  257. return count;
  258. }
  259. static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
  260. /*--------------------------------------------------------------------------*/
  261. /*
  262. * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
  263. * to retrieve touchscreen status.
  264. *
  265. * The SPI transfer completion callback does the real work. It reports
  266. * touchscreen events and reactivates the timer (or IRQ) as appropriate.
  267. */
  268. static void ads7846_rx(void *ads)
  269. {
  270. struct ads7846 *ts = ads;
  271. struct input_dev *input_dev = ts->input;
  272. unsigned Rt;
  273. unsigned sync = 0;
  274. u16 x, y, z1, z2;
  275. unsigned long flags;
  276. /* adjust: 12 bit samples (left aligned), built from
  277. * two 8 bit values writen msb-first.
  278. */
  279. x = be16_to_cpu(ts->tc.x) >> 4;
  280. y = be16_to_cpu(ts->tc.y) >> 4;
  281. z1 = be16_to_cpu(ts->tc.z1) >> 4;
  282. z2 = be16_to_cpu(ts->tc.z2) >> 4;
  283. /* range filtering */
  284. if (x == MAX_12BIT)
  285. x = 0;
  286. if (likely(x && z1 && !device_suspended(&ts->spi->dev))) {
  287. /* compute touch pressure resistance using equation #2 */
  288. Rt = z2;
  289. Rt -= z1;
  290. Rt *= x;
  291. Rt *= ts->x_plate_ohms;
  292. Rt /= z1;
  293. Rt = (Rt + 2047) >> 12;
  294. } else
  295. Rt = 0;
  296. /* NOTE: "pendown" is inferred from pressure; we don't rely on
  297. * being able to check nPENIRQ status, or "friendly" trigger modes
  298. * (both-edges is much better than just-falling or low-level).
  299. *
  300. * REVISIT: some boards may require reading nPENIRQ; it's
  301. * needed on 7843. and 7845 reads pressure differently...
  302. *
  303. * REVISIT: the touchscreen might not be connected; this code
  304. * won't notice that, even if nPENIRQ never fires ...
  305. */
  306. if (!ts->pendown && Rt != 0) {
  307. input_report_key(input_dev, BTN_TOUCH, 1);
  308. sync = 1;
  309. } else if (ts->pendown && Rt == 0) {
  310. input_report_key(input_dev, BTN_TOUCH, 0);
  311. sync = 1;
  312. }
  313. if (Rt) {
  314. input_report_abs(input_dev, ABS_X, x);
  315. input_report_abs(input_dev, ABS_Y, y);
  316. sync = 1;
  317. }
  318. if (sync) {
  319. input_report_abs(input_dev, ABS_PRESSURE, Rt);
  320. input_sync(input_dev);
  321. }
  322. #ifdef VERBOSE
  323. if (Rt || ts->pendown)
  324. pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
  325. x, y, Rt, Rt ? "" : " UP");
  326. #endif
  327. spin_lock_irqsave(&ts->lock, flags);
  328. ts->pendown = (Rt != 0);
  329. mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
  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. spin_lock_irq(&ts->lock);
  365. if (unlikely(ts->msg_idx && !ts->pendown)) {
  366. /* measurment cycle ended */
  367. if (!device_suspended(&ts->spi->dev)) {
  368. ts->irq_disabled = 0;
  369. enable_irq(ts->spi->irq);
  370. }
  371. ts->pending = 0;
  372. ts->msg_idx = 0;
  373. } else {
  374. /* pen is still down, continue with the measurement */
  375. ts->msg_idx = 0;
  376. status = spi_async(ts->spi, &ts->msg[0]);
  377. if (status)
  378. dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
  379. }
  380. spin_unlock_irq(&ts->lock);
  381. }
  382. static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
  383. {
  384. struct ads7846 *ts = handle;
  385. unsigned long flags;
  386. spin_lock_irqsave(&ts->lock, flags);
  387. if (likely(ts->get_pendown_state())) {
  388. if (!ts->irq_disabled) {
  389. /* REVISIT irq logic for many ARM chips has cloned a
  390. * bug wherein disabling an irq in its handler won't
  391. * work;(it's disabled lazily, and too late to work.
  392. * until all their irq logic is fixed, we must shadow
  393. * that state here.
  394. */
  395. ts->irq_disabled = 1;
  396. disable_irq(ts->spi->irq);
  397. ts->pending = 1;
  398. mod_timer(&ts->timer, jiffies);
  399. }
  400. }
  401. spin_unlock_irqrestore(&ts->lock, flags);
  402. return IRQ_HANDLED;
  403. }
  404. /*--------------------------------------------------------------------------*/
  405. /* Must be called with ts->lock held */
  406. static void ads7846_disable(struct ads7846 *ts)
  407. {
  408. if (ts->disabled)
  409. return;
  410. ts->disabled = 1;
  411. /* are we waiting for IRQ, or polling? */
  412. if (!ts->pending) {
  413. ts->irq_disabled = 1;
  414. disable_irq(ts->spi->irq);
  415. } else {
  416. /* the timer will run at least once more, and
  417. * leave everything in a clean state, IRQ disabled
  418. */
  419. while (ts->pending) {
  420. spin_unlock_irq(&ts->lock);
  421. msleep(1);
  422. spin_lock_irq(&ts->lock);
  423. }
  424. }
  425. /* we know the chip's in lowpower mode since we always
  426. * leave it that way after every request
  427. */
  428. }
  429. /* Must be called with ts->lock held */
  430. static void ads7846_enable(struct ads7846 *ts)
  431. {
  432. if (!ts->disabled)
  433. return;
  434. ts->disabled = 0;
  435. ts->irq_disabled = 0;
  436. enable_irq(ts->spi->irq);
  437. }
  438. static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
  439. {
  440. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  441. spin_lock_irq(&ts->lock);
  442. spi->dev.power.power_state = message;
  443. ads7846_disable(ts);
  444. spin_unlock_irq(&ts->lock);
  445. return 0;
  446. }
  447. static int ads7846_resume(struct spi_device *spi)
  448. {
  449. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  450. spin_lock_irq(&ts->lock);
  451. spi->dev.power.power_state = PMSG_ON;
  452. ads7846_enable(ts);
  453. spin_unlock_irq(&ts->lock);
  454. return 0;
  455. }
  456. static int __devinit ads7846_probe(struct spi_device *spi)
  457. {
  458. struct ads7846 *ts;
  459. struct input_dev *input_dev;
  460. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  461. struct spi_message *m;
  462. struct spi_transfer *x;
  463. int err;
  464. if (!spi->irq) {
  465. dev_dbg(&spi->dev, "no IRQ?\n");
  466. return -ENODEV;
  467. }
  468. if (!pdata) {
  469. dev_dbg(&spi->dev, "no platform data?\n");
  470. return -ENODEV;
  471. }
  472. /* don't exceed max specified sample rate */
  473. if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
  474. dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
  475. (spi->max_speed_hz/SAMPLE_BITS)/1000);
  476. return -EINVAL;
  477. }
  478. if (pdata->get_pendown_state == NULL) {
  479. dev_dbg(&spi->dev, "no get_pendown_state function?\n");
  480. return -EINVAL;
  481. }
  482. /* We'd set the wordsize to 12 bits ... except that some controllers
  483. * will then treat the 8 bit command words as 12 bits (and drop the
  484. * four MSBs of the 12 bit result). Result: inputs must be shifted
  485. * to discard the four garbage LSBs.
  486. */
  487. ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
  488. input_dev = input_allocate_device();
  489. if (!ts || !input_dev) {
  490. err = -ENOMEM;
  491. goto err_free_mem;
  492. }
  493. dev_set_drvdata(&spi->dev, ts);
  494. spi->dev.power.power_state = PMSG_ON;
  495. ts->spi = spi;
  496. ts->input = input_dev;
  497. init_timer(&ts->timer);
  498. ts->timer.data = (unsigned long) ts;
  499. ts->timer.function = ads7846_timer;
  500. spin_lock_init(&ts->lock);
  501. ts->model = pdata->model ? : 7846;
  502. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  503. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  504. ts->debounce_max = pdata->debounce_max ? : 1;
  505. ts->debounce_tol = pdata->debounce_tol ? : 10;
  506. ts->get_pendown_state = pdata->get_pendown_state;
  507. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
  508. input_dev->name = "ADS784x Touchscreen";
  509. input_dev->phys = ts->phys;
  510. input_dev->cdev.dev = &spi->dev;
  511. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  512. input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
  513. input_set_abs_params(input_dev, ABS_X,
  514. pdata->x_min ? : 0,
  515. pdata->x_max ? : MAX_12BIT,
  516. 0, 0);
  517. input_set_abs_params(input_dev, ABS_Y,
  518. pdata->y_min ? : 0,
  519. pdata->y_max ? : MAX_12BIT,
  520. 0, 0);
  521. input_set_abs_params(input_dev, ABS_PRESSURE,
  522. pdata->pressure_min, pdata->pressure_max, 0, 0);
  523. /* set up the transfers to read touchscreen state; this assumes we
  524. * use formula #2 for pressure, not #3.
  525. */
  526. m = &ts->msg[0];
  527. x = ts->xfer;
  528. spi_message_init(m);
  529. /* y- still on; turn on only y+ (and ADC) */
  530. ts->read_y = READ_Y;
  531. x->tx_buf = &ts->read_y;
  532. x->len = 1;
  533. spi_message_add_tail(x, m);
  534. x++;
  535. x->rx_buf = &ts->tc.y;
  536. x->len = 2;
  537. spi_message_add_tail(x, m);
  538. m->complete = ads7846_debounce;
  539. m->context = ts;
  540. m++;
  541. spi_message_init(m);
  542. /* turn y- off, x+ on, then leave in lowpower */
  543. x++;
  544. ts->read_x = READ_X;
  545. x->tx_buf = &ts->read_x;
  546. x->len = 1;
  547. spi_message_add_tail(x, m);
  548. x++;
  549. x->rx_buf = &ts->tc.x;
  550. x->len = 2;
  551. spi_message_add_tail(x, m);
  552. m->complete = ads7846_debounce;
  553. m->context = ts;
  554. /* turn y+ off, x- on; we'll use formula #2 */
  555. if (ts->model == 7846) {
  556. m++;
  557. spi_message_init(m);
  558. x++;
  559. ts->read_z1 = READ_Z1;
  560. x->tx_buf = &ts->read_z1;
  561. x->len = 1;
  562. spi_message_add_tail(x, m);
  563. x++;
  564. x->rx_buf = &ts->tc.z1;
  565. x->len = 2;
  566. spi_message_add_tail(x, m);
  567. m->complete = ads7846_debounce;
  568. m->context = ts;
  569. m++;
  570. spi_message_init(m);
  571. x++;
  572. ts->read_z2 = READ_Z2;
  573. x->tx_buf = &ts->read_z2;
  574. x->len = 1;
  575. spi_message_add_tail(x, m);
  576. x++;
  577. x->rx_buf = &ts->tc.z2;
  578. x->len = 2;
  579. spi_message_add_tail(x, m);
  580. m->complete = ads7846_debounce;
  581. m->context = ts;
  582. }
  583. /* power down */
  584. m++;
  585. spi_message_init(m);
  586. x++;
  587. ts->pwrdown = PWRDOWN;
  588. x->tx_buf = &ts->pwrdown;
  589. x->len = 1;
  590. spi_message_add_tail(x, m);
  591. x++;
  592. x->rx_buf = &ts->dummy;
  593. x->len = 2;
  594. CS_CHANGE(*x);
  595. spi_message_add_tail(x, m);
  596. m->complete = ads7846_rx;
  597. m->context = ts;
  598. if (request_irq(spi->irq, ads7846_irq,
  599. SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
  600. spi->dev.bus_id, ts)) {
  601. dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
  602. err = -EBUSY;
  603. goto err_free_mem;
  604. }
  605. dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
  606. /* take a first sample, leaving nPENIRQ active; avoid
  607. * the touchscreen, in case it's not connected.
  608. */
  609. (void) ads7846_read12_ser(&spi->dev,
  610. READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
  611. /* ads7843/7845 don't have temperature sensors, and
  612. * use the other sensors a bit differently too
  613. */
  614. if (ts->model == 7846) {
  615. device_create_file(&spi->dev, &dev_attr_temp0);
  616. device_create_file(&spi->dev, &dev_attr_temp1);
  617. }
  618. if (ts->model != 7845)
  619. device_create_file(&spi->dev, &dev_attr_vbatt);
  620. device_create_file(&spi->dev, &dev_attr_vaux);
  621. device_create_file(&spi->dev, &dev_attr_pen_down);
  622. device_create_file(&spi->dev, &dev_attr_disable);
  623. err = input_register_device(input_dev);
  624. if (err)
  625. goto err_remove_attr;
  626. return 0;
  627. err_remove_attr:
  628. device_remove_file(&spi->dev, &dev_attr_disable);
  629. device_remove_file(&spi->dev, &dev_attr_pen_down);
  630. if (ts->model == 7846) {
  631. device_remove_file(&spi->dev, &dev_attr_temp1);
  632. device_remove_file(&spi->dev, &dev_attr_temp0);
  633. }
  634. if (ts->model != 7845)
  635. device_remove_file(&spi->dev, &dev_attr_vbatt);
  636. device_remove_file(&spi->dev, &dev_attr_vaux);
  637. free_irq(spi->irq, ts);
  638. err_free_mem:
  639. input_free_device(input_dev);
  640. kfree(ts);
  641. return err;
  642. }
  643. static int __devexit ads7846_remove(struct spi_device *spi)
  644. {
  645. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  646. input_unregister_device(ts->input);
  647. ads7846_suspend(spi, PMSG_SUSPEND);
  648. device_remove_file(&spi->dev, &dev_attr_disable);
  649. device_remove_file(&spi->dev, &dev_attr_pen_down);
  650. if (ts->model == 7846) {
  651. device_remove_file(&spi->dev, &dev_attr_temp1);
  652. device_remove_file(&spi->dev, &dev_attr_temp0);
  653. }
  654. if (ts->model != 7845)
  655. device_remove_file(&spi->dev, &dev_attr_vbatt);
  656. device_remove_file(&spi->dev, &dev_attr_vaux);
  657. free_irq(ts->spi->irq, ts);
  658. /* suspend left the IRQ disabled */
  659. enable_irq(ts->spi->irq);
  660. kfree(ts);
  661. dev_dbg(&spi->dev, "unregistered touchscreen\n");
  662. return 0;
  663. }
  664. static struct spi_driver ads7846_driver = {
  665. .driver = {
  666. .name = "ads7846",
  667. .bus = &spi_bus_type,
  668. .owner = THIS_MODULE,
  669. },
  670. .probe = ads7846_probe,
  671. .remove = __devexit_p(ads7846_remove),
  672. .suspend = ads7846_suspend,
  673. .resume = ads7846_resume,
  674. };
  675. static int __init ads7846_init(void)
  676. {
  677. /* grr, board-specific init should stay out of drivers!! */
  678. #ifdef CONFIG_ARCH_OMAP
  679. if (machine_is_omap_osk()) {
  680. /* GPIO4 = PENIRQ; GPIO6 = BUSY */
  681. omap_request_gpio(4);
  682. omap_set_gpio_direction(4, 1);
  683. omap_request_gpio(6);
  684. omap_set_gpio_direction(6, 1);
  685. }
  686. // also TI 1510 Innovator, bitbanging through FPGA
  687. // also Nokia 770
  688. // also Palm Tungsten T2
  689. #endif
  690. // PXA:
  691. // also Dell Axim X50
  692. // also HP iPaq H191x/H192x/H415x/H435x
  693. // also Intel Lubbock (additional to UCB1400; as temperature sensor)
  694. // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
  695. // Atmel at91sam9261-EK uses ads7843
  696. // also various AMD Au1x00 devel boards
  697. return spi_register_driver(&ads7846_driver);
  698. }
  699. module_init(ads7846_init);
  700. static void __exit ads7846_exit(void)
  701. {
  702. spi_unregister_driver(&ads7846_driver);
  703. #ifdef CONFIG_ARCH_OMAP
  704. if (machine_is_omap_osk()) {
  705. omap_free_gpio(4);
  706. omap_free_gpio(6);
  707. }
  708. #endif
  709. }
  710. module_exit(ads7846_exit);
  711. MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
  712. MODULE_LICENSE("GPL");