ads7846.c 31 KB

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