ads7846.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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[6];
  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(sizeof *req, GFP_KERNEL);
  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. CS_CHANGE(req->xfer[5]);
  337. spi_message_add_tail(&req->xfer[5], &req->msg);
  338. mutex_lock(&ts->lock);
  339. ads7846_stop(ts);
  340. status = spi_sync(spi, &req->msg);
  341. ads7846_restart(ts);
  342. mutex_unlock(&ts->lock);
  343. if (status == 0) {
  344. /* on-wire is a must-ignore bit, a BE12 value, then padding */
  345. status = be16_to_cpu(req->sample);
  346. status = status >> 3;
  347. status &= 0x0fff;
  348. }
  349. kfree(req);
  350. return status;
  351. }
  352. static int ads7845_read12_ser(struct device *dev, unsigned command)
  353. {
  354. struct spi_device *spi = to_spi_device(dev);
  355. struct ads7846 *ts = dev_get_drvdata(dev);
  356. struct ads7845_ser_req *req;
  357. int status;
  358. req = kzalloc(sizeof *req, GFP_KERNEL);
  359. if (!req)
  360. return -ENOMEM;
  361. spi_message_init(&req->msg);
  362. req->command[0] = (u8) command;
  363. req->xfer[0].tx_buf = req->command;
  364. req->xfer[0].rx_buf = req->sample;
  365. req->xfer[0].len = 3;
  366. spi_message_add_tail(&req->xfer[0], &req->msg);
  367. mutex_lock(&ts->lock);
  368. ads7846_stop(ts);
  369. status = spi_sync(spi, &req->msg);
  370. ads7846_restart(ts);
  371. mutex_unlock(&ts->lock);
  372. if (status == 0) {
  373. /* BE12 value, then padding */
  374. status = get_unaligned_be16(&req->sample[1]);
  375. status = status >> 3;
  376. status &= 0x0fff;
  377. }
  378. kfree(req);
  379. return status;
  380. }
  381. #if IS_ENABLED(CONFIG_HWMON)
  382. #define SHOW(name, var, adjust) static ssize_t \
  383. name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
  384. { \
  385. struct ads7846 *ts = dev_get_drvdata(dev); \
  386. ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
  387. READ_12BIT_SER(var)); \
  388. if (v < 0) \
  389. return v; \
  390. return sprintf(buf, "%u\n", adjust(ts, v)); \
  391. } \
  392. static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
  393. /* Sysfs conventions report temperatures in millidegrees Celsius.
  394. * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
  395. * accuracy scheme without calibration data. For now we won't try either;
  396. * userspace sees raw sensor values, and must scale/calibrate appropriately.
  397. */
  398. static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
  399. {
  400. return v;
  401. }
  402. SHOW(temp0, temp0, null_adjust) /* temp1_input */
  403. SHOW(temp1, temp1, null_adjust) /* temp2_input */
  404. /* sysfs conventions report voltages in millivolts. We can convert voltages
  405. * if we know vREF. userspace may need to scale vAUX to match the board's
  406. * external resistors; we assume that vBATT only uses the internal ones.
  407. */
  408. static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
  409. {
  410. unsigned retval = v;
  411. /* external resistors may scale vAUX into 0..vREF */
  412. retval *= ts->vref_mv;
  413. retval = retval >> 12;
  414. return retval;
  415. }
  416. static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
  417. {
  418. unsigned retval = vaux_adjust(ts, v);
  419. /* ads7846 has a resistor ladder to scale this signal down */
  420. if (ts->model == 7846)
  421. retval *= 4;
  422. return retval;
  423. }
  424. SHOW(in0_input, vaux, vaux_adjust)
  425. SHOW(in1_input, vbatt, vbatt_adjust)
  426. static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
  427. int index)
  428. {
  429. struct device *dev = kobj_to_dev(kobj);
  430. struct ads7846 *ts = dev_get_drvdata(dev);
  431. if (ts->model == 7843 && index < 2) /* in0, in1 */
  432. return 0;
  433. if (ts->model == 7845 && index != 2) /* in0 */
  434. return 0;
  435. return attr->mode;
  436. }
  437. static struct attribute *ads7846_attributes[] = {
  438. &dev_attr_temp0.attr, /* 0 */
  439. &dev_attr_temp1.attr, /* 1 */
  440. &dev_attr_in0_input.attr, /* 2 */
  441. &dev_attr_in1_input.attr, /* 3 */
  442. NULL,
  443. };
  444. static const struct attribute_group ads7846_attr_group = {
  445. .attrs = ads7846_attributes,
  446. .is_visible = ads7846_is_visible,
  447. };
  448. __ATTRIBUTE_GROUPS(ads7846_attr);
  449. static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
  450. {
  451. struct device *hwmon;
  452. /* hwmon sensors need a reference voltage */
  453. switch (ts->model) {
  454. case 7846:
  455. if (!ts->vref_mv) {
  456. dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
  457. ts->vref_mv = 2500;
  458. ts->use_internal = true;
  459. }
  460. break;
  461. case 7845:
  462. case 7843:
  463. if (!ts->vref_mv) {
  464. dev_warn(&spi->dev,
  465. "external vREF for ADS%d not specified\n",
  466. ts->model);
  467. return 0;
  468. }
  469. break;
  470. }
  471. hwmon = devm_hwmon_device_register_with_groups(&spi->dev,
  472. spi->modalias, ts,
  473. ads7846_attr_groups);
  474. return PTR_ERR_OR_ZERO(hwmon);
  475. }
  476. #else
  477. static inline int ads784x_hwmon_register(struct spi_device *spi,
  478. struct ads7846 *ts)
  479. {
  480. return 0;
  481. }
  482. #endif
  483. static ssize_t ads7846_pen_down_show(struct device *dev,
  484. struct device_attribute *attr, char *buf)
  485. {
  486. struct ads7846 *ts = dev_get_drvdata(dev);
  487. return sprintf(buf, "%u\n", ts->pendown);
  488. }
  489. static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
  490. static ssize_t ads7846_disable_show(struct device *dev,
  491. struct device_attribute *attr, char *buf)
  492. {
  493. struct ads7846 *ts = dev_get_drvdata(dev);
  494. return sprintf(buf, "%u\n", ts->disabled);
  495. }
  496. static ssize_t ads7846_disable_store(struct device *dev,
  497. struct device_attribute *attr,
  498. const char *buf, size_t count)
  499. {
  500. struct ads7846 *ts = dev_get_drvdata(dev);
  501. unsigned int i;
  502. int err;
  503. err = kstrtouint(buf, 10, &i);
  504. if (err)
  505. return err;
  506. if (i)
  507. ads7846_disable(ts);
  508. else
  509. ads7846_enable(ts);
  510. return count;
  511. }
  512. static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
  513. static struct attribute *ads784x_attrs[] = {
  514. &dev_attr_pen_down.attr,
  515. &dev_attr_disable.attr,
  516. NULL,
  517. };
  518. ATTRIBUTE_GROUPS(ads784x);
  519. /*--------------------------------------------------------------------------*/
  520. static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
  521. {
  522. struct ads7846 *ts = ads;
  523. if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
  524. /* Start over collecting consistent readings. */
  525. ts->read_rep = 0;
  526. /*
  527. * Repeat it, if this was the first read or the read
  528. * wasn't consistent enough.
  529. */
  530. if (ts->read_cnt < ts->debounce_max) {
  531. ts->last_read = *val;
  532. ts->read_cnt++;
  533. return ADS7846_FILTER_REPEAT;
  534. } else {
  535. /*
  536. * Maximum number of debouncing reached and still
  537. * not enough number of consistent readings. Abort
  538. * the whole sample, repeat it in the next sampling
  539. * period.
  540. */
  541. ts->read_cnt = 0;
  542. return ADS7846_FILTER_IGNORE;
  543. }
  544. } else {
  545. if (++ts->read_rep > ts->debounce_rep) {
  546. /*
  547. * Got a good reading for this coordinate,
  548. * go for the next one.
  549. */
  550. ts->read_cnt = 0;
  551. ts->read_rep = 0;
  552. return ADS7846_FILTER_OK;
  553. } else {
  554. /* Read more values that are consistent. */
  555. ts->read_cnt++;
  556. return ADS7846_FILTER_REPEAT;
  557. }
  558. }
  559. }
  560. static int ads7846_no_filter(void *ads, int data_idx, int *val)
  561. {
  562. return ADS7846_FILTER_OK;
  563. }
  564. static int ads7846_get_value(struct ads7846_buf *buf)
  565. {
  566. int value;
  567. value = be16_to_cpup(&buf->data);
  568. /* enforce ADC output is 12 bits width */
  569. return (value >> 3) & 0xfff;
  570. }
  571. static void ads7846_set_cmd_val(struct ads7846 *ts, enum ads7846_cmds cmd_idx,
  572. u16 val)
  573. {
  574. struct ads7846_packet *packet = ts->packet;
  575. switch (cmd_idx) {
  576. case ADS7846_Y:
  577. packet->y = val;
  578. break;
  579. case ADS7846_X:
  580. packet->x = val;
  581. break;
  582. case ADS7846_Z1:
  583. packet->z1 = val;
  584. break;
  585. case ADS7846_Z2:
  586. packet->z2 = val;
  587. break;
  588. default:
  589. WARN_ON_ONCE(1);
  590. }
  591. }
  592. static u8 ads7846_get_cmd(enum ads7846_cmds cmd_idx, int vref)
  593. {
  594. switch (cmd_idx) {
  595. case ADS7846_Y:
  596. return READ_Y(vref);
  597. case ADS7846_X:
  598. return READ_X(vref);
  599. /* 7846 specific commands */
  600. case ADS7846_Z1:
  601. return READ_Z1(vref);
  602. case ADS7846_Z2:
  603. return READ_Z2(vref);
  604. case ADS7846_PWDOWN:
  605. return PWRDOWN;
  606. default:
  607. WARN_ON_ONCE(1);
  608. }
  609. return 0;
  610. }
  611. static bool ads7846_cmd_need_settle(enum ads7846_cmds cmd_idx)
  612. {
  613. switch (cmd_idx) {
  614. case ADS7846_X:
  615. case ADS7846_Y:
  616. case ADS7846_Z1:
  617. case ADS7846_Z2:
  618. return true;
  619. case ADS7846_PWDOWN:
  620. return false;
  621. default:
  622. WARN_ON_ONCE(1);
  623. }
  624. return false;
  625. }
  626. static int ads7846_filter(struct ads7846 *ts)
  627. {
  628. struct ads7846_packet *packet = ts->packet;
  629. int action;
  630. int val;
  631. unsigned int cmd_idx, b;
  632. packet->ignore = false;
  633. for (cmd_idx = packet->last_cmd_idx; cmd_idx < packet->cmds - 1; cmd_idx++) {
  634. struct ads7846_buf_layout *l = &packet->l[cmd_idx];
  635. packet->last_cmd_idx = cmd_idx;
  636. for (b = l->skip; b < l->count; b++) {
  637. val = ads7846_get_value(&packet->rx[l->offset + b]);
  638. action = ts->filter(ts->filter_data, cmd_idx, &val);
  639. if (action == ADS7846_FILTER_REPEAT) {
  640. if (b == l->count - 1)
  641. return -EAGAIN;
  642. } else if (action == ADS7846_FILTER_OK) {
  643. ads7846_set_cmd_val(ts, cmd_idx, val);
  644. break;
  645. } else {
  646. packet->ignore = true;
  647. return 0;
  648. }
  649. }
  650. }
  651. return 0;
  652. }
  653. static void ads7846_wait_for_hsync(struct ads7846 *ts)
  654. {
  655. if (ts->wait_for_sync) {
  656. ts->wait_for_sync();
  657. return;
  658. }
  659. if (!ts->gpio_hsync)
  660. return;
  661. /*
  662. * Wait for HSYNC to assert the line should be flagged
  663. * as active low so here we are waiting for it to assert
  664. */
  665. while (!gpiod_get_value(ts->gpio_hsync))
  666. cpu_relax();
  667. /* Then we wait for it do de-assert */
  668. while (gpiod_get_value(ts->gpio_hsync))
  669. cpu_relax();
  670. }
  671. static void ads7846_read_state(struct ads7846 *ts)
  672. {
  673. struct ads7846_packet *packet = ts->packet;
  674. struct spi_message *m;
  675. int msg_idx = 0;
  676. int error;
  677. packet->last_cmd_idx = 0;
  678. while (true) {
  679. ads7846_wait_for_hsync(ts);
  680. m = &ts->msg[msg_idx];
  681. error = spi_sync(ts->spi, m);
  682. if (error) {
  683. dev_err_ratelimited(&ts->spi->dev, "spi_sync --> %d\n", error);
  684. packet->ignore = true;
  685. return;
  686. }
  687. error = ads7846_filter(ts);
  688. if (error)
  689. continue;
  690. return;
  691. }
  692. }
  693. static void ads7846_report_state(struct ads7846 *ts)
  694. {
  695. struct ads7846_packet *packet = ts->packet;
  696. unsigned int Rt;
  697. u16 x, y, z1, z2;
  698. x = packet->x;
  699. y = packet->y;
  700. if (ts->model == 7845) {
  701. z1 = 0;
  702. z2 = 0;
  703. } else {
  704. z1 = packet->z1;
  705. z2 = packet->z2;
  706. }
  707. /* range filtering */
  708. if (x == MAX_12BIT)
  709. x = 0;
  710. if (ts->model == 7843 || ts->model == 7845) {
  711. Rt = ts->pressure_max / 2;
  712. } else if (likely(x && z1)) {
  713. /* compute touch pressure resistance using equation #2 */
  714. Rt = z2;
  715. Rt -= z1;
  716. Rt *= ts->x_plate_ohms;
  717. Rt = DIV_ROUND_CLOSEST(Rt, 16);
  718. Rt *= x;
  719. Rt /= z1;
  720. Rt = DIV_ROUND_CLOSEST(Rt, 256);
  721. } else {
  722. Rt = 0;
  723. }
  724. /*
  725. * Sample found inconsistent by debouncing or pressure is beyond
  726. * the maximum. Don't report it to user space, repeat at least
  727. * once more the measurement
  728. */
  729. if (packet->ignore || Rt > ts->pressure_max) {
  730. dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
  731. packet->ignore, Rt);
  732. return;
  733. }
  734. /*
  735. * Maybe check the pendown state before reporting. This discards
  736. * false readings when the pen is lifted.
  737. */
  738. if (ts->penirq_recheck_delay_usecs) {
  739. udelay(ts->penirq_recheck_delay_usecs);
  740. if (!get_pendown_state(ts))
  741. Rt = 0;
  742. }
  743. /*
  744. * NOTE: We can't rely on the pressure to determine the pen down
  745. * state, even this controller has a pressure sensor. The pressure
  746. * value can fluctuate for quite a while after lifting the pen and
  747. * in some cases may not even settle at the expected value.
  748. *
  749. * The only safe way to check for the pen up condition is in the
  750. * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
  751. */
  752. if (Rt) {
  753. struct input_dev *input = ts->input;
  754. if (!ts->pendown) {
  755. input_report_key(input, BTN_TOUCH, 1);
  756. ts->pendown = true;
  757. dev_vdbg(&ts->spi->dev, "DOWN\n");
  758. }
  759. touchscreen_report_pos(input, &ts->core_prop, x, y, false);
  760. input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
  761. input_sync(input);
  762. dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
  763. }
  764. }
  765. static irqreturn_t ads7846_hard_irq(int irq, void *handle)
  766. {
  767. struct ads7846 *ts = handle;
  768. return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
  769. }
  770. static irqreturn_t ads7846_irq(int irq, void *handle)
  771. {
  772. struct ads7846 *ts = handle;
  773. /* Start with a small delay before checking pendown state */
  774. msleep(TS_POLL_DELAY);
  775. while (!ts->stopped && get_pendown_state(ts)) {
  776. /* pen is down, continue with the measurement */
  777. ads7846_read_state(ts);
  778. if (!ts->stopped)
  779. ads7846_report_state(ts);
  780. wait_event_timeout(ts->wait, ts->stopped,
  781. msecs_to_jiffies(TS_POLL_PERIOD));
  782. }
  783. if (ts->pendown && !ts->stopped)
  784. ads7846_report_pen_up(ts);
  785. return IRQ_HANDLED;
  786. }
  787. static int ads7846_suspend(struct device *dev)
  788. {
  789. struct ads7846 *ts = dev_get_drvdata(dev);
  790. mutex_lock(&ts->lock);
  791. if (!ts->suspended) {
  792. if (!ts->disabled)
  793. __ads7846_disable(ts);
  794. if (device_may_wakeup(&ts->spi->dev))
  795. enable_irq_wake(ts->spi->irq);
  796. ts->suspended = true;
  797. }
  798. mutex_unlock(&ts->lock);
  799. return 0;
  800. }
  801. static int ads7846_resume(struct device *dev)
  802. {
  803. struct ads7846 *ts = dev_get_drvdata(dev);
  804. mutex_lock(&ts->lock);
  805. if (ts->suspended) {
  806. ts->suspended = false;
  807. if (device_may_wakeup(&ts->spi->dev))
  808. disable_irq_wake(ts->spi->irq);
  809. if (!ts->disabled)
  810. __ads7846_enable(ts);
  811. }
  812. mutex_unlock(&ts->lock);
  813. return 0;
  814. }
  815. static DEFINE_SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
  816. static int ads7846_setup_pendown(struct spi_device *spi,
  817. struct ads7846 *ts,
  818. const struct ads7846_platform_data *pdata)
  819. {
  820. /*
  821. * REVISIT when the irq can be triggered active-low, or if for some
  822. * reason the touchscreen isn't hooked up, we don't need to access
  823. * the pendown state.
  824. */
  825. if (pdata->get_pendown_state) {
  826. ts->get_pendown_state = pdata->get_pendown_state;
  827. } else {
  828. ts->gpio_pendown = devm_gpiod_get(&spi->dev, "pendown", GPIOD_IN);
  829. if (IS_ERR(ts->gpio_pendown)) {
  830. dev_err(&spi->dev, "failed to request pendown GPIO\n");
  831. return PTR_ERR(ts->gpio_pendown);
  832. }
  833. if (pdata->gpio_pendown_debounce)
  834. gpiod_set_debounce(ts->gpio_pendown,
  835. pdata->gpio_pendown_debounce);
  836. }
  837. return 0;
  838. }
  839. /*
  840. * Set up the transfers to read touchscreen state; this assumes we
  841. * use formula #2 for pressure, not #3.
  842. */
  843. static int ads7846_setup_spi_msg(struct ads7846 *ts,
  844. const struct ads7846_platform_data *pdata)
  845. {
  846. struct spi_message *m = &ts->msg[0];
  847. struct spi_transfer *x = ts->xfer;
  848. struct ads7846_packet *packet = ts->packet;
  849. int vref = pdata->keep_vref_on;
  850. unsigned int count, offset = 0;
  851. unsigned int cmd_idx, b;
  852. unsigned long time;
  853. size_t size = 0;
  854. /* time per bit */
  855. time = NSEC_PER_SEC / ts->spi->max_speed_hz;
  856. count = pdata->settle_delay_usecs * NSEC_PER_USEC / time;
  857. packet->count_skip = DIV_ROUND_UP(count, 24);
  858. if (ts->debounce_max && ts->debounce_rep)
  859. /* ads7846_debounce_filter() is making ts->debounce_rep + 2
  860. * reads. So we need to get all samples for normal case. */
  861. packet->count = ts->debounce_rep + 2;
  862. else
  863. packet->count = 1;
  864. if (ts->model == 7846)
  865. packet->cmds = 5; /* x, y, z1, z2, pwdown */
  866. else
  867. packet->cmds = 3; /* x, y, pwdown */
  868. for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) {
  869. struct ads7846_buf_layout *l = &packet->l[cmd_idx];
  870. unsigned int max_count;
  871. if (cmd_idx == packet->cmds - 1)
  872. cmd_idx = ADS7846_PWDOWN;
  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;
  904. if (cmd_idx == packet->cmds - 1)
  905. cmd_idx = ADS7846_PWDOWN;
  906. cmd = ads7846_get_cmd(cmd_idx, vref);
  907. for (b = 0; b < l->count; b++)
  908. packet->tx[l->offset + b].cmd = cmd;
  909. }
  910. x->tx_buf = packet->tx;
  911. x->rx_buf = packet->rx;
  912. x->len = size;
  913. spi_message_add_tail(x, m);
  914. return 0;
  915. }
  916. static const struct of_device_id ads7846_dt_ids[] = {
  917. { .compatible = "ti,tsc2046", .data = (void *) 7846 },
  918. { .compatible = "ti,ads7843", .data = (void *) 7843 },
  919. { .compatible = "ti,ads7845", .data = (void *) 7845 },
  920. { .compatible = "ti,ads7846", .data = (void *) 7846 },
  921. { .compatible = "ti,ads7873", .data = (void *) 7873 },
  922. { }
  923. };
  924. MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
  925. static const struct spi_device_id ads7846_spi_ids[] = {
  926. { "tsc2046", 7846 },
  927. { "ads7843", 7843 },
  928. { "ads7845", 7845 },
  929. { "ads7846", 7846 },
  930. { "ads7873", 7873 },
  931. { },
  932. };
  933. MODULE_DEVICE_TABLE(spi, ads7846_spi_ids);
  934. static const struct ads7846_platform_data *ads7846_get_props(struct device *dev)
  935. {
  936. struct ads7846_platform_data *pdata;
  937. u32 value;
  938. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  939. if (!pdata)
  940. return ERR_PTR(-ENOMEM);
  941. pdata->model = (uintptr_t)device_get_match_data(dev);
  942. device_property_read_u16(dev, "ti,vref-delay-usecs",
  943. &pdata->vref_delay_usecs);
  944. device_property_read_u16(dev, "ti,vref-mv", &pdata->vref_mv);
  945. pdata->keep_vref_on = device_property_read_bool(dev, "ti,keep-vref-on");
  946. pdata->swap_xy = device_property_read_bool(dev, "ti,swap-xy");
  947. device_property_read_u16(dev, "ti,settle-delay-usec",
  948. &pdata->settle_delay_usecs);
  949. device_property_read_u16(dev, "ti,penirq-recheck-delay-usecs",
  950. &pdata->penirq_recheck_delay_usecs);
  951. device_property_read_u16(dev, "ti,x-plate-ohms", &pdata->x_plate_ohms);
  952. device_property_read_u16(dev, "ti,y-plate-ohms", &pdata->y_plate_ohms);
  953. device_property_read_u16(dev, "ti,x-min", &pdata->x_min);
  954. device_property_read_u16(dev, "ti,y-min", &pdata->y_min);
  955. device_property_read_u16(dev, "ti,x-max", &pdata->x_max);
  956. device_property_read_u16(dev, "ti,y-max", &pdata->y_max);
  957. /*
  958. * touchscreen-max-pressure gets parsed during
  959. * touchscreen_parse_properties()
  960. */
  961. device_property_read_u16(dev, "ti,pressure-min", &pdata->pressure_min);
  962. if (!device_property_read_u32(dev, "touchscreen-min-pressure", &value))
  963. pdata->pressure_min = (u16) value;
  964. device_property_read_u16(dev, "ti,pressure-max", &pdata->pressure_max);
  965. device_property_read_u16(dev, "ti,debounce-max", &pdata->debounce_max);
  966. if (!device_property_read_u32(dev, "touchscreen-average-samples", &value))
  967. pdata->debounce_max = (u16) value;
  968. device_property_read_u16(dev, "ti,debounce-tol", &pdata->debounce_tol);
  969. device_property_read_u16(dev, "ti,debounce-rep", &pdata->debounce_rep);
  970. device_property_read_u32(dev, "ti,pendown-gpio-debounce",
  971. &pdata->gpio_pendown_debounce);
  972. pdata->wakeup = device_property_read_bool(dev, "wakeup-source") ||
  973. device_property_read_bool(dev, "linux,wakeup");
  974. return pdata;
  975. }
  976. static void ads7846_regulator_disable(void *regulator)
  977. {
  978. regulator_disable(regulator);
  979. }
  980. static int ads7846_probe(struct spi_device *spi)
  981. {
  982. const struct ads7846_platform_data *pdata;
  983. struct ads7846 *ts;
  984. struct device *dev = &spi->dev;
  985. struct ads7846_packet *packet;
  986. struct input_dev *input_dev;
  987. unsigned long irq_flags;
  988. int err;
  989. if (!spi->irq) {
  990. dev_dbg(dev, "no IRQ?\n");
  991. return -EINVAL;
  992. }
  993. /* don't exceed max specified sample rate */
  994. if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
  995. dev_err(dev, "f(sample) %d KHz?\n",
  996. (spi->max_speed_hz/SAMPLE_BITS)/1000);
  997. return -EINVAL;
  998. }
  999. /*
  1000. * We'd set TX word size 8 bits and RX word size to 13 bits ... except
  1001. * that even if the hardware can do that, the SPI controller driver
  1002. * may not. So we stick to very-portable 8 bit words, both RX and TX.
  1003. */
  1004. spi->bits_per_word = 8;
  1005. spi->mode &= ~SPI_MODE_X_MASK;
  1006. spi->mode |= SPI_MODE_0;
  1007. err = spi_setup(spi);
  1008. if (err < 0)
  1009. return err;
  1010. ts = devm_kzalloc(dev, sizeof(struct ads7846), GFP_KERNEL);
  1011. if (!ts)
  1012. return -ENOMEM;
  1013. packet = devm_kzalloc(dev, sizeof(struct ads7846_packet), GFP_KERNEL);
  1014. if (!packet)
  1015. return -ENOMEM;
  1016. input_dev = devm_input_allocate_device(dev);
  1017. if (!input_dev)
  1018. return -ENOMEM;
  1019. spi_set_drvdata(spi, ts);
  1020. ts->packet = packet;
  1021. ts->spi = spi;
  1022. ts->input = input_dev;
  1023. mutex_init(&ts->lock);
  1024. init_waitqueue_head(&ts->wait);
  1025. pdata = dev_get_platdata(dev);
  1026. if (!pdata) {
  1027. pdata = ads7846_get_props(dev);
  1028. if (IS_ERR(pdata))
  1029. return PTR_ERR(pdata);
  1030. }
  1031. ts->model = pdata->model ? : 7846;
  1032. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  1033. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  1034. ts->vref_mv = pdata->vref_mv;
  1035. if (pdata->debounce_max) {
  1036. ts->debounce_max = pdata->debounce_max;
  1037. if (ts->debounce_max < 2)
  1038. ts->debounce_max = 2;
  1039. ts->debounce_tol = pdata->debounce_tol;
  1040. ts->debounce_rep = pdata->debounce_rep;
  1041. ts->filter = ads7846_debounce_filter;
  1042. ts->filter_data = ts;
  1043. } else {
  1044. ts->filter = ads7846_no_filter;
  1045. }
  1046. err = ads7846_setup_pendown(spi, ts, pdata);
  1047. if (err)
  1048. return err;
  1049. if (pdata->penirq_recheck_delay_usecs)
  1050. ts->penirq_recheck_delay_usecs =
  1051. pdata->penirq_recheck_delay_usecs;
  1052. ts->wait_for_sync = pdata->wait_for_sync;
  1053. ts->gpio_hsync = devm_gpiod_get_optional(dev, "ti,hsync", GPIOD_IN);
  1054. if (IS_ERR(ts->gpio_hsync))
  1055. return PTR_ERR(ts->gpio_hsync);
  1056. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
  1057. snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
  1058. input_dev->name = ts->name;
  1059. input_dev->phys = ts->phys;
  1060. input_dev->id.bustype = BUS_SPI;
  1061. input_dev->id.product = pdata->model;
  1062. input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
  1063. input_set_abs_params(input_dev, ABS_X,
  1064. pdata->x_min ? : 0,
  1065. pdata->x_max ? : MAX_12BIT,
  1066. 0, 0);
  1067. input_set_abs_params(input_dev, ABS_Y,
  1068. pdata->y_min ? : 0,
  1069. pdata->y_max ? : MAX_12BIT,
  1070. 0, 0);
  1071. if (ts->model != 7845)
  1072. input_set_abs_params(input_dev, ABS_PRESSURE,
  1073. pdata->pressure_min, pdata->pressure_max, 0, 0);
  1074. /*
  1075. * Parse common framework properties. Must be done here to ensure the
  1076. * correct behaviour in case of using the legacy vendor bindings. The
  1077. * general binding value overrides the vendor specific one.
  1078. */
  1079. touchscreen_parse_properties(ts->input, false, &ts->core_prop);
  1080. ts->pressure_max = input_abs_get_max(input_dev, ABS_PRESSURE) ? : ~0;
  1081. /*
  1082. * Check if legacy ti,swap-xy binding is used instead of
  1083. * touchscreen-swapped-x-y
  1084. */
  1085. if (!ts->core_prop.swap_x_y && pdata->swap_xy) {
  1086. swap(input_dev->absinfo[ABS_X], input_dev->absinfo[ABS_Y]);
  1087. ts->core_prop.swap_x_y = true;
  1088. }
  1089. ads7846_setup_spi_msg(ts, pdata);
  1090. ts->reg = devm_regulator_get(dev, "vcc");
  1091. if (IS_ERR(ts->reg)) {
  1092. err = PTR_ERR(ts->reg);
  1093. dev_err(dev, "unable to get regulator: %d\n", err);
  1094. return err;
  1095. }
  1096. err = regulator_enable(ts->reg);
  1097. if (err) {
  1098. dev_err(dev, "unable to enable regulator: %d\n", err);
  1099. return err;
  1100. }
  1101. err = devm_add_action_or_reset(dev, ads7846_regulator_disable, ts->reg);
  1102. if (err)
  1103. return err;
  1104. irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
  1105. irq_flags |= IRQF_ONESHOT;
  1106. err = devm_request_threaded_irq(dev, spi->irq,
  1107. ads7846_hard_irq, ads7846_irq,
  1108. irq_flags, dev->driver->name, ts);
  1109. if (err && err != -EPROBE_DEFER && !pdata->irq_flags) {
  1110. dev_info(dev,
  1111. "trying pin change workaround on irq %d\n", spi->irq);
  1112. irq_flags |= IRQF_TRIGGER_RISING;
  1113. err = devm_request_threaded_irq(dev, spi->irq,
  1114. ads7846_hard_irq, ads7846_irq,
  1115. irq_flags, dev->driver->name,
  1116. ts);
  1117. }
  1118. if (err) {
  1119. dev_dbg(dev, "irq %d busy?\n", spi->irq);
  1120. return err;
  1121. }
  1122. err = ads784x_hwmon_register(spi, ts);
  1123. if (err)
  1124. return err;
  1125. dev_info(dev, "touchscreen, irq %d\n", spi->irq);
  1126. /*
  1127. * Take a first sample, leaving nPENIRQ active and vREF off; avoid
  1128. * the touchscreen, in case it's not connected.
  1129. */
  1130. if (ts->model == 7845)
  1131. ads7845_read12_ser(dev, PWRDOWN);
  1132. else
  1133. (void) ads7846_read12_ser(dev, READ_12BIT_SER(vaux));
  1134. err = input_register_device(input_dev);
  1135. if (err)
  1136. return err;
  1137. device_init_wakeup(dev, pdata->wakeup);
  1138. /*
  1139. * If device does not carry platform data we must have allocated it
  1140. * when parsing DT data.
  1141. */
  1142. if (!dev_get_platdata(dev))
  1143. devm_kfree(dev, (void *)pdata);
  1144. return 0;
  1145. }
  1146. static void ads7846_remove(struct spi_device *spi)
  1147. {
  1148. struct ads7846 *ts = spi_get_drvdata(spi);
  1149. ads7846_stop(ts);
  1150. }
  1151. static struct spi_driver ads7846_driver = {
  1152. .driver = {
  1153. .name = "ads7846",
  1154. .dev_groups = ads784x_groups,
  1155. .pm = pm_sleep_ptr(&ads7846_pm),
  1156. .of_match_table = ads7846_dt_ids,
  1157. },
  1158. .probe = ads7846_probe,
  1159. .remove = ads7846_remove,
  1160. .id_table = ads7846_spi_ids,
  1161. };
  1162. module_spi_driver(ads7846_driver);
  1163. MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
  1164. MODULE_LICENSE("GPL");