ads7846.c 34 KB

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