ads7846.c 28 KB

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