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