xref: /openbmc/linux/drivers/regulator/core.c (revision 16fbcc3b)
1414c70cbSLiam Girdwood /*
2414c70cbSLiam Girdwood  * core.c  --  Voltage/Current Regulator framework.
3414c70cbSLiam Girdwood  *
4414c70cbSLiam Girdwood  * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5a5766f11SLiam Girdwood  * Copyright 2008 SlimLogic Ltd.
6414c70cbSLiam Girdwood  *
7a5766f11SLiam Girdwood  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8414c70cbSLiam Girdwood  *
9414c70cbSLiam Girdwood  *  This program is free software; you can redistribute  it and/or modify it
10414c70cbSLiam Girdwood  *  under  the terms of  the GNU General  Public License as published by the
11414c70cbSLiam Girdwood  *  Free Software Foundation;  either version 2 of the  License, or (at your
12414c70cbSLiam Girdwood  *  option) any later version.
13414c70cbSLiam Girdwood  *
14414c70cbSLiam Girdwood  */
15414c70cbSLiam Girdwood 
16414c70cbSLiam Girdwood #include <linux/kernel.h>
17414c70cbSLiam Girdwood #include <linux/init.h>
181130e5b3SMark Brown #include <linux/debugfs.h>
19414c70cbSLiam Girdwood #include <linux/device.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21f21e0e81SMark Brown #include <linux/async.h>
22414c70cbSLiam Girdwood #include <linux/err.h>
23414c70cbSLiam Girdwood #include <linux/mutex.h>
24414c70cbSLiam Girdwood #include <linux/suspend.h>
2531aae2beSMark Brown #include <linux/delay.h>
2669511a45SRajendra Nayak #include <linux/of.h>
2769511a45SRajendra Nayak #include <linux/regulator/of_regulator.h>
28414c70cbSLiam Girdwood #include <linux/regulator/consumer.h>
29414c70cbSLiam Girdwood #include <linux/regulator/driver.h>
30414c70cbSLiam Girdwood #include <linux/regulator/machine.h>
3165602c32SPaul Gortmaker #include <linux/module.h>
32414c70cbSLiam Girdwood 
3302fa3ec0SMark Brown #define CREATE_TRACE_POINTS
3402fa3ec0SMark Brown #include <trace/events/regulator.h>
3502fa3ec0SMark Brown 
3634abbd68SMark Brown #include "dummy.h"
3734abbd68SMark Brown 
387d51a0dbSMark Brown #define rdev_crit(rdev, fmt, ...)					\
397d51a0dbSMark Brown 	pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
405da84fd9SJoe Perches #define rdev_err(rdev, fmt, ...)					\
415da84fd9SJoe Perches 	pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
425da84fd9SJoe Perches #define rdev_warn(rdev, fmt, ...)					\
435da84fd9SJoe Perches 	pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
445da84fd9SJoe Perches #define rdev_info(rdev, fmt, ...)					\
455da84fd9SJoe Perches 	pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
465da84fd9SJoe Perches #define rdev_dbg(rdev, fmt, ...)					\
475da84fd9SJoe Perches 	pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
485da84fd9SJoe Perches 
49414c70cbSLiam Girdwood static DEFINE_MUTEX(regulator_list_mutex);
50414c70cbSLiam Girdwood static LIST_HEAD(regulator_list);
51414c70cbSLiam Girdwood static LIST_HEAD(regulator_map_list);
5221cf891aSMark Brown static bool has_full_constraints;
53688fe99aSMark Brown static bool board_wants_dummy_regulator;
54414c70cbSLiam Girdwood 
551130e5b3SMark Brown static struct dentry *debugfs_root;
561130e5b3SMark Brown 
578dc5390dSMark Brown /*
58414c70cbSLiam Girdwood  * struct regulator_map
59414c70cbSLiam Girdwood  *
60414c70cbSLiam Girdwood  * Used to provide symbolic supply names to devices.
61414c70cbSLiam Girdwood  */
62414c70cbSLiam Girdwood struct regulator_map {
63414c70cbSLiam Girdwood 	struct list_head list;
6440f9244fSMark Brown 	const char *dev_name;   /* The dev_name() for the consumer */
65414c70cbSLiam Girdwood 	const char *supply;
66a5766f11SLiam Girdwood 	struct regulator_dev *regulator;
67414c70cbSLiam Girdwood };
68414c70cbSLiam Girdwood 
69414c70cbSLiam Girdwood /*
70414c70cbSLiam Girdwood  * struct regulator
71414c70cbSLiam Girdwood  *
72414c70cbSLiam Girdwood  * One for each consumer device.
73414c70cbSLiam Girdwood  */
74414c70cbSLiam Girdwood struct regulator {
75414c70cbSLiam Girdwood 	struct device *dev;
76414c70cbSLiam Girdwood 	struct list_head list;
77414c70cbSLiam Girdwood 	int uA_load;
78414c70cbSLiam Girdwood 	int min_uV;
79414c70cbSLiam Girdwood 	int max_uV;
80414c70cbSLiam Girdwood 	char *supply_name;
81414c70cbSLiam Girdwood 	struct device_attribute dev_attr;
82414c70cbSLiam Girdwood 	struct regulator_dev *rdev;
835de70519SMark Brown 	struct dentry *debugfs;
84414c70cbSLiam Girdwood };
85414c70cbSLiam Girdwood 
86414c70cbSLiam Girdwood static int _regulator_is_enabled(struct regulator_dev *rdev);
873801b86aSMark Brown static int _regulator_disable(struct regulator_dev *rdev);
88414c70cbSLiam Girdwood static int _regulator_get_voltage(struct regulator_dev *rdev);
89414c70cbSLiam Girdwood static int _regulator_get_current_limit(struct regulator_dev *rdev);
90414c70cbSLiam Girdwood static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
91414c70cbSLiam Girdwood static void _notifier_call_chain(struct regulator_dev *rdev,
92414c70cbSLiam Girdwood 				  unsigned long event, void *data);
9375790251SMark Brown static int _regulator_do_set_voltage(struct regulator_dev *rdev,
9475790251SMark Brown 				     int min_uV, int max_uV);
953801b86aSMark Brown static struct regulator *create_regulator(struct regulator_dev *rdev,
963801b86aSMark Brown 					  struct device *dev,
973801b86aSMark Brown 					  const char *supply_name);
98414c70cbSLiam Girdwood 
991083c393SMark Brown static const char *rdev_get_name(struct regulator_dev *rdev)
1001083c393SMark Brown {
1011083c393SMark Brown 	if (rdev->constraints && rdev->constraints->name)
1021083c393SMark Brown 		return rdev->constraints->name;
1031083c393SMark Brown 	else if (rdev->desc->name)
1041083c393SMark Brown 		return rdev->desc->name;
1051083c393SMark Brown 	else
1061083c393SMark Brown 		return "";
1071083c393SMark Brown }
1081083c393SMark Brown 
109414c70cbSLiam Girdwood /* gets the regulator for a given consumer device */
110414c70cbSLiam Girdwood static struct regulator *get_device_regulator(struct device *dev)
111414c70cbSLiam Girdwood {
112414c70cbSLiam Girdwood 	struct regulator *regulator = NULL;
113414c70cbSLiam Girdwood 	struct regulator_dev *rdev;
114414c70cbSLiam Girdwood 
115414c70cbSLiam Girdwood 	mutex_lock(&regulator_list_mutex);
116414c70cbSLiam Girdwood 	list_for_each_entry(rdev, &regulator_list, list) {
117414c70cbSLiam Girdwood 		mutex_lock(&rdev->mutex);
118414c70cbSLiam Girdwood 		list_for_each_entry(regulator, &rdev->consumer_list, list) {
119414c70cbSLiam Girdwood 			if (regulator->dev == dev) {
120414c70cbSLiam Girdwood 				mutex_unlock(&rdev->mutex);
121414c70cbSLiam Girdwood 				mutex_unlock(&regulator_list_mutex);
122414c70cbSLiam Girdwood 				return regulator;
123414c70cbSLiam Girdwood 			}
124414c70cbSLiam Girdwood 		}
125414c70cbSLiam Girdwood 		mutex_unlock(&rdev->mutex);
126414c70cbSLiam Girdwood 	}
127414c70cbSLiam Girdwood 	mutex_unlock(&regulator_list_mutex);
128414c70cbSLiam Girdwood 	return NULL;
129414c70cbSLiam Girdwood }
130414c70cbSLiam Girdwood 
13169511a45SRajendra Nayak /**
13269511a45SRajendra Nayak  * of_get_regulator - get a regulator device node based on supply name
13369511a45SRajendra Nayak  * @dev: Device pointer for the consumer (of regulator) device
13469511a45SRajendra Nayak  * @supply: regulator supply name
13569511a45SRajendra Nayak  *
13669511a45SRajendra Nayak  * Extract the regulator device node corresponding to the supply name.
13769511a45SRajendra Nayak  * retruns the device node corresponding to the regulator if found, else
13869511a45SRajendra Nayak  * returns NULL.
13969511a45SRajendra Nayak  */
14069511a45SRajendra Nayak static struct device_node *of_get_regulator(struct device *dev, const char *supply)
14169511a45SRajendra Nayak {
14269511a45SRajendra Nayak 	struct device_node *regnode = NULL;
14369511a45SRajendra Nayak 	char prop_name[32]; /* 32 is max size of property name */
14469511a45SRajendra Nayak 
14569511a45SRajendra Nayak 	dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
14669511a45SRajendra Nayak 
14769511a45SRajendra Nayak 	snprintf(prop_name, 32, "%s-supply", supply);
14869511a45SRajendra Nayak 	regnode = of_parse_phandle(dev->of_node, prop_name, 0);
14969511a45SRajendra Nayak 
15069511a45SRajendra Nayak 	if (!regnode) {
15116fbcc3bSRajendra Nayak 		dev_dbg(dev, "Looking up %s property in node %s failed",
15269511a45SRajendra Nayak 				prop_name, dev->of_node->full_name);
15369511a45SRajendra Nayak 		return NULL;
15469511a45SRajendra Nayak 	}
15569511a45SRajendra Nayak 	return regnode;
15669511a45SRajendra Nayak }
15769511a45SRajendra Nayak 
158414c70cbSLiam Girdwood /* Platform voltage constraint check */
159414c70cbSLiam Girdwood static int regulator_check_voltage(struct regulator_dev *rdev,
160414c70cbSLiam Girdwood 				   int *min_uV, int *max_uV)
161414c70cbSLiam Girdwood {
162414c70cbSLiam Girdwood 	BUG_ON(*min_uV > *max_uV);
163414c70cbSLiam Girdwood 
164414c70cbSLiam Girdwood 	if (!rdev->constraints) {
1655da84fd9SJoe Perches 		rdev_err(rdev, "no constraints\n");
166414c70cbSLiam Girdwood 		return -ENODEV;
167414c70cbSLiam Girdwood 	}
168414c70cbSLiam Girdwood 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1695da84fd9SJoe Perches 		rdev_err(rdev, "operation not allowed\n");
170414c70cbSLiam Girdwood 		return -EPERM;
171414c70cbSLiam Girdwood 	}
172414c70cbSLiam Girdwood 
173414c70cbSLiam Girdwood 	if (*max_uV > rdev->constraints->max_uV)
174414c70cbSLiam Girdwood 		*max_uV = rdev->constraints->max_uV;
175414c70cbSLiam Girdwood 	if (*min_uV < rdev->constraints->min_uV)
176414c70cbSLiam Girdwood 		*min_uV = rdev->constraints->min_uV;
177414c70cbSLiam Girdwood 
17889f425edSMark Brown 	if (*min_uV > *max_uV) {
17989f425edSMark Brown 		rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
18054abd335SMark Brown 			 *min_uV, *max_uV);
181414c70cbSLiam Girdwood 		return -EINVAL;
18289f425edSMark Brown 	}
183414c70cbSLiam Girdwood 
184414c70cbSLiam Girdwood 	return 0;
185414c70cbSLiam Girdwood }
186414c70cbSLiam Girdwood 
18705fda3b1SThomas Petazzoni /* Make sure we select a voltage that suits the needs of all
18805fda3b1SThomas Petazzoni  * regulator consumers
18905fda3b1SThomas Petazzoni  */
19005fda3b1SThomas Petazzoni static int regulator_check_consumers(struct regulator_dev *rdev,
19105fda3b1SThomas Petazzoni 				     int *min_uV, int *max_uV)
19205fda3b1SThomas Petazzoni {
19305fda3b1SThomas Petazzoni 	struct regulator *regulator;
19405fda3b1SThomas Petazzoni 
19505fda3b1SThomas Petazzoni 	list_for_each_entry(regulator, &rdev->consumer_list, list) {
1964aa922c0SMark Brown 		/*
1974aa922c0SMark Brown 		 * Assume consumers that didn't say anything are OK
1984aa922c0SMark Brown 		 * with anything in the constraint range.
1994aa922c0SMark Brown 		 */
2004aa922c0SMark Brown 		if (!regulator->min_uV && !regulator->max_uV)
2014aa922c0SMark Brown 			continue;
2024aa922c0SMark Brown 
20305fda3b1SThomas Petazzoni 		if (*max_uV > regulator->max_uV)
20405fda3b1SThomas Petazzoni 			*max_uV = regulator->max_uV;
20505fda3b1SThomas Petazzoni 		if (*min_uV < regulator->min_uV)
20605fda3b1SThomas Petazzoni 			*min_uV = regulator->min_uV;
20705fda3b1SThomas Petazzoni 	}
20805fda3b1SThomas Petazzoni 
20905fda3b1SThomas Petazzoni 	if (*min_uV > *max_uV)
21005fda3b1SThomas Petazzoni 		return -EINVAL;
21105fda3b1SThomas Petazzoni 
21205fda3b1SThomas Petazzoni 	return 0;
21305fda3b1SThomas Petazzoni }
21405fda3b1SThomas Petazzoni 
215414c70cbSLiam Girdwood /* current constraint check */
216414c70cbSLiam Girdwood static int regulator_check_current_limit(struct regulator_dev *rdev,
217414c70cbSLiam Girdwood 					int *min_uA, int *max_uA)
218414c70cbSLiam Girdwood {
219414c70cbSLiam Girdwood 	BUG_ON(*min_uA > *max_uA);
220414c70cbSLiam Girdwood 
221414c70cbSLiam Girdwood 	if (!rdev->constraints) {
2225da84fd9SJoe Perches 		rdev_err(rdev, "no constraints\n");
223414c70cbSLiam Girdwood 		return -ENODEV;
224414c70cbSLiam Girdwood 	}
225414c70cbSLiam Girdwood 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
2265da84fd9SJoe Perches 		rdev_err(rdev, "operation not allowed\n");
227414c70cbSLiam Girdwood 		return -EPERM;
228414c70cbSLiam Girdwood 	}
229414c70cbSLiam Girdwood 
230414c70cbSLiam Girdwood 	if (*max_uA > rdev->constraints->max_uA)
231414c70cbSLiam Girdwood 		*max_uA = rdev->constraints->max_uA;
232414c70cbSLiam Girdwood 	if (*min_uA < rdev->constraints->min_uA)
233414c70cbSLiam Girdwood 		*min_uA = rdev->constraints->min_uA;
234414c70cbSLiam Girdwood 
23589f425edSMark Brown 	if (*min_uA > *max_uA) {
23689f425edSMark Brown 		rdev_err(rdev, "unsupportable current range: %d-%duA\n",
23754abd335SMark Brown 			 *min_uA, *max_uA);
238414c70cbSLiam Girdwood 		return -EINVAL;
23989f425edSMark Brown 	}
240414c70cbSLiam Girdwood 
241414c70cbSLiam Girdwood 	return 0;
242414c70cbSLiam Girdwood }
243414c70cbSLiam Girdwood 
244414c70cbSLiam Girdwood /* operating mode constraint check */
2452c608234SMark Brown static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
246414c70cbSLiam Girdwood {
2472c608234SMark Brown 	switch (*mode) {
248e573520bSDavid Brownell 	case REGULATOR_MODE_FAST:
249e573520bSDavid Brownell 	case REGULATOR_MODE_NORMAL:
250e573520bSDavid Brownell 	case REGULATOR_MODE_IDLE:
251e573520bSDavid Brownell 	case REGULATOR_MODE_STANDBY:
252e573520bSDavid Brownell 		break;
253e573520bSDavid Brownell 	default:
25489f425edSMark Brown 		rdev_err(rdev, "invalid mode %x specified\n", *mode);
255e573520bSDavid Brownell 		return -EINVAL;
256e573520bSDavid Brownell 	}
257e573520bSDavid Brownell 
258414c70cbSLiam Girdwood 	if (!rdev->constraints) {
2595da84fd9SJoe Perches 		rdev_err(rdev, "no constraints\n");
260414c70cbSLiam Girdwood 		return -ENODEV;
261414c70cbSLiam Girdwood 	}
262414c70cbSLiam Girdwood 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
2635da84fd9SJoe Perches 		rdev_err(rdev, "operation not allowed\n");
264414c70cbSLiam Girdwood 		return -EPERM;
265414c70cbSLiam Girdwood 	}
2662c608234SMark Brown 
2672c608234SMark Brown 	/* The modes are bitmasks, the most power hungry modes having
2682c608234SMark Brown 	 * the lowest values. If the requested mode isn't supported
2692c608234SMark Brown 	 * try higher modes. */
2702c608234SMark Brown 	while (*mode) {
2712c608234SMark Brown 		if (rdev->constraints->valid_modes_mask & *mode)
272414c70cbSLiam Girdwood 			return 0;
2732c608234SMark Brown 		*mode /= 2;
2742c608234SMark Brown 	}
2752c608234SMark Brown 
2762c608234SMark Brown 	return -EINVAL;
277414c70cbSLiam Girdwood }
278414c70cbSLiam Girdwood 
279414c70cbSLiam Girdwood /* dynamic regulator mode switching constraint check */
280414c70cbSLiam Girdwood static int regulator_check_drms(struct regulator_dev *rdev)
281414c70cbSLiam Girdwood {
282414c70cbSLiam Girdwood 	if (!rdev->constraints) {
2835da84fd9SJoe Perches 		rdev_err(rdev, "no constraints\n");
284414c70cbSLiam Girdwood 		return -ENODEV;
285414c70cbSLiam Girdwood 	}
286414c70cbSLiam Girdwood 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
2875da84fd9SJoe Perches 		rdev_err(rdev, "operation not allowed\n");
288414c70cbSLiam Girdwood 		return -EPERM;
289414c70cbSLiam Girdwood 	}
290414c70cbSLiam Girdwood 	return 0;
291414c70cbSLiam Girdwood }
292414c70cbSLiam Girdwood 
293414c70cbSLiam Girdwood static ssize_t device_requested_uA_show(struct device *dev,
294414c70cbSLiam Girdwood 			     struct device_attribute *attr, char *buf)
295414c70cbSLiam Girdwood {
296414c70cbSLiam Girdwood 	struct regulator *regulator;
297414c70cbSLiam Girdwood 
298414c70cbSLiam Girdwood 	regulator = get_device_regulator(dev);
299414c70cbSLiam Girdwood 	if (regulator == NULL)
300414c70cbSLiam Girdwood 		return 0;
301414c70cbSLiam Girdwood 
302414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", regulator->uA_load);
303414c70cbSLiam Girdwood }
304414c70cbSLiam Girdwood 
305414c70cbSLiam Girdwood static ssize_t regulator_uV_show(struct device *dev,
306414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
307414c70cbSLiam Girdwood {
308a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
309414c70cbSLiam Girdwood 	ssize_t ret;
310414c70cbSLiam Girdwood 
311414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
312414c70cbSLiam Girdwood 	ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
313414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
314414c70cbSLiam Girdwood 
315414c70cbSLiam Girdwood 	return ret;
316414c70cbSLiam Girdwood }
3177ad68e2fSDavid Brownell static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
318414c70cbSLiam Girdwood 
319414c70cbSLiam Girdwood static ssize_t regulator_uA_show(struct device *dev,
320414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
321414c70cbSLiam Girdwood {
322a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
323414c70cbSLiam Girdwood 
324414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
325414c70cbSLiam Girdwood }
3267ad68e2fSDavid Brownell static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
327414c70cbSLiam Girdwood 
328bc558a60SMark Brown static ssize_t regulator_name_show(struct device *dev,
329bc558a60SMark Brown 			     struct device_attribute *attr, char *buf)
330bc558a60SMark Brown {
331bc558a60SMark Brown 	struct regulator_dev *rdev = dev_get_drvdata(dev);
332bc558a60SMark Brown 
3331083c393SMark Brown 	return sprintf(buf, "%s\n", rdev_get_name(rdev));
334bc558a60SMark Brown }
335bc558a60SMark Brown 
3364fca9545SDavid Brownell static ssize_t regulator_print_opmode(char *buf, int mode)
337414c70cbSLiam Girdwood {
338414c70cbSLiam Girdwood 	switch (mode) {
339414c70cbSLiam Girdwood 	case REGULATOR_MODE_FAST:
340414c70cbSLiam Girdwood 		return sprintf(buf, "fast\n");
341414c70cbSLiam Girdwood 	case REGULATOR_MODE_NORMAL:
342414c70cbSLiam Girdwood 		return sprintf(buf, "normal\n");
343414c70cbSLiam Girdwood 	case REGULATOR_MODE_IDLE:
344414c70cbSLiam Girdwood 		return sprintf(buf, "idle\n");
345414c70cbSLiam Girdwood 	case REGULATOR_MODE_STANDBY:
346414c70cbSLiam Girdwood 		return sprintf(buf, "standby\n");
347414c70cbSLiam Girdwood 	}
348414c70cbSLiam Girdwood 	return sprintf(buf, "unknown\n");
349414c70cbSLiam Girdwood }
350414c70cbSLiam Girdwood 
3514fca9545SDavid Brownell static ssize_t regulator_opmode_show(struct device *dev,
352414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
353414c70cbSLiam Girdwood {
354a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
355414c70cbSLiam Girdwood 
3564fca9545SDavid Brownell 	return regulator_print_opmode(buf, _regulator_get_mode(rdev));
3574fca9545SDavid Brownell }
3587ad68e2fSDavid Brownell static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
3594fca9545SDavid Brownell 
3604fca9545SDavid Brownell static ssize_t regulator_print_state(char *buf, int state)
3614fca9545SDavid Brownell {
362414c70cbSLiam Girdwood 	if (state > 0)
363414c70cbSLiam Girdwood 		return sprintf(buf, "enabled\n");
364414c70cbSLiam Girdwood 	else if (state == 0)
365414c70cbSLiam Girdwood 		return sprintf(buf, "disabled\n");
366414c70cbSLiam Girdwood 	else
367414c70cbSLiam Girdwood 		return sprintf(buf, "unknown\n");
368414c70cbSLiam Girdwood }
369414c70cbSLiam Girdwood 
3704fca9545SDavid Brownell static ssize_t regulator_state_show(struct device *dev,
3714fca9545SDavid Brownell 				   struct device_attribute *attr, char *buf)
3724fca9545SDavid Brownell {
3734fca9545SDavid Brownell 	struct regulator_dev *rdev = dev_get_drvdata(dev);
3749332546fSMark Brown 	ssize_t ret;
3754fca9545SDavid Brownell 
3769332546fSMark Brown 	mutex_lock(&rdev->mutex);
3779332546fSMark Brown 	ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
3789332546fSMark Brown 	mutex_unlock(&rdev->mutex);
3799332546fSMark Brown 
3809332546fSMark Brown 	return ret;
3814fca9545SDavid Brownell }
3827ad68e2fSDavid Brownell static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
3834fca9545SDavid Brownell 
384853116a1SDavid Brownell static ssize_t regulator_status_show(struct device *dev,
385853116a1SDavid Brownell 				   struct device_attribute *attr, char *buf)
386853116a1SDavid Brownell {
387853116a1SDavid Brownell 	struct regulator_dev *rdev = dev_get_drvdata(dev);
388853116a1SDavid Brownell 	int status;
389853116a1SDavid Brownell 	char *label;
390853116a1SDavid Brownell 
391853116a1SDavid Brownell 	status = rdev->desc->ops->get_status(rdev);
392853116a1SDavid Brownell 	if (status < 0)
393853116a1SDavid Brownell 		return status;
394853116a1SDavid Brownell 
395853116a1SDavid Brownell 	switch (status) {
396853116a1SDavid Brownell 	case REGULATOR_STATUS_OFF:
397853116a1SDavid Brownell 		label = "off";
398853116a1SDavid Brownell 		break;
399853116a1SDavid Brownell 	case REGULATOR_STATUS_ON:
400853116a1SDavid Brownell 		label = "on";
401853116a1SDavid Brownell 		break;
402853116a1SDavid Brownell 	case REGULATOR_STATUS_ERROR:
403853116a1SDavid Brownell 		label = "error";
404853116a1SDavid Brownell 		break;
405853116a1SDavid Brownell 	case REGULATOR_STATUS_FAST:
406853116a1SDavid Brownell 		label = "fast";
407853116a1SDavid Brownell 		break;
408853116a1SDavid Brownell 	case REGULATOR_STATUS_NORMAL:
409853116a1SDavid Brownell 		label = "normal";
410853116a1SDavid Brownell 		break;
411853116a1SDavid Brownell 	case REGULATOR_STATUS_IDLE:
412853116a1SDavid Brownell 		label = "idle";
413853116a1SDavid Brownell 		break;
414853116a1SDavid Brownell 	case REGULATOR_STATUS_STANDBY:
415853116a1SDavid Brownell 		label = "standby";
416853116a1SDavid Brownell 		break;
417853116a1SDavid Brownell 	default:
418853116a1SDavid Brownell 		return -ERANGE;
419853116a1SDavid Brownell 	}
420853116a1SDavid Brownell 
421853116a1SDavid Brownell 	return sprintf(buf, "%s\n", label);
422853116a1SDavid Brownell }
423853116a1SDavid Brownell static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
424853116a1SDavid Brownell 
425414c70cbSLiam Girdwood static ssize_t regulator_min_uA_show(struct device *dev,
426414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
427414c70cbSLiam Girdwood {
428a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
429414c70cbSLiam Girdwood 
430414c70cbSLiam Girdwood 	if (!rdev->constraints)
431414c70cbSLiam Girdwood 		return sprintf(buf, "constraint not defined\n");
432414c70cbSLiam Girdwood 
433414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->min_uA);
434414c70cbSLiam Girdwood }
4357ad68e2fSDavid Brownell static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
436414c70cbSLiam Girdwood 
437414c70cbSLiam Girdwood static ssize_t regulator_max_uA_show(struct device *dev,
438414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
439414c70cbSLiam Girdwood {
440a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
441414c70cbSLiam Girdwood 
442414c70cbSLiam Girdwood 	if (!rdev->constraints)
443414c70cbSLiam Girdwood 		return sprintf(buf, "constraint not defined\n");
444414c70cbSLiam Girdwood 
445414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->max_uA);
446414c70cbSLiam Girdwood }
4477ad68e2fSDavid Brownell static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
448414c70cbSLiam Girdwood 
449414c70cbSLiam Girdwood static ssize_t regulator_min_uV_show(struct device *dev,
450414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
451414c70cbSLiam Girdwood {
452a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
453414c70cbSLiam Girdwood 
454414c70cbSLiam Girdwood 	if (!rdev->constraints)
455414c70cbSLiam Girdwood 		return sprintf(buf, "constraint not defined\n");
456414c70cbSLiam Girdwood 
457414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->min_uV);
458414c70cbSLiam Girdwood }
4597ad68e2fSDavid Brownell static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
460414c70cbSLiam Girdwood 
461414c70cbSLiam Girdwood static ssize_t regulator_max_uV_show(struct device *dev,
462414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
463414c70cbSLiam Girdwood {
464a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
465414c70cbSLiam Girdwood 
466414c70cbSLiam Girdwood 	if (!rdev->constraints)
467414c70cbSLiam Girdwood 		return sprintf(buf, "constraint not defined\n");
468414c70cbSLiam Girdwood 
469414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->max_uV);
470414c70cbSLiam Girdwood }
4717ad68e2fSDavid Brownell static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
472414c70cbSLiam Girdwood 
473414c70cbSLiam Girdwood static ssize_t regulator_total_uA_show(struct device *dev,
474414c70cbSLiam Girdwood 				      struct device_attribute *attr, char *buf)
475414c70cbSLiam Girdwood {
476a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
477414c70cbSLiam Girdwood 	struct regulator *regulator;
478414c70cbSLiam Girdwood 	int uA = 0;
479414c70cbSLiam Girdwood 
480414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
481414c70cbSLiam Girdwood 	list_for_each_entry(regulator, &rdev->consumer_list, list)
482414c70cbSLiam Girdwood 		uA += regulator->uA_load;
483414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
484414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", uA);
485414c70cbSLiam Girdwood }
4867ad68e2fSDavid Brownell static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
487414c70cbSLiam Girdwood 
488414c70cbSLiam Girdwood static ssize_t regulator_num_users_show(struct device *dev,
489414c70cbSLiam Girdwood 				      struct device_attribute *attr, char *buf)
490414c70cbSLiam Girdwood {
491a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
492414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->use_count);
493414c70cbSLiam Girdwood }
494414c70cbSLiam Girdwood 
495414c70cbSLiam Girdwood static ssize_t regulator_type_show(struct device *dev,
496414c70cbSLiam Girdwood 				  struct device_attribute *attr, char *buf)
497414c70cbSLiam Girdwood {
498a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
499414c70cbSLiam Girdwood 
500414c70cbSLiam Girdwood 	switch (rdev->desc->type) {
501414c70cbSLiam Girdwood 	case REGULATOR_VOLTAGE:
502414c70cbSLiam Girdwood 		return sprintf(buf, "voltage\n");
503414c70cbSLiam Girdwood 	case REGULATOR_CURRENT:
504414c70cbSLiam Girdwood 		return sprintf(buf, "current\n");
505414c70cbSLiam Girdwood 	}
506414c70cbSLiam Girdwood 	return sprintf(buf, "unknown\n");
507414c70cbSLiam Girdwood }
508414c70cbSLiam Girdwood 
509414c70cbSLiam Girdwood static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
510414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
511414c70cbSLiam Girdwood {
512a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
513414c70cbSLiam Girdwood 
514414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
515414c70cbSLiam Girdwood }
5167ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_mem_microvolts, 0444,
5177ad68e2fSDavid Brownell 		regulator_suspend_mem_uV_show, NULL);
518414c70cbSLiam Girdwood 
519414c70cbSLiam Girdwood static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
520414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
521414c70cbSLiam Girdwood {
522a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
523414c70cbSLiam Girdwood 
524414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
525414c70cbSLiam Girdwood }
5267ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_disk_microvolts, 0444,
5277ad68e2fSDavid Brownell 		regulator_suspend_disk_uV_show, NULL);
528414c70cbSLiam Girdwood 
529414c70cbSLiam Girdwood static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
530414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
531414c70cbSLiam Girdwood {
532a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
533414c70cbSLiam Girdwood 
534414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
535414c70cbSLiam Girdwood }
5367ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_standby_microvolts, 0444,
5377ad68e2fSDavid Brownell 		regulator_suspend_standby_uV_show, NULL);
538414c70cbSLiam Girdwood 
539414c70cbSLiam Girdwood static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
540414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
541414c70cbSLiam Girdwood {
542a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
543414c70cbSLiam Girdwood 
5444fca9545SDavid Brownell 	return regulator_print_opmode(buf,
5454fca9545SDavid Brownell 		rdev->constraints->state_mem.mode);
546414c70cbSLiam Girdwood }
5477ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_mem_mode, 0444,
5487ad68e2fSDavid Brownell 		regulator_suspend_mem_mode_show, NULL);
549414c70cbSLiam Girdwood 
550414c70cbSLiam Girdwood static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
551414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
552414c70cbSLiam Girdwood {
553a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
554414c70cbSLiam Girdwood 
5554fca9545SDavid Brownell 	return regulator_print_opmode(buf,
5564fca9545SDavid Brownell 		rdev->constraints->state_disk.mode);
557414c70cbSLiam Girdwood }
5587ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_disk_mode, 0444,
5597ad68e2fSDavid Brownell 		regulator_suspend_disk_mode_show, NULL);
560414c70cbSLiam Girdwood 
561414c70cbSLiam Girdwood static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
562414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
563414c70cbSLiam Girdwood {
564a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
565414c70cbSLiam Girdwood 
5664fca9545SDavid Brownell 	return regulator_print_opmode(buf,
5674fca9545SDavid Brownell 		rdev->constraints->state_standby.mode);
568414c70cbSLiam Girdwood }
5697ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_standby_mode, 0444,
5707ad68e2fSDavid Brownell 		regulator_suspend_standby_mode_show, NULL);
571414c70cbSLiam Girdwood 
572414c70cbSLiam Girdwood static ssize_t regulator_suspend_mem_state_show(struct device *dev,
573414c70cbSLiam Girdwood 				   struct device_attribute *attr, char *buf)
574414c70cbSLiam Girdwood {
575a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
576414c70cbSLiam Girdwood 
5774fca9545SDavid Brownell 	return regulator_print_state(buf,
5784fca9545SDavid Brownell 			rdev->constraints->state_mem.enabled);
579414c70cbSLiam Girdwood }
5807ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_mem_state, 0444,
5817ad68e2fSDavid Brownell 		regulator_suspend_mem_state_show, NULL);
582414c70cbSLiam Girdwood 
583414c70cbSLiam Girdwood static ssize_t regulator_suspend_disk_state_show(struct device *dev,
584414c70cbSLiam Girdwood 				   struct device_attribute *attr, char *buf)
585414c70cbSLiam Girdwood {
586a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
587414c70cbSLiam Girdwood 
5884fca9545SDavid Brownell 	return regulator_print_state(buf,
5894fca9545SDavid Brownell 			rdev->constraints->state_disk.enabled);
590414c70cbSLiam Girdwood }
5917ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_disk_state, 0444,
5927ad68e2fSDavid Brownell 		regulator_suspend_disk_state_show, NULL);
593414c70cbSLiam Girdwood 
594414c70cbSLiam Girdwood static ssize_t regulator_suspend_standby_state_show(struct device *dev,
595414c70cbSLiam Girdwood 				   struct device_attribute *attr, char *buf)
596414c70cbSLiam Girdwood {
597a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
598414c70cbSLiam Girdwood 
5994fca9545SDavid Brownell 	return regulator_print_state(buf,
6004fca9545SDavid Brownell 			rdev->constraints->state_standby.enabled);
601414c70cbSLiam Girdwood }
6027ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_standby_state, 0444,
6037ad68e2fSDavid Brownell 		regulator_suspend_standby_state_show, NULL);
604bc558a60SMark Brown 
6057ad68e2fSDavid Brownell 
6067ad68e2fSDavid Brownell /*
6077ad68e2fSDavid Brownell  * These are the only attributes are present for all regulators.
6087ad68e2fSDavid Brownell  * Other attributes are a function of regulator functionality.
6097ad68e2fSDavid Brownell  */
610414c70cbSLiam Girdwood static struct device_attribute regulator_dev_attrs[] = {
611bc558a60SMark Brown 	__ATTR(name, 0444, regulator_name_show, NULL),
612414c70cbSLiam Girdwood 	__ATTR(num_users, 0444, regulator_num_users_show, NULL),
613414c70cbSLiam Girdwood 	__ATTR(type, 0444, regulator_type_show, NULL),
614414c70cbSLiam Girdwood 	__ATTR_NULL,
615414c70cbSLiam Girdwood };
616414c70cbSLiam Girdwood 
617414c70cbSLiam Girdwood static void regulator_dev_release(struct device *dev)
618414c70cbSLiam Girdwood {
619a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
620414c70cbSLiam Girdwood 	kfree(rdev);
621414c70cbSLiam Girdwood }
622414c70cbSLiam Girdwood 
623414c70cbSLiam Girdwood static struct class regulator_class = {
624414c70cbSLiam Girdwood 	.name = "regulator",
625414c70cbSLiam Girdwood 	.dev_release = regulator_dev_release,
626414c70cbSLiam Girdwood 	.dev_attrs = regulator_dev_attrs,
627414c70cbSLiam Girdwood };
628414c70cbSLiam Girdwood 
629414c70cbSLiam Girdwood /* Calculate the new optimum regulator operating mode based on the new total
630414c70cbSLiam Girdwood  * consumer load. All locks held by caller */
631414c70cbSLiam Girdwood static void drms_uA_update(struct regulator_dev *rdev)
632414c70cbSLiam Girdwood {
633414c70cbSLiam Girdwood 	struct regulator *sibling;
634414c70cbSLiam Girdwood 	int current_uA = 0, output_uV, input_uV, err;
635414c70cbSLiam Girdwood 	unsigned int mode;
636414c70cbSLiam Girdwood 
637414c70cbSLiam Girdwood 	err = regulator_check_drms(rdev);
638414c70cbSLiam Girdwood 	if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
639476c2d83SMark Brown 	    (!rdev->desc->ops->get_voltage &&
640476c2d83SMark Brown 	     !rdev->desc->ops->get_voltage_sel) ||
641476c2d83SMark Brown 	    !rdev->desc->ops->set_mode)
642414c70cbSLiam Girdwood 		return;
643414c70cbSLiam Girdwood 
644414c70cbSLiam Girdwood 	/* get output voltage */
6451bf5a1f8SMark Brown 	output_uV = _regulator_get_voltage(rdev);
646414c70cbSLiam Girdwood 	if (output_uV <= 0)
647414c70cbSLiam Girdwood 		return;
648414c70cbSLiam Girdwood 
649414c70cbSLiam Girdwood 	/* get input voltage */
6501bf5a1f8SMark Brown 	input_uV = 0;
6511bf5a1f8SMark Brown 	if (rdev->supply)
6521bf5a1f8SMark Brown 		input_uV = _regulator_get_voltage(rdev);
6531bf5a1f8SMark Brown 	if (input_uV <= 0)
654414c70cbSLiam Girdwood 		input_uV = rdev->constraints->input_uV;
655414c70cbSLiam Girdwood 	if (input_uV <= 0)
656414c70cbSLiam Girdwood 		return;
657414c70cbSLiam Girdwood 
658414c70cbSLiam Girdwood 	/* calc total requested load */
659414c70cbSLiam Girdwood 	list_for_each_entry(sibling, &rdev->consumer_list, list)
660414c70cbSLiam Girdwood 		current_uA += sibling->uA_load;
661414c70cbSLiam Girdwood 
662414c70cbSLiam Girdwood 	/* now get the optimum mode for our new total regulator load */
663414c70cbSLiam Girdwood 	mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
664414c70cbSLiam Girdwood 						  output_uV, current_uA);
665414c70cbSLiam Girdwood 
666414c70cbSLiam Girdwood 	/* check the new mode is allowed */
6672c608234SMark Brown 	err = regulator_mode_constrain(rdev, &mode);
668414c70cbSLiam Girdwood 	if (err == 0)
669414c70cbSLiam Girdwood 		rdev->desc->ops->set_mode(rdev, mode);
670414c70cbSLiam Girdwood }
671414c70cbSLiam Girdwood 
672414c70cbSLiam Girdwood static int suspend_set_state(struct regulator_dev *rdev,
673414c70cbSLiam Girdwood 	struct regulator_state *rstate)
674414c70cbSLiam Girdwood {
675414c70cbSLiam Girdwood 	int ret = 0;
676638f85c5SMark Brown 	bool can_set_state;
677414c70cbSLiam Girdwood 
678638f85c5SMark Brown 	can_set_state = rdev->desc->ops->set_suspend_enable &&
679638f85c5SMark Brown 		rdev->desc->ops->set_suspend_disable;
680638f85c5SMark Brown 
681638f85c5SMark Brown 	/* If we have no suspend mode configration don't set anything;
682638f85c5SMark Brown 	 * only warn if the driver actually makes the suspend mode
683638f85c5SMark Brown 	 * configurable.
684638f85c5SMark Brown 	 */
685638f85c5SMark Brown 	if (!rstate->enabled && !rstate->disabled) {
686638f85c5SMark Brown 		if (can_set_state)
6875da84fd9SJoe Perches 			rdev_warn(rdev, "No configuration\n");
688638f85c5SMark Brown 		return 0;
689638f85c5SMark Brown 	}
690638f85c5SMark Brown 
691638f85c5SMark Brown 	if (rstate->enabled && rstate->disabled) {
6925da84fd9SJoe Perches 		rdev_err(rdev, "invalid configuration\n");
693638f85c5SMark Brown 		return -EINVAL;
694638f85c5SMark Brown 	}
695638f85c5SMark Brown 
696638f85c5SMark Brown 	if (!can_set_state) {
6975da84fd9SJoe Perches 		rdev_err(rdev, "no way to set suspend state\n");
698414c70cbSLiam Girdwood 		return -EINVAL;
699a5766f11SLiam Girdwood 	}
700414c70cbSLiam Girdwood 
701414c70cbSLiam Girdwood 	if (rstate->enabled)
702414c70cbSLiam Girdwood 		ret = rdev->desc->ops->set_suspend_enable(rdev);
703414c70cbSLiam Girdwood 	else
704414c70cbSLiam Girdwood 		ret = rdev->desc->ops->set_suspend_disable(rdev);
705414c70cbSLiam Girdwood 	if (ret < 0) {
7065da84fd9SJoe Perches 		rdev_err(rdev, "failed to enabled/disable\n");
707414c70cbSLiam Girdwood 		return ret;
708414c70cbSLiam Girdwood 	}
709414c70cbSLiam Girdwood 
710414c70cbSLiam Girdwood 	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
711414c70cbSLiam Girdwood 		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
712414c70cbSLiam Girdwood 		if (ret < 0) {
7135da84fd9SJoe Perches 			rdev_err(rdev, "failed to set voltage\n");
714414c70cbSLiam Girdwood 			return ret;
715414c70cbSLiam Girdwood 		}
716414c70cbSLiam Girdwood 	}
717414c70cbSLiam Girdwood 
718414c70cbSLiam Girdwood 	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
719414c70cbSLiam Girdwood 		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
720414c70cbSLiam Girdwood 		if (ret < 0) {
7215da84fd9SJoe Perches 			rdev_err(rdev, "failed to set mode\n");
722414c70cbSLiam Girdwood 			return ret;
723414c70cbSLiam Girdwood 		}
724414c70cbSLiam Girdwood 	}
725414c70cbSLiam Girdwood 	return ret;
726414c70cbSLiam Girdwood }
727414c70cbSLiam Girdwood 
728414c70cbSLiam Girdwood /* locks held by caller */
729414c70cbSLiam Girdwood static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
730414c70cbSLiam Girdwood {
731414c70cbSLiam Girdwood 	if (!rdev->constraints)
732414c70cbSLiam Girdwood 		return -EINVAL;
733414c70cbSLiam Girdwood 
734414c70cbSLiam Girdwood 	switch (state) {
735414c70cbSLiam Girdwood 	case PM_SUSPEND_STANDBY:
736414c70cbSLiam Girdwood 		return suspend_set_state(rdev,
737414c70cbSLiam Girdwood 			&rdev->constraints->state_standby);
738414c70cbSLiam Girdwood 	case PM_SUSPEND_MEM:
739414c70cbSLiam Girdwood 		return suspend_set_state(rdev,
740414c70cbSLiam Girdwood 			&rdev->constraints->state_mem);
741414c70cbSLiam Girdwood 	case PM_SUSPEND_MAX:
742414c70cbSLiam Girdwood 		return suspend_set_state(rdev,
743414c70cbSLiam Girdwood 			&rdev->constraints->state_disk);
744414c70cbSLiam Girdwood 	default:
745414c70cbSLiam Girdwood 		return -EINVAL;
746414c70cbSLiam Girdwood 	}
747414c70cbSLiam Girdwood }
748414c70cbSLiam Girdwood 
749414c70cbSLiam Girdwood static void print_constraints(struct regulator_dev *rdev)
750414c70cbSLiam Girdwood {
751414c70cbSLiam Girdwood 	struct regulation_constraints *constraints = rdev->constraints;
752973e9a27SMark Brown 	char buf[80] = "";
7538f031b48SMark Brown 	int count = 0;
7548f031b48SMark Brown 	int ret;
755414c70cbSLiam Girdwood 
7568f031b48SMark Brown 	if (constraints->min_uV && constraints->max_uV) {
757414c70cbSLiam Girdwood 		if (constraints->min_uV == constraints->max_uV)
7588f031b48SMark Brown 			count += sprintf(buf + count, "%d mV ",
759414c70cbSLiam Girdwood 					 constraints->min_uV / 1000);
760414c70cbSLiam Girdwood 		else
7618f031b48SMark Brown 			count += sprintf(buf + count, "%d <--> %d mV ",
762414c70cbSLiam Girdwood 					 constraints->min_uV / 1000,
763414c70cbSLiam Girdwood 					 constraints->max_uV / 1000);
7648f031b48SMark Brown 	}
7658f031b48SMark Brown 
7668f031b48SMark Brown 	if (!constraints->min_uV ||
7678f031b48SMark Brown 	    constraints->min_uV != constraints->max_uV) {
7688f031b48SMark Brown 		ret = _regulator_get_voltage(rdev);
7698f031b48SMark Brown 		if (ret > 0)
7708f031b48SMark Brown 			count += sprintf(buf + count, "at %d mV ", ret / 1000);
7718f031b48SMark Brown 	}
7728f031b48SMark Brown 
773bf5892a8SMark Brown 	if (constraints->uV_offset)
774bf5892a8SMark Brown 		count += sprintf(buf, "%dmV offset ",
775bf5892a8SMark Brown 				 constraints->uV_offset / 1000);
776bf5892a8SMark Brown 
7778f031b48SMark Brown 	if (constraints->min_uA && constraints->max_uA) {
778414c70cbSLiam Girdwood 		if (constraints->min_uA == constraints->max_uA)
7798f031b48SMark Brown 			count += sprintf(buf + count, "%d mA ",
780414c70cbSLiam Girdwood 					 constraints->min_uA / 1000);
781414c70cbSLiam Girdwood 		else
7828f031b48SMark Brown 			count += sprintf(buf + count, "%d <--> %d mA ",
783414c70cbSLiam Girdwood 					 constraints->min_uA / 1000,
784414c70cbSLiam Girdwood 					 constraints->max_uA / 1000);
785414c70cbSLiam Girdwood 	}
7868f031b48SMark Brown 
7878f031b48SMark Brown 	if (!constraints->min_uA ||
7888f031b48SMark Brown 	    constraints->min_uA != constraints->max_uA) {
7898f031b48SMark Brown 		ret = _regulator_get_current_limit(rdev);
7908f031b48SMark Brown 		if (ret > 0)
791e4a6376bSCyril Chemparathy 			count += sprintf(buf + count, "at %d mA ", ret / 1000);
7928f031b48SMark Brown 	}
7938f031b48SMark Brown 
794414c70cbSLiam Girdwood 	if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
795414c70cbSLiam Girdwood 		count += sprintf(buf + count, "fast ");
796414c70cbSLiam Girdwood 	if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
797414c70cbSLiam Girdwood 		count += sprintf(buf + count, "normal ");
798414c70cbSLiam Girdwood 	if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
799414c70cbSLiam Girdwood 		count += sprintf(buf + count, "idle ");
800414c70cbSLiam Girdwood 	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
801414c70cbSLiam Girdwood 		count += sprintf(buf + count, "standby");
802414c70cbSLiam Girdwood 
80313ce29f8SMark Brown 	rdev_info(rdev, "%s\n", buf);
8044a682922SMark Brown 
8054a682922SMark Brown 	if ((constraints->min_uV != constraints->max_uV) &&
8064a682922SMark Brown 	    !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
8074a682922SMark Brown 		rdev_warn(rdev,
8084a682922SMark Brown 			  "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
809414c70cbSLiam Girdwood }
810414c70cbSLiam Girdwood 
811e79055d6SMark Brown static int machine_constraints_voltage(struct regulator_dev *rdev,
8121083c393SMark Brown 	struct regulation_constraints *constraints)
813e79055d6SMark Brown {
814e79055d6SMark Brown 	struct regulator_ops *ops = rdev->desc->ops;
815af5866c9SMark Brown 	int ret;
816af5866c9SMark Brown 
817af5866c9SMark Brown 	/* do we need to apply the constraint voltage */
818af5866c9SMark Brown 	if (rdev->constraints->apply_uV &&
81975790251SMark Brown 	    rdev->constraints->min_uV == rdev->constraints->max_uV) {
82075790251SMark Brown 		ret = _regulator_do_set_voltage(rdev,
8213a93f2a9SMark Brown 						rdev->constraints->min_uV,
82275790251SMark Brown 						rdev->constraints->max_uV);
823af5866c9SMark Brown 		if (ret < 0) {
8245da84fd9SJoe Perches 			rdev_err(rdev, "failed to apply %duV constraint\n",
8255da84fd9SJoe Perches 				 rdev->constraints->min_uV);
826af5866c9SMark Brown 			return ret;
827af5866c9SMark Brown 		}
828af5866c9SMark Brown 	}
829e79055d6SMark Brown 
830e79055d6SMark Brown 	/* constrain machine-level voltage specs to fit
831e79055d6SMark Brown 	 * the actual range supported by this regulator.
832e79055d6SMark Brown 	 */
833e79055d6SMark Brown 	if (ops->list_voltage && rdev->desc->n_voltages) {
834e79055d6SMark Brown 		int	count = rdev->desc->n_voltages;
835e79055d6SMark Brown 		int	i;
836e79055d6SMark Brown 		int	min_uV = INT_MAX;
837e79055d6SMark Brown 		int	max_uV = INT_MIN;
838e79055d6SMark Brown 		int	cmin = constraints->min_uV;
839e79055d6SMark Brown 		int	cmax = constraints->max_uV;
840e79055d6SMark Brown 
841e79055d6SMark Brown 		/* it's safe to autoconfigure fixed-voltage supplies
842e79055d6SMark Brown 		   and the constraints are used by list_voltage. */
843e79055d6SMark Brown 		if (count == 1 && !cmin) {
844e79055d6SMark Brown 			cmin = 1;
845e79055d6SMark Brown 			cmax = INT_MAX;
846e79055d6SMark Brown 			constraints->min_uV = cmin;
847e79055d6SMark Brown 			constraints->max_uV = cmax;
848e79055d6SMark Brown 		}
849e79055d6SMark Brown 
850e79055d6SMark Brown 		/* voltage constraints are optional */
851e79055d6SMark Brown 		if ((cmin == 0) && (cmax == 0))
852e79055d6SMark Brown 			return 0;
853e79055d6SMark Brown 
854e79055d6SMark Brown 		/* else require explicit machine-level constraints */
855e79055d6SMark Brown 		if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
8565da84fd9SJoe Perches 			rdev_err(rdev, "invalid voltage constraints\n");
857e79055d6SMark Brown 			return -EINVAL;
858e79055d6SMark Brown 		}
859e79055d6SMark Brown 
860e79055d6SMark Brown 		/* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
861e79055d6SMark Brown 		for (i = 0; i < count; i++) {
862e79055d6SMark Brown 			int	value;
863e79055d6SMark Brown 
864e79055d6SMark Brown 			value = ops->list_voltage(rdev, i);
865e79055d6SMark Brown 			if (value <= 0)
866e79055d6SMark Brown 				continue;
867e79055d6SMark Brown 
868e79055d6SMark Brown 			/* maybe adjust [min_uV..max_uV] */
869e79055d6SMark Brown 			if (value >= cmin && value < min_uV)
870e79055d6SMark Brown 				min_uV = value;
871e79055d6SMark Brown 			if (value <= cmax && value > max_uV)
872e79055d6SMark Brown 				max_uV = value;
873e79055d6SMark Brown 		}
874e79055d6SMark Brown 
875e79055d6SMark Brown 		/* final: [min_uV..max_uV] valid iff constraints valid */
876e79055d6SMark Brown 		if (max_uV < min_uV) {
8775da84fd9SJoe Perches 			rdev_err(rdev, "unsupportable voltage constraints\n");
878e79055d6SMark Brown 			return -EINVAL;
879e79055d6SMark Brown 		}
880e79055d6SMark Brown 
881e79055d6SMark Brown 		/* use regulator's subset of machine constraints */
882e79055d6SMark Brown 		if (constraints->min_uV < min_uV) {
8835da84fd9SJoe Perches 			rdev_dbg(rdev, "override min_uV, %d -> %d\n",
8845da84fd9SJoe Perches 				 constraints->min_uV, min_uV);
885e79055d6SMark Brown 			constraints->min_uV = min_uV;
886e79055d6SMark Brown 		}
887e79055d6SMark Brown 		if (constraints->max_uV > max_uV) {
8885da84fd9SJoe Perches 			rdev_dbg(rdev, "override max_uV, %d -> %d\n",
8895da84fd9SJoe Perches 				 constraints->max_uV, max_uV);
890e79055d6SMark Brown 			constraints->max_uV = max_uV;
891e79055d6SMark Brown 		}
892e79055d6SMark Brown 	}
893e79055d6SMark Brown 
894e79055d6SMark Brown 	return 0;
895e79055d6SMark Brown }
896e79055d6SMark Brown 
897a5766f11SLiam Girdwood /**
898a5766f11SLiam Girdwood  * set_machine_constraints - sets regulator constraints
89969279fb9SMark Brown  * @rdev: regulator source
900c8e7e464SMark Brown  * @constraints: constraints to apply
901a5766f11SLiam Girdwood  *
902a5766f11SLiam Girdwood  * Allows platform initialisation code to define and constrain
903a5766f11SLiam Girdwood  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
904a5766f11SLiam Girdwood  * Constraints *must* be set by platform code in order for some
905a5766f11SLiam Girdwood  * regulator operations to proceed i.e. set_voltage, set_current_limit,
906a5766f11SLiam Girdwood  * set_mode.
907a5766f11SLiam Girdwood  */
908a5766f11SLiam Girdwood static int set_machine_constraints(struct regulator_dev *rdev,
909f8c12fe3SMark Brown 	const struct regulation_constraints *constraints)
910a5766f11SLiam Girdwood {
911a5766f11SLiam Girdwood 	int ret = 0;
912e5fda26cSMark Brown 	struct regulator_ops *ops = rdev->desc->ops;
913e06f5b4fSMark Brown 
9149a8f5e07SMark Brown 	if (constraints)
915f8c12fe3SMark Brown 		rdev->constraints = kmemdup(constraints, sizeof(*constraints),
916f8c12fe3SMark Brown 					    GFP_KERNEL);
9179a8f5e07SMark Brown 	else
9189a8f5e07SMark Brown 		rdev->constraints = kzalloc(sizeof(*constraints),
9199a8f5e07SMark Brown 					    GFP_KERNEL);
920f8c12fe3SMark Brown 	if (!rdev->constraints)
921f8c12fe3SMark Brown 		return -ENOMEM;
922af5866c9SMark Brown 
923f8c12fe3SMark Brown 	ret = machine_constraints_voltage(rdev, rdev->constraints);
924e79055d6SMark Brown 	if (ret != 0)
9253e2b9abdSMark Brown 		goto out;
9263e2b9abdSMark Brown 
927a5766f11SLiam Girdwood 	/* do we need to setup our suspend state */
9289a8f5e07SMark Brown 	if (rdev->constraints->initial_state) {
929f8c12fe3SMark Brown 		ret = suspend_prepare(rdev, rdev->constraints->initial_state);
930e06f5b4fSMark Brown 		if (ret < 0) {
9315da84fd9SJoe Perches 			rdev_err(rdev, "failed to set suspend state\n");
932e06f5b4fSMark Brown 			goto out;
933e06f5b4fSMark Brown 		}
934e06f5b4fSMark Brown 	}
935a5766f11SLiam Girdwood 
9369a8f5e07SMark Brown 	if (rdev->constraints->initial_mode) {
937a308466cSMark Brown 		if (!ops->set_mode) {
9385da84fd9SJoe Perches 			rdev_err(rdev, "no set_mode operation\n");
939a308466cSMark Brown 			ret = -EINVAL;
940a308466cSMark Brown 			goto out;
941a308466cSMark Brown 		}
942a308466cSMark Brown 
943f8c12fe3SMark Brown 		ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
944a308466cSMark Brown 		if (ret < 0) {
9455da84fd9SJoe Perches 			rdev_err(rdev, "failed to set initial mode: %d\n", ret);
946a308466cSMark Brown 			goto out;
947a308466cSMark Brown 		}
948a308466cSMark Brown 	}
949a308466cSMark Brown 
950cacf90f2SMark Brown 	/* If the constraints say the regulator should be on at this point
951cacf90f2SMark Brown 	 * and we have control then make sure it is enabled.
952cacf90f2SMark Brown 	 */
953f8c12fe3SMark Brown 	if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
954f8c12fe3SMark Brown 	    ops->enable) {
955e5fda26cSMark Brown 		ret = ops->enable(rdev);
956e5fda26cSMark Brown 		if (ret < 0) {
9575da84fd9SJoe Perches 			rdev_err(rdev, "failed to enable\n");
958e5fda26cSMark Brown 			goto out;
959e5fda26cSMark Brown 		}
960e5fda26cSMark Brown 	}
961e5fda26cSMark Brown 
962a5766f11SLiam Girdwood 	print_constraints(rdev);
9631a6958e7SAxel Lin 	return 0;
964a5766f11SLiam Girdwood out:
9651a6958e7SAxel Lin 	kfree(rdev->constraints);
9661a6958e7SAxel Lin 	rdev->constraints = NULL;
967a5766f11SLiam Girdwood 	return ret;
968a5766f11SLiam Girdwood }
969a5766f11SLiam Girdwood 
970a5766f11SLiam Girdwood /**
971a5766f11SLiam Girdwood  * set_supply - set regulator supply regulator
97269279fb9SMark Brown  * @rdev: regulator name
97369279fb9SMark Brown  * @supply_rdev: supply regulator name
974a5766f11SLiam Girdwood  *
975a5766f11SLiam Girdwood  * Called by platform initialisation code to set the supply regulator for this
976a5766f11SLiam Girdwood  * regulator. This ensures that a regulators supply will also be enabled by the
977a5766f11SLiam Girdwood  * core if it's child is enabled.
978a5766f11SLiam Girdwood  */
979a5766f11SLiam Girdwood static int set_supply(struct regulator_dev *rdev,
980a5766f11SLiam Girdwood 		      struct regulator_dev *supply_rdev)
981a5766f11SLiam Girdwood {
982a5766f11SLiam Girdwood 	int err;
983a5766f11SLiam Girdwood 
9843801b86aSMark Brown 	rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
9853801b86aSMark Brown 
9863801b86aSMark Brown 	rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
98732c78de8SAxel Lin 	if (rdev->supply == NULL) {
98832c78de8SAxel Lin 		err = -ENOMEM;
989a5766f11SLiam Girdwood 		return err;
990a5766f11SLiam Girdwood 	}
991a5766f11SLiam Girdwood 
9923801b86aSMark Brown 	return 0;
9933801b86aSMark Brown }
9943801b86aSMark Brown 
995a5766f11SLiam Girdwood /**
99606c63f93SRandy Dunlap  * set_consumer_device_supply - Bind a regulator to a symbolic supply
99769279fb9SMark Brown  * @rdev:         regulator source
99869279fb9SMark Brown  * @consumer_dev: device the supply applies to
99940f9244fSMark Brown  * @consumer_dev_name: dev_name() string for device supply applies to
1000a5766f11SLiam Girdwood  * @supply:       symbolic name for supply
1001a5766f11SLiam Girdwood  *
1002a5766f11SLiam Girdwood  * Allows platform initialisation code to map physical regulator
1003a5766f11SLiam Girdwood  * sources to symbolic names for supplies for use by devices.  Devices
1004a5766f11SLiam Girdwood  * should use these symbolic names to request regulators, avoiding the
1005a5766f11SLiam Girdwood  * need to provide board-specific regulator names as platform data.
100640f9244fSMark Brown  *
100740f9244fSMark Brown  * Only one of consumer_dev and consumer_dev_name may be specified.
1008a5766f11SLiam Girdwood  */
1009a5766f11SLiam Girdwood static int set_consumer_device_supply(struct regulator_dev *rdev,
101040f9244fSMark Brown 	struct device *consumer_dev, const char *consumer_dev_name,
101140f9244fSMark Brown 	const char *supply)
1012a5766f11SLiam Girdwood {
1013a5766f11SLiam Girdwood 	struct regulator_map *node;
10149ed2099eSMark Brown 	int has_dev;
1015a5766f11SLiam Girdwood 
101640f9244fSMark Brown 	if (consumer_dev && consumer_dev_name)
101740f9244fSMark Brown 		return -EINVAL;
101840f9244fSMark Brown 
101940f9244fSMark Brown 	if (!consumer_dev_name && consumer_dev)
102040f9244fSMark Brown 		consumer_dev_name = dev_name(consumer_dev);
102140f9244fSMark Brown 
1022a5766f11SLiam Girdwood 	if (supply == NULL)
1023a5766f11SLiam Girdwood 		return -EINVAL;
1024a5766f11SLiam Girdwood 
10259ed2099eSMark Brown 	if (consumer_dev_name != NULL)
10269ed2099eSMark Brown 		has_dev = 1;
10279ed2099eSMark Brown 	else
10289ed2099eSMark Brown 		has_dev = 0;
10299ed2099eSMark Brown 
10306001e13cSDavid Brownell 	list_for_each_entry(node, &regulator_map_list, list) {
103123b5cc2aSJani Nikula 		if (node->dev_name && consumer_dev_name) {
103223b5cc2aSJani Nikula 			if (strcmp(node->dev_name, consumer_dev_name) != 0)
10336001e13cSDavid Brownell 				continue;
103423b5cc2aSJani Nikula 		} else if (node->dev_name || consumer_dev_name) {
103523b5cc2aSJani Nikula 			continue;
103623b5cc2aSJani Nikula 		}
103723b5cc2aSJani Nikula 
10386001e13cSDavid Brownell 		if (strcmp(node->supply, supply) != 0)
10396001e13cSDavid Brownell 			continue;
10406001e13cSDavid Brownell 
10416001e13cSDavid Brownell 		dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
10426001e13cSDavid Brownell 			dev_name(&node->regulator->dev),
10436001e13cSDavid Brownell 			node->regulator->desc->name,
10446001e13cSDavid Brownell 			supply,
10451083c393SMark Brown 			dev_name(&rdev->dev), rdev_get_name(rdev));
10466001e13cSDavid Brownell 		return -EBUSY;
10476001e13cSDavid Brownell 	}
10486001e13cSDavid Brownell 
10499ed2099eSMark Brown 	node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1050a5766f11SLiam Girdwood 	if (node == NULL)
1051a5766f11SLiam Girdwood 		return -ENOMEM;
1052a5766f11SLiam Girdwood 
1053a5766f11SLiam Girdwood 	node->regulator = rdev;
1054a5766f11SLiam Girdwood 	node->supply = supply;
1055a5766f11SLiam Girdwood 
10569ed2099eSMark Brown 	if (has_dev) {
10579ed2099eSMark Brown 		node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
105840f9244fSMark Brown 		if (node->dev_name == NULL) {
105940f9244fSMark Brown 			kfree(node);
106040f9244fSMark Brown 			return -ENOMEM;
106140f9244fSMark Brown 		}
10629ed2099eSMark Brown 	}
106340f9244fSMark Brown 
1064a5766f11SLiam Girdwood 	list_add(&node->list, &regulator_map_list);
1065a5766f11SLiam Girdwood 	return 0;
1066a5766f11SLiam Girdwood }
1067a5766f11SLiam Girdwood 
10680f1d747bSMike Rapoport static void unset_regulator_supplies(struct regulator_dev *rdev)
10690f1d747bSMike Rapoport {
10700f1d747bSMike Rapoport 	struct regulator_map *node, *n;
10710f1d747bSMike Rapoport 
10720f1d747bSMike Rapoport 	list_for_each_entry_safe(node, n, &regulator_map_list, list) {
10730f1d747bSMike Rapoport 		if (rdev == node->regulator) {
10740f1d747bSMike Rapoport 			list_del(&node->list);
107540f9244fSMark Brown 			kfree(node->dev_name);
10760f1d747bSMike Rapoport 			kfree(node);
10770f1d747bSMike Rapoport 		}
10780f1d747bSMike Rapoport 	}
10790f1d747bSMike Rapoport }
10800f1d747bSMike Rapoport 
1081f5726ae3SMark Brown #define REG_STR_SIZE	64
1082414c70cbSLiam Girdwood 
1083414c70cbSLiam Girdwood static struct regulator *create_regulator(struct regulator_dev *rdev,
1084414c70cbSLiam Girdwood 					  struct device *dev,
1085414c70cbSLiam Girdwood 					  const char *supply_name)
1086414c70cbSLiam Girdwood {
1087414c70cbSLiam Girdwood 	struct regulator *regulator;
1088414c70cbSLiam Girdwood 	char buf[REG_STR_SIZE];
1089414c70cbSLiam Girdwood 	int err, size;
1090414c70cbSLiam Girdwood 
1091414c70cbSLiam Girdwood 	regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1092414c70cbSLiam Girdwood 	if (regulator == NULL)
1093414c70cbSLiam Girdwood 		return NULL;
1094414c70cbSLiam Girdwood 
1095414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
1096414c70cbSLiam Girdwood 	regulator->rdev = rdev;
1097414c70cbSLiam Girdwood 	list_add(&regulator->list, &rdev->consumer_list);
1098414c70cbSLiam Girdwood 
1099414c70cbSLiam Girdwood 	if (dev) {
1100414c70cbSLiam Girdwood 		/* create a 'requested_microamps_name' sysfs entry */
1101e0eaedefSMark Brown 		size = scnprintf(buf, REG_STR_SIZE,
1102e0eaedefSMark Brown 				 "microamps_requested_%s-%s",
1103e0eaedefSMark Brown 				 dev_name(dev), supply_name);
1104414c70cbSLiam Girdwood 		if (size >= REG_STR_SIZE)
1105414c70cbSLiam Girdwood 			goto overflow_err;
1106414c70cbSLiam Girdwood 
1107414c70cbSLiam Girdwood 		regulator->dev = dev;
11084f26a2abSAmeya Palande 		sysfs_attr_init(&regulator->dev_attr.attr);
1109414c70cbSLiam Girdwood 		regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1110414c70cbSLiam Girdwood 		if (regulator->dev_attr.attr.name == NULL)
1111414c70cbSLiam Girdwood 			goto attr_name_err;
1112414c70cbSLiam Girdwood 
1113414c70cbSLiam Girdwood 		regulator->dev_attr.attr.mode = 0444;
1114414c70cbSLiam Girdwood 		regulator->dev_attr.show = device_requested_uA_show;
1115414c70cbSLiam Girdwood 		err = device_create_file(dev, &regulator->dev_attr);
1116414c70cbSLiam Girdwood 		if (err < 0) {
11175da84fd9SJoe Perches 			rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1118414c70cbSLiam Girdwood 			goto attr_name_err;
1119414c70cbSLiam Girdwood 		}
1120414c70cbSLiam Girdwood 
1121414c70cbSLiam Girdwood 		/* also add a link to the device sysfs entry */
1122414c70cbSLiam Girdwood 		size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1123414c70cbSLiam Girdwood 				 dev->kobj.name, supply_name);
1124414c70cbSLiam Girdwood 		if (size >= REG_STR_SIZE)
1125414c70cbSLiam Girdwood 			goto attr_err;
1126414c70cbSLiam Girdwood 
1127414c70cbSLiam Girdwood 		regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1128414c70cbSLiam Girdwood 		if (regulator->supply_name == NULL)
1129414c70cbSLiam Girdwood 			goto attr_err;
1130414c70cbSLiam Girdwood 
1131414c70cbSLiam Girdwood 		err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1132414c70cbSLiam Girdwood 					buf);
1133414c70cbSLiam Girdwood 		if (err) {
11345da84fd9SJoe Perches 			rdev_warn(rdev, "could not add device link %s err %d\n",
11351d7372e1SDaniel Walker 				  dev->kobj.name, err);
1136414c70cbSLiam Girdwood 			goto link_name_err;
1137414c70cbSLiam Girdwood 		}
11385de70519SMark Brown 	} else {
11395de70519SMark Brown 		regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
11405de70519SMark Brown 		if (regulator->supply_name == NULL)
11415de70519SMark Brown 			goto attr_err;
1142414c70cbSLiam Girdwood 	}
11435de70519SMark Brown 
11445de70519SMark Brown 	regulator->debugfs = debugfs_create_dir(regulator->supply_name,
11455de70519SMark Brown 						rdev->debugfs);
114624751434SStephen Boyd 	if (!regulator->debugfs) {
11475de70519SMark Brown 		rdev_warn(rdev, "Failed to create debugfs directory\n");
11485de70519SMark Brown 	} else {
11495de70519SMark Brown 		debugfs_create_u32("uA_load", 0444, regulator->debugfs,
11505de70519SMark Brown 				   &regulator->uA_load);
11515de70519SMark Brown 		debugfs_create_u32("min_uV", 0444, regulator->debugfs,
11525de70519SMark Brown 				   &regulator->min_uV);
11535de70519SMark Brown 		debugfs_create_u32("max_uV", 0444, regulator->debugfs,
11545de70519SMark Brown 				   &regulator->max_uV);
11555de70519SMark Brown 	}
11565de70519SMark Brown 
1157414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
1158414c70cbSLiam Girdwood 	return regulator;
1159414c70cbSLiam Girdwood link_name_err:
1160414c70cbSLiam Girdwood 	kfree(regulator->supply_name);
1161414c70cbSLiam Girdwood attr_err:
1162414c70cbSLiam Girdwood 	device_remove_file(regulator->dev, &regulator->dev_attr);
1163414c70cbSLiam Girdwood attr_name_err:
1164414c70cbSLiam Girdwood 	kfree(regulator->dev_attr.attr.name);
1165414c70cbSLiam Girdwood overflow_err:
1166414c70cbSLiam Girdwood 	list_del(&regulator->list);
1167414c70cbSLiam Girdwood 	kfree(regulator);
1168414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
1169414c70cbSLiam Girdwood 	return NULL;
1170414c70cbSLiam Girdwood }
1171414c70cbSLiam Girdwood 
117231aae2beSMark Brown static int _regulator_get_enable_time(struct regulator_dev *rdev)
117331aae2beSMark Brown {
117431aae2beSMark Brown 	if (!rdev->desc->ops->enable_time)
117531aae2beSMark Brown 		return 0;
117631aae2beSMark Brown 	return rdev->desc->ops->enable_time(rdev);
117731aae2beSMark Brown }
117831aae2beSMark Brown 
117969511a45SRajendra Nayak static struct regulator_dev *regulator_dev_lookup(struct device *dev,
118069511a45SRajendra Nayak 							 const char *supply)
118169511a45SRajendra Nayak {
118269511a45SRajendra Nayak 	struct regulator_dev *r;
118369511a45SRajendra Nayak 	struct device_node *node;
118469511a45SRajendra Nayak 
118569511a45SRajendra Nayak 	/* first do a dt based lookup */
118669511a45SRajendra Nayak 	if (dev && dev->of_node) {
118769511a45SRajendra Nayak 		node = of_get_regulator(dev, supply);
118869511a45SRajendra Nayak 		if (node)
118969511a45SRajendra Nayak 			list_for_each_entry(r, &regulator_list, list)
119069511a45SRajendra Nayak 				if (r->dev.parent &&
119169511a45SRajendra Nayak 					node == r->dev.of_node)
119269511a45SRajendra Nayak 					return r;
119369511a45SRajendra Nayak 	}
119469511a45SRajendra Nayak 
119569511a45SRajendra Nayak 	/* if not found, try doing it non-dt way */
119669511a45SRajendra Nayak 	list_for_each_entry(r, &regulator_list, list)
119769511a45SRajendra Nayak 		if (strcmp(rdev_get_name(r), supply) == 0)
119869511a45SRajendra Nayak 			return r;
119969511a45SRajendra Nayak 
120069511a45SRajendra Nayak 	return NULL;
120169511a45SRajendra Nayak }
120269511a45SRajendra Nayak 
12035ffbd136SMark Brown /* Internal regulator request function */
12045ffbd136SMark Brown static struct regulator *_regulator_get(struct device *dev, const char *id,
12055ffbd136SMark Brown 					int exclusive)
1206414c70cbSLiam Girdwood {
1207414c70cbSLiam Girdwood 	struct regulator_dev *rdev;
1208414c70cbSLiam Girdwood 	struct regulator_map *map;
1209414c70cbSLiam Girdwood 	struct regulator *regulator = ERR_PTR(-ENODEV);
121040f9244fSMark Brown 	const char *devname = NULL;
12115ffbd136SMark Brown 	int ret;
1212414c70cbSLiam Girdwood 
1213414c70cbSLiam Girdwood 	if (id == NULL) {
12145da84fd9SJoe Perches 		pr_err("get() with no identifier\n");
1215414c70cbSLiam Girdwood 		return regulator;
1216414c70cbSLiam Girdwood 	}
1217414c70cbSLiam Girdwood 
121840f9244fSMark Brown 	if (dev)
121940f9244fSMark Brown 		devname = dev_name(dev);
122040f9244fSMark Brown 
1221414c70cbSLiam Girdwood 	mutex_lock(&regulator_list_mutex);
1222414c70cbSLiam Girdwood 
122369511a45SRajendra Nayak 	rdev = regulator_dev_lookup(dev, id);
122469511a45SRajendra Nayak 	if (rdev)
122569511a45SRajendra Nayak 		goto found;
122669511a45SRajendra Nayak 
1227414c70cbSLiam Girdwood 	list_for_each_entry(map, &regulator_map_list, list) {
122840f9244fSMark Brown 		/* If the mapping has a device set up it must match */
122940f9244fSMark Brown 		if (map->dev_name &&
123040f9244fSMark Brown 		    (!devname || strcmp(map->dev_name, devname)))
123140f9244fSMark Brown 			continue;
123240f9244fSMark Brown 
123340f9244fSMark Brown 		if (strcmp(map->supply, id) == 0) {
1234a5766f11SLiam Girdwood 			rdev = map->regulator;
1235414c70cbSLiam Girdwood 			goto found;
1236414c70cbSLiam Girdwood 		}
1237a5766f11SLiam Girdwood 	}
123834abbd68SMark Brown 
1239688fe99aSMark Brown 	if (board_wants_dummy_regulator) {
1240688fe99aSMark Brown 		rdev = dummy_regulator_rdev;
1241688fe99aSMark Brown 		goto found;
1242688fe99aSMark Brown 	}
1243688fe99aSMark Brown 
124434abbd68SMark Brown #ifdef CONFIG_REGULATOR_DUMMY
124534abbd68SMark Brown 	if (!devname)
124634abbd68SMark Brown 		devname = "deviceless";
124734abbd68SMark Brown 
124834abbd68SMark Brown 	/* If the board didn't flag that it was fully constrained then
124934abbd68SMark Brown 	 * substitute in a dummy regulator so consumers can continue.
125034abbd68SMark Brown 	 */
125134abbd68SMark Brown 	if (!has_full_constraints) {
12525da84fd9SJoe Perches 		pr_warn("%s supply %s not found, using dummy regulator\n",
125334abbd68SMark Brown 			devname, id);
125434abbd68SMark Brown 		rdev = dummy_regulator_rdev;
125534abbd68SMark Brown 		goto found;
125634abbd68SMark Brown 	}
125734abbd68SMark Brown #endif
125834abbd68SMark Brown 
1259414c70cbSLiam Girdwood 	mutex_unlock(&regulator_list_mutex);
1260414c70cbSLiam Girdwood 	return regulator;
1261414c70cbSLiam Girdwood 
1262414c70cbSLiam Girdwood found:
12635ffbd136SMark Brown 	if (rdev->exclusive) {
12645ffbd136SMark Brown 		regulator = ERR_PTR(-EPERM);
12655ffbd136SMark Brown 		goto out;
12665ffbd136SMark Brown 	}
12675ffbd136SMark Brown 
12685ffbd136SMark Brown 	if (exclusive && rdev->open_count) {
12695ffbd136SMark Brown 		regulator = ERR_PTR(-EBUSY);
12705ffbd136SMark Brown 		goto out;
12715ffbd136SMark Brown 	}
12725ffbd136SMark Brown 
1273a5766f11SLiam Girdwood 	if (!try_module_get(rdev->owner))
1274a5766f11SLiam Girdwood 		goto out;
1275a5766f11SLiam Girdwood 
1276414c70cbSLiam Girdwood 	regulator = create_regulator(rdev, dev, id);
1277414c70cbSLiam Girdwood 	if (regulator == NULL) {
1278414c70cbSLiam Girdwood 		regulator = ERR_PTR(-ENOMEM);
1279414c70cbSLiam Girdwood 		module_put(rdev->owner);
1280bcda4321SAxel Lin 		goto out;
1281414c70cbSLiam Girdwood 	}
1282414c70cbSLiam Girdwood 
12835ffbd136SMark Brown 	rdev->open_count++;
12845ffbd136SMark Brown 	if (exclusive) {
12855ffbd136SMark Brown 		rdev->exclusive = 1;
12865ffbd136SMark Brown 
12875ffbd136SMark Brown 		ret = _regulator_is_enabled(rdev);
12885ffbd136SMark Brown 		if (ret > 0)
12895ffbd136SMark Brown 			rdev->use_count = 1;
12905ffbd136SMark Brown 		else
12915ffbd136SMark Brown 			rdev->use_count = 0;
12925ffbd136SMark Brown 	}
12935ffbd136SMark Brown 
1294a5766f11SLiam Girdwood out:
1295414c70cbSLiam Girdwood 	mutex_unlock(&regulator_list_mutex);
12965ffbd136SMark Brown 
1297414c70cbSLiam Girdwood 	return regulator;
1298414c70cbSLiam Girdwood }
12995ffbd136SMark Brown 
13005ffbd136SMark Brown /**
13015ffbd136SMark Brown  * regulator_get - lookup and obtain a reference to a regulator.
13025ffbd136SMark Brown  * @dev: device for regulator "consumer"
13035ffbd136SMark Brown  * @id: Supply name or regulator ID.
13045ffbd136SMark Brown  *
13055ffbd136SMark Brown  * Returns a struct regulator corresponding to the regulator producer,
13065ffbd136SMark Brown  * or IS_ERR() condition containing errno.
13075ffbd136SMark Brown  *
13085ffbd136SMark Brown  * Use of supply names configured via regulator_set_device_supply() is
13095ffbd136SMark Brown  * strongly encouraged.  It is recommended that the supply name used
13105ffbd136SMark Brown  * should match the name used for the supply and/or the relevant
13115ffbd136SMark Brown  * device pins in the datasheet.
13125ffbd136SMark Brown  */
13135ffbd136SMark Brown struct regulator *regulator_get(struct device *dev, const char *id)
13145ffbd136SMark Brown {
13155ffbd136SMark Brown 	return _regulator_get(dev, id, 0);
13165ffbd136SMark Brown }
1317414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get);
1318414c70cbSLiam Girdwood 
1319414c70cbSLiam Girdwood /**
13205ffbd136SMark Brown  * regulator_get_exclusive - obtain exclusive access to a regulator.
13215ffbd136SMark Brown  * @dev: device for regulator "consumer"
13225ffbd136SMark Brown  * @id: Supply name or regulator ID.
13235ffbd136SMark Brown  *
13245ffbd136SMark Brown  * Returns a struct regulator corresponding to the regulator producer,
13255ffbd136SMark Brown  * or IS_ERR() condition containing errno.  Other consumers will be
13265ffbd136SMark Brown  * unable to obtain this reference is held and the use count for the
13275ffbd136SMark Brown  * regulator will be initialised to reflect the current state of the
13285ffbd136SMark Brown  * regulator.
13295ffbd136SMark Brown  *
13305ffbd136SMark Brown  * This is intended for use by consumers which cannot tolerate shared
13315ffbd136SMark Brown  * use of the regulator such as those which need to force the
13325ffbd136SMark Brown  * regulator off for correct operation of the hardware they are
13335ffbd136SMark Brown  * controlling.
13345ffbd136SMark Brown  *
13355ffbd136SMark Brown  * Use of supply names configured via regulator_set_device_supply() is
13365ffbd136SMark Brown  * strongly encouraged.  It is recommended that the supply name used
13375ffbd136SMark Brown  * should match the name used for the supply and/or the relevant
13385ffbd136SMark Brown  * device pins in the datasheet.
13395ffbd136SMark Brown  */
13405ffbd136SMark Brown struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
13415ffbd136SMark Brown {
13425ffbd136SMark Brown 	return _regulator_get(dev, id, 1);
13435ffbd136SMark Brown }
13445ffbd136SMark Brown EXPORT_SYMBOL_GPL(regulator_get_exclusive);
13455ffbd136SMark Brown 
13465ffbd136SMark Brown /**
1347414c70cbSLiam Girdwood  * regulator_put - "free" the regulator source
1348414c70cbSLiam Girdwood  * @regulator: regulator source
1349414c70cbSLiam Girdwood  *
1350414c70cbSLiam Girdwood  * Note: drivers must ensure that all regulator_enable calls made on this
1351414c70cbSLiam Girdwood  * regulator source are balanced by regulator_disable calls prior to calling
1352414c70cbSLiam Girdwood  * this function.
1353414c70cbSLiam Girdwood  */
1354414c70cbSLiam Girdwood void regulator_put(struct regulator *regulator)
1355414c70cbSLiam Girdwood {
1356414c70cbSLiam Girdwood 	struct regulator_dev *rdev;
1357414c70cbSLiam Girdwood 
1358414c70cbSLiam Girdwood 	if (regulator == NULL || IS_ERR(regulator))
1359414c70cbSLiam Girdwood 		return;
1360414c70cbSLiam Girdwood 
1361414c70cbSLiam Girdwood 	mutex_lock(&regulator_list_mutex);
1362414c70cbSLiam Girdwood 	rdev = regulator->rdev;
1363414c70cbSLiam Girdwood 
13645de70519SMark Brown 	debugfs_remove_recursive(regulator->debugfs);
13655de70519SMark Brown 
1366414c70cbSLiam Girdwood 	/* remove any sysfs entries */
1367414c70cbSLiam Girdwood 	if (regulator->dev) {
1368414c70cbSLiam Girdwood 		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1369414c70cbSLiam Girdwood 		device_remove_file(regulator->dev, &regulator->dev_attr);
1370414c70cbSLiam Girdwood 		kfree(regulator->dev_attr.attr.name);
1371414c70cbSLiam Girdwood 	}
13725de70519SMark Brown 	kfree(regulator->supply_name);
1373414c70cbSLiam Girdwood 	list_del(&regulator->list);
1374414c70cbSLiam Girdwood 	kfree(regulator);
1375414c70cbSLiam Girdwood 
13765ffbd136SMark Brown 	rdev->open_count--;
13775ffbd136SMark Brown 	rdev->exclusive = 0;
13785ffbd136SMark Brown 
1379414c70cbSLiam Girdwood 	module_put(rdev->owner);
1380414c70cbSLiam Girdwood 	mutex_unlock(&regulator_list_mutex);
1381414c70cbSLiam Girdwood }
1382414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_put);
1383414c70cbSLiam Girdwood 
13849a2372faSMark Brown static int _regulator_can_change_status(struct regulator_dev *rdev)
13859a2372faSMark Brown {
13869a2372faSMark Brown 	if (!rdev->constraints)
13879a2372faSMark Brown 		return 0;
13889a2372faSMark Brown 
13899a2372faSMark Brown 	if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
13909a2372faSMark Brown 		return 1;
13919a2372faSMark Brown 	else
13929a2372faSMark Brown 		return 0;
13939a2372faSMark Brown }
13949a2372faSMark Brown 
1395414c70cbSLiam Girdwood /* locks held by regulator_enable() */
1396414c70cbSLiam Girdwood static int _regulator_enable(struct regulator_dev *rdev)
1397414c70cbSLiam Girdwood {
139831aae2beSMark Brown 	int ret, delay;
1399414c70cbSLiam Girdwood 
1400414c70cbSLiam Girdwood 	/* check voltage and requested load before enabling */
1401414c70cbSLiam Girdwood 	if (rdev->constraints &&
14029a2372faSMark Brown 	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1403414c70cbSLiam Girdwood 		drms_uA_update(rdev);
1404414c70cbSLiam Girdwood 
14059a2372faSMark Brown 	if (rdev->use_count == 0) {
14069a2372faSMark Brown 		/* The regulator may on if it's not switchable or left on */
14079a2372faSMark Brown 		ret = _regulator_is_enabled(rdev);
14089a2372faSMark Brown 		if (ret == -EINVAL || ret == 0) {
14099a2372faSMark Brown 			if (!_regulator_can_change_status(rdev))
14109a2372faSMark Brown 				return -EPERM;
14119a2372faSMark Brown 
141231aae2beSMark Brown 			if (!rdev->desc->ops->enable)
141331aae2beSMark Brown 				return -EINVAL;
141431aae2beSMark Brown 
141531aae2beSMark Brown 			/* Query before enabling in case configuration
141625985edcSLucas De Marchi 			 * dependent.  */
141731aae2beSMark Brown 			ret = _regulator_get_enable_time(rdev);
141831aae2beSMark Brown 			if (ret >= 0) {
141931aae2beSMark Brown 				delay = ret;
142031aae2beSMark Brown 			} else {
14215da84fd9SJoe Perches 				rdev_warn(rdev, "enable_time() failed: %d\n",
142231aae2beSMark Brown 					   ret);
142331aae2beSMark Brown 				delay = 0;
142431aae2beSMark Brown 			}
142531aae2beSMark Brown 
142602fa3ec0SMark Brown 			trace_regulator_enable(rdev_get_name(rdev));
142702fa3ec0SMark Brown 
142831aae2beSMark Brown 			/* Allow the regulator to ramp; it would be useful
142931aae2beSMark Brown 			 * to extend this for bulk operations so that the
143031aae2beSMark Brown 			 * regulators can ramp together.  */
1431414c70cbSLiam Girdwood 			ret = rdev->desc->ops->enable(rdev);
14329a2372faSMark Brown 			if (ret < 0)
14339a2372faSMark Brown 				return ret;
143431aae2beSMark Brown 
143502fa3ec0SMark Brown 			trace_regulator_enable_delay(rdev_get_name(rdev));
143602fa3ec0SMark Brown 
1437e36c1df8SAxel Lin 			if (delay >= 1000) {
143831aae2beSMark Brown 				mdelay(delay / 1000);
1439e36c1df8SAxel Lin 				udelay(delay % 1000);
1440e36c1df8SAxel Lin 			} else if (delay) {
144131aae2beSMark Brown 				udelay(delay);
1442e36c1df8SAxel Lin 			}
144331aae2beSMark Brown 
144402fa3ec0SMark Brown 			trace_regulator_enable_complete(rdev_get_name(rdev));
144502fa3ec0SMark Brown 
1446a7433cffSLinus Walleij 		} else if (ret < 0) {
14475da84fd9SJoe Perches 			rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1448414c70cbSLiam Girdwood 			return ret;
1449414c70cbSLiam Girdwood 		}
1450a7433cffSLinus Walleij 		/* Fallthrough on positive return values - already enabled */
1451414c70cbSLiam Girdwood 	}
1452414c70cbSLiam Girdwood 
14539a2372faSMark Brown 	rdev->use_count++;
14549a2372faSMark Brown 
14559a2372faSMark Brown 	return 0;
1456414c70cbSLiam Girdwood }
1457414c70cbSLiam Girdwood 
1458414c70cbSLiam Girdwood /**
1459414c70cbSLiam Girdwood  * regulator_enable - enable regulator output
1460414c70cbSLiam Girdwood  * @regulator: regulator source
1461414c70cbSLiam Girdwood  *
1462cf7bbcdfSMark Brown  * Request that the regulator be enabled with the regulator output at
1463cf7bbcdfSMark Brown  * the predefined voltage or current value.  Calls to regulator_enable()
1464cf7bbcdfSMark Brown  * must be balanced with calls to regulator_disable().
1465cf7bbcdfSMark Brown  *
1466414c70cbSLiam Girdwood  * NOTE: the output value can be set by other drivers, boot loader or may be
1467cf7bbcdfSMark Brown  * hardwired in the regulator.
1468414c70cbSLiam Girdwood  */
1469414c70cbSLiam Girdwood int regulator_enable(struct regulator *regulator)
1470414c70cbSLiam Girdwood {
1471412aec61SDavid Brownell 	struct regulator_dev *rdev = regulator->rdev;
1472412aec61SDavid Brownell 	int ret = 0;
1473414c70cbSLiam Girdwood 
14743801b86aSMark Brown 	if (rdev->supply) {
14753801b86aSMark Brown 		ret = regulator_enable(rdev->supply);
14763801b86aSMark Brown 		if (ret != 0)
14773801b86aSMark Brown 			return ret;
14783801b86aSMark Brown 	}
14793801b86aSMark Brown 
1480412aec61SDavid Brownell 	mutex_lock(&rdev->mutex);
1481412aec61SDavid Brownell 	ret = _regulator_enable(rdev);
1482412aec61SDavid Brownell 	mutex_unlock(&rdev->mutex);
14833801b86aSMark Brown 
1484d1685e4eSHeiko Stübner 	if (ret != 0 && rdev->supply)
14853801b86aSMark Brown 		regulator_disable(rdev->supply);
14863801b86aSMark Brown 
1487414c70cbSLiam Girdwood 	return ret;
1488414c70cbSLiam Girdwood }
1489414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_enable);
1490414c70cbSLiam Girdwood 
1491414c70cbSLiam Girdwood /* locks held by regulator_disable() */
14923801b86aSMark Brown static int _regulator_disable(struct regulator_dev *rdev)
1493414c70cbSLiam Girdwood {
1494414c70cbSLiam Girdwood 	int ret = 0;
1495414c70cbSLiam Girdwood 
1496cd94b505SDavid Brownell 	if (WARN(rdev->use_count <= 0,
149743e7ee33SJoe Perches 		 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1498cd94b505SDavid Brownell 		return -EIO;
1499cd94b505SDavid Brownell 
1500414c70cbSLiam Girdwood 	/* are we the last user and permitted to disable ? */
150160ef66fcSMark Brown 	if (rdev->use_count == 1 &&
150260ef66fcSMark Brown 	    (rdev->constraints && !rdev->constraints->always_on)) {
1503414c70cbSLiam Girdwood 
1504414c70cbSLiam Girdwood 		/* we are last user */
15059a2372faSMark Brown 		if (_regulator_can_change_status(rdev) &&
15069a2372faSMark Brown 		    rdev->desc->ops->disable) {
150702fa3ec0SMark Brown 			trace_regulator_disable(rdev_get_name(rdev));
150802fa3ec0SMark Brown 
1509414c70cbSLiam Girdwood 			ret = rdev->desc->ops->disable(rdev);
1510414c70cbSLiam Girdwood 			if (ret < 0) {
15115da84fd9SJoe Perches 				rdev_err(rdev, "failed to disable\n");
1512414c70cbSLiam Girdwood 				return ret;
1513414c70cbSLiam Girdwood 			}
151484b68263SMark Brown 
151502fa3ec0SMark Brown 			trace_regulator_disable_complete(rdev_get_name(rdev));
151602fa3ec0SMark Brown 
151784b68263SMark Brown 			_notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
151884b68263SMark Brown 					     NULL);
1519414c70cbSLiam Girdwood 		}
1520414c70cbSLiam Girdwood 
1521414c70cbSLiam Girdwood 		rdev->use_count = 0;
1522414c70cbSLiam Girdwood 	} else if (rdev->use_count > 1) {
1523414c70cbSLiam Girdwood 
1524414c70cbSLiam Girdwood 		if (rdev->constraints &&
1525414c70cbSLiam Girdwood 			(rdev->constraints->valid_ops_mask &
1526414c70cbSLiam Girdwood 			REGULATOR_CHANGE_DRMS))
1527414c70cbSLiam Girdwood 			drms_uA_update(rdev);
1528414c70cbSLiam Girdwood 
1529414c70cbSLiam Girdwood 		rdev->use_count--;
1530414c70cbSLiam Girdwood 	}
15313801b86aSMark Brown 
1532414c70cbSLiam Girdwood 	return ret;
1533414c70cbSLiam Girdwood }
1534414c70cbSLiam Girdwood 
1535414c70cbSLiam Girdwood /**
1536414c70cbSLiam Girdwood  * regulator_disable - disable regulator output
1537414c70cbSLiam Girdwood  * @regulator: regulator source
1538414c70cbSLiam Girdwood  *
1539cf7bbcdfSMark Brown  * Disable the regulator output voltage or current.  Calls to
1540cf7bbcdfSMark Brown  * regulator_enable() must be balanced with calls to
1541cf7bbcdfSMark Brown  * regulator_disable().
154269279fb9SMark Brown  *
1543414c70cbSLiam Girdwood  * NOTE: this will only disable the regulator output if no other consumer
1544cf7bbcdfSMark Brown  * devices have it enabled, the regulator device supports disabling and
1545cf7bbcdfSMark Brown  * machine constraints permit this operation.
1546414c70cbSLiam Girdwood  */
1547414c70cbSLiam Girdwood int regulator_disable(struct regulator *regulator)
1548414c70cbSLiam Girdwood {
1549412aec61SDavid Brownell 	struct regulator_dev *rdev = regulator->rdev;
1550412aec61SDavid Brownell 	int ret = 0;
1551414c70cbSLiam Girdwood 
1552412aec61SDavid Brownell 	mutex_lock(&rdev->mutex);
15533801b86aSMark Brown 	ret = _regulator_disable(rdev);
1554412aec61SDavid Brownell 	mutex_unlock(&rdev->mutex);
15558cbf811dSJeffrey Carlyle 
15563801b86aSMark Brown 	if (ret == 0 && rdev->supply)
15573801b86aSMark Brown 		regulator_disable(rdev->supply);
15588cbf811dSJeffrey Carlyle 
1559414c70cbSLiam Girdwood 	return ret;
1560414c70cbSLiam Girdwood }
1561414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_disable);
1562414c70cbSLiam Girdwood 
1563414c70cbSLiam Girdwood /* locks held by regulator_force_disable() */
15643801b86aSMark Brown static int _regulator_force_disable(struct regulator_dev *rdev)
1565414c70cbSLiam Girdwood {
1566414c70cbSLiam Girdwood 	int ret = 0;
1567414c70cbSLiam Girdwood 
1568414c70cbSLiam Girdwood 	/* force disable */
1569414c70cbSLiam Girdwood 	if (rdev->desc->ops->disable) {
1570414c70cbSLiam Girdwood 		/* ah well, who wants to live forever... */
1571414c70cbSLiam Girdwood 		ret = rdev->desc->ops->disable(rdev);
1572414c70cbSLiam Girdwood 		if (ret < 0) {
15735da84fd9SJoe Perches 			rdev_err(rdev, "failed to force disable\n");
1574414c70cbSLiam Girdwood 			return ret;
1575414c70cbSLiam Girdwood 		}
1576414c70cbSLiam Girdwood 		/* notify other consumers that power has been forced off */
157784b68263SMark Brown 		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
157884b68263SMark Brown 			REGULATOR_EVENT_DISABLE, NULL);
1579414c70cbSLiam Girdwood 	}
1580414c70cbSLiam Girdwood 
1581414c70cbSLiam Girdwood 	return ret;
1582414c70cbSLiam Girdwood }
1583414c70cbSLiam Girdwood 
1584414c70cbSLiam Girdwood /**
1585414c70cbSLiam Girdwood  * regulator_force_disable - force disable regulator output
1586414c70cbSLiam Girdwood  * @regulator: regulator source
1587414c70cbSLiam Girdwood  *
1588414c70cbSLiam Girdwood  * Forcibly disable the regulator output voltage or current.
1589414c70cbSLiam Girdwood  * NOTE: this *will* disable the regulator output even if other consumer
1590414c70cbSLiam Girdwood  * devices have it enabled. This should be used for situations when device
1591414c70cbSLiam Girdwood  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1592414c70cbSLiam Girdwood  */
1593414c70cbSLiam Girdwood int regulator_force_disable(struct regulator *regulator)
1594414c70cbSLiam Girdwood {
159582d15839SMark Brown 	struct regulator_dev *rdev = regulator->rdev;
1596414c70cbSLiam Girdwood 	int ret;
1597414c70cbSLiam Girdwood 
159882d15839SMark Brown 	mutex_lock(&rdev->mutex);
1599414c70cbSLiam Girdwood 	regulator->uA_load = 0;
16003801b86aSMark Brown 	ret = _regulator_force_disable(regulator->rdev);
160182d15839SMark Brown 	mutex_unlock(&rdev->mutex);
16028cbf811dSJeffrey Carlyle 
16033801b86aSMark Brown 	if (rdev->supply)
16043801b86aSMark Brown 		while (rdev->open_count--)
16053801b86aSMark Brown 			regulator_disable(rdev->supply);
16068cbf811dSJeffrey Carlyle 
1607414c70cbSLiam Girdwood 	return ret;
1608414c70cbSLiam Girdwood }
1609414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_force_disable);
1610414c70cbSLiam Girdwood 
1611da07ecd9SMark Brown static void regulator_disable_work(struct work_struct *work)
1612da07ecd9SMark Brown {
1613da07ecd9SMark Brown 	struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1614da07ecd9SMark Brown 						  disable_work.work);
1615da07ecd9SMark Brown 	int count, i, ret;
1616da07ecd9SMark Brown 
1617da07ecd9SMark Brown 	mutex_lock(&rdev->mutex);
1618da07ecd9SMark Brown 
1619da07ecd9SMark Brown 	BUG_ON(!rdev->deferred_disables);
1620da07ecd9SMark Brown 
1621da07ecd9SMark Brown 	count = rdev->deferred_disables;
1622da07ecd9SMark Brown 	rdev->deferred_disables = 0;
1623da07ecd9SMark Brown 
1624da07ecd9SMark Brown 	for (i = 0; i < count; i++) {
1625da07ecd9SMark Brown 		ret = _regulator_disable(rdev);
1626da07ecd9SMark Brown 		if (ret != 0)
1627da07ecd9SMark Brown 			rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1628da07ecd9SMark Brown 	}
1629da07ecd9SMark Brown 
1630da07ecd9SMark Brown 	mutex_unlock(&rdev->mutex);
1631da07ecd9SMark Brown 
1632da07ecd9SMark Brown 	if (rdev->supply) {
1633da07ecd9SMark Brown 		for (i = 0; i < count; i++) {
1634da07ecd9SMark Brown 			ret = regulator_disable(rdev->supply);
1635da07ecd9SMark Brown 			if (ret != 0) {
1636da07ecd9SMark Brown 				rdev_err(rdev,
1637da07ecd9SMark Brown 					 "Supply disable failed: %d\n", ret);
1638da07ecd9SMark Brown 			}
1639da07ecd9SMark Brown 		}
1640da07ecd9SMark Brown 	}
1641da07ecd9SMark Brown }
1642da07ecd9SMark Brown 
1643da07ecd9SMark Brown /**
1644da07ecd9SMark Brown  * regulator_disable_deferred - disable regulator output with delay
1645da07ecd9SMark Brown  * @regulator: regulator source
1646da07ecd9SMark Brown  * @ms: miliseconds until the regulator is disabled
1647da07ecd9SMark Brown  *
1648da07ecd9SMark Brown  * Execute regulator_disable() on the regulator after a delay.  This
1649da07ecd9SMark Brown  * is intended for use with devices that require some time to quiesce.
1650da07ecd9SMark Brown  *
1651da07ecd9SMark Brown  * NOTE: this will only disable the regulator output if no other consumer
1652da07ecd9SMark Brown  * devices have it enabled, the regulator device supports disabling and
1653da07ecd9SMark Brown  * machine constraints permit this operation.
1654da07ecd9SMark Brown  */
1655da07ecd9SMark Brown int regulator_disable_deferred(struct regulator *regulator, int ms)
1656da07ecd9SMark Brown {
1657da07ecd9SMark Brown 	struct regulator_dev *rdev = regulator->rdev;
1658aa59802dSMark Brown 	int ret;
1659da07ecd9SMark Brown 
1660da07ecd9SMark Brown 	mutex_lock(&rdev->mutex);
1661da07ecd9SMark Brown 	rdev->deferred_disables++;
1662da07ecd9SMark Brown 	mutex_unlock(&rdev->mutex);
1663da07ecd9SMark Brown 
1664aa59802dSMark Brown 	ret = schedule_delayed_work(&rdev->disable_work,
1665da07ecd9SMark Brown 				    msecs_to_jiffies(ms));
1666aa59802dSMark Brown 	if (ret < 0)
1667aa59802dSMark Brown 		return ret;
1668aa59802dSMark Brown 	else
1669aa59802dSMark Brown 		return 0;
1670da07ecd9SMark Brown }
1671da07ecd9SMark Brown EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1672da07ecd9SMark Brown 
1673414c70cbSLiam Girdwood static int _regulator_is_enabled(struct regulator_dev *rdev)
1674414c70cbSLiam Girdwood {
16759a7f6a4cSMark Brown 	/* If we don't know then assume that the regulator is always on */
16769332546fSMark Brown 	if (!rdev->desc->ops->is_enabled)
16779a7f6a4cSMark Brown 		return 1;
1678414c70cbSLiam Girdwood 
16799332546fSMark Brown 	return rdev->desc->ops->is_enabled(rdev);
1680414c70cbSLiam Girdwood }
1681414c70cbSLiam Girdwood 
1682414c70cbSLiam Girdwood /**
1683414c70cbSLiam Girdwood  * regulator_is_enabled - is the regulator output enabled
1684414c70cbSLiam Girdwood  * @regulator: regulator source
1685414c70cbSLiam Girdwood  *
1686412aec61SDavid Brownell  * Returns positive if the regulator driver backing the source/client
1687412aec61SDavid Brownell  * has requested that the device be enabled, zero if it hasn't, else a
1688412aec61SDavid Brownell  * negative errno code.
1689412aec61SDavid Brownell  *
1690412aec61SDavid Brownell  * Note that the device backing this regulator handle can have multiple
1691412aec61SDavid Brownell  * users, so it might be enabled even if regulator_enable() was never
1692412aec61SDavid Brownell  * called for this particular source.
1693414c70cbSLiam Girdwood  */
1694414c70cbSLiam Girdwood int regulator_is_enabled(struct regulator *regulator)
1695414c70cbSLiam Girdwood {
16969332546fSMark Brown 	int ret;
16979332546fSMark Brown 
16989332546fSMark Brown 	mutex_lock(&regulator->rdev->mutex);
16999332546fSMark Brown 	ret = _regulator_is_enabled(regulator->rdev);
17009332546fSMark Brown 	mutex_unlock(&regulator->rdev->mutex);
17019332546fSMark Brown 
17029332546fSMark Brown 	return ret;
1703414c70cbSLiam Girdwood }
1704414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_is_enabled);
1705414c70cbSLiam Girdwood 
1706414c70cbSLiam Girdwood /**
17074367cfdcSDavid Brownell  * regulator_count_voltages - count regulator_list_voltage() selectors
17084367cfdcSDavid Brownell  * @regulator: regulator source
17094367cfdcSDavid Brownell  *
17104367cfdcSDavid Brownell  * Returns number of selectors, or negative errno.  Selectors are
17114367cfdcSDavid Brownell  * numbered starting at zero, and typically correspond to bitfields
17124367cfdcSDavid Brownell  * in hardware registers.
17134367cfdcSDavid Brownell  */
17144367cfdcSDavid Brownell int regulator_count_voltages(struct regulator *regulator)
17154367cfdcSDavid Brownell {
17164367cfdcSDavid Brownell 	struct regulator_dev	*rdev = regulator->rdev;
17174367cfdcSDavid Brownell 
17184367cfdcSDavid Brownell 	return rdev->desc->n_voltages ? : -EINVAL;
17194367cfdcSDavid Brownell }
17204367cfdcSDavid Brownell EXPORT_SYMBOL_GPL(regulator_count_voltages);
17214367cfdcSDavid Brownell 
17224367cfdcSDavid Brownell /**
17234367cfdcSDavid Brownell  * regulator_list_voltage - enumerate supported voltages
17244367cfdcSDavid Brownell  * @regulator: regulator source
17254367cfdcSDavid Brownell  * @selector: identify voltage to list
17264367cfdcSDavid Brownell  * Context: can sleep
17274367cfdcSDavid Brownell  *
17284367cfdcSDavid Brownell  * Returns a voltage that can be passed to @regulator_set_voltage(),
172988393161SThomas Weber  * zero if this selector code can't be used on this system, or a
17304367cfdcSDavid Brownell  * negative errno.
17314367cfdcSDavid Brownell  */
17324367cfdcSDavid Brownell int regulator_list_voltage(struct regulator *regulator, unsigned selector)
17334367cfdcSDavid Brownell {
17344367cfdcSDavid Brownell 	struct regulator_dev	*rdev = regulator->rdev;
17354367cfdcSDavid Brownell 	struct regulator_ops	*ops = rdev->desc->ops;
17364367cfdcSDavid Brownell 	int			ret;
17374367cfdcSDavid Brownell 
17384367cfdcSDavid Brownell 	if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
17394367cfdcSDavid Brownell 		return -EINVAL;
17404367cfdcSDavid Brownell 
17414367cfdcSDavid Brownell 	mutex_lock(&rdev->mutex);
17424367cfdcSDavid Brownell 	ret = ops->list_voltage(rdev, selector);
17434367cfdcSDavid Brownell 	mutex_unlock(&rdev->mutex);
17444367cfdcSDavid Brownell 
17454367cfdcSDavid Brownell 	if (ret > 0) {
17464367cfdcSDavid Brownell 		if (ret < rdev->constraints->min_uV)
17474367cfdcSDavid Brownell 			ret = 0;
17484367cfdcSDavid Brownell 		else if (ret > rdev->constraints->max_uV)
17494367cfdcSDavid Brownell 			ret = 0;
17504367cfdcSDavid Brownell 	}
17514367cfdcSDavid Brownell 
17524367cfdcSDavid Brownell 	return ret;
17534367cfdcSDavid Brownell }
17544367cfdcSDavid Brownell EXPORT_SYMBOL_GPL(regulator_list_voltage);
17554367cfdcSDavid Brownell 
17564367cfdcSDavid Brownell /**
1757a7a1ad90SMark Brown  * regulator_is_supported_voltage - check if a voltage range can be supported
1758a7a1ad90SMark Brown  *
1759a7a1ad90SMark Brown  * @regulator: Regulator to check.
1760a7a1ad90SMark Brown  * @min_uV: Minimum required voltage in uV.
1761a7a1ad90SMark Brown  * @max_uV: Maximum required voltage in uV.
1762a7a1ad90SMark Brown  *
1763a7a1ad90SMark Brown  * Returns a boolean or a negative error code.
1764a7a1ad90SMark Brown  */
1765a7a1ad90SMark Brown int regulator_is_supported_voltage(struct regulator *regulator,
1766a7a1ad90SMark Brown 				   int min_uV, int max_uV)
1767a7a1ad90SMark Brown {
1768a7a1ad90SMark Brown 	int i, voltages, ret;
1769a7a1ad90SMark Brown 
1770a7a1ad90SMark Brown 	ret = regulator_count_voltages(regulator);
1771a7a1ad90SMark Brown 	if (ret < 0)
1772a7a1ad90SMark Brown 		return ret;
1773a7a1ad90SMark Brown 	voltages = ret;
1774a7a1ad90SMark Brown 
1775a7a1ad90SMark Brown 	for (i = 0; i < voltages; i++) {
1776a7a1ad90SMark Brown 		ret = regulator_list_voltage(regulator, i);
1777a7a1ad90SMark Brown 
1778a7a1ad90SMark Brown 		if (ret >= min_uV && ret <= max_uV)
1779a7a1ad90SMark Brown 			return 1;
1780a7a1ad90SMark Brown 	}
1781a7a1ad90SMark Brown 
1782a7a1ad90SMark Brown 	return 0;
1783a7a1ad90SMark Brown }
1784a398eaa2SMark Brown EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
1785a7a1ad90SMark Brown 
178675790251SMark Brown static int _regulator_do_set_voltage(struct regulator_dev *rdev,
178775790251SMark Brown 				     int min_uV, int max_uV)
178875790251SMark Brown {
178975790251SMark Brown 	int ret;
179077af1b26SLinus Walleij 	int delay = 0;
179175790251SMark Brown 	unsigned int selector;
179275790251SMark Brown 
179375790251SMark Brown 	trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
179475790251SMark Brown 
1795bf5892a8SMark Brown 	min_uV += rdev->constraints->uV_offset;
1796bf5892a8SMark Brown 	max_uV += rdev->constraints->uV_offset;
1797bf5892a8SMark Brown 
179875790251SMark Brown 	if (rdev->desc->ops->set_voltage) {
179975790251SMark Brown 		ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
180075790251SMark Brown 						   &selector);
180175790251SMark Brown 
180275790251SMark Brown 		if (rdev->desc->ops->list_voltage)
180375790251SMark Brown 			selector = rdev->desc->ops->list_voltage(rdev,
180475790251SMark Brown 								 selector);
180575790251SMark Brown 		else
180675790251SMark Brown 			selector = -1;
1807e8eef82bSMark Brown 	} else if (rdev->desc->ops->set_voltage_sel) {
1808e8eef82bSMark Brown 		int best_val = INT_MAX;
1809e8eef82bSMark Brown 		int i;
1810e8eef82bSMark Brown 
1811e8eef82bSMark Brown 		selector = 0;
1812e8eef82bSMark Brown 
1813e8eef82bSMark Brown 		/* Find the smallest voltage that falls within the specified
1814e8eef82bSMark Brown 		 * range.
1815e8eef82bSMark Brown 		 */
1816e8eef82bSMark Brown 		for (i = 0; i < rdev->desc->n_voltages; i++) {
1817e8eef82bSMark Brown 			ret = rdev->desc->ops->list_voltage(rdev, i);
1818e8eef82bSMark Brown 			if (ret < 0)
1819e8eef82bSMark Brown 				continue;
1820e8eef82bSMark Brown 
1821e8eef82bSMark Brown 			if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1822e8eef82bSMark Brown 				best_val = ret;
1823e8eef82bSMark Brown 				selector = i;
1824e8eef82bSMark Brown 			}
1825e8eef82bSMark Brown 		}
1826e8eef82bSMark Brown 
182777af1b26SLinus Walleij 		/*
182877af1b26SLinus Walleij 		 * If we can't obtain the old selector there is not enough
182977af1b26SLinus Walleij 		 * info to call set_voltage_time_sel().
183077af1b26SLinus Walleij 		 */
183177af1b26SLinus Walleij 		if (rdev->desc->ops->set_voltage_time_sel &&
183277af1b26SLinus Walleij 		    rdev->desc->ops->get_voltage_sel) {
183377af1b26SLinus Walleij 			unsigned int old_selector = 0;
183477af1b26SLinus Walleij 
183577af1b26SLinus Walleij 			ret = rdev->desc->ops->get_voltage_sel(rdev);
183677af1b26SLinus Walleij 			if (ret < 0)
183777af1b26SLinus Walleij 				return ret;
183877af1b26SLinus Walleij 			old_selector = ret;
183907351233SAxel Lin 			ret = rdev->desc->ops->set_voltage_time_sel(rdev,
184077af1b26SLinus Walleij 						old_selector, selector);
184107351233SAxel Lin 			if (ret < 0)
184207351233SAxel Lin 				rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
184307351233SAxel Lin 			else
184407351233SAxel Lin 				delay = ret;
184577af1b26SLinus Walleij 		}
184677af1b26SLinus Walleij 
1847e8eef82bSMark Brown 		if (best_val != INT_MAX) {
1848e8eef82bSMark Brown 			ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1849e8eef82bSMark Brown 			selector = best_val;
1850e8eef82bSMark Brown 		} else {
1851e8eef82bSMark Brown 			ret = -EINVAL;
1852e8eef82bSMark Brown 		}
185375790251SMark Brown 	} else {
185475790251SMark Brown 		ret = -EINVAL;
185575790251SMark Brown 	}
185675790251SMark Brown 
185777af1b26SLinus Walleij 	/* Insert any necessary delays */
185877af1b26SLinus Walleij 	if (delay >= 1000) {
185977af1b26SLinus Walleij 		mdelay(delay / 1000);
186077af1b26SLinus Walleij 		udelay(delay % 1000);
186177af1b26SLinus Walleij 	} else if (delay) {
186277af1b26SLinus Walleij 		udelay(delay);
186377af1b26SLinus Walleij 	}
186477af1b26SLinus Walleij 
1865ded06a52SMark Brown 	if (ret == 0)
1866ded06a52SMark Brown 		_notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1867ded06a52SMark Brown 				     NULL);
1868ded06a52SMark Brown 
186975790251SMark Brown 	trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
187075790251SMark Brown 
187175790251SMark Brown 	return ret;
187275790251SMark Brown }
187375790251SMark Brown 
1874a7a1ad90SMark Brown /**
1875414c70cbSLiam Girdwood  * regulator_set_voltage - set regulator output voltage
1876414c70cbSLiam Girdwood  * @regulator: regulator source
1877414c70cbSLiam Girdwood  * @min_uV: Minimum required voltage in uV
1878414c70cbSLiam Girdwood  * @max_uV: Maximum acceptable voltage in uV
1879414c70cbSLiam Girdwood  *
1880414c70cbSLiam Girdwood  * Sets a voltage regulator to the desired output voltage. This can be set
1881414c70cbSLiam Girdwood  * during any regulator state. IOW, regulator can be disabled or enabled.
1882414c70cbSLiam Girdwood  *
1883414c70cbSLiam Girdwood  * If the regulator is enabled then the voltage will change to the new value
1884414c70cbSLiam Girdwood  * immediately otherwise if the regulator is disabled the regulator will
1885414c70cbSLiam Girdwood  * output at the new voltage when enabled.
1886414c70cbSLiam Girdwood  *
1887414c70cbSLiam Girdwood  * NOTE: If the regulator is shared between several devices then the lowest
1888414c70cbSLiam Girdwood  * request voltage that meets the system constraints will be used.
188969279fb9SMark Brown  * Regulator system constraints must be set for this regulator before
1890414c70cbSLiam Girdwood  * calling this function otherwise this call will fail.
1891414c70cbSLiam Girdwood  */
1892414c70cbSLiam Girdwood int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1893414c70cbSLiam Girdwood {
1894414c70cbSLiam Girdwood 	struct regulator_dev *rdev = regulator->rdev;
189595a3c23aSMark Brown 	int ret = 0;
1896414c70cbSLiam Girdwood 
1897414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
1898414c70cbSLiam Girdwood 
189995a3c23aSMark Brown 	/* If we're setting the same range as last time the change
190095a3c23aSMark Brown 	 * should be a noop (some cpufreq implementations use the same
190195a3c23aSMark Brown 	 * voltage for multiple frequencies, for example).
190295a3c23aSMark Brown 	 */
190395a3c23aSMark Brown 	if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
190495a3c23aSMark Brown 		goto out;
190595a3c23aSMark Brown 
1906414c70cbSLiam Girdwood 	/* sanity check */
1907e8eef82bSMark Brown 	if (!rdev->desc->ops->set_voltage &&
1908e8eef82bSMark Brown 	    !rdev->desc->ops->set_voltage_sel) {
1909414c70cbSLiam Girdwood 		ret = -EINVAL;
1910414c70cbSLiam Girdwood 		goto out;
1911414c70cbSLiam Girdwood 	}
1912414c70cbSLiam Girdwood 
1913414c70cbSLiam Girdwood 	/* constraints check */
1914414c70cbSLiam Girdwood 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1915414c70cbSLiam Girdwood 	if (ret < 0)
1916414c70cbSLiam Girdwood 		goto out;
1917414c70cbSLiam Girdwood 	regulator->min_uV = min_uV;
1918414c70cbSLiam Girdwood 	regulator->max_uV = max_uV;
19193a93f2a9SMark Brown 
192005fda3b1SThomas Petazzoni 	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
192105fda3b1SThomas Petazzoni 	if (ret < 0)
192205fda3b1SThomas Petazzoni 		goto out;
192305fda3b1SThomas Petazzoni 
192475790251SMark Brown 	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
192502fa3ec0SMark Brown 
1926414c70cbSLiam Girdwood out:
1927414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
1928414c70cbSLiam Girdwood 	return ret;
1929414c70cbSLiam Girdwood }
1930414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_set_voltage);
1931414c70cbSLiam Girdwood 
1932606a2562SMark Brown /**
193388cd222bSLinus Walleij  * regulator_set_voltage_time - get raise/fall time
193488cd222bSLinus Walleij  * @regulator: regulator source
193588cd222bSLinus Walleij  * @old_uV: starting voltage in microvolts
193688cd222bSLinus Walleij  * @new_uV: target voltage in microvolts
193788cd222bSLinus Walleij  *
193888cd222bSLinus Walleij  * Provided with the starting and ending voltage, this function attempts to
193988cd222bSLinus Walleij  * calculate the time in microseconds required to rise or fall to this new
194088cd222bSLinus Walleij  * voltage.
194188cd222bSLinus Walleij  */
194288cd222bSLinus Walleij int regulator_set_voltage_time(struct regulator *regulator,
194388cd222bSLinus Walleij 			       int old_uV, int new_uV)
194488cd222bSLinus Walleij {
194588cd222bSLinus Walleij 	struct regulator_dev	*rdev = regulator->rdev;
194688cd222bSLinus Walleij 	struct regulator_ops	*ops = rdev->desc->ops;
194788cd222bSLinus Walleij 	int old_sel = -1;
194888cd222bSLinus Walleij 	int new_sel = -1;
194988cd222bSLinus Walleij 	int voltage;
195088cd222bSLinus Walleij 	int i;
195188cd222bSLinus Walleij 
195288cd222bSLinus Walleij 	/* Currently requires operations to do this */
195388cd222bSLinus Walleij 	if (!ops->list_voltage || !ops->set_voltage_time_sel
195488cd222bSLinus Walleij 	    || !rdev->desc->n_voltages)
195588cd222bSLinus Walleij 		return -EINVAL;
195688cd222bSLinus Walleij 
195788cd222bSLinus Walleij 	for (i = 0; i < rdev->desc->n_voltages; i++) {
195888cd222bSLinus Walleij 		/* We only look for exact voltage matches here */
195988cd222bSLinus Walleij 		voltage = regulator_list_voltage(regulator, i);
196088cd222bSLinus Walleij 		if (voltage < 0)
196188cd222bSLinus Walleij 			return -EINVAL;
196288cd222bSLinus Walleij 		if (voltage == 0)
196388cd222bSLinus Walleij 			continue;
196488cd222bSLinus Walleij 		if (voltage == old_uV)
196588cd222bSLinus Walleij 			old_sel = i;
196688cd222bSLinus Walleij 		if (voltage == new_uV)
196788cd222bSLinus Walleij 			new_sel = i;
196888cd222bSLinus Walleij 	}
196988cd222bSLinus Walleij 
197088cd222bSLinus Walleij 	if (old_sel < 0 || new_sel < 0)
197188cd222bSLinus Walleij 		return -EINVAL;
197288cd222bSLinus Walleij 
197388cd222bSLinus Walleij 	return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
197488cd222bSLinus Walleij }
197588cd222bSLinus Walleij EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
197688cd222bSLinus Walleij 
197788cd222bSLinus Walleij /**
1978606a2562SMark Brown  * regulator_sync_voltage - re-apply last regulator output voltage
1979606a2562SMark Brown  * @regulator: regulator source
1980606a2562SMark Brown  *
1981606a2562SMark Brown  * Re-apply the last configured voltage.  This is intended to be used
1982606a2562SMark Brown  * where some external control source the consumer is cooperating with
1983606a2562SMark Brown  * has caused the configured voltage to change.
1984606a2562SMark Brown  */
1985606a2562SMark Brown int regulator_sync_voltage(struct regulator *regulator)
1986606a2562SMark Brown {
1987606a2562SMark Brown 	struct regulator_dev *rdev = regulator->rdev;
1988606a2562SMark Brown 	int ret, min_uV, max_uV;
1989606a2562SMark Brown 
1990606a2562SMark Brown 	mutex_lock(&rdev->mutex);
1991606a2562SMark Brown 
1992606a2562SMark Brown 	if (!rdev->desc->ops->set_voltage &&
1993606a2562SMark Brown 	    !rdev->desc->ops->set_voltage_sel) {
1994606a2562SMark Brown 		ret = -EINVAL;
1995606a2562SMark Brown 		goto out;
1996606a2562SMark Brown 	}
1997606a2562SMark Brown 
1998606a2562SMark Brown 	/* This is only going to work if we've had a voltage configured. */
1999606a2562SMark Brown 	if (!regulator->min_uV && !regulator->max_uV) {
2000606a2562SMark Brown 		ret = -EINVAL;
2001606a2562SMark Brown 		goto out;
2002606a2562SMark Brown 	}
2003606a2562SMark Brown 
2004606a2562SMark Brown 	min_uV = regulator->min_uV;
2005606a2562SMark Brown 	max_uV = regulator->max_uV;
2006606a2562SMark Brown 
2007606a2562SMark Brown 	/* This should be a paranoia check... */
2008606a2562SMark Brown 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2009606a2562SMark Brown 	if (ret < 0)
2010606a2562SMark Brown 		goto out;
2011606a2562SMark Brown 
2012606a2562SMark Brown 	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2013606a2562SMark Brown 	if (ret < 0)
2014606a2562SMark Brown 		goto out;
2015606a2562SMark Brown 
2016606a2562SMark Brown 	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2017606a2562SMark Brown 
2018606a2562SMark Brown out:
2019606a2562SMark Brown 	mutex_unlock(&rdev->mutex);
2020606a2562SMark Brown 	return ret;
2021606a2562SMark Brown }
2022606a2562SMark Brown EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2023606a2562SMark Brown 
2024414c70cbSLiam Girdwood static int _regulator_get_voltage(struct regulator_dev *rdev)
2025414c70cbSLiam Girdwood {
2026bf5892a8SMark Brown 	int sel, ret;
2027476c2d83SMark Brown 
2028476c2d83SMark Brown 	if (rdev->desc->ops->get_voltage_sel) {
2029476c2d83SMark Brown 		sel = rdev->desc->ops->get_voltage_sel(rdev);
2030476c2d83SMark Brown 		if (sel < 0)
2031476c2d83SMark Brown 			return sel;
2032bf5892a8SMark Brown 		ret = rdev->desc->ops->list_voltage(rdev, sel);
2033cb220d16SAxel Lin 	} else if (rdev->desc->ops->get_voltage) {
2034bf5892a8SMark Brown 		ret = rdev->desc->ops->get_voltage(rdev);
2035cb220d16SAxel Lin 	} else {
2036414c70cbSLiam Girdwood 		return -EINVAL;
2037cb220d16SAxel Lin 	}
2038bf5892a8SMark Brown 
2039cb220d16SAxel Lin 	if (ret < 0)
2040cb220d16SAxel Lin 		return ret;
2041bf5892a8SMark Brown 	return ret - rdev->constraints->uV_offset;
2042414c70cbSLiam Girdwood }
2043414c70cbSLiam Girdwood 
2044414c70cbSLiam Girdwood /**
2045414c70cbSLiam Girdwood  * regulator_get_voltage - get regulator output voltage
2046414c70cbSLiam Girdwood  * @regulator: regulator source
2047414c70cbSLiam Girdwood  *
2048414c70cbSLiam Girdwood  * This returns the current regulator voltage in uV.
2049414c70cbSLiam Girdwood  *
2050414c70cbSLiam Girdwood  * NOTE: If the regulator is disabled it will return the voltage value. This
2051414c70cbSLiam Girdwood  * function should not be used to determine regulator state.
2052414c70cbSLiam Girdwood  */
2053414c70cbSLiam Girdwood int regulator_get_voltage(struct regulator *regulator)
2054414c70cbSLiam Girdwood {
2055414c70cbSLiam Girdwood 	int ret;
2056414c70cbSLiam Girdwood 
2057414c70cbSLiam Girdwood 	mutex_lock(&regulator->rdev->mutex);
2058414c70cbSLiam Girdwood 
2059414c70cbSLiam Girdwood 	ret = _regulator_get_voltage(regulator->rdev);
2060414c70cbSLiam Girdwood 
2061414c70cbSLiam Girdwood 	mutex_unlock(&regulator->rdev->mutex);
2062414c70cbSLiam Girdwood 
2063414c70cbSLiam Girdwood 	return ret;
2064414c70cbSLiam Girdwood }
2065414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_voltage);
2066414c70cbSLiam Girdwood 
2067414c70cbSLiam Girdwood /**
2068414c70cbSLiam Girdwood  * regulator_set_current_limit - set regulator output current limit
2069414c70cbSLiam Girdwood  * @regulator: regulator source
2070414c70cbSLiam Girdwood  * @min_uA: Minimuum supported current in uA
2071414c70cbSLiam Girdwood  * @max_uA: Maximum supported current in uA
2072414c70cbSLiam Girdwood  *
2073414c70cbSLiam Girdwood  * Sets current sink to the desired output current. This can be set during
2074414c70cbSLiam Girdwood  * any regulator state. IOW, regulator can be disabled or enabled.
2075414c70cbSLiam Girdwood  *
2076414c70cbSLiam Girdwood  * If the regulator is enabled then the current will change to the new value
2077414c70cbSLiam Girdwood  * immediately otherwise if the regulator is disabled the regulator will
2078414c70cbSLiam Girdwood  * output at the new current when enabled.
2079414c70cbSLiam Girdwood  *
2080414c70cbSLiam Girdwood  * NOTE: Regulator system constraints must be set for this regulator before
2081414c70cbSLiam Girdwood  * calling this function otherwise this call will fail.
2082414c70cbSLiam Girdwood  */
2083414c70cbSLiam Girdwood int regulator_set_current_limit(struct regulator *regulator,
2084414c70cbSLiam Girdwood 			       int min_uA, int max_uA)
2085414c70cbSLiam Girdwood {
2086414c70cbSLiam Girdwood 	struct regulator_dev *rdev = regulator->rdev;
2087414c70cbSLiam Girdwood 	int ret;
2088414c70cbSLiam Girdwood 
2089414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
2090414c70cbSLiam Girdwood 
2091414c70cbSLiam Girdwood 	/* sanity check */
2092414c70cbSLiam Girdwood 	if (!rdev->desc->ops->set_current_limit) {
2093414c70cbSLiam Girdwood 		ret = -EINVAL;
2094414c70cbSLiam Girdwood 		goto out;
2095414c70cbSLiam Girdwood 	}
2096414c70cbSLiam Girdwood 
2097414c70cbSLiam Girdwood 	/* constraints check */
2098414c70cbSLiam Girdwood 	ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2099414c70cbSLiam Girdwood 	if (ret < 0)
2100414c70cbSLiam Girdwood 		goto out;
2101414c70cbSLiam Girdwood 
2102414c70cbSLiam Girdwood 	ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2103414c70cbSLiam Girdwood out:
2104414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
2105414c70cbSLiam Girdwood 	return ret;
2106414c70cbSLiam Girdwood }
2107414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2108414c70cbSLiam Girdwood 
2109414c70cbSLiam Girdwood static int _regulator_get_current_limit(struct regulator_dev *rdev)
2110414c70cbSLiam Girdwood {
2111414c70cbSLiam Girdwood 	int ret;
2112414c70cbSLiam Girdwood 
2113414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
2114414c70cbSLiam Girdwood 
2115414c70cbSLiam Girdwood 	/* sanity check */
2116414c70cbSLiam Girdwood 	if (!rdev->desc->ops->get_current_limit) {
2117414c70cbSLiam Girdwood 		ret = -EINVAL;
2118414c70cbSLiam Girdwood 		goto out;
2119414c70cbSLiam Girdwood 	}
2120414c70cbSLiam Girdwood 
2121414c70cbSLiam Girdwood 	ret = rdev->desc->ops->get_current_limit(rdev);
2122414c70cbSLiam Girdwood out:
2123414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
2124414c70cbSLiam Girdwood 	return ret;
2125414c70cbSLiam Girdwood }
2126414c70cbSLiam Girdwood 
2127414c70cbSLiam Girdwood /**
2128414c70cbSLiam Girdwood  * regulator_get_current_limit - get regulator output current
2129414c70cbSLiam Girdwood  * @regulator: regulator source
2130414c70cbSLiam Girdwood  *
2131414c70cbSLiam Girdwood  * This returns the current supplied by the specified current sink in uA.
2132414c70cbSLiam Girdwood  *
2133414c70cbSLiam Girdwood  * NOTE: If the regulator is disabled it will return the current value. This
2134414c70cbSLiam Girdwood  * function should not be used to determine regulator state.
2135414c70cbSLiam Girdwood  */
2136414c70cbSLiam Girdwood int regulator_get_current_limit(struct regulator *regulator)
2137414c70cbSLiam Girdwood {
2138414c70cbSLiam Girdwood 	return _regulator_get_current_limit(regulator->rdev);
2139414c70cbSLiam Girdwood }
2140414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2141414c70cbSLiam Girdwood 
2142414c70cbSLiam Girdwood /**
2143414c70cbSLiam Girdwood  * regulator_set_mode - set regulator operating mode
2144414c70cbSLiam Girdwood  * @regulator: regulator source
2145414c70cbSLiam Girdwood  * @mode: operating mode - one of the REGULATOR_MODE constants
2146414c70cbSLiam Girdwood  *
2147414c70cbSLiam Girdwood  * Set regulator operating mode to increase regulator efficiency or improve
2148414c70cbSLiam Girdwood  * regulation performance.
2149414c70cbSLiam Girdwood  *
2150414c70cbSLiam Girdwood  * NOTE: Regulator system constraints must be set for this regulator before
2151414c70cbSLiam Girdwood  * calling this function otherwise this call will fail.
2152414c70cbSLiam Girdwood  */
2153414c70cbSLiam Girdwood int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2154414c70cbSLiam Girdwood {
2155414c70cbSLiam Girdwood 	struct regulator_dev *rdev = regulator->rdev;
2156414c70cbSLiam Girdwood 	int ret;
2157500b4ac9SSundar R Iyer 	int regulator_curr_mode;
2158414c70cbSLiam Girdwood 
2159414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
2160414c70cbSLiam Girdwood 
2161414c70cbSLiam Girdwood 	/* sanity check */
2162414c70cbSLiam Girdwood 	if (!rdev->desc->ops->set_mode) {
2163414c70cbSLiam Girdwood 		ret = -EINVAL;
2164414c70cbSLiam Girdwood 		goto out;
2165414c70cbSLiam Girdwood 	}
2166414c70cbSLiam Girdwood 
2167500b4ac9SSundar R Iyer 	/* return if the same mode is requested */
2168500b4ac9SSundar R Iyer 	if (rdev->desc->ops->get_mode) {
2169500b4ac9SSundar R Iyer 		regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2170500b4ac9SSundar R Iyer 		if (regulator_curr_mode == mode) {
2171500b4ac9SSundar R Iyer 			ret = 0;
2172500b4ac9SSundar R Iyer 			goto out;
2173500b4ac9SSundar R Iyer 		}
2174500b4ac9SSundar R Iyer 	}
2175500b4ac9SSundar R Iyer 
2176414c70cbSLiam Girdwood 	/* constraints check */
217722c51b47SAxel Lin 	ret = regulator_mode_constrain(rdev, &mode);
2178414c70cbSLiam Girdwood 	if (ret < 0)
2179414c70cbSLiam Girdwood 		goto out;
2180414c70cbSLiam Girdwood 
2181414c70cbSLiam Girdwood 	ret = rdev->desc->ops->set_mode(rdev, mode);
2182414c70cbSLiam Girdwood out:
2183414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
2184414c70cbSLiam Girdwood 	return ret;
2185414c70cbSLiam Girdwood }
2186414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_set_mode);
2187414c70cbSLiam Girdwood 
2188414c70cbSLiam Girdwood static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2189414c70cbSLiam Girdwood {
2190414c70cbSLiam Girdwood 	int ret;
2191414c70cbSLiam Girdwood 
2192414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
2193414c70cbSLiam Girdwood 
2194414c70cbSLiam Girdwood 	/* sanity check */
2195414c70cbSLiam Girdwood 	if (!rdev->desc->ops->get_mode) {
2196414c70cbSLiam Girdwood 		ret = -EINVAL;
2197414c70cbSLiam Girdwood 		goto out;
2198414c70cbSLiam Girdwood 	}
2199414c70cbSLiam Girdwood 
2200414c70cbSLiam Girdwood 	ret = rdev->desc->ops->get_mode(rdev);
2201414c70cbSLiam Girdwood out:
2202414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
2203414c70cbSLiam Girdwood 	return ret;
2204414c70cbSLiam Girdwood }
2205414c70cbSLiam Girdwood 
2206414c70cbSLiam Girdwood /**
2207414c70cbSLiam Girdwood  * regulator_get_mode - get regulator operating mode
2208414c70cbSLiam Girdwood  * @regulator: regulator source
2209414c70cbSLiam Girdwood  *
2210414c70cbSLiam Girdwood  * Get the current regulator operating mode.
2211414c70cbSLiam Girdwood  */
2212414c70cbSLiam Girdwood unsigned int regulator_get_mode(struct regulator *regulator)
2213414c70cbSLiam Girdwood {
2214414c70cbSLiam Girdwood 	return _regulator_get_mode(regulator->rdev);
2215414c70cbSLiam Girdwood }
2216414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_mode);
2217414c70cbSLiam Girdwood 
2218414c70cbSLiam Girdwood /**
2219414c70cbSLiam Girdwood  * regulator_set_optimum_mode - set regulator optimum operating mode
2220414c70cbSLiam Girdwood  * @regulator: regulator source
2221414c70cbSLiam Girdwood  * @uA_load: load current
2222414c70cbSLiam Girdwood  *
2223414c70cbSLiam Girdwood  * Notifies the regulator core of a new device load. This is then used by
2224414c70cbSLiam Girdwood  * DRMS (if enabled by constraints) to set the most efficient regulator
2225414c70cbSLiam Girdwood  * operating mode for the new regulator loading.
2226414c70cbSLiam Girdwood  *
2227414c70cbSLiam Girdwood  * Consumer devices notify their supply regulator of the maximum power
2228414c70cbSLiam Girdwood  * they will require (can be taken from device datasheet in the power
2229414c70cbSLiam Girdwood  * consumption tables) when they change operational status and hence power
2230414c70cbSLiam Girdwood  * state. Examples of operational state changes that can affect power
2231414c70cbSLiam Girdwood  * consumption are :-
2232414c70cbSLiam Girdwood  *
2233414c70cbSLiam Girdwood  *    o Device is opened / closed.
2234414c70cbSLiam Girdwood  *    o Device I/O is about to begin or has just finished.
2235414c70cbSLiam Girdwood  *    o Device is idling in between work.
2236414c70cbSLiam Girdwood  *
2237414c70cbSLiam Girdwood  * This information is also exported via sysfs to userspace.
2238414c70cbSLiam Girdwood  *
2239414c70cbSLiam Girdwood  * DRMS will sum the total requested load on the regulator and change
2240414c70cbSLiam Girdwood  * to the most efficient operating mode if platform constraints allow.
2241414c70cbSLiam Girdwood  *
2242414c70cbSLiam Girdwood  * Returns the new regulator mode or error.
2243414c70cbSLiam Girdwood  */
2244414c70cbSLiam Girdwood int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2245414c70cbSLiam Girdwood {
2246414c70cbSLiam Girdwood 	struct regulator_dev *rdev = regulator->rdev;
2247414c70cbSLiam Girdwood 	struct regulator *consumer;
2248414c70cbSLiam Girdwood 	int ret, output_uV, input_uV, total_uA_load = 0;
2249414c70cbSLiam Girdwood 	unsigned int mode;
2250414c70cbSLiam Girdwood 
2251414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
2252414c70cbSLiam Girdwood 
2253a4b41483SMark Brown 	/*
2254a4b41483SMark Brown 	 * first check to see if we can set modes at all, otherwise just
2255a4b41483SMark Brown 	 * tell the consumer everything is OK.
2256a4b41483SMark Brown 	 */
2257414c70cbSLiam Girdwood 	regulator->uA_load = uA_load;
2258414c70cbSLiam Girdwood 	ret = regulator_check_drms(rdev);
2259a4b41483SMark Brown 	if (ret < 0) {
2260a4b41483SMark Brown 		ret = 0;
2261414c70cbSLiam Girdwood 		goto out;
2262a4b41483SMark Brown 	}
2263414c70cbSLiam Girdwood 
2264414c70cbSLiam Girdwood 	if (!rdev->desc->ops->get_optimum_mode)
2265414c70cbSLiam Girdwood 		goto out;
2266414c70cbSLiam Girdwood 
2267a4b41483SMark Brown 	/*
2268a4b41483SMark Brown 	 * we can actually do this so any errors are indicators of
2269a4b41483SMark Brown 	 * potential real failure.
2270a4b41483SMark Brown 	 */
2271a4b41483SMark Brown 	ret = -EINVAL;
2272a4b41483SMark Brown 
2273414c70cbSLiam Girdwood 	/* get output voltage */
22741bf5a1f8SMark Brown 	output_uV = _regulator_get_voltage(rdev);
2275414c70cbSLiam Girdwood 	if (output_uV <= 0) {
22765da84fd9SJoe Perches 		rdev_err(rdev, "invalid output voltage found\n");
2277414c70cbSLiam Girdwood 		goto out;
2278414c70cbSLiam Girdwood 	}
2279414c70cbSLiam Girdwood 
2280414c70cbSLiam Girdwood 	/* get input voltage */
22811bf5a1f8SMark Brown 	input_uV = 0;
22821bf5a1f8SMark Brown 	if (rdev->supply)
22833801b86aSMark Brown 		input_uV = regulator_get_voltage(rdev->supply);
22841bf5a1f8SMark Brown 	if (input_uV <= 0)
2285414c70cbSLiam Girdwood 		input_uV = rdev->constraints->input_uV;
2286414c70cbSLiam Girdwood 	if (input_uV <= 0) {
22875da84fd9SJoe Perches 		rdev_err(rdev, "invalid input voltage found\n");
2288414c70cbSLiam Girdwood 		goto out;
2289414c70cbSLiam Girdwood 	}
2290414c70cbSLiam Girdwood 
2291414c70cbSLiam Girdwood 	/* calc total requested load for this regulator */
2292414c70cbSLiam Girdwood 	list_for_each_entry(consumer, &rdev->consumer_list, list)
2293414c70cbSLiam Girdwood 		total_uA_load += consumer->uA_load;
2294414c70cbSLiam Girdwood 
2295414c70cbSLiam Girdwood 	mode = rdev->desc->ops->get_optimum_mode(rdev,
2296414c70cbSLiam Girdwood 						 input_uV, output_uV,
2297414c70cbSLiam Girdwood 						 total_uA_load);
22982c608234SMark Brown 	ret = regulator_mode_constrain(rdev, &mode);
2299e573520bSDavid Brownell 	if (ret < 0) {
23005da84fd9SJoe Perches 		rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2301414c70cbSLiam Girdwood 			 total_uA_load, input_uV, output_uV);
2302414c70cbSLiam Girdwood 		goto out;
2303414c70cbSLiam Girdwood 	}
2304414c70cbSLiam Girdwood 
2305414c70cbSLiam Girdwood 	ret = rdev->desc->ops->set_mode(rdev, mode);
2306e573520bSDavid Brownell 	if (ret < 0) {
23075da84fd9SJoe Perches 		rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2308414c70cbSLiam Girdwood 		goto out;
2309414c70cbSLiam Girdwood 	}
2310414c70cbSLiam Girdwood 	ret = mode;
2311414c70cbSLiam Girdwood out:
2312414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
2313414c70cbSLiam Girdwood 	return ret;
2314414c70cbSLiam Girdwood }
2315414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2316414c70cbSLiam Girdwood 
2317414c70cbSLiam Girdwood /**
2318414c70cbSLiam Girdwood  * regulator_register_notifier - register regulator event notifier
2319414c70cbSLiam Girdwood  * @regulator: regulator source
232069279fb9SMark Brown  * @nb: notifier block
2321414c70cbSLiam Girdwood  *
2322414c70cbSLiam Girdwood  * Register notifier block to receive regulator events.
2323414c70cbSLiam Girdwood  */
2324414c70cbSLiam Girdwood int regulator_register_notifier(struct regulator *regulator,
2325414c70cbSLiam Girdwood 			      struct notifier_block *nb)
2326414c70cbSLiam Girdwood {
2327414c70cbSLiam Girdwood 	return blocking_notifier_chain_register(&regulator->rdev->notifier,
2328414c70cbSLiam Girdwood 						nb);
2329414c70cbSLiam Girdwood }
2330414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_register_notifier);
2331414c70cbSLiam Girdwood 
2332414c70cbSLiam Girdwood /**
2333414c70cbSLiam Girdwood  * regulator_unregister_notifier - unregister regulator event notifier
2334414c70cbSLiam Girdwood  * @regulator: regulator source
233569279fb9SMark Brown  * @nb: notifier block
2336414c70cbSLiam Girdwood  *
2337414c70cbSLiam Girdwood  * Unregister regulator event notifier block.
2338414c70cbSLiam Girdwood  */
2339414c70cbSLiam Girdwood int regulator_unregister_notifier(struct regulator *regulator,
2340414c70cbSLiam Girdwood 				struct notifier_block *nb)
2341414c70cbSLiam Girdwood {
2342414c70cbSLiam Girdwood 	return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2343414c70cbSLiam Girdwood 						  nb);
2344414c70cbSLiam Girdwood }
2345414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2346414c70cbSLiam Girdwood 
2347b136fb44SJonathan Cameron /* notify regulator consumers and downstream regulator consumers.
2348b136fb44SJonathan Cameron  * Note mutex must be held by caller.
2349b136fb44SJonathan Cameron  */
2350414c70cbSLiam Girdwood static void _notifier_call_chain(struct regulator_dev *rdev,
2351414c70cbSLiam Girdwood 				  unsigned long event, void *data)
2352414c70cbSLiam Girdwood {
2353414c70cbSLiam Girdwood 	/* call rdev chain first */
2354414c70cbSLiam Girdwood 	blocking_notifier_call_chain(&rdev->notifier, event, NULL);
2355414c70cbSLiam Girdwood }
2356414c70cbSLiam Girdwood 
2357414c70cbSLiam Girdwood /**
2358414c70cbSLiam Girdwood  * regulator_bulk_get - get multiple regulator consumers
2359414c70cbSLiam Girdwood  *
2360414c70cbSLiam Girdwood  * @dev:           Device to supply
2361414c70cbSLiam Girdwood  * @num_consumers: Number of consumers to register
2362414c70cbSLiam Girdwood  * @consumers:     Configuration of consumers; clients are stored here.
2363414c70cbSLiam Girdwood  *
2364414c70cbSLiam Girdwood  * @return 0 on success, an errno on failure.
2365414c70cbSLiam Girdwood  *
2366414c70cbSLiam Girdwood  * This helper function allows drivers to get several regulator
2367414c70cbSLiam Girdwood  * consumers in one operation.  If any of the regulators cannot be
2368414c70cbSLiam Girdwood  * acquired then any regulators that were allocated will be freed
2369414c70cbSLiam Girdwood  * before returning to the caller.
2370414c70cbSLiam Girdwood  */
2371414c70cbSLiam Girdwood int regulator_bulk_get(struct device *dev, int num_consumers,
2372414c70cbSLiam Girdwood 		       struct regulator_bulk_data *consumers)
2373414c70cbSLiam Girdwood {
2374414c70cbSLiam Girdwood 	int i;
2375414c70cbSLiam Girdwood 	int ret;
2376414c70cbSLiam Girdwood 
2377414c70cbSLiam Girdwood 	for (i = 0; i < num_consumers; i++)
2378414c70cbSLiam Girdwood 		consumers[i].consumer = NULL;
2379414c70cbSLiam Girdwood 
2380414c70cbSLiam Girdwood 	for (i = 0; i < num_consumers; i++) {
2381414c70cbSLiam Girdwood 		consumers[i].consumer = regulator_get(dev,
2382414c70cbSLiam Girdwood 						      consumers[i].supply);
2383414c70cbSLiam Girdwood 		if (IS_ERR(consumers[i].consumer)) {
2384414c70cbSLiam Girdwood 			ret = PTR_ERR(consumers[i].consumer);
23855b307627SMark Brown 			dev_err(dev, "Failed to get supply '%s': %d\n",
23865b307627SMark Brown 				consumers[i].supply, ret);
2387414c70cbSLiam Girdwood 			consumers[i].consumer = NULL;
2388414c70cbSLiam Girdwood 			goto err;
2389414c70cbSLiam Girdwood 		}
2390414c70cbSLiam Girdwood 	}
2391414c70cbSLiam Girdwood 
2392414c70cbSLiam Girdwood 	return 0;
2393414c70cbSLiam Girdwood 
2394414c70cbSLiam Girdwood err:
2395b29c7690SAxel Lin 	while (--i >= 0)
2396414c70cbSLiam Girdwood 		regulator_put(consumers[i].consumer);
2397414c70cbSLiam Girdwood 
2398414c70cbSLiam Girdwood 	return ret;
2399414c70cbSLiam Girdwood }
2400414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_bulk_get);
2401414c70cbSLiam Girdwood 
2402f21e0e81SMark Brown static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2403f21e0e81SMark Brown {
2404f21e0e81SMark Brown 	struct regulator_bulk_data *bulk = data;
2405f21e0e81SMark Brown 
2406f21e0e81SMark Brown 	bulk->ret = regulator_enable(bulk->consumer);
2407f21e0e81SMark Brown }
2408f21e0e81SMark Brown 
2409414c70cbSLiam Girdwood /**
2410414c70cbSLiam Girdwood  * regulator_bulk_enable - enable multiple regulator consumers
2411414c70cbSLiam Girdwood  *
2412414c70cbSLiam Girdwood  * @num_consumers: Number of consumers
2413414c70cbSLiam Girdwood  * @consumers:     Consumer data; clients are stored here.
2414414c70cbSLiam Girdwood  * @return         0 on success, an errno on failure
2415414c70cbSLiam Girdwood  *
2416414c70cbSLiam Girdwood  * This convenience API allows consumers to enable multiple regulator
2417414c70cbSLiam Girdwood  * clients in a single API call.  If any consumers cannot be enabled
2418414c70cbSLiam Girdwood  * then any others that were enabled will be disabled again prior to
2419414c70cbSLiam Girdwood  * return.
2420414c70cbSLiam Girdwood  */
2421414c70cbSLiam Girdwood int regulator_bulk_enable(int num_consumers,
2422414c70cbSLiam Girdwood 			  struct regulator_bulk_data *consumers)
2423414c70cbSLiam Girdwood {
2424f21e0e81SMark Brown 	LIST_HEAD(async_domain);
2425414c70cbSLiam Girdwood 	int i;
2426f21e0e81SMark Brown 	int ret = 0;
2427414c70cbSLiam Girdwood 
2428f21e0e81SMark Brown 	for (i = 0; i < num_consumers; i++)
2429f21e0e81SMark Brown 		async_schedule_domain(regulator_bulk_enable_async,
2430f21e0e81SMark Brown 				      &consumers[i], &async_domain);
2431f21e0e81SMark Brown 
2432f21e0e81SMark Brown 	async_synchronize_full_domain(&async_domain);
2433f21e0e81SMark Brown 
2434f21e0e81SMark Brown 	/* If any consumer failed we need to unwind any that succeeded */
2435414c70cbSLiam Girdwood 	for (i = 0; i < num_consumers; i++) {
2436f21e0e81SMark Brown 		if (consumers[i].ret != 0) {
2437f21e0e81SMark Brown 			ret = consumers[i].ret;
2438414c70cbSLiam Girdwood 			goto err;
2439414c70cbSLiam Girdwood 		}
2440f21e0e81SMark Brown 	}
2441414c70cbSLiam Girdwood 
2442414c70cbSLiam Girdwood 	return 0;
2443414c70cbSLiam Girdwood 
2444414c70cbSLiam Girdwood err:
2445b29c7690SAxel Lin 	pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2446b29c7690SAxel Lin 	while (--i >= 0)
2447414c70cbSLiam Girdwood 		regulator_disable(consumers[i].consumer);
2448414c70cbSLiam Girdwood 
2449414c70cbSLiam Girdwood 	return ret;
2450414c70cbSLiam Girdwood }
2451414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2452414c70cbSLiam Girdwood 
2453414c70cbSLiam Girdwood /**
2454414c70cbSLiam Girdwood  * regulator_bulk_disable - disable multiple regulator consumers
2455414c70cbSLiam Girdwood  *
2456414c70cbSLiam Girdwood  * @num_consumers: Number of consumers
2457414c70cbSLiam Girdwood  * @consumers:     Consumer data; clients are stored here.
2458414c70cbSLiam Girdwood  * @return         0 on success, an errno on failure
2459414c70cbSLiam Girdwood  *
2460414c70cbSLiam Girdwood  * This convenience API allows consumers to disable multiple regulator
246149e22632SSylwester Nawrocki  * clients in a single API call.  If any consumers cannot be disabled
246249e22632SSylwester Nawrocki  * then any others that were disabled will be enabled again prior to
2463414c70cbSLiam Girdwood  * return.
2464414c70cbSLiam Girdwood  */
2465414c70cbSLiam Girdwood int regulator_bulk_disable(int num_consumers,
2466414c70cbSLiam Girdwood 			   struct regulator_bulk_data *consumers)
2467414c70cbSLiam Girdwood {
2468414c70cbSLiam Girdwood 	int i;
2469414c70cbSLiam Girdwood 	int ret;
2470414c70cbSLiam Girdwood 
247149e22632SSylwester Nawrocki 	for (i = num_consumers - 1; i >= 0; --i) {
2472414c70cbSLiam Girdwood 		ret = regulator_disable(consumers[i].consumer);
2473414c70cbSLiam Girdwood 		if (ret != 0)
2474414c70cbSLiam Girdwood 			goto err;
2475414c70cbSLiam Girdwood 	}
2476414c70cbSLiam Girdwood 
2477414c70cbSLiam Girdwood 	return 0;
2478414c70cbSLiam Girdwood 
2479414c70cbSLiam Girdwood err:
24805da84fd9SJoe Perches 	pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
248149e22632SSylwester Nawrocki 	for (++i; i < num_consumers; ++i)
2482414c70cbSLiam Girdwood 		regulator_enable(consumers[i].consumer);
2483414c70cbSLiam Girdwood 
2484414c70cbSLiam Girdwood 	return ret;
2485414c70cbSLiam Girdwood }
2486414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2487414c70cbSLiam Girdwood 
2488414c70cbSLiam Girdwood /**
2489e1de2f42SDonggeun Kim  * regulator_bulk_force_disable - force disable multiple regulator consumers
2490e1de2f42SDonggeun Kim  *
2491e1de2f42SDonggeun Kim  * @num_consumers: Number of consumers
2492e1de2f42SDonggeun Kim  * @consumers:     Consumer data; clients are stored here.
2493e1de2f42SDonggeun Kim  * @return         0 on success, an errno on failure
2494e1de2f42SDonggeun Kim  *
2495e1de2f42SDonggeun Kim  * This convenience API allows consumers to forcibly disable multiple regulator
2496e1de2f42SDonggeun Kim  * clients in a single API call.
2497e1de2f42SDonggeun Kim  * NOTE: This should be used for situations when device damage will
2498e1de2f42SDonggeun Kim  * likely occur if the regulators are not disabled (e.g. over temp).
2499e1de2f42SDonggeun Kim  * Although regulator_force_disable function call for some consumers can
2500e1de2f42SDonggeun Kim  * return error numbers, the function is called for all consumers.
2501e1de2f42SDonggeun Kim  */
2502e1de2f42SDonggeun Kim int regulator_bulk_force_disable(int num_consumers,
2503e1de2f42SDonggeun Kim 			   struct regulator_bulk_data *consumers)
2504e1de2f42SDonggeun Kim {
2505e1de2f42SDonggeun Kim 	int i;
2506e1de2f42SDonggeun Kim 	int ret;
2507e1de2f42SDonggeun Kim 
2508e1de2f42SDonggeun Kim 	for (i = 0; i < num_consumers; i++)
2509e1de2f42SDonggeun Kim 		consumers[i].ret =
2510e1de2f42SDonggeun Kim 			    regulator_force_disable(consumers[i].consumer);
2511e1de2f42SDonggeun Kim 
2512e1de2f42SDonggeun Kim 	for (i = 0; i < num_consumers; i++) {
2513e1de2f42SDonggeun Kim 		if (consumers[i].ret != 0) {
2514e1de2f42SDonggeun Kim 			ret = consumers[i].ret;
2515e1de2f42SDonggeun Kim 			goto out;
2516e1de2f42SDonggeun Kim 		}
2517e1de2f42SDonggeun Kim 	}
2518e1de2f42SDonggeun Kim 
2519e1de2f42SDonggeun Kim 	return 0;
2520e1de2f42SDonggeun Kim out:
2521e1de2f42SDonggeun Kim 	return ret;
2522e1de2f42SDonggeun Kim }
2523e1de2f42SDonggeun Kim EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2524e1de2f42SDonggeun Kim 
2525e1de2f42SDonggeun Kim /**
2526414c70cbSLiam Girdwood  * regulator_bulk_free - free multiple regulator consumers
2527414c70cbSLiam Girdwood  *
2528414c70cbSLiam Girdwood  * @num_consumers: Number of consumers
2529414c70cbSLiam Girdwood  * @consumers:     Consumer data; clients are stored here.
2530414c70cbSLiam Girdwood  *
2531414c70cbSLiam Girdwood  * This convenience API allows consumers to free multiple regulator
2532414c70cbSLiam Girdwood  * clients in a single API call.
2533414c70cbSLiam Girdwood  */
2534414c70cbSLiam Girdwood void regulator_bulk_free(int num_consumers,
2535414c70cbSLiam Girdwood 			 struct regulator_bulk_data *consumers)
2536414c70cbSLiam Girdwood {
2537414c70cbSLiam Girdwood 	int i;
2538414c70cbSLiam Girdwood 
2539414c70cbSLiam Girdwood 	for (i = 0; i < num_consumers; i++) {
2540414c70cbSLiam Girdwood 		regulator_put(consumers[i].consumer);
2541414c70cbSLiam Girdwood 		consumers[i].consumer = NULL;
2542414c70cbSLiam Girdwood 	}
2543414c70cbSLiam Girdwood }
2544414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_bulk_free);
2545414c70cbSLiam Girdwood 
2546414c70cbSLiam Girdwood /**
2547414c70cbSLiam Girdwood  * regulator_notifier_call_chain - call regulator event notifier
254869279fb9SMark Brown  * @rdev: regulator source
2549414c70cbSLiam Girdwood  * @event: notifier block
255069279fb9SMark Brown  * @data: callback-specific data.
2551414c70cbSLiam Girdwood  *
2552414c70cbSLiam Girdwood  * Called by regulator drivers to notify clients a regulator event has
2553414c70cbSLiam Girdwood  * occurred. We also notify regulator clients downstream.
2554b136fb44SJonathan Cameron  * Note lock must be held by caller.
2555414c70cbSLiam Girdwood  */
2556414c70cbSLiam Girdwood int regulator_notifier_call_chain(struct regulator_dev *rdev,
2557414c70cbSLiam Girdwood 				  unsigned long event, void *data)
2558414c70cbSLiam Girdwood {
2559414c70cbSLiam Girdwood 	_notifier_call_chain(rdev, event, data);
2560414c70cbSLiam Girdwood 	return NOTIFY_DONE;
2561414c70cbSLiam Girdwood 
2562414c70cbSLiam Girdwood }
2563414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2564414c70cbSLiam Girdwood 
2565be721979SMark Brown /**
2566be721979SMark Brown  * regulator_mode_to_status - convert a regulator mode into a status
2567be721979SMark Brown  *
2568be721979SMark Brown  * @mode: Mode to convert
2569be721979SMark Brown  *
2570be721979SMark Brown  * Convert a regulator mode into a status.
2571be721979SMark Brown  */
2572be721979SMark Brown int regulator_mode_to_status(unsigned int mode)
2573be721979SMark Brown {
2574be721979SMark Brown 	switch (mode) {
2575be721979SMark Brown 	case REGULATOR_MODE_FAST:
2576be721979SMark Brown 		return REGULATOR_STATUS_FAST;
2577be721979SMark Brown 	case REGULATOR_MODE_NORMAL:
2578be721979SMark Brown 		return REGULATOR_STATUS_NORMAL;
2579be721979SMark Brown 	case REGULATOR_MODE_IDLE:
2580be721979SMark Brown 		return REGULATOR_STATUS_IDLE;
2581be721979SMark Brown 	case REGULATOR_STATUS_STANDBY:
2582be721979SMark Brown 		return REGULATOR_STATUS_STANDBY;
2583be721979SMark Brown 	default:
2584be721979SMark Brown 		return 0;
2585be721979SMark Brown 	}
2586be721979SMark Brown }
2587be721979SMark Brown EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2588be721979SMark Brown 
25897ad68e2fSDavid Brownell /*
25907ad68e2fSDavid Brownell  * To avoid cluttering sysfs (and memory) with useless state, only
25917ad68e2fSDavid Brownell  * create attributes that can be meaningfully displayed.
25927ad68e2fSDavid Brownell  */
25937ad68e2fSDavid Brownell static int add_regulator_attributes(struct regulator_dev *rdev)
25947ad68e2fSDavid Brownell {
25957ad68e2fSDavid Brownell 	struct device		*dev = &rdev->dev;
25967ad68e2fSDavid Brownell 	struct regulator_ops	*ops = rdev->desc->ops;
25977ad68e2fSDavid Brownell 	int			status = 0;
25987ad68e2fSDavid Brownell 
25997ad68e2fSDavid Brownell 	/* some attributes need specific methods to be displayed */
26004c78899bSMark Brown 	if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
26014c78899bSMark Brown 	    (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
26027ad68e2fSDavid Brownell 		status = device_create_file(dev, &dev_attr_microvolts);
26037ad68e2fSDavid Brownell 		if (status < 0)
26047ad68e2fSDavid Brownell 			return status;
26057ad68e2fSDavid Brownell 	}
26067ad68e2fSDavid Brownell 	if (ops->get_current_limit) {
26077ad68e2fSDavid Brownell 		status = device_create_file(dev, &dev_attr_microamps);
26087ad68e2fSDavid Brownell 		if (status < 0)
26097ad68e2fSDavid Brownell 			return status;
26107ad68e2fSDavid Brownell 	}
26117ad68e2fSDavid Brownell 	if (ops->get_mode) {
26127ad68e2fSDavid Brownell 		status = device_create_file(dev, &dev_attr_opmode);
26137ad68e2fSDavid Brownell 		if (status < 0)
26147ad68e2fSDavid Brownell 			return status;
26157ad68e2fSDavid Brownell 	}
26167ad68e2fSDavid Brownell 	if (ops->is_enabled) {
26177ad68e2fSDavid Brownell 		status = device_create_file(dev, &dev_attr_state);
26187ad68e2fSDavid Brownell 		if (status < 0)
26197ad68e2fSDavid Brownell 			return status;
26207ad68e2fSDavid Brownell 	}
2621853116a1SDavid Brownell 	if (ops->get_status) {
2622853116a1SDavid Brownell 		status = device_create_file(dev, &dev_attr_status);
2623853116a1SDavid Brownell 		if (status < 0)
2624853116a1SDavid Brownell 			return status;
2625853116a1SDavid Brownell 	}
26267ad68e2fSDavid Brownell 
26277ad68e2fSDavid Brownell 	/* some attributes are type-specific */
26287ad68e2fSDavid Brownell 	if (rdev->desc->type == REGULATOR_CURRENT) {
26297ad68e2fSDavid Brownell 		status = device_create_file(dev, &dev_attr_requested_microamps);
26307ad68e2fSDavid Brownell 		if (status < 0)
26317ad68e2fSDavid Brownell 			return status;
26327ad68e2fSDavid Brownell 	}
26337ad68e2fSDavid Brownell 
26347ad68e2fSDavid Brownell 	/* all the other attributes exist to support constraints;
26357ad68e2fSDavid Brownell 	 * don't show them if there are no constraints, or if the
26367ad68e2fSDavid Brownell 	 * relevant supporting methods are missing.
26377ad68e2fSDavid Brownell 	 */
26387ad68e2fSDavid Brownell 	if (!rdev->constraints)
26397ad68e2fSDavid Brownell 		return status;
26407ad68e2fSDavid Brownell 
26417ad68e2fSDavid Brownell 	/* constraints need specific supporting methods */
2642e8eef82bSMark Brown 	if (ops->set_voltage || ops->set_voltage_sel) {
26437ad68e2fSDavid Brownell 		status = device_create_file(dev, &dev_attr_min_microvolts);
26447ad68e2fSDavid Brownell 		if (status < 0)
26457ad68e2fSDavid Brownell 			return status;
26467ad68e2fSDavid Brownell 		status = device_create_file(dev, &dev_attr_max_microvolts);
26477ad68e2fSDavid Brownell 		if (status < 0)
26487ad68e2fSDavid Brownell 			return status;
26497ad68e2fSDavid Brownell 	}
26507ad68e2fSDavid Brownell 	if (ops->set_current_limit) {
26517ad68e2fSDavid Brownell 		status = device_create_file(dev, &dev_attr_min_microamps);
26527ad68e2fSDavid Brownell 		if (status < 0)
26537ad68e2fSDavid Brownell 			return status;
26547ad68e2fSDavid Brownell 		status = device_create_file(dev, &dev_attr_max_microamps);
26557ad68e2fSDavid Brownell 		if (status < 0)
26567ad68e2fSDavid Brownell 			return status;
26577ad68e2fSDavid Brownell 	}
26587ad68e2fSDavid Brownell 
26597ad68e2fSDavid Brownell 	/* suspend mode constraints need multiple supporting methods */
26607ad68e2fSDavid Brownell 	if (!(ops->set_suspend_enable && ops->set_suspend_disable))
26617ad68e2fSDavid Brownell 		return status;
26627ad68e2fSDavid Brownell 
26637ad68e2fSDavid Brownell 	status = device_create_file(dev, &dev_attr_suspend_standby_state);
26647ad68e2fSDavid Brownell 	if (status < 0)
26657ad68e2fSDavid Brownell 		return status;
26667ad68e2fSDavid Brownell 	status = device_create_file(dev, &dev_attr_suspend_mem_state);
26677ad68e2fSDavid Brownell 	if (status < 0)
26687ad68e2fSDavid Brownell 		return status;
26697ad68e2fSDavid Brownell 	status = device_create_file(dev, &dev_attr_suspend_disk_state);
26707ad68e2fSDavid Brownell 	if (status < 0)
26717ad68e2fSDavid Brownell 		return status;
26727ad68e2fSDavid Brownell 
26737ad68e2fSDavid Brownell 	if (ops->set_suspend_voltage) {
26747ad68e2fSDavid Brownell 		status = device_create_file(dev,
26757ad68e2fSDavid Brownell 				&dev_attr_suspend_standby_microvolts);
26767ad68e2fSDavid Brownell 		if (status < 0)
26777ad68e2fSDavid Brownell 			return status;
26787ad68e2fSDavid Brownell 		status = device_create_file(dev,
26797ad68e2fSDavid Brownell 				&dev_attr_suspend_mem_microvolts);
26807ad68e2fSDavid Brownell 		if (status < 0)
26817ad68e2fSDavid Brownell 			return status;
26827ad68e2fSDavid Brownell 		status = device_create_file(dev,
26837ad68e2fSDavid Brownell 				&dev_attr_suspend_disk_microvolts);
26847ad68e2fSDavid Brownell 		if (status < 0)
26857ad68e2fSDavid Brownell 			return status;
26867ad68e2fSDavid Brownell 	}
26877ad68e2fSDavid Brownell 
26887ad68e2fSDavid Brownell 	if (ops->set_suspend_mode) {
26897ad68e2fSDavid Brownell 		status = device_create_file(dev,
26907ad68e2fSDavid Brownell 				&dev_attr_suspend_standby_mode);
26917ad68e2fSDavid Brownell 		if (status < 0)
26927ad68e2fSDavid Brownell 			return status;
26937ad68e2fSDavid Brownell 		status = device_create_file(dev,
26947ad68e2fSDavid Brownell 				&dev_attr_suspend_mem_mode);
26957ad68e2fSDavid Brownell 		if (status < 0)
26967ad68e2fSDavid Brownell 			return status;
26977ad68e2fSDavid Brownell 		status = device_create_file(dev,
26987ad68e2fSDavid Brownell 				&dev_attr_suspend_disk_mode);
26997ad68e2fSDavid Brownell 		if (status < 0)
27007ad68e2fSDavid Brownell 			return status;
27017ad68e2fSDavid Brownell 	}
27027ad68e2fSDavid Brownell 
27037ad68e2fSDavid Brownell 	return status;
27047ad68e2fSDavid Brownell }
27057ad68e2fSDavid Brownell 
27061130e5b3SMark Brown static void rdev_init_debugfs(struct regulator_dev *rdev)
27071130e5b3SMark Brown {
27081130e5b3SMark Brown 	rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
270924751434SStephen Boyd 	if (!rdev->debugfs) {
27101130e5b3SMark Brown 		rdev_warn(rdev, "Failed to create debugfs directory\n");
27111130e5b3SMark Brown 		return;
27121130e5b3SMark Brown 	}
27131130e5b3SMark Brown 
27141130e5b3SMark Brown 	debugfs_create_u32("use_count", 0444, rdev->debugfs,
27151130e5b3SMark Brown 			   &rdev->use_count);
27161130e5b3SMark Brown 	debugfs_create_u32("open_count", 0444, rdev->debugfs,
27171130e5b3SMark Brown 			   &rdev->open_count);
27181130e5b3SMark Brown }
27191130e5b3SMark Brown 
2720414c70cbSLiam Girdwood /**
2721414c70cbSLiam Girdwood  * regulator_register - register regulator
272269279fb9SMark Brown  * @regulator_desc: regulator to register
272369279fb9SMark Brown  * @dev: struct device for the regulator
27240527100fSMark Brown  * @init_data: platform provided init data, passed through by driver
272569279fb9SMark Brown  * @driver_data: private regulator data
2726414c70cbSLiam Girdwood  *
2727414c70cbSLiam Girdwood  * Called by regulator drivers to register a regulator.
2728414c70cbSLiam Girdwood  * Returns 0 on success.
2729414c70cbSLiam Girdwood  */
2730414c70cbSLiam Girdwood struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2731f8c12fe3SMark Brown 	struct device *dev, const struct regulator_init_data *init_data,
27322c043bcbSRajendra Nayak 	void *driver_data, struct device_node *of_node)
2733414c70cbSLiam Girdwood {
27349a8f5e07SMark Brown 	const struct regulation_constraints *constraints = NULL;
2735414c70cbSLiam Girdwood 	static atomic_t regulator_no = ATOMIC_INIT(0);
2736414c70cbSLiam Girdwood 	struct regulator_dev *rdev;
2737a5766f11SLiam Girdwood 	int ret, i;
273869511a45SRajendra Nayak 	const char *supply = NULL;
2739414c70cbSLiam Girdwood 
2740414c70cbSLiam Girdwood 	if (regulator_desc == NULL)
2741414c70cbSLiam Girdwood 		return ERR_PTR(-EINVAL);
2742414c70cbSLiam Girdwood 
2743414c70cbSLiam Girdwood 	if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2744414c70cbSLiam Girdwood 		return ERR_PTR(-EINVAL);
2745414c70cbSLiam Girdwood 
2746cd78dfc6SDiego Liziero 	if (regulator_desc->type != REGULATOR_VOLTAGE &&
2747cd78dfc6SDiego Liziero 	    regulator_desc->type != REGULATOR_CURRENT)
2748414c70cbSLiam Girdwood 		return ERR_PTR(-EINVAL);
2749414c70cbSLiam Girdwood 
2750476c2d83SMark Brown 	/* Only one of each should be implemented */
2751476c2d83SMark Brown 	WARN_ON(regulator_desc->ops->get_voltage &&
2752476c2d83SMark Brown 		regulator_desc->ops->get_voltage_sel);
2753e8eef82bSMark Brown 	WARN_ON(regulator_desc->ops->set_voltage &&
2754e8eef82bSMark Brown 		regulator_desc->ops->set_voltage_sel);
2755476c2d83SMark Brown 
2756476c2d83SMark Brown 	/* If we're using selectors we must implement list_voltage. */
2757476c2d83SMark Brown 	if (regulator_desc->ops->get_voltage_sel &&
2758476c2d83SMark Brown 	    !regulator_desc->ops->list_voltage) {
2759476c2d83SMark Brown 		return ERR_PTR(-EINVAL);
2760476c2d83SMark Brown 	}
2761e8eef82bSMark Brown 	if (regulator_desc->ops->set_voltage_sel &&
2762e8eef82bSMark Brown 	    !regulator_desc->ops->list_voltage) {
2763e8eef82bSMark Brown 		return ERR_PTR(-EINVAL);
2764e8eef82bSMark Brown 	}
2765476c2d83SMark Brown 
2766414c70cbSLiam Girdwood 	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2767414c70cbSLiam Girdwood 	if (rdev == NULL)
2768414c70cbSLiam Girdwood 		return ERR_PTR(-ENOMEM);
2769414c70cbSLiam Girdwood 
2770414c70cbSLiam Girdwood 	mutex_lock(&regulator_list_mutex);
2771414c70cbSLiam Girdwood 
2772414c70cbSLiam Girdwood 	mutex_init(&rdev->mutex);
2773a5766f11SLiam Girdwood 	rdev->reg_data = driver_data;
2774414c70cbSLiam Girdwood 	rdev->owner = regulator_desc->owner;
2775414c70cbSLiam Girdwood 	rdev->desc = regulator_desc;
2776414c70cbSLiam Girdwood 	INIT_LIST_HEAD(&rdev->consumer_list);
2777414c70cbSLiam Girdwood 	INIT_LIST_HEAD(&rdev->list);
2778414c70cbSLiam Girdwood 	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2779da07ecd9SMark Brown 	INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
2780414c70cbSLiam Girdwood 
2781a5766f11SLiam Girdwood 	/* preform any regulator specific init */
27829a8f5e07SMark Brown 	if (init_data && init_data->regulator_init) {
2783a5766f11SLiam Girdwood 		ret = init_data->regulator_init(rdev->reg_data);
27844fca9545SDavid Brownell 		if (ret < 0)
27854fca9545SDavid Brownell 			goto clean;
2786a5766f11SLiam Girdwood 	}
2787a5766f11SLiam Girdwood 
2788a5766f11SLiam Girdwood 	/* register with sysfs */
2789a5766f11SLiam Girdwood 	rdev->dev.class = &regulator_class;
27902c043bcbSRajendra Nayak 	rdev->dev.of_node = of_node;
2791a5766f11SLiam Girdwood 	rdev->dev.parent = dev;
2792812460a9SKay Sievers 	dev_set_name(&rdev->dev, "regulator.%d",
2793812460a9SKay Sievers 		     atomic_inc_return(&regulator_no) - 1);
2794a5766f11SLiam Girdwood 	ret = device_register(&rdev->dev);
2795ad7725cbSVasiliy Kulikov 	if (ret != 0) {
2796ad7725cbSVasiliy Kulikov 		put_device(&rdev->dev);
27974fca9545SDavid Brownell 		goto clean;
2798ad7725cbSVasiliy Kulikov 	}
2799a5766f11SLiam Girdwood 
2800a5766f11SLiam Girdwood 	dev_set_drvdata(&rdev->dev, rdev);
2801a5766f11SLiam Girdwood 
280274f544c1SMike Rapoport 	/* set regulator constraints */
28039a8f5e07SMark Brown 	if (init_data)
28049a8f5e07SMark Brown 		constraints = &init_data->constraints;
28059a8f5e07SMark Brown 
28069a8f5e07SMark Brown 	ret = set_machine_constraints(rdev, constraints);
280774f544c1SMike Rapoport 	if (ret < 0)
280874f544c1SMike Rapoport 		goto scrub;
280974f544c1SMike Rapoport 
28107ad68e2fSDavid Brownell 	/* add attributes supported by this regulator */
28117ad68e2fSDavid Brownell 	ret = add_regulator_attributes(rdev);
28127ad68e2fSDavid Brownell 	if (ret < 0)
28137ad68e2fSDavid Brownell 		goto scrub;
28147ad68e2fSDavid Brownell 
28159a8f5e07SMark Brown 	if (init_data && init_data->supply_regulator)
281669511a45SRajendra Nayak 		supply = init_data->supply_regulator;
281769511a45SRajendra Nayak 	else if (regulator_desc->supply_name)
281869511a45SRajendra Nayak 		supply = regulator_desc->supply_name;
281969511a45SRajendra Nayak 
282069511a45SRajendra Nayak 	if (supply) {
28210178f3e2SMark Brown 		struct regulator_dev *r;
28220178f3e2SMark Brown 
282369511a45SRajendra Nayak 		r = regulator_dev_lookup(dev, supply);
28240178f3e2SMark Brown 
282569511a45SRajendra Nayak 		if (!r) {
282669511a45SRajendra Nayak 			dev_err(dev, "Failed to find supply %s\n", supply);
28277727da22SAxel Lin 			ret = -ENODEV;
28280178f3e2SMark Brown 			goto scrub;
28290178f3e2SMark Brown 		}
28300178f3e2SMark Brown 
28310178f3e2SMark Brown 		ret = set_supply(rdev, r);
28320178f3e2SMark Brown 		if (ret < 0)
28330178f3e2SMark Brown 			goto scrub;
2834b2296bd4SLaxman Dewangan 
2835b2296bd4SLaxman Dewangan 		/* Enable supply if rail is enabled */
2836b2296bd4SLaxman Dewangan 		if (rdev->desc->ops->is_enabled &&
2837b2296bd4SLaxman Dewangan 				rdev->desc->ops->is_enabled(rdev)) {
2838b2296bd4SLaxman Dewangan 			ret = regulator_enable(rdev->supply);
2839b2296bd4SLaxman Dewangan 			if (ret < 0)
2840b2296bd4SLaxman Dewangan 				goto scrub;
2841b2296bd4SLaxman Dewangan 		}
28420178f3e2SMark Brown 	}
28430178f3e2SMark Brown 
2844a5766f11SLiam Girdwood 	/* add consumers devices */
28459a8f5e07SMark Brown 	if (init_data) {
2846a5766f11SLiam Girdwood 		for (i = 0; i < init_data->num_consumer_supplies; i++) {
2847a5766f11SLiam Girdwood 			ret = set_consumer_device_supply(rdev,
2848a5766f11SLiam Girdwood 				init_data->consumer_supplies[i].dev,
284940f9244fSMark Brown 				init_data->consumer_supplies[i].dev_name,
2850a5766f11SLiam Girdwood 				init_data->consumer_supplies[i].supply);
285123c2f041SMark Brown 			if (ret < 0) {
285223c2f041SMark Brown 				dev_err(dev, "Failed to set supply %s\n",
285323c2f041SMark Brown 					init_data->consumer_supplies[i].supply);
2854d4033b54SJani Nikula 				goto unset_supplies;
2855a5766f11SLiam Girdwood 			}
285623c2f041SMark Brown 		}
28579a8f5e07SMark Brown 	}
2858a5766f11SLiam Girdwood 
2859a5766f11SLiam Girdwood 	list_add(&rdev->list, &regulator_list);
28601130e5b3SMark Brown 
28611130e5b3SMark Brown 	rdev_init_debugfs(rdev);
2862a5766f11SLiam Girdwood out:
2863414c70cbSLiam Girdwood 	mutex_unlock(&regulator_list_mutex);
2864414c70cbSLiam Girdwood 	return rdev;
28654fca9545SDavid Brownell 
2866d4033b54SJani Nikula unset_supplies:
2867d4033b54SJani Nikula 	unset_regulator_supplies(rdev);
2868d4033b54SJani Nikula 
28694fca9545SDavid Brownell scrub:
28701a6958e7SAxel Lin 	kfree(rdev->constraints);
28714fca9545SDavid Brownell 	device_unregister(&rdev->dev);
287253032dafSPaul Walmsley 	/* device core frees rdev */
287353032dafSPaul Walmsley 	rdev = ERR_PTR(ret);
287453032dafSPaul Walmsley 	goto out;
287553032dafSPaul Walmsley 
28764fca9545SDavid Brownell clean:
28774fca9545SDavid Brownell 	kfree(rdev);
28784fca9545SDavid Brownell 	rdev = ERR_PTR(ret);
28794fca9545SDavid Brownell 	goto out;
2880414c70cbSLiam Girdwood }
2881414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_register);
2882414c70cbSLiam Girdwood 
2883414c70cbSLiam Girdwood /**
2884414c70cbSLiam Girdwood  * regulator_unregister - unregister regulator
288569279fb9SMark Brown  * @rdev: regulator to unregister
2886414c70cbSLiam Girdwood  *
2887414c70cbSLiam Girdwood  * Called by regulator drivers to unregister a regulator.
2888414c70cbSLiam Girdwood  */
2889414c70cbSLiam Girdwood void regulator_unregister(struct regulator_dev *rdev)
2890414c70cbSLiam Girdwood {
2891414c70cbSLiam Girdwood 	if (rdev == NULL)
2892414c70cbSLiam Girdwood 		return;
2893414c70cbSLiam Girdwood 
2894414c70cbSLiam Girdwood 	mutex_lock(&regulator_list_mutex);
28951130e5b3SMark Brown 	debugfs_remove_recursive(rdev->debugfs);
2896da07ecd9SMark Brown 	flush_work_sync(&rdev->disable_work.work);
28976bf87d17SMark Brown 	WARN_ON(rdev->open_count);
28980f1d747bSMike Rapoport 	unset_regulator_supplies(rdev);
2899414c70cbSLiam Girdwood 	list_del(&rdev->list);
2900414c70cbSLiam Girdwood 	if (rdev->supply)
29013801b86aSMark Brown 		regulator_put(rdev->supply);
2902f8c12fe3SMark Brown 	kfree(rdev->constraints);
290358fb5cf5SLothar Waßmann 	device_unregister(&rdev->dev);
2904414c70cbSLiam Girdwood 	mutex_unlock(&regulator_list_mutex);
2905414c70cbSLiam Girdwood }
2906414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_unregister);
2907414c70cbSLiam Girdwood 
2908414c70cbSLiam Girdwood /**
2909cf7bbcdfSMark Brown  * regulator_suspend_prepare - prepare regulators for system wide suspend
2910414c70cbSLiam Girdwood  * @state: system suspend state
2911414c70cbSLiam Girdwood  *
2912414c70cbSLiam Girdwood  * Configure each regulator with it's suspend operating parameters for state.
2913414c70cbSLiam Girdwood  * This will usually be called by machine suspend code prior to supending.
2914414c70cbSLiam Girdwood  */
2915414c70cbSLiam Girdwood int regulator_suspend_prepare(suspend_state_t state)
2916414c70cbSLiam Girdwood {
2917414c70cbSLiam Girdwood 	struct regulator_dev *rdev;
2918414c70cbSLiam Girdwood 	int ret = 0;
2919414c70cbSLiam Girdwood 
2920414c70cbSLiam Girdwood 	/* ON is handled by regulator active state */
2921414c70cbSLiam Girdwood 	if (state == PM_SUSPEND_ON)
2922414c70cbSLiam Girdwood 		return -EINVAL;
2923414c70cbSLiam Girdwood 
2924414c70cbSLiam Girdwood 	mutex_lock(&regulator_list_mutex);
2925414c70cbSLiam Girdwood 	list_for_each_entry(rdev, &regulator_list, list) {
2926414c70cbSLiam Girdwood 
2927414c70cbSLiam Girdwood 		mutex_lock(&rdev->mutex);
2928414c70cbSLiam Girdwood 		ret = suspend_prepare(rdev, state);
2929414c70cbSLiam Girdwood 		mutex_unlock(&rdev->mutex);
2930414c70cbSLiam Girdwood 
2931414c70cbSLiam Girdwood 		if (ret < 0) {
29325da84fd9SJoe Perches 			rdev_err(rdev, "failed to prepare\n");
2933414c70cbSLiam Girdwood 			goto out;
2934414c70cbSLiam Girdwood 		}
2935414c70cbSLiam Girdwood 	}
2936414c70cbSLiam Girdwood out:
2937414c70cbSLiam Girdwood 	mutex_unlock(&regulator_list_mutex);
2938414c70cbSLiam Girdwood 	return ret;
2939414c70cbSLiam Girdwood }
2940414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2941414c70cbSLiam Girdwood 
2942414c70cbSLiam Girdwood /**
29437a32b589SMyungJoo Ham  * regulator_suspend_finish - resume regulators from system wide suspend
29447a32b589SMyungJoo Ham  *
29457a32b589SMyungJoo Ham  * Turn on regulators that might be turned off by regulator_suspend_prepare
29467a32b589SMyungJoo Ham  * and that should be turned on according to the regulators properties.
29477a32b589SMyungJoo Ham  */
29487a32b589SMyungJoo Ham int regulator_suspend_finish(void)
29497a32b589SMyungJoo Ham {
29507a32b589SMyungJoo Ham 	struct regulator_dev *rdev;
29517a32b589SMyungJoo Ham 	int ret = 0, error;
29527a32b589SMyungJoo Ham 
29537a32b589SMyungJoo Ham 	mutex_lock(&regulator_list_mutex);
29547a32b589SMyungJoo Ham 	list_for_each_entry(rdev, &regulator_list, list) {
29557a32b589SMyungJoo Ham 		struct regulator_ops *ops = rdev->desc->ops;
29567a32b589SMyungJoo Ham 
29577a32b589SMyungJoo Ham 		mutex_lock(&rdev->mutex);
29587a32b589SMyungJoo Ham 		if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
29597a32b589SMyungJoo Ham 				ops->enable) {
29607a32b589SMyungJoo Ham 			error = ops->enable(rdev);
29617a32b589SMyungJoo Ham 			if (error)
29627a32b589SMyungJoo Ham 				ret = error;
29637a32b589SMyungJoo Ham 		} else {
29647a32b589SMyungJoo Ham 			if (!has_full_constraints)
29657a32b589SMyungJoo Ham 				goto unlock;
29667a32b589SMyungJoo Ham 			if (!ops->disable)
29677a32b589SMyungJoo Ham 				goto unlock;
29687a32b589SMyungJoo Ham 			if (ops->is_enabled && !ops->is_enabled(rdev))
29697a32b589SMyungJoo Ham 				goto unlock;
29707a32b589SMyungJoo Ham 
29717a32b589SMyungJoo Ham 			error = ops->disable(rdev);
29727a32b589SMyungJoo Ham 			if (error)
29737a32b589SMyungJoo Ham 				ret = error;
29747a32b589SMyungJoo Ham 		}
29757a32b589SMyungJoo Ham unlock:
29767a32b589SMyungJoo Ham 		mutex_unlock(&rdev->mutex);
29777a32b589SMyungJoo Ham 	}
29787a32b589SMyungJoo Ham 	mutex_unlock(&regulator_list_mutex);
29797a32b589SMyungJoo Ham 	return ret;
29807a32b589SMyungJoo Ham }
29817a32b589SMyungJoo Ham EXPORT_SYMBOL_GPL(regulator_suspend_finish);
29827a32b589SMyungJoo Ham 
29837a32b589SMyungJoo Ham /**
2984ca725561SMark Brown  * regulator_has_full_constraints - the system has fully specified constraints
2985ca725561SMark Brown  *
2986ca725561SMark Brown  * Calling this function will cause the regulator API to disable all
2987ca725561SMark Brown  * regulators which have a zero use count and don't have an always_on
2988ca725561SMark Brown  * constraint in a late_initcall.
2989ca725561SMark Brown  *
2990ca725561SMark Brown  * The intention is that this will become the default behaviour in a
2991ca725561SMark Brown  * future kernel release so users are encouraged to use this facility
2992ca725561SMark Brown  * now.
2993ca725561SMark Brown  */
2994ca725561SMark Brown void regulator_has_full_constraints(void)
2995ca725561SMark Brown {
2996ca725561SMark Brown 	has_full_constraints = 1;
2997ca725561SMark Brown }
2998ca725561SMark Brown EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2999ca725561SMark Brown 
3000ca725561SMark Brown /**
3001688fe99aSMark Brown  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3002688fe99aSMark Brown  *
3003688fe99aSMark Brown  * Calling this function will cause the regulator API to provide a
3004688fe99aSMark Brown  * dummy regulator to consumers if no physical regulator is found,
3005688fe99aSMark Brown  * allowing most consumers to proceed as though a regulator were
3006688fe99aSMark Brown  * configured.  This allows systems such as those with software
3007688fe99aSMark Brown  * controllable regulators for the CPU core only to be brought up more
3008688fe99aSMark Brown  * readily.
3009688fe99aSMark Brown  */
3010688fe99aSMark Brown void regulator_use_dummy_regulator(void)
3011688fe99aSMark Brown {
3012688fe99aSMark Brown 	board_wants_dummy_regulator = true;
3013688fe99aSMark Brown }
3014688fe99aSMark Brown EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3015688fe99aSMark Brown 
3016688fe99aSMark Brown /**
3017414c70cbSLiam Girdwood  * rdev_get_drvdata - get rdev regulator driver data
301869279fb9SMark Brown  * @rdev: regulator
3019414c70cbSLiam Girdwood  *
3020414c70cbSLiam Girdwood  * Get rdev regulator driver private data. This call can be used in the
3021414c70cbSLiam Girdwood  * regulator driver context.
3022414c70cbSLiam Girdwood  */
3023414c70cbSLiam Girdwood void *rdev_get_drvdata(struct regulator_dev *rdev)
3024414c70cbSLiam Girdwood {
3025414c70cbSLiam Girdwood 	return rdev->reg_data;
3026414c70cbSLiam Girdwood }
3027414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3028414c70cbSLiam Girdwood 
3029414c70cbSLiam Girdwood /**
3030414c70cbSLiam Girdwood  * regulator_get_drvdata - get regulator driver data
3031414c70cbSLiam Girdwood  * @regulator: regulator
3032414c70cbSLiam Girdwood  *
3033414c70cbSLiam Girdwood  * Get regulator driver private data. This call can be used in the consumer
3034414c70cbSLiam Girdwood  * driver context when non API regulator specific functions need to be called.
3035414c70cbSLiam Girdwood  */
3036414c70cbSLiam Girdwood void *regulator_get_drvdata(struct regulator *regulator)
3037414c70cbSLiam Girdwood {
3038414c70cbSLiam Girdwood 	return regulator->rdev->reg_data;
3039414c70cbSLiam Girdwood }
3040414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3041414c70cbSLiam Girdwood 
3042414c70cbSLiam Girdwood /**
3043414c70cbSLiam Girdwood  * regulator_set_drvdata - set regulator driver data
3044414c70cbSLiam Girdwood  * @regulator: regulator
3045414c70cbSLiam Girdwood  * @data: data
3046414c70cbSLiam Girdwood  */
3047414c70cbSLiam Girdwood void regulator_set_drvdata(struct regulator *regulator, void *data)
3048414c70cbSLiam Girdwood {
3049414c70cbSLiam Girdwood 	regulator->rdev->reg_data = data;
3050414c70cbSLiam Girdwood }
3051414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3052414c70cbSLiam Girdwood 
3053414c70cbSLiam Girdwood /**
3054414c70cbSLiam Girdwood  * regulator_get_id - get regulator ID
305569279fb9SMark Brown  * @rdev: regulator
3056414c70cbSLiam Girdwood  */
3057414c70cbSLiam Girdwood int rdev_get_id(struct regulator_dev *rdev)
3058414c70cbSLiam Girdwood {
3059414c70cbSLiam Girdwood 	return rdev->desc->id;
3060414c70cbSLiam Girdwood }
3061414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(rdev_get_id);
3062414c70cbSLiam Girdwood 
3063a5766f11SLiam Girdwood struct device *rdev_get_dev(struct regulator_dev *rdev)
3064a5766f11SLiam Girdwood {
3065a5766f11SLiam Girdwood 	return &rdev->dev;
3066a5766f11SLiam Girdwood }
3067a5766f11SLiam Girdwood EXPORT_SYMBOL_GPL(rdev_get_dev);
3068a5766f11SLiam Girdwood 
3069a5766f11SLiam Girdwood void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3070a5766f11SLiam Girdwood {
3071a5766f11SLiam Girdwood 	return reg_init_data->driver_data;
3072a5766f11SLiam Girdwood }
3073a5766f11SLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3074a5766f11SLiam Girdwood 
3075ba55a974SMark Brown #ifdef CONFIG_DEBUG_FS
3076ba55a974SMark Brown static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3077ba55a974SMark Brown 				    size_t count, loff_t *ppos)
3078ba55a974SMark Brown {
3079ba55a974SMark Brown 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3080ba55a974SMark Brown 	ssize_t len, ret = 0;
3081ba55a974SMark Brown 	struct regulator_map *map;
3082ba55a974SMark Brown 
3083ba55a974SMark Brown 	if (!buf)
3084ba55a974SMark Brown 		return -ENOMEM;
3085ba55a974SMark Brown 
3086ba55a974SMark Brown 	list_for_each_entry(map, &regulator_map_list, list) {
3087ba55a974SMark Brown 		len = snprintf(buf + ret, PAGE_SIZE - ret,
3088ba55a974SMark Brown 			       "%s -> %s.%s\n",
3089ba55a974SMark Brown 			       rdev_get_name(map->regulator), map->dev_name,
3090ba55a974SMark Brown 			       map->supply);
3091ba55a974SMark Brown 		if (len >= 0)
3092ba55a974SMark Brown 			ret += len;
3093ba55a974SMark Brown 		if (ret > PAGE_SIZE) {
3094ba55a974SMark Brown 			ret = PAGE_SIZE;
3095ba55a974SMark Brown 			break;
3096ba55a974SMark Brown 		}
3097ba55a974SMark Brown 	}
3098ba55a974SMark Brown 
3099ba55a974SMark Brown 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3100ba55a974SMark Brown 
3101ba55a974SMark Brown 	kfree(buf);
3102ba55a974SMark Brown 
3103ba55a974SMark Brown 	return ret;
3104ba55a974SMark Brown }
310524751434SStephen Boyd #endif
3106ba55a974SMark Brown 
3107ba55a974SMark Brown static const struct file_operations supply_map_fops = {
310824751434SStephen Boyd #ifdef CONFIG_DEBUG_FS
3109ba55a974SMark Brown 	.read = supply_map_read_file,
3110ba55a974SMark Brown 	.llseek = default_llseek,
3111ba55a974SMark Brown #endif
311224751434SStephen Boyd };
3113ba55a974SMark Brown 
3114414c70cbSLiam Girdwood static int __init regulator_init(void)
3115414c70cbSLiam Girdwood {
311634abbd68SMark Brown 	int ret;
311734abbd68SMark Brown 
311834abbd68SMark Brown 	ret = class_register(&regulator_class);
311934abbd68SMark Brown 
31201130e5b3SMark Brown 	debugfs_root = debugfs_create_dir("regulator", NULL);
312124751434SStephen Boyd 	if (!debugfs_root)
31221130e5b3SMark Brown 		pr_warn("regulator: Failed to create debugfs directory\n");
3123ba55a974SMark Brown 
3124f4d562c6SMark Brown 	debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3125f4d562c6SMark Brown 			    &supply_map_fops);
31261130e5b3SMark Brown 
312734abbd68SMark Brown 	regulator_dummy_init();
312834abbd68SMark Brown 
312934abbd68SMark Brown 	return ret;
3130414c70cbSLiam Girdwood }
3131414c70cbSLiam Girdwood 
3132414c70cbSLiam Girdwood /* init early to allow our consumers to complete system booting */
3133414c70cbSLiam Girdwood core_initcall(regulator_init);
3134ca725561SMark Brown 
3135ca725561SMark Brown static int __init regulator_init_complete(void)
3136ca725561SMark Brown {
3137ca725561SMark Brown 	struct regulator_dev *rdev;
3138ca725561SMark Brown 	struct regulator_ops *ops;
3139ca725561SMark Brown 	struct regulation_constraints *c;
3140ca725561SMark Brown 	int enabled, ret;
3141ca725561SMark Brown 
3142ca725561SMark Brown 	mutex_lock(&regulator_list_mutex);
3143ca725561SMark Brown 
3144ca725561SMark Brown 	/* If we have a full configuration then disable any regulators
3145ca725561SMark Brown 	 * which are not in use or always_on.  This will become the
3146ca725561SMark Brown 	 * default behaviour in the future.
3147ca725561SMark Brown 	 */
3148ca725561SMark Brown 	list_for_each_entry(rdev, &regulator_list, list) {
3149ca725561SMark Brown 		ops = rdev->desc->ops;
3150ca725561SMark Brown 		c = rdev->constraints;
3151ca725561SMark Brown 
3152f25e0b4fSMark Brown 		if (!ops->disable || (c && c->always_on))
3153ca725561SMark Brown 			continue;
3154ca725561SMark Brown 
3155ca725561SMark Brown 		mutex_lock(&rdev->mutex);
3156ca725561SMark Brown 
3157ca725561SMark Brown 		if (rdev->use_count)
3158ca725561SMark Brown 			goto unlock;
3159ca725561SMark Brown 
3160ca725561SMark Brown 		/* If we can't read the status assume it's on. */
3161ca725561SMark Brown 		if (ops->is_enabled)
3162ca725561SMark Brown 			enabled = ops->is_enabled(rdev);
3163ca725561SMark Brown 		else
3164ca725561SMark Brown 			enabled = 1;
3165ca725561SMark Brown 
3166ca725561SMark Brown 		if (!enabled)
3167ca725561SMark Brown 			goto unlock;
3168ca725561SMark Brown 
3169ca725561SMark Brown 		if (has_full_constraints) {
3170ca725561SMark Brown 			/* We log since this may kill the system if it
3171ca725561SMark Brown 			 * goes wrong. */
31725da84fd9SJoe Perches 			rdev_info(rdev, "disabling\n");
3173ca725561SMark Brown 			ret = ops->disable(rdev);
3174ca725561SMark Brown 			if (ret != 0) {
31755da84fd9SJoe Perches 				rdev_err(rdev, "couldn't disable: %d\n", ret);
3176ca725561SMark Brown 			}
3177ca725561SMark Brown 		} else {
3178ca725561SMark Brown 			/* The intention is that in future we will
3179ca725561SMark Brown 			 * assume that full constraints are provided
3180ca725561SMark Brown 			 * so warn even if we aren't going to do
3181ca725561SMark Brown 			 * anything here.
3182ca725561SMark Brown 			 */
31835da84fd9SJoe Perches 			rdev_warn(rdev, "incomplete constraints, leaving on\n");
3184ca725561SMark Brown 		}
3185ca725561SMark Brown 
3186ca725561SMark Brown unlock:
3187ca725561SMark Brown 		mutex_unlock(&rdev->mutex);
3188ca725561SMark Brown 	}
3189ca725561SMark Brown 
3190ca725561SMark Brown 	mutex_unlock(&regulator_list_mutex);
3191ca725561SMark Brown 
3192ca725561SMark Brown 	return 0;
3193ca725561SMark Brown }
3194ca725561SMark Brown late_initcall(regulator_init_complete);
3195