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