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