ads7846.c 36 KB

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