ads7846.c 36 KB

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