0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/clk.h>
0011 #include <linux/delay.h>
0012 #include <linux/module.h>
0013 #include <linux/of_platform.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/regulator/consumer.h>
0016 #include <linux/reset.h>
0017
0018 #include "stm32-dac-core.h"
0019
0020
0021
0022
0023
0024
0025
0026 struct stm32_dac_priv {
0027 struct clk *pclk;
0028 struct regulator *vref;
0029 struct stm32_dac_common common;
0030 };
0031
0032
0033
0034
0035
0036 struct stm32_dac_cfg {
0037 bool has_hfsel;
0038 };
0039
0040 static struct stm32_dac_priv *to_stm32_dac_priv(struct stm32_dac_common *com)
0041 {
0042 return container_of(com, struct stm32_dac_priv, common);
0043 }
0044
0045 static const struct regmap_config stm32_dac_regmap_cfg = {
0046 .reg_bits = 32,
0047 .val_bits = 32,
0048 .reg_stride = sizeof(u32),
0049 .max_register = 0x3fc,
0050 };
0051
0052 static int stm32_dac_core_hw_start(struct device *dev)
0053 {
0054 struct stm32_dac_common *common = dev_get_drvdata(dev);
0055 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
0056 int ret;
0057
0058 ret = regulator_enable(priv->vref);
0059 if (ret < 0) {
0060 dev_err(dev, "vref enable failed: %d\n", ret);
0061 return ret;
0062 }
0063
0064 ret = clk_prepare_enable(priv->pclk);
0065 if (ret < 0) {
0066 dev_err(dev, "pclk enable failed: %d\n", ret);
0067 goto err_regulator_disable;
0068 }
0069
0070 return 0;
0071
0072 err_regulator_disable:
0073 regulator_disable(priv->vref);
0074
0075 return ret;
0076 }
0077
0078 static void stm32_dac_core_hw_stop(struct device *dev)
0079 {
0080 struct stm32_dac_common *common = dev_get_drvdata(dev);
0081 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
0082
0083 clk_disable_unprepare(priv->pclk);
0084 regulator_disable(priv->vref);
0085 }
0086
0087 static int stm32_dac_probe(struct platform_device *pdev)
0088 {
0089 struct device *dev = &pdev->dev;
0090 const struct stm32_dac_cfg *cfg;
0091 struct stm32_dac_priv *priv;
0092 struct regmap *regmap;
0093 void __iomem *mmio;
0094 struct reset_control *rst;
0095 int ret;
0096
0097 if (!dev->of_node)
0098 return -ENODEV;
0099
0100 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0101 if (!priv)
0102 return -ENOMEM;
0103 platform_set_drvdata(pdev, &priv->common);
0104
0105 cfg = (const struct stm32_dac_cfg *)
0106 of_match_device(dev->driver->of_match_table, dev)->data;
0107
0108 mmio = devm_platform_ioremap_resource(pdev, 0);
0109 if (IS_ERR(mmio))
0110 return PTR_ERR(mmio);
0111
0112 regmap = devm_regmap_init_mmio_clk(dev, "pclk", mmio,
0113 &stm32_dac_regmap_cfg);
0114 if (IS_ERR(regmap))
0115 return PTR_ERR(regmap);
0116 priv->common.regmap = regmap;
0117
0118 priv->pclk = devm_clk_get(dev, "pclk");
0119 if (IS_ERR(priv->pclk))
0120 return dev_err_probe(dev, PTR_ERR(priv->pclk), "pclk get failed\n");
0121
0122 priv->vref = devm_regulator_get(dev, "vref");
0123 if (IS_ERR(priv->vref))
0124 return dev_err_probe(dev, PTR_ERR(priv->vref), "vref get failed\n");
0125
0126 pm_runtime_get_noresume(dev);
0127 pm_runtime_set_active(dev);
0128 pm_runtime_enable(dev);
0129
0130 ret = stm32_dac_core_hw_start(dev);
0131 if (ret)
0132 goto err_pm_stop;
0133
0134 ret = regulator_get_voltage(priv->vref);
0135 if (ret < 0) {
0136 dev_err(dev, "vref get voltage failed, %d\n", ret);
0137 goto err_hw_stop;
0138 }
0139 priv->common.vref_mv = ret / 1000;
0140 dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
0141
0142 rst = devm_reset_control_get_optional_exclusive(dev, NULL);
0143 if (rst) {
0144 if (IS_ERR(rst)) {
0145 ret = dev_err_probe(dev, PTR_ERR(rst), "reset get failed\n");
0146 goto err_hw_stop;
0147 }
0148
0149 reset_control_assert(rst);
0150 udelay(2);
0151 reset_control_deassert(rst);
0152 }
0153
0154 if (cfg && cfg->has_hfsel) {
0155
0156 priv->common.hfsel = (clk_get_rate(priv->pclk) > 80000000UL);
0157 ret = regmap_update_bits(regmap, STM32_DAC_CR,
0158 STM32H7_DAC_CR_HFSEL,
0159 priv->common.hfsel ?
0160 STM32H7_DAC_CR_HFSEL : 0);
0161 if (ret)
0162 goto err_hw_stop;
0163 }
0164
0165
0166 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
0167 if (ret < 0) {
0168 dev_err(dev, "failed to populate DT children\n");
0169 goto err_hw_stop;
0170 }
0171
0172 pm_runtime_put(dev);
0173
0174 return 0;
0175
0176 err_hw_stop:
0177 stm32_dac_core_hw_stop(dev);
0178 err_pm_stop:
0179 pm_runtime_disable(dev);
0180 pm_runtime_set_suspended(dev);
0181 pm_runtime_put_noidle(dev);
0182
0183 return ret;
0184 }
0185
0186 static int stm32_dac_remove(struct platform_device *pdev)
0187 {
0188 pm_runtime_get_sync(&pdev->dev);
0189 of_platform_depopulate(&pdev->dev);
0190 stm32_dac_core_hw_stop(&pdev->dev);
0191 pm_runtime_disable(&pdev->dev);
0192 pm_runtime_set_suspended(&pdev->dev);
0193 pm_runtime_put_noidle(&pdev->dev);
0194
0195 return 0;
0196 }
0197
0198 static int stm32_dac_core_resume(struct device *dev)
0199 {
0200 struct stm32_dac_common *common = dev_get_drvdata(dev);
0201 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
0202 int ret;
0203
0204 if (priv->common.hfsel) {
0205
0206 ret = regmap_update_bits(priv->common.regmap, STM32_DAC_CR,
0207 STM32H7_DAC_CR_HFSEL,
0208 STM32H7_DAC_CR_HFSEL);
0209 if (ret)
0210 return ret;
0211 }
0212
0213 return pm_runtime_force_resume(dev);
0214 }
0215
0216 static int stm32_dac_core_runtime_suspend(struct device *dev)
0217 {
0218 stm32_dac_core_hw_stop(dev);
0219
0220 return 0;
0221 }
0222
0223 static int stm32_dac_core_runtime_resume(struct device *dev)
0224 {
0225 return stm32_dac_core_hw_start(dev);
0226 }
0227
0228 static const struct dev_pm_ops stm32_dac_core_pm_ops = {
0229 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, stm32_dac_core_resume)
0230 RUNTIME_PM_OPS(stm32_dac_core_runtime_suspend,
0231 stm32_dac_core_runtime_resume,
0232 NULL)
0233 };
0234
0235 static const struct stm32_dac_cfg stm32h7_dac_cfg = {
0236 .has_hfsel = true,
0237 };
0238
0239 static const struct of_device_id stm32_dac_of_match[] = {
0240 {
0241 .compatible = "st,stm32f4-dac-core",
0242 }, {
0243 .compatible = "st,stm32h7-dac-core",
0244 .data = (void *)&stm32h7_dac_cfg,
0245 },
0246 {},
0247 };
0248 MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
0249
0250 static struct platform_driver stm32_dac_driver = {
0251 .probe = stm32_dac_probe,
0252 .remove = stm32_dac_remove,
0253 .driver = {
0254 .name = "stm32-dac-core",
0255 .of_match_table = stm32_dac_of_match,
0256 .pm = pm_ptr(&stm32_dac_core_pm_ops),
0257 },
0258 };
0259 module_platform_driver(stm32_dac_driver);
0260
0261 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
0262 MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
0263 MODULE_LICENSE("GPL v2");
0264 MODULE_ALIAS("platform:stm32-dac-core");