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
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 /* STM32H7 - common registers for all ADC instances */
53 #define STM32H7_ADC_CSR (STM32_ADCX_COMN_OFFSET + 0x00)
54 #define STM32H7_ADC_CCR (STM32_ADCX_COMN_OFFSET + 0x08)
56 /* STM32H7_ADC_CSR - bit fields */
57 #define STM32H7_EOC_SLV BIT(18)
58 #define STM32H7_EOC_MST BIT(2)
60 /* STM32H7_ADC_CCR - bit fields */
61 #define STM32H7_PRESC_SHIFT 18
62 #define STM32H7_PRESC_MASK GENMASK(21, 18)
63 #define STM32H7_CKMODE_SHIFT 16
64 #define STM32H7_CKMODE_MASK GENMASK(17, 16)
66 /* STM32 H7 maximum analog clock rate (from datasheet) */
67 #define STM32H7_ADC_MAX_CLK_RATE 72000000
70 * stm32_adc_common_regs - stm32 common registers, compatible dependent data
71 * @csr: common status register offset
72 * @eoc1: adc1 end of conversion flag in @csr
73 * @eoc2: adc2 end of conversion flag in @csr
74 * @eoc3: adc3 end of conversion flag in @csr
76 struct stm32_adc_common_regs
{
83 struct stm32_adc_priv
;
86 * stm32_adc_priv_cfg - stm32 core compatible configuration data
87 * @regs: common registers for all instances
88 * @clk_sel: clock selection routine
90 struct stm32_adc_priv_cfg
{
91 const struct stm32_adc_common_regs
*regs
;
92 int (*clk_sel
)(struct platform_device
*, struct stm32_adc_priv
*);
96 * struct stm32_adc_priv - stm32 ADC core private data
97 * @irq: irq for ADC block
98 * @domain: irq domain reference
99 * @aclk: clock reference for the analog circuitry
100 * @bclk: bus clock common for all ADCs, depends on part used
101 * @vref: regulator reference
102 * @cfg: compatible configuration data
103 * @common: common data for all ADC instances
105 struct stm32_adc_priv
{
107 struct irq_domain
*domain
;
110 struct regulator
*vref
;
111 const struct stm32_adc_priv_cfg
*cfg
;
112 struct stm32_adc_common common
;
115 static struct stm32_adc_priv
*to_stm32_adc_priv(struct stm32_adc_common
*com
)
117 return container_of(com
, struct stm32_adc_priv
, common
);
120 /* STM32F4 ADC internal common clock prescaler division ratios */
121 static int stm32f4_pclk_div
[] = {2, 4, 6, 8};
124 * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
125 * @priv: stm32 ADC core private data
126 * Select clock prescaler used for analog conversions, before using ADC.
128 static int stm32f4_adc_clk_sel(struct platform_device
*pdev
,
129 struct stm32_adc_priv
*priv
)
135 /* stm32f4 has one clk input for analog (mandatory), enforce it here */
137 dev_err(&pdev
->dev
, "No 'adc' clock found\n");
141 rate
= clk_get_rate(priv
->aclk
);
142 for (i
= 0; i
< ARRAY_SIZE(stm32f4_pclk_div
); i
++) {
143 if ((rate
/ stm32f4_pclk_div
[i
]) <= STM32F4_ADC_MAX_CLK_RATE
)
146 if (i
>= ARRAY_SIZE(stm32f4_pclk_div
)) {
147 dev_err(&pdev
->dev
, "adc clk selection failed\n");
151 priv
->common
.rate
= rate
;
152 val
= readl_relaxed(priv
->common
.base
+ STM32F4_ADC_CCR
);
153 val
&= ~STM32F4_ADC_ADCPRE_MASK
;
154 val
|= i
<< STM32F4_ADC_ADCPRE_SHIFT
;
155 writel_relaxed(val
, priv
->common
.base
+ STM32F4_ADC_CCR
);
157 dev_dbg(&pdev
->dev
, "Using analog clock source at %ld kHz\n",
158 rate
/ (stm32f4_pclk_div
[i
] * 1000));
164 * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
165 * @ckmode: ADC clock mode, Async or sync with prescaler.
166 * @presc: prescaler bitfield for async clock mode
167 * @div: prescaler division ratio
169 struct stm32h7_adc_ck_spec
{
175 const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec
[] = {
176 /* 00: CK_ADC[1..3]: Asynchronous clock modes */
189 /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
195 static int stm32h7_adc_clk_sel(struct platform_device
*pdev
,
196 struct stm32_adc_priv
*priv
)
198 u32 ckmode
, presc
, val
;
202 /* stm32h7 bus clock is common for all ADC instances (mandatory) */
204 dev_err(&pdev
->dev
, "No 'bus' clock found\n");
209 * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
210 * So, choice is to have bus clock mandatory and adc clock optional.
211 * If optional 'adc' clock has been found, then try to use it first.
215 * Asynchronous clock modes (e.g. ckmode == 0)
216 * From spec: PLL output musn't exceed max rate
218 rate
= clk_get_rate(priv
->aclk
);
220 for (i
= 0; i
< ARRAY_SIZE(stm32h7_adc_ckmodes_spec
); i
++) {
221 ckmode
= stm32h7_adc_ckmodes_spec
[i
].ckmode
;
222 presc
= stm32h7_adc_ckmodes_spec
[i
].presc
;
223 div
= stm32h7_adc_ckmodes_spec
[i
].div
;
228 if ((rate
/ div
) <= STM32H7_ADC_MAX_CLK_RATE
)
233 /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
234 rate
= clk_get_rate(priv
->bclk
);
236 for (i
= 0; i
< ARRAY_SIZE(stm32h7_adc_ckmodes_spec
); i
++) {
237 ckmode
= stm32h7_adc_ckmodes_spec
[i
].ckmode
;
238 presc
= stm32h7_adc_ckmodes_spec
[i
].presc
;
239 div
= stm32h7_adc_ckmodes_spec
[i
].div
;
244 if ((rate
/ div
) <= STM32H7_ADC_MAX_CLK_RATE
)
248 dev_err(&pdev
->dev
, "adc clk selection failed\n");
252 /* rate used later by each ADC instance to control BOOST mode */
253 priv
->common
.rate
= rate
;
255 /* Set common clock mode and prescaler */
256 val
= readl_relaxed(priv
->common
.base
+ STM32H7_ADC_CCR
);
257 val
&= ~(STM32H7_CKMODE_MASK
| STM32H7_PRESC_MASK
);
258 val
|= ckmode
<< STM32H7_CKMODE_SHIFT
;
259 val
|= presc
<< STM32H7_PRESC_SHIFT
;
260 writel_relaxed(val
, priv
->common
.base
+ STM32H7_ADC_CCR
);
262 dev_dbg(&pdev
->dev
, "Using %s clock/%d source at %ld kHz\n",
263 ckmode
? "bus" : "adc", div
, rate
/ (div
* 1000));
268 /* STM32F4 common registers definitions */
269 static const struct stm32_adc_common_regs stm32f4_adc_common_regs
= {
270 .csr
= STM32F4_ADC_CSR
,
271 .eoc1_msk
= STM32F4_EOC1
,
272 .eoc2_msk
= STM32F4_EOC2
,
273 .eoc3_msk
= STM32F4_EOC3
,
276 /* STM32H7 common registers definitions */
277 static const struct stm32_adc_common_regs stm32h7_adc_common_regs
= {
278 .csr
= STM32H7_ADC_CSR
,
279 .eoc1_msk
= STM32H7_EOC_MST
,
280 .eoc2_msk
= STM32H7_EOC_SLV
,
283 /* ADC common interrupt for all instances */
284 static void stm32_adc_irq_handler(struct irq_desc
*desc
)
286 struct stm32_adc_priv
*priv
= irq_desc_get_handler_data(desc
);
287 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
290 chained_irq_enter(chip
, desc
);
291 status
= readl_relaxed(priv
->common
.base
+ priv
->cfg
->regs
->csr
);
293 if (status
& priv
->cfg
->regs
->eoc1_msk
)
294 generic_handle_irq(irq_find_mapping(priv
->domain
, 0));
296 if (status
& priv
->cfg
->regs
->eoc2_msk
)
297 generic_handle_irq(irq_find_mapping(priv
->domain
, 1));
299 if (status
& priv
->cfg
->regs
->eoc3_msk
)
300 generic_handle_irq(irq_find_mapping(priv
->domain
, 2));
302 chained_irq_exit(chip
, desc
);
305 static int stm32_adc_domain_map(struct irq_domain
*d
, unsigned int irq
,
306 irq_hw_number_t hwirq
)
308 irq_set_chip_data(irq
, d
->host_data
);
309 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_level_irq
);
314 static void stm32_adc_domain_unmap(struct irq_domain
*d
, unsigned int irq
)
316 irq_set_chip_and_handler(irq
, NULL
, NULL
);
317 irq_set_chip_data(irq
, NULL
);
320 static const struct irq_domain_ops stm32_adc_domain_ops
= {
321 .map
= stm32_adc_domain_map
,
322 .unmap
= stm32_adc_domain_unmap
,
323 .xlate
= irq_domain_xlate_onecell
,
326 static int stm32_adc_irq_probe(struct platform_device
*pdev
,
327 struct stm32_adc_priv
*priv
)
329 struct device_node
*np
= pdev
->dev
.of_node
;
331 priv
->irq
= platform_get_irq(pdev
, 0);
333 dev_err(&pdev
->dev
, "failed to get irq\n");
337 priv
->domain
= irq_domain_add_simple(np
, STM32_ADC_MAX_ADCS
, 0,
338 &stm32_adc_domain_ops
,
341 dev_err(&pdev
->dev
, "Failed to add irq domain\n");
345 irq_set_chained_handler(priv
->irq
, stm32_adc_irq_handler
);
346 irq_set_handler_data(priv
->irq
, priv
);
351 static void stm32_adc_irq_remove(struct platform_device
*pdev
,
352 struct stm32_adc_priv
*priv
)
356 for (hwirq
= 0; hwirq
< STM32_ADC_MAX_ADCS
; hwirq
++)
357 irq_dispose_mapping(irq_find_mapping(priv
->domain
, hwirq
));
358 irq_domain_remove(priv
->domain
);
359 irq_set_chained_handler(priv
->irq
, NULL
);
362 static int stm32_adc_probe(struct platform_device
*pdev
)
364 struct stm32_adc_priv
*priv
;
365 struct device
*dev
= &pdev
->dev
;
366 struct device_node
*np
= pdev
->dev
.of_node
;
367 struct resource
*res
;
370 if (!pdev
->dev
.of_node
)
373 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
377 priv
->cfg
= (const struct stm32_adc_priv_cfg
*)
378 of_match_device(dev
->driver
->of_match_table
, dev
)->data
;
380 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
381 priv
->common
.base
= devm_ioremap_resource(&pdev
->dev
, res
);
382 if (IS_ERR(priv
->common
.base
))
383 return PTR_ERR(priv
->common
.base
);
384 priv
->common
.phys_base
= res
->start
;
386 priv
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
387 if (IS_ERR(priv
->vref
)) {
388 ret
= PTR_ERR(priv
->vref
);
389 dev_err(&pdev
->dev
, "vref get failed, %d\n", ret
);
393 ret
= regulator_enable(priv
->vref
);
395 dev_err(&pdev
->dev
, "vref enable failed\n");
399 ret
= regulator_get_voltage(priv
->vref
);
401 dev_err(&pdev
->dev
, "vref get voltage failed, %d\n", ret
);
402 goto err_regulator_disable
;
404 priv
->common
.vref_mv
= ret
/ 1000;
405 dev_dbg(&pdev
->dev
, "vref+=%dmV\n", priv
->common
.vref_mv
);
407 priv
->aclk
= devm_clk_get(&pdev
->dev
, "adc");
408 if (IS_ERR(priv
->aclk
)) {
409 ret
= PTR_ERR(priv
->aclk
);
410 if (ret
== -ENOENT
) {
413 dev_err(&pdev
->dev
, "Can't get 'adc' clock\n");
414 goto err_regulator_disable
;
419 ret
= clk_prepare_enable(priv
->aclk
);
421 dev_err(&pdev
->dev
, "adc clk enable failed\n");
422 goto err_regulator_disable
;
426 priv
->bclk
= devm_clk_get(&pdev
->dev
, "bus");
427 if (IS_ERR(priv
->bclk
)) {
428 ret
= PTR_ERR(priv
->bclk
);
429 if (ret
== -ENOENT
) {
432 dev_err(&pdev
->dev
, "Can't get 'bus' clock\n");
433 goto err_aclk_disable
;
438 ret
= clk_prepare_enable(priv
->bclk
);
440 dev_err(&pdev
->dev
, "adc clk enable failed\n");
441 goto err_aclk_disable
;
445 ret
= priv
->cfg
->clk_sel(pdev
, priv
);
447 goto err_bclk_disable
;
449 ret
= stm32_adc_irq_probe(pdev
, priv
);
451 goto err_bclk_disable
;
453 platform_set_drvdata(pdev
, &priv
->common
);
455 ret
= of_platform_populate(np
, NULL
, NULL
, &pdev
->dev
);
457 dev_err(&pdev
->dev
, "failed to populate DT children\n");
464 stm32_adc_irq_remove(pdev
, priv
);
468 clk_disable_unprepare(priv
->bclk
);
472 clk_disable_unprepare(priv
->aclk
);
474 err_regulator_disable
:
475 regulator_disable(priv
->vref
);
480 static int stm32_adc_remove(struct platform_device
*pdev
)
482 struct stm32_adc_common
*common
= platform_get_drvdata(pdev
);
483 struct stm32_adc_priv
*priv
= to_stm32_adc_priv(common
);
485 of_platform_depopulate(&pdev
->dev
);
486 stm32_adc_irq_remove(pdev
, priv
);
488 clk_disable_unprepare(priv
->bclk
);
490 clk_disable_unprepare(priv
->aclk
);
491 regulator_disable(priv
->vref
);
496 static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg
= {
497 .regs
= &stm32f4_adc_common_regs
,
498 .clk_sel
= stm32f4_adc_clk_sel
,
501 static const struct stm32_adc_priv_cfg stm32h7_adc_priv_cfg
= {
502 .regs
= &stm32h7_adc_common_regs
,
503 .clk_sel
= stm32h7_adc_clk_sel
,
506 static const struct of_device_id stm32_adc_of_match
[] = {
508 .compatible
= "st,stm32f4-adc-core",
509 .data
= (void *)&stm32f4_adc_priv_cfg
511 .compatible
= "st,stm32h7-adc-core",
512 .data
= (void *)&stm32h7_adc_priv_cfg
516 MODULE_DEVICE_TABLE(of
, stm32_adc_of_match
);
518 static struct platform_driver stm32_adc_driver
= {
519 .probe
= stm32_adc_probe
,
520 .remove
= stm32_adc_remove
,
522 .name
= "stm32-adc-core",
523 .of_match_table
= stm32_adc_of_match
,
526 module_platform_driver(stm32_adc_driver
);
528 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
529 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
530 MODULE_LICENSE("GPL v2");
531 MODULE_ALIAS("platform:stm32-adc-core");