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