1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of STM32 DAC driver
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/reset.h>
17 #include "stm32-dac-core.h"
20 * struct stm32_dac_priv - stm32 DAC core private data
21 * @pclk: peripheral clock common for all DACs
22 * @rst: peripheral reset control
23 * @vref: regulator reference
24 * @common: Common data for all DAC instances
26 struct stm32_dac_priv
{
28 struct reset_control
*rst
;
29 struct regulator
*vref
;
30 struct stm32_dac_common common
;
34 * struct stm32_dac_cfg - DAC configuration
35 * @has_hfsel: DAC has high frequency control
37 struct stm32_dac_cfg
{
41 static struct stm32_dac_priv
*to_stm32_dac_priv(struct stm32_dac_common
*com
)
43 return container_of(com
, struct stm32_dac_priv
, common
);
46 static const struct regmap_config stm32_dac_regmap_cfg
= {
49 .reg_stride
= sizeof(u32
),
50 .max_register
= 0x3fc,
53 static int stm32_dac_probe(struct platform_device
*pdev
)
55 struct device
*dev
= &pdev
->dev
;
56 const struct stm32_dac_cfg
*cfg
;
57 struct stm32_dac_priv
*priv
;
58 struct regmap
*regmap
;
66 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
69 cfg
= (const struct stm32_dac_cfg
*)
70 of_match_device(dev
->driver
->of_match_table
, dev
)->data
;
72 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
73 mmio
= devm_ioremap_resource(dev
, res
);
77 regmap
= devm_regmap_init_mmio(dev
, mmio
, &stm32_dac_regmap_cfg
);
79 return PTR_ERR(regmap
);
80 priv
->common
.regmap
= regmap
;
82 priv
->vref
= devm_regulator_get(dev
, "vref");
83 if (IS_ERR(priv
->vref
)) {
84 ret
= PTR_ERR(priv
->vref
);
85 dev_err(dev
, "vref get failed, %d\n", ret
);
89 ret
= regulator_enable(priv
->vref
);
91 dev_err(dev
, "vref enable failed\n");
95 ret
= regulator_get_voltage(priv
->vref
);
97 dev_err(dev
, "vref get voltage failed, %d\n", ret
);
100 priv
->common
.vref_mv
= ret
/ 1000;
101 dev_dbg(dev
, "vref+=%dmV\n", priv
->common
.vref_mv
);
103 priv
->pclk
= devm_clk_get(dev
, "pclk");
104 if (IS_ERR(priv
->pclk
)) {
105 ret
= PTR_ERR(priv
->pclk
);
106 dev_err(dev
, "pclk get failed\n");
110 ret
= clk_prepare_enable(priv
->pclk
);
112 dev_err(dev
, "pclk enable failed\n");
116 priv
->rst
= devm_reset_control_get_exclusive(dev
, NULL
);
117 if (!IS_ERR(priv
->rst
)) {
118 reset_control_assert(priv
->rst
);
120 reset_control_deassert(priv
->rst
);
123 if (cfg
&& cfg
->has_hfsel
) {
124 /* When clock speed is higher than 80MHz, set HFSEL */
125 priv
->common
.hfsel
= (clk_get_rate(priv
->pclk
) > 80000000UL);
126 ret
= regmap_update_bits(regmap
, STM32_DAC_CR
,
127 STM32H7_DAC_CR_HFSEL
,
129 STM32H7_DAC_CR_HFSEL
: 0);
134 platform_set_drvdata(pdev
, &priv
->common
);
136 ret
= of_platform_populate(pdev
->dev
.of_node
, NULL
, NULL
, dev
);
138 dev_err(dev
, "failed to populate DT children\n");
145 clk_disable_unprepare(priv
->pclk
);
147 regulator_disable(priv
->vref
);
152 static int stm32_dac_remove(struct platform_device
*pdev
)
154 struct stm32_dac_common
*common
= platform_get_drvdata(pdev
);
155 struct stm32_dac_priv
*priv
= to_stm32_dac_priv(common
);
157 of_platform_depopulate(&pdev
->dev
);
158 clk_disable_unprepare(priv
->pclk
);
159 regulator_disable(priv
->vref
);
164 static const struct stm32_dac_cfg stm32h7_dac_cfg
= {
168 static const struct of_device_id stm32_dac_of_match
[] = {
170 .compatible
= "st,stm32f4-dac-core",
172 .compatible
= "st,stm32h7-dac-core",
173 .data
= (void *)&stm32h7_dac_cfg
,
177 MODULE_DEVICE_TABLE(of
, stm32_dac_of_match
);
179 static struct platform_driver stm32_dac_driver
= {
180 .probe
= stm32_dac_probe
,
181 .remove
= stm32_dac_remove
,
183 .name
= "stm32-dac-core",
184 .of_match_table
= stm32_dac_of_match
,
187 module_platform_driver(stm32_dac_driver
);
189 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
190 MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
191 MODULE_LICENSE("GPL v2");
192 MODULE_ALIAS("platform:stm32-dac-core");