ads7846.c 34 KB

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