ads7846.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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 int i;
  501. int err;
  502. err = kstrtouint(buf, 10, &i);
  503. if (err)
  504. return err;
  505. if (i)
  506. ads7846_disable(ts);
  507. else
  508. ads7846_enable(ts);
  509. return count;
  510. }
  511. static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
  512. static struct attribute *ads784x_attributes[] = {
  513. &dev_attr_pen_down.attr,
  514. &dev_attr_disable.attr,
  515. NULL,
  516. };
  517. static struct attribute_group ads784x_attr_group = {
  518. .attrs = ads784x_attributes,
  519. };
  520. /*--------------------------------------------------------------------------*/
  521. static int get_pendown_state(struct ads7846 *ts)
  522. {
  523. if (ts->get_pendown_state)
  524. return ts->get_pendown_state();
  525. return !gpio_get_value(ts->gpio_pendown);
  526. }
  527. static void null_wait_for_sync(void)
  528. {
  529. }
  530. static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
  531. {
  532. struct ads7846 *ts = ads;
  533. if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
  534. /* Start over collecting consistent readings. */
  535. ts->read_rep = 0;
  536. /*
  537. * Repeat it, if this was the first read or the read
  538. * wasn't consistent enough.
  539. */
  540. if (ts->read_cnt < ts->debounce_max) {
  541. ts->last_read = *val;
  542. ts->read_cnt++;
  543. return ADS7846_FILTER_REPEAT;
  544. } else {
  545. /*
  546. * Maximum number of debouncing reached and still
  547. * not enough number of consistent readings. Abort
  548. * the whole sample, repeat it in the next sampling
  549. * period.
  550. */
  551. ts->read_cnt = 0;
  552. return ADS7846_FILTER_IGNORE;
  553. }
  554. } else {
  555. if (++ts->read_rep > ts->debounce_rep) {
  556. /*
  557. * Got a good reading for this coordinate,
  558. * go for the next one.
  559. */
  560. ts->read_cnt = 0;
  561. ts->read_rep = 0;
  562. return ADS7846_FILTER_OK;
  563. } else {
  564. /* Read more values that are consistent. */
  565. ts->read_cnt++;
  566. return ADS7846_FILTER_REPEAT;
  567. }
  568. }
  569. }
  570. static int ads7846_no_filter(void *ads, int data_idx, int *val)
  571. {
  572. return ADS7846_FILTER_OK;
  573. }
  574. static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
  575. {
  576. struct spi_transfer *t =
  577. list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
  578. if (ts->model == 7845) {
  579. return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
  580. } else {
  581. /*
  582. * adjust: on-wire is a must-ignore bit, a BE12 value, then
  583. * padding; built from two 8 bit values written msb-first.
  584. */
  585. return be16_to_cpup((__be16 *)t->rx_buf) >> 3;
  586. }
  587. }
  588. static void ads7846_update_value(struct spi_message *m, int val)
  589. {
  590. struct spi_transfer *t =
  591. list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
  592. *(u16 *)t->rx_buf = val;
  593. }
  594. static void ads7846_read_state(struct ads7846 *ts)
  595. {
  596. struct ads7846_packet *packet = ts->packet;
  597. struct spi_message *m;
  598. int msg_idx = 0;
  599. int val;
  600. int action;
  601. int error;
  602. while (msg_idx < ts->msg_count) {
  603. ts->wait_for_sync();
  604. m = &ts->msg[msg_idx];
  605. error = spi_sync(ts->spi, m);
  606. if (error) {
  607. dev_err(&ts->spi->dev, "spi_async --> %d\n", error);
  608. packet->tc.ignore = true;
  609. return;
  610. }
  611. /*
  612. * Last message is power down request, no need to convert
  613. * or filter the value.
  614. */
  615. if (msg_idx < ts->msg_count - 1) {
  616. val = ads7846_get_value(ts, m);
  617. action = ts->filter(ts->filter_data, msg_idx, &val);
  618. switch (action) {
  619. case ADS7846_FILTER_REPEAT:
  620. continue;
  621. case ADS7846_FILTER_IGNORE:
  622. packet->tc.ignore = true;
  623. msg_idx = ts->msg_count - 1;
  624. continue;
  625. case ADS7846_FILTER_OK:
  626. ads7846_update_value(m, val);
  627. packet->tc.ignore = false;
  628. msg_idx++;
  629. break;
  630. default:
  631. BUG();
  632. }
  633. } else {
  634. msg_idx++;
  635. }
  636. }
  637. }
  638. static void ads7846_report_state(struct ads7846 *ts)
  639. {
  640. struct ads7846_packet *packet = ts->packet;
  641. unsigned int Rt;
  642. u16 x, y, z1, z2;
  643. /*
  644. * ads7846_get_value() does in-place conversion (including byte swap)
  645. * from on-the-wire format as part of debouncing to get stable
  646. * readings.
  647. */
  648. if (ts->model == 7845) {
  649. x = *(u16 *)packet->tc.x_buf;
  650. y = *(u16 *)packet->tc.y_buf;
  651. z1 = 0;
  652. z2 = 0;
  653. } else {
  654. x = packet->tc.x;
  655. y = packet->tc.y;
  656. z1 = packet->tc.z1;
  657. z2 = packet->tc.z2;
  658. }
  659. /* range filtering */
  660. if (x == MAX_12BIT)
  661. x = 0;
  662. if (ts->model == 7843) {
  663. Rt = ts->pressure_max / 2;
  664. } else if (ts->model == 7845) {
  665. if (get_pendown_state(ts))
  666. Rt = ts->pressure_max / 2;
  667. else
  668. Rt = 0;
  669. dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
  670. } else if (likely(x && z1)) {
  671. /* compute touch pressure resistance using equation #2 */
  672. Rt = z2;
  673. Rt -= z1;
  674. Rt *= x;
  675. Rt *= ts->x_plate_ohms;
  676. Rt /= z1;
  677. Rt = (Rt + 2047) >> 12;
  678. } else {
  679. Rt = 0;
  680. }
  681. /*
  682. * Sample found inconsistent by debouncing or pressure is beyond
  683. * the maximum. Don't report it to user space, repeat at least
  684. * once more the measurement
  685. */
  686. if (packet->tc.ignore || Rt > ts->pressure_max) {
  687. dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
  688. packet->tc.ignore, Rt);
  689. return;
  690. }
  691. /*
  692. * Maybe check the pendown state before reporting. This discards
  693. * false readings when the pen is lifted.
  694. */
  695. if (ts->penirq_recheck_delay_usecs) {
  696. udelay(ts->penirq_recheck_delay_usecs);
  697. if (!get_pendown_state(ts))
  698. Rt = 0;
  699. }
  700. /*
  701. * NOTE: We can't rely on the pressure to determine the pen down
  702. * state, even this controller has a pressure sensor. The pressure
  703. * value can fluctuate for quite a while after lifting the pen and
  704. * in some cases may not even settle at the expected value.
  705. *
  706. * The only safe way to check for the pen up condition is in the
  707. * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
  708. */
  709. if (Rt) {
  710. struct input_dev *input = ts->input;
  711. if (ts->swap_xy)
  712. swap(x, y);
  713. if (!ts->pendown) {
  714. input_report_key(input, BTN_TOUCH, 1);
  715. ts->pendown = true;
  716. dev_vdbg(&ts->spi->dev, "DOWN\n");
  717. }
  718. input_report_abs(input, ABS_X, x);
  719. input_report_abs(input, ABS_Y, y);
  720. input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
  721. input_sync(input);
  722. dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
  723. }
  724. }
  725. static irqreturn_t ads7846_hard_irq(int irq, void *handle)
  726. {
  727. struct ads7846 *ts = handle;
  728. return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
  729. }
  730. static irqreturn_t ads7846_irq(int irq, void *handle)
  731. {
  732. struct ads7846 *ts = handle;
  733. /* Start with a small delay before checking pendown state */
  734. msleep(TS_POLL_DELAY);
  735. while (!ts->stopped && get_pendown_state(ts)) {
  736. /* pen is down, continue with the measurement */
  737. ads7846_read_state(ts);
  738. if (!ts->stopped)
  739. ads7846_report_state(ts);
  740. wait_event_timeout(ts->wait, ts->stopped,
  741. msecs_to_jiffies(TS_POLL_PERIOD));
  742. }
  743. if (ts->pendown) {
  744. struct input_dev *input = ts->input;
  745. input_report_key(input, BTN_TOUCH, 0);
  746. input_report_abs(input, ABS_PRESSURE, 0);
  747. input_sync(input);
  748. ts->pendown = false;
  749. dev_vdbg(&ts->spi->dev, "UP\n");
  750. }
  751. return IRQ_HANDLED;
  752. }
  753. #ifdef CONFIG_PM_SLEEP
  754. static int ads7846_suspend(struct device *dev)
  755. {
  756. struct ads7846 *ts = dev_get_drvdata(dev);
  757. mutex_lock(&ts->lock);
  758. if (!ts->suspended) {
  759. if (!ts->disabled)
  760. __ads7846_disable(ts);
  761. if (device_may_wakeup(&ts->spi->dev))
  762. enable_irq_wake(ts->spi->irq);
  763. ts->suspended = true;
  764. }
  765. mutex_unlock(&ts->lock);
  766. return 0;
  767. }
  768. static int ads7846_resume(struct device *dev)
  769. {
  770. struct ads7846 *ts = dev_get_drvdata(dev);
  771. mutex_lock(&ts->lock);
  772. if (ts->suspended) {
  773. ts->suspended = false;
  774. if (device_may_wakeup(&ts->spi->dev))
  775. disable_irq_wake(ts->spi->irq);
  776. if (!ts->disabled)
  777. __ads7846_enable(ts);
  778. }
  779. mutex_unlock(&ts->lock);
  780. return 0;
  781. }
  782. #endif
  783. static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
  784. static int __devinit ads7846_setup_pendown(struct spi_device *spi, struct ads7846 *ts)
  785. {
  786. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  787. int err;
  788. /*
  789. * REVISIT when the irq can be triggered active-low, or if for some
  790. * reason the touchscreen isn't hooked up, we don't need to access
  791. * the pendown state.
  792. */
  793. if (pdata->get_pendown_state) {
  794. ts->get_pendown_state = pdata->get_pendown_state;
  795. } else if (gpio_is_valid(pdata->gpio_pendown)) {
  796. err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN,
  797. "ads7846_pendown");
  798. if (err) {
  799. dev_err(&spi->dev,
  800. "failed to request/setup pendown GPIO%d: %d\n",
  801. pdata->gpio_pendown, err);
  802. return err;
  803. }
  804. ts->gpio_pendown = pdata->gpio_pendown;
  805. } else {
  806. dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
  807. return -EINVAL;
  808. }
  809. return 0;
  810. }
  811. /*
  812. * Set up the transfers to read touchscreen state; this assumes we
  813. * use formula #2 for pressure, not #3.
  814. */
  815. static void __devinit ads7846_setup_spi_msg(struct ads7846 *ts,
  816. const struct ads7846_platform_data *pdata)
  817. {
  818. struct spi_message *m = &ts->msg[0];
  819. struct spi_transfer *x = ts->xfer;
  820. struct ads7846_packet *packet = ts->packet;
  821. int vref = pdata->keep_vref_on;
  822. if (ts->model == 7873) {
  823. /*
  824. * The AD7873 is almost identical to the ADS7846
  825. * keep VREF off during differential/ratiometric
  826. * conversion modes.
  827. */
  828. ts->model = 7846;
  829. vref = 0;
  830. }
  831. ts->msg_count = 1;
  832. spi_message_init(m);
  833. m->context = ts;
  834. if (ts->model == 7845) {
  835. packet->read_y_cmd[0] = READ_Y(vref);
  836. packet->read_y_cmd[1] = 0;
  837. packet->read_y_cmd[2] = 0;
  838. x->tx_buf = &packet->read_y_cmd[0];
  839. x->rx_buf = &packet->tc.y_buf[0];
  840. x->len = 3;
  841. spi_message_add_tail(x, m);
  842. } else {
  843. /* y- still on; turn on only y+ (and ADC) */
  844. packet->read_y = READ_Y(vref);
  845. x->tx_buf = &packet->read_y;
  846. x->len = 1;
  847. spi_message_add_tail(x, m);
  848. x++;
  849. x->rx_buf = &packet->tc.y;
  850. x->len = 2;
  851. spi_message_add_tail(x, m);
  852. }
  853. /*
  854. * The first sample after switching drivers can be low quality;
  855. * optionally discard it, using a second one after the signals
  856. * have had enough time to stabilize.
  857. */
  858. if (pdata->settle_delay_usecs) {
  859. x->delay_usecs = pdata->settle_delay_usecs;
  860. x++;
  861. x->tx_buf = &packet->read_y;
  862. x->len = 1;
  863. spi_message_add_tail(x, m);
  864. x++;
  865. x->rx_buf = &packet->tc.y;
  866. x->len = 2;
  867. spi_message_add_tail(x, m);
  868. }
  869. ts->msg_count++;
  870. m++;
  871. spi_message_init(m);
  872. m->context = ts;
  873. if (ts->model == 7845) {
  874. x++;
  875. packet->read_x_cmd[0] = READ_X(vref);
  876. packet->read_x_cmd[1] = 0;
  877. packet->read_x_cmd[2] = 0;
  878. x->tx_buf = &packet->read_x_cmd[0];
  879. x->rx_buf = &packet->tc.x_buf[0];
  880. x->len = 3;
  881. spi_message_add_tail(x, m);
  882. } else {
  883. /* turn y- off, x+ on, then leave in lowpower */
  884. x++;
  885. packet->read_x = READ_X(vref);
  886. x->tx_buf = &packet->read_x;
  887. x->len = 1;
  888. spi_message_add_tail(x, m);
  889. x++;
  890. x->rx_buf = &packet->tc.x;
  891. x->len = 2;
  892. spi_message_add_tail(x, m);
  893. }
  894. /* ... maybe discard first sample ... */
  895. if (pdata->settle_delay_usecs) {
  896. x->delay_usecs = pdata->settle_delay_usecs;
  897. x++;
  898. x->tx_buf = &packet->read_x;
  899. x->len = 1;
  900. spi_message_add_tail(x, m);
  901. x++;
  902. x->rx_buf = &packet->tc.x;
  903. x->len = 2;
  904. spi_message_add_tail(x, m);
  905. }
  906. /* turn y+ off, x- on; we'll use formula #2 */
  907. if (ts->model == 7846) {
  908. ts->msg_count++;
  909. m++;
  910. spi_message_init(m);
  911. m->context = ts;
  912. x++;
  913. packet->read_z1 = READ_Z1(vref);
  914. x->tx_buf = &packet->read_z1;
  915. x->len = 1;
  916. spi_message_add_tail(x, m);
  917. x++;
  918. x->rx_buf = &packet->tc.z1;
  919. x->len = 2;
  920. spi_message_add_tail(x, m);
  921. /* ... maybe discard first sample ... */
  922. if (pdata->settle_delay_usecs) {
  923. x->delay_usecs = pdata->settle_delay_usecs;
  924. x++;
  925. x->tx_buf = &packet->read_z1;
  926. x->len = 1;
  927. spi_message_add_tail(x, m);
  928. x++;
  929. x->rx_buf = &packet->tc.z1;
  930. x->len = 2;
  931. spi_message_add_tail(x, m);
  932. }
  933. ts->msg_count++;
  934. m++;
  935. spi_message_init(m);
  936. m->context = ts;
  937. x++;
  938. packet->read_z2 = READ_Z2(vref);
  939. x->tx_buf = &packet->read_z2;
  940. x->len = 1;
  941. spi_message_add_tail(x, m);
  942. x++;
  943. x->rx_buf = &packet->tc.z2;
  944. x->len = 2;
  945. spi_message_add_tail(x, m);
  946. /* ... maybe discard first sample ... */
  947. if (pdata->settle_delay_usecs) {
  948. x->delay_usecs = pdata->settle_delay_usecs;
  949. x++;
  950. x->tx_buf = &packet->read_z2;
  951. x->len = 1;
  952. spi_message_add_tail(x, m);
  953. x++;
  954. x->rx_buf = &packet->tc.z2;
  955. x->len = 2;
  956. spi_message_add_tail(x, m);
  957. }
  958. }
  959. /* power down */
  960. ts->msg_count++;
  961. m++;
  962. spi_message_init(m);
  963. m->context = ts;
  964. if (ts->model == 7845) {
  965. x++;
  966. packet->pwrdown_cmd[0] = PWRDOWN;
  967. packet->pwrdown_cmd[1] = 0;
  968. packet->pwrdown_cmd[2] = 0;
  969. x->tx_buf = &packet->pwrdown_cmd[0];
  970. x->len = 3;
  971. } else {
  972. x++;
  973. packet->pwrdown = PWRDOWN;
  974. x->tx_buf = &packet->pwrdown;
  975. x->len = 1;
  976. spi_message_add_tail(x, m);
  977. x++;
  978. x->rx_buf = &packet->dummy;
  979. x->len = 2;
  980. }
  981. CS_CHANGE(*x);
  982. spi_message_add_tail(x, m);
  983. }
  984. static int __devinit ads7846_probe(struct spi_device *spi)
  985. {
  986. struct ads7846 *ts;
  987. struct ads7846_packet *packet;
  988. struct input_dev *input_dev;
  989. struct ads7846_platform_data *pdata = spi->dev.platform_data;
  990. unsigned long irq_flags;
  991. int err;
  992. if (!spi->irq) {
  993. dev_dbg(&spi->dev, "no IRQ?\n");
  994. return -ENODEV;
  995. }
  996. if (!pdata) {
  997. dev_dbg(&spi->dev, "no platform data?\n");
  998. return -ENODEV;
  999. }
  1000. /* don't exceed max specified sample rate */
  1001. if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
  1002. dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
  1003. (spi->max_speed_hz/SAMPLE_BITS)/1000);
  1004. return -EINVAL;
  1005. }
  1006. /* We'd set TX word size 8 bits and RX word size to 13 bits ... except
  1007. * that even if the hardware can do that, the SPI controller driver
  1008. * may not. So we stick to very-portable 8 bit words, both RX and TX.
  1009. */
  1010. spi->bits_per_word = 8;
  1011. spi->mode = SPI_MODE_0;
  1012. err = spi_setup(spi);
  1013. if (err < 0)
  1014. return err;
  1015. ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
  1016. packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
  1017. input_dev = input_allocate_device();
  1018. if (!ts || !packet || !input_dev) {
  1019. err = -ENOMEM;
  1020. goto err_free_mem;
  1021. }
  1022. dev_set_drvdata(&spi->dev, ts);
  1023. ts->packet = packet;
  1024. ts->spi = spi;
  1025. ts->input = input_dev;
  1026. ts->vref_mv = pdata->vref_mv;
  1027. ts->swap_xy = pdata->swap_xy;
  1028. mutex_init(&ts->lock);
  1029. init_waitqueue_head(&ts->wait);
  1030. ts->model = pdata->model ? : 7846;
  1031. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  1032. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  1033. ts->pressure_max = pdata->pressure_max ? : ~0;
  1034. if (pdata->filter != NULL) {
  1035. if (pdata->filter_init != NULL) {
  1036. err = pdata->filter_init(pdata, &ts->filter_data);
  1037. if (err < 0)
  1038. goto err_free_mem;
  1039. }
  1040. ts->filter = pdata->filter;
  1041. ts->filter_cleanup = pdata->filter_cleanup;
  1042. } else if (pdata->debounce_max) {
  1043. ts->debounce_max = pdata->debounce_max;
  1044. if (ts->debounce_max < 2)
  1045. ts->debounce_max = 2;
  1046. ts->debounce_tol = pdata->debounce_tol;
  1047. ts->debounce_rep = pdata->debounce_rep;
  1048. ts->filter = ads7846_debounce_filter;
  1049. ts->filter_data = ts;
  1050. } else {
  1051. ts->filter = ads7846_no_filter;
  1052. }
  1053. err = ads7846_setup_pendown(spi, ts);
  1054. if (err)
  1055. goto err_cleanup_filter;
  1056. if (pdata->penirq_recheck_delay_usecs)
  1057. ts->penirq_recheck_delay_usecs =
  1058. pdata->penirq_recheck_delay_usecs;
  1059. ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
  1060. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
  1061. snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
  1062. input_dev->name = ts->name;
  1063. input_dev->phys = ts->phys;
  1064. input_dev->dev.parent = &spi->dev;
  1065. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  1066. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  1067. input_set_abs_params(input_dev, ABS_X,
  1068. pdata->x_min ? : 0,
  1069. pdata->x_max ? : MAX_12BIT,
  1070. 0, 0);
  1071. input_set_abs_params(input_dev, ABS_Y,
  1072. pdata->y_min ? : 0,
  1073. pdata->y_max ? : MAX_12BIT,
  1074. 0, 0);
  1075. input_set_abs_params(input_dev, ABS_PRESSURE,
  1076. pdata->pressure_min, pdata->pressure_max, 0, 0);
  1077. ads7846_setup_spi_msg(ts, pdata);
  1078. ts->reg = regulator_get(&spi->dev, "vcc");
  1079. if (IS_ERR(ts->reg)) {
  1080. err = PTR_ERR(ts->reg);
  1081. dev_err(&spi->dev, "unable to get regulator: %d\n", err);
  1082. goto err_free_gpio;
  1083. }
  1084. err = regulator_enable(ts->reg);
  1085. if (err) {
  1086. dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
  1087. goto err_put_regulator;
  1088. }
  1089. irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
  1090. irq_flags |= IRQF_ONESHOT;
  1091. err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
  1092. irq_flags, spi->dev.driver->name, ts);
  1093. if (err && !pdata->irq_flags) {
  1094. dev_info(&spi->dev,
  1095. "trying pin change workaround on irq %d\n", spi->irq);
  1096. irq_flags |= IRQF_TRIGGER_RISING;
  1097. err = request_threaded_irq(spi->irq,
  1098. ads7846_hard_irq, ads7846_irq,
  1099. irq_flags, spi->dev.driver->name, ts);
  1100. }
  1101. if (err) {
  1102. dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
  1103. goto err_disable_regulator;
  1104. }
  1105. err = ads784x_hwmon_register(spi, ts);
  1106. if (err)
  1107. goto err_free_irq;
  1108. dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
  1109. /*
  1110. * Take a first sample, leaving nPENIRQ active and vREF off; avoid
  1111. * the touchscreen, in case it's not connected.
  1112. */
  1113. if (ts->model == 7845)
  1114. ads7845_read12_ser(&spi->dev, PWRDOWN);
  1115. else
  1116. (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
  1117. err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
  1118. if (err)
  1119. goto err_remove_hwmon;
  1120. err = input_register_device(input_dev);
  1121. if (err)
  1122. goto err_remove_attr_group;
  1123. device_init_wakeup(&spi->dev, pdata->wakeup);
  1124. return 0;
  1125. err_remove_attr_group:
  1126. sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
  1127. err_remove_hwmon:
  1128. ads784x_hwmon_unregister(spi, ts);
  1129. err_free_irq:
  1130. free_irq(spi->irq, ts);
  1131. err_disable_regulator:
  1132. regulator_disable(ts->reg);
  1133. err_put_regulator:
  1134. regulator_put(ts->reg);
  1135. err_free_gpio:
  1136. if (!ts->get_pendown_state)
  1137. gpio_free(ts->gpio_pendown);
  1138. err_cleanup_filter:
  1139. if (ts->filter_cleanup)
  1140. ts->filter_cleanup(ts->filter_data);
  1141. err_free_mem:
  1142. input_free_device(input_dev);
  1143. kfree(packet);
  1144. kfree(ts);
  1145. return err;
  1146. }
  1147. static int __devexit ads7846_remove(struct spi_device *spi)
  1148. {
  1149. struct ads7846 *ts = dev_get_drvdata(&spi->dev);
  1150. device_init_wakeup(&spi->dev, false);
  1151. sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
  1152. ads7846_disable(ts);
  1153. free_irq(ts->spi->irq, ts);
  1154. input_unregister_device(ts->input);
  1155. ads784x_hwmon_unregister(spi, ts);
  1156. regulator_disable(ts->reg);
  1157. regulator_put(ts->reg);
  1158. if (!ts->get_pendown_state) {
  1159. /*
  1160. * If we are not using specialized pendown method we must
  1161. * have been relying on gpio we set up ourselves.
  1162. */
  1163. gpio_free(ts->gpio_pendown);
  1164. }
  1165. if (ts->filter_cleanup)
  1166. ts->filter_cleanup(ts->filter_data);
  1167. kfree(ts->packet);
  1168. kfree(ts);
  1169. dev_dbg(&spi->dev, "unregistered touchscreen\n");
  1170. return 0;
  1171. }
  1172. static struct spi_driver ads7846_driver = {
  1173. .driver = {
  1174. .name = "ads7846",
  1175. .bus = &spi_bus_type,
  1176. .owner = THIS_MODULE,
  1177. .pm = &ads7846_pm,
  1178. },
  1179. .probe = ads7846_probe,
  1180. .remove = __devexit_p(ads7846_remove),
  1181. };
  1182. static int __init ads7846_init(void)
  1183. {
  1184. return spi_register_driver(&ads7846_driver);
  1185. }
  1186. module_init(ads7846_init);
  1187. static void __exit ads7846_exit(void)
  1188. {
  1189. spi_unregister_driver(&ads7846_driver);
  1190. }
  1191. module_exit(ads7846_exit);
  1192. MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
  1193. MODULE_LICENSE("GPL");
  1194. MODULE_ALIAS("spi:ads7846");