ads7846.c 24 KB

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