at91_adc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for the ADC present in the Atmel AT91 evaluation boards.
  4. *
  5. * Copyright 2011 Free Electrons
  6. */
  7. #include <linux/bitmap.h>
  8. #include <linux/bitops.h>
  9. #include <linux/cleanup.h>
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/input.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/wait.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/buffer.h>
  25. #include <linux/iio/trigger.h>
  26. #include <linux/iio/trigger_consumer.h>
  27. #include <linux/iio/triggered_buffer.h>
  28. #include <linux/pinctrl/consumer.h>
  29. /* Registers */
  30. #define AT91_ADC_CR 0x00 /* Control Register */
  31. #define AT91_ADC_SWRST (1 << 0) /* Software Reset */
  32. #define AT91_ADC_START (1 << 1) /* Start Conversion */
  33. #define AT91_ADC_MR 0x04 /* Mode Register */
  34. #define AT91_ADC_TSAMOD (3 << 0) /* ADC mode */
  35. #define AT91_ADC_TSAMOD_ADC_ONLY_MODE (0 << 0) /* ADC Mode */
  36. #define AT91_ADC_TSAMOD_TS_ONLY_MODE (1 << 0) /* Touch Screen Only Mode */
  37. #define AT91_ADC_TRGEN (1 << 0) /* Trigger Enable */
  38. #define AT91_ADC_TRGSEL (7 << 1) /* Trigger Selection */
  39. #define AT91_ADC_TRGSEL_TC0 (0 << 1)
  40. #define AT91_ADC_TRGSEL_TC1 (1 << 1)
  41. #define AT91_ADC_TRGSEL_TC2 (2 << 1)
  42. #define AT91_ADC_TRGSEL_EXTERNAL (6 << 1)
  43. #define AT91_ADC_LOWRES (1 << 4) /* Low Resolution */
  44. #define AT91_ADC_SLEEP (1 << 5) /* Sleep Mode */
  45. #define AT91_ADC_PENDET (1 << 6) /* Pen contact detection enable */
  46. #define AT91_ADC_PRESCAL_9260 (0x3f << 8) /* Prescalar Rate Selection */
  47. #define AT91_ADC_PRESCAL_9G45 (0xff << 8)
  48. #define AT91_ADC_PRESCAL_(x) ((x) << 8)
  49. #define AT91_ADC_STARTUP_9260 (0x1f << 16) /* Startup Up Time */
  50. #define AT91_ADC_STARTUP_9G45 (0x7f << 16)
  51. #define AT91_ADC_STARTUP_9X5 (0xf << 16)
  52. #define AT91_ADC_STARTUP_(x) ((x) << 16)
  53. #define AT91_ADC_SHTIM (0xf << 24) /* Sample & Hold Time */
  54. #define AT91_ADC_SHTIM_(x) ((x) << 24)
  55. #define AT91_ADC_PENDBC (0x0f << 28) /* Pen Debounce time */
  56. #define AT91_ADC_PENDBC_(x) ((x) << 28)
  57. #define AT91_ADC_TSR 0x0C
  58. #define AT91_ADC_TSR_SHTIM (0xf << 24) /* Sample & Hold Time */
  59. #define AT91_ADC_TSR_SHTIM_(x) ((x) << 24)
  60. #define AT91_ADC_CHER 0x10 /* Channel Enable Register */
  61. #define AT91_ADC_CHDR 0x14 /* Channel Disable Register */
  62. #define AT91_ADC_CHSR 0x18 /* Channel Status Register */
  63. #define AT91_ADC_CH(n) (1 << (n)) /* Channel Number */
  64. #define AT91_ADC_SR 0x1C /* Status Register */
  65. #define AT91_ADC_EOC(n) (1 << (n)) /* End of Conversion on Channel N */
  66. #define AT91_ADC_OVRE(n) (1 << ((n) + 8))/* Overrun Error on Channel N */
  67. #define AT91_ADC_DRDY (1 << 16) /* Data Ready */
  68. #define AT91_ADC_GOVRE (1 << 17) /* General Overrun Error */
  69. #define AT91_ADC_ENDRX (1 << 18) /* End of RX Buffer */
  70. #define AT91_ADC_RXFUFF (1 << 19) /* RX Buffer Full */
  71. #define AT91_ADC_SR_9X5 0x30 /* Status Register for 9x5 */
  72. #define AT91_ADC_SR_DRDY_9X5 (1 << 24) /* Data Ready */
  73. #define AT91_ADC_LCDR 0x20 /* Last Converted Data Register */
  74. #define AT91_ADC_LDATA (0x3ff)
  75. #define AT91_ADC_IER 0x24 /* Interrupt Enable Register */
  76. #define AT91_ADC_IDR 0x28 /* Interrupt Disable Register */
  77. #define AT91_ADC_IMR 0x2C /* Interrupt Mask Register */
  78. #define AT91RL_ADC_IER_PEN (1 << 20)
  79. #define AT91RL_ADC_IER_NOPEN (1 << 21)
  80. #define AT91_ADC_IER_PEN (1 << 29)
  81. #define AT91_ADC_IER_NOPEN (1 << 30)
  82. #define AT91_ADC_IER_XRDY (1 << 20)
  83. #define AT91_ADC_IER_YRDY (1 << 21)
  84. #define AT91_ADC_IER_PRDY (1 << 22)
  85. #define AT91_ADC_ISR_PENS (1 << 31)
  86. #define AT91_ADC_CHR(n) (0x30 + ((n) * 4)) /* Channel Data Register N */
  87. #define AT91_ADC_DATA (0x3ff)
  88. #define AT91_ADC_CDR0_9X5 (0x50) /* Channel Data Register 0 for 9X5 */
  89. #define AT91_ADC_ACR 0x94 /* Analog Control Register */
  90. #define AT91_ADC_ACR_PENDETSENS (0x3 << 0) /* pull-up resistor */
  91. #define AT91_ADC_TSMR 0xB0
  92. #define AT91_ADC_TSMR_TSMODE (3 << 0) /* Touch Screen Mode */
  93. #define AT91_ADC_TSMR_TSMODE_NONE (0 << 0)
  94. #define AT91_ADC_TSMR_TSMODE_4WIRE_NO_PRESS (1 << 0)
  95. #define AT91_ADC_TSMR_TSMODE_4WIRE_PRESS (2 << 0)
  96. #define AT91_ADC_TSMR_TSMODE_5WIRE (3 << 0)
  97. #define AT91_ADC_TSMR_TSAV (3 << 4) /* Averages samples */
  98. #define AT91_ADC_TSMR_TSAV_(x) ((x) << 4)
  99. #define AT91_ADC_TSMR_SCTIM (0x0f << 16) /* Switch closure time */
  100. #define AT91_ADC_TSMR_SCTIM_(x) ((x) << 16)
  101. #define AT91_ADC_TSMR_PENDBC (0x0f << 28) /* Pen Debounce time */
  102. #define AT91_ADC_TSMR_PENDBC_(x) ((x) << 28)
  103. #define AT91_ADC_TSMR_NOTSDMA (1 << 22) /* No Touchscreen DMA */
  104. #define AT91_ADC_TSMR_PENDET_DIS (0 << 24) /* Pen contact detection disable */
  105. #define AT91_ADC_TSMR_PENDET_ENA (1 << 24) /* Pen contact detection enable */
  106. #define AT91_ADC_TSXPOSR 0xB4
  107. #define AT91_ADC_TSYPOSR 0xB8
  108. #define AT91_ADC_TSPRESSR 0xBC
  109. #define AT91_ADC_TRGR_9260 AT91_ADC_MR
  110. #define AT91_ADC_TRGR_9G45 0x08
  111. #define AT91_ADC_TRGR_9X5 0xC0
  112. /* Trigger Register bit field */
  113. #define AT91_ADC_TRGR_TRGPER (0xffff << 16)
  114. #define AT91_ADC_TRGR_TRGPER_(x) ((x) << 16)
  115. #define AT91_ADC_TRGR_TRGMOD (0x7 << 0)
  116. #define AT91_ADC_TRGR_NONE (0 << 0)
  117. #define AT91_ADC_TRGR_MOD_PERIOD_TRIG (5 << 0)
  118. #define AT91_ADC_CHAN(st, ch) \
  119. (st->registers->channel_base + (ch * 4))
  120. #define at91_adc_readl(st, reg) \
  121. (readl_relaxed(st->reg_base + reg))
  122. #define at91_adc_writel(st, reg, val) \
  123. (writel_relaxed(val, st->reg_base + reg))
  124. #define DRIVER_NAME "at91_adc"
  125. #define MAX_POS_BITS 12
  126. #define TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */
  127. #define TOUCH_PEN_DETECT_DEBOUNCE_US 200
  128. #define MAX_RLPOS_BITS 10
  129. #define TOUCH_SAMPLE_PERIOD_US_RL 10000 /* 10ms, the SoC can't keep up with 2ms */
  130. #define TOUCH_SHTIM 0xa
  131. #define TOUCH_SCTIM_US 10 /* 10us for the Touchscreen Switches Closure Time */
  132. enum atmel_adc_ts_type {
  133. ATMEL_ADC_TOUCHSCREEN_NONE = 0,
  134. ATMEL_ADC_TOUCHSCREEN_4WIRE = 4,
  135. ATMEL_ADC_TOUCHSCREEN_5WIRE = 5,
  136. };
  137. /**
  138. * struct at91_adc_trigger - description of triggers
  139. * @name: name of the trigger advertised to the user
  140. * @value: value to set in the ADC's trigger setup register
  141. * to enable the trigger
  142. * @is_external: Does the trigger rely on an external pin?
  143. */
  144. struct at91_adc_trigger {
  145. const char *name;
  146. u8 value;
  147. bool is_external;
  148. };
  149. /**
  150. * struct at91_adc_reg_desc - Various informations relative to registers
  151. * @channel_base: Base offset for the channel data registers
  152. * @drdy_mask: Mask of the DRDY field in the relevant registers
  153. * (Interruptions registers mostly)
  154. * @status_register: Offset of the Interrupt Status Register
  155. * @trigger_register: Offset of the Trigger setup register
  156. * @mr_prescal_mask: Mask of the PRESCAL field in the adc MR register
  157. * @mr_startup_mask: Mask of the STARTUP field in the adc MR register
  158. */
  159. struct at91_adc_reg_desc {
  160. u8 channel_base;
  161. u32 drdy_mask;
  162. u8 status_register;
  163. u8 trigger_register;
  164. u32 mr_prescal_mask;
  165. u32 mr_startup_mask;
  166. };
  167. struct at91_adc_caps {
  168. bool has_ts; /* Support touch screen */
  169. bool has_tsmr; /* only at91sam9x5, sama5d3 have TSMR reg */
  170. /*
  171. * Numbers of sampling data will be averaged. Can be 0~3.
  172. * Hardware can average (2 ^ ts_filter_average) sample data.
  173. */
  174. u8 ts_filter_average;
  175. /* Pen Detection input pull-up resistor, can be 0~3 */
  176. u8 ts_pen_detect_sensitivity;
  177. /* startup time calculate function */
  178. u32 (*calc_startup_ticks)(u32 startup_time, u32 adc_clk_khz);
  179. u8 num_channels;
  180. u8 low_res_bits;
  181. u8 high_res_bits;
  182. u32 trigger_number;
  183. const struct at91_adc_trigger *triggers;
  184. struct at91_adc_reg_desc registers;
  185. };
  186. struct at91_adc_state {
  187. struct clk *adc_clk;
  188. u16 *buffer;
  189. unsigned long channels_mask;
  190. struct clk *clk;
  191. bool done;
  192. int irq;
  193. u16 last_value;
  194. int chnb;
  195. struct mutex lock;
  196. u8 num_channels;
  197. void __iomem *reg_base;
  198. const struct at91_adc_reg_desc *registers;
  199. u32 startup_time;
  200. u8 sample_hold_time;
  201. bool sleep_mode;
  202. struct iio_trigger **trig;
  203. bool use_external;
  204. u32 vref_mv;
  205. u32 res; /* resolution used for convertions */
  206. wait_queue_head_t wq_data_avail;
  207. const struct at91_adc_caps *caps;
  208. /*
  209. * Following ADC channels are shared by touchscreen:
  210. *
  211. * CH0 -- Touch screen XP/UL
  212. * CH1 -- Touch screen XM/UR
  213. * CH2 -- Touch screen YP/LL
  214. * CH3 -- Touch screen YM/Sense
  215. * CH4 -- Touch screen LR(5-wire only)
  216. *
  217. * The bitfields below represents the reserved channel in the
  218. * touchscreen mode.
  219. */
  220. #define CHAN_MASK_TOUCHSCREEN_4WIRE (0xf << 0)
  221. #define CHAN_MASK_TOUCHSCREEN_5WIRE (0x1f << 0)
  222. enum atmel_adc_ts_type touchscreen_type;
  223. struct input_dev *ts_input;
  224. u16 ts_sample_period_val;
  225. u32 ts_pressure_threshold;
  226. u16 ts_pendbc;
  227. bool ts_bufferedmeasure;
  228. u32 ts_prev_absx;
  229. u32 ts_prev_absy;
  230. };
  231. static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
  232. {
  233. struct iio_poll_func *pf = p;
  234. struct iio_dev *idev = pf->indio_dev;
  235. struct at91_adc_state *st = iio_priv(idev);
  236. struct iio_chan_spec const *chan;
  237. int i, j = 0;
  238. iio_for_each_active_channel(idev, i) {
  239. chan = idev->channels + i;
  240. st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, chan->channel));
  241. j++;
  242. }
  243. iio_push_to_buffers_with_timestamp(idev, st->buffer, pf->timestamp);
  244. iio_trigger_notify_done(idev->trig);
  245. /* Needed to ACK the DRDY interruption */
  246. at91_adc_readl(st, AT91_ADC_LCDR);
  247. enable_irq(st->irq);
  248. return IRQ_HANDLED;
  249. }
  250. /* Handler for classic adc channel eoc trigger */
  251. static void handle_adc_eoc_trigger(int irq, struct iio_dev *idev)
  252. {
  253. struct at91_adc_state *st = iio_priv(idev);
  254. if (iio_buffer_enabled(idev)) {
  255. disable_irq_nosync(irq);
  256. iio_trigger_poll(idev->trig);
  257. } else {
  258. st->last_value = at91_adc_readl(st, AT91_ADC_CHAN(st, st->chnb));
  259. /* Needed to ACK the DRDY interruption */
  260. at91_adc_readl(st, AT91_ADC_LCDR);
  261. st->done = true;
  262. wake_up_interruptible(&st->wq_data_avail);
  263. }
  264. }
  265. static int at91_ts_sample(struct iio_dev *idev)
  266. {
  267. struct at91_adc_state *st = iio_priv(idev);
  268. unsigned int xscale, yscale, reg, z1, z2;
  269. unsigned int x, y, pres, xpos, ypos;
  270. unsigned int rxp = 1;
  271. unsigned int factor = 1000;
  272. unsigned int xyz_mask_bits = st->res;
  273. unsigned int xyz_mask = (1 << xyz_mask_bits) - 1;
  274. /* calculate position */
  275. /* x position = (x / xscale) * max, max = 2^MAX_POS_BITS - 1 */
  276. reg = at91_adc_readl(st, AT91_ADC_TSXPOSR);
  277. xpos = reg & xyz_mask;
  278. x = (xpos << MAX_POS_BITS) - xpos;
  279. xscale = (reg >> 16) & xyz_mask;
  280. if (xscale == 0) {
  281. dev_err(&idev->dev, "Error: xscale == 0!\n");
  282. return -1;
  283. }
  284. x /= xscale;
  285. /* y position = (y / yscale) * max, max = 2^MAX_POS_BITS - 1 */
  286. reg = at91_adc_readl(st, AT91_ADC_TSYPOSR);
  287. ypos = reg & xyz_mask;
  288. y = (ypos << MAX_POS_BITS) - ypos;
  289. yscale = (reg >> 16) & xyz_mask;
  290. if (yscale == 0) {
  291. dev_err(&idev->dev, "Error: yscale == 0!\n");
  292. return -1;
  293. }
  294. y /= yscale;
  295. /* calculate the pressure */
  296. reg = at91_adc_readl(st, AT91_ADC_TSPRESSR);
  297. z1 = reg & xyz_mask;
  298. z2 = (reg >> 16) & xyz_mask;
  299. if (z1 != 0)
  300. pres = rxp * (x * factor / 1024) * (z2 * factor / z1 - factor)
  301. / factor;
  302. else
  303. pres = st->ts_pressure_threshold; /* no pen contacted */
  304. dev_dbg(&idev->dev, "xpos = %d, xscale = %d, ypos = %d, yscale = %d, z1 = %d, z2 = %d, press = %d\n",
  305. xpos, xscale, ypos, yscale, z1, z2, pres);
  306. if (pres < st->ts_pressure_threshold) {
  307. dev_dbg(&idev->dev, "x = %d, y = %d, pressure = %d\n",
  308. x, y, pres / factor);
  309. input_report_abs(st->ts_input, ABS_X, x);
  310. input_report_abs(st->ts_input, ABS_Y, y);
  311. input_report_abs(st->ts_input, ABS_PRESSURE, pres);
  312. input_report_key(st->ts_input, BTN_TOUCH, 1);
  313. input_sync(st->ts_input);
  314. } else {
  315. dev_dbg(&idev->dev, "pressure too low: not reporting\n");
  316. }
  317. return 0;
  318. }
  319. static irqreturn_t at91_adc_rl_interrupt(int irq, void *private)
  320. {
  321. struct iio_dev *idev = private;
  322. struct at91_adc_state *st = iio_priv(idev);
  323. u32 status = at91_adc_readl(st, st->registers->status_register);
  324. unsigned int reg;
  325. status &= at91_adc_readl(st, AT91_ADC_IMR);
  326. if (status & GENMASK(st->num_channels - 1, 0))
  327. handle_adc_eoc_trigger(irq, idev);
  328. if (status & AT91RL_ADC_IER_PEN) {
  329. /* Disabling pen debounce is required to get a NOPEN irq */
  330. reg = at91_adc_readl(st, AT91_ADC_MR);
  331. reg &= ~AT91_ADC_PENDBC;
  332. at91_adc_writel(st, AT91_ADC_MR, reg);
  333. at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_PEN);
  334. at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_NOPEN
  335. | AT91_ADC_EOC(3));
  336. /* Set up period trigger for sampling */
  337. at91_adc_writel(st, st->registers->trigger_register,
  338. AT91_ADC_TRGR_MOD_PERIOD_TRIG |
  339. AT91_ADC_TRGR_TRGPER_(st->ts_sample_period_val));
  340. } else if (status & AT91RL_ADC_IER_NOPEN) {
  341. reg = at91_adc_readl(st, AT91_ADC_MR);
  342. reg |= AT91_ADC_PENDBC_(st->ts_pendbc) & AT91_ADC_PENDBC;
  343. at91_adc_writel(st, AT91_ADC_MR, reg);
  344. at91_adc_writel(st, st->registers->trigger_register,
  345. AT91_ADC_TRGR_NONE);
  346. at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_NOPEN
  347. | AT91_ADC_EOC(3));
  348. at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_PEN);
  349. st->ts_bufferedmeasure = false;
  350. input_report_key(st->ts_input, BTN_TOUCH, 0);
  351. input_sync(st->ts_input);
  352. } else if (status & AT91_ADC_EOC(3) && st->ts_input) {
  353. /* Conversion finished and we've a touchscreen */
  354. if (st->ts_bufferedmeasure) {
  355. /*
  356. * Last measurement is always discarded, since it can
  357. * be erroneous.
  358. * Always report previous measurement
  359. */
  360. input_report_abs(st->ts_input, ABS_X, st->ts_prev_absx);
  361. input_report_abs(st->ts_input, ABS_Y, st->ts_prev_absy);
  362. input_report_key(st->ts_input, BTN_TOUCH, 1);
  363. input_sync(st->ts_input);
  364. } else
  365. st->ts_bufferedmeasure = true;
  366. /* Now make new measurement */
  367. st->ts_prev_absx = at91_adc_readl(st, AT91_ADC_CHAN(st, 3))
  368. << MAX_RLPOS_BITS;
  369. st->ts_prev_absx /= at91_adc_readl(st, AT91_ADC_CHAN(st, 2));
  370. st->ts_prev_absy = at91_adc_readl(st, AT91_ADC_CHAN(st, 1))
  371. << MAX_RLPOS_BITS;
  372. st->ts_prev_absy /= at91_adc_readl(st, AT91_ADC_CHAN(st, 0));
  373. }
  374. return IRQ_HANDLED;
  375. }
  376. static irqreturn_t at91_adc_9x5_interrupt(int irq, void *private)
  377. {
  378. struct iio_dev *idev = private;
  379. struct at91_adc_state *st = iio_priv(idev);
  380. u32 status = at91_adc_readl(st, st->registers->status_register);
  381. const uint32_t ts_data_irq_mask =
  382. AT91_ADC_IER_XRDY |
  383. AT91_ADC_IER_YRDY |
  384. AT91_ADC_IER_PRDY;
  385. if (status & GENMASK(st->num_channels - 1, 0))
  386. handle_adc_eoc_trigger(irq, idev);
  387. if (status & AT91_ADC_IER_PEN) {
  388. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
  389. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_NOPEN |
  390. ts_data_irq_mask);
  391. /* Set up period trigger for sampling */
  392. at91_adc_writel(st, st->registers->trigger_register,
  393. AT91_ADC_TRGR_MOD_PERIOD_TRIG |
  394. AT91_ADC_TRGR_TRGPER_(st->ts_sample_period_val));
  395. } else if (status & AT91_ADC_IER_NOPEN) {
  396. at91_adc_writel(st, st->registers->trigger_register, 0);
  397. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_NOPEN |
  398. ts_data_irq_mask);
  399. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
  400. input_report_key(st->ts_input, BTN_TOUCH, 0);
  401. input_sync(st->ts_input);
  402. } else if ((status & ts_data_irq_mask) == ts_data_irq_mask) {
  403. /* Now all touchscreen data is ready */
  404. if (status & AT91_ADC_ISR_PENS) {
  405. /* validate data by pen contact */
  406. at91_ts_sample(idev);
  407. } else {
  408. /* triggered by event that is no pen contact, just read
  409. * them to clean the interrupt and discard all.
  410. */
  411. at91_adc_readl(st, AT91_ADC_TSXPOSR);
  412. at91_adc_readl(st, AT91_ADC_TSYPOSR);
  413. at91_adc_readl(st, AT91_ADC_TSPRESSR);
  414. }
  415. }
  416. return IRQ_HANDLED;
  417. }
  418. static int at91_adc_channel_init(struct iio_dev *idev)
  419. {
  420. struct at91_adc_state *st = iio_priv(idev);
  421. struct iio_chan_spec *chan_array, *timestamp;
  422. int bit, idx = 0;
  423. unsigned long rsvd_mask = 0;
  424. /* If touchscreen is enable, then reserve the adc channels */
  425. if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
  426. rsvd_mask = CHAN_MASK_TOUCHSCREEN_4WIRE;
  427. else if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_5WIRE)
  428. rsvd_mask = CHAN_MASK_TOUCHSCREEN_5WIRE;
  429. /* set up the channel mask to reserve touchscreen channels */
  430. st->channels_mask &= ~rsvd_mask;
  431. idev->num_channels = bitmap_weight(&st->channels_mask,
  432. st->num_channels) + 1;
  433. chan_array = devm_kzalloc(&idev->dev,
  434. ((idev->num_channels + 1) *
  435. sizeof(struct iio_chan_spec)),
  436. GFP_KERNEL);
  437. if (!chan_array)
  438. return -ENOMEM;
  439. for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
  440. struct iio_chan_spec *chan = chan_array + idx;
  441. chan->type = IIO_VOLTAGE;
  442. chan->indexed = 1;
  443. chan->channel = bit;
  444. chan->scan_index = idx;
  445. chan->scan_type.sign = 'u';
  446. chan->scan_type.realbits = st->res;
  447. chan->scan_type.storagebits = 16;
  448. chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
  449. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  450. idx++;
  451. }
  452. timestamp = chan_array + idx;
  453. timestamp->type = IIO_TIMESTAMP;
  454. timestamp->channel = -1;
  455. timestamp->scan_index = idx;
  456. timestamp->scan_type.sign = 's';
  457. timestamp->scan_type.realbits = 64;
  458. timestamp->scan_type.storagebits = 64;
  459. idev->channels = chan_array;
  460. return idev->num_channels;
  461. }
  462. static int at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
  463. const struct at91_adc_trigger *triggers,
  464. const char *trigger_name)
  465. {
  466. struct at91_adc_state *st = iio_priv(idev);
  467. int i;
  468. for (i = 0; i < st->caps->trigger_number; i++) {
  469. char *name __free(kfree) = kasprintf(GFP_KERNEL, "%s-dev%d-%s",
  470. idev->name,
  471. iio_device_id(idev),
  472. triggers[i].name);
  473. if (!name)
  474. return -ENOMEM;
  475. if (strcmp(trigger_name, name) == 0) {
  476. if (triggers[i].value == 0)
  477. return -EINVAL;
  478. return triggers[i].value;
  479. }
  480. }
  481. return -EINVAL;
  482. }
  483. static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
  484. {
  485. struct iio_dev *idev = iio_trigger_get_drvdata(trig);
  486. struct at91_adc_state *st = iio_priv(idev);
  487. const struct at91_adc_reg_desc *reg = st->registers;
  488. u32 status = at91_adc_readl(st, reg->trigger_register);
  489. int value;
  490. u8 bit;
  491. value = at91_adc_get_trigger_value_by_name(idev,
  492. st->caps->triggers,
  493. idev->trig->name);
  494. if (value < 0)
  495. return value;
  496. if (state) {
  497. st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
  498. if (st->buffer == NULL)
  499. return -ENOMEM;
  500. at91_adc_writel(st, reg->trigger_register,
  501. status | value);
  502. for_each_set_bit(bit, idev->active_scan_mask,
  503. st->num_channels) {
  504. struct iio_chan_spec const *chan = idev->channels + bit;
  505. at91_adc_writel(st, AT91_ADC_CHER,
  506. AT91_ADC_CH(chan->channel));
  507. }
  508. at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
  509. } else {
  510. at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
  511. at91_adc_writel(st, reg->trigger_register,
  512. status & ~value);
  513. for_each_set_bit(bit, idev->active_scan_mask,
  514. st->num_channels) {
  515. struct iio_chan_spec const *chan = idev->channels + bit;
  516. at91_adc_writel(st, AT91_ADC_CHDR,
  517. AT91_ADC_CH(chan->channel));
  518. }
  519. kfree(st->buffer);
  520. }
  521. return 0;
  522. }
  523. static const struct iio_trigger_ops at91_adc_trigger_ops = {
  524. .set_trigger_state = &at91_adc_configure_trigger,
  525. };
  526. static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
  527. const struct at91_adc_trigger *trigger)
  528. {
  529. struct iio_trigger *trig;
  530. int ret;
  531. trig = iio_trigger_alloc(idev->dev.parent, "%s-dev%d-%s", idev->name,
  532. iio_device_id(idev), trigger->name);
  533. if (trig == NULL)
  534. return NULL;
  535. iio_trigger_set_drvdata(trig, idev);
  536. trig->ops = &at91_adc_trigger_ops;
  537. ret = iio_trigger_register(trig);
  538. if (ret) {
  539. iio_trigger_free(trig);
  540. return NULL;
  541. }
  542. return trig;
  543. }
  544. static int at91_adc_trigger_init(struct iio_dev *idev)
  545. {
  546. struct at91_adc_state *st = iio_priv(idev);
  547. int i, ret;
  548. st->trig = devm_kcalloc(&idev->dev,
  549. st->caps->trigger_number, sizeof(*st->trig),
  550. GFP_KERNEL);
  551. if (st->trig == NULL) {
  552. ret = -ENOMEM;
  553. goto error_ret;
  554. }
  555. for (i = 0; i < st->caps->trigger_number; i++) {
  556. if (st->caps->triggers[i].is_external && !(st->use_external))
  557. continue;
  558. st->trig[i] = at91_adc_allocate_trigger(idev,
  559. st->caps->triggers + i);
  560. if (st->trig[i] == NULL) {
  561. dev_err(&idev->dev,
  562. "Could not allocate trigger %d\n", i);
  563. ret = -ENOMEM;
  564. goto error_trigger;
  565. }
  566. }
  567. return 0;
  568. error_trigger:
  569. for (i--; i >= 0; i--) {
  570. iio_trigger_unregister(st->trig[i]);
  571. iio_trigger_free(st->trig[i]);
  572. }
  573. error_ret:
  574. return ret;
  575. }
  576. static void at91_adc_trigger_remove(struct iio_dev *idev)
  577. {
  578. struct at91_adc_state *st = iio_priv(idev);
  579. int i;
  580. for (i = 0; i < st->caps->trigger_number; i++) {
  581. iio_trigger_unregister(st->trig[i]);
  582. iio_trigger_free(st->trig[i]);
  583. }
  584. }
  585. static int at91_adc_buffer_init(struct iio_dev *idev)
  586. {
  587. return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
  588. &at91_adc_trigger_handler, NULL);
  589. }
  590. static void at91_adc_buffer_remove(struct iio_dev *idev)
  591. {
  592. iio_triggered_buffer_cleanup(idev);
  593. }
  594. static int at91_adc_read_raw(struct iio_dev *idev,
  595. struct iio_chan_spec const *chan,
  596. int *val, int *val2, long mask)
  597. {
  598. struct at91_adc_state *st = iio_priv(idev);
  599. int ret;
  600. switch (mask) {
  601. case IIO_CHAN_INFO_RAW:
  602. mutex_lock(&st->lock);
  603. st->chnb = chan->channel;
  604. at91_adc_writel(st, AT91_ADC_CHER,
  605. AT91_ADC_CH(chan->channel));
  606. at91_adc_writel(st, AT91_ADC_IER, BIT(chan->channel));
  607. at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
  608. ret = wait_event_interruptible_timeout(st->wq_data_avail,
  609. st->done,
  610. msecs_to_jiffies(1000));
  611. /* Disable interrupts, regardless if adc conversion was
  612. * successful or not
  613. */
  614. at91_adc_writel(st, AT91_ADC_CHDR,
  615. AT91_ADC_CH(chan->channel));
  616. at91_adc_writel(st, AT91_ADC_IDR, BIT(chan->channel));
  617. if (ret > 0) {
  618. /* a valid conversion took place */
  619. *val = st->last_value;
  620. st->last_value = 0;
  621. st->done = false;
  622. ret = IIO_VAL_INT;
  623. } else if (ret == 0) {
  624. /* conversion timeout */
  625. dev_err(&idev->dev, "ADC Channel %d timeout.\n",
  626. chan->channel);
  627. ret = -ETIMEDOUT;
  628. }
  629. mutex_unlock(&st->lock);
  630. return ret;
  631. case IIO_CHAN_INFO_SCALE:
  632. *val = st->vref_mv;
  633. *val2 = chan->scan_type.realbits;
  634. return IIO_VAL_FRACTIONAL_LOG2;
  635. default:
  636. break;
  637. }
  638. return -EINVAL;
  639. }
  640. static u32 calc_startup_ticks_9260(u32 startup_time, u32 adc_clk_khz)
  641. {
  642. /*
  643. * Number of ticks needed to cover the startup time of the ADC
  644. * as defined in the electrical characteristics of the board,
  645. * divided by 8. The formula thus is :
  646. * Startup Time = (ticks + 1) * 8 / ADC Clock
  647. */
  648. return round_up((startup_time * adc_clk_khz / 1000) - 1, 8) / 8;
  649. }
  650. static u32 calc_startup_ticks_9x5(u32 startup_time, u32 adc_clk_khz)
  651. {
  652. /*
  653. * For sama5d3x and at91sam9x5, the formula changes to:
  654. * Startup Time = <lookup_table_value> / ADC Clock
  655. */
  656. static const int startup_lookup[] = {
  657. 0, 8, 16, 24,
  658. 64, 80, 96, 112,
  659. 512, 576, 640, 704,
  660. 768, 832, 896, 960
  661. };
  662. int i, size = ARRAY_SIZE(startup_lookup);
  663. unsigned int ticks;
  664. ticks = startup_time * adc_clk_khz / 1000;
  665. for (i = 0; i < size; i++)
  666. if (ticks < startup_lookup[i])
  667. break;
  668. ticks = i;
  669. if (ticks == size)
  670. /* Reach the end of lookup table */
  671. ticks = size - 1;
  672. return ticks;
  673. }
  674. static int at91_adc_probe_dt_ts(struct device_node *node,
  675. struct at91_adc_state *st, struct device *dev)
  676. {
  677. int ret;
  678. u32 prop;
  679. ret = of_property_read_u32(node, "atmel,adc-ts-wires", &prop);
  680. if (ret) {
  681. dev_info(dev, "ADC Touch screen is disabled.\n");
  682. return 0;
  683. }
  684. switch (prop) {
  685. case 4:
  686. case 5:
  687. st->touchscreen_type = prop;
  688. break;
  689. default:
  690. dev_err(dev, "Unsupported number of touchscreen wires (%d). Should be 4 or 5.\n", prop);
  691. return -EINVAL;
  692. }
  693. if (!st->caps->has_tsmr)
  694. return 0;
  695. prop = 0;
  696. of_property_read_u32(node, "atmel,adc-ts-pressure-threshold", &prop);
  697. st->ts_pressure_threshold = prop;
  698. if (st->ts_pressure_threshold) {
  699. return 0;
  700. } else {
  701. dev_err(dev, "Invalid pressure threshold for the touchscreen\n");
  702. return -EINVAL;
  703. }
  704. }
  705. static const struct iio_info at91_adc_info = {
  706. .read_raw = &at91_adc_read_raw,
  707. };
  708. /* Touchscreen related functions */
  709. static int atmel_ts_open(struct input_dev *dev)
  710. {
  711. struct at91_adc_state *st = input_get_drvdata(dev);
  712. if (st->caps->has_tsmr)
  713. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
  714. else
  715. at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_PEN);
  716. return 0;
  717. }
  718. static void atmel_ts_close(struct input_dev *dev)
  719. {
  720. struct at91_adc_state *st = input_get_drvdata(dev);
  721. if (st->caps->has_tsmr)
  722. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
  723. else
  724. at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_PEN);
  725. }
  726. static int at91_ts_hw_init(struct iio_dev *idev, u32 adc_clk_khz)
  727. {
  728. struct at91_adc_state *st = iio_priv(idev);
  729. u32 reg = 0;
  730. u32 tssctim = 0;
  731. int i = 0;
  732. /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid
  733. * pen detect noise.
  734. * The formula is : Pen Detect Debounce Time = (2 ^ pendbc) / ADCClock
  735. */
  736. st->ts_pendbc = round_up(TOUCH_PEN_DETECT_DEBOUNCE_US * adc_clk_khz /
  737. 1000, 1);
  738. while (st->ts_pendbc >> ++i)
  739. ; /* Empty! Find the shift offset */
  740. if (abs(st->ts_pendbc - (1 << i)) < abs(st->ts_pendbc - (1 << (i - 1))))
  741. st->ts_pendbc = i;
  742. else
  743. st->ts_pendbc = i - 1;
  744. if (!st->caps->has_tsmr) {
  745. reg = at91_adc_readl(st, AT91_ADC_MR);
  746. reg |= AT91_ADC_TSAMOD_TS_ONLY_MODE | AT91_ADC_PENDET;
  747. reg |= AT91_ADC_PENDBC_(st->ts_pendbc) & AT91_ADC_PENDBC;
  748. at91_adc_writel(st, AT91_ADC_MR, reg);
  749. reg = AT91_ADC_TSR_SHTIM_(TOUCH_SHTIM) & AT91_ADC_TSR_SHTIM;
  750. at91_adc_writel(st, AT91_ADC_TSR, reg);
  751. st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US_RL *
  752. adc_clk_khz / 1000) - 1, 1);
  753. return 0;
  754. }
  755. /* Touchscreen Switches Closure time needed for allowing the value to
  756. * stabilize.
  757. * Switch Closure Time = (TSSCTIM * 4) ADCClock periods
  758. */
  759. tssctim = DIV_ROUND_UP(TOUCH_SCTIM_US * adc_clk_khz / 1000, 4);
  760. dev_dbg(&idev->dev, "adc_clk at: %d KHz, tssctim at: %d\n",
  761. adc_clk_khz, tssctim);
  762. if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
  763. reg = AT91_ADC_TSMR_TSMODE_4WIRE_PRESS;
  764. else
  765. reg = AT91_ADC_TSMR_TSMODE_5WIRE;
  766. reg |= AT91_ADC_TSMR_SCTIM_(tssctim) & AT91_ADC_TSMR_SCTIM;
  767. reg |= AT91_ADC_TSMR_TSAV_(st->caps->ts_filter_average)
  768. & AT91_ADC_TSMR_TSAV;
  769. reg |= AT91_ADC_TSMR_PENDBC_(st->ts_pendbc) & AT91_ADC_TSMR_PENDBC;
  770. reg |= AT91_ADC_TSMR_NOTSDMA;
  771. reg |= AT91_ADC_TSMR_PENDET_ENA;
  772. reg |= 0x03 << 8; /* TSFREQ, needs to be bigger than TSAV */
  773. at91_adc_writel(st, AT91_ADC_TSMR, reg);
  774. /* Change adc internal resistor value for better pen detection,
  775. * default value is 100 kOhm.
  776. * 0 = 200 kOhm, 1 = 150 kOhm, 2 = 100 kOhm, 3 = 50 kOhm
  777. * option only available on ES2 and higher
  778. */
  779. at91_adc_writel(st, AT91_ADC_ACR, st->caps->ts_pen_detect_sensitivity
  780. & AT91_ADC_ACR_PENDETSENS);
  781. /* Sample Period Time = (TRGPER + 1) / ADCClock */
  782. st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US *
  783. adc_clk_khz / 1000) - 1, 1);
  784. return 0;
  785. }
  786. static int at91_ts_register(struct iio_dev *idev,
  787. struct platform_device *pdev)
  788. {
  789. struct at91_adc_state *st = iio_priv(idev);
  790. struct input_dev *input;
  791. int ret;
  792. input = input_allocate_device();
  793. if (!input) {
  794. dev_err(&idev->dev, "Failed to allocate TS device!\n");
  795. return -ENOMEM;
  796. }
  797. input->name = DRIVER_NAME;
  798. input->id.bustype = BUS_HOST;
  799. input->dev.parent = &pdev->dev;
  800. input->open = atmel_ts_open;
  801. input->close = atmel_ts_close;
  802. __set_bit(EV_ABS, input->evbit);
  803. __set_bit(EV_KEY, input->evbit);
  804. __set_bit(BTN_TOUCH, input->keybit);
  805. if (st->caps->has_tsmr) {
  806. input_set_abs_params(input, ABS_X, 0, (1 << MAX_POS_BITS) - 1,
  807. 0, 0);
  808. input_set_abs_params(input, ABS_Y, 0, (1 << MAX_POS_BITS) - 1,
  809. 0, 0);
  810. input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
  811. } else {
  812. if (st->touchscreen_type != ATMEL_ADC_TOUCHSCREEN_4WIRE) {
  813. dev_err(&pdev->dev,
  814. "This touchscreen controller only support 4 wires\n");
  815. ret = -EINVAL;
  816. goto err;
  817. }
  818. input_set_abs_params(input, ABS_X, 0, (1 << MAX_RLPOS_BITS) - 1,
  819. 0, 0);
  820. input_set_abs_params(input, ABS_Y, 0, (1 << MAX_RLPOS_BITS) - 1,
  821. 0, 0);
  822. }
  823. st->ts_input = input;
  824. input_set_drvdata(input, st);
  825. ret = input_register_device(input);
  826. if (ret)
  827. goto err;
  828. return ret;
  829. err:
  830. input_free_device(input);
  831. return ret;
  832. }
  833. static void at91_ts_unregister(struct at91_adc_state *st)
  834. {
  835. input_unregister_device(st->ts_input);
  836. }
  837. static int at91_adc_probe(struct platform_device *pdev)
  838. {
  839. unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim;
  840. struct device_node *node = pdev->dev.of_node;
  841. int ret;
  842. struct iio_dev *idev;
  843. struct at91_adc_state *st;
  844. u32 reg, prop;
  845. char *s;
  846. idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
  847. if (!idev)
  848. return -ENOMEM;
  849. st = iio_priv(idev);
  850. st->caps = of_device_get_match_data(&pdev->dev);
  851. st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
  852. if (of_property_read_u32(node, "atmel,adc-channels-used", &prop))
  853. return dev_err_probe(&idev->dev, -EINVAL,
  854. "Missing adc-channels-used property in the DT.\n");
  855. st->channels_mask = prop;
  856. st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
  857. if (of_property_read_u32(node, "atmel,adc-startup-time", &prop))
  858. return dev_err_probe(&idev->dev, -EINVAL,
  859. "Missing adc-startup-time property in the DT.\n");
  860. st->startup_time = prop;
  861. prop = 0;
  862. of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
  863. st->sample_hold_time = prop;
  864. if (of_property_read_u32(node, "atmel,adc-vref", &prop))
  865. return dev_err_probe(&idev->dev, -EINVAL,
  866. "Missing adc-vref property in the DT.\n");
  867. st->vref_mv = prop;
  868. st->res = st->caps->high_res_bits;
  869. if (st->caps->low_res_bits &&
  870. !of_property_read_string(node, "atmel,adc-use-res", (const char **)&s)
  871. && !strcmp(s, "lowres"))
  872. st->res = st->caps->low_res_bits;
  873. dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
  874. st->registers = &st->caps->registers;
  875. st->num_channels = st->caps->num_channels;
  876. /* Check if touchscreen is supported. */
  877. if (st->caps->has_ts) {
  878. ret = at91_adc_probe_dt_ts(node, st, &idev->dev);
  879. if (ret)
  880. return ret;
  881. }
  882. platform_set_drvdata(pdev, idev);
  883. idev->name = dev_name(&pdev->dev);
  884. idev->modes = INDIO_DIRECT_MODE;
  885. idev->info = &at91_adc_info;
  886. st->irq = platform_get_irq(pdev, 0);
  887. if (st->irq < 0)
  888. return -ENODEV;
  889. st->reg_base = devm_platform_ioremap_resource(pdev, 0);
  890. if (IS_ERR(st->reg_base))
  891. return PTR_ERR(st->reg_base);
  892. /*
  893. * Disable all IRQs before setting up the handler
  894. */
  895. at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
  896. at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
  897. if (st->caps->has_tsmr)
  898. ret = devm_request_irq(&pdev->dev, st->irq,
  899. at91_adc_9x5_interrupt, 0,
  900. pdev->dev.driver->name, idev);
  901. else
  902. ret = devm_request_irq(&pdev->dev, st->irq,
  903. at91_adc_rl_interrupt, 0,
  904. pdev->dev.driver->name, idev);
  905. if (ret)
  906. return dev_err_probe(&pdev->dev, ret,
  907. "Failed to allocate IRQ.\n");
  908. st->clk = devm_clk_get_enabled(&pdev->dev, "adc_clk");
  909. if (IS_ERR(st->clk))
  910. return dev_err_probe(&pdev->dev, PTR_ERR(st->clk),
  911. "Could not prepare or enable the clock.\n");
  912. st->adc_clk = devm_clk_get_enabled(&pdev->dev, "adc_op_clk");
  913. if (IS_ERR(st->adc_clk))
  914. return dev_err_probe(&pdev->dev, PTR_ERR(st->adc_clk),
  915. "Could not prepare or enable the ADC clock.\n");
  916. /*
  917. * Prescaler rate computation using the formula from the Atmel's
  918. * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
  919. * specified by the electrical characteristics of the board.
  920. */
  921. mstrclk = clk_get_rate(st->clk);
  922. adc_clk = clk_get_rate(st->adc_clk);
  923. adc_clk_khz = adc_clk / 1000;
  924. dev_dbg(&pdev->dev, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
  925. mstrclk, adc_clk);
  926. prsc = (mstrclk / (2 * adc_clk)) - 1;
  927. if (!st->startup_time)
  928. return dev_err_probe(&pdev->dev, -EINVAL,
  929. "No startup time available.\n");
  930. ticks = (*st->caps->calc_startup_ticks)(st->startup_time, adc_clk_khz);
  931. /*
  932. * a minimal Sample and Hold Time is necessary for the ADC to guarantee
  933. * the best converted final value between two channels selection
  934. * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
  935. */
  936. if (st->sample_hold_time > 0)
  937. shtim = round_up((st->sample_hold_time * adc_clk_khz / 1000)
  938. - 1, 1);
  939. else
  940. shtim = 0;
  941. reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask;
  942. reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
  943. if (st->res == st->caps->low_res_bits)
  944. reg |= AT91_ADC_LOWRES;
  945. if (st->sleep_mode)
  946. reg |= AT91_ADC_SLEEP;
  947. reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
  948. at91_adc_writel(st, AT91_ADC_MR, reg);
  949. /* Setup the ADC channels available on the board */
  950. ret = at91_adc_channel_init(idev);
  951. if (ret < 0)
  952. return dev_err_probe(&pdev->dev, ret,
  953. "Couldn't initialize the channels.\n");
  954. init_waitqueue_head(&st->wq_data_avail);
  955. mutex_init(&st->lock);
  956. /*
  957. * Since touch screen will set trigger register as period trigger. So
  958. * when touch screen is enabled, then we have to disable hardware
  959. * trigger for classic adc.
  960. */
  961. if (!st->touchscreen_type) {
  962. ret = at91_adc_buffer_init(idev);
  963. if (ret < 0)
  964. return dev_err_probe(&pdev->dev, ret,
  965. "Couldn't initialize the buffer.\n");
  966. ret = at91_adc_trigger_init(idev);
  967. if (ret < 0) {
  968. dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
  969. at91_adc_buffer_remove(idev);
  970. return ret;
  971. }
  972. } else {
  973. ret = at91_ts_register(idev, pdev);
  974. if (ret)
  975. return ret;
  976. at91_ts_hw_init(idev, adc_clk_khz);
  977. }
  978. ret = iio_device_register(idev);
  979. if (ret < 0) {
  980. dev_err(&pdev->dev, "Couldn't register the device.\n");
  981. goto error_iio_device_register;
  982. }
  983. return 0;
  984. error_iio_device_register:
  985. if (!st->touchscreen_type) {
  986. at91_adc_trigger_remove(idev);
  987. at91_adc_buffer_remove(idev);
  988. } else {
  989. at91_ts_unregister(st);
  990. }
  991. return ret;
  992. }
  993. static void at91_adc_remove(struct platform_device *pdev)
  994. {
  995. struct iio_dev *idev = platform_get_drvdata(pdev);
  996. struct at91_adc_state *st = iio_priv(idev);
  997. iio_device_unregister(idev);
  998. if (!st->touchscreen_type) {
  999. at91_adc_trigger_remove(idev);
  1000. at91_adc_buffer_remove(idev);
  1001. } else {
  1002. at91_ts_unregister(st);
  1003. }
  1004. }
  1005. static int at91_adc_suspend(struct device *dev)
  1006. {
  1007. struct iio_dev *idev = dev_get_drvdata(dev);
  1008. struct at91_adc_state *st = iio_priv(idev);
  1009. pinctrl_pm_select_sleep_state(dev);
  1010. clk_disable_unprepare(st->clk);
  1011. return 0;
  1012. }
  1013. static int at91_adc_resume(struct device *dev)
  1014. {
  1015. struct iio_dev *idev = dev_get_drvdata(dev);
  1016. struct at91_adc_state *st = iio_priv(idev);
  1017. clk_prepare_enable(st->clk);
  1018. pinctrl_pm_select_default_state(dev);
  1019. return 0;
  1020. }
  1021. static DEFINE_SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend,
  1022. at91_adc_resume);
  1023. static const struct at91_adc_trigger at91sam9260_triggers[] = {
  1024. { .name = "timer-counter-0", .value = 0x1 },
  1025. { .name = "timer-counter-1", .value = 0x3 },
  1026. { .name = "timer-counter-2", .value = 0x5 },
  1027. { .name = "external", .value = 0xd, .is_external = true },
  1028. };
  1029. static struct at91_adc_caps at91sam9260_caps = {
  1030. .calc_startup_ticks = calc_startup_ticks_9260,
  1031. .num_channels = 4,
  1032. .low_res_bits = 8,
  1033. .high_res_bits = 10,
  1034. .registers = {
  1035. .channel_base = AT91_ADC_CHR(0),
  1036. .drdy_mask = AT91_ADC_DRDY,
  1037. .status_register = AT91_ADC_SR,
  1038. .trigger_register = AT91_ADC_TRGR_9260,
  1039. .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
  1040. .mr_startup_mask = AT91_ADC_STARTUP_9260,
  1041. },
  1042. .triggers = at91sam9260_triggers,
  1043. .trigger_number = ARRAY_SIZE(at91sam9260_triggers),
  1044. };
  1045. static const struct at91_adc_trigger at91sam9x5_triggers[] = {
  1046. { .name = "external-rising", .value = 0x1, .is_external = true },
  1047. { .name = "external-falling", .value = 0x2, .is_external = true },
  1048. { .name = "external-any", .value = 0x3, .is_external = true },
  1049. { .name = "continuous", .value = 0x6 },
  1050. };
  1051. static struct at91_adc_caps at91sam9rl_caps = {
  1052. .has_ts = true,
  1053. .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
  1054. .num_channels = 6,
  1055. .low_res_bits = 8,
  1056. .high_res_bits = 10,
  1057. .registers = {
  1058. .channel_base = AT91_ADC_CHR(0),
  1059. .drdy_mask = AT91_ADC_DRDY,
  1060. .status_register = AT91_ADC_SR,
  1061. .trigger_register = AT91_ADC_TRGR_9G45,
  1062. .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
  1063. .mr_startup_mask = AT91_ADC_STARTUP_9G45,
  1064. },
  1065. .triggers = at91sam9x5_triggers,
  1066. .trigger_number = ARRAY_SIZE(at91sam9x5_triggers),
  1067. };
  1068. static struct at91_adc_caps at91sam9g45_caps = {
  1069. .has_ts = true,
  1070. .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
  1071. .num_channels = 8,
  1072. .low_res_bits = 8,
  1073. .high_res_bits = 10,
  1074. .registers = {
  1075. .channel_base = AT91_ADC_CHR(0),
  1076. .drdy_mask = AT91_ADC_DRDY,
  1077. .status_register = AT91_ADC_SR,
  1078. .trigger_register = AT91_ADC_TRGR_9G45,
  1079. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  1080. .mr_startup_mask = AT91_ADC_STARTUP_9G45,
  1081. },
  1082. .triggers = at91sam9x5_triggers,
  1083. .trigger_number = ARRAY_SIZE(at91sam9x5_triggers),
  1084. };
  1085. static struct at91_adc_caps at91sam9x5_caps = {
  1086. .has_ts = true,
  1087. .has_tsmr = true,
  1088. .ts_filter_average = 3,
  1089. .ts_pen_detect_sensitivity = 2,
  1090. .calc_startup_ticks = calc_startup_ticks_9x5,
  1091. .num_channels = 12,
  1092. .low_res_bits = 8,
  1093. .high_res_bits = 10,
  1094. .registers = {
  1095. .channel_base = AT91_ADC_CDR0_9X5,
  1096. .drdy_mask = AT91_ADC_SR_DRDY_9X5,
  1097. .status_register = AT91_ADC_SR_9X5,
  1098. .trigger_register = AT91_ADC_TRGR_9X5,
  1099. /* prescal mask is same as 9G45 */
  1100. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  1101. .mr_startup_mask = AT91_ADC_STARTUP_9X5,
  1102. },
  1103. .triggers = at91sam9x5_triggers,
  1104. .trigger_number = ARRAY_SIZE(at91sam9x5_triggers),
  1105. };
  1106. static struct at91_adc_caps sama5d3_caps = {
  1107. .has_ts = true,
  1108. .has_tsmr = true,
  1109. .ts_filter_average = 3,
  1110. .ts_pen_detect_sensitivity = 2,
  1111. .calc_startup_ticks = calc_startup_ticks_9x5,
  1112. .num_channels = 12,
  1113. .low_res_bits = 0,
  1114. .high_res_bits = 12,
  1115. .registers = {
  1116. .channel_base = AT91_ADC_CDR0_9X5,
  1117. .drdy_mask = AT91_ADC_SR_DRDY_9X5,
  1118. .status_register = AT91_ADC_SR_9X5,
  1119. .trigger_register = AT91_ADC_TRGR_9X5,
  1120. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  1121. .mr_startup_mask = AT91_ADC_STARTUP_9X5,
  1122. },
  1123. .triggers = at91sam9x5_triggers,
  1124. .trigger_number = ARRAY_SIZE(at91sam9x5_triggers),
  1125. };
  1126. static const struct of_device_id at91_adc_dt_ids[] = {
  1127. { .compatible = "atmel,at91sam9260-adc", .data = &at91sam9260_caps },
  1128. { .compatible = "atmel,at91sam9rl-adc", .data = &at91sam9rl_caps },
  1129. { .compatible = "atmel,at91sam9g45-adc", .data = &at91sam9g45_caps },
  1130. { .compatible = "atmel,at91sam9x5-adc", .data = &at91sam9x5_caps },
  1131. { .compatible = "atmel,sama5d3-adc", .data = &sama5d3_caps },
  1132. { }
  1133. };
  1134. MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
  1135. static struct platform_driver at91_adc_driver = {
  1136. .probe = at91_adc_probe,
  1137. .remove_new = at91_adc_remove,
  1138. .driver = {
  1139. .name = DRIVER_NAME,
  1140. .of_match_table = at91_adc_dt_ids,
  1141. .pm = pm_sleep_ptr(&at91_adc_pm_ops),
  1142. },
  1143. };
  1144. module_platform_driver(at91_adc_driver);
  1145. MODULE_LICENSE("GPL");
  1146. MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
  1147. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");