xref: /rk3399_rockchip-uboot/drivers/power/regulator/regulator-uclass.c (revision d6f8cec4d54b00221a4e2560b3576fe70b46b8cf)
1 /*
2  * Copyright (C) 2014-2015 Samsung Electronics
3  * Przemyslaw Marczak <p.marczak@samsung.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <dm/uclass-internal.h>
12 #include <power/pmic.h>
13 #include <power/regulator.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
18 {
19 	struct dm_regulator_uclass_platdata *uc_pdata;
20 
21 	*modep = NULL;
22 
23 	uc_pdata = dev_get_uclass_platdata(dev);
24 	if (!uc_pdata)
25 		return -ENXIO;
26 
27 	*modep = uc_pdata->mode;
28 	return uc_pdata->mode_count;
29 }
30 
31 int regulator_get_value(struct udevice *dev)
32 {
33 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
34 
35 	if (!ops || !ops->get_value)
36 		return -ENOSYS;
37 
38 	return ops->get_value(dev);
39 }
40 
41 int regulator_set_value(struct udevice *dev, int uV)
42 {
43 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
44 	struct dm_regulator_uclass_platdata *uc_pdata;
45 	u32 old_uV = -ENODATA, us;
46 	int ret;
47 
48 	uc_pdata = dev_get_uclass_platdata(dev);
49 	if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
50 		return -EINVAL;
51 	if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
52 		return -EINVAL;
53 
54 	if (!ops || !ops->set_value)
55 		return -ENOSYS;
56 
57 	if (uc_pdata->ramp_delay != -ENODATA) {
58 		if (!ops->get_value)
59 			return -ENOSYS;
60 		old_uV = ops->get_value(dev);
61 		if (old_uV < 0)
62 			return -EINVAL;
63 	}
64 
65 	ret = ops->set_value(dev, uV);
66 
67 	if (!ret && (old_uV != -ENODATA) && (old_uV != uV)) {
68 		us = DIV_ROUND_UP(abs(uV - old_uV), uc_pdata->ramp_delay);
69 		udelay(us);
70 		debug("%s: ramp=%d, old_uV=%d, uV=%d, us=%d\n",
71 		      uc_pdata->name, uc_pdata->ramp_delay, old_uV, uV, us);
72 	}
73 
74 	return ret;
75 }
76 
77 int regulator_set_suspend_value(struct udevice *dev, int uV)
78 {
79 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
80 
81 	if (!ops || !ops->set_suspend_value)
82 		return -ENOSYS;
83 
84 	return ops->set_suspend_value(dev, uV);
85 }
86 
87 int regulator_get_suspend_value(struct udevice *dev)
88 {
89 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
90 
91 	if (!ops || !ops->get_suspend_value)
92 		return -ENOSYS;
93 
94 	return ops->get_suspend_value(dev);
95 }
96 
97 /*
98  * To be called with at most caution as there is no check
99  * before setting the actual voltage value.
100  */
101 int regulator_set_value_force(struct udevice *dev, int uV)
102 {
103 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
104 
105 	if (!ops || !ops->set_value)
106 		return -ENOSYS;
107 
108 	return ops->set_value(dev, uV);
109 }
110 
111 int regulator_get_current(struct udevice *dev)
112 {
113 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
114 
115 	if (!ops || !ops->get_current)
116 		return -ENOSYS;
117 
118 	return ops->get_current(dev);
119 }
120 
121 int regulator_set_current(struct udevice *dev, int uA)
122 {
123 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
124 	struct dm_regulator_uclass_platdata *uc_pdata;
125 
126 	uc_pdata = dev_get_uclass_platdata(dev);
127 	if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
128 		return -EINVAL;
129 	if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
130 		return -EINVAL;
131 
132 	if (!ops || !ops->set_current)
133 		return -ENOSYS;
134 
135 	return ops->set_current(dev, uA);
136 }
137 
138 int regulator_get_enable(struct udevice *dev)
139 {
140 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
141 
142 	if (!ops || !ops->get_enable)
143 		return -ENOSYS;
144 
145 	return ops->get_enable(dev);
146 }
147 
148 int regulator_set_enable(struct udevice *dev, bool enable)
149 {
150 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
151 	struct dm_regulator_uclass_platdata *uc_pdata;
152 
153 	if (!ops || !ops->set_enable)
154 		return -ENOSYS;
155 
156 	uc_pdata = dev_get_uclass_platdata(dev);
157 	if (!enable && uc_pdata->always_on) {
158 		printf("the always on regulator (%s) should never be disabled!\n", dev->name);
159 		return -EACCES;
160 	}
161 
162 	return ops->set_enable(dev, enable);
163 }
164 
165 int regulator_set_suspend_enable(struct udevice *dev, bool enable)
166 {
167 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
168 
169 	if (!ops || !ops->set_suspend_enable)
170 		return -ENOSYS;
171 
172 	return ops->set_suspend_enable(dev, enable);
173 }
174 
175 int regulator_get_suspend_enable(struct udevice *dev)
176 {
177 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
178 
179 	if (!ops || !ops->get_suspend_enable)
180 		return -ENOSYS;
181 
182 	return ops->get_suspend_enable(dev);
183 }
184 
185 int regulator_set_ramp_delay(struct udevice *dev, u32 ramp_delay)
186 {
187 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
188 
189 	if (!ops || !ops->set_ramp_delay)
190 		return -ENOSYS;
191 
192 	return ops->set_ramp_delay(dev, ramp_delay);
193 }
194 
195 int regulator_get_mode(struct udevice *dev)
196 {
197 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
198 
199 	if (!ops || !ops->get_mode)
200 		return -ENOSYS;
201 
202 	return ops->get_mode(dev);
203 }
204 
205 int regulator_set_mode(struct udevice *dev, int mode)
206 {
207 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
208 
209 	if (!ops || !ops->set_mode)
210 		return -ENOSYS;
211 
212 	return ops->set_mode(dev, mode);
213 }
214 
215 int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
216 {
217 	struct dm_regulator_uclass_platdata *uc_pdata;
218 	struct udevice *dev;
219 	int ret;
220 
221 	*devp = NULL;
222 
223 	for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
224 	     ret = uclass_find_next_device(&dev)) {
225 		if (ret) {
226 			debug("regulator %s, ret=%d\n", dev->name, ret);
227 			continue;
228 		}
229 
230 		uc_pdata = dev_get_uclass_platdata(dev);
231 		if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
232 			continue;
233 
234 		return uclass_get_device_tail(dev, 0, devp);
235 	}
236 
237 	debug("%s: can't find: %s, ret=%d\n", __func__, plat_name, ret);
238 
239 	return -ENODEV;
240 }
241 
242 int regulator_get_by_devname(const char *devname, struct udevice **devp)
243 {
244 	return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
245 }
246 
247 int device_get_supply_regulator(struct udevice *dev, const char *supply_name,
248 				struct udevice **devp)
249 {
250 	return uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
251 					    supply_name, devp);
252 }
253 
254 static int regulator_init_suspend(struct udevice *dev)
255 {
256 	struct dm_regulator_uclass_platdata *uc_pdata;
257 	int ret;
258 
259 	uc_pdata = dev_get_uclass_platdata(dev);
260 
261 	ret = regulator_set_suspend_enable(dev, uc_pdata->suspend_on);
262 	if (!ret && uc_pdata->suspend_on)
263 		return regulator_set_suspend_value(dev, uc_pdata->suspend_uV);
264 
265 	return 0;
266 }
267 
268 int regulator_autoset(struct udevice *dev)
269 {
270 	struct dm_regulator_uclass_platdata *uc_pdata;
271 	int ret = 0;
272 
273 	uc_pdata = dev_get_uclass_platdata(dev);
274 
275 	if (uc_pdata->ignore)
276 		return ret;
277 
278 	if (uc_pdata->ramp_delay != -ENODATA)
279 		regulator_set_ramp_delay(dev, uc_pdata->ramp_delay);
280 
281 	if (!uc_pdata->always_on && !uc_pdata->boot_on)
282 		return -EMEDIUMTYPE;
283 
284 	/*
285 	 * To compatible the old possible failure before adding this code,
286 	 * ignore the result.
287 	 */
288 	if (uc_pdata->type == REGULATOR_TYPE_FIXED) {
289 		regulator_set_enable(dev, true);
290 		return 0;
291 	}
292 
293 	if (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UV) {
294 		ret = regulator_set_value(dev, uc_pdata->min_uV);
295 	} else {
296 		if ((uc_pdata->type == REGULATOR_TYPE_BUCK) &&
297 		    (uc_pdata->min_uV != -ENODATA) &&
298 		    (uc_pdata->max_uV != -ENODATA) &&
299 		    (uc_pdata->init_uV <= 0))
300 			printf("%s %d uV\n",
301 			       uc_pdata->name, regulator_get_value(dev));
302 	}
303 
304 	if (uc_pdata->init_uV > 0) {
305 		ret = regulator_set_value(dev, uc_pdata->init_uV);
306 		if (!ret)
307 			printf("%s init %d uV\n",
308 			       uc_pdata->name, uc_pdata->init_uV);
309 	}
310 
311 	if (!ret && (uc_pdata->flags & REGULATOR_FLAG_AUTOSET_UA))
312 		ret = regulator_set_current(dev, uc_pdata->min_uA);
313 
314 	if (!ret)
315 		ret = regulator_set_enable(dev, true);
316 
317 	return ret;
318 }
319 
320 static void regulator_show(struct udevice *dev, int ret)
321 {
322 	struct dm_regulator_uclass_platdata *uc_pdata;
323 	int uV = 0;
324 
325 	uc_pdata = dev_get_uclass_platdata(dev);
326 	uV = regulator_get_value(dev);
327 
328 	printf("%25s@%15s: ", dev->name, uc_pdata->name);
329 	printf("%7duV <-> %7duV, set %7duV, %s",
330 	       uc_pdata->min_uV, uc_pdata->max_uV, uV,
331 	       (uc_pdata->always_on || uc_pdata->boot_on) ?
332 	       "enabling" : "disabled");
333 
334 	printf(" | supsend %7duV, %s",
335 	       uc_pdata->suspend_uV,
336 	       uc_pdata->suspend_on ? "enabling" : "disabled");
337 	if (uc_pdata->init_uV != -ENODATA)
338 		printf(" ; init %7duV", uc_pdata->init_uV);
339 
340 	if (ret)
341 		printf(" (ret: %d)", ret);
342 
343 	printf("\n");
344 }
345 
346 int regulator_autoset_by_name(const char *platname, struct udevice **devp)
347 {
348 	struct udevice *dev;
349 	int ret;
350 
351 	ret = regulator_get_by_platname(platname, &dev);
352 	if (devp)
353 		*devp = dev;
354 	if (ret) {
355 		debug("Can get the regulator: %s (err=%d)\n", platname, ret);
356 		return ret;
357 	}
358 
359 	return regulator_autoset(dev);
360 }
361 
362 int regulator_list_autoset(const char *list_platname[],
363 			   struct udevice *list_devp[],
364 			   bool verbose)
365 {
366 	struct udevice *dev;
367 	int error = 0, i = 0, ret;
368 
369 	while (list_platname[i]) {
370 		ret = regulator_autoset_by_name(list_platname[i], &dev);
371 		if (ret != -EMEDIUMTYPE && verbose)
372 			regulator_show(dev, ret);
373 		if (ret & !error)
374 			error = ret;
375 
376 		if (list_devp)
377 			list_devp[i] = dev;
378 
379 		i++;
380 	}
381 
382 	return error;
383 }
384 
385 static bool regulator_name_is_unique(struct udevice *check_dev,
386 				     const char *check_name)
387 {
388 	struct dm_regulator_uclass_platdata *uc_pdata;
389 	struct udevice *dev;
390 	int check_len = strlen(check_name);
391 	int ret;
392 	int len;
393 
394 	for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
395 	     ret = uclass_find_next_device(&dev)) {
396 		if (ret || dev == check_dev)
397 			continue;
398 
399 		uc_pdata = dev_get_uclass_platdata(dev);
400 		len = strlen(uc_pdata->name);
401 		if (len != check_len)
402 			continue;
403 
404 		if (!strcmp(uc_pdata->name, check_name))
405 			return false;
406 	}
407 
408 	return true;
409 }
410 
411 static int regulator_post_bind(struct udevice *dev)
412 {
413 	struct dm_regulator_uclass_platdata *uc_pdata;
414 	const char *property = "regulator-name";
415 
416 	uc_pdata = dev_get_uclass_platdata(dev);
417 
418 	/* Regulator's mandatory constraint */
419 	uc_pdata->name = dev_read_string(dev, property);
420 	if (!uc_pdata->name) {
421 		debug("%s: dev '%s' has no property '%s'\n",
422 		      __func__, dev->name, property);
423 		uc_pdata->name = dev_read_name(dev);
424 		if (!uc_pdata->name)
425 			return -EINVAL;
426 	}
427 
428 	if (regulator_name_is_unique(dev, uc_pdata->name))
429 		return 0;
430 
431 	debug("'%s' of dev: '%s', has nonunique value: '%s\n",
432 	      property, dev->name, uc_pdata->name);
433 
434 	return -EINVAL;
435 }
436 
437 static int regulator_pre_probe(struct udevice *dev)
438 {
439 	struct dm_regulator_uclass_platdata *uc_pdata;
440 	ofnode node;
441 
442 	uc_pdata = dev_get_uclass_platdata(dev);
443 	if (!uc_pdata)
444 		return -ENXIO;
445 
446 	/* Regulator's optional constraints */
447 	uc_pdata->min_uV = dev_read_u32_default(dev, "regulator-min-microvolt",
448 						-ENODATA);
449 	uc_pdata->max_uV = dev_read_u32_default(dev, "regulator-max-microvolt",
450 						-ENODATA);
451 	uc_pdata->init_uV = dev_read_u32_default(dev, "regulator-init-microvolt",
452 						-ENODATA);
453 	uc_pdata->min_uA = dev_read_u32_default(dev, "regulator-min-microamp",
454 						-ENODATA);
455 	uc_pdata->max_uA = dev_read_u32_default(dev, "regulator-max-microamp",
456 						-ENODATA);
457 	uc_pdata->always_on = dev_read_bool(dev, "regulator-always-on");
458 	uc_pdata->boot_on = dev_read_bool(dev, "regulator-boot-on");
459 	uc_pdata->ignore = dev_read_bool(dev, "regulator-loader-ignore");
460 	uc_pdata->ramp_delay = dev_read_u32_default(dev, "regulator-ramp-delay",
461 						    -ENODATA);
462 	node = dev_read_subnode(dev, "regulator-state-mem");
463 	if (ofnode_valid(node)) {
464 		uc_pdata->suspend_on = !ofnode_read_bool(node, "regulator-off-in-suspend");
465 		if (ofnode_read_u32(node, "regulator-suspend-microvolt", &uc_pdata->suspend_uV))
466 			uc_pdata->suspend_uV = uc_pdata->max_uA;
467 	} else {
468 		uc_pdata->suspend_on = true;
469 		uc_pdata->suspend_uV = uc_pdata->max_uA;
470 	}
471 
472 	/* Those values are optional (-ENODATA if unset) */
473 	if ((uc_pdata->min_uV != -ENODATA) &&
474 	    (uc_pdata->max_uV != -ENODATA) &&
475 	    (uc_pdata->min_uV == uc_pdata->max_uV))
476 		uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UV;
477 
478 	/* Those values are optional (-ENODATA if unset) */
479 	if ((uc_pdata->min_uA != -ENODATA) &&
480 	    (uc_pdata->max_uA != -ENODATA) &&
481 	    (uc_pdata->min_uA == uc_pdata->max_uA))
482 		uc_pdata->flags |= REGULATOR_FLAG_AUTOSET_UA;
483 
484 	debug("dev.name=%s: min_uV=%d, max_uV=%d, boot-on=%d, always-on=%d, "
485 	      "off-in-suspend=%d, suspend_volt=%d\n",
486 	      dev->name, uc_pdata->min_uV, uc_pdata->max_uV, uc_pdata->boot_on,
487 	      uc_pdata->always_on, !uc_pdata->suspend_on, uc_pdata->suspend_uV);
488 
489 	return 0;
490 }
491 
492 int regulators_enable_state_mem(bool verbose)
493 {
494 	struct udevice *dev;
495 	struct uclass *uc;
496 	int ret;
497 
498 	ret = uclass_get(UCLASS_REGULATOR, &uc);
499 	if (ret)
500 		return ret;
501 	for (uclass_first_device(UCLASS_REGULATOR, &dev);
502 	     dev;
503 	     uclass_next_device(&dev)) {
504 		ret = regulator_init_suspend(dev);
505 
506 		if (ret == -EMEDIUMTYPE)
507 			ret = 0;
508 		if (verbose)
509 			regulator_show(dev, ret);
510 		if (ret == -ENOSYS)
511 			ret = 0;
512 	}
513 
514 	return ret;
515 }
516 
517 int regulators_enable_boot_on(bool verbose)
518 {
519 	struct udevice *dev;
520 	struct uclass *uc;
521 	int ret;
522 
523 	ret = uclass_get(UCLASS_REGULATOR, &uc);
524 	if (ret)
525 		return ret;
526 	for (uclass_first_device(UCLASS_REGULATOR, &dev);
527 	     dev;
528 	     uclass_next_device(&dev)) {
529 		ret = regulator_autoset(dev);
530 
531 		if (ret == -EMEDIUMTYPE)
532 			ret = 0;
533 		if (verbose)
534 			regulator_show(dev, ret);
535 		if (ret == -ENOSYS)
536 			ret = 0;
537 	}
538 
539 	return ret;
540 }
541 
542 UCLASS_DRIVER(regulator) = {
543 	.id		= UCLASS_REGULATOR,
544 	.name		= "regulator",
545 	.post_bind	= regulator_post_bind,
546 	.pre_probe	= regulator_pre_probe,
547 	.per_device_platdata_auto_alloc_size =
548 				sizeof(struct dm_regulator_uclass_platdata),
549 };
550