xref: /openbmc/linux/drivers/regulator/core.c (revision f35e839a)
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/of.h>
28 #include <linux/regmap.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/module.h>
34 
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/regulator.h>
37 
38 #include "dummy.h"
39 
40 #define rdev_crit(rdev, fmt, ...)					\
41 	pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_err(rdev, fmt, ...)					\
43 	pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_warn(rdev, fmt, ...)					\
45 	pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_info(rdev, fmt, ...)					\
47 	pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_dbg(rdev, fmt, ...)					\
49 	pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50 
51 static DEFINE_MUTEX(regulator_list_mutex);
52 static LIST_HEAD(regulator_list);
53 static LIST_HEAD(regulator_map_list);
54 static LIST_HEAD(regulator_ena_gpio_list);
55 static bool has_full_constraints;
56 static bool board_wants_dummy_regulator;
57 
58 static struct dentry *debugfs_root;
59 
60 /*
61  * struct regulator_map
62  *
63  * Used to provide symbolic supply names to devices.
64  */
65 struct regulator_map {
66 	struct list_head list;
67 	const char *dev_name;   /* The dev_name() for the consumer */
68 	const char *supply;
69 	struct regulator_dev *regulator;
70 };
71 
72 /*
73  * struct regulator_enable_gpio
74  *
75  * Management for shared enable GPIO pin
76  */
77 struct regulator_enable_gpio {
78 	struct list_head list;
79 	int gpio;
80 	u32 enable_count;	/* a number of enabled shared GPIO */
81 	u32 request_count;	/* a number of requested shared GPIO */
82 	unsigned int ena_gpio_invert:1;
83 };
84 
85 /*
86  * struct regulator
87  *
88  * One for each consumer device.
89  */
90 struct regulator {
91 	struct device *dev;
92 	struct list_head list;
93 	unsigned int always_on:1;
94 	unsigned int bypass:1;
95 	int uA_load;
96 	int min_uV;
97 	int max_uV;
98 	char *supply_name;
99 	struct device_attribute dev_attr;
100 	struct regulator_dev *rdev;
101 	struct dentry *debugfs;
102 };
103 
104 static int _regulator_is_enabled(struct regulator_dev *rdev);
105 static int _regulator_disable(struct regulator_dev *rdev);
106 static int _regulator_get_voltage(struct regulator_dev *rdev);
107 static int _regulator_get_current_limit(struct regulator_dev *rdev);
108 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
109 static void _notifier_call_chain(struct regulator_dev *rdev,
110 				  unsigned long event, void *data);
111 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
112 				     int min_uV, int max_uV);
113 static struct regulator *create_regulator(struct regulator_dev *rdev,
114 					  struct device *dev,
115 					  const char *supply_name);
116 
117 static const char *rdev_get_name(struct regulator_dev *rdev)
118 {
119 	if (rdev->constraints && rdev->constraints->name)
120 		return rdev->constraints->name;
121 	else if (rdev->desc->name)
122 		return rdev->desc->name;
123 	else
124 		return "";
125 }
126 
127 /**
128  * of_get_regulator - get a regulator device node based on supply name
129  * @dev: Device pointer for the consumer (of regulator) device
130  * @supply: regulator supply name
131  *
132  * Extract the regulator device node corresponding to the supply name.
133  * returns the device node corresponding to the regulator if found, else
134  * returns NULL.
135  */
136 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
137 {
138 	struct device_node *regnode = NULL;
139 	char prop_name[32]; /* 32 is max size of property name */
140 
141 	dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
142 
143 	snprintf(prop_name, 32, "%s-supply", supply);
144 	regnode = of_parse_phandle(dev->of_node, prop_name, 0);
145 
146 	if (!regnode) {
147 		dev_dbg(dev, "Looking up %s property in node %s failed",
148 				prop_name, dev->of_node->full_name);
149 		return NULL;
150 	}
151 	return regnode;
152 }
153 
154 static int _regulator_can_change_status(struct regulator_dev *rdev)
155 {
156 	if (!rdev->constraints)
157 		return 0;
158 
159 	if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
160 		return 1;
161 	else
162 		return 0;
163 }
164 
165 /* Platform voltage constraint check */
166 static int regulator_check_voltage(struct regulator_dev *rdev,
167 				   int *min_uV, int *max_uV)
168 {
169 	BUG_ON(*min_uV > *max_uV);
170 
171 	if (!rdev->constraints) {
172 		rdev_err(rdev, "no constraints\n");
173 		return -ENODEV;
174 	}
175 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
176 		rdev_err(rdev, "operation not allowed\n");
177 		return -EPERM;
178 	}
179 
180 	if (*max_uV > rdev->constraints->max_uV)
181 		*max_uV = rdev->constraints->max_uV;
182 	if (*min_uV < rdev->constraints->min_uV)
183 		*min_uV = rdev->constraints->min_uV;
184 
185 	if (*min_uV > *max_uV) {
186 		rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
187 			 *min_uV, *max_uV);
188 		return -EINVAL;
189 	}
190 
191 	return 0;
192 }
193 
194 /* Make sure we select a voltage that suits the needs of all
195  * regulator consumers
196  */
197 static int regulator_check_consumers(struct regulator_dev *rdev,
198 				     int *min_uV, int *max_uV)
199 {
200 	struct regulator *regulator;
201 
202 	list_for_each_entry(regulator, &rdev->consumer_list, list) {
203 		/*
204 		 * Assume consumers that didn't say anything are OK
205 		 * with anything in the constraint range.
206 		 */
207 		if (!regulator->min_uV && !regulator->max_uV)
208 			continue;
209 
210 		if (*max_uV > regulator->max_uV)
211 			*max_uV = regulator->max_uV;
212 		if (*min_uV < regulator->min_uV)
213 			*min_uV = regulator->min_uV;
214 	}
215 
216 	if (*min_uV > *max_uV) {
217 		rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
218 			*min_uV, *max_uV);
219 		return -EINVAL;
220 	}
221 
222 	return 0;
223 }
224 
225 /* current constraint check */
226 static int regulator_check_current_limit(struct regulator_dev *rdev,
227 					int *min_uA, int *max_uA)
228 {
229 	BUG_ON(*min_uA > *max_uA);
230 
231 	if (!rdev->constraints) {
232 		rdev_err(rdev, "no constraints\n");
233 		return -ENODEV;
234 	}
235 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
236 		rdev_err(rdev, "operation not allowed\n");
237 		return -EPERM;
238 	}
239 
240 	if (*max_uA > rdev->constraints->max_uA)
241 		*max_uA = rdev->constraints->max_uA;
242 	if (*min_uA < rdev->constraints->min_uA)
243 		*min_uA = rdev->constraints->min_uA;
244 
245 	if (*min_uA > *max_uA) {
246 		rdev_err(rdev, "unsupportable current range: %d-%duA\n",
247 			 *min_uA, *max_uA);
248 		return -EINVAL;
249 	}
250 
251 	return 0;
252 }
253 
254 /* operating mode constraint check */
255 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
256 {
257 	switch (*mode) {
258 	case REGULATOR_MODE_FAST:
259 	case REGULATOR_MODE_NORMAL:
260 	case REGULATOR_MODE_IDLE:
261 	case REGULATOR_MODE_STANDBY:
262 		break;
263 	default:
264 		rdev_err(rdev, "invalid mode %x specified\n", *mode);
265 		return -EINVAL;
266 	}
267 
268 	if (!rdev->constraints) {
269 		rdev_err(rdev, "no constraints\n");
270 		return -ENODEV;
271 	}
272 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
273 		rdev_err(rdev, "operation not allowed\n");
274 		return -EPERM;
275 	}
276 
277 	/* The modes are bitmasks, the most power hungry modes having
278 	 * the lowest values. If the requested mode isn't supported
279 	 * try higher modes. */
280 	while (*mode) {
281 		if (rdev->constraints->valid_modes_mask & *mode)
282 			return 0;
283 		*mode /= 2;
284 	}
285 
286 	return -EINVAL;
287 }
288 
289 /* dynamic regulator mode switching constraint check */
290 static int regulator_check_drms(struct regulator_dev *rdev)
291 {
292 	if (!rdev->constraints) {
293 		rdev_err(rdev, "no constraints\n");
294 		return -ENODEV;
295 	}
296 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
297 		rdev_err(rdev, "operation not allowed\n");
298 		return -EPERM;
299 	}
300 	return 0;
301 }
302 
303 static ssize_t regulator_uV_show(struct device *dev,
304 				struct device_attribute *attr, char *buf)
305 {
306 	struct regulator_dev *rdev = dev_get_drvdata(dev);
307 	ssize_t ret;
308 
309 	mutex_lock(&rdev->mutex);
310 	ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
311 	mutex_unlock(&rdev->mutex);
312 
313 	return ret;
314 }
315 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
316 
317 static ssize_t regulator_uA_show(struct device *dev,
318 				struct device_attribute *attr, char *buf)
319 {
320 	struct regulator_dev *rdev = dev_get_drvdata(dev);
321 
322 	return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
323 }
324 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
325 
326 static ssize_t regulator_name_show(struct device *dev,
327 			     struct device_attribute *attr, char *buf)
328 {
329 	struct regulator_dev *rdev = dev_get_drvdata(dev);
330 
331 	return sprintf(buf, "%s\n", rdev_get_name(rdev));
332 }
333 
334 static ssize_t regulator_print_opmode(char *buf, int mode)
335 {
336 	switch (mode) {
337 	case REGULATOR_MODE_FAST:
338 		return sprintf(buf, "fast\n");
339 	case REGULATOR_MODE_NORMAL:
340 		return sprintf(buf, "normal\n");
341 	case REGULATOR_MODE_IDLE:
342 		return sprintf(buf, "idle\n");
343 	case REGULATOR_MODE_STANDBY:
344 		return sprintf(buf, "standby\n");
345 	}
346 	return sprintf(buf, "unknown\n");
347 }
348 
349 static ssize_t regulator_opmode_show(struct device *dev,
350 				    struct device_attribute *attr, char *buf)
351 {
352 	struct regulator_dev *rdev = dev_get_drvdata(dev);
353 
354 	return regulator_print_opmode(buf, _regulator_get_mode(rdev));
355 }
356 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
357 
358 static ssize_t regulator_print_state(char *buf, int state)
359 {
360 	if (state > 0)
361 		return sprintf(buf, "enabled\n");
362 	else if (state == 0)
363 		return sprintf(buf, "disabled\n");
364 	else
365 		return sprintf(buf, "unknown\n");
366 }
367 
368 static ssize_t regulator_state_show(struct device *dev,
369 				   struct device_attribute *attr, char *buf)
370 {
371 	struct regulator_dev *rdev = dev_get_drvdata(dev);
372 	ssize_t ret;
373 
374 	mutex_lock(&rdev->mutex);
375 	ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
376 	mutex_unlock(&rdev->mutex);
377 
378 	return ret;
379 }
380 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
381 
382 static ssize_t regulator_status_show(struct device *dev,
383 				   struct device_attribute *attr, char *buf)
384 {
385 	struct regulator_dev *rdev = dev_get_drvdata(dev);
386 	int status;
387 	char *label;
388 
389 	status = rdev->desc->ops->get_status(rdev);
390 	if (status < 0)
391 		return status;
392 
393 	switch (status) {
394 	case REGULATOR_STATUS_OFF:
395 		label = "off";
396 		break;
397 	case REGULATOR_STATUS_ON:
398 		label = "on";
399 		break;
400 	case REGULATOR_STATUS_ERROR:
401 		label = "error";
402 		break;
403 	case REGULATOR_STATUS_FAST:
404 		label = "fast";
405 		break;
406 	case REGULATOR_STATUS_NORMAL:
407 		label = "normal";
408 		break;
409 	case REGULATOR_STATUS_IDLE:
410 		label = "idle";
411 		break;
412 	case REGULATOR_STATUS_STANDBY:
413 		label = "standby";
414 		break;
415 	case REGULATOR_STATUS_BYPASS:
416 		label = "bypass";
417 		break;
418 	case REGULATOR_STATUS_UNDEFINED:
419 		label = "undefined";
420 		break;
421 	default:
422 		return -ERANGE;
423 	}
424 
425 	return sprintf(buf, "%s\n", label);
426 }
427 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
428 
429 static ssize_t regulator_min_uA_show(struct device *dev,
430 				    struct device_attribute *attr, char *buf)
431 {
432 	struct regulator_dev *rdev = dev_get_drvdata(dev);
433 
434 	if (!rdev->constraints)
435 		return sprintf(buf, "constraint not defined\n");
436 
437 	return sprintf(buf, "%d\n", rdev->constraints->min_uA);
438 }
439 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
440 
441 static ssize_t regulator_max_uA_show(struct device *dev,
442 				    struct device_attribute *attr, char *buf)
443 {
444 	struct regulator_dev *rdev = dev_get_drvdata(dev);
445 
446 	if (!rdev->constraints)
447 		return sprintf(buf, "constraint not defined\n");
448 
449 	return sprintf(buf, "%d\n", rdev->constraints->max_uA);
450 }
451 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
452 
453 static ssize_t regulator_min_uV_show(struct device *dev,
454 				    struct device_attribute *attr, char *buf)
455 {
456 	struct regulator_dev *rdev = dev_get_drvdata(dev);
457 
458 	if (!rdev->constraints)
459 		return sprintf(buf, "constraint not defined\n");
460 
461 	return sprintf(buf, "%d\n", rdev->constraints->min_uV);
462 }
463 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
464 
465 static ssize_t regulator_max_uV_show(struct device *dev,
466 				    struct device_attribute *attr, char *buf)
467 {
468 	struct regulator_dev *rdev = dev_get_drvdata(dev);
469 
470 	if (!rdev->constraints)
471 		return sprintf(buf, "constraint not defined\n");
472 
473 	return sprintf(buf, "%d\n", rdev->constraints->max_uV);
474 }
475 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
476 
477 static ssize_t regulator_total_uA_show(struct device *dev,
478 				      struct device_attribute *attr, char *buf)
479 {
480 	struct regulator_dev *rdev = dev_get_drvdata(dev);
481 	struct regulator *regulator;
482 	int uA = 0;
483 
484 	mutex_lock(&rdev->mutex);
485 	list_for_each_entry(regulator, &rdev->consumer_list, list)
486 		uA += regulator->uA_load;
487 	mutex_unlock(&rdev->mutex);
488 	return sprintf(buf, "%d\n", uA);
489 }
490 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
491 
492 static ssize_t regulator_num_users_show(struct device *dev,
493 				      struct device_attribute *attr, char *buf)
494 {
495 	struct regulator_dev *rdev = dev_get_drvdata(dev);
496 	return sprintf(buf, "%d\n", rdev->use_count);
497 }
498 
499 static ssize_t regulator_type_show(struct device *dev,
500 				  struct device_attribute *attr, char *buf)
501 {
502 	struct regulator_dev *rdev = dev_get_drvdata(dev);
503 
504 	switch (rdev->desc->type) {
505 	case REGULATOR_VOLTAGE:
506 		return sprintf(buf, "voltage\n");
507 	case REGULATOR_CURRENT:
508 		return sprintf(buf, "current\n");
509 	}
510 	return sprintf(buf, "unknown\n");
511 }
512 
513 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
514 				struct device_attribute *attr, char *buf)
515 {
516 	struct regulator_dev *rdev = dev_get_drvdata(dev);
517 
518 	return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
519 }
520 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
521 		regulator_suspend_mem_uV_show, NULL);
522 
523 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
524 				struct device_attribute *attr, char *buf)
525 {
526 	struct regulator_dev *rdev = dev_get_drvdata(dev);
527 
528 	return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
529 }
530 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
531 		regulator_suspend_disk_uV_show, NULL);
532 
533 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
534 				struct device_attribute *attr, char *buf)
535 {
536 	struct regulator_dev *rdev = dev_get_drvdata(dev);
537 
538 	return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
539 }
540 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
541 		regulator_suspend_standby_uV_show, NULL);
542 
543 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
544 				struct device_attribute *attr, char *buf)
545 {
546 	struct regulator_dev *rdev = dev_get_drvdata(dev);
547 
548 	return regulator_print_opmode(buf,
549 		rdev->constraints->state_mem.mode);
550 }
551 static DEVICE_ATTR(suspend_mem_mode, 0444,
552 		regulator_suspend_mem_mode_show, NULL);
553 
554 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
555 				struct device_attribute *attr, char *buf)
556 {
557 	struct regulator_dev *rdev = dev_get_drvdata(dev);
558 
559 	return regulator_print_opmode(buf,
560 		rdev->constraints->state_disk.mode);
561 }
562 static DEVICE_ATTR(suspend_disk_mode, 0444,
563 		regulator_suspend_disk_mode_show, NULL);
564 
565 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
566 				struct device_attribute *attr, char *buf)
567 {
568 	struct regulator_dev *rdev = dev_get_drvdata(dev);
569 
570 	return regulator_print_opmode(buf,
571 		rdev->constraints->state_standby.mode);
572 }
573 static DEVICE_ATTR(suspend_standby_mode, 0444,
574 		regulator_suspend_standby_mode_show, NULL);
575 
576 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
577 				   struct device_attribute *attr, char *buf)
578 {
579 	struct regulator_dev *rdev = dev_get_drvdata(dev);
580 
581 	return regulator_print_state(buf,
582 			rdev->constraints->state_mem.enabled);
583 }
584 static DEVICE_ATTR(suspend_mem_state, 0444,
585 		regulator_suspend_mem_state_show, NULL);
586 
587 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
588 				   struct device_attribute *attr, char *buf)
589 {
590 	struct regulator_dev *rdev = dev_get_drvdata(dev);
591 
592 	return regulator_print_state(buf,
593 			rdev->constraints->state_disk.enabled);
594 }
595 static DEVICE_ATTR(suspend_disk_state, 0444,
596 		regulator_suspend_disk_state_show, NULL);
597 
598 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
599 				   struct device_attribute *attr, char *buf)
600 {
601 	struct regulator_dev *rdev = dev_get_drvdata(dev);
602 
603 	return regulator_print_state(buf,
604 			rdev->constraints->state_standby.enabled);
605 }
606 static DEVICE_ATTR(suspend_standby_state, 0444,
607 		regulator_suspend_standby_state_show, NULL);
608 
609 static ssize_t regulator_bypass_show(struct device *dev,
610 				     struct device_attribute *attr, char *buf)
611 {
612 	struct regulator_dev *rdev = dev_get_drvdata(dev);
613 	const char *report;
614 	bool bypass;
615 	int ret;
616 
617 	ret = rdev->desc->ops->get_bypass(rdev, &bypass);
618 
619 	if (ret != 0)
620 		report = "unknown";
621 	else if (bypass)
622 		report = "enabled";
623 	else
624 		report = "disabled";
625 
626 	return sprintf(buf, "%s\n", report);
627 }
628 static DEVICE_ATTR(bypass, 0444,
629 		   regulator_bypass_show, NULL);
630 
631 /*
632  * These are the only attributes are present for all regulators.
633  * Other attributes are a function of regulator functionality.
634  */
635 static struct device_attribute regulator_dev_attrs[] = {
636 	__ATTR(name, 0444, regulator_name_show, NULL),
637 	__ATTR(num_users, 0444, regulator_num_users_show, NULL),
638 	__ATTR(type, 0444, regulator_type_show, NULL),
639 	__ATTR_NULL,
640 };
641 
642 static void regulator_dev_release(struct device *dev)
643 {
644 	struct regulator_dev *rdev = dev_get_drvdata(dev);
645 	kfree(rdev);
646 }
647 
648 static struct class regulator_class = {
649 	.name = "regulator",
650 	.dev_release = regulator_dev_release,
651 	.dev_attrs = regulator_dev_attrs,
652 };
653 
654 /* Calculate the new optimum regulator operating mode based on the new total
655  * consumer load. All locks held by caller */
656 static void drms_uA_update(struct regulator_dev *rdev)
657 {
658 	struct regulator *sibling;
659 	int current_uA = 0, output_uV, input_uV, err;
660 	unsigned int mode;
661 
662 	err = regulator_check_drms(rdev);
663 	if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
664 	    (!rdev->desc->ops->get_voltage &&
665 	     !rdev->desc->ops->get_voltage_sel) ||
666 	    !rdev->desc->ops->set_mode)
667 		return;
668 
669 	/* get output voltage */
670 	output_uV = _regulator_get_voltage(rdev);
671 	if (output_uV <= 0)
672 		return;
673 
674 	/* get input voltage */
675 	input_uV = 0;
676 	if (rdev->supply)
677 		input_uV = regulator_get_voltage(rdev->supply);
678 	if (input_uV <= 0)
679 		input_uV = rdev->constraints->input_uV;
680 	if (input_uV <= 0)
681 		return;
682 
683 	/* calc total requested load */
684 	list_for_each_entry(sibling, &rdev->consumer_list, list)
685 		current_uA += sibling->uA_load;
686 
687 	/* now get the optimum mode for our new total regulator load */
688 	mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
689 						  output_uV, current_uA);
690 
691 	/* check the new mode is allowed */
692 	err = regulator_mode_constrain(rdev, &mode);
693 	if (err == 0)
694 		rdev->desc->ops->set_mode(rdev, mode);
695 }
696 
697 static int suspend_set_state(struct regulator_dev *rdev,
698 	struct regulator_state *rstate)
699 {
700 	int ret = 0;
701 
702 	/* If we have no suspend mode configration don't set anything;
703 	 * only warn if the driver implements set_suspend_voltage or
704 	 * set_suspend_mode callback.
705 	 */
706 	if (!rstate->enabled && !rstate->disabled) {
707 		if (rdev->desc->ops->set_suspend_voltage ||
708 		    rdev->desc->ops->set_suspend_mode)
709 			rdev_warn(rdev, "No configuration\n");
710 		return 0;
711 	}
712 
713 	if (rstate->enabled && rstate->disabled) {
714 		rdev_err(rdev, "invalid configuration\n");
715 		return -EINVAL;
716 	}
717 
718 	if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
719 		ret = rdev->desc->ops->set_suspend_enable(rdev);
720 	else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
721 		ret = rdev->desc->ops->set_suspend_disable(rdev);
722 	else /* OK if set_suspend_enable or set_suspend_disable is NULL */
723 		ret = 0;
724 
725 	if (ret < 0) {
726 		rdev_err(rdev, "failed to enabled/disable\n");
727 		return ret;
728 	}
729 
730 	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
731 		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
732 		if (ret < 0) {
733 			rdev_err(rdev, "failed to set voltage\n");
734 			return ret;
735 		}
736 	}
737 
738 	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
739 		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
740 		if (ret < 0) {
741 			rdev_err(rdev, "failed to set mode\n");
742 			return ret;
743 		}
744 	}
745 	return ret;
746 }
747 
748 /* locks held by caller */
749 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
750 {
751 	if (!rdev->constraints)
752 		return -EINVAL;
753 
754 	switch (state) {
755 	case PM_SUSPEND_STANDBY:
756 		return suspend_set_state(rdev,
757 			&rdev->constraints->state_standby);
758 	case PM_SUSPEND_MEM:
759 		return suspend_set_state(rdev,
760 			&rdev->constraints->state_mem);
761 	case PM_SUSPEND_MAX:
762 		return suspend_set_state(rdev,
763 			&rdev->constraints->state_disk);
764 	default:
765 		return -EINVAL;
766 	}
767 }
768 
769 static void print_constraints(struct regulator_dev *rdev)
770 {
771 	struct regulation_constraints *constraints = rdev->constraints;
772 	char buf[80] = "";
773 	int count = 0;
774 	int ret;
775 
776 	if (constraints->min_uV && constraints->max_uV) {
777 		if (constraints->min_uV == constraints->max_uV)
778 			count += sprintf(buf + count, "%d mV ",
779 					 constraints->min_uV / 1000);
780 		else
781 			count += sprintf(buf + count, "%d <--> %d mV ",
782 					 constraints->min_uV / 1000,
783 					 constraints->max_uV / 1000);
784 	}
785 
786 	if (!constraints->min_uV ||
787 	    constraints->min_uV != constraints->max_uV) {
788 		ret = _regulator_get_voltage(rdev);
789 		if (ret > 0)
790 			count += sprintf(buf + count, "at %d mV ", ret / 1000);
791 	}
792 
793 	if (constraints->uV_offset)
794 		count += sprintf(buf, "%dmV offset ",
795 				 constraints->uV_offset / 1000);
796 
797 	if (constraints->min_uA && constraints->max_uA) {
798 		if (constraints->min_uA == constraints->max_uA)
799 			count += sprintf(buf + count, "%d mA ",
800 					 constraints->min_uA / 1000);
801 		else
802 			count += sprintf(buf + count, "%d <--> %d mA ",
803 					 constraints->min_uA / 1000,
804 					 constraints->max_uA / 1000);
805 	}
806 
807 	if (!constraints->min_uA ||
808 	    constraints->min_uA != constraints->max_uA) {
809 		ret = _regulator_get_current_limit(rdev);
810 		if (ret > 0)
811 			count += sprintf(buf + count, "at %d mA ", ret / 1000);
812 	}
813 
814 	if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
815 		count += sprintf(buf + count, "fast ");
816 	if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
817 		count += sprintf(buf + count, "normal ");
818 	if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
819 		count += sprintf(buf + count, "idle ");
820 	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
821 		count += sprintf(buf + count, "standby");
822 
823 	if (!count)
824 		sprintf(buf, "no parameters");
825 
826 	rdev_info(rdev, "%s\n", buf);
827 
828 	if ((constraints->min_uV != constraints->max_uV) &&
829 	    !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
830 		rdev_warn(rdev,
831 			  "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
832 }
833 
834 static int machine_constraints_voltage(struct regulator_dev *rdev,
835 	struct regulation_constraints *constraints)
836 {
837 	struct regulator_ops *ops = rdev->desc->ops;
838 	int ret;
839 
840 	/* do we need to apply the constraint voltage */
841 	if (rdev->constraints->apply_uV &&
842 	    rdev->constraints->min_uV == rdev->constraints->max_uV) {
843 		ret = _regulator_do_set_voltage(rdev,
844 						rdev->constraints->min_uV,
845 						rdev->constraints->max_uV);
846 		if (ret < 0) {
847 			rdev_err(rdev, "failed to apply %duV constraint\n",
848 				 rdev->constraints->min_uV);
849 			return ret;
850 		}
851 	}
852 
853 	/* constrain machine-level voltage specs to fit
854 	 * the actual range supported by this regulator.
855 	 */
856 	if (ops->list_voltage && rdev->desc->n_voltages) {
857 		int	count = rdev->desc->n_voltages;
858 		int	i;
859 		int	min_uV = INT_MAX;
860 		int	max_uV = INT_MIN;
861 		int	cmin = constraints->min_uV;
862 		int	cmax = constraints->max_uV;
863 
864 		/* it's safe to autoconfigure fixed-voltage supplies
865 		   and the constraints are used by list_voltage. */
866 		if (count == 1 && !cmin) {
867 			cmin = 1;
868 			cmax = INT_MAX;
869 			constraints->min_uV = cmin;
870 			constraints->max_uV = cmax;
871 		}
872 
873 		/* voltage constraints are optional */
874 		if ((cmin == 0) && (cmax == 0))
875 			return 0;
876 
877 		/* else require explicit machine-level constraints */
878 		if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
879 			rdev_err(rdev, "invalid voltage constraints\n");
880 			return -EINVAL;
881 		}
882 
883 		/* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
884 		for (i = 0; i < count; i++) {
885 			int	value;
886 
887 			value = ops->list_voltage(rdev, i);
888 			if (value <= 0)
889 				continue;
890 
891 			/* maybe adjust [min_uV..max_uV] */
892 			if (value >= cmin && value < min_uV)
893 				min_uV = value;
894 			if (value <= cmax && value > max_uV)
895 				max_uV = value;
896 		}
897 
898 		/* final: [min_uV..max_uV] valid iff constraints valid */
899 		if (max_uV < min_uV) {
900 			rdev_err(rdev,
901 				 "unsupportable voltage constraints %u-%uuV\n",
902 				 min_uV, max_uV);
903 			return -EINVAL;
904 		}
905 
906 		/* use regulator's subset of machine constraints */
907 		if (constraints->min_uV < min_uV) {
908 			rdev_dbg(rdev, "override min_uV, %d -> %d\n",
909 				 constraints->min_uV, min_uV);
910 			constraints->min_uV = min_uV;
911 		}
912 		if (constraints->max_uV > max_uV) {
913 			rdev_dbg(rdev, "override max_uV, %d -> %d\n",
914 				 constraints->max_uV, max_uV);
915 			constraints->max_uV = max_uV;
916 		}
917 	}
918 
919 	return 0;
920 }
921 
922 /**
923  * set_machine_constraints - sets regulator constraints
924  * @rdev: regulator source
925  * @constraints: constraints to apply
926  *
927  * Allows platform initialisation code to define and constrain
928  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
929  * Constraints *must* be set by platform code in order for some
930  * regulator operations to proceed i.e. set_voltage, set_current_limit,
931  * set_mode.
932  */
933 static int set_machine_constraints(struct regulator_dev *rdev,
934 	const struct regulation_constraints *constraints)
935 {
936 	int ret = 0;
937 	struct regulator_ops *ops = rdev->desc->ops;
938 
939 	if (constraints)
940 		rdev->constraints = kmemdup(constraints, sizeof(*constraints),
941 					    GFP_KERNEL);
942 	else
943 		rdev->constraints = kzalloc(sizeof(*constraints),
944 					    GFP_KERNEL);
945 	if (!rdev->constraints)
946 		return -ENOMEM;
947 
948 	ret = machine_constraints_voltage(rdev, rdev->constraints);
949 	if (ret != 0)
950 		goto out;
951 
952 	/* do we need to setup our suspend state */
953 	if (rdev->constraints->initial_state) {
954 		ret = suspend_prepare(rdev, rdev->constraints->initial_state);
955 		if (ret < 0) {
956 			rdev_err(rdev, "failed to set suspend state\n");
957 			goto out;
958 		}
959 	}
960 
961 	if (rdev->constraints->initial_mode) {
962 		if (!ops->set_mode) {
963 			rdev_err(rdev, "no set_mode operation\n");
964 			ret = -EINVAL;
965 			goto out;
966 		}
967 
968 		ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
969 		if (ret < 0) {
970 			rdev_err(rdev, "failed to set initial mode: %d\n", ret);
971 			goto out;
972 		}
973 	}
974 
975 	/* If the constraints say the regulator should be on at this point
976 	 * and we have control then make sure it is enabled.
977 	 */
978 	if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
979 	    ops->enable) {
980 		ret = ops->enable(rdev);
981 		if (ret < 0) {
982 			rdev_err(rdev, "failed to enable\n");
983 			goto out;
984 		}
985 	}
986 
987 	if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
988 		ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
989 		if (ret < 0) {
990 			rdev_err(rdev, "failed to set ramp_delay\n");
991 			goto out;
992 		}
993 	}
994 
995 	print_constraints(rdev);
996 	return 0;
997 out:
998 	kfree(rdev->constraints);
999 	rdev->constraints = NULL;
1000 	return ret;
1001 }
1002 
1003 /**
1004  * set_supply - set regulator supply regulator
1005  * @rdev: regulator name
1006  * @supply_rdev: supply regulator name
1007  *
1008  * Called by platform initialisation code to set the supply regulator for this
1009  * regulator. This ensures that a regulators supply will also be enabled by the
1010  * core if it's child is enabled.
1011  */
1012 static int set_supply(struct regulator_dev *rdev,
1013 		      struct regulator_dev *supply_rdev)
1014 {
1015 	int err;
1016 
1017 	rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1018 
1019 	rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1020 	if (rdev->supply == NULL) {
1021 		err = -ENOMEM;
1022 		return err;
1023 	}
1024 	supply_rdev->open_count++;
1025 
1026 	return 0;
1027 }
1028 
1029 /**
1030  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1031  * @rdev:         regulator source
1032  * @consumer_dev_name: dev_name() string for device supply applies to
1033  * @supply:       symbolic name for supply
1034  *
1035  * Allows platform initialisation code to map physical regulator
1036  * sources to symbolic names for supplies for use by devices.  Devices
1037  * should use these symbolic names to request regulators, avoiding the
1038  * need to provide board-specific regulator names as platform data.
1039  */
1040 static int set_consumer_device_supply(struct regulator_dev *rdev,
1041 				      const char *consumer_dev_name,
1042 				      const char *supply)
1043 {
1044 	struct regulator_map *node;
1045 	int has_dev;
1046 
1047 	if (supply == NULL)
1048 		return -EINVAL;
1049 
1050 	if (consumer_dev_name != NULL)
1051 		has_dev = 1;
1052 	else
1053 		has_dev = 0;
1054 
1055 	list_for_each_entry(node, &regulator_map_list, list) {
1056 		if (node->dev_name && consumer_dev_name) {
1057 			if (strcmp(node->dev_name, consumer_dev_name) != 0)
1058 				continue;
1059 		} else if (node->dev_name || consumer_dev_name) {
1060 			continue;
1061 		}
1062 
1063 		if (strcmp(node->supply, supply) != 0)
1064 			continue;
1065 
1066 		pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1067 			 consumer_dev_name,
1068 			 dev_name(&node->regulator->dev),
1069 			 node->regulator->desc->name,
1070 			 supply,
1071 			 dev_name(&rdev->dev), rdev_get_name(rdev));
1072 		return -EBUSY;
1073 	}
1074 
1075 	node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1076 	if (node == NULL)
1077 		return -ENOMEM;
1078 
1079 	node->regulator = rdev;
1080 	node->supply = supply;
1081 
1082 	if (has_dev) {
1083 		node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1084 		if (node->dev_name == NULL) {
1085 			kfree(node);
1086 			return -ENOMEM;
1087 		}
1088 	}
1089 
1090 	list_add(&node->list, &regulator_map_list);
1091 	return 0;
1092 }
1093 
1094 static void unset_regulator_supplies(struct regulator_dev *rdev)
1095 {
1096 	struct regulator_map *node, *n;
1097 
1098 	list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1099 		if (rdev == node->regulator) {
1100 			list_del(&node->list);
1101 			kfree(node->dev_name);
1102 			kfree(node);
1103 		}
1104 	}
1105 }
1106 
1107 #define REG_STR_SIZE	64
1108 
1109 static struct regulator *create_regulator(struct regulator_dev *rdev,
1110 					  struct device *dev,
1111 					  const char *supply_name)
1112 {
1113 	struct regulator *regulator;
1114 	char buf[REG_STR_SIZE];
1115 	int err, size;
1116 
1117 	regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1118 	if (regulator == NULL)
1119 		return NULL;
1120 
1121 	mutex_lock(&rdev->mutex);
1122 	regulator->rdev = rdev;
1123 	list_add(&regulator->list, &rdev->consumer_list);
1124 
1125 	if (dev) {
1126 		regulator->dev = dev;
1127 
1128 		/* Add a link to the device sysfs entry */
1129 		size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1130 				 dev->kobj.name, supply_name);
1131 		if (size >= REG_STR_SIZE)
1132 			goto overflow_err;
1133 
1134 		regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1135 		if (regulator->supply_name == NULL)
1136 			goto overflow_err;
1137 
1138 		err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1139 					buf);
1140 		if (err) {
1141 			rdev_warn(rdev, "could not add device link %s err %d\n",
1142 				  dev->kobj.name, err);
1143 			/* non-fatal */
1144 		}
1145 	} else {
1146 		regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1147 		if (regulator->supply_name == NULL)
1148 			goto overflow_err;
1149 	}
1150 
1151 	regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1152 						rdev->debugfs);
1153 	if (!regulator->debugfs) {
1154 		rdev_warn(rdev, "Failed to create debugfs directory\n");
1155 	} else {
1156 		debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1157 				   &regulator->uA_load);
1158 		debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1159 				   &regulator->min_uV);
1160 		debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1161 				   &regulator->max_uV);
1162 	}
1163 
1164 	/*
1165 	 * Check now if the regulator is an always on regulator - if
1166 	 * it is then we don't need to do nearly so much work for
1167 	 * enable/disable calls.
1168 	 */
1169 	if (!_regulator_can_change_status(rdev) &&
1170 	    _regulator_is_enabled(rdev))
1171 		regulator->always_on = true;
1172 
1173 	mutex_unlock(&rdev->mutex);
1174 	return regulator;
1175 overflow_err:
1176 	list_del(&regulator->list);
1177 	kfree(regulator);
1178 	mutex_unlock(&rdev->mutex);
1179 	return NULL;
1180 }
1181 
1182 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1183 {
1184 	if (!rdev->desc->ops->enable_time)
1185 		return rdev->desc->enable_time;
1186 	return rdev->desc->ops->enable_time(rdev);
1187 }
1188 
1189 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1190 						  const char *supply,
1191 						  int *ret)
1192 {
1193 	struct regulator_dev *r;
1194 	struct device_node *node;
1195 	struct regulator_map *map;
1196 	const char *devname = NULL;
1197 
1198 	/* first do a dt based lookup */
1199 	if (dev && dev->of_node) {
1200 		node = of_get_regulator(dev, supply);
1201 		if (node) {
1202 			list_for_each_entry(r, &regulator_list, list)
1203 				if (r->dev.parent &&
1204 					node == r->dev.of_node)
1205 					return r;
1206 		} else {
1207 			/*
1208 			 * If we couldn't even get the node then it's
1209 			 * not just that the device didn't register
1210 			 * yet, there's no node and we'll never
1211 			 * succeed.
1212 			 */
1213 			*ret = -ENODEV;
1214 		}
1215 	}
1216 
1217 	/* if not found, try doing it non-dt way */
1218 	if (dev)
1219 		devname = dev_name(dev);
1220 
1221 	list_for_each_entry(r, &regulator_list, list)
1222 		if (strcmp(rdev_get_name(r), supply) == 0)
1223 			return r;
1224 
1225 	list_for_each_entry(map, &regulator_map_list, list) {
1226 		/* If the mapping has a device set up it must match */
1227 		if (map->dev_name &&
1228 		    (!devname || strcmp(map->dev_name, devname)))
1229 			continue;
1230 
1231 		if (strcmp(map->supply, supply) == 0)
1232 			return map->regulator;
1233 	}
1234 
1235 
1236 	return NULL;
1237 }
1238 
1239 /* Internal regulator request function */
1240 static struct regulator *_regulator_get(struct device *dev, const char *id,
1241 					int exclusive)
1242 {
1243 	struct regulator_dev *rdev;
1244 	struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1245 	const char *devname = NULL;
1246 	int ret = 0;
1247 
1248 	if (id == NULL) {
1249 		pr_err("get() with no identifier\n");
1250 		return regulator;
1251 	}
1252 
1253 	if (dev)
1254 		devname = dev_name(dev);
1255 
1256 	mutex_lock(&regulator_list_mutex);
1257 
1258 	rdev = regulator_dev_lookup(dev, id, &ret);
1259 	if (rdev)
1260 		goto found;
1261 
1262 	/*
1263 	 * If we have return value from dev_lookup fail, we do not expect to
1264 	 * succeed, so, quit with appropriate error value
1265 	 */
1266 	if (ret) {
1267 		regulator = ERR_PTR(ret);
1268 		goto out;
1269 	}
1270 
1271 	if (board_wants_dummy_regulator) {
1272 		rdev = dummy_regulator_rdev;
1273 		goto found;
1274 	}
1275 
1276 #ifdef CONFIG_REGULATOR_DUMMY
1277 	if (!devname)
1278 		devname = "deviceless";
1279 
1280 	/* If the board didn't flag that it was fully constrained then
1281 	 * substitute in a dummy regulator so consumers can continue.
1282 	 */
1283 	if (!has_full_constraints) {
1284 		pr_warn("%s supply %s not found, using dummy regulator\n",
1285 			devname, id);
1286 		rdev = dummy_regulator_rdev;
1287 		goto found;
1288 	}
1289 #endif
1290 
1291 	mutex_unlock(&regulator_list_mutex);
1292 	return regulator;
1293 
1294 found:
1295 	if (rdev->exclusive) {
1296 		regulator = ERR_PTR(-EPERM);
1297 		goto out;
1298 	}
1299 
1300 	if (exclusive && rdev->open_count) {
1301 		regulator = ERR_PTR(-EBUSY);
1302 		goto out;
1303 	}
1304 
1305 	if (!try_module_get(rdev->owner))
1306 		goto out;
1307 
1308 	regulator = create_regulator(rdev, dev, id);
1309 	if (regulator == NULL) {
1310 		regulator = ERR_PTR(-ENOMEM);
1311 		module_put(rdev->owner);
1312 		goto out;
1313 	}
1314 
1315 	rdev->open_count++;
1316 	if (exclusive) {
1317 		rdev->exclusive = 1;
1318 
1319 		ret = _regulator_is_enabled(rdev);
1320 		if (ret > 0)
1321 			rdev->use_count = 1;
1322 		else
1323 			rdev->use_count = 0;
1324 	}
1325 
1326 out:
1327 	mutex_unlock(&regulator_list_mutex);
1328 
1329 	return regulator;
1330 }
1331 
1332 /**
1333  * regulator_get - lookup and obtain a reference to a regulator.
1334  * @dev: device for regulator "consumer"
1335  * @id: Supply name or regulator ID.
1336  *
1337  * Returns a struct regulator corresponding to the regulator producer,
1338  * or IS_ERR() condition containing errno.
1339  *
1340  * Use of supply names configured via regulator_set_device_supply() is
1341  * strongly encouraged.  It is recommended that the supply name used
1342  * should match the name used for the supply and/or the relevant
1343  * device pins in the datasheet.
1344  */
1345 struct regulator *regulator_get(struct device *dev, const char *id)
1346 {
1347 	return _regulator_get(dev, id, 0);
1348 }
1349 EXPORT_SYMBOL_GPL(regulator_get);
1350 
1351 static void devm_regulator_release(struct device *dev, void *res)
1352 {
1353 	regulator_put(*(struct regulator **)res);
1354 }
1355 
1356 /**
1357  * devm_regulator_get - Resource managed regulator_get()
1358  * @dev: device for regulator "consumer"
1359  * @id: Supply name or regulator ID.
1360  *
1361  * Managed regulator_get(). Regulators returned from this function are
1362  * automatically regulator_put() on driver detach. See regulator_get() for more
1363  * information.
1364  */
1365 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1366 {
1367 	struct regulator **ptr, *regulator;
1368 
1369 	ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1370 	if (!ptr)
1371 		return ERR_PTR(-ENOMEM);
1372 
1373 	regulator = regulator_get(dev, id);
1374 	if (!IS_ERR(regulator)) {
1375 		*ptr = regulator;
1376 		devres_add(dev, ptr);
1377 	} else {
1378 		devres_free(ptr);
1379 	}
1380 
1381 	return regulator;
1382 }
1383 EXPORT_SYMBOL_GPL(devm_regulator_get);
1384 
1385 /**
1386  * regulator_get_exclusive - obtain exclusive access to a regulator.
1387  * @dev: device for regulator "consumer"
1388  * @id: Supply name or regulator ID.
1389  *
1390  * Returns a struct regulator corresponding to the regulator producer,
1391  * or IS_ERR() condition containing errno.  Other consumers will be
1392  * unable to obtain this reference is held and the use count for the
1393  * regulator will be initialised to reflect the current state of the
1394  * regulator.
1395  *
1396  * This is intended for use by consumers which cannot tolerate shared
1397  * use of the regulator such as those which need to force the
1398  * regulator off for correct operation of the hardware they are
1399  * controlling.
1400  *
1401  * Use of supply names configured via regulator_set_device_supply() is
1402  * strongly encouraged.  It is recommended that the supply name used
1403  * should match the name used for the supply and/or the relevant
1404  * device pins in the datasheet.
1405  */
1406 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1407 {
1408 	return _regulator_get(dev, id, 1);
1409 }
1410 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1411 
1412 /* Locks held by regulator_put() */
1413 static void _regulator_put(struct regulator *regulator)
1414 {
1415 	struct regulator_dev *rdev;
1416 
1417 	if (regulator == NULL || IS_ERR(regulator))
1418 		return;
1419 
1420 	rdev = regulator->rdev;
1421 
1422 	debugfs_remove_recursive(regulator->debugfs);
1423 
1424 	/* remove any sysfs entries */
1425 	if (regulator->dev)
1426 		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1427 	kfree(regulator->supply_name);
1428 	list_del(&regulator->list);
1429 	kfree(regulator);
1430 
1431 	rdev->open_count--;
1432 	rdev->exclusive = 0;
1433 
1434 	module_put(rdev->owner);
1435 }
1436 
1437 /**
1438  * regulator_put - "free" the regulator source
1439  * @regulator: regulator source
1440  *
1441  * Note: drivers must ensure that all regulator_enable calls made on this
1442  * regulator source are balanced by regulator_disable calls prior to calling
1443  * this function.
1444  */
1445 void regulator_put(struct regulator *regulator)
1446 {
1447 	mutex_lock(&regulator_list_mutex);
1448 	_regulator_put(regulator);
1449 	mutex_unlock(&regulator_list_mutex);
1450 }
1451 EXPORT_SYMBOL_GPL(regulator_put);
1452 
1453 static int devm_regulator_match(struct device *dev, void *res, void *data)
1454 {
1455 	struct regulator **r = res;
1456 	if (!r || !*r) {
1457 		WARN_ON(!r || !*r);
1458 		return 0;
1459 	}
1460 	return *r == data;
1461 }
1462 
1463 /**
1464  * devm_regulator_put - Resource managed regulator_put()
1465  * @regulator: regulator to free
1466  *
1467  * Deallocate a regulator allocated with devm_regulator_get(). Normally
1468  * this function will not need to be called and the resource management
1469  * code will ensure that the resource is freed.
1470  */
1471 void devm_regulator_put(struct regulator *regulator)
1472 {
1473 	int rc;
1474 
1475 	rc = devres_release(regulator->dev, devm_regulator_release,
1476 			    devm_regulator_match, regulator);
1477 	if (rc != 0)
1478 		WARN_ON(rc);
1479 }
1480 EXPORT_SYMBOL_GPL(devm_regulator_put);
1481 
1482 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1483 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1484 				const struct regulator_config *config)
1485 {
1486 	struct regulator_enable_gpio *pin;
1487 	int ret;
1488 
1489 	list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1490 		if (pin->gpio == config->ena_gpio) {
1491 			rdev_dbg(rdev, "GPIO %d is already used\n",
1492 				config->ena_gpio);
1493 			goto update_ena_gpio_to_rdev;
1494 		}
1495 	}
1496 
1497 	ret = gpio_request_one(config->ena_gpio,
1498 				GPIOF_DIR_OUT | config->ena_gpio_flags,
1499 				rdev_get_name(rdev));
1500 	if (ret)
1501 		return ret;
1502 
1503 	pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1504 	if (pin == NULL) {
1505 		gpio_free(config->ena_gpio);
1506 		return -ENOMEM;
1507 	}
1508 
1509 	pin->gpio = config->ena_gpio;
1510 	pin->ena_gpio_invert = config->ena_gpio_invert;
1511 	list_add(&pin->list, &regulator_ena_gpio_list);
1512 
1513 update_ena_gpio_to_rdev:
1514 	pin->request_count++;
1515 	rdev->ena_pin = pin;
1516 	return 0;
1517 }
1518 
1519 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1520 {
1521 	struct regulator_enable_gpio *pin, *n;
1522 
1523 	if (!rdev->ena_pin)
1524 		return;
1525 
1526 	/* Free the GPIO only in case of no use */
1527 	list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1528 		if (pin->gpio == rdev->ena_pin->gpio) {
1529 			if (pin->request_count <= 1) {
1530 				pin->request_count = 0;
1531 				gpio_free(pin->gpio);
1532 				list_del(&pin->list);
1533 				kfree(pin);
1534 			} else {
1535 				pin->request_count--;
1536 			}
1537 		}
1538 	}
1539 }
1540 
1541 /**
1542  * Balance enable_count of each GPIO and actual GPIO pin control.
1543  * GPIO is enabled in case of initial use. (enable_count is 0)
1544  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1545  */
1546 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1547 {
1548 	struct regulator_enable_gpio *pin = rdev->ena_pin;
1549 
1550 	if (!pin)
1551 		return -EINVAL;
1552 
1553 	if (enable) {
1554 		/* Enable GPIO at initial use */
1555 		if (pin->enable_count == 0)
1556 			gpio_set_value_cansleep(pin->gpio,
1557 						!pin->ena_gpio_invert);
1558 
1559 		pin->enable_count++;
1560 	} else {
1561 		if (pin->enable_count > 1) {
1562 			pin->enable_count--;
1563 			return 0;
1564 		}
1565 
1566 		/* Disable GPIO if not used */
1567 		if (pin->enable_count <= 1) {
1568 			gpio_set_value_cansleep(pin->gpio,
1569 						pin->ena_gpio_invert);
1570 			pin->enable_count = 0;
1571 		}
1572 	}
1573 
1574 	return 0;
1575 }
1576 
1577 static int _regulator_do_enable(struct regulator_dev *rdev)
1578 {
1579 	int ret, delay;
1580 
1581 	/* Query before enabling in case configuration dependent.  */
1582 	ret = _regulator_get_enable_time(rdev);
1583 	if (ret >= 0) {
1584 		delay = ret;
1585 	} else {
1586 		rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1587 		delay = 0;
1588 	}
1589 
1590 	trace_regulator_enable(rdev_get_name(rdev));
1591 
1592 	if (rdev->ena_pin) {
1593 		ret = regulator_ena_gpio_ctrl(rdev, true);
1594 		if (ret < 0)
1595 			return ret;
1596 		rdev->ena_gpio_state = 1;
1597 	} else if (rdev->desc->ops->enable) {
1598 		ret = rdev->desc->ops->enable(rdev);
1599 		if (ret < 0)
1600 			return ret;
1601 	} else {
1602 		return -EINVAL;
1603 	}
1604 
1605 	/* Allow the regulator to ramp; it would be useful to extend
1606 	 * this for bulk operations so that the regulators can ramp
1607 	 * together.  */
1608 	trace_regulator_enable_delay(rdev_get_name(rdev));
1609 
1610 	if (delay >= 1000) {
1611 		mdelay(delay / 1000);
1612 		udelay(delay % 1000);
1613 	} else if (delay) {
1614 		udelay(delay);
1615 	}
1616 
1617 	trace_regulator_enable_complete(rdev_get_name(rdev));
1618 
1619 	return 0;
1620 }
1621 
1622 /* locks held by regulator_enable() */
1623 static int _regulator_enable(struct regulator_dev *rdev)
1624 {
1625 	int ret;
1626 
1627 	/* check voltage and requested load before enabling */
1628 	if (rdev->constraints &&
1629 	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1630 		drms_uA_update(rdev);
1631 
1632 	if (rdev->use_count == 0) {
1633 		/* The regulator may on if it's not switchable or left on */
1634 		ret = _regulator_is_enabled(rdev);
1635 		if (ret == -EINVAL || ret == 0) {
1636 			if (!_regulator_can_change_status(rdev))
1637 				return -EPERM;
1638 
1639 			ret = _regulator_do_enable(rdev);
1640 			if (ret < 0)
1641 				return ret;
1642 
1643 		} else if (ret < 0) {
1644 			rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1645 			return ret;
1646 		}
1647 		/* Fallthrough on positive return values - already enabled */
1648 	}
1649 
1650 	rdev->use_count++;
1651 
1652 	return 0;
1653 }
1654 
1655 /**
1656  * regulator_enable - enable regulator output
1657  * @regulator: regulator source
1658  *
1659  * Request that the regulator be enabled with the regulator output at
1660  * the predefined voltage or current value.  Calls to regulator_enable()
1661  * must be balanced with calls to regulator_disable().
1662  *
1663  * NOTE: the output value can be set by other drivers, boot loader or may be
1664  * hardwired in the regulator.
1665  */
1666 int regulator_enable(struct regulator *regulator)
1667 {
1668 	struct regulator_dev *rdev = regulator->rdev;
1669 	int ret = 0;
1670 
1671 	if (regulator->always_on)
1672 		return 0;
1673 
1674 	if (rdev->supply) {
1675 		ret = regulator_enable(rdev->supply);
1676 		if (ret != 0)
1677 			return ret;
1678 	}
1679 
1680 	mutex_lock(&rdev->mutex);
1681 	ret = _regulator_enable(rdev);
1682 	mutex_unlock(&rdev->mutex);
1683 
1684 	if (ret != 0 && rdev->supply)
1685 		regulator_disable(rdev->supply);
1686 
1687 	return ret;
1688 }
1689 EXPORT_SYMBOL_GPL(regulator_enable);
1690 
1691 static int _regulator_do_disable(struct regulator_dev *rdev)
1692 {
1693 	int ret;
1694 
1695 	trace_regulator_disable(rdev_get_name(rdev));
1696 
1697 	if (rdev->ena_pin) {
1698 		ret = regulator_ena_gpio_ctrl(rdev, false);
1699 		if (ret < 0)
1700 			return ret;
1701 		rdev->ena_gpio_state = 0;
1702 
1703 	} else if (rdev->desc->ops->disable) {
1704 		ret = rdev->desc->ops->disable(rdev);
1705 		if (ret != 0)
1706 			return ret;
1707 	}
1708 
1709 	trace_regulator_disable_complete(rdev_get_name(rdev));
1710 
1711 	_notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1712 			     NULL);
1713 	return 0;
1714 }
1715 
1716 /* locks held by regulator_disable() */
1717 static int _regulator_disable(struct regulator_dev *rdev)
1718 {
1719 	int ret = 0;
1720 
1721 	if (WARN(rdev->use_count <= 0,
1722 		 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1723 		return -EIO;
1724 
1725 	/* are we the last user and permitted to disable ? */
1726 	if (rdev->use_count == 1 &&
1727 	    (rdev->constraints && !rdev->constraints->always_on)) {
1728 
1729 		/* we are last user */
1730 		if (_regulator_can_change_status(rdev)) {
1731 			ret = _regulator_do_disable(rdev);
1732 			if (ret < 0) {
1733 				rdev_err(rdev, "failed to disable\n");
1734 				return ret;
1735 			}
1736 		}
1737 
1738 		rdev->use_count = 0;
1739 	} else if (rdev->use_count > 1) {
1740 
1741 		if (rdev->constraints &&
1742 			(rdev->constraints->valid_ops_mask &
1743 			REGULATOR_CHANGE_DRMS))
1744 			drms_uA_update(rdev);
1745 
1746 		rdev->use_count--;
1747 	}
1748 
1749 	return ret;
1750 }
1751 
1752 /**
1753  * regulator_disable - disable regulator output
1754  * @regulator: regulator source
1755  *
1756  * Disable the regulator output voltage or current.  Calls to
1757  * regulator_enable() must be balanced with calls to
1758  * regulator_disable().
1759  *
1760  * NOTE: this will only disable the regulator output if no other consumer
1761  * devices have it enabled, the regulator device supports disabling and
1762  * machine constraints permit this operation.
1763  */
1764 int regulator_disable(struct regulator *regulator)
1765 {
1766 	struct regulator_dev *rdev = regulator->rdev;
1767 	int ret = 0;
1768 
1769 	if (regulator->always_on)
1770 		return 0;
1771 
1772 	mutex_lock(&rdev->mutex);
1773 	ret = _regulator_disable(rdev);
1774 	mutex_unlock(&rdev->mutex);
1775 
1776 	if (ret == 0 && rdev->supply)
1777 		regulator_disable(rdev->supply);
1778 
1779 	return ret;
1780 }
1781 EXPORT_SYMBOL_GPL(regulator_disable);
1782 
1783 /* locks held by regulator_force_disable() */
1784 static int _regulator_force_disable(struct regulator_dev *rdev)
1785 {
1786 	int ret = 0;
1787 
1788 	/* force disable */
1789 	if (rdev->desc->ops->disable) {
1790 		/* ah well, who wants to live forever... */
1791 		ret = rdev->desc->ops->disable(rdev);
1792 		if (ret < 0) {
1793 			rdev_err(rdev, "failed to force disable\n");
1794 			return ret;
1795 		}
1796 		/* notify other consumers that power has been forced off */
1797 		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1798 			REGULATOR_EVENT_DISABLE, NULL);
1799 	}
1800 
1801 	return ret;
1802 }
1803 
1804 /**
1805  * regulator_force_disable - force disable regulator output
1806  * @regulator: regulator source
1807  *
1808  * Forcibly disable the regulator output voltage or current.
1809  * NOTE: this *will* disable the regulator output even if other consumer
1810  * devices have it enabled. This should be used for situations when device
1811  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1812  */
1813 int regulator_force_disable(struct regulator *regulator)
1814 {
1815 	struct regulator_dev *rdev = regulator->rdev;
1816 	int ret;
1817 
1818 	mutex_lock(&rdev->mutex);
1819 	regulator->uA_load = 0;
1820 	ret = _regulator_force_disable(regulator->rdev);
1821 	mutex_unlock(&rdev->mutex);
1822 
1823 	if (rdev->supply)
1824 		while (rdev->open_count--)
1825 			regulator_disable(rdev->supply);
1826 
1827 	return ret;
1828 }
1829 EXPORT_SYMBOL_GPL(regulator_force_disable);
1830 
1831 static void regulator_disable_work(struct work_struct *work)
1832 {
1833 	struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1834 						  disable_work.work);
1835 	int count, i, ret;
1836 
1837 	mutex_lock(&rdev->mutex);
1838 
1839 	BUG_ON(!rdev->deferred_disables);
1840 
1841 	count = rdev->deferred_disables;
1842 	rdev->deferred_disables = 0;
1843 
1844 	for (i = 0; i < count; i++) {
1845 		ret = _regulator_disable(rdev);
1846 		if (ret != 0)
1847 			rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1848 	}
1849 
1850 	mutex_unlock(&rdev->mutex);
1851 
1852 	if (rdev->supply) {
1853 		for (i = 0; i < count; i++) {
1854 			ret = regulator_disable(rdev->supply);
1855 			if (ret != 0) {
1856 				rdev_err(rdev,
1857 					 "Supply disable failed: %d\n", ret);
1858 			}
1859 		}
1860 	}
1861 }
1862 
1863 /**
1864  * regulator_disable_deferred - disable regulator output with delay
1865  * @regulator: regulator source
1866  * @ms: miliseconds until the regulator is disabled
1867  *
1868  * Execute regulator_disable() on the regulator after a delay.  This
1869  * is intended for use with devices that require some time to quiesce.
1870  *
1871  * NOTE: this will only disable the regulator output if no other consumer
1872  * devices have it enabled, the regulator device supports disabling and
1873  * machine constraints permit this operation.
1874  */
1875 int regulator_disable_deferred(struct regulator *regulator, int ms)
1876 {
1877 	struct regulator_dev *rdev = regulator->rdev;
1878 	int ret;
1879 
1880 	if (regulator->always_on)
1881 		return 0;
1882 
1883 	if (!ms)
1884 		return regulator_disable(regulator);
1885 
1886 	mutex_lock(&rdev->mutex);
1887 	rdev->deferred_disables++;
1888 	mutex_unlock(&rdev->mutex);
1889 
1890 	ret = schedule_delayed_work(&rdev->disable_work,
1891 				    msecs_to_jiffies(ms));
1892 	if (ret < 0)
1893 		return ret;
1894 	else
1895 		return 0;
1896 }
1897 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1898 
1899 /**
1900  * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1901  *
1902  * @rdev: regulator to operate on
1903  *
1904  * Regulators that use regmap for their register I/O can set the
1905  * enable_reg and enable_mask fields in their descriptor and then use
1906  * this as their is_enabled operation, saving some code.
1907  */
1908 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1909 {
1910 	unsigned int val;
1911 	int ret;
1912 
1913 	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1914 	if (ret != 0)
1915 		return ret;
1916 
1917 	if (rdev->desc->enable_is_inverted)
1918 		return (val & rdev->desc->enable_mask) == 0;
1919 	else
1920 		return (val & rdev->desc->enable_mask) != 0;
1921 }
1922 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1923 
1924 /**
1925  * regulator_enable_regmap - standard enable() for regmap users
1926  *
1927  * @rdev: regulator to operate on
1928  *
1929  * Regulators that use regmap for their register I/O can set the
1930  * enable_reg and enable_mask fields in their descriptor and then use
1931  * this as their enable() operation, saving some code.
1932  */
1933 int regulator_enable_regmap(struct regulator_dev *rdev)
1934 {
1935 	unsigned int val;
1936 
1937 	if (rdev->desc->enable_is_inverted)
1938 		val = 0;
1939 	else
1940 		val = rdev->desc->enable_mask;
1941 
1942 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1943 				  rdev->desc->enable_mask, val);
1944 }
1945 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1946 
1947 /**
1948  * regulator_disable_regmap - standard disable() for regmap users
1949  *
1950  * @rdev: regulator to operate on
1951  *
1952  * Regulators that use regmap for their register I/O can set the
1953  * enable_reg and enable_mask fields in their descriptor and then use
1954  * this as their disable() operation, saving some code.
1955  */
1956 int regulator_disable_regmap(struct regulator_dev *rdev)
1957 {
1958 	unsigned int val;
1959 
1960 	if (rdev->desc->enable_is_inverted)
1961 		val = rdev->desc->enable_mask;
1962 	else
1963 		val = 0;
1964 
1965 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1966 				  rdev->desc->enable_mask, val);
1967 }
1968 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1969 
1970 static int _regulator_is_enabled(struct regulator_dev *rdev)
1971 {
1972 	/* A GPIO control always takes precedence */
1973 	if (rdev->ena_pin)
1974 		return rdev->ena_gpio_state;
1975 
1976 	/* If we don't know then assume that the regulator is always on */
1977 	if (!rdev->desc->ops->is_enabled)
1978 		return 1;
1979 
1980 	return rdev->desc->ops->is_enabled(rdev);
1981 }
1982 
1983 /**
1984  * regulator_is_enabled - is the regulator output enabled
1985  * @regulator: regulator source
1986  *
1987  * Returns positive if the regulator driver backing the source/client
1988  * has requested that the device be enabled, zero if it hasn't, else a
1989  * negative errno code.
1990  *
1991  * Note that the device backing this regulator handle can have multiple
1992  * users, so it might be enabled even if regulator_enable() was never
1993  * called for this particular source.
1994  */
1995 int regulator_is_enabled(struct regulator *regulator)
1996 {
1997 	int ret;
1998 
1999 	if (regulator->always_on)
2000 		return 1;
2001 
2002 	mutex_lock(&regulator->rdev->mutex);
2003 	ret = _regulator_is_enabled(regulator->rdev);
2004 	mutex_unlock(&regulator->rdev->mutex);
2005 
2006 	return ret;
2007 }
2008 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2009 
2010 /**
2011  * regulator_can_change_voltage - check if regulator can change voltage
2012  * @regulator: regulator source
2013  *
2014  * Returns positive if the regulator driver backing the source/client
2015  * can change its voltage, false otherwise. Usefull for detecting fixed
2016  * or dummy regulators and disabling voltage change logic in the client
2017  * driver.
2018  */
2019 int regulator_can_change_voltage(struct regulator *regulator)
2020 {
2021 	struct regulator_dev	*rdev = regulator->rdev;
2022 
2023 	if (rdev->constraints &&
2024 	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2025 		if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2026 			return 1;
2027 
2028 		if (rdev->desc->continuous_voltage_range &&
2029 		    rdev->constraints->min_uV && rdev->constraints->max_uV &&
2030 		    rdev->constraints->min_uV != rdev->constraints->max_uV)
2031 			return 1;
2032 	}
2033 
2034 	return 0;
2035 }
2036 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2037 
2038 /**
2039  * regulator_count_voltages - count regulator_list_voltage() selectors
2040  * @regulator: regulator source
2041  *
2042  * Returns number of selectors, or negative errno.  Selectors are
2043  * numbered starting at zero, and typically correspond to bitfields
2044  * in hardware registers.
2045  */
2046 int regulator_count_voltages(struct regulator *regulator)
2047 {
2048 	struct regulator_dev	*rdev = regulator->rdev;
2049 
2050 	return rdev->desc->n_voltages ? : -EINVAL;
2051 }
2052 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2053 
2054 /**
2055  * regulator_list_voltage_linear - List voltages with simple calculation
2056  *
2057  * @rdev: Regulator device
2058  * @selector: Selector to convert into a voltage
2059  *
2060  * Regulators with a simple linear mapping between voltages and
2061  * selectors can set min_uV and uV_step in the regulator descriptor
2062  * and then use this function as their list_voltage() operation,
2063  */
2064 int regulator_list_voltage_linear(struct regulator_dev *rdev,
2065 				  unsigned int selector)
2066 {
2067 	if (selector >= rdev->desc->n_voltages)
2068 		return -EINVAL;
2069 	if (selector < rdev->desc->linear_min_sel)
2070 		return 0;
2071 
2072 	selector -= rdev->desc->linear_min_sel;
2073 
2074 	return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2075 }
2076 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2077 
2078 /**
2079  * regulator_list_voltage_table - List voltages with table based mapping
2080  *
2081  * @rdev: Regulator device
2082  * @selector: Selector to convert into a voltage
2083  *
2084  * Regulators with table based mapping between voltages and
2085  * selectors can set volt_table in the regulator descriptor
2086  * and then use this function as their list_voltage() operation.
2087  */
2088 int regulator_list_voltage_table(struct regulator_dev *rdev,
2089 				 unsigned int selector)
2090 {
2091 	if (!rdev->desc->volt_table) {
2092 		BUG_ON(!rdev->desc->volt_table);
2093 		return -EINVAL;
2094 	}
2095 
2096 	if (selector >= rdev->desc->n_voltages)
2097 		return -EINVAL;
2098 
2099 	return rdev->desc->volt_table[selector];
2100 }
2101 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2102 
2103 /**
2104  * regulator_list_voltage - enumerate supported voltages
2105  * @regulator: regulator source
2106  * @selector: identify voltage to list
2107  * Context: can sleep
2108  *
2109  * Returns a voltage that can be passed to @regulator_set_voltage(),
2110  * zero if this selector code can't be used on this system, or a
2111  * negative errno.
2112  */
2113 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2114 {
2115 	struct regulator_dev	*rdev = regulator->rdev;
2116 	struct regulator_ops	*ops = rdev->desc->ops;
2117 	int			ret;
2118 
2119 	if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2120 		return -EINVAL;
2121 
2122 	mutex_lock(&rdev->mutex);
2123 	ret = ops->list_voltage(rdev, selector);
2124 	mutex_unlock(&rdev->mutex);
2125 
2126 	if (ret > 0) {
2127 		if (ret < rdev->constraints->min_uV)
2128 			ret = 0;
2129 		else if (ret > rdev->constraints->max_uV)
2130 			ret = 0;
2131 	}
2132 
2133 	return ret;
2134 }
2135 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2136 
2137 /**
2138  * regulator_is_supported_voltage - check if a voltage range can be supported
2139  *
2140  * @regulator: Regulator to check.
2141  * @min_uV: Minimum required voltage in uV.
2142  * @max_uV: Maximum required voltage in uV.
2143  *
2144  * Returns a boolean or a negative error code.
2145  */
2146 int regulator_is_supported_voltage(struct regulator *regulator,
2147 				   int min_uV, int max_uV)
2148 {
2149 	struct regulator_dev *rdev = regulator->rdev;
2150 	int i, voltages, ret;
2151 
2152 	/* If we can't change voltage check the current voltage */
2153 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2154 		ret = regulator_get_voltage(regulator);
2155 		if (ret >= 0)
2156 			return (min_uV <= ret && ret <= max_uV);
2157 		else
2158 			return ret;
2159 	}
2160 
2161 	/* Any voltage within constrains range is fine? */
2162 	if (rdev->desc->continuous_voltage_range)
2163 		return min_uV >= rdev->constraints->min_uV &&
2164 				max_uV <= rdev->constraints->max_uV;
2165 
2166 	ret = regulator_count_voltages(regulator);
2167 	if (ret < 0)
2168 		return ret;
2169 	voltages = ret;
2170 
2171 	for (i = 0; i < voltages; i++) {
2172 		ret = regulator_list_voltage(regulator, i);
2173 
2174 		if (ret >= min_uV && ret <= max_uV)
2175 			return 1;
2176 	}
2177 
2178 	return 0;
2179 }
2180 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2181 
2182 /**
2183  * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2184  *
2185  * @rdev: regulator to operate on
2186  *
2187  * Regulators that use regmap for their register I/O can set the
2188  * vsel_reg and vsel_mask fields in their descriptor and then use this
2189  * as their get_voltage_vsel operation, saving some code.
2190  */
2191 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2192 {
2193 	unsigned int val;
2194 	int ret;
2195 
2196 	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2197 	if (ret != 0)
2198 		return ret;
2199 
2200 	val &= rdev->desc->vsel_mask;
2201 	val >>= ffs(rdev->desc->vsel_mask) - 1;
2202 
2203 	return val;
2204 }
2205 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2206 
2207 /**
2208  * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2209  *
2210  * @rdev: regulator to operate on
2211  * @sel: Selector to set
2212  *
2213  * Regulators that use regmap for their register I/O can set the
2214  * vsel_reg and vsel_mask fields in their descriptor and then use this
2215  * as their set_voltage_vsel operation, saving some code.
2216  */
2217 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2218 {
2219 	int ret;
2220 
2221 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
2222 
2223 	ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2224 				  rdev->desc->vsel_mask, sel);
2225 	if (ret)
2226 		return ret;
2227 
2228 	if (rdev->desc->apply_bit)
2229 		ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2230 					 rdev->desc->apply_bit,
2231 					 rdev->desc->apply_bit);
2232 	return ret;
2233 }
2234 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2235 
2236 /**
2237  * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2238  *
2239  * @rdev: Regulator to operate on
2240  * @min_uV: Lower bound for voltage
2241  * @max_uV: Upper bound for voltage
2242  *
2243  * Drivers implementing set_voltage_sel() and list_voltage() can use
2244  * this as their map_voltage() operation.  It will find a suitable
2245  * voltage by calling list_voltage() until it gets something in bounds
2246  * for the requested voltages.
2247  */
2248 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2249 				  int min_uV, int max_uV)
2250 {
2251 	int best_val = INT_MAX;
2252 	int selector = 0;
2253 	int i, ret;
2254 
2255 	/* Find the smallest voltage that falls within the specified
2256 	 * range.
2257 	 */
2258 	for (i = 0; i < rdev->desc->n_voltages; i++) {
2259 		ret = rdev->desc->ops->list_voltage(rdev, i);
2260 		if (ret < 0)
2261 			continue;
2262 
2263 		if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2264 			best_val = ret;
2265 			selector = i;
2266 		}
2267 	}
2268 
2269 	if (best_val != INT_MAX)
2270 		return selector;
2271 	else
2272 		return -EINVAL;
2273 }
2274 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2275 
2276 /**
2277  * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2278  *
2279  * @rdev: Regulator to operate on
2280  * @min_uV: Lower bound for voltage
2281  * @max_uV: Upper bound for voltage
2282  *
2283  * Drivers that have ascendant voltage list can use this as their
2284  * map_voltage() operation.
2285  */
2286 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
2287 				 int min_uV, int max_uV)
2288 {
2289 	int i, ret;
2290 
2291 	for (i = 0; i < rdev->desc->n_voltages; i++) {
2292 		ret = rdev->desc->ops->list_voltage(rdev, i);
2293 		if (ret < 0)
2294 			continue;
2295 
2296 		if (ret > max_uV)
2297 			break;
2298 
2299 		if (ret >= min_uV && ret <= max_uV)
2300 			return i;
2301 	}
2302 
2303 	return -EINVAL;
2304 }
2305 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
2306 
2307 /**
2308  * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2309  *
2310  * @rdev: Regulator to operate on
2311  * @min_uV: Lower bound for voltage
2312  * @max_uV: Upper bound for voltage
2313  *
2314  * Drivers providing min_uV and uV_step in their regulator_desc can
2315  * use this as their map_voltage() operation.
2316  */
2317 int regulator_map_voltage_linear(struct regulator_dev *rdev,
2318 				 int min_uV, int max_uV)
2319 {
2320 	int ret, voltage;
2321 
2322 	/* Allow uV_step to be 0 for fixed voltage */
2323 	if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2324 		if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2325 			return 0;
2326 		else
2327 			return -EINVAL;
2328 	}
2329 
2330 	if (!rdev->desc->uV_step) {
2331 		BUG_ON(!rdev->desc->uV_step);
2332 		return -EINVAL;
2333 	}
2334 
2335 	if (min_uV < rdev->desc->min_uV)
2336 		min_uV = rdev->desc->min_uV;
2337 
2338 	ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2339 	if (ret < 0)
2340 		return ret;
2341 
2342 	ret += rdev->desc->linear_min_sel;
2343 
2344 	/* Map back into a voltage to verify we're still in bounds */
2345 	voltage = rdev->desc->ops->list_voltage(rdev, ret);
2346 	if (voltage < min_uV || voltage > max_uV)
2347 		return -EINVAL;
2348 
2349 	return ret;
2350 }
2351 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2352 
2353 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2354 				     int min_uV, int max_uV)
2355 {
2356 	int ret;
2357 	int delay = 0;
2358 	int best_val = 0;
2359 	unsigned int selector;
2360 	int old_selector = -1;
2361 
2362 	trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2363 
2364 	min_uV += rdev->constraints->uV_offset;
2365 	max_uV += rdev->constraints->uV_offset;
2366 
2367 	/*
2368 	 * If we can't obtain the old selector there is not enough
2369 	 * info to call set_voltage_time_sel().
2370 	 */
2371 	if (_regulator_is_enabled(rdev) &&
2372 	    rdev->desc->ops->set_voltage_time_sel &&
2373 	    rdev->desc->ops->get_voltage_sel) {
2374 		old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2375 		if (old_selector < 0)
2376 			return old_selector;
2377 	}
2378 
2379 	if (rdev->desc->ops->set_voltage) {
2380 		ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2381 						   &selector);
2382 
2383 		if (ret >= 0) {
2384 			if (rdev->desc->ops->list_voltage)
2385 				best_val = rdev->desc->ops->list_voltage(rdev,
2386 									 selector);
2387 			else
2388 				best_val = _regulator_get_voltage(rdev);
2389 		}
2390 
2391 	} else if (rdev->desc->ops->set_voltage_sel) {
2392 		if (rdev->desc->ops->map_voltage) {
2393 			ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2394 							   max_uV);
2395 		} else {
2396 			if (rdev->desc->ops->list_voltage ==
2397 			    regulator_list_voltage_linear)
2398 				ret = regulator_map_voltage_linear(rdev,
2399 								min_uV, max_uV);
2400 			else
2401 				ret = regulator_map_voltage_iterate(rdev,
2402 								min_uV, max_uV);
2403 		}
2404 
2405 		if (ret >= 0) {
2406 			best_val = rdev->desc->ops->list_voltage(rdev, ret);
2407 			if (min_uV <= best_val && max_uV >= best_val) {
2408 				selector = ret;
2409 				if (old_selector == selector)
2410 					ret = 0;
2411 				else
2412 					ret = rdev->desc->ops->set_voltage_sel(
2413 								rdev, ret);
2414 			} else {
2415 				ret = -EINVAL;
2416 			}
2417 		}
2418 	} else {
2419 		ret = -EINVAL;
2420 	}
2421 
2422 	/* Call set_voltage_time_sel if successfully obtained old_selector */
2423 	if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2424 	    old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
2425 
2426 		delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2427 						old_selector, selector);
2428 		if (delay < 0) {
2429 			rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2430 				  delay);
2431 			delay = 0;
2432 		}
2433 
2434 		/* Insert any necessary delays */
2435 		if (delay >= 1000) {
2436 			mdelay(delay / 1000);
2437 			udelay(delay % 1000);
2438 		} else if (delay) {
2439 			udelay(delay);
2440 		}
2441 	}
2442 
2443 	if (ret == 0 && best_val >= 0) {
2444 		unsigned long data = best_val;
2445 
2446 		_notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2447 				     (void *)data);
2448 	}
2449 
2450 	trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2451 
2452 	return ret;
2453 }
2454 
2455 /**
2456  * regulator_set_voltage - set regulator output voltage
2457  * @regulator: regulator source
2458  * @min_uV: Minimum required voltage in uV
2459  * @max_uV: Maximum acceptable voltage in uV
2460  *
2461  * Sets a voltage regulator to the desired output voltage. This can be set
2462  * during any regulator state. IOW, regulator can be disabled or enabled.
2463  *
2464  * If the regulator is enabled then the voltage will change to the new value
2465  * immediately otherwise if the regulator is disabled the regulator will
2466  * output at the new voltage when enabled.
2467  *
2468  * NOTE: If the regulator is shared between several devices then the lowest
2469  * request voltage that meets the system constraints will be used.
2470  * Regulator system constraints must be set for this regulator before
2471  * calling this function otherwise this call will fail.
2472  */
2473 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2474 {
2475 	struct regulator_dev *rdev = regulator->rdev;
2476 	int ret = 0;
2477 	int old_min_uV, old_max_uV;
2478 
2479 	mutex_lock(&rdev->mutex);
2480 
2481 	/* If we're setting the same range as last time the change
2482 	 * should be a noop (some cpufreq implementations use the same
2483 	 * voltage for multiple frequencies, for example).
2484 	 */
2485 	if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2486 		goto out;
2487 
2488 	/* sanity check */
2489 	if (!rdev->desc->ops->set_voltage &&
2490 	    !rdev->desc->ops->set_voltage_sel) {
2491 		ret = -EINVAL;
2492 		goto out;
2493 	}
2494 
2495 	/* constraints check */
2496 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2497 	if (ret < 0)
2498 		goto out;
2499 
2500 	/* restore original values in case of error */
2501 	old_min_uV = regulator->min_uV;
2502 	old_max_uV = regulator->max_uV;
2503 	regulator->min_uV = min_uV;
2504 	regulator->max_uV = max_uV;
2505 
2506 	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2507 	if (ret < 0)
2508 		goto out2;
2509 
2510 	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2511 	if (ret < 0)
2512 		goto out2;
2513 
2514 out:
2515 	mutex_unlock(&rdev->mutex);
2516 	return ret;
2517 out2:
2518 	regulator->min_uV = old_min_uV;
2519 	regulator->max_uV = old_max_uV;
2520 	mutex_unlock(&rdev->mutex);
2521 	return ret;
2522 }
2523 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2524 
2525 /**
2526  * regulator_set_voltage_time - get raise/fall time
2527  * @regulator: regulator source
2528  * @old_uV: starting voltage in microvolts
2529  * @new_uV: target voltage in microvolts
2530  *
2531  * Provided with the starting and ending voltage, this function attempts to
2532  * calculate the time in microseconds required to rise or fall to this new
2533  * voltage.
2534  */
2535 int regulator_set_voltage_time(struct regulator *regulator,
2536 			       int old_uV, int new_uV)
2537 {
2538 	struct regulator_dev	*rdev = regulator->rdev;
2539 	struct regulator_ops	*ops = rdev->desc->ops;
2540 	int old_sel = -1;
2541 	int new_sel = -1;
2542 	int voltage;
2543 	int i;
2544 
2545 	/* Currently requires operations to do this */
2546 	if (!ops->list_voltage || !ops->set_voltage_time_sel
2547 	    || !rdev->desc->n_voltages)
2548 		return -EINVAL;
2549 
2550 	for (i = 0; i < rdev->desc->n_voltages; i++) {
2551 		/* We only look for exact voltage matches here */
2552 		voltage = regulator_list_voltage(regulator, i);
2553 		if (voltage < 0)
2554 			return -EINVAL;
2555 		if (voltage == 0)
2556 			continue;
2557 		if (voltage == old_uV)
2558 			old_sel = i;
2559 		if (voltage == new_uV)
2560 			new_sel = i;
2561 	}
2562 
2563 	if (old_sel < 0 || new_sel < 0)
2564 		return -EINVAL;
2565 
2566 	return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2567 }
2568 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2569 
2570 /**
2571  * regulator_set_voltage_time_sel - get raise/fall time
2572  * @rdev: regulator source device
2573  * @old_selector: selector for starting voltage
2574  * @new_selector: selector for target voltage
2575  *
2576  * Provided with the starting and target voltage selectors, this function
2577  * returns time in microseconds required to rise or fall to this new voltage
2578  *
2579  * Drivers providing ramp_delay in regulation_constraints can use this as their
2580  * set_voltage_time_sel() operation.
2581  */
2582 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2583 				   unsigned int old_selector,
2584 				   unsigned int new_selector)
2585 {
2586 	unsigned int ramp_delay = 0;
2587 	int old_volt, new_volt;
2588 
2589 	if (rdev->constraints->ramp_delay)
2590 		ramp_delay = rdev->constraints->ramp_delay;
2591 	else if (rdev->desc->ramp_delay)
2592 		ramp_delay = rdev->desc->ramp_delay;
2593 
2594 	if (ramp_delay == 0) {
2595 		rdev_warn(rdev, "ramp_delay not set\n");
2596 		return 0;
2597 	}
2598 
2599 	/* sanity check */
2600 	if (!rdev->desc->ops->list_voltage)
2601 		return -EINVAL;
2602 
2603 	old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2604 	new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2605 
2606 	return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2607 }
2608 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2609 
2610 /**
2611  * regulator_sync_voltage - re-apply last regulator output voltage
2612  * @regulator: regulator source
2613  *
2614  * Re-apply the last configured voltage.  This is intended to be used
2615  * where some external control source the consumer is cooperating with
2616  * has caused the configured voltage to change.
2617  */
2618 int regulator_sync_voltage(struct regulator *regulator)
2619 {
2620 	struct regulator_dev *rdev = regulator->rdev;
2621 	int ret, min_uV, max_uV;
2622 
2623 	mutex_lock(&rdev->mutex);
2624 
2625 	if (!rdev->desc->ops->set_voltage &&
2626 	    !rdev->desc->ops->set_voltage_sel) {
2627 		ret = -EINVAL;
2628 		goto out;
2629 	}
2630 
2631 	/* This is only going to work if we've had a voltage configured. */
2632 	if (!regulator->min_uV && !regulator->max_uV) {
2633 		ret = -EINVAL;
2634 		goto out;
2635 	}
2636 
2637 	min_uV = regulator->min_uV;
2638 	max_uV = regulator->max_uV;
2639 
2640 	/* This should be a paranoia check... */
2641 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2642 	if (ret < 0)
2643 		goto out;
2644 
2645 	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2646 	if (ret < 0)
2647 		goto out;
2648 
2649 	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2650 
2651 out:
2652 	mutex_unlock(&rdev->mutex);
2653 	return ret;
2654 }
2655 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2656 
2657 static int _regulator_get_voltage(struct regulator_dev *rdev)
2658 {
2659 	int sel, ret;
2660 
2661 	if (rdev->desc->ops->get_voltage_sel) {
2662 		sel = rdev->desc->ops->get_voltage_sel(rdev);
2663 		if (sel < 0)
2664 			return sel;
2665 		ret = rdev->desc->ops->list_voltage(rdev, sel);
2666 	} else if (rdev->desc->ops->get_voltage) {
2667 		ret = rdev->desc->ops->get_voltage(rdev);
2668 	} else if (rdev->desc->ops->list_voltage) {
2669 		ret = rdev->desc->ops->list_voltage(rdev, 0);
2670 	} else {
2671 		return -EINVAL;
2672 	}
2673 
2674 	if (ret < 0)
2675 		return ret;
2676 	return ret - rdev->constraints->uV_offset;
2677 }
2678 
2679 /**
2680  * regulator_get_voltage - get regulator output voltage
2681  * @regulator: regulator source
2682  *
2683  * This returns the current regulator voltage in uV.
2684  *
2685  * NOTE: If the regulator is disabled it will return the voltage value. This
2686  * function should not be used to determine regulator state.
2687  */
2688 int regulator_get_voltage(struct regulator *regulator)
2689 {
2690 	int ret;
2691 
2692 	mutex_lock(&regulator->rdev->mutex);
2693 
2694 	ret = _regulator_get_voltage(regulator->rdev);
2695 
2696 	mutex_unlock(&regulator->rdev->mutex);
2697 
2698 	return ret;
2699 }
2700 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2701 
2702 /**
2703  * regulator_set_current_limit - set regulator output current limit
2704  * @regulator: regulator source
2705  * @min_uA: Minimuum supported current in uA
2706  * @max_uA: Maximum supported current in uA
2707  *
2708  * Sets current sink to the desired output current. This can be set during
2709  * any regulator state. IOW, regulator can be disabled or enabled.
2710  *
2711  * If the regulator is enabled then the current will change to the new value
2712  * immediately otherwise if the regulator is disabled the regulator will
2713  * output at the new current when enabled.
2714  *
2715  * NOTE: Regulator system constraints must be set for this regulator before
2716  * calling this function otherwise this call will fail.
2717  */
2718 int regulator_set_current_limit(struct regulator *regulator,
2719 			       int min_uA, int max_uA)
2720 {
2721 	struct regulator_dev *rdev = regulator->rdev;
2722 	int ret;
2723 
2724 	mutex_lock(&rdev->mutex);
2725 
2726 	/* sanity check */
2727 	if (!rdev->desc->ops->set_current_limit) {
2728 		ret = -EINVAL;
2729 		goto out;
2730 	}
2731 
2732 	/* constraints check */
2733 	ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2734 	if (ret < 0)
2735 		goto out;
2736 
2737 	ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2738 out:
2739 	mutex_unlock(&rdev->mutex);
2740 	return ret;
2741 }
2742 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2743 
2744 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2745 {
2746 	int ret;
2747 
2748 	mutex_lock(&rdev->mutex);
2749 
2750 	/* sanity check */
2751 	if (!rdev->desc->ops->get_current_limit) {
2752 		ret = -EINVAL;
2753 		goto out;
2754 	}
2755 
2756 	ret = rdev->desc->ops->get_current_limit(rdev);
2757 out:
2758 	mutex_unlock(&rdev->mutex);
2759 	return ret;
2760 }
2761 
2762 /**
2763  * regulator_get_current_limit - get regulator output current
2764  * @regulator: regulator source
2765  *
2766  * This returns the current supplied by the specified current sink in uA.
2767  *
2768  * NOTE: If the regulator is disabled it will return the current value. This
2769  * function should not be used to determine regulator state.
2770  */
2771 int regulator_get_current_limit(struct regulator *regulator)
2772 {
2773 	return _regulator_get_current_limit(regulator->rdev);
2774 }
2775 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2776 
2777 /**
2778  * regulator_set_mode - set regulator operating mode
2779  * @regulator: regulator source
2780  * @mode: operating mode - one of the REGULATOR_MODE constants
2781  *
2782  * Set regulator operating mode to increase regulator efficiency or improve
2783  * regulation performance.
2784  *
2785  * NOTE: Regulator system constraints must be set for this regulator before
2786  * calling this function otherwise this call will fail.
2787  */
2788 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2789 {
2790 	struct regulator_dev *rdev = regulator->rdev;
2791 	int ret;
2792 	int regulator_curr_mode;
2793 
2794 	mutex_lock(&rdev->mutex);
2795 
2796 	/* sanity check */
2797 	if (!rdev->desc->ops->set_mode) {
2798 		ret = -EINVAL;
2799 		goto out;
2800 	}
2801 
2802 	/* return if the same mode is requested */
2803 	if (rdev->desc->ops->get_mode) {
2804 		regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2805 		if (regulator_curr_mode == mode) {
2806 			ret = 0;
2807 			goto out;
2808 		}
2809 	}
2810 
2811 	/* constraints check */
2812 	ret = regulator_mode_constrain(rdev, &mode);
2813 	if (ret < 0)
2814 		goto out;
2815 
2816 	ret = rdev->desc->ops->set_mode(rdev, mode);
2817 out:
2818 	mutex_unlock(&rdev->mutex);
2819 	return ret;
2820 }
2821 EXPORT_SYMBOL_GPL(regulator_set_mode);
2822 
2823 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2824 {
2825 	int ret;
2826 
2827 	mutex_lock(&rdev->mutex);
2828 
2829 	/* sanity check */
2830 	if (!rdev->desc->ops->get_mode) {
2831 		ret = -EINVAL;
2832 		goto out;
2833 	}
2834 
2835 	ret = rdev->desc->ops->get_mode(rdev);
2836 out:
2837 	mutex_unlock(&rdev->mutex);
2838 	return ret;
2839 }
2840 
2841 /**
2842  * regulator_get_mode - get regulator operating mode
2843  * @regulator: regulator source
2844  *
2845  * Get the current regulator operating mode.
2846  */
2847 unsigned int regulator_get_mode(struct regulator *regulator)
2848 {
2849 	return _regulator_get_mode(regulator->rdev);
2850 }
2851 EXPORT_SYMBOL_GPL(regulator_get_mode);
2852 
2853 /**
2854  * regulator_set_optimum_mode - set regulator optimum operating mode
2855  * @regulator: regulator source
2856  * @uA_load: load current
2857  *
2858  * Notifies the regulator core of a new device load. This is then used by
2859  * DRMS (if enabled by constraints) to set the most efficient regulator
2860  * operating mode for the new regulator loading.
2861  *
2862  * Consumer devices notify their supply regulator of the maximum power
2863  * they will require (can be taken from device datasheet in the power
2864  * consumption tables) when they change operational status and hence power
2865  * state. Examples of operational state changes that can affect power
2866  * consumption are :-
2867  *
2868  *    o Device is opened / closed.
2869  *    o Device I/O is about to begin or has just finished.
2870  *    o Device is idling in between work.
2871  *
2872  * This information is also exported via sysfs to userspace.
2873  *
2874  * DRMS will sum the total requested load on the regulator and change
2875  * to the most efficient operating mode if platform constraints allow.
2876  *
2877  * Returns the new regulator mode or error.
2878  */
2879 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2880 {
2881 	struct regulator_dev *rdev = regulator->rdev;
2882 	struct regulator *consumer;
2883 	int ret, output_uV, input_uV = 0, total_uA_load = 0;
2884 	unsigned int mode;
2885 
2886 	if (rdev->supply)
2887 		input_uV = regulator_get_voltage(rdev->supply);
2888 
2889 	mutex_lock(&rdev->mutex);
2890 
2891 	/*
2892 	 * first check to see if we can set modes at all, otherwise just
2893 	 * tell the consumer everything is OK.
2894 	 */
2895 	regulator->uA_load = uA_load;
2896 	ret = regulator_check_drms(rdev);
2897 	if (ret < 0) {
2898 		ret = 0;
2899 		goto out;
2900 	}
2901 
2902 	if (!rdev->desc->ops->get_optimum_mode)
2903 		goto out;
2904 
2905 	/*
2906 	 * we can actually do this so any errors are indicators of
2907 	 * potential real failure.
2908 	 */
2909 	ret = -EINVAL;
2910 
2911 	if (!rdev->desc->ops->set_mode)
2912 		goto out;
2913 
2914 	/* get output voltage */
2915 	output_uV = _regulator_get_voltage(rdev);
2916 	if (output_uV <= 0) {
2917 		rdev_err(rdev, "invalid output voltage found\n");
2918 		goto out;
2919 	}
2920 
2921 	/* No supply? Use constraint voltage */
2922 	if (input_uV <= 0)
2923 		input_uV = rdev->constraints->input_uV;
2924 	if (input_uV <= 0) {
2925 		rdev_err(rdev, "invalid input voltage found\n");
2926 		goto out;
2927 	}
2928 
2929 	/* calc total requested load for this regulator */
2930 	list_for_each_entry(consumer, &rdev->consumer_list, list)
2931 		total_uA_load += consumer->uA_load;
2932 
2933 	mode = rdev->desc->ops->get_optimum_mode(rdev,
2934 						 input_uV, output_uV,
2935 						 total_uA_load);
2936 	ret = regulator_mode_constrain(rdev, &mode);
2937 	if (ret < 0) {
2938 		rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2939 			 total_uA_load, input_uV, output_uV);
2940 		goto out;
2941 	}
2942 
2943 	ret = rdev->desc->ops->set_mode(rdev, mode);
2944 	if (ret < 0) {
2945 		rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2946 		goto out;
2947 	}
2948 	ret = mode;
2949 out:
2950 	mutex_unlock(&rdev->mutex);
2951 	return ret;
2952 }
2953 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2954 
2955 /**
2956  * regulator_set_bypass_regmap - Default set_bypass() using regmap
2957  *
2958  * @rdev: device to operate on.
2959  * @enable: state to set.
2960  */
2961 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2962 {
2963 	unsigned int val;
2964 
2965 	if (enable)
2966 		val = rdev->desc->bypass_mask;
2967 	else
2968 		val = 0;
2969 
2970 	return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2971 				  rdev->desc->bypass_mask, val);
2972 }
2973 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2974 
2975 /**
2976  * regulator_get_bypass_regmap - Default get_bypass() using regmap
2977  *
2978  * @rdev: device to operate on.
2979  * @enable: current state.
2980  */
2981 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2982 {
2983 	unsigned int val;
2984 	int ret;
2985 
2986 	ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2987 	if (ret != 0)
2988 		return ret;
2989 
2990 	*enable = val & rdev->desc->bypass_mask;
2991 
2992 	return 0;
2993 }
2994 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2995 
2996 /**
2997  * regulator_allow_bypass - allow the regulator to go into bypass mode
2998  *
2999  * @regulator: Regulator to configure
3000  * @enable: enable or disable bypass mode
3001  *
3002  * Allow the regulator to go into bypass mode if all other consumers
3003  * for the regulator also enable bypass mode and the machine
3004  * constraints allow this.  Bypass mode means that the regulator is
3005  * simply passing the input directly to the output with no regulation.
3006  */
3007 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3008 {
3009 	struct regulator_dev *rdev = regulator->rdev;
3010 	int ret = 0;
3011 
3012 	if (!rdev->desc->ops->set_bypass)
3013 		return 0;
3014 
3015 	if (rdev->constraints &&
3016 	    !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3017 		return 0;
3018 
3019 	mutex_lock(&rdev->mutex);
3020 
3021 	if (enable && !regulator->bypass) {
3022 		rdev->bypass_count++;
3023 
3024 		if (rdev->bypass_count == rdev->open_count) {
3025 			ret = rdev->desc->ops->set_bypass(rdev, enable);
3026 			if (ret != 0)
3027 				rdev->bypass_count--;
3028 		}
3029 
3030 	} else if (!enable && regulator->bypass) {
3031 		rdev->bypass_count--;
3032 
3033 		if (rdev->bypass_count != rdev->open_count) {
3034 			ret = rdev->desc->ops->set_bypass(rdev, enable);
3035 			if (ret != 0)
3036 				rdev->bypass_count++;
3037 		}
3038 	}
3039 
3040 	if (ret == 0)
3041 		regulator->bypass = enable;
3042 
3043 	mutex_unlock(&rdev->mutex);
3044 
3045 	return ret;
3046 }
3047 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3048 
3049 /**
3050  * regulator_register_notifier - register regulator event notifier
3051  * @regulator: regulator source
3052  * @nb: notifier block
3053  *
3054  * Register notifier block to receive regulator events.
3055  */
3056 int regulator_register_notifier(struct regulator *regulator,
3057 			      struct notifier_block *nb)
3058 {
3059 	return blocking_notifier_chain_register(&regulator->rdev->notifier,
3060 						nb);
3061 }
3062 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3063 
3064 /**
3065  * regulator_unregister_notifier - unregister regulator event notifier
3066  * @regulator: regulator source
3067  * @nb: notifier block
3068  *
3069  * Unregister regulator event notifier block.
3070  */
3071 int regulator_unregister_notifier(struct regulator *regulator,
3072 				struct notifier_block *nb)
3073 {
3074 	return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3075 						  nb);
3076 }
3077 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3078 
3079 /* notify regulator consumers and downstream regulator consumers.
3080  * Note mutex must be held by caller.
3081  */
3082 static void _notifier_call_chain(struct regulator_dev *rdev,
3083 				  unsigned long event, void *data)
3084 {
3085 	/* call rdev chain first */
3086 	blocking_notifier_call_chain(&rdev->notifier, event, data);
3087 }
3088 
3089 /**
3090  * regulator_bulk_get - get multiple regulator consumers
3091  *
3092  * @dev:           Device to supply
3093  * @num_consumers: Number of consumers to register
3094  * @consumers:     Configuration of consumers; clients are stored here.
3095  *
3096  * @return 0 on success, an errno on failure.
3097  *
3098  * This helper function allows drivers to get several regulator
3099  * consumers in one operation.  If any of the regulators cannot be
3100  * acquired then any regulators that were allocated will be freed
3101  * before returning to the caller.
3102  */
3103 int regulator_bulk_get(struct device *dev, int num_consumers,
3104 		       struct regulator_bulk_data *consumers)
3105 {
3106 	int i;
3107 	int ret;
3108 
3109 	for (i = 0; i < num_consumers; i++)
3110 		consumers[i].consumer = NULL;
3111 
3112 	for (i = 0; i < num_consumers; i++) {
3113 		consumers[i].consumer = regulator_get(dev,
3114 						      consumers[i].supply);
3115 		if (IS_ERR(consumers[i].consumer)) {
3116 			ret = PTR_ERR(consumers[i].consumer);
3117 			dev_err(dev, "Failed to get supply '%s': %d\n",
3118 				consumers[i].supply, ret);
3119 			consumers[i].consumer = NULL;
3120 			goto err;
3121 		}
3122 	}
3123 
3124 	return 0;
3125 
3126 err:
3127 	while (--i >= 0)
3128 		regulator_put(consumers[i].consumer);
3129 
3130 	return ret;
3131 }
3132 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3133 
3134 /**
3135  * devm_regulator_bulk_get - managed get multiple regulator consumers
3136  *
3137  * @dev:           Device to supply
3138  * @num_consumers: Number of consumers to register
3139  * @consumers:     Configuration of consumers; clients are stored here.
3140  *
3141  * @return 0 on success, an errno on failure.
3142  *
3143  * This helper function allows drivers to get several regulator
3144  * consumers in one operation with management, the regulators will
3145  * automatically be freed when the device is unbound.  If any of the
3146  * regulators cannot be acquired then any regulators that were
3147  * allocated will be freed before returning to the caller.
3148  */
3149 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
3150 			    struct regulator_bulk_data *consumers)
3151 {
3152 	int i;
3153 	int ret;
3154 
3155 	for (i = 0; i < num_consumers; i++)
3156 		consumers[i].consumer = NULL;
3157 
3158 	for (i = 0; i < num_consumers; i++) {
3159 		consumers[i].consumer = devm_regulator_get(dev,
3160 							   consumers[i].supply);
3161 		if (IS_ERR(consumers[i].consumer)) {
3162 			ret = PTR_ERR(consumers[i].consumer);
3163 			dev_err(dev, "Failed to get supply '%s': %d\n",
3164 				consumers[i].supply, ret);
3165 			consumers[i].consumer = NULL;
3166 			goto err;
3167 		}
3168 	}
3169 
3170 	return 0;
3171 
3172 err:
3173 	for (i = 0; i < num_consumers && consumers[i].consumer; i++)
3174 		devm_regulator_put(consumers[i].consumer);
3175 
3176 	return ret;
3177 }
3178 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
3179 
3180 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3181 {
3182 	struct regulator_bulk_data *bulk = data;
3183 
3184 	bulk->ret = regulator_enable(bulk->consumer);
3185 }
3186 
3187 /**
3188  * regulator_bulk_enable - enable multiple regulator consumers
3189  *
3190  * @num_consumers: Number of consumers
3191  * @consumers:     Consumer data; clients are stored here.
3192  * @return         0 on success, an errno on failure
3193  *
3194  * This convenience API allows consumers to enable multiple regulator
3195  * clients in a single API call.  If any consumers cannot be enabled
3196  * then any others that were enabled will be disabled again prior to
3197  * return.
3198  */
3199 int regulator_bulk_enable(int num_consumers,
3200 			  struct regulator_bulk_data *consumers)
3201 {
3202 	ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3203 	int i;
3204 	int ret = 0;
3205 
3206 	for (i = 0; i < num_consumers; i++) {
3207 		if (consumers[i].consumer->always_on)
3208 			consumers[i].ret = 0;
3209 		else
3210 			async_schedule_domain(regulator_bulk_enable_async,
3211 					      &consumers[i], &async_domain);
3212 	}
3213 
3214 	async_synchronize_full_domain(&async_domain);
3215 
3216 	/* If any consumer failed we need to unwind any that succeeded */
3217 	for (i = 0; i < num_consumers; i++) {
3218 		if (consumers[i].ret != 0) {
3219 			ret = consumers[i].ret;
3220 			goto err;
3221 		}
3222 	}
3223 
3224 	return 0;
3225 
3226 err:
3227 	for (i = 0; i < num_consumers; i++) {
3228 		if (consumers[i].ret < 0)
3229 			pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3230 			       consumers[i].ret);
3231 		else
3232 			regulator_disable(consumers[i].consumer);
3233 	}
3234 
3235 	return ret;
3236 }
3237 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3238 
3239 /**
3240  * regulator_bulk_disable - disable multiple regulator consumers
3241  *
3242  * @num_consumers: Number of consumers
3243  * @consumers:     Consumer data; clients are stored here.
3244  * @return         0 on success, an errno on failure
3245  *
3246  * This convenience API allows consumers to disable multiple regulator
3247  * clients in a single API call.  If any consumers cannot be disabled
3248  * then any others that were disabled will be enabled again prior to
3249  * return.
3250  */
3251 int regulator_bulk_disable(int num_consumers,
3252 			   struct regulator_bulk_data *consumers)
3253 {
3254 	int i;
3255 	int ret, r;
3256 
3257 	for (i = num_consumers - 1; i >= 0; --i) {
3258 		ret = regulator_disable(consumers[i].consumer);
3259 		if (ret != 0)
3260 			goto err;
3261 	}
3262 
3263 	return 0;
3264 
3265 err:
3266 	pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3267 	for (++i; i < num_consumers; ++i) {
3268 		r = regulator_enable(consumers[i].consumer);
3269 		if (r != 0)
3270 			pr_err("Failed to reename %s: %d\n",
3271 			       consumers[i].supply, r);
3272 	}
3273 
3274 	return ret;
3275 }
3276 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3277 
3278 /**
3279  * regulator_bulk_force_disable - force disable multiple regulator consumers
3280  *
3281  * @num_consumers: Number of consumers
3282  * @consumers:     Consumer data; clients are stored here.
3283  * @return         0 on success, an errno on failure
3284  *
3285  * This convenience API allows consumers to forcibly disable multiple regulator
3286  * clients in a single API call.
3287  * NOTE: This should be used for situations when device damage will
3288  * likely occur if the regulators are not disabled (e.g. over temp).
3289  * Although regulator_force_disable function call for some consumers can
3290  * return error numbers, the function is called for all consumers.
3291  */
3292 int regulator_bulk_force_disable(int num_consumers,
3293 			   struct regulator_bulk_data *consumers)
3294 {
3295 	int i;
3296 	int ret;
3297 
3298 	for (i = 0; i < num_consumers; i++)
3299 		consumers[i].ret =
3300 			    regulator_force_disable(consumers[i].consumer);
3301 
3302 	for (i = 0; i < num_consumers; i++) {
3303 		if (consumers[i].ret != 0) {
3304 			ret = consumers[i].ret;
3305 			goto out;
3306 		}
3307 	}
3308 
3309 	return 0;
3310 out:
3311 	return ret;
3312 }
3313 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3314 
3315 /**
3316  * regulator_bulk_free - free multiple regulator consumers
3317  *
3318  * @num_consumers: Number of consumers
3319  * @consumers:     Consumer data; clients are stored here.
3320  *
3321  * This convenience API allows consumers to free multiple regulator
3322  * clients in a single API call.
3323  */
3324 void regulator_bulk_free(int num_consumers,
3325 			 struct regulator_bulk_data *consumers)
3326 {
3327 	int i;
3328 
3329 	for (i = 0; i < num_consumers; i++) {
3330 		regulator_put(consumers[i].consumer);
3331 		consumers[i].consumer = NULL;
3332 	}
3333 }
3334 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3335 
3336 /**
3337  * regulator_notifier_call_chain - call regulator event notifier
3338  * @rdev: regulator source
3339  * @event: notifier block
3340  * @data: callback-specific data.
3341  *
3342  * Called by regulator drivers to notify clients a regulator event has
3343  * occurred. We also notify regulator clients downstream.
3344  * Note lock must be held by caller.
3345  */
3346 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3347 				  unsigned long event, void *data)
3348 {
3349 	_notifier_call_chain(rdev, event, data);
3350 	return NOTIFY_DONE;
3351 
3352 }
3353 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3354 
3355 /**
3356  * regulator_mode_to_status - convert a regulator mode into a status
3357  *
3358  * @mode: Mode to convert
3359  *
3360  * Convert a regulator mode into a status.
3361  */
3362 int regulator_mode_to_status(unsigned int mode)
3363 {
3364 	switch (mode) {
3365 	case REGULATOR_MODE_FAST:
3366 		return REGULATOR_STATUS_FAST;
3367 	case REGULATOR_MODE_NORMAL:
3368 		return REGULATOR_STATUS_NORMAL;
3369 	case REGULATOR_MODE_IDLE:
3370 		return REGULATOR_STATUS_IDLE;
3371 	case REGULATOR_MODE_STANDBY:
3372 		return REGULATOR_STATUS_STANDBY;
3373 	default:
3374 		return REGULATOR_STATUS_UNDEFINED;
3375 	}
3376 }
3377 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3378 
3379 /*
3380  * To avoid cluttering sysfs (and memory) with useless state, only
3381  * create attributes that can be meaningfully displayed.
3382  */
3383 static int add_regulator_attributes(struct regulator_dev *rdev)
3384 {
3385 	struct device		*dev = &rdev->dev;
3386 	struct regulator_ops	*ops = rdev->desc->ops;
3387 	int			status = 0;
3388 
3389 	/* some attributes need specific methods to be displayed */
3390 	if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3391 	    (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3392 	    (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3393 		status = device_create_file(dev, &dev_attr_microvolts);
3394 		if (status < 0)
3395 			return status;
3396 	}
3397 	if (ops->get_current_limit) {
3398 		status = device_create_file(dev, &dev_attr_microamps);
3399 		if (status < 0)
3400 			return status;
3401 	}
3402 	if (ops->get_mode) {
3403 		status = device_create_file(dev, &dev_attr_opmode);
3404 		if (status < 0)
3405 			return status;
3406 	}
3407 	if (rdev->ena_pin || ops->is_enabled) {
3408 		status = device_create_file(dev, &dev_attr_state);
3409 		if (status < 0)
3410 			return status;
3411 	}
3412 	if (ops->get_status) {
3413 		status = device_create_file(dev, &dev_attr_status);
3414 		if (status < 0)
3415 			return status;
3416 	}
3417 	if (ops->get_bypass) {
3418 		status = device_create_file(dev, &dev_attr_bypass);
3419 		if (status < 0)
3420 			return status;
3421 	}
3422 
3423 	/* some attributes are type-specific */
3424 	if (rdev->desc->type == REGULATOR_CURRENT) {
3425 		status = device_create_file(dev, &dev_attr_requested_microamps);
3426 		if (status < 0)
3427 			return status;
3428 	}
3429 
3430 	/* all the other attributes exist to support constraints;
3431 	 * don't show them if there are no constraints, or if the
3432 	 * relevant supporting methods are missing.
3433 	 */
3434 	if (!rdev->constraints)
3435 		return status;
3436 
3437 	/* constraints need specific supporting methods */
3438 	if (ops->set_voltage || ops->set_voltage_sel) {
3439 		status = device_create_file(dev, &dev_attr_min_microvolts);
3440 		if (status < 0)
3441 			return status;
3442 		status = device_create_file(dev, &dev_attr_max_microvolts);
3443 		if (status < 0)
3444 			return status;
3445 	}
3446 	if (ops->set_current_limit) {
3447 		status = device_create_file(dev, &dev_attr_min_microamps);
3448 		if (status < 0)
3449 			return status;
3450 		status = device_create_file(dev, &dev_attr_max_microamps);
3451 		if (status < 0)
3452 			return status;
3453 	}
3454 
3455 	status = device_create_file(dev, &dev_attr_suspend_standby_state);
3456 	if (status < 0)
3457 		return status;
3458 	status = device_create_file(dev, &dev_attr_suspend_mem_state);
3459 	if (status < 0)
3460 		return status;
3461 	status = device_create_file(dev, &dev_attr_suspend_disk_state);
3462 	if (status < 0)
3463 		return status;
3464 
3465 	if (ops->set_suspend_voltage) {
3466 		status = device_create_file(dev,
3467 				&dev_attr_suspend_standby_microvolts);
3468 		if (status < 0)
3469 			return status;
3470 		status = device_create_file(dev,
3471 				&dev_attr_suspend_mem_microvolts);
3472 		if (status < 0)
3473 			return status;
3474 		status = device_create_file(dev,
3475 				&dev_attr_suspend_disk_microvolts);
3476 		if (status < 0)
3477 			return status;
3478 	}
3479 
3480 	if (ops->set_suspend_mode) {
3481 		status = device_create_file(dev,
3482 				&dev_attr_suspend_standby_mode);
3483 		if (status < 0)
3484 			return status;
3485 		status = device_create_file(dev,
3486 				&dev_attr_suspend_mem_mode);
3487 		if (status < 0)
3488 			return status;
3489 		status = device_create_file(dev,
3490 				&dev_attr_suspend_disk_mode);
3491 		if (status < 0)
3492 			return status;
3493 	}
3494 
3495 	return status;
3496 }
3497 
3498 static void rdev_init_debugfs(struct regulator_dev *rdev)
3499 {
3500 	rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3501 	if (!rdev->debugfs) {
3502 		rdev_warn(rdev, "Failed to create debugfs directory\n");
3503 		return;
3504 	}
3505 
3506 	debugfs_create_u32("use_count", 0444, rdev->debugfs,
3507 			   &rdev->use_count);
3508 	debugfs_create_u32("open_count", 0444, rdev->debugfs,
3509 			   &rdev->open_count);
3510 	debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3511 			   &rdev->bypass_count);
3512 }
3513 
3514 /**
3515  * regulator_register - register regulator
3516  * @regulator_desc: regulator to register
3517  * @config: runtime configuration for regulator
3518  *
3519  * Called by regulator drivers to register a regulator.
3520  * Returns a valid pointer to struct regulator_dev on success
3521  * or an ERR_PTR() on error.
3522  */
3523 struct regulator_dev *
3524 regulator_register(const struct regulator_desc *regulator_desc,
3525 		   const struct regulator_config *config)
3526 {
3527 	const struct regulation_constraints *constraints = NULL;
3528 	const struct regulator_init_data *init_data;
3529 	static atomic_t regulator_no = ATOMIC_INIT(0);
3530 	struct regulator_dev *rdev;
3531 	struct device *dev;
3532 	int ret, i;
3533 	const char *supply = NULL;
3534 
3535 	if (regulator_desc == NULL || config == NULL)
3536 		return ERR_PTR(-EINVAL);
3537 
3538 	dev = config->dev;
3539 	WARN_ON(!dev);
3540 
3541 	if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3542 		return ERR_PTR(-EINVAL);
3543 
3544 	if (regulator_desc->type != REGULATOR_VOLTAGE &&
3545 	    regulator_desc->type != REGULATOR_CURRENT)
3546 		return ERR_PTR(-EINVAL);
3547 
3548 	/* Only one of each should be implemented */
3549 	WARN_ON(regulator_desc->ops->get_voltage &&
3550 		regulator_desc->ops->get_voltage_sel);
3551 	WARN_ON(regulator_desc->ops->set_voltage &&
3552 		regulator_desc->ops->set_voltage_sel);
3553 
3554 	/* If we're using selectors we must implement list_voltage. */
3555 	if (regulator_desc->ops->get_voltage_sel &&
3556 	    !regulator_desc->ops->list_voltage) {
3557 		return ERR_PTR(-EINVAL);
3558 	}
3559 	if (regulator_desc->ops->set_voltage_sel &&
3560 	    !regulator_desc->ops->list_voltage) {
3561 		return ERR_PTR(-EINVAL);
3562 	}
3563 
3564 	init_data = config->init_data;
3565 
3566 	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3567 	if (rdev == NULL)
3568 		return ERR_PTR(-ENOMEM);
3569 
3570 	mutex_lock(&regulator_list_mutex);
3571 
3572 	mutex_init(&rdev->mutex);
3573 	rdev->reg_data = config->driver_data;
3574 	rdev->owner = regulator_desc->owner;
3575 	rdev->desc = regulator_desc;
3576 	if (config->regmap)
3577 		rdev->regmap = config->regmap;
3578 	else if (dev_get_regmap(dev, NULL))
3579 		rdev->regmap = dev_get_regmap(dev, NULL);
3580 	else if (dev->parent)
3581 		rdev->regmap = dev_get_regmap(dev->parent, NULL);
3582 	INIT_LIST_HEAD(&rdev->consumer_list);
3583 	INIT_LIST_HEAD(&rdev->list);
3584 	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3585 	INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3586 
3587 	/* preform any regulator specific init */
3588 	if (init_data && init_data->regulator_init) {
3589 		ret = init_data->regulator_init(rdev->reg_data);
3590 		if (ret < 0)
3591 			goto clean;
3592 	}
3593 
3594 	/* register with sysfs */
3595 	rdev->dev.class = &regulator_class;
3596 	rdev->dev.of_node = config->of_node;
3597 	rdev->dev.parent = dev;
3598 	dev_set_name(&rdev->dev, "regulator.%d",
3599 		     atomic_inc_return(&regulator_no) - 1);
3600 	ret = device_register(&rdev->dev);
3601 	if (ret != 0) {
3602 		put_device(&rdev->dev);
3603 		goto clean;
3604 	}
3605 
3606 	dev_set_drvdata(&rdev->dev, rdev);
3607 
3608 	if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3609 		ret = regulator_ena_gpio_request(rdev, config);
3610 		if (ret != 0) {
3611 			rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3612 				 config->ena_gpio, ret);
3613 			goto wash;
3614 		}
3615 
3616 		if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3617 			rdev->ena_gpio_state = 1;
3618 
3619 		if (config->ena_gpio_invert)
3620 			rdev->ena_gpio_state = !rdev->ena_gpio_state;
3621 	}
3622 
3623 	/* set regulator constraints */
3624 	if (init_data)
3625 		constraints = &init_data->constraints;
3626 
3627 	ret = set_machine_constraints(rdev, constraints);
3628 	if (ret < 0)
3629 		goto scrub;
3630 
3631 	/* add attributes supported by this regulator */
3632 	ret = add_regulator_attributes(rdev);
3633 	if (ret < 0)
3634 		goto scrub;
3635 
3636 	if (init_data && init_data->supply_regulator)
3637 		supply = init_data->supply_regulator;
3638 	else if (regulator_desc->supply_name)
3639 		supply = regulator_desc->supply_name;
3640 
3641 	if (supply) {
3642 		struct regulator_dev *r;
3643 
3644 		r = regulator_dev_lookup(dev, supply, &ret);
3645 
3646 		if (ret == -ENODEV) {
3647 			/*
3648 			 * No supply was specified for this regulator and
3649 			 * there will never be one.
3650 			 */
3651 			ret = 0;
3652 			goto add_dev;
3653 		} else if (!r) {
3654 			dev_err(dev, "Failed to find supply %s\n", supply);
3655 			ret = -EPROBE_DEFER;
3656 			goto scrub;
3657 		}
3658 
3659 		ret = set_supply(rdev, r);
3660 		if (ret < 0)
3661 			goto scrub;
3662 
3663 		/* Enable supply if rail is enabled */
3664 		if (_regulator_is_enabled(rdev)) {
3665 			ret = regulator_enable(rdev->supply);
3666 			if (ret < 0)
3667 				goto scrub;
3668 		}
3669 	}
3670 
3671 add_dev:
3672 	/* add consumers devices */
3673 	if (init_data) {
3674 		for (i = 0; i < init_data->num_consumer_supplies; i++) {
3675 			ret = set_consumer_device_supply(rdev,
3676 				init_data->consumer_supplies[i].dev_name,
3677 				init_data->consumer_supplies[i].supply);
3678 			if (ret < 0) {
3679 				dev_err(dev, "Failed to set supply %s\n",
3680 					init_data->consumer_supplies[i].supply);
3681 				goto unset_supplies;
3682 			}
3683 		}
3684 	}
3685 
3686 	list_add(&rdev->list, &regulator_list);
3687 
3688 	rdev_init_debugfs(rdev);
3689 out:
3690 	mutex_unlock(&regulator_list_mutex);
3691 	return rdev;
3692 
3693 unset_supplies:
3694 	unset_regulator_supplies(rdev);
3695 
3696 scrub:
3697 	if (rdev->supply)
3698 		_regulator_put(rdev->supply);
3699 	regulator_ena_gpio_free(rdev);
3700 	kfree(rdev->constraints);
3701 wash:
3702 	device_unregister(&rdev->dev);
3703 	/* device core frees rdev */
3704 	rdev = ERR_PTR(ret);
3705 	goto out;
3706 
3707 clean:
3708 	kfree(rdev);
3709 	rdev = ERR_PTR(ret);
3710 	goto out;
3711 }
3712 EXPORT_SYMBOL_GPL(regulator_register);
3713 
3714 /**
3715  * regulator_unregister - unregister regulator
3716  * @rdev: regulator to unregister
3717  *
3718  * Called by regulator drivers to unregister a regulator.
3719  */
3720 void regulator_unregister(struct regulator_dev *rdev)
3721 {
3722 	if (rdev == NULL)
3723 		return;
3724 
3725 	if (rdev->supply)
3726 		regulator_put(rdev->supply);
3727 	mutex_lock(&regulator_list_mutex);
3728 	debugfs_remove_recursive(rdev->debugfs);
3729 	flush_work(&rdev->disable_work.work);
3730 	WARN_ON(rdev->open_count);
3731 	unset_regulator_supplies(rdev);
3732 	list_del(&rdev->list);
3733 	kfree(rdev->constraints);
3734 	regulator_ena_gpio_free(rdev);
3735 	device_unregister(&rdev->dev);
3736 	mutex_unlock(&regulator_list_mutex);
3737 }
3738 EXPORT_SYMBOL_GPL(regulator_unregister);
3739 
3740 /**
3741  * regulator_suspend_prepare - prepare regulators for system wide suspend
3742  * @state: system suspend state
3743  *
3744  * Configure each regulator with it's suspend operating parameters for state.
3745  * This will usually be called by machine suspend code prior to supending.
3746  */
3747 int regulator_suspend_prepare(suspend_state_t state)
3748 {
3749 	struct regulator_dev *rdev;
3750 	int ret = 0;
3751 
3752 	/* ON is handled by regulator active state */
3753 	if (state == PM_SUSPEND_ON)
3754 		return -EINVAL;
3755 
3756 	mutex_lock(&regulator_list_mutex);
3757 	list_for_each_entry(rdev, &regulator_list, list) {
3758 
3759 		mutex_lock(&rdev->mutex);
3760 		ret = suspend_prepare(rdev, state);
3761 		mutex_unlock(&rdev->mutex);
3762 
3763 		if (ret < 0) {
3764 			rdev_err(rdev, "failed to prepare\n");
3765 			goto out;
3766 		}
3767 	}
3768 out:
3769 	mutex_unlock(&regulator_list_mutex);
3770 	return ret;
3771 }
3772 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3773 
3774 /**
3775  * regulator_suspend_finish - resume regulators from system wide suspend
3776  *
3777  * Turn on regulators that might be turned off by regulator_suspend_prepare
3778  * and that should be turned on according to the regulators properties.
3779  */
3780 int regulator_suspend_finish(void)
3781 {
3782 	struct regulator_dev *rdev;
3783 	int ret = 0, error;
3784 
3785 	mutex_lock(&regulator_list_mutex);
3786 	list_for_each_entry(rdev, &regulator_list, list) {
3787 		struct regulator_ops *ops = rdev->desc->ops;
3788 
3789 		mutex_lock(&rdev->mutex);
3790 		if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
3791 				ops->enable) {
3792 			error = ops->enable(rdev);
3793 			if (error)
3794 				ret = error;
3795 		} else {
3796 			if (!has_full_constraints)
3797 				goto unlock;
3798 			if (!ops->disable)
3799 				goto unlock;
3800 			if (!_regulator_is_enabled(rdev))
3801 				goto unlock;
3802 
3803 			error = ops->disable(rdev);
3804 			if (error)
3805 				ret = error;
3806 		}
3807 unlock:
3808 		mutex_unlock(&rdev->mutex);
3809 	}
3810 	mutex_unlock(&regulator_list_mutex);
3811 	return ret;
3812 }
3813 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3814 
3815 /**
3816  * regulator_has_full_constraints - the system has fully specified constraints
3817  *
3818  * Calling this function will cause the regulator API to disable all
3819  * regulators which have a zero use count and don't have an always_on
3820  * constraint in a late_initcall.
3821  *
3822  * The intention is that this will become the default behaviour in a
3823  * future kernel release so users are encouraged to use this facility
3824  * now.
3825  */
3826 void regulator_has_full_constraints(void)
3827 {
3828 	has_full_constraints = 1;
3829 }
3830 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3831 
3832 /**
3833  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3834  *
3835  * Calling this function will cause the regulator API to provide a
3836  * dummy regulator to consumers if no physical regulator is found,
3837  * allowing most consumers to proceed as though a regulator were
3838  * configured.  This allows systems such as those with software
3839  * controllable regulators for the CPU core only to be brought up more
3840  * readily.
3841  */
3842 void regulator_use_dummy_regulator(void)
3843 {
3844 	board_wants_dummy_regulator = true;
3845 }
3846 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3847 
3848 /**
3849  * rdev_get_drvdata - get rdev regulator driver data
3850  * @rdev: regulator
3851  *
3852  * Get rdev regulator driver private data. This call can be used in the
3853  * regulator driver context.
3854  */
3855 void *rdev_get_drvdata(struct regulator_dev *rdev)
3856 {
3857 	return rdev->reg_data;
3858 }
3859 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3860 
3861 /**
3862  * regulator_get_drvdata - get regulator driver data
3863  * @regulator: regulator
3864  *
3865  * Get regulator driver private data. This call can be used in the consumer
3866  * driver context when non API regulator specific functions need to be called.
3867  */
3868 void *regulator_get_drvdata(struct regulator *regulator)
3869 {
3870 	return regulator->rdev->reg_data;
3871 }
3872 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3873 
3874 /**
3875  * regulator_set_drvdata - set regulator driver data
3876  * @regulator: regulator
3877  * @data: data
3878  */
3879 void regulator_set_drvdata(struct regulator *regulator, void *data)
3880 {
3881 	regulator->rdev->reg_data = data;
3882 }
3883 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3884 
3885 /**
3886  * regulator_get_id - get regulator ID
3887  * @rdev: regulator
3888  */
3889 int rdev_get_id(struct regulator_dev *rdev)
3890 {
3891 	return rdev->desc->id;
3892 }
3893 EXPORT_SYMBOL_GPL(rdev_get_id);
3894 
3895 struct device *rdev_get_dev(struct regulator_dev *rdev)
3896 {
3897 	return &rdev->dev;
3898 }
3899 EXPORT_SYMBOL_GPL(rdev_get_dev);
3900 
3901 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3902 {
3903 	return reg_init_data->driver_data;
3904 }
3905 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3906 
3907 #ifdef CONFIG_DEBUG_FS
3908 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3909 				    size_t count, loff_t *ppos)
3910 {
3911 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3912 	ssize_t len, ret = 0;
3913 	struct regulator_map *map;
3914 
3915 	if (!buf)
3916 		return -ENOMEM;
3917 
3918 	list_for_each_entry(map, &regulator_map_list, list) {
3919 		len = snprintf(buf + ret, PAGE_SIZE - ret,
3920 			       "%s -> %s.%s\n",
3921 			       rdev_get_name(map->regulator), map->dev_name,
3922 			       map->supply);
3923 		if (len >= 0)
3924 			ret += len;
3925 		if (ret > PAGE_SIZE) {
3926 			ret = PAGE_SIZE;
3927 			break;
3928 		}
3929 	}
3930 
3931 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3932 
3933 	kfree(buf);
3934 
3935 	return ret;
3936 }
3937 #endif
3938 
3939 static const struct file_operations supply_map_fops = {
3940 #ifdef CONFIG_DEBUG_FS
3941 	.read = supply_map_read_file,
3942 	.llseek = default_llseek,
3943 #endif
3944 };
3945 
3946 static int __init regulator_init(void)
3947 {
3948 	int ret;
3949 
3950 	ret = class_register(&regulator_class);
3951 
3952 	debugfs_root = debugfs_create_dir("regulator", NULL);
3953 	if (!debugfs_root)
3954 		pr_warn("regulator: Failed to create debugfs directory\n");
3955 
3956 	debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3957 			    &supply_map_fops);
3958 
3959 	regulator_dummy_init();
3960 
3961 	return ret;
3962 }
3963 
3964 /* init early to allow our consumers to complete system booting */
3965 core_initcall(regulator_init);
3966 
3967 static int __init regulator_init_complete(void)
3968 {
3969 	struct regulator_dev *rdev;
3970 	struct regulator_ops *ops;
3971 	struct regulation_constraints *c;
3972 	int enabled, ret;
3973 
3974 	/*
3975 	 * Since DT doesn't provide an idiomatic mechanism for
3976 	 * enabling full constraints and since it's much more natural
3977 	 * with DT to provide them just assume that a DT enabled
3978 	 * system has full constraints.
3979 	 */
3980 	if (of_have_populated_dt())
3981 		has_full_constraints = true;
3982 
3983 	mutex_lock(&regulator_list_mutex);
3984 
3985 	/* If we have a full configuration then disable any regulators
3986 	 * which are not in use or always_on.  This will become the
3987 	 * default behaviour in the future.
3988 	 */
3989 	list_for_each_entry(rdev, &regulator_list, list) {
3990 		ops = rdev->desc->ops;
3991 		c = rdev->constraints;
3992 
3993 		if (!ops->disable || (c && c->always_on))
3994 			continue;
3995 
3996 		mutex_lock(&rdev->mutex);
3997 
3998 		if (rdev->use_count)
3999 			goto unlock;
4000 
4001 		/* If we can't read the status assume it's on. */
4002 		if (ops->is_enabled)
4003 			enabled = ops->is_enabled(rdev);
4004 		else
4005 			enabled = 1;
4006 
4007 		if (!enabled)
4008 			goto unlock;
4009 
4010 		if (has_full_constraints) {
4011 			/* We log since this may kill the system if it
4012 			 * goes wrong. */
4013 			rdev_info(rdev, "disabling\n");
4014 			ret = ops->disable(rdev);
4015 			if (ret != 0) {
4016 				rdev_err(rdev, "couldn't disable: %d\n", ret);
4017 			}
4018 		} else {
4019 			/* The intention is that in future we will
4020 			 * assume that full constraints are provided
4021 			 * so warn even if we aren't going to do
4022 			 * anything here.
4023 			 */
4024 			rdev_warn(rdev, "incomplete constraints, leaving on\n");
4025 		}
4026 
4027 unlock:
4028 		mutex_unlock(&rdev->mutex);
4029 	}
4030 
4031 	mutex_unlock(&regulator_list_mutex);
4032 
4033 	return 0;
4034 }
4035 late_initcall(regulator_init_complete);
4036