ads7846.c 28 KB

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