ads7846.c 29 KB

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