stm32-adc-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 36000000
  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. if (!rate) {
  124. dev_err(&pdev->dev, "Invalid clock rate: 0\n");
  125. return -EINVAL;
  126. }
  127. for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
  128. if ((rate / stm32f4_pclk_div[i]) <= STM32F4_ADC_MAX_CLK_RATE)
  129. break;
  130. }
  131. if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
  132. dev_err(&pdev->dev, "adc clk selection failed\n");
  133. return -EINVAL;
  134. }
  135. priv->common.rate = rate / stm32f4_pclk_div[i];
  136. val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
  137. val &= ~STM32F4_ADC_ADCPRE_MASK;
  138. val |= i << STM32F4_ADC_ADCPRE_SHIFT;
  139. writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
  140. dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
  141. priv->common.rate / 1000);
  142. return 0;
  143. }
  144. /**
  145. * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
  146. * @ckmode: ADC clock mode, Async or sync with prescaler.
  147. * @presc: prescaler bitfield for async clock mode
  148. * @div: prescaler division ratio
  149. */
  150. struct stm32h7_adc_ck_spec {
  151. u32 ckmode;
  152. u32 presc;
  153. int div;
  154. };
  155. static const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec[] = {
  156. /* 00: CK_ADC[1..3]: Asynchronous clock modes */
  157. { 0, 0, 1 },
  158. { 0, 1, 2 },
  159. { 0, 2, 4 },
  160. { 0, 3, 6 },
  161. { 0, 4, 8 },
  162. { 0, 5, 10 },
  163. { 0, 6, 12 },
  164. { 0, 7, 16 },
  165. { 0, 8, 32 },
  166. { 0, 9, 64 },
  167. { 0, 10, 128 },
  168. { 0, 11, 256 },
  169. /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
  170. { 1, 0, 1 },
  171. { 2, 0, 2 },
  172. { 3, 0, 4 },
  173. };
  174. static int stm32h7_adc_clk_sel(struct platform_device *pdev,
  175. struct stm32_adc_priv *priv)
  176. {
  177. u32 ckmode, presc, val;
  178. unsigned long rate;
  179. int i, div;
  180. /* stm32h7 bus clock is common for all ADC instances (mandatory) */
  181. if (!priv->bclk) {
  182. dev_err(&pdev->dev, "No 'bus' clock found\n");
  183. return -ENOENT;
  184. }
  185. /*
  186. * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
  187. * So, choice is to have bus clock mandatory and adc clock optional.
  188. * If optional 'adc' clock has been found, then try to use it first.
  189. */
  190. if (priv->aclk) {
  191. /*
  192. * Asynchronous clock modes (e.g. ckmode == 0)
  193. * From spec: PLL output musn't exceed max rate
  194. */
  195. rate = clk_get_rate(priv->aclk);
  196. if (!rate) {
  197. dev_err(&pdev->dev, "Invalid adc clock rate: 0\n");
  198. return -EINVAL;
  199. }
  200. for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
  201. ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
  202. presc = stm32h7_adc_ckmodes_spec[i].presc;
  203. div = stm32h7_adc_ckmodes_spec[i].div;
  204. if (ckmode)
  205. continue;
  206. if ((rate / div) <= STM32H7_ADC_MAX_CLK_RATE)
  207. goto out;
  208. }
  209. }
  210. /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
  211. rate = clk_get_rate(priv->bclk);
  212. if (!rate) {
  213. dev_err(&pdev->dev, "Invalid bus clock rate: 0\n");
  214. return -EINVAL;
  215. }
  216. for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
  217. ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
  218. presc = stm32h7_adc_ckmodes_spec[i].presc;
  219. div = stm32h7_adc_ckmodes_spec[i].div;
  220. if (!ckmode)
  221. continue;
  222. if ((rate / div) <= STM32H7_ADC_MAX_CLK_RATE)
  223. goto out;
  224. }
  225. dev_err(&pdev->dev, "adc clk selection failed\n");
  226. return -EINVAL;
  227. out:
  228. /* rate used later by each ADC instance to control BOOST mode */
  229. priv->common.rate = rate / div;
  230. /* Set common clock mode and prescaler */
  231. val = readl_relaxed(priv->common.base + STM32H7_ADC_CCR);
  232. val &= ~(STM32H7_CKMODE_MASK | STM32H7_PRESC_MASK);
  233. val |= ckmode << STM32H7_CKMODE_SHIFT;
  234. val |= presc << STM32H7_PRESC_SHIFT;
  235. writel_relaxed(val, priv->common.base + STM32H7_ADC_CCR);
  236. dev_dbg(&pdev->dev, "Using %s clock/%d source at %ld kHz\n",
  237. ckmode ? "bus" : "adc", div, priv->common.rate / 1000);
  238. return 0;
  239. }
  240. /* STM32F4 common registers definitions */
  241. static const struct stm32_adc_common_regs stm32f4_adc_common_regs = {
  242. .csr = STM32F4_ADC_CSR,
  243. .eoc1_msk = STM32F4_EOC1,
  244. .eoc2_msk = STM32F4_EOC2,
  245. .eoc3_msk = STM32F4_EOC3,
  246. };
  247. /* STM32H7 common registers definitions */
  248. static const struct stm32_adc_common_regs stm32h7_adc_common_regs = {
  249. .csr = STM32H7_ADC_CSR,
  250. .eoc1_msk = STM32H7_EOC_MST,
  251. .eoc2_msk = STM32H7_EOC_SLV,
  252. };
  253. /* ADC common interrupt for all instances */
  254. static void stm32_adc_irq_handler(struct irq_desc *desc)
  255. {
  256. struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
  257. struct irq_chip *chip = irq_desc_get_chip(desc);
  258. u32 status;
  259. chained_irq_enter(chip, desc);
  260. status = readl_relaxed(priv->common.base + priv->cfg->regs->csr);
  261. if (status & priv->cfg->regs->eoc1_msk)
  262. generic_handle_irq(irq_find_mapping(priv->domain, 0));
  263. if (status & priv->cfg->regs->eoc2_msk)
  264. generic_handle_irq(irq_find_mapping(priv->domain, 1));
  265. if (status & priv->cfg->regs->eoc3_msk)
  266. generic_handle_irq(irq_find_mapping(priv->domain, 2));
  267. chained_irq_exit(chip, desc);
  268. };
  269. static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
  270. irq_hw_number_t hwirq)
  271. {
  272. irq_set_chip_data(irq, d->host_data);
  273. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
  274. return 0;
  275. }
  276. static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
  277. {
  278. irq_set_chip_and_handler(irq, NULL, NULL);
  279. irq_set_chip_data(irq, NULL);
  280. }
  281. static const struct irq_domain_ops stm32_adc_domain_ops = {
  282. .map = stm32_adc_domain_map,
  283. .unmap = stm32_adc_domain_unmap,
  284. .xlate = irq_domain_xlate_onecell,
  285. };
  286. static int stm32_adc_irq_probe(struct platform_device *pdev,
  287. struct stm32_adc_priv *priv)
  288. {
  289. struct device_node *np = pdev->dev.of_node;
  290. priv->irq = platform_get_irq(pdev, 0);
  291. if (priv->irq < 0) {
  292. dev_err(&pdev->dev, "failed to get irq\n");
  293. return priv->irq;
  294. }
  295. priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
  296. &stm32_adc_domain_ops,
  297. priv);
  298. if (!priv->domain) {
  299. dev_err(&pdev->dev, "Failed to add irq domain\n");
  300. return -ENOMEM;
  301. }
  302. irq_set_chained_handler(priv->irq, stm32_adc_irq_handler);
  303. irq_set_handler_data(priv->irq, priv);
  304. return 0;
  305. }
  306. static void stm32_adc_irq_remove(struct platform_device *pdev,
  307. struct stm32_adc_priv *priv)
  308. {
  309. int hwirq;
  310. for (hwirq = 0; hwirq < STM32_ADC_MAX_ADCS; hwirq++)
  311. irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
  312. irq_domain_remove(priv->domain);
  313. irq_set_chained_handler(priv->irq, NULL);
  314. }
  315. static int stm32_adc_probe(struct platform_device *pdev)
  316. {
  317. struct stm32_adc_priv *priv;
  318. struct device *dev = &pdev->dev;
  319. struct device_node *np = pdev->dev.of_node;
  320. struct resource *res;
  321. int ret;
  322. if (!pdev->dev.of_node)
  323. return -ENODEV;
  324. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  325. if (!priv)
  326. return -ENOMEM;
  327. priv->cfg = (const struct stm32_adc_priv_cfg *)
  328. of_match_device(dev->driver->of_match_table, dev)->data;
  329. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  330. priv->common.base = devm_ioremap_resource(&pdev->dev, res);
  331. if (IS_ERR(priv->common.base))
  332. return PTR_ERR(priv->common.base);
  333. priv->common.phys_base = res->start;
  334. priv->vref = devm_regulator_get(&pdev->dev, "vref");
  335. if (IS_ERR(priv->vref)) {
  336. ret = PTR_ERR(priv->vref);
  337. dev_err(&pdev->dev, "vref get failed, %d\n", ret);
  338. return ret;
  339. }
  340. ret = regulator_enable(priv->vref);
  341. if (ret < 0) {
  342. dev_err(&pdev->dev, "vref enable failed\n");
  343. return ret;
  344. }
  345. ret = regulator_get_voltage(priv->vref);
  346. if (ret < 0) {
  347. dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
  348. goto err_regulator_disable;
  349. }
  350. priv->common.vref_mv = ret / 1000;
  351. dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
  352. priv->aclk = devm_clk_get(&pdev->dev, "adc");
  353. if (IS_ERR(priv->aclk)) {
  354. ret = PTR_ERR(priv->aclk);
  355. if (ret == -ENOENT) {
  356. priv->aclk = NULL;
  357. } else {
  358. dev_err(&pdev->dev, "Can't get 'adc' clock\n");
  359. goto err_regulator_disable;
  360. }
  361. }
  362. if (priv->aclk) {
  363. ret = clk_prepare_enable(priv->aclk);
  364. if (ret < 0) {
  365. dev_err(&pdev->dev, "adc clk enable failed\n");
  366. goto err_regulator_disable;
  367. }
  368. }
  369. priv->bclk = devm_clk_get(&pdev->dev, "bus");
  370. if (IS_ERR(priv->bclk)) {
  371. ret = PTR_ERR(priv->bclk);
  372. if (ret == -ENOENT) {
  373. priv->bclk = NULL;
  374. } else {
  375. dev_err(&pdev->dev, "Can't get 'bus' clock\n");
  376. goto err_aclk_disable;
  377. }
  378. }
  379. if (priv->bclk) {
  380. ret = clk_prepare_enable(priv->bclk);
  381. if (ret < 0) {
  382. dev_err(&pdev->dev, "adc clk enable failed\n");
  383. goto err_aclk_disable;
  384. }
  385. }
  386. ret = priv->cfg->clk_sel(pdev, priv);
  387. if (ret < 0)
  388. goto err_bclk_disable;
  389. ret = stm32_adc_irq_probe(pdev, priv);
  390. if (ret < 0)
  391. goto err_bclk_disable;
  392. platform_set_drvdata(pdev, &priv->common);
  393. ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
  394. if (ret < 0) {
  395. dev_err(&pdev->dev, "failed to populate DT children\n");
  396. goto err_irq_remove;
  397. }
  398. return 0;
  399. err_irq_remove:
  400. stm32_adc_irq_remove(pdev, priv);
  401. err_bclk_disable:
  402. if (priv->bclk)
  403. clk_disable_unprepare(priv->bclk);
  404. err_aclk_disable:
  405. if (priv->aclk)
  406. clk_disable_unprepare(priv->aclk);
  407. err_regulator_disable:
  408. regulator_disable(priv->vref);
  409. return ret;
  410. }
  411. static int stm32_adc_remove(struct platform_device *pdev)
  412. {
  413. struct stm32_adc_common *common = platform_get_drvdata(pdev);
  414. struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
  415. of_platform_depopulate(&pdev->dev);
  416. stm32_adc_irq_remove(pdev, priv);
  417. if (priv->bclk)
  418. clk_disable_unprepare(priv->bclk);
  419. if (priv->aclk)
  420. clk_disable_unprepare(priv->aclk);
  421. regulator_disable(priv->vref);
  422. return 0;
  423. }
  424. static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg = {
  425. .regs = &stm32f4_adc_common_regs,
  426. .clk_sel = stm32f4_adc_clk_sel,
  427. };
  428. static const struct stm32_adc_priv_cfg stm32h7_adc_priv_cfg = {
  429. .regs = &stm32h7_adc_common_regs,
  430. .clk_sel = stm32h7_adc_clk_sel,
  431. };
  432. static const struct of_device_id stm32_adc_of_match[] = {
  433. {
  434. .compatible = "st,stm32f4-adc-core",
  435. .data = (void *)&stm32f4_adc_priv_cfg
  436. }, {
  437. .compatible = "st,stm32h7-adc-core",
  438. .data = (void *)&stm32h7_adc_priv_cfg
  439. }, {
  440. },
  441. };
  442. MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
  443. static struct platform_driver stm32_adc_driver = {
  444. .probe = stm32_adc_probe,
  445. .remove = stm32_adc_remove,
  446. .driver = {
  447. .name = "stm32-adc-core",
  448. .of_match_table = stm32_adc_of_match,
  449. },
  450. };
  451. module_platform_driver(stm32_adc_driver);
  452. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  453. MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
  454. MODULE_LICENSE("GPL v2");
  455. MODULE_ALIAS("platform:stm32-adc-core");