ads7846.c 34 KB

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