ads7846.c 29 KB

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