1 /*-
2  * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: stable/12/sys/dev/extres/regulator/regulator.c 362243 2020-06-16 20:41:59Z manu $");
29 
30 #include "opt_platform.h"
31 #include <sys/param.h>
32 #include <sys/conf.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/queue.h>
36 #include <sys/kobj.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
43 #include <sys/sx.h>
44 
45 #ifdef FDT
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 #endif
50 #include <dev/extres/regulator/regulator.h>
51 
52 #include "regdev_if.h"
53 
54 SYSCTL_NODE(_hw, OID_AUTO, regulator, CTLFLAG_RD, NULL, "Regulators");
55 
56 MALLOC_DEFINE(M_REGULATOR, "regulator", "Regulator framework");
57 
58 #define	DIV_ROUND_UP(n,d) howmany(n, d)
59 
60 /* Forward declarations. */
61 struct regulator;
62 struct regnode;
63 
64 typedef TAILQ_HEAD(regnode_list, regnode) regnode_list_t;
65 typedef TAILQ_HEAD(regulator_list, regulator) regulator_list_t;
66 
67 /* Default regulator methods. */
68 static int regnode_method_init(struct regnode *regnode);
69 static int regnode_method_enable(struct regnode *regnode, bool enable,
70     int *udelay);
71 static int regnode_method_status(struct regnode *regnode, int *status);
72 static int regnode_method_set_voltage(struct regnode *regnode, int min_uvolt,
73     int max_uvolt, int *udelay);
74 static int regnode_method_get_voltage(struct regnode *regnode, int *uvolt);
75 static void regulator_constraint(void *dummy);
76 static void regulator_shutdown(void *dummy);
77 
78 /*
79  * Regulator controller methods.
80  */
81 static regnode_method_t regnode_methods[] = {
82 	REGNODEMETHOD(regnode_init,		regnode_method_init),
83 	REGNODEMETHOD(regnode_enable,		regnode_method_enable),
84 	REGNODEMETHOD(regnode_status,		regnode_method_status),
85 	REGNODEMETHOD(regnode_set_voltage,	regnode_method_set_voltage),
86 	REGNODEMETHOD(regnode_get_voltage,	regnode_method_get_voltage),
87 	REGNODEMETHOD(regnode_check_voltage,	regnode_method_check_voltage),
88 
89 	REGNODEMETHOD_END
90 };
91 DEFINE_CLASS_0(regnode, regnode_class, regnode_methods, 0);
92 
93 /*
94  * Regulator node - basic element for modelling SOC and bard power supply
95  * chains. Its contains producer data.
96  */
97 struct regnode {
98 	KOBJ_FIELDS;
99 
100 	TAILQ_ENTRY(regnode)	reglist_link;	/* Global list entry */
101 	regulator_list_t	consumers_list;	/* Consumers list */
102 
103 	/* Cache for already resolved names */
104 	struct regnode		*parent;	/* Resolved parent */
105 
106 	/* Details of this device. */
107 	const char		*name;		/* Globally unique name */
108 	const char		*parent_name;	/* Parent name */
109 
110 	device_t		pdev;		/* Producer device_t */
111 	void			*softc;		/* Producer softc */
112 	intptr_t		id;		/* Per producer unique id */
113 #ifdef FDT
114 	 phandle_t 		ofw_node;	/* OFW node of regulator */
115 #endif
116 	int			flags;		/* REGULATOR_FLAGS_ */
117 	struct sx		lock;		/* Lock for this regulator */
118 	int			ref_cnt;	/* Reference counter */
119 	int			enable_cnt;	/* Enabled counter */
120 
121 	struct regnode_std_param std_param;	/* Standard parameters */
122 
123 	struct sysctl_ctx_list	sysctl_ctx;
124 };
125 
126 /*
127  * Per consumer data, information about how a consumer is using a regulator
128  * node.
129  * A pointer to this structure is used as a handle in the consumer interface.
130  */
131 struct regulator {
132 	device_t		cdev;		/* Consumer device */
133 	struct regnode		*regnode;
134 	TAILQ_ENTRY(regulator)	link;		/* Consumers list entry */
135 
136 	int			enable_cnt;
137 	int 			min_uvolt;	/* Requested uvolt range */
138 	int 			max_uvolt;
139 };
140 
141 /*
142  * Regulator names must be system wide unique.
143  */
144 static regnode_list_t regnode_list = TAILQ_HEAD_INITIALIZER(regnode_list);
145 
146 static struct sx		regnode_topo_lock;
147 SX_SYSINIT(regulator_topology, &regnode_topo_lock, "Regulator topology lock");
148 
149 #define REG_TOPO_SLOCK()	sx_slock(&regnode_topo_lock)
150 #define REG_TOPO_XLOCK()	sx_xlock(&regnode_topo_lock)
151 #define REG_TOPO_UNLOCK()	sx_unlock(&regnode_topo_lock)
152 #define REG_TOPO_ASSERT()	sx_assert(&regnode_topo_lock, SA_LOCKED)
153 #define REG_TOPO_XASSERT() 	sx_assert(&regnode_topo_lock, SA_XLOCKED)
154 
155 #define REGNODE_SLOCK(_sc)	sx_slock(&((_sc)->lock))
156 #define REGNODE_XLOCK(_sc)	sx_xlock(&((_sc)->lock))
157 #define REGNODE_UNLOCK(_sc)	sx_unlock(&((_sc)->lock))
158 
159 SYSINIT(regulator_constraint, SI_SUB_LAST, SI_ORDER_ANY, regulator_constraint,
160     NULL);
161 SYSINIT(regulator_shutdown, SI_SUB_LAST, SI_ORDER_ANY, regulator_shutdown,
162     NULL);
163 
164 static void
regulator_constraint(void * dummy)165 regulator_constraint(void *dummy)
166 {
167 	struct regnode *entry;
168 	int rv;
169 
170 	REG_TOPO_SLOCK();
171 	TAILQ_FOREACH(entry, &regnode_list, reglist_link) {
172 		rv = regnode_set_constraint(entry);
173 		if (rv != 0 && bootverbose)
174 			printf("regulator: setting constraint on %s failed (%d)\n",
175 			    entry->name, rv);
176 	}
177 	REG_TOPO_UNLOCK();
178 }
179 
180 /*
181  * Disable unused regulator
182  * We run this function at SI_SUB_LAST which mean that every driver that needs
183  * regulator should have already enable them.
184  * All the remaining regulators should be those left enabled by the bootloader
185  * or enable by default by the PMIC.
186  */
187 static void
regulator_shutdown(void * dummy)188 regulator_shutdown(void *dummy)
189 {
190 	struct regnode *entry;
191 	int status, ret;
192 	int disable = 1;
193 
194 	TUNABLE_INT_FETCH("hw.regulator.disable_unused", &disable);
195 	if (!disable)
196 		return;
197 	REG_TOPO_SLOCK();
198 
199 	if (bootverbose)
200 		printf("regulator: shutting down unused regulators\n");
201 	TAILQ_FOREACH(entry, &regnode_list, reglist_link) {
202 		if (!entry->std_param.always_on) {
203 			ret = regnode_status(entry, &status);
204 			if (ret == 0 && status == REGULATOR_STATUS_ENABLED) {
205 				if (bootverbose)
206 					printf("regulator: shutting down %s... ",
207 					    entry->name);
208 				ret = regnode_stop(entry, 0);
209 				if (bootverbose) {
210 					/*
211 					 * Call out busy in particular, here,
212 					 * because it's not unexpected to fail
213 					 * shutdown if the regulator is simply
214 					 * in-use.
215 					 */
216 					if (ret == EBUSY)
217 						printf("busy\n");
218 					else if (ret != 0)
219 						printf("error (%d)\n", ret);
220 					else
221 						printf("ok\n");
222 				}
223 			}
224 		}
225 	}
226 	REG_TOPO_UNLOCK();
227 }
228 
229 /*
230  * sysctl handler
231  */
232 static int
regnode_uvolt_sysctl(SYSCTL_HANDLER_ARGS)233 regnode_uvolt_sysctl(SYSCTL_HANDLER_ARGS)
234 {
235 	struct regnode *regnode = arg1;
236 	int rv, uvolt;
237 
238 	if (regnode->std_param.min_uvolt == regnode->std_param.max_uvolt) {
239 		uvolt = regnode->std_param.min_uvolt;
240 	} else {
241 		REG_TOPO_SLOCK();
242 		if ((rv = regnode_get_voltage(regnode, &uvolt)) != 0) {
243 			REG_TOPO_UNLOCK();
244 			return (rv);
245 		}
246 		REG_TOPO_UNLOCK();
247 	}
248 
249 	return sysctl_handle_int(oidp, &uvolt, sizeof(uvolt), req);
250 }
251 
252 /* ----------------------------------------------------------------------------
253  *
254  * Default regulator methods for base class.
255  *
256  */
257 static int
regnode_method_init(struct regnode * regnode)258 regnode_method_init(struct regnode *regnode)
259 {
260 
261 	return (0);
262 }
263 
264 static int
regnode_method_enable(struct regnode * regnode,bool enable,int * udelay)265 regnode_method_enable(struct regnode *regnode, bool enable, int *udelay)
266 {
267 
268 	if (!enable)
269 		return (ENXIO);
270 
271 	*udelay = 0;
272 	return (0);
273 }
274 
275 static int
regnode_method_status(struct regnode * regnode,int * status)276 regnode_method_status(struct regnode *regnode, int *status)
277 {
278 	*status = REGULATOR_STATUS_ENABLED;
279 	return (0);
280 }
281 
282 static int
regnode_method_set_voltage(struct regnode * regnode,int min_uvolt,int max_uvolt,int * udelay)283 regnode_method_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt,
284     int *udelay)
285 {
286 
287 	if ((min_uvolt > regnode->std_param.max_uvolt) ||
288 	    (max_uvolt < regnode->std_param.min_uvolt))
289 		return (ERANGE);
290 	*udelay = 0;
291 	return (0);
292 }
293 
294 static int
regnode_method_get_voltage(struct regnode * regnode,int * uvolt)295 regnode_method_get_voltage(struct regnode *regnode, int *uvolt)
296 {
297 
298 	*uvolt = regnode->std_param.min_uvolt +
299 	    (regnode->std_param.max_uvolt - regnode->std_param.min_uvolt) / 2;
300 	return (0);
301 }
302 
303 int
regnode_method_check_voltage(struct regnode * regnode,int uvolt)304 regnode_method_check_voltage(struct regnode *regnode, int uvolt)
305 {
306 
307 	if ((uvolt > regnode->std_param.max_uvolt) ||
308 	    (uvolt < regnode->std_param.min_uvolt))
309 		return (ERANGE);
310 	return (0);
311 }
312 
313 /* ----------------------------------------------------------------------------
314  *
315  * Internal functions.
316  *
317  */
318 
319 static struct regnode *
regnode_find_by_name(const char * name)320 regnode_find_by_name(const char *name)
321 {
322 	struct regnode *entry;
323 
324 	REG_TOPO_ASSERT();
325 
326 	TAILQ_FOREACH(entry, &regnode_list, reglist_link) {
327 		if (strcmp(entry->name, name) == 0)
328 			return (entry);
329 	}
330 	return (NULL);
331 }
332 
333 static struct regnode *
regnode_find_by_id(device_t dev,intptr_t id)334 regnode_find_by_id(device_t dev, intptr_t id)
335 {
336 	struct regnode *entry;
337 
338 	REG_TOPO_ASSERT();
339 
340 	TAILQ_FOREACH(entry, &regnode_list, reglist_link) {
341 		if ((entry->pdev == dev) && (entry->id ==  id))
342 			return (entry);
343 	}
344 
345 	return (NULL);
346 }
347 
348 /*
349  * Create and initialize regulator object, but do not register it.
350  */
351 struct regnode *
regnode_create(device_t pdev,regnode_class_t regnode_class,struct regnode_init_def * def)352 regnode_create(device_t pdev, regnode_class_t regnode_class,
353     struct regnode_init_def *def)
354 {
355 	struct regnode *regnode;
356 	struct sysctl_oid *regnode_oid;
357 
358 	KASSERT(def->name != NULL, ("regulator name is NULL"));
359 	KASSERT(def->name[0] != '\0', ("regulator name is empty"));
360 
361 	REG_TOPO_SLOCK();
362 	if (regnode_find_by_name(def->name) != NULL)
363 		panic("Duplicated regulator registration: %s\n", def->name);
364 	REG_TOPO_UNLOCK();
365 
366 	/* Create object and initialize it. */
367 	regnode = malloc(sizeof(struct regnode), M_REGULATOR,
368 	    M_WAITOK | M_ZERO);
369 	kobj_init((kobj_t)regnode, (kobj_class_t)regnode_class);
370 	sx_init(&regnode->lock, "Regulator node lock");
371 
372 	/* Allocate softc if required. */
373 	if (regnode_class->size > 0) {
374 		regnode->softc = malloc(regnode_class->size, M_REGULATOR,
375 		    M_WAITOK | M_ZERO);
376 	}
377 
378 
379 	/* Copy all strings unless they're flagged as static. */
380 	if (def->flags & REGULATOR_FLAGS_STATIC) {
381 		regnode->name = def->name;
382 		regnode->parent_name = def->parent_name;
383 	} else {
384 		regnode->name = strdup(def->name, M_REGULATOR);
385 		if (def->parent_name != NULL)
386 			regnode->parent_name = strdup(def->parent_name,
387 			    M_REGULATOR);
388 	}
389 
390 	/* Rest of init. */
391 	TAILQ_INIT(&regnode->consumers_list);
392 	regnode->id = def->id;
393 	regnode->pdev = pdev;
394 	regnode->flags = def->flags;
395 	regnode->parent = NULL;
396 	regnode->std_param = def->std_param;
397 #ifdef FDT
398 	regnode->ofw_node = def->ofw_node;
399 #endif
400 
401 	sysctl_ctx_init(&regnode->sysctl_ctx);
402 	regnode_oid = SYSCTL_ADD_NODE(&regnode->sysctl_ctx,
403 	    SYSCTL_STATIC_CHILDREN(_hw_regulator),
404 	    OID_AUTO, regnode->name,
405 	    CTLFLAG_RD, 0, "A regulator node");
406 
407 	SYSCTL_ADD_INT(&regnode->sysctl_ctx,
408 	    SYSCTL_CHILDREN(regnode_oid),
409 	    OID_AUTO, "min_uvolt",
410 	    CTLFLAG_RD, &regnode->std_param.min_uvolt, 0,
411 	    "Minimal voltage (in uV)");
412 	SYSCTL_ADD_INT(&regnode->sysctl_ctx,
413 	    SYSCTL_CHILDREN(regnode_oid),
414 	    OID_AUTO, "max_uvolt",
415 	    CTLFLAG_RD, &regnode->std_param.max_uvolt, 0,
416 	    "Maximal voltage (in uV)");
417 	SYSCTL_ADD_INT(&regnode->sysctl_ctx,
418 	    SYSCTL_CHILDREN(regnode_oid),
419 	    OID_AUTO, "min_uamp",
420 	    CTLFLAG_RD, &regnode->std_param.min_uamp, 0,
421 	    "Minimal amperage (in uA)");
422 	SYSCTL_ADD_INT(&regnode->sysctl_ctx,
423 	    SYSCTL_CHILDREN(regnode_oid),
424 	    OID_AUTO, "max_uamp",
425 	    CTLFLAG_RD, &regnode->std_param.max_uamp, 0,
426 	    "Maximal amperage (in uA)");
427 	SYSCTL_ADD_INT(&regnode->sysctl_ctx,
428 	    SYSCTL_CHILDREN(regnode_oid),
429 	    OID_AUTO, "ramp_delay",
430 	    CTLFLAG_RD, &regnode->std_param.ramp_delay, 0,
431 	    "Ramp delay (in uV/us)");
432 	SYSCTL_ADD_INT(&regnode->sysctl_ctx,
433 	    SYSCTL_CHILDREN(regnode_oid),
434 	    OID_AUTO, "enable_delay",
435 	    CTLFLAG_RD, &regnode->std_param.enable_delay, 0,
436 	    "Enable delay (in us)");
437 	SYSCTL_ADD_INT(&regnode->sysctl_ctx,
438 	    SYSCTL_CHILDREN(regnode_oid),
439 	    OID_AUTO, "enable_cnt",
440 	    CTLFLAG_RD, &regnode->enable_cnt, 0,
441 	    "The regulator enable counter");
442 	SYSCTL_ADD_U8(&regnode->sysctl_ctx,
443 	    SYSCTL_CHILDREN(regnode_oid),
444 	    OID_AUTO, "boot_on",
445 	    CTLFLAG_RD, (uint8_t *) &regnode->std_param.boot_on, 0,
446 	    "Is enabled on boot");
447 	SYSCTL_ADD_U8(&regnode->sysctl_ctx,
448 	    SYSCTL_CHILDREN(regnode_oid),
449 	    OID_AUTO, "always_on",
450 	    CTLFLAG_RD, (uint8_t *)&regnode->std_param.always_on, 0,
451 	    "Is always enabled");
452 
453 	SYSCTL_ADD_PROC(&regnode->sysctl_ctx,
454 	    SYSCTL_CHILDREN(regnode_oid),
455 	    OID_AUTO, "uvolt",
456 	    CTLTYPE_INT | CTLFLAG_RD,
457 	    regnode, 0, regnode_uvolt_sysctl,
458 	    "I",
459 	    "Current voltage (in uV)");
460 
461 	return (regnode);
462 }
463 
464 /* Register regulator object. */
465 struct regnode *
regnode_register(struct regnode * regnode)466 regnode_register(struct regnode *regnode)
467 {
468 	int rv;
469 
470 #ifdef FDT
471 	if (regnode->ofw_node <= 0)
472 		regnode->ofw_node = ofw_bus_get_node(regnode->pdev);
473 	if (regnode->ofw_node <= 0)
474 		return (NULL);
475 #endif
476 
477 	rv = REGNODE_INIT(regnode);
478 	if (rv != 0) {
479 		printf("REGNODE_INIT failed: %d\n", rv);
480 		return (NULL);
481 	}
482 
483 	REG_TOPO_XLOCK();
484 	TAILQ_INSERT_TAIL(&regnode_list, regnode, reglist_link);
485 	REG_TOPO_UNLOCK();
486 #ifdef FDT
487 	OF_device_register_xref(OF_xref_from_node(regnode->ofw_node),
488 	    regnode->pdev);
489 #endif
490 	return (regnode);
491 }
492 
493 static int
regnode_resolve_parent(struct regnode * regnode)494 regnode_resolve_parent(struct regnode *regnode)
495 {
496 
497 	/* All ready resolved or no parent? */
498 	if ((regnode->parent != NULL) ||
499 	    (regnode->parent_name == NULL))
500 		return (0);
501 
502 	regnode->parent = regnode_find_by_name(regnode->parent_name);
503 	if (regnode->parent == NULL)
504 		return (ENODEV);
505 	return (0);
506 }
507 
508 static void
regnode_delay(int usec)509 regnode_delay(int usec)
510 {
511 	int ticks;
512 
513 	if (usec == 0)
514 		return;
515 	ticks = (usec * hz + 999999) / 1000000;
516 
517 	if (cold || ticks < 2)
518 		DELAY(usec);
519 	else
520 		pause("REGULATOR", ticks);
521 }
522 
523 /* --------------------------------------------------------------------------
524  *
525  * Regulator providers interface
526  *
527  */
528 
529 const char *
regnode_get_name(struct regnode * regnode)530 regnode_get_name(struct regnode *regnode)
531 {
532 
533 	return (regnode->name);
534 }
535 
536 const char *
regnode_get_parent_name(struct regnode * regnode)537 regnode_get_parent_name(struct regnode *regnode)
538 {
539 
540 	return (regnode->parent_name);
541 }
542 
543 int
regnode_get_flags(struct regnode * regnode)544 regnode_get_flags(struct regnode *regnode)
545 {
546 
547 	return (regnode->flags);
548 }
549 
550 void *
regnode_get_softc(struct regnode * regnode)551 regnode_get_softc(struct regnode *regnode)
552 {
553 
554 	return (regnode->softc);
555 }
556 
557 device_t
regnode_get_device(struct regnode * regnode)558 regnode_get_device(struct regnode *regnode)
559 {
560 
561 	return (regnode->pdev);
562 }
563 
regnode_get_stdparam(struct regnode * regnode)564 struct regnode_std_param *regnode_get_stdparam(struct regnode *regnode)
565 {
566 
567 	return (&regnode->std_param);
568 }
569 
regnode_topo_unlock(void)570 void regnode_topo_unlock(void)
571 {
572 
573 	REG_TOPO_UNLOCK();
574 }
575 
regnode_topo_xlock(void)576 void regnode_topo_xlock(void)
577 {
578 
579 	REG_TOPO_XLOCK();
580 }
581 
regnode_topo_slock(void)582 void regnode_topo_slock(void)
583 {
584 
585 	REG_TOPO_SLOCK();
586 }
587 
588 
589 /* --------------------------------------------------------------------------
590  *
591  * Real consumers executive
592  *
593  */
594 struct regnode *
regnode_get_parent(struct regnode * regnode)595 regnode_get_parent(struct regnode *regnode)
596 {
597 	int rv;
598 
599 	REG_TOPO_ASSERT();
600 
601 	rv = regnode_resolve_parent(regnode);
602 	if (rv != 0)
603 		return (NULL);
604 
605 	return (regnode->parent);
606 }
607 
608 /*
609  * Enable regulator.
610  */
611 int
regnode_enable(struct regnode * regnode)612 regnode_enable(struct regnode *regnode)
613 {
614 	int udelay;
615 	int rv;
616 
617 	REG_TOPO_ASSERT();
618 
619 	/* Enable regulator for each node in chain, starting from source. */
620 	rv = regnode_resolve_parent(regnode);
621 	if (rv != 0)
622 		return (rv);
623 	if (regnode->parent != NULL) {
624 		rv = regnode_enable(regnode->parent);
625 		if (rv != 0)
626 			return (rv);
627 	}
628 
629 	/* Handle this node. */
630 	REGNODE_XLOCK(regnode);
631 	if (regnode->enable_cnt == 0) {
632 		rv = REGNODE_ENABLE(regnode, true, &udelay);
633 		if (rv != 0) {
634 			REGNODE_UNLOCK(regnode);
635 			return (rv);
636 		}
637 		regnode_delay(udelay);
638 	}
639 	regnode->enable_cnt++;
640 	REGNODE_UNLOCK(regnode);
641 	return (0);
642 }
643 
644 /*
645  * Disable regulator.
646  */
647 int
regnode_disable(struct regnode * regnode)648 regnode_disable(struct regnode *regnode)
649 {
650 	int udelay;
651 	int rv;
652 
653 	REG_TOPO_ASSERT();
654 	rv = 0;
655 
656 	REGNODE_XLOCK(regnode);
657 	/* Disable regulator for each node in chain, starting from consumer. */
658 	if (regnode->enable_cnt == 1 &&
659 	    (regnode->flags & REGULATOR_FLAGS_NOT_DISABLE) == 0 &&
660 	    !regnode->std_param.always_on) {
661 		rv = REGNODE_ENABLE(regnode, false, &udelay);
662 		if (rv != 0) {
663 			REGNODE_UNLOCK(regnode);
664 			return (rv);
665 		}
666 		regnode_delay(udelay);
667 	}
668 	regnode->enable_cnt--;
669 	REGNODE_UNLOCK(regnode);
670 
671 	rv = regnode_resolve_parent(regnode);
672 	if (rv != 0)
673 		return (rv);
674 	if (regnode->parent != NULL)
675 		rv = regnode_disable(regnode->parent);
676 	return (rv);
677 }
678 
679 /*
680  * Stop regulator.
681  */
682 int
regnode_stop(struct regnode * regnode,int depth)683 regnode_stop(struct regnode *regnode, int depth)
684 {
685 	int udelay;
686 	int rv;
687 
688 	REG_TOPO_ASSERT();
689 	rv = 0;
690 
691 	REGNODE_XLOCK(regnode);
692 	/* The first node must not be enabled. */
693 	if ((regnode->enable_cnt != 0) && (depth == 0)) {
694 		REGNODE_UNLOCK(regnode);
695 		return (EBUSY);
696 	}
697 	/* Disable regulator for each node in chain, starting from consumer */
698 	if ((regnode->enable_cnt == 0) &&
699 	    ((regnode->flags & REGULATOR_FLAGS_NOT_DISABLE) == 0)) {
700 		rv = REGNODE_STOP(regnode, &udelay);
701 		if (rv != 0) {
702 			REGNODE_UNLOCK(regnode);
703 			return (rv);
704 		}
705 		regnode_delay(udelay);
706 	}
707 	REGNODE_UNLOCK(regnode);
708 
709 	rv = regnode_resolve_parent(regnode);
710 	if (rv != 0)
711 		return (rv);
712 	if (regnode->parent != NULL && regnode->parent->enable_cnt == 0)
713 		rv = regnode_stop(regnode->parent, depth + 1);
714 	return (rv);
715 }
716 
717 /*
718  * Get regulator status. (REGULATOR_STATUS_*)
719  */
720 int
regnode_status(struct regnode * regnode,int * status)721 regnode_status(struct regnode *regnode, int *status)
722 {
723 	int rv;
724 
725 	REG_TOPO_ASSERT();
726 
727 	REGNODE_XLOCK(regnode);
728 	rv = REGNODE_STATUS(regnode, status);
729 	REGNODE_UNLOCK(regnode);
730 	return (rv);
731 }
732 
733 /*
734  * Get actual regulator voltage.
735  */
736 int
regnode_get_voltage(struct regnode * regnode,int * uvolt)737 regnode_get_voltage(struct regnode *regnode, int *uvolt)
738 {
739 	int rv;
740 
741 	REG_TOPO_ASSERT();
742 
743 	REGNODE_XLOCK(regnode);
744 	rv = REGNODE_GET_VOLTAGE(regnode, uvolt);
745 	REGNODE_UNLOCK(regnode);
746 
747 	/* Pass call into parent, if regulator is in bypass mode. */
748 	if (rv == ENOENT) {
749 		rv = regnode_resolve_parent(regnode);
750 		if (rv != 0)
751 			return (rv);
752 		if (regnode->parent != NULL)
753 			rv = regnode_get_voltage(regnode->parent, uvolt);
754 
755 	}
756 	return (rv);
757 }
758 
759 /*
760  * Set regulator voltage.
761  */
762 int
regnode_set_voltage(struct regnode * regnode,int min_uvolt,int max_uvolt)763 regnode_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt)
764 {
765 	int udelay;
766 	int rv;
767 
768 	REG_TOPO_ASSERT();
769 
770 	REGNODE_XLOCK(regnode);
771 
772 	rv = REGNODE_SET_VOLTAGE(regnode, min_uvolt, max_uvolt, &udelay);
773 	if (rv == 0)
774 		regnode_delay(udelay);
775 	REGNODE_UNLOCK(regnode);
776 	return (rv);
777 }
778 
779 /*
780  * Consumer variant of regnode_set_voltage().
781  */
782 static int
regnode_set_voltage_checked(struct regnode * regnode,struct regulator * reg,int min_uvolt,int max_uvolt)783 regnode_set_voltage_checked(struct regnode *regnode, struct regulator *reg,
784     int min_uvolt, int max_uvolt)
785 {
786 	int udelay;
787 	int all_max_uvolt;
788 	int all_min_uvolt;
789 	struct regulator *tmp;
790 	int rv;
791 
792 	REG_TOPO_ASSERT();
793 
794 	REGNODE_XLOCK(regnode);
795 	/* Return error if requested range is outside of regulator range. */
796 	if ((min_uvolt > regnode->std_param.max_uvolt) ||
797 	    (max_uvolt < regnode->std_param.min_uvolt)) {
798 		REGNODE_UNLOCK(regnode);
799 		return (ERANGE);
800 	}
801 
802 	/* Get actual voltage range for all consumers. */
803 	all_min_uvolt = regnode->std_param.min_uvolt;
804 	all_max_uvolt = regnode->std_param.max_uvolt;
805 	TAILQ_FOREACH(tmp, &regnode->consumers_list, link) {
806 		/* Don't take requestor in account. */
807 		if (tmp == reg)
808 			continue;
809 		if (all_min_uvolt < tmp->min_uvolt)
810 			all_min_uvolt = tmp->min_uvolt;
811 		if (all_max_uvolt > tmp->max_uvolt)
812 			all_max_uvolt = tmp->max_uvolt;
813 	}
814 
815 	/* Test if request fits to actual contract. */
816 	if ((min_uvolt > all_max_uvolt) ||
817 	    (max_uvolt < all_min_uvolt)) {
818 		REGNODE_UNLOCK(regnode);
819 		return (ERANGE);
820 	}
821 
822 	/* Adjust new range.*/
823 	if (min_uvolt < all_min_uvolt)
824 		min_uvolt = all_min_uvolt;
825 	if (max_uvolt > all_max_uvolt)
826 		max_uvolt = all_max_uvolt;
827 
828 	rv = REGNODE_SET_VOLTAGE(regnode, min_uvolt, max_uvolt, &udelay);
829 	regnode_delay(udelay);
830 	REGNODE_UNLOCK(regnode);
831 	return (rv);
832 }
833 
834 int
regnode_set_constraint(struct regnode * regnode)835 regnode_set_constraint(struct regnode *regnode)
836 {
837 	int status, rv, uvolt;
838 
839 	if (regnode->std_param.boot_on != true &&
840 	    regnode->std_param.always_on != true)
841 		return (0);
842 
843 	rv = regnode_status(regnode, &status);
844 	if (rv != 0) {
845 		if (bootverbose)
846 			printf("Cannot get regulator status for %s\n",
847 			    regnode_get_name(regnode));
848 		return (rv);
849 	}
850 
851 	if (status == REGULATOR_STATUS_ENABLED)
852 		return (0);
853 
854 	rv = regnode_get_voltage(regnode, &uvolt);
855 	if (rv != 0) {
856 		if (bootverbose)
857 			printf("Cannot get regulator voltage for %s\n",
858 			    regnode_get_name(regnode));
859 		return (rv);
860 	}
861 
862 	if (uvolt < regnode->std_param.min_uvolt ||
863 	  uvolt > regnode->std_param.max_uvolt) {
864 		if (bootverbose)
865 			printf("Regulator %s current voltage %d is not in the"
866 			    " acceptable range : %d<->%d\n",
867 			    regnode_get_name(regnode),
868 			    uvolt, regnode->std_param.min_uvolt,
869 			    regnode->std_param.max_uvolt);
870 		return (ERANGE);
871 	}
872 
873 	rv = regnode_enable(regnode);
874 	if (rv != 0) {
875 		if (bootverbose)
876 			printf("Cannot enable regulator %s\n",
877 			    regnode_get_name(regnode));
878 		return (rv);
879 	}
880 
881 	return (0);
882 }
883 
884 #ifdef FDT
885 phandle_t
regnode_get_ofw_node(struct regnode * regnode)886 regnode_get_ofw_node(struct regnode *regnode)
887 {
888 
889 	return (regnode->ofw_node);
890 }
891 #endif
892 
893 /* --------------------------------------------------------------------------
894  *
895  * Regulator consumers interface.
896  *
897  */
898 /* Helper function for regulator_get*() */
899 static regulator_t
regulator_create(struct regnode * regnode,device_t cdev)900 regulator_create(struct regnode *regnode, device_t cdev)
901 {
902 	struct regulator *reg;
903 
904 	REG_TOPO_ASSERT();
905 
906 	reg =  malloc(sizeof(struct regulator), M_REGULATOR,
907 	    M_WAITOK | M_ZERO);
908 	reg->cdev = cdev;
909 	reg->regnode = regnode;
910 	reg->enable_cnt = 0;
911 
912 	REGNODE_XLOCK(regnode);
913 	regnode->ref_cnt++;
914 	TAILQ_INSERT_TAIL(&regnode->consumers_list, reg, link);
915 	reg ->min_uvolt = regnode->std_param.min_uvolt;
916 	reg ->max_uvolt = regnode->std_param.max_uvolt;
917 	REGNODE_UNLOCK(regnode);
918 
919 	return (reg);
920 }
921 
922 int
regulator_enable(regulator_t reg)923 regulator_enable(regulator_t reg)
924 {
925 	int rv;
926 	struct regnode *regnode;
927 
928 	regnode = reg->regnode;
929 	KASSERT(regnode->ref_cnt > 0,
930 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
931 	REG_TOPO_SLOCK();
932 	rv = regnode_enable(regnode);
933 	if (rv == 0)
934 		reg->enable_cnt++;
935 	REG_TOPO_UNLOCK();
936 	return (rv);
937 }
938 
939 int
regulator_disable(regulator_t reg)940 regulator_disable(regulator_t reg)
941 {
942 	int rv;
943 	struct regnode *regnode;
944 
945 	regnode = reg->regnode;
946 	KASSERT(regnode->ref_cnt > 0,
947 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
948 	KASSERT(reg->enable_cnt > 0,
949 	   ("Attempt to disable already disabled regulator: %s\n",
950 	   regnode->name));
951 	REG_TOPO_SLOCK();
952 	rv = regnode_disable(regnode);
953 	if (rv == 0)
954 		reg->enable_cnt--;
955 	REG_TOPO_UNLOCK();
956 	return (rv);
957 }
958 
959 int
regulator_stop(regulator_t reg)960 regulator_stop(regulator_t reg)
961 {
962 	int rv;
963 	struct regnode *regnode;
964 
965 	regnode = reg->regnode;
966 	KASSERT(regnode->ref_cnt > 0,
967 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
968 	KASSERT(reg->enable_cnt == 0,
969 	   ("Attempt to stop already enabled regulator: %s\n", regnode->name));
970 
971 	REG_TOPO_SLOCK();
972 	rv = regnode_stop(regnode, 0);
973 	REG_TOPO_UNLOCK();
974 	return (rv);
975 }
976 
977 int
regulator_status(regulator_t reg,int * status)978 regulator_status(regulator_t reg, int *status)
979 {
980 	int rv;
981 	struct regnode *regnode;
982 
983 	regnode = reg->regnode;
984 	KASSERT(regnode->ref_cnt > 0,
985 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
986 
987 	REG_TOPO_SLOCK();
988 	rv = regnode_status(regnode, status);
989 	REG_TOPO_UNLOCK();
990 	return (rv);
991 }
992 
993 int
regulator_get_voltage(regulator_t reg,int * uvolt)994 regulator_get_voltage(regulator_t reg, int *uvolt)
995 {
996 	int rv;
997 	struct regnode *regnode;
998 
999 	regnode = reg->regnode;
1000 	KASSERT(regnode->ref_cnt > 0,
1001 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
1002 
1003 	REG_TOPO_SLOCK();
1004 	rv = regnode_get_voltage(regnode, uvolt);
1005 	REG_TOPO_UNLOCK();
1006 	return (rv);
1007 }
1008 
1009 int
regulator_set_voltage(regulator_t reg,int min_uvolt,int max_uvolt)1010 regulator_set_voltage(regulator_t reg, int min_uvolt, int max_uvolt)
1011 {
1012 	struct regnode *regnode;
1013 	int rv;
1014 
1015 	regnode = reg->regnode;
1016 	KASSERT(regnode->ref_cnt > 0,
1017 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
1018 
1019 	REG_TOPO_SLOCK();
1020 
1021 	rv = regnode_set_voltage_checked(regnode, reg, min_uvolt, max_uvolt);
1022 	if (rv == 0) {
1023 		reg->min_uvolt = min_uvolt;
1024 		reg->max_uvolt = max_uvolt;
1025 	}
1026 	REG_TOPO_UNLOCK();
1027 	return (rv);
1028 }
1029 
1030 int
regulator_check_voltage(regulator_t reg,int uvolt)1031 regulator_check_voltage(regulator_t reg, int uvolt)
1032 {
1033 	int rv;
1034 	struct regnode *regnode;
1035 
1036 	regnode = reg->regnode;
1037 	KASSERT(regnode->ref_cnt > 0,
1038 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
1039 
1040 	REG_TOPO_SLOCK();
1041 	rv = REGNODE_CHECK_VOLTAGE(regnode, uvolt);
1042 	REG_TOPO_UNLOCK();
1043 	return (rv);
1044 }
1045 
1046 const char *
regulator_get_name(regulator_t reg)1047 regulator_get_name(regulator_t reg)
1048 {
1049 	struct regnode *regnode;
1050 
1051 	regnode = reg->regnode;
1052 	KASSERT(regnode->ref_cnt > 0,
1053 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
1054 	return (regnode->name);
1055 }
1056 
1057 int
regulator_get_by_name(device_t cdev,const char * name,regulator_t * reg)1058 regulator_get_by_name(device_t cdev, const char *name, regulator_t *reg)
1059 {
1060 	struct regnode *regnode;
1061 
1062 	REG_TOPO_SLOCK();
1063 	regnode = regnode_find_by_name(name);
1064 	if (regnode == NULL) {
1065 		REG_TOPO_UNLOCK();
1066 		return (ENODEV);
1067 	}
1068 	*reg = regulator_create(regnode, cdev);
1069 	REG_TOPO_UNLOCK();
1070 	return (0);
1071 }
1072 
1073 int
regulator_get_by_id(device_t cdev,device_t pdev,intptr_t id,regulator_t * reg)1074 regulator_get_by_id(device_t cdev, device_t pdev, intptr_t id, regulator_t *reg)
1075 {
1076 	struct regnode *regnode;
1077 
1078 	REG_TOPO_SLOCK();
1079 
1080 	regnode = regnode_find_by_id(pdev, id);
1081 	if (regnode == NULL) {
1082 		REG_TOPO_UNLOCK();
1083 		return (ENODEV);
1084 	}
1085 	*reg = regulator_create(regnode, cdev);
1086 	REG_TOPO_UNLOCK();
1087 
1088 	return (0);
1089 }
1090 
1091 int
regulator_release(regulator_t reg)1092 regulator_release(regulator_t reg)
1093 {
1094 	struct regnode *regnode;
1095 
1096 	regnode = reg->regnode;
1097 	KASSERT(regnode->ref_cnt > 0,
1098 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
1099 	REG_TOPO_SLOCK();
1100 	while (reg->enable_cnt > 0) {
1101 		regnode_disable(regnode);
1102 		reg->enable_cnt--;
1103 	}
1104 	REGNODE_XLOCK(regnode);
1105 	TAILQ_REMOVE(&regnode->consumers_list, reg, link);
1106 	regnode->ref_cnt--;
1107 	REGNODE_UNLOCK(regnode);
1108 	REG_TOPO_UNLOCK();
1109 
1110 	free(reg, M_REGULATOR);
1111 	return (0);
1112 }
1113 
1114 #ifdef FDT
1115 /* Default DT mapper. */
1116 int
regdev_default_ofw_map(device_t dev,phandle_t xref,int ncells,pcell_t * cells,intptr_t * id)1117 regdev_default_ofw_map(device_t dev, phandle_t 	xref, int ncells,
1118     pcell_t *cells, intptr_t *id)
1119 {
1120 	if (ncells == 0)
1121 		*id = 1;
1122 	else if (ncells == 1)
1123 		*id = cells[0];
1124 	else
1125 		return  (ERANGE);
1126 
1127 	return (0);
1128 }
1129 
1130 int
regulator_parse_ofw_stdparam(device_t pdev,phandle_t node,struct regnode_init_def * def)1131 regulator_parse_ofw_stdparam(device_t pdev, phandle_t node,
1132     struct regnode_init_def *def)
1133 {
1134 	phandle_t supply_xref;
1135 	struct regnode_std_param *par;
1136 	int rv;
1137 
1138 	par = &def->std_param;
1139 	rv = OF_getprop_alloc(node, "regulator-name",
1140 	    (void **)&def->name);
1141 	if (rv <= 0) {
1142 		device_printf(pdev, "%s: Missing regulator name\n",
1143 		 __func__);
1144 		return (ENXIO);
1145 	}
1146 
1147 	rv = OF_getencprop(node, "regulator-min-microvolt", &par->min_uvolt,
1148 	    sizeof(par->min_uvolt));
1149 	if (rv <= 0)
1150 		par->min_uvolt = 0;
1151 
1152 	rv = OF_getencprop(node, "regulator-max-microvolt", &par->max_uvolt,
1153 	    sizeof(par->max_uvolt));
1154 	if (rv <= 0)
1155 		par->max_uvolt = 0;
1156 
1157 	rv = OF_getencprop(node, "regulator-min-microamp", &par->min_uamp,
1158 	    sizeof(par->min_uamp));
1159 	if (rv <= 0)
1160 		par->min_uamp = 0;
1161 
1162 	rv = OF_getencprop(node, "regulator-max-microamp", &par->max_uamp,
1163 	    sizeof(par->max_uamp));
1164 	if (rv <= 0)
1165 		par->max_uamp = 0;
1166 
1167 	rv = OF_getencprop(node, "regulator-ramp-delay", &par->ramp_delay,
1168 	    sizeof(par->ramp_delay));
1169 	if (rv <= 0)
1170 		par->ramp_delay = 0;
1171 
1172 	rv = OF_getencprop(node, "regulator-enable-ramp-delay",
1173 	    &par->enable_delay, sizeof(par->enable_delay));
1174 	if (rv <= 0)
1175 		par->enable_delay = 0;
1176 
1177 	if (OF_hasprop(node, "regulator-boot-on"))
1178 		par->boot_on = true;
1179 
1180 	if (OF_hasprop(node, "regulator-always-on"))
1181 		par->always_on = true;
1182 
1183 	if (OF_hasprop(node, "enable-active-high"))
1184 		par->enable_active_high = 1;
1185 
1186 	rv = OF_getencprop(node, "vin-supply", &supply_xref,
1187 	    sizeof(supply_xref));
1188 	if (rv >=  0) {
1189 		rv = OF_getprop_alloc(supply_xref, "regulator-name",
1190 		    (void **)&def->parent_name);
1191 		if (rv <= 0)
1192 			def->parent_name = NULL;
1193 	}
1194 	return (0);
1195 }
1196 
1197 int
regulator_get_by_ofw_property(device_t cdev,phandle_t cnode,char * name,regulator_t * reg)1198 regulator_get_by_ofw_property(device_t cdev, phandle_t cnode, char *name,
1199     regulator_t *reg)
1200 {
1201 	phandle_t *cells;
1202 	device_t regdev;
1203 	int ncells, rv;
1204 	intptr_t id;
1205 
1206 	*reg = NULL;
1207 
1208 	if (cnode <= 0)
1209 		cnode = ofw_bus_get_node(cdev);
1210 	if (cnode <= 0) {
1211 		device_printf(cdev, "%s called on not ofw based device\n",
1212 		 __func__);
1213 		return (ENXIO);
1214 	}
1215 
1216 	cells = NULL;
1217 	ncells = OF_getencprop_alloc_multi(cnode, name, sizeof(*cells),
1218 	    (void **)&cells);
1219 	if (ncells <= 0)
1220 		return (ENOENT);
1221 
1222 	/* Translate xref to device */
1223 	regdev = OF_device_from_xref(cells[0]);
1224 	if (regdev == NULL) {
1225 		OF_prop_free(cells);
1226 		return (ENODEV);
1227 	}
1228 
1229 	/* Map regulator to number */
1230 	rv = REGDEV_MAP(regdev, cells[0], ncells - 1, cells + 1, &id);
1231 	OF_prop_free(cells);
1232 	if (rv != 0)
1233 		return (rv);
1234 	return (regulator_get_by_id(cdev, regdev, id, reg));
1235 }
1236 #endif
1237 
1238 /* --------------------------------------------------------------------------
1239  *
1240  * Regulator utility functions.
1241  *
1242  */
1243 
1244 /* Convert raw selector value to real voltage */
1245 int
regulator_range_sel8_to_volt(struct regulator_range * ranges,int nranges,uint8_t sel,int * volt)1246 regulator_range_sel8_to_volt(struct regulator_range *ranges, int nranges,
1247    uint8_t sel, int *volt)
1248 {
1249 	struct regulator_range *range;
1250 	int i;
1251 
1252 	if (nranges == 0)
1253 		panic("Voltage regulator have zero ranges\n");
1254 
1255 	for (i = 0; i < nranges ; i++) {
1256 		range = ranges  + i;
1257 
1258 		if (!(sel >= range->min_sel &&
1259 		      sel <= range->max_sel))
1260 			continue;
1261 
1262 		sel -= range->min_sel;
1263 
1264 		*volt = range->min_uvolt + sel * range->step_uvolt;
1265 		return (0);
1266 	}
1267 
1268 	return (ERANGE);
1269 }
1270 
1271 int
regulator_range_volt_to_sel8(struct regulator_range * ranges,int nranges,int min_uvolt,int max_uvolt,uint8_t * out_sel)1272 regulator_range_volt_to_sel8(struct regulator_range *ranges, int nranges,
1273     int min_uvolt, int max_uvolt, uint8_t *out_sel)
1274 {
1275 	struct regulator_range *range;
1276 	uint8_t sel;
1277 	int uvolt;
1278 	int rv, i;
1279 
1280 	if (nranges == 0)
1281 		panic("Voltage regulator have zero ranges\n");
1282 
1283 	for (i = 0; i < nranges; i++) {
1284 		range = ranges  + i;
1285 		uvolt = range->min_uvolt +
1286 		    (range->max_sel - range->min_sel) * range->step_uvolt;
1287 
1288 		if ((min_uvolt > uvolt) ||
1289 		    (max_uvolt < range->min_uvolt))
1290 			continue;
1291 
1292 		if (min_uvolt <= range->min_uvolt)
1293 			min_uvolt = range->min_uvolt;
1294 
1295 		/* if step == 0 -> fixed voltage range. */
1296 		if (range->step_uvolt == 0)
1297 			sel = 0;
1298 		else
1299 			sel = DIV_ROUND_UP(min_uvolt - range->min_uvolt,
1300 			   range->step_uvolt);
1301 
1302 
1303 		sel += range->min_sel;
1304 
1305 		break;
1306 	}
1307 
1308 	if (i >= nranges)
1309 		return (ERANGE);
1310 
1311 	/* Verify new settings. */
1312 	rv = regulator_range_sel8_to_volt(ranges, nranges, sel, &uvolt);
1313 	if (rv != 0)
1314 		return (rv);
1315 	if ((uvolt < min_uvolt) || (uvolt > max_uvolt))
1316 		return (ERANGE);
1317 
1318 	*out_sel = sel;
1319 	return (0);
1320 }
1321