ads7846.c 30 KB

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