sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / iio / adc / stm32-adc-core.c
blob4214b0cd6b1b828949e7f59b1a8d3df571cc7075
1 /*
2 * This file is part of STM32 ADC driver
4 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
7 * Inspired from: fsl-imx25-tsadc
9 * License type: GPLv2
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.
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.
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/>.
24 #include <linux/clk.h>
25 #include <linux/interrupt.h>
26 #include <linux/irqchip/chained_irq.h>
27 #include <linux/irqdesc.h>
28 #include <linux/irqdomain.h>
29 #include <linux/module.h>
30 #include <linux/of_device.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/slab.h>
34 #include "stm32-adc-core.h"
36 /* STM32F4 - common registers for all ADC instances: 1, 2 & 3 */
37 #define STM32F4_ADC_CSR (STM32_ADCX_COMN_OFFSET + 0x00)
38 #define STM32F4_ADC_CCR (STM32_ADCX_COMN_OFFSET + 0x04)
40 /* STM32F4_ADC_CSR - bit fields */
41 #define STM32F4_EOC3 BIT(17)
42 #define STM32F4_EOC2 BIT(9)
43 #define STM32F4_EOC1 BIT(1)
45 /* STM32F4_ADC_CCR - bit fields */
46 #define STM32F4_ADC_ADCPRE_SHIFT 16
47 #define STM32F4_ADC_ADCPRE_MASK GENMASK(17, 16)
49 /* STM32 F4 maximum analog clock rate (from datasheet) */
50 #define STM32F4_ADC_MAX_CLK_RATE 36000000
52 /**
53 * struct stm32_adc_priv - stm32 ADC core private data
54 * @irq: irq for ADC block
55 * @domain: irq domain reference
56 * @aclk: clock reference for the analog circuitry
57 * @vref: regulator reference
58 * @common: common data for all ADC instances
60 struct stm32_adc_priv {
61 int irq;
62 struct irq_domain *domain;
63 struct clk *aclk;
64 struct regulator *vref;
65 struct stm32_adc_common common;
68 static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
70 return container_of(com, struct stm32_adc_priv, common);
73 /* STM32F4 ADC internal common clock prescaler division ratios */
74 static int stm32f4_pclk_div[] = {2, 4, 6, 8};
76 /**
77 * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
78 * @priv: stm32 ADC core private data
79 * Select clock prescaler used for analog conversions, before using ADC.
81 static int stm32f4_adc_clk_sel(struct platform_device *pdev,
82 struct stm32_adc_priv *priv)
84 unsigned long rate;
85 u32 val;
86 int i;
88 rate = clk_get_rate(priv->aclk);
89 for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
90 if ((rate / stm32f4_pclk_div[i]) <= STM32F4_ADC_MAX_CLK_RATE)
91 break;
93 if (i >= ARRAY_SIZE(stm32f4_pclk_div))
94 return -EINVAL;
96 val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
97 val &= ~STM32F4_ADC_ADCPRE_MASK;
98 val |= i << STM32F4_ADC_ADCPRE_SHIFT;
99 writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
101 dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
102 rate / (stm32f4_pclk_div[i] * 1000));
104 return 0;
107 /* ADC common interrupt for all instances */
108 static void stm32_adc_irq_handler(struct irq_desc *desc)
110 struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
111 struct irq_chip *chip = irq_desc_get_chip(desc);
112 u32 status;
114 chained_irq_enter(chip, desc);
115 status = readl_relaxed(priv->common.base + STM32F4_ADC_CSR);
117 if (status & STM32F4_EOC1)
118 generic_handle_irq(irq_find_mapping(priv->domain, 0));
120 if (status & STM32F4_EOC2)
121 generic_handle_irq(irq_find_mapping(priv->domain, 1));
123 if (status & STM32F4_EOC3)
124 generic_handle_irq(irq_find_mapping(priv->domain, 2));
126 chained_irq_exit(chip, desc);
129 static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
130 irq_hw_number_t hwirq)
132 irq_set_chip_data(irq, d->host_data);
133 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
135 return 0;
138 static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
140 irq_set_chip_and_handler(irq, NULL, NULL);
141 irq_set_chip_data(irq, NULL);
144 static const struct irq_domain_ops stm32_adc_domain_ops = {
145 .map = stm32_adc_domain_map,
146 .unmap = stm32_adc_domain_unmap,
147 .xlate = irq_domain_xlate_onecell,
150 static int stm32_adc_irq_probe(struct platform_device *pdev,
151 struct stm32_adc_priv *priv)
153 struct device_node *np = pdev->dev.of_node;
155 priv->irq = platform_get_irq(pdev, 0);
156 if (priv->irq < 0) {
157 dev_err(&pdev->dev, "failed to get irq\n");
158 return priv->irq;
161 priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
162 &stm32_adc_domain_ops,
163 priv);
164 if (!priv->domain) {
165 dev_err(&pdev->dev, "Failed to add irq domain\n");
166 return -ENOMEM;
169 irq_set_chained_handler(priv->irq, stm32_adc_irq_handler);
170 irq_set_handler_data(priv->irq, priv);
172 return 0;
175 static void stm32_adc_irq_remove(struct platform_device *pdev,
176 struct stm32_adc_priv *priv)
178 int hwirq;
180 for (hwirq = 0; hwirq < STM32_ADC_MAX_ADCS; hwirq++)
181 irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
182 irq_domain_remove(priv->domain);
183 irq_set_chained_handler(priv->irq, NULL);
186 static int stm32_adc_probe(struct platform_device *pdev)
188 struct stm32_adc_priv *priv;
189 struct device_node *np = pdev->dev.of_node;
190 struct resource *res;
191 int ret;
193 if (!pdev->dev.of_node)
194 return -ENODEV;
196 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
197 if (!priv)
198 return -ENOMEM;
200 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
201 priv->common.base = devm_ioremap_resource(&pdev->dev, res);
202 if (IS_ERR(priv->common.base))
203 return PTR_ERR(priv->common.base);
205 priv->vref = devm_regulator_get(&pdev->dev, "vref");
206 if (IS_ERR(priv->vref)) {
207 ret = PTR_ERR(priv->vref);
208 dev_err(&pdev->dev, "vref get failed, %d\n", ret);
209 return ret;
212 ret = regulator_enable(priv->vref);
213 if (ret < 0) {
214 dev_err(&pdev->dev, "vref enable failed\n");
215 return ret;
218 ret = regulator_get_voltage(priv->vref);
219 if (ret < 0) {
220 dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
221 goto err_regulator_disable;
223 priv->common.vref_mv = ret / 1000;
224 dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
226 priv->aclk = devm_clk_get(&pdev->dev, "adc");
227 if (IS_ERR(priv->aclk)) {
228 ret = PTR_ERR(priv->aclk);
229 dev_err(&pdev->dev, "Can't get 'adc' clock\n");
230 goto err_regulator_disable;
233 ret = clk_prepare_enable(priv->aclk);
234 if (ret < 0) {
235 dev_err(&pdev->dev, "adc clk enable failed\n");
236 goto err_regulator_disable;
239 ret = stm32f4_adc_clk_sel(pdev, priv);
240 if (ret < 0) {
241 dev_err(&pdev->dev, "adc clk selection failed\n");
242 goto err_clk_disable;
245 ret = stm32_adc_irq_probe(pdev, priv);
246 if (ret < 0)
247 goto err_clk_disable;
249 platform_set_drvdata(pdev, &priv->common);
251 ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
252 if (ret < 0) {
253 dev_err(&pdev->dev, "failed to populate DT children\n");
254 goto err_irq_remove;
257 return 0;
259 err_irq_remove:
260 stm32_adc_irq_remove(pdev, priv);
262 err_clk_disable:
263 clk_disable_unprepare(priv->aclk);
265 err_regulator_disable:
266 regulator_disable(priv->vref);
268 return ret;
271 static int stm32_adc_remove(struct platform_device *pdev)
273 struct stm32_adc_common *common = platform_get_drvdata(pdev);
274 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
276 of_platform_depopulate(&pdev->dev);
277 stm32_adc_irq_remove(pdev, priv);
278 clk_disable_unprepare(priv->aclk);
279 regulator_disable(priv->vref);
281 return 0;
284 static const struct of_device_id stm32_adc_of_match[] = {
285 { .compatible = "st,stm32f4-adc-core" },
288 MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
290 static struct platform_driver stm32_adc_driver = {
291 .probe = stm32_adc_probe,
292 .remove = stm32_adc_remove,
293 .driver = {
294 .name = "stm32-adc-core",
295 .of_match_table = stm32_adc_of_match,
298 module_platform_driver(stm32_adc_driver);
300 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
301 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
302 MODULE_LICENSE("GPL v2");
303 MODULE_ALIAS("platform:stm32-adc-core");