ads7846.c 28 KB

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