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