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