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