ads7846.c 36 KB

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