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