ads7846.c 36 KB

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