ads7846.c 23 KB

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