stm32-adc-core.c 7.9 KB

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