ads7846.c 36 KB

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