ads7846.c 27 KB

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