stm32-adc-core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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/clk.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irqchip/chained_irq.h>
  14. #include <linux/irqdesc.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/module.h>
  17. #include <linux/of_device.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/slab.h>
  20. #include "stm32-adc-core.h"
  21. /**
  22. * stm32_adc_common_regs - stm32 common registers, compatible dependent data
  23. * @csr: common status register offset
  24. * @eoc1: adc1 end of conversion flag in @csr
  25. * @eoc2: adc2 end of conversion flag in @csr
  26. * @eoc3: adc3 end of conversion flag in @csr
  27. * @ier: interrupt enable register offset for each adc
  28. * @eocie_msk: end of conversion interrupt enable mask in @ier
  29. */
  30. struct stm32_adc_common_regs {
  31. u32 csr;
  32. u32 eoc1_msk;
  33. u32 eoc2_msk;
  34. u32 eoc3_msk;
  35. u32 ier;
  36. u32 eocie_msk;
  37. };
  38. struct stm32_adc_priv;
  39. /**
  40. * stm32_adc_priv_cfg - stm32 core compatible configuration data
  41. * @regs: common registers for all instances
  42. * @clk_sel: clock selection routine
  43. * @max_clk_rate_hz: maximum analog clock rate (Hz, from datasheet)
  44. */
  45. struct stm32_adc_priv_cfg {
  46. const struct stm32_adc_common_regs *regs;
  47. int (*clk_sel)(struct platform_device *, struct stm32_adc_priv *);
  48. u32 max_clk_rate_hz;
  49. };
  50. /**
  51. * struct stm32_adc_priv - stm32 ADC core private data
  52. * @irq: irq(s) for ADC block
  53. * @domain: irq domain reference
  54. * @aclk: clock reference for the analog circuitry
  55. * @bclk: bus clock common for all ADCs, depends on part used
  56. * @vref: regulator reference
  57. * @cfg: compatible configuration data
  58. * @common: common data for all ADC instances
  59. */
  60. struct stm32_adc_priv {
  61. int irq[STM32_ADC_MAX_ADCS];
  62. struct irq_domain *domain;
  63. struct clk *aclk;
  64. struct clk *bclk;
  65. struct regulator *vref;
  66. const struct stm32_adc_priv_cfg *cfg;
  67. struct stm32_adc_common common;
  68. };
  69. static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
  70. {
  71. return container_of(com, struct stm32_adc_priv, common);
  72. }
  73. /* STM32F4 ADC internal common clock prescaler division ratios */
  74. static int stm32f4_pclk_div[] = {2, 4, 6, 8};
  75. /**
  76. * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
  77. * @priv: stm32 ADC core private data
  78. * Select clock prescaler used for analog conversions, before using ADC.
  79. */
  80. static int stm32f4_adc_clk_sel(struct platform_device *pdev,
  81. struct stm32_adc_priv *priv)
  82. {
  83. unsigned long rate;
  84. u32 val;
  85. int i;
  86. /* stm32f4 has one clk input for analog (mandatory), enforce it here */
  87. if (!priv->aclk) {
  88. dev_err(&pdev->dev, "No 'adc' clock found\n");
  89. return -ENOENT;
  90. }
  91. rate = clk_get_rate(priv->aclk);
  92. if (!rate) {
  93. dev_err(&pdev->dev, "Invalid clock rate: 0\n");
  94. return -EINVAL;
  95. }
  96. for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
  97. if ((rate / stm32f4_pclk_div[i]) <= priv->cfg->max_clk_rate_hz)
  98. break;
  99. }
  100. if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
  101. dev_err(&pdev->dev, "adc clk selection failed\n");
  102. return -EINVAL;
  103. }
  104. priv->common.rate = rate / stm32f4_pclk_div[i];
  105. val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
  106. val &= ~STM32F4_ADC_ADCPRE_MASK;
  107. val |= i << STM32F4_ADC_ADCPRE_SHIFT;
  108. writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
  109. dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
  110. priv->common.rate / 1000);
  111. return 0;
  112. }
  113. /**
  114. * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
  115. * @ckmode: ADC clock mode, Async or sync with prescaler.
  116. * @presc: prescaler bitfield for async clock mode
  117. * @div: prescaler division ratio
  118. */
  119. struct stm32h7_adc_ck_spec {
  120. u32 ckmode;
  121. u32 presc;
  122. int div;
  123. };
  124. static const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec[] = {
  125. /* 00: CK_ADC[1..3]: Asynchronous clock modes */
  126. { 0, 0, 1 },
  127. { 0, 1, 2 },
  128. { 0, 2, 4 },
  129. { 0, 3, 6 },
  130. { 0, 4, 8 },
  131. { 0, 5, 10 },
  132. { 0, 6, 12 },
  133. { 0, 7, 16 },
  134. { 0, 8, 32 },
  135. { 0, 9, 64 },
  136. { 0, 10, 128 },
  137. { 0, 11, 256 },
  138. /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
  139. { 1, 0, 1 },
  140. { 2, 0, 2 },
  141. { 3, 0, 4 },
  142. };
  143. static int stm32h7_adc_clk_sel(struct platform_device *pdev,
  144. struct stm32_adc_priv *priv)
  145. {
  146. u32 ckmode, presc, val;
  147. unsigned long rate;
  148. int i, div;
  149. /* stm32h7 bus clock is common for all ADC instances (mandatory) */
  150. if (!priv->bclk) {
  151. dev_err(&pdev->dev, "No 'bus' clock found\n");
  152. return -ENOENT;
  153. }
  154. /*
  155. * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
  156. * So, choice is to have bus clock mandatory and adc clock optional.
  157. * If optional 'adc' clock has been found, then try to use it first.
  158. */
  159. if (priv->aclk) {
  160. /*
  161. * Asynchronous clock modes (e.g. ckmode == 0)
  162. * From spec: PLL output musn't exceed max rate
  163. */
  164. rate = clk_get_rate(priv->aclk);
  165. if (!rate) {
  166. dev_err(&pdev->dev, "Invalid adc clock rate: 0\n");
  167. return -EINVAL;
  168. }
  169. for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
  170. ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
  171. presc = stm32h7_adc_ckmodes_spec[i].presc;
  172. div = stm32h7_adc_ckmodes_spec[i].div;
  173. if (ckmode)
  174. continue;
  175. if ((rate / div) <= priv->cfg->max_clk_rate_hz)
  176. goto out;
  177. }
  178. }
  179. /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
  180. rate = clk_get_rate(priv->bclk);
  181. if (!rate) {
  182. dev_err(&pdev->dev, "Invalid bus clock rate: 0\n");
  183. return -EINVAL;
  184. }
  185. for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
  186. ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
  187. presc = stm32h7_adc_ckmodes_spec[i].presc;
  188. div = stm32h7_adc_ckmodes_spec[i].div;
  189. if (!ckmode)
  190. continue;
  191. if ((rate / div) <= priv->cfg->max_clk_rate_hz)
  192. goto out;
  193. }
  194. dev_err(&pdev->dev, "adc clk selection failed\n");
  195. return -EINVAL;
  196. out:
  197. /* rate used later by each ADC instance to control BOOST mode */
  198. priv->common.rate = rate / div;
  199. /* Set common clock mode and prescaler */
  200. val = readl_relaxed(priv->common.base + STM32H7_ADC_CCR);
  201. val &= ~(STM32H7_CKMODE_MASK | STM32H7_PRESC_MASK);
  202. val |= ckmode << STM32H7_CKMODE_SHIFT;
  203. val |= presc << STM32H7_PRESC_SHIFT;
  204. writel_relaxed(val, priv->common.base + STM32H7_ADC_CCR);
  205. dev_dbg(&pdev->dev, "Using %s clock/%d source at %ld kHz\n",
  206. ckmode ? "bus" : "adc", div, priv->common.rate / 1000);
  207. return 0;
  208. }
  209. /* STM32F4 common registers definitions */
  210. static const struct stm32_adc_common_regs stm32f4_adc_common_regs = {
  211. .csr = STM32F4_ADC_CSR,
  212. .eoc1_msk = STM32F4_EOC1,
  213. .eoc2_msk = STM32F4_EOC2,
  214. .eoc3_msk = STM32F4_EOC3,
  215. .ier = STM32F4_ADC_CR1,
  216. .eocie_msk = STM32F4_EOCIE,
  217. };
  218. /* STM32H7 common registers definitions */
  219. static const struct stm32_adc_common_regs stm32h7_adc_common_regs = {
  220. .csr = STM32H7_ADC_CSR,
  221. .eoc1_msk = STM32H7_EOC_MST,
  222. .eoc2_msk = STM32H7_EOC_SLV,
  223. .ier = STM32H7_ADC_IER,
  224. .eocie_msk = STM32H7_EOCIE,
  225. };
  226. static const unsigned int stm32_adc_offset[STM32_ADC_MAX_ADCS] = {
  227. 0, STM32_ADC_OFFSET, STM32_ADC_OFFSET * 2,
  228. };
  229. static unsigned int stm32_adc_eoc_enabled(struct stm32_adc_priv *priv,
  230. unsigned int adc)
  231. {
  232. u32 ier, offset = stm32_adc_offset[adc];
  233. ier = readl_relaxed(priv->common.base + offset + priv->cfg->regs->ier);
  234. return ier & priv->cfg->regs->eocie_msk;
  235. }
  236. /* ADC common interrupt for all instances */
  237. static void stm32_adc_irq_handler(struct irq_desc *desc)
  238. {
  239. struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
  240. struct irq_chip *chip = irq_desc_get_chip(desc);
  241. u32 status;
  242. chained_irq_enter(chip, desc);
  243. status = readl_relaxed(priv->common.base + priv->cfg->regs->csr);
  244. /*
  245. * End of conversion may be handled by using IRQ or DMA. There may be a
  246. * race here when two conversions complete at the same time on several
  247. * ADCs. EOC may be read 'set' for several ADCs, with:
  248. * - an ADC configured to use DMA (EOC triggers the DMA request, and
  249. * is then automatically cleared by DR read in hardware)
  250. * - an ADC configured to use IRQs (EOCIE bit is set. The handler must
  251. * be called in this case)
  252. * So both EOC status bit in CSR and EOCIE control bit must be checked
  253. * before invoking the interrupt handler (e.g. call ISR only for
  254. * IRQ-enabled ADCs).
  255. */
  256. if (status & priv->cfg->regs->eoc1_msk &&
  257. stm32_adc_eoc_enabled(priv, 0))
  258. generic_handle_irq(irq_find_mapping(priv->domain, 0));
  259. if (status & priv->cfg->regs->eoc2_msk &&
  260. stm32_adc_eoc_enabled(priv, 1))
  261. generic_handle_irq(irq_find_mapping(priv->domain, 1));
  262. if (status & priv->cfg->regs->eoc3_msk &&
  263. stm32_adc_eoc_enabled(priv, 2))
  264. generic_handle_irq(irq_find_mapping(priv->domain, 2));
  265. chained_irq_exit(chip, desc);
  266. };
  267. static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
  268. irq_hw_number_t hwirq)
  269. {
  270. irq_set_chip_data(irq, d->host_data);
  271. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
  272. return 0;
  273. }
  274. static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
  275. {
  276. irq_set_chip_and_handler(irq, NULL, NULL);
  277. irq_set_chip_data(irq, NULL);
  278. }
  279. static const struct irq_domain_ops stm32_adc_domain_ops = {
  280. .map = stm32_adc_domain_map,
  281. .unmap = stm32_adc_domain_unmap,
  282. .xlate = irq_domain_xlate_onecell,
  283. };
  284. static int stm32_adc_irq_probe(struct platform_device *pdev,
  285. struct stm32_adc_priv *priv)
  286. {
  287. struct device_node *np = pdev->dev.of_node;
  288. unsigned int i;
  289. for (i = 0; i < STM32_ADC_MAX_ADCS; i++) {
  290. priv->irq[i] = platform_get_irq(pdev, i);
  291. if (priv->irq[i] < 0) {
  292. /*
  293. * At least one interrupt must be provided, make others
  294. * optional:
  295. * - stm32f4/h7 shares a common interrupt.
  296. * - stm32mp1, has one line per ADC (either for ADC1,
  297. * ADC2 or both).
  298. */
  299. if (i && priv->irq[i] == -ENXIO)
  300. continue;
  301. dev_err(&pdev->dev, "failed to get irq\n");
  302. return priv->irq[i];
  303. }
  304. }
  305. priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
  306. &stm32_adc_domain_ops,
  307. priv);
  308. if (!priv->domain) {
  309. dev_err(&pdev->dev, "Failed to add irq domain\n");
  310. return -ENOMEM;
  311. }
  312. for (i = 0; i < STM32_ADC_MAX_ADCS; i++) {
  313. if (priv->irq[i] < 0)
  314. continue;
  315. irq_set_chained_handler(priv->irq[i], stm32_adc_irq_handler);
  316. irq_set_handler_data(priv->irq[i], priv);
  317. }
  318. return 0;
  319. }
  320. static void stm32_adc_irq_remove(struct platform_device *pdev,
  321. struct stm32_adc_priv *priv)
  322. {
  323. int hwirq;
  324. unsigned int i;
  325. for (hwirq = 0; hwirq < STM32_ADC_MAX_ADCS; hwirq++)
  326. irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
  327. irq_domain_remove(priv->domain);
  328. for (i = 0; i < STM32_ADC_MAX_ADCS; i++) {
  329. if (priv->irq[i] < 0)
  330. continue;
  331. irq_set_chained_handler(priv->irq[i], NULL);
  332. }
  333. }
  334. static int stm32_adc_probe(struct platform_device *pdev)
  335. {
  336. struct stm32_adc_priv *priv;
  337. struct device *dev = &pdev->dev;
  338. struct device_node *np = pdev->dev.of_node;
  339. struct resource *res;
  340. int ret;
  341. if (!pdev->dev.of_node)
  342. return -ENODEV;
  343. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  344. if (!priv)
  345. return -ENOMEM;
  346. priv->cfg = (const struct stm32_adc_priv_cfg *)
  347. of_match_device(dev->driver->of_match_table, dev)->data;
  348. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  349. priv->common.base = devm_ioremap_resource(&pdev->dev, res);
  350. if (IS_ERR(priv->common.base))
  351. return PTR_ERR(priv->common.base);
  352. priv->common.phys_base = res->start;
  353. priv->vref = devm_regulator_get(&pdev->dev, "vref");
  354. if (IS_ERR(priv->vref)) {
  355. ret = PTR_ERR(priv->vref);
  356. dev_err(&pdev->dev, "vref get failed, %d\n", ret);
  357. return ret;
  358. }
  359. ret = regulator_enable(priv->vref);
  360. if (ret < 0) {
  361. dev_err(&pdev->dev, "vref enable failed\n");
  362. return ret;
  363. }
  364. ret = regulator_get_voltage(priv->vref);
  365. if (ret < 0) {
  366. dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
  367. goto err_regulator_disable;
  368. }
  369. priv->common.vref_mv = ret / 1000;
  370. dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
  371. priv->aclk = devm_clk_get(&pdev->dev, "adc");
  372. if (IS_ERR(priv->aclk)) {
  373. ret = PTR_ERR(priv->aclk);
  374. if (ret == -ENOENT) {
  375. priv->aclk = NULL;
  376. } else {
  377. dev_err(&pdev->dev, "Can't get 'adc' clock\n");
  378. goto err_regulator_disable;
  379. }
  380. }
  381. if (priv->aclk) {
  382. ret = clk_prepare_enable(priv->aclk);
  383. if (ret < 0) {
  384. dev_err(&pdev->dev, "adc clk enable failed\n");
  385. goto err_regulator_disable;
  386. }
  387. }
  388. priv->bclk = devm_clk_get(&pdev->dev, "bus");
  389. if (IS_ERR(priv->bclk)) {
  390. ret = PTR_ERR(priv->bclk);
  391. if (ret == -ENOENT) {
  392. priv->bclk = NULL;
  393. } else {
  394. dev_err(&pdev->dev, "Can't get 'bus' clock\n");
  395. goto err_aclk_disable;
  396. }
  397. }
  398. if (priv->bclk) {
  399. ret = clk_prepare_enable(priv->bclk);
  400. if (ret < 0) {
  401. dev_err(&pdev->dev, "adc clk enable failed\n");
  402. goto err_aclk_disable;
  403. }
  404. }
  405. ret = priv->cfg->clk_sel(pdev, priv);
  406. if (ret < 0)
  407. goto err_bclk_disable;
  408. ret = stm32_adc_irq_probe(pdev, priv);
  409. if (ret < 0)
  410. goto err_bclk_disable;
  411. platform_set_drvdata(pdev, &priv->common);
  412. ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
  413. if (ret < 0) {
  414. dev_err(&pdev->dev, "failed to populate DT children\n");
  415. goto err_irq_remove;
  416. }
  417. return 0;
  418. err_irq_remove:
  419. stm32_adc_irq_remove(pdev, priv);
  420. err_bclk_disable:
  421. if (priv->bclk)
  422. clk_disable_unprepare(priv->bclk);
  423. err_aclk_disable:
  424. if (priv->aclk)
  425. clk_disable_unprepare(priv->aclk);
  426. err_regulator_disable:
  427. regulator_disable(priv->vref);
  428. return ret;
  429. }
  430. static int stm32_adc_remove(struct platform_device *pdev)
  431. {
  432. struct stm32_adc_common *common = platform_get_drvdata(pdev);
  433. struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
  434. of_platform_depopulate(&pdev->dev);
  435. stm32_adc_irq_remove(pdev, priv);
  436. if (priv->bclk)
  437. clk_disable_unprepare(priv->bclk);
  438. if (priv->aclk)
  439. clk_disable_unprepare(priv->aclk);
  440. regulator_disable(priv->vref);
  441. return 0;
  442. }
  443. static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg = {
  444. .regs = &stm32f4_adc_common_regs,
  445. .clk_sel = stm32f4_adc_clk_sel,
  446. .max_clk_rate_hz = 36000000,
  447. };
  448. static const struct stm32_adc_priv_cfg stm32h7_adc_priv_cfg = {
  449. .regs = &stm32h7_adc_common_regs,
  450. .clk_sel = stm32h7_adc_clk_sel,
  451. .max_clk_rate_hz = 36000000,
  452. };
  453. static const struct stm32_adc_priv_cfg stm32mp1_adc_priv_cfg = {
  454. .regs = &stm32h7_adc_common_regs,
  455. .clk_sel = stm32h7_adc_clk_sel,
  456. .max_clk_rate_hz = 40000000,
  457. };
  458. static const struct of_device_id stm32_adc_of_match[] = {
  459. {
  460. .compatible = "st,stm32f4-adc-core",
  461. .data = (void *)&stm32f4_adc_priv_cfg
  462. }, {
  463. .compatible = "st,stm32h7-adc-core",
  464. .data = (void *)&stm32h7_adc_priv_cfg
  465. }, {
  466. .compatible = "st,stm32mp1-adc-core",
  467. .data = (void *)&stm32mp1_adc_priv_cfg
  468. }, {
  469. },
  470. };
  471. MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
  472. static struct platform_driver stm32_adc_driver = {
  473. .probe = stm32_adc_probe,
  474. .remove = stm32_adc_remove,
  475. .driver = {
  476. .name = "stm32-adc-core",
  477. .of_match_table = stm32_adc_of_match,
  478. },
  479. };
  480. module_platform_driver(stm32_adc_driver);
  481. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  482. MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
  483. MODULE_LICENSE("GPL v2");
  484. MODULE_ALIAS("platform:stm32-adc-core");