xref: /openbmc/u-boot/drivers/power/regulator/stpmu1.c (revision 844f9bf1eea75ff1ac1b1552ec4cac32ba829abb)
1069f0b63SChristophe Kerello // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2069f0b63SChristophe Kerello /*
3069f0b63SChristophe Kerello  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4069f0b63SChristophe Kerello  * Author: Christophe Kerello <christophe.kerello@st.com>
5069f0b63SChristophe Kerello  */
6069f0b63SChristophe Kerello 
7069f0b63SChristophe Kerello #include <common.h>
8069f0b63SChristophe Kerello #include <dm.h>
9069f0b63SChristophe Kerello #include <errno.h>
10069f0b63SChristophe Kerello #include <power/pmic.h>
11069f0b63SChristophe Kerello #include <power/regulator.h>
12069f0b63SChristophe Kerello #include <power/stpmu1.h>
13069f0b63SChristophe Kerello 
14069f0b63SChristophe Kerello struct stpmu1_range {
15069f0b63SChristophe Kerello 	int min_uv;
16069f0b63SChristophe Kerello 	int min_sel;
17069f0b63SChristophe Kerello 	int max_sel;
18069f0b63SChristophe Kerello 	int step;
19069f0b63SChristophe Kerello };
20069f0b63SChristophe Kerello 
21069f0b63SChristophe Kerello struct stpmu1_output_range {
22069f0b63SChristophe Kerello 	const struct stpmu1_range *ranges;
23069f0b63SChristophe Kerello 	int nbranges;
24069f0b63SChristophe Kerello };
25069f0b63SChristophe Kerello 
26069f0b63SChristophe Kerello #define STPMU1_MODE(_id, _val, _name) { \
27069f0b63SChristophe Kerello 	.id = _id,			\
28069f0b63SChristophe Kerello 	.register_value = _val,		\
29069f0b63SChristophe Kerello 	.name = _name,			\
30069f0b63SChristophe Kerello }
31069f0b63SChristophe Kerello 
32069f0b63SChristophe Kerello #define STPMU1_RANGE(_min_uv, _min_sel, _max_sel, _step) { \
33069f0b63SChristophe Kerello 	.min_uv = _min_uv,		\
34069f0b63SChristophe Kerello 	.min_sel = _min_sel,		\
35069f0b63SChristophe Kerello 	.max_sel = _max_sel,		\
36069f0b63SChristophe Kerello 	.step = _step,			\
37069f0b63SChristophe Kerello }
38069f0b63SChristophe Kerello 
39069f0b63SChristophe Kerello #define STPMU1_OUTPUT_RANGE(_ranges, _nbranges) { \
40069f0b63SChristophe Kerello 	.ranges = _ranges,		\
41069f0b63SChristophe Kerello 	.nbranges = _nbranges,		\
42069f0b63SChristophe Kerello }
43069f0b63SChristophe Kerello 
stpmu1_output_find_uv(int sel,const struct stpmu1_output_range * output_range)44069f0b63SChristophe Kerello static int stpmu1_output_find_uv(int sel,
45069f0b63SChristophe Kerello 				 const struct stpmu1_output_range *output_range)
46069f0b63SChristophe Kerello {
47069f0b63SChristophe Kerello 	const struct stpmu1_range *range;
48069f0b63SChristophe Kerello 	int i;
49069f0b63SChristophe Kerello 
50069f0b63SChristophe Kerello 	for (i = 0, range = output_range->ranges;
51069f0b63SChristophe Kerello 	     i < output_range->nbranges; i++, range++) {
52069f0b63SChristophe Kerello 		if (sel >= range->min_sel && sel <= range->max_sel)
53069f0b63SChristophe Kerello 			return range->min_uv +
54069f0b63SChristophe Kerello 			       (sel - range->min_sel) * range->step;
55069f0b63SChristophe Kerello 	}
56069f0b63SChristophe Kerello 
57069f0b63SChristophe Kerello 	return -EINVAL;
58069f0b63SChristophe Kerello }
59069f0b63SChristophe Kerello 
stpmu1_output_find_sel(int uv,const struct stpmu1_output_range * output_range)60069f0b63SChristophe Kerello static int stpmu1_output_find_sel(int uv,
61069f0b63SChristophe Kerello 				  const struct stpmu1_output_range *output_range)
62069f0b63SChristophe Kerello {
63069f0b63SChristophe Kerello 	const struct stpmu1_range *range;
64069f0b63SChristophe Kerello 	int i;
65069f0b63SChristophe Kerello 
66069f0b63SChristophe Kerello 	for (i = 0, range = output_range->ranges;
67069f0b63SChristophe Kerello 	     i < output_range->nbranges; i++, range++) {
68069f0b63SChristophe Kerello 		if (uv == range->min_uv && !range->step)
69069f0b63SChristophe Kerello 			return range->min_sel;
70069f0b63SChristophe Kerello 
71069f0b63SChristophe Kerello 		if (uv >= range->min_uv &&
72069f0b63SChristophe Kerello 		    uv <= range->min_uv +
73069f0b63SChristophe Kerello 			  (range->max_sel - range->min_sel) * range->step)
74069f0b63SChristophe Kerello 			return range->min_sel +
75069f0b63SChristophe Kerello 			       (uv - range->min_uv) / range->step;
76069f0b63SChristophe Kerello 	}
77069f0b63SChristophe Kerello 
78069f0b63SChristophe Kerello 	return -EINVAL;
79069f0b63SChristophe Kerello }
80069f0b63SChristophe Kerello 
81069f0b63SChristophe Kerello /*
82069f0b63SChristophe Kerello  * BUCK regulators
83069f0b63SChristophe Kerello  */
84069f0b63SChristophe Kerello 
85069f0b63SChristophe Kerello static const struct stpmu1_range buck1_ranges[] = {
86069f0b63SChristophe Kerello 	STPMU1_RANGE(600000, 0, 30, 25000),
87069f0b63SChristophe Kerello 	STPMU1_RANGE(1350000, 31, 63, 0),
88069f0b63SChristophe Kerello };
89069f0b63SChristophe Kerello 
90069f0b63SChristophe Kerello static const struct stpmu1_range buck2_ranges[] = {
91069f0b63SChristophe Kerello 	STPMU1_RANGE(1000000, 0, 17, 0),
92069f0b63SChristophe Kerello 	STPMU1_RANGE(1050000, 18, 19, 0),
93069f0b63SChristophe Kerello 	STPMU1_RANGE(1100000, 20, 21, 0),
94069f0b63SChristophe Kerello 	STPMU1_RANGE(1150000, 22, 23, 0),
95069f0b63SChristophe Kerello 	STPMU1_RANGE(1200000, 24, 25, 0),
96069f0b63SChristophe Kerello 	STPMU1_RANGE(1250000, 26, 27, 0),
97069f0b63SChristophe Kerello 	STPMU1_RANGE(1300000, 28, 29, 0),
98069f0b63SChristophe Kerello 	STPMU1_RANGE(1350000, 30, 31, 0),
99069f0b63SChristophe Kerello 	STPMU1_RANGE(1400000, 32, 33, 0),
100069f0b63SChristophe Kerello 	STPMU1_RANGE(1450000, 34, 35, 0),
101069f0b63SChristophe Kerello 	STPMU1_RANGE(1500000, 36, 63, 0),
102069f0b63SChristophe Kerello };
103069f0b63SChristophe Kerello 
104069f0b63SChristophe Kerello static const struct stpmu1_range buck3_ranges[] = {
105069f0b63SChristophe Kerello 	STPMU1_RANGE(1000000, 0, 19, 0),
106069f0b63SChristophe Kerello 	STPMU1_RANGE(1100000, 20, 23, 0),
107069f0b63SChristophe Kerello 	STPMU1_RANGE(1200000, 24, 27, 0),
108069f0b63SChristophe Kerello 	STPMU1_RANGE(1300000, 28, 31, 0),
109069f0b63SChristophe Kerello 	STPMU1_RANGE(1400000, 32, 35, 0),
110069f0b63SChristophe Kerello 	STPMU1_RANGE(1500000, 36, 55, 100000),
111069f0b63SChristophe Kerello 	STPMU1_RANGE(3400000, 56, 63, 0),
112069f0b63SChristophe Kerello };
113069f0b63SChristophe Kerello 
114069f0b63SChristophe Kerello static const struct stpmu1_range buck4_ranges[] = {
115069f0b63SChristophe Kerello 	STPMU1_RANGE(600000, 0, 27, 25000),
116069f0b63SChristophe Kerello 	STPMU1_RANGE(1300000, 28, 29, 0),
117069f0b63SChristophe Kerello 	STPMU1_RANGE(1350000, 30, 31, 0),
118069f0b63SChristophe Kerello 	STPMU1_RANGE(1400000, 32, 33, 0),
119069f0b63SChristophe Kerello 	STPMU1_RANGE(1450000, 34, 35, 0),
120069f0b63SChristophe Kerello 	STPMU1_RANGE(1500000, 36, 60, 100000),
121069f0b63SChristophe Kerello 	STPMU1_RANGE(3900000, 61, 63, 0),
122069f0b63SChristophe Kerello };
123069f0b63SChristophe Kerello 
124069f0b63SChristophe Kerello /* BUCK: 1,2,3,4 - voltage ranges */
125069f0b63SChristophe Kerello static const struct stpmu1_output_range buck_voltage_range[] = {
126069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(buck1_ranges, ARRAY_SIZE(buck1_ranges)),
127069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(buck2_ranges, ARRAY_SIZE(buck2_ranges)),
128069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(buck3_ranges, ARRAY_SIZE(buck3_ranges)),
129069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(buck4_ranges, ARRAY_SIZE(buck4_ranges)),
130069f0b63SChristophe Kerello };
131069f0b63SChristophe Kerello 
132069f0b63SChristophe Kerello /* BUCK modes */
133069f0b63SChristophe Kerello static const struct dm_regulator_mode buck_modes[] = {
134069f0b63SChristophe Kerello 	STPMU1_MODE(STPMU1_BUCK_MODE_HP, STPMU1_BUCK_MODE_HP, "HP"),
135069f0b63SChristophe Kerello 	STPMU1_MODE(STPMU1_BUCK_MODE_LP, STPMU1_BUCK_MODE_LP, "LP"),
136069f0b63SChristophe Kerello };
137069f0b63SChristophe Kerello 
stpmu1_buck_get_uv(struct udevice * dev,int buck)138069f0b63SChristophe Kerello static int stpmu1_buck_get_uv(struct udevice *dev, int buck)
139069f0b63SChristophe Kerello {
140069f0b63SChristophe Kerello 	int sel;
141069f0b63SChristophe Kerello 
142069f0b63SChristophe Kerello 	sel = pmic_reg_read(dev, STPMU1_BUCKX_CTRL_REG(buck));
143069f0b63SChristophe Kerello 	if (sel < 0)
144069f0b63SChristophe Kerello 		return sel;
145069f0b63SChristophe Kerello 
146069f0b63SChristophe Kerello 	sel &= STPMU1_BUCK_OUTPUT_MASK;
147069f0b63SChristophe Kerello 	sel >>= STPMU1_BUCK_OUTPUT_SHIFT;
148069f0b63SChristophe Kerello 
149069f0b63SChristophe Kerello 	return stpmu1_output_find_uv(sel, &buck_voltage_range[buck]);
150069f0b63SChristophe Kerello }
151069f0b63SChristophe Kerello 
stpmu1_buck_get_value(struct udevice * dev)152069f0b63SChristophe Kerello static int stpmu1_buck_get_value(struct udevice *dev)
153069f0b63SChristophe Kerello {
154069f0b63SChristophe Kerello 	return stpmu1_buck_get_uv(dev->parent, dev->driver_data - 1);
155069f0b63SChristophe Kerello }
156069f0b63SChristophe Kerello 
stpmu1_buck_set_value(struct udevice * dev,int uv)157069f0b63SChristophe Kerello static int stpmu1_buck_set_value(struct udevice *dev, int uv)
158069f0b63SChristophe Kerello {
159069f0b63SChristophe Kerello 	int sel, buck = dev->driver_data - 1;
160069f0b63SChristophe Kerello 
161069f0b63SChristophe Kerello 	sel = stpmu1_output_find_sel(uv, &buck_voltage_range[buck]);
162069f0b63SChristophe Kerello 	if (sel < 0)
163069f0b63SChristophe Kerello 		return sel;
164069f0b63SChristophe Kerello 
165069f0b63SChristophe Kerello 	return pmic_clrsetbits(dev->parent,
166069f0b63SChristophe Kerello 			       STPMU1_BUCKX_CTRL_REG(buck),
167069f0b63SChristophe Kerello 			       STPMU1_BUCK_OUTPUT_MASK,
168069f0b63SChristophe Kerello 			       sel << STPMU1_BUCK_OUTPUT_SHIFT);
169069f0b63SChristophe Kerello }
170069f0b63SChristophe Kerello 
stpmu1_buck_get_enable(struct udevice * dev)171069f0b63SChristophe Kerello static int stpmu1_buck_get_enable(struct udevice *dev)
172069f0b63SChristophe Kerello {
173069f0b63SChristophe Kerello 	int ret;
174069f0b63SChristophe Kerello 
175069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent,
176069f0b63SChristophe Kerello 			    STPMU1_BUCKX_CTRL_REG(dev->driver_data - 1));
177069f0b63SChristophe Kerello 	if (ret < 0)
178069f0b63SChristophe Kerello 		return false;
179069f0b63SChristophe Kerello 
180069f0b63SChristophe Kerello 	return ret & STPMU1_BUCK_EN ? true : false;
181069f0b63SChristophe Kerello }
182069f0b63SChristophe Kerello 
stpmu1_buck_set_enable(struct udevice * dev,bool enable)183069f0b63SChristophe Kerello static int stpmu1_buck_set_enable(struct udevice *dev, bool enable)
184069f0b63SChristophe Kerello {
185069f0b63SChristophe Kerello 	struct dm_regulator_uclass_platdata *uc_pdata;
186*844f9bf1SChristophe Kerello 	int delay = enable ? STPMU1_DEFAULT_START_UP_DELAY_MS :
187*844f9bf1SChristophe Kerello 			     STPMU1_DEFAULT_STOP_DELAY_MS;
188069f0b63SChristophe Kerello 	int ret, uv;
189069f0b63SChristophe Kerello 
190069f0b63SChristophe Kerello 	/* if regulator is already in the wanted state, nothing to do */
191069f0b63SChristophe Kerello 	if (stpmu1_buck_get_enable(dev) == enable)
192069f0b63SChristophe Kerello 		return 0;
193069f0b63SChristophe Kerello 
194069f0b63SChristophe Kerello 	if (enable) {
195069f0b63SChristophe Kerello 		uc_pdata = dev_get_uclass_platdata(dev);
196069f0b63SChristophe Kerello 		uv = stpmu1_buck_get_value(dev);
197069f0b63SChristophe Kerello 		if ((uv < uc_pdata->min_uV) || (uv > uc_pdata->max_uV))
198069f0b63SChristophe Kerello 			stpmu1_buck_set_value(dev, uc_pdata->min_uV);
199069f0b63SChristophe Kerello 	}
200069f0b63SChristophe Kerello 
201069f0b63SChristophe Kerello 	ret = pmic_clrsetbits(dev->parent,
202069f0b63SChristophe Kerello 			      STPMU1_BUCKX_CTRL_REG(dev->driver_data - 1),
203069f0b63SChristophe Kerello 			      STPMU1_BUCK_EN, enable ? STPMU1_BUCK_EN : 0);
204*844f9bf1SChristophe Kerello 	mdelay(delay);
205069f0b63SChristophe Kerello 
206069f0b63SChristophe Kerello 	return ret;
207069f0b63SChristophe Kerello }
208069f0b63SChristophe Kerello 
stpmu1_buck_get_mode(struct udevice * dev)209069f0b63SChristophe Kerello static int stpmu1_buck_get_mode(struct udevice *dev)
210069f0b63SChristophe Kerello {
211069f0b63SChristophe Kerello 	int ret;
212069f0b63SChristophe Kerello 
213069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent,
214069f0b63SChristophe Kerello 			    STPMU1_BUCKX_CTRL_REG(dev->driver_data - 1));
215069f0b63SChristophe Kerello 	if (ret < 0)
216069f0b63SChristophe Kerello 		return ret;
217069f0b63SChristophe Kerello 
218069f0b63SChristophe Kerello 	return ret & STPMU1_BUCK_MODE ? STPMU1_BUCK_MODE_LP :
219069f0b63SChristophe Kerello 					 STPMU1_BUCK_MODE_HP;
220069f0b63SChristophe Kerello }
221069f0b63SChristophe Kerello 
stpmu1_buck_set_mode(struct udevice * dev,int mode)222069f0b63SChristophe Kerello static int stpmu1_buck_set_mode(struct udevice *dev, int mode)
223069f0b63SChristophe Kerello {
224069f0b63SChristophe Kerello 	return pmic_clrsetbits(dev->parent,
225069f0b63SChristophe Kerello 			       STPMU1_BUCKX_CTRL_REG(dev->driver_data - 1),
226069f0b63SChristophe Kerello 			       STPMU1_BUCK_MODE,
227069f0b63SChristophe Kerello 			       mode ? STPMU1_BUCK_MODE : 0);
228069f0b63SChristophe Kerello }
229069f0b63SChristophe Kerello 
stpmu1_buck_probe(struct udevice * dev)230069f0b63SChristophe Kerello static int stpmu1_buck_probe(struct udevice *dev)
231069f0b63SChristophe Kerello {
232069f0b63SChristophe Kerello 	struct dm_regulator_uclass_platdata *uc_pdata;
233069f0b63SChristophe Kerello 
234069f0b63SChristophe Kerello 	if (!dev->driver_data || dev->driver_data > STPMU1_MAX_BUCK)
235069f0b63SChristophe Kerello 		return -EINVAL;
236069f0b63SChristophe Kerello 
237069f0b63SChristophe Kerello 	uc_pdata = dev_get_uclass_platdata(dev);
238069f0b63SChristophe Kerello 
239069f0b63SChristophe Kerello 	uc_pdata->type = REGULATOR_TYPE_BUCK;
240069f0b63SChristophe Kerello 	uc_pdata->mode = (struct dm_regulator_mode *)buck_modes;
241069f0b63SChristophe Kerello 	uc_pdata->mode_count = ARRAY_SIZE(buck_modes);
242069f0b63SChristophe Kerello 
243069f0b63SChristophe Kerello 	return 0;
244069f0b63SChristophe Kerello }
245069f0b63SChristophe Kerello 
246069f0b63SChristophe Kerello static const struct dm_regulator_ops stpmu1_buck_ops = {
247069f0b63SChristophe Kerello 	.get_value  = stpmu1_buck_get_value,
248069f0b63SChristophe Kerello 	.set_value  = stpmu1_buck_set_value,
249069f0b63SChristophe Kerello 	.get_enable = stpmu1_buck_get_enable,
250069f0b63SChristophe Kerello 	.set_enable = stpmu1_buck_set_enable,
251069f0b63SChristophe Kerello 	.get_mode   = stpmu1_buck_get_mode,
252069f0b63SChristophe Kerello 	.set_mode   = stpmu1_buck_set_mode,
253069f0b63SChristophe Kerello };
254069f0b63SChristophe Kerello 
255069f0b63SChristophe Kerello U_BOOT_DRIVER(stpmu1_buck) = {
256069f0b63SChristophe Kerello 	.name = "stpmu1_buck",
257069f0b63SChristophe Kerello 	.id = UCLASS_REGULATOR,
258069f0b63SChristophe Kerello 	.ops = &stpmu1_buck_ops,
259069f0b63SChristophe Kerello 	.probe = stpmu1_buck_probe,
260069f0b63SChristophe Kerello };
261069f0b63SChristophe Kerello 
262069f0b63SChristophe Kerello /*
263069f0b63SChristophe Kerello  * LDO regulators
264069f0b63SChristophe Kerello  */
265069f0b63SChristophe Kerello 
266069f0b63SChristophe Kerello static const struct stpmu1_range ldo12_ranges[] = {
267069f0b63SChristophe Kerello 	STPMU1_RANGE(1700000, 0, 7, 0),
268069f0b63SChristophe Kerello 	STPMU1_RANGE(1700000, 8, 24, 100000),
269069f0b63SChristophe Kerello 	STPMU1_RANGE(3300000, 25, 31, 0),
270069f0b63SChristophe Kerello };
271069f0b63SChristophe Kerello 
272069f0b63SChristophe Kerello static const struct stpmu1_range ldo3_ranges[] = {
273069f0b63SChristophe Kerello 	STPMU1_RANGE(1700000, 0, 7, 0),
274069f0b63SChristophe Kerello 	STPMU1_RANGE(1700000, 8, 24, 100000),
275069f0b63SChristophe Kerello 	STPMU1_RANGE(3300000, 25, 30, 0),
276069f0b63SChristophe Kerello 	/* Sel 31 is special case when LDO3 is in mode sync_source (BUCK2/2) */
277069f0b63SChristophe Kerello };
278069f0b63SChristophe Kerello 
279069f0b63SChristophe Kerello static const struct stpmu1_range ldo5_ranges[] = {
280069f0b63SChristophe Kerello 	STPMU1_RANGE(1700000, 0, 7, 0),
281069f0b63SChristophe Kerello 	STPMU1_RANGE(1700000, 8, 30, 100000),
282069f0b63SChristophe Kerello 	STPMU1_RANGE(3900000, 31, 31, 0),
283069f0b63SChristophe Kerello };
284069f0b63SChristophe Kerello 
285069f0b63SChristophe Kerello static const struct stpmu1_range ldo6_ranges[] = {
286069f0b63SChristophe Kerello 	STPMU1_RANGE(900000, 0, 24, 100000),
287069f0b63SChristophe Kerello 	STPMU1_RANGE(3300000, 25, 31, 0),
288069f0b63SChristophe Kerello };
289069f0b63SChristophe Kerello 
290069f0b63SChristophe Kerello /* LDO: 1,2,3,4,5,6 - voltage ranges */
291069f0b63SChristophe Kerello static const struct stpmu1_output_range ldo_voltage_range[] = {
292069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(ldo12_ranges, ARRAY_SIZE(ldo12_ranges)),
293069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(ldo12_ranges, ARRAY_SIZE(ldo12_ranges)),
294069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(ldo3_ranges, ARRAY_SIZE(ldo3_ranges)),
295069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(NULL, 0),
296069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(ldo5_ranges, ARRAY_SIZE(ldo5_ranges)),
297069f0b63SChristophe Kerello 	STPMU1_OUTPUT_RANGE(ldo6_ranges, ARRAY_SIZE(ldo6_ranges)),
298069f0b63SChristophe Kerello };
299069f0b63SChristophe Kerello 
300069f0b63SChristophe Kerello /* LDO modes */
301069f0b63SChristophe Kerello static const struct dm_regulator_mode ldo_modes[] = {
302069f0b63SChristophe Kerello 	STPMU1_MODE(STPMU1_LDO_MODE_NORMAL,
303069f0b63SChristophe Kerello 		    STPMU1_LDO_MODE_NORMAL, "NORMAL"),
304069f0b63SChristophe Kerello 	STPMU1_MODE(STPMU1_LDO_MODE_BYPASS,
305069f0b63SChristophe Kerello 		    STPMU1_LDO_MODE_BYPASS, "BYPASS"),
306069f0b63SChristophe Kerello 	STPMU1_MODE(STPMU1_LDO_MODE_SINK_SOURCE,
307069f0b63SChristophe Kerello 		    STPMU1_LDO_MODE_SINK_SOURCE, "SINK SOURCE"),
308069f0b63SChristophe Kerello };
309069f0b63SChristophe Kerello 
stpmu1_ldo_get_value(struct udevice * dev)310069f0b63SChristophe Kerello static int stpmu1_ldo_get_value(struct udevice *dev)
311069f0b63SChristophe Kerello {
312069f0b63SChristophe Kerello 	int sel, ldo = dev->driver_data - 1;
313069f0b63SChristophe Kerello 
314069f0b63SChristophe Kerello 	sel = pmic_reg_read(dev->parent, STPMU1_LDOX_CTRL_REG(ldo));
315069f0b63SChristophe Kerello 	if (sel < 0)
316069f0b63SChristophe Kerello 		return sel;
317069f0b63SChristophe Kerello 
318069f0b63SChristophe Kerello 	/* ldo4 => 3,3V */
319069f0b63SChristophe Kerello 	if (ldo == STPMU1_LDO4)
320069f0b63SChristophe Kerello 		return STPMU1_LDO4_UV;
321069f0b63SChristophe Kerello 
322069f0b63SChristophe Kerello 	sel &= STPMU1_LDO12356_OUTPUT_MASK;
323069f0b63SChristophe Kerello 	sel >>= STPMU1_LDO12356_OUTPUT_SHIFT;
324069f0b63SChristophe Kerello 
325069f0b63SChristophe Kerello 	/* ldo3, sel = 31 => BUCK2/2 */
326069f0b63SChristophe Kerello 	if (ldo == STPMU1_LDO3 && sel == STPMU1_LDO3_DDR_SEL)
327069f0b63SChristophe Kerello 		return stpmu1_buck_get_uv(dev->parent, STPMU1_BUCK2) / 2;
328069f0b63SChristophe Kerello 
329069f0b63SChristophe Kerello 	return stpmu1_output_find_uv(sel, &ldo_voltage_range[ldo]);
330069f0b63SChristophe Kerello }
331069f0b63SChristophe Kerello 
stpmu1_ldo_set_value(struct udevice * dev,int uv)332069f0b63SChristophe Kerello static int stpmu1_ldo_set_value(struct udevice *dev, int uv)
333069f0b63SChristophe Kerello {
334069f0b63SChristophe Kerello 	int sel, ldo = dev->driver_data - 1;
335069f0b63SChristophe Kerello 
336069f0b63SChristophe Kerello 	/* ldo4 => not possible */
337069f0b63SChristophe Kerello 	if (ldo == STPMU1_LDO4)
338069f0b63SChristophe Kerello 		return -EINVAL;
339069f0b63SChristophe Kerello 
340069f0b63SChristophe Kerello 	sel = stpmu1_output_find_sel(uv, &ldo_voltage_range[ldo]);
341069f0b63SChristophe Kerello 	if (sel < 0)
342069f0b63SChristophe Kerello 		return sel;
343069f0b63SChristophe Kerello 
344069f0b63SChristophe Kerello 	return pmic_clrsetbits(dev->parent,
345069f0b63SChristophe Kerello 			       STPMU1_LDOX_CTRL_REG(ldo),
346069f0b63SChristophe Kerello 			       STPMU1_LDO12356_OUTPUT_MASK,
347069f0b63SChristophe Kerello 			       sel << STPMU1_LDO12356_OUTPUT_SHIFT);
348069f0b63SChristophe Kerello }
349069f0b63SChristophe Kerello 
stpmu1_ldo_get_enable(struct udevice * dev)350069f0b63SChristophe Kerello static int stpmu1_ldo_get_enable(struct udevice *dev)
351069f0b63SChristophe Kerello {
352069f0b63SChristophe Kerello 	int ret;
353069f0b63SChristophe Kerello 
354069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent,
355069f0b63SChristophe Kerello 			    STPMU1_LDOX_CTRL_REG(dev->driver_data - 1));
356069f0b63SChristophe Kerello 	if (ret < 0)
357069f0b63SChristophe Kerello 		return false;
358069f0b63SChristophe Kerello 
359069f0b63SChristophe Kerello 	return ret & STPMU1_LDO_EN ? true : false;
360069f0b63SChristophe Kerello }
361069f0b63SChristophe Kerello 
stpmu1_ldo_set_enable(struct udevice * dev,bool enable)362069f0b63SChristophe Kerello static int stpmu1_ldo_set_enable(struct udevice *dev, bool enable)
363069f0b63SChristophe Kerello {
364069f0b63SChristophe Kerello 	struct dm_regulator_uclass_platdata *uc_pdata;
365*844f9bf1SChristophe Kerello 	int delay = enable ? STPMU1_DEFAULT_START_UP_DELAY_MS :
366*844f9bf1SChristophe Kerello 			     STPMU1_DEFAULT_STOP_DELAY_MS;
367069f0b63SChristophe Kerello 	int ret, uv;
368069f0b63SChristophe Kerello 
369069f0b63SChristophe Kerello 	/* if regulator is already in the wanted state, nothing to do */
370069f0b63SChristophe Kerello 	if (stpmu1_ldo_get_enable(dev) == enable)
371069f0b63SChristophe Kerello 		return 0;
372069f0b63SChristophe Kerello 
373069f0b63SChristophe Kerello 	if (enable) {
374069f0b63SChristophe Kerello 		uc_pdata = dev_get_uclass_platdata(dev);
375069f0b63SChristophe Kerello 		uv = stpmu1_ldo_get_value(dev);
376069f0b63SChristophe Kerello 		if ((uv < uc_pdata->min_uV) || (uv > uc_pdata->max_uV))
377069f0b63SChristophe Kerello 			stpmu1_ldo_set_value(dev, uc_pdata->min_uV);
378069f0b63SChristophe Kerello 	}
379069f0b63SChristophe Kerello 
380069f0b63SChristophe Kerello 	ret = pmic_clrsetbits(dev->parent,
381069f0b63SChristophe Kerello 			      STPMU1_LDOX_CTRL_REG(dev->driver_data - 1),
382069f0b63SChristophe Kerello 			      STPMU1_LDO_EN, enable ? STPMU1_LDO_EN : 0);
383*844f9bf1SChristophe Kerello 	mdelay(delay);
384069f0b63SChristophe Kerello 
385069f0b63SChristophe Kerello 	return ret;
386069f0b63SChristophe Kerello }
387069f0b63SChristophe Kerello 
stpmu1_ldo_get_mode(struct udevice * dev)388069f0b63SChristophe Kerello static int stpmu1_ldo_get_mode(struct udevice *dev)
389069f0b63SChristophe Kerello {
390069f0b63SChristophe Kerello 	int ret, ldo = dev->driver_data - 1;
391069f0b63SChristophe Kerello 
392069f0b63SChristophe Kerello 	if (ldo != STPMU1_LDO3)
393069f0b63SChristophe Kerello 		return -EINVAL;
394069f0b63SChristophe Kerello 
395069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent, STPMU1_LDOX_CTRL_REG(ldo));
396069f0b63SChristophe Kerello 	if (ret < 0)
397069f0b63SChristophe Kerello 		return ret;
398069f0b63SChristophe Kerello 
399069f0b63SChristophe Kerello 	if (ret & STPMU1_LDO3_MODE)
400069f0b63SChristophe Kerello 		return STPMU1_LDO_MODE_BYPASS;
401069f0b63SChristophe Kerello 
402069f0b63SChristophe Kerello 	ret &= STPMU1_LDO12356_OUTPUT_MASK;
403069f0b63SChristophe Kerello 	ret >>= STPMU1_LDO12356_OUTPUT_SHIFT;
404069f0b63SChristophe Kerello 
405069f0b63SChristophe Kerello 	return ret == STPMU1_LDO3_DDR_SEL ? STPMU1_LDO_MODE_SINK_SOURCE :
406069f0b63SChristophe Kerello 					     STPMU1_LDO_MODE_NORMAL;
407069f0b63SChristophe Kerello }
408069f0b63SChristophe Kerello 
stpmu1_ldo_set_mode(struct udevice * dev,int mode)409069f0b63SChristophe Kerello static int stpmu1_ldo_set_mode(struct udevice *dev, int mode)
410069f0b63SChristophe Kerello {
411069f0b63SChristophe Kerello 	int ret, ldo = dev->driver_data - 1;
412069f0b63SChristophe Kerello 
413069f0b63SChristophe Kerello 	if (ldo != STPMU1_LDO3)
414069f0b63SChristophe Kerello 		return -EINVAL;
415069f0b63SChristophe Kerello 
416069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent, STPMU1_LDOX_CTRL_REG(ldo));
417069f0b63SChristophe Kerello 	if (ret < 0)
418069f0b63SChristophe Kerello 		return ret;
419069f0b63SChristophe Kerello 
420069f0b63SChristophe Kerello 	switch (mode) {
421069f0b63SChristophe Kerello 	case STPMU1_LDO_MODE_SINK_SOURCE:
422069f0b63SChristophe Kerello 		ret &= ~STPMU1_LDO12356_OUTPUT_MASK;
423069f0b63SChristophe Kerello 		ret |= STPMU1_LDO3_DDR_SEL << STPMU1_LDO12356_OUTPUT_SHIFT;
424069f0b63SChristophe Kerello 	case STPMU1_LDO_MODE_NORMAL:
425069f0b63SChristophe Kerello 		ret &= ~STPMU1_LDO3_MODE;
426069f0b63SChristophe Kerello 		break;
427069f0b63SChristophe Kerello 	case STPMU1_LDO_MODE_BYPASS:
428069f0b63SChristophe Kerello 		ret |= STPMU1_LDO3_MODE;
429069f0b63SChristophe Kerello 		break;
430069f0b63SChristophe Kerello 	}
431069f0b63SChristophe Kerello 
432069f0b63SChristophe Kerello 	return pmic_reg_write(dev->parent, STPMU1_LDOX_CTRL_REG(ldo), ret);
433069f0b63SChristophe Kerello }
434069f0b63SChristophe Kerello 
stpmu1_ldo_probe(struct udevice * dev)435069f0b63SChristophe Kerello static int stpmu1_ldo_probe(struct udevice *dev)
436069f0b63SChristophe Kerello {
437069f0b63SChristophe Kerello 	struct dm_regulator_uclass_platdata *uc_pdata;
438069f0b63SChristophe Kerello 
439069f0b63SChristophe Kerello 	if (!dev->driver_data || dev->driver_data > STPMU1_MAX_LDO)
440069f0b63SChristophe Kerello 		return -EINVAL;
441069f0b63SChristophe Kerello 
442069f0b63SChristophe Kerello 	uc_pdata = dev_get_uclass_platdata(dev);
443069f0b63SChristophe Kerello 
444069f0b63SChristophe Kerello 	uc_pdata->type = REGULATOR_TYPE_LDO;
445069f0b63SChristophe Kerello 	if (dev->driver_data - 1 == STPMU1_LDO3) {
446069f0b63SChristophe Kerello 		uc_pdata->mode = (struct dm_regulator_mode *)ldo_modes;
447069f0b63SChristophe Kerello 		uc_pdata->mode_count = ARRAY_SIZE(ldo_modes);
448069f0b63SChristophe Kerello 	} else {
449069f0b63SChristophe Kerello 		uc_pdata->mode_count = 0;
450069f0b63SChristophe Kerello 	}
451069f0b63SChristophe Kerello 
452069f0b63SChristophe Kerello 	return 0;
453069f0b63SChristophe Kerello }
454069f0b63SChristophe Kerello 
455069f0b63SChristophe Kerello static const struct dm_regulator_ops stpmu1_ldo_ops = {
456069f0b63SChristophe Kerello 	.get_value  = stpmu1_ldo_get_value,
457069f0b63SChristophe Kerello 	.set_value  = stpmu1_ldo_set_value,
458069f0b63SChristophe Kerello 	.get_enable = stpmu1_ldo_get_enable,
459069f0b63SChristophe Kerello 	.set_enable = stpmu1_ldo_set_enable,
460069f0b63SChristophe Kerello 	.get_mode   = stpmu1_ldo_get_mode,
461069f0b63SChristophe Kerello 	.set_mode   = stpmu1_ldo_set_mode,
462069f0b63SChristophe Kerello };
463069f0b63SChristophe Kerello 
464069f0b63SChristophe Kerello U_BOOT_DRIVER(stpmu1_ldo) = {
465069f0b63SChristophe Kerello 	.name = "stpmu1_ldo",
466069f0b63SChristophe Kerello 	.id = UCLASS_REGULATOR,
467069f0b63SChristophe Kerello 	.ops = &stpmu1_ldo_ops,
468069f0b63SChristophe Kerello 	.probe = stpmu1_ldo_probe,
469069f0b63SChristophe Kerello };
470069f0b63SChristophe Kerello 
471069f0b63SChristophe Kerello /*
472069f0b63SChristophe Kerello  * VREF DDR regulator
473069f0b63SChristophe Kerello  */
474069f0b63SChristophe Kerello 
stpmu1_vref_ddr_get_value(struct udevice * dev)475069f0b63SChristophe Kerello static int stpmu1_vref_ddr_get_value(struct udevice *dev)
476069f0b63SChristophe Kerello {
477069f0b63SChristophe Kerello 	/* BUCK2/2 */
478069f0b63SChristophe Kerello 	return stpmu1_buck_get_uv(dev->parent, STPMU1_BUCK2) / 2;
479069f0b63SChristophe Kerello }
480069f0b63SChristophe Kerello 
stpmu1_vref_ddr_get_enable(struct udevice * dev)481069f0b63SChristophe Kerello static int stpmu1_vref_ddr_get_enable(struct udevice *dev)
482069f0b63SChristophe Kerello {
483069f0b63SChristophe Kerello 	int ret;
484069f0b63SChristophe Kerello 
485069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent, STPMU1_VREF_CTRL_REG);
486069f0b63SChristophe Kerello 	if (ret < 0)
487069f0b63SChristophe Kerello 		return false;
488069f0b63SChristophe Kerello 
489069f0b63SChristophe Kerello 	return ret & STPMU1_VREF_EN ? true : false;
490069f0b63SChristophe Kerello }
491069f0b63SChristophe Kerello 
stpmu1_vref_ddr_set_enable(struct udevice * dev,bool enable)492069f0b63SChristophe Kerello static int stpmu1_vref_ddr_set_enable(struct udevice *dev, bool enable)
493069f0b63SChristophe Kerello {
494*844f9bf1SChristophe Kerello 	int delay = enable ? STPMU1_DEFAULT_START_UP_DELAY_MS :
495*844f9bf1SChristophe Kerello 			     STPMU1_DEFAULT_STOP_DELAY_MS;
496069f0b63SChristophe Kerello 	int ret;
497069f0b63SChristophe Kerello 
498069f0b63SChristophe Kerello 	/* if regulator is already in the wanted state, nothing to do */
499069f0b63SChristophe Kerello 	if (stpmu1_vref_ddr_get_enable(dev) == enable)
500069f0b63SChristophe Kerello 		return 0;
501069f0b63SChristophe Kerello 
502069f0b63SChristophe Kerello 	ret = pmic_clrsetbits(dev->parent, STPMU1_VREF_CTRL_REG,
503069f0b63SChristophe Kerello 			      STPMU1_VREF_EN, enable ? STPMU1_VREF_EN : 0);
504*844f9bf1SChristophe Kerello 	mdelay(delay);
505069f0b63SChristophe Kerello 
506069f0b63SChristophe Kerello 	return ret;
507069f0b63SChristophe Kerello }
508069f0b63SChristophe Kerello 
stpmu1_vref_ddr_probe(struct udevice * dev)509069f0b63SChristophe Kerello static int stpmu1_vref_ddr_probe(struct udevice *dev)
510069f0b63SChristophe Kerello {
511069f0b63SChristophe Kerello 	struct dm_regulator_uclass_platdata *uc_pdata;
512069f0b63SChristophe Kerello 
513069f0b63SChristophe Kerello 	uc_pdata = dev_get_uclass_platdata(dev);
514069f0b63SChristophe Kerello 
515069f0b63SChristophe Kerello 	uc_pdata->type = REGULATOR_TYPE_FIXED;
516069f0b63SChristophe Kerello 	uc_pdata->mode_count = 0;
517069f0b63SChristophe Kerello 
518069f0b63SChristophe Kerello 	return 0;
519069f0b63SChristophe Kerello }
520069f0b63SChristophe Kerello 
521069f0b63SChristophe Kerello static const struct dm_regulator_ops stpmu1_vref_ddr_ops = {
522069f0b63SChristophe Kerello 	.get_value  = stpmu1_vref_ddr_get_value,
523069f0b63SChristophe Kerello 	.get_enable = stpmu1_vref_ddr_get_enable,
524069f0b63SChristophe Kerello 	.set_enable = stpmu1_vref_ddr_set_enable,
525069f0b63SChristophe Kerello };
526069f0b63SChristophe Kerello 
527069f0b63SChristophe Kerello U_BOOT_DRIVER(stpmu1_vref_ddr) = {
528069f0b63SChristophe Kerello 	.name = "stpmu1_vref_ddr",
529069f0b63SChristophe Kerello 	.id = UCLASS_REGULATOR,
530069f0b63SChristophe Kerello 	.ops = &stpmu1_vref_ddr_ops,
531069f0b63SChristophe Kerello 	.probe = stpmu1_vref_ddr_probe,
532069f0b63SChristophe Kerello };
533069f0b63SChristophe Kerello 
534069f0b63SChristophe Kerello /*
535069f0b63SChristophe Kerello  * BOOST regulator
536069f0b63SChristophe Kerello  */
537069f0b63SChristophe Kerello 
stpmu1_boost_get_enable(struct udevice * dev)538069f0b63SChristophe Kerello static int stpmu1_boost_get_enable(struct udevice *dev)
539069f0b63SChristophe Kerello {
540069f0b63SChristophe Kerello 	int ret;
541069f0b63SChristophe Kerello 
542069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent, STPMU1_USB_CTRL_REG);
543069f0b63SChristophe Kerello 	if (ret < 0)
544069f0b63SChristophe Kerello 		return false;
545069f0b63SChristophe Kerello 
546069f0b63SChristophe Kerello 	return ret & STPMU1_USB_BOOST_EN ? true : false;
547069f0b63SChristophe Kerello }
548069f0b63SChristophe Kerello 
stpmu1_boost_set_enable(struct udevice * dev,bool enable)549069f0b63SChristophe Kerello static int stpmu1_boost_set_enable(struct udevice *dev, bool enable)
550069f0b63SChristophe Kerello {
551069f0b63SChristophe Kerello 	int ret;
552069f0b63SChristophe Kerello 
553069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent, STPMU1_USB_CTRL_REG);
554069f0b63SChristophe Kerello 	if (ret < 0)
555069f0b63SChristophe Kerello 		return ret;
556069f0b63SChristophe Kerello 
557069f0b63SChristophe Kerello 	if (!enable && ret & STPMU1_USB_PWR_SW_EN)
558069f0b63SChristophe Kerello 		return -EINVAL;
559069f0b63SChristophe Kerello 
560069f0b63SChristophe Kerello 	/* if regulator is already in the wanted state, nothing to do */
561069f0b63SChristophe Kerello 	if (!!(ret & STPMU1_USB_BOOST_EN) == enable)
562069f0b63SChristophe Kerello 		return 0;
563069f0b63SChristophe Kerello 
564069f0b63SChristophe Kerello 	ret = pmic_clrsetbits(dev->parent, STPMU1_USB_CTRL_REG,
565069f0b63SChristophe Kerello 			      STPMU1_USB_BOOST_EN,
566069f0b63SChristophe Kerello 			      enable ? STPMU1_USB_BOOST_EN : 0);
567069f0b63SChristophe Kerello 	if (enable)
568069f0b63SChristophe Kerello 		mdelay(STPMU1_USB_BOOST_START_UP_DELAY_MS);
569069f0b63SChristophe Kerello 
570069f0b63SChristophe Kerello 	return ret;
571069f0b63SChristophe Kerello }
572069f0b63SChristophe Kerello 
stpmu1_boost_probe(struct udevice * dev)573069f0b63SChristophe Kerello static int stpmu1_boost_probe(struct udevice *dev)
574069f0b63SChristophe Kerello {
575069f0b63SChristophe Kerello 	struct dm_regulator_uclass_platdata *uc_pdata;
576069f0b63SChristophe Kerello 
577069f0b63SChristophe Kerello 	uc_pdata = dev_get_uclass_platdata(dev);
578069f0b63SChristophe Kerello 
579069f0b63SChristophe Kerello 	uc_pdata->type = REGULATOR_TYPE_FIXED;
580069f0b63SChristophe Kerello 	uc_pdata->mode_count = 0;
581069f0b63SChristophe Kerello 
582069f0b63SChristophe Kerello 	return 0;
583069f0b63SChristophe Kerello }
584069f0b63SChristophe Kerello 
585069f0b63SChristophe Kerello static const struct dm_regulator_ops stpmu1_boost_ops = {
586069f0b63SChristophe Kerello 	.get_enable = stpmu1_boost_get_enable,
587069f0b63SChristophe Kerello 	.set_enable = stpmu1_boost_set_enable,
588069f0b63SChristophe Kerello };
589069f0b63SChristophe Kerello 
590069f0b63SChristophe Kerello U_BOOT_DRIVER(stpmu1_boost) = {
591069f0b63SChristophe Kerello 	.name = "stpmu1_boost",
592069f0b63SChristophe Kerello 	.id = UCLASS_REGULATOR,
593069f0b63SChristophe Kerello 	.ops = &stpmu1_boost_ops,
594069f0b63SChristophe Kerello 	.probe = stpmu1_boost_probe,
595069f0b63SChristophe Kerello };
596069f0b63SChristophe Kerello 
597069f0b63SChristophe Kerello /*
598069f0b63SChristophe Kerello  * USB power switch
599069f0b63SChristophe Kerello  */
600069f0b63SChristophe Kerello 
stpmu1_pwr_sw_get_enable(struct udevice * dev)601069f0b63SChristophe Kerello static int stpmu1_pwr_sw_get_enable(struct udevice *dev)
602069f0b63SChristophe Kerello {
603069f0b63SChristophe Kerello 	uint mask = 1 << dev->driver_data;
604069f0b63SChristophe Kerello 	int ret;
605069f0b63SChristophe Kerello 
606069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent, STPMU1_USB_CTRL_REG);
607069f0b63SChristophe Kerello 	if (ret < 0)
608069f0b63SChristophe Kerello 		return false;
609069f0b63SChristophe Kerello 
610069f0b63SChristophe Kerello 	return ret & mask ? true : false;
611069f0b63SChristophe Kerello }
612069f0b63SChristophe Kerello 
stpmu1_pwr_sw_set_enable(struct udevice * dev,bool enable)613069f0b63SChristophe Kerello static int stpmu1_pwr_sw_set_enable(struct udevice *dev, bool enable)
614069f0b63SChristophe Kerello {
615069f0b63SChristophe Kerello 	uint mask = 1 << dev->driver_data;
616*844f9bf1SChristophe Kerello 	int delay = enable ? STPMU1_DEFAULT_START_UP_DELAY_MS :
617*844f9bf1SChristophe Kerello 			     STPMU1_DEFAULT_STOP_DELAY_MS;
618069f0b63SChristophe Kerello 	int ret;
619069f0b63SChristophe Kerello 
620069f0b63SChristophe Kerello 	ret = pmic_reg_read(dev->parent, STPMU1_USB_CTRL_REG);
621069f0b63SChristophe Kerello 	if (ret < 0)
622069f0b63SChristophe Kerello 		return ret;
623069f0b63SChristophe Kerello 
624069f0b63SChristophe Kerello 	/* if regulator is already in the wanted state, nothing to do */
625069f0b63SChristophe Kerello 	if (!!(ret & mask) == enable)
626069f0b63SChristophe Kerello 		return 0;
627069f0b63SChristophe Kerello 
628069f0b63SChristophe Kerello 	/* Boost management */
629069f0b63SChristophe Kerello 	if (enable && !(ret & STPMU1_USB_BOOST_EN)) {
630069f0b63SChristophe Kerello 		pmic_clrsetbits(dev->parent, STPMU1_USB_CTRL_REG,
631069f0b63SChristophe Kerello 				STPMU1_USB_BOOST_EN, STPMU1_USB_BOOST_EN);
632069f0b63SChristophe Kerello 		mdelay(STPMU1_USB_BOOST_START_UP_DELAY_MS);
633069f0b63SChristophe Kerello 	} else if (!enable && ret & STPMU1_USB_BOOST_EN &&
634069f0b63SChristophe Kerello 		   (ret & STPMU1_USB_PWR_SW_EN) != STPMU1_USB_PWR_SW_EN) {
635069f0b63SChristophe Kerello 		pmic_clrsetbits(dev->parent, STPMU1_USB_CTRL_REG,
636069f0b63SChristophe Kerello 				STPMU1_USB_BOOST_EN, 0);
637069f0b63SChristophe Kerello 	}
638069f0b63SChristophe Kerello 
639069f0b63SChristophe Kerello 	ret = pmic_clrsetbits(dev->parent, STPMU1_USB_CTRL_REG,
640069f0b63SChristophe Kerello 			      mask, enable ? mask : 0);
641*844f9bf1SChristophe Kerello 	mdelay(delay);
642069f0b63SChristophe Kerello 
643069f0b63SChristophe Kerello 	return ret;
644069f0b63SChristophe Kerello }
645069f0b63SChristophe Kerello 
stpmu1_pwr_sw_probe(struct udevice * dev)646069f0b63SChristophe Kerello static int stpmu1_pwr_sw_probe(struct udevice *dev)
647069f0b63SChristophe Kerello {
648069f0b63SChristophe Kerello 	struct dm_regulator_uclass_platdata *uc_pdata;
649069f0b63SChristophe Kerello 
650069f0b63SChristophe Kerello 	if (!dev->driver_data || dev->driver_data > STPMU1_MAX_PWR_SW)
651069f0b63SChristophe Kerello 		return -EINVAL;
652069f0b63SChristophe Kerello 
653069f0b63SChristophe Kerello 	uc_pdata = dev_get_uclass_platdata(dev);
654069f0b63SChristophe Kerello 
655069f0b63SChristophe Kerello 	uc_pdata->type = REGULATOR_TYPE_FIXED;
656069f0b63SChristophe Kerello 	uc_pdata->mode_count = 0;
657069f0b63SChristophe Kerello 
658069f0b63SChristophe Kerello 	return 0;
659069f0b63SChristophe Kerello }
660069f0b63SChristophe Kerello 
661069f0b63SChristophe Kerello static const struct dm_regulator_ops stpmu1_pwr_sw_ops = {
662069f0b63SChristophe Kerello 	.get_enable = stpmu1_pwr_sw_get_enable,
663069f0b63SChristophe Kerello 	.set_enable = stpmu1_pwr_sw_set_enable,
664069f0b63SChristophe Kerello };
665069f0b63SChristophe Kerello 
666069f0b63SChristophe Kerello U_BOOT_DRIVER(stpmu1_pwr_sw) = {
667069f0b63SChristophe Kerello 	.name = "stpmu1_pwr_sw",
668069f0b63SChristophe Kerello 	.id = UCLASS_REGULATOR,
669069f0b63SChristophe Kerello 	.ops = &stpmu1_pwr_sw_ops,
670069f0b63SChristophe Kerello 	.probe = stpmu1_pwr_sw_probe,
671069f0b63SChristophe Kerello };
672