ads7846.c 34 KB

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