ads7846.c 24 KB

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