ads7846.c 30 KB

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