at91-sama5d2_adc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. bool hw_trig;
  214. };
  215. struct at91_adc_state {
  216. void __iomem *base;
  217. int irq;
  218. struct clk *per_clk;
  219. struct regulator *reg;
  220. struct regulator *vref;
  221. int vref_uv;
  222. struct iio_trigger *trig;
  223. const struct at91_adc_trigger *selected_trig;
  224. const struct iio_chan_spec *chan;
  225. bool conversion_done;
  226. u32 conversion_value;
  227. struct at91_adc_soc_info soc_info;
  228. wait_queue_head_t wq_data_available;
  229. u16 buffer[AT91_BUFFER_MAX_HWORDS];
  230. /*
  231. * lock to prevent concurrent 'single conversion' requests through
  232. * sysfs.
  233. */
  234. struct mutex lock;
  235. };
  236. static const struct at91_adc_trigger at91_adc_trigger_list[] = {
  237. {
  238. .name = "external_rising",
  239. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE,
  240. .edge_type = IRQ_TYPE_EDGE_RISING,
  241. .hw_trig = true,
  242. },
  243. {
  244. .name = "external_falling",
  245. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL,
  246. .edge_type = IRQ_TYPE_EDGE_FALLING,
  247. .hw_trig = true,
  248. },
  249. {
  250. .name = "external_any",
  251. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY,
  252. .edge_type = IRQ_TYPE_EDGE_BOTH,
  253. .hw_trig = true,
  254. },
  255. {
  256. .name = "software",
  257. .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER,
  258. .edge_type = IRQ_TYPE_NONE,
  259. .hw_trig = false,
  260. },
  261. };
  262. static const struct iio_chan_spec at91_adc_channels[] = {
  263. AT91_SAMA5D2_CHAN_SINGLE(0, 0x50),
  264. AT91_SAMA5D2_CHAN_SINGLE(1, 0x54),
  265. AT91_SAMA5D2_CHAN_SINGLE(2, 0x58),
  266. AT91_SAMA5D2_CHAN_SINGLE(3, 0x5c),
  267. AT91_SAMA5D2_CHAN_SINGLE(4, 0x60),
  268. AT91_SAMA5D2_CHAN_SINGLE(5, 0x64),
  269. AT91_SAMA5D2_CHAN_SINGLE(6, 0x68),
  270. AT91_SAMA5D2_CHAN_SINGLE(7, 0x6c),
  271. AT91_SAMA5D2_CHAN_SINGLE(8, 0x70),
  272. AT91_SAMA5D2_CHAN_SINGLE(9, 0x74),
  273. AT91_SAMA5D2_CHAN_SINGLE(10, 0x78),
  274. AT91_SAMA5D2_CHAN_SINGLE(11, 0x7c),
  275. AT91_SAMA5D2_CHAN_DIFF(0, 1, 0x50),
  276. AT91_SAMA5D2_CHAN_DIFF(2, 3, 0x58),
  277. AT91_SAMA5D2_CHAN_DIFF(4, 5, 0x60),
  278. AT91_SAMA5D2_CHAN_DIFF(6, 7, 0x68),
  279. AT91_SAMA5D2_CHAN_DIFF(8, 9, 0x70),
  280. AT91_SAMA5D2_CHAN_DIFF(10, 11, 0x78),
  281. IIO_CHAN_SOFT_TIMESTAMP(AT91_SAMA5D2_SINGLE_CHAN_CNT
  282. + AT91_SAMA5D2_DIFF_CHAN_CNT + 1),
  283. };
  284. static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
  285. {
  286. struct iio_dev *indio = iio_trigger_get_drvdata(trig);
  287. struct at91_adc_state *st = iio_priv(indio);
  288. u32 status = at91_adc_readl(st, AT91_SAMA5D2_TRGR);
  289. u8 bit;
  290. /* clear TRGMOD */
  291. status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK;
  292. if (state)
  293. status |= st->selected_trig->trgmod_value;
  294. /* set/unset hw trigger */
  295. at91_adc_writel(st, AT91_SAMA5D2_TRGR, status);
  296. for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) {
  297. struct iio_chan_spec const *chan = indio->channels + bit;
  298. if (state) {
  299. at91_adc_writel(st, AT91_SAMA5D2_CHER,
  300. BIT(chan->channel));
  301. at91_adc_writel(st, AT91_SAMA5D2_IER,
  302. BIT(chan->channel));
  303. } else {
  304. at91_adc_writel(st, AT91_SAMA5D2_IDR,
  305. BIT(chan->channel));
  306. at91_adc_writel(st, AT91_SAMA5D2_CHDR,
  307. BIT(chan->channel));
  308. }
  309. }
  310. return 0;
  311. }
  312. static int at91_adc_reenable_trigger(struct iio_trigger *trig)
  313. {
  314. struct iio_dev *indio = iio_trigger_get_drvdata(trig);
  315. struct at91_adc_state *st = iio_priv(indio);
  316. enable_irq(st->irq);
  317. /* Needed to ACK the DRDY interruption */
  318. at91_adc_readl(st, AT91_SAMA5D2_LCDR);
  319. return 0;
  320. }
  321. static const struct iio_trigger_ops at91_adc_trigger_ops = {
  322. .owner = THIS_MODULE,
  323. .set_trigger_state = &at91_adc_configure_trigger,
  324. .try_reenable = &at91_adc_reenable_trigger,
  325. };
  326. static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *indio,
  327. char *trigger_name)
  328. {
  329. struct iio_trigger *trig;
  330. int ret;
  331. trig = devm_iio_trigger_alloc(&indio->dev, "%s-dev%d-%s", indio->name,
  332. indio->id, trigger_name);
  333. if (!trig)
  334. return NULL;
  335. trig->dev.parent = indio->dev.parent;
  336. iio_trigger_set_drvdata(trig, indio);
  337. trig->ops = &at91_adc_trigger_ops;
  338. ret = devm_iio_trigger_register(&indio->dev, trig);
  339. if (ret)
  340. return ERR_PTR(ret);
  341. return trig;
  342. }
  343. static int at91_adc_trigger_init(struct iio_dev *indio)
  344. {
  345. struct at91_adc_state *st = iio_priv(indio);
  346. st->trig = at91_adc_allocate_trigger(indio, st->selected_trig->name);
  347. if (IS_ERR(st->trig)) {
  348. dev_err(&indio->dev,
  349. "could not allocate trigger\n");
  350. return PTR_ERR(st->trig);
  351. }
  352. return 0;
  353. }
  354. static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
  355. {
  356. struct iio_poll_func *pf = p;
  357. struct iio_dev *indio = pf->indio_dev;
  358. struct at91_adc_state *st = iio_priv(indio);
  359. int i = 0;
  360. u8 bit;
  361. for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) {
  362. struct iio_chan_spec const *chan = indio->channels + bit;
  363. st->buffer[i] = at91_adc_readl(st, chan->address);
  364. i++;
  365. }
  366. iio_push_to_buffers_with_timestamp(indio, st->buffer, pf->timestamp);
  367. iio_trigger_notify_done(indio->trig);
  368. return IRQ_HANDLED;
  369. }
  370. static int at91_adc_buffer_init(struct iio_dev *indio)
  371. {
  372. return devm_iio_triggered_buffer_setup(&indio->dev, indio,
  373. &iio_pollfunc_store_time,
  374. &at91_adc_trigger_handler, NULL);
  375. }
  376. static unsigned at91_adc_startup_time(unsigned startup_time_min,
  377. unsigned adc_clk_khz)
  378. {
  379. static const unsigned int startup_lookup[] = {
  380. 0, 8, 16, 24,
  381. 64, 80, 96, 112,
  382. 512, 576, 640, 704,
  383. 768, 832, 896, 960
  384. };
  385. unsigned ticks_min, i;
  386. /*
  387. * Since the adc frequency is checked before, there is no reason
  388. * to not meet the startup time constraint.
  389. */
  390. ticks_min = startup_time_min * adc_clk_khz / 1000;
  391. for (i = 0; i < ARRAY_SIZE(startup_lookup); i++)
  392. if (startup_lookup[i] > ticks_min)
  393. break;
  394. return i;
  395. }
  396. static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq)
  397. {
  398. struct iio_dev *indio_dev = iio_priv_to_dev(st);
  399. unsigned f_per, prescal, startup, mr;
  400. f_per = clk_get_rate(st->per_clk);
  401. prescal = (f_per / (2 * freq)) - 1;
  402. startup = at91_adc_startup_time(st->soc_info.startup_time,
  403. freq / 1000);
  404. mr = at91_adc_readl(st, AT91_SAMA5D2_MR);
  405. mr &= ~(AT91_SAMA5D2_MR_STARTUP_MASK | AT91_SAMA5D2_MR_PRESCAL_MASK);
  406. mr |= AT91_SAMA5D2_MR_STARTUP(startup);
  407. mr |= AT91_SAMA5D2_MR_PRESCAL(prescal);
  408. at91_adc_writel(st, AT91_SAMA5D2_MR, mr);
  409. dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n",
  410. freq, startup, prescal);
  411. }
  412. static unsigned at91_adc_get_sample_freq(struct at91_adc_state *st)
  413. {
  414. unsigned f_adc, f_per = clk_get_rate(st->per_clk);
  415. unsigned mr, prescal;
  416. mr = at91_adc_readl(st, AT91_SAMA5D2_MR);
  417. prescal = (mr >> AT91_SAMA5D2_MR_PRESCAL_OFFSET)
  418. & AT91_SAMA5D2_MR_PRESCAL_MAX;
  419. f_adc = f_per / (2 * (prescal + 1));
  420. return f_adc;
  421. }
  422. static irqreturn_t at91_adc_interrupt(int irq, void *private)
  423. {
  424. struct iio_dev *indio = private;
  425. struct at91_adc_state *st = iio_priv(indio);
  426. u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR);
  427. u32 imr = at91_adc_readl(st, AT91_SAMA5D2_IMR);
  428. if (!(status & imr))
  429. return IRQ_NONE;
  430. if (iio_buffer_enabled(indio)) {
  431. disable_irq_nosync(irq);
  432. iio_trigger_poll(indio->trig);
  433. } else {
  434. st->conversion_value = at91_adc_readl(st, st->chan->address);
  435. st->conversion_done = true;
  436. wake_up_interruptible(&st->wq_data_available);
  437. }
  438. return IRQ_HANDLED;
  439. }
  440. static int at91_adc_read_raw(struct iio_dev *indio_dev,
  441. struct iio_chan_spec const *chan,
  442. int *val, int *val2, long mask)
  443. {
  444. struct at91_adc_state *st = iio_priv(indio_dev);
  445. u32 cor = 0;
  446. int ret;
  447. switch (mask) {
  448. case IIO_CHAN_INFO_RAW:
  449. /* we cannot use software trigger if hw trigger enabled */
  450. ret = iio_device_claim_direct_mode(indio_dev);
  451. if (ret)
  452. return ret;
  453. mutex_lock(&st->lock);
  454. st->chan = chan;
  455. if (chan->differential)
  456. cor = (BIT(chan->channel) | BIT(chan->channel2)) <<
  457. AT91_SAMA5D2_COR_DIFF_OFFSET;
  458. at91_adc_writel(st, AT91_SAMA5D2_COR, cor);
  459. at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel));
  460. at91_adc_writel(st, AT91_SAMA5D2_IER, BIT(chan->channel));
  461. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START);
  462. ret = wait_event_interruptible_timeout(st->wq_data_available,
  463. st->conversion_done,
  464. msecs_to_jiffies(1000));
  465. if (ret == 0)
  466. ret = -ETIMEDOUT;
  467. if (ret > 0) {
  468. *val = st->conversion_value;
  469. if (chan->scan_type.sign == 's')
  470. *val = sign_extend32(*val, 11);
  471. ret = IIO_VAL_INT;
  472. st->conversion_done = false;
  473. }
  474. at91_adc_writel(st, AT91_SAMA5D2_IDR, BIT(chan->channel));
  475. at91_adc_writel(st, AT91_SAMA5D2_CHDR, BIT(chan->channel));
  476. mutex_unlock(&st->lock);
  477. iio_device_release_direct_mode(indio_dev);
  478. return ret;
  479. case IIO_CHAN_INFO_SCALE:
  480. *val = st->vref_uv / 1000;
  481. if (chan->differential)
  482. *val *= 2;
  483. *val2 = chan->scan_type.realbits;
  484. return IIO_VAL_FRACTIONAL_LOG2;
  485. case IIO_CHAN_INFO_SAMP_FREQ:
  486. *val = at91_adc_get_sample_freq(st);
  487. return IIO_VAL_INT;
  488. default:
  489. return -EINVAL;
  490. }
  491. }
  492. static int at91_adc_write_raw(struct iio_dev *indio_dev,
  493. struct iio_chan_spec const *chan,
  494. int val, int val2, long mask)
  495. {
  496. struct at91_adc_state *st = iio_priv(indio_dev);
  497. if (mask != IIO_CHAN_INFO_SAMP_FREQ)
  498. return -EINVAL;
  499. if (val < st->soc_info.min_sample_rate ||
  500. val > st->soc_info.max_sample_rate)
  501. return -EINVAL;
  502. at91_adc_setup_samp_freq(st, val);
  503. return 0;
  504. }
  505. static const struct iio_info at91_adc_info = {
  506. .read_raw = &at91_adc_read_raw,
  507. .write_raw = &at91_adc_write_raw,
  508. .driver_module = THIS_MODULE,
  509. };
  510. static void at91_adc_hw_init(struct at91_adc_state *st)
  511. {
  512. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
  513. at91_adc_writel(st, AT91_SAMA5D2_IDR, 0xffffffff);
  514. /*
  515. * Transfer field must be set to 2 according to the datasheet and
  516. * allows different analog settings for each channel.
  517. */
  518. at91_adc_writel(st, AT91_SAMA5D2_MR,
  519. AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH);
  520. at91_adc_setup_samp_freq(st, st->soc_info.min_sample_rate);
  521. }
  522. static int at91_adc_probe(struct platform_device *pdev)
  523. {
  524. struct iio_dev *indio_dev;
  525. struct at91_adc_state *st;
  526. struct resource *res;
  527. int ret, i;
  528. u32 edge_type = IRQ_TYPE_NONE;
  529. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
  530. if (!indio_dev)
  531. return -ENOMEM;
  532. indio_dev->dev.parent = &pdev->dev;
  533. indio_dev->name = dev_name(&pdev->dev);
  534. indio_dev->modes = INDIO_DIRECT_MODE;
  535. indio_dev->info = &at91_adc_info;
  536. indio_dev->channels = at91_adc_channels;
  537. indio_dev->num_channels = ARRAY_SIZE(at91_adc_channels);
  538. st = iio_priv(indio_dev);
  539. ret = of_property_read_u32(pdev->dev.of_node,
  540. "atmel,min-sample-rate-hz",
  541. &st->soc_info.min_sample_rate);
  542. if (ret) {
  543. dev_err(&pdev->dev,
  544. "invalid or missing value for atmel,min-sample-rate-hz\n");
  545. return ret;
  546. }
  547. ret = of_property_read_u32(pdev->dev.of_node,
  548. "atmel,max-sample-rate-hz",
  549. &st->soc_info.max_sample_rate);
  550. if (ret) {
  551. dev_err(&pdev->dev,
  552. "invalid or missing value for atmel,max-sample-rate-hz\n");
  553. return ret;
  554. }
  555. ret = of_property_read_u32(pdev->dev.of_node, "atmel,startup-time-ms",
  556. &st->soc_info.startup_time);
  557. if (ret) {
  558. dev_err(&pdev->dev,
  559. "invalid or missing value for atmel,startup-time-ms\n");
  560. return ret;
  561. }
  562. ret = of_property_read_u32(pdev->dev.of_node,
  563. "atmel,trigger-edge-type", &edge_type);
  564. if (ret) {
  565. dev_dbg(&pdev->dev,
  566. "atmel,trigger-edge-type not specified, only software trigger available\n");
  567. }
  568. st->selected_trig = NULL;
  569. /* find the right trigger, or no trigger at all */
  570. for (i = 0; i < AT91_SAMA5D2_HW_TRIG_CNT + 1; i++)
  571. if (at91_adc_trigger_list[i].edge_type == edge_type) {
  572. st->selected_trig = &at91_adc_trigger_list[i];
  573. break;
  574. }
  575. if (!st->selected_trig) {
  576. dev_err(&pdev->dev, "invalid external trigger edge value\n");
  577. return -EINVAL;
  578. }
  579. init_waitqueue_head(&st->wq_data_available);
  580. mutex_init(&st->lock);
  581. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  582. if (!res)
  583. return -EINVAL;
  584. st->base = devm_ioremap_resource(&pdev->dev, res);
  585. if (IS_ERR(st->base))
  586. return PTR_ERR(st->base);
  587. st->irq = platform_get_irq(pdev, 0);
  588. if (st->irq <= 0) {
  589. if (!st->irq)
  590. st->irq = -ENXIO;
  591. return st->irq;
  592. }
  593. st->per_clk = devm_clk_get(&pdev->dev, "adc_clk");
  594. if (IS_ERR(st->per_clk))
  595. return PTR_ERR(st->per_clk);
  596. st->reg = devm_regulator_get(&pdev->dev, "vddana");
  597. if (IS_ERR(st->reg))
  598. return PTR_ERR(st->reg);
  599. st->vref = devm_regulator_get(&pdev->dev, "vref");
  600. if (IS_ERR(st->vref))
  601. return PTR_ERR(st->vref);
  602. ret = devm_request_irq(&pdev->dev, st->irq, at91_adc_interrupt, 0,
  603. pdev->dev.driver->name, indio_dev);
  604. if (ret)
  605. return ret;
  606. ret = regulator_enable(st->reg);
  607. if (ret)
  608. return ret;
  609. ret = regulator_enable(st->vref);
  610. if (ret)
  611. goto reg_disable;
  612. st->vref_uv = regulator_get_voltage(st->vref);
  613. if (st->vref_uv <= 0) {
  614. ret = -EINVAL;
  615. goto vref_disable;
  616. }
  617. at91_adc_hw_init(st);
  618. ret = clk_prepare_enable(st->per_clk);
  619. if (ret)
  620. goto vref_disable;
  621. platform_set_drvdata(pdev, indio_dev);
  622. if (st->selected_trig->hw_trig) {
  623. ret = at91_adc_buffer_init(indio_dev);
  624. if (ret < 0) {
  625. dev_err(&pdev->dev, "couldn't initialize the buffer.\n");
  626. goto per_clk_disable_unprepare;
  627. }
  628. ret = at91_adc_trigger_init(indio_dev);
  629. if (ret < 0) {
  630. dev_err(&pdev->dev, "couldn't setup the triggers.\n");
  631. goto per_clk_disable_unprepare;
  632. }
  633. }
  634. ret = iio_device_register(indio_dev);
  635. if (ret < 0)
  636. goto per_clk_disable_unprepare;
  637. if (st->selected_trig->hw_trig)
  638. dev_info(&pdev->dev, "setting up trigger as %s\n",
  639. st->selected_trig->name);
  640. dev_info(&pdev->dev, "version: %x\n",
  641. readl_relaxed(st->base + AT91_SAMA5D2_VERSION));
  642. return 0;
  643. per_clk_disable_unprepare:
  644. clk_disable_unprepare(st->per_clk);
  645. vref_disable:
  646. regulator_disable(st->vref);
  647. reg_disable:
  648. regulator_disable(st->reg);
  649. return ret;
  650. }
  651. static int at91_adc_remove(struct platform_device *pdev)
  652. {
  653. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  654. struct at91_adc_state *st = iio_priv(indio_dev);
  655. iio_device_unregister(indio_dev);
  656. clk_disable_unprepare(st->per_clk);
  657. regulator_disable(st->vref);
  658. regulator_disable(st->reg);
  659. return 0;
  660. }
  661. static __maybe_unused int at91_adc_suspend(struct device *dev)
  662. {
  663. struct iio_dev *indio_dev =
  664. platform_get_drvdata(to_platform_device(dev));
  665. struct at91_adc_state *st = iio_priv(indio_dev);
  666. /*
  667. * Do a sofware reset of the ADC before we go to suspend.
  668. * this will ensure that all pins are free from being muxed by the ADC
  669. * and can be used by for other devices.
  670. * Otherwise, ADC will hog them and we can't go to suspend mode.
  671. */
  672. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
  673. clk_disable_unprepare(st->per_clk);
  674. regulator_disable(st->vref);
  675. regulator_disable(st->reg);
  676. return pinctrl_pm_select_sleep_state(dev);
  677. }
  678. static __maybe_unused int at91_adc_resume(struct device *dev)
  679. {
  680. struct iio_dev *indio_dev =
  681. platform_get_drvdata(to_platform_device(dev));
  682. struct at91_adc_state *st = iio_priv(indio_dev);
  683. int ret;
  684. ret = pinctrl_pm_select_default_state(dev);
  685. if (ret)
  686. goto resume_failed;
  687. ret = regulator_enable(st->reg);
  688. if (ret)
  689. goto resume_failed;
  690. ret = regulator_enable(st->vref);
  691. if (ret)
  692. goto reg_disable_resume;
  693. ret = clk_prepare_enable(st->per_clk);
  694. if (ret)
  695. goto vref_disable_resume;
  696. at91_adc_hw_init(st);
  697. /* reconfiguring trigger hardware state */
  698. if (iio_buffer_enabled(indio_dev))
  699. at91_adc_configure_trigger(st->trig, true);
  700. return 0;
  701. vref_disable_resume:
  702. regulator_disable(st->vref);
  703. reg_disable_resume:
  704. regulator_disable(st->reg);
  705. resume_failed:
  706. dev_err(&indio_dev->dev, "failed to resume\n");
  707. return ret;
  708. }
  709. static SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend, at91_adc_resume);
  710. static const struct of_device_id at91_adc_dt_match[] = {
  711. {
  712. .compatible = "atmel,sama5d2-adc",
  713. }, {
  714. /* sentinel */
  715. }
  716. };
  717. MODULE_DEVICE_TABLE(of, at91_adc_dt_match);
  718. static struct platform_driver at91_adc_driver = {
  719. .probe = at91_adc_probe,
  720. .remove = at91_adc_remove,
  721. .driver = {
  722. .name = "at91-sama5d2_adc",
  723. .of_match_table = at91_adc_dt_match,
  724. .pm = &at91_adc_pm_ops,
  725. },
  726. };
  727. module_platform_driver(at91_adc_driver)
  728. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  729. MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC");
  730. MODULE_LICENSE("GPL v2");