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