ads7846.c 31 KB

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