stm32-adc-core.c 14 KB

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