ads7846.c 22 KB

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