xref: /OK3568_Linux_fs/kernel/drivers/regulator/core.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // core.c  --  Voltage/Current Regulator framework.
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6*4882a593Smuzhiyun // Copyright 2008 SlimLogic Ltd.
7*4882a593Smuzhiyun //
8*4882a593Smuzhiyun // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/init.h>
12*4882a593Smuzhiyun #include <linux/debugfs.h>
13*4882a593Smuzhiyun #include <linux/device.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/async.h>
16*4882a593Smuzhiyun #include <linux/err.h>
17*4882a593Smuzhiyun #include <linux/mutex.h>
18*4882a593Smuzhiyun #include <linux/suspend.h>
19*4882a593Smuzhiyun #include <linux/delay.h>
20*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
21*4882a593Smuzhiyun #include <linux/of.h>
22*4882a593Smuzhiyun #include <linux/regmap.h>
23*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
24*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
25*4882a593Smuzhiyun #include <linux/regulator/coupler.h>
26*4882a593Smuzhiyun #include <linux/regulator/driver.h>
27*4882a593Smuzhiyun #include <linux/regulator/machine.h>
28*4882a593Smuzhiyun #include <linux/module.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
31*4882a593Smuzhiyun #include <trace/events/regulator.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include "dummy.h"
34*4882a593Smuzhiyun #include "internal.h"
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define rdev_crit(rdev, fmt, ...)					\
37*4882a593Smuzhiyun 	pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38*4882a593Smuzhiyun #define rdev_err(rdev, fmt, ...)					\
39*4882a593Smuzhiyun 	pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40*4882a593Smuzhiyun #define rdev_warn(rdev, fmt, ...)					\
41*4882a593Smuzhiyun 	pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42*4882a593Smuzhiyun #define rdev_info(rdev, fmt, ...)					\
43*4882a593Smuzhiyun 	pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44*4882a593Smuzhiyun #define rdev_dbg(rdev, fmt, ...)					\
45*4882a593Smuzhiyun 	pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static DEFINE_WW_CLASS(regulator_ww_class);
48*4882a593Smuzhiyun static DEFINE_MUTEX(regulator_nesting_mutex);
49*4882a593Smuzhiyun static DEFINE_MUTEX(regulator_list_mutex);
50*4882a593Smuzhiyun static LIST_HEAD(regulator_map_list);
51*4882a593Smuzhiyun static LIST_HEAD(regulator_ena_gpio_list);
52*4882a593Smuzhiyun static LIST_HEAD(regulator_supply_alias_list);
53*4882a593Smuzhiyun static LIST_HEAD(regulator_coupler_list);
54*4882a593Smuzhiyun static LIST_HEAD(regulator_debug_list);
55*4882a593Smuzhiyun static bool has_full_constraints;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun static struct dentry *debugfs_root;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  * struct regulator_map
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Used to provide symbolic supply names to devices.
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun struct regulator_map {
65*4882a593Smuzhiyun 	struct list_head list;
66*4882a593Smuzhiyun 	const char *dev_name;   /* The dev_name() for the consumer */
67*4882a593Smuzhiyun 	const char *supply;
68*4882a593Smuzhiyun 	struct regulator_dev *regulator;
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun  * struct regulator_enable_gpio
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * Management for shared enable GPIO pin
75*4882a593Smuzhiyun  */
76*4882a593Smuzhiyun struct regulator_enable_gpio {
77*4882a593Smuzhiyun 	struct list_head list;
78*4882a593Smuzhiyun 	struct gpio_desc *gpiod;
79*4882a593Smuzhiyun 	u32 enable_count;	/* a number of enabled shared GPIO */
80*4882a593Smuzhiyun 	u32 request_count;	/* a number of requested shared GPIO */
81*4882a593Smuzhiyun };
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /*
84*4882a593Smuzhiyun  * struct regulator_supply_alias
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  * Used to map lookups for a supply onto an alternative device.
87*4882a593Smuzhiyun  */
88*4882a593Smuzhiyun struct regulator_supply_alias {
89*4882a593Smuzhiyun 	struct list_head list;
90*4882a593Smuzhiyun 	struct device *src_dev;
91*4882a593Smuzhiyun 	const char *src_supply;
92*4882a593Smuzhiyun 	struct device *alias_dev;
93*4882a593Smuzhiyun 	const char *alias_supply;
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun struct regulator_limit_volt {
97*4882a593Smuzhiyun 	struct list_head list;
98*4882a593Smuzhiyun 	struct regulator *reg;
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun static int _regulator_is_enabled(struct regulator_dev *rdev);
102*4882a593Smuzhiyun static int _regulator_disable(struct regulator *regulator);
103*4882a593Smuzhiyun static int _regulator_get_current_limit(struct regulator_dev *rdev);
104*4882a593Smuzhiyun static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
105*4882a593Smuzhiyun static int _notifier_call_chain(struct regulator_dev *rdev,
106*4882a593Smuzhiyun 				  unsigned long event, void *data);
107*4882a593Smuzhiyun static int _regulator_do_set_voltage(struct regulator_dev *rdev,
108*4882a593Smuzhiyun 				     int min_uV, int max_uV);
109*4882a593Smuzhiyun static int regulator_balance_voltage(struct regulator_dev *rdev,
110*4882a593Smuzhiyun 				     suspend_state_t state);
111*4882a593Smuzhiyun static struct regulator *create_regulator(struct regulator_dev *rdev,
112*4882a593Smuzhiyun 					  struct device *dev,
113*4882a593Smuzhiyun 					  const char *supply_name);
114*4882a593Smuzhiyun static void destroy_regulator(struct regulator *regulator);
115*4882a593Smuzhiyun static void _regulator_put(struct regulator *regulator);
116*4882a593Smuzhiyun 
rdev_get_name(struct regulator_dev * rdev)117*4882a593Smuzhiyun const char *rdev_get_name(struct regulator_dev *rdev)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	if (rdev->constraints && rdev->constraints->name)
120*4882a593Smuzhiyun 		return rdev->constraints->name;
121*4882a593Smuzhiyun 	else if (rdev->desc->name)
122*4882a593Smuzhiyun 		return rdev->desc->name;
123*4882a593Smuzhiyun 	else
124*4882a593Smuzhiyun 		return "";
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
have_full_constraints(void)127*4882a593Smuzhiyun static bool have_full_constraints(void)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	return has_full_constraints || of_have_populated_dt();
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
regulator_ops_is_valid(struct regulator_dev * rdev,int ops)132*4882a593Smuzhiyun static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	if (!rdev->constraints) {
135*4882a593Smuzhiyun 		rdev_err(rdev, "no constraints\n");
136*4882a593Smuzhiyun 		return false;
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	if (rdev->constraints->valid_ops_mask & ops)
140*4882a593Smuzhiyun 		return true;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return false;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun  * regulator_lock_nested - lock a single regulator
147*4882a593Smuzhiyun  * @rdev:		regulator source
148*4882a593Smuzhiyun  * @ww_ctx:		w/w mutex acquire context
149*4882a593Smuzhiyun  *
150*4882a593Smuzhiyun  * This function can be called many times by one task on
151*4882a593Smuzhiyun  * a single regulator and its mutex will be locked only
152*4882a593Smuzhiyun  * once. If a task, which is calling this function is other
153*4882a593Smuzhiyun  * than the one, which initially locked the mutex, it will
154*4882a593Smuzhiyun  * wait on mutex.
155*4882a593Smuzhiyun  */
regulator_lock_nested(struct regulator_dev * rdev,struct ww_acquire_ctx * ww_ctx)156*4882a593Smuzhiyun static inline int regulator_lock_nested(struct regulator_dev *rdev,
157*4882a593Smuzhiyun 					struct ww_acquire_ctx *ww_ctx)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	bool lock = false;
160*4882a593Smuzhiyun 	int ret = 0;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	mutex_lock(&regulator_nesting_mutex);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
165*4882a593Smuzhiyun 		if (rdev->mutex_owner == current)
166*4882a593Smuzhiyun 			rdev->ref_cnt++;
167*4882a593Smuzhiyun 		else
168*4882a593Smuzhiyun 			lock = true;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 		if (lock) {
171*4882a593Smuzhiyun 			mutex_unlock(&regulator_nesting_mutex);
172*4882a593Smuzhiyun 			ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
173*4882a593Smuzhiyun 			mutex_lock(&regulator_nesting_mutex);
174*4882a593Smuzhiyun 		}
175*4882a593Smuzhiyun 	} else {
176*4882a593Smuzhiyun 		lock = true;
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	if (lock && ret != -EDEADLK) {
180*4882a593Smuzhiyun 		rdev->ref_cnt++;
181*4882a593Smuzhiyun 		rdev->mutex_owner = current;
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	mutex_unlock(&regulator_nesting_mutex);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	return ret;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /**
190*4882a593Smuzhiyun  * regulator_lock - lock a single regulator
191*4882a593Smuzhiyun  * @rdev:		regulator source
192*4882a593Smuzhiyun  *
193*4882a593Smuzhiyun  * This function can be called many times by one task on
194*4882a593Smuzhiyun  * a single regulator and its mutex will be locked only
195*4882a593Smuzhiyun  * once. If a task, which is calling this function is other
196*4882a593Smuzhiyun  * than the one, which initially locked the mutex, it will
197*4882a593Smuzhiyun  * wait on mutex.
198*4882a593Smuzhiyun  */
regulator_lock(struct regulator_dev * rdev)199*4882a593Smuzhiyun static void regulator_lock(struct regulator_dev *rdev)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	regulator_lock_nested(rdev, NULL);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun /**
205*4882a593Smuzhiyun  * regulator_unlock - unlock a single regulator
206*4882a593Smuzhiyun  * @rdev:		regulator_source
207*4882a593Smuzhiyun  *
208*4882a593Smuzhiyun  * This function unlocks the mutex when the
209*4882a593Smuzhiyun  * reference counter reaches 0.
210*4882a593Smuzhiyun  */
regulator_unlock(struct regulator_dev * rdev)211*4882a593Smuzhiyun static void regulator_unlock(struct regulator_dev *rdev)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	mutex_lock(&regulator_nesting_mutex);
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	if (--rdev->ref_cnt == 0) {
216*4882a593Smuzhiyun 		rdev->mutex_owner = NULL;
217*4882a593Smuzhiyun 		ww_mutex_unlock(&rdev->mutex);
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	WARN_ON_ONCE(rdev->ref_cnt < 0);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	mutex_unlock(&regulator_nesting_mutex);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
regulator_supply_is_couple(struct regulator_dev * rdev)225*4882a593Smuzhiyun static bool regulator_supply_is_couple(struct regulator_dev *rdev)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	struct regulator_dev *c_rdev;
228*4882a593Smuzhiyun 	int i;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
231*4882a593Smuzhiyun 		c_rdev = rdev->coupling_desc.coupled_rdevs[i];
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 		if (rdev->supply->rdev == c_rdev)
234*4882a593Smuzhiyun 			return true;
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	return false;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
regulator_unlock_recursive(struct regulator_dev * rdev,unsigned int n_coupled)240*4882a593Smuzhiyun static void regulator_unlock_recursive(struct regulator_dev *rdev,
241*4882a593Smuzhiyun 				       unsigned int n_coupled)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	struct regulator_dev *c_rdev, *supply_rdev;
244*4882a593Smuzhiyun 	int i, supply_n_coupled;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	for (i = n_coupled; i > 0; i--) {
247*4882a593Smuzhiyun 		c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 		if (!c_rdev)
250*4882a593Smuzhiyun 			continue;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 		if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
253*4882a593Smuzhiyun 			supply_rdev = c_rdev->supply->rdev;
254*4882a593Smuzhiyun 			supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 			regulator_unlock_recursive(supply_rdev,
257*4882a593Smuzhiyun 						   supply_n_coupled);
258*4882a593Smuzhiyun 		}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 		regulator_unlock(c_rdev);
261*4882a593Smuzhiyun 	}
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
regulator_lock_recursive(struct regulator_dev * rdev,struct regulator_dev ** new_contended_rdev,struct regulator_dev ** old_contended_rdev,struct ww_acquire_ctx * ww_ctx)264*4882a593Smuzhiyun static int regulator_lock_recursive(struct regulator_dev *rdev,
265*4882a593Smuzhiyun 				    struct regulator_dev **new_contended_rdev,
266*4882a593Smuzhiyun 				    struct regulator_dev **old_contended_rdev,
267*4882a593Smuzhiyun 				    struct ww_acquire_ctx *ww_ctx)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun 	struct regulator_dev *c_rdev;
270*4882a593Smuzhiyun 	int i, err;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
273*4882a593Smuzhiyun 		c_rdev = rdev->coupling_desc.coupled_rdevs[i];
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 		if (!c_rdev)
276*4882a593Smuzhiyun 			continue;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 		if (c_rdev != *old_contended_rdev) {
279*4882a593Smuzhiyun 			err = regulator_lock_nested(c_rdev, ww_ctx);
280*4882a593Smuzhiyun 			if (err) {
281*4882a593Smuzhiyun 				if (err == -EDEADLK) {
282*4882a593Smuzhiyun 					*new_contended_rdev = c_rdev;
283*4882a593Smuzhiyun 					goto err_unlock;
284*4882a593Smuzhiyun 				}
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 				/* shouldn't happen */
287*4882a593Smuzhiyun 				WARN_ON_ONCE(err != -EALREADY);
288*4882a593Smuzhiyun 			}
289*4882a593Smuzhiyun 		} else {
290*4882a593Smuzhiyun 			*old_contended_rdev = NULL;
291*4882a593Smuzhiyun 		}
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 		if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
294*4882a593Smuzhiyun 			err = regulator_lock_recursive(c_rdev->supply->rdev,
295*4882a593Smuzhiyun 						       new_contended_rdev,
296*4882a593Smuzhiyun 						       old_contended_rdev,
297*4882a593Smuzhiyun 						       ww_ctx);
298*4882a593Smuzhiyun 			if (err) {
299*4882a593Smuzhiyun 				regulator_unlock(c_rdev);
300*4882a593Smuzhiyun 				goto err_unlock;
301*4882a593Smuzhiyun 			}
302*4882a593Smuzhiyun 		}
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	return 0;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun err_unlock:
308*4882a593Smuzhiyun 	regulator_unlock_recursive(rdev, i);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	return err;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun /**
314*4882a593Smuzhiyun  * regulator_unlock_dependent - unlock regulator's suppliers and coupled
315*4882a593Smuzhiyun  *				regulators
316*4882a593Smuzhiyun  * @rdev:			regulator source
317*4882a593Smuzhiyun  * @ww_ctx:			w/w mutex acquire context
318*4882a593Smuzhiyun  *
319*4882a593Smuzhiyun  * Unlock all regulators related with rdev by coupling or supplying.
320*4882a593Smuzhiyun  */
regulator_unlock_dependent(struct regulator_dev * rdev,struct ww_acquire_ctx * ww_ctx)321*4882a593Smuzhiyun static void regulator_unlock_dependent(struct regulator_dev *rdev,
322*4882a593Smuzhiyun 				       struct ww_acquire_ctx *ww_ctx)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
325*4882a593Smuzhiyun 	ww_acquire_fini(ww_ctx);
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun /**
329*4882a593Smuzhiyun  * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
330*4882a593Smuzhiyun  * @rdev:			regulator source
331*4882a593Smuzhiyun  * @ww_ctx:			w/w mutex acquire context
332*4882a593Smuzhiyun  *
333*4882a593Smuzhiyun  * This function as a wrapper on regulator_lock_recursive(), which locks
334*4882a593Smuzhiyun  * all regulators related with rdev by coupling or supplying.
335*4882a593Smuzhiyun  */
regulator_lock_dependent(struct regulator_dev * rdev,struct ww_acquire_ctx * ww_ctx)336*4882a593Smuzhiyun static void regulator_lock_dependent(struct regulator_dev *rdev,
337*4882a593Smuzhiyun 				     struct ww_acquire_ctx *ww_ctx)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	struct regulator_dev *new_contended_rdev = NULL;
340*4882a593Smuzhiyun 	struct regulator_dev *old_contended_rdev = NULL;
341*4882a593Smuzhiyun 	int err;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	ww_acquire_init(ww_ctx, &regulator_ww_class);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	do {
348*4882a593Smuzhiyun 		if (new_contended_rdev) {
349*4882a593Smuzhiyun 			ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
350*4882a593Smuzhiyun 			old_contended_rdev = new_contended_rdev;
351*4882a593Smuzhiyun 			old_contended_rdev->ref_cnt++;
352*4882a593Smuzhiyun 		}
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 		err = regulator_lock_recursive(rdev,
355*4882a593Smuzhiyun 					       &new_contended_rdev,
356*4882a593Smuzhiyun 					       &old_contended_rdev,
357*4882a593Smuzhiyun 					       ww_ctx);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 		if (old_contended_rdev)
360*4882a593Smuzhiyun 			regulator_unlock(old_contended_rdev);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	} while (err == -EDEADLK);
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	ww_acquire_done(ww_ctx);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun /**
370*4882a593Smuzhiyun  * of_get_child_regulator - get a child regulator device node
371*4882a593Smuzhiyun  * based on supply name
372*4882a593Smuzhiyun  * @parent: Parent device node
373*4882a593Smuzhiyun  * @prop_name: Combination regulator supply name and "-supply"
374*4882a593Smuzhiyun  *
375*4882a593Smuzhiyun  * Traverse all child nodes.
376*4882a593Smuzhiyun  * Extract the child regulator device node corresponding to the supply name.
377*4882a593Smuzhiyun  * returns the device node corresponding to the regulator if found, else
378*4882a593Smuzhiyun  * returns NULL.
379*4882a593Smuzhiyun  */
of_get_child_regulator(struct device_node * parent,const char * prop_name)380*4882a593Smuzhiyun static struct device_node *of_get_child_regulator(struct device_node *parent,
381*4882a593Smuzhiyun 						  const char *prop_name)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun 	struct device_node *regnode = NULL;
384*4882a593Smuzhiyun 	struct device_node *child = NULL;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	for_each_child_of_node(parent, child) {
387*4882a593Smuzhiyun 		regnode = of_parse_phandle(child, prop_name, 0);
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 		if (!regnode) {
390*4882a593Smuzhiyun 			regnode = of_get_child_regulator(child, prop_name);
391*4882a593Smuzhiyun 			if (regnode)
392*4882a593Smuzhiyun 				goto err_node_put;
393*4882a593Smuzhiyun 		} else {
394*4882a593Smuzhiyun 			goto err_node_put;
395*4882a593Smuzhiyun 		}
396*4882a593Smuzhiyun 	}
397*4882a593Smuzhiyun 	return NULL;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun err_node_put:
400*4882a593Smuzhiyun 	of_node_put(child);
401*4882a593Smuzhiyun 	return regnode;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun /**
405*4882a593Smuzhiyun  * of_get_regulator - get a regulator device node based on supply name
406*4882a593Smuzhiyun  * @dev: Device pointer for the consumer (of regulator) device
407*4882a593Smuzhiyun  * @supply: regulator supply name
408*4882a593Smuzhiyun  *
409*4882a593Smuzhiyun  * Extract the regulator device node corresponding to the supply name.
410*4882a593Smuzhiyun  * returns the device node corresponding to the regulator if found, else
411*4882a593Smuzhiyun  * returns NULL.
412*4882a593Smuzhiyun  */
of_get_regulator(struct device * dev,const char * supply)413*4882a593Smuzhiyun static struct device_node *of_get_regulator(struct device *dev, const char *supply)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	struct device_node *regnode = NULL;
416*4882a593Smuzhiyun 	char prop_name[64]; /* 64 is max size of property name */
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	snprintf(prop_name, 64, "%s-supply", supply);
421*4882a593Smuzhiyun 	regnode = of_parse_phandle(dev->of_node, prop_name, 0);
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (!regnode) {
424*4882a593Smuzhiyun 		regnode = of_get_child_regulator(dev->of_node, prop_name);
425*4882a593Smuzhiyun 		if (regnode)
426*4882a593Smuzhiyun 			return regnode;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 		dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
429*4882a593Smuzhiyun 				prop_name, dev->of_node);
430*4882a593Smuzhiyun 		return NULL;
431*4882a593Smuzhiyun 	}
432*4882a593Smuzhiyun 	return regnode;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun /* Platform voltage constraint check */
regulator_check_voltage(struct regulator_dev * rdev,int * min_uV,int * max_uV)436*4882a593Smuzhiyun int regulator_check_voltage(struct regulator_dev *rdev,
437*4882a593Smuzhiyun 			    int *min_uV, int *max_uV)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun 	BUG_ON(*min_uV > *max_uV);
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
442*4882a593Smuzhiyun 		rdev_err(rdev, "voltage operation not allowed\n");
443*4882a593Smuzhiyun 		return -EPERM;
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	if (*max_uV > rdev->constraints->max_uV)
447*4882a593Smuzhiyun 		*max_uV = rdev->constraints->max_uV;
448*4882a593Smuzhiyun 	if (*min_uV < rdev->constraints->min_uV)
449*4882a593Smuzhiyun 		*min_uV = rdev->constraints->min_uV;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	if (*min_uV > *max_uV) {
452*4882a593Smuzhiyun 		rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
453*4882a593Smuzhiyun 			 *min_uV, *max_uV);
454*4882a593Smuzhiyun 		return -EINVAL;
455*4882a593Smuzhiyun 	}
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	return 0;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun /* return 0 if the state is valid */
regulator_check_states(suspend_state_t state)461*4882a593Smuzhiyun static int regulator_check_states(suspend_state_t state)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun 	return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun /* Make sure we select a voltage that suits the needs of all
467*4882a593Smuzhiyun  * regulator consumers
468*4882a593Smuzhiyun  */
regulator_check_consumers(struct regulator_dev * rdev,int * min_uV,int * max_uV,suspend_state_t state)469*4882a593Smuzhiyun int regulator_check_consumers(struct regulator_dev *rdev,
470*4882a593Smuzhiyun 			      int *min_uV, int *max_uV,
471*4882a593Smuzhiyun 			      suspend_state_t state)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	struct regulator *regulator;
474*4882a593Smuzhiyun 	struct regulator_voltage *voltage;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	list_for_each_entry(regulator, &rdev->consumer_list, list) {
477*4882a593Smuzhiyun 		voltage = &regulator->voltage[state];
478*4882a593Smuzhiyun 		/*
479*4882a593Smuzhiyun 		 * Assume consumers that didn't say anything are OK
480*4882a593Smuzhiyun 		 * with anything in the constraint range.
481*4882a593Smuzhiyun 		 */
482*4882a593Smuzhiyun 		if (!voltage->min_uV && !voltage->max_uV)
483*4882a593Smuzhiyun 			continue;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 		if (*max_uV > voltage->max_uV)
486*4882a593Smuzhiyun 			*max_uV = voltage->max_uV;
487*4882a593Smuzhiyun 		if (*min_uV < voltage->min_uV)
488*4882a593Smuzhiyun 			*min_uV = voltage->min_uV;
489*4882a593Smuzhiyun 	}
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	if (*min_uV > *max_uV) {
492*4882a593Smuzhiyun 		rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
493*4882a593Smuzhiyun 			*min_uV, *max_uV);
494*4882a593Smuzhiyun 		return -EINVAL;
495*4882a593Smuzhiyun 	}
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	return 0;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun /* current constraint check */
regulator_check_current_limit(struct regulator_dev * rdev,int * min_uA,int * max_uA)501*4882a593Smuzhiyun static int regulator_check_current_limit(struct regulator_dev *rdev,
502*4882a593Smuzhiyun 					int *min_uA, int *max_uA)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	BUG_ON(*min_uA > *max_uA);
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
507*4882a593Smuzhiyun 		rdev_err(rdev, "current operation not allowed\n");
508*4882a593Smuzhiyun 		return -EPERM;
509*4882a593Smuzhiyun 	}
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	if (*max_uA > rdev->constraints->max_uA)
512*4882a593Smuzhiyun 		*max_uA = rdev->constraints->max_uA;
513*4882a593Smuzhiyun 	if (*min_uA < rdev->constraints->min_uA)
514*4882a593Smuzhiyun 		*min_uA = rdev->constraints->min_uA;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	if (*min_uA > *max_uA) {
517*4882a593Smuzhiyun 		rdev_err(rdev, "unsupportable current range: %d-%duA\n",
518*4882a593Smuzhiyun 			 *min_uA, *max_uA);
519*4882a593Smuzhiyun 		return -EINVAL;
520*4882a593Smuzhiyun 	}
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	return 0;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun /* operating mode constraint check */
regulator_mode_constrain(struct regulator_dev * rdev,unsigned int * mode)526*4882a593Smuzhiyun static int regulator_mode_constrain(struct regulator_dev *rdev,
527*4882a593Smuzhiyun 				    unsigned int *mode)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 	switch (*mode) {
530*4882a593Smuzhiyun 	case REGULATOR_MODE_FAST:
531*4882a593Smuzhiyun 	case REGULATOR_MODE_NORMAL:
532*4882a593Smuzhiyun 	case REGULATOR_MODE_IDLE:
533*4882a593Smuzhiyun 	case REGULATOR_MODE_STANDBY:
534*4882a593Smuzhiyun 		break;
535*4882a593Smuzhiyun 	default:
536*4882a593Smuzhiyun 		rdev_err(rdev, "invalid mode %x specified\n", *mode);
537*4882a593Smuzhiyun 		return -EINVAL;
538*4882a593Smuzhiyun 	}
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
541*4882a593Smuzhiyun 		rdev_err(rdev, "mode operation not allowed\n");
542*4882a593Smuzhiyun 		return -EPERM;
543*4882a593Smuzhiyun 	}
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	/* The modes are bitmasks, the most power hungry modes having
546*4882a593Smuzhiyun 	 * the lowest values. If the requested mode isn't supported
547*4882a593Smuzhiyun 	 * try higher modes. */
548*4882a593Smuzhiyun 	while (*mode) {
549*4882a593Smuzhiyun 		if (rdev->constraints->valid_modes_mask & *mode)
550*4882a593Smuzhiyun 			return 0;
551*4882a593Smuzhiyun 		*mode /= 2;
552*4882a593Smuzhiyun 	}
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	return -EINVAL;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun static inline struct regulator_state *
regulator_get_suspend_state(struct regulator_dev * rdev,suspend_state_t state)558*4882a593Smuzhiyun regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	if (rdev->constraints == NULL)
561*4882a593Smuzhiyun 		return NULL;
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	switch (state) {
564*4882a593Smuzhiyun 	case PM_SUSPEND_STANDBY:
565*4882a593Smuzhiyun 		return &rdev->constraints->state_standby;
566*4882a593Smuzhiyun 	case PM_SUSPEND_MEM:
567*4882a593Smuzhiyun 		return &rdev->constraints->state_mem;
568*4882a593Smuzhiyun 	case PM_SUSPEND_MAX:
569*4882a593Smuzhiyun 		return &rdev->constraints->state_disk;
570*4882a593Smuzhiyun 	default:
571*4882a593Smuzhiyun 		return NULL;
572*4882a593Smuzhiyun 	}
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun static const struct regulator_state *
regulator_get_suspend_state_check(struct regulator_dev * rdev,suspend_state_t state)576*4882a593Smuzhiyun regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun 	const struct regulator_state *rstate;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	rstate = regulator_get_suspend_state(rdev, state);
581*4882a593Smuzhiyun 	if (rstate == NULL)
582*4882a593Smuzhiyun 		return NULL;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* If we have no suspend mode configuration don't set anything;
585*4882a593Smuzhiyun 	 * only warn if the driver implements set_suspend_voltage or
586*4882a593Smuzhiyun 	 * set_suspend_mode callback.
587*4882a593Smuzhiyun 	 */
588*4882a593Smuzhiyun 	if (rstate->enabled != ENABLE_IN_SUSPEND &&
589*4882a593Smuzhiyun 	    rstate->enabled != DISABLE_IN_SUSPEND) {
590*4882a593Smuzhiyun 		if (rdev->desc->ops->set_suspend_voltage ||
591*4882a593Smuzhiyun 		    rdev->desc->ops->set_suspend_mode)
592*4882a593Smuzhiyun 			rdev_warn(rdev, "No configuration\n");
593*4882a593Smuzhiyun 		return NULL;
594*4882a593Smuzhiyun 	}
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	return rstate;
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun 
regulator_uV_show(struct device * dev,struct device_attribute * attr,char * buf)599*4882a593Smuzhiyun static ssize_t regulator_uV_show(struct device *dev,
600*4882a593Smuzhiyun 				struct device_attribute *attr, char *buf)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
603*4882a593Smuzhiyun 	int uV;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	regulator_lock(rdev);
606*4882a593Smuzhiyun 	uV = regulator_get_voltage_rdev(rdev);
607*4882a593Smuzhiyun 	regulator_unlock(rdev);
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	if (uV < 0)
610*4882a593Smuzhiyun 		return uV;
611*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", uV);
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
614*4882a593Smuzhiyun 
regulator_uA_show(struct device * dev,struct device_attribute * attr,char * buf)615*4882a593Smuzhiyun static ssize_t regulator_uA_show(struct device *dev,
616*4882a593Smuzhiyun 				struct device_attribute *attr, char *buf)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
623*4882a593Smuzhiyun 
name_show(struct device * dev,struct device_attribute * attr,char * buf)624*4882a593Smuzhiyun static ssize_t name_show(struct device *dev, struct device_attribute *attr,
625*4882a593Smuzhiyun 			 char *buf)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", rdev_get_name(rdev));
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun static DEVICE_ATTR_RO(name);
632*4882a593Smuzhiyun 
regulator_opmode_to_str(int mode)633*4882a593Smuzhiyun static const char *regulator_opmode_to_str(int mode)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun 	switch (mode) {
636*4882a593Smuzhiyun 	case REGULATOR_MODE_FAST:
637*4882a593Smuzhiyun 		return "fast";
638*4882a593Smuzhiyun 	case REGULATOR_MODE_NORMAL:
639*4882a593Smuzhiyun 		return "normal";
640*4882a593Smuzhiyun 	case REGULATOR_MODE_IDLE:
641*4882a593Smuzhiyun 		return "idle";
642*4882a593Smuzhiyun 	case REGULATOR_MODE_STANDBY:
643*4882a593Smuzhiyun 		return "standby";
644*4882a593Smuzhiyun 	}
645*4882a593Smuzhiyun 	return "unknown";
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun 
regulator_print_opmode(char * buf,int mode)648*4882a593Smuzhiyun static ssize_t regulator_print_opmode(char *buf, int mode)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun 
regulator_opmode_show(struct device * dev,struct device_attribute * attr,char * buf)653*4882a593Smuzhiyun static ssize_t regulator_opmode_show(struct device *dev,
654*4882a593Smuzhiyun 				    struct device_attribute *attr, char *buf)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	return regulator_print_opmode(buf, _regulator_get_mode(rdev));
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
661*4882a593Smuzhiyun 
regulator_print_state(char * buf,int state)662*4882a593Smuzhiyun static ssize_t regulator_print_state(char *buf, int state)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun 	if (state > 0)
665*4882a593Smuzhiyun 		return sprintf(buf, "enabled\n");
666*4882a593Smuzhiyun 	else if (state == 0)
667*4882a593Smuzhiyun 		return sprintf(buf, "disabled\n");
668*4882a593Smuzhiyun 	else
669*4882a593Smuzhiyun 		return sprintf(buf, "unknown\n");
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun 
regulator_state_show(struct device * dev,struct device_attribute * attr,char * buf)672*4882a593Smuzhiyun static ssize_t regulator_state_show(struct device *dev,
673*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
676*4882a593Smuzhiyun 	ssize_t ret;
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	regulator_lock(rdev);
679*4882a593Smuzhiyun 	ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
680*4882a593Smuzhiyun 	regulator_unlock(rdev);
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	return ret;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
685*4882a593Smuzhiyun 
regulator_status_show(struct device * dev,struct device_attribute * attr,char * buf)686*4882a593Smuzhiyun static ssize_t regulator_status_show(struct device *dev,
687*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
690*4882a593Smuzhiyun 	int status;
691*4882a593Smuzhiyun 	char *label;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	status = rdev->desc->ops->get_status(rdev);
694*4882a593Smuzhiyun 	if (status < 0)
695*4882a593Smuzhiyun 		return status;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	switch (status) {
698*4882a593Smuzhiyun 	case REGULATOR_STATUS_OFF:
699*4882a593Smuzhiyun 		label = "off";
700*4882a593Smuzhiyun 		break;
701*4882a593Smuzhiyun 	case REGULATOR_STATUS_ON:
702*4882a593Smuzhiyun 		label = "on";
703*4882a593Smuzhiyun 		break;
704*4882a593Smuzhiyun 	case REGULATOR_STATUS_ERROR:
705*4882a593Smuzhiyun 		label = "error";
706*4882a593Smuzhiyun 		break;
707*4882a593Smuzhiyun 	case REGULATOR_STATUS_FAST:
708*4882a593Smuzhiyun 		label = "fast";
709*4882a593Smuzhiyun 		break;
710*4882a593Smuzhiyun 	case REGULATOR_STATUS_NORMAL:
711*4882a593Smuzhiyun 		label = "normal";
712*4882a593Smuzhiyun 		break;
713*4882a593Smuzhiyun 	case REGULATOR_STATUS_IDLE:
714*4882a593Smuzhiyun 		label = "idle";
715*4882a593Smuzhiyun 		break;
716*4882a593Smuzhiyun 	case REGULATOR_STATUS_STANDBY:
717*4882a593Smuzhiyun 		label = "standby";
718*4882a593Smuzhiyun 		break;
719*4882a593Smuzhiyun 	case REGULATOR_STATUS_BYPASS:
720*4882a593Smuzhiyun 		label = "bypass";
721*4882a593Smuzhiyun 		break;
722*4882a593Smuzhiyun 	case REGULATOR_STATUS_UNDEFINED:
723*4882a593Smuzhiyun 		label = "undefined";
724*4882a593Smuzhiyun 		break;
725*4882a593Smuzhiyun 	default:
726*4882a593Smuzhiyun 		return -ERANGE;
727*4882a593Smuzhiyun 	}
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", label);
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
732*4882a593Smuzhiyun 
regulator_min_uA_show(struct device * dev,struct device_attribute * attr,char * buf)733*4882a593Smuzhiyun static ssize_t regulator_min_uA_show(struct device *dev,
734*4882a593Smuzhiyun 				    struct device_attribute *attr, char *buf)
735*4882a593Smuzhiyun {
736*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	if (!rdev->constraints)
739*4882a593Smuzhiyun 		return sprintf(buf, "constraint not defined\n");
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", rdev->constraints->min_uA);
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
744*4882a593Smuzhiyun 
regulator_max_uA_show(struct device * dev,struct device_attribute * attr,char * buf)745*4882a593Smuzhiyun static ssize_t regulator_max_uA_show(struct device *dev,
746*4882a593Smuzhiyun 				    struct device_attribute *attr, char *buf)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	if (!rdev->constraints)
751*4882a593Smuzhiyun 		return sprintf(buf, "constraint not defined\n");
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", rdev->constraints->max_uA);
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
756*4882a593Smuzhiyun 
regulator_min_uV_show(struct device * dev,struct device_attribute * attr,char * buf)757*4882a593Smuzhiyun static ssize_t regulator_min_uV_show(struct device *dev,
758*4882a593Smuzhiyun 				    struct device_attribute *attr, char *buf)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	if (!rdev->constraints)
763*4882a593Smuzhiyun 		return sprintf(buf, "constraint not defined\n");
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", rdev->constraints->min_uV);
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
768*4882a593Smuzhiyun 
regulator_max_uV_show(struct device * dev,struct device_attribute * attr,char * buf)769*4882a593Smuzhiyun static ssize_t regulator_max_uV_show(struct device *dev,
770*4882a593Smuzhiyun 				    struct device_attribute *attr, char *buf)
771*4882a593Smuzhiyun {
772*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	if (!rdev->constraints)
775*4882a593Smuzhiyun 		return sprintf(buf, "constraint not defined\n");
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", rdev->constraints->max_uV);
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
780*4882a593Smuzhiyun 
regulator_total_uA_show(struct device * dev,struct device_attribute * attr,char * buf)781*4882a593Smuzhiyun static ssize_t regulator_total_uA_show(struct device *dev,
782*4882a593Smuzhiyun 				      struct device_attribute *attr, char *buf)
783*4882a593Smuzhiyun {
784*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
785*4882a593Smuzhiyun 	struct regulator *regulator;
786*4882a593Smuzhiyun 	int uA = 0;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	regulator_lock(rdev);
789*4882a593Smuzhiyun 	list_for_each_entry(regulator, &rdev->consumer_list, list) {
790*4882a593Smuzhiyun 		if (regulator->enable_count)
791*4882a593Smuzhiyun 			uA += regulator->uA_load;
792*4882a593Smuzhiyun 	}
793*4882a593Smuzhiyun 	regulator_unlock(rdev);
794*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", uA);
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
797*4882a593Smuzhiyun 
num_users_show(struct device * dev,struct device_attribute * attr,char * buf)798*4882a593Smuzhiyun static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
799*4882a593Smuzhiyun 			      char *buf)
800*4882a593Smuzhiyun {
801*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
802*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", rdev->use_count);
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun static DEVICE_ATTR_RO(num_users);
805*4882a593Smuzhiyun 
type_show(struct device * dev,struct device_attribute * attr,char * buf)806*4882a593Smuzhiyun static ssize_t type_show(struct device *dev, struct device_attribute *attr,
807*4882a593Smuzhiyun 			 char *buf)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	switch (rdev->desc->type) {
812*4882a593Smuzhiyun 	case REGULATOR_VOLTAGE:
813*4882a593Smuzhiyun 		return sprintf(buf, "voltage\n");
814*4882a593Smuzhiyun 	case REGULATOR_CURRENT:
815*4882a593Smuzhiyun 		return sprintf(buf, "current\n");
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun 	return sprintf(buf, "unknown\n");
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun static DEVICE_ATTR_RO(type);
820*4882a593Smuzhiyun 
regulator_suspend_mem_uV_show(struct device * dev,struct device_attribute * attr,char * buf)821*4882a593Smuzhiyun static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
822*4882a593Smuzhiyun 				struct device_attribute *attr, char *buf)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun static DEVICE_ATTR(suspend_mem_microvolts, 0444,
829*4882a593Smuzhiyun 		regulator_suspend_mem_uV_show, NULL);
830*4882a593Smuzhiyun 
regulator_suspend_disk_uV_show(struct device * dev,struct device_attribute * attr,char * buf)831*4882a593Smuzhiyun static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
832*4882a593Smuzhiyun 				struct device_attribute *attr, char *buf)
833*4882a593Smuzhiyun {
834*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun static DEVICE_ATTR(suspend_disk_microvolts, 0444,
839*4882a593Smuzhiyun 		regulator_suspend_disk_uV_show, NULL);
840*4882a593Smuzhiyun 
regulator_suspend_standby_uV_show(struct device * dev,struct device_attribute * attr,char * buf)841*4882a593Smuzhiyun static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
842*4882a593Smuzhiyun 				struct device_attribute *attr, char *buf)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
847*4882a593Smuzhiyun }
848*4882a593Smuzhiyun static DEVICE_ATTR(suspend_standby_microvolts, 0444,
849*4882a593Smuzhiyun 		regulator_suspend_standby_uV_show, NULL);
850*4882a593Smuzhiyun 
regulator_suspend_mem_mode_show(struct device * dev,struct device_attribute * attr,char * buf)851*4882a593Smuzhiyun static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
852*4882a593Smuzhiyun 				struct device_attribute *attr, char *buf)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	return regulator_print_opmode(buf,
857*4882a593Smuzhiyun 		rdev->constraints->state_mem.mode);
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun static DEVICE_ATTR(suspend_mem_mode, 0444,
860*4882a593Smuzhiyun 		regulator_suspend_mem_mode_show, NULL);
861*4882a593Smuzhiyun 
regulator_suspend_disk_mode_show(struct device * dev,struct device_attribute * attr,char * buf)862*4882a593Smuzhiyun static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
863*4882a593Smuzhiyun 				struct device_attribute *attr, char *buf)
864*4882a593Smuzhiyun {
865*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	return regulator_print_opmode(buf,
868*4882a593Smuzhiyun 		rdev->constraints->state_disk.mode);
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun static DEVICE_ATTR(suspend_disk_mode, 0444,
871*4882a593Smuzhiyun 		regulator_suspend_disk_mode_show, NULL);
872*4882a593Smuzhiyun 
regulator_suspend_standby_mode_show(struct device * dev,struct device_attribute * attr,char * buf)873*4882a593Smuzhiyun static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
874*4882a593Smuzhiyun 				struct device_attribute *attr, char *buf)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	return regulator_print_opmode(buf,
879*4882a593Smuzhiyun 		rdev->constraints->state_standby.mode);
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun static DEVICE_ATTR(suspend_standby_mode, 0444,
882*4882a593Smuzhiyun 		regulator_suspend_standby_mode_show, NULL);
883*4882a593Smuzhiyun 
regulator_suspend_mem_state_show(struct device * dev,struct device_attribute * attr,char * buf)884*4882a593Smuzhiyun static ssize_t regulator_suspend_mem_state_show(struct device *dev,
885*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
886*4882a593Smuzhiyun {
887*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	return regulator_print_state(buf,
890*4882a593Smuzhiyun 			rdev->constraints->state_mem.enabled);
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun static DEVICE_ATTR(suspend_mem_state, 0444,
893*4882a593Smuzhiyun 		regulator_suspend_mem_state_show, NULL);
894*4882a593Smuzhiyun 
regulator_suspend_disk_state_show(struct device * dev,struct device_attribute * attr,char * buf)895*4882a593Smuzhiyun static ssize_t regulator_suspend_disk_state_show(struct device *dev,
896*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
897*4882a593Smuzhiyun {
898*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	return regulator_print_state(buf,
901*4882a593Smuzhiyun 			rdev->constraints->state_disk.enabled);
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun static DEVICE_ATTR(suspend_disk_state, 0444,
904*4882a593Smuzhiyun 		regulator_suspend_disk_state_show, NULL);
905*4882a593Smuzhiyun 
regulator_suspend_standby_state_show(struct device * dev,struct device_attribute * attr,char * buf)906*4882a593Smuzhiyun static ssize_t regulator_suspend_standby_state_show(struct device *dev,
907*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	return regulator_print_state(buf,
912*4882a593Smuzhiyun 			rdev->constraints->state_standby.enabled);
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun static DEVICE_ATTR(suspend_standby_state, 0444,
915*4882a593Smuzhiyun 		regulator_suspend_standby_state_show, NULL);
916*4882a593Smuzhiyun 
regulator_bypass_show(struct device * dev,struct device_attribute * attr,char * buf)917*4882a593Smuzhiyun static ssize_t regulator_bypass_show(struct device *dev,
918*4882a593Smuzhiyun 				     struct device_attribute *attr, char *buf)
919*4882a593Smuzhiyun {
920*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
921*4882a593Smuzhiyun 	const char *report;
922*4882a593Smuzhiyun 	bool bypass;
923*4882a593Smuzhiyun 	int ret;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	ret = rdev->desc->ops->get_bypass(rdev, &bypass);
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	if (ret != 0)
928*4882a593Smuzhiyun 		report = "unknown";
929*4882a593Smuzhiyun 	else if (bypass)
930*4882a593Smuzhiyun 		report = "enabled";
931*4882a593Smuzhiyun 	else
932*4882a593Smuzhiyun 		report = "disabled";
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", report);
935*4882a593Smuzhiyun }
936*4882a593Smuzhiyun static DEVICE_ATTR(bypass, 0444,
937*4882a593Smuzhiyun 		   regulator_bypass_show, NULL);
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun /* Calculate the new optimum regulator operating mode based on the new total
940*4882a593Smuzhiyun  * consumer load. All locks held by caller */
drms_uA_update(struct regulator_dev * rdev)941*4882a593Smuzhiyun static int drms_uA_update(struct regulator_dev *rdev)
942*4882a593Smuzhiyun {
943*4882a593Smuzhiyun 	struct regulator *sibling;
944*4882a593Smuzhiyun 	int current_uA = 0, output_uV, input_uV, err;
945*4882a593Smuzhiyun 	unsigned int mode;
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	/*
948*4882a593Smuzhiyun 	 * first check to see if we can set modes at all, otherwise just
949*4882a593Smuzhiyun 	 * tell the consumer everything is OK.
950*4882a593Smuzhiyun 	 */
951*4882a593Smuzhiyun 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
952*4882a593Smuzhiyun 		rdev_dbg(rdev, "DRMS operation not allowed\n");
953*4882a593Smuzhiyun 		return 0;
954*4882a593Smuzhiyun 	}
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	if (!rdev->desc->ops->get_optimum_mode &&
957*4882a593Smuzhiyun 	    !rdev->desc->ops->set_load)
958*4882a593Smuzhiyun 		return 0;
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	if (!rdev->desc->ops->set_mode &&
961*4882a593Smuzhiyun 	    !rdev->desc->ops->set_load)
962*4882a593Smuzhiyun 		return -EINVAL;
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	/* calc total requested load */
965*4882a593Smuzhiyun 	list_for_each_entry(sibling, &rdev->consumer_list, list) {
966*4882a593Smuzhiyun 		if (sibling->enable_count)
967*4882a593Smuzhiyun 			current_uA += sibling->uA_load;
968*4882a593Smuzhiyun 	}
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	current_uA += rdev->constraints->system_load;
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	if (rdev->desc->ops->set_load) {
973*4882a593Smuzhiyun 		/* set the optimum mode for our new total regulator load */
974*4882a593Smuzhiyun 		err = rdev->desc->ops->set_load(rdev, current_uA);
975*4882a593Smuzhiyun 		if (err < 0)
976*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set load %d: %pe\n",
977*4882a593Smuzhiyun 				 current_uA, ERR_PTR(err));
978*4882a593Smuzhiyun 	} else {
979*4882a593Smuzhiyun 		/* get output voltage */
980*4882a593Smuzhiyun 		output_uV = regulator_get_voltage_rdev(rdev);
981*4882a593Smuzhiyun 		if (output_uV <= 0) {
982*4882a593Smuzhiyun 			rdev_err(rdev, "invalid output voltage found\n");
983*4882a593Smuzhiyun 			return -EINVAL;
984*4882a593Smuzhiyun 		}
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 		/* get input voltage */
987*4882a593Smuzhiyun 		input_uV = 0;
988*4882a593Smuzhiyun 		if (rdev->supply)
989*4882a593Smuzhiyun 			input_uV = regulator_get_voltage(rdev->supply);
990*4882a593Smuzhiyun 		if (input_uV <= 0)
991*4882a593Smuzhiyun 			input_uV = rdev->constraints->input_uV;
992*4882a593Smuzhiyun 		if (input_uV <= 0) {
993*4882a593Smuzhiyun 			rdev_err(rdev, "invalid input voltage found\n");
994*4882a593Smuzhiyun 			return -EINVAL;
995*4882a593Smuzhiyun 		}
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 		/* now get the optimum mode for our new total regulator load */
998*4882a593Smuzhiyun 		mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
999*4882a593Smuzhiyun 							 output_uV, current_uA);
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun 		/* check the new mode is allowed */
1002*4882a593Smuzhiyun 		err = regulator_mode_constrain(rdev, &mode);
1003*4882a593Smuzhiyun 		if (err < 0) {
1004*4882a593Smuzhiyun 			rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
1005*4882a593Smuzhiyun 				 current_uA, input_uV, output_uV, ERR_PTR(err));
1006*4882a593Smuzhiyun 			return err;
1007*4882a593Smuzhiyun 		}
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 		err = rdev->desc->ops->set_mode(rdev, mode);
1010*4882a593Smuzhiyun 		if (err < 0)
1011*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set optimum mode %x: %pe\n",
1012*4882a593Smuzhiyun 				 mode, ERR_PTR(err));
1013*4882a593Smuzhiyun 	}
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 	return err;
1016*4882a593Smuzhiyun }
1017*4882a593Smuzhiyun 
__suspend_set_state(struct regulator_dev * rdev,const struct regulator_state * rstate)1018*4882a593Smuzhiyun static int __suspend_set_state(struct regulator_dev *rdev,
1019*4882a593Smuzhiyun 			       const struct regulator_state *rstate)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun 	int ret = 0;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	if (rstate->enabled == ENABLE_IN_SUSPEND &&
1024*4882a593Smuzhiyun 		rdev->desc->ops->set_suspend_enable)
1025*4882a593Smuzhiyun 		ret = rdev->desc->ops->set_suspend_enable(rdev);
1026*4882a593Smuzhiyun 	else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1027*4882a593Smuzhiyun 		rdev->desc->ops->set_suspend_disable)
1028*4882a593Smuzhiyun 		ret = rdev->desc->ops->set_suspend_disable(rdev);
1029*4882a593Smuzhiyun 	else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1030*4882a593Smuzhiyun 		ret = 0;
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun 	if (ret < 0) {
1033*4882a593Smuzhiyun 		rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret));
1034*4882a593Smuzhiyun 		return ret;
1035*4882a593Smuzhiyun 	}
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1038*4882a593Smuzhiyun 		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1039*4882a593Smuzhiyun 		if (ret < 0) {
1040*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret));
1041*4882a593Smuzhiyun 			return ret;
1042*4882a593Smuzhiyun 		}
1043*4882a593Smuzhiyun 	}
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1046*4882a593Smuzhiyun 		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1047*4882a593Smuzhiyun 		if (ret < 0) {
1048*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret));
1049*4882a593Smuzhiyun 			return ret;
1050*4882a593Smuzhiyun 		}
1051*4882a593Smuzhiyun 	}
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	return ret;
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun 
suspend_set_initial_state(struct regulator_dev * rdev)1056*4882a593Smuzhiyun static int suspend_set_initial_state(struct regulator_dev *rdev)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun 	const struct regulator_state *rstate;
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	rstate = regulator_get_suspend_state_check(rdev,
1061*4882a593Smuzhiyun 			rdev->constraints->initial_state);
1062*4882a593Smuzhiyun 	if (!rstate)
1063*4882a593Smuzhiyun 		return 0;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	return __suspend_set_state(rdev, rstate);
1066*4882a593Smuzhiyun }
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
print_constraints_debug(struct regulator_dev * rdev)1069*4882a593Smuzhiyun static void print_constraints_debug(struct regulator_dev *rdev)
1070*4882a593Smuzhiyun {
1071*4882a593Smuzhiyun 	struct regulation_constraints *constraints = rdev->constraints;
1072*4882a593Smuzhiyun 	char buf[160] = "";
1073*4882a593Smuzhiyun 	size_t len = sizeof(buf) - 1;
1074*4882a593Smuzhiyun 	int count = 0;
1075*4882a593Smuzhiyun 	int ret;
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	if (constraints->min_uV && constraints->max_uV) {
1078*4882a593Smuzhiyun 		if (constraints->min_uV == constraints->max_uV)
1079*4882a593Smuzhiyun 			count += scnprintf(buf + count, len - count, "%d mV ",
1080*4882a593Smuzhiyun 					   constraints->min_uV / 1000);
1081*4882a593Smuzhiyun 		else
1082*4882a593Smuzhiyun 			count += scnprintf(buf + count, len - count,
1083*4882a593Smuzhiyun 					   "%d <--> %d mV ",
1084*4882a593Smuzhiyun 					   constraints->min_uV / 1000,
1085*4882a593Smuzhiyun 					   constraints->max_uV / 1000);
1086*4882a593Smuzhiyun 	}
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	if (!constraints->min_uV ||
1089*4882a593Smuzhiyun 	    constraints->min_uV != constraints->max_uV) {
1090*4882a593Smuzhiyun 		ret = regulator_get_voltage_rdev(rdev);
1091*4882a593Smuzhiyun 		if (ret > 0)
1092*4882a593Smuzhiyun 			count += scnprintf(buf + count, len - count,
1093*4882a593Smuzhiyun 					   "at %d mV ", ret / 1000);
1094*4882a593Smuzhiyun 	}
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	if (constraints->uV_offset)
1097*4882a593Smuzhiyun 		count += scnprintf(buf + count, len - count, "%dmV offset ",
1098*4882a593Smuzhiyun 				   constraints->uV_offset / 1000);
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	if (constraints->min_uA && constraints->max_uA) {
1101*4882a593Smuzhiyun 		if (constraints->min_uA == constraints->max_uA)
1102*4882a593Smuzhiyun 			count += scnprintf(buf + count, len - count, "%d mA ",
1103*4882a593Smuzhiyun 					   constraints->min_uA / 1000);
1104*4882a593Smuzhiyun 		else
1105*4882a593Smuzhiyun 			count += scnprintf(buf + count, len - count,
1106*4882a593Smuzhiyun 					   "%d <--> %d mA ",
1107*4882a593Smuzhiyun 					   constraints->min_uA / 1000,
1108*4882a593Smuzhiyun 					   constraints->max_uA / 1000);
1109*4882a593Smuzhiyun 	}
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	if (!constraints->min_uA ||
1112*4882a593Smuzhiyun 	    constraints->min_uA != constraints->max_uA) {
1113*4882a593Smuzhiyun 		ret = _regulator_get_current_limit(rdev);
1114*4882a593Smuzhiyun 		if (ret > 0)
1115*4882a593Smuzhiyun 			count += scnprintf(buf + count, len - count,
1116*4882a593Smuzhiyun 					   "at %d mA ", ret / 1000);
1117*4882a593Smuzhiyun 	}
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1120*4882a593Smuzhiyun 		count += scnprintf(buf + count, len - count, "fast ");
1121*4882a593Smuzhiyun 	if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1122*4882a593Smuzhiyun 		count += scnprintf(buf + count, len - count, "normal ");
1123*4882a593Smuzhiyun 	if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1124*4882a593Smuzhiyun 		count += scnprintf(buf + count, len - count, "idle ");
1125*4882a593Smuzhiyun 	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1126*4882a593Smuzhiyun 		count += scnprintf(buf + count, len - count, "standby ");
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	if (!count)
1129*4882a593Smuzhiyun 		count = scnprintf(buf, len, "no parameters");
1130*4882a593Smuzhiyun 	else
1131*4882a593Smuzhiyun 		--count;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	count += scnprintf(buf + count, len - count, ", %s",
1134*4882a593Smuzhiyun 		_regulator_is_enabled(rdev) ? "enabled" : "disabled");
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 	rdev_dbg(rdev, "%s\n", buf);
1137*4882a593Smuzhiyun }
1138*4882a593Smuzhiyun #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
print_constraints_debug(struct regulator_dev * rdev)1139*4882a593Smuzhiyun static inline void print_constraints_debug(struct regulator_dev *rdev) {}
1140*4882a593Smuzhiyun #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1141*4882a593Smuzhiyun 
print_constraints(struct regulator_dev * rdev)1142*4882a593Smuzhiyun static void print_constraints(struct regulator_dev *rdev)
1143*4882a593Smuzhiyun {
1144*4882a593Smuzhiyun 	struct regulation_constraints *constraints = rdev->constraints;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	print_constraints_debug(rdev);
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	if ((constraints->min_uV != constraints->max_uV) &&
1149*4882a593Smuzhiyun 	    !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1150*4882a593Smuzhiyun 		rdev_warn(rdev,
1151*4882a593Smuzhiyun 			  "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1152*4882a593Smuzhiyun }
1153*4882a593Smuzhiyun 
machine_constraints_voltage(struct regulator_dev * rdev,struct regulation_constraints * constraints)1154*4882a593Smuzhiyun static int machine_constraints_voltage(struct regulator_dev *rdev,
1155*4882a593Smuzhiyun 	struct regulation_constraints *constraints)
1156*4882a593Smuzhiyun {
1157*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
1158*4882a593Smuzhiyun 	int ret;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	/* do we need to apply the constraint voltage */
1161*4882a593Smuzhiyun 	if (rdev->constraints->apply_uV &&
1162*4882a593Smuzhiyun 	    rdev->constraints->min_uV && rdev->constraints->max_uV) {
1163*4882a593Smuzhiyun 		int target_min, target_max;
1164*4882a593Smuzhiyun 		int current_uV = regulator_get_voltage_rdev(rdev);
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 		if (current_uV == -ENOTRECOVERABLE) {
1167*4882a593Smuzhiyun 			/* This regulator can't be read and must be initialized */
1168*4882a593Smuzhiyun 			rdev_info(rdev, "Setting %d-%duV\n",
1169*4882a593Smuzhiyun 				  rdev->constraints->min_uV,
1170*4882a593Smuzhiyun 				  rdev->constraints->max_uV);
1171*4882a593Smuzhiyun 			_regulator_do_set_voltage(rdev,
1172*4882a593Smuzhiyun 						  rdev->constraints->min_uV,
1173*4882a593Smuzhiyun 						  rdev->constraints->max_uV);
1174*4882a593Smuzhiyun 			current_uV = regulator_get_voltage_rdev(rdev);
1175*4882a593Smuzhiyun 		}
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 		if (current_uV < 0) {
1178*4882a593Smuzhiyun 			rdev_err(rdev,
1179*4882a593Smuzhiyun 				 "failed to get the current voltage: %pe\n",
1180*4882a593Smuzhiyun 				 ERR_PTR(current_uV));
1181*4882a593Smuzhiyun 			return current_uV;
1182*4882a593Smuzhiyun 		}
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 		/*
1185*4882a593Smuzhiyun 		 * If we're below the minimum voltage move up to the
1186*4882a593Smuzhiyun 		 * minimum voltage, if we're above the maximum voltage
1187*4882a593Smuzhiyun 		 * then move down to the maximum.
1188*4882a593Smuzhiyun 		 */
1189*4882a593Smuzhiyun 		target_min = current_uV;
1190*4882a593Smuzhiyun 		target_max = current_uV;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 		if (current_uV < rdev->constraints->min_uV) {
1193*4882a593Smuzhiyun 			target_min = rdev->constraints->min_uV;
1194*4882a593Smuzhiyun 			target_max = rdev->constraints->min_uV;
1195*4882a593Smuzhiyun 		}
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 		if (current_uV > rdev->constraints->max_uV) {
1198*4882a593Smuzhiyun 			target_min = rdev->constraints->max_uV;
1199*4882a593Smuzhiyun 			target_max = rdev->constraints->max_uV;
1200*4882a593Smuzhiyun 		}
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 		if (target_min != current_uV || target_max != current_uV) {
1203*4882a593Smuzhiyun 			rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1204*4882a593Smuzhiyun 				  current_uV, target_min, target_max);
1205*4882a593Smuzhiyun 			ret = _regulator_do_set_voltage(
1206*4882a593Smuzhiyun 				rdev, target_min, target_max);
1207*4882a593Smuzhiyun 			if (ret < 0) {
1208*4882a593Smuzhiyun 				rdev_err(rdev,
1209*4882a593Smuzhiyun 					"failed to apply %d-%duV constraint: %pe\n",
1210*4882a593Smuzhiyun 					target_min, target_max, ERR_PTR(ret));
1211*4882a593Smuzhiyun 				return ret;
1212*4882a593Smuzhiyun 			}
1213*4882a593Smuzhiyun 		}
1214*4882a593Smuzhiyun 	}
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	/* constrain machine-level voltage specs to fit
1217*4882a593Smuzhiyun 	 * the actual range supported by this regulator.
1218*4882a593Smuzhiyun 	 */
1219*4882a593Smuzhiyun 	if (ops->list_voltage && rdev->desc->n_voltages) {
1220*4882a593Smuzhiyun 		int	count = rdev->desc->n_voltages;
1221*4882a593Smuzhiyun 		int	i;
1222*4882a593Smuzhiyun 		int	min_uV = INT_MAX;
1223*4882a593Smuzhiyun 		int	max_uV = INT_MIN;
1224*4882a593Smuzhiyun 		int	cmin = constraints->min_uV;
1225*4882a593Smuzhiyun 		int	cmax = constraints->max_uV;
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun 		/* it's safe to autoconfigure fixed-voltage supplies
1228*4882a593Smuzhiyun 		   and the constraints are used by list_voltage. */
1229*4882a593Smuzhiyun 		if (count == 1 && !cmin) {
1230*4882a593Smuzhiyun 			cmin = 1;
1231*4882a593Smuzhiyun 			cmax = INT_MAX;
1232*4882a593Smuzhiyun 			constraints->min_uV = cmin;
1233*4882a593Smuzhiyun 			constraints->max_uV = cmax;
1234*4882a593Smuzhiyun 		}
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 		/* voltage constraints are optional */
1237*4882a593Smuzhiyun 		if ((cmin == 0) && (cmax == 0))
1238*4882a593Smuzhiyun 			return 0;
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 		/* else require explicit machine-level constraints */
1241*4882a593Smuzhiyun 		if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1242*4882a593Smuzhiyun 			rdev_err(rdev, "invalid voltage constraints\n");
1243*4882a593Smuzhiyun 			return -EINVAL;
1244*4882a593Smuzhiyun 		}
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 		/* no need to loop voltages if range is continuous */
1247*4882a593Smuzhiyun 		if (rdev->desc->continuous_voltage_range)
1248*4882a593Smuzhiyun 			return 0;
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 		/* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1251*4882a593Smuzhiyun 		for (i = 0; i < count; i++) {
1252*4882a593Smuzhiyun 			int	value;
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 			value = ops->list_voltage(rdev, i);
1255*4882a593Smuzhiyun 			if (value <= 0)
1256*4882a593Smuzhiyun 				continue;
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun 			/* maybe adjust [min_uV..max_uV] */
1259*4882a593Smuzhiyun 			if (value >= cmin && value < min_uV)
1260*4882a593Smuzhiyun 				min_uV = value;
1261*4882a593Smuzhiyun 			if (value <= cmax && value > max_uV)
1262*4882a593Smuzhiyun 				max_uV = value;
1263*4882a593Smuzhiyun 		}
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 		/* final: [min_uV..max_uV] valid iff constraints valid */
1266*4882a593Smuzhiyun 		if (max_uV < min_uV) {
1267*4882a593Smuzhiyun 			rdev_err(rdev,
1268*4882a593Smuzhiyun 				 "unsupportable voltage constraints %u-%uuV\n",
1269*4882a593Smuzhiyun 				 min_uV, max_uV);
1270*4882a593Smuzhiyun 			return -EINVAL;
1271*4882a593Smuzhiyun 		}
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 		/* use regulator's subset of machine constraints */
1274*4882a593Smuzhiyun 		if (constraints->min_uV < min_uV) {
1275*4882a593Smuzhiyun 			rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1276*4882a593Smuzhiyun 				 constraints->min_uV, min_uV);
1277*4882a593Smuzhiyun 			constraints->min_uV = min_uV;
1278*4882a593Smuzhiyun 		}
1279*4882a593Smuzhiyun 		if (constraints->max_uV > max_uV) {
1280*4882a593Smuzhiyun 			rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1281*4882a593Smuzhiyun 				 constraints->max_uV, max_uV);
1282*4882a593Smuzhiyun 			constraints->max_uV = max_uV;
1283*4882a593Smuzhiyun 		}
1284*4882a593Smuzhiyun 	}
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	return 0;
1287*4882a593Smuzhiyun }
1288*4882a593Smuzhiyun 
machine_constraints_current(struct regulator_dev * rdev,struct regulation_constraints * constraints)1289*4882a593Smuzhiyun static int machine_constraints_current(struct regulator_dev *rdev,
1290*4882a593Smuzhiyun 	struct regulation_constraints *constraints)
1291*4882a593Smuzhiyun {
1292*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
1293*4882a593Smuzhiyun 	int ret;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	if (!constraints->min_uA && !constraints->max_uA)
1296*4882a593Smuzhiyun 		return 0;
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	if (constraints->min_uA > constraints->max_uA) {
1299*4882a593Smuzhiyun 		rdev_err(rdev, "Invalid current constraints\n");
1300*4882a593Smuzhiyun 		return -EINVAL;
1301*4882a593Smuzhiyun 	}
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 	if (!ops->set_current_limit || !ops->get_current_limit) {
1304*4882a593Smuzhiyun 		rdev_warn(rdev, "Operation of current configuration missing\n");
1305*4882a593Smuzhiyun 		return 0;
1306*4882a593Smuzhiyun 	}
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	/* Set regulator current in constraints range */
1309*4882a593Smuzhiyun 	ret = ops->set_current_limit(rdev, constraints->min_uA,
1310*4882a593Smuzhiyun 			constraints->max_uA);
1311*4882a593Smuzhiyun 	if (ret < 0) {
1312*4882a593Smuzhiyun 		rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1313*4882a593Smuzhiyun 		return ret;
1314*4882a593Smuzhiyun 	}
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 	return 0;
1317*4882a593Smuzhiyun }
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun static int _regulator_do_enable(struct regulator_dev *rdev);
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun /**
1322*4882a593Smuzhiyun  * set_machine_constraints - sets regulator constraints
1323*4882a593Smuzhiyun  * @rdev: regulator source
1324*4882a593Smuzhiyun  *
1325*4882a593Smuzhiyun  * Allows platform initialisation code to define and constrain
1326*4882a593Smuzhiyun  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
1327*4882a593Smuzhiyun  * Constraints *must* be set by platform code in order for some
1328*4882a593Smuzhiyun  * regulator operations to proceed i.e. set_voltage, set_current_limit,
1329*4882a593Smuzhiyun  * set_mode.
1330*4882a593Smuzhiyun  */
set_machine_constraints(struct regulator_dev * rdev)1331*4882a593Smuzhiyun static int set_machine_constraints(struct regulator_dev *rdev)
1332*4882a593Smuzhiyun {
1333*4882a593Smuzhiyun 	int ret = 0;
1334*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun 	ret = machine_constraints_voltage(rdev, rdev->constraints);
1337*4882a593Smuzhiyun 	if (ret != 0)
1338*4882a593Smuzhiyun 		return ret;
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	ret = machine_constraints_current(rdev, rdev->constraints);
1341*4882a593Smuzhiyun 	if (ret != 0)
1342*4882a593Smuzhiyun 		return ret;
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun 	if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1345*4882a593Smuzhiyun 		ret = ops->set_input_current_limit(rdev,
1346*4882a593Smuzhiyun 						   rdev->constraints->ilim_uA);
1347*4882a593Smuzhiyun 		if (ret < 0) {
1348*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret));
1349*4882a593Smuzhiyun 			return ret;
1350*4882a593Smuzhiyun 		}
1351*4882a593Smuzhiyun 	}
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	/* do we need to setup our suspend state */
1354*4882a593Smuzhiyun 	if (rdev->constraints->initial_state) {
1355*4882a593Smuzhiyun 		ret = suspend_set_initial_state(rdev);
1356*4882a593Smuzhiyun 		if (ret < 0) {
1357*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret));
1358*4882a593Smuzhiyun 			return ret;
1359*4882a593Smuzhiyun 		}
1360*4882a593Smuzhiyun 	}
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	if (rdev->constraints->initial_mode) {
1363*4882a593Smuzhiyun 		if (!ops->set_mode) {
1364*4882a593Smuzhiyun 			rdev_err(rdev, "no set_mode operation\n");
1365*4882a593Smuzhiyun 			return -EINVAL;
1366*4882a593Smuzhiyun 		}
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 		ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1369*4882a593Smuzhiyun 		if (ret < 0) {
1370*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret));
1371*4882a593Smuzhiyun 			return ret;
1372*4882a593Smuzhiyun 		}
1373*4882a593Smuzhiyun 	} else if (rdev->constraints->system_load) {
1374*4882a593Smuzhiyun 		/*
1375*4882a593Smuzhiyun 		 * We'll only apply the initial system load if an
1376*4882a593Smuzhiyun 		 * initial mode wasn't specified.
1377*4882a593Smuzhiyun 		 */
1378*4882a593Smuzhiyun 		drms_uA_update(rdev);
1379*4882a593Smuzhiyun 	}
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1382*4882a593Smuzhiyun 		&& ops->set_ramp_delay) {
1383*4882a593Smuzhiyun 		ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1384*4882a593Smuzhiyun 		if (ret < 0) {
1385*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret));
1386*4882a593Smuzhiyun 			return ret;
1387*4882a593Smuzhiyun 		}
1388*4882a593Smuzhiyun 	}
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun 	if (rdev->constraints->pull_down && ops->set_pull_down) {
1391*4882a593Smuzhiyun 		ret = ops->set_pull_down(rdev);
1392*4882a593Smuzhiyun 		if (ret < 0) {
1393*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret));
1394*4882a593Smuzhiyun 			return ret;
1395*4882a593Smuzhiyun 		}
1396*4882a593Smuzhiyun 	}
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	if (rdev->constraints->soft_start && ops->set_soft_start) {
1399*4882a593Smuzhiyun 		ret = ops->set_soft_start(rdev);
1400*4882a593Smuzhiyun 		if (ret < 0) {
1401*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret));
1402*4882a593Smuzhiyun 			return ret;
1403*4882a593Smuzhiyun 		}
1404*4882a593Smuzhiyun 	}
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	if (rdev->constraints->over_current_protection
1407*4882a593Smuzhiyun 		&& ops->set_over_current_protection) {
1408*4882a593Smuzhiyun 		ret = ops->set_over_current_protection(rdev);
1409*4882a593Smuzhiyun 		if (ret < 0) {
1410*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set over current protection: %pe\n",
1411*4882a593Smuzhiyun 				 ERR_PTR(ret));
1412*4882a593Smuzhiyun 			return ret;
1413*4882a593Smuzhiyun 		}
1414*4882a593Smuzhiyun 	}
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1417*4882a593Smuzhiyun 		bool ad_state = (rdev->constraints->active_discharge ==
1418*4882a593Smuzhiyun 			      REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 		ret = ops->set_active_discharge(rdev, ad_state);
1421*4882a593Smuzhiyun 		if (ret < 0) {
1422*4882a593Smuzhiyun 			rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret));
1423*4882a593Smuzhiyun 			return ret;
1424*4882a593Smuzhiyun 		}
1425*4882a593Smuzhiyun 	}
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun 	/* If the constraints say the regulator should be on at this point
1428*4882a593Smuzhiyun 	 * and we have control then make sure it is enabled.
1429*4882a593Smuzhiyun 	 */
1430*4882a593Smuzhiyun 	if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1431*4882a593Smuzhiyun 		/* If we want to enable this regulator, make sure that we know
1432*4882a593Smuzhiyun 		 * the supplying regulator.
1433*4882a593Smuzhiyun 		 */
1434*4882a593Smuzhiyun 		if (rdev->supply_name && !rdev->supply)
1435*4882a593Smuzhiyun 			return -EPROBE_DEFER;
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 		if (rdev->supply) {
1438*4882a593Smuzhiyun 			ret = regulator_enable(rdev->supply);
1439*4882a593Smuzhiyun 			if (ret < 0) {
1440*4882a593Smuzhiyun 				_regulator_put(rdev->supply);
1441*4882a593Smuzhiyun 				rdev->supply = NULL;
1442*4882a593Smuzhiyun 				return ret;
1443*4882a593Smuzhiyun 			}
1444*4882a593Smuzhiyun 		}
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun 		ret = _regulator_do_enable(rdev);
1447*4882a593Smuzhiyun 		if (ret < 0 && ret != -EINVAL) {
1448*4882a593Smuzhiyun 			rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret));
1449*4882a593Smuzhiyun 			return ret;
1450*4882a593Smuzhiyun 		}
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 		if (rdev->constraints->always_on)
1453*4882a593Smuzhiyun 			rdev->use_count++;
1454*4882a593Smuzhiyun 	}
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	print_constraints(rdev);
1457*4882a593Smuzhiyun 	return 0;
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun /**
1461*4882a593Smuzhiyun  * set_supply - set regulator supply regulator
1462*4882a593Smuzhiyun  * @rdev: regulator name
1463*4882a593Smuzhiyun  * @supply_rdev: supply regulator name
1464*4882a593Smuzhiyun  *
1465*4882a593Smuzhiyun  * Called by platform initialisation code to set the supply regulator for this
1466*4882a593Smuzhiyun  * regulator. This ensures that a regulators supply will also be enabled by the
1467*4882a593Smuzhiyun  * core if it's child is enabled.
1468*4882a593Smuzhiyun  */
set_supply(struct regulator_dev * rdev,struct regulator_dev * supply_rdev)1469*4882a593Smuzhiyun static int set_supply(struct regulator_dev *rdev,
1470*4882a593Smuzhiyun 		      struct regulator_dev *supply_rdev)
1471*4882a593Smuzhiyun {
1472*4882a593Smuzhiyun 	int err;
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 	rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	if (!try_module_get(supply_rdev->owner))
1477*4882a593Smuzhiyun 		return -ENODEV;
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun 	rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1480*4882a593Smuzhiyun 	if (rdev->supply == NULL) {
1481*4882a593Smuzhiyun 		err = -ENOMEM;
1482*4882a593Smuzhiyun 		return err;
1483*4882a593Smuzhiyun 	}
1484*4882a593Smuzhiyun 	supply_rdev->open_count++;
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	return 0;
1487*4882a593Smuzhiyun }
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun /**
1490*4882a593Smuzhiyun  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1491*4882a593Smuzhiyun  * @rdev:         regulator source
1492*4882a593Smuzhiyun  * @consumer_dev_name: dev_name() string for device supply applies to
1493*4882a593Smuzhiyun  * @supply:       symbolic name for supply
1494*4882a593Smuzhiyun  *
1495*4882a593Smuzhiyun  * Allows platform initialisation code to map physical regulator
1496*4882a593Smuzhiyun  * sources to symbolic names for supplies for use by devices.  Devices
1497*4882a593Smuzhiyun  * should use these symbolic names to request regulators, avoiding the
1498*4882a593Smuzhiyun  * need to provide board-specific regulator names as platform data.
1499*4882a593Smuzhiyun  */
set_consumer_device_supply(struct regulator_dev * rdev,const char * consumer_dev_name,const char * supply)1500*4882a593Smuzhiyun static int set_consumer_device_supply(struct regulator_dev *rdev,
1501*4882a593Smuzhiyun 				      const char *consumer_dev_name,
1502*4882a593Smuzhiyun 				      const char *supply)
1503*4882a593Smuzhiyun {
1504*4882a593Smuzhiyun 	struct regulator_map *node, *new_node;
1505*4882a593Smuzhiyun 	int has_dev;
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 	if (supply == NULL)
1508*4882a593Smuzhiyun 		return -EINVAL;
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 	if (consumer_dev_name != NULL)
1511*4882a593Smuzhiyun 		has_dev = 1;
1512*4882a593Smuzhiyun 	else
1513*4882a593Smuzhiyun 		has_dev = 0;
1514*4882a593Smuzhiyun 
1515*4882a593Smuzhiyun 	new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1516*4882a593Smuzhiyun 	if (new_node == NULL)
1517*4882a593Smuzhiyun 		return -ENOMEM;
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 	new_node->regulator = rdev;
1520*4882a593Smuzhiyun 	new_node->supply = supply;
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun 	if (has_dev) {
1523*4882a593Smuzhiyun 		new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1524*4882a593Smuzhiyun 		if (new_node->dev_name == NULL) {
1525*4882a593Smuzhiyun 			kfree(new_node);
1526*4882a593Smuzhiyun 			return -ENOMEM;
1527*4882a593Smuzhiyun 		}
1528*4882a593Smuzhiyun 	}
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
1531*4882a593Smuzhiyun 	list_for_each_entry(node, &regulator_map_list, list) {
1532*4882a593Smuzhiyun 		if (node->dev_name && consumer_dev_name) {
1533*4882a593Smuzhiyun 			if (strcmp(node->dev_name, consumer_dev_name) != 0)
1534*4882a593Smuzhiyun 				continue;
1535*4882a593Smuzhiyun 		} else if (node->dev_name || consumer_dev_name) {
1536*4882a593Smuzhiyun 			continue;
1537*4882a593Smuzhiyun 		}
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 		if (strcmp(node->supply, supply) != 0)
1540*4882a593Smuzhiyun 			continue;
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 		pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1543*4882a593Smuzhiyun 			 consumer_dev_name,
1544*4882a593Smuzhiyun 			 dev_name(&node->regulator->dev),
1545*4882a593Smuzhiyun 			 node->regulator->desc->name,
1546*4882a593Smuzhiyun 			 supply,
1547*4882a593Smuzhiyun 			 dev_name(&rdev->dev), rdev_get_name(rdev));
1548*4882a593Smuzhiyun 		goto fail;
1549*4882a593Smuzhiyun 	}
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 	list_add(&new_node->list, &regulator_map_list);
1552*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun 	return 0;
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun fail:
1557*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
1558*4882a593Smuzhiyun 	kfree(new_node->dev_name);
1559*4882a593Smuzhiyun 	kfree(new_node);
1560*4882a593Smuzhiyun 	return -EBUSY;
1561*4882a593Smuzhiyun }
1562*4882a593Smuzhiyun 
unset_regulator_supplies(struct regulator_dev * rdev)1563*4882a593Smuzhiyun static void unset_regulator_supplies(struct regulator_dev *rdev)
1564*4882a593Smuzhiyun {
1565*4882a593Smuzhiyun 	struct regulator_map *node, *n;
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun 	list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1568*4882a593Smuzhiyun 		if (rdev == node->regulator) {
1569*4882a593Smuzhiyun 			list_del(&node->list);
1570*4882a593Smuzhiyun 			kfree(node->dev_name);
1571*4882a593Smuzhiyun 			kfree(node);
1572*4882a593Smuzhiyun 		}
1573*4882a593Smuzhiyun 	}
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
constraint_flags_read_file(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1577*4882a593Smuzhiyun static ssize_t constraint_flags_read_file(struct file *file,
1578*4882a593Smuzhiyun 					  char __user *user_buf,
1579*4882a593Smuzhiyun 					  size_t count, loff_t *ppos)
1580*4882a593Smuzhiyun {
1581*4882a593Smuzhiyun 	const struct regulator *regulator = file->private_data;
1582*4882a593Smuzhiyun 	const struct regulation_constraints *c = regulator->rdev->constraints;
1583*4882a593Smuzhiyun 	char *buf;
1584*4882a593Smuzhiyun 	ssize_t ret;
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun 	if (!c)
1587*4882a593Smuzhiyun 		return 0;
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1590*4882a593Smuzhiyun 	if (!buf)
1591*4882a593Smuzhiyun 		return -ENOMEM;
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	ret = snprintf(buf, PAGE_SIZE,
1594*4882a593Smuzhiyun 			"always_on: %u\n"
1595*4882a593Smuzhiyun 			"boot_on: %u\n"
1596*4882a593Smuzhiyun 			"apply_uV: %u\n"
1597*4882a593Smuzhiyun 			"ramp_disable: %u\n"
1598*4882a593Smuzhiyun 			"soft_start: %u\n"
1599*4882a593Smuzhiyun 			"pull_down: %u\n"
1600*4882a593Smuzhiyun 			"over_current_protection: %u\n",
1601*4882a593Smuzhiyun 			c->always_on,
1602*4882a593Smuzhiyun 			c->boot_on,
1603*4882a593Smuzhiyun 			c->apply_uV,
1604*4882a593Smuzhiyun 			c->ramp_disable,
1605*4882a593Smuzhiyun 			c->soft_start,
1606*4882a593Smuzhiyun 			c->pull_down,
1607*4882a593Smuzhiyun 			c->over_current_protection);
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1610*4882a593Smuzhiyun 	kfree(buf);
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun 	return ret;
1613*4882a593Smuzhiyun }
1614*4882a593Smuzhiyun 
1615*4882a593Smuzhiyun #endif
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun static const struct file_operations constraint_flags_fops = {
1618*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
1619*4882a593Smuzhiyun 	.open = simple_open,
1620*4882a593Smuzhiyun 	.read = constraint_flags_read_file,
1621*4882a593Smuzhiyun 	.llseek = default_llseek,
1622*4882a593Smuzhiyun #endif
1623*4882a593Smuzhiyun };
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun #define REG_STR_SIZE	64
1626*4882a593Smuzhiyun 
create_regulator(struct regulator_dev * rdev,struct device * dev,const char * supply_name)1627*4882a593Smuzhiyun static struct regulator *create_regulator(struct regulator_dev *rdev,
1628*4882a593Smuzhiyun 					  struct device *dev,
1629*4882a593Smuzhiyun 					  const char *supply_name)
1630*4882a593Smuzhiyun {
1631*4882a593Smuzhiyun 	struct regulator *regulator;
1632*4882a593Smuzhiyun 	int err = 0;
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun 	if (dev) {
1635*4882a593Smuzhiyun 		char buf[REG_STR_SIZE];
1636*4882a593Smuzhiyun 		int size;
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun 		size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1639*4882a593Smuzhiyun 				dev->kobj.name, supply_name);
1640*4882a593Smuzhiyun 		if (size >= REG_STR_SIZE)
1641*4882a593Smuzhiyun 			return NULL;
1642*4882a593Smuzhiyun 
1643*4882a593Smuzhiyun 		supply_name = kstrdup(buf, GFP_KERNEL);
1644*4882a593Smuzhiyun 		if (supply_name == NULL)
1645*4882a593Smuzhiyun 			return NULL;
1646*4882a593Smuzhiyun 	} else {
1647*4882a593Smuzhiyun 		supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1648*4882a593Smuzhiyun 		if (supply_name == NULL)
1649*4882a593Smuzhiyun 			return NULL;
1650*4882a593Smuzhiyun 	}
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun 	regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1653*4882a593Smuzhiyun 	if (regulator == NULL) {
1654*4882a593Smuzhiyun 		kfree(supply_name);
1655*4882a593Smuzhiyun 		return NULL;
1656*4882a593Smuzhiyun 	}
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun 	regulator->rdev = rdev;
1659*4882a593Smuzhiyun 	regulator->supply_name = supply_name;
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 	regulator_lock(rdev);
1662*4882a593Smuzhiyun 	list_add(&regulator->list, &rdev->consumer_list);
1663*4882a593Smuzhiyun 	regulator_unlock(rdev);
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun 	if (dev) {
1666*4882a593Smuzhiyun 		regulator->dev = dev;
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun 		/* Add a link to the device sysfs entry */
1669*4882a593Smuzhiyun 		err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1670*4882a593Smuzhiyun 					       supply_name);
1671*4882a593Smuzhiyun 		if (err) {
1672*4882a593Smuzhiyun 			rdev_dbg(rdev, "could not add device link %s: %pe\n",
1673*4882a593Smuzhiyun 				  dev->kobj.name, ERR_PTR(err));
1674*4882a593Smuzhiyun 			/* non-fatal */
1675*4882a593Smuzhiyun 		}
1676*4882a593Smuzhiyun 	}
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 	if (err != -EEXIST)
1679*4882a593Smuzhiyun 		regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1680*4882a593Smuzhiyun 	if (!regulator->debugfs) {
1681*4882a593Smuzhiyun 		rdev_dbg(rdev, "Failed to create debugfs directory\n");
1682*4882a593Smuzhiyun 	} else {
1683*4882a593Smuzhiyun 		debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1684*4882a593Smuzhiyun 				   &regulator->uA_load);
1685*4882a593Smuzhiyun 		debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1686*4882a593Smuzhiyun 				   &regulator->voltage[PM_SUSPEND_ON].min_uV);
1687*4882a593Smuzhiyun 		debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1688*4882a593Smuzhiyun 				   &regulator->voltage[PM_SUSPEND_ON].max_uV);
1689*4882a593Smuzhiyun 		debugfs_create_file("constraint_flags", 0444,
1690*4882a593Smuzhiyun 				    regulator->debugfs, regulator,
1691*4882a593Smuzhiyun 				    &constraint_flags_fops);
1692*4882a593Smuzhiyun 	}
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 	/*
1695*4882a593Smuzhiyun 	 * Check now if the regulator is an always on regulator - if
1696*4882a593Smuzhiyun 	 * it is then we don't need to do nearly so much work for
1697*4882a593Smuzhiyun 	 * enable/disable calls.
1698*4882a593Smuzhiyun 	 */
1699*4882a593Smuzhiyun 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1700*4882a593Smuzhiyun 	    _regulator_is_enabled(rdev))
1701*4882a593Smuzhiyun 		regulator->always_on = true;
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun 	return regulator;
1704*4882a593Smuzhiyun }
1705*4882a593Smuzhiyun 
_regulator_get_enable_time(struct regulator_dev * rdev)1706*4882a593Smuzhiyun static int _regulator_get_enable_time(struct regulator_dev *rdev)
1707*4882a593Smuzhiyun {
1708*4882a593Smuzhiyun 	if (rdev->constraints && rdev->constraints->enable_time)
1709*4882a593Smuzhiyun 		return rdev->constraints->enable_time;
1710*4882a593Smuzhiyun 	if (rdev->desc->ops->enable_time)
1711*4882a593Smuzhiyun 		return rdev->desc->ops->enable_time(rdev);
1712*4882a593Smuzhiyun 	return rdev->desc->enable_time;
1713*4882a593Smuzhiyun }
1714*4882a593Smuzhiyun 
regulator_find_supply_alias(struct device * dev,const char * supply)1715*4882a593Smuzhiyun static struct regulator_supply_alias *regulator_find_supply_alias(
1716*4882a593Smuzhiyun 		struct device *dev, const char *supply)
1717*4882a593Smuzhiyun {
1718*4882a593Smuzhiyun 	struct regulator_supply_alias *map;
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun 	list_for_each_entry(map, &regulator_supply_alias_list, list)
1721*4882a593Smuzhiyun 		if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1722*4882a593Smuzhiyun 			return map;
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun 	return NULL;
1725*4882a593Smuzhiyun }
1726*4882a593Smuzhiyun 
regulator_supply_alias(struct device ** dev,const char ** supply)1727*4882a593Smuzhiyun static void regulator_supply_alias(struct device **dev, const char **supply)
1728*4882a593Smuzhiyun {
1729*4882a593Smuzhiyun 	struct regulator_supply_alias *map;
1730*4882a593Smuzhiyun 
1731*4882a593Smuzhiyun 	map = regulator_find_supply_alias(*dev, *supply);
1732*4882a593Smuzhiyun 	if (map) {
1733*4882a593Smuzhiyun 		dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1734*4882a593Smuzhiyun 				*supply, map->alias_supply,
1735*4882a593Smuzhiyun 				dev_name(map->alias_dev));
1736*4882a593Smuzhiyun 		*dev = map->alias_dev;
1737*4882a593Smuzhiyun 		*supply = map->alias_supply;
1738*4882a593Smuzhiyun 	}
1739*4882a593Smuzhiyun }
1740*4882a593Smuzhiyun 
regulator_match(struct device * dev,const void * data)1741*4882a593Smuzhiyun static int regulator_match(struct device *dev, const void *data)
1742*4882a593Smuzhiyun {
1743*4882a593Smuzhiyun 	struct regulator_dev *r = dev_to_rdev(dev);
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun 	return strcmp(rdev_get_name(r), data) == 0;
1746*4882a593Smuzhiyun }
1747*4882a593Smuzhiyun 
regulator_lookup_by_name(const char * name)1748*4882a593Smuzhiyun static struct regulator_dev *regulator_lookup_by_name(const char *name)
1749*4882a593Smuzhiyun {
1750*4882a593Smuzhiyun 	struct device *dev;
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun 	dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1753*4882a593Smuzhiyun 
1754*4882a593Smuzhiyun 	return dev ? dev_to_rdev(dev) : NULL;
1755*4882a593Smuzhiyun }
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun /**
1758*4882a593Smuzhiyun  * regulator_dev_lookup - lookup a regulator device.
1759*4882a593Smuzhiyun  * @dev: device for regulator "consumer".
1760*4882a593Smuzhiyun  * @supply: Supply name or regulator ID.
1761*4882a593Smuzhiyun  *
1762*4882a593Smuzhiyun  * If successful, returns a struct regulator_dev that corresponds to the name
1763*4882a593Smuzhiyun  * @supply and with the embedded struct device refcount incremented by one.
1764*4882a593Smuzhiyun  * The refcount must be dropped by calling put_device().
1765*4882a593Smuzhiyun  * On failure one of the following ERR-PTR-encoded values is returned:
1766*4882a593Smuzhiyun  * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1767*4882a593Smuzhiyun  * in the future.
1768*4882a593Smuzhiyun  */
regulator_dev_lookup(struct device * dev,const char * supply)1769*4882a593Smuzhiyun static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1770*4882a593Smuzhiyun 						  const char *supply)
1771*4882a593Smuzhiyun {
1772*4882a593Smuzhiyun 	struct regulator_dev *r = NULL;
1773*4882a593Smuzhiyun 	struct device_node *node;
1774*4882a593Smuzhiyun 	struct regulator_map *map;
1775*4882a593Smuzhiyun 	const char *devname = NULL;
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun 	regulator_supply_alias(&dev, &supply);
1778*4882a593Smuzhiyun 
1779*4882a593Smuzhiyun 	/* first do a dt based lookup */
1780*4882a593Smuzhiyun 	if (dev && dev->of_node) {
1781*4882a593Smuzhiyun 		node = of_get_regulator(dev, supply);
1782*4882a593Smuzhiyun 		if (node) {
1783*4882a593Smuzhiyun 			r = of_find_regulator_by_node(node);
1784*4882a593Smuzhiyun 			if (r)
1785*4882a593Smuzhiyun 				return r;
1786*4882a593Smuzhiyun 
1787*4882a593Smuzhiyun 			/*
1788*4882a593Smuzhiyun 			 * We have a node, but there is no device.
1789*4882a593Smuzhiyun 			 * assume it has not registered yet.
1790*4882a593Smuzhiyun 			 */
1791*4882a593Smuzhiyun 			return ERR_PTR(-EPROBE_DEFER);
1792*4882a593Smuzhiyun 		}
1793*4882a593Smuzhiyun 	}
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun 	/* if not found, try doing it non-dt way */
1796*4882a593Smuzhiyun 	if (dev)
1797*4882a593Smuzhiyun 		devname = dev_name(dev);
1798*4882a593Smuzhiyun 
1799*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
1800*4882a593Smuzhiyun 	list_for_each_entry(map, &regulator_map_list, list) {
1801*4882a593Smuzhiyun 		/* If the mapping has a device set up it must match */
1802*4882a593Smuzhiyun 		if (map->dev_name &&
1803*4882a593Smuzhiyun 		    (!devname || strcmp(map->dev_name, devname)))
1804*4882a593Smuzhiyun 			continue;
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 		if (strcmp(map->supply, supply) == 0 &&
1807*4882a593Smuzhiyun 		    get_device(&map->regulator->dev)) {
1808*4882a593Smuzhiyun 			r = map->regulator;
1809*4882a593Smuzhiyun 			break;
1810*4882a593Smuzhiyun 		}
1811*4882a593Smuzhiyun 	}
1812*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun 	if (r)
1815*4882a593Smuzhiyun 		return r;
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun 	r = regulator_lookup_by_name(supply);
1818*4882a593Smuzhiyun 	if (r)
1819*4882a593Smuzhiyun 		return r;
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun 	return ERR_PTR(-ENODEV);
1822*4882a593Smuzhiyun }
1823*4882a593Smuzhiyun 
regulator_resolve_supply(struct regulator_dev * rdev)1824*4882a593Smuzhiyun static int regulator_resolve_supply(struct regulator_dev *rdev)
1825*4882a593Smuzhiyun {
1826*4882a593Smuzhiyun 	struct regulator_dev *r;
1827*4882a593Smuzhiyun 	struct device *dev = rdev->dev.parent;
1828*4882a593Smuzhiyun 	int ret = 0;
1829*4882a593Smuzhiyun 
1830*4882a593Smuzhiyun 	/* No supply to resolve? */
1831*4882a593Smuzhiyun 	if (!rdev->supply_name)
1832*4882a593Smuzhiyun 		return 0;
1833*4882a593Smuzhiyun 
1834*4882a593Smuzhiyun 	/* Supply already resolved? (fast-path without locking contention) */
1835*4882a593Smuzhiyun 	if (rdev->supply)
1836*4882a593Smuzhiyun 		return 0;
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 	r = regulator_dev_lookup(dev, rdev->supply_name);
1839*4882a593Smuzhiyun 	if (IS_ERR(r)) {
1840*4882a593Smuzhiyun 		ret = PTR_ERR(r);
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 		/* Did the lookup explicitly defer for us? */
1843*4882a593Smuzhiyun 		if (ret == -EPROBE_DEFER)
1844*4882a593Smuzhiyun 			goto out;
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 		if (have_full_constraints()) {
1847*4882a593Smuzhiyun 			r = dummy_regulator_rdev;
1848*4882a593Smuzhiyun 			get_device(&r->dev);
1849*4882a593Smuzhiyun 		} else {
1850*4882a593Smuzhiyun 			dev_err(dev, "Failed to resolve %s-supply for %s\n",
1851*4882a593Smuzhiyun 				rdev->supply_name, rdev->desc->name);
1852*4882a593Smuzhiyun 			ret = -EPROBE_DEFER;
1853*4882a593Smuzhiyun 			goto out;
1854*4882a593Smuzhiyun 		}
1855*4882a593Smuzhiyun 	}
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun 	if (r == rdev) {
1858*4882a593Smuzhiyun 		dev_err(dev, "Supply for %s (%s) resolved to itself\n",
1859*4882a593Smuzhiyun 			rdev->desc->name, rdev->supply_name);
1860*4882a593Smuzhiyun 		if (!have_full_constraints()) {
1861*4882a593Smuzhiyun 			ret = -EINVAL;
1862*4882a593Smuzhiyun 			goto out;
1863*4882a593Smuzhiyun 		}
1864*4882a593Smuzhiyun 		r = dummy_regulator_rdev;
1865*4882a593Smuzhiyun 		get_device(&r->dev);
1866*4882a593Smuzhiyun 	}
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 	/*
1869*4882a593Smuzhiyun 	 * If the supply's parent device is not the same as the
1870*4882a593Smuzhiyun 	 * regulator's parent device, then ensure the parent device
1871*4882a593Smuzhiyun 	 * is bound before we resolve the supply, in case the parent
1872*4882a593Smuzhiyun 	 * device get probe deferred and unregisters the supply.
1873*4882a593Smuzhiyun 	 */
1874*4882a593Smuzhiyun 	if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1875*4882a593Smuzhiyun 		if (!device_is_bound(r->dev.parent)) {
1876*4882a593Smuzhiyun 			put_device(&r->dev);
1877*4882a593Smuzhiyun 			ret = -EPROBE_DEFER;
1878*4882a593Smuzhiyun 			goto out;
1879*4882a593Smuzhiyun 		}
1880*4882a593Smuzhiyun 	}
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun 	/* Recursively resolve the supply of the supply */
1883*4882a593Smuzhiyun 	ret = regulator_resolve_supply(r);
1884*4882a593Smuzhiyun 	if (ret < 0) {
1885*4882a593Smuzhiyun 		put_device(&r->dev);
1886*4882a593Smuzhiyun 		goto out;
1887*4882a593Smuzhiyun 	}
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun 	/*
1890*4882a593Smuzhiyun 	 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
1891*4882a593Smuzhiyun 	 * between rdev->supply null check and setting rdev->supply in
1892*4882a593Smuzhiyun 	 * set_supply() from concurrent tasks.
1893*4882a593Smuzhiyun 	 */
1894*4882a593Smuzhiyun 	regulator_lock(rdev);
1895*4882a593Smuzhiyun 
1896*4882a593Smuzhiyun 	/* Supply just resolved by a concurrent task? */
1897*4882a593Smuzhiyun 	if (rdev->supply) {
1898*4882a593Smuzhiyun 		regulator_unlock(rdev);
1899*4882a593Smuzhiyun 		put_device(&r->dev);
1900*4882a593Smuzhiyun 		goto out;
1901*4882a593Smuzhiyun 	}
1902*4882a593Smuzhiyun 
1903*4882a593Smuzhiyun 	ret = set_supply(rdev, r);
1904*4882a593Smuzhiyun 	if (ret < 0) {
1905*4882a593Smuzhiyun 		regulator_unlock(rdev);
1906*4882a593Smuzhiyun 		put_device(&r->dev);
1907*4882a593Smuzhiyun 		goto out;
1908*4882a593Smuzhiyun 	}
1909*4882a593Smuzhiyun 
1910*4882a593Smuzhiyun 	regulator_unlock(rdev);
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun 	/*
1913*4882a593Smuzhiyun 	 * In set_machine_constraints() we may have turned this regulator on
1914*4882a593Smuzhiyun 	 * but we couldn't propagate to the supply if it hadn't been resolved
1915*4882a593Smuzhiyun 	 * yet.  Do it now.
1916*4882a593Smuzhiyun 	 */
1917*4882a593Smuzhiyun 	if (rdev->use_count) {
1918*4882a593Smuzhiyun 		ret = regulator_enable(rdev->supply);
1919*4882a593Smuzhiyun 		if (ret < 0) {
1920*4882a593Smuzhiyun 			_regulator_put(rdev->supply);
1921*4882a593Smuzhiyun 			rdev->supply = NULL;
1922*4882a593Smuzhiyun 			goto out;
1923*4882a593Smuzhiyun 		}
1924*4882a593Smuzhiyun 	}
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun out:
1927*4882a593Smuzhiyun 	return ret;
1928*4882a593Smuzhiyun }
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun /* Internal regulator request function */
_regulator_get(struct device * dev,const char * id,enum regulator_get_type get_type)1931*4882a593Smuzhiyun struct regulator *_regulator_get(struct device *dev, const char *id,
1932*4882a593Smuzhiyun 				 enum regulator_get_type get_type)
1933*4882a593Smuzhiyun {
1934*4882a593Smuzhiyun 	struct regulator_dev *rdev;
1935*4882a593Smuzhiyun 	struct regulator *regulator;
1936*4882a593Smuzhiyun 	struct device_link *link;
1937*4882a593Smuzhiyun 	int ret;
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	if (get_type >= MAX_GET_TYPE) {
1940*4882a593Smuzhiyun 		dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1941*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
1942*4882a593Smuzhiyun 	}
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun 	if (id == NULL) {
1945*4882a593Smuzhiyun 		pr_err("get() with no identifier\n");
1946*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
1947*4882a593Smuzhiyun 	}
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun 	rdev = regulator_dev_lookup(dev, id);
1950*4882a593Smuzhiyun 	if (IS_ERR(rdev)) {
1951*4882a593Smuzhiyun 		ret = PTR_ERR(rdev);
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 		/*
1954*4882a593Smuzhiyun 		 * If regulator_dev_lookup() fails with error other
1955*4882a593Smuzhiyun 		 * than -ENODEV our job here is done, we simply return it.
1956*4882a593Smuzhiyun 		 */
1957*4882a593Smuzhiyun 		if (ret != -ENODEV)
1958*4882a593Smuzhiyun 			return ERR_PTR(ret);
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 		if (!have_full_constraints()) {
1961*4882a593Smuzhiyun 			dev_warn(dev,
1962*4882a593Smuzhiyun 				 "incomplete constraints, dummy supplies not allowed\n");
1963*4882a593Smuzhiyun 			return ERR_PTR(-ENODEV);
1964*4882a593Smuzhiyun 		}
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun 		switch (get_type) {
1967*4882a593Smuzhiyun 		case NORMAL_GET:
1968*4882a593Smuzhiyun 			/*
1969*4882a593Smuzhiyun 			 * Assume that a regulator is physically present and
1970*4882a593Smuzhiyun 			 * enabled, even if it isn't hooked up, and just
1971*4882a593Smuzhiyun 			 * provide a dummy.
1972*4882a593Smuzhiyun 			 */
1973*4882a593Smuzhiyun 			dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
1974*4882a593Smuzhiyun 			rdev = dummy_regulator_rdev;
1975*4882a593Smuzhiyun 			get_device(&rdev->dev);
1976*4882a593Smuzhiyun 			break;
1977*4882a593Smuzhiyun 
1978*4882a593Smuzhiyun 		case EXCLUSIVE_GET:
1979*4882a593Smuzhiyun 			dev_warn(dev,
1980*4882a593Smuzhiyun 				 "dummy supplies not allowed for exclusive requests\n");
1981*4882a593Smuzhiyun 			fallthrough;
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 		default:
1984*4882a593Smuzhiyun 			return ERR_PTR(-ENODEV);
1985*4882a593Smuzhiyun 		}
1986*4882a593Smuzhiyun 	}
1987*4882a593Smuzhiyun 
1988*4882a593Smuzhiyun 	if (rdev->exclusive) {
1989*4882a593Smuzhiyun 		regulator = ERR_PTR(-EPERM);
1990*4882a593Smuzhiyun 		put_device(&rdev->dev);
1991*4882a593Smuzhiyun 		return regulator;
1992*4882a593Smuzhiyun 	}
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun 	if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1995*4882a593Smuzhiyun 		regulator = ERR_PTR(-EBUSY);
1996*4882a593Smuzhiyun 		put_device(&rdev->dev);
1997*4882a593Smuzhiyun 		return regulator;
1998*4882a593Smuzhiyun 	}
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
2001*4882a593Smuzhiyun 	ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
2002*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
2003*4882a593Smuzhiyun 
2004*4882a593Smuzhiyun 	if (ret != 0) {
2005*4882a593Smuzhiyun 		regulator = ERR_PTR(-EPROBE_DEFER);
2006*4882a593Smuzhiyun 		put_device(&rdev->dev);
2007*4882a593Smuzhiyun 		return regulator;
2008*4882a593Smuzhiyun 	}
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun 	ret = regulator_resolve_supply(rdev);
2011*4882a593Smuzhiyun 	if (ret < 0) {
2012*4882a593Smuzhiyun 		regulator = ERR_PTR(ret);
2013*4882a593Smuzhiyun 		put_device(&rdev->dev);
2014*4882a593Smuzhiyun 		return regulator;
2015*4882a593Smuzhiyun 	}
2016*4882a593Smuzhiyun 
2017*4882a593Smuzhiyun 	if (!try_module_get(rdev->owner)) {
2018*4882a593Smuzhiyun 		regulator = ERR_PTR(-EPROBE_DEFER);
2019*4882a593Smuzhiyun 		put_device(&rdev->dev);
2020*4882a593Smuzhiyun 		return regulator;
2021*4882a593Smuzhiyun 	}
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun 	regulator = create_regulator(rdev, dev, id);
2024*4882a593Smuzhiyun 	if (regulator == NULL) {
2025*4882a593Smuzhiyun 		regulator = ERR_PTR(-ENOMEM);
2026*4882a593Smuzhiyun 		module_put(rdev->owner);
2027*4882a593Smuzhiyun 		put_device(&rdev->dev);
2028*4882a593Smuzhiyun 		return regulator;
2029*4882a593Smuzhiyun 	}
2030*4882a593Smuzhiyun 
2031*4882a593Smuzhiyun 	rdev->open_count++;
2032*4882a593Smuzhiyun 	if (get_type == EXCLUSIVE_GET) {
2033*4882a593Smuzhiyun 		rdev->exclusive = 1;
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 		ret = _regulator_is_enabled(rdev);
2036*4882a593Smuzhiyun 		if (ret > 0) {
2037*4882a593Smuzhiyun 			rdev->use_count = 1;
2038*4882a593Smuzhiyun 			regulator->enable_count = 1;
2039*4882a593Smuzhiyun 		} else {
2040*4882a593Smuzhiyun 			rdev->use_count = 0;
2041*4882a593Smuzhiyun 			regulator->enable_count = 0;
2042*4882a593Smuzhiyun 		}
2043*4882a593Smuzhiyun 	}
2044*4882a593Smuzhiyun 
2045*4882a593Smuzhiyun 	link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2046*4882a593Smuzhiyun 	if (!IS_ERR_OR_NULL(link))
2047*4882a593Smuzhiyun 		regulator->device_link = true;
2048*4882a593Smuzhiyun 
2049*4882a593Smuzhiyun 	return regulator;
2050*4882a593Smuzhiyun }
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun /**
2053*4882a593Smuzhiyun  * regulator_get - lookup and obtain a reference to a regulator.
2054*4882a593Smuzhiyun  * @dev: device for regulator "consumer"
2055*4882a593Smuzhiyun  * @id: Supply name or regulator ID.
2056*4882a593Smuzhiyun  *
2057*4882a593Smuzhiyun  * Returns a struct regulator corresponding to the regulator producer,
2058*4882a593Smuzhiyun  * or IS_ERR() condition containing errno.
2059*4882a593Smuzhiyun  *
2060*4882a593Smuzhiyun  * Use of supply names configured via regulator_set_device_supply() is
2061*4882a593Smuzhiyun  * strongly encouraged.  It is recommended that the supply name used
2062*4882a593Smuzhiyun  * should match the name used for the supply and/or the relevant
2063*4882a593Smuzhiyun  * device pins in the datasheet.
2064*4882a593Smuzhiyun  */
regulator_get(struct device * dev,const char * id)2065*4882a593Smuzhiyun struct regulator *regulator_get(struct device *dev, const char *id)
2066*4882a593Smuzhiyun {
2067*4882a593Smuzhiyun 	return _regulator_get(dev, id, NORMAL_GET);
2068*4882a593Smuzhiyun }
2069*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get);
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun /**
2072*4882a593Smuzhiyun  * regulator_get_exclusive - obtain exclusive access to a regulator.
2073*4882a593Smuzhiyun  * @dev: device for regulator "consumer"
2074*4882a593Smuzhiyun  * @id: Supply name or regulator ID.
2075*4882a593Smuzhiyun  *
2076*4882a593Smuzhiyun  * Returns a struct regulator corresponding to the regulator producer,
2077*4882a593Smuzhiyun  * or IS_ERR() condition containing errno.  Other consumers will be
2078*4882a593Smuzhiyun  * unable to obtain this regulator while this reference is held and the
2079*4882a593Smuzhiyun  * use count for the regulator will be initialised to reflect the current
2080*4882a593Smuzhiyun  * state of the regulator.
2081*4882a593Smuzhiyun  *
2082*4882a593Smuzhiyun  * This is intended for use by consumers which cannot tolerate shared
2083*4882a593Smuzhiyun  * use of the regulator such as those which need to force the
2084*4882a593Smuzhiyun  * regulator off for correct operation of the hardware they are
2085*4882a593Smuzhiyun  * controlling.
2086*4882a593Smuzhiyun  *
2087*4882a593Smuzhiyun  * Use of supply names configured via regulator_set_device_supply() is
2088*4882a593Smuzhiyun  * strongly encouraged.  It is recommended that the supply name used
2089*4882a593Smuzhiyun  * should match the name used for the supply and/or the relevant
2090*4882a593Smuzhiyun  * device pins in the datasheet.
2091*4882a593Smuzhiyun  */
regulator_get_exclusive(struct device * dev,const char * id)2092*4882a593Smuzhiyun struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2093*4882a593Smuzhiyun {
2094*4882a593Smuzhiyun 	return _regulator_get(dev, id, EXCLUSIVE_GET);
2095*4882a593Smuzhiyun }
2096*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2097*4882a593Smuzhiyun 
2098*4882a593Smuzhiyun /**
2099*4882a593Smuzhiyun  * regulator_get_optional - obtain optional access to a regulator.
2100*4882a593Smuzhiyun  * @dev: device for regulator "consumer"
2101*4882a593Smuzhiyun  * @id: Supply name or regulator ID.
2102*4882a593Smuzhiyun  *
2103*4882a593Smuzhiyun  * Returns a struct regulator corresponding to the regulator producer,
2104*4882a593Smuzhiyun  * or IS_ERR() condition containing errno.
2105*4882a593Smuzhiyun  *
2106*4882a593Smuzhiyun  * This is intended for use by consumers for devices which can have
2107*4882a593Smuzhiyun  * some supplies unconnected in normal use, such as some MMC devices.
2108*4882a593Smuzhiyun  * It can allow the regulator core to provide stub supplies for other
2109*4882a593Smuzhiyun  * supplies requested using normal regulator_get() calls without
2110*4882a593Smuzhiyun  * disrupting the operation of drivers that can handle absent
2111*4882a593Smuzhiyun  * supplies.
2112*4882a593Smuzhiyun  *
2113*4882a593Smuzhiyun  * Use of supply names configured via regulator_set_device_supply() is
2114*4882a593Smuzhiyun  * strongly encouraged.  It is recommended that the supply name used
2115*4882a593Smuzhiyun  * should match the name used for the supply and/or the relevant
2116*4882a593Smuzhiyun  * device pins in the datasheet.
2117*4882a593Smuzhiyun  */
regulator_get_optional(struct device * dev,const char * id)2118*4882a593Smuzhiyun struct regulator *regulator_get_optional(struct device *dev, const char *id)
2119*4882a593Smuzhiyun {
2120*4882a593Smuzhiyun 	return _regulator_get(dev, id, OPTIONAL_GET);
2121*4882a593Smuzhiyun }
2122*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_optional);
2123*4882a593Smuzhiyun 
destroy_regulator(struct regulator * regulator)2124*4882a593Smuzhiyun static void destroy_regulator(struct regulator *regulator)
2125*4882a593Smuzhiyun {
2126*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
2127*4882a593Smuzhiyun 
2128*4882a593Smuzhiyun 	debugfs_remove_recursive(regulator->debugfs);
2129*4882a593Smuzhiyun 
2130*4882a593Smuzhiyun 	if (regulator->dev) {
2131*4882a593Smuzhiyun 		if (regulator->device_link)
2132*4882a593Smuzhiyun 			device_link_remove(regulator->dev, &rdev->dev);
2133*4882a593Smuzhiyun 
2134*4882a593Smuzhiyun 		/* remove any sysfs entries */
2135*4882a593Smuzhiyun 		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2136*4882a593Smuzhiyun 	}
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun 	regulator_lock(rdev);
2139*4882a593Smuzhiyun 	list_del(&regulator->list);
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun 	rdev->open_count--;
2142*4882a593Smuzhiyun 	rdev->exclusive = 0;
2143*4882a593Smuzhiyun 	regulator_unlock(rdev);
2144*4882a593Smuzhiyun 
2145*4882a593Smuzhiyun 	kfree_const(regulator->supply_name);
2146*4882a593Smuzhiyun 	kfree(regulator);
2147*4882a593Smuzhiyun }
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun /* regulator_list_mutex lock held by regulator_put() */
_regulator_put(struct regulator * regulator)2150*4882a593Smuzhiyun static void _regulator_put(struct regulator *regulator)
2151*4882a593Smuzhiyun {
2152*4882a593Smuzhiyun 	struct regulator_dev *rdev;
2153*4882a593Smuzhiyun 
2154*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(regulator))
2155*4882a593Smuzhiyun 		return;
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 	lockdep_assert_held_once(&regulator_list_mutex);
2158*4882a593Smuzhiyun 
2159*4882a593Smuzhiyun 	/* Docs say you must disable before calling regulator_put() */
2160*4882a593Smuzhiyun 	WARN_ON(regulator->enable_count);
2161*4882a593Smuzhiyun 
2162*4882a593Smuzhiyun 	rdev = regulator->rdev;
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	destroy_regulator(regulator);
2165*4882a593Smuzhiyun 
2166*4882a593Smuzhiyun 	module_put(rdev->owner);
2167*4882a593Smuzhiyun 	put_device(&rdev->dev);
2168*4882a593Smuzhiyun }
2169*4882a593Smuzhiyun 
2170*4882a593Smuzhiyun /**
2171*4882a593Smuzhiyun  * regulator_put - "free" the regulator source
2172*4882a593Smuzhiyun  * @regulator: regulator source
2173*4882a593Smuzhiyun  *
2174*4882a593Smuzhiyun  * Note: drivers must ensure that all regulator_enable calls made on this
2175*4882a593Smuzhiyun  * regulator source are balanced by regulator_disable calls prior to calling
2176*4882a593Smuzhiyun  * this function.
2177*4882a593Smuzhiyun  */
regulator_put(struct regulator * regulator)2178*4882a593Smuzhiyun void regulator_put(struct regulator *regulator)
2179*4882a593Smuzhiyun {
2180*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
2181*4882a593Smuzhiyun 	_regulator_put(regulator);
2182*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
2183*4882a593Smuzhiyun }
2184*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_put);
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun /**
2187*4882a593Smuzhiyun  * regulator_register_supply_alias - Provide device alias for supply lookup
2188*4882a593Smuzhiyun  *
2189*4882a593Smuzhiyun  * @dev: device that will be given as the regulator "consumer"
2190*4882a593Smuzhiyun  * @id: Supply name or regulator ID
2191*4882a593Smuzhiyun  * @alias_dev: device that should be used to lookup the supply
2192*4882a593Smuzhiyun  * @alias_id: Supply name or regulator ID that should be used to lookup the
2193*4882a593Smuzhiyun  * supply
2194*4882a593Smuzhiyun  *
2195*4882a593Smuzhiyun  * All lookups for id on dev will instead be conducted for alias_id on
2196*4882a593Smuzhiyun  * alias_dev.
2197*4882a593Smuzhiyun  */
regulator_register_supply_alias(struct device * dev,const char * id,struct device * alias_dev,const char * alias_id)2198*4882a593Smuzhiyun int regulator_register_supply_alias(struct device *dev, const char *id,
2199*4882a593Smuzhiyun 				    struct device *alias_dev,
2200*4882a593Smuzhiyun 				    const char *alias_id)
2201*4882a593Smuzhiyun {
2202*4882a593Smuzhiyun 	struct regulator_supply_alias *map;
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun 	map = regulator_find_supply_alias(dev, id);
2205*4882a593Smuzhiyun 	if (map)
2206*4882a593Smuzhiyun 		return -EEXIST;
2207*4882a593Smuzhiyun 
2208*4882a593Smuzhiyun 	map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2209*4882a593Smuzhiyun 	if (!map)
2210*4882a593Smuzhiyun 		return -ENOMEM;
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 	map->src_dev = dev;
2213*4882a593Smuzhiyun 	map->src_supply = id;
2214*4882a593Smuzhiyun 	map->alias_dev = alias_dev;
2215*4882a593Smuzhiyun 	map->alias_supply = alias_id;
2216*4882a593Smuzhiyun 
2217*4882a593Smuzhiyun 	list_add(&map->list, &regulator_supply_alias_list);
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun 	pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2220*4882a593Smuzhiyun 		id, dev_name(dev), alias_id, dev_name(alias_dev));
2221*4882a593Smuzhiyun 
2222*4882a593Smuzhiyun 	return 0;
2223*4882a593Smuzhiyun }
2224*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2225*4882a593Smuzhiyun 
2226*4882a593Smuzhiyun /**
2227*4882a593Smuzhiyun  * regulator_unregister_supply_alias - Remove device alias
2228*4882a593Smuzhiyun  *
2229*4882a593Smuzhiyun  * @dev: device that will be given as the regulator "consumer"
2230*4882a593Smuzhiyun  * @id: Supply name or regulator ID
2231*4882a593Smuzhiyun  *
2232*4882a593Smuzhiyun  * Remove a lookup alias if one exists for id on dev.
2233*4882a593Smuzhiyun  */
regulator_unregister_supply_alias(struct device * dev,const char * id)2234*4882a593Smuzhiyun void regulator_unregister_supply_alias(struct device *dev, const char *id)
2235*4882a593Smuzhiyun {
2236*4882a593Smuzhiyun 	struct regulator_supply_alias *map;
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 	map = regulator_find_supply_alias(dev, id);
2239*4882a593Smuzhiyun 	if (map) {
2240*4882a593Smuzhiyun 		list_del(&map->list);
2241*4882a593Smuzhiyun 		kfree(map);
2242*4882a593Smuzhiyun 	}
2243*4882a593Smuzhiyun }
2244*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2245*4882a593Smuzhiyun 
2246*4882a593Smuzhiyun /**
2247*4882a593Smuzhiyun  * regulator_bulk_register_supply_alias - register multiple aliases
2248*4882a593Smuzhiyun  *
2249*4882a593Smuzhiyun  * @dev: device that will be given as the regulator "consumer"
2250*4882a593Smuzhiyun  * @id: List of supply names or regulator IDs
2251*4882a593Smuzhiyun  * @alias_dev: device that should be used to lookup the supply
2252*4882a593Smuzhiyun  * @alias_id: List of supply names or regulator IDs that should be used to
2253*4882a593Smuzhiyun  * lookup the supply
2254*4882a593Smuzhiyun  * @num_id: Number of aliases to register
2255*4882a593Smuzhiyun  *
2256*4882a593Smuzhiyun  * @return 0 on success, an errno on failure.
2257*4882a593Smuzhiyun  *
2258*4882a593Smuzhiyun  * This helper function allows drivers to register several supply
2259*4882a593Smuzhiyun  * aliases in one operation.  If any of the aliases cannot be
2260*4882a593Smuzhiyun  * registered any aliases that were registered will be removed
2261*4882a593Smuzhiyun  * before returning to the caller.
2262*4882a593Smuzhiyun  */
regulator_bulk_register_supply_alias(struct device * dev,const char * const * id,struct device * alias_dev,const char * const * alias_id,int num_id)2263*4882a593Smuzhiyun int regulator_bulk_register_supply_alias(struct device *dev,
2264*4882a593Smuzhiyun 					 const char *const *id,
2265*4882a593Smuzhiyun 					 struct device *alias_dev,
2266*4882a593Smuzhiyun 					 const char *const *alias_id,
2267*4882a593Smuzhiyun 					 int num_id)
2268*4882a593Smuzhiyun {
2269*4882a593Smuzhiyun 	int i;
2270*4882a593Smuzhiyun 	int ret;
2271*4882a593Smuzhiyun 
2272*4882a593Smuzhiyun 	for (i = 0; i < num_id; ++i) {
2273*4882a593Smuzhiyun 		ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2274*4882a593Smuzhiyun 						      alias_id[i]);
2275*4882a593Smuzhiyun 		if (ret < 0)
2276*4882a593Smuzhiyun 			goto err;
2277*4882a593Smuzhiyun 	}
2278*4882a593Smuzhiyun 
2279*4882a593Smuzhiyun 	return 0;
2280*4882a593Smuzhiyun 
2281*4882a593Smuzhiyun err:
2282*4882a593Smuzhiyun 	dev_err(dev,
2283*4882a593Smuzhiyun 		"Failed to create supply alias %s,%s -> %s,%s\n",
2284*4882a593Smuzhiyun 		id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2285*4882a593Smuzhiyun 
2286*4882a593Smuzhiyun 	while (--i >= 0)
2287*4882a593Smuzhiyun 		regulator_unregister_supply_alias(dev, id[i]);
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 	return ret;
2290*4882a593Smuzhiyun }
2291*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun /**
2294*4882a593Smuzhiyun  * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2295*4882a593Smuzhiyun  *
2296*4882a593Smuzhiyun  * @dev: device that will be given as the regulator "consumer"
2297*4882a593Smuzhiyun  * @id: List of supply names or regulator IDs
2298*4882a593Smuzhiyun  * @num_id: Number of aliases to unregister
2299*4882a593Smuzhiyun  *
2300*4882a593Smuzhiyun  * This helper function allows drivers to unregister several supply
2301*4882a593Smuzhiyun  * aliases in one operation.
2302*4882a593Smuzhiyun  */
regulator_bulk_unregister_supply_alias(struct device * dev,const char * const * id,int num_id)2303*4882a593Smuzhiyun void regulator_bulk_unregister_supply_alias(struct device *dev,
2304*4882a593Smuzhiyun 					    const char *const *id,
2305*4882a593Smuzhiyun 					    int num_id)
2306*4882a593Smuzhiyun {
2307*4882a593Smuzhiyun 	int i;
2308*4882a593Smuzhiyun 
2309*4882a593Smuzhiyun 	for (i = 0; i < num_id; ++i)
2310*4882a593Smuzhiyun 		regulator_unregister_supply_alias(dev, id[i]);
2311*4882a593Smuzhiyun }
2312*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun 
2315*4882a593Smuzhiyun /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
regulator_ena_gpio_request(struct regulator_dev * rdev,const struct regulator_config * config)2316*4882a593Smuzhiyun static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2317*4882a593Smuzhiyun 				const struct regulator_config *config)
2318*4882a593Smuzhiyun {
2319*4882a593Smuzhiyun 	struct regulator_enable_gpio *pin, *new_pin;
2320*4882a593Smuzhiyun 	struct gpio_desc *gpiod;
2321*4882a593Smuzhiyun 
2322*4882a593Smuzhiyun 	gpiod = config->ena_gpiod;
2323*4882a593Smuzhiyun 	new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2324*4882a593Smuzhiyun 
2325*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
2326*4882a593Smuzhiyun 
2327*4882a593Smuzhiyun 	list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
2328*4882a593Smuzhiyun 		if (pin->gpiod == gpiod) {
2329*4882a593Smuzhiyun 			rdev_dbg(rdev, "GPIO is already used\n");
2330*4882a593Smuzhiyun 			goto update_ena_gpio_to_rdev;
2331*4882a593Smuzhiyun 		}
2332*4882a593Smuzhiyun 	}
2333*4882a593Smuzhiyun 
2334*4882a593Smuzhiyun 	if (new_pin == NULL) {
2335*4882a593Smuzhiyun 		mutex_unlock(&regulator_list_mutex);
2336*4882a593Smuzhiyun 		return -ENOMEM;
2337*4882a593Smuzhiyun 	}
2338*4882a593Smuzhiyun 
2339*4882a593Smuzhiyun 	pin = new_pin;
2340*4882a593Smuzhiyun 	new_pin = NULL;
2341*4882a593Smuzhiyun 
2342*4882a593Smuzhiyun 	pin->gpiod = gpiod;
2343*4882a593Smuzhiyun 	list_add(&pin->list, &regulator_ena_gpio_list);
2344*4882a593Smuzhiyun 
2345*4882a593Smuzhiyun update_ena_gpio_to_rdev:
2346*4882a593Smuzhiyun 	pin->request_count++;
2347*4882a593Smuzhiyun 	rdev->ena_pin = pin;
2348*4882a593Smuzhiyun 
2349*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
2350*4882a593Smuzhiyun 	kfree(new_pin);
2351*4882a593Smuzhiyun 
2352*4882a593Smuzhiyun 	return 0;
2353*4882a593Smuzhiyun }
2354*4882a593Smuzhiyun 
regulator_ena_gpio_free(struct regulator_dev * rdev)2355*4882a593Smuzhiyun static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2356*4882a593Smuzhiyun {
2357*4882a593Smuzhiyun 	struct regulator_enable_gpio *pin, *n;
2358*4882a593Smuzhiyun 
2359*4882a593Smuzhiyun 	if (!rdev->ena_pin)
2360*4882a593Smuzhiyun 		return;
2361*4882a593Smuzhiyun 
2362*4882a593Smuzhiyun 	/* Free the GPIO only in case of no use */
2363*4882a593Smuzhiyun 	list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
2364*4882a593Smuzhiyun 		if (pin != rdev->ena_pin)
2365*4882a593Smuzhiyun 			continue;
2366*4882a593Smuzhiyun 
2367*4882a593Smuzhiyun 		if (--pin->request_count)
2368*4882a593Smuzhiyun 			break;
2369*4882a593Smuzhiyun 
2370*4882a593Smuzhiyun 		gpiod_put(pin->gpiod);
2371*4882a593Smuzhiyun 		list_del(&pin->list);
2372*4882a593Smuzhiyun 		kfree(pin);
2373*4882a593Smuzhiyun 		break;
2374*4882a593Smuzhiyun 	}
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 	rdev->ena_pin = NULL;
2377*4882a593Smuzhiyun }
2378*4882a593Smuzhiyun 
2379*4882a593Smuzhiyun /**
2380*4882a593Smuzhiyun  * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2381*4882a593Smuzhiyun  * @rdev: regulator_dev structure
2382*4882a593Smuzhiyun  * @enable: enable GPIO at initial use?
2383*4882a593Smuzhiyun  *
2384*4882a593Smuzhiyun  * GPIO is enabled in case of initial use. (enable_count is 0)
2385*4882a593Smuzhiyun  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2386*4882a593Smuzhiyun  */
regulator_ena_gpio_ctrl(struct regulator_dev * rdev,bool enable)2387*4882a593Smuzhiyun static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2388*4882a593Smuzhiyun {
2389*4882a593Smuzhiyun 	struct regulator_enable_gpio *pin = rdev->ena_pin;
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun 	if (!pin)
2392*4882a593Smuzhiyun 		return -EINVAL;
2393*4882a593Smuzhiyun 
2394*4882a593Smuzhiyun 	if (enable) {
2395*4882a593Smuzhiyun 		/* Enable GPIO at initial use */
2396*4882a593Smuzhiyun 		if (pin->enable_count == 0)
2397*4882a593Smuzhiyun 			gpiod_set_value_cansleep(pin->gpiod, 1);
2398*4882a593Smuzhiyun 
2399*4882a593Smuzhiyun 		pin->enable_count++;
2400*4882a593Smuzhiyun 	} else {
2401*4882a593Smuzhiyun 		if (pin->enable_count > 1) {
2402*4882a593Smuzhiyun 			pin->enable_count--;
2403*4882a593Smuzhiyun 			return 0;
2404*4882a593Smuzhiyun 		}
2405*4882a593Smuzhiyun 
2406*4882a593Smuzhiyun 		/* Disable GPIO if not used */
2407*4882a593Smuzhiyun 		if (pin->enable_count <= 1) {
2408*4882a593Smuzhiyun 			gpiod_set_value_cansleep(pin->gpiod, 0);
2409*4882a593Smuzhiyun 			pin->enable_count = 0;
2410*4882a593Smuzhiyun 		}
2411*4882a593Smuzhiyun 	}
2412*4882a593Smuzhiyun 
2413*4882a593Smuzhiyun 	return 0;
2414*4882a593Smuzhiyun }
2415*4882a593Smuzhiyun 
2416*4882a593Smuzhiyun /**
2417*4882a593Smuzhiyun  * _regulator_enable_delay - a delay helper function
2418*4882a593Smuzhiyun  * @delay: time to delay in microseconds
2419*4882a593Smuzhiyun  *
2420*4882a593Smuzhiyun  * Delay for the requested amount of time as per the guidelines in:
2421*4882a593Smuzhiyun  *
2422*4882a593Smuzhiyun  *     Documentation/timers/timers-howto.rst
2423*4882a593Smuzhiyun  *
2424*4882a593Smuzhiyun  * The assumption here is that regulators will never be enabled in
2425*4882a593Smuzhiyun  * atomic context and therefore sleeping functions can be used.
2426*4882a593Smuzhiyun  */
_regulator_enable_delay(unsigned int delay)2427*4882a593Smuzhiyun static void _regulator_enable_delay(unsigned int delay)
2428*4882a593Smuzhiyun {
2429*4882a593Smuzhiyun 	unsigned int ms = delay / 1000;
2430*4882a593Smuzhiyun 	unsigned int us = delay % 1000;
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun 	if (ms > 0) {
2433*4882a593Smuzhiyun 		/*
2434*4882a593Smuzhiyun 		 * For small enough values, handle super-millisecond
2435*4882a593Smuzhiyun 		 * delays in the usleep_range() call below.
2436*4882a593Smuzhiyun 		 */
2437*4882a593Smuzhiyun 		if (ms < 20)
2438*4882a593Smuzhiyun 			us += ms * 1000;
2439*4882a593Smuzhiyun 		else
2440*4882a593Smuzhiyun 			msleep(ms);
2441*4882a593Smuzhiyun 	}
2442*4882a593Smuzhiyun 
2443*4882a593Smuzhiyun 	/*
2444*4882a593Smuzhiyun 	 * Give the scheduler some room to coalesce with any other
2445*4882a593Smuzhiyun 	 * wakeup sources. For delays shorter than 10 us, don't even
2446*4882a593Smuzhiyun 	 * bother setting up high-resolution timers and just busy-
2447*4882a593Smuzhiyun 	 * loop.
2448*4882a593Smuzhiyun 	 */
2449*4882a593Smuzhiyun 	if (us >= 10)
2450*4882a593Smuzhiyun 		usleep_range(us, us + 100);
2451*4882a593Smuzhiyun 	else
2452*4882a593Smuzhiyun 		udelay(us);
2453*4882a593Smuzhiyun }
2454*4882a593Smuzhiyun 
2455*4882a593Smuzhiyun /**
2456*4882a593Smuzhiyun  * _regulator_check_status_enabled
2457*4882a593Smuzhiyun  *
2458*4882a593Smuzhiyun  * A helper function to check if the regulator status can be interpreted
2459*4882a593Smuzhiyun  * as 'regulator is enabled'.
2460*4882a593Smuzhiyun  * @rdev: the regulator device to check
2461*4882a593Smuzhiyun  *
2462*4882a593Smuzhiyun  * Return:
2463*4882a593Smuzhiyun  * * 1			- if status shows regulator is in enabled state
2464*4882a593Smuzhiyun  * * 0			- if not enabled state
2465*4882a593Smuzhiyun  * * Error Value	- as received from ops->get_status()
2466*4882a593Smuzhiyun  */
_regulator_check_status_enabled(struct regulator_dev * rdev)2467*4882a593Smuzhiyun static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2468*4882a593Smuzhiyun {
2469*4882a593Smuzhiyun 	int ret = rdev->desc->ops->get_status(rdev);
2470*4882a593Smuzhiyun 
2471*4882a593Smuzhiyun 	if (ret < 0) {
2472*4882a593Smuzhiyun 		rdev_info(rdev, "get_status returned error: %d\n", ret);
2473*4882a593Smuzhiyun 		return ret;
2474*4882a593Smuzhiyun 	}
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun 	switch (ret) {
2477*4882a593Smuzhiyun 	case REGULATOR_STATUS_OFF:
2478*4882a593Smuzhiyun 	case REGULATOR_STATUS_ERROR:
2479*4882a593Smuzhiyun 	case REGULATOR_STATUS_UNDEFINED:
2480*4882a593Smuzhiyun 		return 0;
2481*4882a593Smuzhiyun 	default:
2482*4882a593Smuzhiyun 		return 1;
2483*4882a593Smuzhiyun 	}
2484*4882a593Smuzhiyun }
2485*4882a593Smuzhiyun 
_regulator_do_enable(struct regulator_dev * rdev)2486*4882a593Smuzhiyun static int _regulator_do_enable(struct regulator_dev *rdev)
2487*4882a593Smuzhiyun {
2488*4882a593Smuzhiyun 	int ret, delay;
2489*4882a593Smuzhiyun 
2490*4882a593Smuzhiyun 	/* Query before enabling in case configuration dependent.  */
2491*4882a593Smuzhiyun 	ret = _regulator_get_enable_time(rdev);
2492*4882a593Smuzhiyun 	if (ret >= 0) {
2493*4882a593Smuzhiyun 		delay = ret;
2494*4882a593Smuzhiyun 	} else {
2495*4882a593Smuzhiyun 		rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret));
2496*4882a593Smuzhiyun 		delay = 0;
2497*4882a593Smuzhiyun 	}
2498*4882a593Smuzhiyun 
2499*4882a593Smuzhiyun 	trace_regulator_enable(rdev_get_name(rdev));
2500*4882a593Smuzhiyun 
2501*4882a593Smuzhiyun 	if (rdev->desc->off_on_delay) {
2502*4882a593Smuzhiyun 		/* if needed, keep a distance of off_on_delay from last time
2503*4882a593Smuzhiyun 		 * this regulator was disabled.
2504*4882a593Smuzhiyun 		 */
2505*4882a593Smuzhiyun 		unsigned long start_jiffy = jiffies;
2506*4882a593Smuzhiyun 		unsigned long intended, max_delay, remaining;
2507*4882a593Smuzhiyun 
2508*4882a593Smuzhiyun 		max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2509*4882a593Smuzhiyun 		intended = rdev->last_off_jiffy + max_delay;
2510*4882a593Smuzhiyun 
2511*4882a593Smuzhiyun 		if (time_before(start_jiffy, intended)) {
2512*4882a593Smuzhiyun 			/* calc remaining jiffies to deal with one-time
2513*4882a593Smuzhiyun 			 * timer wrapping.
2514*4882a593Smuzhiyun 			 * in case of multiple timer wrapping, either it can be
2515*4882a593Smuzhiyun 			 * detected by out-of-range remaining, or it cannot be
2516*4882a593Smuzhiyun 			 * detected and we get a penalty of
2517*4882a593Smuzhiyun 			 * _regulator_enable_delay().
2518*4882a593Smuzhiyun 			 */
2519*4882a593Smuzhiyun 			remaining = intended - start_jiffy;
2520*4882a593Smuzhiyun 			if (remaining <= max_delay)
2521*4882a593Smuzhiyun 				_regulator_enable_delay(
2522*4882a593Smuzhiyun 						jiffies_to_usecs(remaining));
2523*4882a593Smuzhiyun 		}
2524*4882a593Smuzhiyun 	}
2525*4882a593Smuzhiyun 
2526*4882a593Smuzhiyun 	if (rdev->ena_pin) {
2527*4882a593Smuzhiyun 		if (!rdev->ena_gpio_state) {
2528*4882a593Smuzhiyun 			ret = regulator_ena_gpio_ctrl(rdev, true);
2529*4882a593Smuzhiyun 			if (ret < 0)
2530*4882a593Smuzhiyun 				return ret;
2531*4882a593Smuzhiyun 			rdev->ena_gpio_state = 1;
2532*4882a593Smuzhiyun 		}
2533*4882a593Smuzhiyun 	} else if (rdev->desc->ops->enable) {
2534*4882a593Smuzhiyun 		ret = rdev->desc->ops->enable(rdev);
2535*4882a593Smuzhiyun 		if (ret < 0)
2536*4882a593Smuzhiyun 			return ret;
2537*4882a593Smuzhiyun 	} else {
2538*4882a593Smuzhiyun 		return -EINVAL;
2539*4882a593Smuzhiyun 	}
2540*4882a593Smuzhiyun 
2541*4882a593Smuzhiyun 	/* Allow the regulator to ramp; it would be useful to extend
2542*4882a593Smuzhiyun 	 * this for bulk operations so that the regulators can ramp
2543*4882a593Smuzhiyun 	 * together.  */
2544*4882a593Smuzhiyun 	trace_regulator_enable_delay(rdev_get_name(rdev));
2545*4882a593Smuzhiyun 
2546*4882a593Smuzhiyun 	/* If poll_enabled_time is set, poll upto the delay calculated
2547*4882a593Smuzhiyun 	 * above, delaying poll_enabled_time uS to check if the regulator
2548*4882a593Smuzhiyun 	 * actually got enabled.
2549*4882a593Smuzhiyun 	 * If the regulator isn't enabled after enable_delay has
2550*4882a593Smuzhiyun 	 * expired, return -ETIMEDOUT.
2551*4882a593Smuzhiyun 	 */
2552*4882a593Smuzhiyun 	if (rdev->desc->poll_enabled_time) {
2553*4882a593Smuzhiyun 		int time_remaining = delay;
2554*4882a593Smuzhiyun 
2555*4882a593Smuzhiyun 		while (time_remaining > 0) {
2556*4882a593Smuzhiyun 			_regulator_enable_delay(rdev->desc->poll_enabled_time);
2557*4882a593Smuzhiyun 
2558*4882a593Smuzhiyun 			if (rdev->desc->ops->get_status) {
2559*4882a593Smuzhiyun 				ret = _regulator_check_status_enabled(rdev);
2560*4882a593Smuzhiyun 				if (ret < 0)
2561*4882a593Smuzhiyun 					return ret;
2562*4882a593Smuzhiyun 				else if (ret)
2563*4882a593Smuzhiyun 					break;
2564*4882a593Smuzhiyun 			} else if (rdev->desc->ops->is_enabled(rdev))
2565*4882a593Smuzhiyun 				break;
2566*4882a593Smuzhiyun 
2567*4882a593Smuzhiyun 			time_remaining -= rdev->desc->poll_enabled_time;
2568*4882a593Smuzhiyun 		}
2569*4882a593Smuzhiyun 
2570*4882a593Smuzhiyun 		if (time_remaining <= 0) {
2571*4882a593Smuzhiyun 			rdev_err(rdev, "Enabled check timed out\n");
2572*4882a593Smuzhiyun 			return -ETIMEDOUT;
2573*4882a593Smuzhiyun 		}
2574*4882a593Smuzhiyun 	} else {
2575*4882a593Smuzhiyun 		_regulator_enable_delay(delay);
2576*4882a593Smuzhiyun 	}
2577*4882a593Smuzhiyun 
2578*4882a593Smuzhiyun 	trace_regulator_enable_complete(rdev_get_name(rdev));
2579*4882a593Smuzhiyun 
2580*4882a593Smuzhiyun 	return 0;
2581*4882a593Smuzhiyun }
2582*4882a593Smuzhiyun 
2583*4882a593Smuzhiyun /**
2584*4882a593Smuzhiyun  * _regulator_handle_consumer_enable - handle that a consumer enabled
2585*4882a593Smuzhiyun  * @regulator: regulator source
2586*4882a593Smuzhiyun  *
2587*4882a593Smuzhiyun  * Some things on a regulator consumer (like the contribution towards total
2588*4882a593Smuzhiyun  * load on the regulator) only have an effect when the consumer wants the
2589*4882a593Smuzhiyun  * regulator enabled.  Explained in example with two consumers of the same
2590*4882a593Smuzhiyun  * regulator:
2591*4882a593Smuzhiyun  *   consumer A: set_load(100);       => total load = 0
2592*4882a593Smuzhiyun  *   consumer A: regulator_enable();  => total load = 100
2593*4882a593Smuzhiyun  *   consumer B: set_load(1000);      => total load = 100
2594*4882a593Smuzhiyun  *   consumer B: regulator_enable();  => total load = 1100
2595*4882a593Smuzhiyun  *   consumer A: regulator_disable(); => total_load = 1000
2596*4882a593Smuzhiyun  *
2597*4882a593Smuzhiyun  * This function (together with _regulator_handle_consumer_disable) is
2598*4882a593Smuzhiyun  * responsible for keeping track of the refcount for a given regulator consumer
2599*4882a593Smuzhiyun  * and applying / unapplying these things.
2600*4882a593Smuzhiyun  *
2601*4882a593Smuzhiyun  * Returns 0 upon no error; -error upon error.
2602*4882a593Smuzhiyun  */
_regulator_handle_consumer_enable(struct regulator * regulator)2603*4882a593Smuzhiyun static int _regulator_handle_consumer_enable(struct regulator *regulator)
2604*4882a593Smuzhiyun {
2605*4882a593Smuzhiyun 	int ret;
2606*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
2607*4882a593Smuzhiyun 
2608*4882a593Smuzhiyun 	lockdep_assert_held_once(&rdev->mutex.base);
2609*4882a593Smuzhiyun 
2610*4882a593Smuzhiyun 	regulator->enable_count++;
2611*4882a593Smuzhiyun 	if (regulator->uA_load && regulator->enable_count == 1) {
2612*4882a593Smuzhiyun 		ret = drms_uA_update(rdev);
2613*4882a593Smuzhiyun 		if (ret)
2614*4882a593Smuzhiyun 			regulator->enable_count--;
2615*4882a593Smuzhiyun 		return ret;
2616*4882a593Smuzhiyun 	}
2617*4882a593Smuzhiyun 
2618*4882a593Smuzhiyun 	return 0;
2619*4882a593Smuzhiyun }
2620*4882a593Smuzhiyun 
2621*4882a593Smuzhiyun /**
2622*4882a593Smuzhiyun  * _regulator_handle_consumer_disable - handle that a consumer disabled
2623*4882a593Smuzhiyun  * @regulator: regulator source
2624*4882a593Smuzhiyun  *
2625*4882a593Smuzhiyun  * The opposite of _regulator_handle_consumer_enable().
2626*4882a593Smuzhiyun  *
2627*4882a593Smuzhiyun  * Returns 0 upon no error; -error upon error.
2628*4882a593Smuzhiyun  */
_regulator_handle_consumer_disable(struct regulator * regulator)2629*4882a593Smuzhiyun static int _regulator_handle_consumer_disable(struct regulator *regulator)
2630*4882a593Smuzhiyun {
2631*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
2632*4882a593Smuzhiyun 
2633*4882a593Smuzhiyun 	lockdep_assert_held_once(&rdev->mutex.base);
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun 	if (!regulator->enable_count) {
2636*4882a593Smuzhiyun 		rdev_err(rdev, "Underflow of regulator enable count\n");
2637*4882a593Smuzhiyun 		return -EINVAL;
2638*4882a593Smuzhiyun 	}
2639*4882a593Smuzhiyun 
2640*4882a593Smuzhiyun 	regulator->enable_count--;
2641*4882a593Smuzhiyun 	if (regulator->uA_load && regulator->enable_count == 0)
2642*4882a593Smuzhiyun 		return drms_uA_update(rdev);
2643*4882a593Smuzhiyun 
2644*4882a593Smuzhiyun 	return 0;
2645*4882a593Smuzhiyun }
2646*4882a593Smuzhiyun 
2647*4882a593Smuzhiyun /* locks held by regulator_enable() */
_regulator_enable(struct regulator * regulator)2648*4882a593Smuzhiyun static int _regulator_enable(struct regulator *regulator)
2649*4882a593Smuzhiyun {
2650*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
2651*4882a593Smuzhiyun 	int ret;
2652*4882a593Smuzhiyun 
2653*4882a593Smuzhiyun 	lockdep_assert_held_once(&rdev->mutex.base);
2654*4882a593Smuzhiyun 
2655*4882a593Smuzhiyun 	if (rdev->use_count == 0 && rdev->supply) {
2656*4882a593Smuzhiyun 		ret = _regulator_enable(rdev->supply);
2657*4882a593Smuzhiyun 		if (ret < 0)
2658*4882a593Smuzhiyun 			return ret;
2659*4882a593Smuzhiyun 	}
2660*4882a593Smuzhiyun 
2661*4882a593Smuzhiyun 	/* balance only if there are regulators coupled */
2662*4882a593Smuzhiyun 	if (rdev->coupling_desc.n_coupled > 1) {
2663*4882a593Smuzhiyun 		ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2664*4882a593Smuzhiyun 		if (ret < 0)
2665*4882a593Smuzhiyun 			goto err_disable_supply;
2666*4882a593Smuzhiyun 	}
2667*4882a593Smuzhiyun 
2668*4882a593Smuzhiyun 	ret = _regulator_handle_consumer_enable(regulator);
2669*4882a593Smuzhiyun 	if (ret < 0)
2670*4882a593Smuzhiyun 		goto err_disable_supply;
2671*4882a593Smuzhiyun 
2672*4882a593Smuzhiyun 	if (rdev->use_count == 0) {
2673*4882a593Smuzhiyun 		/* The regulator may on if it's not switchable or left on */
2674*4882a593Smuzhiyun 		ret = _regulator_is_enabled(rdev);
2675*4882a593Smuzhiyun 		if (ret == -EINVAL || ret == 0) {
2676*4882a593Smuzhiyun 			if (!regulator_ops_is_valid(rdev,
2677*4882a593Smuzhiyun 					REGULATOR_CHANGE_STATUS)) {
2678*4882a593Smuzhiyun 				ret = -EPERM;
2679*4882a593Smuzhiyun 				goto err_consumer_disable;
2680*4882a593Smuzhiyun 			}
2681*4882a593Smuzhiyun 
2682*4882a593Smuzhiyun 			ret = _regulator_do_enable(rdev);
2683*4882a593Smuzhiyun 			if (ret < 0)
2684*4882a593Smuzhiyun 				goto err_consumer_disable;
2685*4882a593Smuzhiyun 
2686*4882a593Smuzhiyun 			_notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2687*4882a593Smuzhiyun 					     NULL);
2688*4882a593Smuzhiyun 		} else if (ret < 0) {
2689*4882a593Smuzhiyun 			rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret));
2690*4882a593Smuzhiyun 			goto err_consumer_disable;
2691*4882a593Smuzhiyun 		}
2692*4882a593Smuzhiyun 		/* Fallthrough on positive return values - already enabled */
2693*4882a593Smuzhiyun 	}
2694*4882a593Smuzhiyun 
2695*4882a593Smuzhiyun 	rdev->use_count++;
2696*4882a593Smuzhiyun 
2697*4882a593Smuzhiyun 	return 0;
2698*4882a593Smuzhiyun 
2699*4882a593Smuzhiyun err_consumer_disable:
2700*4882a593Smuzhiyun 	_regulator_handle_consumer_disable(regulator);
2701*4882a593Smuzhiyun 
2702*4882a593Smuzhiyun err_disable_supply:
2703*4882a593Smuzhiyun 	if (rdev->use_count == 0 && rdev->supply)
2704*4882a593Smuzhiyun 		_regulator_disable(rdev->supply);
2705*4882a593Smuzhiyun 
2706*4882a593Smuzhiyun 	return ret;
2707*4882a593Smuzhiyun }
2708*4882a593Smuzhiyun 
2709*4882a593Smuzhiyun /**
2710*4882a593Smuzhiyun  * regulator_enable - enable regulator output
2711*4882a593Smuzhiyun  * @regulator: regulator source
2712*4882a593Smuzhiyun  *
2713*4882a593Smuzhiyun  * Request that the regulator be enabled with the regulator output at
2714*4882a593Smuzhiyun  * the predefined voltage or current value.  Calls to regulator_enable()
2715*4882a593Smuzhiyun  * must be balanced with calls to regulator_disable().
2716*4882a593Smuzhiyun  *
2717*4882a593Smuzhiyun  * NOTE: the output value can be set by other drivers, boot loader or may be
2718*4882a593Smuzhiyun  * hardwired in the regulator.
2719*4882a593Smuzhiyun  */
regulator_enable(struct regulator * regulator)2720*4882a593Smuzhiyun int regulator_enable(struct regulator *regulator)
2721*4882a593Smuzhiyun {
2722*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
2723*4882a593Smuzhiyun 	struct ww_acquire_ctx ww_ctx;
2724*4882a593Smuzhiyun 	int ret;
2725*4882a593Smuzhiyun 
2726*4882a593Smuzhiyun 	regulator_lock_dependent(rdev, &ww_ctx);
2727*4882a593Smuzhiyun 	ret = _regulator_enable(regulator);
2728*4882a593Smuzhiyun 	regulator_unlock_dependent(rdev, &ww_ctx);
2729*4882a593Smuzhiyun 
2730*4882a593Smuzhiyun 	return ret;
2731*4882a593Smuzhiyun }
2732*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_enable);
2733*4882a593Smuzhiyun 
_regulator_do_disable(struct regulator_dev * rdev)2734*4882a593Smuzhiyun static int _regulator_do_disable(struct regulator_dev *rdev)
2735*4882a593Smuzhiyun {
2736*4882a593Smuzhiyun 	int ret;
2737*4882a593Smuzhiyun 
2738*4882a593Smuzhiyun 	trace_regulator_disable(rdev_get_name(rdev));
2739*4882a593Smuzhiyun 
2740*4882a593Smuzhiyun 	if (rdev->ena_pin) {
2741*4882a593Smuzhiyun 		if (rdev->ena_gpio_state) {
2742*4882a593Smuzhiyun 			ret = regulator_ena_gpio_ctrl(rdev, false);
2743*4882a593Smuzhiyun 			if (ret < 0)
2744*4882a593Smuzhiyun 				return ret;
2745*4882a593Smuzhiyun 			rdev->ena_gpio_state = 0;
2746*4882a593Smuzhiyun 		}
2747*4882a593Smuzhiyun 
2748*4882a593Smuzhiyun 	} else if (rdev->desc->ops->disable) {
2749*4882a593Smuzhiyun 		ret = rdev->desc->ops->disable(rdev);
2750*4882a593Smuzhiyun 		if (ret != 0)
2751*4882a593Smuzhiyun 			return ret;
2752*4882a593Smuzhiyun 	}
2753*4882a593Smuzhiyun 
2754*4882a593Smuzhiyun 	/* cares about last_off_jiffy only if off_on_delay is required by
2755*4882a593Smuzhiyun 	 * device.
2756*4882a593Smuzhiyun 	 */
2757*4882a593Smuzhiyun 	if (rdev->desc->off_on_delay)
2758*4882a593Smuzhiyun 		rdev->last_off_jiffy = jiffies;
2759*4882a593Smuzhiyun 
2760*4882a593Smuzhiyun 	trace_regulator_disable_complete(rdev_get_name(rdev));
2761*4882a593Smuzhiyun 
2762*4882a593Smuzhiyun 	return 0;
2763*4882a593Smuzhiyun }
2764*4882a593Smuzhiyun 
2765*4882a593Smuzhiyun /* locks held by regulator_disable() */
_regulator_disable(struct regulator * regulator)2766*4882a593Smuzhiyun static int _regulator_disable(struct regulator *regulator)
2767*4882a593Smuzhiyun {
2768*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
2769*4882a593Smuzhiyun 	int ret = 0;
2770*4882a593Smuzhiyun 
2771*4882a593Smuzhiyun 	lockdep_assert_held_once(&rdev->mutex.base);
2772*4882a593Smuzhiyun 
2773*4882a593Smuzhiyun 	if (WARN(rdev->use_count <= 0,
2774*4882a593Smuzhiyun 		 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2775*4882a593Smuzhiyun 		return -EIO;
2776*4882a593Smuzhiyun 
2777*4882a593Smuzhiyun 	/* are we the last user and permitted to disable ? */
2778*4882a593Smuzhiyun 	if (rdev->use_count == 1 &&
2779*4882a593Smuzhiyun 	    (rdev->constraints && !rdev->constraints->always_on)) {
2780*4882a593Smuzhiyun 
2781*4882a593Smuzhiyun 		/* we are last user */
2782*4882a593Smuzhiyun 		if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2783*4882a593Smuzhiyun 			ret = _notifier_call_chain(rdev,
2784*4882a593Smuzhiyun 						   REGULATOR_EVENT_PRE_DISABLE,
2785*4882a593Smuzhiyun 						   NULL);
2786*4882a593Smuzhiyun 			if (ret & NOTIFY_STOP_MASK)
2787*4882a593Smuzhiyun 				return -EINVAL;
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun 			ret = _regulator_do_disable(rdev);
2790*4882a593Smuzhiyun 			if (ret < 0) {
2791*4882a593Smuzhiyun 				rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret));
2792*4882a593Smuzhiyun 				_notifier_call_chain(rdev,
2793*4882a593Smuzhiyun 						REGULATOR_EVENT_ABORT_DISABLE,
2794*4882a593Smuzhiyun 						NULL);
2795*4882a593Smuzhiyun 				return ret;
2796*4882a593Smuzhiyun 			}
2797*4882a593Smuzhiyun 			_notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2798*4882a593Smuzhiyun 					NULL);
2799*4882a593Smuzhiyun 		}
2800*4882a593Smuzhiyun 
2801*4882a593Smuzhiyun 		rdev->use_count = 0;
2802*4882a593Smuzhiyun 	} else if (rdev->use_count > 1) {
2803*4882a593Smuzhiyun 		rdev->use_count--;
2804*4882a593Smuzhiyun 	}
2805*4882a593Smuzhiyun 
2806*4882a593Smuzhiyun 	if (ret == 0)
2807*4882a593Smuzhiyun 		ret = _regulator_handle_consumer_disable(regulator);
2808*4882a593Smuzhiyun 
2809*4882a593Smuzhiyun 	if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2810*4882a593Smuzhiyun 		ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun 	if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2813*4882a593Smuzhiyun 		ret = _regulator_disable(rdev->supply);
2814*4882a593Smuzhiyun 
2815*4882a593Smuzhiyun 	return ret;
2816*4882a593Smuzhiyun }
2817*4882a593Smuzhiyun 
2818*4882a593Smuzhiyun /**
2819*4882a593Smuzhiyun  * regulator_disable - disable regulator output
2820*4882a593Smuzhiyun  * @regulator: regulator source
2821*4882a593Smuzhiyun  *
2822*4882a593Smuzhiyun  * Disable the regulator output voltage or current.  Calls to
2823*4882a593Smuzhiyun  * regulator_enable() must be balanced with calls to
2824*4882a593Smuzhiyun  * regulator_disable().
2825*4882a593Smuzhiyun  *
2826*4882a593Smuzhiyun  * NOTE: this will only disable the regulator output if no other consumer
2827*4882a593Smuzhiyun  * devices have it enabled, the regulator device supports disabling and
2828*4882a593Smuzhiyun  * machine constraints permit this operation.
2829*4882a593Smuzhiyun  */
regulator_disable(struct regulator * regulator)2830*4882a593Smuzhiyun int regulator_disable(struct regulator *regulator)
2831*4882a593Smuzhiyun {
2832*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
2833*4882a593Smuzhiyun 	struct ww_acquire_ctx ww_ctx;
2834*4882a593Smuzhiyun 	int ret;
2835*4882a593Smuzhiyun 
2836*4882a593Smuzhiyun 	regulator_lock_dependent(rdev, &ww_ctx);
2837*4882a593Smuzhiyun 	ret = _regulator_disable(regulator);
2838*4882a593Smuzhiyun 	regulator_unlock_dependent(rdev, &ww_ctx);
2839*4882a593Smuzhiyun 
2840*4882a593Smuzhiyun 	return ret;
2841*4882a593Smuzhiyun }
2842*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_disable);
2843*4882a593Smuzhiyun 
2844*4882a593Smuzhiyun /* locks held by regulator_force_disable() */
_regulator_force_disable(struct regulator_dev * rdev)2845*4882a593Smuzhiyun static int _regulator_force_disable(struct regulator_dev *rdev)
2846*4882a593Smuzhiyun {
2847*4882a593Smuzhiyun 	int ret = 0;
2848*4882a593Smuzhiyun 
2849*4882a593Smuzhiyun 	lockdep_assert_held_once(&rdev->mutex.base);
2850*4882a593Smuzhiyun 
2851*4882a593Smuzhiyun 	ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2852*4882a593Smuzhiyun 			REGULATOR_EVENT_PRE_DISABLE, NULL);
2853*4882a593Smuzhiyun 	if (ret & NOTIFY_STOP_MASK)
2854*4882a593Smuzhiyun 		return -EINVAL;
2855*4882a593Smuzhiyun 
2856*4882a593Smuzhiyun 	ret = _regulator_do_disable(rdev);
2857*4882a593Smuzhiyun 	if (ret < 0) {
2858*4882a593Smuzhiyun 		rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret));
2859*4882a593Smuzhiyun 		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2860*4882a593Smuzhiyun 				REGULATOR_EVENT_ABORT_DISABLE, NULL);
2861*4882a593Smuzhiyun 		return ret;
2862*4882a593Smuzhiyun 	}
2863*4882a593Smuzhiyun 
2864*4882a593Smuzhiyun 	_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2865*4882a593Smuzhiyun 			REGULATOR_EVENT_DISABLE, NULL);
2866*4882a593Smuzhiyun 
2867*4882a593Smuzhiyun 	return 0;
2868*4882a593Smuzhiyun }
2869*4882a593Smuzhiyun 
2870*4882a593Smuzhiyun /**
2871*4882a593Smuzhiyun  * regulator_force_disable - force disable regulator output
2872*4882a593Smuzhiyun  * @regulator: regulator source
2873*4882a593Smuzhiyun  *
2874*4882a593Smuzhiyun  * Forcibly disable the regulator output voltage or current.
2875*4882a593Smuzhiyun  * NOTE: this *will* disable the regulator output even if other consumer
2876*4882a593Smuzhiyun  * devices have it enabled. This should be used for situations when device
2877*4882a593Smuzhiyun  * damage will likely occur if the regulator is not disabled (e.g. over temp).
2878*4882a593Smuzhiyun  */
regulator_force_disable(struct regulator * regulator)2879*4882a593Smuzhiyun int regulator_force_disable(struct regulator *regulator)
2880*4882a593Smuzhiyun {
2881*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
2882*4882a593Smuzhiyun 	struct ww_acquire_ctx ww_ctx;
2883*4882a593Smuzhiyun 	int ret;
2884*4882a593Smuzhiyun 
2885*4882a593Smuzhiyun 	regulator_lock_dependent(rdev, &ww_ctx);
2886*4882a593Smuzhiyun 
2887*4882a593Smuzhiyun 	ret = _regulator_force_disable(regulator->rdev);
2888*4882a593Smuzhiyun 
2889*4882a593Smuzhiyun 	if (rdev->coupling_desc.n_coupled > 1)
2890*4882a593Smuzhiyun 		regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2891*4882a593Smuzhiyun 
2892*4882a593Smuzhiyun 	if (regulator->uA_load) {
2893*4882a593Smuzhiyun 		regulator->uA_load = 0;
2894*4882a593Smuzhiyun 		ret = drms_uA_update(rdev);
2895*4882a593Smuzhiyun 	}
2896*4882a593Smuzhiyun 
2897*4882a593Smuzhiyun 	if (rdev->use_count != 0 && rdev->supply)
2898*4882a593Smuzhiyun 		_regulator_disable(rdev->supply);
2899*4882a593Smuzhiyun 
2900*4882a593Smuzhiyun 	regulator_unlock_dependent(rdev, &ww_ctx);
2901*4882a593Smuzhiyun 
2902*4882a593Smuzhiyun 	return ret;
2903*4882a593Smuzhiyun }
2904*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_force_disable);
2905*4882a593Smuzhiyun 
regulator_disable_work(struct work_struct * work)2906*4882a593Smuzhiyun static void regulator_disable_work(struct work_struct *work)
2907*4882a593Smuzhiyun {
2908*4882a593Smuzhiyun 	struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2909*4882a593Smuzhiyun 						  disable_work.work);
2910*4882a593Smuzhiyun 	struct ww_acquire_ctx ww_ctx;
2911*4882a593Smuzhiyun 	int count, i, ret;
2912*4882a593Smuzhiyun 	struct regulator *regulator;
2913*4882a593Smuzhiyun 	int total_count = 0;
2914*4882a593Smuzhiyun 
2915*4882a593Smuzhiyun 	regulator_lock_dependent(rdev, &ww_ctx);
2916*4882a593Smuzhiyun 
2917*4882a593Smuzhiyun 	/*
2918*4882a593Smuzhiyun 	 * Workqueue functions queue the new work instance while the previous
2919*4882a593Smuzhiyun 	 * work instance is being processed. Cancel the queued work instance
2920*4882a593Smuzhiyun 	 * as the work instance under processing does the job of the queued
2921*4882a593Smuzhiyun 	 * work instance.
2922*4882a593Smuzhiyun 	 */
2923*4882a593Smuzhiyun 	cancel_delayed_work(&rdev->disable_work);
2924*4882a593Smuzhiyun 
2925*4882a593Smuzhiyun 	list_for_each_entry(regulator, &rdev->consumer_list, list) {
2926*4882a593Smuzhiyun 		count = regulator->deferred_disables;
2927*4882a593Smuzhiyun 
2928*4882a593Smuzhiyun 		if (!count)
2929*4882a593Smuzhiyun 			continue;
2930*4882a593Smuzhiyun 
2931*4882a593Smuzhiyun 		total_count += count;
2932*4882a593Smuzhiyun 		regulator->deferred_disables = 0;
2933*4882a593Smuzhiyun 
2934*4882a593Smuzhiyun 		for (i = 0; i < count; i++) {
2935*4882a593Smuzhiyun 			ret = _regulator_disable(regulator);
2936*4882a593Smuzhiyun 			if (ret != 0)
2937*4882a593Smuzhiyun 				rdev_err(rdev, "Deferred disable failed: %pe\n",
2938*4882a593Smuzhiyun 					 ERR_PTR(ret));
2939*4882a593Smuzhiyun 		}
2940*4882a593Smuzhiyun 	}
2941*4882a593Smuzhiyun 	WARN_ON(!total_count);
2942*4882a593Smuzhiyun 
2943*4882a593Smuzhiyun 	if (rdev->coupling_desc.n_coupled > 1)
2944*4882a593Smuzhiyun 		regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2945*4882a593Smuzhiyun 
2946*4882a593Smuzhiyun 	regulator_unlock_dependent(rdev, &ww_ctx);
2947*4882a593Smuzhiyun }
2948*4882a593Smuzhiyun 
2949*4882a593Smuzhiyun /**
2950*4882a593Smuzhiyun  * regulator_disable_deferred - disable regulator output with delay
2951*4882a593Smuzhiyun  * @regulator: regulator source
2952*4882a593Smuzhiyun  * @ms: milliseconds until the regulator is disabled
2953*4882a593Smuzhiyun  *
2954*4882a593Smuzhiyun  * Execute regulator_disable() on the regulator after a delay.  This
2955*4882a593Smuzhiyun  * is intended for use with devices that require some time to quiesce.
2956*4882a593Smuzhiyun  *
2957*4882a593Smuzhiyun  * NOTE: this will only disable the regulator output if no other consumer
2958*4882a593Smuzhiyun  * devices have it enabled, the regulator device supports disabling and
2959*4882a593Smuzhiyun  * machine constraints permit this operation.
2960*4882a593Smuzhiyun  */
regulator_disable_deferred(struct regulator * regulator,int ms)2961*4882a593Smuzhiyun int regulator_disable_deferred(struct regulator *regulator, int ms)
2962*4882a593Smuzhiyun {
2963*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
2964*4882a593Smuzhiyun 
2965*4882a593Smuzhiyun 	if (!ms)
2966*4882a593Smuzhiyun 		return regulator_disable(regulator);
2967*4882a593Smuzhiyun 
2968*4882a593Smuzhiyun 	regulator_lock(rdev);
2969*4882a593Smuzhiyun 	regulator->deferred_disables++;
2970*4882a593Smuzhiyun 	mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2971*4882a593Smuzhiyun 			 msecs_to_jiffies(ms));
2972*4882a593Smuzhiyun 	regulator_unlock(rdev);
2973*4882a593Smuzhiyun 
2974*4882a593Smuzhiyun 	return 0;
2975*4882a593Smuzhiyun }
2976*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2977*4882a593Smuzhiyun 
_regulator_is_enabled(struct regulator_dev * rdev)2978*4882a593Smuzhiyun static int _regulator_is_enabled(struct regulator_dev *rdev)
2979*4882a593Smuzhiyun {
2980*4882a593Smuzhiyun 	/* A GPIO control always takes precedence */
2981*4882a593Smuzhiyun 	if (rdev->ena_pin)
2982*4882a593Smuzhiyun 		return rdev->ena_gpio_state;
2983*4882a593Smuzhiyun 
2984*4882a593Smuzhiyun 	/* If we don't know then assume that the regulator is always on */
2985*4882a593Smuzhiyun 	if (!rdev->desc->ops->is_enabled)
2986*4882a593Smuzhiyun 		return 1;
2987*4882a593Smuzhiyun 
2988*4882a593Smuzhiyun 	return rdev->desc->ops->is_enabled(rdev);
2989*4882a593Smuzhiyun }
2990*4882a593Smuzhiyun 
_regulator_list_voltage(struct regulator_dev * rdev,unsigned selector,int lock)2991*4882a593Smuzhiyun static int _regulator_list_voltage(struct regulator_dev *rdev,
2992*4882a593Smuzhiyun 				   unsigned selector, int lock)
2993*4882a593Smuzhiyun {
2994*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
2995*4882a593Smuzhiyun 	int ret;
2996*4882a593Smuzhiyun 
2997*4882a593Smuzhiyun 	if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2998*4882a593Smuzhiyun 		return rdev->desc->fixed_uV;
2999*4882a593Smuzhiyun 
3000*4882a593Smuzhiyun 	if (ops->list_voltage) {
3001*4882a593Smuzhiyun 		if (selector >= rdev->desc->n_voltages)
3002*4882a593Smuzhiyun 			return -EINVAL;
3003*4882a593Smuzhiyun 		if (lock)
3004*4882a593Smuzhiyun 			regulator_lock(rdev);
3005*4882a593Smuzhiyun 		ret = ops->list_voltage(rdev, selector);
3006*4882a593Smuzhiyun 		if (lock)
3007*4882a593Smuzhiyun 			regulator_unlock(rdev);
3008*4882a593Smuzhiyun 	} else if (rdev->is_switch && rdev->supply) {
3009*4882a593Smuzhiyun 		ret = _regulator_list_voltage(rdev->supply->rdev,
3010*4882a593Smuzhiyun 					      selector, lock);
3011*4882a593Smuzhiyun 	} else {
3012*4882a593Smuzhiyun 		return -EINVAL;
3013*4882a593Smuzhiyun 	}
3014*4882a593Smuzhiyun 
3015*4882a593Smuzhiyun 	if (ret > 0) {
3016*4882a593Smuzhiyun 		if (ret < rdev->constraints->min_uV)
3017*4882a593Smuzhiyun 			ret = 0;
3018*4882a593Smuzhiyun 		else if (ret > rdev->constraints->max_uV)
3019*4882a593Smuzhiyun 			ret = 0;
3020*4882a593Smuzhiyun 	}
3021*4882a593Smuzhiyun 
3022*4882a593Smuzhiyun 	return ret;
3023*4882a593Smuzhiyun }
3024*4882a593Smuzhiyun 
3025*4882a593Smuzhiyun /**
3026*4882a593Smuzhiyun  * regulator_is_enabled - is the regulator output enabled
3027*4882a593Smuzhiyun  * @regulator: regulator source
3028*4882a593Smuzhiyun  *
3029*4882a593Smuzhiyun  * Returns positive if the regulator driver backing the source/client
3030*4882a593Smuzhiyun  * has requested that the device be enabled, zero if it hasn't, else a
3031*4882a593Smuzhiyun  * negative errno code.
3032*4882a593Smuzhiyun  *
3033*4882a593Smuzhiyun  * Note that the device backing this regulator handle can have multiple
3034*4882a593Smuzhiyun  * users, so it might be enabled even if regulator_enable() was never
3035*4882a593Smuzhiyun  * called for this particular source.
3036*4882a593Smuzhiyun  */
regulator_is_enabled(struct regulator * regulator)3037*4882a593Smuzhiyun int regulator_is_enabled(struct regulator *regulator)
3038*4882a593Smuzhiyun {
3039*4882a593Smuzhiyun 	int ret;
3040*4882a593Smuzhiyun 
3041*4882a593Smuzhiyun 	if (regulator->always_on)
3042*4882a593Smuzhiyun 		return 1;
3043*4882a593Smuzhiyun 
3044*4882a593Smuzhiyun 	regulator_lock(regulator->rdev);
3045*4882a593Smuzhiyun 	ret = _regulator_is_enabled(regulator->rdev);
3046*4882a593Smuzhiyun 	regulator_unlock(regulator->rdev);
3047*4882a593Smuzhiyun 
3048*4882a593Smuzhiyun 	return ret;
3049*4882a593Smuzhiyun }
3050*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_is_enabled);
3051*4882a593Smuzhiyun 
3052*4882a593Smuzhiyun /**
3053*4882a593Smuzhiyun  * regulator_count_voltages - count regulator_list_voltage() selectors
3054*4882a593Smuzhiyun  * @regulator: regulator source
3055*4882a593Smuzhiyun  *
3056*4882a593Smuzhiyun  * Returns number of selectors, or negative errno.  Selectors are
3057*4882a593Smuzhiyun  * numbered starting at zero, and typically correspond to bitfields
3058*4882a593Smuzhiyun  * in hardware registers.
3059*4882a593Smuzhiyun  */
regulator_count_voltages(struct regulator * regulator)3060*4882a593Smuzhiyun int regulator_count_voltages(struct regulator *regulator)
3061*4882a593Smuzhiyun {
3062*4882a593Smuzhiyun 	struct regulator_dev	*rdev = regulator->rdev;
3063*4882a593Smuzhiyun 
3064*4882a593Smuzhiyun 	if (rdev->desc->n_voltages)
3065*4882a593Smuzhiyun 		return rdev->desc->n_voltages;
3066*4882a593Smuzhiyun 
3067*4882a593Smuzhiyun 	if (!rdev->is_switch || !rdev->supply)
3068*4882a593Smuzhiyun 		return -EINVAL;
3069*4882a593Smuzhiyun 
3070*4882a593Smuzhiyun 	return regulator_count_voltages(rdev->supply);
3071*4882a593Smuzhiyun }
3072*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_count_voltages);
3073*4882a593Smuzhiyun 
3074*4882a593Smuzhiyun /**
3075*4882a593Smuzhiyun  * regulator_list_voltage - enumerate supported voltages
3076*4882a593Smuzhiyun  * @regulator: regulator source
3077*4882a593Smuzhiyun  * @selector: identify voltage to list
3078*4882a593Smuzhiyun  * Context: can sleep
3079*4882a593Smuzhiyun  *
3080*4882a593Smuzhiyun  * Returns a voltage that can be passed to @regulator_set_voltage(),
3081*4882a593Smuzhiyun  * zero if this selector code can't be used on this system, or a
3082*4882a593Smuzhiyun  * negative errno.
3083*4882a593Smuzhiyun  */
regulator_list_voltage(struct regulator * regulator,unsigned selector)3084*4882a593Smuzhiyun int regulator_list_voltage(struct regulator *regulator, unsigned selector)
3085*4882a593Smuzhiyun {
3086*4882a593Smuzhiyun 	return _regulator_list_voltage(regulator->rdev, selector, 1);
3087*4882a593Smuzhiyun }
3088*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_list_voltage);
3089*4882a593Smuzhiyun 
3090*4882a593Smuzhiyun /**
3091*4882a593Smuzhiyun  * regulator_get_regmap - get the regulator's register map
3092*4882a593Smuzhiyun  * @regulator: regulator source
3093*4882a593Smuzhiyun  *
3094*4882a593Smuzhiyun  * Returns the register map for the given regulator, or an ERR_PTR value
3095*4882a593Smuzhiyun  * if the regulator doesn't use regmap.
3096*4882a593Smuzhiyun  */
regulator_get_regmap(struct regulator * regulator)3097*4882a593Smuzhiyun struct regmap *regulator_get_regmap(struct regulator *regulator)
3098*4882a593Smuzhiyun {
3099*4882a593Smuzhiyun 	struct regmap *map = regulator->rdev->regmap;
3100*4882a593Smuzhiyun 
3101*4882a593Smuzhiyun 	return map ? map : ERR_PTR(-EOPNOTSUPP);
3102*4882a593Smuzhiyun }
3103*4882a593Smuzhiyun 
3104*4882a593Smuzhiyun /**
3105*4882a593Smuzhiyun  * regulator_get_hardware_vsel_register - get the HW voltage selector register
3106*4882a593Smuzhiyun  * @regulator: regulator source
3107*4882a593Smuzhiyun  * @vsel_reg: voltage selector register, output parameter
3108*4882a593Smuzhiyun  * @vsel_mask: mask for voltage selector bitfield, output parameter
3109*4882a593Smuzhiyun  *
3110*4882a593Smuzhiyun  * Returns the hardware register offset and bitmask used for setting the
3111*4882a593Smuzhiyun  * regulator voltage. This might be useful when configuring voltage-scaling
3112*4882a593Smuzhiyun  * hardware or firmware that can make I2C requests behind the kernel's back,
3113*4882a593Smuzhiyun  * for example.
3114*4882a593Smuzhiyun  *
3115*4882a593Smuzhiyun  * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3116*4882a593Smuzhiyun  * and 0 is returned, otherwise a negative errno is returned.
3117*4882a593Smuzhiyun  */
regulator_get_hardware_vsel_register(struct regulator * regulator,unsigned * vsel_reg,unsigned * vsel_mask)3118*4882a593Smuzhiyun int regulator_get_hardware_vsel_register(struct regulator *regulator,
3119*4882a593Smuzhiyun 					 unsigned *vsel_reg,
3120*4882a593Smuzhiyun 					 unsigned *vsel_mask)
3121*4882a593Smuzhiyun {
3122*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
3123*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
3124*4882a593Smuzhiyun 
3125*4882a593Smuzhiyun 	if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3126*4882a593Smuzhiyun 		return -EOPNOTSUPP;
3127*4882a593Smuzhiyun 
3128*4882a593Smuzhiyun 	*vsel_reg = rdev->desc->vsel_reg;
3129*4882a593Smuzhiyun 	*vsel_mask = rdev->desc->vsel_mask;
3130*4882a593Smuzhiyun 
3131*4882a593Smuzhiyun 	return 0;
3132*4882a593Smuzhiyun }
3133*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3134*4882a593Smuzhiyun 
3135*4882a593Smuzhiyun /**
3136*4882a593Smuzhiyun  * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3137*4882a593Smuzhiyun  * @regulator: regulator source
3138*4882a593Smuzhiyun  * @selector: identify voltage to list
3139*4882a593Smuzhiyun  *
3140*4882a593Smuzhiyun  * Converts the selector to a hardware-specific voltage selector that can be
3141*4882a593Smuzhiyun  * directly written to the regulator registers. The address of the voltage
3142*4882a593Smuzhiyun  * register can be determined by calling @regulator_get_hardware_vsel_register.
3143*4882a593Smuzhiyun  *
3144*4882a593Smuzhiyun  * On error a negative errno is returned.
3145*4882a593Smuzhiyun  */
regulator_list_hardware_vsel(struct regulator * regulator,unsigned selector)3146*4882a593Smuzhiyun int regulator_list_hardware_vsel(struct regulator *regulator,
3147*4882a593Smuzhiyun 				 unsigned selector)
3148*4882a593Smuzhiyun {
3149*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
3150*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
3151*4882a593Smuzhiyun 
3152*4882a593Smuzhiyun 	if (selector >= rdev->desc->n_voltages)
3153*4882a593Smuzhiyun 		return -EINVAL;
3154*4882a593Smuzhiyun 	if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3155*4882a593Smuzhiyun 		return -EOPNOTSUPP;
3156*4882a593Smuzhiyun 
3157*4882a593Smuzhiyun 	return selector;
3158*4882a593Smuzhiyun }
3159*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3160*4882a593Smuzhiyun 
3161*4882a593Smuzhiyun /**
3162*4882a593Smuzhiyun  * regulator_get_linear_step - return the voltage step size between VSEL values
3163*4882a593Smuzhiyun  * @regulator: regulator source
3164*4882a593Smuzhiyun  *
3165*4882a593Smuzhiyun  * Returns the voltage step size between VSEL values for linear
3166*4882a593Smuzhiyun  * regulators, or return 0 if the regulator isn't a linear regulator.
3167*4882a593Smuzhiyun  */
regulator_get_linear_step(struct regulator * regulator)3168*4882a593Smuzhiyun unsigned int regulator_get_linear_step(struct regulator *regulator)
3169*4882a593Smuzhiyun {
3170*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
3171*4882a593Smuzhiyun 
3172*4882a593Smuzhiyun 	return rdev->desc->uV_step;
3173*4882a593Smuzhiyun }
3174*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3175*4882a593Smuzhiyun 
3176*4882a593Smuzhiyun /**
3177*4882a593Smuzhiyun  * regulator_is_supported_voltage - check if a voltage range can be supported
3178*4882a593Smuzhiyun  *
3179*4882a593Smuzhiyun  * @regulator: Regulator to check.
3180*4882a593Smuzhiyun  * @min_uV: Minimum required voltage in uV.
3181*4882a593Smuzhiyun  * @max_uV: Maximum required voltage in uV.
3182*4882a593Smuzhiyun  *
3183*4882a593Smuzhiyun  * Returns a boolean.
3184*4882a593Smuzhiyun  */
regulator_is_supported_voltage(struct regulator * regulator,int min_uV,int max_uV)3185*4882a593Smuzhiyun int regulator_is_supported_voltage(struct regulator *regulator,
3186*4882a593Smuzhiyun 				   int min_uV, int max_uV)
3187*4882a593Smuzhiyun {
3188*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
3189*4882a593Smuzhiyun 	int i, voltages, ret;
3190*4882a593Smuzhiyun 
3191*4882a593Smuzhiyun 	/* If we can't change voltage check the current voltage */
3192*4882a593Smuzhiyun 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3193*4882a593Smuzhiyun 		ret = regulator_get_voltage(regulator);
3194*4882a593Smuzhiyun 		if (ret >= 0)
3195*4882a593Smuzhiyun 			return min_uV <= ret && ret <= max_uV;
3196*4882a593Smuzhiyun 		else
3197*4882a593Smuzhiyun 			return ret;
3198*4882a593Smuzhiyun 	}
3199*4882a593Smuzhiyun 
3200*4882a593Smuzhiyun 	/* Any voltage within constrains range is fine? */
3201*4882a593Smuzhiyun 	if (rdev->desc->continuous_voltage_range)
3202*4882a593Smuzhiyun 		return min_uV >= rdev->constraints->min_uV &&
3203*4882a593Smuzhiyun 				max_uV <= rdev->constraints->max_uV;
3204*4882a593Smuzhiyun 
3205*4882a593Smuzhiyun 	ret = regulator_count_voltages(regulator);
3206*4882a593Smuzhiyun 	if (ret < 0)
3207*4882a593Smuzhiyun 		return 0;
3208*4882a593Smuzhiyun 	voltages = ret;
3209*4882a593Smuzhiyun 
3210*4882a593Smuzhiyun 	for (i = 0; i < voltages; i++) {
3211*4882a593Smuzhiyun 		ret = regulator_list_voltage(regulator, i);
3212*4882a593Smuzhiyun 
3213*4882a593Smuzhiyun 		if (ret >= min_uV && ret <= max_uV)
3214*4882a593Smuzhiyun 			return 1;
3215*4882a593Smuzhiyun 	}
3216*4882a593Smuzhiyun 
3217*4882a593Smuzhiyun 	return 0;
3218*4882a593Smuzhiyun }
3219*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3220*4882a593Smuzhiyun 
regulator_map_voltage(struct regulator_dev * rdev,int min_uV,int max_uV)3221*4882a593Smuzhiyun static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3222*4882a593Smuzhiyun 				 int max_uV)
3223*4882a593Smuzhiyun {
3224*4882a593Smuzhiyun 	const struct regulator_desc *desc = rdev->desc;
3225*4882a593Smuzhiyun 
3226*4882a593Smuzhiyun 	if (desc->ops->map_voltage)
3227*4882a593Smuzhiyun 		return desc->ops->map_voltage(rdev, min_uV, max_uV);
3228*4882a593Smuzhiyun 
3229*4882a593Smuzhiyun 	if (desc->ops->list_voltage == regulator_list_voltage_linear)
3230*4882a593Smuzhiyun 		return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3231*4882a593Smuzhiyun 
3232*4882a593Smuzhiyun 	if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3233*4882a593Smuzhiyun 		return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3234*4882a593Smuzhiyun 
3235*4882a593Smuzhiyun 	if (desc->ops->list_voltage ==
3236*4882a593Smuzhiyun 		regulator_list_voltage_pickable_linear_range)
3237*4882a593Smuzhiyun 		return regulator_map_voltage_pickable_linear_range(rdev,
3238*4882a593Smuzhiyun 							min_uV, max_uV);
3239*4882a593Smuzhiyun 
3240*4882a593Smuzhiyun 	return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3241*4882a593Smuzhiyun }
3242*4882a593Smuzhiyun 
_regulator_call_set_voltage(struct regulator_dev * rdev,int min_uV,int max_uV,unsigned * selector)3243*4882a593Smuzhiyun static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3244*4882a593Smuzhiyun 				       int min_uV, int max_uV,
3245*4882a593Smuzhiyun 				       unsigned *selector)
3246*4882a593Smuzhiyun {
3247*4882a593Smuzhiyun 	struct pre_voltage_change_data data;
3248*4882a593Smuzhiyun 	int ret;
3249*4882a593Smuzhiyun 
3250*4882a593Smuzhiyun 	data.old_uV = regulator_get_voltage_rdev(rdev);
3251*4882a593Smuzhiyun 	data.min_uV = min_uV;
3252*4882a593Smuzhiyun 	data.max_uV = max_uV;
3253*4882a593Smuzhiyun 	ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3254*4882a593Smuzhiyun 				   &data);
3255*4882a593Smuzhiyun 	if (ret & NOTIFY_STOP_MASK)
3256*4882a593Smuzhiyun 		return -EINVAL;
3257*4882a593Smuzhiyun 
3258*4882a593Smuzhiyun 	ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3259*4882a593Smuzhiyun 	if (ret >= 0)
3260*4882a593Smuzhiyun 		return ret;
3261*4882a593Smuzhiyun 
3262*4882a593Smuzhiyun 	_notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3263*4882a593Smuzhiyun 			     (void *)data.old_uV);
3264*4882a593Smuzhiyun 
3265*4882a593Smuzhiyun 	return ret;
3266*4882a593Smuzhiyun }
3267*4882a593Smuzhiyun 
_regulator_call_set_voltage_sel(struct regulator_dev * rdev,int uV,unsigned selector)3268*4882a593Smuzhiyun static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3269*4882a593Smuzhiyun 					   int uV, unsigned selector)
3270*4882a593Smuzhiyun {
3271*4882a593Smuzhiyun 	struct pre_voltage_change_data data;
3272*4882a593Smuzhiyun 	int ret;
3273*4882a593Smuzhiyun 
3274*4882a593Smuzhiyun 	data.old_uV = regulator_get_voltage_rdev(rdev);
3275*4882a593Smuzhiyun 	data.min_uV = uV;
3276*4882a593Smuzhiyun 	data.max_uV = uV;
3277*4882a593Smuzhiyun 	ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3278*4882a593Smuzhiyun 				   &data);
3279*4882a593Smuzhiyun 	if (ret & NOTIFY_STOP_MASK)
3280*4882a593Smuzhiyun 		return -EINVAL;
3281*4882a593Smuzhiyun 
3282*4882a593Smuzhiyun 	ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3283*4882a593Smuzhiyun 	if (ret >= 0)
3284*4882a593Smuzhiyun 		return ret;
3285*4882a593Smuzhiyun 
3286*4882a593Smuzhiyun 	_notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3287*4882a593Smuzhiyun 			     (void *)data.old_uV);
3288*4882a593Smuzhiyun 
3289*4882a593Smuzhiyun 	return ret;
3290*4882a593Smuzhiyun }
3291*4882a593Smuzhiyun 
_regulator_set_voltage_sel_step(struct regulator_dev * rdev,int uV,int new_selector)3292*4882a593Smuzhiyun static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3293*4882a593Smuzhiyun 					   int uV, int new_selector)
3294*4882a593Smuzhiyun {
3295*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
3296*4882a593Smuzhiyun 	int diff, old_sel, curr_sel, ret;
3297*4882a593Smuzhiyun 
3298*4882a593Smuzhiyun 	/* Stepping is only needed if the regulator is enabled. */
3299*4882a593Smuzhiyun 	if (!_regulator_is_enabled(rdev))
3300*4882a593Smuzhiyun 		goto final_set;
3301*4882a593Smuzhiyun 
3302*4882a593Smuzhiyun 	if (!ops->get_voltage_sel)
3303*4882a593Smuzhiyun 		return -EINVAL;
3304*4882a593Smuzhiyun 
3305*4882a593Smuzhiyun 	old_sel = ops->get_voltage_sel(rdev);
3306*4882a593Smuzhiyun 	if (old_sel < 0)
3307*4882a593Smuzhiyun 		return old_sel;
3308*4882a593Smuzhiyun 
3309*4882a593Smuzhiyun 	diff = new_selector - old_sel;
3310*4882a593Smuzhiyun 	if (diff == 0)
3311*4882a593Smuzhiyun 		return 0; /* No change needed. */
3312*4882a593Smuzhiyun 
3313*4882a593Smuzhiyun 	if (diff > 0) {
3314*4882a593Smuzhiyun 		/* Stepping up. */
3315*4882a593Smuzhiyun 		for (curr_sel = old_sel + rdev->desc->vsel_step;
3316*4882a593Smuzhiyun 		     curr_sel < new_selector;
3317*4882a593Smuzhiyun 		     curr_sel += rdev->desc->vsel_step) {
3318*4882a593Smuzhiyun 			/*
3319*4882a593Smuzhiyun 			 * Call the callback directly instead of using
3320*4882a593Smuzhiyun 			 * _regulator_call_set_voltage_sel() as we don't
3321*4882a593Smuzhiyun 			 * want to notify anyone yet. Same in the branch
3322*4882a593Smuzhiyun 			 * below.
3323*4882a593Smuzhiyun 			 */
3324*4882a593Smuzhiyun 			ret = ops->set_voltage_sel(rdev, curr_sel);
3325*4882a593Smuzhiyun 			if (ret)
3326*4882a593Smuzhiyun 				goto try_revert;
3327*4882a593Smuzhiyun 		}
3328*4882a593Smuzhiyun 	} else {
3329*4882a593Smuzhiyun 		/* Stepping down. */
3330*4882a593Smuzhiyun 		for (curr_sel = old_sel - rdev->desc->vsel_step;
3331*4882a593Smuzhiyun 		     curr_sel > new_selector;
3332*4882a593Smuzhiyun 		     curr_sel -= rdev->desc->vsel_step) {
3333*4882a593Smuzhiyun 			ret = ops->set_voltage_sel(rdev, curr_sel);
3334*4882a593Smuzhiyun 			if (ret)
3335*4882a593Smuzhiyun 				goto try_revert;
3336*4882a593Smuzhiyun 		}
3337*4882a593Smuzhiyun 	}
3338*4882a593Smuzhiyun 
3339*4882a593Smuzhiyun final_set:
3340*4882a593Smuzhiyun 	/* The final selector will trigger the notifiers. */
3341*4882a593Smuzhiyun 	return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3342*4882a593Smuzhiyun 
3343*4882a593Smuzhiyun try_revert:
3344*4882a593Smuzhiyun 	/*
3345*4882a593Smuzhiyun 	 * At least try to return to the previous voltage if setting a new
3346*4882a593Smuzhiyun 	 * one failed.
3347*4882a593Smuzhiyun 	 */
3348*4882a593Smuzhiyun 	(void)ops->set_voltage_sel(rdev, old_sel);
3349*4882a593Smuzhiyun 	return ret;
3350*4882a593Smuzhiyun }
3351*4882a593Smuzhiyun 
_regulator_set_voltage_time(struct regulator_dev * rdev,int old_uV,int new_uV)3352*4882a593Smuzhiyun static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3353*4882a593Smuzhiyun 				       int old_uV, int new_uV)
3354*4882a593Smuzhiyun {
3355*4882a593Smuzhiyun 	unsigned int ramp_delay = 0;
3356*4882a593Smuzhiyun 
3357*4882a593Smuzhiyun 	if (rdev->constraints->ramp_delay)
3358*4882a593Smuzhiyun 		ramp_delay = rdev->constraints->ramp_delay;
3359*4882a593Smuzhiyun 	else if (rdev->desc->ramp_delay)
3360*4882a593Smuzhiyun 		ramp_delay = rdev->desc->ramp_delay;
3361*4882a593Smuzhiyun 	else if (rdev->constraints->settling_time)
3362*4882a593Smuzhiyun 		return rdev->constraints->settling_time;
3363*4882a593Smuzhiyun 	else if (rdev->constraints->settling_time_up &&
3364*4882a593Smuzhiyun 		 (new_uV > old_uV))
3365*4882a593Smuzhiyun 		return rdev->constraints->settling_time_up;
3366*4882a593Smuzhiyun 	else if (rdev->constraints->settling_time_down &&
3367*4882a593Smuzhiyun 		 (new_uV < old_uV))
3368*4882a593Smuzhiyun 		return rdev->constraints->settling_time_down;
3369*4882a593Smuzhiyun 
3370*4882a593Smuzhiyun 	if (ramp_delay == 0) {
3371*4882a593Smuzhiyun 		rdev_dbg(rdev, "ramp_delay not set\n");
3372*4882a593Smuzhiyun 		return 0;
3373*4882a593Smuzhiyun 	}
3374*4882a593Smuzhiyun 
3375*4882a593Smuzhiyun 	return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3376*4882a593Smuzhiyun }
3377*4882a593Smuzhiyun 
_regulator_do_set_voltage(struct regulator_dev * rdev,int min_uV,int max_uV)3378*4882a593Smuzhiyun static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3379*4882a593Smuzhiyun 				     int min_uV, int max_uV)
3380*4882a593Smuzhiyun {
3381*4882a593Smuzhiyun 	int ret;
3382*4882a593Smuzhiyun 	int delay = 0;
3383*4882a593Smuzhiyun 	int best_val = 0;
3384*4882a593Smuzhiyun 	unsigned int selector;
3385*4882a593Smuzhiyun 	int old_selector = -1;
3386*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
3387*4882a593Smuzhiyun 	int old_uV = regulator_get_voltage_rdev(rdev);
3388*4882a593Smuzhiyun 
3389*4882a593Smuzhiyun 	trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3390*4882a593Smuzhiyun 
3391*4882a593Smuzhiyun 	min_uV += rdev->constraints->uV_offset;
3392*4882a593Smuzhiyun 	max_uV += rdev->constraints->uV_offset;
3393*4882a593Smuzhiyun 
3394*4882a593Smuzhiyun 	/*
3395*4882a593Smuzhiyun 	 * If we can't obtain the old selector there is not enough
3396*4882a593Smuzhiyun 	 * info to call set_voltage_time_sel().
3397*4882a593Smuzhiyun 	 */
3398*4882a593Smuzhiyun 	if (_regulator_is_enabled(rdev) &&
3399*4882a593Smuzhiyun 	    ops->set_voltage_time_sel && ops->get_voltage_sel) {
3400*4882a593Smuzhiyun 		old_selector = ops->get_voltage_sel(rdev);
3401*4882a593Smuzhiyun 		if (old_selector < 0)
3402*4882a593Smuzhiyun 			return old_selector;
3403*4882a593Smuzhiyun 	}
3404*4882a593Smuzhiyun 
3405*4882a593Smuzhiyun 	if (ops->set_voltage) {
3406*4882a593Smuzhiyun 		ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3407*4882a593Smuzhiyun 						  &selector);
3408*4882a593Smuzhiyun 
3409*4882a593Smuzhiyun 		if (ret >= 0) {
3410*4882a593Smuzhiyun 			if (ops->list_voltage)
3411*4882a593Smuzhiyun 				best_val = ops->list_voltage(rdev,
3412*4882a593Smuzhiyun 							     selector);
3413*4882a593Smuzhiyun 			else
3414*4882a593Smuzhiyun 				best_val = regulator_get_voltage_rdev(rdev);
3415*4882a593Smuzhiyun 		}
3416*4882a593Smuzhiyun 
3417*4882a593Smuzhiyun 	} else if (ops->set_voltage_sel) {
3418*4882a593Smuzhiyun 		ret = regulator_map_voltage(rdev, min_uV, max_uV);
3419*4882a593Smuzhiyun 		if (ret >= 0) {
3420*4882a593Smuzhiyun 			best_val = ops->list_voltage(rdev, ret);
3421*4882a593Smuzhiyun 			if (min_uV <= best_val && max_uV >= best_val) {
3422*4882a593Smuzhiyun 				selector = ret;
3423*4882a593Smuzhiyun 				if (old_selector == selector)
3424*4882a593Smuzhiyun 					ret = 0;
3425*4882a593Smuzhiyun 				else if (rdev->desc->vsel_step)
3426*4882a593Smuzhiyun 					ret = _regulator_set_voltage_sel_step(
3427*4882a593Smuzhiyun 						rdev, best_val, selector);
3428*4882a593Smuzhiyun 				else
3429*4882a593Smuzhiyun 					ret = _regulator_call_set_voltage_sel(
3430*4882a593Smuzhiyun 						rdev, best_val, selector);
3431*4882a593Smuzhiyun 			} else {
3432*4882a593Smuzhiyun 				ret = -EINVAL;
3433*4882a593Smuzhiyun 			}
3434*4882a593Smuzhiyun 		}
3435*4882a593Smuzhiyun 	} else {
3436*4882a593Smuzhiyun 		ret = -EINVAL;
3437*4882a593Smuzhiyun 	}
3438*4882a593Smuzhiyun 
3439*4882a593Smuzhiyun 	if (ret)
3440*4882a593Smuzhiyun 		goto out;
3441*4882a593Smuzhiyun 
3442*4882a593Smuzhiyun 	if (ops->set_voltage_time_sel) {
3443*4882a593Smuzhiyun 		/*
3444*4882a593Smuzhiyun 		 * Call set_voltage_time_sel if successfully obtained
3445*4882a593Smuzhiyun 		 * old_selector
3446*4882a593Smuzhiyun 		 */
3447*4882a593Smuzhiyun 		if (old_selector >= 0 && old_selector != selector)
3448*4882a593Smuzhiyun 			delay = ops->set_voltage_time_sel(rdev, old_selector,
3449*4882a593Smuzhiyun 							  selector);
3450*4882a593Smuzhiyun 	} else {
3451*4882a593Smuzhiyun 		if (old_uV != best_val) {
3452*4882a593Smuzhiyun 			if (ops->set_voltage_time)
3453*4882a593Smuzhiyun 				delay = ops->set_voltage_time(rdev, old_uV,
3454*4882a593Smuzhiyun 							      best_val);
3455*4882a593Smuzhiyun 			else
3456*4882a593Smuzhiyun 				delay = _regulator_set_voltage_time(rdev,
3457*4882a593Smuzhiyun 								    old_uV,
3458*4882a593Smuzhiyun 								    best_val);
3459*4882a593Smuzhiyun 		}
3460*4882a593Smuzhiyun 	}
3461*4882a593Smuzhiyun 
3462*4882a593Smuzhiyun 	if (delay < 0) {
3463*4882a593Smuzhiyun 		rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay));
3464*4882a593Smuzhiyun 		delay = 0;
3465*4882a593Smuzhiyun 	}
3466*4882a593Smuzhiyun 
3467*4882a593Smuzhiyun 	/* Insert any necessary delays */
3468*4882a593Smuzhiyun 	if (delay >= 1000) {
3469*4882a593Smuzhiyun 		mdelay(delay / 1000);
3470*4882a593Smuzhiyun 		udelay(delay % 1000);
3471*4882a593Smuzhiyun 	} else if (delay) {
3472*4882a593Smuzhiyun 		udelay(delay);
3473*4882a593Smuzhiyun 	}
3474*4882a593Smuzhiyun 
3475*4882a593Smuzhiyun 	if (best_val >= 0) {
3476*4882a593Smuzhiyun 		unsigned long data = best_val;
3477*4882a593Smuzhiyun 
3478*4882a593Smuzhiyun 		_notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3479*4882a593Smuzhiyun 				     (void *)data);
3480*4882a593Smuzhiyun 	}
3481*4882a593Smuzhiyun 
3482*4882a593Smuzhiyun out:
3483*4882a593Smuzhiyun 	trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3484*4882a593Smuzhiyun 
3485*4882a593Smuzhiyun 	return ret;
3486*4882a593Smuzhiyun }
3487*4882a593Smuzhiyun 
_regulator_do_set_suspend_voltage(struct regulator_dev * rdev,int min_uV,int max_uV,suspend_state_t state)3488*4882a593Smuzhiyun static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3489*4882a593Smuzhiyun 				  int min_uV, int max_uV, suspend_state_t state)
3490*4882a593Smuzhiyun {
3491*4882a593Smuzhiyun 	struct regulator_state *rstate;
3492*4882a593Smuzhiyun 	int uV, sel;
3493*4882a593Smuzhiyun 
3494*4882a593Smuzhiyun 	rstate = regulator_get_suspend_state(rdev, state);
3495*4882a593Smuzhiyun 	if (rstate == NULL)
3496*4882a593Smuzhiyun 		return -EINVAL;
3497*4882a593Smuzhiyun 
3498*4882a593Smuzhiyun 	if (min_uV < rstate->min_uV)
3499*4882a593Smuzhiyun 		min_uV = rstate->min_uV;
3500*4882a593Smuzhiyun 	if (max_uV > rstate->max_uV)
3501*4882a593Smuzhiyun 		max_uV = rstate->max_uV;
3502*4882a593Smuzhiyun 
3503*4882a593Smuzhiyun 	sel = regulator_map_voltage(rdev, min_uV, max_uV);
3504*4882a593Smuzhiyun 	if (sel < 0)
3505*4882a593Smuzhiyun 		return sel;
3506*4882a593Smuzhiyun 
3507*4882a593Smuzhiyun 	uV = rdev->desc->ops->list_voltage(rdev, sel);
3508*4882a593Smuzhiyun 	if (uV >= min_uV && uV <= max_uV)
3509*4882a593Smuzhiyun 		rstate->uV = uV;
3510*4882a593Smuzhiyun 
3511*4882a593Smuzhiyun 	return 0;
3512*4882a593Smuzhiyun }
3513*4882a593Smuzhiyun 
regulator_set_voltage_unlocked(struct regulator * regulator,int min_uV,int max_uV,suspend_state_t state)3514*4882a593Smuzhiyun static int regulator_set_voltage_unlocked(struct regulator *regulator,
3515*4882a593Smuzhiyun 					  int min_uV, int max_uV,
3516*4882a593Smuzhiyun 					  suspend_state_t state)
3517*4882a593Smuzhiyun {
3518*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
3519*4882a593Smuzhiyun 	struct regulator_voltage *voltage = &regulator->voltage[state];
3520*4882a593Smuzhiyun 	int ret = 0;
3521*4882a593Smuzhiyun 	int old_min_uV, old_max_uV;
3522*4882a593Smuzhiyun 	int current_uV;
3523*4882a593Smuzhiyun 
3524*4882a593Smuzhiyun 	/* If we're setting the same range as last time the change
3525*4882a593Smuzhiyun 	 * should be a noop (some cpufreq implementations use the same
3526*4882a593Smuzhiyun 	 * voltage for multiple frequencies, for example).
3527*4882a593Smuzhiyun 	 */
3528*4882a593Smuzhiyun 	if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3529*4882a593Smuzhiyun 		goto out;
3530*4882a593Smuzhiyun 
3531*4882a593Smuzhiyun 	/* If we're trying to set a range that overlaps the current voltage,
3532*4882a593Smuzhiyun 	 * return successfully even though the regulator does not support
3533*4882a593Smuzhiyun 	 * changing the voltage.
3534*4882a593Smuzhiyun 	 */
3535*4882a593Smuzhiyun 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3536*4882a593Smuzhiyun 		current_uV = regulator_get_voltage_rdev(rdev);
3537*4882a593Smuzhiyun 		if (min_uV <= current_uV && current_uV <= max_uV) {
3538*4882a593Smuzhiyun 			voltage->min_uV = min_uV;
3539*4882a593Smuzhiyun 			voltage->max_uV = max_uV;
3540*4882a593Smuzhiyun 			goto out;
3541*4882a593Smuzhiyun 		}
3542*4882a593Smuzhiyun 	}
3543*4882a593Smuzhiyun 
3544*4882a593Smuzhiyun 	/* sanity check */
3545*4882a593Smuzhiyun 	if (!rdev->desc->ops->set_voltage &&
3546*4882a593Smuzhiyun 	    !rdev->desc->ops->set_voltage_sel) {
3547*4882a593Smuzhiyun 		ret = -EINVAL;
3548*4882a593Smuzhiyun 		goto out;
3549*4882a593Smuzhiyun 	}
3550*4882a593Smuzhiyun 
3551*4882a593Smuzhiyun 	/* constraints check */
3552*4882a593Smuzhiyun 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3553*4882a593Smuzhiyun 	if (ret < 0)
3554*4882a593Smuzhiyun 		goto out;
3555*4882a593Smuzhiyun 
3556*4882a593Smuzhiyun 	/* restore original values in case of error */
3557*4882a593Smuzhiyun 	old_min_uV = voltage->min_uV;
3558*4882a593Smuzhiyun 	old_max_uV = voltage->max_uV;
3559*4882a593Smuzhiyun 	voltage->min_uV = min_uV;
3560*4882a593Smuzhiyun 	voltage->max_uV = max_uV;
3561*4882a593Smuzhiyun 
3562*4882a593Smuzhiyun 	/* for not coupled regulators this will just set the voltage */
3563*4882a593Smuzhiyun 	ret = regulator_balance_voltage(rdev, state);
3564*4882a593Smuzhiyun 	if (ret < 0) {
3565*4882a593Smuzhiyun 		voltage->min_uV = old_min_uV;
3566*4882a593Smuzhiyun 		voltage->max_uV = old_max_uV;
3567*4882a593Smuzhiyun 	}
3568*4882a593Smuzhiyun 
3569*4882a593Smuzhiyun out:
3570*4882a593Smuzhiyun 	return ret;
3571*4882a593Smuzhiyun }
3572*4882a593Smuzhiyun 
regulator_set_voltage_rdev(struct regulator_dev * rdev,int min_uV,int max_uV,suspend_state_t state)3573*4882a593Smuzhiyun int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3574*4882a593Smuzhiyun 			       int max_uV, suspend_state_t state)
3575*4882a593Smuzhiyun {
3576*4882a593Smuzhiyun 	int best_supply_uV = 0;
3577*4882a593Smuzhiyun 	int supply_change_uV = 0;
3578*4882a593Smuzhiyun 	int ret;
3579*4882a593Smuzhiyun 
3580*4882a593Smuzhiyun 	if (rdev->supply &&
3581*4882a593Smuzhiyun 	    regulator_ops_is_valid(rdev->supply->rdev,
3582*4882a593Smuzhiyun 				   REGULATOR_CHANGE_VOLTAGE) &&
3583*4882a593Smuzhiyun 	    (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3584*4882a593Smuzhiyun 					   rdev->desc->ops->get_voltage_sel))) {
3585*4882a593Smuzhiyun 		int current_supply_uV;
3586*4882a593Smuzhiyun 		int selector;
3587*4882a593Smuzhiyun 
3588*4882a593Smuzhiyun 		selector = regulator_map_voltage(rdev, min_uV, max_uV);
3589*4882a593Smuzhiyun 		if (selector < 0) {
3590*4882a593Smuzhiyun 			ret = selector;
3591*4882a593Smuzhiyun 			goto out;
3592*4882a593Smuzhiyun 		}
3593*4882a593Smuzhiyun 
3594*4882a593Smuzhiyun 		best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3595*4882a593Smuzhiyun 		if (best_supply_uV < 0) {
3596*4882a593Smuzhiyun 			ret = best_supply_uV;
3597*4882a593Smuzhiyun 			goto out;
3598*4882a593Smuzhiyun 		}
3599*4882a593Smuzhiyun 
3600*4882a593Smuzhiyun 		best_supply_uV += rdev->desc->min_dropout_uV;
3601*4882a593Smuzhiyun 
3602*4882a593Smuzhiyun 		current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3603*4882a593Smuzhiyun 		if (current_supply_uV < 0) {
3604*4882a593Smuzhiyun 			ret = current_supply_uV;
3605*4882a593Smuzhiyun 			goto out;
3606*4882a593Smuzhiyun 		}
3607*4882a593Smuzhiyun 
3608*4882a593Smuzhiyun 		supply_change_uV = best_supply_uV - current_supply_uV;
3609*4882a593Smuzhiyun 	}
3610*4882a593Smuzhiyun 
3611*4882a593Smuzhiyun 	if (supply_change_uV > 0) {
3612*4882a593Smuzhiyun 		ret = regulator_set_voltage_unlocked(rdev->supply,
3613*4882a593Smuzhiyun 				best_supply_uV, INT_MAX, state);
3614*4882a593Smuzhiyun 		if (ret) {
3615*4882a593Smuzhiyun 			dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n",
3616*4882a593Smuzhiyun 				ERR_PTR(ret));
3617*4882a593Smuzhiyun 			goto out;
3618*4882a593Smuzhiyun 		}
3619*4882a593Smuzhiyun 	}
3620*4882a593Smuzhiyun 
3621*4882a593Smuzhiyun 	if (state == PM_SUSPEND_ON)
3622*4882a593Smuzhiyun 		ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3623*4882a593Smuzhiyun 	else
3624*4882a593Smuzhiyun 		ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3625*4882a593Smuzhiyun 							max_uV, state);
3626*4882a593Smuzhiyun 	if (ret < 0)
3627*4882a593Smuzhiyun 		goto out;
3628*4882a593Smuzhiyun 
3629*4882a593Smuzhiyun 	if (supply_change_uV < 0) {
3630*4882a593Smuzhiyun 		ret = regulator_set_voltage_unlocked(rdev->supply,
3631*4882a593Smuzhiyun 				best_supply_uV, INT_MAX, state);
3632*4882a593Smuzhiyun 		if (ret)
3633*4882a593Smuzhiyun 			dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n",
3634*4882a593Smuzhiyun 				 ERR_PTR(ret));
3635*4882a593Smuzhiyun 		/* No need to fail here */
3636*4882a593Smuzhiyun 		ret = 0;
3637*4882a593Smuzhiyun 	}
3638*4882a593Smuzhiyun 
3639*4882a593Smuzhiyun out:
3640*4882a593Smuzhiyun 	return ret;
3641*4882a593Smuzhiyun }
3642*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3643*4882a593Smuzhiyun 
regulator_limit_voltage_step(struct regulator_dev * rdev,int * current_uV,int * min_uV)3644*4882a593Smuzhiyun static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3645*4882a593Smuzhiyun 					int *current_uV, int *min_uV)
3646*4882a593Smuzhiyun {
3647*4882a593Smuzhiyun 	struct regulation_constraints *constraints = rdev->constraints;
3648*4882a593Smuzhiyun 
3649*4882a593Smuzhiyun 	/* Limit voltage change only if necessary */
3650*4882a593Smuzhiyun 	if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3651*4882a593Smuzhiyun 		return 1;
3652*4882a593Smuzhiyun 
3653*4882a593Smuzhiyun 	if (*current_uV < 0) {
3654*4882a593Smuzhiyun 		*current_uV = regulator_get_voltage_rdev(rdev);
3655*4882a593Smuzhiyun 
3656*4882a593Smuzhiyun 		if (*current_uV < 0)
3657*4882a593Smuzhiyun 			return *current_uV;
3658*4882a593Smuzhiyun 	}
3659*4882a593Smuzhiyun 
3660*4882a593Smuzhiyun 	if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3661*4882a593Smuzhiyun 		return 1;
3662*4882a593Smuzhiyun 
3663*4882a593Smuzhiyun 	/* Clamp target voltage within the given step */
3664*4882a593Smuzhiyun 	if (*current_uV < *min_uV)
3665*4882a593Smuzhiyun 		*min_uV = min(*current_uV + constraints->max_uV_step,
3666*4882a593Smuzhiyun 			      *min_uV);
3667*4882a593Smuzhiyun 	else
3668*4882a593Smuzhiyun 		*min_uV = max(*current_uV - constraints->max_uV_step,
3669*4882a593Smuzhiyun 			      *min_uV);
3670*4882a593Smuzhiyun 
3671*4882a593Smuzhiyun 	return 0;
3672*4882a593Smuzhiyun }
3673*4882a593Smuzhiyun 
regulator_get_optimal_voltage(struct regulator_dev * rdev,int * current_uV,int * min_uV,int * max_uV,suspend_state_t state,int n_coupled)3674*4882a593Smuzhiyun static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3675*4882a593Smuzhiyun 					 int *current_uV,
3676*4882a593Smuzhiyun 					 int *min_uV, int *max_uV,
3677*4882a593Smuzhiyun 					 suspend_state_t state,
3678*4882a593Smuzhiyun 					 int n_coupled)
3679*4882a593Smuzhiyun {
3680*4882a593Smuzhiyun 	struct coupling_desc *c_desc = &rdev->coupling_desc;
3681*4882a593Smuzhiyun 	struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3682*4882a593Smuzhiyun 	struct regulation_constraints *constraints = rdev->constraints;
3683*4882a593Smuzhiyun 	int desired_min_uV = 0, desired_max_uV = INT_MAX;
3684*4882a593Smuzhiyun 	int max_current_uV = 0, min_current_uV = INT_MAX;
3685*4882a593Smuzhiyun 	int highest_min_uV = 0, target_uV, possible_uV;
3686*4882a593Smuzhiyun 	int i, ret, max_spread;
3687*4882a593Smuzhiyun 	bool done;
3688*4882a593Smuzhiyun 
3689*4882a593Smuzhiyun 	*current_uV = -1;
3690*4882a593Smuzhiyun 
3691*4882a593Smuzhiyun 	/*
3692*4882a593Smuzhiyun 	 * If there are no coupled regulators, simply set the voltage
3693*4882a593Smuzhiyun 	 * demanded by consumers.
3694*4882a593Smuzhiyun 	 */
3695*4882a593Smuzhiyun 	if (n_coupled == 1) {
3696*4882a593Smuzhiyun 		/*
3697*4882a593Smuzhiyun 		 * If consumers don't provide any demands, set voltage
3698*4882a593Smuzhiyun 		 * to min_uV
3699*4882a593Smuzhiyun 		 */
3700*4882a593Smuzhiyun 		desired_min_uV = constraints->min_uV;
3701*4882a593Smuzhiyun 		desired_max_uV = constraints->max_uV;
3702*4882a593Smuzhiyun 
3703*4882a593Smuzhiyun 		ret = regulator_check_consumers(rdev,
3704*4882a593Smuzhiyun 						&desired_min_uV,
3705*4882a593Smuzhiyun 						&desired_max_uV, state);
3706*4882a593Smuzhiyun 		if (ret < 0)
3707*4882a593Smuzhiyun 			return ret;
3708*4882a593Smuzhiyun 
3709*4882a593Smuzhiyun 		possible_uV = desired_min_uV;
3710*4882a593Smuzhiyun 		done = true;
3711*4882a593Smuzhiyun 
3712*4882a593Smuzhiyun 		goto finish;
3713*4882a593Smuzhiyun 	}
3714*4882a593Smuzhiyun 
3715*4882a593Smuzhiyun 	/* Find highest min desired voltage */
3716*4882a593Smuzhiyun 	for (i = 0; i < n_coupled; i++) {
3717*4882a593Smuzhiyun 		int tmp_min = 0;
3718*4882a593Smuzhiyun 		int tmp_max = INT_MAX;
3719*4882a593Smuzhiyun 
3720*4882a593Smuzhiyun 		lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3721*4882a593Smuzhiyun 
3722*4882a593Smuzhiyun 		ret = regulator_check_consumers(c_rdevs[i],
3723*4882a593Smuzhiyun 						&tmp_min,
3724*4882a593Smuzhiyun 						&tmp_max, state);
3725*4882a593Smuzhiyun 		if (ret < 0)
3726*4882a593Smuzhiyun 			return ret;
3727*4882a593Smuzhiyun 
3728*4882a593Smuzhiyun 		ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3729*4882a593Smuzhiyun 		if (ret < 0)
3730*4882a593Smuzhiyun 			return ret;
3731*4882a593Smuzhiyun 
3732*4882a593Smuzhiyun 		highest_min_uV = max(highest_min_uV, tmp_min);
3733*4882a593Smuzhiyun 
3734*4882a593Smuzhiyun 		if (i == 0) {
3735*4882a593Smuzhiyun 			desired_min_uV = tmp_min;
3736*4882a593Smuzhiyun 			desired_max_uV = tmp_max;
3737*4882a593Smuzhiyun 		}
3738*4882a593Smuzhiyun 	}
3739*4882a593Smuzhiyun 
3740*4882a593Smuzhiyun 	max_spread = constraints->max_spread[0];
3741*4882a593Smuzhiyun 
3742*4882a593Smuzhiyun 	/*
3743*4882a593Smuzhiyun 	 * Let target_uV be equal to the desired one if possible.
3744*4882a593Smuzhiyun 	 * If not, set it to minimum voltage, allowed by other coupled
3745*4882a593Smuzhiyun 	 * regulators.
3746*4882a593Smuzhiyun 	 */
3747*4882a593Smuzhiyun 	target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3748*4882a593Smuzhiyun 
3749*4882a593Smuzhiyun 	/*
3750*4882a593Smuzhiyun 	 * Find min and max voltages, which currently aren't violating
3751*4882a593Smuzhiyun 	 * max_spread.
3752*4882a593Smuzhiyun 	 */
3753*4882a593Smuzhiyun 	for (i = 1; i < n_coupled; i++) {
3754*4882a593Smuzhiyun 		int tmp_act;
3755*4882a593Smuzhiyun 
3756*4882a593Smuzhiyun 		if (!_regulator_is_enabled(c_rdevs[i]))
3757*4882a593Smuzhiyun 			continue;
3758*4882a593Smuzhiyun 
3759*4882a593Smuzhiyun 		tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3760*4882a593Smuzhiyun 		if (tmp_act < 0)
3761*4882a593Smuzhiyun 			return tmp_act;
3762*4882a593Smuzhiyun 
3763*4882a593Smuzhiyun 		min_current_uV = min(tmp_act, min_current_uV);
3764*4882a593Smuzhiyun 		max_current_uV = max(tmp_act, max_current_uV);
3765*4882a593Smuzhiyun 	}
3766*4882a593Smuzhiyun 
3767*4882a593Smuzhiyun 	/* There aren't any other regulators enabled */
3768*4882a593Smuzhiyun 	if (max_current_uV == 0) {
3769*4882a593Smuzhiyun 		possible_uV = target_uV;
3770*4882a593Smuzhiyun 	} else {
3771*4882a593Smuzhiyun 		/*
3772*4882a593Smuzhiyun 		 * Correct target voltage, so as it currently isn't
3773*4882a593Smuzhiyun 		 * violating max_spread
3774*4882a593Smuzhiyun 		 */
3775*4882a593Smuzhiyun 		possible_uV = max(target_uV, max_current_uV - max_spread);
3776*4882a593Smuzhiyun 		possible_uV = min(possible_uV, min_current_uV + max_spread);
3777*4882a593Smuzhiyun 	}
3778*4882a593Smuzhiyun 
3779*4882a593Smuzhiyun 	if (possible_uV > desired_max_uV)
3780*4882a593Smuzhiyun 		return -EINVAL;
3781*4882a593Smuzhiyun 
3782*4882a593Smuzhiyun 	done = (possible_uV == target_uV);
3783*4882a593Smuzhiyun 	desired_min_uV = possible_uV;
3784*4882a593Smuzhiyun 
3785*4882a593Smuzhiyun finish:
3786*4882a593Smuzhiyun 	/* Apply max_uV_step constraint if necessary */
3787*4882a593Smuzhiyun 	if (state == PM_SUSPEND_ON) {
3788*4882a593Smuzhiyun 		ret = regulator_limit_voltage_step(rdev, current_uV,
3789*4882a593Smuzhiyun 						   &desired_min_uV);
3790*4882a593Smuzhiyun 		if (ret < 0)
3791*4882a593Smuzhiyun 			return ret;
3792*4882a593Smuzhiyun 
3793*4882a593Smuzhiyun 		if (ret == 0)
3794*4882a593Smuzhiyun 			done = false;
3795*4882a593Smuzhiyun 	}
3796*4882a593Smuzhiyun 
3797*4882a593Smuzhiyun 	/* Set current_uV if wasn't done earlier in the code and if necessary */
3798*4882a593Smuzhiyun 	if (n_coupled > 1 && *current_uV == -1) {
3799*4882a593Smuzhiyun 
3800*4882a593Smuzhiyun 		if (_regulator_is_enabled(rdev)) {
3801*4882a593Smuzhiyun 			ret = regulator_get_voltage_rdev(rdev);
3802*4882a593Smuzhiyun 			if (ret < 0)
3803*4882a593Smuzhiyun 				return ret;
3804*4882a593Smuzhiyun 
3805*4882a593Smuzhiyun 			*current_uV = ret;
3806*4882a593Smuzhiyun 		} else {
3807*4882a593Smuzhiyun 			*current_uV = desired_min_uV;
3808*4882a593Smuzhiyun 		}
3809*4882a593Smuzhiyun 	}
3810*4882a593Smuzhiyun 
3811*4882a593Smuzhiyun 	*min_uV = desired_min_uV;
3812*4882a593Smuzhiyun 	*max_uV = desired_max_uV;
3813*4882a593Smuzhiyun 
3814*4882a593Smuzhiyun 	return done;
3815*4882a593Smuzhiyun }
3816*4882a593Smuzhiyun 
regulator_do_balance_voltage(struct regulator_dev * rdev,suspend_state_t state,bool skip_coupled)3817*4882a593Smuzhiyun int regulator_do_balance_voltage(struct regulator_dev *rdev,
3818*4882a593Smuzhiyun 				 suspend_state_t state, bool skip_coupled)
3819*4882a593Smuzhiyun {
3820*4882a593Smuzhiyun 	struct regulator_dev **c_rdevs;
3821*4882a593Smuzhiyun 	struct regulator_dev *best_rdev;
3822*4882a593Smuzhiyun 	struct coupling_desc *c_desc = &rdev->coupling_desc;
3823*4882a593Smuzhiyun 	int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3824*4882a593Smuzhiyun 	unsigned int delta, best_delta;
3825*4882a593Smuzhiyun 	unsigned long c_rdev_done = 0;
3826*4882a593Smuzhiyun 	bool best_c_rdev_done;
3827*4882a593Smuzhiyun 
3828*4882a593Smuzhiyun 	c_rdevs = c_desc->coupled_rdevs;
3829*4882a593Smuzhiyun 	n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
3830*4882a593Smuzhiyun 
3831*4882a593Smuzhiyun 	/*
3832*4882a593Smuzhiyun 	 * Find the best possible voltage change on each loop. Leave the loop
3833*4882a593Smuzhiyun 	 * if there isn't any possible change.
3834*4882a593Smuzhiyun 	 */
3835*4882a593Smuzhiyun 	do {
3836*4882a593Smuzhiyun 		best_c_rdev_done = false;
3837*4882a593Smuzhiyun 		best_delta = 0;
3838*4882a593Smuzhiyun 		best_min_uV = 0;
3839*4882a593Smuzhiyun 		best_max_uV = 0;
3840*4882a593Smuzhiyun 		best_c_rdev = 0;
3841*4882a593Smuzhiyun 		best_rdev = NULL;
3842*4882a593Smuzhiyun 
3843*4882a593Smuzhiyun 		/*
3844*4882a593Smuzhiyun 		 * Find highest difference between optimal voltage
3845*4882a593Smuzhiyun 		 * and current voltage.
3846*4882a593Smuzhiyun 		 */
3847*4882a593Smuzhiyun 		for (i = 0; i < n_coupled; i++) {
3848*4882a593Smuzhiyun 			/*
3849*4882a593Smuzhiyun 			 * optimal_uV is the best voltage that can be set for
3850*4882a593Smuzhiyun 			 * i-th regulator at the moment without violating
3851*4882a593Smuzhiyun 			 * max_spread constraint in order to balance
3852*4882a593Smuzhiyun 			 * the coupled voltages.
3853*4882a593Smuzhiyun 			 */
3854*4882a593Smuzhiyun 			int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3855*4882a593Smuzhiyun 
3856*4882a593Smuzhiyun 			if (test_bit(i, &c_rdev_done))
3857*4882a593Smuzhiyun 				continue;
3858*4882a593Smuzhiyun 
3859*4882a593Smuzhiyun 			ret = regulator_get_optimal_voltage(c_rdevs[i],
3860*4882a593Smuzhiyun 							    &current_uV,
3861*4882a593Smuzhiyun 							    &optimal_uV,
3862*4882a593Smuzhiyun 							    &optimal_max_uV,
3863*4882a593Smuzhiyun 							    state, n_coupled);
3864*4882a593Smuzhiyun 			if (ret < 0)
3865*4882a593Smuzhiyun 				goto out;
3866*4882a593Smuzhiyun 
3867*4882a593Smuzhiyun 			delta = abs(optimal_uV - current_uV);
3868*4882a593Smuzhiyun 
3869*4882a593Smuzhiyun 			if (delta && best_delta <= delta) {
3870*4882a593Smuzhiyun 				best_c_rdev_done = ret;
3871*4882a593Smuzhiyun 				best_delta = delta;
3872*4882a593Smuzhiyun 				best_rdev = c_rdevs[i];
3873*4882a593Smuzhiyun 				best_min_uV = optimal_uV;
3874*4882a593Smuzhiyun 				best_max_uV = optimal_max_uV;
3875*4882a593Smuzhiyun 				best_c_rdev = i;
3876*4882a593Smuzhiyun 			}
3877*4882a593Smuzhiyun 		}
3878*4882a593Smuzhiyun 
3879*4882a593Smuzhiyun 		/* Nothing to change, return successfully */
3880*4882a593Smuzhiyun 		if (!best_rdev) {
3881*4882a593Smuzhiyun 			ret = 0;
3882*4882a593Smuzhiyun 			goto out;
3883*4882a593Smuzhiyun 		}
3884*4882a593Smuzhiyun 
3885*4882a593Smuzhiyun 		ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3886*4882a593Smuzhiyun 						 best_max_uV, state);
3887*4882a593Smuzhiyun 
3888*4882a593Smuzhiyun 		if (ret < 0)
3889*4882a593Smuzhiyun 			goto out;
3890*4882a593Smuzhiyun 
3891*4882a593Smuzhiyun 		if (best_c_rdev_done)
3892*4882a593Smuzhiyun 			set_bit(best_c_rdev, &c_rdev_done);
3893*4882a593Smuzhiyun 
3894*4882a593Smuzhiyun 	} while (n_coupled > 1);
3895*4882a593Smuzhiyun 
3896*4882a593Smuzhiyun out:
3897*4882a593Smuzhiyun 	return ret;
3898*4882a593Smuzhiyun }
3899*4882a593Smuzhiyun 
regulator_balance_voltage(struct regulator_dev * rdev,suspend_state_t state)3900*4882a593Smuzhiyun static int regulator_balance_voltage(struct regulator_dev *rdev,
3901*4882a593Smuzhiyun 				     suspend_state_t state)
3902*4882a593Smuzhiyun {
3903*4882a593Smuzhiyun 	struct coupling_desc *c_desc = &rdev->coupling_desc;
3904*4882a593Smuzhiyun 	struct regulator_coupler *coupler = c_desc->coupler;
3905*4882a593Smuzhiyun 	bool skip_coupled = false;
3906*4882a593Smuzhiyun 
3907*4882a593Smuzhiyun 	/*
3908*4882a593Smuzhiyun 	 * If system is in a state other than PM_SUSPEND_ON, don't check
3909*4882a593Smuzhiyun 	 * other coupled regulators.
3910*4882a593Smuzhiyun 	 */
3911*4882a593Smuzhiyun 	if (state != PM_SUSPEND_ON)
3912*4882a593Smuzhiyun 		skip_coupled = true;
3913*4882a593Smuzhiyun 
3914*4882a593Smuzhiyun 	if (c_desc->n_resolved < c_desc->n_coupled) {
3915*4882a593Smuzhiyun 		rdev_err(rdev, "Not all coupled regulators registered\n");
3916*4882a593Smuzhiyun 		return -EPERM;
3917*4882a593Smuzhiyun 	}
3918*4882a593Smuzhiyun 
3919*4882a593Smuzhiyun 	/* Invoke custom balancer for customized couplers */
3920*4882a593Smuzhiyun 	if (coupler && coupler->balance_voltage)
3921*4882a593Smuzhiyun 		return coupler->balance_voltage(coupler, rdev, state);
3922*4882a593Smuzhiyun 
3923*4882a593Smuzhiyun 	return regulator_do_balance_voltage(rdev, state, skip_coupled);
3924*4882a593Smuzhiyun }
3925*4882a593Smuzhiyun 
3926*4882a593Smuzhiyun /**
3927*4882a593Smuzhiyun  * regulator_set_voltage - set regulator output voltage
3928*4882a593Smuzhiyun  * @regulator: regulator source
3929*4882a593Smuzhiyun  * @min_uV: Minimum required voltage in uV
3930*4882a593Smuzhiyun  * @max_uV: Maximum acceptable voltage in uV
3931*4882a593Smuzhiyun  *
3932*4882a593Smuzhiyun  * Sets a voltage regulator to the desired output voltage. This can be set
3933*4882a593Smuzhiyun  * during any regulator state. IOW, regulator can be disabled or enabled.
3934*4882a593Smuzhiyun  *
3935*4882a593Smuzhiyun  * If the regulator is enabled then the voltage will change to the new value
3936*4882a593Smuzhiyun  * immediately otherwise if the regulator is disabled the regulator will
3937*4882a593Smuzhiyun  * output at the new voltage when enabled.
3938*4882a593Smuzhiyun  *
3939*4882a593Smuzhiyun  * NOTE: If the regulator is shared between several devices then the lowest
3940*4882a593Smuzhiyun  * request voltage that meets the system constraints will be used.
3941*4882a593Smuzhiyun  * Regulator system constraints must be set for this regulator before
3942*4882a593Smuzhiyun  * calling this function otherwise this call will fail.
3943*4882a593Smuzhiyun  */
regulator_set_voltage(struct regulator * regulator,int min_uV,int max_uV)3944*4882a593Smuzhiyun int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3945*4882a593Smuzhiyun {
3946*4882a593Smuzhiyun 	struct ww_acquire_ctx ww_ctx;
3947*4882a593Smuzhiyun 	int ret;
3948*4882a593Smuzhiyun 
3949*4882a593Smuzhiyun 	regulator_lock_dependent(regulator->rdev, &ww_ctx);
3950*4882a593Smuzhiyun 
3951*4882a593Smuzhiyun 	ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3952*4882a593Smuzhiyun 					     PM_SUSPEND_ON);
3953*4882a593Smuzhiyun 
3954*4882a593Smuzhiyun 	regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3955*4882a593Smuzhiyun 
3956*4882a593Smuzhiyun 	return ret;
3957*4882a593Smuzhiyun }
3958*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_set_voltage);
3959*4882a593Smuzhiyun 
regulator_suspend_toggle(struct regulator_dev * rdev,suspend_state_t state,bool en)3960*4882a593Smuzhiyun static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3961*4882a593Smuzhiyun 					   suspend_state_t state, bool en)
3962*4882a593Smuzhiyun {
3963*4882a593Smuzhiyun 	struct regulator_state *rstate;
3964*4882a593Smuzhiyun 
3965*4882a593Smuzhiyun 	rstate = regulator_get_suspend_state(rdev, state);
3966*4882a593Smuzhiyun 	if (rstate == NULL)
3967*4882a593Smuzhiyun 		return -EINVAL;
3968*4882a593Smuzhiyun 
3969*4882a593Smuzhiyun 	if (!rstate->changeable)
3970*4882a593Smuzhiyun 		return -EPERM;
3971*4882a593Smuzhiyun 
3972*4882a593Smuzhiyun 	rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3973*4882a593Smuzhiyun 
3974*4882a593Smuzhiyun 	return 0;
3975*4882a593Smuzhiyun }
3976*4882a593Smuzhiyun 
regulator_suspend_enable(struct regulator_dev * rdev,suspend_state_t state)3977*4882a593Smuzhiyun int regulator_suspend_enable(struct regulator_dev *rdev,
3978*4882a593Smuzhiyun 				    suspend_state_t state)
3979*4882a593Smuzhiyun {
3980*4882a593Smuzhiyun 	return regulator_suspend_toggle(rdev, state, true);
3981*4882a593Smuzhiyun }
3982*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3983*4882a593Smuzhiyun 
regulator_suspend_disable(struct regulator_dev * rdev,suspend_state_t state)3984*4882a593Smuzhiyun int regulator_suspend_disable(struct regulator_dev *rdev,
3985*4882a593Smuzhiyun 				     suspend_state_t state)
3986*4882a593Smuzhiyun {
3987*4882a593Smuzhiyun 	struct regulator *regulator;
3988*4882a593Smuzhiyun 	struct regulator_voltage *voltage;
3989*4882a593Smuzhiyun 
3990*4882a593Smuzhiyun 	/*
3991*4882a593Smuzhiyun 	 * if any consumer wants this regulator device keeping on in
3992*4882a593Smuzhiyun 	 * suspend states, don't set it as disabled.
3993*4882a593Smuzhiyun 	 */
3994*4882a593Smuzhiyun 	list_for_each_entry(regulator, &rdev->consumer_list, list) {
3995*4882a593Smuzhiyun 		voltage = &regulator->voltage[state];
3996*4882a593Smuzhiyun 		if (voltage->min_uV || voltage->max_uV)
3997*4882a593Smuzhiyun 			return 0;
3998*4882a593Smuzhiyun 	}
3999*4882a593Smuzhiyun 
4000*4882a593Smuzhiyun 	return regulator_suspend_toggle(rdev, state, false);
4001*4882a593Smuzhiyun }
4002*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_suspend_disable);
4003*4882a593Smuzhiyun 
_regulator_set_suspend_voltage(struct regulator * regulator,int min_uV,int max_uV,suspend_state_t state)4004*4882a593Smuzhiyun static int _regulator_set_suspend_voltage(struct regulator *regulator,
4005*4882a593Smuzhiyun 					  int min_uV, int max_uV,
4006*4882a593Smuzhiyun 					  suspend_state_t state)
4007*4882a593Smuzhiyun {
4008*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
4009*4882a593Smuzhiyun 	struct regulator_state *rstate;
4010*4882a593Smuzhiyun 
4011*4882a593Smuzhiyun 	rstate = regulator_get_suspend_state(rdev, state);
4012*4882a593Smuzhiyun 	if (rstate == NULL)
4013*4882a593Smuzhiyun 		return -EINVAL;
4014*4882a593Smuzhiyun 
4015*4882a593Smuzhiyun 	if (rstate->min_uV == rstate->max_uV) {
4016*4882a593Smuzhiyun 		rdev_err(rdev, "The suspend voltage can't be changed!\n");
4017*4882a593Smuzhiyun 		return -EPERM;
4018*4882a593Smuzhiyun 	}
4019*4882a593Smuzhiyun 
4020*4882a593Smuzhiyun 	return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
4021*4882a593Smuzhiyun }
4022*4882a593Smuzhiyun 
regulator_set_suspend_voltage(struct regulator * regulator,int min_uV,int max_uV,suspend_state_t state)4023*4882a593Smuzhiyun int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
4024*4882a593Smuzhiyun 				  int max_uV, suspend_state_t state)
4025*4882a593Smuzhiyun {
4026*4882a593Smuzhiyun 	struct ww_acquire_ctx ww_ctx;
4027*4882a593Smuzhiyun 	int ret;
4028*4882a593Smuzhiyun 
4029*4882a593Smuzhiyun 	/* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4030*4882a593Smuzhiyun 	if (regulator_check_states(state) || state == PM_SUSPEND_ON)
4031*4882a593Smuzhiyun 		return -EINVAL;
4032*4882a593Smuzhiyun 
4033*4882a593Smuzhiyun 	regulator_lock_dependent(regulator->rdev, &ww_ctx);
4034*4882a593Smuzhiyun 
4035*4882a593Smuzhiyun 	ret = _regulator_set_suspend_voltage(regulator, min_uV,
4036*4882a593Smuzhiyun 					     max_uV, state);
4037*4882a593Smuzhiyun 
4038*4882a593Smuzhiyun 	regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4039*4882a593Smuzhiyun 
4040*4882a593Smuzhiyun 	return ret;
4041*4882a593Smuzhiyun }
4042*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
4043*4882a593Smuzhiyun 
4044*4882a593Smuzhiyun /**
4045*4882a593Smuzhiyun  * regulator_set_voltage_time - get raise/fall time
4046*4882a593Smuzhiyun  * @regulator: regulator source
4047*4882a593Smuzhiyun  * @old_uV: starting voltage in microvolts
4048*4882a593Smuzhiyun  * @new_uV: target voltage in microvolts
4049*4882a593Smuzhiyun  *
4050*4882a593Smuzhiyun  * Provided with the starting and ending voltage, this function attempts to
4051*4882a593Smuzhiyun  * calculate the time in microseconds required to rise or fall to this new
4052*4882a593Smuzhiyun  * voltage.
4053*4882a593Smuzhiyun  */
regulator_set_voltage_time(struct regulator * regulator,int old_uV,int new_uV)4054*4882a593Smuzhiyun int regulator_set_voltage_time(struct regulator *regulator,
4055*4882a593Smuzhiyun 			       int old_uV, int new_uV)
4056*4882a593Smuzhiyun {
4057*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
4058*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
4059*4882a593Smuzhiyun 	int old_sel = -1;
4060*4882a593Smuzhiyun 	int new_sel = -1;
4061*4882a593Smuzhiyun 	int voltage;
4062*4882a593Smuzhiyun 	int i;
4063*4882a593Smuzhiyun 
4064*4882a593Smuzhiyun 	if (ops->set_voltage_time)
4065*4882a593Smuzhiyun 		return ops->set_voltage_time(rdev, old_uV, new_uV);
4066*4882a593Smuzhiyun 	else if (!ops->set_voltage_time_sel)
4067*4882a593Smuzhiyun 		return _regulator_set_voltage_time(rdev, old_uV, new_uV);
4068*4882a593Smuzhiyun 
4069*4882a593Smuzhiyun 	/* Currently requires operations to do this */
4070*4882a593Smuzhiyun 	if (!ops->list_voltage || !rdev->desc->n_voltages)
4071*4882a593Smuzhiyun 		return -EINVAL;
4072*4882a593Smuzhiyun 
4073*4882a593Smuzhiyun 	for (i = 0; i < rdev->desc->n_voltages; i++) {
4074*4882a593Smuzhiyun 		/* We only look for exact voltage matches here */
4075*4882a593Smuzhiyun 		voltage = regulator_list_voltage(regulator, i);
4076*4882a593Smuzhiyun 		if (voltage < 0)
4077*4882a593Smuzhiyun 			return -EINVAL;
4078*4882a593Smuzhiyun 		if (voltage == 0)
4079*4882a593Smuzhiyun 			continue;
4080*4882a593Smuzhiyun 		if (voltage == old_uV)
4081*4882a593Smuzhiyun 			old_sel = i;
4082*4882a593Smuzhiyun 		if (voltage == new_uV)
4083*4882a593Smuzhiyun 			new_sel = i;
4084*4882a593Smuzhiyun 	}
4085*4882a593Smuzhiyun 
4086*4882a593Smuzhiyun 	if (old_sel < 0 || new_sel < 0)
4087*4882a593Smuzhiyun 		return -EINVAL;
4088*4882a593Smuzhiyun 
4089*4882a593Smuzhiyun 	return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
4090*4882a593Smuzhiyun }
4091*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
4092*4882a593Smuzhiyun 
4093*4882a593Smuzhiyun /**
4094*4882a593Smuzhiyun  * regulator_set_voltage_time_sel - get raise/fall time
4095*4882a593Smuzhiyun  * @rdev: regulator source device
4096*4882a593Smuzhiyun  * @old_selector: selector for starting voltage
4097*4882a593Smuzhiyun  * @new_selector: selector for target voltage
4098*4882a593Smuzhiyun  *
4099*4882a593Smuzhiyun  * Provided with the starting and target voltage selectors, this function
4100*4882a593Smuzhiyun  * returns time in microseconds required to rise or fall to this new voltage
4101*4882a593Smuzhiyun  *
4102*4882a593Smuzhiyun  * Drivers providing ramp_delay in regulation_constraints can use this as their
4103*4882a593Smuzhiyun  * set_voltage_time_sel() operation.
4104*4882a593Smuzhiyun  */
regulator_set_voltage_time_sel(struct regulator_dev * rdev,unsigned int old_selector,unsigned int new_selector)4105*4882a593Smuzhiyun int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4106*4882a593Smuzhiyun 				   unsigned int old_selector,
4107*4882a593Smuzhiyun 				   unsigned int new_selector)
4108*4882a593Smuzhiyun {
4109*4882a593Smuzhiyun 	int old_volt, new_volt;
4110*4882a593Smuzhiyun 
4111*4882a593Smuzhiyun 	/* sanity check */
4112*4882a593Smuzhiyun 	if (!rdev->desc->ops->list_voltage)
4113*4882a593Smuzhiyun 		return -EINVAL;
4114*4882a593Smuzhiyun 
4115*4882a593Smuzhiyun 	old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4116*4882a593Smuzhiyun 	new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4117*4882a593Smuzhiyun 
4118*4882a593Smuzhiyun 	if (rdev->desc->ops->set_voltage_time)
4119*4882a593Smuzhiyun 		return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4120*4882a593Smuzhiyun 							 new_volt);
4121*4882a593Smuzhiyun 	else
4122*4882a593Smuzhiyun 		return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4123*4882a593Smuzhiyun }
4124*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4125*4882a593Smuzhiyun 
4126*4882a593Smuzhiyun /**
4127*4882a593Smuzhiyun  * regulator_sync_voltage - re-apply last regulator output voltage
4128*4882a593Smuzhiyun  * @regulator: regulator source
4129*4882a593Smuzhiyun  *
4130*4882a593Smuzhiyun  * Re-apply the last configured voltage.  This is intended to be used
4131*4882a593Smuzhiyun  * where some external control source the consumer is cooperating with
4132*4882a593Smuzhiyun  * has caused the configured voltage to change.
4133*4882a593Smuzhiyun  */
regulator_sync_voltage(struct regulator * regulator)4134*4882a593Smuzhiyun int regulator_sync_voltage(struct regulator *regulator)
4135*4882a593Smuzhiyun {
4136*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
4137*4882a593Smuzhiyun 	struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
4138*4882a593Smuzhiyun 	int ret, min_uV, max_uV;
4139*4882a593Smuzhiyun 
4140*4882a593Smuzhiyun 	regulator_lock(rdev);
4141*4882a593Smuzhiyun 
4142*4882a593Smuzhiyun 	if (!rdev->desc->ops->set_voltage &&
4143*4882a593Smuzhiyun 	    !rdev->desc->ops->set_voltage_sel) {
4144*4882a593Smuzhiyun 		ret = -EINVAL;
4145*4882a593Smuzhiyun 		goto out;
4146*4882a593Smuzhiyun 	}
4147*4882a593Smuzhiyun 
4148*4882a593Smuzhiyun 	/* This is only going to work if we've had a voltage configured. */
4149*4882a593Smuzhiyun 	if (!voltage->min_uV && !voltage->max_uV) {
4150*4882a593Smuzhiyun 		ret = -EINVAL;
4151*4882a593Smuzhiyun 		goto out;
4152*4882a593Smuzhiyun 	}
4153*4882a593Smuzhiyun 
4154*4882a593Smuzhiyun 	min_uV = voltage->min_uV;
4155*4882a593Smuzhiyun 	max_uV = voltage->max_uV;
4156*4882a593Smuzhiyun 
4157*4882a593Smuzhiyun 	/* This should be a paranoia check... */
4158*4882a593Smuzhiyun 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4159*4882a593Smuzhiyun 	if (ret < 0)
4160*4882a593Smuzhiyun 		goto out;
4161*4882a593Smuzhiyun 
4162*4882a593Smuzhiyun 	ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4163*4882a593Smuzhiyun 	if (ret < 0)
4164*4882a593Smuzhiyun 		goto out;
4165*4882a593Smuzhiyun 
4166*4882a593Smuzhiyun 	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4167*4882a593Smuzhiyun 
4168*4882a593Smuzhiyun out:
4169*4882a593Smuzhiyun 	regulator_unlock(rdev);
4170*4882a593Smuzhiyun 	return ret;
4171*4882a593Smuzhiyun }
4172*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4173*4882a593Smuzhiyun 
regulator_get_voltage_rdev(struct regulator_dev * rdev)4174*4882a593Smuzhiyun int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4175*4882a593Smuzhiyun {
4176*4882a593Smuzhiyun 	int sel, ret;
4177*4882a593Smuzhiyun 	bool bypassed;
4178*4882a593Smuzhiyun 
4179*4882a593Smuzhiyun 	if (rdev->desc->ops->get_bypass) {
4180*4882a593Smuzhiyun 		ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4181*4882a593Smuzhiyun 		if (ret < 0)
4182*4882a593Smuzhiyun 			return ret;
4183*4882a593Smuzhiyun 		if (bypassed) {
4184*4882a593Smuzhiyun 			/* if bypassed the regulator must have a supply */
4185*4882a593Smuzhiyun 			if (!rdev->supply) {
4186*4882a593Smuzhiyun 				rdev_err(rdev,
4187*4882a593Smuzhiyun 					 "bypassed regulator has no supply!\n");
4188*4882a593Smuzhiyun 				return -EPROBE_DEFER;
4189*4882a593Smuzhiyun 			}
4190*4882a593Smuzhiyun 
4191*4882a593Smuzhiyun 			return regulator_get_voltage_rdev(rdev->supply->rdev);
4192*4882a593Smuzhiyun 		}
4193*4882a593Smuzhiyun 	}
4194*4882a593Smuzhiyun 
4195*4882a593Smuzhiyun 	if (rdev->desc->ops->get_voltage_sel) {
4196*4882a593Smuzhiyun 		sel = rdev->desc->ops->get_voltage_sel(rdev);
4197*4882a593Smuzhiyun 		if (sel < 0)
4198*4882a593Smuzhiyun 			return sel;
4199*4882a593Smuzhiyun 		ret = rdev->desc->ops->list_voltage(rdev, sel);
4200*4882a593Smuzhiyun 	} else if (rdev->desc->ops->get_voltage) {
4201*4882a593Smuzhiyun 		ret = rdev->desc->ops->get_voltage(rdev);
4202*4882a593Smuzhiyun 	} else if (rdev->desc->ops->list_voltage) {
4203*4882a593Smuzhiyun 		ret = rdev->desc->ops->list_voltage(rdev, 0);
4204*4882a593Smuzhiyun 	} else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4205*4882a593Smuzhiyun 		ret = rdev->desc->fixed_uV;
4206*4882a593Smuzhiyun 	} else if (rdev->supply) {
4207*4882a593Smuzhiyun 		ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4208*4882a593Smuzhiyun 	} else if (rdev->supply_name) {
4209*4882a593Smuzhiyun 		return -EPROBE_DEFER;
4210*4882a593Smuzhiyun 	} else {
4211*4882a593Smuzhiyun 		return -EINVAL;
4212*4882a593Smuzhiyun 	}
4213*4882a593Smuzhiyun 
4214*4882a593Smuzhiyun 	if (ret < 0)
4215*4882a593Smuzhiyun 		return ret;
4216*4882a593Smuzhiyun 	return ret - rdev->constraints->uV_offset;
4217*4882a593Smuzhiyun }
4218*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4219*4882a593Smuzhiyun 
4220*4882a593Smuzhiyun /**
4221*4882a593Smuzhiyun  * regulator_get_voltage - get regulator output voltage
4222*4882a593Smuzhiyun  * @regulator: regulator source
4223*4882a593Smuzhiyun  *
4224*4882a593Smuzhiyun  * This returns the current regulator voltage in uV.
4225*4882a593Smuzhiyun  *
4226*4882a593Smuzhiyun  * NOTE: If the regulator is disabled it will return the voltage value. This
4227*4882a593Smuzhiyun  * function should not be used to determine regulator state.
4228*4882a593Smuzhiyun  */
regulator_get_voltage(struct regulator * regulator)4229*4882a593Smuzhiyun int regulator_get_voltage(struct regulator *regulator)
4230*4882a593Smuzhiyun {
4231*4882a593Smuzhiyun 	struct ww_acquire_ctx ww_ctx;
4232*4882a593Smuzhiyun 	int ret;
4233*4882a593Smuzhiyun 
4234*4882a593Smuzhiyun 	regulator_lock_dependent(regulator->rdev, &ww_ctx);
4235*4882a593Smuzhiyun 	ret = regulator_get_voltage_rdev(regulator->rdev);
4236*4882a593Smuzhiyun 	regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4237*4882a593Smuzhiyun 
4238*4882a593Smuzhiyun 	return ret;
4239*4882a593Smuzhiyun }
4240*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_voltage);
4241*4882a593Smuzhiyun 
4242*4882a593Smuzhiyun /**
4243*4882a593Smuzhiyun  * regulator_set_current_limit - set regulator output current limit
4244*4882a593Smuzhiyun  * @regulator: regulator source
4245*4882a593Smuzhiyun  * @min_uA: Minimum supported current in uA
4246*4882a593Smuzhiyun  * @max_uA: Maximum supported current in uA
4247*4882a593Smuzhiyun  *
4248*4882a593Smuzhiyun  * Sets current sink to the desired output current. This can be set during
4249*4882a593Smuzhiyun  * any regulator state. IOW, regulator can be disabled or enabled.
4250*4882a593Smuzhiyun  *
4251*4882a593Smuzhiyun  * If the regulator is enabled then the current will change to the new value
4252*4882a593Smuzhiyun  * immediately otherwise if the regulator is disabled the regulator will
4253*4882a593Smuzhiyun  * output at the new current when enabled.
4254*4882a593Smuzhiyun  *
4255*4882a593Smuzhiyun  * NOTE: Regulator system constraints must be set for this regulator before
4256*4882a593Smuzhiyun  * calling this function otherwise this call will fail.
4257*4882a593Smuzhiyun  */
regulator_set_current_limit(struct regulator * regulator,int min_uA,int max_uA)4258*4882a593Smuzhiyun int regulator_set_current_limit(struct regulator *regulator,
4259*4882a593Smuzhiyun 			       int min_uA, int max_uA)
4260*4882a593Smuzhiyun {
4261*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
4262*4882a593Smuzhiyun 	int ret;
4263*4882a593Smuzhiyun 
4264*4882a593Smuzhiyun 	regulator_lock(rdev);
4265*4882a593Smuzhiyun 
4266*4882a593Smuzhiyun 	/* sanity check */
4267*4882a593Smuzhiyun 	if (!rdev->desc->ops->set_current_limit) {
4268*4882a593Smuzhiyun 		ret = -EINVAL;
4269*4882a593Smuzhiyun 		goto out;
4270*4882a593Smuzhiyun 	}
4271*4882a593Smuzhiyun 
4272*4882a593Smuzhiyun 	/* constraints check */
4273*4882a593Smuzhiyun 	ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4274*4882a593Smuzhiyun 	if (ret < 0)
4275*4882a593Smuzhiyun 		goto out;
4276*4882a593Smuzhiyun 
4277*4882a593Smuzhiyun 	ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4278*4882a593Smuzhiyun out:
4279*4882a593Smuzhiyun 	regulator_unlock(rdev);
4280*4882a593Smuzhiyun 	return ret;
4281*4882a593Smuzhiyun }
4282*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4283*4882a593Smuzhiyun 
_regulator_get_current_limit_unlocked(struct regulator_dev * rdev)4284*4882a593Smuzhiyun static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4285*4882a593Smuzhiyun {
4286*4882a593Smuzhiyun 	/* sanity check */
4287*4882a593Smuzhiyun 	if (!rdev->desc->ops->get_current_limit)
4288*4882a593Smuzhiyun 		return -EINVAL;
4289*4882a593Smuzhiyun 
4290*4882a593Smuzhiyun 	return rdev->desc->ops->get_current_limit(rdev);
4291*4882a593Smuzhiyun }
4292*4882a593Smuzhiyun 
_regulator_get_current_limit(struct regulator_dev * rdev)4293*4882a593Smuzhiyun static int _regulator_get_current_limit(struct regulator_dev *rdev)
4294*4882a593Smuzhiyun {
4295*4882a593Smuzhiyun 	int ret;
4296*4882a593Smuzhiyun 
4297*4882a593Smuzhiyun 	regulator_lock(rdev);
4298*4882a593Smuzhiyun 	ret = _regulator_get_current_limit_unlocked(rdev);
4299*4882a593Smuzhiyun 	regulator_unlock(rdev);
4300*4882a593Smuzhiyun 
4301*4882a593Smuzhiyun 	return ret;
4302*4882a593Smuzhiyun }
4303*4882a593Smuzhiyun 
4304*4882a593Smuzhiyun /**
4305*4882a593Smuzhiyun  * regulator_get_current_limit - get regulator output current
4306*4882a593Smuzhiyun  * @regulator: regulator source
4307*4882a593Smuzhiyun  *
4308*4882a593Smuzhiyun  * This returns the current supplied by the specified current sink in uA.
4309*4882a593Smuzhiyun  *
4310*4882a593Smuzhiyun  * NOTE: If the regulator is disabled it will return the current value. This
4311*4882a593Smuzhiyun  * function should not be used to determine regulator state.
4312*4882a593Smuzhiyun  */
regulator_get_current_limit(struct regulator * regulator)4313*4882a593Smuzhiyun int regulator_get_current_limit(struct regulator *regulator)
4314*4882a593Smuzhiyun {
4315*4882a593Smuzhiyun 	return _regulator_get_current_limit(regulator->rdev);
4316*4882a593Smuzhiyun }
4317*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4318*4882a593Smuzhiyun 
4319*4882a593Smuzhiyun /**
4320*4882a593Smuzhiyun  * regulator_set_mode - set regulator operating mode
4321*4882a593Smuzhiyun  * @regulator: regulator source
4322*4882a593Smuzhiyun  * @mode: operating mode - one of the REGULATOR_MODE constants
4323*4882a593Smuzhiyun  *
4324*4882a593Smuzhiyun  * Set regulator operating mode to increase regulator efficiency or improve
4325*4882a593Smuzhiyun  * regulation performance.
4326*4882a593Smuzhiyun  *
4327*4882a593Smuzhiyun  * NOTE: Regulator system constraints must be set for this regulator before
4328*4882a593Smuzhiyun  * calling this function otherwise this call will fail.
4329*4882a593Smuzhiyun  */
regulator_set_mode(struct regulator * regulator,unsigned int mode)4330*4882a593Smuzhiyun int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4331*4882a593Smuzhiyun {
4332*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
4333*4882a593Smuzhiyun 	int ret;
4334*4882a593Smuzhiyun 	int regulator_curr_mode;
4335*4882a593Smuzhiyun 
4336*4882a593Smuzhiyun 	regulator_lock(rdev);
4337*4882a593Smuzhiyun 
4338*4882a593Smuzhiyun 	/* sanity check */
4339*4882a593Smuzhiyun 	if (!rdev->desc->ops->set_mode) {
4340*4882a593Smuzhiyun 		ret = -EINVAL;
4341*4882a593Smuzhiyun 		goto out;
4342*4882a593Smuzhiyun 	}
4343*4882a593Smuzhiyun 
4344*4882a593Smuzhiyun 	/* return if the same mode is requested */
4345*4882a593Smuzhiyun 	if (rdev->desc->ops->get_mode) {
4346*4882a593Smuzhiyun 		regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4347*4882a593Smuzhiyun 		if (regulator_curr_mode == mode) {
4348*4882a593Smuzhiyun 			ret = 0;
4349*4882a593Smuzhiyun 			goto out;
4350*4882a593Smuzhiyun 		}
4351*4882a593Smuzhiyun 	}
4352*4882a593Smuzhiyun 
4353*4882a593Smuzhiyun 	/* constraints check */
4354*4882a593Smuzhiyun 	ret = regulator_mode_constrain(rdev, &mode);
4355*4882a593Smuzhiyun 	if (ret < 0)
4356*4882a593Smuzhiyun 		goto out;
4357*4882a593Smuzhiyun 
4358*4882a593Smuzhiyun 	ret = rdev->desc->ops->set_mode(rdev, mode);
4359*4882a593Smuzhiyun out:
4360*4882a593Smuzhiyun 	regulator_unlock(rdev);
4361*4882a593Smuzhiyun 	return ret;
4362*4882a593Smuzhiyun }
4363*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_set_mode);
4364*4882a593Smuzhiyun 
_regulator_get_mode_unlocked(struct regulator_dev * rdev)4365*4882a593Smuzhiyun static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4366*4882a593Smuzhiyun {
4367*4882a593Smuzhiyun 	/* sanity check */
4368*4882a593Smuzhiyun 	if (!rdev->desc->ops->get_mode)
4369*4882a593Smuzhiyun 		return -EINVAL;
4370*4882a593Smuzhiyun 
4371*4882a593Smuzhiyun 	return rdev->desc->ops->get_mode(rdev);
4372*4882a593Smuzhiyun }
4373*4882a593Smuzhiyun 
_regulator_get_mode(struct regulator_dev * rdev)4374*4882a593Smuzhiyun static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4375*4882a593Smuzhiyun {
4376*4882a593Smuzhiyun 	int ret;
4377*4882a593Smuzhiyun 
4378*4882a593Smuzhiyun 	regulator_lock(rdev);
4379*4882a593Smuzhiyun 	ret = _regulator_get_mode_unlocked(rdev);
4380*4882a593Smuzhiyun 	regulator_unlock(rdev);
4381*4882a593Smuzhiyun 
4382*4882a593Smuzhiyun 	return ret;
4383*4882a593Smuzhiyun }
4384*4882a593Smuzhiyun 
4385*4882a593Smuzhiyun /**
4386*4882a593Smuzhiyun  * regulator_get_mode - get regulator operating mode
4387*4882a593Smuzhiyun  * @regulator: regulator source
4388*4882a593Smuzhiyun  *
4389*4882a593Smuzhiyun  * Get the current regulator operating mode.
4390*4882a593Smuzhiyun  */
regulator_get_mode(struct regulator * regulator)4391*4882a593Smuzhiyun unsigned int regulator_get_mode(struct regulator *regulator)
4392*4882a593Smuzhiyun {
4393*4882a593Smuzhiyun 	return _regulator_get_mode(regulator->rdev);
4394*4882a593Smuzhiyun }
4395*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_mode);
4396*4882a593Smuzhiyun 
_regulator_get_error_flags(struct regulator_dev * rdev,unsigned int * flags)4397*4882a593Smuzhiyun static int _regulator_get_error_flags(struct regulator_dev *rdev,
4398*4882a593Smuzhiyun 					unsigned int *flags)
4399*4882a593Smuzhiyun {
4400*4882a593Smuzhiyun 	int ret;
4401*4882a593Smuzhiyun 
4402*4882a593Smuzhiyun 	regulator_lock(rdev);
4403*4882a593Smuzhiyun 
4404*4882a593Smuzhiyun 	/* sanity check */
4405*4882a593Smuzhiyun 	if (!rdev->desc->ops->get_error_flags) {
4406*4882a593Smuzhiyun 		ret = -EINVAL;
4407*4882a593Smuzhiyun 		goto out;
4408*4882a593Smuzhiyun 	}
4409*4882a593Smuzhiyun 
4410*4882a593Smuzhiyun 	ret = rdev->desc->ops->get_error_flags(rdev, flags);
4411*4882a593Smuzhiyun out:
4412*4882a593Smuzhiyun 	regulator_unlock(rdev);
4413*4882a593Smuzhiyun 	return ret;
4414*4882a593Smuzhiyun }
4415*4882a593Smuzhiyun 
4416*4882a593Smuzhiyun /**
4417*4882a593Smuzhiyun  * regulator_get_error_flags - get regulator error information
4418*4882a593Smuzhiyun  * @regulator: regulator source
4419*4882a593Smuzhiyun  * @flags: pointer to store error flags
4420*4882a593Smuzhiyun  *
4421*4882a593Smuzhiyun  * Get the current regulator error information.
4422*4882a593Smuzhiyun  */
regulator_get_error_flags(struct regulator * regulator,unsigned int * flags)4423*4882a593Smuzhiyun int regulator_get_error_flags(struct regulator *regulator,
4424*4882a593Smuzhiyun 				unsigned int *flags)
4425*4882a593Smuzhiyun {
4426*4882a593Smuzhiyun 	return _regulator_get_error_flags(regulator->rdev, flags);
4427*4882a593Smuzhiyun }
4428*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4429*4882a593Smuzhiyun 
4430*4882a593Smuzhiyun /**
4431*4882a593Smuzhiyun  * regulator_set_load - set regulator load
4432*4882a593Smuzhiyun  * @regulator: regulator source
4433*4882a593Smuzhiyun  * @uA_load: load current
4434*4882a593Smuzhiyun  *
4435*4882a593Smuzhiyun  * Notifies the regulator core of a new device load. This is then used by
4436*4882a593Smuzhiyun  * DRMS (if enabled by constraints) to set the most efficient regulator
4437*4882a593Smuzhiyun  * operating mode for the new regulator loading.
4438*4882a593Smuzhiyun  *
4439*4882a593Smuzhiyun  * Consumer devices notify their supply regulator of the maximum power
4440*4882a593Smuzhiyun  * they will require (can be taken from device datasheet in the power
4441*4882a593Smuzhiyun  * consumption tables) when they change operational status and hence power
4442*4882a593Smuzhiyun  * state. Examples of operational state changes that can affect power
4443*4882a593Smuzhiyun  * consumption are :-
4444*4882a593Smuzhiyun  *
4445*4882a593Smuzhiyun  *    o Device is opened / closed.
4446*4882a593Smuzhiyun  *    o Device I/O is about to begin or has just finished.
4447*4882a593Smuzhiyun  *    o Device is idling in between work.
4448*4882a593Smuzhiyun  *
4449*4882a593Smuzhiyun  * This information is also exported via sysfs to userspace.
4450*4882a593Smuzhiyun  *
4451*4882a593Smuzhiyun  * DRMS will sum the total requested load on the regulator and change
4452*4882a593Smuzhiyun  * to the most efficient operating mode if platform constraints allow.
4453*4882a593Smuzhiyun  *
4454*4882a593Smuzhiyun  * NOTE: when a regulator consumer requests to have a regulator
4455*4882a593Smuzhiyun  * disabled then any load that consumer requested no longer counts
4456*4882a593Smuzhiyun  * toward the total requested load.  If the regulator is re-enabled
4457*4882a593Smuzhiyun  * then the previously requested load will start counting again.
4458*4882a593Smuzhiyun  *
4459*4882a593Smuzhiyun  * If a regulator is an always-on regulator then an individual consumer's
4460*4882a593Smuzhiyun  * load will still be removed if that consumer is fully disabled.
4461*4882a593Smuzhiyun  *
4462*4882a593Smuzhiyun  * On error a negative errno is returned.
4463*4882a593Smuzhiyun  */
regulator_set_load(struct regulator * regulator,int uA_load)4464*4882a593Smuzhiyun int regulator_set_load(struct regulator *regulator, int uA_load)
4465*4882a593Smuzhiyun {
4466*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
4467*4882a593Smuzhiyun 	int old_uA_load;
4468*4882a593Smuzhiyun 	int ret = 0;
4469*4882a593Smuzhiyun 
4470*4882a593Smuzhiyun 	regulator_lock(rdev);
4471*4882a593Smuzhiyun 	old_uA_load = regulator->uA_load;
4472*4882a593Smuzhiyun 	regulator->uA_load = uA_load;
4473*4882a593Smuzhiyun 	if (regulator->enable_count && old_uA_load != uA_load) {
4474*4882a593Smuzhiyun 		ret = drms_uA_update(rdev);
4475*4882a593Smuzhiyun 		if (ret < 0)
4476*4882a593Smuzhiyun 			regulator->uA_load = old_uA_load;
4477*4882a593Smuzhiyun 	}
4478*4882a593Smuzhiyun 	regulator_unlock(rdev);
4479*4882a593Smuzhiyun 
4480*4882a593Smuzhiyun 	return ret;
4481*4882a593Smuzhiyun }
4482*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_set_load);
4483*4882a593Smuzhiyun 
4484*4882a593Smuzhiyun /**
4485*4882a593Smuzhiyun  * regulator_allow_bypass - allow the regulator to go into bypass mode
4486*4882a593Smuzhiyun  *
4487*4882a593Smuzhiyun  * @regulator: Regulator to configure
4488*4882a593Smuzhiyun  * @enable: enable or disable bypass mode
4489*4882a593Smuzhiyun  *
4490*4882a593Smuzhiyun  * Allow the regulator to go into bypass mode if all other consumers
4491*4882a593Smuzhiyun  * for the regulator also enable bypass mode and the machine
4492*4882a593Smuzhiyun  * constraints allow this.  Bypass mode means that the regulator is
4493*4882a593Smuzhiyun  * simply passing the input directly to the output with no regulation.
4494*4882a593Smuzhiyun  */
regulator_allow_bypass(struct regulator * regulator,bool enable)4495*4882a593Smuzhiyun int regulator_allow_bypass(struct regulator *regulator, bool enable)
4496*4882a593Smuzhiyun {
4497*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
4498*4882a593Smuzhiyun 	const char *name = rdev_get_name(rdev);
4499*4882a593Smuzhiyun 	int ret = 0;
4500*4882a593Smuzhiyun 
4501*4882a593Smuzhiyun 	if (!rdev->desc->ops->set_bypass)
4502*4882a593Smuzhiyun 		return 0;
4503*4882a593Smuzhiyun 
4504*4882a593Smuzhiyun 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4505*4882a593Smuzhiyun 		return 0;
4506*4882a593Smuzhiyun 
4507*4882a593Smuzhiyun 	regulator_lock(rdev);
4508*4882a593Smuzhiyun 
4509*4882a593Smuzhiyun 	if (enable && !regulator->bypass) {
4510*4882a593Smuzhiyun 		rdev->bypass_count++;
4511*4882a593Smuzhiyun 
4512*4882a593Smuzhiyun 		if (rdev->bypass_count == rdev->open_count) {
4513*4882a593Smuzhiyun 			trace_regulator_bypass_enable(name);
4514*4882a593Smuzhiyun 
4515*4882a593Smuzhiyun 			ret = rdev->desc->ops->set_bypass(rdev, enable);
4516*4882a593Smuzhiyun 			if (ret != 0)
4517*4882a593Smuzhiyun 				rdev->bypass_count--;
4518*4882a593Smuzhiyun 			else
4519*4882a593Smuzhiyun 				trace_regulator_bypass_enable_complete(name);
4520*4882a593Smuzhiyun 		}
4521*4882a593Smuzhiyun 
4522*4882a593Smuzhiyun 	} else if (!enable && regulator->bypass) {
4523*4882a593Smuzhiyun 		rdev->bypass_count--;
4524*4882a593Smuzhiyun 
4525*4882a593Smuzhiyun 		if (rdev->bypass_count != rdev->open_count) {
4526*4882a593Smuzhiyun 			trace_regulator_bypass_disable(name);
4527*4882a593Smuzhiyun 
4528*4882a593Smuzhiyun 			ret = rdev->desc->ops->set_bypass(rdev, enable);
4529*4882a593Smuzhiyun 			if (ret != 0)
4530*4882a593Smuzhiyun 				rdev->bypass_count++;
4531*4882a593Smuzhiyun 			else
4532*4882a593Smuzhiyun 				trace_regulator_bypass_disable_complete(name);
4533*4882a593Smuzhiyun 		}
4534*4882a593Smuzhiyun 	}
4535*4882a593Smuzhiyun 
4536*4882a593Smuzhiyun 	if (ret == 0)
4537*4882a593Smuzhiyun 		regulator->bypass = enable;
4538*4882a593Smuzhiyun 
4539*4882a593Smuzhiyun 	regulator_unlock(rdev);
4540*4882a593Smuzhiyun 
4541*4882a593Smuzhiyun 	return ret;
4542*4882a593Smuzhiyun }
4543*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4544*4882a593Smuzhiyun 
4545*4882a593Smuzhiyun /**
4546*4882a593Smuzhiyun  * regulator_register_notifier - register regulator event notifier
4547*4882a593Smuzhiyun  * @regulator: regulator source
4548*4882a593Smuzhiyun  * @nb: notifier block
4549*4882a593Smuzhiyun  *
4550*4882a593Smuzhiyun  * Register notifier block to receive regulator events.
4551*4882a593Smuzhiyun  */
regulator_register_notifier(struct regulator * regulator,struct notifier_block * nb)4552*4882a593Smuzhiyun int regulator_register_notifier(struct regulator *regulator,
4553*4882a593Smuzhiyun 			      struct notifier_block *nb)
4554*4882a593Smuzhiyun {
4555*4882a593Smuzhiyun 	return blocking_notifier_chain_register(&regulator->rdev->notifier,
4556*4882a593Smuzhiyun 						nb);
4557*4882a593Smuzhiyun }
4558*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_register_notifier);
4559*4882a593Smuzhiyun 
4560*4882a593Smuzhiyun /**
4561*4882a593Smuzhiyun  * regulator_unregister_notifier - unregister regulator event notifier
4562*4882a593Smuzhiyun  * @regulator: regulator source
4563*4882a593Smuzhiyun  * @nb: notifier block
4564*4882a593Smuzhiyun  *
4565*4882a593Smuzhiyun  * Unregister regulator event notifier block.
4566*4882a593Smuzhiyun  */
regulator_unregister_notifier(struct regulator * regulator,struct notifier_block * nb)4567*4882a593Smuzhiyun int regulator_unregister_notifier(struct regulator *regulator,
4568*4882a593Smuzhiyun 				struct notifier_block *nb)
4569*4882a593Smuzhiyun {
4570*4882a593Smuzhiyun 	return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
4571*4882a593Smuzhiyun 						  nb);
4572*4882a593Smuzhiyun }
4573*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4574*4882a593Smuzhiyun 
4575*4882a593Smuzhiyun /* notify regulator consumers and downstream regulator consumers.
4576*4882a593Smuzhiyun  * Note mutex must be held by caller.
4577*4882a593Smuzhiyun  */
_notifier_call_chain(struct regulator_dev * rdev,unsigned long event,void * data)4578*4882a593Smuzhiyun static int _notifier_call_chain(struct regulator_dev *rdev,
4579*4882a593Smuzhiyun 				  unsigned long event, void *data)
4580*4882a593Smuzhiyun {
4581*4882a593Smuzhiyun 	/* call rdev chain first */
4582*4882a593Smuzhiyun 	return blocking_notifier_call_chain(&rdev->notifier, event, data);
4583*4882a593Smuzhiyun }
4584*4882a593Smuzhiyun 
4585*4882a593Smuzhiyun /**
4586*4882a593Smuzhiyun  * regulator_bulk_get - get multiple regulator consumers
4587*4882a593Smuzhiyun  *
4588*4882a593Smuzhiyun  * @dev:           Device to supply
4589*4882a593Smuzhiyun  * @num_consumers: Number of consumers to register
4590*4882a593Smuzhiyun  * @consumers:     Configuration of consumers; clients are stored here.
4591*4882a593Smuzhiyun  *
4592*4882a593Smuzhiyun  * @return 0 on success, an errno on failure.
4593*4882a593Smuzhiyun  *
4594*4882a593Smuzhiyun  * This helper function allows drivers to get several regulator
4595*4882a593Smuzhiyun  * consumers in one operation.  If any of the regulators cannot be
4596*4882a593Smuzhiyun  * acquired then any regulators that were allocated will be freed
4597*4882a593Smuzhiyun  * before returning to the caller.
4598*4882a593Smuzhiyun  */
regulator_bulk_get(struct device * dev,int num_consumers,struct regulator_bulk_data * consumers)4599*4882a593Smuzhiyun int regulator_bulk_get(struct device *dev, int num_consumers,
4600*4882a593Smuzhiyun 		       struct regulator_bulk_data *consumers)
4601*4882a593Smuzhiyun {
4602*4882a593Smuzhiyun 	int i;
4603*4882a593Smuzhiyun 	int ret;
4604*4882a593Smuzhiyun 
4605*4882a593Smuzhiyun 	for (i = 0; i < num_consumers; i++)
4606*4882a593Smuzhiyun 		consumers[i].consumer = NULL;
4607*4882a593Smuzhiyun 
4608*4882a593Smuzhiyun 	for (i = 0; i < num_consumers; i++) {
4609*4882a593Smuzhiyun 		consumers[i].consumer = regulator_get(dev,
4610*4882a593Smuzhiyun 						      consumers[i].supply);
4611*4882a593Smuzhiyun 		if (IS_ERR(consumers[i].consumer)) {
4612*4882a593Smuzhiyun 			ret = PTR_ERR(consumers[i].consumer);
4613*4882a593Smuzhiyun 			consumers[i].consumer = NULL;
4614*4882a593Smuzhiyun 			goto err;
4615*4882a593Smuzhiyun 		}
4616*4882a593Smuzhiyun 	}
4617*4882a593Smuzhiyun 
4618*4882a593Smuzhiyun 	return 0;
4619*4882a593Smuzhiyun 
4620*4882a593Smuzhiyun err:
4621*4882a593Smuzhiyun 	if (ret != -EPROBE_DEFER)
4622*4882a593Smuzhiyun 		dev_err(dev, "Failed to get supply '%s': %pe\n",
4623*4882a593Smuzhiyun 			consumers[i].supply, ERR_PTR(ret));
4624*4882a593Smuzhiyun 	else
4625*4882a593Smuzhiyun 		dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4626*4882a593Smuzhiyun 			consumers[i].supply);
4627*4882a593Smuzhiyun 
4628*4882a593Smuzhiyun 	while (--i >= 0)
4629*4882a593Smuzhiyun 		regulator_put(consumers[i].consumer);
4630*4882a593Smuzhiyun 
4631*4882a593Smuzhiyun 	return ret;
4632*4882a593Smuzhiyun }
4633*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_bulk_get);
4634*4882a593Smuzhiyun 
regulator_bulk_enable_async(void * data,async_cookie_t cookie)4635*4882a593Smuzhiyun static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4636*4882a593Smuzhiyun {
4637*4882a593Smuzhiyun 	struct regulator_bulk_data *bulk = data;
4638*4882a593Smuzhiyun 
4639*4882a593Smuzhiyun 	bulk->ret = regulator_enable(bulk->consumer);
4640*4882a593Smuzhiyun }
4641*4882a593Smuzhiyun 
4642*4882a593Smuzhiyun /**
4643*4882a593Smuzhiyun  * regulator_bulk_enable - enable multiple regulator consumers
4644*4882a593Smuzhiyun  *
4645*4882a593Smuzhiyun  * @num_consumers: Number of consumers
4646*4882a593Smuzhiyun  * @consumers:     Consumer data; clients are stored here.
4647*4882a593Smuzhiyun  * @return         0 on success, an errno on failure
4648*4882a593Smuzhiyun  *
4649*4882a593Smuzhiyun  * This convenience API allows consumers to enable multiple regulator
4650*4882a593Smuzhiyun  * clients in a single API call.  If any consumers cannot be enabled
4651*4882a593Smuzhiyun  * then any others that were enabled will be disabled again prior to
4652*4882a593Smuzhiyun  * return.
4653*4882a593Smuzhiyun  */
regulator_bulk_enable(int num_consumers,struct regulator_bulk_data * consumers)4654*4882a593Smuzhiyun int regulator_bulk_enable(int num_consumers,
4655*4882a593Smuzhiyun 			  struct regulator_bulk_data *consumers)
4656*4882a593Smuzhiyun {
4657*4882a593Smuzhiyun 	ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4658*4882a593Smuzhiyun 	int i;
4659*4882a593Smuzhiyun 	int ret = 0;
4660*4882a593Smuzhiyun 
4661*4882a593Smuzhiyun 	for (i = 0; i < num_consumers; i++) {
4662*4882a593Smuzhiyun 		async_schedule_domain(regulator_bulk_enable_async,
4663*4882a593Smuzhiyun 				      &consumers[i], &async_domain);
4664*4882a593Smuzhiyun 	}
4665*4882a593Smuzhiyun 
4666*4882a593Smuzhiyun 	async_synchronize_full_domain(&async_domain);
4667*4882a593Smuzhiyun 
4668*4882a593Smuzhiyun 	/* If any consumer failed we need to unwind any that succeeded */
4669*4882a593Smuzhiyun 	for (i = 0; i < num_consumers; i++) {
4670*4882a593Smuzhiyun 		if (consumers[i].ret != 0) {
4671*4882a593Smuzhiyun 			ret = consumers[i].ret;
4672*4882a593Smuzhiyun 			goto err;
4673*4882a593Smuzhiyun 		}
4674*4882a593Smuzhiyun 	}
4675*4882a593Smuzhiyun 
4676*4882a593Smuzhiyun 	return 0;
4677*4882a593Smuzhiyun 
4678*4882a593Smuzhiyun err:
4679*4882a593Smuzhiyun 	for (i = 0; i < num_consumers; i++) {
4680*4882a593Smuzhiyun 		if (consumers[i].ret < 0)
4681*4882a593Smuzhiyun 			pr_err("Failed to enable %s: %pe\n", consumers[i].supply,
4682*4882a593Smuzhiyun 			       ERR_PTR(consumers[i].ret));
4683*4882a593Smuzhiyun 		else
4684*4882a593Smuzhiyun 			regulator_disable(consumers[i].consumer);
4685*4882a593Smuzhiyun 	}
4686*4882a593Smuzhiyun 
4687*4882a593Smuzhiyun 	return ret;
4688*4882a593Smuzhiyun }
4689*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4690*4882a593Smuzhiyun 
4691*4882a593Smuzhiyun /**
4692*4882a593Smuzhiyun  * regulator_bulk_disable - disable multiple regulator consumers
4693*4882a593Smuzhiyun  *
4694*4882a593Smuzhiyun  * @num_consumers: Number of consumers
4695*4882a593Smuzhiyun  * @consumers:     Consumer data; clients are stored here.
4696*4882a593Smuzhiyun  * @return         0 on success, an errno on failure
4697*4882a593Smuzhiyun  *
4698*4882a593Smuzhiyun  * This convenience API allows consumers to disable multiple regulator
4699*4882a593Smuzhiyun  * clients in a single API call.  If any consumers cannot be disabled
4700*4882a593Smuzhiyun  * then any others that were disabled will be enabled again prior to
4701*4882a593Smuzhiyun  * return.
4702*4882a593Smuzhiyun  */
regulator_bulk_disable(int num_consumers,struct regulator_bulk_data * consumers)4703*4882a593Smuzhiyun int regulator_bulk_disable(int num_consumers,
4704*4882a593Smuzhiyun 			   struct regulator_bulk_data *consumers)
4705*4882a593Smuzhiyun {
4706*4882a593Smuzhiyun 	int i;
4707*4882a593Smuzhiyun 	int ret, r;
4708*4882a593Smuzhiyun 
4709*4882a593Smuzhiyun 	for (i = num_consumers - 1; i >= 0; --i) {
4710*4882a593Smuzhiyun 		ret = regulator_disable(consumers[i].consumer);
4711*4882a593Smuzhiyun 		if (ret != 0)
4712*4882a593Smuzhiyun 			goto err;
4713*4882a593Smuzhiyun 	}
4714*4882a593Smuzhiyun 
4715*4882a593Smuzhiyun 	return 0;
4716*4882a593Smuzhiyun 
4717*4882a593Smuzhiyun err:
4718*4882a593Smuzhiyun 	pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret));
4719*4882a593Smuzhiyun 	for (++i; i < num_consumers; ++i) {
4720*4882a593Smuzhiyun 		r = regulator_enable(consumers[i].consumer);
4721*4882a593Smuzhiyun 		if (r != 0)
4722*4882a593Smuzhiyun 			pr_err("Failed to re-enable %s: %pe\n",
4723*4882a593Smuzhiyun 			       consumers[i].supply, ERR_PTR(r));
4724*4882a593Smuzhiyun 	}
4725*4882a593Smuzhiyun 
4726*4882a593Smuzhiyun 	return ret;
4727*4882a593Smuzhiyun }
4728*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4729*4882a593Smuzhiyun 
4730*4882a593Smuzhiyun /**
4731*4882a593Smuzhiyun  * regulator_bulk_force_disable - force disable multiple regulator consumers
4732*4882a593Smuzhiyun  *
4733*4882a593Smuzhiyun  * @num_consumers: Number of consumers
4734*4882a593Smuzhiyun  * @consumers:     Consumer data; clients are stored here.
4735*4882a593Smuzhiyun  * @return         0 on success, an errno on failure
4736*4882a593Smuzhiyun  *
4737*4882a593Smuzhiyun  * This convenience API allows consumers to forcibly disable multiple regulator
4738*4882a593Smuzhiyun  * clients in a single API call.
4739*4882a593Smuzhiyun  * NOTE: This should be used for situations when device damage will
4740*4882a593Smuzhiyun  * likely occur if the regulators are not disabled (e.g. over temp).
4741*4882a593Smuzhiyun  * Although regulator_force_disable function call for some consumers can
4742*4882a593Smuzhiyun  * return error numbers, the function is called for all consumers.
4743*4882a593Smuzhiyun  */
regulator_bulk_force_disable(int num_consumers,struct regulator_bulk_data * consumers)4744*4882a593Smuzhiyun int regulator_bulk_force_disable(int num_consumers,
4745*4882a593Smuzhiyun 			   struct regulator_bulk_data *consumers)
4746*4882a593Smuzhiyun {
4747*4882a593Smuzhiyun 	int i;
4748*4882a593Smuzhiyun 	int ret = 0;
4749*4882a593Smuzhiyun 
4750*4882a593Smuzhiyun 	for (i = 0; i < num_consumers; i++) {
4751*4882a593Smuzhiyun 		consumers[i].ret =
4752*4882a593Smuzhiyun 			    regulator_force_disable(consumers[i].consumer);
4753*4882a593Smuzhiyun 
4754*4882a593Smuzhiyun 		/* Store first error for reporting */
4755*4882a593Smuzhiyun 		if (consumers[i].ret && !ret)
4756*4882a593Smuzhiyun 			ret = consumers[i].ret;
4757*4882a593Smuzhiyun 	}
4758*4882a593Smuzhiyun 
4759*4882a593Smuzhiyun 	return ret;
4760*4882a593Smuzhiyun }
4761*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4762*4882a593Smuzhiyun 
4763*4882a593Smuzhiyun /**
4764*4882a593Smuzhiyun  * regulator_bulk_free - free multiple regulator consumers
4765*4882a593Smuzhiyun  *
4766*4882a593Smuzhiyun  * @num_consumers: Number of consumers
4767*4882a593Smuzhiyun  * @consumers:     Consumer data; clients are stored here.
4768*4882a593Smuzhiyun  *
4769*4882a593Smuzhiyun  * This convenience API allows consumers to free multiple regulator
4770*4882a593Smuzhiyun  * clients in a single API call.
4771*4882a593Smuzhiyun  */
regulator_bulk_free(int num_consumers,struct regulator_bulk_data * consumers)4772*4882a593Smuzhiyun void regulator_bulk_free(int num_consumers,
4773*4882a593Smuzhiyun 			 struct regulator_bulk_data *consumers)
4774*4882a593Smuzhiyun {
4775*4882a593Smuzhiyun 	int i;
4776*4882a593Smuzhiyun 
4777*4882a593Smuzhiyun 	for (i = 0; i < num_consumers; i++) {
4778*4882a593Smuzhiyun 		regulator_put(consumers[i].consumer);
4779*4882a593Smuzhiyun 		consumers[i].consumer = NULL;
4780*4882a593Smuzhiyun 	}
4781*4882a593Smuzhiyun }
4782*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_bulk_free);
4783*4882a593Smuzhiyun 
4784*4882a593Smuzhiyun /**
4785*4882a593Smuzhiyun  * regulator_notifier_call_chain - call regulator event notifier
4786*4882a593Smuzhiyun  * @rdev: regulator source
4787*4882a593Smuzhiyun  * @event: notifier block
4788*4882a593Smuzhiyun  * @data: callback-specific data.
4789*4882a593Smuzhiyun  *
4790*4882a593Smuzhiyun  * Called by regulator drivers to notify clients a regulator event has
4791*4882a593Smuzhiyun  * occurred.
4792*4882a593Smuzhiyun  */
regulator_notifier_call_chain(struct regulator_dev * rdev,unsigned long event,void * data)4793*4882a593Smuzhiyun int regulator_notifier_call_chain(struct regulator_dev *rdev,
4794*4882a593Smuzhiyun 				  unsigned long event, void *data)
4795*4882a593Smuzhiyun {
4796*4882a593Smuzhiyun 	_notifier_call_chain(rdev, event, data);
4797*4882a593Smuzhiyun 	return NOTIFY_DONE;
4798*4882a593Smuzhiyun 
4799*4882a593Smuzhiyun }
4800*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4801*4882a593Smuzhiyun 
4802*4882a593Smuzhiyun /**
4803*4882a593Smuzhiyun  * regulator_mode_to_status - convert a regulator mode into a status
4804*4882a593Smuzhiyun  *
4805*4882a593Smuzhiyun  * @mode: Mode to convert
4806*4882a593Smuzhiyun  *
4807*4882a593Smuzhiyun  * Convert a regulator mode into a status.
4808*4882a593Smuzhiyun  */
regulator_mode_to_status(unsigned int mode)4809*4882a593Smuzhiyun int regulator_mode_to_status(unsigned int mode)
4810*4882a593Smuzhiyun {
4811*4882a593Smuzhiyun 	switch (mode) {
4812*4882a593Smuzhiyun 	case REGULATOR_MODE_FAST:
4813*4882a593Smuzhiyun 		return REGULATOR_STATUS_FAST;
4814*4882a593Smuzhiyun 	case REGULATOR_MODE_NORMAL:
4815*4882a593Smuzhiyun 		return REGULATOR_STATUS_NORMAL;
4816*4882a593Smuzhiyun 	case REGULATOR_MODE_IDLE:
4817*4882a593Smuzhiyun 		return REGULATOR_STATUS_IDLE;
4818*4882a593Smuzhiyun 	case REGULATOR_MODE_STANDBY:
4819*4882a593Smuzhiyun 		return REGULATOR_STATUS_STANDBY;
4820*4882a593Smuzhiyun 	default:
4821*4882a593Smuzhiyun 		return REGULATOR_STATUS_UNDEFINED;
4822*4882a593Smuzhiyun 	}
4823*4882a593Smuzhiyun }
4824*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4825*4882a593Smuzhiyun 
4826*4882a593Smuzhiyun static struct attribute *regulator_dev_attrs[] = {
4827*4882a593Smuzhiyun 	&dev_attr_name.attr,
4828*4882a593Smuzhiyun 	&dev_attr_num_users.attr,
4829*4882a593Smuzhiyun 	&dev_attr_type.attr,
4830*4882a593Smuzhiyun 	&dev_attr_microvolts.attr,
4831*4882a593Smuzhiyun 	&dev_attr_microamps.attr,
4832*4882a593Smuzhiyun 	&dev_attr_opmode.attr,
4833*4882a593Smuzhiyun 	&dev_attr_state.attr,
4834*4882a593Smuzhiyun 	&dev_attr_status.attr,
4835*4882a593Smuzhiyun 	&dev_attr_bypass.attr,
4836*4882a593Smuzhiyun 	&dev_attr_requested_microamps.attr,
4837*4882a593Smuzhiyun 	&dev_attr_min_microvolts.attr,
4838*4882a593Smuzhiyun 	&dev_attr_max_microvolts.attr,
4839*4882a593Smuzhiyun 	&dev_attr_min_microamps.attr,
4840*4882a593Smuzhiyun 	&dev_attr_max_microamps.attr,
4841*4882a593Smuzhiyun 	&dev_attr_suspend_standby_state.attr,
4842*4882a593Smuzhiyun 	&dev_attr_suspend_mem_state.attr,
4843*4882a593Smuzhiyun 	&dev_attr_suspend_disk_state.attr,
4844*4882a593Smuzhiyun 	&dev_attr_suspend_standby_microvolts.attr,
4845*4882a593Smuzhiyun 	&dev_attr_suspend_mem_microvolts.attr,
4846*4882a593Smuzhiyun 	&dev_attr_suspend_disk_microvolts.attr,
4847*4882a593Smuzhiyun 	&dev_attr_suspend_standby_mode.attr,
4848*4882a593Smuzhiyun 	&dev_attr_suspend_mem_mode.attr,
4849*4882a593Smuzhiyun 	&dev_attr_suspend_disk_mode.attr,
4850*4882a593Smuzhiyun 	NULL
4851*4882a593Smuzhiyun };
4852*4882a593Smuzhiyun 
4853*4882a593Smuzhiyun /*
4854*4882a593Smuzhiyun  * To avoid cluttering sysfs (and memory) with useless state, only
4855*4882a593Smuzhiyun  * create attributes that can be meaningfully displayed.
4856*4882a593Smuzhiyun  */
regulator_attr_is_visible(struct kobject * kobj,struct attribute * attr,int idx)4857*4882a593Smuzhiyun static umode_t regulator_attr_is_visible(struct kobject *kobj,
4858*4882a593Smuzhiyun 					 struct attribute *attr, int idx)
4859*4882a593Smuzhiyun {
4860*4882a593Smuzhiyun 	struct device *dev = kobj_to_dev(kobj);
4861*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_to_rdev(dev);
4862*4882a593Smuzhiyun 	const struct regulator_ops *ops = rdev->desc->ops;
4863*4882a593Smuzhiyun 	umode_t mode = attr->mode;
4864*4882a593Smuzhiyun 
4865*4882a593Smuzhiyun 	/* these three are always present */
4866*4882a593Smuzhiyun 	if (attr == &dev_attr_name.attr ||
4867*4882a593Smuzhiyun 	    attr == &dev_attr_num_users.attr ||
4868*4882a593Smuzhiyun 	    attr == &dev_attr_type.attr)
4869*4882a593Smuzhiyun 		return mode;
4870*4882a593Smuzhiyun 
4871*4882a593Smuzhiyun 	/* some attributes need specific methods to be displayed */
4872*4882a593Smuzhiyun 	if (attr == &dev_attr_microvolts.attr) {
4873*4882a593Smuzhiyun 		if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4874*4882a593Smuzhiyun 		    (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4875*4882a593Smuzhiyun 		    (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4876*4882a593Smuzhiyun 		    (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4877*4882a593Smuzhiyun 			return mode;
4878*4882a593Smuzhiyun 		return 0;
4879*4882a593Smuzhiyun 	}
4880*4882a593Smuzhiyun 
4881*4882a593Smuzhiyun 	if (attr == &dev_attr_microamps.attr)
4882*4882a593Smuzhiyun 		return ops->get_current_limit ? mode : 0;
4883*4882a593Smuzhiyun 
4884*4882a593Smuzhiyun 	if (attr == &dev_attr_opmode.attr)
4885*4882a593Smuzhiyun 		return ops->get_mode ? mode : 0;
4886*4882a593Smuzhiyun 
4887*4882a593Smuzhiyun 	if (attr == &dev_attr_state.attr)
4888*4882a593Smuzhiyun 		return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4889*4882a593Smuzhiyun 
4890*4882a593Smuzhiyun 	if (attr == &dev_attr_status.attr)
4891*4882a593Smuzhiyun 		return ops->get_status ? mode : 0;
4892*4882a593Smuzhiyun 
4893*4882a593Smuzhiyun 	if (attr == &dev_attr_bypass.attr)
4894*4882a593Smuzhiyun 		return ops->get_bypass ? mode : 0;
4895*4882a593Smuzhiyun 
4896*4882a593Smuzhiyun 	/* constraints need specific supporting methods */
4897*4882a593Smuzhiyun 	if (attr == &dev_attr_min_microvolts.attr ||
4898*4882a593Smuzhiyun 	    attr == &dev_attr_max_microvolts.attr)
4899*4882a593Smuzhiyun 		return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4900*4882a593Smuzhiyun 
4901*4882a593Smuzhiyun 	if (attr == &dev_attr_min_microamps.attr ||
4902*4882a593Smuzhiyun 	    attr == &dev_attr_max_microamps.attr)
4903*4882a593Smuzhiyun 		return ops->set_current_limit ? mode : 0;
4904*4882a593Smuzhiyun 
4905*4882a593Smuzhiyun 	if (attr == &dev_attr_suspend_standby_state.attr ||
4906*4882a593Smuzhiyun 	    attr == &dev_attr_suspend_mem_state.attr ||
4907*4882a593Smuzhiyun 	    attr == &dev_attr_suspend_disk_state.attr)
4908*4882a593Smuzhiyun 		return mode;
4909*4882a593Smuzhiyun 
4910*4882a593Smuzhiyun 	if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4911*4882a593Smuzhiyun 	    attr == &dev_attr_suspend_mem_microvolts.attr ||
4912*4882a593Smuzhiyun 	    attr == &dev_attr_suspend_disk_microvolts.attr)
4913*4882a593Smuzhiyun 		return ops->set_suspend_voltage ? mode : 0;
4914*4882a593Smuzhiyun 
4915*4882a593Smuzhiyun 	if (attr == &dev_attr_suspend_standby_mode.attr ||
4916*4882a593Smuzhiyun 	    attr == &dev_attr_suspend_mem_mode.attr ||
4917*4882a593Smuzhiyun 	    attr == &dev_attr_suspend_disk_mode.attr)
4918*4882a593Smuzhiyun 		return ops->set_suspend_mode ? mode : 0;
4919*4882a593Smuzhiyun 
4920*4882a593Smuzhiyun 	return mode;
4921*4882a593Smuzhiyun }
4922*4882a593Smuzhiyun 
4923*4882a593Smuzhiyun static const struct attribute_group regulator_dev_group = {
4924*4882a593Smuzhiyun 	.attrs = regulator_dev_attrs,
4925*4882a593Smuzhiyun 	.is_visible = regulator_attr_is_visible,
4926*4882a593Smuzhiyun };
4927*4882a593Smuzhiyun 
4928*4882a593Smuzhiyun static const struct attribute_group *regulator_dev_groups[] = {
4929*4882a593Smuzhiyun 	&regulator_dev_group,
4930*4882a593Smuzhiyun 	NULL
4931*4882a593Smuzhiyun };
4932*4882a593Smuzhiyun 
4933*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
4934*4882a593Smuzhiyun static void rdev_deinit_debugfs(struct regulator_dev *rdev);
4935*4882a593Smuzhiyun #else
rdev_deinit_debugfs(struct regulator_dev * rdev)4936*4882a593Smuzhiyun static inline void rdev_deinit_debugfs(struct regulator_dev *rdev)
4937*4882a593Smuzhiyun {
4938*4882a593Smuzhiyun }
4939*4882a593Smuzhiyun #endif
4940*4882a593Smuzhiyun 
regulator_dev_release(struct device * dev)4941*4882a593Smuzhiyun static void regulator_dev_release(struct device *dev)
4942*4882a593Smuzhiyun {
4943*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_get_drvdata(dev);
4944*4882a593Smuzhiyun 
4945*4882a593Smuzhiyun 	rdev_deinit_debugfs(rdev);
4946*4882a593Smuzhiyun 	kfree(rdev->constraints);
4947*4882a593Smuzhiyun 	of_node_put(rdev->dev.of_node);
4948*4882a593Smuzhiyun 	kfree(rdev);
4949*4882a593Smuzhiyun }
4950*4882a593Smuzhiyun 
4951*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
4952*4882a593Smuzhiyun 
4953*4882a593Smuzhiyun #define MAX_DEBUG_BUF_LEN 50
4954*4882a593Smuzhiyun #define REGULATOR_ALLOW_WRITE_DEBUGFS
4955*4882a593Smuzhiyun 
4956*4882a593Smuzhiyun #ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
4957*4882a593Smuzhiyun 
reg_debug_enable_set(void * data,u64 val)4958*4882a593Smuzhiyun static int reg_debug_enable_set(void *data, u64 val)
4959*4882a593Smuzhiyun {
4960*4882a593Smuzhiyun 	struct regulator *regulator = data;
4961*4882a593Smuzhiyun 	int ret;
4962*4882a593Smuzhiyun 
4963*4882a593Smuzhiyun 	if (val) {
4964*4882a593Smuzhiyun 		ret = regulator_enable(regulator);
4965*4882a593Smuzhiyun 		if (ret)
4966*4882a593Smuzhiyun 			rdev_err(regulator->rdev, "enable failed, ret=%d\n",
4967*4882a593Smuzhiyun 				 ret);
4968*4882a593Smuzhiyun 	} else {
4969*4882a593Smuzhiyun 		ret = regulator_disable(regulator);
4970*4882a593Smuzhiyun 		if (ret)
4971*4882a593Smuzhiyun 			rdev_err(regulator->rdev, "disable failed, ret=%d\n",
4972*4882a593Smuzhiyun 				 ret);
4973*4882a593Smuzhiyun 	}
4974*4882a593Smuzhiyun 
4975*4882a593Smuzhiyun 	return ret;
4976*4882a593Smuzhiyun }
4977*4882a593Smuzhiyun 
reg_debug_force_disable_set(void * data,u64 val)4978*4882a593Smuzhiyun static int reg_debug_force_disable_set(void *data, u64 val)
4979*4882a593Smuzhiyun {
4980*4882a593Smuzhiyun 	struct regulator *regulator = data;
4981*4882a593Smuzhiyun 	int ret = 0;
4982*4882a593Smuzhiyun 
4983*4882a593Smuzhiyun 	if (val > 0) {
4984*4882a593Smuzhiyun 		ret = regulator_force_disable(regulator);
4985*4882a593Smuzhiyun 		if (ret)
4986*4882a593Smuzhiyun 			rdev_err(regulator->rdev, "force_disable failed, ret=%d\n",
4987*4882a593Smuzhiyun 				 ret);
4988*4882a593Smuzhiyun 	}
4989*4882a593Smuzhiyun 
4990*4882a593Smuzhiyun 	return ret;
4991*4882a593Smuzhiyun }
4992*4882a593Smuzhiyun 
4993*4882a593Smuzhiyun 
reg_debug_voltage_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)4994*4882a593Smuzhiyun static ssize_t reg_debug_voltage_write(struct file *file,
4995*4882a593Smuzhiyun 				       const char __user *ubuf,
4996*4882a593Smuzhiyun 				       size_t count,
4997*4882a593Smuzhiyun 				       loff_t *ppos)
4998*4882a593Smuzhiyun {
4999*4882a593Smuzhiyun 	struct regulator *regulator = file->private_data;
5000*4882a593Smuzhiyun 	struct regulator_dev *rdev = regulator->rdev;
5001*4882a593Smuzhiyun 	struct regulator *reg;
5002*4882a593Smuzhiyun 
5003*4882a593Smuzhiyun 	char buf[MAX_DEBUG_BUF_LEN];
5004*4882a593Smuzhiyun 	int ret;
5005*4882a593Smuzhiyun 	int min_uV, max_uV = -1;
5006*4882a593Smuzhiyun 
5007*4882a593Smuzhiyun 	if (count < MAX_DEBUG_BUF_LEN) {
5008*4882a593Smuzhiyun 		if (copy_from_user(buf, ubuf, count))
5009*4882a593Smuzhiyun 			return -EFAULT;
5010*4882a593Smuzhiyun 
5011*4882a593Smuzhiyun 		buf[count] = '\0';
5012*4882a593Smuzhiyun 		ret = kstrtoint(buf, 10, &min_uV);
5013*4882a593Smuzhiyun 
5014*4882a593Smuzhiyun 		/* Check that target voltage were specified. */
5015*4882a593Smuzhiyun 		if (ret || min_uV < 0) {
5016*4882a593Smuzhiyun 			rdev_err(regulator->rdev, "incorrect values specified: \"%s\"; should be: \"target_uV\"\n",
5017*4882a593Smuzhiyun 				 buf);
5018*4882a593Smuzhiyun 			return -EINVAL;
5019*4882a593Smuzhiyun 		}
5020*4882a593Smuzhiyun 
5021*4882a593Smuzhiyun 		max_uV = rdev->constraints->max_uV;
5022*4882a593Smuzhiyun 
5023*4882a593Smuzhiyun 		list_for_each_entry(reg, &rdev->consumer_list, list) {
5024*4882a593Smuzhiyun 			if ((!reg->voltage->min_uV && !reg->voltage->max_uV) ||
5025*4882a593Smuzhiyun 			    (reg == regulator))
5026*4882a593Smuzhiyun 				continue;
5027*4882a593Smuzhiyun 			reg->voltage->min_uV = min_uV;
5028*4882a593Smuzhiyun 			reg->voltage->max_uV = max_uV;
5029*4882a593Smuzhiyun 		}
5030*4882a593Smuzhiyun 
5031*4882a593Smuzhiyun 		ret = regulator_set_voltage(regulator, min_uV, max_uV);
5032*4882a593Smuzhiyun 		if (ret) {
5033*4882a593Smuzhiyun 			rdev_err(regulator->rdev, "set voltage(%d, %d) failed, ret=%d\n",
5034*4882a593Smuzhiyun 				 min_uV, max_uV, ret);
5035*4882a593Smuzhiyun 			return ret;
5036*4882a593Smuzhiyun 		}
5037*4882a593Smuzhiyun 	} else {
5038*4882a593Smuzhiyun 		rdev_err(regulator->rdev, "voltage request string exceeds maximum buffer size\n");
5039*4882a593Smuzhiyun 		return -EINVAL;
5040*4882a593Smuzhiyun 	}
5041*4882a593Smuzhiyun 
5042*4882a593Smuzhiyun 	return count;
5043*4882a593Smuzhiyun }
5044*4882a593Smuzhiyun 
reg_debug_mode_set(void * data,u64 val)5045*4882a593Smuzhiyun static int reg_debug_mode_set(void *data, u64 val)
5046*4882a593Smuzhiyun {
5047*4882a593Smuzhiyun 	struct regulator *regulator = data;
5048*4882a593Smuzhiyun 	unsigned int mode = val;
5049*4882a593Smuzhiyun 	int ret;
5050*4882a593Smuzhiyun 
5051*4882a593Smuzhiyun 	ret = regulator_set_mode(regulator, mode);
5052*4882a593Smuzhiyun 	if (ret)
5053*4882a593Smuzhiyun 		rdev_err(regulator->rdev, "set mode=%u failed, ret=%d\n",
5054*4882a593Smuzhiyun 			 mode, ret);
5055*4882a593Smuzhiyun 
5056*4882a593Smuzhiyun 	return ret;
5057*4882a593Smuzhiyun }
5058*4882a593Smuzhiyun 
reg_debug_set_load(void * data,u64 val)5059*4882a593Smuzhiyun static int reg_debug_set_load(void *data, u64 val)
5060*4882a593Smuzhiyun {
5061*4882a593Smuzhiyun 	struct regulator *regulator = data;
5062*4882a593Smuzhiyun 	int load = val;
5063*4882a593Smuzhiyun 	int ret;
5064*4882a593Smuzhiyun 
5065*4882a593Smuzhiyun 	ret = regulator_set_load(regulator, load);
5066*4882a593Smuzhiyun 	if (ret)
5067*4882a593Smuzhiyun 		rdev_err(regulator->rdev, "set load=%d failed, ret=%d\n",
5068*4882a593Smuzhiyun 			 load, ret);
5069*4882a593Smuzhiyun 
5070*4882a593Smuzhiyun 	return ret;
5071*4882a593Smuzhiyun }
5072*4882a593Smuzhiyun 
5073*4882a593Smuzhiyun #else
5074*4882a593Smuzhiyun #define reg_debug_enable_set NULL
5075*4882a593Smuzhiyun #define reg_debug_force_disable_set NULL
5076*4882a593Smuzhiyun #define reg_debug_voltage_write NULL
5077*4882a593Smuzhiyun #define reg_debug_mode_set NULL
5078*4882a593Smuzhiyun #define reg_debug_set_load NULL
5079*4882a593Smuzhiyun #endif
5080*4882a593Smuzhiyun 
reg_debug_enable_get(void * data,u64 * val)5081*4882a593Smuzhiyun static int reg_debug_enable_get(void *data, u64 *val)
5082*4882a593Smuzhiyun {
5083*4882a593Smuzhiyun 	struct regulator *regulator = data;
5084*4882a593Smuzhiyun 
5085*4882a593Smuzhiyun 	*val = regulator_is_enabled(regulator);
5086*4882a593Smuzhiyun 
5087*4882a593Smuzhiyun 	return 0;
5088*4882a593Smuzhiyun }
5089*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
5090*4882a593Smuzhiyun 			reg_debug_enable_set, "%llu\n");
5091*4882a593Smuzhiyun 
5092*4882a593Smuzhiyun 
5093*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get,
5094*4882a593Smuzhiyun 			reg_debug_force_disable_set, "%llu\n");
5095*4882a593Smuzhiyun 
reg_debug_voltage_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)5096*4882a593Smuzhiyun static ssize_t reg_debug_voltage_read(struct file *file, char __user *ubuf,
5097*4882a593Smuzhiyun 				      size_t count, loff_t *ppos)
5098*4882a593Smuzhiyun {
5099*4882a593Smuzhiyun 	struct regulator *regulator = file->private_data;
5100*4882a593Smuzhiyun 	char buf[MAX_DEBUG_BUF_LEN];
5101*4882a593Smuzhiyun 	int voltage, ret;
5102*4882a593Smuzhiyun 
5103*4882a593Smuzhiyun 	voltage = regulator_get_voltage(regulator);
5104*4882a593Smuzhiyun 
5105*4882a593Smuzhiyun 	ret = snprintf(buf, MAX_DEBUG_BUF_LEN - 1, "%d\n", voltage);
5106*4882a593Smuzhiyun 
5107*4882a593Smuzhiyun 	return simple_read_from_buffer(ubuf, count, ppos, buf, ret);
5108*4882a593Smuzhiyun }
5109*4882a593Smuzhiyun 
reg_debug_voltage_open(struct inode * inode,struct file * file)5110*4882a593Smuzhiyun static int reg_debug_voltage_open(struct inode *inode, struct file *file)
5111*4882a593Smuzhiyun {
5112*4882a593Smuzhiyun 	file->private_data = inode->i_private;
5113*4882a593Smuzhiyun 
5114*4882a593Smuzhiyun 	return 0;
5115*4882a593Smuzhiyun }
5116*4882a593Smuzhiyun 
5117*4882a593Smuzhiyun static const struct file_operations reg_voltage_fops = {
5118*4882a593Smuzhiyun 	.write	= reg_debug_voltage_write,
5119*4882a593Smuzhiyun 	.open   = reg_debug_voltage_open,
5120*4882a593Smuzhiyun 	.read	= reg_debug_voltage_read,
5121*4882a593Smuzhiyun };
5122*4882a593Smuzhiyun 
reg_debug_mode_get(void * data,u64 * val)5123*4882a593Smuzhiyun static int reg_debug_mode_get(void *data, u64 *val)
5124*4882a593Smuzhiyun {
5125*4882a593Smuzhiyun 	struct regulator *regulator = data;
5126*4882a593Smuzhiyun 	int mode;
5127*4882a593Smuzhiyun 
5128*4882a593Smuzhiyun 	mode = regulator_get_mode(regulator);
5129*4882a593Smuzhiyun 	if (mode < 0) {
5130*4882a593Smuzhiyun 		rdev_err(regulator->rdev, "get mode failed, ret=%d\n", mode);
5131*4882a593Smuzhiyun 		return mode;
5132*4882a593Smuzhiyun 	}
5133*4882a593Smuzhiyun 
5134*4882a593Smuzhiyun 	*val = mode;
5135*4882a593Smuzhiyun 
5136*4882a593Smuzhiyun 	return 0;
5137*4882a593Smuzhiyun }
5138*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get, reg_debug_mode_set,
5139*4882a593Smuzhiyun 			"%llu\n");
5140*4882a593Smuzhiyun 
5141*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(reg_set_load_fops, reg_debug_mode_get,
5142*4882a593Smuzhiyun 			reg_debug_set_load, "%llu\n");
5143*4882a593Smuzhiyun 
reg_debug_consumers_show(struct seq_file * m,void * v)5144*4882a593Smuzhiyun static int reg_debug_consumers_show(struct seq_file *m, void *v)
5145*4882a593Smuzhiyun {
5146*4882a593Smuzhiyun 	struct regulator_dev *rdev = m->private;
5147*4882a593Smuzhiyun 	struct regulator *reg;
5148*4882a593Smuzhiyun 	const char *supply_name;
5149*4882a593Smuzhiyun 
5150*4882a593Smuzhiyun 	regulator_lock(rdev);
5151*4882a593Smuzhiyun 
5152*4882a593Smuzhiyun 	/* Print a header if there are consumers. */
5153*4882a593Smuzhiyun 	if (rdev->open_count)
5154*4882a593Smuzhiyun 		seq_printf(m, "%-32s   Min_uV   Max_uV  load_uA\n",
5155*4882a593Smuzhiyun 			   "Device-Supply");
5156*4882a593Smuzhiyun 
5157*4882a593Smuzhiyun 	list_for_each_entry(reg, &rdev->consumer_list, list) {
5158*4882a593Smuzhiyun 		if (reg->supply_name)
5159*4882a593Smuzhiyun 			supply_name = reg->supply_name;
5160*4882a593Smuzhiyun 		else
5161*4882a593Smuzhiyun 			supply_name = "(null)-(null)";
5162*4882a593Smuzhiyun 
5163*4882a593Smuzhiyun 		seq_printf(m, "%-32s %8d %8d %8d\n", supply_name,
5164*4882a593Smuzhiyun 			   reg->voltage->min_uV, reg->voltage->max_uV, reg->uA_load);
5165*4882a593Smuzhiyun 	}
5166*4882a593Smuzhiyun 
5167*4882a593Smuzhiyun 	regulator_unlock(rdev);
5168*4882a593Smuzhiyun 
5169*4882a593Smuzhiyun 	return 0;
5170*4882a593Smuzhiyun }
5171*4882a593Smuzhiyun 
reg_debug_consumers_open(struct inode * inode,struct file * file)5172*4882a593Smuzhiyun static int reg_debug_consumers_open(struct inode *inode, struct file *file)
5173*4882a593Smuzhiyun {
5174*4882a593Smuzhiyun 	return single_open(file, reg_debug_consumers_show, inode->i_private);
5175*4882a593Smuzhiyun }
5176*4882a593Smuzhiyun 
5177*4882a593Smuzhiyun static const struct file_operations reg_consumers_fops = {
5178*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
5179*4882a593Smuzhiyun 	.open		= reg_debug_consumers_open,
5180*4882a593Smuzhiyun 	.read		= seq_read,
5181*4882a593Smuzhiyun 	.llseek		= seq_lseek,
5182*4882a593Smuzhiyun 	.release	= single_release,
5183*4882a593Smuzhiyun };
5184*4882a593Smuzhiyun 
rdev_deinit_debugfs(struct regulator_dev * rdev)5185*4882a593Smuzhiyun static void rdev_deinit_debugfs(struct regulator_dev *rdev)
5186*4882a593Smuzhiyun {
5187*4882a593Smuzhiyun 	struct regulator_limit_volt *reg_debug, *n;
5188*4882a593Smuzhiyun 
5189*4882a593Smuzhiyun 	debugfs_remove_recursive(rdev->debugfs);
5190*4882a593Smuzhiyun 
5191*4882a593Smuzhiyun 	list_for_each_entry_safe(reg_debug, n, &regulator_debug_list, list) {
5192*4882a593Smuzhiyun 		if (reg_debug->reg->rdev == rdev) {
5193*4882a593Smuzhiyun 			reg_debug->reg->debugfs = NULL;
5194*4882a593Smuzhiyun 			list_del(&reg_debug->list);
5195*4882a593Smuzhiyun 			regulator_put(reg_debug->reg);
5196*4882a593Smuzhiyun 			kfree(reg_debug);
5197*4882a593Smuzhiyun 		}
5198*4882a593Smuzhiyun 	}
5199*4882a593Smuzhiyun }
5200*4882a593Smuzhiyun 
rdev_init_debugfs(struct regulator_dev * rdev)5201*4882a593Smuzhiyun static void rdev_init_debugfs(struct regulator_dev *rdev)
5202*4882a593Smuzhiyun {
5203*4882a593Smuzhiyun 	struct device *parent = rdev->dev.parent;
5204*4882a593Smuzhiyun 	const char *rname = rdev_get_name(rdev);
5205*4882a593Smuzhiyun 	char name[NAME_MAX];
5206*4882a593Smuzhiyun 	struct regulator *regulator;
5207*4882a593Smuzhiyun 	const struct regulator_ops *ops;
5208*4882a593Smuzhiyun 	struct regulator_limit_volt *reg_debug;
5209*4882a593Smuzhiyun 	mode_t mode;
5210*4882a593Smuzhiyun 
5211*4882a593Smuzhiyun 	/* Avoid duplicate debugfs directory names */
5212*4882a593Smuzhiyun 	if (parent && rname == rdev->desc->name) {
5213*4882a593Smuzhiyun 		snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
5214*4882a593Smuzhiyun 			 rname);
5215*4882a593Smuzhiyun 		rname = name;
5216*4882a593Smuzhiyun 	}
5217*4882a593Smuzhiyun 
5218*4882a593Smuzhiyun 	rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
5219*4882a593Smuzhiyun 	if (!rdev->debugfs) {
5220*4882a593Smuzhiyun 		rdev_warn(rdev, "Failed to create debugfs directory\n");
5221*4882a593Smuzhiyun 		return;
5222*4882a593Smuzhiyun 	}
5223*4882a593Smuzhiyun 
5224*4882a593Smuzhiyun 	debugfs_create_u32("use_count", 0444, rdev->debugfs,
5225*4882a593Smuzhiyun 			   &rdev->use_count);
5226*4882a593Smuzhiyun 	debugfs_create_u32("open_count", 0444, rdev->debugfs,
5227*4882a593Smuzhiyun 			   &rdev->open_count);
5228*4882a593Smuzhiyun 	debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
5229*4882a593Smuzhiyun 			   &rdev->bypass_count);
5230*4882a593Smuzhiyun 	debugfs_create_file("consumers", 0444, rdev->debugfs, rdev,
5231*4882a593Smuzhiyun 			    &reg_consumers_fops);
5232*4882a593Smuzhiyun 
5233*4882a593Smuzhiyun 	regulator = regulator_get(NULL, rdev_get_name(rdev));
5234*4882a593Smuzhiyun 	if (IS_ERR(regulator)) {
5235*4882a593Smuzhiyun 		rdev_err(rdev, "regulator get failed, ret=%ld\n",
5236*4882a593Smuzhiyun 			 PTR_ERR(regulator));
5237*4882a593Smuzhiyun 		return;
5238*4882a593Smuzhiyun 	}
5239*4882a593Smuzhiyun 
5240*4882a593Smuzhiyun 	reg_debug = kzalloc(sizeof(*reg_debug), GFP_KERNEL);
5241*4882a593Smuzhiyun 	if (reg_debug == NULL) {
5242*4882a593Smuzhiyun 		regulator_put(regulator);
5243*4882a593Smuzhiyun 		return;
5244*4882a593Smuzhiyun 	}
5245*4882a593Smuzhiyun 	reg_debug->reg = regulator;
5246*4882a593Smuzhiyun 	list_add(&reg_debug->list, &regulator_debug_list);
5247*4882a593Smuzhiyun 
5248*4882a593Smuzhiyun 	ops = rdev->desc->ops;
5249*4882a593Smuzhiyun 
5250*4882a593Smuzhiyun 	mode = 0444;
5251*4882a593Smuzhiyun #ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
5252*4882a593Smuzhiyun 	mode |= 0200;
5253*4882a593Smuzhiyun #endif
5254*4882a593Smuzhiyun 	debugfs_create_file("enable", mode, rdev->debugfs, regulator,
5255*4882a593Smuzhiyun 			    &reg_enable_fops);
5256*4882a593Smuzhiyun 
5257*4882a593Smuzhiyun 	mode = 0;
5258*4882a593Smuzhiyun 	if (ops->is_enabled)
5259*4882a593Smuzhiyun 		mode |= 0444;
5260*4882a593Smuzhiyun #ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
5261*4882a593Smuzhiyun 	if (ops->disable)
5262*4882a593Smuzhiyun 		mode |= 0200;
5263*4882a593Smuzhiyun #endif
5264*4882a593Smuzhiyun 	if (mode)
5265*4882a593Smuzhiyun 		debugfs_create_file("force_disable", mode, rdev->debugfs,
5266*4882a593Smuzhiyun 				    regulator, &reg_force_disable_fops);
5267*4882a593Smuzhiyun 
5268*4882a593Smuzhiyun 	mode = 0;
5269*4882a593Smuzhiyun 	if (ops->get_voltage || ops->get_voltage_sel)
5270*4882a593Smuzhiyun 		mode |= 0444;
5271*4882a593Smuzhiyun #ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
5272*4882a593Smuzhiyun 	if (ops->set_voltage || ops->set_voltage_sel)
5273*4882a593Smuzhiyun 		mode |= 0200;
5274*4882a593Smuzhiyun #endif
5275*4882a593Smuzhiyun 	if (mode)
5276*4882a593Smuzhiyun 		debugfs_create_file("voltage", mode, rdev->debugfs, regulator,
5277*4882a593Smuzhiyun 				    &reg_voltage_fops);
5278*4882a593Smuzhiyun 
5279*4882a593Smuzhiyun 	mode = 0;
5280*4882a593Smuzhiyun 	if (ops->get_mode)
5281*4882a593Smuzhiyun 		mode |= 0444;
5282*4882a593Smuzhiyun #ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
5283*4882a593Smuzhiyun 	if (ops->set_mode)
5284*4882a593Smuzhiyun 		mode |= 0200;
5285*4882a593Smuzhiyun #endif
5286*4882a593Smuzhiyun 	if (mode)
5287*4882a593Smuzhiyun 		debugfs_create_file("mode", mode, rdev->debugfs, regulator,
5288*4882a593Smuzhiyun 				    &reg_mode_fops);
5289*4882a593Smuzhiyun 
5290*4882a593Smuzhiyun 	mode = 0;
5291*4882a593Smuzhiyun 	if (ops->get_mode)
5292*4882a593Smuzhiyun 		mode |= 0444;
5293*4882a593Smuzhiyun #ifdef REGULATOR_ALLOW_WRITE_DEBUGFS
5294*4882a593Smuzhiyun 	if (ops->set_load || (ops->get_optimum_mode && ops->set_mode))
5295*4882a593Smuzhiyun 		mode |= 0200;
5296*4882a593Smuzhiyun #endif
5297*4882a593Smuzhiyun 	if (mode)
5298*4882a593Smuzhiyun 		debugfs_create_file("load", mode, rdev->debugfs, regulator,
5299*4882a593Smuzhiyun 				    &reg_set_load_fops);
5300*4882a593Smuzhiyun }
5301*4882a593Smuzhiyun 
5302*4882a593Smuzhiyun #else
rdev_init_debugfs(struct regulator_dev * rdev)5303*4882a593Smuzhiyun static inline void rdev_init_debugfs(struct regulator_dev *rdev)
5304*4882a593Smuzhiyun {
5305*4882a593Smuzhiyun }
5306*4882a593Smuzhiyun #endif
5307*4882a593Smuzhiyun 
regulator_register_resolve_supply(struct device * dev,void * data)5308*4882a593Smuzhiyun static int regulator_register_resolve_supply(struct device *dev, void *data)
5309*4882a593Smuzhiyun {
5310*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_to_rdev(dev);
5311*4882a593Smuzhiyun 
5312*4882a593Smuzhiyun 	if (regulator_resolve_supply(rdev))
5313*4882a593Smuzhiyun 		rdev_dbg(rdev, "unable to resolve supply\n");
5314*4882a593Smuzhiyun 
5315*4882a593Smuzhiyun 	return 0;
5316*4882a593Smuzhiyun }
5317*4882a593Smuzhiyun 
regulator_coupler_register(struct regulator_coupler * coupler)5318*4882a593Smuzhiyun int regulator_coupler_register(struct regulator_coupler *coupler)
5319*4882a593Smuzhiyun {
5320*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
5321*4882a593Smuzhiyun 	list_add_tail(&coupler->list, &regulator_coupler_list);
5322*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
5323*4882a593Smuzhiyun 
5324*4882a593Smuzhiyun 	return 0;
5325*4882a593Smuzhiyun }
5326*4882a593Smuzhiyun 
5327*4882a593Smuzhiyun static struct regulator_coupler *
regulator_find_coupler(struct regulator_dev * rdev)5328*4882a593Smuzhiyun regulator_find_coupler(struct regulator_dev *rdev)
5329*4882a593Smuzhiyun {
5330*4882a593Smuzhiyun 	struct regulator_coupler *coupler;
5331*4882a593Smuzhiyun 	int err;
5332*4882a593Smuzhiyun 
5333*4882a593Smuzhiyun 	/*
5334*4882a593Smuzhiyun 	 * Note that regulators are appended to the list and the generic
5335*4882a593Smuzhiyun 	 * coupler is registered first, hence it will be attached at last
5336*4882a593Smuzhiyun 	 * if nobody cared.
5337*4882a593Smuzhiyun 	 */
5338*4882a593Smuzhiyun 	list_for_each_entry_reverse(coupler, &regulator_coupler_list, list) {
5339*4882a593Smuzhiyun 		err = coupler->attach_regulator(coupler, rdev);
5340*4882a593Smuzhiyun 		if (!err) {
5341*4882a593Smuzhiyun 			if (!coupler->balance_voltage &&
5342*4882a593Smuzhiyun 			    rdev->coupling_desc.n_coupled > 2)
5343*4882a593Smuzhiyun 				goto err_unsupported;
5344*4882a593Smuzhiyun 
5345*4882a593Smuzhiyun 			return coupler;
5346*4882a593Smuzhiyun 		}
5347*4882a593Smuzhiyun 
5348*4882a593Smuzhiyun 		if (err < 0)
5349*4882a593Smuzhiyun 			return ERR_PTR(err);
5350*4882a593Smuzhiyun 
5351*4882a593Smuzhiyun 		if (err == 1)
5352*4882a593Smuzhiyun 			continue;
5353*4882a593Smuzhiyun 
5354*4882a593Smuzhiyun 		break;
5355*4882a593Smuzhiyun 	}
5356*4882a593Smuzhiyun 
5357*4882a593Smuzhiyun 	return ERR_PTR(-EINVAL);
5358*4882a593Smuzhiyun 
5359*4882a593Smuzhiyun err_unsupported:
5360*4882a593Smuzhiyun 	if (coupler->detach_regulator)
5361*4882a593Smuzhiyun 		coupler->detach_regulator(coupler, rdev);
5362*4882a593Smuzhiyun 
5363*4882a593Smuzhiyun 	rdev_err(rdev,
5364*4882a593Smuzhiyun 		"Voltage balancing for multiple regulator couples is unimplemented\n");
5365*4882a593Smuzhiyun 
5366*4882a593Smuzhiyun 	return ERR_PTR(-EPERM);
5367*4882a593Smuzhiyun }
5368*4882a593Smuzhiyun 
regulator_resolve_coupling(struct regulator_dev * rdev)5369*4882a593Smuzhiyun static void regulator_resolve_coupling(struct regulator_dev *rdev)
5370*4882a593Smuzhiyun {
5371*4882a593Smuzhiyun 	struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5372*4882a593Smuzhiyun 	struct coupling_desc *c_desc = &rdev->coupling_desc;
5373*4882a593Smuzhiyun 	int n_coupled = c_desc->n_coupled;
5374*4882a593Smuzhiyun 	struct regulator_dev *c_rdev;
5375*4882a593Smuzhiyun 	int i;
5376*4882a593Smuzhiyun 
5377*4882a593Smuzhiyun 	for (i = 1; i < n_coupled; i++) {
5378*4882a593Smuzhiyun 		/* already resolved */
5379*4882a593Smuzhiyun 		if (c_desc->coupled_rdevs[i])
5380*4882a593Smuzhiyun 			continue;
5381*4882a593Smuzhiyun 
5382*4882a593Smuzhiyun 		c_rdev = of_parse_coupled_regulator(rdev, i - 1);
5383*4882a593Smuzhiyun 
5384*4882a593Smuzhiyun 		if (!c_rdev)
5385*4882a593Smuzhiyun 			continue;
5386*4882a593Smuzhiyun 
5387*4882a593Smuzhiyun 		if (c_rdev->coupling_desc.coupler != coupler) {
5388*4882a593Smuzhiyun 			rdev_err(rdev, "coupler mismatch with %s\n",
5389*4882a593Smuzhiyun 				 rdev_get_name(c_rdev));
5390*4882a593Smuzhiyun 			return;
5391*4882a593Smuzhiyun 		}
5392*4882a593Smuzhiyun 
5393*4882a593Smuzhiyun 		c_desc->coupled_rdevs[i] = c_rdev;
5394*4882a593Smuzhiyun 		c_desc->n_resolved++;
5395*4882a593Smuzhiyun 
5396*4882a593Smuzhiyun 		regulator_resolve_coupling(c_rdev);
5397*4882a593Smuzhiyun 	}
5398*4882a593Smuzhiyun }
5399*4882a593Smuzhiyun 
regulator_remove_coupling(struct regulator_dev * rdev)5400*4882a593Smuzhiyun static void regulator_remove_coupling(struct regulator_dev *rdev)
5401*4882a593Smuzhiyun {
5402*4882a593Smuzhiyun 	struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
5403*4882a593Smuzhiyun 	struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
5404*4882a593Smuzhiyun 	struct regulator_dev *__c_rdev, *c_rdev;
5405*4882a593Smuzhiyun 	unsigned int __n_coupled, n_coupled;
5406*4882a593Smuzhiyun 	int i, k;
5407*4882a593Smuzhiyun 	int err;
5408*4882a593Smuzhiyun 
5409*4882a593Smuzhiyun 	n_coupled = c_desc->n_coupled;
5410*4882a593Smuzhiyun 
5411*4882a593Smuzhiyun 	for (i = 1; i < n_coupled; i++) {
5412*4882a593Smuzhiyun 		c_rdev = c_desc->coupled_rdevs[i];
5413*4882a593Smuzhiyun 
5414*4882a593Smuzhiyun 		if (!c_rdev)
5415*4882a593Smuzhiyun 			continue;
5416*4882a593Smuzhiyun 
5417*4882a593Smuzhiyun 		regulator_lock(c_rdev);
5418*4882a593Smuzhiyun 
5419*4882a593Smuzhiyun 		__c_desc = &c_rdev->coupling_desc;
5420*4882a593Smuzhiyun 		__n_coupled = __c_desc->n_coupled;
5421*4882a593Smuzhiyun 
5422*4882a593Smuzhiyun 		for (k = 1; k < __n_coupled; k++) {
5423*4882a593Smuzhiyun 			__c_rdev = __c_desc->coupled_rdevs[k];
5424*4882a593Smuzhiyun 
5425*4882a593Smuzhiyun 			if (__c_rdev == rdev) {
5426*4882a593Smuzhiyun 				__c_desc->coupled_rdevs[k] = NULL;
5427*4882a593Smuzhiyun 				__c_desc->n_resolved--;
5428*4882a593Smuzhiyun 				break;
5429*4882a593Smuzhiyun 			}
5430*4882a593Smuzhiyun 		}
5431*4882a593Smuzhiyun 
5432*4882a593Smuzhiyun 		regulator_unlock(c_rdev);
5433*4882a593Smuzhiyun 
5434*4882a593Smuzhiyun 		c_desc->coupled_rdevs[i] = NULL;
5435*4882a593Smuzhiyun 		c_desc->n_resolved--;
5436*4882a593Smuzhiyun 	}
5437*4882a593Smuzhiyun 
5438*4882a593Smuzhiyun 	if (coupler && coupler->detach_regulator) {
5439*4882a593Smuzhiyun 		err = coupler->detach_regulator(coupler, rdev);
5440*4882a593Smuzhiyun 		if (err)
5441*4882a593Smuzhiyun 			rdev_err(rdev, "failed to detach from coupler: %pe\n",
5442*4882a593Smuzhiyun 				 ERR_PTR(err));
5443*4882a593Smuzhiyun 	}
5444*4882a593Smuzhiyun 
5445*4882a593Smuzhiyun 	kfree(rdev->coupling_desc.coupled_rdevs);
5446*4882a593Smuzhiyun 	rdev->coupling_desc.coupled_rdevs = NULL;
5447*4882a593Smuzhiyun }
5448*4882a593Smuzhiyun 
regulator_init_coupling(struct regulator_dev * rdev)5449*4882a593Smuzhiyun static int regulator_init_coupling(struct regulator_dev *rdev)
5450*4882a593Smuzhiyun {
5451*4882a593Smuzhiyun 	struct regulator_dev **coupled;
5452*4882a593Smuzhiyun 	int err, n_phandles;
5453*4882a593Smuzhiyun 
5454*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_OF))
5455*4882a593Smuzhiyun 		n_phandles = 0;
5456*4882a593Smuzhiyun 	else
5457*4882a593Smuzhiyun 		n_phandles = of_get_n_coupled(rdev);
5458*4882a593Smuzhiyun 
5459*4882a593Smuzhiyun 	coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL);
5460*4882a593Smuzhiyun 	if (!coupled)
5461*4882a593Smuzhiyun 		return -ENOMEM;
5462*4882a593Smuzhiyun 
5463*4882a593Smuzhiyun 	rdev->coupling_desc.coupled_rdevs = coupled;
5464*4882a593Smuzhiyun 
5465*4882a593Smuzhiyun 	/*
5466*4882a593Smuzhiyun 	 * Every regulator should always have coupling descriptor filled with
5467*4882a593Smuzhiyun 	 * at least pointer to itself.
5468*4882a593Smuzhiyun 	 */
5469*4882a593Smuzhiyun 	rdev->coupling_desc.coupled_rdevs[0] = rdev;
5470*4882a593Smuzhiyun 	rdev->coupling_desc.n_coupled = n_phandles + 1;
5471*4882a593Smuzhiyun 	rdev->coupling_desc.n_resolved++;
5472*4882a593Smuzhiyun 
5473*4882a593Smuzhiyun 	/* regulator isn't coupled */
5474*4882a593Smuzhiyun 	if (n_phandles == 0)
5475*4882a593Smuzhiyun 		return 0;
5476*4882a593Smuzhiyun 
5477*4882a593Smuzhiyun 	if (!of_check_coupling_data(rdev))
5478*4882a593Smuzhiyun 		return -EPERM;
5479*4882a593Smuzhiyun 
5480*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
5481*4882a593Smuzhiyun 	rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5482*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
5483*4882a593Smuzhiyun 
5484*4882a593Smuzhiyun 	if (IS_ERR(rdev->coupling_desc.coupler)) {
5485*4882a593Smuzhiyun 		err = PTR_ERR(rdev->coupling_desc.coupler);
5486*4882a593Smuzhiyun 		rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err));
5487*4882a593Smuzhiyun 		return err;
5488*4882a593Smuzhiyun 	}
5489*4882a593Smuzhiyun 
5490*4882a593Smuzhiyun 	return 0;
5491*4882a593Smuzhiyun }
5492*4882a593Smuzhiyun 
generic_coupler_attach(struct regulator_coupler * coupler,struct regulator_dev * rdev)5493*4882a593Smuzhiyun static int generic_coupler_attach(struct regulator_coupler *coupler,
5494*4882a593Smuzhiyun 				  struct regulator_dev *rdev)
5495*4882a593Smuzhiyun {
5496*4882a593Smuzhiyun 	if (rdev->coupling_desc.n_coupled > 2) {
5497*4882a593Smuzhiyun 		rdev_err(rdev,
5498*4882a593Smuzhiyun 			 "Voltage balancing for multiple regulator couples is unimplemented\n");
5499*4882a593Smuzhiyun 		return -EPERM;
5500*4882a593Smuzhiyun 	}
5501*4882a593Smuzhiyun 
5502*4882a593Smuzhiyun 	if (!rdev->constraints->always_on) {
5503*4882a593Smuzhiyun 		rdev_err(rdev,
5504*4882a593Smuzhiyun 			 "Coupling of a non always-on regulator is unimplemented\n");
5505*4882a593Smuzhiyun 		return -ENOTSUPP;
5506*4882a593Smuzhiyun 	}
5507*4882a593Smuzhiyun 
5508*4882a593Smuzhiyun 	return 0;
5509*4882a593Smuzhiyun }
5510*4882a593Smuzhiyun 
5511*4882a593Smuzhiyun static struct regulator_coupler generic_regulator_coupler = {
5512*4882a593Smuzhiyun 	.attach_regulator = generic_coupler_attach,
5513*4882a593Smuzhiyun };
5514*4882a593Smuzhiyun 
5515*4882a593Smuzhiyun /**
5516*4882a593Smuzhiyun  * regulator_register - register regulator
5517*4882a593Smuzhiyun  * @regulator_desc: regulator to register
5518*4882a593Smuzhiyun  * @cfg: runtime configuration for regulator
5519*4882a593Smuzhiyun  *
5520*4882a593Smuzhiyun  * Called by regulator drivers to register a regulator.
5521*4882a593Smuzhiyun  * Returns a valid pointer to struct regulator_dev on success
5522*4882a593Smuzhiyun  * or an ERR_PTR() on error.
5523*4882a593Smuzhiyun  */
5524*4882a593Smuzhiyun struct regulator_dev *
regulator_register(const struct regulator_desc * regulator_desc,const struct regulator_config * cfg)5525*4882a593Smuzhiyun regulator_register(const struct regulator_desc *regulator_desc,
5526*4882a593Smuzhiyun 		   const struct regulator_config *cfg)
5527*4882a593Smuzhiyun {
5528*4882a593Smuzhiyun 	const struct regulator_init_data *init_data;
5529*4882a593Smuzhiyun 	struct regulator_config *config = NULL;
5530*4882a593Smuzhiyun 	static atomic_t regulator_no = ATOMIC_INIT(-1);
5531*4882a593Smuzhiyun 	struct regulator_dev *rdev;
5532*4882a593Smuzhiyun 	bool dangling_cfg_gpiod = false;
5533*4882a593Smuzhiyun 	bool dangling_of_gpiod = false;
5534*4882a593Smuzhiyun 	struct device *dev;
5535*4882a593Smuzhiyun 	int ret, i;
5536*4882a593Smuzhiyun 
5537*4882a593Smuzhiyun 	if (cfg == NULL)
5538*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
5539*4882a593Smuzhiyun 	if (cfg->ena_gpiod)
5540*4882a593Smuzhiyun 		dangling_cfg_gpiod = true;
5541*4882a593Smuzhiyun 	if (regulator_desc == NULL) {
5542*4882a593Smuzhiyun 		ret = -EINVAL;
5543*4882a593Smuzhiyun 		goto rinse;
5544*4882a593Smuzhiyun 	}
5545*4882a593Smuzhiyun 
5546*4882a593Smuzhiyun 	dev = cfg->dev;
5547*4882a593Smuzhiyun 	WARN_ON(!dev);
5548*4882a593Smuzhiyun 
5549*4882a593Smuzhiyun 	if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5550*4882a593Smuzhiyun 		ret = -EINVAL;
5551*4882a593Smuzhiyun 		goto rinse;
5552*4882a593Smuzhiyun 	}
5553*4882a593Smuzhiyun 
5554*4882a593Smuzhiyun 	if (regulator_desc->type != REGULATOR_VOLTAGE &&
5555*4882a593Smuzhiyun 	    regulator_desc->type != REGULATOR_CURRENT) {
5556*4882a593Smuzhiyun 		ret = -EINVAL;
5557*4882a593Smuzhiyun 		goto rinse;
5558*4882a593Smuzhiyun 	}
5559*4882a593Smuzhiyun 
5560*4882a593Smuzhiyun 	/* Only one of each should be implemented */
5561*4882a593Smuzhiyun 	WARN_ON(regulator_desc->ops->get_voltage &&
5562*4882a593Smuzhiyun 		regulator_desc->ops->get_voltage_sel);
5563*4882a593Smuzhiyun 	WARN_ON(regulator_desc->ops->set_voltage &&
5564*4882a593Smuzhiyun 		regulator_desc->ops->set_voltage_sel);
5565*4882a593Smuzhiyun 
5566*4882a593Smuzhiyun 	/* If we're using selectors we must implement list_voltage. */
5567*4882a593Smuzhiyun 	if (regulator_desc->ops->get_voltage_sel &&
5568*4882a593Smuzhiyun 	    !regulator_desc->ops->list_voltage) {
5569*4882a593Smuzhiyun 		ret = -EINVAL;
5570*4882a593Smuzhiyun 		goto rinse;
5571*4882a593Smuzhiyun 	}
5572*4882a593Smuzhiyun 	if (regulator_desc->ops->set_voltage_sel &&
5573*4882a593Smuzhiyun 	    !regulator_desc->ops->list_voltage) {
5574*4882a593Smuzhiyun 		ret = -EINVAL;
5575*4882a593Smuzhiyun 		goto rinse;
5576*4882a593Smuzhiyun 	}
5577*4882a593Smuzhiyun 
5578*4882a593Smuzhiyun 	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5579*4882a593Smuzhiyun 	if (rdev == NULL) {
5580*4882a593Smuzhiyun 		ret = -ENOMEM;
5581*4882a593Smuzhiyun 		goto rinse;
5582*4882a593Smuzhiyun 	}
5583*4882a593Smuzhiyun 	device_initialize(&rdev->dev);
5584*4882a593Smuzhiyun 
5585*4882a593Smuzhiyun 	/*
5586*4882a593Smuzhiyun 	 * Duplicate the config so the driver could override it after
5587*4882a593Smuzhiyun 	 * parsing init data.
5588*4882a593Smuzhiyun 	 */
5589*4882a593Smuzhiyun 	config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5590*4882a593Smuzhiyun 	if (config == NULL) {
5591*4882a593Smuzhiyun 		ret = -ENOMEM;
5592*4882a593Smuzhiyun 		goto clean;
5593*4882a593Smuzhiyun 	}
5594*4882a593Smuzhiyun 
5595*4882a593Smuzhiyun 	init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5596*4882a593Smuzhiyun 					       &rdev->dev.of_node);
5597*4882a593Smuzhiyun 
5598*4882a593Smuzhiyun 	/*
5599*4882a593Smuzhiyun 	 * Sometimes not all resources are probed already so we need to take
5600*4882a593Smuzhiyun 	 * that into account. This happens most the time if the ena_gpiod comes
5601*4882a593Smuzhiyun 	 * from a gpio extender or something else.
5602*4882a593Smuzhiyun 	 */
5603*4882a593Smuzhiyun 	if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5604*4882a593Smuzhiyun 		ret = -EPROBE_DEFER;
5605*4882a593Smuzhiyun 		goto clean;
5606*4882a593Smuzhiyun 	}
5607*4882a593Smuzhiyun 
5608*4882a593Smuzhiyun 	/*
5609*4882a593Smuzhiyun 	 * We need to keep track of any GPIO descriptor coming from the
5610*4882a593Smuzhiyun 	 * device tree until we have handled it over to the core. If the
5611*4882a593Smuzhiyun 	 * config that was passed in to this function DOES NOT contain
5612*4882a593Smuzhiyun 	 * a descriptor, and the config after this call DOES contain
5613*4882a593Smuzhiyun 	 * a descriptor, we definitely got one from parsing the device
5614*4882a593Smuzhiyun 	 * tree.
5615*4882a593Smuzhiyun 	 */
5616*4882a593Smuzhiyun 	if (!cfg->ena_gpiod && config->ena_gpiod)
5617*4882a593Smuzhiyun 		dangling_of_gpiod = true;
5618*4882a593Smuzhiyun 	if (!init_data) {
5619*4882a593Smuzhiyun 		init_data = config->init_data;
5620*4882a593Smuzhiyun 		rdev->dev.of_node = of_node_get(config->of_node);
5621*4882a593Smuzhiyun 	}
5622*4882a593Smuzhiyun 
5623*4882a593Smuzhiyun 	ww_mutex_init(&rdev->mutex, &regulator_ww_class);
5624*4882a593Smuzhiyun 	rdev->reg_data = config->driver_data;
5625*4882a593Smuzhiyun 	rdev->owner = regulator_desc->owner;
5626*4882a593Smuzhiyun 	rdev->desc = regulator_desc;
5627*4882a593Smuzhiyun 	if (config->regmap)
5628*4882a593Smuzhiyun 		rdev->regmap = config->regmap;
5629*4882a593Smuzhiyun 	else if (dev_get_regmap(dev, NULL))
5630*4882a593Smuzhiyun 		rdev->regmap = dev_get_regmap(dev, NULL);
5631*4882a593Smuzhiyun 	else if (dev->parent)
5632*4882a593Smuzhiyun 		rdev->regmap = dev_get_regmap(dev->parent, NULL);
5633*4882a593Smuzhiyun 	INIT_LIST_HEAD(&rdev->consumer_list);
5634*4882a593Smuzhiyun 	INIT_LIST_HEAD(&rdev->list);
5635*4882a593Smuzhiyun 	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5636*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5637*4882a593Smuzhiyun 
5638*4882a593Smuzhiyun 	/* preform any regulator specific init */
5639*4882a593Smuzhiyun 	if (init_data && init_data->regulator_init) {
5640*4882a593Smuzhiyun 		ret = init_data->regulator_init(rdev->reg_data);
5641*4882a593Smuzhiyun 		if (ret < 0)
5642*4882a593Smuzhiyun 			goto clean;
5643*4882a593Smuzhiyun 	}
5644*4882a593Smuzhiyun 
5645*4882a593Smuzhiyun 	if (config->ena_gpiod) {
5646*4882a593Smuzhiyun 		ret = regulator_ena_gpio_request(rdev, config);
5647*4882a593Smuzhiyun 		if (ret != 0) {
5648*4882a593Smuzhiyun 			rdev_err(rdev, "Failed to request enable GPIO: %pe\n",
5649*4882a593Smuzhiyun 				 ERR_PTR(ret));
5650*4882a593Smuzhiyun 			goto clean;
5651*4882a593Smuzhiyun 		}
5652*4882a593Smuzhiyun 		/* The regulator core took over the GPIO descriptor */
5653*4882a593Smuzhiyun 		dangling_cfg_gpiod = false;
5654*4882a593Smuzhiyun 		dangling_of_gpiod = false;
5655*4882a593Smuzhiyun 	}
5656*4882a593Smuzhiyun 
5657*4882a593Smuzhiyun 	/* register with sysfs */
5658*4882a593Smuzhiyun 	rdev->dev.class = &regulator_class;
5659*4882a593Smuzhiyun 	rdev->dev.parent = dev;
5660*4882a593Smuzhiyun 	dev_set_name(&rdev->dev, "regulator.%lu",
5661*4882a593Smuzhiyun 		    (unsigned long) atomic_inc_return(&regulator_no));
5662*4882a593Smuzhiyun 	dev_set_drvdata(&rdev->dev, rdev);
5663*4882a593Smuzhiyun 
5664*4882a593Smuzhiyun 	/* set regulator constraints */
5665*4882a593Smuzhiyun 	if (init_data)
5666*4882a593Smuzhiyun 		rdev->constraints = kmemdup(&init_data->constraints,
5667*4882a593Smuzhiyun 					    sizeof(*rdev->constraints),
5668*4882a593Smuzhiyun 					    GFP_KERNEL);
5669*4882a593Smuzhiyun 	else
5670*4882a593Smuzhiyun 		rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5671*4882a593Smuzhiyun 					    GFP_KERNEL);
5672*4882a593Smuzhiyun 	if (!rdev->constraints) {
5673*4882a593Smuzhiyun 		ret = -ENOMEM;
5674*4882a593Smuzhiyun 		goto wash;
5675*4882a593Smuzhiyun 	}
5676*4882a593Smuzhiyun 
5677*4882a593Smuzhiyun 	if (init_data && init_data->supply_regulator)
5678*4882a593Smuzhiyun 		rdev->supply_name = init_data->supply_regulator;
5679*4882a593Smuzhiyun 	else if (regulator_desc->supply_name)
5680*4882a593Smuzhiyun 		rdev->supply_name = regulator_desc->supply_name;
5681*4882a593Smuzhiyun 
5682*4882a593Smuzhiyun 	ret = set_machine_constraints(rdev);
5683*4882a593Smuzhiyun 	if (ret == -EPROBE_DEFER) {
5684*4882a593Smuzhiyun 		/* Regulator might be in bypass mode and so needs its supply
5685*4882a593Smuzhiyun 		 * to set the constraints */
5686*4882a593Smuzhiyun 		/* FIXME: this currently triggers a chicken-and-egg problem
5687*4882a593Smuzhiyun 		 * when creating -SUPPLY symlink in sysfs to a regulator
5688*4882a593Smuzhiyun 		 * that is just being created */
5689*4882a593Smuzhiyun 		ret = regulator_resolve_supply(rdev);
5690*4882a593Smuzhiyun 		if (!ret)
5691*4882a593Smuzhiyun 			ret = set_machine_constraints(rdev);
5692*4882a593Smuzhiyun 		else
5693*4882a593Smuzhiyun 			rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5694*4882a593Smuzhiyun 				 ERR_PTR(ret));
5695*4882a593Smuzhiyun 	}
5696*4882a593Smuzhiyun 	if (ret < 0)
5697*4882a593Smuzhiyun 		goto wash;
5698*4882a593Smuzhiyun 
5699*4882a593Smuzhiyun 	ret = regulator_init_coupling(rdev);
5700*4882a593Smuzhiyun 	if (ret < 0)
5701*4882a593Smuzhiyun 		goto wash;
5702*4882a593Smuzhiyun 
5703*4882a593Smuzhiyun 	/* add consumers devices */
5704*4882a593Smuzhiyun 	if (init_data) {
5705*4882a593Smuzhiyun 		for (i = 0; i < init_data->num_consumer_supplies; i++) {
5706*4882a593Smuzhiyun 			ret = set_consumer_device_supply(rdev,
5707*4882a593Smuzhiyun 				init_data->consumer_supplies[i].dev_name,
5708*4882a593Smuzhiyun 				init_data->consumer_supplies[i].supply);
5709*4882a593Smuzhiyun 			if (ret < 0) {
5710*4882a593Smuzhiyun 				dev_err(dev, "Failed to set supply %s\n",
5711*4882a593Smuzhiyun 					init_data->consumer_supplies[i].supply);
5712*4882a593Smuzhiyun 				goto unset_supplies;
5713*4882a593Smuzhiyun 			}
5714*4882a593Smuzhiyun 		}
5715*4882a593Smuzhiyun 	}
5716*4882a593Smuzhiyun 
5717*4882a593Smuzhiyun 	if (!rdev->desc->ops->get_voltage &&
5718*4882a593Smuzhiyun 	    !rdev->desc->ops->list_voltage &&
5719*4882a593Smuzhiyun 	    !rdev->desc->fixed_uV)
5720*4882a593Smuzhiyun 		rdev->is_switch = true;
5721*4882a593Smuzhiyun 
5722*4882a593Smuzhiyun 	ret = device_add(&rdev->dev);
5723*4882a593Smuzhiyun 	if (ret != 0)
5724*4882a593Smuzhiyun 		goto unset_supplies;
5725*4882a593Smuzhiyun 
5726*4882a593Smuzhiyun 	rdev_init_debugfs(rdev);
5727*4882a593Smuzhiyun 
5728*4882a593Smuzhiyun 	/* try to resolve regulators coupling since a new one was registered */
5729*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
5730*4882a593Smuzhiyun 	regulator_resolve_coupling(rdev);
5731*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
5732*4882a593Smuzhiyun 
5733*4882a593Smuzhiyun 	/* try to resolve regulators supply since a new one was registered */
5734*4882a593Smuzhiyun 	class_for_each_device(&regulator_class, NULL, NULL,
5735*4882a593Smuzhiyun 			      regulator_register_resolve_supply);
5736*4882a593Smuzhiyun 	kfree(config);
5737*4882a593Smuzhiyun 	return rdev;
5738*4882a593Smuzhiyun 
5739*4882a593Smuzhiyun unset_supplies:
5740*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
5741*4882a593Smuzhiyun 	unset_regulator_supplies(rdev);
5742*4882a593Smuzhiyun 	regulator_remove_coupling(rdev);
5743*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
5744*4882a593Smuzhiyun wash:
5745*4882a593Smuzhiyun 	kfree(rdev->coupling_desc.coupled_rdevs);
5746*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
5747*4882a593Smuzhiyun 	regulator_ena_gpio_free(rdev);
5748*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
5749*4882a593Smuzhiyun 	put_device(&rdev->dev);
5750*4882a593Smuzhiyun 	rdev = NULL;
5751*4882a593Smuzhiyun clean:
5752*4882a593Smuzhiyun 	if (dangling_of_gpiod)
5753*4882a593Smuzhiyun 		gpiod_put(config->ena_gpiod);
5754*4882a593Smuzhiyun 	if (rdev && rdev->dev.of_node)
5755*4882a593Smuzhiyun 		of_node_put(rdev->dev.of_node);
5756*4882a593Smuzhiyun 	kfree(rdev);
5757*4882a593Smuzhiyun 	kfree(config);
5758*4882a593Smuzhiyun rinse:
5759*4882a593Smuzhiyun 	if (dangling_cfg_gpiod)
5760*4882a593Smuzhiyun 		gpiod_put(cfg->ena_gpiod);
5761*4882a593Smuzhiyun 	return ERR_PTR(ret);
5762*4882a593Smuzhiyun }
5763*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_register);
5764*4882a593Smuzhiyun 
5765*4882a593Smuzhiyun /**
5766*4882a593Smuzhiyun  * regulator_unregister - unregister regulator
5767*4882a593Smuzhiyun  * @rdev: regulator to unregister
5768*4882a593Smuzhiyun  *
5769*4882a593Smuzhiyun  * Called by regulator drivers to unregister a regulator.
5770*4882a593Smuzhiyun  */
regulator_unregister(struct regulator_dev * rdev)5771*4882a593Smuzhiyun void regulator_unregister(struct regulator_dev *rdev)
5772*4882a593Smuzhiyun {
5773*4882a593Smuzhiyun 	if (rdev == NULL)
5774*4882a593Smuzhiyun 		return;
5775*4882a593Smuzhiyun 
5776*4882a593Smuzhiyun 	if (rdev->supply) {
5777*4882a593Smuzhiyun 		while (rdev->use_count--)
5778*4882a593Smuzhiyun 			regulator_disable(rdev->supply);
5779*4882a593Smuzhiyun 		regulator_put(rdev->supply);
5780*4882a593Smuzhiyun 	}
5781*4882a593Smuzhiyun 
5782*4882a593Smuzhiyun 	flush_work(&rdev->disable_work.work);
5783*4882a593Smuzhiyun 
5784*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
5785*4882a593Smuzhiyun 
5786*4882a593Smuzhiyun 	WARN_ON(rdev->open_count);
5787*4882a593Smuzhiyun 	regulator_remove_coupling(rdev);
5788*4882a593Smuzhiyun 	unset_regulator_supplies(rdev);
5789*4882a593Smuzhiyun 	list_del(&rdev->list);
5790*4882a593Smuzhiyun 	regulator_ena_gpio_free(rdev);
5791*4882a593Smuzhiyun 	device_unregister(&rdev->dev);
5792*4882a593Smuzhiyun 
5793*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
5794*4882a593Smuzhiyun }
5795*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_unregister);
5796*4882a593Smuzhiyun 
5797*4882a593Smuzhiyun #ifdef CONFIG_SUSPEND
5798*4882a593Smuzhiyun /**
5799*4882a593Smuzhiyun  * regulator_suspend - prepare regulators for system wide suspend
5800*4882a593Smuzhiyun  * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5801*4882a593Smuzhiyun  *
5802*4882a593Smuzhiyun  * Configure each regulator with it's suspend operating parameters for state.
5803*4882a593Smuzhiyun  */
regulator_suspend(struct device * dev)5804*4882a593Smuzhiyun static int regulator_suspend(struct device *dev)
5805*4882a593Smuzhiyun {
5806*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_to_rdev(dev);
5807*4882a593Smuzhiyun 	suspend_state_t state = pm_suspend_target_state;
5808*4882a593Smuzhiyun 	int ret;
5809*4882a593Smuzhiyun 	const struct regulator_state *rstate;
5810*4882a593Smuzhiyun 
5811*4882a593Smuzhiyun 	rstate = regulator_get_suspend_state_check(rdev, state);
5812*4882a593Smuzhiyun 	if (!rstate)
5813*4882a593Smuzhiyun 		return 0;
5814*4882a593Smuzhiyun 
5815*4882a593Smuzhiyun 	regulator_lock(rdev);
5816*4882a593Smuzhiyun 	ret = __suspend_set_state(rdev, rstate);
5817*4882a593Smuzhiyun 	regulator_unlock(rdev);
5818*4882a593Smuzhiyun 
5819*4882a593Smuzhiyun 	return ret;
5820*4882a593Smuzhiyun }
5821*4882a593Smuzhiyun 
regulator_resume(struct device * dev)5822*4882a593Smuzhiyun static int regulator_resume(struct device *dev)
5823*4882a593Smuzhiyun {
5824*4882a593Smuzhiyun 	suspend_state_t state = pm_suspend_target_state;
5825*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_to_rdev(dev);
5826*4882a593Smuzhiyun 	struct regulator_state *rstate;
5827*4882a593Smuzhiyun 	int ret = 0;
5828*4882a593Smuzhiyun 
5829*4882a593Smuzhiyun 	rstate = regulator_get_suspend_state(rdev, state);
5830*4882a593Smuzhiyun 	if (rstate == NULL)
5831*4882a593Smuzhiyun 		return 0;
5832*4882a593Smuzhiyun 
5833*4882a593Smuzhiyun 	/* Avoid grabbing the lock if we don't need to */
5834*4882a593Smuzhiyun 	if (!rdev->desc->ops->resume)
5835*4882a593Smuzhiyun 		return 0;
5836*4882a593Smuzhiyun 
5837*4882a593Smuzhiyun 	regulator_lock(rdev);
5838*4882a593Smuzhiyun 
5839*4882a593Smuzhiyun 	if (rstate->enabled == ENABLE_IN_SUSPEND ||
5840*4882a593Smuzhiyun 	    rstate->enabled == DISABLE_IN_SUSPEND)
5841*4882a593Smuzhiyun 		ret = rdev->desc->ops->resume(rdev);
5842*4882a593Smuzhiyun 
5843*4882a593Smuzhiyun 	regulator_unlock(rdev);
5844*4882a593Smuzhiyun 
5845*4882a593Smuzhiyun 	return ret;
5846*4882a593Smuzhiyun }
5847*4882a593Smuzhiyun #else /* !CONFIG_SUSPEND */
5848*4882a593Smuzhiyun 
5849*4882a593Smuzhiyun #define regulator_suspend	NULL
5850*4882a593Smuzhiyun #define regulator_resume	NULL
5851*4882a593Smuzhiyun 
5852*4882a593Smuzhiyun #endif /* !CONFIG_SUSPEND */
5853*4882a593Smuzhiyun 
5854*4882a593Smuzhiyun #ifdef CONFIG_PM
5855*4882a593Smuzhiyun static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5856*4882a593Smuzhiyun 	.suspend	= regulator_suspend,
5857*4882a593Smuzhiyun 	.resume		= regulator_resume,
5858*4882a593Smuzhiyun };
5859*4882a593Smuzhiyun #endif
5860*4882a593Smuzhiyun 
5861*4882a593Smuzhiyun struct class regulator_class = {
5862*4882a593Smuzhiyun 	.name = "regulator",
5863*4882a593Smuzhiyun 	.dev_release = regulator_dev_release,
5864*4882a593Smuzhiyun 	.dev_groups = regulator_dev_groups,
5865*4882a593Smuzhiyun #ifdef CONFIG_PM
5866*4882a593Smuzhiyun 	.pm = &regulator_pm_ops,
5867*4882a593Smuzhiyun #endif
5868*4882a593Smuzhiyun };
5869*4882a593Smuzhiyun /**
5870*4882a593Smuzhiyun  * regulator_has_full_constraints - the system has fully specified constraints
5871*4882a593Smuzhiyun  *
5872*4882a593Smuzhiyun  * Calling this function will cause the regulator API to disable all
5873*4882a593Smuzhiyun  * regulators which have a zero use count and don't have an always_on
5874*4882a593Smuzhiyun  * constraint in a late_initcall.
5875*4882a593Smuzhiyun  *
5876*4882a593Smuzhiyun  * The intention is that this will become the default behaviour in a
5877*4882a593Smuzhiyun  * future kernel release so users are encouraged to use this facility
5878*4882a593Smuzhiyun  * now.
5879*4882a593Smuzhiyun  */
regulator_has_full_constraints(void)5880*4882a593Smuzhiyun void regulator_has_full_constraints(void)
5881*4882a593Smuzhiyun {
5882*4882a593Smuzhiyun 	has_full_constraints = 1;
5883*4882a593Smuzhiyun }
5884*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5885*4882a593Smuzhiyun 
5886*4882a593Smuzhiyun /**
5887*4882a593Smuzhiyun  * rdev_get_drvdata - get rdev regulator driver data
5888*4882a593Smuzhiyun  * @rdev: regulator
5889*4882a593Smuzhiyun  *
5890*4882a593Smuzhiyun  * Get rdev regulator driver private data. This call can be used in the
5891*4882a593Smuzhiyun  * regulator driver context.
5892*4882a593Smuzhiyun  */
rdev_get_drvdata(struct regulator_dev * rdev)5893*4882a593Smuzhiyun void *rdev_get_drvdata(struct regulator_dev *rdev)
5894*4882a593Smuzhiyun {
5895*4882a593Smuzhiyun 	return rdev->reg_data;
5896*4882a593Smuzhiyun }
5897*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5898*4882a593Smuzhiyun 
5899*4882a593Smuzhiyun /**
5900*4882a593Smuzhiyun  * regulator_get_drvdata - get regulator driver data
5901*4882a593Smuzhiyun  * @regulator: regulator
5902*4882a593Smuzhiyun  *
5903*4882a593Smuzhiyun  * Get regulator driver private data. This call can be used in the consumer
5904*4882a593Smuzhiyun  * driver context when non API regulator specific functions need to be called.
5905*4882a593Smuzhiyun  */
regulator_get_drvdata(struct regulator * regulator)5906*4882a593Smuzhiyun void *regulator_get_drvdata(struct regulator *regulator)
5907*4882a593Smuzhiyun {
5908*4882a593Smuzhiyun 	return regulator->rdev->reg_data;
5909*4882a593Smuzhiyun }
5910*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5911*4882a593Smuzhiyun 
5912*4882a593Smuzhiyun /**
5913*4882a593Smuzhiyun  * regulator_set_drvdata - set regulator driver data
5914*4882a593Smuzhiyun  * @regulator: regulator
5915*4882a593Smuzhiyun  * @data: data
5916*4882a593Smuzhiyun  */
regulator_set_drvdata(struct regulator * regulator,void * data)5917*4882a593Smuzhiyun void regulator_set_drvdata(struct regulator *regulator, void *data)
5918*4882a593Smuzhiyun {
5919*4882a593Smuzhiyun 	regulator->rdev->reg_data = data;
5920*4882a593Smuzhiyun }
5921*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5922*4882a593Smuzhiyun 
5923*4882a593Smuzhiyun /**
5924*4882a593Smuzhiyun  * regulator_get_id - get regulator ID
5925*4882a593Smuzhiyun  * @rdev: regulator
5926*4882a593Smuzhiyun  */
rdev_get_id(struct regulator_dev * rdev)5927*4882a593Smuzhiyun int rdev_get_id(struct regulator_dev *rdev)
5928*4882a593Smuzhiyun {
5929*4882a593Smuzhiyun 	return rdev->desc->id;
5930*4882a593Smuzhiyun }
5931*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rdev_get_id);
5932*4882a593Smuzhiyun 
rdev_get_dev(struct regulator_dev * rdev)5933*4882a593Smuzhiyun struct device *rdev_get_dev(struct regulator_dev *rdev)
5934*4882a593Smuzhiyun {
5935*4882a593Smuzhiyun 	return &rdev->dev;
5936*4882a593Smuzhiyun }
5937*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rdev_get_dev);
5938*4882a593Smuzhiyun 
rdev_get_regmap(struct regulator_dev * rdev)5939*4882a593Smuzhiyun struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5940*4882a593Smuzhiyun {
5941*4882a593Smuzhiyun 	return rdev->regmap;
5942*4882a593Smuzhiyun }
5943*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rdev_get_regmap);
5944*4882a593Smuzhiyun 
regulator_get_init_drvdata(struct regulator_init_data * reg_init_data)5945*4882a593Smuzhiyun void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5946*4882a593Smuzhiyun {
5947*4882a593Smuzhiyun 	return reg_init_data->driver_data;
5948*4882a593Smuzhiyun }
5949*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5950*4882a593Smuzhiyun 
5951*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
supply_map_show(struct seq_file * sf,void * data)5952*4882a593Smuzhiyun static int supply_map_show(struct seq_file *sf, void *data)
5953*4882a593Smuzhiyun {
5954*4882a593Smuzhiyun 	struct regulator_map *map;
5955*4882a593Smuzhiyun 
5956*4882a593Smuzhiyun 	list_for_each_entry(map, &regulator_map_list, list) {
5957*4882a593Smuzhiyun 		seq_printf(sf, "%s -> %s.%s\n",
5958*4882a593Smuzhiyun 				rdev_get_name(map->regulator), map->dev_name,
5959*4882a593Smuzhiyun 				map->supply);
5960*4882a593Smuzhiyun 	}
5961*4882a593Smuzhiyun 
5962*4882a593Smuzhiyun 	return 0;
5963*4882a593Smuzhiyun }
5964*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(supply_map);
5965*4882a593Smuzhiyun 
5966*4882a593Smuzhiyun struct summary_data {
5967*4882a593Smuzhiyun 	struct seq_file *s;
5968*4882a593Smuzhiyun 	struct regulator_dev *parent;
5969*4882a593Smuzhiyun 	int level;
5970*4882a593Smuzhiyun };
5971*4882a593Smuzhiyun 
5972*4882a593Smuzhiyun static void regulator_summary_show_subtree(struct seq_file *s,
5973*4882a593Smuzhiyun 					   struct regulator_dev *rdev,
5974*4882a593Smuzhiyun 					   int level);
5975*4882a593Smuzhiyun 
regulator_summary_show_children(struct device * dev,void * data)5976*4882a593Smuzhiyun static int regulator_summary_show_children(struct device *dev, void *data)
5977*4882a593Smuzhiyun {
5978*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_to_rdev(dev);
5979*4882a593Smuzhiyun 	struct summary_data *summary_data = data;
5980*4882a593Smuzhiyun 
5981*4882a593Smuzhiyun 	if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5982*4882a593Smuzhiyun 		regulator_summary_show_subtree(summary_data->s, rdev,
5983*4882a593Smuzhiyun 					       summary_data->level + 1);
5984*4882a593Smuzhiyun 
5985*4882a593Smuzhiyun 	return 0;
5986*4882a593Smuzhiyun }
5987*4882a593Smuzhiyun 
regulator_summary_show_subtree(struct seq_file * s,struct regulator_dev * rdev,int level)5988*4882a593Smuzhiyun static void regulator_summary_show_subtree(struct seq_file *s,
5989*4882a593Smuzhiyun 					   struct regulator_dev *rdev,
5990*4882a593Smuzhiyun 					   int level)
5991*4882a593Smuzhiyun {
5992*4882a593Smuzhiyun 	struct regulation_constraints *c;
5993*4882a593Smuzhiyun 	struct regulator *consumer;
5994*4882a593Smuzhiyun 	struct summary_data summary_data;
5995*4882a593Smuzhiyun 	unsigned int opmode;
5996*4882a593Smuzhiyun 
5997*4882a593Smuzhiyun 	if (!rdev)
5998*4882a593Smuzhiyun 		return;
5999*4882a593Smuzhiyun 
6000*4882a593Smuzhiyun 	opmode = _regulator_get_mode_unlocked(rdev);
6001*4882a593Smuzhiyun 	seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
6002*4882a593Smuzhiyun 		   level * 3 + 1, "",
6003*4882a593Smuzhiyun 		   30 - level * 3, rdev_get_name(rdev),
6004*4882a593Smuzhiyun 		   rdev->use_count, rdev->open_count, rdev->bypass_count,
6005*4882a593Smuzhiyun 		   regulator_opmode_to_str(opmode));
6006*4882a593Smuzhiyun 
6007*4882a593Smuzhiyun 	seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
6008*4882a593Smuzhiyun 	seq_printf(s, "%5dmA ",
6009*4882a593Smuzhiyun 		   _regulator_get_current_limit_unlocked(rdev) / 1000);
6010*4882a593Smuzhiyun 
6011*4882a593Smuzhiyun 	c = rdev->constraints;
6012*4882a593Smuzhiyun 	if (c) {
6013*4882a593Smuzhiyun 		switch (rdev->desc->type) {
6014*4882a593Smuzhiyun 		case REGULATOR_VOLTAGE:
6015*4882a593Smuzhiyun 			seq_printf(s, "%5dmV %5dmV ",
6016*4882a593Smuzhiyun 				   c->min_uV / 1000, c->max_uV / 1000);
6017*4882a593Smuzhiyun 			break;
6018*4882a593Smuzhiyun 		case REGULATOR_CURRENT:
6019*4882a593Smuzhiyun 			seq_printf(s, "%5dmA %5dmA ",
6020*4882a593Smuzhiyun 				   c->min_uA / 1000, c->max_uA / 1000);
6021*4882a593Smuzhiyun 			break;
6022*4882a593Smuzhiyun 		}
6023*4882a593Smuzhiyun 	}
6024*4882a593Smuzhiyun 
6025*4882a593Smuzhiyun 	seq_puts(s, "\n");
6026*4882a593Smuzhiyun 
6027*4882a593Smuzhiyun 	list_for_each_entry(consumer, &rdev->consumer_list, list) {
6028*4882a593Smuzhiyun 		if (consumer->dev && consumer->dev->class == &regulator_class)
6029*4882a593Smuzhiyun 			continue;
6030*4882a593Smuzhiyun 
6031*4882a593Smuzhiyun 		seq_printf(s, "%*s%-*s ",
6032*4882a593Smuzhiyun 			   (level + 1) * 3 + 1, "",
6033*4882a593Smuzhiyun 			   30 - (level + 1) * 3,
6034*4882a593Smuzhiyun 			   consumer->supply_name ? consumer->supply_name :
6035*4882a593Smuzhiyun 			   consumer->dev ? dev_name(consumer->dev) : "deviceless");
6036*4882a593Smuzhiyun 
6037*4882a593Smuzhiyun 		switch (rdev->desc->type) {
6038*4882a593Smuzhiyun 		case REGULATOR_VOLTAGE:
6039*4882a593Smuzhiyun 			seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
6040*4882a593Smuzhiyun 				   consumer->enable_count,
6041*4882a593Smuzhiyun 				   consumer->uA_load / 1000,
6042*4882a593Smuzhiyun 				   consumer->uA_load && !consumer->enable_count ?
6043*4882a593Smuzhiyun 				   '*' : ' ',
6044*4882a593Smuzhiyun 				   consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
6045*4882a593Smuzhiyun 				   consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
6046*4882a593Smuzhiyun 			break;
6047*4882a593Smuzhiyun 		case REGULATOR_CURRENT:
6048*4882a593Smuzhiyun 			break;
6049*4882a593Smuzhiyun 		}
6050*4882a593Smuzhiyun 
6051*4882a593Smuzhiyun 		seq_puts(s, "\n");
6052*4882a593Smuzhiyun 	}
6053*4882a593Smuzhiyun 
6054*4882a593Smuzhiyun 	summary_data.s = s;
6055*4882a593Smuzhiyun 	summary_data.level = level;
6056*4882a593Smuzhiyun 	summary_data.parent = rdev;
6057*4882a593Smuzhiyun 
6058*4882a593Smuzhiyun 	class_for_each_device(&regulator_class, NULL, &summary_data,
6059*4882a593Smuzhiyun 			      regulator_summary_show_children);
6060*4882a593Smuzhiyun }
6061*4882a593Smuzhiyun 
6062*4882a593Smuzhiyun struct summary_lock_data {
6063*4882a593Smuzhiyun 	struct ww_acquire_ctx *ww_ctx;
6064*4882a593Smuzhiyun 	struct regulator_dev **new_contended_rdev;
6065*4882a593Smuzhiyun 	struct regulator_dev **old_contended_rdev;
6066*4882a593Smuzhiyun };
6067*4882a593Smuzhiyun 
regulator_summary_lock_one(struct device * dev,void * data)6068*4882a593Smuzhiyun static int regulator_summary_lock_one(struct device *dev, void *data)
6069*4882a593Smuzhiyun {
6070*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_to_rdev(dev);
6071*4882a593Smuzhiyun 	struct summary_lock_data *lock_data = data;
6072*4882a593Smuzhiyun 	int ret = 0;
6073*4882a593Smuzhiyun 
6074*4882a593Smuzhiyun 	if (rdev != *lock_data->old_contended_rdev) {
6075*4882a593Smuzhiyun 		ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
6076*4882a593Smuzhiyun 
6077*4882a593Smuzhiyun 		if (ret == -EDEADLK)
6078*4882a593Smuzhiyun 			*lock_data->new_contended_rdev = rdev;
6079*4882a593Smuzhiyun 		else
6080*4882a593Smuzhiyun 			WARN_ON_ONCE(ret);
6081*4882a593Smuzhiyun 	} else {
6082*4882a593Smuzhiyun 		*lock_data->old_contended_rdev = NULL;
6083*4882a593Smuzhiyun 	}
6084*4882a593Smuzhiyun 
6085*4882a593Smuzhiyun 	return ret;
6086*4882a593Smuzhiyun }
6087*4882a593Smuzhiyun 
regulator_summary_unlock_one(struct device * dev,void * data)6088*4882a593Smuzhiyun static int regulator_summary_unlock_one(struct device *dev, void *data)
6089*4882a593Smuzhiyun {
6090*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_to_rdev(dev);
6091*4882a593Smuzhiyun 	struct summary_lock_data *lock_data = data;
6092*4882a593Smuzhiyun 
6093*4882a593Smuzhiyun 	if (lock_data) {
6094*4882a593Smuzhiyun 		if (rdev == *lock_data->new_contended_rdev)
6095*4882a593Smuzhiyun 			return -EDEADLK;
6096*4882a593Smuzhiyun 	}
6097*4882a593Smuzhiyun 
6098*4882a593Smuzhiyun 	regulator_unlock(rdev);
6099*4882a593Smuzhiyun 
6100*4882a593Smuzhiyun 	return 0;
6101*4882a593Smuzhiyun }
6102*4882a593Smuzhiyun 
regulator_summary_lock_all(struct ww_acquire_ctx * ww_ctx,struct regulator_dev ** new_contended_rdev,struct regulator_dev ** old_contended_rdev)6103*4882a593Smuzhiyun static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
6104*4882a593Smuzhiyun 				      struct regulator_dev **new_contended_rdev,
6105*4882a593Smuzhiyun 				      struct regulator_dev **old_contended_rdev)
6106*4882a593Smuzhiyun {
6107*4882a593Smuzhiyun 	struct summary_lock_data lock_data;
6108*4882a593Smuzhiyun 	int ret;
6109*4882a593Smuzhiyun 
6110*4882a593Smuzhiyun 	lock_data.ww_ctx = ww_ctx;
6111*4882a593Smuzhiyun 	lock_data.new_contended_rdev = new_contended_rdev;
6112*4882a593Smuzhiyun 	lock_data.old_contended_rdev = old_contended_rdev;
6113*4882a593Smuzhiyun 
6114*4882a593Smuzhiyun 	ret = class_for_each_device(&regulator_class, NULL, &lock_data,
6115*4882a593Smuzhiyun 				    regulator_summary_lock_one);
6116*4882a593Smuzhiyun 	if (ret)
6117*4882a593Smuzhiyun 		class_for_each_device(&regulator_class, NULL, &lock_data,
6118*4882a593Smuzhiyun 				      regulator_summary_unlock_one);
6119*4882a593Smuzhiyun 
6120*4882a593Smuzhiyun 	return ret;
6121*4882a593Smuzhiyun }
6122*4882a593Smuzhiyun 
regulator_summary_lock(struct ww_acquire_ctx * ww_ctx)6123*4882a593Smuzhiyun static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
6124*4882a593Smuzhiyun {
6125*4882a593Smuzhiyun 	struct regulator_dev *new_contended_rdev = NULL;
6126*4882a593Smuzhiyun 	struct regulator_dev *old_contended_rdev = NULL;
6127*4882a593Smuzhiyun 	int err;
6128*4882a593Smuzhiyun 
6129*4882a593Smuzhiyun 	mutex_lock(&regulator_list_mutex);
6130*4882a593Smuzhiyun 
6131*4882a593Smuzhiyun 	ww_acquire_init(ww_ctx, &regulator_ww_class);
6132*4882a593Smuzhiyun 
6133*4882a593Smuzhiyun 	do {
6134*4882a593Smuzhiyun 		if (new_contended_rdev) {
6135*4882a593Smuzhiyun 			ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
6136*4882a593Smuzhiyun 			old_contended_rdev = new_contended_rdev;
6137*4882a593Smuzhiyun 			old_contended_rdev->ref_cnt++;
6138*4882a593Smuzhiyun 		}
6139*4882a593Smuzhiyun 
6140*4882a593Smuzhiyun 		err = regulator_summary_lock_all(ww_ctx,
6141*4882a593Smuzhiyun 						 &new_contended_rdev,
6142*4882a593Smuzhiyun 						 &old_contended_rdev);
6143*4882a593Smuzhiyun 
6144*4882a593Smuzhiyun 		if (old_contended_rdev)
6145*4882a593Smuzhiyun 			regulator_unlock(old_contended_rdev);
6146*4882a593Smuzhiyun 
6147*4882a593Smuzhiyun 	} while (err == -EDEADLK);
6148*4882a593Smuzhiyun 
6149*4882a593Smuzhiyun 	ww_acquire_done(ww_ctx);
6150*4882a593Smuzhiyun }
6151*4882a593Smuzhiyun 
regulator_summary_unlock(struct ww_acquire_ctx * ww_ctx)6152*4882a593Smuzhiyun static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
6153*4882a593Smuzhiyun {
6154*4882a593Smuzhiyun 	class_for_each_device(&regulator_class, NULL, NULL,
6155*4882a593Smuzhiyun 			      regulator_summary_unlock_one);
6156*4882a593Smuzhiyun 	ww_acquire_fini(ww_ctx);
6157*4882a593Smuzhiyun 
6158*4882a593Smuzhiyun 	mutex_unlock(&regulator_list_mutex);
6159*4882a593Smuzhiyun }
6160*4882a593Smuzhiyun 
regulator_summary_show_roots(struct device * dev,void * data)6161*4882a593Smuzhiyun static int regulator_summary_show_roots(struct device *dev, void *data)
6162*4882a593Smuzhiyun {
6163*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_to_rdev(dev);
6164*4882a593Smuzhiyun 	struct seq_file *s = data;
6165*4882a593Smuzhiyun 
6166*4882a593Smuzhiyun 	if (!rdev->supply)
6167*4882a593Smuzhiyun 		regulator_summary_show_subtree(s, rdev, 0);
6168*4882a593Smuzhiyun 
6169*4882a593Smuzhiyun 	return 0;
6170*4882a593Smuzhiyun }
6171*4882a593Smuzhiyun 
regulator_summary_show(struct seq_file * s,void * data)6172*4882a593Smuzhiyun static int regulator_summary_show(struct seq_file *s, void *data)
6173*4882a593Smuzhiyun {
6174*4882a593Smuzhiyun 	struct ww_acquire_ctx ww_ctx;
6175*4882a593Smuzhiyun 
6176*4882a593Smuzhiyun 	seq_puts(s, " regulator                      use open bypass  opmode voltage current     min     max\n");
6177*4882a593Smuzhiyun 	seq_puts(s, "---------------------------------------------------------------------------------------\n");
6178*4882a593Smuzhiyun 
6179*4882a593Smuzhiyun 	regulator_summary_lock(&ww_ctx);
6180*4882a593Smuzhiyun 
6181*4882a593Smuzhiyun 	class_for_each_device(&regulator_class, NULL, s,
6182*4882a593Smuzhiyun 			      regulator_summary_show_roots);
6183*4882a593Smuzhiyun 
6184*4882a593Smuzhiyun 	regulator_summary_unlock(&ww_ctx);
6185*4882a593Smuzhiyun 
6186*4882a593Smuzhiyun 	return 0;
6187*4882a593Smuzhiyun }
6188*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(regulator_summary);
6189*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
6190*4882a593Smuzhiyun 
regulator_init(void)6191*4882a593Smuzhiyun static int __init regulator_init(void)
6192*4882a593Smuzhiyun {
6193*4882a593Smuzhiyun 	int ret;
6194*4882a593Smuzhiyun 
6195*4882a593Smuzhiyun 	ret = class_register(&regulator_class);
6196*4882a593Smuzhiyun 
6197*4882a593Smuzhiyun 	debugfs_root = debugfs_create_dir("regulator", NULL);
6198*4882a593Smuzhiyun 	if (!debugfs_root)
6199*4882a593Smuzhiyun 		pr_warn("regulator: Failed to create debugfs directory\n");
6200*4882a593Smuzhiyun 
6201*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
6202*4882a593Smuzhiyun 	debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
6203*4882a593Smuzhiyun 			    &supply_map_fops);
6204*4882a593Smuzhiyun 
6205*4882a593Smuzhiyun 	debugfs_create_file("regulator_summary", 0444, debugfs_root,
6206*4882a593Smuzhiyun 			    NULL, &regulator_summary_fops);
6207*4882a593Smuzhiyun #endif
6208*4882a593Smuzhiyun 	regulator_dummy_init();
6209*4882a593Smuzhiyun 
6210*4882a593Smuzhiyun 	regulator_coupler_register(&generic_regulator_coupler);
6211*4882a593Smuzhiyun 
6212*4882a593Smuzhiyun 	return ret;
6213*4882a593Smuzhiyun }
6214*4882a593Smuzhiyun 
6215*4882a593Smuzhiyun /* init early to allow our consumers to complete system booting */
6216*4882a593Smuzhiyun #ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
6217*4882a593Smuzhiyun core_initcall_sync(regulator_init);
6218*4882a593Smuzhiyun #else
6219*4882a593Smuzhiyun core_initcall(regulator_init);
6220*4882a593Smuzhiyun #endif
6221*4882a593Smuzhiyun 
regulator_late_cleanup(struct device * dev,void * data)6222*4882a593Smuzhiyun static int regulator_late_cleanup(struct device *dev, void *data)
6223*4882a593Smuzhiyun {
6224*4882a593Smuzhiyun 	struct regulator_dev *rdev = dev_to_rdev(dev);
6225*4882a593Smuzhiyun 	struct regulation_constraints *c = rdev->constraints;
6226*4882a593Smuzhiyun 	int ret;
6227*4882a593Smuzhiyun 
6228*4882a593Smuzhiyun 	if (c && c->always_on)
6229*4882a593Smuzhiyun 		return 0;
6230*4882a593Smuzhiyun 
6231*4882a593Smuzhiyun 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
6232*4882a593Smuzhiyun 		return 0;
6233*4882a593Smuzhiyun 
6234*4882a593Smuzhiyun 	regulator_lock(rdev);
6235*4882a593Smuzhiyun 
6236*4882a593Smuzhiyun 	if (rdev->use_count)
6237*4882a593Smuzhiyun 		goto unlock;
6238*4882a593Smuzhiyun 
6239*4882a593Smuzhiyun 	/* If reading the status failed, assume that it's off. */
6240*4882a593Smuzhiyun 	if (_regulator_is_enabled(rdev) <= 0)
6241*4882a593Smuzhiyun 		goto unlock;
6242*4882a593Smuzhiyun 
6243*4882a593Smuzhiyun 	if (have_full_constraints()) {
6244*4882a593Smuzhiyun 		/* We log since this may kill the system if it goes
6245*4882a593Smuzhiyun 		 * wrong. */
6246*4882a593Smuzhiyun 		rdev_info(rdev, "disabling\n");
6247*4882a593Smuzhiyun 		ret = _regulator_do_disable(rdev);
6248*4882a593Smuzhiyun 		if (ret != 0)
6249*4882a593Smuzhiyun 			rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret));
6250*4882a593Smuzhiyun 	} else {
6251*4882a593Smuzhiyun 		/* The intention is that in future we will
6252*4882a593Smuzhiyun 		 * assume that full constraints are provided
6253*4882a593Smuzhiyun 		 * so warn even if we aren't going to do
6254*4882a593Smuzhiyun 		 * anything here.
6255*4882a593Smuzhiyun 		 */
6256*4882a593Smuzhiyun 		rdev_warn(rdev, "incomplete constraints, leaving on\n");
6257*4882a593Smuzhiyun 	}
6258*4882a593Smuzhiyun 
6259*4882a593Smuzhiyun unlock:
6260*4882a593Smuzhiyun 	regulator_unlock(rdev);
6261*4882a593Smuzhiyun 
6262*4882a593Smuzhiyun 	return 0;
6263*4882a593Smuzhiyun }
6264*4882a593Smuzhiyun 
regulator_init_complete_work_function(struct work_struct * work)6265*4882a593Smuzhiyun static void regulator_init_complete_work_function(struct work_struct *work)
6266*4882a593Smuzhiyun {
6267*4882a593Smuzhiyun 	/*
6268*4882a593Smuzhiyun 	 * Regulators may had failed to resolve their input supplies
6269*4882a593Smuzhiyun 	 * when were registered, either because the input supply was
6270*4882a593Smuzhiyun 	 * not registered yet or because its parent device was not
6271*4882a593Smuzhiyun 	 * bound yet. So attempt to resolve the input supplies for
6272*4882a593Smuzhiyun 	 * pending regulators before trying to disable unused ones.
6273*4882a593Smuzhiyun 	 */
6274*4882a593Smuzhiyun 	class_for_each_device(&regulator_class, NULL, NULL,
6275*4882a593Smuzhiyun 			      regulator_register_resolve_supply);
6276*4882a593Smuzhiyun 
6277*4882a593Smuzhiyun 	/* If we have a full configuration then disable any regulators
6278*4882a593Smuzhiyun 	 * we have permission to change the status for and which are
6279*4882a593Smuzhiyun 	 * not in use or always_on.  This is effectively the default
6280*4882a593Smuzhiyun 	 * for DT and ACPI as they have full constraints.
6281*4882a593Smuzhiyun 	 */
6282*4882a593Smuzhiyun 	class_for_each_device(&regulator_class, NULL, NULL,
6283*4882a593Smuzhiyun 			      regulator_late_cleanup);
6284*4882a593Smuzhiyun }
6285*4882a593Smuzhiyun 
6286*4882a593Smuzhiyun static DECLARE_DELAYED_WORK(regulator_init_complete_work,
6287*4882a593Smuzhiyun 			    regulator_init_complete_work_function);
6288*4882a593Smuzhiyun 
regulator_init_complete(void)6289*4882a593Smuzhiyun static int __init regulator_init_complete(void)
6290*4882a593Smuzhiyun {
6291*4882a593Smuzhiyun 	/*
6292*4882a593Smuzhiyun 	 * Since DT doesn't provide an idiomatic mechanism for
6293*4882a593Smuzhiyun 	 * enabling full constraints and since it's much more natural
6294*4882a593Smuzhiyun 	 * with DT to provide them just assume that a DT enabled
6295*4882a593Smuzhiyun 	 * system has full constraints.
6296*4882a593Smuzhiyun 	 */
6297*4882a593Smuzhiyun 	if (of_have_populated_dt())
6298*4882a593Smuzhiyun 		has_full_constraints = true;
6299*4882a593Smuzhiyun 
6300*4882a593Smuzhiyun 	/*
6301*4882a593Smuzhiyun 	 * We punt completion for an arbitrary amount of time since
6302*4882a593Smuzhiyun 	 * systems like distros will load many drivers from userspace
6303*4882a593Smuzhiyun 	 * so consumers might not always be ready yet, this is
6304*4882a593Smuzhiyun 	 * particularly an issue with laptops where this might bounce
6305*4882a593Smuzhiyun 	 * the display off then on.  Ideally we'd get a notification
6306*4882a593Smuzhiyun 	 * from userspace when this happens but we don't so just wait
6307*4882a593Smuzhiyun 	 * a bit and hope we waited long enough.  It'd be better if
6308*4882a593Smuzhiyun 	 * we'd only do this on systems that need it, and a kernel
6309*4882a593Smuzhiyun 	 * command line option might be useful.
6310*4882a593Smuzhiyun 	 */
6311*4882a593Smuzhiyun 	schedule_delayed_work(&regulator_init_complete_work,
6312*4882a593Smuzhiyun 			      msecs_to_jiffies(30000));
6313*4882a593Smuzhiyun 
6314*4882a593Smuzhiyun 	return 0;
6315*4882a593Smuzhiyun }
6316*4882a593Smuzhiyun late_initcall_sync(regulator_init_complete);
6317