stm32-adc-core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file is part of STM32 ADC driver
  4. *
  5. * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
  6. * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
  7. *
  8. * Inspired from: fsl-imx25-tsadc
  9. *
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/clk.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/irqdesc.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/property.h>
  24. #include <linux/regmap.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/slab.h>
  27. #include <linux/units.h>
  28. #include "stm32-adc-core.h"
  29. #define STM32_ADC_CORE_SLEEP_DELAY_MS 2000
  30. /* SYSCFG registers */
  31. #define STM32MP1_SYSCFG_PMCSETR 0x04
  32. #define STM32MP1_SYSCFG_PMCCLRR 0x44
  33. /* SYSCFG bit fields */
  34. #define STM32MP1_SYSCFG_ANASWVDD_MASK BIT(9)
  35. /* SYSCFG capability flags */
  36. #define HAS_VBOOSTER BIT(0)
  37. #define HAS_ANASWVDD BIT(1)
  38. /**
  39. * struct stm32_adc_common_regs - stm32 common registers
  40. * @csr: common status register offset
  41. * @ccr: common control register offset
  42. * @eoc_msk: array of eoc (end of conversion flag) masks in csr for adc1..n
  43. * @ovr_msk: array of ovr (overrun flag) masks in csr for adc1..n
  44. * @ier: interrupt enable register offset for each adc
  45. * @eocie_msk: end of conversion interrupt enable mask in @ier
  46. */
  47. struct stm32_adc_common_regs {
  48. u32 csr;
  49. u32 ccr;
  50. u32 eoc_msk[STM32_ADC_MAX_ADCS];
  51. u32 ovr_msk[STM32_ADC_MAX_ADCS];
  52. u32 ier;
  53. u32 eocie_msk;
  54. };
  55. struct stm32_adc_priv;
  56. /**
  57. * struct stm32_adc_priv_cfg - stm32 core compatible configuration data
  58. * @regs: common registers for all instances
  59. * @clk_sel: clock selection routine
  60. * @max_clk_rate_hz: maximum analog clock rate (Hz, from datasheet)
  61. * @ipid: adc identification number
  62. * @has_syscfg: SYSCFG capability flags
  63. * @num_irqs: number of interrupt lines
  64. * @num_adcs: maximum number of ADC instances in the common registers
  65. */
  66. struct stm32_adc_priv_cfg {
  67. const struct stm32_adc_common_regs *regs;
  68. int (*clk_sel)(struct platform_device *, struct stm32_adc_priv *);
  69. u32 max_clk_rate_hz;
  70. u32 ipid;
  71. unsigned int has_syscfg;
  72. unsigned int num_irqs;
  73. unsigned int num_adcs;
  74. };
  75. /**
  76. * struct stm32_adc_priv - stm32 ADC core private data
  77. * @irq: irq(s) for ADC block
  78. * @nb_adc_max: actual maximum number of instance per ADC block
  79. * @domain: irq domain reference
  80. * @aclk: clock reference for the analog circuitry
  81. * @bclk: bus clock common for all ADCs, depends on part used
  82. * @max_clk_rate: desired maximum clock rate
  83. * @booster: booster supply reference
  84. * @vdd: vdd supply reference
  85. * @vdda: vdda analog supply reference
  86. * @vref: regulator reference
  87. * @vdd_uv: vdd supply voltage (microvolts)
  88. * @vdda_uv: vdda supply voltage (microvolts)
  89. * @cfg: compatible configuration data
  90. * @common: common data for all ADC instances
  91. * @ccr_bak: backup CCR in low power mode
  92. * @syscfg: reference to syscon, system control registers
  93. */
  94. struct stm32_adc_priv {
  95. int irq[STM32_ADC_MAX_ADCS];
  96. unsigned int nb_adc_max;
  97. struct irq_domain *domain;
  98. struct clk *aclk;
  99. struct clk *bclk;
  100. u32 max_clk_rate;
  101. struct regulator *booster;
  102. struct regulator *vdd;
  103. struct regulator *vdda;
  104. struct regulator *vref;
  105. int vdd_uv;
  106. int vdda_uv;
  107. const struct stm32_adc_priv_cfg *cfg;
  108. struct stm32_adc_common common;
  109. u32 ccr_bak;
  110. struct regmap *syscfg;
  111. };
  112. static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
  113. {
  114. return container_of(com, struct stm32_adc_priv, common);
  115. }
  116. /* STM32F4 ADC internal common clock prescaler division ratios */
  117. static int stm32f4_pclk_div[] = {2, 4, 6, 8};
  118. /**
  119. * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
  120. * @pdev: platform device
  121. * @priv: stm32 ADC core private data
  122. * Select clock prescaler used for analog conversions, before using ADC.
  123. */
  124. static int stm32f4_adc_clk_sel(struct platform_device *pdev,
  125. struct stm32_adc_priv *priv)
  126. {
  127. unsigned long rate;
  128. u32 val;
  129. int i;
  130. /* stm32f4 has one clk input for analog (mandatory), enforce it here */
  131. if (!priv->aclk) {
  132. dev_err(&pdev->dev, "No 'adc' clock found\n");
  133. return -ENOENT;
  134. }
  135. rate = clk_get_rate(priv->aclk);
  136. if (!rate) {
  137. dev_err(&pdev->dev, "Invalid clock rate: 0\n");
  138. return -EINVAL;
  139. }
  140. for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
  141. if ((rate / stm32f4_pclk_div[i]) <= priv->max_clk_rate)
  142. break;
  143. }
  144. if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
  145. dev_err(&pdev->dev, "adc clk selection failed\n");
  146. return -EINVAL;
  147. }
  148. priv->common.rate = rate / stm32f4_pclk_div[i];
  149. val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
  150. val &= ~STM32F4_ADC_ADCPRE_MASK;
  151. val |= i << STM32F4_ADC_ADCPRE_SHIFT;
  152. writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
  153. dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
  154. priv->common.rate / 1000);
  155. return 0;
  156. }
  157. /**
  158. * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
  159. * @ckmode: ADC clock mode, Async or sync with prescaler.
  160. * @presc: prescaler bitfield for async clock mode
  161. * @div: prescaler division ratio
  162. */
  163. struct stm32h7_adc_ck_spec {
  164. u32 ckmode;
  165. u32 presc;
  166. int div;
  167. };
  168. static const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec[] = {
  169. /* 00: CK_ADC[1..3]: Asynchronous clock modes */
  170. { 0, 0, 1 },
  171. { 0, 1, 2 },
  172. { 0, 2, 4 },
  173. { 0, 3, 6 },
  174. { 0, 4, 8 },
  175. { 0, 5, 10 },
  176. { 0, 6, 12 },
  177. { 0, 7, 16 },
  178. { 0, 8, 32 },
  179. { 0, 9, 64 },
  180. { 0, 10, 128 },
  181. { 0, 11, 256 },
  182. /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
  183. { 1, 0, 1 },
  184. { 2, 0, 2 },
  185. { 3, 0, 4 },
  186. };
  187. static int stm32h7_adc_clk_sel(struct platform_device *pdev,
  188. struct stm32_adc_priv *priv)
  189. {
  190. u32 ckmode, presc, val;
  191. unsigned long rate;
  192. int i, div, duty;
  193. /* stm32h7 bus clock is common for all ADC instances (mandatory) */
  194. if (!priv->bclk) {
  195. dev_err(&pdev->dev, "No 'bus' clock found\n");
  196. return -ENOENT;
  197. }
  198. /*
  199. * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
  200. * So, choice is to have bus clock mandatory and adc clock optional.
  201. * If optional 'adc' clock has been found, then try to use it first.
  202. */
  203. if (priv->aclk) {
  204. /*
  205. * Asynchronous clock modes (e.g. ckmode == 0)
  206. * From spec: PLL output musn't exceed max rate
  207. */
  208. rate = clk_get_rate(priv->aclk);
  209. if (!rate) {
  210. dev_err(&pdev->dev, "Invalid adc clock rate: 0\n");
  211. return -EINVAL;
  212. }
  213. /* If duty is an error, kindly use at least /2 divider */
  214. duty = clk_get_scaled_duty_cycle(priv->aclk, 100);
  215. if (duty < 0)
  216. dev_warn(&pdev->dev, "adc clock duty: %d\n", duty);
  217. for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
  218. ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
  219. presc = stm32h7_adc_ckmodes_spec[i].presc;
  220. div = stm32h7_adc_ckmodes_spec[i].div;
  221. if (ckmode)
  222. continue;
  223. /*
  224. * For proper operation, clock duty cycle range is 49%
  225. * to 51%. Apply at least /2 prescaler otherwise.
  226. */
  227. if (div == 1 && (duty < 49 || duty > 51))
  228. continue;
  229. if ((rate / div) <= priv->max_clk_rate)
  230. goto out;
  231. }
  232. }
  233. /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
  234. rate = clk_get_rate(priv->bclk);
  235. if (!rate) {
  236. dev_err(&pdev->dev, "Invalid bus clock rate: 0\n");
  237. return -EINVAL;
  238. }
  239. duty = clk_get_scaled_duty_cycle(priv->bclk, 100);
  240. if (duty < 0)
  241. dev_warn(&pdev->dev, "bus clock duty: %d\n", duty);
  242. for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
  243. ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
  244. presc = stm32h7_adc_ckmodes_spec[i].presc;
  245. div = stm32h7_adc_ckmodes_spec[i].div;
  246. if (!ckmode)
  247. continue;
  248. if (div == 1 && (duty < 49 || duty > 51))
  249. continue;
  250. if ((rate / div) <= priv->max_clk_rate)
  251. goto out;
  252. }
  253. dev_err(&pdev->dev, "adc clk selection failed\n");
  254. return -EINVAL;
  255. out:
  256. /* rate used later by each ADC instance to control BOOST mode */
  257. priv->common.rate = rate / div;
  258. /* Set common clock mode and prescaler */
  259. val = readl_relaxed(priv->common.base + STM32H7_ADC_CCR);
  260. val &= ~(STM32H7_CKMODE_MASK | STM32H7_PRESC_MASK);
  261. val |= ckmode << STM32H7_CKMODE_SHIFT;
  262. val |= presc << STM32H7_PRESC_SHIFT;
  263. writel_relaxed(val, priv->common.base + STM32H7_ADC_CCR);
  264. dev_dbg(&pdev->dev, "Using %s clock/%d source at %ld kHz\n",
  265. ckmode ? "bus" : "adc", div, priv->common.rate / 1000);
  266. return 0;
  267. }
  268. /* STM32F4 common registers definitions */
  269. static const struct stm32_adc_common_regs stm32f4_adc_common_regs = {
  270. .csr = STM32F4_ADC_CSR,
  271. .ccr = STM32F4_ADC_CCR,
  272. .eoc_msk = { STM32F4_EOC1, STM32F4_EOC2, STM32F4_EOC3 },
  273. .ovr_msk = { STM32F4_OVR1, STM32F4_OVR2, STM32F4_OVR3 },
  274. .ier = STM32F4_ADC_CR1,
  275. .eocie_msk = STM32F4_EOCIE,
  276. };
  277. /* STM32H7 common registers definitions */
  278. static const struct stm32_adc_common_regs stm32h7_adc_common_regs = {
  279. .csr = STM32H7_ADC_CSR,
  280. .ccr = STM32H7_ADC_CCR,
  281. .eoc_msk = { STM32H7_EOC_MST, STM32H7_EOC_SLV },
  282. .ovr_msk = { STM32H7_OVR_MST, STM32H7_OVR_SLV },
  283. .ier = STM32H7_ADC_IER,
  284. .eocie_msk = STM32H7_EOCIE,
  285. };
  286. /* STM32MP13 common registers definitions */
  287. static const struct stm32_adc_common_regs stm32mp13_adc_common_regs = {
  288. .csr = STM32H7_ADC_CSR,
  289. .ccr = STM32H7_ADC_CCR,
  290. .eoc_msk = { STM32H7_EOC_MST },
  291. .ovr_msk = { STM32H7_OVR_MST },
  292. .ier = STM32H7_ADC_IER,
  293. .eocie_msk = STM32H7_EOCIE,
  294. };
  295. static const unsigned int stm32_adc_offset[STM32_ADC_MAX_ADCS] = {
  296. 0, STM32_ADC_OFFSET, STM32_ADC_OFFSET * 2,
  297. };
  298. static unsigned int stm32_adc_eoc_enabled(struct stm32_adc_priv *priv,
  299. unsigned int adc)
  300. {
  301. u32 ier, offset = stm32_adc_offset[adc];
  302. ier = readl_relaxed(priv->common.base + offset + priv->cfg->regs->ier);
  303. return ier & priv->cfg->regs->eocie_msk;
  304. }
  305. /* ADC common interrupt for all instances */
  306. static void stm32_adc_irq_handler(struct irq_desc *desc)
  307. {
  308. struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
  309. struct irq_chip *chip = irq_desc_get_chip(desc);
  310. int i;
  311. u32 status;
  312. chained_irq_enter(chip, desc);
  313. status = readl_relaxed(priv->common.base + priv->cfg->regs->csr);
  314. /*
  315. * End of conversion may be handled by using IRQ or DMA. There may be a
  316. * race here when two conversions complete at the same time on several
  317. * ADCs. EOC may be read 'set' for several ADCs, with:
  318. * - an ADC configured to use DMA (EOC triggers the DMA request, and
  319. * is then automatically cleared by DR read in hardware)
  320. * - an ADC configured to use IRQs (EOCIE bit is set. The handler must
  321. * be called in this case)
  322. * So both EOC status bit in CSR and EOCIE control bit must be checked
  323. * before invoking the interrupt handler (e.g. call ISR only for
  324. * IRQ-enabled ADCs).
  325. */
  326. for (i = 0; i < priv->nb_adc_max; i++) {
  327. if ((status & priv->cfg->regs->eoc_msk[i] &&
  328. stm32_adc_eoc_enabled(priv, i)) ||
  329. (status & priv->cfg->regs->ovr_msk[i]))
  330. generic_handle_domain_irq(priv->domain, i);
  331. }
  332. chained_irq_exit(chip, desc);
  333. };
  334. static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
  335. irq_hw_number_t hwirq)
  336. {
  337. irq_set_chip_data(irq, d->host_data);
  338. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
  339. return 0;
  340. }
  341. static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
  342. {
  343. irq_set_chip_and_handler(irq, NULL, NULL);
  344. irq_set_chip_data(irq, NULL);
  345. }
  346. static const struct irq_domain_ops stm32_adc_domain_ops = {
  347. .map = stm32_adc_domain_map,
  348. .unmap = stm32_adc_domain_unmap,
  349. .xlate = irq_domain_xlate_onecell,
  350. };
  351. static int stm32_adc_irq_probe(struct platform_device *pdev,
  352. struct stm32_adc_priv *priv)
  353. {
  354. unsigned int i;
  355. /*
  356. * Interrupt(s) must be provided, depending on the compatible:
  357. * - stm32f4/h7 shares a common interrupt line.
  358. * - stm32mp1, has one line per ADC
  359. */
  360. for (i = 0; i < priv->cfg->num_irqs; i++) {
  361. priv->irq[i] = platform_get_irq(pdev, i);
  362. if (priv->irq[i] < 0)
  363. return priv->irq[i];
  364. }
  365. priv->domain = irq_domain_create_simple(dev_fwnode(&pdev->dev),
  366. STM32_ADC_MAX_ADCS, 0,
  367. &stm32_adc_domain_ops,
  368. priv);
  369. if (!priv->domain) {
  370. dev_err(&pdev->dev, "Failed to add irq domain\n");
  371. return -ENOMEM;
  372. }
  373. for (i = 0; i < priv->cfg->num_irqs; i++)
  374. irq_set_chained_handler_and_data(priv->irq[i],
  375. stm32_adc_irq_handler, priv);
  376. return 0;
  377. }
  378. static void stm32_adc_irq_remove(struct platform_device *pdev,
  379. struct stm32_adc_priv *priv)
  380. {
  381. int hwirq;
  382. unsigned int i;
  383. for (hwirq = 0; hwirq < priv->nb_adc_max; hwirq++)
  384. irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
  385. irq_domain_remove(priv->domain);
  386. for (i = 0; i < priv->cfg->num_irqs; i++)
  387. irq_set_chained_handler(priv->irq[i], NULL);
  388. }
  389. static int stm32_adc_core_switches_supply_en(struct stm32_adc_priv *priv,
  390. struct device *dev)
  391. {
  392. int ret;
  393. /*
  394. * On STM32H7 and STM32MP1, the ADC inputs are multiplexed with analog
  395. * switches (via PCSEL) which have reduced performances when their
  396. * supply is below 2.7V (vdda by default):
  397. * - Voltage booster can be used, to get full ADC performances
  398. * (increases power consumption).
  399. * - Vdd can be used to supply them, if above 2.7V (STM32MP1 only).
  400. *
  401. * Recommended settings for ANASWVDD and EN_BOOSTER:
  402. * - vdda < 2.7V but vdd > 2.7V: ANASWVDD = 1, EN_BOOSTER = 0 (stm32mp1)
  403. * - vdda < 2.7V and vdd < 2.7V: ANASWVDD = 0, EN_BOOSTER = 1
  404. * - vdda >= 2.7V: ANASWVDD = 0, EN_BOOSTER = 0 (default)
  405. */
  406. if (priv->vdda_uv < 2700000) {
  407. if (priv->syscfg && priv->vdd_uv > 2700000) {
  408. ret = regulator_enable(priv->vdd);
  409. if (ret < 0) {
  410. dev_err(dev, "vdd enable failed %d\n", ret);
  411. return ret;
  412. }
  413. ret = regmap_write(priv->syscfg,
  414. STM32MP1_SYSCFG_PMCSETR,
  415. STM32MP1_SYSCFG_ANASWVDD_MASK);
  416. if (ret < 0) {
  417. regulator_disable(priv->vdd);
  418. dev_err(dev, "vdd select failed, %d\n", ret);
  419. return ret;
  420. }
  421. dev_dbg(dev, "analog switches supplied by vdd\n");
  422. return 0;
  423. }
  424. if (priv->booster) {
  425. /*
  426. * This is optional, as this is a trade-off between
  427. * analog performance and power consumption.
  428. */
  429. ret = regulator_enable(priv->booster);
  430. if (ret < 0) {
  431. dev_err(dev, "booster enable failed %d\n", ret);
  432. return ret;
  433. }
  434. dev_dbg(dev, "analog switches supplied by booster\n");
  435. return 0;
  436. }
  437. }
  438. /* Fallback using vdda (default), nothing to do */
  439. dev_dbg(dev, "analog switches supplied by vdda (%d uV)\n",
  440. priv->vdda_uv);
  441. return 0;
  442. }
  443. static void stm32_adc_core_switches_supply_dis(struct stm32_adc_priv *priv)
  444. {
  445. if (priv->vdda_uv < 2700000) {
  446. if (priv->syscfg && priv->vdd_uv > 2700000) {
  447. regmap_write(priv->syscfg, STM32MP1_SYSCFG_PMCCLRR,
  448. STM32MP1_SYSCFG_ANASWVDD_MASK);
  449. regulator_disable(priv->vdd);
  450. return;
  451. }
  452. if (priv->booster)
  453. regulator_disable(priv->booster);
  454. }
  455. }
  456. static int stm32_adc_core_hw_start(struct device *dev)
  457. {
  458. struct stm32_adc_common *common = dev_get_drvdata(dev);
  459. struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
  460. int ret;
  461. ret = regulator_enable(priv->vdda);
  462. if (ret < 0) {
  463. dev_err(dev, "vdda enable failed %d\n", ret);
  464. return ret;
  465. }
  466. ret = regulator_get_voltage(priv->vdda);
  467. if (ret < 0) {
  468. dev_err(dev, "vdda get voltage failed, %d\n", ret);
  469. goto err_vdda_disable;
  470. }
  471. priv->vdda_uv = ret;
  472. ret = stm32_adc_core_switches_supply_en(priv, dev);
  473. if (ret < 0)
  474. goto err_vdda_disable;
  475. ret = regulator_enable(priv->vref);
  476. if (ret < 0) {
  477. dev_err(dev, "vref enable failed\n");
  478. goto err_switches_dis;
  479. }
  480. ret = clk_prepare_enable(priv->bclk);
  481. if (ret < 0) {
  482. dev_err(dev, "bus clk enable failed\n");
  483. goto err_regulator_disable;
  484. }
  485. ret = clk_prepare_enable(priv->aclk);
  486. if (ret < 0) {
  487. dev_err(dev, "adc clk enable failed\n");
  488. goto err_bclk_disable;
  489. }
  490. writel_relaxed(priv->ccr_bak, priv->common.base + priv->cfg->regs->ccr);
  491. return 0;
  492. err_bclk_disable:
  493. clk_disable_unprepare(priv->bclk);
  494. err_regulator_disable:
  495. regulator_disable(priv->vref);
  496. err_switches_dis:
  497. stm32_adc_core_switches_supply_dis(priv);
  498. err_vdda_disable:
  499. regulator_disable(priv->vdda);
  500. return ret;
  501. }
  502. static void stm32_adc_core_hw_stop(struct device *dev)
  503. {
  504. struct stm32_adc_common *common = dev_get_drvdata(dev);
  505. struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
  506. /* Backup CCR that may be lost (depends on power state to achieve) */
  507. priv->ccr_bak = readl_relaxed(priv->common.base + priv->cfg->regs->ccr);
  508. clk_disable_unprepare(priv->aclk);
  509. clk_disable_unprepare(priv->bclk);
  510. regulator_disable(priv->vref);
  511. stm32_adc_core_switches_supply_dis(priv);
  512. regulator_disable(priv->vdda);
  513. }
  514. static int stm32_adc_core_switches_probe(struct device *dev,
  515. struct stm32_adc_priv *priv)
  516. {
  517. struct device_node *np = dev->of_node;
  518. int ret;
  519. /* Analog switches supply can be controlled by syscfg (optional) */
  520. priv->syscfg = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  521. if (IS_ERR(priv->syscfg)) {
  522. ret = PTR_ERR(priv->syscfg);
  523. if (ret != -ENODEV)
  524. return dev_err_probe(dev, ret, "Can't probe syscfg\n");
  525. priv->syscfg = NULL;
  526. }
  527. /* Booster can be used to supply analog switches (optional) */
  528. if (priv->cfg->has_syscfg & HAS_VBOOSTER) {
  529. priv->booster = devm_regulator_get_optional(dev, "booster");
  530. if (IS_ERR(priv->booster)) {
  531. ret = PTR_ERR(priv->booster);
  532. if (ret != -ENODEV)
  533. return dev_err_probe(dev, ret, "can't get booster\n");
  534. priv->booster = NULL;
  535. }
  536. }
  537. /* Vdd can be used to supply analog switches (optional) */
  538. if (priv->cfg->has_syscfg & HAS_ANASWVDD) {
  539. priv->vdd = devm_regulator_get_optional(dev, "vdd");
  540. if (IS_ERR(priv->vdd)) {
  541. ret = PTR_ERR(priv->vdd);
  542. if (ret != -ENODEV)
  543. return dev_err_probe(dev, ret, "can't get vdd\n");
  544. priv->vdd = NULL;
  545. }
  546. }
  547. if (priv->vdd) {
  548. ret = regulator_enable(priv->vdd);
  549. if (ret < 0) {
  550. dev_err(dev, "vdd enable failed %d\n", ret);
  551. return ret;
  552. }
  553. ret = regulator_get_voltage(priv->vdd);
  554. if (ret < 0) {
  555. dev_err(dev, "vdd get voltage failed %d\n", ret);
  556. regulator_disable(priv->vdd);
  557. return ret;
  558. }
  559. priv->vdd_uv = ret;
  560. regulator_disable(priv->vdd);
  561. }
  562. return 0;
  563. }
  564. static int stm32_adc_probe_identification(struct platform_device *pdev,
  565. struct stm32_adc_priv *priv)
  566. {
  567. struct device_node *np = pdev->dev.of_node;
  568. struct device_node *child;
  569. const char *compat;
  570. int ret, count = 0;
  571. u32 id, val;
  572. if (!priv->cfg->ipid)
  573. return 0;
  574. id = FIELD_GET(STM32MP1_IPIDR_MASK,
  575. readl_relaxed(priv->common.base + STM32MP1_ADC_IPDR));
  576. if (id != priv->cfg->ipid) {
  577. dev_err(&pdev->dev, "Unexpected IP version: 0x%x", id);
  578. return -EINVAL;
  579. }
  580. for_each_child_of_node(np, child) {
  581. ret = of_property_read_string(child, "compatible", &compat);
  582. if (ret)
  583. continue;
  584. /* Count child nodes with stm32 adc compatible */
  585. if (strstr(compat, "st,stm32") && strstr(compat, "adc"))
  586. count++;
  587. }
  588. val = readl_relaxed(priv->common.base + STM32MP1_ADC_HWCFGR0);
  589. priv->nb_adc_max = FIELD_GET(STM32MP1_ADCNUM_MASK, val);
  590. if (count > priv->nb_adc_max) {
  591. dev_err(&pdev->dev, "Unexpected child number: %d", count);
  592. return -EINVAL;
  593. }
  594. val = readl_relaxed(priv->common.base + STM32MP1_ADC_VERR);
  595. dev_dbg(&pdev->dev, "ADC version: %lu.%lu\n",
  596. FIELD_GET(STM32MP1_MAJREV_MASK, val),
  597. FIELD_GET(STM32MP1_MINREV_MASK, val));
  598. return 0;
  599. }
  600. static int stm32_adc_probe(struct platform_device *pdev)
  601. {
  602. struct stm32_adc_priv *priv;
  603. struct device *dev = &pdev->dev;
  604. struct device_node *np = pdev->dev.of_node;
  605. struct resource *res;
  606. u32 max_rate;
  607. int ret;
  608. if (!pdev->dev.of_node)
  609. return -ENODEV;
  610. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  611. if (!priv)
  612. return -ENOMEM;
  613. platform_set_drvdata(pdev, &priv->common);
  614. priv->cfg = device_get_match_data(dev);
  615. priv->nb_adc_max = priv->cfg->num_adcs;
  616. spin_lock_init(&priv->common.lock);
  617. priv->common.base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  618. if (IS_ERR(priv->common.base))
  619. return PTR_ERR(priv->common.base);
  620. priv->common.phys_base = res->start;
  621. priv->vdda = devm_regulator_get(&pdev->dev, "vdda");
  622. if (IS_ERR(priv->vdda))
  623. return dev_err_probe(&pdev->dev, PTR_ERR(priv->vdda),
  624. "vdda get failed\n");
  625. priv->vref = devm_regulator_get(&pdev->dev, "vref");
  626. if (IS_ERR(priv->vref))
  627. return dev_err_probe(&pdev->dev, PTR_ERR(priv->vref),
  628. "vref get failed\n");
  629. priv->aclk = devm_clk_get_optional(&pdev->dev, "adc");
  630. if (IS_ERR(priv->aclk))
  631. return dev_err_probe(&pdev->dev, PTR_ERR(priv->aclk),
  632. "Can't get 'adc' clock\n");
  633. priv->bclk = devm_clk_get_optional(&pdev->dev, "bus");
  634. if (IS_ERR(priv->bclk))
  635. return dev_err_probe(&pdev->dev, PTR_ERR(priv->bclk),
  636. "Can't get 'bus' clock\n");
  637. ret = stm32_adc_core_switches_probe(dev, priv);
  638. if (ret)
  639. return ret;
  640. pm_runtime_get_noresume(dev);
  641. pm_runtime_set_active(dev);
  642. pm_runtime_set_autosuspend_delay(dev, STM32_ADC_CORE_SLEEP_DELAY_MS);
  643. pm_runtime_use_autosuspend(dev);
  644. pm_runtime_enable(dev);
  645. ret = stm32_adc_core_hw_start(dev);
  646. if (ret)
  647. goto err_pm_stop;
  648. ret = stm32_adc_probe_identification(pdev, priv);
  649. if (ret < 0)
  650. goto err_hw_stop;
  651. ret = regulator_get_voltage(priv->vref);
  652. if (ret < 0) {
  653. dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
  654. goto err_hw_stop;
  655. }
  656. priv->common.vref_mv = ret / 1000;
  657. dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
  658. ret = of_property_read_u32(pdev->dev.of_node, "st,max-clk-rate-hz",
  659. &max_rate);
  660. if (!ret)
  661. priv->max_clk_rate = min(max_rate, priv->cfg->max_clk_rate_hz);
  662. else
  663. priv->max_clk_rate = priv->cfg->max_clk_rate_hz;
  664. ret = priv->cfg->clk_sel(pdev, priv);
  665. if (ret < 0)
  666. goto err_hw_stop;
  667. ret = stm32_adc_irq_probe(pdev, priv);
  668. if (ret < 0)
  669. goto err_hw_stop;
  670. ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
  671. if (ret < 0) {
  672. dev_err(&pdev->dev, "failed to populate DT children\n");
  673. goto err_irq_remove;
  674. }
  675. pm_runtime_put_autosuspend(dev);
  676. return 0;
  677. err_irq_remove:
  678. stm32_adc_irq_remove(pdev, priv);
  679. err_hw_stop:
  680. stm32_adc_core_hw_stop(dev);
  681. err_pm_stop:
  682. pm_runtime_disable(dev);
  683. pm_runtime_set_suspended(dev);
  684. pm_runtime_put_noidle(dev);
  685. return ret;
  686. }
  687. static void stm32_adc_remove(struct platform_device *pdev)
  688. {
  689. struct stm32_adc_common *common = platform_get_drvdata(pdev);
  690. struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
  691. pm_runtime_get_sync(&pdev->dev);
  692. of_platform_depopulate(&pdev->dev);
  693. stm32_adc_irq_remove(pdev, priv);
  694. stm32_adc_core_hw_stop(&pdev->dev);
  695. pm_runtime_disable(&pdev->dev);
  696. pm_runtime_set_suspended(&pdev->dev);
  697. pm_runtime_put_noidle(&pdev->dev);
  698. }
  699. static int stm32_adc_core_runtime_suspend(struct device *dev)
  700. {
  701. stm32_adc_core_hw_stop(dev);
  702. return 0;
  703. }
  704. static int stm32_adc_core_runtime_resume(struct device *dev)
  705. {
  706. return stm32_adc_core_hw_start(dev);
  707. }
  708. static int stm32_adc_core_runtime_idle(struct device *dev)
  709. {
  710. pm_runtime_mark_last_busy(dev);
  711. return 0;
  712. }
  713. static DEFINE_RUNTIME_DEV_PM_OPS(stm32_adc_core_pm_ops,
  714. stm32_adc_core_runtime_suspend,
  715. stm32_adc_core_runtime_resume,
  716. stm32_adc_core_runtime_idle);
  717. static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg = {
  718. .regs = &stm32f4_adc_common_regs,
  719. .clk_sel = stm32f4_adc_clk_sel,
  720. .max_clk_rate_hz = 36000000,
  721. .num_irqs = 1,
  722. .num_adcs = 3,
  723. };
  724. static const struct stm32_adc_priv_cfg stm32h7_adc_priv_cfg = {
  725. .regs = &stm32h7_adc_common_regs,
  726. .clk_sel = stm32h7_adc_clk_sel,
  727. .max_clk_rate_hz = 36000000,
  728. .has_syscfg = HAS_VBOOSTER,
  729. .num_irqs = 1,
  730. .num_adcs = 2,
  731. };
  732. static const struct stm32_adc_priv_cfg stm32mp1_adc_priv_cfg = {
  733. .regs = &stm32h7_adc_common_regs,
  734. .clk_sel = stm32h7_adc_clk_sel,
  735. .max_clk_rate_hz = 36000000,
  736. .has_syscfg = HAS_VBOOSTER | HAS_ANASWVDD,
  737. .ipid = STM32MP15_IPIDR_NUMBER,
  738. .num_irqs = 2,
  739. };
  740. static const struct stm32_adc_priv_cfg stm32mp13_adc_priv_cfg = {
  741. .regs = &stm32mp13_adc_common_regs,
  742. .clk_sel = stm32h7_adc_clk_sel,
  743. .max_clk_rate_hz = 75 * HZ_PER_MHZ,
  744. .ipid = STM32MP13_IPIDR_NUMBER,
  745. .num_irqs = 1,
  746. };
  747. static const struct of_device_id stm32_adc_of_match[] = {
  748. {
  749. .compatible = "st,stm32f4-adc-core",
  750. .data = (void *)&stm32f4_adc_priv_cfg
  751. }, {
  752. .compatible = "st,stm32h7-adc-core",
  753. .data = (void *)&stm32h7_adc_priv_cfg
  754. }, {
  755. .compatible = "st,stm32mp1-adc-core",
  756. .data = (void *)&stm32mp1_adc_priv_cfg
  757. }, {
  758. .compatible = "st,stm32mp13-adc-core",
  759. .data = (void *)&stm32mp13_adc_priv_cfg
  760. }, {
  761. },
  762. };
  763. MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
  764. static struct platform_driver stm32_adc_driver = {
  765. .probe = stm32_adc_probe,
  766. .remove = stm32_adc_remove,
  767. .driver = {
  768. .name = "stm32-adc-core",
  769. .of_match_table = stm32_adc_of_match,
  770. .pm = pm_ptr(&stm32_adc_core_pm_ops),
  771. },
  772. };
  773. module_platform_driver(stm32_adc_driver);
  774. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  775. MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
  776. MODULE_LICENSE("GPL v2");
  777. MODULE_ALIAS("platform:stm32-adc-core");