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
regulator_shutdown(void * dummy)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 && 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
regnode_uvolt_sysctl(SYSCTL_HANDLER_ARGS)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
regnode_method_enable(struct regnode * regnode,bool enable,int * udelay)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
regnode_method_status(struct regnode * regnode,int * status)227 regnode_method_status(struct regnode *regnode, int *status)
228 {
229 	*status = REGULATOR_STATUS_ENABLED;
230 	return (0);
231 }
232 
233 static int
regnode_method_set_voltage(struct regnode * regnode,int min_uvolt,int max_uvolt,int * udelay)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
regnode_method_get_voltage(struct regnode * regnode,int * uvolt)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 *
regnode_find_by_name(const char * name)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 *
regnode_find_by_id(device_t dev,intptr_t id)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 *
regnode_create(device_t pdev,regnode_class_t regnode_class,struct regnode_init_def * def)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 *
regnode_register(struct regnode * 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
regnode_resolve_parent(struct regnode * regnode)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
regnode_delay(int usec)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 *
regnode_get_name(struct regnode * regnode)470 regnode_get_name(struct regnode *regnode)
471 {
472 
473 	return (regnode->name);
474 }
475 
476 const char *
regnode_get_parent_name(struct regnode * regnode)477 regnode_get_parent_name(struct regnode *regnode)
478 {
479 
480 	return (regnode->parent_name);
481 }
482 
483 int
regnode_get_flags(struct regnode * regnode)484 regnode_get_flags(struct regnode *regnode)
485 {
486 
487 	return (regnode->flags);
488 }
489 
490 void *
regnode_get_softc(struct regnode * regnode)491 regnode_get_softc(struct regnode *regnode)
492 {
493 
494 	return (regnode->softc);
495 }
496 
497 device_t
regnode_get_device(struct regnode * regnode)498 regnode_get_device(struct regnode *regnode)
499 {
500 
501 	return (regnode->pdev);
502 }
503 
regnode_get_stdparam(struct regnode * regnode)504 struct regnode_std_param *regnode_get_stdparam(struct regnode *regnode)
505 {
506 
507 	return (&regnode->std_param);
508 }
509 
regnode_topo_unlock(void)510 void regnode_topo_unlock(void)
511 {
512 
513 	REG_TOPO_UNLOCK();
514 }
515 
regnode_topo_xlock(void)516 void regnode_topo_xlock(void)
517 {
518 
519 	REG_TOPO_XLOCK();
520 }
521 
regnode_topo_slock(void)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 *
regnode_get_parent(struct regnode * 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
regnode_enable(struct regnode * regnode)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
regnode_disable(struct regnode * regnode)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 	    !regnode->std_param.always_on) {
601 		rv = REGNODE_ENABLE(regnode, false, &udelay);
602 		if (rv != 0) {
603 			REGNODE_UNLOCK(regnode);
604 			return (rv);
605 		}
606 		regnode_delay(udelay);
607 	}
608 	regnode->enable_cnt--;
609 	REGNODE_UNLOCK(regnode);
610 
611 	rv = regnode_resolve_parent(regnode);
612 	if (rv != 0)
613 		return (rv);
614 	if (regnode->parent != NULL)
615 		rv = regnode_disable(regnode->parent);
616 	return (rv);
617 }
618 
619 /*
620  * Stop regulator.
621  */
622 int
regnode_stop(struct regnode * regnode,int depth)623 regnode_stop(struct regnode *regnode, int depth)
624 {
625 	int udelay;
626 	int rv;
627 
628 	REG_TOPO_ASSERT();
629 	rv = 0;
630 
631 	REGNODE_XLOCK(regnode);
632 	/* The first node must not be enabled. */
633 	if ((regnode->enable_cnt != 0) && (depth == 0)) {
634 		REGNODE_UNLOCK(regnode);
635 		return (EBUSY);
636 	}
637 	/* Disable regulator for each node in chain, starting from consumer */
638 	if ((regnode->enable_cnt == 0) &&
639 	    ((regnode->flags & REGULATOR_FLAGS_NOT_DISABLE) == 0)) {
640 		rv = REGNODE_STOP(regnode, &udelay);
641 		if (rv != 0) {
642 			REGNODE_UNLOCK(regnode);
643 			return (rv);
644 		}
645 		regnode_delay(udelay);
646 	}
647 	REGNODE_UNLOCK(regnode);
648 
649 	rv = regnode_resolve_parent(regnode);
650 	if (rv != 0)
651 		return (rv);
652 	if (regnode->parent != NULL && regnode->parent->enable_cnt == 0)
653 		rv = regnode_stop(regnode->parent, depth + 1);
654 	return (rv);
655 }
656 
657 /*
658  * Get regulator status. (REGULATOR_STATUS_*)
659  */
660 int
regnode_status(struct regnode * regnode,int * status)661 regnode_status(struct regnode *regnode, int *status)
662 {
663 	int rv;
664 
665 	REG_TOPO_ASSERT();
666 
667 	REGNODE_XLOCK(regnode);
668 	rv = REGNODE_STATUS(regnode, status);
669 	REGNODE_UNLOCK(regnode);
670 	return (rv);
671 }
672 
673 /*
674  * Get actual regulator voltage.
675  */
676 int
regnode_get_voltage(struct regnode * regnode,int * uvolt)677 regnode_get_voltage(struct regnode *regnode, int *uvolt)
678 {
679 	int rv;
680 
681 	REG_TOPO_ASSERT();
682 
683 	REGNODE_XLOCK(regnode);
684 	rv = REGNODE_GET_VOLTAGE(regnode, uvolt);
685 	REGNODE_UNLOCK(regnode);
686 
687 	/* Pass call into parent, if regulator is in bypass mode. */
688 	if (rv == ENOENT) {
689 		rv = regnode_resolve_parent(regnode);
690 		if (rv != 0)
691 			return (rv);
692 		if (regnode->parent != NULL)
693 			rv = regnode_get_voltage(regnode->parent, uvolt);
694 
695 	}
696 	return (rv);
697 }
698 
699 /*
700  * Set regulator voltage.
701  */
702 int
regnode_set_voltage(struct regnode * regnode,int min_uvolt,int max_uvolt)703 regnode_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt)
704 {
705 	int udelay;
706 	int rv;
707 
708 	REG_TOPO_ASSERT();
709 
710 	REGNODE_XLOCK(regnode);
711 
712 	rv = REGNODE_SET_VOLTAGE(regnode, min_uvolt, max_uvolt, &udelay);
713 	if (rv == 0)
714 		regnode_delay(udelay);
715 	REGNODE_UNLOCK(regnode);
716 	return (rv);
717 }
718 
719 /*
720  * Consumer variant of regnode_set_voltage().
721  */
722 static int
regnode_set_voltage_checked(struct regnode * regnode,struct regulator * reg,int min_uvolt,int max_uvolt)723 regnode_set_voltage_checked(struct regnode *regnode, struct regulator *reg,
724     int min_uvolt, int max_uvolt)
725 {
726 	int udelay;
727 	int all_max_uvolt;
728 	int all_min_uvolt;
729 	struct regulator *tmp;
730 	int rv;
731 
732 	REG_TOPO_ASSERT();
733 
734 	REGNODE_XLOCK(regnode);
735 	/* Return error if requested range is outside of regulator range. */
736 	if ((min_uvolt > regnode->std_param.max_uvolt) ||
737 	    (max_uvolt < regnode->std_param.min_uvolt)) {
738 		REGNODE_UNLOCK(regnode);
739 		return (ERANGE);
740 	}
741 
742 	/* Get actual voltage range for all consumers. */
743 	all_min_uvolt = regnode->std_param.min_uvolt;
744 	all_max_uvolt = regnode->std_param.max_uvolt;
745 	TAILQ_FOREACH(tmp, &regnode->consumers_list, link) {
746 		/* Don't take requestor in account. */
747 		if (tmp == reg)
748 			continue;
749 		if (all_min_uvolt < tmp->min_uvolt)
750 			all_min_uvolt = tmp->min_uvolt;
751 		if (all_max_uvolt > tmp->max_uvolt)
752 			all_max_uvolt = tmp->max_uvolt;
753 	}
754 
755 	/* Test if request fits to actual contract. */
756 	if ((min_uvolt > all_max_uvolt) ||
757 	    (max_uvolt < all_min_uvolt)) {
758 		REGNODE_UNLOCK(regnode);
759 		return (ERANGE);
760 	}
761 
762 	/* Adjust new range.*/
763 	if (min_uvolt < all_min_uvolt)
764 		min_uvolt = all_min_uvolt;
765 	if (max_uvolt > all_max_uvolt)
766 		max_uvolt = all_max_uvolt;
767 
768 	rv = REGNODE_SET_VOLTAGE(regnode, min_uvolt, max_uvolt, &udelay);
769 	regnode_delay(udelay);
770 	REGNODE_UNLOCK(regnode);
771 	return (rv);
772 }
773 
774 #ifdef FDT
775 phandle_t
regnode_get_ofw_node(struct regnode * regnode)776 regnode_get_ofw_node(struct regnode *regnode)
777 {
778 
779 	return (regnode->ofw_node);
780 }
781 #endif
782 
783 /* --------------------------------------------------------------------------
784  *
785  * Regulator consumers interface.
786  *
787  */
788 /* Helper function for regulator_get*() */
789 static regulator_t
regulator_create(struct regnode * regnode,device_t cdev)790 regulator_create(struct regnode *regnode, device_t cdev)
791 {
792 	struct regulator *reg;
793 
794 	REG_TOPO_ASSERT();
795 
796 	reg =  malloc(sizeof(struct regulator), M_REGULATOR,
797 	    M_WAITOK | M_ZERO);
798 	reg->cdev = cdev;
799 	reg->regnode = regnode;
800 	reg->enable_cnt = 0;
801 
802 	REGNODE_XLOCK(regnode);
803 	regnode->ref_cnt++;
804 	TAILQ_INSERT_TAIL(&regnode->consumers_list, reg, link);
805 	reg ->min_uvolt = regnode->std_param.min_uvolt;
806 	reg ->max_uvolt = regnode->std_param.max_uvolt;
807 	REGNODE_UNLOCK(regnode);
808 
809 	return (reg);
810 }
811 
812 int
regulator_enable(regulator_t reg)813 regulator_enable(regulator_t reg)
814 {
815 	int rv;
816 	struct regnode *regnode;
817 
818 	regnode = reg->regnode;
819 	KASSERT(regnode->ref_cnt > 0,
820 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
821 	REG_TOPO_SLOCK();
822 	rv = regnode_enable(regnode);
823 	if (rv == 0)
824 		reg->enable_cnt++;
825 	REG_TOPO_UNLOCK();
826 	return (rv);
827 }
828 
829 int
regulator_disable(regulator_t reg)830 regulator_disable(regulator_t reg)
831 {
832 	int rv;
833 	struct regnode *regnode;
834 
835 	regnode = reg->regnode;
836 	KASSERT(regnode->ref_cnt > 0,
837 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
838 	KASSERT(reg->enable_cnt > 0,
839 	   ("Attempt to disable already disabled regulator: %s\n",
840 	   regnode->name));
841 	REG_TOPO_SLOCK();
842 	rv = regnode_disable(regnode);
843 	if (rv == 0)
844 		reg->enable_cnt--;
845 	REG_TOPO_UNLOCK();
846 	return (rv);
847 }
848 
849 int
regulator_stop(regulator_t reg)850 regulator_stop(regulator_t reg)
851 {
852 	int rv;
853 	struct regnode *regnode;
854 
855 	regnode = reg->regnode;
856 	KASSERT(regnode->ref_cnt > 0,
857 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
858 	KASSERT(reg->enable_cnt == 0,
859 	   ("Attempt to stop already enabled regulator: %s\n", regnode->name));
860 
861 	REG_TOPO_SLOCK();
862 	rv = regnode_stop(regnode, 0);
863 	REG_TOPO_UNLOCK();
864 	return (rv);
865 }
866 
867 int
regulator_status(regulator_t reg,int * status)868 regulator_status(regulator_t reg, int *status)
869 {
870 	int rv;
871 	struct regnode *regnode;
872 
873 	regnode = reg->regnode;
874 	KASSERT(regnode->ref_cnt > 0,
875 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
876 
877 	REG_TOPO_SLOCK();
878 	rv = regnode_status(regnode, status);
879 	REG_TOPO_UNLOCK();
880 	return (rv);
881 }
882 
883 int
regulator_get_voltage(regulator_t reg,int * uvolt)884 regulator_get_voltage(regulator_t reg, int *uvolt)
885 {
886 	int rv;
887 	struct regnode *regnode;
888 
889 	regnode = reg->regnode;
890 	KASSERT(regnode->ref_cnt > 0,
891 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
892 
893 	REG_TOPO_SLOCK();
894 	rv = regnode_get_voltage(regnode, uvolt);
895 	REG_TOPO_UNLOCK();
896 	return (rv);
897 }
898 
899 int
regulator_set_voltage(regulator_t reg,int min_uvolt,int max_uvolt)900 regulator_set_voltage(regulator_t reg, int min_uvolt, int max_uvolt)
901 {
902 	struct regnode *regnode;
903 	int rv;
904 
905 	regnode = reg->regnode;
906 	KASSERT(regnode->ref_cnt > 0,
907 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
908 
909 	REG_TOPO_SLOCK();
910 
911 	rv = regnode_set_voltage_checked(regnode, reg, min_uvolt, max_uvolt);
912 	if (rv == 0) {
913 		reg->min_uvolt = min_uvolt;
914 		reg->max_uvolt = max_uvolt;
915 	}
916 	REG_TOPO_UNLOCK();
917 	return (rv);
918 }
919 
920 const char *
regulator_get_name(regulator_t reg)921 regulator_get_name(regulator_t reg)
922 {
923 	struct regnode *regnode;
924 
925 	regnode = reg->regnode;
926 	KASSERT(regnode->ref_cnt > 0,
927 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
928 	return (regnode->name);
929 }
930 
931 int
regulator_get_by_name(device_t cdev,const char * name,regulator_t * reg)932 regulator_get_by_name(device_t cdev, const char *name, regulator_t *reg)
933 {
934 	struct regnode *regnode;
935 
936 	REG_TOPO_SLOCK();
937 	regnode = regnode_find_by_name(name);
938 	if (regnode == NULL) {
939 		REG_TOPO_UNLOCK();
940 		return (ENODEV);
941 	}
942 	*reg = regulator_create(regnode, cdev);
943 	REG_TOPO_UNLOCK();
944 	return (0);
945 }
946 
947 int
regulator_get_by_id(device_t cdev,device_t pdev,intptr_t id,regulator_t * reg)948 regulator_get_by_id(device_t cdev, device_t pdev, intptr_t id, regulator_t *reg)
949 {
950 	struct regnode *regnode;
951 
952 	REG_TOPO_SLOCK();
953 
954 	regnode = regnode_find_by_id(pdev, id);
955 	if (regnode == NULL) {
956 		REG_TOPO_UNLOCK();
957 		return (ENODEV);
958 	}
959 	*reg = regulator_create(regnode, cdev);
960 	REG_TOPO_UNLOCK();
961 
962 	return (0);
963 }
964 
965 int
regulator_release(regulator_t reg)966 regulator_release(regulator_t reg)
967 {
968 	struct regnode *regnode;
969 
970 	regnode = reg->regnode;
971 	KASSERT(regnode->ref_cnt > 0,
972 	   ("Attempt to access unreferenced regulator: %s\n", regnode->name));
973 	REG_TOPO_SLOCK();
974 	while (reg->enable_cnt > 0) {
975 		regnode_disable(regnode);
976 		reg->enable_cnt--;
977 	}
978 	REGNODE_XLOCK(regnode);
979 	TAILQ_REMOVE(&regnode->consumers_list, reg, link);
980 	regnode->ref_cnt--;
981 	REGNODE_UNLOCK(regnode);
982 	REG_TOPO_UNLOCK();
983 
984 	free(reg, M_REGULATOR);
985 	return (0);
986 }
987 
988 #ifdef FDT
989 /* Default DT mapper. */
990 int
regdev_default_ofw_map(device_t dev,phandle_t xref,int ncells,pcell_t * cells,intptr_t * id)991 regdev_default_ofw_map(device_t dev, phandle_t 	xref, int ncells,
992     pcell_t *cells, intptr_t *id)
993 {
994 	if (ncells == 0)
995 		*id = 1;
996 	else if (ncells == 1)
997 		*id = cells[0];
998 	else
999 		return  (ERANGE);
1000 
1001 	return (0);
1002 }
1003 
1004 int
regulator_parse_ofw_stdparam(device_t pdev,phandle_t node,struct regnode_init_def * def)1005 regulator_parse_ofw_stdparam(device_t pdev, phandle_t node,
1006     struct regnode_init_def *def)
1007 {
1008 	phandle_t supply_xref;
1009 	struct regnode_std_param *par;
1010 	int rv;
1011 
1012 	par = &def->std_param;
1013 	rv = OF_getprop_alloc(node, "regulator-name",
1014 	    (void **)&def->name);
1015 	if (rv <= 0) {
1016 		device_printf(pdev, "%s: Missing regulator name\n",
1017 		 __func__);
1018 		return (ENXIO);
1019 	}
1020 
1021 	rv = OF_getencprop(node, "regulator-min-microvolt", &par->min_uvolt,
1022 	    sizeof(par->min_uvolt));
1023 	if (rv <= 0)
1024 		par->min_uvolt = 0;
1025 
1026 	rv = OF_getencprop(node, "regulator-max-microvolt", &par->max_uvolt,
1027 	    sizeof(par->max_uvolt));
1028 	if (rv <= 0)
1029 		par->max_uvolt = 0;
1030 
1031 	rv = OF_getencprop(node, "regulator-min-microamp", &par->min_uamp,
1032 	    sizeof(par->min_uamp));
1033 	if (rv <= 0)
1034 		par->min_uamp = 0;
1035 
1036 	rv = OF_getencprop(node, "regulator-max-microamp", &par->max_uamp,
1037 	    sizeof(par->max_uamp));
1038 	if (rv <= 0)
1039 		par->max_uamp = 0;
1040 
1041 	rv = OF_getencprop(node, "regulator-ramp-delay", &par->ramp_delay,
1042 	    sizeof(par->ramp_delay));
1043 	if (rv <= 0)
1044 		par->ramp_delay = 0;
1045 
1046 	rv = OF_getencprop(node, "regulator-enable-ramp-delay",
1047 	    &par->enable_delay, sizeof(par->enable_delay));
1048 	if (rv <= 0)
1049 		par->enable_delay = 0;
1050 
1051 	if (OF_hasprop(node, "regulator-boot-on"))
1052 		par->boot_on = true;
1053 
1054 	if (OF_hasprop(node, "regulator-always-on"))
1055 		par->always_on = true;
1056 
1057 	if (OF_hasprop(node, "enable-active-high"))
1058 		par->enable_active_high = 1;
1059 
1060 	rv = OF_getencprop(node, "vin-supply", &supply_xref,
1061 	    sizeof(supply_xref));
1062 	if (rv >=  0) {
1063 		rv = OF_getprop_alloc(supply_xref, "regulator-name",
1064 		    (void **)&def->parent_name);
1065 		if (rv <= 0)
1066 			def->parent_name = NULL;
1067 	}
1068 	return (0);
1069 }
1070 
1071 int
regulator_get_by_ofw_property(device_t cdev,phandle_t cnode,char * name,regulator_t * reg)1072 regulator_get_by_ofw_property(device_t cdev, phandle_t cnode, char *name,
1073     regulator_t *reg)
1074 {
1075 	phandle_t *cells;
1076 	device_t regdev;
1077 	int ncells, rv;
1078 	intptr_t id;
1079 
1080 	*reg = NULL;
1081 
1082 	if (cnode <= 0)
1083 		cnode = ofw_bus_get_node(cdev);
1084 	if (cnode <= 0) {
1085 		device_printf(cdev, "%s called on not ofw based device\n",
1086 		 __func__);
1087 		return (ENXIO);
1088 	}
1089 
1090 	cells = NULL;
1091 	ncells = OF_getencprop_alloc_multi(cnode, name, sizeof(*cells),
1092 	    (void **)&cells);
1093 	if (ncells <= 0)
1094 		return (ENOENT);
1095 
1096 	/* Translate xref to device */
1097 	regdev = OF_device_from_xref(cells[0]);
1098 	if (regdev == NULL) {
1099 		OF_prop_free(cells);
1100 		return (ENODEV);
1101 	}
1102 
1103 	/* Map regulator to number */
1104 	rv = REGDEV_MAP(regdev, cells[0], ncells - 1, cells + 1, &id);
1105 	OF_prop_free(cells);
1106 	if (rv != 0)
1107 		return (rv);
1108 	return (regulator_get_by_id(cdev, regdev, id, reg));
1109 }
1110 #endif
1111 
1112 /* --------------------------------------------------------------------------
1113  *
1114  * Regulator utility functions.
1115  *
1116  */
1117 
1118 /* Convert raw selector value to real voltage */
1119 int
regulator_range_sel8_to_volt(struct regulator_range * ranges,int nranges,uint8_t sel,int * volt)1120 regulator_range_sel8_to_volt(struct regulator_range *ranges, int nranges,
1121    uint8_t sel, int *volt)
1122 {
1123 	struct regulator_range *range;
1124 	int i;
1125 
1126 	if (nranges == 0)
1127 		panic("Voltage regulator have zero ranges\n");
1128 
1129 	for (i = 0; i < nranges ; i++) {
1130 		range = ranges  + i;
1131 
1132 		if (!(sel >= range->min_sel &&
1133 		      sel <= range->max_sel))
1134 			continue;
1135 
1136 		sel -= range->min_sel;
1137 
1138 		*volt = range->min_uvolt + sel * range->step_uvolt;
1139 		return (0);
1140 	}
1141 
1142 	return (ERANGE);
1143 }
1144 
1145 int
regulator_range_volt_to_sel8(struct regulator_range * ranges,int nranges,int min_uvolt,int max_uvolt,uint8_t * out_sel)1146 regulator_range_volt_to_sel8(struct regulator_range *ranges, int nranges,
1147     int min_uvolt, int max_uvolt, uint8_t *out_sel)
1148 {
1149 	struct regulator_range *range;
1150 	uint8_t sel;
1151 	int uvolt;
1152 	int rv, i;
1153 
1154 	if (nranges == 0)
1155 		panic("Voltage regulator have zero ranges\n");
1156 
1157 	for (i = 0; i < nranges; i++) {
1158 		range = ranges  + i;
1159 		uvolt = range->min_uvolt +
1160 		    (range->max_sel - range->min_sel) * range->step_uvolt;
1161 
1162 		if ((min_uvolt > uvolt) ||
1163 		    (max_uvolt < range->min_uvolt))
1164 			continue;
1165 
1166 		if (min_uvolt <= range->min_uvolt)
1167 			min_uvolt = range->min_uvolt;
1168 
1169 		/* if step == 0 -> fixed voltage range. */
1170 		if (range->step_uvolt == 0)
1171 			sel = 0;
1172 		else
1173 			sel = DIV_ROUND_UP(min_uvolt - range->min_uvolt,
1174 			   range->step_uvolt);
1175 
1176 
1177 		sel += range->min_sel;
1178 
1179 		break;
1180 	}
1181 
1182 	if (i >= nranges)
1183 		return (ERANGE);
1184 
1185 	/* Verify new settings. */
1186 	rv = regulator_range_sel8_to_volt(ranges, nranges, sel, &uvolt);
1187 	if (rv != 0)
1188 		return (rv);
1189 	if ((uvolt < min_uvolt) || (uvolt > max_uvolt))
1190 		return (ERANGE);
1191 
1192 	*out_sel = sel;
1193 	return (0);
1194 }
1195