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