ads7846.c 34 KB

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