ads7846.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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/hwmon.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/delay.h>
  24. #include <linux/input.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/slab.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/ads7846.h>
  29. #include <asm/irq.h>
  30. /*
  31. * This code has been heavily tested on a Nokia 770, and lightly
  32. * tested on other ads7846 devices (OSK/Mistral, Lubbock).
  33. * TSC2046 is just newer ads7846 silicon.
  34. * Support for ads7843 tested on Atmel at91sam926x-EK.
  35. * Support for ads7845 has only been stubbed in.
  36. *
  37. * IRQ handling needs a workaround because of a shortcoming in handling
  38. * edge triggered IRQs on some platforms like the OMAP1/2. These
  39. * platforms don't handle the ARM lazy IRQ disabling properly, thus we
  40. * have to maintain our own SW IRQ disabled status. This should be
  41. * removed as soon as the affected platform's IRQ handling is fixed.
  42. *
  43. * app note sbaa036 talks in more detail about accurate sampling...
  44. * that ought to help in situations like LCDs inducing noise (which
  45. * can also be helped by using synch signals) and more generally.
  46. * This driver tries to utilize the measures described in the app
  47. * note. The strength of filtering can be set in the board-* specific
  48. * files.
  49. */
  50. #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
  51. #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
  52. /* this driver doesn't aim at the peak continuous sample rate */
  53. #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
  54. struct ts_event {
  55. /* For portability, we can't read 12 bit values using SPI (which
  56. * would make the controller deliver them as native byteorder u16
  57. * with msbs zeroed). Instead, we read them as two 8-bit values,
  58. * *** WHICH NEED BYTESWAPPING *** and range adjustment.
  59. */
  60. u16 x;
  61. u16 y;
  62. u16 z1, z2;
  63. int ignore;
  64. };
  65. struct ads7846 {
  66. struct input_dev *input;
  67. char phys[32];
  68. struct spi_device *spi;
  69. #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
  70. struct attribute_group *attr_group;
  71. struct device *hwmon;
  72. #endif
  73. u16 model;
  74. u16 vref_mv;
  75. u16 vref_delay_usecs;
  76. u16 x_plate_ohms;
  77. u16 pressure_max;
  78. u8 read_x, read_y, read_z1, read_z2, pwrdown;
  79. u16 dummy; /* for the pwrdown read */
  80. struct ts_event tc;
  81. struct spi_transfer xfer[18];
  82. struct spi_message msg[5];
  83. struct spi_message *last_msg;
  84. int msg_idx;
  85. int read_cnt;
  86. int read_rep;
  87. int last_read;
  88. u16 debounce_max;
  89. u16 debounce_tol;
  90. u16 debounce_rep;
  91. u16 penirq_recheck_delay_usecs;
  92. spinlock_t lock;
  93. struct hrtimer timer;
  94. unsigned pendown:1; /* P: lock */
  95. unsigned pending:1; /* P: lock */
  96. // FIXME remove "irq_disabled"
  97. unsigned irq_disabled:1; /* P: lock */
  98. unsigned disabled:1;
  99. unsigned is_suspended:1;
  100. int (*filter)(void *data, int data_idx, int *val);
  101. void *filter_data;
  102. void (*filter_cleanup)(void *data);
  103. int (*get_pendown_state)(void);
  104. };
  105. /* leave chip selected when we're done, for quicker re-select? */
  106. #if 0
  107. #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
  108. #else
  109. #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
  110. #endif
  111. /*--------------------------------------------------------------------------*/
  112. /* The ADS7846 has touchscreen and other sensors.
  113. * Earlier ads784x chips are somewhat compatible.
  114. */
  115. #define ADS_START (1 << 7)
  116. #define ADS_A2A1A0_d_y (1 << 4) /* differential */
  117. #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
  118. #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
  119. #define ADS_A2A1A0_d_x (5 << 4) /* differential */
  120. #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
  121. #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
  122. #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
  123. #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
  124. #define ADS_8_BIT (1 << 3)
  125. #define ADS_12_BIT (0 << 3)
  126. #define ADS_SER (1 << 2) /* non-differential */
  127. #define ADS_DFR (0 << 2) /* differential */
  128. #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
  129. #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
  130. #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
  131. #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
  132. #define MAX_12BIT ((1<<12)-1)
  133. /* leave ADC powered up (disables penirq) between differential samples */
  134. #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
  135. | ADS_12_BIT | ADS_DFR | \
  136. (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
  137. #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
  138. #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
  139. #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
  140. #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
  141. #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
  142. /* single-ended samples need to first power up reference voltage;
  143. * we leave both ADC and VREF powered
  144. */
  145. #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
  146. | ADS_12_BIT | ADS_SER)
  147. #define REF_ON (READ_12BIT_DFR(x, 1, 1))
  148. #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
  149. /*--------------------------------------------------------------------------*/
  150. /*
  151. * Non-touchscreen sensors only use single-ended conversions.
  152. * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
  153. * ads7846 lets that pin be unconnected, to use internal vREF.
  154. */
  155. struct ser_req {
  156. u8 ref_on;
  157. u8 command;
  158. u8 ref_off;
  159. u16 scratch;
  160. __be16 sample;
  161. struct spi_message msg;
  162. struct spi_transfer xfer[6];
  163. };
  164. static void ads7846_enable(struct ads7846 *ts);
  165. static void ads7846_disable(struct ads7846 *ts);
  166. static int device_suspended(struct device *dev)
  167. {
  168. struct ads7846 *ts = dev_get_drvdata(dev);
  169. return ts->is_suspended || ts->disabled;
  170. }
  171. static int ads7846_read12_ser(struct device *dev, unsigned command)
  172. {
  173. struct spi_device *spi = to_spi_device(dev);
  174. struct ads7846 *ts = dev_get_drvdata(dev);
  175. struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
  176. int status;
  177. int use_internal;
  178. if (!req)
  179. return -ENOMEM;
  180. spi_message_init(&req->msg);
  181. /* FIXME boards with ads7846 might use external vref instead ... */
  182. use_internal = (ts->model == 7846);
  183. /* maybe turn on internal vREF, and let it settle */
  184. if (use_internal) {
  185. req->ref_on = REF_ON;
  186. req->xfer[0].tx_buf = &req->ref_on;
  187. req->xfer[0].len = 1;
  188. spi_message_add_tail(&req->xfer[0], &req->msg);
  189. req->xfer[1].rx_buf = &req->scratch;
  190. req->xfer[1].len = 2;
  191. /* for 1uF, settle for 800 usec; no cap, 100 usec. */
  192. req->xfer[1].delay_usecs = ts->vref_delay_usecs;
  193. spi_message_add_tail(&req->xfer[1], &req->msg);
  194. }
  195. /* take sample */
  196. req->command = (u8) command;
  197. req->xfer[2].tx_buf = &req->command;
  198. req->xfer[2].len = 1;
  199. spi_message_add_tail(&req->xfer[2], &req->msg);
  200. req->xfer[3].rx_buf = &req->sample;
  201. req->xfer[3].len = 2;
  202. spi_message_add_tail(&req->xfer[3], &req->msg);
  203. /* REVISIT: take a few more samples, and compare ... */
  204. /* converter in low power mode & enable PENIRQ */
  205. req->ref_off = PWRDOWN;
  206. req->xfer[4].tx_buf = &req->ref_off;
  207. req->xfer[4].len = 1;
  208. spi_message_add_tail(&req->xfer[4], &req->msg);
  209. req->xfer[5].rx_buf = &req->scratch;
  210. req->xfer[5].len = 2;
  211. CS_CHANGE(req->xfer[5]);
  212. spi_message_add_tail(&req->xfer[5], &req->msg);
  213. ts->irq_disabled = 1;
  214. disable_irq(spi->irq);
  215. status = spi_sync(spi, &req->msg);
  216. ts->irq_disabled = 0;
  217. enable_irq(spi->irq);
  218. if (status == 0) {
  219. /* on-wire is a must-ignore bit, a BE12 value, then padding */
  220. status = be16_to_cpu(req->sample);
  221. status = status >> 3;
  222. status &= 0x0fff;
  223. }
  224. kfree(req);
  225. return status;
  226. }
  227. #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
  228. #define SHOW(name, var, adjust) static ssize_t \
  229. name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
  230. { \
  231. struct ads7846 *ts = dev_get_drvdata(dev); \
  232. ssize_t v = ads7846_read12_ser(dev, \
  233. READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
  234. if (v < 0) \
  235. return v; \
  236. return sprintf(buf, "%u\n", adjust(ts, v)); \
  237. } \
  238. static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
  239. /* Sysfs conventions report temperatures in millidegrees Celcius.
  240. * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
  241. * accuracy scheme without calibration data. For now we won't try either;
  242. * userspace sees raw sensor values, and must scale/calibrate appropriately.
  243. */
  244. static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
  245. {
  246. return v;
  247. }
  248. SHOW(temp0, temp0, null_adjust) /* temp1_input */
  249. SHOW(temp1, temp1, null_adjust) /* temp2_input */
  250. /* sysfs conventions report voltages in millivolts. We can convert voltages
  251. * if we know vREF. userspace may need to scale vAUX to match the board's
  252. * external resistors; we assume that vBATT only uses the internal ones.
  253. */
  254. static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
  255. {
  256. unsigned retval = v;
  257. /* external resistors may scale vAUX into 0..vREF */
  258. retval *= ts->vref_mv;
  259. retval = retval >> 12;
  260. return retval;
  261. }
  262. static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
  263. {
  264. unsigned retval = vaux_adjust(ts, v);
  265. /* ads7846 has a resistor ladder to scale this signal down */
  266. if (ts->model == 7846)
  267. retval *= 4;
  268. return retval;
  269. }
  270. SHOW(in0_input, vaux, vaux_adjust)
  271. SHOW(in1_input, vbatt, vbatt_adjust)
  272. static struct attribute *ads7846_attributes[] = {
  273. &dev_attr_temp0.attr,
  274. &dev_attr_temp1.attr,
  275. &dev_attr_in0_input.attr,
  276. &dev_attr_in1_input.attr,
  277. NULL,
  278. };
  279. static struct attribute_group ads7846_attr_group = {
  280. .attrs = ads7846_attributes,
  281. };
  282. static struct attribute *ads7843_attributes[] = {
  283. &dev_attr_in0_input.attr,
  284. &dev_attr_in1_input.attr,
  285. NULL,
  286. };
  287. static struct attribute_group ads7843_attr_group = {
  288. .attrs = ads7843_attributes,
  289. };
  290. static struct attribute *ads7845_attributes[] = {
  291. &dev_attr_in0_input.attr,
  292. NULL,
  293. };
  294. static struct attribute_group ads7845_attr_group = {
  295. .attrs = ads7845_attributes,
  296. };
  297. static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
  298. {
  299. struct device *hwmon;
  300. int err;
  301. /* hwmon sensors need a reference voltage */
  302. switch (ts->model) {
  303. case 7846:
  304. if (!ts->vref_mv) {
  305. dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
  306. ts->vref_mv = 2500;
  307. }
  308. break;
  309. case 7845:
  310. case 7843:
  311. if (!ts->vref_mv) {
  312. dev_warn(&spi->dev,
  313. "external vREF for ADS%d not specified\n",
  314. ts->model);
  315. return 0;
  316. }
  317. break;
  318. }
  319. /* different chips have different sensor groups */
  320. switch (ts->model) {
  321. case 7846:
  322. ts->attr_group = &ads7846_attr_group;
  323. break;
  324. case 7845:
  325. ts->attr_group = &ads7845_attr_group;
  326. break;
  327. case 7843:
  328. ts->attr_group = &ads7843_attr_group;
  329. break;
  330. default:
  331. dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
  332. return 0;
  333. }
  334. err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
  335. if (err)
  336. return err;
  337. hwmon = hwmon_device_register(&spi->dev);
  338. if (IS_ERR(hwmon)) {
  339. sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
  340. return PTR_ERR(hwmon);
  341. }
  342. ts->hwmon = hwmon;
  343. return 0;
  344. }
  345. static void ads784x_hwmon_unregister(struct spi_device *spi,
  346. struct ads7846 *ts)
  347. {
  348. if (ts->hwmon) {
  349. sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
  350. hwmon_device_unregister(ts->hwmon);
  351. }
  352. }
  353. #else
  354. static inline int ads784x_hwmon_register(struct spi_device *spi,
  355. struct ads7846 *ts)
  356. {
  357. return 0;
  358. }
  359. static inline void ads784x_hwmon_unregister(struct spi_device *spi,
  360. struct ads7846 *ts)
  361. {
  362. }
  363. #endif
  364. static int is_pen_down(struct device *dev)
  365. {
  366. struct ads7846 *ts = dev_get_drvdata(dev);
  367. return ts->pendown;
  368. }
  369. static ssize_t ads7846_pen_down_show(struct device *dev,
  370. struct device_attribute *attr, char *buf)
  371. {
  372. return sprintf(buf, "%u\n", is_pen_down(dev));
  373. }
  374. static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
  375. static ssize_t ads7846_disable_show(struct device *dev,
  376. struct device_attribute *attr, char *buf)
  377. {
  378. struct ads7846 *ts = dev_get_drvdata(dev);
  379. return sprintf(buf, "%u\n", ts->disabled);
  380. }
  381. static ssize_t ads7846_disable_store(struct device *dev,
  382. struct device_attribute *attr,
  383. const char *buf, size_t count)
  384. {
  385. struct ads7846 *ts = dev_get_drvdata(dev);
  386. char *endp;
  387. int i;
  388. i = simple_strtoul(buf, &endp, 10);
  389. spin_lock_irq(&ts->lock);
  390. if (i)
  391. ads7846_disable(ts);
  392. else
  393. ads7846_enable(ts);
  394. spin_unlock_irq(&ts->lock);
  395. return count;
  396. }
  397. static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
  398. static struct attribute *ads784x_attributes[] = {
  399. &dev_attr_pen_down.attr,
  400. &dev_attr_disable.attr,
  401. NULL,
  402. };
  403. static struct attribute_group ads784x_attr_group = {
  404. .attrs = ads784x_attributes,
  405. };
  406. /*--------------------------------------------------------------------------*/
  407. /*
  408. * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
  409. * to retrieve touchscreen status.
  410. *
  411. * The SPI transfer completion callback does the real work. It reports
  412. * touchscreen events and reactivates the timer (or IRQ) as appropriate.
  413. */
  414. static void ads7846_rx(void *ads)
  415. {
  416. struct ads7846 *ts = ads;
  417. unsigned Rt;
  418. u16 x, y, z1, z2;
  419. /* ads7846_rx_val() did in-place conversion (including byteswap) from
  420. * on-the-wire format as part of debouncing to get stable readings.
  421. */
  422. x = ts->tc.x;
  423. y = ts->tc.y;
  424. z1 = ts->tc.z1;
  425. z2 = ts->tc.z2;
  426. /* range filtering */
  427. if (x == MAX_12BIT)
  428. x = 0;
  429. if (ts->model == 7843) {
  430. Rt = ts->pressure_max / 2;
  431. } else if (likely(x && z1)) {
  432. /* compute touch pressure resistance using equation #2 */
  433. Rt = z2;
  434. Rt -= z1;
  435. Rt *= x;
  436. Rt *= ts->x_plate_ohms;
  437. Rt /= z1;
  438. Rt = (Rt + 2047) >> 12;
  439. } else {
  440. Rt = 0;
  441. }
  442. /* Sample found inconsistent by debouncing or pressure is beyond
  443. * the maximum. Don't report it to user space, repeat at least
  444. * once more the measurement
  445. */
  446. if (ts->tc.ignore || Rt > ts->pressure_max) {
  447. #ifdef VERBOSE
  448. pr_debug("%s: ignored %d pressure %d\n",
  449. ts->spi->dev.bus_id, ts->tc.ignore, Rt);
  450. #endif
  451. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
  452. HRTIMER_MODE_REL);
  453. return;
  454. }
  455. /* Maybe check the pendown state before reporting. This discards
  456. * false readings when the pen is lifted.
  457. */
  458. if (ts->penirq_recheck_delay_usecs) {
  459. udelay(ts->penirq_recheck_delay_usecs);
  460. if (!ts->get_pendown_state())
  461. Rt = 0;
  462. }
  463. /* NOTE: We can't rely on the pressure to determine the pen down
  464. * state, even this controller has a pressure sensor. The pressure
  465. * value can fluctuate for quite a while after lifting the pen and
  466. * in some cases may not even settle at the expected value.
  467. *
  468. * The only safe way to check for the pen up condition is in the
  469. * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
  470. */
  471. if (Rt) {
  472. struct input_dev *input = ts->input;
  473. if (!ts->pendown) {
  474. input_report_key(input, BTN_TOUCH, 1);
  475. ts->pendown = 1;
  476. #ifdef VERBOSE
  477. dev_dbg(&ts->spi->dev, "DOWN\n");
  478. #endif
  479. }
  480. input_report_abs(input, ABS_X, x);
  481. input_report_abs(input, ABS_Y, y);
  482. input_report_abs(input, ABS_PRESSURE, Rt);
  483. input_sync(input);
  484. #ifdef VERBOSE
  485. dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
  486. #endif
  487. }
  488. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
  489. HRTIMER_MODE_REL);
  490. }
  491. static int ads7846_debounce(void *ads, int data_idx, int *val)
  492. {
  493. struct ads7846 *ts = ads;
  494. if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
  495. /* Start over collecting consistent readings. */
  496. ts->read_rep = 0;
  497. /* Repeat it, if this was the first read or the read
  498. * wasn't consistent enough. */
  499. if (ts->read_cnt < ts->debounce_max) {
  500. ts->last_read = *val;
  501. ts->read_cnt++;
  502. return ADS7846_FILTER_REPEAT;
  503. } else {
  504. /* Maximum number of debouncing reached and still
  505. * not enough number of consistent readings. Abort
  506. * the whole sample, repeat it in the next sampling
  507. * period.
  508. */
  509. ts->read_cnt = 0;
  510. return ADS7846_FILTER_IGNORE;
  511. }
  512. } else {
  513. if (++ts->read_rep > ts->debounce_rep) {
  514. /* Got a good reading for this coordinate,
  515. * go for the next one. */
  516. ts->read_cnt = 0;
  517. ts->read_rep = 0;
  518. return ADS7846_FILTER_OK;
  519. } else {
  520. /* Read more values that are consistent. */
  521. ts->read_cnt++;
  522. return ADS7846_FILTER_REPEAT;
  523. }
  524. }
  525. }
  526. static int ads7846_no_filter(void *ads, int data_idx, int *val)
  527. {
  528. return ADS7846_FILTER_OK;
  529. }
  530. static void ads7846_rx_val(void *ads)
  531. {
  532. struct ads7846 *ts = ads;
  533. struct spi_message *m;
  534. struct spi_transfer *t;
  535. int val;
  536. int action;
  537. int status;
  538. m = &ts->msg[ts->msg_idx];
  539. t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
  540. /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
  541. * built from two 8 bit values written msb-first.
  542. */
  543. val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
  544. action = ts->filter(ts->filter_data, ts->msg_idx, &val);
  545. switch (action) {
  546. case ADS7846_FILTER_REPEAT:
  547. break;
  548. case ADS7846_FILTER_IGNORE:
  549. ts->tc.ignore = 1;
  550. /* Last message will contain ads7846_rx() as the
  551. * completion function.
  552. */
  553. m = ts->last_msg;
  554. break;
  555. case ADS7846_FILTER_OK:
  556. *(u16 *)t->rx_buf = val;
  557. ts->tc.ignore = 0;
  558. m = &ts->msg[++ts->msg_idx];
  559. break;
  560. default:
  561. BUG();
  562. }
  563. status = spi_async(ts->spi, m);
  564. if (status)
  565. dev_err(&ts->spi->dev, "spi_async --> %d\n",
  566. status);
  567. }
  568. static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
  569. {
  570. struct ads7846 *ts = container_of(handle, struct ads7846, timer);
  571. int status = 0;
  572. spin_lock_irq(&ts->lock);
  573. if (unlikely(!ts->get_pendown_state() ||
  574. device_suspended(&ts->spi->dev))) {
  575. if (ts->pendown) {
  576. struct input_dev *input = ts->input;
  577. input_report_key(input, BTN_TOUCH, 0);
  578. input_report_abs(input, ABS_PRESSURE, 0);
  579. input_sync(input);
  580. ts->pendown = 0;
  581. #ifdef VERBOSE
  582. dev_dbg(&ts->spi->dev, "UP\n");
  583. #endif
  584. }
  585. /* measurement cycle ended */
  586. if (!device_suspended(&ts->spi->dev)) {
  587. ts->irq_disabled = 0;
  588. enable_irq(ts->spi->irq);
  589. }
  590. ts->pending = 0;
  591. } else {
  592. /* pen is still down, continue with the measurement */
  593. ts->msg_idx = 0;
  594. status = spi_async(ts->spi, &ts->msg[0]);
  595. if (status)
  596. dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
  597. }
  598. spin_unlock_irq(&ts->lock);
  599. return HRTIMER_NORESTART;
  600. }
  601. static irqreturn_t ads7846_irq(int irq, void *handle)
  602. {
  603. struct ads7846 *ts = handle;
  604. unsigned long flags;
  605. spin_lock_irqsave(&ts->lock, flags);
  606. if (likely(ts->get_pendown_state())) {
  607. if (!ts->irq_disabled) {
  608. /* The ARM do_simple_IRQ() dispatcher doesn't act
  609. * like the other dispatchers: it will report IRQs
  610. * even after they've been disabled. We work around
  611. * that here. (The "generic irq" framework may help...)
  612. */
  613. ts->irq_disabled = 1;
  614. disable_irq(ts->spi->irq);
  615. ts->pending = 1;
  616. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
  617. HRTIMER_MODE_REL);
  618. }
  619. }
  620. spin_unlock_irqrestore(&ts->lock, flags);
  621. return IRQ_HANDLED;
  622. }
  623. /*--------------------------------------------------------------------------*/
  624. /* Must be called with ts->lock held */
  625. static void ads7846_disable(struct ads7846 *ts)
  626. {
  627. if (ts->disabled)
  628. return;
  629. ts->disabled = 1;
  630. /* are we waiting for IRQ, or polling? */
  631. if (!ts->pending) {
  632. ts->irq_disabled = 1;
  633. disable_irq(ts->spi->irq);
  634. } else {
  635. /* the timer will run at least once more, and
  636. * leave everything in a clean state, IRQ disabled
  637. */
  638. while (ts->pending) {
  639. spin_unlock_irq(&ts->lock);
  640. msleep(1);
  641. spin_lock_irq(&ts->lock);
  642. }
  643. }
  644. /* we know the chip's in lowpower mode since we always
  645. * leave it that way after every request
  646. */
  647. }
  648. /* Must be called with ts->lock held */
  649. static void ads7846_enable(struct ads7846 *ts)
  650. {
  651. if (!ts->disabled)
  652. return;
  653. ts->disabled = 0;
  654. ts->irq_disabled = 0;
  655. enable_irq(ts->spi->irq);
  656. }
  657. static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
  658. {
  659. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  660. spin_lock_irq(&ts->lock);
  661. ts->is_suspended = 1;
  662. ads7846_disable(ts);
  663. spin_unlock_irq(&ts->lock);
  664. return 0;
  665. }
  666. static int ads7846_resume(struct spi_device *spi)
  667. {
  668. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  669. spin_lock_irq(&ts->lock);
  670. ts->is_suspended = 0;
  671. ads7846_enable(ts);
  672. spin_unlock_irq(&ts->lock);
  673. return 0;
  674. }
  675. static int __devinit ads7846_probe(struct spi_device *spi)
  676. {
  677. struct ads7846 *ts;
  678. struct input_dev *input_dev;
  679. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  680. struct spi_message *m;
  681. struct spi_transfer *x;
  682. int vref;
  683. int err;
  684. if (!spi->irq) {
  685. dev_dbg(&spi->dev, "no IRQ?\n");
  686. return -ENODEV;
  687. }
  688. if (!pdata) {
  689. dev_dbg(&spi->dev, "no platform data?\n");
  690. return -ENODEV;
  691. }
  692. /* don't exceed max specified sample rate */
  693. if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
  694. dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
  695. (spi->max_speed_hz/SAMPLE_BITS)/1000);
  696. return -EINVAL;
  697. }
  698. /* REVISIT when the irq can be triggered active-low, or if for some
  699. * reason the touchscreen isn't hooked up, we don't need to access
  700. * the pendown state.
  701. */
  702. if (pdata->get_pendown_state == NULL) {
  703. dev_dbg(&spi->dev, "no get_pendown_state function?\n");
  704. return -EINVAL;
  705. }
  706. /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
  707. * that even if the hardware can do that, the SPI controller driver
  708. * may not. So we stick to very-portable 8 bit words, both RX and TX.
  709. */
  710. spi->bits_per_word = 8;
  711. spi->mode = SPI_MODE_0;
  712. err = spi_setup(spi);
  713. if (err < 0)
  714. return err;
  715. ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
  716. input_dev = input_allocate_device();
  717. if (!ts || !input_dev) {
  718. err = -ENOMEM;
  719. goto err_free_mem;
  720. }
  721. dev_set_drvdata(&spi->dev, ts);
  722. ts->spi = spi;
  723. ts->input = input_dev;
  724. ts->vref_mv = pdata->vref_mv;
  725. hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  726. ts->timer.function = ads7846_timer;
  727. spin_lock_init(&ts->lock);
  728. ts->model = pdata->model ? : 7846;
  729. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  730. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  731. ts->pressure_max = pdata->pressure_max ? : ~0;
  732. if (pdata->filter != NULL) {
  733. if (pdata->filter_init != NULL) {
  734. err = pdata->filter_init(pdata, &ts->filter_data);
  735. if (err < 0)
  736. goto err_free_mem;
  737. }
  738. ts->filter = pdata->filter;
  739. ts->filter_cleanup = pdata->filter_cleanup;
  740. } else if (pdata->debounce_max) {
  741. ts->debounce_max = pdata->debounce_max;
  742. if (ts->debounce_max < 2)
  743. ts->debounce_max = 2;
  744. ts->debounce_tol = pdata->debounce_tol;
  745. ts->debounce_rep = pdata->debounce_rep;
  746. ts->filter = ads7846_debounce;
  747. ts->filter_data = ts;
  748. } else
  749. ts->filter = ads7846_no_filter;
  750. ts->get_pendown_state = pdata->get_pendown_state;
  751. if (pdata->penirq_recheck_delay_usecs)
  752. ts->penirq_recheck_delay_usecs =
  753. pdata->penirq_recheck_delay_usecs;
  754. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
  755. input_dev->name = "ADS784x Touchscreen";
  756. input_dev->phys = ts->phys;
  757. input_dev->dev.parent = &spi->dev;
  758. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  759. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  760. input_set_abs_params(input_dev, ABS_X,
  761. pdata->x_min ? : 0,
  762. pdata->x_max ? : MAX_12BIT,
  763. 0, 0);
  764. input_set_abs_params(input_dev, ABS_Y,
  765. pdata->y_min ? : 0,
  766. pdata->y_max ? : MAX_12BIT,
  767. 0, 0);
  768. input_set_abs_params(input_dev, ABS_PRESSURE,
  769. pdata->pressure_min, pdata->pressure_max, 0, 0);
  770. vref = pdata->keep_vref_on;
  771. /* set up the transfers to read touchscreen state; this assumes we
  772. * use formula #2 for pressure, not #3.
  773. */
  774. m = &ts->msg[0];
  775. x = ts->xfer;
  776. spi_message_init(m);
  777. /* y- still on; turn on only y+ (and ADC) */
  778. ts->read_y = READ_Y(vref);
  779. x->tx_buf = &ts->read_y;
  780. x->len = 1;
  781. spi_message_add_tail(x, m);
  782. x++;
  783. x->rx_buf = &ts->tc.y;
  784. x->len = 2;
  785. spi_message_add_tail(x, m);
  786. /* the first sample after switching drivers can be low quality;
  787. * optionally discard it, using a second one after the signals
  788. * have had enough time to stabilize.
  789. */
  790. if (pdata->settle_delay_usecs) {
  791. x->delay_usecs = pdata->settle_delay_usecs;
  792. x++;
  793. x->tx_buf = &ts->read_y;
  794. x->len = 1;
  795. spi_message_add_tail(x, m);
  796. x++;
  797. x->rx_buf = &ts->tc.y;
  798. x->len = 2;
  799. spi_message_add_tail(x, m);
  800. }
  801. m->complete = ads7846_rx_val;
  802. m->context = ts;
  803. m++;
  804. spi_message_init(m);
  805. /* turn y- off, x+ on, then leave in lowpower */
  806. x++;
  807. ts->read_x = READ_X(vref);
  808. x->tx_buf = &ts->read_x;
  809. x->len = 1;
  810. spi_message_add_tail(x, m);
  811. x++;
  812. x->rx_buf = &ts->tc.x;
  813. x->len = 2;
  814. spi_message_add_tail(x, m);
  815. /* ... maybe discard first sample ... */
  816. if (pdata->settle_delay_usecs) {
  817. x->delay_usecs = pdata->settle_delay_usecs;
  818. x++;
  819. x->tx_buf = &ts->read_x;
  820. x->len = 1;
  821. spi_message_add_tail(x, m);
  822. x++;
  823. x->rx_buf = &ts->tc.x;
  824. x->len = 2;
  825. spi_message_add_tail(x, m);
  826. }
  827. m->complete = ads7846_rx_val;
  828. m->context = ts;
  829. /* turn y+ off, x- on; we'll use formula #2 */
  830. if (ts->model == 7846) {
  831. m++;
  832. spi_message_init(m);
  833. x++;
  834. ts->read_z1 = READ_Z1(vref);
  835. x->tx_buf = &ts->read_z1;
  836. x->len = 1;
  837. spi_message_add_tail(x, m);
  838. x++;
  839. x->rx_buf = &ts->tc.z1;
  840. x->len = 2;
  841. spi_message_add_tail(x, m);
  842. /* ... maybe discard first sample ... */
  843. if (pdata->settle_delay_usecs) {
  844. x->delay_usecs = pdata->settle_delay_usecs;
  845. x++;
  846. x->tx_buf = &ts->read_z1;
  847. x->len = 1;
  848. spi_message_add_tail(x, m);
  849. x++;
  850. x->rx_buf = &ts->tc.z1;
  851. x->len = 2;
  852. spi_message_add_tail(x, m);
  853. }
  854. m->complete = ads7846_rx_val;
  855. m->context = ts;
  856. m++;
  857. spi_message_init(m);
  858. x++;
  859. ts->read_z2 = READ_Z2(vref);
  860. x->tx_buf = &ts->read_z2;
  861. x->len = 1;
  862. spi_message_add_tail(x, m);
  863. x++;
  864. x->rx_buf = &ts->tc.z2;
  865. x->len = 2;
  866. spi_message_add_tail(x, m);
  867. /* ... maybe discard first sample ... */
  868. if (pdata->settle_delay_usecs) {
  869. x->delay_usecs = pdata->settle_delay_usecs;
  870. x++;
  871. x->tx_buf = &ts->read_z2;
  872. x->len = 1;
  873. spi_message_add_tail(x, m);
  874. x++;
  875. x->rx_buf = &ts->tc.z2;
  876. x->len = 2;
  877. spi_message_add_tail(x, m);
  878. }
  879. m->complete = ads7846_rx_val;
  880. m->context = ts;
  881. }
  882. /* power down */
  883. m++;
  884. spi_message_init(m);
  885. x++;
  886. ts->pwrdown = PWRDOWN;
  887. x->tx_buf = &ts->pwrdown;
  888. x->len = 1;
  889. spi_message_add_tail(x, m);
  890. x++;
  891. x->rx_buf = &ts->dummy;
  892. x->len = 2;
  893. CS_CHANGE(*x);
  894. spi_message_add_tail(x, m);
  895. m->complete = ads7846_rx;
  896. m->context = ts;
  897. ts->last_msg = m;
  898. if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
  899. spi->dev.driver->name, ts)) {
  900. dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
  901. err = -EBUSY;
  902. goto err_cleanup_filter;
  903. }
  904. err = ads784x_hwmon_register(spi, ts);
  905. if (err)
  906. goto err_free_irq;
  907. dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
  908. /* take a first sample, leaving nPENIRQ active and vREF off; avoid
  909. * the touchscreen, in case it's not connected.
  910. */
  911. (void) ads7846_read12_ser(&spi->dev,
  912. READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
  913. err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
  914. if (err)
  915. goto err_remove_hwmon;
  916. err = input_register_device(input_dev);
  917. if (err)
  918. goto err_remove_attr_group;
  919. return 0;
  920. err_remove_attr_group:
  921. sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
  922. err_remove_hwmon:
  923. ads784x_hwmon_unregister(spi, ts);
  924. err_free_irq:
  925. free_irq(spi->irq, ts);
  926. err_cleanup_filter:
  927. if (ts->filter_cleanup)
  928. ts->filter_cleanup(ts->filter_data);
  929. err_free_mem:
  930. input_free_device(input_dev);
  931. kfree(ts);
  932. return err;
  933. }
  934. static int __devexit ads7846_remove(struct spi_device *spi)
  935. {
  936. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  937. ads784x_hwmon_unregister(spi, ts);
  938. input_unregister_device(ts->input);
  939. ads7846_suspend(spi, PMSG_SUSPEND);
  940. sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
  941. free_irq(ts->spi->irq, ts);
  942. /* suspend left the IRQ disabled */
  943. enable_irq(ts->spi->irq);
  944. if (ts->filter_cleanup)
  945. ts->filter_cleanup(ts->filter_data);
  946. kfree(ts);
  947. dev_dbg(&spi->dev, "unregistered touchscreen\n");
  948. return 0;
  949. }
  950. static struct spi_driver ads7846_driver = {
  951. .driver = {
  952. .name = "ads7846",
  953. .bus = &spi_bus_type,
  954. .owner = THIS_MODULE,
  955. },
  956. .probe = ads7846_probe,
  957. .remove = __devexit_p(ads7846_remove),
  958. .suspend = ads7846_suspend,
  959. .resume = ads7846_resume,
  960. };
  961. static int __init ads7846_init(void)
  962. {
  963. return spi_register_driver(&ads7846_driver);
  964. }
  965. module_init(ads7846_init);
  966. static void __exit ads7846_exit(void)
  967. {
  968. spi_unregister_driver(&ads7846_driver);
  969. }
  970. module_exit(ads7846_exit);
  971. MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
  972. MODULE_LICENSE("GPL");