ads7846.c 34 KB

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