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