ads7846.c 33 KB

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