xref: /rk3399_rockchip-uboot/drivers/power/regulator/rk801_regulator.c (revision 5e8035f640f19b9c86319888db954a93a2aa4a9f)
1 /*
2  * (C) Copyright 2024 Rockchip Electronics Co., Ltd.
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <asm/gpio.h>
10 #include <power/pmic.h>
11 #include <power/rk801_pmic.h>
12 #include <power/regulator.h>
13 
14 #define RK801_BUCK_VSEL_MASK		0x7f
15 #define RK801_LDO_VSEL_MASK		0x3f
16 #define ENABLE_MASK(id)			(BIT(4 + (id)) | BIT(id))
17 #define ENABLE_VAL(id)			(BIT(4 + (id)) | BIT(id))
18 #define DISABLE_VAL(id)			(BIT(4 + (id)) | 0)
19 
20 enum {
21 	PM_SLEEP = 0,
22 	PM_RUNTIME = 1,
23 };
24 
25 struct runtime_device {
26 	int reg_src;
27 	int reg_dst;
28 };
29 
30 struct regulator_desc {
31 	int id;
32 	unsigned int min_uV;
33 	unsigned int uV_step;
34 	const struct linear_range *linear_ranges;
35 	int n_linear_ranges;
36 	unsigned int vsel_reg;
37 	unsigned int vsel_mask;
38 	unsigned int enable_reg;
39 	unsigned int enable_mask;
40 	unsigned int enable_val;
41 	unsigned int disable_val;
42 	unsigned int ramp_delay;
43 };
44 
45 struct linear_range {
46 	unsigned int min;
47 	unsigned int min_sel;
48 	unsigned int max_sel;
49 	unsigned int step;
50 };
51 
52 #define REGULATOR_LINEAR_RANGE(_min_uV, _min_sel, _max_sel, _step_uV)	\
53 {									\
54 	.min		= _min_uV,					\
55 	.min_sel	= _min_sel,					\
56 	.max_sel	= _max_sel,					\
57 	.step		= _step_uV,					\
58 }
59 
60 static const struct linear_range rk801_buck1_voltage_ranges[] = {
61 	REGULATOR_LINEAR_RANGE(500000,   0, 80, 12500),	/* 0.5v - 1.5v */
62 	REGULATOR_LINEAR_RANGE(1800000, 81, 82, 400000),/* 1.8v - 2.2v */
63 	REGULATOR_LINEAR_RANGE(3300000, 83, 83, 0),	/* 3.3v */
64 	REGULATOR_LINEAR_RANGE(5000000, 84, 84, 0),	/* 5.0v */
65 	REGULATOR_LINEAR_RANGE(5250000, 85, 85, 0),	/* 5.25v */
66 };
67 
68 static const struct linear_range rk801_buck2_voltage_ranges[] = {
69 	REGULATOR_LINEAR_RANGE(800000,  0, 2, 50000),	/* 0.8v - 0.9v */
70 	REGULATOR_LINEAR_RANGE(1800000, 3, 4, 400000),	/* 1.8v - 2.2v */
71 	REGULATOR_LINEAR_RANGE(3300000, 5, 5, 0),	/* 3.3v */
72 	REGULATOR_LINEAR_RANGE(5000000, 6, 6, 0),	/* 5.0v */
73 	REGULATOR_LINEAR_RANGE(5250000, 7, 7, 0),	/* 5.25v */
74 };
75 
76 static const struct linear_range rk801_buck4_voltage_ranges[] = {
77 	REGULATOR_LINEAR_RANGE(500000,   0, 80, 12500),	/* 0.5v - 1.5v */
78 	REGULATOR_LINEAR_RANGE(1800000, 81, 82, 400000),/* 1.8v - 2.2v */
79 	REGULATOR_LINEAR_RANGE(2500000, 83, 83, 0),	/* 2.5v */
80 	REGULATOR_LINEAR_RANGE(2800000, 84, 84, 0),	/* 2.8v */
81 	REGULATOR_LINEAR_RANGE(3000000, 85, 85, 0),	/* 3.0v */
82 	REGULATOR_LINEAR_RANGE(3300000, 86, 86, 0),	/* 3.3v */
83 };
84 
85 static const struct linear_range rk801_ldo1_voltage_ranges[] = {
86 	REGULATOR_LINEAR_RANGE(500000, 0, 58, 50000),	/* 0.5v - 3.4v */
87 };
88 
89 static const struct linear_range rk801_ldo2_voltage_ranges[] = {
90 	REGULATOR_LINEAR_RANGE(500000, 0, 58, 50000),	/* 0.5v - 3.4v */
91 };
92 
93 static const struct regulator_desc rk801_desc[] = {
94 	{
95 		.id = RK801_ID_DCDC1,
96 		.linear_ranges = rk801_buck1_voltage_ranges,
97 		.n_linear_ranges = ARRAY_SIZE(rk801_buck1_voltage_ranges),
98 		.vsel_reg = RK801_BUCK1_ON_VSEL_REG,
99 		.vsel_mask = RK801_BUCK_VSEL_MASK,
100 		.enable_reg = RK801_POWER_EN0_REG,
101 		.enable_mask = ENABLE_MASK(RK801_ID_DCDC1),
102 		.enable_val = ENABLE_VAL(RK801_ID_DCDC1),
103 		.disable_val = DISABLE_VAL(RK801_ID_DCDC1),
104 		.ramp_delay = 1, // TODO: +32
105 	}, {
106 		.id = RK801_ID_DCDC2,
107 		.linear_ranges = rk801_buck2_voltage_ranges,
108 		.n_linear_ranges = ARRAY_SIZE(rk801_buck2_voltage_ranges),
109 		.vsel_reg = RK801_BUCK2_ON_VSEL_REG,
110 		.vsel_mask = RK801_BUCK_VSEL_MASK,
111 		.enable_reg = RK801_POWER_EN0_REG,
112 		.enable_mask = ENABLE_MASK(RK801_ID_DCDC2),
113 		.enable_val = ENABLE_VAL(RK801_ID_DCDC2),
114 		.disable_val = DISABLE_VAL(RK801_ID_DCDC2),
115 		.ramp_delay = 1,
116 	}, {
117 		.id = RK801_ID_DCDC3,
118 		.enable_reg = RK801_POWER_EN0_REG,
119 		.enable_mask = ENABLE_MASK(RK801_ID_DCDC3),
120 		.enable_val = ENABLE_VAL(RK801_ID_DCDC3),
121 		.disable_val = DISABLE_VAL(RK801_ID_DCDC3),
122 	}, {
123 		.id = RK801_ID_DCDC4,
124 		.linear_ranges = rk801_buck4_voltage_ranges,
125 		.n_linear_ranges = ARRAY_SIZE(rk801_buck4_voltage_ranges),
126 		.vsel_reg = RK801_BUCK4_ON_VSEL_REG,
127 		.vsel_mask = RK801_BUCK_VSEL_MASK,
128 		.enable_reg = RK801_POWER_EN0_REG,
129 		.enable_mask = ENABLE_MASK(RK801_ID_DCDC4),
130 		.enable_val = ENABLE_VAL(RK801_ID_DCDC4),
131 		.disable_val = DISABLE_VAL(RK801_ID_DCDC4),
132 		.ramp_delay = 1,
133 	}, {
134 		.id = RK801_ID_LDO1,
135 		.linear_ranges = rk801_ldo1_voltage_ranges,
136 		.n_linear_ranges = ARRAY_SIZE(rk801_ldo1_voltage_ranges),
137 		.vsel_reg = RK801_LDO1_ON_VSEL_REG,
138 		.vsel_mask = RK801_LDO_VSEL_MASK,
139 		.enable_reg = RK801_POWER_EN1_REG,
140 		.enable_mask = ENABLE_MASK(0),
141 		.enable_val = ENABLE_VAL(0),
142 		.disable_val = DISABLE_VAL(0),
143 		.ramp_delay = 1,
144 	}, {
145 		.id = RK801_ID_LDO2,
146 		.linear_ranges = rk801_ldo2_voltage_ranges,
147 		.n_linear_ranges = ARRAY_SIZE(rk801_ldo2_voltage_ranges),
148 		.vsel_reg = RK801_LDO2_ON_VSEL_REG,
149 		.vsel_mask = RK801_LDO_VSEL_MASK,
150 		.enable_reg = RK801_POWER_EN1_REG,
151 		.enable_mask = ENABLE_MASK(1),
152 		.enable_val = ENABLE_VAL(1),
153 		.disable_val = DISABLE_VAL(1),
154 		.ramp_delay = 1,
155 	}, {
156 		.id = RK801_ID_SWITCH,
157 		.n_linear_ranges = 0,
158 		.enable_reg = RK801_POWER_EN1_REG,
159 		.enable_mask = ENABLE_MASK(2),
160 		.enable_val = ENABLE_VAL(2),
161 		.disable_val = DISABLE_VAL(2),
162 		.ramp_delay = 1,
163 	},
164 };
165 
linear_range_get_value(const struct linear_range * r,unsigned int selector,unsigned int * val)166 static int linear_range_get_value(const struct linear_range *r,
167 				  unsigned int selector, unsigned int *val)
168 {
169 	if (r->min_sel > selector || r->max_sel < selector)
170 		return -EINVAL;
171 
172 	*val = r->min + (selector - r->min_sel) * r->step;
173 
174 	return 0;
175 }
176 
linear_range_get_value_array(const struct linear_range * r,int ranges,unsigned int selector,unsigned int * uV)177 static int linear_range_get_value_array(const struct linear_range *r, int ranges,
178 					unsigned int selector, unsigned int *uV)
179 {
180 	int i;
181 
182 	for (i = 0; i < ranges; i++) {
183 		if (r[i].min_sel <= selector && r[i].max_sel >= selector)
184 			return linear_range_get_value(&r[i], selector, uV);
185 	}
186 
187 	return -EINVAL;
188 }
189 
linear_range_get_max_value(const struct linear_range * r)190 static unsigned int linear_range_get_max_value(const struct linear_range *r)
191 {
192 	return r->min + (r->max_sel - r->min_sel) * r->step;
193 }
194 
linear_range_get_selector_high(const struct linear_range * r,unsigned int val,unsigned int * selector,bool * found)195 static int linear_range_get_selector_high(const struct linear_range *r,
196 					  unsigned int val,
197 					  unsigned int *selector,
198 					  bool *found)
199 {
200 	*found = false;
201 
202 	if (linear_range_get_max_value(r) < val)
203 		return -EINVAL;
204 
205 	if (r->min > val) {
206 		*selector = r->min_sel;
207 		return 0;
208 	}
209 
210 	*found = true;
211 
212 	if (r->step == 0)
213 		*selector = r->max_sel;
214 	else
215 		*selector = DIV_ROUND_UP(val - r->min, r->step) + r->min_sel;
216 
217 	return 0;
218 }
219 
regulator_map_voltage_linear_range(const struct regulator_desc * desc,int min_uV,int max_uV)220 int regulator_map_voltage_linear_range(const struct regulator_desc *desc,
221 				       int min_uV, int max_uV)
222 {
223 	const struct linear_range *range;
224 	int ret = -EINVAL;
225 	unsigned int sel;
226 	bool found;
227 	uint voltage, i;
228 
229 	if (!desc->n_linear_ranges)
230 		return -EINVAL;
231 
232 	for (i = 0; i < desc->n_linear_ranges; i++) {
233 		range = &desc->linear_ranges[i];
234 
235 		ret = linear_range_get_selector_high(range, min_uV, &sel,
236 						     &found);
237 		if (ret)
238 			continue;
239 
240 		ret = sel;
241 
242 		/*
243 		 * Map back into a voltage to verify we're still in bounds.
244 		 * If we are not, then continue checking rest of the ranges.
245 		 */
246 		if (linear_range_get_value_array(desc->linear_ranges,
247 					desc->n_linear_ranges, sel, &voltage))
248 			continue;
249 
250 		if (voltage >= min_uV && voltage <= max_uV)
251 			break;
252 	}
253 
254 	if (i == desc->n_linear_ranges)
255 		return -EINVAL;
256 
257 	return ret;
258 }
259 
rk801_get_desc(struct udevice * dev)260 static const struct regulator_desc *rk801_get_desc(struct udevice *dev)
261 {
262 	struct dm_regulator_uclass_platdata *uc_pdata;
263 	int i, id;
264 
265 	/* Why? Because: RK801_ID_DCDC3=4, RK801_ID_DCDC4=3 */
266 	uc_pdata = dev_get_uclass_platdata(dev);
267 	if (uc_pdata->type == REGULATOR_TYPE_BUCK) {
268 		switch (dev->driver_data) {
269 		case 1:
270 			id = RK801_ID_DCDC1;
271 			break;
272 		case 2:
273 			id = RK801_ID_DCDC2;
274 			break;
275 		case 3:
276 			id = RK801_ID_DCDC3;
277 			break;
278 		case 4:
279 			id = RK801_ID_DCDC4;
280 			break;
281 		default:
282 			id = -EINVAL;
283 		}
284 	} else if (uc_pdata->type == REGULATOR_TYPE_LDO) {
285 		switch (dev->driver_data) {
286 		case 1:
287 			id = RK801_ID_LDO1;
288 			break;
289 		case 2:
290 			id = RK801_ID_LDO2;
291 			break;
292 		default:
293 			id = -EINVAL;
294 		}
295 	} else {
296 		id = RK801_ID_SWITCH;
297 	}
298 
299 	if (id != -EINVAL) {
300 		for (i = 0; i < ARRAY_SIZE(rk801_desc); i++) {
301 			if (rk801_desc[i].id == id)
302 				return &rk801_desc[i];
303 		}
304 	}
305 
306 	return NULL;
307 }
308 
rk801_regulator_get_value(struct udevice * dev,bool runtime)309 static int rk801_regulator_get_value(struct udevice *dev, bool runtime)
310 {
311 	const struct regulator_desc *desc = rk801_get_desc(dev);
312 	struct udevice *pmic = dev->parent;
313 	int sel, val, vsel_reg, ret;
314 	uint uV;
315 
316 	if (!desc)
317 		return -ENODEV;
318 
319 	if (desc->id == RK801_ID_DCDC3)
320 		return -ENOSYS;
321 
322 	if (runtime)
323 		vsel_reg = desc->vsel_reg;
324 	else
325 		vsel_reg = desc->vsel_reg + RK801_SLP_REG_OFFSET;
326 
327 	val = pmic_reg_read(pmic, vsel_reg);
328 	if (val < 0)
329 		return val;
330 
331 	sel = (val & desc->vsel_mask) >> (ffs(desc->vsel_mask) - 1);
332 	ret = linear_range_get_value_array(desc->linear_ranges,
333 					   desc->n_linear_ranges, sel, &uV);
334 
335 	debug("%s, %s, desc[%d]: reg=%02x, ret=%d, sel=0x%02x(%d), uV=%d\n",
336 	      __func__, dev->name, desc->id, vsel_reg, ret, sel, sel, uV);
337 
338 	return ret ? ret : uV;
339 }
340 
rk801_regulator_set_value(struct udevice * dev,int uV,bool runtime)341 static int rk801_regulator_set_value(struct udevice *dev, int uV, bool runtime)
342 {
343 	const struct regulator_desc *desc = rk801_get_desc(dev);
344 	struct udevice *pmic = dev->parent;
345 	struct rk801_priv *priv = dev_get_priv(pmic);
346 	const struct gpio_desc *gpio = &priv->pwrctrl_gpio;
347 	uint reg, reg0, reg1, sel;
348 	int ret, gpio_level;
349 
350 	if (!desc)
351 		return -ENODEV;
352 
353 	if (desc->id == RK801_ID_DCDC3)
354 		return -ENOSYS;
355 
356 	if (priv->req_pwrctrl_dvs) {
357 		reg0 = desc->vsel_reg;
358 		reg1 = desc->vsel_reg + RK801_SLP_REG_OFFSET;
359 
360 		gpio_level = dm_gpio_get_value(gpio);
361 		reg = (gpio_level == 1) ? reg0 : reg1;
362 
363 		sel = regulator_map_voltage_linear_range(desc, uV, uV);
364 		if (sel < 0)
365 			return sel;
366 
367 		sel <<= ffs(desc->vsel_mask) - 1;
368 
369 		debug("%s, %s, desc[%d]: reg=%02x, uV=%d, sel=0x%02x(%d), gpio=%d\n",
370 		      __func__, dev->name, desc->id, reg, uV, sel, sel, gpio_level);
371 
372 		ret = pmic_clrsetbits(pmic, reg, desc->vsel_mask, sel);
373 		if (ret)
374 			return ret;
375 
376 		udelay(40); /* hw sync */
377 
378 		dm_gpio_set_value(gpio, !gpio_level);
379 
380 		if (reg == reg0)
381 			ret = pmic_clrsetbits(pmic, reg1, desc->vsel_mask, sel);
382 		else
383 			ret = pmic_clrsetbits(pmic, reg0, desc->vsel_mask, sel);
384 
385 		/* if sleep mode: set pwrctrl pin inactive anyway */
386 		if (!runtime) {
387 			dm_gpio_set_value(gpio, 0);
388 			udelay(40); /* hw sync */
389 		}
390 
391 		return ret;
392 	} else {
393 		if (runtime)
394 			reg = desc->vsel_reg;
395 		else
396 			reg = desc->vsel_reg + RK801_SLP_REG_OFFSET;
397 
398 		sel = regulator_map_voltage_linear_range(desc, uV, uV);
399 		if (sel < 0)
400 			return sel;
401 
402 		debug("%s, %s, desc[%d]: reg=%02x, uV=%d, sel=0x%02x(%d)\n",
403 		      __func__, dev->name, desc->id, reg, uV, sel, sel);
404 
405 		sel <<= ffs(desc->vsel_mask) - 1;
406 
407 		return pmic_clrsetbits(pmic, reg, desc->vsel_mask, sel);
408 	}
409 }
410 
rk801_regulator_get_enable(struct udevice * dev,bool runtime)411 static int rk801_regulator_get_enable(struct udevice *dev, bool runtime)
412 {
413 	const struct regulator_desc *desc = rk801_get_desc(dev);
414 	struct udevice *pmic = dev->parent;
415 	int val;
416 
417 	if (!desc)
418 		return -ENODEV;
419 
420 	if (runtime) {
421 		val = pmic_reg_read(pmic, desc->enable_reg);
422 		if (val < 0)
423 			return val;
424 
425 		val &= desc->enable_mask;
426 
427 		return val == desc->enable_val;
428 	} else {
429 		val = pmic_reg_read(pmic, RK801_POWER_SLP_EN_REG);
430 		if (val < 0)
431 			return val;
432 
433 		return (val & BIT(desc->id));
434 	}
435 }
436 
rk801_regulator_set_enable(struct udevice * dev,bool enable)437 static int rk801_regulator_set_enable(struct udevice *dev, bool enable)
438 {
439 	const struct regulator_desc *desc = rk801_get_desc(dev);
440 	struct udevice *pmic = dev->parent;
441 	int val;
442 
443 	if (!desc)
444 		return -ENODEV;
445 
446 	val = enable ? desc->enable_val : desc->disable_val;
447 
448 	debug("%s, %s, desc[%d]: reg=%02x, mask=%02x, enable=%d, val=0x%02x(%d)\n",
449 	      __func__, dev->name, desc->id, desc->enable_reg,
450 	      desc->enable_mask, enable, val, val);
451 
452 	return pmic_clrsetbits(pmic, desc->enable_reg, desc->enable_mask, val);
453 }
454 
rk801_regulator_set_suspend_enable(struct udevice * dev,bool enable)455 static int rk801_regulator_set_suspend_enable(struct udevice *dev, bool enable)
456 {
457 	const struct regulator_desc *desc = rk801_get_desc(dev);
458 	struct udevice *pmic = dev->parent;
459 
460 	if (!desc)
461 		return -ENODEV;
462 
463 	debug("%s, %s, desc[%d]: reg=%02x, mask=%02lx, enable=0x%02x, val=0x%02lx\n",
464 	      __func__, dev->name, desc->id, RK801_POWER_SLP_EN_REG,
465 	      BIT(desc->id), enable, BIT(desc->id));
466 
467 	return pmic_clrsetbits(pmic, RK801_POWER_SLP_EN_REG,
468 			       BIT(desc->id), BIT(desc->id));
469 }
470 
buck_ldo_get_value(struct udevice * dev)471 static int buck_ldo_get_value(struct udevice *dev)
472 {
473 	return rk801_regulator_get_value(dev, PM_RUNTIME);
474 }
475 
buck_ldo_set_value(struct udevice * dev,int uV)476 static int buck_ldo_set_value(struct udevice *dev, int uV)
477 {
478 	return rk801_regulator_set_value(dev, uV, PM_RUNTIME);
479 }
480 
buck_ldo_get_enable(struct udevice * dev)481 static int buck_ldo_get_enable(struct udevice *dev)
482 {
483 	return rk801_regulator_get_enable(dev, PM_RUNTIME);
484 }
485 
buck_ldo_set_enable(struct udevice * dev,bool enable)486 static int buck_ldo_set_enable(struct udevice *dev, bool enable)
487 {
488 	struct rk801_priv *priv = dev_get_priv(dev->parent);
489 	int ret;
490 
491 	ret = rk801_regulator_set_enable(dev, enable);
492 	if (ret)
493 		return ret;
494 
495 	if (priv->req_pwrctrl_dvs)
496 		return rk801_regulator_set_suspend_enable(dev, enable);
497 
498 	return 0;
499 }
500 
buck_ldo_get_suspend_value(struct udevice * dev)501 static int buck_ldo_get_suspend_value(struct udevice *dev)
502 {
503 	return rk801_regulator_get_value(dev, PM_SLEEP);
504 }
505 
buck_ldo_set_suspend_value(struct udevice * dev,int uV)506 static int buck_ldo_set_suspend_value(struct udevice *dev, int uV)
507 {
508 	return rk801_regulator_set_value(dev, uV, PM_SLEEP);
509 }
510 
buck_ldo_get_suspend_enable(struct udevice * dev)511 static int buck_ldo_get_suspend_enable(struct udevice *dev)
512 {
513 	return rk801_regulator_get_enable(dev, PM_SLEEP);
514 }
515 
buck_ldo_set_suspend_enable(struct udevice * dev,bool enable)516 static int buck_ldo_set_suspend_enable(struct udevice *dev, bool enable)
517 {
518 	struct rk801_priv *priv = dev_get_priv(dev->parent);
519 	int ret;
520 
521 	ret = rk801_regulator_set_suspend_enable(dev, enable);
522 	if (ret)
523 		return ret;
524 
525 	if (priv->req_pwrctrl_dvs)
526 		return rk801_regulator_set_enable(dev, enable);
527 
528 	return 0;
529 }
530 
buck_ldo_get_ramp_delay(struct udevice * dev,int old_uV,int new_uV)531 int buck_ldo_get_ramp_delay(struct udevice *dev, int old_uV, int new_uV)
532 {
533 	const struct regulator_desc *desc = rk801_get_desc(dev);
534 	struct dm_regulator_uclass_platdata *uc_pdata;
535 
536 	if (!desc)
537 		return 0;
538 
539 	uc_pdata = dev_get_uclass_platdata(dev);
540 	if (uc_pdata->type != REGULATOR_TYPE_BUCK)
541 		return 0;
542 
543 	return abs(new_uV - old_uV) / 1000 + 32;
544 }
545 
switch_set_enable(struct udevice * dev,bool enable)546 static int switch_set_enable(struct udevice *dev, bool enable)
547 {
548 	return buck_ldo_set_enable(dev, enable);
549 }
550 
switch_get_enable(struct udevice * dev)551 static int switch_get_enable(struct udevice *dev)
552 {
553 	return rk801_regulator_get_enable(dev, PM_RUNTIME);
554 }
555 
switch_set_suspend_enable(struct udevice * dev,bool enable)556 static int switch_set_suspend_enable(struct udevice *dev, bool enable)
557 {
558 	return buck_ldo_set_suspend_enable(dev, enable);
559 }
560 
switch_get_suspend_enable(struct udevice * dev)561 static int switch_get_suspend_enable(struct udevice *dev)
562 {
563 	return rk801_regulator_get_enable(dev, PM_SLEEP);
564 }
565 
switch_get_value(struct udevice * dev)566 static int switch_get_value(struct udevice *dev)
567 {
568 	return 0;
569 }
570 
switch_set_value(struct udevice * dev,int uV)571 static int switch_set_value(struct udevice *dev, int uV)
572 {
573 	return 0;
574 }
575 
switch_set_suspend_value(struct udevice * dev,int uV)576 static int switch_set_suspend_value(struct udevice *dev, int uV)
577 {
578 	return 0;
579 }
580 
switch_get_suspend_value(struct udevice * dev)581 static int switch_get_suspend_value(struct udevice *dev)
582 {
583 	return 0;
584 }
585 
rk801_buck_probe(struct udevice * dev)586 static int rk801_buck_probe(struct udevice *dev)
587 {
588 	struct rk801_priv *priv = dev_get_priv(dev->parent);
589 	struct dm_regulator_uclass_platdata *uc_pdata;
590 	struct udevice *pmic = dev->parent;
591 	struct runtime_device rdev[] = {
592 		{ RK801_BUCK1_ON_VSEL_REG, RK801_BUCK1_SLP_VSEL_REG },
593 		{ RK801_BUCK2_ON_VSEL_REG, RK801_BUCK2_SLP_VSEL_REG },
594 		{ RK801_BUCK4_ON_VSEL_REG, RK801_BUCK4_SLP_VSEL_REG },
595 		{ RK801_LDO1_ON_VSEL_REG,  RK801_LDO1_SLP_VSEL_REG },
596 		{ RK801_LDO2_ON_VSEL_REG,  RK801_LDO2_SLP_VSEL_REG },
597 	};
598 	uint val, en0, en1;
599 	int i, ret;
600 
601 	uc_pdata = dev_get_uclass_platdata(dev);
602 	uc_pdata->type = REGULATOR_TYPE_BUCK;
603 	uc_pdata->mode_count = 0;
604 
605 	/* probe only once by buck1 */
606 	if (dev->driver_data != 1)
607 		return 0;
608 
609 	/* set pwrctrl active pol and use sleep function */
610 	val = (priv->pwrctrl_gpio.flags & GPIOD_ACTIVE_LOW) ?
611 			      RK801_SLEEP_ACT_L : RK801_SLEEP_ACT_H;
612 	ret = pmic_clrsetbits(pmic, RK801_SYS_CFG2_REG,
613 			      RK801_SLEEP_POL_MSK, val);
614 	if (ret < 0)
615 		return ret;
616 
617 	ret = pmic_clrsetbits(pmic, RK801_SLEEP_CFG_REG,
618 			      RK801_SLEEP_FUN_MSK, RK801_SLEEP_FUN);
619 	if (ret < 0)
620 		return ret;
621 
622 	/* disable buck/pldo slp lp */
623 	ret = pmic_clrsetbits(pmic, RK801_SLP_LP_CONFIG_REG,
624 			      RK801_SLP_LP_MASK, 0);
625 	if (ret < 0)
626 		return ret;
627 
628 	/* copy on/slp enabel */
629 	en0 = pmic_reg_read(pmic, RK801_POWER_EN0_REG);
630 	if (en0 < 0)
631 		return en0;
632 
633 	en1 = pmic_reg_read(pmic, RK801_POWER_EN1_REG);
634 	if (en1 < 0)
635 		return en1;
636 
637 	val = (en0 & 0x0f) | ((en1 & 0x0f) << 4);
638 	ret = pmic_reg_write(pmic, RK801_POWER_SLP_EN_REG, val);
639 	if (ret < 0)
640 		return ret;
641 
642 	/* copy on/slp vsel */
643 	for (i = 0; i < ARRAY_SIZE(rdev); i++) {
644 		val = pmic_reg_read(pmic, rdev[i].reg_src);
645 		if (val < 0)
646 			return val;
647 
648 		ret = pmic_reg_write(pmic, rdev[i].reg_dst, val);
649 		if (ret < 0)
650 			return ret;
651 	}
652 
653 	return 0;
654 }
655 
rk801_ldo_probe(struct udevice * dev)656 static int rk801_ldo_probe(struct udevice *dev)
657 {
658 	struct dm_regulator_uclass_platdata *uc_pdata;
659 
660 	uc_pdata = dev_get_uclass_platdata(dev);
661 	uc_pdata->type = REGULATOR_TYPE_LDO;
662 	uc_pdata->mode_count = 0;
663 
664 	return 0;
665 }
666 
rk801_switch_probe(struct udevice * dev)667 static int rk801_switch_probe(struct udevice *dev)
668 {
669 	struct dm_regulator_uclass_platdata *uc_pdata;
670 
671 	uc_pdata = dev_get_uclass_platdata(dev);
672 	uc_pdata->type = REGULATOR_TYPE_FIXED;
673 	uc_pdata->mode_count = 0;
674 
675 	return 0;
676 }
677 
678 static const struct dm_regulator_ops rk801_buck_ldo_ops = {
679 	.get_value  = buck_ldo_get_value,
680 	.set_value  = buck_ldo_set_value,
681 	.set_enable = buck_ldo_set_enable,
682 	.get_enable = buck_ldo_get_enable,
683 	.set_suspend_value = buck_ldo_set_suspend_value,
684 	.get_suspend_value = buck_ldo_get_suspend_value,
685 	.set_suspend_enable = buck_ldo_set_suspend_enable,
686 	.get_suspend_enable = buck_ldo_get_suspend_enable,
687 	.get_ramp_delay = buck_ldo_get_ramp_delay,
688 };
689 
690 static const struct dm_regulator_ops rk801_switch_ops = {
691 	.get_value  = switch_get_value,
692 	.set_value  = switch_set_value,
693 	.set_enable = switch_set_enable,
694 	.get_enable = switch_get_enable,
695 	.set_suspend_enable = switch_set_suspend_enable,
696 	.get_suspend_enable = switch_get_suspend_enable,
697 	.set_suspend_value = switch_set_suspend_value,
698 	.get_suspend_value = switch_get_suspend_value,
699 };
700 
701 U_BOOT_DRIVER(rk801_buck) = {
702 	.name = "rk801_buck",
703 	.id = UCLASS_REGULATOR,
704 	.ops = &rk801_buck_ldo_ops,
705 	.probe = rk801_buck_probe,
706 };
707 
708 U_BOOT_DRIVER(rk801_ldo) = {
709 	.name = "rk801_ldo",
710 	.id = UCLASS_REGULATOR,
711 	.ops = &rk801_buck_ldo_ops,
712 	.probe = rk801_ldo_probe,
713 };
714 
715 U_BOOT_DRIVER(rk801_switch) = {
716 	.name = "rk801_switch",
717 	.id = UCLASS_REGULATOR,
718 	.ops = &rk801_switch_ops,
719 	.probe = rk801_switch_probe,
720 };
721 
722