Skip to content

Commit 6cdae81

Browse files
ppailletbroonie
authored andcommitted
regulator: Add support for stm32 power regulators
Add support for 1V1 1V8 USB3V3 power regulators. Signed-off-by: Pascal Paillet <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent c29687c commit 6cdae81

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

drivers/regulator/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,13 @@ config REGULATOR_STM32_VREFBUF
840840
This driver can also be built as a module. If so, the module
841841
will be called stm32-vrefbuf.
842842

843+
config REGULATOR_STM32_PWR
844+
bool "STMicroelectronics STM32 PWR"
845+
depends on ARCH_STM32 || COMPILE_TEST
846+
help
847+
This driver supports internal regulators (1V1, 1V8, 3V3) in the
848+
STMicroelectronics STM32 chips.
849+
843850
config REGULATOR_STPMIC1
844851
tristate "STMicroelectronics STPMIC1 PMIC Regulators"
845852
depends on MFD_STPMIC1

drivers/regulator/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ obj-$(CONFIG_REGULATOR_S5M8767) += s5m8767.o
105105
obj-$(CONFIG_REGULATOR_SC2731) += sc2731-regulator.o
106106
obj-$(CONFIG_REGULATOR_SKY81452) += sky81452-regulator.o
107107
obj-$(CONFIG_REGULATOR_STM32_VREFBUF) += stm32-vrefbuf.o
108+
obj-$(CONFIG_REGULATOR_STM32_PWR) += stm32-pwr.o
108109
obj-$(CONFIG_REGULATOR_STPMIC1) += stpmic1_regulator.o
109110
obj-$(CONFIG_REGULATOR_STW481X_VMMC) += stw481x-vmmc.o
110111
obj-$(CONFIG_REGULATOR_SY8106A) += sy8106a-regulator.o

drivers/regulator/stm32-pwr.c

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) STMicroelectronics 2019
3+
// Authors: Gabriel Fernandez <[email protected]>
4+
// Pascal Paillet <[email protected]>.
5+
6+
#include <linux/io.h>
7+
#include <linux/iopoll.h>
8+
#include <linux/module.h>
9+
#include <linux/of_address.h>
10+
#include <linux/of_device.h>
11+
#include <linux/platform_device.h>
12+
#include <linux/regulator/driver.h>
13+
#include <linux/regulator/of_regulator.h>
14+
15+
/*
16+
* Registers description
17+
*/
18+
#define REG_PWR_CR3 0x0C
19+
20+
#define USB_3_3_EN BIT(24)
21+
#define USB_3_3_RDY BIT(26)
22+
#define REG_1_8_EN BIT(28)
23+
#define REG_1_8_RDY BIT(29)
24+
#define REG_1_1_EN BIT(30)
25+
#define REG_1_1_RDY BIT(31)
26+
27+
/* list of supported regulators */
28+
enum {
29+
PWR_REG11,
30+
PWR_REG18,
31+
PWR_USB33,
32+
STM32PWR_REG_NUM_REGS
33+
};
34+
35+
u32 ready_mask_table[STM32PWR_REG_NUM_REGS] = {
36+
[PWR_REG11] = REG_1_1_RDY,
37+
[PWR_REG18] = REG_1_8_RDY,
38+
[PWR_USB33] = USB_3_3_RDY,
39+
};
40+
41+
struct stm32_pwr_reg {
42+
void __iomem *base;
43+
const struct regulator_desc *desc;
44+
u32 ready_mask;
45+
};
46+
47+
int stm32_pwr_reg_is_ready(struct regulator_dev *rdev)
48+
{
49+
struct stm32_pwr_reg *priv = rdev_get_drvdata(rdev);
50+
u32 val;
51+
52+
val = readl_relaxed(priv->base + REG_PWR_CR3);
53+
54+
return (val & priv->ready_mask);
55+
}
56+
57+
int stm32_pwr_reg_is_enabled(struct regulator_dev *rdev)
58+
{
59+
struct stm32_pwr_reg *priv = rdev_get_drvdata(rdev);
60+
u32 val;
61+
62+
val = readl_relaxed(priv->base + REG_PWR_CR3);
63+
64+
return (val & priv->desc->enable_mask);
65+
}
66+
67+
static int stm32_pwr_reg_enable(struct regulator_dev *rdev)
68+
{
69+
struct stm32_pwr_reg *priv = rdev_get_drvdata(rdev);
70+
int ret;
71+
u32 val;
72+
73+
val = readl_relaxed(priv->base + REG_PWR_CR3);
74+
val |= priv->desc->enable_mask;
75+
writel_relaxed(val, priv->base + REG_PWR_CR3);
76+
77+
/* use an arbitrary timeout of 20ms */
78+
ret = readx_poll_timeout(stm32_pwr_reg_is_ready, rdev, val, val,
79+
100, 20 * 1000);
80+
if (ret)
81+
dev_err(&rdev->dev, "regulator enable timed out!\n");
82+
83+
return ret;
84+
}
85+
86+
static int stm32_pwr_reg_disable(struct regulator_dev *rdev)
87+
{
88+
struct stm32_pwr_reg *priv = rdev_get_drvdata(rdev);
89+
int ret;
90+
u32 val;
91+
92+
val = readl_relaxed(priv->base + REG_PWR_CR3);
93+
val &= ~priv->desc->enable_mask;
94+
writel_relaxed(val, priv->base + REG_PWR_CR3);
95+
96+
/* use an arbitrary timeout of 20ms */
97+
ret = readx_poll_timeout(stm32_pwr_reg_is_ready, rdev, val, !val,
98+
100, 20 * 1000);
99+
if (ret)
100+
dev_err(&rdev->dev, "regulator disable timed out!\n");
101+
102+
return ret;
103+
}
104+
105+
static const struct regulator_ops stm32_pwr_reg_ops = {
106+
.list_voltage = regulator_list_voltage_linear,
107+
.enable = stm32_pwr_reg_enable,
108+
.disable = stm32_pwr_reg_disable,
109+
.is_enabled = stm32_pwr_reg_is_enabled,
110+
};
111+
112+
#define PWR_REG(_id, _name, _volt, _en, _supply) \
113+
[_id] = { \
114+
.id = _id, \
115+
.name = _name, \
116+
.of_match = of_match_ptr(_name), \
117+
.n_voltages = 1, \
118+
.type = REGULATOR_VOLTAGE, \
119+
.min_uV = _volt, \
120+
.fixed_uV = _volt, \
121+
.ops = &stm32_pwr_reg_ops, \
122+
.enable_mask = _en, \
123+
.owner = THIS_MODULE, \
124+
.supply_name = _supply, \
125+
} \
126+
127+
static const struct regulator_desc stm32_pwr_desc[] = {
128+
PWR_REG(PWR_REG11, "reg11", 1100000, REG_1_1_EN, "vdd"),
129+
PWR_REG(PWR_REG18, "reg18", 1800000, REG_1_8_EN, "vdd"),
130+
PWR_REG(PWR_USB33, "usb33", 3300000, USB_3_3_EN, "vdd_3v3_usbfs"),
131+
};
132+
133+
static int stm32_pwr_regulator_probe(struct platform_device *pdev)
134+
{
135+
struct device_node *np = pdev->dev.of_node;
136+
struct stm32_pwr_reg *priv;
137+
void __iomem *base;
138+
struct regulator_dev *rdev;
139+
struct regulator_config config = { };
140+
int i, ret = 0;
141+
142+
base = of_iomap(np, 0);
143+
if (IS_ERR(base)) {
144+
dev_err(&pdev->dev, "Unable to map IO memory\n");
145+
return PTR_ERR(base);
146+
}
147+
148+
config.dev = &pdev->dev;
149+
150+
for (i = 0; i < STM32PWR_REG_NUM_REGS; i++) {
151+
priv = devm_kzalloc(&pdev->dev, sizeof(struct stm32_pwr_reg),
152+
GFP_KERNEL);
153+
if (!priv)
154+
return -ENOMEM;
155+
priv->base = base;
156+
priv->desc = &stm32_pwr_desc[i];
157+
priv->ready_mask = ready_mask_table[i];
158+
config.driver_data = priv;
159+
160+
rdev = devm_regulator_register(&pdev->dev,
161+
&stm32_pwr_desc[i],
162+
&config);
163+
if (IS_ERR(rdev)) {
164+
ret = PTR_ERR(rdev);
165+
dev_err(&pdev->dev,
166+
"Failed to register regulator: %d\n", ret);
167+
break;
168+
}
169+
}
170+
return ret;
171+
}
172+
173+
static const struct of_device_id stm32_pwr_of_match[] = {
174+
{ .compatible = "st,stm32mp1,pwr-reg", },
175+
{},
176+
};
177+
MODULE_DEVICE_TABLE(of, stm32_pwr_of_match);
178+
179+
static struct platform_driver stm32_pwr_driver = {
180+
.probe = stm32_pwr_regulator_probe,
181+
.driver = {
182+
.name = "stm32-pwr-regulator",
183+
.of_match_table = of_match_ptr(stm32_pwr_of_match),
184+
},
185+
};
186+
module_platform_driver(stm32_pwr_driver);
187+
188+
MODULE_DESCRIPTION("STM32MP1 PWR voltage regulator driver");
189+
MODULE_AUTHOR("Pascal Paillet <[email protected]>");
190+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)