ads7846.c 30 KB

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