ads7846.c 33 KB

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