ads7846.c 34 KB

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