at91_adc.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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/clk.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/input.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.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. for (i = 0; i < idev->masklength; i++) {
  239. if (!test_bit(i, idev->active_scan_mask))
  240. continue;
  241. chan = idev->channels + i;
  242. st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, chan->channel));
  243. j++;
  244. }
  245. iio_push_to_buffers_with_timestamp(idev, st->buffer, pf->timestamp);
  246. iio_trigger_notify_done(idev->trig);
  247. /* Needed to ACK the DRDY interruption */
  248. at91_adc_readl(st, AT91_ADC_LCDR);
  249. enable_irq(st->irq);
  250. return IRQ_HANDLED;
  251. }
  252. /* Handler for classic adc channel eoc trigger */
  253. static void handle_adc_eoc_trigger(int irq, struct iio_dev *idev)
  254. {
  255. struct at91_adc_state *st = iio_priv(idev);
  256. if (iio_buffer_enabled(idev)) {
  257. disable_irq_nosync(irq);
  258. iio_trigger_poll(idev->trig);
  259. } else {
  260. st->last_value = at91_adc_readl(st, AT91_ADC_CHAN(st, st->chnb));
  261. /* Needed to ACK the DRDY interruption */
  262. at91_adc_readl(st, AT91_ADC_LCDR);
  263. st->done = true;
  264. wake_up_interruptible(&st->wq_data_avail);
  265. }
  266. }
  267. static int at91_ts_sample(struct iio_dev *idev)
  268. {
  269. struct at91_adc_state *st = iio_priv(idev);
  270. unsigned int xscale, yscale, reg, z1, z2;
  271. unsigned int x, y, pres, xpos, ypos;
  272. unsigned int rxp = 1;
  273. unsigned int factor = 1000;
  274. unsigned int xyz_mask_bits = st->res;
  275. unsigned int xyz_mask = (1 << xyz_mask_bits) - 1;
  276. /* calculate position */
  277. /* x position = (x / xscale) * max, max = 2^MAX_POS_BITS - 1 */
  278. reg = at91_adc_readl(st, AT91_ADC_TSXPOSR);
  279. xpos = reg & xyz_mask;
  280. x = (xpos << MAX_POS_BITS) - xpos;
  281. xscale = (reg >> 16) & xyz_mask;
  282. if (xscale == 0) {
  283. dev_err(&idev->dev, "Error: xscale == 0!\n");
  284. return -1;
  285. }
  286. x /= xscale;
  287. /* y position = (y / yscale) * max, max = 2^MAX_POS_BITS - 1 */
  288. reg = at91_adc_readl(st, AT91_ADC_TSYPOSR);
  289. ypos = reg & xyz_mask;
  290. y = (ypos << MAX_POS_BITS) - ypos;
  291. yscale = (reg >> 16) & xyz_mask;
  292. if (yscale == 0) {
  293. dev_err(&idev->dev, "Error: yscale == 0!\n");
  294. return -1;
  295. }
  296. y /= yscale;
  297. /* calculate the pressure */
  298. reg = at91_adc_readl(st, AT91_ADC_TSPRESSR);
  299. z1 = reg & xyz_mask;
  300. z2 = (reg >> 16) & xyz_mask;
  301. if (z1 != 0)
  302. pres = rxp * (x * factor / 1024) * (z2 * factor / z1 - factor)
  303. / factor;
  304. else
  305. pres = st->ts_pressure_threshold; /* no pen contacted */
  306. dev_dbg(&idev->dev, "xpos = %d, xscale = %d, ypos = %d, yscale = %d, z1 = %d, z2 = %d, press = %d\n",
  307. xpos, xscale, ypos, yscale, z1, z2, pres);
  308. if (pres < st->ts_pressure_threshold) {
  309. dev_dbg(&idev->dev, "x = %d, y = %d, pressure = %d\n",
  310. x, y, pres / factor);
  311. input_report_abs(st->ts_input, ABS_X, x);
  312. input_report_abs(st->ts_input, ABS_Y, y);
  313. input_report_abs(st->ts_input, ABS_PRESSURE, pres);
  314. input_report_key(st->ts_input, BTN_TOUCH, 1);
  315. input_sync(st->ts_input);
  316. } else {
  317. dev_dbg(&idev->dev, "pressure too low: not reporting\n");
  318. }
  319. return 0;
  320. }
  321. static irqreturn_t at91_adc_rl_interrupt(int irq, void *private)
  322. {
  323. struct iio_dev *idev = private;
  324. struct at91_adc_state *st = iio_priv(idev);
  325. u32 status = at91_adc_readl(st, st->registers->status_register);
  326. unsigned int reg;
  327. status &= at91_adc_readl(st, AT91_ADC_IMR);
  328. if (status & GENMASK(st->num_channels - 1, 0))
  329. handle_adc_eoc_trigger(irq, idev);
  330. if (status & AT91RL_ADC_IER_PEN) {
  331. /* Disabling pen debounce is required to get a NOPEN irq */
  332. reg = at91_adc_readl(st, AT91_ADC_MR);
  333. reg &= ~AT91_ADC_PENDBC;
  334. at91_adc_writel(st, AT91_ADC_MR, reg);
  335. at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_PEN);
  336. at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_NOPEN
  337. | AT91_ADC_EOC(3));
  338. /* Set up period trigger for sampling */
  339. at91_adc_writel(st, st->registers->trigger_register,
  340. AT91_ADC_TRGR_MOD_PERIOD_TRIG |
  341. AT91_ADC_TRGR_TRGPER_(st->ts_sample_period_val));
  342. } else if (status & AT91RL_ADC_IER_NOPEN) {
  343. reg = at91_adc_readl(st, AT91_ADC_MR);
  344. reg |= AT91_ADC_PENDBC_(st->ts_pendbc) & AT91_ADC_PENDBC;
  345. at91_adc_writel(st, AT91_ADC_MR, reg);
  346. at91_adc_writel(st, st->registers->trigger_register,
  347. AT91_ADC_TRGR_NONE);
  348. at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_NOPEN
  349. | AT91_ADC_EOC(3));
  350. at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_PEN);
  351. st->ts_bufferedmeasure = false;
  352. input_report_key(st->ts_input, BTN_TOUCH, 0);
  353. input_sync(st->ts_input);
  354. } else if (status & AT91_ADC_EOC(3) && st->ts_input) {
  355. /* Conversion finished and we've a touchscreen */
  356. if (st->ts_bufferedmeasure) {
  357. /*
  358. * Last measurement is always discarded, since it can
  359. * be erroneous.
  360. * Always report previous measurement
  361. */
  362. input_report_abs(st->ts_input, ABS_X, st->ts_prev_absx);
  363. input_report_abs(st->ts_input, ABS_Y, st->ts_prev_absy);
  364. input_report_key(st->ts_input, BTN_TOUCH, 1);
  365. input_sync(st->ts_input);
  366. } else
  367. st->ts_bufferedmeasure = true;
  368. /* Now make new measurement */
  369. st->ts_prev_absx = at91_adc_readl(st, AT91_ADC_CHAN(st, 3))
  370. << MAX_RLPOS_BITS;
  371. st->ts_prev_absx /= at91_adc_readl(st, AT91_ADC_CHAN(st, 2));
  372. st->ts_prev_absy = at91_adc_readl(st, AT91_ADC_CHAN(st, 1))
  373. << MAX_RLPOS_BITS;
  374. st->ts_prev_absy /= at91_adc_readl(st, AT91_ADC_CHAN(st, 0));
  375. }
  376. return IRQ_HANDLED;
  377. }
  378. static irqreturn_t at91_adc_9x5_interrupt(int irq, void *private)
  379. {
  380. struct iio_dev *idev = private;
  381. struct at91_adc_state *st = iio_priv(idev);
  382. u32 status = at91_adc_readl(st, st->registers->status_register);
  383. const uint32_t ts_data_irq_mask =
  384. AT91_ADC_IER_XRDY |
  385. AT91_ADC_IER_YRDY |
  386. AT91_ADC_IER_PRDY;
  387. if (status & GENMASK(st->num_channels - 1, 0))
  388. handle_adc_eoc_trigger(irq, idev);
  389. if (status & AT91_ADC_IER_PEN) {
  390. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
  391. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_NOPEN |
  392. ts_data_irq_mask);
  393. /* Set up period trigger for sampling */
  394. at91_adc_writel(st, st->registers->trigger_register,
  395. AT91_ADC_TRGR_MOD_PERIOD_TRIG |
  396. AT91_ADC_TRGR_TRGPER_(st->ts_sample_period_val));
  397. } else if (status & AT91_ADC_IER_NOPEN) {
  398. at91_adc_writel(st, st->registers->trigger_register, 0);
  399. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_NOPEN |
  400. ts_data_irq_mask);
  401. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
  402. input_report_key(st->ts_input, BTN_TOUCH, 0);
  403. input_sync(st->ts_input);
  404. } else if ((status & ts_data_irq_mask) == ts_data_irq_mask) {
  405. /* Now all touchscreen data is ready */
  406. if (status & AT91_ADC_ISR_PENS) {
  407. /* validate data by pen contact */
  408. at91_ts_sample(idev);
  409. } else {
  410. /* triggered by event that is no pen contact, just read
  411. * them to clean the interrupt and discard all.
  412. */
  413. at91_adc_readl(st, AT91_ADC_TSXPOSR);
  414. at91_adc_readl(st, AT91_ADC_TSYPOSR);
  415. at91_adc_readl(st, AT91_ADC_TSPRESSR);
  416. }
  417. }
  418. return IRQ_HANDLED;
  419. }
  420. static int at91_adc_channel_init(struct iio_dev *idev)
  421. {
  422. struct at91_adc_state *st = iio_priv(idev);
  423. struct iio_chan_spec *chan_array, *timestamp;
  424. int bit, idx = 0;
  425. unsigned long rsvd_mask = 0;
  426. /* If touchscreen is enable, then reserve the adc channels */
  427. if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
  428. rsvd_mask = CHAN_MASK_TOUCHSCREEN_4WIRE;
  429. else if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_5WIRE)
  430. rsvd_mask = CHAN_MASK_TOUCHSCREEN_5WIRE;
  431. /* set up the channel mask to reserve touchscreen channels */
  432. st->channels_mask &= ~rsvd_mask;
  433. idev->num_channels = bitmap_weight(&st->channels_mask,
  434. st->num_channels) + 1;
  435. chan_array = devm_kzalloc(&idev->dev,
  436. ((idev->num_channels + 1) *
  437. sizeof(struct iio_chan_spec)),
  438. GFP_KERNEL);
  439. if (!chan_array)
  440. return -ENOMEM;
  441. for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
  442. struct iio_chan_spec *chan = chan_array + idx;
  443. chan->type = IIO_VOLTAGE;
  444. chan->indexed = 1;
  445. chan->channel = bit;
  446. chan->scan_index = idx;
  447. chan->scan_type.sign = 'u';
  448. chan->scan_type.realbits = st->res;
  449. chan->scan_type.storagebits = 16;
  450. chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
  451. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  452. idx++;
  453. }
  454. timestamp = chan_array + idx;
  455. timestamp->type = IIO_TIMESTAMP;
  456. timestamp->channel = -1;
  457. timestamp->scan_index = idx;
  458. timestamp->scan_type.sign = 's';
  459. timestamp->scan_type.realbits = 64;
  460. timestamp->scan_type.storagebits = 64;
  461. idev->channels = chan_array;
  462. return idev->num_channels;
  463. }
  464. static int at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
  465. const struct at91_adc_trigger *triggers,
  466. const char *trigger_name)
  467. {
  468. struct at91_adc_state *st = iio_priv(idev);
  469. int i;
  470. for (i = 0; i < st->caps->trigger_number; i++) {
  471. char *name = kasprintf(GFP_KERNEL,
  472. "%s-dev%d-%s",
  473. idev->name,
  474. iio_device_id(idev),
  475. triggers[i].name);
  476. if (!name)
  477. return -ENOMEM;
  478. if (strcmp(trigger_name, name) == 0) {
  479. kfree(name);
  480. if (triggers[i].value == 0)
  481. return -EINVAL;
  482. return triggers[i].value;
  483. }
  484. kfree(name);
  485. }
  486. return -EINVAL;
  487. }
  488. static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
  489. {
  490. struct iio_dev *idev = iio_trigger_get_drvdata(trig);
  491. struct at91_adc_state *st = iio_priv(idev);
  492. const struct at91_adc_reg_desc *reg = st->registers;
  493. u32 status = at91_adc_readl(st, reg->trigger_register);
  494. int value;
  495. u8 bit;
  496. value = at91_adc_get_trigger_value_by_name(idev,
  497. st->caps->triggers,
  498. idev->trig->name);
  499. if (value < 0)
  500. return value;
  501. if (state) {
  502. st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
  503. if (st->buffer == NULL)
  504. return -ENOMEM;
  505. at91_adc_writel(st, reg->trigger_register,
  506. status | value);
  507. for_each_set_bit(bit, idev->active_scan_mask,
  508. st->num_channels) {
  509. struct iio_chan_spec const *chan = idev->channels + bit;
  510. at91_adc_writel(st, AT91_ADC_CHER,
  511. AT91_ADC_CH(chan->channel));
  512. }
  513. at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
  514. } else {
  515. at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
  516. at91_adc_writel(st, reg->trigger_register,
  517. status & ~value);
  518. for_each_set_bit(bit, idev->active_scan_mask,
  519. st->num_channels) {
  520. struct iio_chan_spec const *chan = idev->channels + bit;
  521. at91_adc_writel(st, AT91_ADC_CHDR,
  522. AT91_ADC_CH(chan->channel));
  523. }
  524. kfree(st->buffer);
  525. }
  526. return 0;
  527. }
  528. static const struct iio_trigger_ops at91_adc_trigger_ops = {
  529. .set_trigger_state = &at91_adc_configure_trigger,
  530. };
  531. static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
  532. const struct at91_adc_trigger *trigger)
  533. {
  534. struct iio_trigger *trig;
  535. int ret;
  536. trig = iio_trigger_alloc(idev->dev.parent, "%s-dev%d-%s", idev->name,
  537. iio_device_id(idev), trigger->name);
  538. if (trig == NULL)
  539. return NULL;
  540. iio_trigger_set_drvdata(trig, idev);
  541. trig->ops = &at91_adc_trigger_ops;
  542. ret = iio_trigger_register(trig);
  543. if (ret) {
  544. iio_trigger_free(trig);
  545. return NULL;
  546. }
  547. return trig;
  548. }
  549. static int at91_adc_trigger_init(struct iio_dev *idev)
  550. {
  551. struct at91_adc_state *st = iio_priv(idev);
  552. int i, ret;
  553. st->trig = devm_kcalloc(&idev->dev,
  554. st->caps->trigger_number, sizeof(*st->trig),
  555. GFP_KERNEL);
  556. if (st->trig == NULL) {
  557. ret = -ENOMEM;
  558. goto error_ret;
  559. }
  560. for (i = 0; i < st->caps->trigger_number; i++) {
  561. if (st->caps->triggers[i].is_external && !(st->use_external))
  562. continue;
  563. st->trig[i] = at91_adc_allocate_trigger(idev,
  564. st->caps->triggers + i);
  565. if (st->trig[i] == NULL) {
  566. dev_err(&idev->dev,
  567. "Could not allocate trigger %d\n", i);
  568. ret = -ENOMEM;
  569. goto error_trigger;
  570. }
  571. }
  572. return 0;
  573. error_trigger:
  574. for (i--; i >= 0; i--) {
  575. iio_trigger_unregister(st->trig[i]);
  576. iio_trigger_free(st->trig[i]);
  577. }
  578. error_ret:
  579. return ret;
  580. }
  581. static void at91_adc_trigger_remove(struct iio_dev *idev)
  582. {
  583. struct at91_adc_state *st = iio_priv(idev);
  584. int i;
  585. for (i = 0; i < st->caps->trigger_number; i++) {
  586. iio_trigger_unregister(st->trig[i]);
  587. iio_trigger_free(st->trig[i]);
  588. }
  589. }
  590. static int at91_adc_buffer_init(struct iio_dev *idev)
  591. {
  592. return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
  593. &at91_adc_trigger_handler, NULL);
  594. }
  595. static void at91_adc_buffer_remove(struct iio_dev *idev)
  596. {
  597. iio_triggered_buffer_cleanup(idev);
  598. }
  599. static int at91_adc_read_raw(struct iio_dev *idev,
  600. struct iio_chan_spec const *chan,
  601. int *val, int *val2, long mask)
  602. {
  603. struct at91_adc_state *st = iio_priv(idev);
  604. int ret;
  605. switch (mask) {
  606. case IIO_CHAN_INFO_RAW:
  607. mutex_lock(&st->lock);
  608. st->chnb = chan->channel;
  609. at91_adc_writel(st, AT91_ADC_CHER,
  610. AT91_ADC_CH(chan->channel));
  611. at91_adc_writel(st, AT91_ADC_IER, BIT(chan->channel));
  612. at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
  613. ret = wait_event_interruptible_timeout(st->wq_data_avail,
  614. st->done,
  615. msecs_to_jiffies(1000));
  616. /* Disable interrupts, regardless if adc conversion was
  617. * successful or not
  618. */
  619. at91_adc_writel(st, AT91_ADC_CHDR,
  620. AT91_ADC_CH(chan->channel));
  621. at91_adc_writel(st, AT91_ADC_IDR, BIT(chan->channel));
  622. if (ret > 0) {
  623. /* a valid conversion took place */
  624. *val = st->last_value;
  625. st->last_value = 0;
  626. st->done = false;
  627. ret = IIO_VAL_INT;
  628. } else if (ret == 0) {
  629. /* conversion timeout */
  630. dev_err(&idev->dev, "ADC Channel %d timeout.\n",
  631. chan->channel);
  632. ret = -ETIMEDOUT;
  633. }
  634. mutex_unlock(&st->lock);
  635. return ret;
  636. case IIO_CHAN_INFO_SCALE:
  637. *val = st->vref_mv;
  638. *val2 = chan->scan_type.realbits;
  639. return IIO_VAL_FRACTIONAL_LOG2;
  640. default:
  641. break;
  642. }
  643. return -EINVAL;
  644. }
  645. static u32 calc_startup_ticks_9260(u32 startup_time, u32 adc_clk_khz)
  646. {
  647. /*
  648. * Number of ticks needed to cover the startup time of the ADC
  649. * as defined in the electrical characteristics of the board,
  650. * divided by 8. The formula thus is :
  651. * Startup Time = (ticks + 1) * 8 / ADC Clock
  652. */
  653. return round_up((startup_time * adc_clk_khz / 1000) - 1, 8) / 8;
  654. }
  655. static u32 calc_startup_ticks_9x5(u32 startup_time, u32 adc_clk_khz)
  656. {
  657. /*
  658. * For sama5d3x and at91sam9x5, the formula changes to:
  659. * Startup Time = <lookup_table_value> / ADC Clock
  660. */
  661. static const int startup_lookup[] = {
  662. 0, 8, 16, 24,
  663. 64, 80, 96, 112,
  664. 512, 576, 640, 704,
  665. 768, 832, 896, 960
  666. };
  667. int i, size = ARRAY_SIZE(startup_lookup);
  668. unsigned int ticks;
  669. ticks = startup_time * adc_clk_khz / 1000;
  670. for (i = 0; i < size; i++)
  671. if (ticks < startup_lookup[i])
  672. break;
  673. ticks = i;
  674. if (ticks == size)
  675. /* Reach the end of lookup table */
  676. ticks = size - 1;
  677. return ticks;
  678. }
  679. static int at91_adc_probe_dt_ts(struct device_node *node,
  680. struct at91_adc_state *st, struct device *dev)
  681. {
  682. int ret;
  683. u32 prop;
  684. ret = of_property_read_u32(node, "atmel,adc-ts-wires", &prop);
  685. if (ret) {
  686. dev_info(dev, "ADC Touch screen is disabled.\n");
  687. return 0;
  688. }
  689. switch (prop) {
  690. case 4:
  691. case 5:
  692. st->touchscreen_type = prop;
  693. break;
  694. default:
  695. dev_err(dev, "Unsupported number of touchscreen wires (%d). Should be 4 or 5.\n", prop);
  696. return -EINVAL;
  697. }
  698. if (!st->caps->has_tsmr)
  699. return 0;
  700. prop = 0;
  701. of_property_read_u32(node, "atmel,adc-ts-pressure-threshold", &prop);
  702. st->ts_pressure_threshold = prop;
  703. if (st->ts_pressure_threshold) {
  704. return 0;
  705. } else {
  706. dev_err(dev, "Invalid pressure threshold for the touchscreen\n");
  707. return -EINVAL;
  708. }
  709. }
  710. static const struct iio_info at91_adc_info = {
  711. .read_raw = &at91_adc_read_raw,
  712. };
  713. /* Touchscreen related functions */
  714. static int atmel_ts_open(struct input_dev *dev)
  715. {
  716. struct at91_adc_state *st = input_get_drvdata(dev);
  717. if (st->caps->has_tsmr)
  718. at91_adc_writel(st, AT91_ADC_IER, AT91_ADC_IER_PEN);
  719. else
  720. at91_adc_writel(st, AT91_ADC_IER, AT91RL_ADC_IER_PEN);
  721. return 0;
  722. }
  723. static void atmel_ts_close(struct input_dev *dev)
  724. {
  725. struct at91_adc_state *st = input_get_drvdata(dev);
  726. if (st->caps->has_tsmr)
  727. at91_adc_writel(st, AT91_ADC_IDR, AT91_ADC_IER_PEN);
  728. else
  729. at91_adc_writel(st, AT91_ADC_IDR, AT91RL_ADC_IER_PEN);
  730. }
  731. static int at91_ts_hw_init(struct iio_dev *idev, u32 adc_clk_khz)
  732. {
  733. struct at91_adc_state *st = iio_priv(idev);
  734. u32 reg = 0;
  735. u32 tssctim = 0;
  736. int i = 0;
  737. /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid
  738. * pen detect noise.
  739. * The formula is : Pen Detect Debounce Time = (2 ^ pendbc) / ADCClock
  740. */
  741. st->ts_pendbc = round_up(TOUCH_PEN_DETECT_DEBOUNCE_US * adc_clk_khz /
  742. 1000, 1);
  743. while (st->ts_pendbc >> ++i)
  744. ; /* Empty! Find the shift offset */
  745. if (abs(st->ts_pendbc - (1 << i)) < abs(st->ts_pendbc - (1 << (i - 1))))
  746. st->ts_pendbc = i;
  747. else
  748. st->ts_pendbc = i - 1;
  749. if (!st->caps->has_tsmr) {
  750. reg = at91_adc_readl(st, AT91_ADC_MR);
  751. reg |= AT91_ADC_TSAMOD_TS_ONLY_MODE | AT91_ADC_PENDET;
  752. reg |= AT91_ADC_PENDBC_(st->ts_pendbc) & AT91_ADC_PENDBC;
  753. at91_adc_writel(st, AT91_ADC_MR, reg);
  754. reg = AT91_ADC_TSR_SHTIM_(TOUCH_SHTIM) & AT91_ADC_TSR_SHTIM;
  755. at91_adc_writel(st, AT91_ADC_TSR, reg);
  756. st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US_RL *
  757. adc_clk_khz / 1000) - 1, 1);
  758. return 0;
  759. }
  760. /* Touchscreen Switches Closure time needed for allowing the value to
  761. * stabilize.
  762. * Switch Closure Time = (TSSCTIM * 4) ADCClock periods
  763. */
  764. tssctim = DIV_ROUND_UP(TOUCH_SCTIM_US * adc_clk_khz / 1000, 4);
  765. dev_dbg(&idev->dev, "adc_clk at: %d KHz, tssctim at: %d\n",
  766. adc_clk_khz, tssctim);
  767. if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
  768. reg = AT91_ADC_TSMR_TSMODE_4WIRE_PRESS;
  769. else
  770. reg = AT91_ADC_TSMR_TSMODE_5WIRE;
  771. reg |= AT91_ADC_TSMR_SCTIM_(tssctim) & AT91_ADC_TSMR_SCTIM;
  772. reg |= AT91_ADC_TSMR_TSAV_(st->caps->ts_filter_average)
  773. & AT91_ADC_TSMR_TSAV;
  774. reg |= AT91_ADC_TSMR_PENDBC_(st->ts_pendbc) & AT91_ADC_TSMR_PENDBC;
  775. reg |= AT91_ADC_TSMR_NOTSDMA;
  776. reg |= AT91_ADC_TSMR_PENDET_ENA;
  777. reg |= 0x03 << 8; /* TSFREQ, needs to be bigger than TSAV */
  778. at91_adc_writel(st, AT91_ADC_TSMR, reg);
  779. /* Change adc internal resistor value for better pen detection,
  780. * default value is 100 kOhm.
  781. * 0 = 200 kOhm, 1 = 150 kOhm, 2 = 100 kOhm, 3 = 50 kOhm
  782. * option only available on ES2 and higher
  783. */
  784. at91_adc_writel(st, AT91_ADC_ACR, st->caps->ts_pen_detect_sensitivity
  785. & AT91_ADC_ACR_PENDETSENS);
  786. /* Sample Period Time = (TRGPER + 1) / ADCClock */
  787. st->ts_sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US *
  788. adc_clk_khz / 1000) - 1, 1);
  789. return 0;
  790. }
  791. static int at91_ts_register(struct iio_dev *idev,
  792. struct platform_device *pdev)
  793. {
  794. struct at91_adc_state *st = iio_priv(idev);
  795. struct input_dev *input;
  796. int ret;
  797. input = input_allocate_device();
  798. if (!input) {
  799. dev_err(&idev->dev, "Failed to allocate TS device!\n");
  800. return -ENOMEM;
  801. }
  802. input->name = DRIVER_NAME;
  803. input->id.bustype = BUS_HOST;
  804. input->dev.parent = &pdev->dev;
  805. input->open = atmel_ts_open;
  806. input->close = atmel_ts_close;
  807. __set_bit(EV_ABS, input->evbit);
  808. __set_bit(EV_KEY, input->evbit);
  809. __set_bit(BTN_TOUCH, input->keybit);
  810. if (st->caps->has_tsmr) {
  811. input_set_abs_params(input, ABS_X, 0, (1 << MAX_POS_BITS) - 1,
  812. 0, 0);
  813. input_set_abs_params(input, ABS_Y, 0, (1 << MAX_POS_BITS) - 1,
  814. 0, 0);
  815. input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
  816. } else {
  817. if (st->touchscreen_type != ATMEL_ADC_TOUCHSCREEN_4WIRE) {
  818. dev_err(&pdev->dev,
  819. "This touchscreen controller only support 4 wires\n");
  820. ret = -EINVAL;
  821. goto err;
  822. }
  823. input_set_abs_params(input, ABS_X, 0, (1 << MAX_RLPOS_BITS) - 1,
  824. 0, 0);
  825. input_set_abs_params(input, ABS_Y, 0, (1 << MAX_RLPOS_BITS) - 1,
  826. 0, 0);
  827. }
  828. st->ts_input = input;
  829. input_set_drvdata(input, st);
  830. ret = input_register_device(input);
  831. if (ret)
  832. goto err;
  833. return ret;
  834. err:
  835. input_free_device(st->ts_input);
  836. return ret;
  837. }
  838. static void at91_ts_unregister(struct at91_adc_state *st)
  839. {
  840. input_unregister_device(st->ts_input);
  841. }
  842. static int at91_adc_probe(struct platform_device *pdev)
  843. {
  844. unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim;
  845. struct device_node *node = pdev->dev.of_node;
  846. int ret;
  847. struct iio_dev *idev;
  848. struct at91_adc_state *st;
  849. u32 reg, prop;
  850. char *s;
  851. idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
  852. if (!idev)
  853. return -ENOMEM;
  854. st = iio_priv(idev);
  855. st->caps = of_device_get_match_data(&pdev->dev);
  856. st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
  857. if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
  858. dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
  859. return -EINVAL;
  860. }
  861. st->channels_mask = prop;
  862. st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
  863. if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
  864. dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
  865. return -EINVAL;
  866. }
  867. st->startup_time = prop;
  868. prop = 0;
  869. of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
  870. st->sample_hold_time = prop;
  871. if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
  872. dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
  873. return -EINVAL;
  874. }
  875. st->vref_mv = prop;
  876. st->res = st->caps->high_res_bits;
  877. if (st->caps->low_res_bits &&
  878. !of_property_read_string(node, "atmel,adc-use-res", (const char **)&s)
  879. && !strcmp(s, "lowres"))
  880. st->res = st->caps->low_res_bits;
  881. dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
  882. st->registers = &st->caps->registers;
  883. st->num_channels = st->caps->num_channels;
  884. /* Check if touchscreen is supported. */
  885. if (st->caps->has_ts) {
  886. ret = at91_adc_probe_dt_ts(node, st, &idev->dev);
  887. if (ret)
  888. return ret;
  889. }
  890. platform_set_drvdata(pdev, idev);
  891. idev->name = dev_name(&pdev->dev);
  892. idev->modes = INDIO_DIRECT_MODE;
  893. idev->info = &at91_adc_info;
  894. st->irq = platform_get_irq(pdev, 0);
  895. if (st->irq < 0)
  896. return -ENODEV;
  897. st->reg_base = devm_platform_ioremap_resource(pdev, 0);
  898. if (IS_ERR(st->reg_base))
  899. return PTR_ERR(st->reg_base);
  900. /*
  901. * Disable all IRQs before setting up the handler
  902. */
  903. at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
  904. at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
  905. if (st->caps->has_tsmr)
  906. ret = request_irq(st->irq, at91_adc_9x5_interrupt, 0,
  907. pdev->dev.driver->name, idev);
  908. else
  909. ret = request_irq(st->irq, at91_adc_rl_interrupt, 0,
  910. pdev->dev.driver->name, idev);
  911. if (ret) {
  912. dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
  913. return ret;
  914. }
  915. st->clk = devm_clk_get(&pdev->dev, "adc_clk");
  916. if (IS_ERR(st->clk)) {
  917. dev_err(&pdev->dev, "Failed to get the clock.\n");
  918. ret = PTR_ERR(st->clk);
  919. goto error_free_irq;
  920. }
  921. ret = clk_prepare_enable(st->clk);
  922. if (ret) {
  923. dev_err(&pdev->dev,
  924. "Could not prepare or enable the clock.\n");
  925. goto error_free_irq;
  926. }
  927. st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
  928. if (IS_ERR(st->adc_clk)) {
  929. dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
  930. ret = PTR_ERR(st->adc_clk);
  931. goto error_disable_clk;
  932. }
  933. ret = clk_prepare_enable(st->adc_clk);
  934. if (ret) {
  935. dev_err(&pdev->dev,
  936. "Could not prepare or enable the ADC clock.\n");
  937. goto error_disable_clk;
  938. }
  939. /*
  940. * Prescaler rate computation using the formula from the Atmel's
  941. * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
  942. * specified by the electrical characteristics of the board.
  943. */
  944. mstrclk = clk_get_rate(st->clk);
  945. adc_clk = clk_get_rate(st->adc_clk);
  946. adc_clk_khz = adc_clk / 1000;
  947. dev_dbg(&pdev->dev, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
  948. mstrclk, adc_clk);
  949. prsc = (mstrclk / (2 * adc_clk)) - 1;
  950. if (!st->startup_time) {
  951. dev_err(&pdev->dev, "No startup time available.\n");
  952. ret = -EINVAL;
  953. goto error_disable_adc_clk;
  954. }
  955. ticks = (*st->caps->calc_startup_ticks)(st->startup_time, adc_clk_khz);
  956. /*
  957. * a minimal Sample and Hold Time is necessary for the ADC to guarantee
  958. * the best converted final value between two channels selection
  959. * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
  960. */
  961. if (st->sample_hold_time > 0)
  962. shtim = round_up((st->sample_hold_time * adc_clk_khz / 1000)
  963. - 1, 1);
  964. else
  965. shtim = 0;
  966. reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask;
  967. reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
  968. if (st->res == st->caps->low_res_bits)
  969. reg |= AT91_ADC_LOWRES;
  970. if (st->sleep_mode)
  971. reg |= AT91_ADC_SLEEP;
  972. reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
  973. at91_adc_writel(st, AT91_ADC_MR, reg);
  974. /* Setup the ADC channels available on the board */
  975. ret = at91_adc_channel_init(idev);
  976. if (ret < 0) {
  977. dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
  978. goto error_disable_adc_clk;
  979. }
  980. init_waitqueue_head(&st->wq_data_avail);
  981. mutex_init(&st->lock);
  982. /*
  983. * Since touch screen will set trigger register as period trigger. So
  984. * when touch screen is enabled, then we have to disable hardware
  985. * trigger for classic adc.
  986. */
  987. if (!st->touchscreen_type) {
  988. ret = at91_adc_buffer_init(idev);
  989. if (ret < 0) {
  990. dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
  991. goto error_disable_adc_clk;
  992. }
  993. ret = at91_adc_trigger_init(idev);
  994. if (ret < 0) {
  995. dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
  996. at91_adc_buffer_remove(idev);
  997. goto error_disable_adc_clk;
  998. }
  999. } else {
  1000. ret = at91_ts_register(idev, pdev);
  1001. if (ret)
  1002. goto error_disable_adc_clk;
  1003. at91_ts_hw_init(idev, adc_clk_khz);
  1004. }
  1005. ret = iio_device_register(idev);
  1006. if (ret < 0) {
  1007. dev_err(&pdev->dev, "Couldn't register the device.\n");
  1008. goto error_iio_device_register;
  1009. }
  1010. return 0;
  1011. error_iio_device_register:
  1012. if (!st->touchscreen_type) {
  1013. at91_adc_trigger_remove(idev);
  1014. at91_adc_buffer_remove(idev);
  1015. } else {
  1016. at91_ts_unregister(st);
  1017. }
  1018. error_disable_adc_clk:
  1019. clk_disable_unprepare(st->adc_clk);
  1020. error_disable_clk:
  1021. clk_disable_unprepare(st->clk);
  1022. error_free_irq:
  1023. free_irq(st->irq, idev);
  1024. return ret;
  1025. }
  1026. static int at91_adc_remove(struct platform_device *pdev)
  1027. {
  1028. struct iio_dev *idev = platform_get_drvdata(pdev);
  1029. struct at91_adc_state *st = iio_priv(idev);
  1030. iio_device_unregister(idev);
  1031. if (!st->touchscreen_type) {
  1032. at91_adc_trigger_remove(idev);
  1033. at91_adc_buffer_remove(idev);
  1034. } else {
  1035. at91_ts_unregister(st);
  1036. }
  1037. clk_disable_unprepare(st->adc_clk);
  1038. clk_disable_unprepare(st->clk);
  1039. free_irq(st->irq, idev);
  1040. return 0;
  1041. }
  1042. static int at91_adc_suspend(struct device *dev)
  1043. {
  1044. struct iio_dev *idev = dev_get_drvdata(dev);
  1045. struct at91_adc_state *st = iio_priv(idev);
  1046. pinctrl_pm_select_sleep_state(dev);
  1047. clk_disable_unprepare(st->clk);
  1048. return 0;
  1049. }
  1050. static int at91_adc_resume(struct device *dev)
  1051. {
  1052. struct iio_dev *idev = dev_get_drvdata(dev);
  1053. struct at91_adc_state *st = iio_priv(idev);
  1054. clk_prepare_enable(st->clk);
  1055. pinctrl_pm_select_default_state(dev);
  1056. return 0;
  1057. }
  1058. static DEFINE_SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend,
  1059. at91_adc_resume);
  1060. static const struct at91_adc_trigger at91sam9260_triggers[] = {
  1061. { .name = "timer-counter-0", .value = 0x1 },
  1062. { .name = "timer-counter-1", .value = 0x3 },
  1063. { .name = "timer-counter-2", .value = 0x5 },
  1064. { .name = "external", .value = 0xd, .is_external = true },
  1065. };
  1066. static struct at91_adc_caps at91sam9260_caps = {
  1067. .calc_startup_ticks = calc_startup_ticks_9260,
  1068. .num_channels = 4,
  1069. .low_res_bits = 8,
  1070. .high_res_bits = 10,
  1071. .registers = {
  1072. .channel_base = AT91_ADC_CHR(0),
  1073. .drdy_mask = AT91_ADC_DRDY,
  1074. .status_register = AT91_ADC_SR,
  1075. .trigger_register = AT91_ADC_TRGR_9260,
  1076. .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
  1077. .mr_startup_mask = AT91_ADC_STARTUP_9260,
  1078. },
  1079. .triggers = at91sam9260_triggers,
  1080. .trigger_number = ARRAY_SIZE(at91sam9260_triggers),
  1081. };
  1082. static const struct at91_adc_trigger at91sam9x5_triggers[] = {
  1083. { .name = "external-rising", .value = 0x1, .is_external = true },
  1084. { .name = "external-falling", .value = 0x2, .is_external = true },
  1085. { .name = "external-any", .value = 0x3, .is_external = true },
  1086. { .name = "continuous", .value = 0x6 },
  1087. };
  1088. static struct at91_adc_caps at91sam9rl_caps = {
  1089. .has_ts = true,
  1090. .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
  1091. .num_channels = 6,
  1092. .low_res_bits = 8,
  1093. .high_res_bits = 10,
  1094. .registers = {
  1095. .channel_base = AT91_ADC_CHR(0),
  1096. .drdy_mask = AT91_ADC_DRDY,
  1097. .status_register = AT91_ADC_SR,
  1098. .trigger_register = AT91_ADC_TRGR_9G45,
  1099. .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
  1100. .mr_startup_mask = AT91_ADC_STARTUP_9G45,
  1101. },
  1102. .triggers = at91sam9x5_triggers,
  1103. .trigger_number = ARRAY_SIZE(at91sam9x5_triggers),
  1104. };
  1105. static struct at91_adc_caps at91sam9g45_caps = {
  1106. .has_ts = true,
  1107. .calc_startup_ticks = calc_startup_ticks_9260, /* same as 9260 */
  1108. .num_channels = 8,
  1109. .low_res_bits = 8,
  1110. .high_res_bits = 10,
  1111. .registers = {
  1112. .channel_base = AT91_ADC_CHR(0),
  1113. .drdy_mask = AT91_ADC_DRDY,
  1114. .status_register = AT91_ADC_SR,
  1115. .trigger_register = AT91_ADC_TRGR_9G45,
  1116. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  1117. .mr_startup_mask = AT91_ADC_STARTUP_9G45,
  1118. },
  1119. .triggers = at91sam9x5_triggers,
  1120. .trigger_number = ARRAY_SIZE(at91sam9x5_triggers),
  1121. };
  1122. static struct at91_adc_caps at91sam9x5_caps = {
  1123. .has_ts = true,
  1124. .has_tsmr = true,
  1125. .ts_filter_average = 3,
  1126. .ts_pen_detect_sensitivity = 2,
  1127. .calc_startup_ticks = calc_startup_ticks_9x5,
  1128. .num_channels = 12,
  1129. .low_res_bits = 8,
  1130. .high_res_bits = 10,
  1131. .registers = {
  1132. .channel_base = AT91_ADC_CDR0_9X5,
  1133. .drdy_mask = AT91_ADC_SR_DRDY_9X5,
  1134. .status_register = AT91_ADC_SR_9X5,
  1135. .trigger_register = AT91_ADC_TRGR_9X5,
  1136. /* prescal mask is same as 9G45 */
  1137. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  1138. .mr_startup_mask = AT91_ADC_STARTUP_9X5,
  1139. },
  1140. .triggers = at91sam9x5_triggers,
  1141. .trigger_number = ARRAY_SIZE(at91sam9x5_triggers),
  1142. };
  1143. static struct at91_adc_caps sama5d3_caps = {
  1144. .has_ts = true,
  1145. .has_tsmr = true,
  1146. .ts_filter_average = 3,
  1147. .ts_pen_detect_sensitivity = 2,
  1148. .calc_startup_ticks = calc_startup_ticks_9x5,
  1149. .num_channels = 12,
  1150. .low_res_bits = 0,
  1151. .high_res_bits = 12,
  1152. .registers = {
  1153. .channel_base = AT91_ADC_CDR0_9X5,
  1154. .drdy_mask = AT91_ADC_SR_DRDY_9X5,
  1155. .status_register = AT91_ADC_SR_9X5,
  1156. .trigger_register = AT91_ADC_TRGR_9X5,
  1157. .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
  1158. .mr_startup_mask = AT91_ADC_STARTUP_9X5,
  1159. },
  1160. .triggers = at91sam9x5_triggers,
  1161. .trigger_number = ARRAY_SIZE(at91sam9x5_triggers),
  1162. };
  1163. static const struct of_device_id at91_adc_dt_ids[] = {
  1164. { .compatible = "atmel,at91sam9260-adc", .data = &at91sam9260_caps },
  1165. { .compatible = "atmel,at91sam9rl-adc", .data = &at91sam9rl_caps },
  1166. { .compatible = "atmel,at91sam9g45-adc", .data = &at91sam9g45_caps },
  1167. { .compatible = "atmel,at91sam9x5-adc", .data = &at91sam9x5_caps },
  1168. { .compatible = "atmel,sama5d3-adc", .data = &sama5d3_caps },
  1169. {},
  1170. };
  1171. MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
  1172. static struct platform_driver at91_adc_driver = {
  1173. .probe = at91_adc_probe,
  1174. .remove = at91_adc_remove,
  1175. .driver = {
  1176. .name = DRIVER_NAME,
  1177. .of_match_table = at91_adc_dt_ids,
  1178. .pm = pm_sleep_ptr(&at91_adc_pm_ops),
  1179. },
  1180. };
  1181. module_platform_driver(at91_adc_driver);
  1182. MODULE_LICENSE("GPL");
  1183. MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
  1184. MODULE_AUTHOR("Maxime Ripard <[email protected]>");