at91-sama5d2_adc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * Atmel ADC driver for SAMA5D2 devices and compatible.
  3. *
  4. * Copyright (C) 2015 Atmel,
  5. * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/bitops.h>
  17. #include <linux/clk.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/sched.h>
  24. #include <linux/wait.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/sysfs.h>
  27. #include <linux/iio/buffer.h>
  28. #include <linux/iio/trigger.h>
  29. #include <linux/iio/trigger_consumer.h>
  30. #include <linux/iio/triggered_buffer.h>
  31. #include <linux/pinctrl/consumer.h>
  32. #include <linux/regulator/consumer.h>
  33. /* Control Register */
  34. #define AT91_SAMA5D2_CR 0x00
  35. /* Software Reset */
  36. #define AT91_SAMA5D2_CR_SWRST BIT(0)
  37. /* Start Conversion */
  38. #define AT91_SAMA5D2_CR_START BIT(1)
  39. /* Touchscreen Calibration */
  40. #define AT91_SAMA5D2_CR_TSCALIB BIT(2)
  41. /* Comparison Restart */
  42. #define AT91_SAMA5D2_CR_CMPRST BIT(4)
  43. /* Mode Register */
  44. #define AT91_SAMA5D2_MR 0x04
  45. /* Trigger Selection */
  46. #define AT91_SAMA5D2_MR_TRGSEL(v) ((v) << 1)
  47. /* ADTRG */
  48. #define AT91_SAMA5D2_MR_TRGSEL_TRIG0 0
  49. /* TIOA0 */
  50. #define AT91_SAMA5D2_MR_TRGSEL_TRIG1 1
  51. /* TIOA1 */
  52. #define AT91_SAMA5D2_MR_TRGSEL_TRIG2 2
  53. /* TIOA2 */
  54. #define AT91_SAMA5D2_MR_TRGSEL_TRIG3 3
  55. /* PWM event line 0 */
  56. #define AT91_SAMA5D2_MR_TRGSEL_TRIG4 4
  57. /* PWM event line 1 */
  58. #define AT91_SAMA5D2_MR_TRGSEL_TRIG5 5
  59. /* TIOA3 */
  60. #define AT91_SAMA5D2_MR_TRGSEL_TRIG6 6
  61. /* RTCOUT0 */
  62. #define AT91_SAMA5D2_MR_TRGSEL_TRIG7 7
  63. /* Sleep Mode */
  64. #define AT91_SAMA5D2_MR_SLEEP BIT(5)
  65. /* Fast Wake Up */
  66. #define AT91_SAMA5D2_MR_FWUP BIT(6)
  67. /* Prescaler Rate Selection */
  68. #define AT91_SAMA5D2_MR_PRESCAL(v) ((v) << AT91_SAMA5D2_MR_PRESCAL_OFFSET)
  69. #define AT91_SAMA5D2_MR_PRESCAL_OFFSET 8
  70. #define AT91_SAMA5D2_MR_PRESCAL_MAX 0xff
  71. #define AT91_SAMA5D2_MR_PRESCAL_MASK GENMASK(15, 8)
  72. /* Startup Time */
  73. #define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16)
  74. #define AT91_SAMA5D2_MR_STARTUP_MASK GENMASK(19, 16)
  75. /* Analog Change */
  76. #define AT91_SAMA5D2_MR_ANACH BIT(23)
  77. /* Tracking Time */
  78. #define AT91_SAMA5D2_MR_TRACKTIM(v) ((v) << 24)
  79. #define AT91_SAMA5D2_MR_TRACKTIM_MAX 0xff
  80. /* Transfer Time */
  81. #define AT91_SAMA5D2_MR_TRANSFER(v) ((v) << 28)
  82. #define AT91_SAMA5D2_MR_TRANSFER_MAX 0x3
  83. /* Use Sequence Enable */
  84. #define AT91_SAMA5D2_MR_USEQ BIT(31)
  85. /* Channel Sequence Register 1 */
  86. #define AT91_SAMA5D2_SEQR1 0x08
  87. /* Channel Sequence Register 2 */
  88. #define AT91_SAMA5D2_SEQR2 0x0c
  89. /* Channel Enable Register */
  90. #define AT91_SAMA5D2_CHER 0x10
  91. /* Channel Disable Register */
  92. #define AT91_SAMA5D2_CHDR 0x14
  93. /* Channel Status Register */
  94. #define AT91_SAMA5D2_CHSR 0x18
  95. /* Last Converted Data Register */
  96. #define AT91_SAMA5D2_LCDR 0x20
  97. /* Interrupt Enable Register */
  98. #define AT91_SAMA5D2_IER 0x24
  99. /* Interrupt Disable Register */
  100. #define AT91_SAMA5D2_IDR 0x28
  101. /* Interrupt Mask Register */
  102. #define AT91_SAMA5D2_IMR 0x2c
  103. /* Interrupt Status Register */
  104. #define AT91_SAMA5D2_ISR 0x30
  105. /* Last Channel Trigger Mode Register */
  106. #define AT91_SAMA5D2_LCTMR 0x34
  107. /* Last Channel Compare Window Register */
  108. #define AT91_SAMA5D2_LCCWR 0x38
  109. /* Overrun Status Register */
  110. #define AT91_SAMA5D2_OVER 0x3c
  111. /* Extended Mode Register */
  112. #define AT91_SAMA5D2_EMR 0x40
  113. /* Compare Window Register */
  114. #define AT91_SAMA5D2_CWR 0x44
  115. /* Channel Gain Register */
  116. #define AT91_SAMA5D2_CGR 0x48
  117. /* Channel Offset Register */
  118. #define AT91_SAMA5D2_COR 0x4c
  119. #define AT91_SAMA5D2_COR_DIFF_OFFSET 16
  120. /* Channel Data Register 0 */
  121. #define AT91_SAMA5D2_CDR0 0x50
  122. /* Analog Control Register */
  123. #define AT91_SAMA5D2_ACR 0x94
  124. /* Touchscreen Mode Register */
  125. #define AT91_SAMA5D2_TSMR 0xb0
  126. /* Touchscreen X Position Register */
  127. #define AT91_SAMA5D2_XPOSR 0xb4
  128. /* Touchscreen Y Position Register */
  129. #define AT91_SAMA5D2_YPOSR 0xb8
  130. /* Touchscreen Pressure Register */
  131. #define AT91_SAMA5D2_PRESSR 0xbc
  132. /* Trigger Register */
  133. #define AT91_SAMA5D2_TRGR 0xc0
  134. /* Mask for TRGMOD field of TRGR register */
  135. #define AT91_SAMA5D2_TRGR_TRGMOD_MASK GENMASK(2, 0)
  136. /* No trigger, only software trigger can start conversions */
  137. #define AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER 0
  138. /* Trigger Mode external trigger rising edge */
  139. #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE 1
  140. /* Trigger Mode external trigger falling edge */
  141. #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL 2
  142. /* Trigger Mode external trigger any edge */
  143. #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY 3
  144. /* Correction Select Register */
  145. #define AT91_SAMA5D2_COSR 0xd0
  146. /* Correction Value Register */
  147. #define AT91_SAMA5D2_CVR 0xd4
  148. /* Channel Error Correction Register */
  149. #define AT91_SAMA5D2_CECR 0xd8
  150. /* Write Protection Mode Register */
  151. #define AT91_SAMA5D2_WPMR 0xe4
  152. /* Write Protection Status Register */
  153. #define AT91_SAMA5D2_WPSR 0xe8
  154. /* Version Register */
  155. #define AT91_SAMA5D2_VERSION 0xfc
  156. #define AT91_SAMA5D2_HW_TRIG_CNT 3
  157. #define AT91_SAMA5D2_SINGLE_CHAN_CNT 12
  158. #define AT91_SAMA5D2_DIFF_CHAN_CNT 6
  159. /*
  160. * Maximum number of bytes to hold conversion from all channels
  161. * plus the timestamp
  162. */
  163. #define AT91_BUFFER_MAX_BYTES ((AT91_SAMA5D2_SINGLE_CHAN_CNT + \
  164. AT91_SAMA5D2_DIFF_CHAN_CNT) * 2 + 8)
  165. #define AT91_BUFFER_MAX_HWORDS (AT91_BUFFER_MAX_BYTES / 2)
  166. #define AT91_SAMA5D2_CHAN_SINGLE(num, addr) \
  167. { \
  168. .type = IIO_VOLTAGE, \
  169. .channel = num, \
  170. .address = addr, \
  171. .scan_index = num, \
  172. .scan_type = { \
  173. .sign = 'u', \
  174. .realbits = 12, \
  175. .storagebits = 16, \
  176. }, \
  177. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  178. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  179. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
  180. .datasheet_name = "CH"#num, \
  181. .indexed = 1, \
  182. }
  183. #define AT91_SAMA5D2_CHAN_DIFF(num, num2, addr) \
  184. { \
  185. .type = IIO_VOLTAGE, \
  186. .differential = 1, \
  187. .channel = num, \
  188. .channel2 = num2, \
  189. .address = addr, \
  190. .scan_index = num + AT91_SAMA5D2_SINGLE_CHAN_CNT, \
  191. .scan_type = { \
  192. .sign = 's', \
  193. .realbits = 12, \
  194. .storagebits = 16, \
  195. }, \
  196. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  197. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  198. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
  199. .datasheet_name = "CH"#num"-CH"#num2, \
  200. .indexed = 1, \
  201. }
  202. #define at91_adc_readl(st, reg) readl_relaxed(st->base + reg)
  203. #define at91_adc_writel(st, reg, val) writel_relaxed(val, st->base + reg)
  204. struct at91_adc_soc_info {
  205. unsigned startup_time;
  206. unsigned min_sample_rate;
  207. unsigned max_sample_rate;
  208. };
  209. struct at91_adc_trigger {
  210. char *name;
  211. unsigned int trgmod_value;
  212. unsigned int edge_type;
  213. };
  214. struct at91_adc_state {
  215. void __iomem *base;
  216. int irq;
  217. struct clk *per_clk;
  218. struct regulator *reg;
  219. struct regulator *vref;
  220. int vref_uv;
  221. struct iio_trigger *trig;
  222. const struct at91_adc_trigger *selected_trig;
  223. const struct iio_chan_spec *chan;
  224. bool conversion_done;
  225. u32 conversion_value;
  226. struct at91_adc_soc_info soc_info;
  227. wait_queue_head_t wq_data_available;
  228. u16 buffer[AT91_BUFFER_MAX_HWORDS];
  229. /*
  230. * lock to prevent concurrent 'single conversion' requests through
  231. * sysfs.
  232. */
  233. struct mutex lock;
  234. };
  235. static const struct at91_adc_trigger at91_adc_trigger_list[] = {
  236. {
  237. .name = "external_rising",
  238. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE,
  239. .edge_type = IRQ_TYPE_EDGE_RISING,
  240. },
  241. {
  242. .name = "external_falling",
  243. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL,
  244. .edge_type = IRQ_TYPE_EDGE_FALLING,
  245. },
  246. {
  247. .name = "external_any",
  248. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY,
  249. .edge_type = IRQ_TYPE_EDGE_BOTH,
  250. },
  251. };
  252. static const struct iio_chan_spec at91_adc_channels[] = {
  253. AT91_SAMA5D2_CHAN_SINGLE(0, 0x50),
  254. AT91_SAMA5D2_CHAN_SINGLE(1, 0x54),
  255. AT91_SAMA5D2_CHAN_SINGLE(2, 0x58),
  256. AT91_SAMA5D2_CHAN_SINGLE(3, 0x5c),
  257. AT91_SAMA5D2_CHAN_SINGLE(4, 0x60),
  258. AT91_SAMA5D2_CHAN_SINGLE(5, 0x64),
  259. AT91_SAMA5D2_CHAN_SINGLE(6, 0x68),
  260. AT91_SAMA5D2_CHAN_SINGLE(7, 0x6c),
  261. AT91_SAMA5D2_CHAN_SINGLE(8, 0x70),
  262. AT91_SAMA5D2_CHAN_SINGLE(9, 0x74),
  263. AT91_SAMA5D2_CHAN_SINGLE(10, 0x78),
  264. AT91_SAMA5D2_CHAN_SINGLE(11, 0x7c),
  265. AT91_SAMA5D2_CHAN_DIFF(0, 1, 0x50),
  266. AT91_SAMA5D2_CHAN_DIFF(2, 3, 0x58),
  267. AT91_SAMA5D2_CHAN_DIFF(4, 5, 0x60),
  268. AT91_SAMA5D2_CHAN_DIFF(6, 7, 0x68),
  269. AT91_SAMA5D2_CHAN_DIFF(8, 9, 0x70),
  270. AT91_SAMA5D2_CHAN_DIFF(10, 11, 0x78),
  271. IIO_CHAN_SOFT_TIMESTAMP(AT91_SAMA5D2_SINGLE_CHAN_CNT
  272. + AT91_SAMA5D2_DIFF_CHAN_CNT + 1),
  273. };
  274. static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
  275. {
  276. struct iio_dev *indio = iio_trigger_get_drvdata(trig);
  277. struct at91_adc_state *st = iio_priv(indio);
  278. u32 status = at91_adc_readl(st, AT91_SAMA5D2_TRGR);
  279. u8 bit;
  280. /* clear TRGMOD */
  281. status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK;
  282. if (state)
  283. status |= st->selected_trig->trgmod_value;
  284. /* set/unset hw trigger */
  285. at91_adc_writel(st, AT91_SAMA5D2_TRGR, status);
  286. for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) {
  287. struct iio_chan_spec const *chan = indio->channels + bit;
  288. if (state) {
  289. at91_adc_writel(st, AT91_SAMA5D2_CHER,
  290. BIT(chan->channel));
  291. at91_adc_writel(st, AT91_SAMA5D2_IER,
  292. BIT(chan->channel));
  293. } else {
  294. at91_adc_writel(st, AT91_SAMA5D2_IDR,
  295. BIT(chan->channel));
  296. at91_adc_writel(st, AT91_SAMA5D2_CHDR,
  297. BIT(chan->channel));
  298. }
  299. }
  300. return 0;
  301. }
  302. static int at91_adc_reenable_trigger(struct iio_trigger *trig)
  303. {
  304. struct iio_dev *indio = iio_trigger_get_drvdata(trig);
  305. struct at91_adc_state *st = iio_priv(indio);
  306. enable_irq(st->irq);
  307. /* Needed to ACK the DRDY interruption */
  308. at91_adc_readl(st, AT91_SAMA5D2_LCDR);
  309. return 0;
  310. }
  311. static const struct iio_trigger_ops at91_adc_trigger_ops = {
  312. .owner = THIS_MODULE,
  313. .set_trigger_state = &at91_adc_configure_trigger,
  314. .try_reenable = &at91_adc_reenable_trigger,
  315. };
  316. static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *indio,
  317. char *trigger_name)
  318. {
  319. struct iio_trigger *trig;
  320. int ret;
  321. trig = devm_iio_trigger_alloc(&indio->dev, "%s-dev%d-%s", indio->name,
  322. indio->id, trigger_name);
  323. if (!trig)
  324. return NULL;
  325. trig->dev.parent = indio->dev.parent;
  326. iio_trigger_set_drvdata(trig, indio);
  327. trig->ops = &at91_adc_trigger_ops;
  328. ret = devm_iio_trigger_register(&indio->dev, trig);
  329. if (ret)
  330. return ERR_PTR(ret);
  331. return trig;
  332. }
  333. static int at91_adc_trigger_init(struct iio_dev *indio)
  334. {
  335. struct at91_adc_state *st = iio_priv(indio);
  336. st->trig = at91_adc_allocate_trigger(indio, st->selected_trig->name);
  337. if (IS_ERR(st->trig)) {
  338. dev_err(&indio->dev,
  339. "could not allocate trigger\n");
  340. return PTR_ERR(st->trig);
  341. }
  342. return 0;
  343. }
  344. static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
  345. {
  346. struct iio_poll_func *pf = p;
  347. struct iio_dev *indio = pf->indio_dev;
  348. struct at91_adc_state *st = iio_priv(indio);
  349. int i = 0;
  350. u8 bit;
  351. for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) {
  352. struct iio_chan_spec const *chan = indio->channels + bit;
  353. st->buffer[i] = at91_adc_readl(st, chan->address);
  354. i++;
  355. }
  356. iio_push_to_buffers_with_timestamp(indio, st->buffer, pf->timestamp);
  357. iio_trigger_notify_done(indio->trig);
  358. return IRQ_HANDLED;
  359. }
  360. static int at91_adc_buffer_init(struct iio_dev *indio)
  361. {
  362. return devm_iio_triggered_buffer_setup(&indio->dev, indio,
  363. &iio_pollfunc_store_time,
  364. &at91_adc_trigger_handler, NULL);
  365. }
  366. static unsigned at91_adc_startup_time(unsigned startup_time_min,
  367. unsigned adc_clk_khz)
  368. {
  369. static const unsigned int startup_lookup[] = {
  370. 0, 8, 16, 24,
  371. 64, 80, 96, 112,
  372. 512, 576, 640, 704,
  373. 768, 832, 896, 960
  374. };
  375. unsigned ticks_min, i;
  376. /*
  377. * Since the adc frequency is checked before, there is no reason
  378. * to not meet the startup time constraint.
  379. */
  380. ticks_min = startup_time_min * adc_clk_khz / 1000;
  381. for (i = 0; i < ARRAY_SIZE(startup_lookup); i++)
  382. if (startup_lookup[i] > ticks_min)
  383. break;
  384. return i;
  385. }
  386. static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq)
  387. {
  388. struct iio_dev *indio_dev = iio_priv_to_dev(st);
  389. unsigned f_per, prescal, startup, mr;
  390. f_per = clk_get_rate(st->per_clk);
  391. prescal = (f_per / (2 * freq)) - 1;
  392. startup = at91_adc_startup_time(st->soc_info.startup_time,
  393. freq / 1000);
  394. mr = at91_adc_readl(st, AT91_SAMA5D2_MR);
  395. mr &= ~(AT91_SAMA5D2_MR_STARTUP_MASK | AT91_SAMA5D2_MR_PRESCAL_MASK);
  396. mr |= AT91_SAMA5D2_MR_STARTUP(startup);
  397. mr |= AT91_SAMA5D2_MR_PRESCAL(prescal);
  398. at91_adc_writel(st, AT91_SAMA5D2_MR, mr);
  399. dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n",
  400. freq, startup, prescal);
  401. }
  402. static unsigned at91_adc_get_sample_freq(struct at91_adc_state *st)
  403. {
  404. unsigned f_adc, f_per = clk_get_rate(st->per_clk);
  405. unsigned mr, prescal;
  406. mr = at91_adc_readl(st, AT91_SAMA5D2_MR);
  407. prescal = (mr >> AT91_SAMA5D2_MR_PRESCAL_OFFSET)
  408. & AT91_SAMA5D2_MR_PRESCAL_MAX;
  409. f_adc = f_per / (2 * (prescal + 1));
  410. return f_adc;
  411. }
  412. static irqreturn_t at91_adc_interrupt(int irq, void *private)
  413. {
  414. struct iio_dev *indio = private;
  415. struct at91_adc_state *st = iio_priv(indio);
  416. u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR);
  417. u32 imr = at91_adc_readl(st, AT91_SAMA5D2_IMR);
  418. if (!(status & imr))
  419. return IRQ_NONE;
  420. if (iio_buffer_enabled(indio)) {
  421. disable_irq_nosync(irq);
  422. iio_trigger_poll(indio->trig);
  423. } else {
  424. st->conversion_value = at91_adc_readl(st, st->chan->address);
  425. st->conversion_done = true;
  426. wake_up_interruptible(&st->wq_data_available);
  427. }
  428. return IRQ_HANDLED;
  429. }
  430. static int at91_adc_read_raw(struct iio_dev *indio_dev,
  431. struct iio_chan_spec const *chan,
  432. int *val, int *val2, long mask)
  433. {
  434. struct at91_adc_state *st = iio_priv(indio_dev);
  435. u32 cor = 0;
  436. int ret;
  437. switch (mask) {
  438. case IIO_CHAN_INFO_RAW:
  439. /* we cannot use software trigger if hw trigger enabled */
  440. ret = iio_device_claim_direct_mode(indio_dev);
  441. if (ret)
  442. return ret;
  443. mutex_lock(&st->lock);
  444. st->chan = chan;
  445. if (chan->differential)
  446. cor = (BIT(chan->channel) | BIT(chan->channel2)) <<
  447. AT91_SAMA5D2_COR_DIFF_OFFSET;
  448. at91_adc_writel(st, AT91_SAMA5D2_COR, cor);
  449. at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel));
  450. at91_adc_writel(st, AT91_SAMA5D2_IER, BIT(chan->channel));
  451. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START);
  452. ret = wait_event_interruptible_timeout(st->wq_data_available,
  453. st->conversion_done,
  454. msecs_to_jiffies(1000));
  455. if (ret == 0)
  456. ret = -ETIMEDOUT;
  457. if (ret > 0) {
  458. *val = st->conversion_value;
  459. if (chan->scan_type.sign == 's')
  460. *val = sign_extend32(*val, 11);
  461. ret = IIO_VAL_INT;
  462. st->conversion_done = false;
  463. }
  464. at91_adc_writel(st, AT91_SAMA5D2_IDR, BIT(chan->channel));
  465. at91_adc_writel(st, AT91_SAMA5D2_CHDR, BIT(chan->channel));
  466. mutex_unlock(&st->lock);
  467. iio_device_release_direct_mode(indio_dev);
  468. return ret;
  469. case IIO_CHAN_INFO_SCALE:
  470. *val = st->vref_uv / 1000;
  471. if (chan->differential)
  472. *val *= 2;
  473. *val2 = chan->scan_type.realbits;
  474. return IIO_VAL_FRACTIONAL_LOG2;
  475. case IIO_CHAN_INFO_SAMP_FREQ:
  476. *val = at91_adc_get_sample_freq(st);
  477. return IIO_VAL_INT;
  478. default:
  479. return -EINVAL;
  480. }
  481. }
  482. static int at91_adc_write_raw(struct iio_dev *indio_dev,
  483. struct iio_chan_spec const *chan,
  484. int val, int val2, long mask)
  485. {
  486. struct at91_adc_state *st = iio_priv(indio_dev);
  487. if (mask != IIO_CHAN_INFO_SAMP_FREQ)
  488. return -EINVAL;
  489. if (val < st->soc_info.min_sample_rate ||
  490. val > st->soc_info.max_sample_rate)
  491. return -EINVAL;
  492. at91_adc_setup_samp_freq(st, val);
  493. return 0;
  494. }
  495. static const struct iio_info at91_adc_info = {
  496. .read_raw = &at91_adc_read_raw,
  497. .write_raw = &at91_adc_write_raw,
  498. .driver_module = THIS_MODULE,
  499. };
  500. static void at91_adc_hw_init(struct at91_adc_state *st)
  501. {
  502. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
  503. at91_adc_writel(st, AT91_SAMA5D2_IDR, 0xffffffff);
  504. /*
  505. * Transfer field must be set to 2 according to the datasheet and
  506. * allows different analog settings for each channel.
  507. */
  508. at91_adc_writel(st, AT91_SAMA5D2_MR,
  509. AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH);
  510. at91_adc_setup_samp_freq(st, st->soc_info.min_sample_rate);
  511. }
  512. static int at91_adc_probe(struct platform_device *pdev)
  513. {
  514. struct iio_dev *indio_dev;
  515. struct at91_adc_state *st;
  516. struct resource *res;
  517. int ret, i;
  518. u32 edge_type;
  519. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
  520. if (!indio_dev)
  521. return -ENOMEM;
  522. indio_dev->dev.parent = &pdev->dev;
  523. indio_dev->name = dev_name(&pdev->dev);
  524. indio_dev->modes = INDIO_DIRECT_MODE;
  525. indio_dev->info = &at91_adc_info;
  526. indio_dev->channels = at91_adc_channels;
  527. indio_dev->num_channels = ARRAY_SIZE(at91_adc_channels);
  528. st = iio_priv(indio_dev);
  529. ret = of_property_read_u32(pdev->dev.of_node,
  530. "atmel,min-sample-rate-hz",
  531. &st->soc_info.min_sample_rate);
  532. if (ret) {
  533. dev_err(&pdev->dev,
  534. "invalid or missing value for atmel,min-sample-rate-hz\n");
  535. return ret;
  536. }
  537. ret = of_property_read_u32(pdev->dev.of_node,
  538. "atmel,max-sample-rate-hz",
  539. &st->soc_info.max_sample_rate);
  540. if (ret) {
  541. dev_err(&pdev->dev,
  542. "invalid or missing value for atmel,max-sample-rate-hz\n");
  543. return ret;
  544. }
  545. ret = of_property_read_u32(pdev->dev.of_node, "atmel,startup-time-ms",
  546. &st->soc_info.startup_time);
  547. if (ret) {
  548. dev_err(&pdev->dev,
  549. "invalid or missing value for atmel,startup-time-ms\n");
  550. return ret;
  551. }
  552. ret = of_property_read_u32(pdev->dev.of_node,
  553. "atmel,trigger-edge-type", &edge_type);
  554. if (ret) {
  555. dev_err(&pdev->dev,
  556. "invalid or missing value for atmel,trigger-edge-type\n");
  557. return ret;
  558. }
  559. st->selected_trig = NULL;
  560. for (i = 0; i < AT91_SAMA5D2_HW_TRIG_CNT; i++)
  561. if (at91_adc_trigger_list[i].edge_type == edge_type) {
  562. st->selected_trig = &at91_adc_trigger_list[i];
  563. break;
  564. }
  565. if (!st->selected_trig) {
  566. dev_err(&pdev->dev, "invalid external trigger edge value\n");
  567. return -EINVAL;
  568. }
  569. init_waitqueue_head(&st->wq_data_available);
  570. mutex_init(&st->lock);
  571. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  572. if (!res)
  573. return -EINVAL;
  574. st->base = devm_ioremap_resource(&pdev->dev, res);
  575. if (IS_ERR(st->base))
  576. return PTR_ERR(st->base);
  577. st->irq = platform_get_irq(pdev, 0);
  578. if (st->irq <= 0) {
  579. if (!st->irq)
  580. st->irq = -ENXIO;
  581. return st->irq;
  582. }
  583. st->per_clk = devm_clk_get(&pdev->dev, "adc_clk");
  584. if (IS_ERR(st->per_clk))
  585. return PTR_ERR(st->per_clk);
  586. st->reg = devm_regulator_get(&pdev->dev, "vddana");
  587. if (IS_ERR(st->reg))
  588. return PTR_ERR(st->reg);
  589. st->vref = devm_regulator_get(&pdev->dev, "vref");
  590. if (IS_ERR(st->vref))
  591. return PTR_ERR(st->vref);
  592. ret = devm_request_irq(&pdev->dev, st->irq, at91_adc_interrupt, 0,
  593. pdev->dev.driver->name, indio_dev);
  594. if (ret)
  595. return ret;
  596. ret = regulator_enable(st->reg);
  597. if (ret)
  598. return ret;
  599. ret = regulator_enable(st->vref);
  600. if (ret)
  601. goto reg_disable;
  602. st->vref_uv = regulator_get_voltage(st->vref);
  603. if (st->vref_uv <= 0) {
  604. ret = -EINVAL;
  605. goto vref_disable;
  606. }
  607. at91_adc_hw_init(st);
  608. ret = clk_prepare_enable(st->per_clk);
  609. if (ret)
  610. goto vref_disable;
  611. platform_set_drvdata(pdev, indio_dev);
  612. ret = at91_adc_buffer_init(indio_dev);
  613. if (ret < 0) {
  614. dev_err(&pdev->dev, "couldn't initialize the buffer.\n");
  615. goto per_clk_disable_unprepare;
  616. }
  617. ret = at91_adc_trigger_init(indio_dev);
  618. if (ret < 0) {
  619. dev_err(&pdev->dev, "couldn't setup the triggers.\n");
  620. goto per_clk_disable_unprepare;
  621. }
  622. ret = iio_device_register(indio_dev);
  623. if (ret < 0)
  624. goto per_clk_disable_unprepare;
  625. dev_info(&pdev->dev, "setting up trigger as %s\n",
  626. st->selected_trig->name);
  627. dev_info(&pdev->dev, "version: %x\n",
  628. readl_relaxed(st->base + AT91_SAMA5D2_VERSION));
  629. return 0;
  630. per_clk_disable_unprepare:
  631. clk_disable_unprepare(st->per_clk);
  632. vref_disable:
  633. regulator_disable(st->vref);
  634. reg_disable:
  635. regulator_disable(st->reg);
  636. return ret;
  637. }
  638. static int at91_adc_remove(struct platform_device *pdev)
  639. {
  640. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  641. struct at91_adc_state *st = iio_priv(indio_dev);
  642. iio_device_unregister(indio_dev);
  643. clk_disable_unprepare(st->per_clk);
  644. regulator_disable(st->vref);
  645. regulator_disable(st->reg);
  646. return 0;
  647. }
  648. static __maybe_unused int at91_adc_suspend(struct device *dev)
  649. {
  650. struct iio_dev *indio_dev =
  651. platform_get_drvdata(to_platform_device(dev));
  652. struct at91_adc_state *st = iio_priv(indio_dev);
  653. /*
  654. * Do a sofware reset of the ADC before we go to suspend.
  655. * this will ensure that all pins are free from being muxed by the ADC
  656. * and can be used by for other devices.
  657. * Otherwise, ADC will hog them and we can't go to suspend mode.
  658. */
  659. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
  660. clk_disable_unprepare(st->per_clk);
  661. regulator_disable(st->vref);
  662. regulator_disable(st->reg);
  663. return pinctrl_pm_select_sleep_state(dev);
  664. }
  665. static __maybe_unused int at91_adc_resume(struct device *dev)
  666. {
  667. struct iio_dev *indio_dev =
  668. platform_get_drvdata(to_platform_device(dev));
  669. struct at91_adc_state *st = iio_priv(indio_dev);
  670. int ret;
  671. ret = pinctrl_pm_select_default_state(dev);
  672. if (ret)
  673. goto resume_failed;
  674. ret = regulator_enable(st->reg);
  675. if (ret)
  676. goto resume_failed;
  677. ret = regulator_enable(st->vref);
  678. if (ret)
  679. goto reg_disable_resume;
  680. ret = clk_prepare_enable(st->per_clk);
  681. if (ret)
  682. goto vref_disable_resume;
  683. at91_adc_hw_init(st);
  684. /* reconfiguring trigger hardware state */
  685. if (iio_buffer_enabled(indio_dev))
  686. at91_adc_configure_trigger(st->trig, true);
  687. return 0;
  688. vref_disable_resume:
  689. regulator_disable(st->vref);
  690. reg_disable_resume:
  691. regulator_disable(st->reg);
  692. resume_failed:
  693. dev_err(&indio_dev->dev, "failed to resume\n");
  694. return ret;
  695. }
  696. static SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend, at91_adc_resume);
  697. static const struct of_device_id at91_adc_dt_match[] = {
  698. {
  699. .compatible = "atmel,sama5d2-adc",
  700. }, {
  701. /* sentinel */
  702. }
  703. };
  704. MODULE_DEVICE_TABLE(of, at91_adc_dt_match);
  705. static struct platform_driver at91_adc_driver = {
  706. .probe = at91_adc_probe,
  707. .remove = at91_adc_remove,
  708. .driver = {
  709. .name = "at91-sama5d2_adc",
  710. .of_match_table = at91_adc_dt_match,
  711. .pm = &at91_adc_pm_ops,
  712. },
  713. };
  714. module_platform_driver(at91_adc_driver)
  715. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  716. MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC");
  717. MODULE_LICENSE("GPL v2");