ads7846.c 30 KB

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