ads7846.c 28 KB

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