1// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) STMicroelectronics 2019
3// Author(s): Fabrice Gasnier <fabrice.gasnier@st.com>.
4
5#include <linux/mfd/syscon.h>
6#include <linux/module.h>
7#include <linux/of.h>
8#include <linux/platform_device.h>
9#include <linux/regmap.h>
10#include <linux/regulator/driver.h>
11#include <linux/regulator/of_regulator.h>
12
13/* STM32H7 SYSCFG register */
14#define STM32H7_SYSCFG_PMCR		0x04
15#define STM32H7_SYSCFG_BOOSTE_MASK	BIT(8)
16
17/* STM32MP1 SYSCFG has set and clear registers */
18#define STM32MP1_SYSCFG_PMCSETR		0x04
19#define STM32MP1_SYSCFG_PMCCLRR		0x44
20#define STM32MP1_SYSCFG_EN_BOOSTER_MASK	BIT(8)
21
22static const struct regulator_ops stm32h7_booster_ops = {
23	.enable		= regulator_enable_regmap,
24	.disable	= regulator_disable_regmap,
25	.is_enabled	= regulator_is_enabled_regmap,
26};
27
28static const struct regulator_desc stm32h7_booster_desc = {
29	.name = "booster",
30	.supply_name = "vdda",
31	.n_voltages = 1,
32	.type = REGULATOR_VOLTAGE,
33	.fixed_uV = 3300000,
34	.ramp_delay = 66000, /* up to 50us to stabilize */
35	.ops = &stm32h7_booster_ops,
36	.enable_reg = STM32H7_SYSCFG_PMCR,
37	.enable_mask = STM32H7_SYSCFG_BOOSTE_MASK,
38	.owner = THIS_MODULE,
39};
40
41static int stm32mp1_booster_enable(struct regulator_dev *rdev)
42{
43	return regmap_write(rdev->regmap, STM32MP1_SYSCFG_PMCSETR,
44			    STM32MP1_SYSCFG_EN_BOOSTER_MASK);
45}
46
47static int stm32mp1_booster_disable(struct regulator_dev *rdev)
48{
49	return regmap_write(rdev->regmap, STM32MP1_SYSCFG_PMCCLRR,
50			    STM32MP1_SYSCFG_EN_BOOSTER_MASK);
51}
52
53static const struct regulator_ops stm32mp1_booster_ops = {
54	.enable		= stm32mp1_booster_enable,
55	.disable	= stm32mp1_booster_disable,
56	.is_enabled	= regulator_is_enabled_regmap,
57};
58
59static const struct regulator_desc stm32mp1_booster_desc = {
60	.name = "booster",
61	.supply_name = "vdda",
62	.n_voltages = 1,
63	.type = REGULATOR_VOLTAGE,
64	.fixed_uV = 3300000,
65	.ramp_delay = 66000,
66	.ops = &stm32mp1_booster_ops,
67	.enable_reg = STM32MP1_SYSCFG_PMCSETR,
68	.enable_mask = STM32MP1_SYSCFG_EN_BOOSTER_MASK,
69	.owner = THIS_MODULE,
70};
71
72static int stm32_booster_probe(struct platform_device *pdev)
73{
74	struct device *dev = &pdev->dev;
75	struct device_node *np = pdev->dev.of_node;
76	struct regulator_config config = { };
77	const struct regulator_desc *desc;
78	struct regulator_dev *rdev;
79	struct regmap *regmap;
80	int ret;
81
82	regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
83	if (IS_ERR(regmap))
84		return PTR_ERR(regmap);
85
86	desc = device_get_match_data(dev);
87
88	config.regmap = regmap;
89	config.dev = dev;
90	config.of_node = np;
91	config.init_data = of_get_regulator_init_data(dev, np, desc);
92
93	rdev = devm_regulator_register(dev, desc, &config);
94	if (IS_ERR(rdev)) {
95		ret = PTR_ERR(rdev);
96		dev_err(dev, "register failed with error %d\n", ret);
97		return ret;
98	}
99
100	return 0;
101}
102
103static const struct of_device_id __maybe_unused stm32_booster_of_match[] = {
104	{
105		.compatible = "st,stm32h7-booster",
106		.data = (void *)&stm32h7_booster_desc
107	}, {
108		.compatible = "st,stm32mp1-booster",
109		.data = (void *)&stm32mp1_booster_desc
110	}, {
111	},
112};
113MODULE_DEVICE_TABLE(of, stm32_booster_of_match);
114
115static struct platform_driver stm32_booster_driver = {
116	.probe = stm32_booster_probe,
117	.driver = {
118		.name  = "stm32-booster",
119		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
120		.of_match_table = of_match_ptr(stm32_booster_of_match),
121	},
122};
123module_platform_driver(stm32_booster_driver);
124
125MODULE_LICENSE("GPL v2");
126MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
127MODULE_DESCRIPTION("STMicroelectronics STM32 booster regulator driver");
128MODULE_ALIAS("platform:stm32-booster");
129