ads7846.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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/types.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/sched.h>
  25. #include <linux/delay.h>
  26. #include <linux/input.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/slab.h>
  29. #include <linux/pm.h>
  30. #include <linux/gpio.h>
  31. #include <linux/spi/spi.h>
  32. #include <linux/spi/ads7846.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <asm/irq.h>
  35. /*
  36. * This code has been heavily tested on a Nokia 770, and lightly
  37. * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
  38. * TSC2046 is just newer ads7846 silicon.
  39. * Support for ads7843 tested on Atmel at91sam926x-EK.
  40. * Support for ads7845 has only been stubbed in.
  41. * Support for Analog Devices AD7873 and AD7843 tested.
  42. *
  43. * IRQ handling needs a workaround because of a shortcoming in handling
  44. * edge triggered IRQs on some platforms like the OMAP1/2. These
  45. * platforms don't handle the ARM lazy IRQ disabling properly, thus we
  46. * have to maintain our own SW IRQ disabled status. This should be
  47. * removed as soon as the affected platform's IRQ handling is fixed.
  48. *
  49. * App note sbaa036 talks in more detail about accurate sampling...
  50. * that ought to help in situations like LCDs inducing noise (which
  51. * can also be helped by using synch signals) and more generally.
  52. * This driver tries to utilize the measures described in the app
  53. * note. The strength of filtering can be set in the board-* specific
  54. * files.
  55. */
  56. #define TS_POLL_DELAY 1 /* ms delay before the first sample */
  57. #define TS_POLL_PERIOD 5 /* ms delay between samples */
  58. /* this driver doesn't aim at the peak continuous sample rate */
  59. #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
  60. struct ts_event {
  61. /*
  62. * For portability, we can't read 12 bit values using SPI (which
  63. * would make the controller deliver them as native byte order u16
  64. * with msbs zeroed). Instead, we read them as two 8-bit values,
  65. * *** WHICH NEED BYTESWAPPING *** and range adjustment.
  66. */
  67. u16 x;
  68. u16 y;
  69. u16 z1, z2;
  70. bool ignore;
  71. u8 x_buf[3];
  72. u8 y_buf[3];
  73. };
  74. /*
  75. * We allocate this separately to avoid cache line sharing issues when
  76. * driver is used with DMA-based SPI controllers (like atmel_spi) on
  77. * systems where main memory is not DMA-coherent (most non-x86 boards).
  78. */
  79. struct ads7846_packet {
  80. u8 read_x, read_y, read_z1, read_z2, pwrdown;
  81. u16 dummy; /* for the pwrdown read */
  82. struct ts_event tc;
  83. /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
  84. u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];
  85. };
  86. struct ads7846 {
  87. struct input_dev *input;
  88. char phys[32];
  89. char name[32];
  90. struct spi_device *spi;
  91. struct regulator *reg;
  92. #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
  93. struct attribute_group *attr_group;
  94. struct device *hwmon;
  95. #endif
  96. u16 model;
  97. u16 vref_mv;
  98. u16 vref_delay_usecs;
  99. u16 x_plate_ohms;
  100. u16 pressure_max;
  101. bool swap_xy;
  102. bool use_internal;
  103. struct ads7846_packet *packet;
  104. struct spi_transfer xfer[18];
  105. struct spi_message msg[5];
  106. int msg_count;
  107. wait_queue_head_t wait;
  108. bool pendown;
  109. int read_cnt;
  110. int read_rep;
  111. int last_read;
  112. u16 debounce_max;
  113. u16 debounce_tol;
  114. u16 debounce_rep;
  115. u16 penirq_recheck_delay_usecs;
  116. struct mutex lock;
  117. bool stopped; /* P: lock */
  118. bool disabled; /* P: lock */
  119. bool suspended; /* P: lock */
  120. int (*filter)(void *data, int data_idx, int *val);
  121. void *filter_data;
  122. void (*filter_cleanup)(void *data);
  123. int (*get_pendown_state)(void);
  124. int gpio_pendown;
  125. void (*wait_for_sync)(void);
  126. };
  127. /* leave chip selected when we're done, for quicker re-select? */
  128. #if 0
  129. #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
  130. #else
  131. #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
  132. #endif
  133. /*--------------------------------------------------------------------------*/
  134. /* The ADS7846 has touchscreen and other sensors.
  135. * Earlier ads784x chips are somewhat compatible.
  136. */
  137. #define ADS_START (1 << 7)
  138. #define ADS_A2A1A0_d_y (1 << 4) /* differential */
  139. #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
  140. #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
  141. #define ADS_A2A1A0_d_x (5 << 4) /* differential */
  142. #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
  143. #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
  144. #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
  145. #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
  146. #define ADS_8_BIT (1 << 3)
  147. #define ADS_12_BIT (0 << 3)
  148. #define ADS_SER (1 << 2) /* non-differential */
  149. #define ADS_DFR (0 << 2) /* differential */
  150. #define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
  151. #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
  152. #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
  153. #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
  154. #define MAX_12BIT ((1<<12)-1)
  155. /* leave ADC powered up (disables penirq) between differential samples */
  156. #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
  157. | ADS_12_BIT | ADS_DFR | \
  158. (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
  159. #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
  160. #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
  161. #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
  162. #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
  163. #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
  164. /* single-ended samples need to first power up reference voltage;
  165. * we leave both ADC and VREF powered
  166. */
  167. #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
  168. | ADS_12_BIT | ADS_SER)
  169. #define REF_ON (READ_12BIT_DFR(x, 1, 1))
  170. #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
  171. /* Must be called with ts->lock held */
  172. static void ads7846_stop(struct ads7846 *ts)
  173. {
  174. if (!ts->disabled && !ts->suspended) {
  175. /* Signal IRQ thread to stop polling and disable the handler. */
  176. ts->stopped = true;
  177. mb();
  178. wake_up(&ts->wait);
  179. disable_irq(ts->spi->irq);
  180. }
  181. }
  182. /* Must be called with ts->lock held */
  183. static void ads7846_restart(struct ads7846 *ts)
  184. {
  185. if (!ts->disabled && !ts->suspended) {
  186. /* Tell IRQ thread that it may poll the device. */
  187. ts->stopped = false;
  188. mb();
  189. enable_irq(ts->spi->irq);
  190. }
  191. }
  192. /* Must be called with ts->lock held */
  193. static void __ads7846_disable(struct ads7846 *ts)
  194. {
  195. ads7846_stop(ts);
  196. regulator_disable(ts->reg);
  197. /*
  198. * We know the chip's in low power mode since we always
  199. * leave it that way after every request
  200. */
  201. }
  202. /* Must be called with ts->lock held */
  203. static void __ads7846_enable(struct ads7846 *ts)
  204. {
  205. regulator_enable(ts->reg);
  206. ads7846_restart(ts);
  207. }
  208. static void ads7846_disable(struct ads7846 *ts)
  209. {
  210. mutex_lock(&ts->lock);
  211. if (!ts->disabled) {
  212. if (!ts->suspended)
  213. __ads7846_disable(ts);
  214. ts->disabled = true;
  215. }
  216. mutex_unlock(&ts->lock);
  217. }
  218. static void ads7846_enable(struct ads7846 *ts)
  219. {
  220. mutex_lock(&ts->lock);
  221. if (ts->disabled) {
  222. ts->disabled = false;
  223. if (!ts->suspended)
  224. __ads7846_enable(ts);
  225. }
  226. mutex_unlock(&ts->lock);
  227. }
  228. /*--------------------------------------------------------------------------*/
  229. /*
  230. * Non-touchscreen sensors only use single-ended conversions.
  231. * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
  232. * ads7846 lets that pin be unconnected, to use internal vREF.
  233. */
  234. struct ser_req {
  235. u8 ref_on;
  236. u8 command;
  237. u8 ref_off;
  238. u16 scratch;
  239. struct spi_message msg;
  240. struct spi_transfer xfer[6];
  241. /*
  242. * DMA (thus cache coherency maintenance) requires the
  243. * transfer buffers to live in their own cache lines.
  244. */
  245. __be16 sample ____cacheline_aligned;
  246. };
  247. struct ads7845_ser_req {
  248. u8 command[3];
  249. struct spi_message msg;
  250. struct spi_transfer xfer[2];
  251. /*
  252. * DMA (thus cache coherency maintenance) requires the
  253. * transfer buffers to live in their own cache lines.
  254. */
  255. u8 sample[3] ____cacheline_aligned;
  256. };
  257. static int ads7846_read12_ser(struct device *dev, unsigned command)
  258. {
  259. struct spi_device *spi = to_spi_device(dev);
  260. struct ads7846 *ts = dev_get_drvdata(dev);
  261. struct ser_req *req;
  262. int status;
  263. req = kzalloc(sizeof *req, GFP_KERNEL);
  264. if (!req)
  265. return -ENOMEM;
  266. spi_message_init(&req->msg);
  267. /* maybe turn on internal vREF, and let it settle */
  268. if (ts->use_internal) {
  269. req->ref_on = REF_ON;
  270. req->xfer[0].tx_buf = &req->ref_on;
  271. req->xfer[0].len = 1;
  272. spi_message_add_tail(&req->xfer[0], &req->msg);
  273. req->xfer[1].rx_buf = &req->scratch;
  274. req->xfer[1].len = 2;
  275. /* for 1uF, settle for 800 usec; no cap, 100 usec. */
  276. req->xfer[1].delay_usecs = ts->vref_delay_usecs;
  277. spi_message_add_tail(&req->xfer[1], &req->msg);
  278. /* Enable reference voltage */
  279. command |= ADS_PD10_REF_ON;
  280. }
  281. /* Enable ADC in every case */
  282. command |= ADS_PD10_ADC_ON;
  283. /* take sample */
  284. req->command = (u8) command;
  285. req->xfer[2].tx_buf = &req->command;
  286. req->xfer[2].len = 1;
  287. spi_message_add_tail(&req->xfer[2], &req->msg);
  288. req->xfer[3].rx_buf = &req->sample;
  289. req->xfer[3].len = 2;
  290. spi_message_add_tail(&req->xfer[3], &req->msg);
  291. /* REVISIT: take a few more samples, and compare ... */
  292. /* converter in low power mode & enable PENIRQ */
  293. req->ref_off = PWRDOWN;
  294. req->xfer[4].tx_buf = &req->ref_off;
  295. req->xfer[4].len = 1;
  296. spi_message_add_tail(&req->xfer[4], &req->msg);
  297. req->xfer[5].rx_buf = &req->scratch;
  298. req->xfer[5].len = 2;
  299. CS_CHANGE(req->xfer[5]);
  300. spi_message_add_tail(&req->xfer[5], &req->msg);
  301. mutex_lock(&ts->lock);
  302. ads7846_stop(ts);
  303. status = spi_sync(spi, &req->msg);
  304. ads7846_restart(ts);
  305. mutex_unlock(&ts->lock);
  306. if (status == 0) {
  307. /* on-wire is a must-ignore bit, a BE12 value, then padding */
  308. status = be16_to_cpu(req->sample);
  309. status = status >> 3;
  310. status &= 0x0fff;
  311. }
  312. kfree(req);
  313. return status;
  314. }
  315. static int ads7845_read12_ser(struct device *dev, unsigned command)
  316. {
  317. struct spi_device *spi = to_spi_device(dev);
  318. struct ads7846 *ts = dev_get_drvdata(dev);
  319. struct ads7845_ser_req *req;
  320. int status;
  321. req = kzalloc(sizeof *req, GFP_KERNEL);
  322. if (!req)
  323. return -ENOMEM;
  324. spi_message_init(&req->msg);
  325. req->command[0] = (u8) command;
  326. req->xfer[0].tx_buf = req->command;
  327. req->xfer[0].rx_buf = req->sample;
  328. req->xfer[0].len = 3;
  329. spi_message_add_tail(&req->xfer[0], &req->msg);
  330. mutex_lock(&ts->lock);
  331. ads7846_stop(ts);
  332. status = spi_sync(spi, &req->msg);
  333. ads7846_restart(ts);
  334. mutex_unlock(&ts->lock);
  335. if (status == 0) {
  336. /* BE12 value, then padding */
  337. status = be16_to_cpu(*((u16 *)&req->sample[1]));
  338. status = status >> 3;
  339. status &= 0x0fff;
  340. }
  341. kfree(req);
  342. return status;
  343. }
  344. #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
  345. #define SHOW(name, var, adjust) static ssize_t \
  346. name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
  347. { \
  348. struct ads7846 *ts = dev_get_drvdata(dev); \
  349. ssize_t v = ads7846_read12_ser(dev, \
  350. READ_12BIT_SER(var)); \
  351. if (v < 0) \
  352. return v; \
  353. return sprintf(buf, "%u\n", adjust(ts, v)); \
  354. } \
  355. static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
  356. /* Sysfs conventions report temperatures in millidegrees Celsius.
  357. * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
  358. * accuracy scheme without calibration data. For now we won't try either;
  359. * userspace sees raw sensor values, and must scale/calibrate appropriately.
  360. */
  361. static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
  362. {
  363. return v;
  364. }
  365. SHOW(temp0, temp0, null_adjust) /* temp1_input */
  366. SHOW(temp1, temp1, null_adjust) /* temp2_input */
  367. /* sysfs conventions report voltages in millivolts. We can convert voltages
  368. * if we know vREF. userspace may need to scale vAUX to match the board's
  369. * external resistors; we assume that vBATT only uses the internal ones.
  370. */
  371. static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
  372. {
  373. unsigned retval = v;
  374. /* external resistors may scale vAUX into 0..vREF */
  375. retval *= ts->vref_mv;
  376. retval = retval >> 12;
  377. return retval;
  378. }
  379. static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
  380. {
  381. unsigned retval = vaux_adjust(ts, v);
  382. /* ads7846 has a resistor ladder to scale this signal down */
  383. if (ts->model == 7846)
  384. retval *= 4;
  385. return retval;
  386. }
  387. SHOW(in0_input, vaux, vaux_adjust)
  388. SHOW(in1_input, vbatt, vbatt_adjust)
  389. static struct attribute *ads7846_attributes[] = {
  390. &dev_attr_temp0.attr,
  391. &dev_attr_temp1.attr,
  392. &dev_attr_in0_input.attr,
  393. &dev_attr_in1_input.attr,
  394. NULL,
  395. };
  396. static struct attribute_group ads7846_attr_group = {
  397. .attrs = ads7846_attributes,
  398. };
  399. static struct attribute *ads7843_attributes[] = {
  400. &dev_attr_in0_input.attr,
  401. &dev_attr_in1_input.attr,
  402. NULL,
  403. };
  404. static struct attribute_group ads7843_attr_group = {
  405. .attrs = ads7843_attributes,
  406. };
  407. static struct attribute *ads7845_attributes[] = {
  408. &dev_attr_in0_input.attr,
  409. NULL,
  410. };
  411. static struct attribute_group ads7845_attr_group = {
  412. .attrs = ads7845_attributes,
  413. };
  414. static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
  415. {
  416. struct device *hwmon;
  417. int err;
  418. /* hwmon sensors need a reference voltage */
  419. switch (ts->model) {
  420. case 7846:
  421. if (!ts->vref_mv) {
  422. dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
  423. ts->vref_mv = 2500;
  424. ts->use_internal = true;
  425. }
  426. break;
  427. case 7845:
  428. case 7843:
  429. if (!ts->vref_mv) {
  430. dev_warn(&spi->dev,
  431. "external vREF for ADS%d not specified\n",
  432. ts->model);
  433. return 0;
  434. }
  435. break;
  436. }
  437. /* different chips have different sensor groups */
  438. switch (ts->model) {
  439. case 7846:
  440. ts->attr_group = &ads7846_attr_group;
  441. break;
  442. case 7845:
  443. ts->attr_group = &ads7845_attr_group;
  444. break;
  445. case 7843:
  446. ts->attr_group = &ads7843_attr_group;
  447. break;
  448. default:
  449. dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
  450. return 0;
  451. }
  452. err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
  453. if (err)
  454. return err;
  455. hwmon = hwmon_device_register(&spi->dev);
  456. if (IS_ERR(hwmon)) {
  457. sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
  458. return PTR_ERR(hwmon);
  459. }
  460. ts->hwmon = hwmon;
  461. return 0;
  462. }
  463. static void ads784x_hwmon_unregister(struct spi_device *spi,
  464. struct ads7846 *ts)
  465. {
  466. if (ts->hwmon) {
  467. sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
  468. hwmon_device_unregister(ts->hwmon);
  469. }
  470. }
  471. #else
  472. static inline int ads784x_hwmon_register(struct spi_device *spi,
  473. struct ads7846 *ts)
  474. {
  475. return 0;
  476. }
  477. static inline void ads784x_hwmon_unregister(struct spi_device *spi,
  478. struct ads7846 *ts)
  479. {
  480. }
  481. #endif
  482. static ssize_t ads7846_pen_down_show(struct device *dev,
  483. struct device_attribute *attr, char *buf)
  484. {
  485. struct ads7846 *ts = dev_get_drvdata(dev);
  486. return sprintf(buf, "%u\n", ts->pendown);
  487. }
  488. static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
  489. static ssize_t ads7846_disable_show(struct device *dev,
  490. struct device_attribute *attr, char *buf)
  491. {
  492. struct ads7846 *ts = dev_get_drvdata(dev);
  493. return sprintf(buf, "%u\n", ts->disabled);
  494. }
  495. static ssize_t ads7846_disable_store(struct device *dev,
  496. struct device_attribute *attr,
  497. const char *buf, size_t count)
  498. {
  499. struct ads7846 *ts = dev_get_drvdata(dev);
  500. unsigned long i;
  501. if (strict_strtoul(buf, 10, &i))
  502. return -EINVAL;
  503. if (i)
  504. ads7846_disable(ts);
  505. else
  506. ads7846_enable(ts);
  507. return count;
  508. }
  509. static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
  510. static struct attribute *ads784x_attributes[] = {
  511. &dev_attr_pen_down.attr,
  512. &dev_attr_disable.attr,
  513. NULL,
  514. };
  515. static struct attribute_group ads784x_attr_group = {
  516. .attrs = ads784x_attributes,
  517. };
  518. /*--------------------------------------------------------------------------*/
  519. static int get_pendown_state(struct ads7846 *ts)
  520. {
  521. if (ts->get_pendown_state)
  522. return ts->get_pendown_state();
  523. return !gpio_get_value(ts->gpio_pendown);
  524. }
  525. static void null_wait_for_sync(void)
  526. {
  527. }
  528. static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
  529. {
  530. struct ads7846 *ts = ads;
  531. if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
  532. /* Start over collecting consistent readings. */
  533. ts->read_rep = 0;
  534. /*
  535. * Repeat it, if this was the first read or the read
  536. * wasn't consistent enough.
  537. */
  538. if (ts->read_cnt < ts->debounce_max) {
  539. ts->last_read = *val;
  540. ts->read_cnt++;
  541. return ADS7846_FILTER_REPEAT;
  542. } else {
  543. /*
  544. * Maximum number of debouncing reached and still
  545. * not enough number of consistent readings. Abort
  546. * the whole sample, repeat it in the next sampling
  547. * period.
  548. */
  549. ts->read_cnt = 0;
  550. return ADS7846_FILTER_IGNORE;
  551. }
  552. } else {
  553. if (++ts->read_rep > ts->debounce_rep) {
  554. /*
  555. * Got a good reading for this coordinate,
  556. * go for the next one.
  557. */
  558. ts->read_cnt = 0;
  559. ts->read_rep = 0;
  560. return ADS7846_FILTER_OK;
  561. } else {
  562. /* Read more values that are consistent. */
  563. ts->read_cnt++;
  564. return ADS7846_FILTER_REPEAT;
  565. }
  566. }
  567. }
  568. static int ads7846_no_filter(void *ads, int data_idx, int *val)
  569. {
  570. return ADS7846_FILTER_OK;
  571. }
  572. static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
  573. {
  574. struct spi_transfer *t =
  575. list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
  576. if (ts->model == 7845) {
  577. return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
  578. } else {
  579. /*
  580. * adjust: on-wire is a must-ignore bit, a BE12 value, then
  581. * padding; built from two 8 bit values written msb-first.
  582. */
  583. return be16_to_cpup((__be16 *)t->rx_buf) >> 3;
  584. }
  585. }
  586. static void ads7846_update_value(struct spi_message *m, int val)
  587. {
  588. struct spi_transfer *t =
  589. list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
  590. *(u16 *)t->rx_buf = val;
  591. }
  592. static void ads7846_read_state(struct ads7846 *ts)
  593. {
  594. struct ads7846_packet *packet = ts->packet;
  595. struct spi_message *m;
  596. int msg_idx = 0;
  597. int val;
  598. int action;
  599. int error;
  600. while (msg_idx < ts->msg_count) {
  601. ts->wait_for_sync();
  602. m = &ts->msg[msg_idx];
  603. error = spi_sync(ts->spi, m);
  604. if (error) {
  605. dev_err(&ts->spi->dev, "spi_async --> %d\n", error);
  606. packet->tc.ignore = true;
  607. return;
  608. }
  609. /*
  610. * Last message is power down request, no need to convert
  611. * or filter the value.
  612. */
  613. if (msg_idx < ts->msg_count - 1) {
  614. val = ads7846_get_value(ts, m);
  615. action = ts->filter(ts->filter_data, msg_idx, &val);
  616. switch (action) {
  617. case ADS7846_FILTER_REPEAT:
  618. continue;
  619. case ADS7846_FILTER_IGNORE:
  620. packet->tc.ignore = true;
  621. msg_idx = ts->msg_count - 1;
  622. continue;
  623. case ADS7846_FILTER_OK:
  624. ads7846_update_value(m, val);
  625. packet->tc.ignore = false;
  626. msg_idx++;
  627. break;
  628. default:
  629. BUG();
  630. }
  631. } else {
  632. msg_idx++;
  633. }
  634. }
  635. }
  636. static void ads7846_report_state(struct ads7846 *ts)
  637. {
  638. struct ads7846_packet *packet = ts->packet;
  639. unsigned int Rt;
  640. u16 x, y, z1, z2;
  641. /*
  642. * ads7846_get_value() does in-place conversion (including byte swap)
  643. * from on-the-wire format as part of debouncing to get stable
  644. * readings.
  645. */
  646. if (ts->model == 7845) {
  647. x = *(u16 *)packet->tc.x_buf;
  648. y = *(u16 *)packet->tc.y_buf;
  649. z1 = 0;
  650. z2 = 0;
  651. } else {
  652. x = packet->tc.x;
  653. y = packet->tc.y;
  654. z1 = packet->tc.z1;
  655. z2 = packet->tc.z2;
  656. }
  657. /* range filtering */
  658. if (x == MAX_12BIT)
  659. x = 0;
  660. if (ts->model == 7843) {
  661. Rt = ts->pressure_max / 2;
  662. } else if (ts->model == 7845) {
  663. if (get_pendown_state(ts))
  664. Rt = ts->pressure_max / 2;
  665. else
  666. Rt = 0;
  667. dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
  668. } else if (likely(x && z1)) {
  669. /* compute touch pressure resistance using equation #2 */
  670. Rt = z2;
  671. Rt -= z1;
  672. Rt *= x;
  673. Rt *= ts->x_plate_ohms;
  674. Rt /= z1;
  675. Rt = (Rt + 2047) >> 12;
  676. } else {
  677. Rt = 0;
  678. }
  679. /*
  680. * Sample found inconsistent by debouncing or pressure is beyond
  681. * the maximum. Don't report it to user space, repeat at least
  682. * once more the measurement
  683. */
  684. if (packet->tc.ignore || Rt > ts->pressure_max) {
  685. dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
  686. packet->tc.ignore, Rt);
  687. return;
  688. }
  689. /*
  690. * Maybe check the pendown state before reporting. This discards
  691. * false readings when the pen is lifted.
  692. */
  693. if (ts->penirq_recheck_delay_usecs) {
  694. udelay(ts->penirq_recheck_delay_usecs);
  695. if (!get_pendown_state(ts))
  696. Rt = 0;
  697. }
  698. /*
  699. * NOTE: We can't rely on the pressure to determine the pen down
  700. * state, even this controller has a pressure sensor. The pressure
  701. * value can fluctuate for quite a while after lifting the pen and
  702. * in some cases may not even settle at the expected value.
  703. *
  704. * The only safe way to check for the pen up condition is in the
  705. * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
  706. */
  707. if (Rt) {
  708. struct input_dev *input = ts->input;
  709. if (ts->swap_xy)
  710. swap(x, y);
  711. if (!ts->pendown) {
  712. input_report_key(input, BTN_TOUCH, 1);
  713. ts->pendown = true;
  714. dev_vdbg(&ts->spi->dev, "DOWN\n");
  715. }
  716. input_report_abs(input, ABS_X, x);
  717. input_report_abs(input, ABS_Y, y);
  718. input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
  719. input_sync(input);
  720. dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
  721. }
  722. }
  723. static irqreturn_t ads7846_hard_irq(int irq, void *handle)
  724. {
  725. struct ads7846 *ts = handle;
  726. return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
  727. }
  728. static irqreturn_t ads7846_irq(int irq, void *handle)
  729. {
  730. struct ads7846 *ts = handle;
  731. /* Start with a small delay before checking pendown state */
  732. msleep(TS_POLL_DELAY);
  733. while (!ts->stopped && get_pendown_state(ts)) {
  734. /* pen is down, continue with the measurement */
  735. ads7846_read_state(ts);
  736. if (!ts->stopped)
  737. ads7846_report_state(ts);
  738. wait_event_timeout(ts->wait, ts->stopped,
  739. msecs_to_jiffies(TS_POLL_PERIOD));
  740. }
  741. if (ts->pendown) {
  742. struct input_dev *input = ts->input;
  743. input_report_key(input, BTN_TOUCH, 0);
  744. input_report_abs(input, ABS_PRESSURE, 0);
  745. input_sync(input);
  746. ts->pendown = false;
  747. dev_vdbg(&ts->spi->dev, "UP\n");
  748. }
  749. return IRQ_HANDLED;
  750. }
  751. #ifdef CONFIG_PM_SLEEP
  752. static int ads7846_suspend(struct device *dev)
  753. {
  754. struct ads7846 *ts = dev_get_drvdata(dev);
  755. mutex_lock(&ts->lock);
  756. if (!ts->suspended) {
  757. if (!ts->disabled)
  758. __ads7846_disable(ts);
  759. if (device_may_wakeup(&ts->spi->dev))
  760. enable_irq_wake(ts->spi->irq);
  761. ts->suspended = true;
  762. }
  763. mutex_unlock(&ts->lock);
  764. return 0;
  765. }
  766. static int ads7846_resume(struct device *dev)
  767. {
  768. struct ads7846 *ts = dev_get_drvdata(dev);
  769. mutex_lock(&ts->lock);
  770. if (ts->suspended) {
  771. ts->suspended = false;
  772. if (device_may_wakeup(&ts->spi->dev))
  773. disable_irq_wake(ts->spi->irq);
  774. if (!ts->disabled)
  775. __ads7846_enable(ts);
  776. }
  777. mutex_unlock(&ts->lock);
  778. return 0;
  779. }
  780. #endif
  781. static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
  782. static int __devinit ads7846_setup_pendown(struct spi_device *spi, struct ads7846 *ts)
  783. {
  784. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  785. int err;
  786. /*
  787. * REVISIT when the irq can be triggered active-low, or if for some
  788. * reason the touchscreen isn't hooked up, we don't need to access
  789. * the pendown state.
  790. */
  791. if (pdata->get_pendown_state) {
  792. ts->get_pendown_state = pdata->get_pendown_state;
  793. } else if (gpio_is_valid(pdata->gpio_pendown)) {
  794. err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
  795. if (err) {
  796. dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
  797. pdata->gpio_pendown);
  798. return err;
  799. }
  800. err = gpio_direction_input(pdata->gpio_pendown);
  801. if (err) {
  802. dev_err(&spi->dev, "failed to setup pendown GPIO%d\n",
  803. pdata->gpio_pendown);
  804. gpio_free(pdata->gpio_pendown);
  805. return err;
  806. }
  807. ts->gpio_pendown = pdata->gpio_pendown;
  808. } else {
  809. dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
  810. return -EINVAL;
  811. }
  812. return 0;
  813. }
  814. /*
  815. * Set up the transfers to read touchscreen state; this assumes we
  816. * use formula #2 for pressure, not #3.
  817. */
  818. static void __devinit ads7846_setup_spi_msg(struct ads7846 *ts,
  819. const struct ads7846_platform_data *pdata)
  820. {
  821. struct spi_message *m = &ts->msg[0];
  822. struct spi_transfer *x = ts->xfer;
  823. struct ads7846_packet *packet = ts->packet;
  824. int vref = pdata->keep_vref_on;
  825. if (ts->model == 7873) {
  826. /*
  827. * The AD7873 is almost identical to the ADS7846
  828. * keep VREF off during differential/ratiometric
  829. * conversion modes.
  830. */
  831. ts->model = 7846;
  832. vref = 0;
  833. }
  834. ts->msg_count = 1;
  835. spi_message_init(m);
  836. m->context = ts;
  837. if (ts->model == 7845) {
  838. packet->read_y_cmd[0] = READ_Y(vref);
  839. packet->read_y_cmd[1] = 0;
  840. packet->read_y_cmd[2] = 0;
  841. x->tx_buf = &packet->read_y_cmd[0];
  842. x->rx_buf = &packet->tc.y_buf[0];
  843. x->len = 3;
  844. spi_message_add_tail(x, m);
  845. } else {
  846. /* y- still on; turn on only y+ (and ADC) */
  847. packet->read_y = READ_Y(vref);
  848. x->tx_buf = &packet->read_y;
  849. x->len = 1;
  850. spi_message_add_tail(x, m);
  851. x++;
  852. x->rx_buf = &packet->tc.y;
  853. x->len = 2;
  854. spi_message_add_tail(x, m);
  855. }
  856. /*
  857. * The first sample after switching drivers can be low quality;
  858. * optionally discard it, using a second one after the signals
  859. * have had enough time to stabilize.
  860. */
  861. if (pdata->settle_delay_usecs) {
  862. x->delay_usecs = pdata->settle_delay_usecs;
  863. x++;
  864. x->tx_buf = &packet->read_y;
  865. x->len = 1;
  866. spi_message_add_tail(x, m);
  867. x++;
  868. x->rx_buf = &packet->tc.y;
  869. x->len = 2;
  870. spi_message_add_tail(x, m);
  871. }
  872. ts->msg_count++;
  873. m++;
  874. spi_message_init(m);
  875. m->context = ts;
  876. if (ts->model == 7845) {
  877. x++;
  878. packet->read_x_cmd[0] = READ_X(vref);
  879. packet->read_x_cmd[1] = 0;
  880. packet->read_x_cmd[2] = 0;
  881. x->tx_buf = &packet->read_x_cmd[0];
  882. x->rx_buf = &packet->tc.x_buf[0];
  883. x->len = 3;
  884. spi_message_add_tail(x, m);
  885. } else {
  886. /* turn y- off, x+ on, then leave in lowpower */
  887. x++;
  888. packet->read_x = READ_X(vref);
  889. x->tx_buf = &packet->read_x;
  890. x->len = 1;
  891. spi_message_add_tail(x, m);
  892. x++;
  893. x->rx_buf = &packet->tc.x;
  894. x->len = 2;
  895. spi_message_add_tail(x, m);
  896. }
  897. /* ... maybe discard first sample ... */
  898. if (pdata->settle_delay_usecs) {
  899. x->delay_usecs = pdata->settle_delay_usecs;
  900. x++;
  901. x->tx_buf = &packet->read_x;
  902. x->len = 1;
  903. spi_message_add_tail(x, m);
  904. x++;
  905. x->rx_buf = &packet->tc.x;
  906. x->len = 2;
  907. spi_message_add_tail(x, m);
  908. }
  909. /* turn y+ off, x- on; we'll use formula #2 */
  910. if (ts->model == 7846) {
  911. ts->msg_count++;
  912. m++;
  913. spi_message_init(m);
  914. m->context = ts;
  915. x++;
  916. packet->read_z1 = READ_Z1(vref);
  917. x->tx_buf = &packet->read_z1;
  918. x->len = 1;
  919. spi_message_add_tail(x, m);
  920. x++;
  921. x->rx_buf = &packet->tc.z1;
  922. x->len = 2;
  923. spi_message_add_tail(x, m);
  924. /* ... maybe discard first sample ... */
  925. if (pdata->settle_delay_usecs) {
  926. x->delay_usecs = pdata->settle_delay_usecs;
  927. x++;
  928. x->tx_buf = &packet->read_z1;
  929. x->len = 1;
  930. spi_message_add_tail(x, m);
  931. x++;
  932. x->rx_buf = &packet->tc.z1;
  933. x->len = 2;
  934. spi_message_add_tail(x, m);
  935. }
  936. ts->msg_count++;
  937. m++;
  938. spi_message_init(m);
  939. m->context = ts;
  940. x++;
  941. packet->read_z2 = READ_Z2(vref);
  942. x->tx_buf = &packet->read_z2;
  943. x->len = 1;
  944. spi_message_add_tail(x, m);
  945. x++;
  946. x->rx_buf = &packet->tc.z2;
  947. x->len = 2;
  948. spi_message_add_tail(x, m);
  949. /* ... maybe discard first sample ... */
  950. if (pdata->settle_delay_usecs) {
  951. x->delay_usecs = pdata->settle_delay_usecs;
  952. x++;
  953. x->tx_buf = &packet->read_z2;
  954. x->len = 1;
  955. spi_message_add_tail(x, m);
  956. x++;
  957. x->rx_buf = &packet->tc.z2;
  958. x->len = 2;
  959. spi_message_add_tail(x, m);
  960. }
  961. }
  962. /* power down */
  963. ts->msg_count++;
  964. m++;
  965. spi_message_init(m);
  966. m->context = ts;
  967. if (ts->model == 7845) {
  968. x++;
  969. packet->pwrdown_cmd[0] = PWRDOWN;
  970. packet->pwrdown_cmd[1] = 0;
  971. packet->pwrdown_cmd[2] = 0;
  972. x->tx_buf = &packet->pwrdown_cmd[0];
  973. x->len = 3;
  974. } else {
  975. x++;
  976. packet->pwrdown = PWRDOWN;
  977. x->tx_buf = &packet->pwrdown;
  978. x->len = 1;
  979. spi_message_add_tail(x, m);
  980. x++;
  981. x->rx_buf = &packet->dummy;
  982. x->len = 2;
  983. }
  984. CS_CHANGE(*x);
  985. spi_message_add_tail(x, m);
  986. }
  987. static int __devinit ads7846_probe(struct spi_device *spi)
  988. {
  989. struct ads7846 *ts;
  990. struct ads7846_packet *packet;
  991. struct input_dev *input_dev;
  992. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  993. unsigned long irq_flags;
  994. int err;
  995. if (!spi->irq) {
  996. dev_dbg(&spi->dev, "no IRQ?\n");
  997. return -ENODEV;
  998. }
  999. if (!pdata) {
  1000. dev_dbg(&spi->dev, "no platform data?\n");
  1001. return -ENODEV;
  1002. }
  1003. /* don't exceed max specified sample rate */
  1004. if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
  1005. dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
  1006. (spi->max_speed_hz/SAMPLE_BITS)/1000);
  1007. return -EINVAL;
  1008. }
  1009. /* We'd set TX word size 8 bits and RX word size to 13 bits ... except
  1010. * that even if the hardware can do that, the SPI controller driver
  1011. * may not. So we stick to very-portable 8 bit words, both RX and TX.
  1012. */
  1013. spi->bits_per_word = 8;
  1014. spi->mode = SPI_MODE_0;
  1015. err = spi_setup(spi);
  1016. if (err < 0)
  1017. return err;
  1018. ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
  1019. packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
  1020. input_dev = input_allocate_device();
  1021. if (!ts || !packet || !input_dev) {
  1022. err = -ENOMEM;
  1023. goto err_free_mem;
  1024. }
  1025. dev_set_drvdata(&spi->dev, ts);
  1026. ts->packet = packet;
  1027. ts->spi = spi;
  1028. ts->input = input_dev;
  1029. ts->vref_mv = pdata->vref_mv;
  1030. ts->swap_xy = pdata->swap_xy;
  1031. mutex_init(&ts->lock);
  1032. init_waitqueue_head(&ts->wait);
  1033. ts->model = pdata->model ? : 7846;
  1034. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  1035. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  1036. ts->pressure_max = pdata->pressure_max ? : ~0;
  1037. if (pdata->filter != NULL) {
  1038. if (pdata->filter_init != NULL) {
  1039. err = pdata->filter_init(pdata, &ts->filter_data);
  1040. if (err < 0)
  1041. goto err_free_mem;
  1042. }
  1043. ts->filter = pdata->filter;
  1044. ts->filter_cleanup = pdata->filter_cleanup;
  1045. } else if (pdata->debounce_max) {
  1046. ts->debounce_max = pdata->debounce_max;
  1047. if (ts->debounce_max < 2)
  1048. ts->debounce_max = 2;
  1049. ts->debounce_tol = pdata->debounce_tol;
  1050. ts->debounce_rep = pdata->debounce_rep;
  1051. ts->filter = ads7846_debounce_filter;
  1052. ts->filter_data = ts;
  1053. } else {
  1054. ts->filter = ads7846_no_filter;
  1055. }
  1056. err = ads7846_setup_pendown(spi, ts);
  1057. if (err)
  1058. goto err_cleanup_filter;
  1059. if (pdata->penirq_recheck_delay_usecs)
  1060. ts->penirq_recheck_delay_usecs =
  1061. pdata->penirq_recheck_delay_usecs;
  1062. ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
  1063. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
  1064. snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
  1065. input_dev->name = ts->name;
  1066. input_dev->phys = ts->phys;
  1067. input_dev->dev.parent = &spi->dev;
  1068. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  1069. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  1070. input_set_abs_params(input_dev, ABS_X,
  1071. pdata->x_min ? : 0,
  1072. pdata->x_max ? : MAX_12BIT,
  1073. 0, 0);
  1074. input_set_abs_params(input_dev, ABS_Y,
  1075. pdata->y_min ? : 0,
  1076. pdata->y_max ? : MAX_12BIT,
  1077. 0, 0);
  1078. input_set_abs_params(input_dev, ABS_PRESSURE,
  1079. pdata->pressure_min, pdata->pressure_max, 0, 0);
  1080. ads7846_setup_spi_msg(ts, pdata);
  1081. ts->reg = regulator_get(&spi->dev, "vcc");
  1082. if (IS_ERR(ts->reg)) {
  1083. err = PTR_ERR(ts->reg);
  1084. dev_err(&spi->dev, "unable to get regulator: %d\n", err);
  1085. goto err_free_gpio;
  1086. }
  1087. err = regulator_enable(ts->reg);
  1088. if (err) {
  1089. dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
  1090. goto err_put_regulator;
  1091. }
  1092. irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
  1093. irq_flags |= IRQF_ONESHOT;
  1094. err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
  1095. irq_flags, spi->dev.driver->name, ts);
  1096. if (err && !pdata->irq_flags) {
  1097. dev_info(&spi->dev,
  1098. "trying pin change workaround on irq %d\n", spi->irq);
  1099. irq_flags |= IRQF_TRIGGER_RISING;
  1100. err = request_threaded_irq(spi->irq,
  1101. ads7846_hard_irq, ads7846_irq,
  1102. irq_flags, spi->dev.driver->name, ts);
  1103. }
  1104. if (err) {
  1105. dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
  1106. goto err_disable_regulator;
  1107. }
  1108. err = ads784x_hwmon_register(spi, ts);
  1109. if (err)
  1110. goto err_free_irq;
  1111. dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
  1112. /*
  1113. * Take a first sample, leaving nPENIRQ active and vREF off; avoid
  1114. * the touchscreen, in case it's not connected.
  1115. */
  1116. if (ts->model == 7845)
  1117. ads7845_read12_ser(&spi->dev, PWRDOWN);
  1118. else
  1119. (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
  1120. err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
  1121. if (err)
  1122. goto err_remove_hwmon;
  1123. err = input_register_device(input_dev);
  1124. if (err)
  1125. goto err_remove_attr_group;
  1126. device_init_wakeup(&spi->dev, pdata->wakeup);
  1127. return 0;
  1128. err_remove_attr_group:
  1129. sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
  1130. err_remove_hwmon:
  1131. ads784x_hwmon_unregister(spi, ts);
  1132. err_free_irq:
  1133. free_irq(spi->irq, ts);
  1134. err_disable_regulator:
  1135. regulator_disable(ts->reg);
  1136. err_put_regulator:
  1137. regulator_put(ts->reg);
  1138. err_free_gpio:
  1139. if (!ts->get_pendown_state)
  1140. gpio_free(ts->gpio_pendown);
  1141. err_cleanup_filter:
  1142. if (ts->filter_cleanup)
  1143. ts->filter_cleanup(ts->filter_data);
  1144. err_free_mem:
  1145. input_free_device(input_dev);
  1146. kfree(packet);
  1147. kfree(ts);
  1148. return err;
  1149. }
  1150. static int __devexit ads7846_remove(struct spi_device *spi)
  1151. {
  1152. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  1153. device_init_wakeup(&spi->dev, false);
  1154. sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
  1155. ads7846_disable(ts);
  1156. free_irq(ts->spi->irq, ts);
  1157. input_unregister_device(ts->input);
  1158. ads784x_hwmon_unregister(spi, ts);
  1159. regulator_disable(ts->reg);
  1160. regulator_put(ts->reg);
  1161. if (!ts->get_pendown_state) {
  1162. /*
  1163. * If we are not using specialized pendown method we must
  1164. * have been relying on gpio we set up ourselves.
  1165. */
  1166. gpio_free(ts->gpio_pendown);
  1167. }
  1168. if (ts->filter_cleanup)
  1169. ts->filter_cleanup(ts->filter_data);
  1170. kfree(ts->packet);
  1171. kfree(ts);
  1172. dev_dbg(&spi->dev, "unregistered touchscreen\n");
  1173. return 0;
  1174. }
  1175. static struct spi_driver ads7846_driver = {
  1176. .driver = {
  1177. .name = "ads7846",
  1178. .bus = &spi_bus_type,
  1179. .owner = THIS_MODULE,
  1180. .pm = &ads7846_pm,
  1181. },
  1182. .probe = ads7846_probe,
  1183. .remove = __devexit_p(ads7846_remove),
  1184. };
  1185. static int __init ads7846_init(void)
  1186. {
  1187. return spi_register_driver(&ads7846_driver);
  1188. }
  1189. module_init(ads7846_init);
  1190. static void __exit ads7846_exit(void)
  1191. {
  1192. spi_unregister_driver(&ads7846_driver);
  1193. }
  1194. module_exit(ads7846_exit);
  1195. MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
  1196. MODULE_LICENSE("GPL");
  1197. MODULE_ALIAS("spi:ads7846");