ads7846.c 29 KB

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