stm32-adc-core.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. /**
  46. * stm32_adc_common_regs - stm32 common registers, compatible dependent data
  47. * @csr: common status register offset
  48. * @eoc1: adc1 end of conversion flag in @csr
  49. * @eoc2: adc2 end of conversion flag in @csr
  50. * @eoc3: adc3 end of conversion flag in @csr
  51. */
  52. struct stm32_adc_common_regs {
  53. u32 csr;
  54. u32 eoc1_msk;
  55. u32 eoc2_msk;
  56. u32 eoc3_msk;
  57. };
  58. struct stm32_adc_priv;
  59. /**
  60. * stm32_adc_priv_cfg - stm32 core compatible configuration data
  61. * @regs: common registers for all instances
  62. * @clk_sel: clock selection routine
  63. */
  64. struct stm32_adc_priv_cfg {
  65. const struct stm32_adc_common_regs *regs;
  66. int (*clk_sel)(struct platform_device *, struct stm32_adc_priv *);
  67. };
  68. /**
  69. * struct stm32_adc_priv - stm32 ADC core private data
  70. * @irq: irq for ADC block
  71. * @domain: irq domain reference
  72. * @aclk: clock reference for the analog circuitry
  73. * @vref: regulator reference
  74. * @cfg: compatible configuration data
  75. * @common: common data for all ADC instances
  76. */
  77. struct stm32_adc_priv {
  78. int irq;
  79. struct irq_domain *domain;
  80. struct clk *aclk;
  81. struct regulator *vref;
  82. const struct stm32_adc_priv_cfg *cfg;
  83. struct stm32_adc_common common;
  84. };
  85. static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
  86. {
  87. return container_of(com, struct stm32_adc_priv, common);
  88. }
  89. /* STM32F4 ADC internal common clock prescaler division ratios */
  90. static int stm32f4_pclk_div[] = {2, 4, 6, 8};
  91. /**
  92. * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
  93. * @priv: stm32 ADC core private data
  94. * Select clock prescaler used for analog conversions, before using ADC.
  95. */
  96. static int stm32f4_adc_clk_sel(struct platform_device *pdev,
  97. struct stm32_adc_priv *priv)
  98. {
  99. unsigned long rate;
  100. u32 val;
  101. int i;
  102. /* stm32f4 has one clk input for analog (mandatory), enforce it here */
  103. if (!priv->aclk) {
  104. dev_err(&pdev->dev, "No 'adc' clock found\n");
  105. return -ENOENT;
  106. }
  107. rate = clk_get_rate(priv->aclk);
  108. for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
  109. if ((rate / stm32f4_pclk_div[i]) <= STM32F4_ADC_MAX_CLK_RATE)
  110. break;
  111. }
  112. if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
  113. dev_err(&pdev->dev, "adc clk selection failed\n");
  114. return -EINVAL;
  115. }
  116. val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
  117. val &= ~STM32F4_ADC_ADCPRE_MASK;
  118. val |= i << STM32F4_ADC_ADCPRE_SHIFT;
  119. writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
  120. dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
  121. rate / (stm32f4_pclk_div[i] * 1000));
  122. return 0;
  123. }
  124. /* STM32F4 common registers definitions */
  125. static const struct stm32_adc_common_regs stm32f4_adc_common_regs = {
  126. .csr = STM32F4_ADC_CSR,
  127. .eoc1_msk = STM32F4_EOC1,
  128. .eoc2_msk = STM32F4_EOC2,
  129. .eoc3_msk = STM32F4_EOC3,
  130. };
  131. /* ADC common interrupt for all instances */
  132. static void stm32_adc_irq_handler(struct irq_desc *desc)
  133. {
  134. struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
  135. struct irq_chip *chip = irq_desc_get_chip(desc);
  136. u32 status;
  137. chained_irq_enter(chip, desc);
  138. status = readl_relaxed(priv->common.base + priv->cfg->regs->csr);
  139. if (status & priv->cfg->regs->eoc1_msk)
  140. generic_handle_irq(irq_find_mapping(priv->domain, 0));
  141. if (status & priv->cfg->regs->eoc2_msk)
  142. generic_handle_irq(irq_find_mapping(priv->domain, 1));
  143. if (status & priv->cfg->regs->eoc3_msk)
  144. generic_handle_irq(irq_find_mapping(priv->domain, 2));
  145. chained_irq_exit(chip, desc);
  146. };
  147. static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
  148. irq_hw_number_t hwirq)
  149. {
  150. irq_set_chip_data(irq, d->host_data);
  151. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
  152. return 0;
  153. }
  154. static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
  155. {
  156. irq_set_chip_and_handler(irq, NULL, NULL);
  157. irq_set_chip_data(irq, NULL);
  158. }
  159. static const struct irq_domain_ops stm32_adc_domain_ops = {
  160. .map = stm32_adc_domain_map,
  161. .unmap = stm32_adc_domain_unmap,
  162. .xlate = irq_domain_xlate_onecell,
  163. };
  164. static int stm32_adc_irq_probe(struct platform_device *pdev,
  165. struct stm32_adc_priv *priv)
  166. {
  167. struct device_node *np = pdev->dev.of_node;
  168. priv->irq = platform_get_irq(pdev, 0);
  169. if (priv->irq < 0) {
  170. dev_err(&pdev->dev, "failed to get irq\n");
  171. return priv->irq;
  172. }
  173. priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
  174. &stm32_adc_domain_ops,
  175. priv);
  176. if (!priv->domain) {
  177. dev_err(&pdev->dev, "Failed to add irq domain\n");
  178. return -ENOMEM;
  179. }
  180. irq_set_chained_handler(priv->irq, stm32_adc_irq_handler);
  181. irq_set_handler_data(priv->irq, priv);
  182. return 0;
  183. }
  184. static void stm32_adc_irq_remove(struct platform_device *pdev,
  185. struct stm32_adc_priv *priv)
  186. {
  187. int hwirq;
  188. for (hwirq = 0; hwirq < STM32_ADC_MAX_ADCS; hwirq++)
  189. irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
  190. irq_domain_remove(priv->domain);
  191. irq_set_chained_handler(priv->irq, NULL);
  192. }
  193. static int stm32_adc_probe(struct platform_device *pdev)
  194. {
  195. struct stm32_adc_priv *priv;
  196. struct device *dev = &pdev->dev;
  197. struct device_node *np = pdev->dev.of_node;
  198. struct resource *res;
  199. int ret;
  200. if (!pdev->dev.of_node)
  201. return -ENODEV;
  202. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  203. if (!priv)
  204. return -ENOMEM;
  205. priv->cfg = (const struct stm32_adc_priv_cfg *)
  206. of_match_device(dev->driver->of_match_table, dev)->data;
  207. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  208. priv->common.base = devm_ioremap_resource(&pdev->dev, res);
  209. if (IS_ERR(priv->common.base))
  210. return PTR_ERR(priv->common.base);
  211. priv->common.phys_base = res->start;
  212. priv->vref = devm_regulator_get(&pdev->dev, "vref");
  213. if (IS_ERR(priv->vref)) {
  214. ret = PTR_ERR(priv->vref);
  215. dev_err(&pdev->dev, "vref get failed, %d\n", ret);
  216. return ret;
  217. }
  218. ret = regulator_enable(priv->vref);
  219. if (ret < 0) {
  220. dev_err(&pdev->dev, "vref enable failed\n");
  221. return ret;
  222. }
  223. ret = regulator_get_voltage(priv->vref);
  224. if (ret < 0) {
  225. dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
  226. goto err_regulator_disable;
  227. }
  228. priv->common.vref_mv = ret / 1000;
  229. dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
  230. priv->aclk = devm_clk_get(&pdev->dev, "adc");
  231. if (IS_ERR(priv->aclk)) {
  232. ret = PTR_ERR(priv->aclk);
  233. if (ret == -ENOENT) {
  234. priv->aclk = NULL;
  235. } else {
  236. dev_err(&pdev->dev, "Can't get 'adc' clock\n");
  237. goto err_regulator_disable;
  238. }
  239. }
  240. if (priv->aclk) {
  241. ret = clk_prepare_enable(priv->aclk);
  242. if (ret < 0) {
  243. dev_err(&pdev->dev, "adc clk enable failed\n");
  244. goto err_regulator_disable;
  245. }
  246. }
  247. ret = priv->cfg->clk_sel(pdev, priv);
  248. if (ret < 0)
  249. goto err_clk_disable;
  250. ret = stm32_adc_irq_probe(pdev, priv);
  251. if (ret < 0)
  252. goto err_clk_disable;
  253. platform_set_drvdata(pdev, &priv->common);
  254. ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
  255. if (ret < 0) {
  256. dev_err(&pdev->dev, "failed to populate DT children\n");
  257. goto err_irq_remove;
  258. }
  259. return 0;
  260. err_irq_remove:
  261. stm32_adc_irq_remove(pdev, priv);
  262. err_clk_disable:
  263. if (priv->aclk)
  264. clk_disable_unprepare(priv->aclk);
  265. err_regulator_disable:
  266. regulator_disable(priv->vref);
  267. return ret;
  268. }
  269. static int stm32_adc_remove(struct platform_device *pdev)
  270. {
  271. struct stm32_adc_common *common = platform_get_drvdata(pdev);
  272. struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
  273. of_platform_depopulate(&pdev->dev);
  274. stm32_adc_irq_remove(pdev, priv);
  275. if (priv->aclk)
  276. clk_disable_unprepare(priv->aclk);
  277. regulator_disable(priv->vref);
  278. return 0;
  279. }
  280. static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg = {
  281. .regs = &stm32f4_adc_common_regs,
  282. .clk_sel = stm32f4_adc_clk_sel,
  283. };
  284. static const struct of_device_id stm32_adc_of_match[] = {
  285. {
  286. .compatible = "st,stm32f4-adc-core",
  287. .data = (void *)&stm32f4_adc_priv_cfg
  288. }, {
  289. },
  290. };
  291. MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
  292. static struct platform_driver stm32_adc_driver = {
  293. .probe = stm32_adc_probe,
  294. .remove = stm32_adc_remove,
  295. .driver = {
  296. .name = "stm32-adc-core",
  297. .of_match_table = stm32_adc_of_match,
  298. },
  299. };
  300. module_platform_driver(stm32_adc_driver);
  301. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  302. MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
  303. MODULE_LICENSE("GPL v2");
  304. MODULE_ALIAS("platform:stm32-adc-core");