ads7846.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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. /*
  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. #ifdef VERBOSE
  471. pr_debug("%s: ignored %d pressure %d\n",
  472. dev_name(&ts->spi->dev), packet->tc.ignore, Rt);
  473. #endif
  474. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
  475. HRTIMER_MODE_REL);
  476. return;
  477. }
  478. /* Maybe check the pendown state before reporting. This discards
  479. * false readings when the pen is lifted.
  480. */
  481. if (ts->penirq_recheck_delay_usecs) {
  482. udelay(ts->penirq_recheck_delay_usecs);
  483. if (!get_pendown_state(ts))
  484. Rt = 0;
  485. }
  486. /* NOTE: We can't rely on the pressure to determine the pen down
  487. * state, even this controller has a pressure sensor. The pressure
  488. * value can fluctuate for quite a while after lifting the pen and
  489. * in some cases may not even settle at the expected value.
  490. *
  491. * The only safe way to check for the pen up condition is in the
  492. * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
  493. */
  494. if (Rt) {
  495. struct input_dev *input = ts->input;
  496. if (!ts->pendown) {
  497. input_report_key(input, BTN_TOUCH, 1);
  498. ts->pendown = 1;
  499. #ifdef VERBOSE
  500. dev_dbg(&ts->spi->dev, "DOWN\n");
  501. #endif
  502. }
  503. if (ts->swap_xy)
  504. swap(x, y);
  505. input_report_abs(input, ABS_X, x);
  506. input_report_abs(input, ABS_Y, y);
  507. input_report_abs(input, ABS_PRESSURE, Rt);
  508. input_sync(input);
  509. #ifdef VERBOSE
  510. dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
  511. #endif
  512. }
  513. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
  514. HRTIMER_MODE_REL);
  515. }
  516. static int ads7846_debounce(void *ads, int data_idx, int *val)
  517. {
  518. struct ads7846 *ts = ads;
  519. if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
  520. /* Start over collecting consistent readings. */
  521. ts->read_rep = 0;
  522. /* Repeat it, if this was the first read or the read
  523. * wasn't consistent enough. */
  524. if (ts->read_cnt < ts->debounce_max) {
  525. ts->last_read = *val;
  526. ts->read_cnt++;
  527. return ADS7846_FILTER_REPEAT;
  528. } else {
  529. /* Maximum number of debouncing reached and still
  530. * not enough number of consistent readings. Abort
  531. * the whole sample, repeat it in the next sampling
  532. * period.
  533. */
  534. ts->read_cnt = 0;
  535. return ADS7846_FILTER_IGNORE;
  536. }
  537. } else {
  538. if (++ts->read_rep > ts->debounce_rep) {
  539. /* Got a good reading for this coordinate,
  540. * go for the next one. */
  541. ts->read_cnt = 0;
  542. ts->read_rep = 0;
  543. return ADS7846_FILTER_OK;
  544. } else {
  545. /* Read more values that are consistent. */
  546. ts->read_cnt++;
  547. return ADS7846_FILTER_REPEAT;
  548. }
  549. }
  550. }
  551. static int ads7846_no_filter(void *ads, int data_idx, int *val)
  552. {
  553. return ADS7846_FILTER_OK;
  554. }
  555. static void ads7846_rx_val(void *ads)
  556. {
  557. struct ads7846 *ts = ads;
  558. struct ads7846_packet *packet = ts->packet;
  559. struct spi_message *m;
  560. struct spi_transfer *t;
  561. int val;
  562. int action;
  563. int status;
  564. m = &ts->msg[ts->msg_idx];
  565. t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
  566. /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
  567. * built from two 8 bit values written msb-first.
  568. */
  569. val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
  570. action = ts->filter(ts->filter_data, ts->msg_idx, &val);
  571. switch (action) {
  572. case ADS7846_FILTER_REPEAT:
  573. break;
  574. case ADS7846_FILTER_IGNORE:
  575. packet->tc.ignore = 1;
  576. /* Last message will contain ads7846_rx() as the
  577. * completion function.
  578. */
  579. m = ts->last_msg;
  580. break;
  581. case ADS7846_FILTER_OK:
  582. *(u16 *)t->rx_buf = val;
  583. packet->tc.ignore = 0;
  584. m = &ts->msg[++ts->msg_idx];
  585. break;
  586. default:
  587. BUG();
  588. }
  589. ts->wait_for_sync();
  590. status = spi_async(ts->spi, m);
  591. if (status)
  592. dev_err(&ts->spi->dev, "spi_async --> %d\n",
  593. status);
  594. }
  595. static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
  596. {
  597. struct ads7846 *ts = container_of(handle, struct ads7846, timer);
  598. int status = 0;
  599. spin_lock(&ts->lock);
  600. if (unlikely(!get_pendown_state(ts) ||
  601. device_suspended(&ts->spi->dev))) {
  602. if (ts->pendown) {
  603. struct input_dev *input = ts->input;
  604. input_report_key(input, BTN_TOUCH, 0);
  605. input_report_abs(input, ABS_PRESSURE, 0);
  606. input_sync(input);
  607. ts->pendown = 0;
  608. #ifdef VERBOSE
  609. dev_dbg(&ts->spi->dev, "UP\n");
  610. #endif
  611. }
  612. /* measurement cycle ended */
  613. if (!device_suspended(&ts->spi->dev)) {
  614. ts->irq_disabled = 0;
  615. enable_irq(ts->spi->irq);
  616. }
  617. ts->pending = 0;
  618. } else {
  619. /* pen is still down, continue with the measurement */
  620. ts->msg_idx = 0;
  621. ts->wait_for_sync();
  622. status = spi_async(ts->spi, &ts->msg[0]);
  623. if (status)
  624. dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
  625. }
  626. spin_unlock(&ts->lock);
  627. return HRTIMER_NORESTART;
  628. }
  629. static irqreturn_t ads7846_irq(int irq, void *handle)
  630. {
  631. struct ads7846 *ts = handle;
  632. unsigned long flags;
  633. spin_lock_irqsave(&ts->lock, flags);
  634. if (likely(get_pendown_state(ts))) {
  635. if (!ts->irq_disabled) {
  636. /* The ARM do_simple_IRQ() dispatcher doesn't act
  637. * like the other dispatchers: it will report IRQs
  638. * even after they've been disabled. We work around
  639. * that here. (The "generic irq" framework may help...)
  640. */
  641. ts->irq_disabled = 1;
  642. disable_irq_nosync(ts->spi->irq);
  643. ts->pending = 1;
  644. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
  645. HRTIMER_MODE_REL);
  646. }
  647. }
  648. spin_unlock_irqrestore(&ts->lock, flags);
  649. return IRQ_HANDLED;
  650. }
  651. /*--------------------------------------------------------------------------*/
  652. /* Must be called with ts->lock held */
  653. static void ads7846_disable(struct ads7846 *ts)
  654. {
  655. if (ts->disabled)
  656. return;
  657. ts->disabled = 1;
  658. /* are we waiting for IRQ, or polling? */
  659. if (!ts->pending) {
  660. ts->irq_disabled = 1;
  661. disable_irq(ts->spi->irq);
  662. } else {
  663. /* the timer will run at least once more, and
  664. * leave everything in a clean state, IRQ disabled
  665. */
  666. while (ts->pending) {
  667. spin_unlock_irq(&ts->lock);
  668. msleep(1);
  669. spin_lock_irq(&ts->lock);
  670. }
  671. }
  672. /* we know the chip's in lowpower mode since we always
  673. * leave it that way after every request
  674. */
  675. }
  676. /* Must be called with ts->lock held */
  677. static void ads7846_enable(struct ads7846 *ts)
  678. {
  679. if (!ts->disabled)
  680. return;
  681. ts->disabled = 0;
  682. ts->irq_disabled = 0;
  683. enable_irq(ts->spi->irq);
  684. }
  685. static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
  686. {
  687. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  688. spin_lock_irq(&ts->lock);
  689. ts->is_suspended = 1;
  690. ads7846_disable(ts);
  691. spin_unlock_irq(&ts->lock);
  692. return 0;
  693. }
  694. static int ads7846_resume(struct spi_device *spi)
  695. {
  696. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  697. spin_lock_irq(&ts->lock);
  698. ts->is_suspended = 0;
  699. ads7846_enable(ts);
  700. spin_unlock_irq(&ts->lock);
  701. return 0;
  702. }
  703. static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
  704. {
  705. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  706. int err;
  707. /* REVISIT when the irq can be triggered active-low, or if for some
  708. * reason the touchscreen isn't hooked up, we don't need to access
  709. * the pendown state.
  710. */
  711. if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
  712. dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
  713. return -EINVAL;
  714. }
  715. if (pdata->get_pendown_state) {
  716. ts->get_pendown_state = pdata->get_pendown_state;
  717. return 0;
  718. }
  719. err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
  720. if (err) {
  721. dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
  722. pdata->gpio_pendown);
  723. return err;
  724. }
  725. ts->gpio_pendown = pdata->gpio_pendown;
  726. return 0;
  727. }
  728. static int __devinit ads7846_probe(struct spi_device *spi)
  729. {
  730. struct ads7846 *ts;
  731. struct ads7846_packet *packet;
  732. struct input_dev *input_dev;
  733. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  734. struct spi_message *m;
  735. struct spi_transfer *x;
  736. int vref;
  737. int err;
  738. if (!spi->irq) {
  739. dev_dbg(&spi->dev, "no IRQ?\n");
  740. return -ENODEV;
  741. }
  742. if (!pdata) {
  743. dev_dbg(&spi->dev, "no platform data?\n");
  744. return -ENODEV;
  745. }
  746. /* don't exceed max specified sample rate */
  747. if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
  748. dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
  749. (spi->max_speed_hz/SAMPLE_BITS)/1000);
  750. return -EINVAL;
  751. }
  752. /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
  753. * that even if the hardware can do that, the SPI controller driver
  754. * may not. So we stick to very-portable 8 bit words, both RX and TX.
  755. */
  756. spi->bits_per_word = 8;
  757. spi->mode = SPI_MODE_0;
  758. err = spi_setup(spi);
  759. if (err < 0)
  760. return err;
  761. ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
  762. packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
  763. input_dev = input_allocate_device();
  764. if (!ts || !packet || !input_dev) {
  765. err = -ENOMEM;
  766. goto err_free_mem;
  767. }
  768. dev_set_drvdata(&spi->dev, ts);
  769. ts->packet = packet;
  770. ts->spi = spi;
  771. ts->input = input_dev;
  772. ts->vref_mv = pdata->vref_mv;
  773. ts->swap_xy = pdata->swap_xy;
  774. hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  775. ts->timer.function = ads7846_timer;
  776. spin_lock_init(&ts->lock);
  777. ts->model = pdata->model ? : 7846;
  778. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  779. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  780. ts->pressure_max = pdata->pressure_max ? : ~0;
  781. if (pdata->filter != NULL) {
  782. if (pdata->filter_init != NULL) {
  783. err = pdata->filter_init(pdata, &ts->filter_data);
  784. if (err < 0)
  785. goto err_free_mem;
  786. }
  787. ts->filter = pdata->filter;
  788. ts->filter_cleanup = pdata->filter_cleanup;
  789. } else if (pdata->debounce_max) {
  790. ts->debounce_max = pdata->debounce_max;
  791. if (ts->debounce_max < 2)
  792. ts->debounce_max = 2;
  793. ts->debounce_tol = pdata->debounce_tol;
  794. ts->debounce_rep = pdata->debounce_rep;
  795. ts->filter = ads7846_debounce;
  796. ts->filter_data = ts;
  797. } else
  798. ts->filter = ads7846_no_filter;
  799. err = setup_pendown(spi, ts);
  800. if (err)
  801. goto err_cleanup_filter;
  802. if (pdata->penirq_recheck_delay_usecs)
  803. ts->penirq_recheck_delay_usecs =
  804. pdata->penirq_recheck_delay_usecs;
  805. ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
  806. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
  807. snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
  808. input_dev->name = ts->name;
  809. input_dev->phys = ts->phys;
  810. input_dev->dev.parent = &spi->dev;
  811. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  812. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  813. input_set_abs_params(input_dev, ABS_X,
  814. pdata->x_min ? : 0,
  815. pdata->x_max ? : MAX_12BIT,
  816. 0, 0);
  817. input_set_abs_params(input_dev, ABS_Y,
  818. pdata->y_min ? : 0,
  819. pdata->y_max ? : MAX_12BIT,
  820. 0, 0);
  821. input_set_abs_params(input_dev, ABS_PRESSURE,
  822. pdata->pressure_min, pdata->pressure_max, 0, 0);
  823. vref = pdata->keep_vref_on;
  824. /* set up the transfers to read touchscreen state; this assumes we
  825. * use formula #2 for pressure, not #3.
  826. */
  827. m = &ts->msg[0];
  828. x = ts->xfer;
  829. spi_message_init(m);
  830. /* y- still on; turn on only y+ (and ADC) */
  831. packet->read_y = READ_Y(vref);
  832. x->tx_buf = &packet->read_y;
  833. x->len = 1;
  834. spi_message_add_tail(x, m);
  835. x++;
  836. x->rx_buf = &packet->tc.y;
  837. x->len = 2;
  838. spi_message_add_tail(x, m);
  839. /* the first sample after switching drivers can be low quality;
  840. * optionally discard it, using a second one after the signals
  841. * have had enough time to stabilize.
  842. */
  843. if (pdata->settle_delay_usecs) {
  844. x->delay_usecs = pdata->settle_delay_usecs;
  845. x++;
  846. x->tx_buf = &packet->read_y;
  847. x->len = 1;
  848. spi_message_add_tail(x, m);
  849. x++;
  850. x->rx_buf = &packet->tc.y;
  851. x->len = 2;
  852. spi_message_add_tail(x, m);
  853. }
  854. m->complete = ads7846_rx_val;
  855. m->context = ts;
  856. m++;
  857. spi_message_init(m);
  858. /* turn y- off, x+ on, then leave in lowpower */
  859. x++;
  860. packet->read_x = READ_X(vref);
  861. x->tx_buf = &packet->read_x;
  862. x->len = 1;
  863. spi_message_add_tail(x, m);
  864. x++;
  865. x->rx_buf = &packet->tc.x;
  866. x->len = 2;
  867. spi_message_add_tail(x, m);
  868. /* ... maybe discard first sample ... */
  869. if (pdata->settle_delay_usecs) {
  870. x->delay_usecs = pdata->settle_delay_usecs;
  871. x++;
  872. x->tx_buf = &packet->read_x;
  873. x->len = 1;
  874. spi_message_add_tail(x, m);
  875. x++;
  876. x->rx_buf = &packet->tc.x;
  877. x->len = 2;
  878. spi_message_add_tail(x, m);
  879. }
  880. m->complete = ads7846_rx_val;
  881. m->context = ts;
  882. /* turn y+ off, x- on; we'll use formula #2 */
  883. if (ts->model == 7846) {
  884. m++;
  885. spi_message_init(m);
  886. x++;
  887. packet->read_z1 = READ_Z1(vref);
  888. x->tx_buf = &packet->read_z1;
  889. x->len = 1;
  890. spi_message_add_tail(x, m);
  891. x++;
  892. x->rx_buf = &packet->tc.z1;
  893. x->len = 2;
  894. spi_message_add_tail(x, m);
  895. /* ... maybe discard first sample ... */
  896. if (pdata->settle_delay_usecs) {
  897. x->delay_usecs = pdata->settle_delay_usecs;
  898. x++;
  899. x->tx_buf = &packet->read_z1;
  900. x->len = 1;
  901. spi_message_add_tail(x, m);
  902. x++;
  903. x->rx_buf = &packet->tc.z1;
  904. x->len = 2;
  905. spi_message_add_tail(x, m);
  906. }
  907. m->complete = ads7846_rx_val;
  908. m->context = ts;
  909. m++;
  910. spi_message_init(m);
  911. x++;
  912. packet->read_z2 = READ_Z2(vref);
  913. x->tx_buf = &packet->read_z2;
  914. x->len = 1;
  915. spi_message_add_tail(x, m);
  916. x++;
  917. x->rx_buf = &packet->tc.z2;
  918. x->len = 2;
  919. spi_message_add_tail(x, m);
  920. /* ... maybe discard first sample ... */
  921. if (pdata->settle_delay_usecs) {
  922. x->delay_usecs = pdata->settle_delay_usecs;
  923. x++;
  924. x->tx_buf = &packet->read_z2;
  925. x->len = 1;
  926. spi_message_add_tail(x, m);
  927. x++;
  928. x->rx_buf = &packet->tc.z2;
  929. x->len = 2;
  930. spi_message_add_tail(x, m);
  931. }
  932. m->complete = ads7846_rx_val;
  933. m->context = ts;
  934. }
  935. /* power down */
  936. m++;
  937. spi_message_init(m);
  938. x++;
  939. packet->pwrdown = PWRDOWN;
  940. x->tx_buf = &packet->pwrdown;
  941. x->len = 1;
  942. spi_message_add_tail(x, m);
  943. x++;
  944. x->rx_buf = &packet->dummy;
  945. x->len = 2;
  946. CS_CHANGE(*x);
  947. spi_message_add_tail(x, m);
  948. m->complete = ads7846_rx;
  949. m->context = ts;
  950. ts->last_msg = m;
  951. if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
  952. spi->dev.driver->name, ts)) {
  953. dev_info(&spi->dev,
  954. "trying pin change workaround on irq %d\n", spi->irq);
  955. err = request_irq(spi->irq, ads7846_irq,
  956. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  957. spi->dev.driver->name, ts);
  958. if (err) {
  959. dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
  960. goto err_free_gpio;
  961. }
  962. }
  963. err = ads784x_hwmon_register(spi, ts);
  964. if (err)
  965. goto err_free_irq;
  966. dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
  967. /* take a first sample, leaving nPENIRQ active and vREF off; avoid
  968. * the touchscreen, in case it's not connected.
  969. */
  970. (void) ads7846_read12_ser(&spi->dev,
  971. READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
  972. err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
  973. if (err)
  974. goto err_remove_hwmon;
  975. err = input_register_device(input_dev);
  976. if (err)
  977. goto err_remove_attr_group;
  978. return 0;
  979. err_remove_attr_group:
  980. sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
  981. err_remove_hwmon:
  982. ads784x_hwmon_unregister(spi, ts);
  983. err_free_irq:
  984. free_irq(spi->irq, ts);
  985. err_free_gpio:
  986. if (ts->gpio_pendown != -1)
  987. gpio_free(ts->gpio_pendown);
  988. err_cleanup_filter:
  989. if (ts->filter_cleanup)
  990. ts->filter_cleanup(ts->filter_data);
  991. err_free_mem:
  992. input_free_device(input_dev);
  993. kfree(packet);
  994. kfree(ts);
  995. return err;
  996. }
  997. static int __devexit ads7846_remove(struct spi_device *spi)
  998. {
  999. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  1000. ads784x_hwmon_unregister(spi, ts);
  1001. input_unregister_device(ts->input);
  1002. ads7846_suspend(spi, PMSG_SUSPEND);
  1003. sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
  1004. free_irq(ts->spi->irq, ts);
  1005. /* suspend left the IRQ disabled */
  1006. enable_irq(ts->spi->irq);
  1007. if (ts->gpio_pendown != -1)
  1008. gpio_free(ts->gpio_pendown);
  1009. if (ts->filter_cleanup)
  1010. ts->filter_cleanup(ts->filter_data);
  1011. kfree(ts->packet);
  1012. kfree(ts);
  1013. dev_dbg(&spi->dev, "unregistered touchscreen\n");
  1014. return 0;
  1015. }
  1016. static struct spi_driver ads7846_driver = {
  1017. .driver = {
  1018. .name = "ads7846",
  1019. .bus = &spi_bus_type,
  1020. .owner = THIS_MODULE,
  1021. },
  1022. .probe = ads7846_probe,
  1023. .remove = __devexit_p(ads7846_remove),
  1024. .suspend = ads7846_suspend,
  1025. .resume = ads7846_resume,
  1026. };
  1027. static int __init ads7846_init(void)
  1028. {
  1029. return spi_register_driver(&ads7846_driver);
  1030. }
  1031. module_init(ads7846_init);
  1032. static void __exit ads7846_exit(void)
  1033. {
  1034. spi_unregister_driver(&ads7846_driver);
  1035. }
  1036. module_exit(ads7846_exit);
  1037. MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
  1038. MODULE_LICENSE("GPL");
  1039. MODULE_ALIAS("spi:ads7846");