ads7846.c 23 KB

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