ads7846.c 33 KB

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