xref: /openbmc/linux/drivers/regulator/core.c (revision c9ccaa0c)
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>
2665f73508SMark Brown #include <linux/gpio.h>
27778b28b4SRussell King #include <linux/gpio/consumer.h>
2869511a45SRajendra Nayak #include <linux/of.h>
2965b19ce6SMark Brown #include <linux/regmap.h>
3069511a45SRajendra Nayak #include <linux/regulator/of_regulator.h>
31414c70cbSLiam Girdwood #include <linux/regulator/consumer.h>
32414c70cbSLiam Girdwood #include <linux/regulator/driver.h>
33414c70cbSLiam Girdwood #include <linux/regulator/machine.h>
3465602c32SPaul Gortmaker #include <linux/module.h>
35414c70cbSLiam Girdwood 
3602fa3ec0SMark Brown #define CREATE_TRACE_POINTS
3702fa3ec0SMark Brown #include <trace/events/regulator.h>
3802fa3ec0SMark Brown 
3934abbd68SMark Brown #include "dummy.h"
400cdfcc0fSMark Brown #include "internal.h"
4134abbd68SMark Brown 
427d51a0dbSMark Brown #define rdev_crit(rdev, fmt, ...)					\
437d51a0dbSMark Brown 	pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
445da84fd9SJoe Perches #define rdev_err(rdev, fmt, ...)					\
455da84fd9SJoe Perches 	pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
465da84fd9SJoe Perches #define rdev_warn(rdev, fmt, ...)					\
475da84fd9SJoe Perches 	pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
485da84fd9SJoe Perches #define rdev_info(rdev, fmt, ...)					\
495da84fd9SJoe Perches 	pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
505da84fd9SJoe Perches #define rdev_dbg(rdev, fmt, ...)					\
515da84fd9SJoe Perches 	pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
525da84fd9SJoe Perches 
53414c70cbSLiam Girdwood static DEFINE_MUTEX(regulator_list_mutex);
54414c70cbSLiam Girdwood static LIST_HEAD(regulator_map_list);
55f19b00daSKim, Milo static LIST_HEAD(regulator_ena_gpio_list);
56a06ccd9cSCharles Keepax static LIST_HEAD(regulator_supply_alias_list);
5721cf891aSMark Brown static bool has_full_constraints;
58414c70cbSLiam Girdwood 
591130e5b3SMark Brown static struct dentry *debugfs_root;
601130e5b3SMark Brown 
6185f3b431STomeu Vizoso static struct class regulator_class;
6285f3b431STomeu Vizoso 
638dc5390dSMark Brown /*
64414c70cbSLiam Girdwood  * struct regulator_map
65414c70cbSLiam Girdwood  *
66414c70cbSLiam Girdwood  * Used to provide symbolic supply names to devices.
67414c70cbSLiam Girdwood  */
68414c70cbSLiam Girdwood struct regulator_map {
69414c70cbSLiam Girdwood 	struct list_head list;
7040f9244fSMark Brown 	const char *dev_name;   /* The dev_name() for the consumer */
71414c70cbSLiam Girdwood 	const char *supply;
72a5766f11SLiam Girdwood 	struct regulator_dev *regulator;
73414c70cbSLiam Girdwood };
74414c70cbSLiam Girdwood 
75414c70cbSLiam Girdwood /*
76f19b00daSKim, Milo  * struct regulator_enable_gpio
77f19b00daSKim, Milo  *
78f19b00daSKim, Milo  * Management for shared enable GPIO pin
79f19b00daSKim, Milo  */
80f19b00daSKim, Milo struct regulator_enable_gpio {
81f19b00daSKim, Milo 	struct list_head list;
82778b28b4SRussell King 	struct gpio_desc *gpiod;
83f19b00daSKim, Milo 	u32 enable_count;	/* a number of enabled shared GPIO */
84f19b00daSKim, Milo 	u32 request_count;	/* a number of requested shared GPIO */
85f19b00daSKim, Milo 	unsigned int ena_gpio_invert:1;
86f19b00daSKim, Milo };
87f19b00daSKim, Milo 
88a06ccd9cSCharles Keepax /*
89a06ccd9cSCharles Keepax  * struct regulator_supply_alias
90a06ccd9cSCharles Keepax  *
91a06ccd9cSCharles Keepax  * Used to map lookups for a supply onto an alternative device.
92a06ccd9cSCharles Keepax  */
93a06ccd9cSCharles Keepax struct regulator_supply_alias {
94a06ccd9cSCharles Keepax 	struct list_head list;
95a06ccd9cSCharles Keepax 	struct device *src_dev;
96a06ccd9cSCharles Keepax 	const char *src_supply;
97a06ccd9cSCharles Keepax 	struct device *alias_dev;
98a06ccd9cSCharles Keepax 	const char *alias_supply;
99a06ccd9cSCharles Keepax };
100a06ccd9cSCharles Keepax 
101414c70cbSLiam Girdwood static int _regulator_is_enabled(struct regulator_dev *rdev);
1023801b86aSMark Brown static int _regulator_disable(struct regulator_dev *rdev);
103414c70cbSLiam Girdwood static int _regulator_get_voltage(struct regulator_dev *rdev);
104414c70cbSLiam Girdwood static int _regulator_get_current_limit(struct regulator_dev *rdev);
105414c70cbSLiam Girdwood static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
1067179569aSHeiko Stübner static int _notifier_call_chain(struct regulator_dev *rdev,
107414c70cbSLiam Girdwood 				  unsigned long event, void *data);
10875790251SMark Brown static int _regulator_do_set_voltage(struct regulator_dev *rdev,
10975790251SMark Brown 				     int min_uV, int max_uV);
1103801b86aSMark Brown static struct regulator *create_regulator(struct regulator_dev *rdev,
1113801b86aSMark Brown 					  struct device *dev,
1123801b86aSMark Brown 					  const char *supply_name);
11336a1f1b6SJavier Martinez Canillas static void _regulator_put(struct regulator *regulator);
114414c70cbSLiam Girdwood 
115609ca5f3SMark Brown static struct regulator_dev *dev_to_rdev(struct device *dev)
116609ca5f3SMark Brown {
117609ca5f3SMark Brown 	return container_of(dev, struct regulator_dev, dev);
118609ca5f3SMark Brown }
119414c70cbSLiam Girdwood 
1201083c393SMark Brown static const char *rdev_get_name(struct regulator_dev *rdev)
1211083c393SMark Brown {
1221083c393SMark Brown 	if (rdev->constraints && rdev->constraints->name)
1231083c393SMark Brown 		return rdev->constraints->name;
1241083c393SMark Brown 	else if (rdev->desc->name)
1251083c393SMark Brown 		return rdev->desc->name;
1261083c393SMark Brown 	else
1271083c393SMark Brown 		return "";
1281083c393SMark Brown }
1291083c393SMark Brown 
13087b28417SMark Brown static bool have_full_constraints(void)
13187b28417SMark Brown {
13275bc9641SMark Brown 	return has_full_constraints || of_have_populated_dt();
13387b28417SMark Brown }
13487b28417SMark Brown 
1358a34e979SWEN Pingbo static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
1368a34e979SWEN Pingbo {
1378a34e979SWEN Pingbo 	if (!rdev->constraints) {
1388a34e979SWEN Pingbo 		rdev_err(rdev, "no constraints\n");
1398a34e979SWEN Pingbo 		return false;
1408a34e979SWEN Pingbo 	}
1418a34e979SWEN Pingbo 
1428a34e979SWEN Pingbo 	if (rdev->constraints->valid_ops_mask & ops)
1438a34e979SWEN Pingbo 		return true;
1448a34e979SWEN Pingbo 
1458a34e979SWEN Pingbo 	return false;
1468a34e979SWEN Pingbo }
1478a34e979SWEN Pingbo 
14870a7fb80SThierry Reding static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
14970a7fb80SThierry Reding {
15070a7fb80SThierry Reding 	if (rdev && rdev->supply)
15170a7fb80SThierry Reding 		return rdev->supply->rdev;
15270a7fb80SThierry Reding 
15370a7fb80SThierry Reding 	return NULL;
15470a7fb80SThierry Reding }
15570a7fb80SThierry Reding 
15669511a45SRajendra Nayak /**
1579f01cd4aSSascha Hauer  * regulator_lock_supply - lock a regulator and its supplies
1589f01cd4aSSascha Hauer  * @rdev:         regulator source
1599f01cd4aSSascha Hauer  */
1609f01cd4aSSascha Hauer static void regulator_lock_supply(struct regulator_dev *rdev)
1619f01cd4aSSascha Hauer {
162fa731ac7SArnd Bergmann 	int i;
1639f01cd4aSSascha Hauer 
16470a7fb80SThierry Reding 	for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
165fa731ac7SArnd Bergmann 		mutex_lock_nested(&rdev->mutex, i);
1669f01cd4aSSascha Hauer }
1679f01cd4aSSascha Hauer 
1689f01cd4aSSascha Hauer /**
1699f01cd4aSSascha Hauer  * regulator_unlock_supply - unlock a regulator and its supplies
1709f01cd4aSSascha Hauer  * @rdev:         regulator source
1719f01cd4aSSascha Hauer  */
1729f01cd4aSSascha Hauer static void regulator_unlock_supply(struct regulator_dev *rdev)
1739f01cd4aSSascha Hauer {
1749f01cd4aSSascha Hauer 	struct regulator *supply;
1759f01cd4aSSascha Hauer 
1769f01cd4aSSascha Hauer 	while (1) {
1779f01cd4aSSascha Hauer 		mutex_unlock(&rdev->mutex);
1789f01cd4aSSascha Hauer 		supply = rdev->supply;
1799f01cd4aSSascha Hauer 
1809f01cd4aSSascha Hauer 		if (!rdev->supply)
1819f01cd4aSSascha Hauer 			return;
1829f01cd4aSSascha Hauer 
1839f01cd4aSSascha Hauer 		rdev = supply->rdev;
1849f01cd4aSSascha Hauer 	}
1859f01cd4aSSascha Hauer }
1869f01cd4aSSascha Hauer 
1879f01cd4aSSascha Hauer /**
18869511a45SRajendra Nayak  * of_get_regulator - get a regulator device node based on supply name
18969511a45SRajendra Nayak  * @dev: Device pointer for the consumer (of regulator) device
19069511a45SRajendra Nayak  * @supply: regulator supply name
19169511a45SRajendra Nayak  *
19269511a45SRajendra Nayak  * Extract the regulator device node corresponding to the supply name.
193167d41dcSMaxime Ripard  * returns the device node corresponding to the regulator if found, else
19469511a45SRajendra Nayak  * returns NULL.
19569511a45SRajendra Nayak  */
19669511a45SRajendra Nayak static struct device_node *of_get_regulator(struct device *dev, const char *supply)
19769511a45SRajendra Nayak {
19869511a45SRajendra Nayak 	struct device_node *regnode = NULL;
19969511a45SRajendra Nayak 	char prop_name[32]; /* 32 is max size of property name */
20069511a45SRajendra Nayak 
20169511a45SRajendra Nayak 	dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
20269511a45SRajendra Nayak 
20369511a45SRajendra Nayak 	snprintf(prop_name, 32, "%s-supply", supply);
20469511a45SRajendra Nayak 	regnode = of_parse_phandle(dev->of_node, prop_name, 0);
20569511a45SRajendra Nayak 
20669511a45SRajendra Nayak 	if (!regnode) {
207b2661e98SDavid Lechner 		dev_dbg(dev, "Looking up %s property in node %s failed\n",
20869511a45SRajendra Nayak 				prop_name, dev->of_node->full_name);
20969511a45SRajendra Nayak 		return NULL;
21069511a45SRajendra Nayak 	}
21169511a45SRajendra Nayak 	return regnode;
21269511a45SRajendra Nayak }
21369511a45SRajendra Nayak 
214414c70cbSLiam Girdwood /* Platform voltage constraint check */
215414c70cbSLiam Girdwood static int regulator_check_voltage(struct regulator_dev *rdev,
216414c70cbSLiam Girdwood 				   int *min_uV, int *max_uV)
217414c70cbSLiam Girdwood {
218414c70cbSLiam Girdwood 	BUG_ON(*min_uV > *max_uV);
219414c70cbSLiam Girdwood 
2208a34e979SWEN Pingbo 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2217ebcf26cSStephen Boyd 		rdev_err(rdev, "voltage operation not allowed\n");
222414c70cbSLiam Girdwood 		return -EPERM;
223414c70cbSLiam Girdwood 	}
224414c70cbSLiam Girdwood 
225414c70cbSLiam Girdwood 	if (*max_uV > rdev->constraints->max_uV)
226414c70cbSLiam Girdwood 		*max_uV = rdev->constraints->max_uV;
227414c70cbSLiam Girdwood 	if (*min_uV < rdev->constraints->min_uV)
228414c70cbSLiam Girdwood 		*min_uV = rdev->constraints->min_uV;
229414c70cbSLiam Girdwood 
23089f425edSMark Brown 	if (*min_uV > *max_uV) {
23189f425edSMark Brown 		rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
23254abd335SMark Brown 			 *min_uV, *max_uV);
233414c70cbSLiam Girdwood 		return -EINVAL;
23489f425edSMark Brown 	}
235414c70cbSLiam Girdwood 
236414c70cbSLiam Girdwood 	return 0;
237414c70cbSLiam Girdwood }
238414c70cbSLiam Girdwood 
23905fda3b1SThomas Petazzoni /* Make sure we select a voltage that suits the needs of all
24005fda3b1SThomas Petazzoni  * regulator consumers
24105fda3b1SThomas Petazzoni  */
24205fda3b1SThomas Petazzoni static int regulator_check_consumers(struct regulator_dev *rdev,
24305fda3b1SThomas Petazzoni 				     int *min_uV, int *max_uV)
24405fda3b1SThomas Petazzoni {
24505fda3b1SThomas Petazzoni 	struct regulator *regulator;
24605fda3b1SThomas Petazzoni 
24705fda3b1SThomas Petazzoni 	list_for_each_entry(regulator, &rdev->consumer_list, list) {
2484aa922c0SMark Brown 		/*
2494aa922c0SMark Brown 		 * Assume consumers that didn't say anything are OK
2504aa922c0SMark Brown 		 * with anything in the constraint range.
2514aa922c0SMark Brown 		 */
2524aa922c0SMark Brown 		if (!regulator->min_uV && !regulator->max_uV)
2534aa922c0SMark Brown 			continue;
2544aa922c0SMark Brown 
25505fda3b1SThomas Petazzoni 		if (*max_uV > regulator->max_uV)
25605fda3b1SThomas Petazzoni 			*max_uV = regulator->max_uV;
25705fda3b1SThomas Petazzoni 		if (*min_uV < regulator->min_uV)
25805fda3b1SThomas Petazzoni 			*min_uV = regulator->min_uV;
25905fda3b1SThomas Petazzoni 	}
26005fda3b1SThomas Petazzoni 
261dd8004afSMark Brown 	if (*min_uV > *max_uV) {
2629c7b4e8aSRuss Dill 		rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
2639c7b4e8aSRuss Dill 			*min_uV, *max_uV);
26405fda3b1SThomas Petazzoni 		return -EINVAL;
265dd8004afSMark Brown 	}
26605fda3b1SThomas Petazzoni 
26705fda3b1SThomas Petazzoni 	return 0;
26805fda3b1SThomas Petazzoni }
26905fda3b1SThomas Petazzoni 
270414c70cbSLiam Girdwood /* current constraint check */
271414c70cbSLiam Girdwood static int regulator_check_current_limit(struct regulator_dev *rdev,
272414c70cbSLiam Girdwood 					int *min_uA, int *max_uA)
273414c70cbSLiam Girdwood {
274414c70cbSLiam Girdwood 	BUG_ON(*min_uA > *max_uA);
275414c70cbSLiam Girdwood 
2768a34e979SWEN Pingbo 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
2777ebcf26cSStephen Boyd 		rdev_err(rdev, "current operation not allowed\n");
278414c70cbSLiam Girdwood 		return -EPERM;
279414c70cbSLiam Girdwood 	}
280414c70cbSLiam Girdwood 
281414c70cbSLiam Girdwood 	if (*max_uA > rdev->constraints->max_uA)
282414c70cbSLiam Girdwood 		*max_uA = rdev->constraints->max_uA;
283414c70cbSLiam Girdwood 	if (*min_uA < rdev->constraints->min_uA)
284414c70cbSLiam Girdwood 		*min_uA = rdev->constraints->min_uA;
285414c70cbSLiam Girdwood 
28689f425edSMark Brown 	if (*min_uA > *max_uA) {
28789f425edSMark Brown 		rdev_err(rdev, "unsupportable current range: %d-%duA\n",
28854abd335SMark Brown 			 *min_uA, *max_uA);
289414c70cbSLiam Girdwood 		return -EINVAL;
29089f425edSMark Brown 	}
291414c70cbSLiam Girdwood 
292414c70cbSLiam Girdwood 	return 0;
293414c70cbSLiam Girdwood }
294414c70cbSLiam Girdwood 
295414c70cbSLiam Girdwood /* operating mode constraint check */
296109c75afSCharles Keepax static int regulator_mode_constrain(struct regulator_dev *rdev,
297109c75afSCharles Keepax 				    unsigned int *mode)
298414c70cbSLiam Girdwood {
2992c608234SMark Brown 	switch (*mode) {
300e573520bSDavid Brownell 	case REGULATOR_MODE_FAST:
301e573520bSDavid Brownell 	case REGULATOR_MODE_NORMAL:
302e573520bSDavid Brownell 	case REGULATOR_MODE_IDLE:
303e573520bSDavid Brownell 	case REGULATOR_MODE_STANDBY:
304e573520bSDavid Brownell 		break;
305e573520bSDavid Brownell 	default:
30689f425edSMark Brown 		rdev_err(rdev, "invalid mode %x specified\n", *mode);
307e573520bSDavid Brownell 		return -EINVAL;
308e573520bSDavid Brownell 	}
309e573520bSDavid Brownell 
3108a34e979SWEN Pingbo 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
3117ebcf26cSStephen Boyd 		rdev_err(rdev, "mode operation not allowed\n");
312414c70cbSLiam Girdwood 		return -EPERM;
313414c70cbSLiam Girdwood 	}
3142c608234SMark Brown 
3152c608234SMark Brown 	/* The modes are bitmasks, the most power hungry modes having
3162c608234SMark Brown 	 * the lowest values. If the requested mode isn't supported
3172c608234SMark Brown 	 * try higher modes. */
3182c608234SMark Brown 	while (*mode) {
3192c608234SMark Brown 		if (rdev->constraints->valid_modes_mask & *mode)
320414c70cbSLiam Girdwood 			return 0;
3212c608234SMark Brown 		*mode /= 2;
3222c608234SMark Brown 	}
3232c608234SMark Brown 
3242c608234SMark Brown 	return -EINVAL;
325414c70cbSLiam Girdwood }
326414c70cbSLiam Girdwood 
327414c70cbSLiam Girdwood static ssize_t regulator_uV_show(struct device *dev,
328414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
329414c70cbSLiam Girdwood {
330a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
331414c70cbSLiam Girdwood 	ssize_t ret;
332414c70cbSLiam Girdwood 
333414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
334414c70cbSLiam Girdwood 	ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
335414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
336414c70cbSLiam Girdwood 
337414c70cbSLiam Girdwood 	return ret;
338414c70cbSLiam Girdwood }
3397ad68e2fSDavid Brownell static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
340414c70cbSLiam Girdwood 
341414c70cbSLiam Girdwood static ssize_t regulator_uA_show(struct device *dev,
342414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
343414c70cbSLiam Girdwood {
344a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
345414c70cbSLiam Girdwood 
346414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
347414c70cbSLiam Girdwood }
3487ad68e2fSDavid Brownell static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
349414c70cbSLiam Girdwood 
350587cea27SGreg Kroah-Hartman static ssize_t name_show(struct device *dev, struct device_attribute *attr,
351587cea27SGreg Kroah-Hartman 			 char *buf)
352bc558a60SMark Brown {
353bc558a60SMark Brown 	struct regulator_dev *rdev = dev_get_drvdata(dev);
354bc558a60SMark Brown 
3551083c393SMark Brown 	return sprintf(buf, "%s\n", rdev_get_name(rdev));
356bc558a60SMark Brown }
357587cea27SGreg Kroah-Hartman static DEVICE_ATTR_RO(name);
358bc558a60SMark Brown 
3594fca9545SDavid Brownell static ssize_t regulator_print_opmode(char *buf, int mode)
360414c70cbSLiam Girdwood {
361414c70cbSLiam Girdwood 	switch (mode) {
362414c70cbSLiam Girdwood 	case REGULATOR_MODE_FAST:
363414c70cbSLiam Girdwood 		return sprintf(buf, "fast\n");
364414c70cbSLiam Girdwood 	case REGULATOR_MODE_NORMAL:
365414c70cbSLiam Girdwood 		return sprintf(buf, "normal\n");
366414c70cbSLiam Girdwood 	case REGULATOR_MODE_IDLE:
367414c70cbSLiam Girdwood 		return sprintf(buf, "idle\n");
368414c70cbSLiam Girdwood 	case REGULATOR_MODE_STANDBY:
369414c70cbSLiam Girdwood 		return sprintf(buf, "standby\n");
370414c70cbSLiam Girdwood 	}
371414c70cbSLiam Girdwood 	return sprintf(buf, "unknown\n");
372414c70cbSLiam Girdwood }
373414c70cbSLiam Girdwood 
3744fca9545SDavid Brownell static ssize_t regulator_opmode_show(struct device *dev,
375414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
376414c70cbSLiam Girdwood {
377a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
378414c70cbSLiam Girdwood 
3794fca9545SDavid Brownell 	return regulator_print_opmode(buf, _regulator_get_mode(rdev));
3804fca9545SDavid Brownell }
3817ad68e2fSDavid Brownell static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
3824fca9545SDavid Brownell 
3834fca9545SDavid Brownell static ssize_t regulator_print_state(char *buf, int state)
3844fca9545SDavid Brownell {
385414c70cbSLiam Girdwood 	if (state > 0)
386414c70cbSLiam Girdwood 		return sprintf(buf, "enabled\n");
387414c70cbSLiam Girdwood 	else if (state == 0)
388414c70cbSLiam Girdwood 		return sprintf(buf, "disabled\n");
389414c70cbSLiam Girdwood 	else
390414c70cbSLiam Girdwood 		return sprintf(buf, "unknown\n");
391414c70cbSLiam Girdwood }
392414c70cbSLiam Girdwood 
3934fca9545SDavid Brownell static ssize_t regulator_state_show(struct device *dev,
3944fca9545SDavid Brownell 				   struct device_attribute *attr, char *buf)
3954fca9545SDavid Brownell {
3964fca9545SDavid Brownell 	struct regulator_dev *rdev = dev_get_drvdata(dev);
3979332546fSMark Brown 	ssize_t ret;
3984fca9545SDavid Brownell 
3999332546fSMark Brown 	mutex_lock(&rdev->mutex);
4009332546fSMark Brown 	ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
4019332546fSMark Brown 	mutex_unlock(&rdev->mutex);
4029332546fSMark Brown 
4039332546fSMark Brown 	return ret;
4044fca9545SDavid Brownell }
4057ad68e2fSDavid Brownell static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
4064fca9545SDavid Brownell 
407853116a1SDavid Brownell static ssize_t regulator_status_show(struct device *dev,
408853116a1SDavid Brownell 				   struct device_attribute *attr, char *buf)
409853116a1SDavid Brownell {
410853116a1SDavid Brownell 	struct regulator_dev *rdev = dev_get_drvdata(dev);
411853116a1SDavid Brownell 	int status;
412853116a1SDavid Brownell 	char *label;
413853116a1SDavid Brownell 
414853116a1SDavid Brownell 	status = rdev->desc->ops->get_status(rdev);
415853116a1SDavid Brownell 	if (status < 0)
416853116a1SDavid Brownell 		return status;
417853116a1SDavid Brownell 
418853116a1SDavid Brownell 	switch (status) {
419853116a1SDavid Brownell 	case REGULATOR_STATUS_OFF:
420853116a1SDavid Brownell 		label = "off";
421853116a1SDavid Brownell 		break;
422853116a1SDavid Brownell 	case REGULATOR_STATUS_ON:
423853116a1SDavid Brownell 		label = "on";
424853116a1SDavid Brownell 		break;
425853116a1SDavid Brownell 	case REGULATOR_STATUS_ERROR:
426853116a1SDavid Brownell 		label = "error";
427853116a1SDavid Brownell 		break;
428853116a1SDavid Brownell 	case REGULATOR_STATUS_FAST:
429853116a1SDavid Brownell 		label = "fast";
430853116a1SDavid Brownell 		break;
431853116a1SDavid Brownell 	case REGULATOR_STATUS_NORMAL:
432853116a1SDavid Brownell 		label = "normal";
433853116a1SDavid Brownell 		break;
434853116a1SDavid Brownell 	case REGULATOR_STATUS_IDLE:
435853116a1SDavid Brownell 		label = "idle";
436853116a1SDavid Brownell 		break;
437853116a1SDavid Brownell 	case REGULATOR_STATUS_STANDBY:
438853116a1SDavid Brownell 		label = "standby";
439853116a1SDavid Brownell 		break;
440f59c8f9fSMark Brown 	case REGULATOR_STATUS_BYPASS:
441f59c8f9fSMark Brown 		label = "bypass";
442f59c8f9fSMark Brown 		break;
4431beaf762SKrystian Garbaciak 	case REGULATOR_STATUS_UNDEFINED:
4441beaf762SKrystian Garbaciak 		label = "undefined";
4451beaf762SKrystian Garbaciak 		break;
446853116a1SDavid Brownell 	default:
447853116a1SDavid Brownell 		return -ERANGE;
448853116a1SDavid Brownell 	}
449853116a1SDavid Brownell 
450853116a1SDavid Brownell 	return sprintf(buf, "%s\n", label);
451853116a1SDavid Brownell }
452853116a1SDavid Brownell static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
453853116a1SDavid Brownell 
454414c70cbSLiam Girdwood static ssize_t regulator_min_uA_show(struct device *dev,
455414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
456414c70cbSLiam Girdwood {
457a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
458414c70cbSLiam Girdwood 
459414c70cbSLiam Girdwood 	if (!rdev->constraints)
460414c70cbSLiam Girdwood 		return sprintf(buf, "constraint not defined\n");
461414c70cbSLiam Girdwood 
462414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->min_uA);
463414c70cbSLiam Girdwood }
4647ad68e2fSDavid Brownell static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
465414c70cbSLiam Girdwood 
466414c70cbSLiam Girdwood static ssize_t regulator_max_uA_show(struct device *dev,
467414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
468414c70cbSLiam Girdwood {
469a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
470414c70cbSLiam Girdwood 
471414c70cbSLiam Girdwood 	if (!rdev->constraints)
472414c70cbSLiam Girdwood 		return sprintf(buf, "constraint not defined\n");
473414c70cbSLiam Girdwood 
474414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->max_uA);
475414c70cbSLiam Girdwood }
4767ad68e2fSDavid Brownell static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
477414c70cbSLiam Girdwood 
478414c70cbSLiam Girdwood static ssize_t regulator_min_uV_show(struct device *dev,
479414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
480414c70cbSLiam Girdwood {
481a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
482414c70cbSLiam Girdwood 
483414c70cbSLiam Girdwood 	if (!rdev->constraints)
484414c70cbSLiam Girdwood 		return sprintf(buf, "constraint not defined\n");
485414c70cbSLiam Girdwood 
486414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->min_uV);
487414c70cbSLiam Girdwood }
4887ad68e2fSDavid Brownell static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
489414c70cbSLiam Girdwood 
490414c70cbSLiam Girdwood static ssize_t regulator_max_uV_show(struct device *dev,
491414c70cbSLiam Girdwood 				    struct device_attribute *attr, char *buf)
492414c70cbSLiam Girdwood {
493a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
494414c70cbSLiam Girdwood 
495414c70cbSLiam Girdwood 	if (!rdev->constraints)
496414c70cbSLiam Girdwood 		return sprintf(buf, "constraint not defined\n");
497414c70cbSLiam Girdwood 
498414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->max_uV);
499414c70cbSLiam Girdwood }
5007ad68e2fSDavid Brownell static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
501414c70cbSLiam Girdwood 
502414c70cbSLiam Girdwood static ssize_t regulator_total_uA_show(struct device *dev,
503414c70cbSLiam Girdwood 				      struct device_attribute *attr, char *buf)
504414c70cbSLiam Girdwood {
505a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
506414c70cbSLiam Girdwood 	struct regulator *regulator;
507414c70cbSLiam Girdwood 	int uA = 0;
508414c70cbSLiam Girdwood 
509414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
510414c70cbSLiam Girdwood 	list_for_each_entry(regulator, &rdev->consumer_list, list)
511414c70cbSLiam Girdwood 		uA += regulator->uA_load;
512414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
513414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", uA);
514414c70cbSLiam Girdwood }
5157ad68e2fSDavid Brownell static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
516414c70cbSLiam Girdwood 
517587cea27SGreg Kroah-Hartman static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
518587cea27SGreg Kroah-Hartman 			      char *buf)
519414c70cbSLiam Girdwood {
520a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
521414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->use_count);
522414c70cbSLiam Girdwood }
523587cea27SGreg Kroah-Hartman static DEVICE_ATTR_RO(num_users);
524414c70cbSLiam Girdwood 
525587cea27SGreg Kroah-Hartman static ssize_t type_show(struct device *dev, struct device_attribute *attr,
526587cea27SGreg Kroah-Hartman 			 char *buf)
527414c70cbSLiam Girdwood {
528a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
529414c70cbSLiam Girdwood 
530414c70cbSLiam Girdwood 	switch (rdev->desc->type) {
531414c70cbSLiam Girdwood 	case REGULATOR_VOLTAGE:
532414c70cbSLiam Girdwood 		return sprintf(buf, "voltage\n");
533414c70cbSLiam Girdwood 	case REGULATOR_CURRENT:
534414c70cbSLiam Girdwood 		return sprintf(buf, "current\n");
535414c70cbSLiam Girdwood 	}
536414c70cbSLiam Girdwood 	return sprintf(buf, "unknown\n");
537414c70cbSLiam Girdwood }
538587cea27SGreg Kroah-Hartman static DEVICE_ATTR_RO(type);
539414c70cbSLiam Girdwood 
540414c70cbSLiam Girdwood static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
541414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
542414c70cbSLiam Girdwood {
543a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
544414c70cbSLiam Girdwood 
545414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
546414c70cbSLiam Girdwood }
5477ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_mem_microvolts, 0444,
5487ad68e2fSDavid Brownell 		regulator_suspend_mem_uV_show, NULL);
549414c70cbSLiam Girdwood 
550414c70cbSLiam Girdwood static ssize_t regulator_suspend_disk_uV_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 
555414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
556414c70cbSLiam Girdwood }
5577ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_disk_microvolts, 0444,
5587ad68e2fSDavid Brownell 		regulator_suspend_disk_uV_show, NULL);
559414c70cbSLiam Girdwood 
560414c70cbSLiam Girdwood static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
561414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
562414c70cbSLiam Girdwood {
563a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
564414c70cbSLiam Girdwood 
565414c70cbSLiam Girdwood 	return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
566414c70cbSLiam Girdwood }
5677ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_standby_microvolts, 0444,
5687ad68e2fSDavid Brownell 		regulator_suspend_standby_uV_show, NULL);
569414c70cbSLiam Girdwood 
570414c70cbSLiam Girdwood static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
571414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
572414c70cbSLiam Girdwood {
573a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
574414c70cbSLiam Girdwood 
5754fca9545SDavid Brownell 	return regulator_print_opmode(buf,
5764fca9545SDavid Brownell 		rdev->constraints->state_mem.mode);
577414c70cbSLiam Girdwood }
5787ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_mem_mode, 0444,
5797ad68e2fSDavid Brownell 		regulator_suspend_mem_mode_show, NULL);
580414c70cbSLiam Girdwood 
581414c70cbSLiam Girdwood static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
582414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
583414c70cbSLiam Girdwood {
584a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
585414c70cbSLiam Girdwood 
5864fca9545SDavid Brownell 	return regulator_print_opmode(buf,
5874fca9545SDavid Brownell 		rdev->constraints->state_disk.mode);
588414c70cbSLiam Girdwood }
5897ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_disk_mode, 0444,
5907ad68e2fSDavid Brownell 		regulator_suspend_disk_mode_show, NULL);
591414c70cbSLiam Girdwood 
592414c70cbSLiam Girdwood static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
593414c70cbSLiam Girdwood 				struct device_attribute *attr, char *buf)
594414c70cbSLiam Girdwood {
595a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
596414c70cbSLiam Girdwood 
5974fca9545SDavid Brownell 	return regulator_print_opmode(buf,
5984fca9545SDavid Brownell 		rdev->constraints->state_standby.mode);
599414c70cbSLiam Girdwood }
6007ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_standby_mode, 0444,
6017ad68e2fSDavid Brownell 		regulator_suspend_standby_mode_show, NULL);
602414c70cbSLiam Girdwood 
603414c70cbSLiam Girdwood static ssize_t regulator_suspend_mem_state_show(struct device *dev,
604414c70cbSLiam Girdwood 				   struct device_attribute *attr, char *buf)
605414c70cbSLiam Girdwood {
606a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
607414c70cbSLiam Girdwood 
6084fca9545SDavid Brownell 	return regulator_print_state(buf,
6094fca9545SDavid Brownell 			rdev->constraints->state_mem.enabled);
610414c70cbSLiam Girdwood }
6117ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_mem_state, 0444,
6127ad68e2fSDavid Brownell 		regulator_suspend_mem_state_show, NULL);
613414c70cbSLiam Girdwood 
614414c70cbSLiam Girdwood static ssize_t regulator_suspend_disk_state_show(struct device *dev,
615414c70cbSLiam Girdwood 				   struct device_attribute *attr, char *buf)
616414c70cbSLiam Girdwood {
617a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
618414c70cbSLiam Girdwood 
6194fca9545SDavid Brownell 	return regulator_print_state(buf,
6204fca9545SDavid Brownell 			rdev->constraints->state_disk.enabled);
621414c70cbSLiam Girdwood }
6227ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_disk_state, 0444,
6237ad68e2fSDavid Brownell 		regulator_suspend_disk_state_show, NULL);
624414c70cbSLiam Girdwood 
625414c70cbSLiam Girdwood static ssize_t regulator_suspend_standby_state_show(struct device *dev,
626414c70cbSLiam Girdwood 				   struct device_attribute *attr, char *buf)
627414c70cbSLiam Girdwood {
628a5766f11SLiam Girdwood 	struct regulator_dev *rdev = dev_get_drvdata(dev);
629414c70cbSLiam Girdwood 
6304fca9545SDavid Brownell 	return regulator_print_state(buf,
6314fca9545SDavid Brownell 			rdev->constraints->state_standby.enabled);
632414c70cbSLiam Girdwood }
6337ad68e2fSDavid Brownell static DEVICE_ATTR(suspend_standby_state, 0444,
6347ad68e2fSDavid Brownell 		regulator_suspend_standby_state_show, NULL);
635bc558a60SMark Brown 
636f59c8f9fSMark Brown static ssize_t regulator_bypass_show(struct device *dev,
637f59c8f9fSMark Brown 				     struct device_attribute *attr, char *buf)
638f59c8f9fSMark Brown {
639f59c8f9fSMark Brown 	struct regulator_dev *rdev = dev_get_drvdata(dev);
640f59c8f9fSMark Brown 	const char *report;
641f59c8f9fSMark Brown 	bool bypass;
642f59c8f9fSMark Brown 	int ret;
643f59c8f9fSMark Brown 
644f59c8f9fSMark Brown 	ret = rdev->desc->ops->get_bypass(rdev, &bypass);
645f59c8f9fSMark Brown 
646f59c8f9fSMark Brown 	if (ret != 0)
647f59c8f9fSMark Brown 		report = "unknown";
648f59c8f9fSMark Brown 	else if (bypass)
649f59c8f9fSMark Brown 		report = "enabled";
650f59c8f9fSMark Brown 	else
651f59c8f9fSMark Brown 		report = "disabled";
652f59c8f9fSMark Brown 
653f59c8f9fSMark Brown 	return sprintf(buf, "%s\n", report);
654f59c8f9fSMark Brown }
655f59c8f9fSMark Brown static DEVICE_ATTR(bypass, 0444,
656f59c8f9fSMark Brown 		   regulator_bypass_show, NULL);
6577ad68e2fSDavid Brownell 
658414c70cbSLiam Girdwood /* Calculate the new optimum regulator operating mode based on the new total
659414c70cbSLiam Girdwood  * consumer load. All locks held by caller */
6608460ef38SBjorn Andersson static int drms_uA_update(struct regulator_dev *rdev)
661414c70cbSLiam Girdwood {
662414c70cbSLiam Girdwood 	struct regulator *sibling;
663414c70cbSLiam Girdwood 	int current_uA = 0, output_uV, input_uV, err;
664414c70cbSLiam Girdwood 	unsigned int mode;
665414c70cbSLiam Girdwood 
66670cfef26SKrzysztof Kozlowski 	lockdep_assert_held_once(&rdev->mutex);
66770cfef26SKrzysztof Kozlowski 
6688460ef38SBjorn Andersson 	/*
6698460ef38SBjorn Andersson 	 * first check to see if we can set modes at all, otherwise just
6708460ef38SBjorn Andersson 	 * tell the consumer everything is OK.
6718460ef38SBjorn Andersson 	 */
6728a34e979SWEN Pingbo 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
6738460ef38SBjorn Andersson 		return 0;
6748460ef38SBjorn Andersson 
6758f4490e0SBjorn Andersson 	if (!rdev->desc->ops->get_optimum_mode &&
6768f4490e0SBjorn Andersson 	    !rdev->desc->ops->set_load)
6778460ef38SBjorn Andersson 		return 0;
6788460ef38SBjorn Andersson 
6798f4490e0SBjorn Andersson 	if (!rdev->desc->ops->set_mode &&
6808f4490e0SBjorn Andersson 	    !rdev->desc->ops->set_load)
6818460ef38SBjorn Andersson 		return -EINVAL;
682414c70cbSLiam Girdwood 
68357776617SJoonwoo Park 	/* calc total requested load */
68457776617SJoonwoo Park 	list_for_each_entry(sibling, &rdev->consumer_list, list)
68557776617SJoonwoo Park 		current_uA += sibling->uA_load;
68657776617SJoonwoo Park 
68757776617SJoonwoo Park 	current_uA += rdev->constraints->system_load;
68857776617SJoonwoo Park 
68957776617SJoonwoo Park 	if (rdev->desc->ops->set_load) {
69057776617SJoonwoo Park 		/* set the optimum mode for our new total regulator load */
69157776617SJoonwoo Park 		err = rdev->desc->ops->set_load(rdev, current_uA);
69257776617SJoonwoo Park 		if (err < 0)
69357776617SJoonwoo Park 			rdev_err(rdev, "failed to set load %d\n", current_uA);
69457776617SJoonwoo Park 	} else {
695414c70cbSLiam Girdwood 		/* get output voltage */
6961bf5a1f8SMark Brown 		output_uV = _regulator_get_voltage(rdev);
6978460ef38SBjorn Andersson 		if (output_uV <= 0) {
6988460ef38SBjorn Andersson 			rdev_err(rdev, "invalid output voltage found\n");
6998460ef38SBjorn Andersson 			return -EINVAL;
7008460ef38SBjorn Andersson 		}
701414c70cbSLiam Girdwood 
702414c70cbSLiam Girdwood 		/* get input voltage */
7031bf5a1f8SMark Brown 		input_uV = 0;
7041bf5a1f8SMark Brown 		if (rdev->supply)
7053f24f5adSAxel Lin 			input_uV = regulator_get_voltage(rdev->supply);
7061bf5a1f8SMark Brown 		if (input_uV <= 0)
707414c70cbSLiam Girdwood 			input_uV = rdev->constraints->input_uV;
7088460ef38SBjorn Andersson 		if (input_uV <= 0) {
7098460ef38SBjorn Andersson 			rdev_err(rdev, "invalid input voltage found\n");
7108460ef38SBjorn Andersson 			return -EINVAL;
7118460ef38SBjorn Andersson 		}
712414c70cbSLiam Girdwood 
713414c70cbSLiam Girdwood 		/* now get the optimum mode for our new total regulator load */
714414c70cbSLiam Girdwood 		mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
715414c70cbSLiam Girdwood 							 output_uV, current_uA);
716414c70cbSLiam Girdwood 
717414c70cbSLiam Girdwood 		/* check the new mode is allowed */
7182c608234SMark Brown 		err = regulator_mode_constrain(rdev, &mode);
7198460ef38SBjorn Andersson 		if (err < 0) {
7208460ef38SBjorn Andersson 			rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
7218460ef38SBjorn Andersson 				 current_uA, input_uV, output_uV);
7228460ef38SBjorn Andersson 			return err;
7238460ef38SBjorn Andersson 		}
7248460ef38SBjorn Andersson 
7258460ef38SBjorn Andersson 		err = rdev->desc->ops->set_mode(rdev, mode);
7268460ef38SBjorn Andersson 		if (err < 0)
7278460ef38SBjorn Andersson 			rdev_err(rdev, "failed to set optimum mode %x\n", mode);
7288f4490e0SBjorn Andersson 	}
7298460ef38SBjorn Andersson 
7308460ef38SBjorn Andersson 	return err;
731414c70cbSLiam Girdwood }
732414c70cbSLiam Girdwood 
733414c70cbSLiam Girdwood static int suspend_set_state(struct regulator_dev *rdev,
734414c70cbSLiam Girdwood 	struct regulator_state *rstate)
735414c70cbSLiam Girdwood {
736414c70cbSLiam Girdwood 	int ret = 0;
737638f85c5SMark Brown 
738638f85c5SMark Brown 	/* If we have no suspend mode configration don't set anything;
7398ac0e95dSAxel Lin 	 * only warn if the driver implements set_suspend_voltage or
7408ac0e95dSAxel Lin 	 * set_suspend_mode callback.
741638f85c5SMark Brown 	 */
742638f85c5SMark Brown 	if (!rstate->enabled && !rstate->disabled) {
7438ac0e95dSAxel Lin 		if (rdev->desc->ops->set_suspend_voltage ||
7448ac0e95dSAxel Lin 		    rdev->desc->ops->set_suspend_mode)
7455da84fd9SJoe Perches 			rdev_warn(rdev, "No configuration\n");
746638f85c5SMark Brown 		return 0;
747638f85c5SMark Brown 	}
748638f85c5SMark Brown 
749638f85c5SMark Brown 	if (rstate->enabled && rstate->disabled) {
7505da84fd9SJoe Perches 		rdev_err(rdev, "invalid configuration\n");
751638f85c5SMark Brown 		return -EINVAL;
752638f85c5SMark Brown 	}
753638f85c5SMark Brown 
7548ac0e95dSAxel Lin 	if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
755414c70cbSLiam Girdwood 		ret = rdev->desc->ops->set_suspend_enable(rdev);
7568ac0e95dSAxel Lin 	else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
757414c70cbSLiam Girdwood 		ret = rdev->desc->ops->set_suspend_disable(rdev);
7588ac0e95dSAxel Lin 	else /* OK if set_suspend_enable or set_suspend_disable is NULL */
7598ac0e95dSAxel Lin 		ret = 0;
7608ac0e95dSAxel Lin 
761414c70cbSLiam Girdwood 	if (ret < 0) {
7625da84fd9SJoe Perches 		rdev_err(rdev, "failed to enabled/disable\n");
763414c70cbSLiam Girdwood 		return ret;
764414c70cbSLiam Girdwood 	}
765414c70cbSLiam Girdwood 
766414c70cbSLiam Girdwood 	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
767414c70cbSLiam Girdwood 		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
768414c70cbSLiam Girdwood 		if (ret < 0) {
7695da84fd9SJoe Perches 			rdev_err(rdev, "failed to set voltage\n");
770414c70cbSLiam Girdwood 			return ret;
771414c70cbSLiam Girdwood 		}
772414c70cbSLiam Girdwood 	}
773414c70cbSLiam Girdwood 
774414c70cbSLiam Girdwood 	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
775414c70cbSLiam Girdwood 		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
776414c70cbSLiam Girdwood 		if (ret < 0) {
7775da84fd9SJoe Perches 			rdev_err(rdev, "failed to set mode\n");
778414c70cbSLiam Girdwood 			return ret;
779414c70cbSLiam Girdwood 		}
780414c70cbSLiam Girdwood 	}
781414c70cbSLiam Girdwood 	return ret;
782414c70cbSLiam Girdwood }
783414c70cbSLiam Girdwood 
784414c70cbSLiam Girdwood /* locks held by caller */
785414c70cbSLiam Girdwood static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
786414c70cbSLiam Girdwood {
787414c70cbSLiam Girdwood 	if (!rdev->constraints)
788414c70cbSLiam Girdwood 		return -EINVAL;
789414c70cbSLiam Girdwood 
790414c70cbSLiam Girdwood 	switch (state) {
791414c70cbSLiam Girdwood 	case PM_SUSPEND_STANDBY:
792414c70cbSLiam Girdwood 		return suspend_set_state(rdev,
793414c70cbSLiam Girdwood 			&rdev->constraints->state_standby);
794414c70cbSLiam Girdwood 	case PM_SUSPEND_MEM:
795414c70cbSLiam Girdwood 		return suspend_set_state(rdev,
796414c70cbSLiam Girdwood 			&rdev->constraints->state_mem);
797414c70cbSLiam Girdwood 	case PM_SUSPEND_MAX:
798414c70cbSLiam Girdwood 		return suspend_set_state(rdev,
799414c70cbSLiam Girdwood 			&rdev->constraints->state_disk);
800414c70cbSLiam Girdwood 	default:
801414c70cbSLiam Girdwood 		return -EINVAL;
802414c70cbSLiam Girdwood 	}
803414c70cbSLiam Girdwood }
804414c70cbSLiam Girdwood 
805414c70cbSLiam Girdwood static void print_constraints(struct regulator_dev *rdev)
806414c70cbSLiam Girdwood {
807414c70cbSLiam Girdwood 	struct regulation_constraints *constraints = rdev->constraints;
808a7068e39SStefan Wahren 	char buf[160] = "";
8095751a99fSStefan Wahren 	size_t len = sizeof(buf) - 1;
8108f031b48SMark Brown 	int count = 0;
8118f031b48SMark Brown 	int ret;
812414c70cbSLiam Girdwood 
8138f031b48SMark Brown 	if (constraints->min_uV && constraints->max_uV) {
814414c70cbSLiam Girdwood 		if (constraints->min_uV == constraints->max_uV)
8155751a99fSStefan Wahren 			count += scnprintf(buf + count, len - count, "%d mV ",
816414c70cbSLiam Girdwood 					   constraints->min_uV / 1000);
817414c70cbSLiam Girdwood 		else
8185751a99fSStefan Wahren 			count += scnprintf(buf + count, len - count,
8195751a99fSStefan Wahren 					   "%d <--> %d mV ",
820414c70cbSLiam Girdwood 					   constraints->min_uV / 1000,
821414c70cbSLiam Girdwood 					   constraints->max_uV / 1000);
8228f031b48SMark Brown 	}
8238f031b48SMark Brown 
8248f031b48SMark Brown 	if (!constraints->min_uV ||
8258f031b48SMark Brown 	    constraints->min_uV != constraints->max_uV) {
8268f031b48SMark Brown 		ret = _regulator_get_voltage(rdev);
8278f031b48SMark Brown 		if (ret > 0)
8285751a99fSStefan Wahren 			count += scnprintf(buf + count, len - count,
8295751a99fSStefan Wahren 					   "at %d mV ", ret / 1000);
8308f031b48SMark Brown 	}
8318f031b48SMark Brown 
832bf5892a8SMark Brown 	if (constraints->uV_offset)
8335751a99fSStefan Wahren 		count += scnprintf(buf + count, len - count, "%dmV offset ",
834bf5892a8SMark Brown 				   constraints->uV_offset / 1000);
835bf5892a8SMark Brown 
8368f031b48SMark Brown 	if (constraints->min_uA && constraints->max_uA) {
837414c70cbSLiam Girdwood 		if (constraints->min_uA == constraints->max_uA)
8385751a99fSStefan Wahren 			count += scnprintf(buf + count, len - count, "%d mA ",
839414c70cbSLiam Girdwood 					   constraints->min_uA / 1000);
840414c70cbSLiam Girdwood 		else
8415751a99fSStefan Wahren 			count += scnprintf(buf + count, len - count,
8425751a99fSStefan Wahren 					   "%d <--> %d mA ",
843414c70cbSLiam Girdwood 					   constraints->min_uA / 1000,
844414c70cbSLiam Girdwood 					   constraints->max_uA / 1000);
845414c70cbSLiam Girdwood 	}
8468f031b48SMark Brown 
8478f031b48SMark Brown 	if (!constraints->min_uA ||
8488f031b48SMark Brown 	    constraints->min_uA != constraints->max_uA) {
8498f031b48SMark Brown 		ret = _regulator_get_current_limit(rdev);
8508f031b48SMark Brown 		if (ret > 0)
8515751a99fSStefan Wahren 			count += scnprintf(buf + count, len - count,
8525751a99fSStefan Wahren 					   "at %d mA ", ret / 1000);
8538f031b48SMark Brown 	}
8548f031b48SMark Brown 
855414c70cbSLiam Girdwood 	if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
8565751a99fSStefan Wahren 		count += scnprintf(buf + count, len - count, "fast ");
857414c70cbSLiam Girdwood 	if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
8585751a99fSStefan Wahren 		count += scnprintf(buf + count, len - count, "normal ");
859414c70cbSLiam Girdwood 	if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
8605751a99fSStefan Wahren 		count += scnprintf(buf + count, len - count, "idle ");
861414c70cbSLiam Girdwood 	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
8625751a99fSStefan Wahren 		count += scnprintf(buf + count, len - count, "standby");
863414c70cbSLiam Girdwood 
864215b8b05SUwe Kleine-König 	if (!count)
8655751a99fSStefan Wahren 		scnprintf(buf, len, "no parameters");
866215b8b05SUwe Kleine-König 
867194dbaefSMark Brown 	rdev_dbg(rdev, "%s\n", buf);
8684a682922SMark Brown 
8694a682922SMark Brown 	if ((constraints->min_uV != constraints->max_uV) &&
8708a34e979SWEN Pingbo 	    !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
8714a682922SMark Brown 		rdev_warn(rdev,
8724a682922SMark Brown 			  "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
873414c70cbSLiam Girdwood }
874414c70cbSLiam Girdwood 
875e79055d6SMark Brown static int machine_constraints_voltage(struct regulator_dev *rdev,
8761083c393SMark Brown 	struct regulation_constraints *constraints)
877e79055d6SMark Brown {
878272e2315SGuodong Xu 	const struct regulator_ops *ops = rdev->desc->ops;
879af5866c9SMark Brown 	int ret;
880af5866c9SMark Brown 
881af5866c9SMark Brown 	/* do we need to apply the constraint voltage */
882af5866c9SMark Brown 	if (rdev->constraints->apply_uV &&
883fa93fd4eSMark Brown 	    rdev->constraints->min_uV && rdev->constraints->max_uV) {
884fa93fd4eSMark Brown 		int target_min, target_max;
885064d5cd1SAlban Bedel 		int current_uV = _regulator_get_voltage(rdev);
886064d5cd1SAlban Bedel 		if (current_uV < 0) {
88769d58839SNishanth Menon 			rdev_err(rdev,
88869d58839SNishanth Menon 				 "failed to get the current voltage(%d)\n",
88969d58839SNishanth Menon 				 current_uV);
890064d5cd1SAlban Bedel 			return current_uV;
891064d5cd1SAlban Bedel 		}
892fa93fd4eSMark Brown 
893fa93fd4eSMark Brown 		/*
894fa93fd4eSMark Brown 		 * If we're below the minimum voltage move up to the
895fa93fd4eSMark Brown 		 * minimum voltage, if we're above the maximum voltage
896fa93fd4eSMark Brown 		 * then move down to the maximum.
897fa93fd4eSMark Brown 		 */
898fa93fd4eSMark Brown 		target_min = current_uV;
899fa93fd4eSMark Brown 		target_max = current_uV;
900fa93fd4eSMark Brown 
901fa93fd4eSMark Brown 		if (current_uV < rdev->constraints->min_uV) {
902fa93fd4eSMark Brown 			target_min = rdev->constraints->min_uV;
903fa93fd4eSMark Brown 			target_max = rdev->constraints->min_uV;
904fa93fd4eSMark Brown 		}
905fa93fd4eSMark Brown 
906fa93fd4eSMark Brown 		if (current_uV > rdev->constraints->max_uV) {
907fa93fd4eSMark Brown 			target_min = rdev->constraints->max_uV;
908fa93fd4eSMark Brown 			target_max = rdev->constraints->max_uV;
909fa93fd4eSMark Brown 		}
910fa93fd4eSMark Brown 
911fa93fd4eSMark Brown 		if (target_min != current_uV || target_max != current_uV) {
91245a91e8fSMark Brown 			rdev_info(rdev, "Bringing %duV into %d-%duV\n",
91345a91e8fSMark Brown 				  current_uV, target_min, target_max);
914064d5cd1SAlban Bedel 			ret = _regulator_do_set_voltage(
915fa93fd4eSMark Brown 				rdev, target_min, target_max);
916af5866c9SMark Brown 			if (ret < 0) {
917064d5cd1SAlban Bedel 				rdev_err(rdev,
918fa93fd4eSMark Brown 					"failed to apply %d-%duV constraint(%d)\n",
919fa93fd4eSMark Brown 					target_min, target_max, ret);
920af5866c9SMark Brown 				return ret;
921af5866c9SMark Brown 			}
922af5866c9SMark Brown 		}
923064d5cd1SAlban Bedel 	}
924e79055d6SMark Brown 
925e79055d6SMark Brown 	/* constrain machine-level voltage specs to fit
926e79055d6SMark Brown 	 * the actual range supported by this regulator.
927e79055d6SMark Brown 	 */
928e79055d6SMark Brown 	if (ops->list_voltage && rdev->desc->n_voltages) {
929e79055d6SMark Brown 		int	count = rdev->desc->n_voltages;
930e79055d6SMark Brown 		int	i;
931e79055d6SMark Brown 		int	min_uV = INT_MAX;
932e79055d6SMark Brown 		int	max_uV = INT_MIN;
933e79055d6SMark Brown 		int	cmin = constraints->min_uV;
934e79055d6SMark Brown 		int	cmax = constraints->max_uV;
935e79055d6SMark Brown 
936e79055d6SMark Brown 		/* it's safe to autoconfigure fixed-voltage supplies
937e79055d6SMark Brown 		   and the constraints are used by list_voltage. */
938e79055d6SMark Brown 		if (count == 1 && !cmin) {
939e79055d6SMark Brown 			cmin = 1;
940e79055d6SMark Brown 			cmax = INT_MAX;
941e79055d6SMark Brown 			constraints->min_uV = cmin;
942e79055d6SMark Brown 			constraints->max_uV = cmax;
943e79055d6SMark Brown 		}
944e79055d6SMark Brown 
945e79055d6SMark Brown 		/* voltage constraints are optional */
946e79055d6SMark Brown 		if ((cmin == 0) && (cmax == 0))
947e79055d6SMark Brown 			return 0;
948e79055d6SMark Brown 
949e79055d6SMark Brown 		/* else require explicit machine-level constraints */
950e79055d6SMark Brown 		if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
9515da84fd9SJoe Perches 			rdev_err(rdev, "invalid voltage constraints\n");
952e79055d6SMark Brown 			return -EINVAL;
953e79055d6SMark Brown 		}
954e79055d6SMark Brown 
955e79055d6SMark Brown 		/* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
956e79055d6SMark Brown 		for (i = 0; i < count; i++) {
957e79055d6SMark Brown 			int	value;
958e79055d6SMark Brown 
959e79055d6SMark Brown 			value = ops->list_voltage(rdev, i);
960e79055d6SMark Brown 			if (value <= 0)
961e79055d6SMark Brown 				continue;
962e79055d6SMark Brown 
963e79055d6SMark Brown 			/* maybe adjust [min_uV..max_uV] */
964e79055d6SMark Brown 			if (value >= cmin && value < min_uV)
965e79055d6SMark Brown 				min_uV = value;
966e79055d6SMark Brown 			if (value <= cmax && value > max_uV)
967e79055d6SMark Brown 				max_uV = value;
968e79055d6SMark Brown 		}
969e79055d6SMark Brown 
970e79055d6SMark Brown 		/* final: [min_uV..max_uV] valid iff constraints valid */
971e79055d6SMark Brown 		if (max_uV < min_uV) {
972fff15befSMark Brown 			rdev_err(rdev,
973fff15befSMark Brown 				 "unsupportable voltage constraints %u-%uuV\n",
974fff15befSMark Brown 				 min_uV, max_uV);
975e79055d6SMark Brown 			return -EINVAL;
976e79055d6SMark Brown 		}
977e79055d6SMark Brown 
978e79055d6SMark Brown 		/* use regulator's subset of machine constraints */
979e79055d6SMark Brown 		if (constraints->min_uV < min_uV) {
9805da84fd9SJoe Perches 			rdev_dbg(rdev, "override min_uV, %d -> %d\n",
9815da84fd9SJoe Perches 				 constraints->min_uV, min_uV);
982e79055d6SMark Brown 			constraints->min_uV = min_uV;
983e79055d6SMark Brown 		}
984e79055d6SMark Brown 		if (constraints->max_uV > max_uV) {
9855da84fd9SJoe Perches 			rdev_dbg(rdev, "override max_uV, %d -> %d\n",
9865da84fd9SJoe Perches 				 constraints->max_uV, max_uV);
987e79055d6SMark Brown 			constraints->max_uV = max_uV;
988e79055d6SMark Brown 		}
989e79055d6SMark Brown 	}
990e79055d6SMark Brown 
991e79055d6SMark Brown 	return 0;
992e79055d6SMark Brown }
993e79055d6SMark Brown 
994f8c1700dSLaxman Dewangan static int machine_constraints_current(struct regulator_dev *rdev,
995f8c1700dSLaxman Dewangan 	struct regulation_constraints *constraints)
996f8c1700dSLaxman Dewangan {
997272e2315SGuodong Xu 	const struct regulator_ops *ops = rdev->desc->ops;
998f8c1700dSLaxman Dewangan 	int ret;
999f8c1700dSLaxman Dewangan 
1000f8c1700dSLaxman Dewangan 	if (!constraints->min_uA && !constraints->max_uA)
1001f8c1700dSLaxman Dewangan 		return 0;
1002f8c1700dSLaxman Dewangan 
1003f8c1700dSLaxman Dewangan 	if (constraints->min_uA > constraints->max_uA) {
1004f8c1700dSLaxman Dewangan 		rdev_err(rdev, "Invalid current constraints\n");
1005f8c1700dSLaxman Dewangan 		return -EINVAL;
1006f8c1700dSLaxman Dewangan 	}
1007f8c1700dSLaxman Dewangan 
1008f8c1700dSLaxman Dewangan 	if (!ops->set_current_limit || !ops->get_current_limit) {
1009f8c1700dSLaxman Dewangan 		rdev_warn(rdev, "Operation of current configuration missing\n");
1010f8c1700dSLaxman Dewangan 		return 0;
1011f8c1700dSLaxman Dewangan 	}
1012f8c1700dSLaxman Dewangan 
1013f8c1700dSLaxman Dewangan 	/* Set regulator current in constraints range */
1014f8c1700dSLaxman Dewangan 	ret = ops->set_current_limit(rdev, constraints->min_uA,
1015f8c1700dSLaxman Dewangan 			constraints->max_uA);
1016f8c1700dSLaxman Dewangan 	if (ret < 0) {
1017f8c1700dSLaxman Dewangan 		rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1018f8c1700dSLaxman Dewangan 		return ret;
1019f8c1700dSLaxman Dewangan 	}
1020f8c1700dSLaxman Dewangan 
1021f8c1700dSLaxman Dewangan 	return 0;
1022f8c1700dSLaxman Dewangan }
1023f8c1700dSLaxman Dewangan 
102430c21971SMarkus Pargmann static int _regulator_do_enable(struct regulator_dev *rdev);
102530c21971SMarkus Pargmann 
1026a5766f11SLiam Girdwood /**
1027a5766f11SLiam Girdwood  * set_machine_constraints - sets regulator constraints
102869279fb9SMark Brown  * @rdev: regulator source
1029c8e7e464SMark Brown  * @constraints: constraints to apply
1030a5766f11SLiam Girdwood  *
1031a5766f11SLiam Girdwood  * Allows platform initialisation code to define and constrain
1032a5766f11SLiam Girdwood  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
1033a5766f11SLiam Girdwood  * Constraints *must* be set by platform code in order for some
1034a5766f11SLiam Girdwood  * regulator operations to proceed i.e. set_voltage, set_current_limit,
1035a5766f11SLiam Girdwood  * set_mode.
1036a5766f11SLiam Girdwood  */
1037a5766f11SLiam Girdwood static int set_machine_constraints(struct regulator_dev *rdev,
1038f8c12fe3SMark Brown 	const struct regulation_constraints *constraints)
1039a5766f11SLiam Girdwood {
1040a5766f11SLiam Girdwood 	int ret = 0;
1041272e2315SGuodong Xu 	const struct regulator_ops *ops = rdev->desc->ops;
1042e06f5b4fSMark Brown 
10439a8f5e07SMark Brown 	if (constraints)
1044f8c12fe3SMark Brown 		rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1045f8c12fe3SMark Brown 					    GFP_KERNEL);
10469a8f5e07SMark Brown 	else
10479a8f5e07SMark Brown 		rdev->constraints = kzalloc(sizeof(*constraints),
10489a8f5e07SMark Brown 					    GFP_KERNEL);
1049f8c12fe3SMark Brown 	if (!rdev->constraints)
1050f8c12fe3SMark Brown 		return -ENOMEM;
1051af5866c9SMark Brown 
1052f8c12fe3SMark Brown 	ret = machine_constraints_voltage(rdev, rdev->constraints);
1053e79055d6SMark Brown 	if (ret != 0)
10546333ef46SCharles Keepax 		return ret;
10553e2b9abdSMark Brown 
1056f8c1700dSLaxman Dewangan 	ret = machine_constraints_current(rdev, rdev->constraints);
1057f8c1700dSLaxman Dewangan 	if (ret != 0)
10586333ef46SCharles Keepax 		return ret;
1059f8c1700dSLaxman Dewangan 
106036e4f839SStephen Boyd 	if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
106136e4f839SStephen Boyd 		ret = ops->set_input_current_limit(rdev,
106236e4f839SStephen Boyd 						   rdev->constraints->ilim_uA);
106336e4f839SStephen Boyd 		if (ret < 0) {
106436e4f839SStephen Boyd 			rdev_err(rdev, "failed to set input limit\n");
10656333ef46SCharles Keepax 			return ret;
106636e4f839SStephen Boyd 		}
106736e4f839SStephen Boyd 	}
106836e4f839SStephen Boyd 
1069a5766f11SLiam Girdwood 	/* do we need to setup our suspend state */
10709a8f5e07SMark Brown 	if (rdev->constraints->initial_state) {
1071f8c12fe3SMark Brown 		ret = suspend_prepare(rdev, rdev->constraints->initial_state);
1072e06f5b4fSMark Brown 		if (ret < 0) {
10735da84fd9SJoe Perches 			rdev_err(rdev, "failed to set suspend state\n");
10746333ef46SCharles Keepax 			return ret;
1075e06f5b4fSMark Brown 		}
1076e06f5b4fSMark Brown 	}
1077a5766f11SLiam Girdwood 
10789a8f5e07SMark Brown 	if (rdev->constraints->initial_mode) {
1079a308466cSMark Brown 		if (!ops->set_mode) {
10805da84fd9SJoe Perches 			rdev_err(rdev, "no set_mode operation\n");
10816333ef46SCharles Keepax 			return -EINVAL;
1082a308466cSMark Brown 		}
1083a308466cSMark Brown 
1084f8c12fe3SMark Brown 		ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1085a308466cSMark Brown 		if (ret < 0) {
10865da84fd9SJoe Perches 			rdev_err(rdev, "failed to set initial mode: %d\n", ret);
10876333ef46SCharles Keepax 			return ret;
1088a308466cSMark Brown 		}
1089a308466cSMark Brown 	}
1090a308466cSMark Brown 
1091cacf90f2SMark Brown 	/* If the constraints say the regulator should be on at this point
1092cacf90f2SMark Brown 	 * and we have control then make sure it is enabled.
1093cacf90f2SMark Brown 	 */
109430c21971SMarkus Pargmann 	if (rdev->constraints->always_on || rdev->constraints->boot_on) {
109530c21971SMarkus Pargmann 		ret = _regulator_do_enable(rdev);
109630c21971SMarkus Pargmann 		if (ret < 0 && ret != -EINVAL) {
10975da84fd9SJoe Perches 			rdev_err(rdev, "failed to enable\n");
10986333ef46SCharles Keepax 			return ret;
1099e5fda26cSMark Brown 		}
1100e5fda26cSMark Brown 	}
1101e5fda26cSMark Brown 
11021653ccf4SYadwinder Singh Brar 	if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
11031653ccf4SYadwinder Singh Brar 		&& ops->set_ramp_delay) {
11046f0b2c69SYadwinder Singh Brar 		ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
11056f0b2c69SYadwinder Singh Brar 		if (ret < 0) {
11066f0b2c69SYadwinder Singh Brar 			rdev_err(rdev, "failed to set ramp_delay\n");
11076333ef46SCharles Keepax 			return ret;
11086f0b2c69SYadwinder Singh Brar 		}
11096f0b2c69SYadwinder Singh Brar 	}
11106f0b2c69SYadwinder Singh Brar 
111123c779b9SStephen Boyd 	if (rdev->constraints->pull_down && ops->set_pull_down) {
111223c779b9SStephen Boyd 		ret = ops->set_pull_down(rdev);
111323c779b9SStephen Boyd 		if (ret < 0) {
111423c779b9SStephen Boyd 			rdev_err(rdev, "failed to set pull down\n");
11156333ef46SCharles Keepax 			return ret;
111623c779b9SStephen Boyd 		}
111723c779b9SStephen Boyd 	}
111823c779b9SStephen Boyd 
111957f66b78SStephen Boyd 	if (rdev->constraints->soft_start && ops->set_soft_start) {
112057f66b78SStephen Boyd 		ret = ops->set_soft_start(rdev);
112157f66b78SStephen Boyd 		if (ret < 0) {
112257f66b78SStephen Boyd 			rdev_err(rdev, "failed to set soft start\n");
11236333ef46SCharles Keepax 			return ret;
112457f66b78SStephen Boyd 		}
112557f66b78SStephen Boyd 	}
112657f66b78SStephen Boyd 
11273a003baeSStephen Boyd 	if (rdev->constraints->over_current_protection
11283a003baeSStephen Boyd 		&& ops->set_over_current_protection) {
11293a003baeSStephen Boyd 		ret = ops->set_over_current_protection(rdev);
11303a003baeSStephen Boyd 		if (ret < 0) {
11313a003baeSStephen Boyd 			rdev_err(rdev, "failed to set over current protection\n");
11326333ef46SCharles Keepax 			return ret;
11333a003baeSStephen Boyd 		}
11343a003baeSStephen Boyd 	}
11353a003baeSStephen Boyd 
1136670666b9SLaxman Dewangan 	if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1137670666b9SLaxman Dewangan 		bool ad_state = (rdev->constraints->active_discharge ==
1138670666b9SLaxman Dewangan 			      REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1139670666b9SLaxman Dewangan 
1140670666b9SLaxman Dewangan 		ret = ops->set_active_discharge(rdev, ad_state);
1141670666b9SLaxman Dewangan 		if (ret < 0) {
1142670666b9SLaxman Dewangan 			rdev_err(rdev, "failed to set active discharge\n");
1143670666b9SLaxman Dewangan 			return ret;
1144670666b9SLaxman Dewangan 		}
1145670666b9SLaxman Dewangan 	}
1146670666b9SLaxman Dewangan 
1147a5766f11SLiam Girdwood 	print_constraints(rdev);
11481a6958e7SAxel Lin 	return 0;
1149a5766f11SLiam Girdwood }
1150a5766f11SLiam Girdwood 
1151a5766f11SLiam Girdwood /**
1152a5766f11SLiam Girdwood  * set_supply - set regulator supply regulator
115369279fb9SMark Brown  * @rdev: regulator name
115469279fb9SMark Brown  * @supply_rdev: supply regulator name
1155a5766f11SLiam Girdwood  *
1156a5766f11SLiam Girdwood  * Called by platform initialisation code to set the supply regulator for this
1157a5766f11SLiam Girdwood  * regulator. This ensures that a regulators supply will also be enabled by the
1158a5766f11SLiam Girdwood  * core if it's child is enabled.
1159a5766f11SLiam Girdwood  */
1160a5766f11SLiam Girdwood static int set_supply(struct regulator_dev *rdev,
1161a5766f11SLiam Girdwood 		      struct regulator_dev *supply_rdev)
1162a5766f11SLiam Girdwood {
1163a5766f11SLiam Girdwood 	int err;
1164a5766f11SLiam Girdwood 
11653801b86aSMark Brown 	rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
11663801b86aSMark Brown 
1167e2c09ae7SJavier Martinez Canillas 	if (!try_module_get(supply_rdev->owner))
1168e2c09ae7SJavier Martinez Canillas 		return -ENODEV;
1169e2c09ae7SJavier Martinez Canillas 
11703801b86aSMark Brown 	rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
117132c78de8SAxel Lin 	if (rdev->supply == NULL) {
117232c78de8SAxel Lin 		err = -ENOMEM;
1173a5766f11SLiam Girdwood 		return err;
1174a5766f11SLiam Girdwood 	}
117557ad526aSLaxman Dewangan 	supply_rdev->open_count++;
1176a5766f11SLiam Girdwood 
11773801b86aSMark Brown 	return 0;
11783801b86aSMark Brown }
11793801b86aSMark Brown 
1180a5766f11SLiam Girdwood /**
118106c63f93SRandy Dunlap  * set_consumer_device_supply - Bind a regulator to a symbolic supply
118269279fb9SMark Brown  * @rdev:         regulator source
118340f9244fSMark Brown  * @consumer_dev_name: dev_name() string for device supply applies to
1184a5766f11SLiam Girdwood  * @supply:       symbolic name for supply
1185a5766f11SLiam Girdwood  *
1186a5766f11SLiam Girdwood  * Allows platform initialisation code to map physical regulator
1187a5766f11SLiam Girdwood  * sources to symbolic names for supplies for use by devices.  Devices
1188a5766f11SLiam Girdwood  * should use these symbolic names to request regulators, avoiding the
1189a5766f11SLiam Girdwood  * need to provide board-specific regulator names as platform data.
1190a5766f11SLiam Girdwood  */
1191a5766f11SLiam Girdwood static int set_consumer_device_supply(struct regulator_dev *rdev,
1192737f360dSMark Brown 				      const char *consumer_dev_name,
119340f9244fSMark Brown 				      const char *supply)
1194a5766f11SLiam Girdwood {
1195a5766f11SLiam Girdwood 	struct regulator_map *node;
11969ed2099eSMark Brown 	int has_dev;
1197a5766f11SLiam Girdwood 
1198a5766f11SLiam Girdwood 	if (supply == NULL)
1199a5766f11SLiam Girdwood 		return -EINVAL;
1200a5766f11SLiam Girdwood 
12019ed2099eSMark Brown 	if (consumer_dev_name != NULL)
12029ed2099eSMark Brown 		has_dev = 1;
12039ed2099eSMark Brown 	else
12049ed2099eSMark Brown 		has_dev = 0;
12059ed2099eSMark Brown 
12066001e13cSDavid Brownell 	list_for_each_entry(node, &regulator_map_list, list) {
120723b5cc2aSJani Nikula 		if (node->dev_name && consumer_dev_name) {
120823b5cc2aSJani Nikula 			if (strcmp(node->dev_name, consumer_dev_name) != 0)
12096001e13cSDavid Brownell 				continue;
121023b5cc2aSJani Nikula 		} else if (node->dev_name || consumer_dev_name) {
121123b5cc2aSJani Nikula 			continue;
121223b5cc2aSJani Nikula 		}
121323b5cc2aSJani Nikula 
12146001e13cSDavid Brownell 		if (strcmp(node->supply, supply) != 0)
12156001e13cSDavid Brownell 			continue;
12166001e13cSDavid Brownell 
1217737f360dSMark Brown 		pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1218737f360dSMark Brown 			 consumer_dev_name,
12196001e13cSDavid Brownell 			 dev_name(&node->regulator->dev),
12206001e13cSDavid Brownell 			 node->regulator->desc->name,
12216001e13cSDavid Brownell 			 supply,
12221083c393SMark Brown 			 dev_name(&rdev->dev), rdev_get_name(rdev));
12236001e13cSDavid Brownell 		return -EBUSY;
12246001e13cSDavid Brownell 	}
12256001e13cSDavid Brownell 
12269ed2099eSMark Brown 	node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1227a5766f11SLiam Girdwood 	if (node == NULL)
1228a5766f11SLiam Girdwood 		return -ENOMEM;
1229a5766f11SLiam Girdwood 
1230a5766f11SLiam Girdwood 	node->regulator = rdev;
1231a5766f11SLiam Girdwood 	node->supply = supply;
1232a5766f11SLiam Girdwood 
12339ed2099eSMark Brown 	if (has_dev) {
12349ed2099eSMark Brown 		node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
123540f9244fSMark Brown 		if (node->dev_name == NULL) {
123640f9244fSMark Brown 			kfree(node);
123740f9244fSMark Brown 			return -ENOMEM;
123840f9244fSMark Brown 		}
12399ed2099eSMark Brown 	}
124040f9244fSMark Brown 
1241a5766f11SLiam Girdwood 	list_add(&node->list, &regulator_map_list);
1242a5766f11SLiam Girdwood 	return 0;
1243a5766f11SLiam Girdwood }
1244a5766f11SLiam Girdwood 
12450f1d747bSMike Rapoport static void unset_regulator_supplies(struct regulator_dev *rdev)
12460f1d747bSMike Rapoport {
12470f1d747bSMike Rapoport 	struct regulator_map *node, *n;
12480f1d747bSMike Rapoport 
12490f1d747bSMike Rapoport 	list_for_each_entry_safe(node, n, &regulator_map_list, list) {
12500f1d747bSMike Rapoport 		if (rdev == node->regulator) {
12510f1d747bSMike Rapoport 			list_del(&node->list);
125240f9244fSMark Brown 			kfree(node->dev_name);
12530f1d747bSMike Rapoport 			kfree(node);
12540f1d747bSMike Rapoport 		}
12550f1d747bSMike Rapoport 	}
12560f1d747bSMike Rapoport }
12570f1d747bSMike Rapoport 
12582d80a91bSRichard Fitzgerald #ifdef CONFIG_DEBUG_FS
12592d80a91bSRichard Fitzgerald static ssize_t constraint_flags_read_file(struct file *file,
12602d80a91bSRichard Fitzgerald 					  char __user *user_buf,
12612d80a91bSRichard Fitzgerald 					  size_t count, loff_t *ppos)
12622d80a91bSRichard Fitzgerald {
12632d80a91bSRichard Fitzgerald 	const struct regulator *regulator = file->private_data;
12642d80a91bSRichard Fitzgerald 	const struct regulation_constraints *c = regulator->rdev->constraints;
12652d80a91bSRichard Fitzgerald 	char *buf;
12662d80a91bSRichard Fitzgerald 	ssize_t ret;
12672d80a91bSRichard Fitzgerald 
12682d80a91bSRichard Fitzgerald 	if (!c)
12692d80a91bSRichard Fitzgerald 		return 0;
12702d80a91bSRichard Fitzgerald 
12712d80a91bSRichard Fitzgerald 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
12722d80a91bSRichard Fitzgerald 	if (!buf)
12732d80a91bSRichard Fitzgerald 		return -ENOMEM;
12742d80a91bSRichard Fitzgerald 
12752d80a91bSRichard Fitzgerald 	ret = snprintf(buf, PAGE_SIZE,
12762d80a91bSRichard Fitzgerald 			"always_on: %u\n"
12772d80a91bSRichard Fitzgerald 			"boot_on: %u\n"
12782d80a91bSRichard Fitzgerald 			"apply_uV: %u\n"
12792d80a91bSRichard Fitzgerald 			"ramp_disable: %u\n"
12802d80a91bSRichard Fitzgerald 			"soft_start: %u\n"
12812d80a91bSRichard Fitzgerald 			"pull_down: %u\n"
12822d80a91bSRichard Fitzgerald 			"over_current_protection: %u\n",
12832d80a91bSRichard Fitzgerald 			c->always_on,
12842d80a91bSRichard Fitzgerald 			c->boot_on,
12852d80a91bSRichard Fitzgerald 			c->apply_uV,
12862d80a91bSRichard Fitzgerald 			c->ramp_disable,
12872d80a91bSRichard Fitzgerald 			c->soft_start,
12882d80a91bSRichard Fitzgerald 			c->pull_down,
12892d80a91bSRichard Fitzgerald 			c->over_current_protection);
12902d80a91bSRichard Fitzgerald 
12912d80a91bSRichard Fitzgerald 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
12922d80a91bSRichard Fitzgerald 	kfree(buf);
12932d80a91bSRichard Fitzgerald 
12942d80a91bSRichard Fitzgerald 	return ret;
12952d80a91bSRichard Fitzgerald }
12962d80a91bSRichard Fitzgerald 
12972d80a91bSRichard Fitzgerald #endif
12982d80a91bSRichard Fitzgerald 
12992d80a91bSRichard Fitzgerald static const struct file_operations constraint_flags_fops = {
13002d80a91bSRichard Fitzgerald #ifdef CONFIG_DEBUG_FS
13012d80a91bSRichard Fitzgerald 	.open = simple_open,
13022d80a91bSRichard Fitzgerald 	.read = constraint_flags_read_file,
13032d80a91bSRichard Fitzgerald 	.llseek = default_llseek,
13042d80a91bSRichard Fitzgerald #endif
13052d80a91bSRichard Fitzgerald };
13062d80a91bSRichard Fitzgerald 
1307f5726ae3SMark Brown #define REG_STR_SIZE	64
1308414c70cbSLiam Girdwood 
1309414c70cbSLiam Girdwood static struct regulator *create_regulator(struct regulator_dev *rdev,
1310414c70cbSLiam Girdwood 					  struct device *dev,
1311414c70cbSLiam Girdwood 					  const char *supply_name)
1312414c70cbSLiam Girdwood {
1313414c70cbSLiam Girdwood 	struct regulator *regulator;
1314414c70cbSLiam Girdwood 	char buf[REG_STR_SIZE];
1315414c70cbSLiam Girdwood 	int err, size;
1316414c70cbSLiam Girdwood 
1317414c70cbSLiam Girdwood 	regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1318414c70cbSLiam Girdwood 	if (regulator == NULL)
1319414c70cbSLiam Girdwood 		return NULL;
1320414c70cbSLiam Girdwood 
1321414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
1322414c70cbSLiam Girdwood 	regulator->rdev = rdev;
1323414c70cbSLiam Girdwood 	list_add(&regulator->list, &rdev->consumer_list);
1324414c70cbSLiam Girdwood 
1325414c70cbSLiam Girdwood 	if (dev) {
1326e2c98eafSShawn Guo 		regulator->dev = dev;
1327e2c98eafSShawn Guo 
1328222cc7b1SMark Brown 		/* Add a link to the device sysfs entry */
1329b7cd1b13SBartosz Golaszewski 		size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1330414c70cbSLiam Girdwood 				dev->kobj.name, supply_name);
1331414c70cbSLiam Girdwood 		if (size >= REG_STR_SIZE)
1332222cc7b1SMark Brown 			goto overflow_err;
1333414c70cbSLiam Girdwood 
1334414c70cbSLiam Girdwood 		regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1335414c70cbSLiam Girdwood 		if (regulator->supply_name == NULL)
1336222cc7b1SMark Brown 			goto overflow_err;
1337414c70cbSLiam Girdwood 
1338ff268b56SStephen Boyd 		err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1339414c70cbSLiam Girdwood 					buf);
1340414c70cbSLiam Girdwood 		if (err) {
1341ff268b56SStephen Boyd 			rdev_dbg(rdev, "could not add device link %s err %d\n",
13421d7372e1SDaniel Walker 				  dev->kobj.name, err);
1343222cc7b1SMark Brown 			/* non-fatal */
1344414c70cbSLiam Girdwood 		}
13455de70519SMark Brown 	} else {
13460630b614SStephen Boyd 		regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
13475de70519SMark Brown 		if (regulator->supply_name == NULL)
1348222cc7b1SMark Brown 			goto overflow_err;
1349414c70cbSLiam Girdwood 	}
13505de70519SMark Brown 
13515de70519SMark Brown 	regulator->debugfs = debugfs_create_dir(regulator->supply_name,
13525de70519SMark Brown 						rdev->debugfs);
135324751434SStephen Boyd 	if (!regulator->debugfs) {
1354ad3a942bSStephen Boyd 		rdev_dbg(rdev, "Failed to create debugfs directory\n");
13555de70519SMark Brown 	} else {
13565de70519SMark Brown 		debugfs_create_u32("uA_load", 0444, regulator->debugfs,
13575de70519SMark Brown 				   &regulator->uA_load);
13585de70519SMark Brown 		debugfs_create_u32("min_uV", 0444, regulator->debugfs,
13595de70519SMark Brown 				   &regulator->min_uV);
13605de70519SMark Brown 		debugfs_create_u32("max_uV", 0444, regulator->debugfs,
13615de70519SMark Brown 				   &regulator->max_uV);
13622d80a91bSRichard Fitzgerald 		debugfs_create_file("constraint_flags", 0444,
13632d80a91bSRichard Fitzgerald 				    regulator->debugfs, regulator,
13642d80a91bSRichard Fitzgerald 				    &constraint_flags_fops);
13655de70519SMark Brown 	}
13665de70519SMark Brown 
13676492bc1bSMark Brown 	/*
13686492bc1bSMark Brown 	 * Check now if the regulator is an always on regulator - if
13696492bc1bSMark Brown 	 * it is then we don't need to do nearly so much work for
13706492bc1bSMark Brown 	 * enable/disable calls.
13716492bc1bSMark Brown 	 */
13728a34e979SWEN Pingbo 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
13736492bc1bSMark Brown 	    _regulator_is_enabled(rdev))
13746492bc1bSMark Brown 		regulator->always_on = true;
13756492bc1bSMark Brown 
1376414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
1377414c70cbSLiam Girdwood 	return regulator;
1378414c70cbSLiam Girdwood overflow_err:
1379414c70cbSLiam Girdwood 	list_del(&regulator->list);
1380414c70cbSLiam Girdwood 	kfree(regulator);
1381414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
1382414c70cbSLiam Girdwood 	return NULL;
1383414c70cbSLiam Girdwood }
1384414c70cbSLiam Girdwood 
138531aae2beSMark Brown static int _regulator_get_enable_time(struct regulator_dev *rdev)
138631aae2beSMark Brown {
138700c877c6SLaxman Dewangan 	if (rdev->constraints && rdev->constraints->enable_time)
138800c877c6SLaxman Dewangan 		return rdev->constraints->enable_time;
138931aae2beSMark Brown 	if (!rdev->desc->ops->enable_time)
139079511ed3SMark Brown 		return rdev->desc->enable_time;
139131aae2beSMark Brown 	return rdev->desc->ops->enable_time(rdev);
139231aae2beSMark Brown }
139331aae2beSMark Brown 
1394a06ccd9cSCharles Keepax static struct regulator_supply_alias *regulator_find_supply_alias(
1395a06ccd9cSCharles Keepax 		struct device *dev, const char *supply)
1396a06ccd9cSCharles Keepax {
1397a06ccd9cSCharles Keepax 	struct regulator_supply_alias *map;
1398a06ccd9cSCharles Keepax 
1399a06ccd9cSCharles Keepax 	list_for_each_entry(map, &regulator_supply_alias_list, list)
1400a06ccd9cSCharles Keepax 		if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1401a06ccd9cSCharles Keepax 			return map;
1402a06ccd9cSCharles Keepax 
1403a06ccd9cSCharles Keepax 	return NULL;
1404a06ccd9cSCharles Keepax }
1405a06ccd9cSCharles Keepax 
1406a06ccd9cSCharles Keepax static void regulator_supply_alias(struct device **dev, const char **supply)
1407a06ccd9cSCharles Keepax {
1408a06ccd9cSCharles Keepax 	struct regulator_supply_alias *map;
1409a06ccd9cSCharles Keepax 
1410a06ccd9cSCharles Keepax 	map = regulator_find_supply_alias(*dev, *supply);
1411a06ccd9cSCharles Keepax 	if (map) {
1412a06ccd9cSCharles Keepax 		dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1413a06ccd9cSCharles Keepax 				*supply, map->alias_supply,
1414a06ccd9cSCharles Keepax 				dev_name(map->alias_dev));
1415a06ccd9cSCharles Keepax 		*dev = map->alias_dev;
1416a06ccd9cSCharles Keepax 		*supply = map->alias_supply;
1417a06ccd9cSCharles Keepax 	}
1418a06ccd9cSCharles Keepax }
1419a06ccd9cSCharles Keepax 
142085f3b431STomeu Vizoso static int of_node_match(struct device *dev, const void *data)
142185f3b431STomeu Vizoso {
142285f3b431STomeu Vizoso 	return dev->of_node == data;
142385f3b431STomeu Vizoso }
142485f3b431STomeu Vizoso 
142585f3b431STomeu Vizoso static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
142685f3b431STomeu Vizoso {
142785f3b431STomeu Vizoso 	struct device *dev;
142885f3b431STomeu Vizoso 
142985f3b431STomeu Vizoso 	dev = class_find_device(&regulator_class, NULL, np, of_node_match);
143085f3b431STomeu Vizoso 
143185f3b431STomeu Vizoso 	return dev ? dev_to_rdev(dev) : NULL;
143285f3b431STomeu Vizoso }
143385f3b431STomeu Vizoso 
143485f3b431STomeu Vizoso static int regulator_match(struct device *dev, const void *data)
143585f3b431STomeu Vizoso {
143685f3b431STomeu Vizoso 	struct regulator_dev *r = dev_to_rdev(dev);
143785f3b431STomeu Vizoso 
143885f3b431STomeu Vizoso 	return strcmp(rdev_get_name(r), data) == 0;
143985f3b431STomeu Vizoso }
144085f3b431STomeu Vizoso 
144185f3b431STomeu Vizoso static struct regulator_dev *regulator_lookup_by_name(const char *name)
144285f3b431STomeu Vizoso {
144385f3b431STomeu Vizoso 	struct device *dev;
144485f3b431STomeu Vizoso 
144585f3b431STomeu Vizoso 	dev = class_find_device(&regulator_class, NULL, name, regulator_match);
144685f3b431STomeu Vizoso 
144785f3b431STomeu Vizoso 	return dev ? dev_to_rdev(dev) : NULL;
144885f3b431STomeu Vizoso }
144985f3b431STomeu Vizoso 
145085f3b431STomeu Vizoso /**
145185f3b431STomeu Vizoso  * regulator_dev_lookup - lookup a regulator device.
145285f3b431STomeu Vizoso  * @dev: device for regulator "consumer".
145385f3b431STomeu Vizoso  * @supply: Supply name or regulator ID.
145485f3b431STomeu Vizoso  *
145585f3b431STomeu Vizoso  * If successful, returns a struct regulator_dev that corresponds to the name
1456163478daSDmitry Torokhov  * @supply and with the embedded struct device refcount incremented by one.
1457163478daSDmitry Torokhov  * The refcount must be dropped by calling put_device().
1458163478daSDmitry Torokhov  * On failure one of the following ERR-PTR-encoded values is returned:
1459163478daSDmitry Torokhov  * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1460163478daSDmitry Torokhov  * in the future.
146185f3b431STomeu Vizoso  */
146269511a45SRajendra Nayak static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1463163478daSDmitry Torokhov 						  const char *supply)
146469511a45SRajendra Nayak {
146506217197SCharles Keepax 	struct regulator_dev *r = NULL;
146669511a45SRajendra Nayak 	struct device_node *node;
1467576ca436SMark Brown 	struct regulator_map *map;
1468576ca436SMark Brown 	const char *devname = NULL;
146969511a45SRajendra Nayak 
1470a06ccd9cSCharles Keepax 	regulator_supply_alias(&dev, &supply);
1471a06ccd9cSCharles Keepax 
147269511a45SRajendra Nayak 	/* first do a dt based lookup */
147369511a45SRajendra Nayak 	if (dev && dev->of_node) {
147469511a45SRajendra Nayak 		node = of_get_regulator(dev, supply);
14756d191a5fSMark Brown 		if (node) {
147685f3b431STomeu Vizoso 			r = of_find_regulator_by_node(node);
147785f3b431STomeu Vizoso 			if (r)
147869511a45SRajendra Nayak 				return r;
1479163478daSDmitry Torokhov 
14806d191a5fSMark Brown 			/*
1481163478daSDmitry Torokhov 			 * We have a node, but there is no device.
1482163478daSDmitry Torokhov 			 * assume it has not registered yet.
14836d191a5fSMark Brown 			 */
1484163478daSDmitry Torokhov 			return ERR_PTR(-EPROBE_DEFER);
14856d191a5fSMark Brown 		}
148669511a45SRajendra Nayak 	}
148769511a45SRajendra Nayak 
148869511a45SRajendra Nayak 	/* if not found, try doing it non-dt way */
1489576ca436SMark Brown 	if (dev)
1490576ca436SMark Brown 		devname = dev_name(dev);
1491576ca436SMark Brown 
149285f3b431STomeu Vizoso 	mutex_lock(&regulator_list_mutex);
1493576ca436SMark Brown 	list_for_each_entry(map, &regulator_map_list, list) {
1494576ca436SMark Brown 		/* If the mapping has a device set up it must match */
1495576ca436SMark Brown 		if (map->dev_name &&
1496576ca436SMark Brown 		    (!devname || strcmp(map->dev_name, devname)))
1497576ca436SMark Brown 			continue;
1498576ca436SMark Brown 
149985f3b431STomeu Vizoso 		if (strcmp(map->supply, supply) == 0 &&
150085f3b431STomeu Vizoso 		    get_device(&map->regulator->dev)) {
1501163478daSDmitry Torokhov 			r = map->regulator;
1502163478daSDmitry Torokhov 			break;
1503576ca436SMark Brown 		}
150485f3b431STomeu Vizoso 	}
150585f3b431STomeu Vizoso 	mutex_unlock(&regulator_list_mutex);
1506576ca436SMark Brown 
1507163478daSDmitry Torokhov 	if (r)
1508163478daSDmitry Torokhov 		return r;
1509163478daSDmitry Torokhov 
151006217197SCharles Keepax 	r = regulator_lookup_by_name(supply);
151106217197SCharles Keepax 	if (r)
151206217197SCharles Keepax 		return r;
151306217197SCharles Keepax 
1514163478daSDmitry Torokhov 	return ERR_PTR(-ENODEV);
151569511a45SRajendra Nayak }
151669511a45SRajendra Nayak 
15176261b06dSBjorn Andersson static int regulator_resolve_supply(struct regulator_dev *rdev)
15186261b06dSBjorn Andersson {
15196261b06dSBjorn Andersson 	struct regulator_dev *r;
15206261b06dSBjorn Andersson 	struct device *dev = rdev->dev.parent;
15216261b06dSBjorn Andersson 	int ret;
15226261b06dSBjorn Andersson 
15236261b06dSBjorn Andersson 	/* No supply to resovle? */
15246261b06dSBjorn Andersson 	if (!rdev->supply_name)
15256261b06dSBjorn Andersson 		return 0;
15266261b06dSBjorn Andersson 
15276261b06dSBjorn Andersson 	/* Supply already resolved? */
15286261b06dSBjorn Andersson 	if (rdev->supply)
15296261b06dSBjorn Andersson 		return 0;
15306261b06dSBjorn Andersson 
1531163478daSDmitry Torokhov 	r = regulator_dev_lookup(dev, rdev->supply_name);
1532163478daSDmitry Torokhov 	if (IS_ERR(r)) {
1533163478daSDmitry Torokhov 		ret = PTR_ERR(r);
1534163478daSDmitry Torokhov 
153506423121SMark Brown 		/* Did the lookup explicitly defer for us? */
153606423121SMark Brown 		if (ret == -EPROBE_DEFER)
153706423121SMark Brown 			return ret;
153806423121SMark Brown 
15399f7e25edSMark Brown 		if (have_full_constraints()) {
15409f7e25edSMark Brown 			r = dummy_regulator_rdev;
154185f3b431STomeu Vizoso 			get_device(&r->dev);
15429f7e25edSMark Brown 		} else {
15436261b06dSBjorn Andersson 			dev_err(dev, "Failed to resolve %s-supply for %s\n",
15446261b06dSBjorn Andersson 				rdev->supply_name, rdev->desc->name);
15456261b06dSBjorn Andersson 			return -EPROBE_DEFER;
15466261b06dSBjorn Andersson 		}
15479f7e25edSMark Brown 	}
15486261b06dSBjorn Andersson 
154966d228a2SJon Hunter 	/*
155066d228a2SJon Hunter 	 * If the supply's parent device is not the same as the
155166d228a2SJon Hunter 	 * regulator's parent device, then ensure the parent device
155266d228a2SJon Hunter 	 * is bound before we resolve the supply, in case the parent
155366d228a2SJon Hunter 	 * device get probe deferred and unregisters the supply.
155466d228a2SJon Hunter 	 */
155566d228a2SJon Hunter 	if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
155666d228a2SJon Hunter 		if (!device_is_bound(r->dev.parent)) {
155766d228a2SJon Hunter 			put_device(&r->dev);
155866d228a2SJon Hunter 			return -EPROBE_DEFER;
155966d228a2SJon Hunter 		}
156066d228a2SJon Hunter 	}
156166d228a2SJon Hunter 
15626261b06dSBjorn Andersson 	/* Recursively resolve the supply of the supply */
15636261b06dSBjorn Andersson 	ret = regulator_resolve_supply(r);
156485f3b431STomeu Vizoso 	if (ret < 0) {
156585f3b431STomeu Vizoso 		put_device(&r->dev);
15666261b06dSBjorn Andersson 		return ret;
156785f3b431STomeu Vizoso 	}
15686261b06dSBjorn Andersson 
15696261b06dSBjorn Andersson 	ret = set_supply(rdev, r);
157085f3b431STomeu Vizoso 	if (ret < 0) {
157185f3b431STomeu Vizoso 		put_device(&r->dev);
15726261b06dSBjorn Andersson 		return ret;
157385f3b431STomeu Vizoso 	}
15746261b06dSBjorn Andersson 
15756261b06dSBjorn Andersson 	/* Cascade always-on state to supply */
157695a293c7SJavier Martinez Canillas 	if (_regulator_is_enabled(rdev)) {
15776261b06dSBjorn Andersson 		ret = regulator_enable(rdev->supply);
157836a1f1b6SJavier Martinez Canillas 		if (ret < 0) {
157936a1f1b6SJavier Martinez Canillas 			_regulator_put(rdev->supply);
15808e5356a7SJon Hunter 			rdev->supply = NULL;
15816261b06dSBjorn Andersson 			return ret;
15826261b06dSBjorn Andersson 		}
158336a1f1b6SJavier Martinez Canillas 	}
15846261b06dSBjorn Andersson 
15856261b06dSBjorn Andersson 	return 0;
15866261b06dSBjorn Andersson }
15876261b06dSBjorn Andersson 
15885ffbd136SMark Brown /* Internal regulator request function */
1589a8bd42a9SDmitry Torokhov struct regulator *_regulator_get(struct device *dev, const char *id,
1590a8bd42a9SDmitry Torokhov 				 enum regulator_get_type get_type)
1591414c70cbSLiam Girdwood {
1592414c70cbSLiam Girdwood 	struct regulator_dev *rdev;
15937d245afaSDmitry Torokhov 	struct regulator *regulator;
1594a4d7641fSDmitry Torokhov 	const char *devname = dev ? dev_name(dev) : "deviceless";
1595317b5684SMark Brown 	int ret;
1596414c70cbSLiam Girdwood 
1597a8bd42a9SDmitry Torokhov 	if (get_type >= MAX_GET_TYPE) {
1598a8bd42a9SDmitry Torokhov 		dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1599a8bd42a9SDmitry Torokhov 		return ERR_PTR(-EINVAL);
1600a8bd42a9SDmitry Torokhov 	}
1601a8bd42a9SDmitry Torokhov 
1602414c70cbSLiam Girdwood 	if (id == NULL) {
16035da84fd9SJoe Perches 		pr_err("get() with no identifier\n");
1604043c998fSMark Brown 		return ERR_PTR(-EINVAL);
1605414c70cbSLiam Girdwood 	}
1606414c70cbSLiam Girdwood 
1607163478daSDmitry Torokhov 	rdev = regulator_dev_lookup(dev, id);
1608a4d7641fSDmitry Torokhov 	if (IS_ERR(rdev)) {
1609163478daSDmitry Torokhov 		ret = PTR_ERR(rdev);
1610ef60abbbSMark Brown 
16111e4b545cSNishanth Menon 		/*
1612a4d7641fSDmitry Torokhov 		 * If regulator_dev_lookup() fails with error other
1613a4d7641fSDmitry Torokhov 		 * than -ENODEV our job here is done, we simply return it.
16141e4b545cSNishanth Menon 		 */
1615a4d7641fSDmitry Torokhov 		if (ret != -ENODEV)
1616a4d7641fSDmitry Torokhov 			return ERR_PTR(ret);
16171e4b545cSNishanth Menon 
1618a4d7641fSDmitry Torokhov 		if (!have_full_constraints()) {
1619a4d7641fSDmitry Torokhov 			dev_warn(dev,
1620a4d7641fSDmitry Torokhov 				 "incomplete constraints, dummy supplies not allowed\n");
1621a4d7641fSDmitry Torokhov 			return ERR_PTR(-ENODEV);
162234abbd68SMark Brown 		}
162334abbd68SMark Brown 
1624a4d7641fSDmitry Torokhov 		switch (get_type) {
1625a4d7641fSDmitry Torokhov 		case NORMAL_GET:
1626a4d7641fSDmitry Torokhov 			/*
1627a4d7641fSDmitry Torokhov 			 * Assume that a regulator is physically present and
1628a4d7641fSDmitry Torokhov 			 * enabled, even if it isn't hooked up, and just
1629a4d7641fSDmitry Torokhov 			 * provide a dummy.
1630a4d7641fSDmitry Torokhov 			 */
1631a4d7641fSDmitry Torokhov 			dev_warn(dev,
1632a4d7641fSDmitry Torokhov 				 "%s supply %s not found, using dummy regulator\n",
1633a4d7641fSDmitry Torokhov 				 devname, id);
1634a4d7641fSDmitry Torokhov 			rdev = dummy_regulator_rdev;
1635a4d7641fSDmitry Torokhov 			get_device(&rdev->dev);
1636a4d7641fSDmitry Torokhov 			break;
1637414c70cbSLiam Girdwood 
1638a4d7641fSDmitry Torokhov 		case EXCLUSIVE_GET:
1639a4d7641fSDmitry Torokhov 			dev_warn(dev,
1640a4d7641fSDmitry Torokhov 				 "dummy supplies not allowed for exclusive requests\n");
1641a4d7641fSDmitry Torokhov 			/* fall through */
1642a4d7641fSDmitry Torokhov 
1643a4d7641fSDmitry Torokhov 		default:
1644a4d7641fSDmitry Torokhov 			return ERR_PTR(-ENODEV);
1645a4d7641fSDmitry Torokhov 		}
1646a4d7641fSDmitry Torokhov 	}
1647a4d7641fSDmitry Torokhov 
16485ffbd136SMark Brown 	if (rdev->exclusive) {
16495ffbd136SMark Brown 		regulator = ERR_PTR(-EPERM);
165085f3b431STomeu Vizoso 		put_device(&rdev->dev);
165185f3b431STomeu Vizoso 		return regulator;
16525ffbd136SMark Brown 	}
16535ffbd136SMark Brown 
1654a8bd42a9SDmitry Torokhov 	if (get_type == EXCLUSIVE_GET && rdev->open_count) {
16555ffbd136SMark Brown 		regulator = ERR_PTR(-EBUSY);
165685f3b431STomeu Vizoso 		put_device(&rdev->dev);
165785f3b431STomeu Vizoso 		return regulator;
16585ffbd136SMark Brown 	}
16595ffbd136SMark Brown 
16606261b06dSBjorn Andersson 	ret = regulator_resolve_supply(rdev);
16616261b06dSBjorn Andersson 	if (ret < 0) {
16626261b06dSBjorn Andersson 		regulator = ERR_PTR(ret);
166385f3b431STomeu Vizoso 		put_device(&rdev->dev);
166485f3b431STomeu Vizoso 		return regulator;
16656261b06dSBjorn Andersson 	}
16666261b06dSBjorn Andersson 
166785f3b431STomeu Vizoso 	if (!try_module_get(rdev->owner)) {
16687d245afaSDmitry Torokhov 		regulator = ERR_PTR(-EPROBE_DEFER);
166985f3b431STomeu Vizoso 		put_device(&rdev->dev);
167085f3b431STomeu Vizoso 		return regulator;
167185f3b431STomeu Vizoso 	}
1672a5766f11SLiam Girdwood 
1673414c70cbSLiam Girdwood 	regulator = create_regulator(rdev, dev, id);
1674414c70cbSLiam Girdwood 	if (regulator == NULL) {
1675414c70cbSLiam Girdwood 		regulator = ERR_PTR(-ENOMEM);
167685f3b431STomeu Vizoso 		put_device(&rdev->dev);
1677414c70cbSLiam Girdwood 		module_put(rdev->owner);
167885f3b431STomeu Vizoso 		return regulator;
1679414c70cbSLiam Girdwood 	}
1680414c70cbSLiam Girdwood 
16815ffbd136SMark Brown 	rdev->open_count++;
1682a8bd42a9SDmitry Torokhov 	if (get_type == EXCLUSIVE_GET) {
16835ffbd136SMark Brown 		rdev->exclusive = 1;
16845ffbd136SMark Brown 
16855ffbd136SMark Brown 		ret = _regulator_is_enabled(rdev);
16865ffbd136SMark Brown 		if (ret > 0)
16875ffbd136SMark Brown 			rdev->use_count = 1;
16885ffbd136SMark Brown 		else
16895ffbd136SMark Brown 			rdev->use_count = 0;
16905ffbd136SMark Brown 	}
16915ffbd136SMark Brown 
1692414c70cbSLiam Girdwood 	return regulator;
1693414c70cbSLiam Girdwood }
16945ffbd136SMark Brown 
16955ffbd136SMark Brown /**
16965ffbd136SMark Brown  * regulator_get - lookup and obtain a reference to a regulator.
16975ffbd136SMark Brown  * @dev: device for regulator "consumer"
16985ffbd136SMark Brown  * @id: Supply name or regulator ID.
16995ffbd136SMark Brown  *
17005ffbd136SMark Brown  * Returns a struct regulator corresponding to the regulator producer,
17015ffbd136SMark Brown  * or IS_ERR() condition containing errno.
17025ffbd136SMark Brown  *
17035ffbd136SMark Brown  * Use of supply names configured via regulator_set_device_supply() is
17045ffbd136SMark Brown  * strongly encouraged.  It is recommended that the supply name used
17055ffbd136SMark Brown  * should match the name used for the supply and/or the relevant
17065ffbd136SMark Brown  * device pins in the datasheet.
17075ffbd136SMark Brown  */
17085ffbd136SMark Brown struct regulator *regulator_get(struct device *dev, const char *id)
17095ffbd136SMark Brown {
1710a8bd42a9SDmitry Torokhov 	return _regulator_get(dev, id, NORMAL_GET);
17115ffbd136SMark Brown }
1712414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get);
1713414c70cbSLiam Girdwood 
1714070b9079SStephen Boyd /**
17155ffbd136SMark Brown  * regulator_get_exclusive - obtain exclusive access to a regulator.
17165ffbd136SMark Brown  * @dev: device for regulator "consumer"
17175ffbd136SMark Brown  * @id: Supply name or regulator ID.
17185ffbd136SMark Brown  *
17195ffbd136SMark Brown  * Returns a struct regulator corresponding to the regulator producer,
17205ffbd136SMark Brown  * or IS_ERR() condition containing errno.  Other consumers will be
172169c3f723SStephen Boyd  * unable to obtain this regulator while this reference is held and the
172269c3f723SStephen Boyd  * use count for the regulator will be initialised to reflect the current
172369c3f723SStephen Boyd  * state of the regulator.
17245ffbd136SMark Brown  *
17255ffbd136SMark Brown  * This is intended for use by consumers which cannot tolerate shared
17265ffbd136SMark Brown  * use of the regulator such as those which need to force the
17275ffbd136SMark Brown  * regulator off for correct operation of the hardware they are
17285ffbd136SMark Brown  * controlling.
17295ffbd136SMark Brown  *
17305ffbd136SMark Brown  * Use of supply names configured via regulator_set_device_supply() is
17315ffbd136SMark Brown  * strongly encouraged.  It is recommended that the supply name used
17325ffbd136SMark Brown  * should match the name used for the supply and/or the relevant
17335ffbd136SMark Brown  * device pins in the datasheet.
17345ffbd136SMark Brown  */
17355ffbd136SMark Brown struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
17365ffbd136SMark Brown {
1737a8bd42a9SDmitry Torokhov 	return _regulator_get(dev, id, EXCLUSIVE_GET);
17385ffbd136SMark Brown }
17395ffbd136SMark Brown EXPORT_SYMBOL_GPL(regulator_get_exclusive);
17405ffbd136SMark Brown 
1741de1dd9fdSMark Brown /**
1742de1dd9fdSMark Brown  * regulator_get_optional - obtain optional access to a regulator.
1743de1dd9fdSMark Brown  * @dev: device for regulator "consumer"
1744de1dd9fdSMark Brown  * @id: Supply name or regulator ID.
1745de1dd9fdSMark Brown  *
1746de1dd9fdSMark Brown  * Returns a struct regulator corresponding to the regulator producer,
174769c3f723SStephen Boyd  * or IS_ERR() condition containing errno.
1748de1dd9fdSMark Brown  *
1749de1dd9fdSMark Brown  * This is intended for use by consumers for devices which can have
1750de1dd9fdSMark Brown  * some supplies unconnected in normal use, such as some MMC devices.
1751de1dd9fdSMark Brown  * It can allow the regulator core to provide stub supplies for other
1752de1dd9fdSMark Brown  * supplies requested using normal regulator_get() calls without
1753de1dd9fdSMark Brown  * disrupting the operation of drivers that can handle absent
1754de1dd9fdSMark Brown  * supplies.
1755de1dd9fdSMark Brown  *
1756de1dd9fdSMark Brown  * Use of supply names configured via regulator_set_device_supply() is
1757de1dd9fdSMark Brown  * strongly encouraged.  It is recommended that the supply name used
1758de1dd9fdSMark Brown  * should match the name used for the supply and/or the relevant
1759de1dd9fdSMark Brown  * device pins in the datasheet.
1760de1dd9fdSMark Brown  */
1761de1dd9fdSMark Brown struct regulator *regulator_get_optional(struct device *dev, const char *id)
1762de1dd9fdSMark Brown {
1763a8bd42a9SDmitry Torokhov 	return _regulator_get(dev, id, OPTIONAL_GET);
1764de1dd9fdSMark Brown }
1765de1dd9fdSMark Brown EXPORT_SYMBOL_GPL(regulator_get_optional);
1766de1dd9fdSMark Brown 
176783b0302dSAshay Jaiswal /* regulator_list_mutex lock held by regulator_put() */
176823ff2f0fSCharles Keepax static void _regulator_put(struct regulator *regulator)
1769414c70cbSLiam Girdwood {
1770414c70cbSLiam Girdwood 	struct regulator_dev *rdev;
1771414c70cbSLiam Girdwood 
177293576842SViresh Kumar 	if (IS_ERR_OR_NULL(regulator))
1773414c70cbSLiam Girdwood 		return;
1774414c70cbSLiam Girdwood 
177570cfef26SKrzysztof Kozlowski 	lockdep_assert_held_once(&regulator_list_mutex);
177670cfef26SKrzysztof Kozlowski 
1777414c70cbSLiam Girdwood 	rdev = regulator->rdev;
1778414c70cbSLiam Girdwood 
17795de70519SMark Brown 	debugfs_remove_recursive(regulator->debugfs);
17805de70519SMark Brown 
1781414c70cbSLiam Girdwood 	/* remove any sysfs entries */
1782e2c98eafSShawn Guo 	if (regulator->dev)
1783414c70cbSLiam Girdwood 		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
178483b0302dSAshay Jaiswal 	mutex_lock(&rdev->mutex);
1785414c70cbSLiam Girdwood 	list_del(&regulator->list);
1786414c70cbSLiam Girdwood 
17875ffbd136SMark Brown 	rdev->open_count--;
17885ffbd136SMark Brown 	rdev->exclusive = 0;
178985f3b431STomeu Vizoso 	put_device(&rdev->dev);
179083b0302dSAshay Jaiswal 	mutex_unlock(&rdev->mutex);
17915ffbd136SMark Brown 
17920630b614SStephen Boyd 	kfree_const(regulator->supply_name);
17931768514eSMark Brown 	kfree(regulator);
17941768514eSMark Brown 
1795414c70cbSLiam Girdwood 	module_put(rdev->owner);
179623ff2f0fSCharles Keepax }
179723ff2f0fSCharles Keepax 
179823ff2f0fSCharles Keepax /**
179923ff2f0fSCharles Keepax  * regulator_put - "free" the regulator source
180023ff2f0fSCharles Keepax  * @regulator: regulator source
180123ff2f0fSCharles Keepax  *
180223ff2f0fSCharles Keepax  * Note: drivers must ensure that all regulator_enable calls made on this
180323ff2f0fSCharles Keepax  * regulator source are balanced by regulator_disable calls prior to calling
180423ff2f0fSCharles Keepax  * this function.
180523ff2f0fSCharles Keepax  */
180623ff2f0fSCharles Keepax void regulator_put(struct regulator *regulator)
180723ff2f0fSCharles Keepax {
180823ff2f0fSCharles Keepax 	mutex_lock(&regulator_list_mutex);
180923ff2f0fSCharles Keepax 	_regulator_put(regulator);
1810414c70cbSLiam Girdwood 	mutex_unlock(&regulator_list_mutex);
1811414c70cbSLiam Girdwood }
1812414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_put);
1813414c70cbSLiam Girdwood 
1814a06ccd9cSCharles Keepax /**
1815a06ccd9cSCharles Keepax  * regulator_register_supply_alias - Provide device alias for supply lookup
1816a06ccd9cSCharles Keepax  *
1817a06ccd9cSCharles Keepax  * @dev: device that will be given as the regulator "consumer"
1818a06ccd9cSCharles Keepax  * @id: Supply name or regulator ID
1819a06ccd9cSCharles Keepax  * @alias_dev: device that should be used to lookup the supply
1820a06ccd9cSCharles Keepax  * @alias_id: Supply name or regulator ID that should be used to lookup the
1821a06ccd9cSCharles Keepax  * supply
1822a06ccd9cSCharles Keepax  *
1823a06ccd9cSCharles Keepax  * All lookups for id on dev will instead be conducted for alias_id on
1824a06ccd9cSCharles Keepax  * alias_dev.
1825a06ccd9cSCharles Keepax  */
1826a06ccd9cSCharles Keepax int regulator_register_supply_alias(struct device *dev, const char *id,
1827a06ccd9cSCharles Keepax 				    struct device *alias_dev,
1828a06ccd9cSCharles Keepax 				    const char *alias_id)
1829a06ccd9cSCharles Keepax {
1830a06ccd9cSCharles Keepax 	struct regulator_supply_alias *map;
1831a06ccd9cSCharles Keepax 
1832a06ccd9cSCharles Keepax 	map = regulator_find_supply_alias(dev, id);
1833a06ccd9cSCharles Keepax 	if (map)
1834a06ccd9cSCharles Keepax 		return -EEXIST;
1835a06ccd9cSCharles Keepax 
1836a06ccd9cSCharles Keepax 	map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1837a06ccd9cSCharles Keepax 	if (!map)
1838a06ccd9cSCharles Keepax 		return -ENOMEM;
1839a06ccd9cSCharles Keepax 
1840a06ccd9cSCharles Keepax 	map->src_dev = dev;
1841a06ccd9cSCharles Keepax 	map->src_supply = id;
1842a06ccd9cSCharles Keepax 	map->alias_dev = alias_dev;
1843a06ccd9cSCharles Keepax 	map->alias_supply = alias_id;
1844a06ccd9cSCharles Keepax 
1845a06ccd9cSCharles Keepax 	list_add(&map->list, &regulator_supply_alias_list);
1846a06ccd9cSCharles Keepax 
1847a06ccd9cSCharles Keepax 	pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1848a06ccd9cSCharles Keepax 		id, dev_name(dev), alias_id, dev_name(alias_dev));
1849a06ccd9cSCharles Keepax 
1850a06ccd9cSCharles Keepax 	return 0;
1851a06ccd9cSCharles Keepax }
1852a06ccd9cSCharles Keepax EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1853a06ccd9cSCharles Keepax 
1854a06ccd9cSCharles Keepax /**
1855a06ccd9cSCharles Keepax  * regulator_unregister_supply_alias - Remove device alias
1856a06ccd9cSCharles Keepax  *
1857a06ccd9cSCharles Keepax  * @dev: device that will be given as the regulator "consumer"
1858a06ccd9cSCharles Keepax  * @id: Supply name or regulator ID
1859a06ccd9cSCharles Keepax  *
1860a06ccd9cSCharles Keepax  * Remove a lookup alias if one exists for id on dev.
1861a06ccd9cSCharles Keepax  */
1862a06ccd9cSCharles Keepax void regulator_unregister_supply_alias(struct device *dev, const char *id)
1863a06ccd9cSCharles Keepax {
1864a06ccd9cSCharles Keepax 	struct regulator_supply_alias *map;
1865a06ccd9cSCharles Keepax 
1866a06ccd9cSCharles Keepax 	map = regulator_find_supply_alias(dev, id);
1867a06ccd9cSCharles Keepax 	if (map) {
1868a06ccd9cSCharles Keepax 		list_del(&map->list);
1869a06ccd9cSCharles Keepax 		kfree(map);
1870a06ccd9cSCharles Keepax 	}
1871a06ccd9cSCharles Keepax }
1872a06ccd9cSCharles Keepax EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1873a06ccd9cSCharles Keepax 
1874a06ccd9cSCharles Keepax /**
1875a06ccd9cSCharles Keepax  * regulator_bulk_register_supply_alias - register multiple aliases
1876a06ccd9cSCharles Keepax  *
1877a06ccd9cSCharles Keepax  * @dev: device that will be given as the regulator "consumer"
1878a06ccd9cSCharles Keepax  * @id: List of supply names or regulator IDs
1879a06ccd9cSCharles Keepax  * @alias_dev: device that should be used to lookup the supply
1880a06ccd9cSCharles Keepax  * @alias_id: List of supply names or regulator IDs that should be used to
1881a06ccd9cSCharles Keepax  * lookup the supply
1882a06ccd9cSCharles Keepax  * @num_id: Number of aliases to register
1883a06ccd9cSCharles Keepax  *
1884a06ccd9cSCharles Keepax  * @return 0 on success, an errno on failure.
1885a06ccd9cSCharles Keepax  *
1886a06ccd9cSCharles Keepax  * This helper function allows drivers to register several supply
1887a06ccd9cSCharles Keepax  * aliases in one operation.  If any of the aliases cannot be
1888a06ccd9cSCharles Keepax  * registered any aliases that were registered will be removed
1889a06ccd9cSCharles Keepax  * before returning to the caller.
1890a06ccd9cSCharles Keepax  */
18919f8c0fe9SLee Jones int regulator_bulk_register_supply_alias(struct device *dev,
18929f8c0fe9SLee Jones 					 const char *const *id,
1893a06ccd9cSCharles Keepax 					 struct device *alias_dev,
18949f8c0fe9SLee Jones 					 const char *const *alias_id,
1895a06ccd9cSCharles Keepax 					 int num_id)
1896a06ccd9cSCharles Keepax {
1897a06ccd9cSCharles Keepax 	int i;
1898a06ccd9cSCharles Keepax 	int ret;
1899a06ccd9cSCharles Keepax 
1900a06ccd9cSCharles Keepax 	for (i = 0; i < num_id; ++i) {
1901a06ccd9cSCharles Keepax 		ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1902a06ccd9cSCharles Keepax 						      alias_id[i]);
1903a06ccd9cSCharles Keepax 		if (ret < 0)
1904a06ccd9cSCharles Keepax 			goto err;
1905a06ccd9cSCharles Keepax 	}
1906a06ccd9cSCharles Keepax 
1907a06ccd9cSCharles Keepax 	return 0;
1908a06ccd9cSCharles Keepax 
1909a06ccd9cSCharles Keepax err:
1910a06ccd9cSCharles Keepax 	dev_err(dev,
1911a06ccd9cSCharles Keepax 		"Failed to create supply alias %s,%s -> %s,%s\n",
1912a06ccd9cSCharles Keepax 		id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1913a06ccd9cSCharles Keepax 
1914a06ccd9cSCharles Keepax 	while (--i >= 0)
1915a06ccd9cSCharles Keepax 		regulator_unregister_supply_alias(dev, id[i]);
1916a06ccd9cSCharles Keepax 
1917a06ccd9cSCharles Keepax 	return ret;
1918a06ccd9cSCharles Keepax }
1919a06ccd9cSCharles Keepax EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1920a06ccd9cSCharles Keepax 
1921a06ccd9cSCharles Keepax /**
1922a06ccd9cSCharles Keepax  * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1923a06ccd9cSCharles Keepax  *
1924a06ccd9cSCharles Keepax  * @dev: device that will be given as the regulator "consumer"
1925a06ccd9cSCharles Keepax  * @id: List of supply names or regulator IDs
1926a06ccd9cSCharles Keepax  * @num_id: Number of aliases to unregister
1927a06ccd9cSCharles Keepax  *
1928a06ccd9cSCharles Keepax  * This helper function allows drivers to unregister several supply
1929a06ccd9cSCharles Keepax  * aliases in one operation.
1930a06ccd9cSCharles Keepax  */
1931a06ccd9cSCharles Keepax void regulator_bulk_unregister_supply_alias(struct device *dev,
19329f8c0fe9SLee Jones 					    const char *const *id,
1933a06ccd9cSCharles Keepax 					    int num_id)
1934a06ccd9cSCharles Keepax {
1935a06ccd9cSCharles Keepax 	int i;
1936a06ccd9cSCharles Keepax 
1937a06ccd9cSCharles Keepax 	for (i = 0; i < num_id; ++i)
1938a06ccd9cSCharles Keepax 		regulator_unregister_supply_alias(dev, id[i]);
1939a06ccd9cSCharles Keepax }
1940a06ccd9cSCharles Keepax EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1941a06ccd9cSCharles Keepax 
1942a06ccd9cSCharles Keepax 
1943f19b00daSKim, Milo /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1944f19b00daSKim, Milo static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1945f19b00daSKim, Milo 				const struct regulator_config *config)
1946f19b00daSKim, Milo {
1947f19b00daSKim, Milo 	struct regulator_enable_gpio *pin;
1948778b28b4SRussell King 	struct gpio_desc *gpiod;
1949f19b00daSKim, Milo 	int ret;
1950f19b00daSKim, Milo 
1951778b28b4SRussell King 	gpiod = gpio_to_desc(config->ena_gpio);
1952778b28b4SRussell King 
1953f19b00daSKim, Milo 	list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1954778b28b4SRussell King 		if (pin->gpiod == gpiod) {
1955f19b00daSKim, Milo 			rdev_dbg(rdev, "GPIO %d is already used\n",
1956f19b00daSKim, Milo 				config->ena_gpio);
1957f19b00daSKim, Milo 			goto update_ena_gpio_to_rdev;
1958f19b00daSKim, Milo 		}
1959f19b00daSKim, Milo 	}
1960f19b00daSKim, Milo 
1961f19b00daSKim, Milo 	ret = gpio_request_one(config->ena_gpio,
1962f19b00daSKim, Milo 				GPIOF_DIR_OUT | config->ena_gpio_flags,
1963f19b00daSKim, Milo 				rdev_get_name(rdev));
1964f19b00daSKim, Milo 	if (ret)
1965f19b00daSKim, Milo 		return ret;
1966f19b00daSKim, Milo 
1967f19b00daSKim, Milo 	pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1968f19b00daSKim, Milo 	if (pin == NULL) {
1969f19b00daSKim, Milo 		gpio_free(config->ena_gpio);
1970f19b00daSKim, Milo 		return -ENOMEM;
1971f19b00daSKim, Milo 	}
1972f19b00daSKim, Milo 
1973778b28b4SRussell King 	pin->gpiod = gpiod;
1974f19b00daSKim, Milo 	pin->ena_gpio_invert = config->ena_gpio_invert;
1975f19b00daSKim, Milo 	list_add(&pin->list, &regulator_ena_gpio_list);
1976f19b00daSKim, Milo 
1977f19b00daSKim, Milo update_ena_gpio_to_rdev:
1978f19b00daSKim, Milo 	pin->request_count++;
1979f19b00daSKim, Milo 	rdev->ena_pin = pin;
1980f19b00daSKim, Milo 	return 0;
1981f19b00daSKim, Milo }
1982f19b00daSKim, Milo 
1983f19b00daSKim, Milo static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1984f19b00daSKim, Milo {
1985f19b00daSKim, Milo 	struct regulator_enable_gpio *pin, *n;
1986f19b00daSKim, Milo 
1987f19b00daSKim, Milo 	if (!rdev->ena_pin)
1988f19b00daSKim, Milo 		return;
1989f19b00daSKim, Milo 
1990f19b00daSKim, Milo 	/* Free the GPIO only in case of no use */
1991f19b00daSKim, Milo 	list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1992778b28b4SRussell King 		if (pin->gpiod == rdev->ena_pin->gpiod) {
1993f19b00daSKim, Milo 			if (pin->request_count <= 1) {
1994f19b00daSKim, Milo 				pin->request_count = 0;
1995778b28b4SRussell King 				gpiod_put(pin->gpiod);
1996f19b00daSKim, Milo 				list_del(&pin->list);
1997f19b00daSKim, Milo 				kfree(pin);
199860a2362fSSeung-Woo Kim 				rdev->ena_pin = NULL;
199960a2362fSSeung-Woo Kim 				return;
2000f19b00daSKim, Milo 			} else {
2001f19b00daSKim, Milo 				pin->request_count--;
2002f19b00daSKim, Milo 			}
2003f19b00daSKim, Milo 		}
2004f19b00daSKim, Milo 	}
2005f19b00daSKim, Milo }
2006f19b00daSKim, Milo 
2007967cfb18SKim, Milo /**
200831d6eebfSRobert P. J. Day  * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
200931d6eebfSRobert P. J. Day  * @rdev: regulator_dev structure
201031d6eebfSRobert P. J. Day  * @enable: enable GPIO at initial use?
201131d6eebfSRobert P. J. Day  *
2012967cfb18SKim, Milo  * GPIO is enabled in case of initial use. (enable_count is 0)
2013967cfb18SKim, Milo  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2014967cfb18SKim, Milo  */
2015967cfb18SKim, Milo static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2016967cfb18SKim, Milo {
2017967cfb18SKim, Milo 	struct regulator_enable_gpio *pin = rdev->ena_pin;
2018967cfb18SKim, Milo 
2019967cfb18SKim, Milo 	if (!pin)
2020967cfb18SKim, Milo 		return -EINVAL;
2021967cfb18SKim, Milo 
2022967cfb18SKim, Milo 	if (enable) {
2023967cfb18SKim, Milo 		/* Enable GPIO at initial use */
2024967cfb18SKim, Milo 		if (pin->enable_count == 0)
2025778b28b4SRussell King 			gpiod_set_value_cansleep(pin->gpiod,
2026967cfb18SKim, Milo 						 !pin->ena_gpio_invert);
2027967cfb18SKim, Milo 
2028967cfb18SKim, Milo 		pin->enable_count++;
2029967cfb18SKim, Milo 	} else {
2030967cfb18SKim, Milo 		if (pin->enable_count > 1) {
2031967cfb18SKim, Milo 			pin->enable_count--;
2032967cfb18SKim, Milo 			return 0;
2033967cfb18SKim, Milo 		}
2034967cfb18SKim, Milo 
2035967cfb18SKim, Milo 		/* Disable GPIO if not used */
2036967cfb18SKim, Milo 		if (pin->enable_count <= 1) {
2037778b28b4SRussell King 			gpiod_set_value_cansleep(pin->gpiod,
2038967cfb18SKim, Milo 						 pin->ena_gpio_invert);
2039967cfb18SKim, Milo 			pin->enable_count = 0;
2040967cfb18SKim, Milo 		}
2041967cfb18SKim, Milo 	}
2042967cfb18SKim, Milo 
2043967cfb18SKim, Milo 	return 0;
2044967cfb18SKim, Milo }
2045967cfb18SKim, Milo 
204679fd1141SGuodong Xu /**
204779fd1141SGuodong Xu  * _regulator_enable_delay - a delay helper function
204879fd1141SGuodong Xu  * @delay: time to delay in microseconds
204979fd1141SGuodong Xu  *
20505df529d4SThierry Reding  * Delay for the requested amount of time as per the guidelines in:
20515df529d4SThierry Reding  *
20525df529d4SThierry Reding  *     Documentation/timers/timers-howto.txt
20535df529d4SThierry Reding  *
20545df529d4SThierry Reding  * The assumption here is that regulators will never be enabled in
20555df529d4SThierry Reding  * atomic context and therefore sleeping functions can be used.
20565df529d4SThierry Reding  */
205779fd1141SGuodong Xu static void _regulator_enable_delay(unsigned int delay)
205879fd1141SGuodong Xu {
20595df529d4SThierry Reding 	unsigned int ms = delay / 1000;
20605df529d4SThierry Reding 	unsigned int us = delay % 1000;
20615df529d4SThierry Reding 
20625df529d4SThierry Reding 	if (ms > 0) {
20635df529d4SThierry Reding 		/*
20645df529d4SThierry Reding 		 * For small enough values, handle super-millisecond
20655df529d4SThierry Reding 		 * delays in the usleep_range() call below.
20665df529d4SThierry Reding 		 */
20675df529d4SThierry Reding 		if (ms < 20)
20685df529d4SThierry Reding 			us += ms * 1000;
20695df529d4SThierry Reding 		else
20705df529d4SThierry Reding 			msleep(ms);
20715df529d4SThierry Reding 	}
20725df529d4SThierry Reding 
20735df529d4SThierry Reding 	/*
20745df529d4SThierry Reding 	 * Give the scheduler some room to coalesce with any other
20755df529d4SThierry Reding 	 * wakeup sources. For delays shorter than 10 us, don't even
20765df529d4SThierry Reding 	 * bother setting up high-resolution timers and just busy-
20775df529d4SThierry Reding 	 * loop.
20785df529d4SThierry Reding 	 */
20795df529d4SThierry Reding 	if (us >= 10)
20805df529d4SThierry Reding 		usleep_range(us, us + 100);
20815df529d4SThierry Reding 	else
20825df529d4SThierry Reding 		udelay(us);
20835c5659d0SMark Brown }
20845c5659d0SMark Brown 
20855c5659d0SMark Brown static int _regulator_do_enable(struct regulator_dev *rdev)
20865c5659d0SMark Brown {
20875c5659d0SMark Brown 	int ret, delay;
20885c5659d0SMark Brown 
20895c5659d0SMark Brown 	/* Query before enabling in case configuration dependent.  */
20905c5659d0SMark Brown 	ret = _regulator_get_enable_time(rdev);
20915c5659d0SMark Brown 	if (ret >= 0) {
20925c5659d0SMark Brown 		delay = ret;
20935c5659d0SMark Brown 	} else {
2094414c70cbSLiam Girdwood 		rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2095414c70cbSLiam Girdwood 		delay = 0;
2096414c70cbSLiam Girdwood 	}
20975c5659d0SMark Brown 
2098414c70cbSLiam Girdwood 	trace_regulator_enable(rdev_get_name(rdev));
2099414c70cbSLiam Girdwood 
2100871f5650SGuodong Xu 	if (rdev->desc->off_on_delay) {
2101871f5650SGuodong Xu 		/* if needed, keep a distance of off_on_delay from last time
2102871f5650SGuodong Xu 		 * this regulator was disabled.
2103871f5650SGuodong Xu 		 */
2104871f5650SGuodong Xu 		unsigned long start_jiffy = jiffies;
2105871f5650SGuodong Xu 		unsigned long intended, max_delay, remaining;
2106871f5650SGuodong Xu 
2107871f5650SGuodong Xu 		max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2108871f5650SGuodong Xu 		intended = rdev->last_off_jiffy + max_delay;
2109871f5650SGuodong Xu 
2110871f5650SGuodong Xu 		if (time_before(start_jiffy, intended)) {
2111871f5650SGuodong Xu 			/* calc remaining jiffies to deal with one-time
2112871f5650SGuodong Xu 			 * timer wrapping.
2113871f5650SGuodong Xu 			 * in case of multiple timer wrapping, either it can be
2114871f5650SGuodong Xu 			 * detected by out-of-range remaining, or it cannot be
2115871f5650SGuodong Xu 			 * detected and we gets a panelty of
2116871f5650SGuodong Xu 			 * _regulator_enable_delay().
2117871f5650SGuodong Xu 			 */
2118871f5650SGuodong Xu 			remaining = intended - start_jiffy;
2119871f5650SGuodong Xu 			if (remaining <= max_delay)
2120871f5650SGuodong Xu 				_regulator_enable_delay(
2121871f5650SGuodong Xu 						jiffies_to_usecs(remaining));
2122871f5650SGuodong Xu 		}
2123871f5650SGuodong Xu 	}
2124871f5650SGuodong Xu 
2125414c70cbSLiam Girdwood 	if (rdev->ena_pin) {
212629d62ec5SDoug Anderson 		if (!rdev->ena_gpio_state) {
21279a2372faSMark Brown 			ret = regulator_ena_gpio_ctrl(rdev, true);
2128414c70cbSLiam Girdwood 			if (ret < 0)
2129414c70cbSLiam Girdwood 				return ret;
21309a2372faSMark Brown 			rdev->ena_gpio_state = 1;
213129d62ec5SDoug Anderson 		}
21329a2372faSMark Brown 	} else if (rdev->desc->ops->enable) {
21339a2372faSMark Brown 		ret = rdev->desc->ops->enable(rdev);
21349a2372faSMark Brown 		if (ret < 0)
21359a2372faSMark Brown 			return ret;
21369a2372faSMark Brown 	} else {
21379a2372faSMark Brown 		return -EINVAL;
21385c5659d0SMark Brown 	}
21399a2372faSMark Brown 
21409a2372faSMark Brown 	/* Allow the regulator to ramp; it would be useful to extend
214131aae2beSMark Brown 	 * this for bulk operations so that the regulators can ramp
2142a7433cffSLinus Walleij 	 * together.  */
21435da84fd9SJoe Perches 	trace_regulator_enable_delay(rdev_get_name(rdev));
2144414c70cbSLiam Girdwood 
214579fd1141SGuodong Xu 	_regulator_enable_delay(delay);
2146a7433cffSLinus Walleij 
2147414c70cbSLiam Girdwood 	trace_regulator_enable_complete(rdev_get_name(rdev));
2148414c70cbSLiam Girdwood 
21499a2372faSMark Brown 	return 0;
21509a2372faSMark Brown }
21519a2372faSMark Brown 
2152414c70cbSLiam Girdwood /* locks held by regulator_enable() */
2153414c70cbSLiam Girdwood static int _regulator_enable(struct regulator_dev *rdev)
2154414c70cbSLiam Girdwood {
2155414c70cbSLiam Girdwood 	int ret;
2156414c70cbSLiam Girdwood 
215770cfef26SKrzysztof Kozlowski 	lockdep_assert_held_once(&rdev->mutex);
215870cfef26SKrzysztof Kozlowski 
2159414c70cbSLiam Girdwood 	/* check voltage and requested load before enabling */
21608a34e979SWEN Pingbo 	if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2161cf7bbcdfSMark Brown 		drms_uA_update(rdev);
2162cf7bbcdfSMark Brown 
2163414c70cbSLiam Girdwood 	if (rdev->use_count == 0) {
2164cf7bbcdfSMark Brown 		/* The regulator may on if it's not switchable or left on */
2165414c70cbSLiam Girdwood 		ret = _regulator_is_enabled(rdev);
2166414c70cbSLiam Girdwood 		if (ret == -EINVAL || ret == 0) {
21678a34e979SWEN Pingbo 			if (!regulator_ops_is_valid(rdev,
21688a34e979SWEN Pingbo 					REGULATOR_CHANGE_STATUS))
2169412aec61SDavid Brownell 				return -EPERM;
2170412aec61SDavid Brownell 
2171414c70cbSLiam Girdwood 			ret = _regulator_do_enable(rdev);
2172412aec61SDavid Brownell 			if (ret < 0)
2173412aec61SDavid Brownell 				return ret;
2174412aec61SDavid Brownell 
2175264b88c9SHarald Geyer 			_notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2176264b88c9SHarald Geyer 					     NULL);
2177414c70cbSLiam Girdwood 		} else if (ret < 0) {
2178414c70cbSLiam Girdwood 			rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2179414c70cbSLiam Girdwood 			return ret;
2180414c70cbSLiam Girdwood 		}
2181414c70cbSLiam Girdwood 		/* Fallthrough on positive return values - already enabled */
2182414c70cbSLiam Girdwood 	}
2183414c70cbSLiam Girdwood 
2184414c70cbSLiam Girdwood 	rdev->use_count++;
2185414c70cbSLiam Girdwood 
2186414c70cbSLiam Girdwood 	return 0;
2187414c70cbSLiam Girdwood }
2188414c70cbSLiam Girdwood 
2189414c70cbSLiam Girdwood /**
2190414c70cbSLiam Girdwood  * regulator_enable - enable regulator output
2191414c70cbSLiam Girdwood  * @regulator: regulator source
2192414c70cbSLiam Girdwood  *
2193414c70cbSLiam Girdwood  * Request that the regulator be enabled with the regulator output at
2194414c70cbSLiam Girdwood  * the predefined voltage or current value.  Calls to regulator_enable()
2195414c70cbSLiam Girdwood  * must be balanced with calls to regulator_disable().
2196414c70cbSLiam Girdwood  *
2197414c70cbSLiam Girdwood  * NOTE: the output value can be set by other drivers, boot loader or may be
2198414c70cbSLiam Girdwood  * hardwired in the regulator.
2199414c70cbSLiam Girdwood  */
2200414c70cbSLiam Girdwood int regulator_enable(struct regulator *regulator)
2201414c70cbSLiam Girdwood {
2202414c70cbSLiam Girdwood 	struct regulator_dev *rdev = regulator->rdev;
2203414c70cbSLiam Girdwood 	int ret = 0;
2204414c70cbSLiam Girdwood 
22056492bc1bSMark Brown 	if (regulator->always_on)
22066492bc1bSMark Brown 		return 0;
22076492bc1bSMark Brown 
22083801b86aSMark Brown 	if (rdev->supply) {
22093801b86aSMark Brown 		ret = regulator_enable(rdev->supply);
22103801b86aSMark Brown 		if (ret != 0)
22113801b86aSMark Brown 			return ret;
22123801b86aSMark Brown 	}
22133801b86aSMark Brown 
2214414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
2215414c70cbSLiam Girdwood 	ret = _regulator_enable(rdev);
2216414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
22173801b86aSMark Brown 
2218d1685e4eSHeiko Stübner 	if (ret != 0 && rdev->supply)
22193801b86aSMark Brown 		regulator_disable(rdev->supply);
22203801b86aSMark Brown 
2221414c70cbSLiam Girdwood 	return ret;
2222414c70cbSLiam Girdwood }
2223414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_enable);
2224414c70cbSLiam Girdwood 
22255c5659d0SMark Brown static int _regulator_do_disable(struct regulator_dev *rdev)
22265c5659d0SMark Brown {
22275c5659d0SMark Brown 	int ret;
22285c5659d0SMark Brown 
22295c5659d0SMark Brown 	trace_regulator_disable(rdev_get_name(rdev));
22305c5659d0SMark Brown 
2231967cfb18SKim, Milo 	if (rdev->ena_pin) {
223229d62ec5SDoug Anderson 		if (rdev->ena_gpio_state) {
2233967cfb18SKim, Milo 			ret = regulator_ena_gpio_ctrl(rdev, false);
2234967cfb18SKim, Milo 			if (ret < 0)
2235967cfb18SKim, Milo 				return ret;
22365c5659d0SMark Brown 			rdev->ena_gpio_state = 0;
223729d62ec5SDoug Anderson 		}
22385c5659d0SMark Brown 
22395c5659d0SMark Brown 	} else if (rdev->desc->ops->disable) {
22405c5659d0SMark Brown 		ret = rdev->desc->ops->disable(rdev);
22415c5659d0SMark Brown 		if (ret != 0)
22425c5659d0SMark Brown 			return ret;
22435c5659d0SMark Brown 	}
22445c5659d0SMark Brown 
2245871f5650SGuodong Xu 	/* cares about last_off_jiffy only if off_on_delay is required by
2246871f5650SGuodong Xu 	 * device.
2247871f5650SGuodong Xu 	 */
2248871f5650SGuodong Xu 	if (rdev->desc->off_on_delay)
2249871f5650SGuodong Xu 		rdev->last_off_jiffy = jiffies;
2250871f5650SGuodong Xu 
22515c5659d0SMark Brown 	trace_regulator_disable_complete(rdev_get_name(rdev));
22525c5659d0SMark Brown 
22535c5659d0SMark Brown 	return 0;
22545c5659d0SMark Brown }
22555c5659d0SMark Brown 
2256414c70cbSLiam Girdwood /* locks held by regulator_disable() */
22573801b86aSMark Brown static int _regulator_disable(struct regulator_dev *rdev)
2258414c70cbSLiam Girdwood {
2259414c70cbSLiam Girdwood 	int ret = 0;
2260414c70cbSLiam Girdwood 
226170cfef26SKrzysztof Kozlowski 	lockdep_assert_held_once(&rdev->mutex);
226270cfef26SKrzysztof Kozlowski 
2263cd94b505SDavid Brownell 	if (WARN(rdev->use_count <= 0,
226443e7ee33SJoe Perches 		 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2265cd94b505SDavid Brownell 		return -EIO;
2266cd94b505SDavid Brownell 
2267414c70cbSLiam Girdwood 	/* are we the last user and permitted to disable ? */
226860ef66fcSMark Brown 	if (rdev->use_count == 1 &&
226960ef66fcSMark Brown 	    (rdev->constraints && !rdev->constraints->always_on)) {
2270414c70cbSLiam Girdwood 
2271414c70cbSLiam Girdwood 		/* we are last user */
22728a34e979SWEN Pingbo 		if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2273a1c8a551SRichard Fitzgerald 			ret = _notifier_call_chain(rdev,
2274a1c8a551SRichard Fitzgerald 						   REGULATOR_EVENT_PRE_DISABLE,
2275a1c8a551SRichard Fitzgerald 						   NULL);
2276a1c8a551SRichard Fitzgerald 			if (ret & NOTIFY_STOP_MASK)
2277a1c8a551SRichard Fitzgerald 				return -EINVAL;
2278a1c8a551SRichard Fitzgerald 
22795c5659d0SMark Brown 			ret = _regulator_do_disable(rdev);
2280414c70cbSLiam Girdwood 			if (ret < 0) {
22815da84fd9SJoe Perches 				rdev_err(rdev, "failed to disable\n");
2282a1c8a551SRichard Fitzgerald 				_notifier_call_chain(rdev,
2283a1c8a551SRichard Fitzgerald 						REGULATOR_EVENT_ABORT_DISABLE,
2284a1c8a551SRichard Fitzgerald 						NULL);
2285414c70cbSLiam Girdwood 				return ret;
2286414c70cbSLiam Girdwood 			}
228766fda75fSMarkus Pargmann 			_notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
228866fda75fSMarkus Pargmann 					NULL);
2289414c70cbSLiam Girdwood 		}
2290414c70cbSLiam Girdwood 
2291414c70cbSLiam Girdwood 		rdev->use_count = 0;
2292414c70cbSLiam Girdwood 	} else if (rdev->use_count > 1) {
22938a34e979SWEN Pingbo 		if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2294414c70cbSLiam Girdwood 			drms_uA_update(rdev);
2295414c70cbSLiam Girdwood 
2296414c70cbSLiam Girdwood 		rdev->use_count--;
2297414c70cbSLiam Girdwood 	}
22983801b86aSMark Brown 
2299414c70cbSLiam Girdwood 	return ret;
2300414c70cbSLiam Girdwood }
2301414c70cbSLiam Girdwood 
2302414c70cbSLiam Girdwood /**
2303414c70cbSLiam Girdwood  * regulator_disable - disable regulator output
2304414c70cbSLiam Girdwood  * @regulator: regulator source
2305414c70cbSLiam Girdwood  *
2306cf7bbcdfSMark Brown  * Disable the regulator output voltage or current.  Calls to
2307cf7bbcdfSMark Brown  * regulator_enable() must be balanced with calls to
2308cf7bbcdfSMark Brown  * regulator_disable().
230969279fb9SMark Brown  *
2310414c70cbSLiam Girdwood  * NOTE: this will only disable the regulator output if no other consumer
2311cf7bbcdfSMark Brown  * devices have it enabled, the regulator device supports disabling and
2312cf7bbcdfSMark Brown  * machine constraints permit this operation.
2313414c70cbSLiam Girdwood  */
2314414c70cbSLiam Girdwood int regulator_disable(struct regulator *regulator)
2315414c70cbSLiam Girdwood {
2316412aec61SDavid Brownell 	struct regulator_dev *rdev = regulator->rdev;
2317412aec61SDavid Brownell 	int ret = 0;
2318414c70cbSLiam Girdwood 
23196492bc1bSMark Brown 	if (regulator->always_on)
23206492bc1bSMark Brown 		return 0;
23216492bc1bSMark Brown 
2322412aec61SDavid Brownell 	mutex_lock(&rdev->mutex);
23233801b86aSMark Brown 	ret = _regulator_disable(rdev);
2324412aec61SDavid Brownell 	mutex_unlock(&rdev->mutex);
23258cbf811dSJeffrey Carlyle 
23263801b86aSMark Brown 	if (ret == 0 && rdev->supply)
23273801b86aSMark Brown 		regulator_disable(rdev->supply);
23288cbf811dSJeffrey Carlyle 
2329414c70cbSLiam Girdwood 	return ret;
2330414c70cbSLiam Girdwood }
2331414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_disable);
2332414c70cbSLiam Girdwood 
2333414c70cbSLiam Girdwood /* locks held by regulator_force_disable() */
23343801b86aSMark Brown static int _regulator_force_disable(struct regulator_dev *rdev)
2335414c70cbSLiam Girdwood {
2336414c70cbSLiam Girdwood 	int ret = 0;
2337414c70cbSLiam Girdwood 
233870cfef26SKrzysztof Kozlowski 	lockdep_assert_held_once(&rdev->mutex);
233970cfef26SKrzysztof Kozlowski 
2340a1c8a551SRichard Fitzgerald 	ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2341a1c8a551SRichard Fitzgerald 			REGULATOR_EVENT_PRE_DISABLE, NULL);
2342a1c8a551SRichard Fitzgerald 	if (ret & NOTIFY_STOP_MASK)
2343a1c8a551SRichard Fitzgerald 		return -EINVAL;
2344a1c8a551SRichard Fitzgerald 
234566fda75fSMarkus Pargmann 	ret = _regulator_do_disable(rdev);
2346414c70cbSLiam Girdwood 	if (ret < 0) {
23475da84fd9SJoe Perches 		rdev_err(rdev, "failed to force disable\n");
2348a1c8a551SRichard Fitzgerald 		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2349a1c8a551SRichard Fitzgerald 				REGULATOR_EVENT_ABORT_DISABLE, NULL);
2350414c70cbSLiam Girdwood 		return ret;
2351414c70cbSLiam Girdwood 	}
235266fda75fSMarkus Pargmann 
235384b68263SMark Brown 	_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
235484b68263SMark Brown 			REGULATOR_EVENT_DISABLE, NULL);
2355414c70cbSLiam Girdwood 
235666fda75fSMarkus Pargmann 	return 0;
2357414c70cbSLiam Girdwood }
2358414c70cbSLiam Girdwood 
2359414c70cbSLiam Girdwood /**
2360414c70cbSLiam Girdwood  * regulator_force_disable - force disable regulator output
2361414c70cbSLiam Girdwood  * @regulator: regulator source
2362414c70cbSLiam Girdwood  *
2363414c70cbSLiam Girdwood  * Forcibly disable the regulator output voltage or current.
2364414c70cbSLiam Girdwood  * NOTE: this *will* disable the regulator output even if other consumer
2365414c70cbSLiam Girdwood  * devices have it enabled. This should be used for situations when device
2366414c70cbSLiam Girdwood  * damage will likely occur if the regulator is not disabled (e.g. over temp).
2367414c70cbSLiam Girdwood  */
2368414c70cbSLiam Girdwood int regulator_force_disable(struct regulator *regulator)
2369414c70cbSLiam Girdwood {
237082d15839SMark Brown 	struct regulator_dev *rdev = regulator->rdev;
2371414c70cbSLiam Girdwood 	int ret;
2372414c70cbSLiam Girdwood 
237382d15839SMark Brown 	mutex_lock(&rdev->mutex);
2374414c70cbSLiam Girdwood 	regulator->uA_load = 0;
23753801b86aSMark Brown 	ret = _regulator_force_disable(regulator->rdev);
237682d15839SMark Brown 	mutex_unlock(&rdev->mutex);
23778cbf811dSJeffrey Carlyle 
23783801b86aSMark Brown 	if (rdev->supply)
23793801b86aSMark Brown 		while (rdev->open_count--)
23803801b86aSMark Brown 			regulator_disable(rdev->supply);
23818cbf811dSJeffrey Carlyle 
2382414c70cbSLiam Girdwood 	return ret;
2383414c70cbSLiam Girdwood }
2384414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_force_disable);
2385414c70cbSLiam Girdwood 
2386da07ecd9SMark Brown static void regulator_disable_work(struct work_struct *work)
2387da07ecd9SMark Brown {
2388da07ecd9SMark Brown 	struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2389da07ecd9SMark Brown 						  disable_work.work);
2390da07ecd9SMark Brown 	int count, i, ret;
2391da07ecd9SMark Brown 
2392da07ecd9SMark Brown 	mutex_lock(&rdev->mutex);
2393da07ecd9SMark Brown 
2394da07ecd9SMark Brown 	BUG_ON(!rdev->deferred_disables);
2395da07ecd9SMark Brown 
2396da07ecd9SMark Brown 	count = rdev->deferred_disables;
2397da07ecd9SMark Brown 	rdev->deferred_disables = 0;
2398da07ecd9SMark Brown 
2399c9ccaa0cSTirupathi Reddy 	/*
2400c9ccaa0cSTirupathi Reddy 	 * Workqueue functions queue the new work instance while the previous
2401c9ccaa0cSTirupathi Reddy 	 * work instance is being processed. Cancel the queued work instance
2402c9ccaa0cSTirupathi Reddy 	 * as the work instance under processing does the job of the queued
2403c9ccaa0cSTirupathi Reddy 	 * work instance.
2404c9ccaa0cSTirupathi Reddy 	 */
2405c9ccaa0cSTirupathi Reddy 	cancel_delayed_work(&rdev->disable_work);
2406c9ccaa0cSTirupathi Reddy 
2407da07ecd9SMark Brown 	for (i = 0; i < count; i++) {
2408da07ecd9SMark Brown 		ret = _regulator_disable(rdev);
2409da07ecd9SMark Brown 		if (ret != 0)
2410da07ecd9SMark Brown 			rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2411da07ecd9SMark Brown 	}
2412da07ecd9SMark Brown 
2413da07ecd9SMark Brown 	mutex_unlock(&rdev->mutex);
2414da07ecd9SMark Brown 
2415da07ecd9SMark Brown 	if (rdev->supply) {
2416da07ecd9SMark Brown 		for (i = 0; i < count; i++) {
2417da07ecd9SMark Brown 			ret = regulator_disable(rdev->supply);
2418da07ecd9SMark Brown 			if (ret != 0) {
2419da07ecd9SMark Brown 				rdev_err(rdev,
2420da07ecd9SMark Brown 					 "Supply disable failed: %d\n", ret);
2421da07ecd9SMark Brown 			}
2422da07ecd9SMark Brown 		}
2423da07ecd9SMark Brown 	}
2424da07ecd9SMark Brown }
2425da07ecd9SMark Brown 
2426da07ecd9SMark Brown /**
2427da07ecd9SMark Brown  * regulator_disable_deferred - disable regulator output with delay
2428da07ecd9SMark Brown  * @regulator: regulator source
2429da07ecd9SMark Brown  * @ms: miliseconds until the regulator is disabled
2430da07ecd9SMark Brown  *
2431da07ecd9SMark Brown  * Execute regulator_disable() on the regulator after a delay.  This
2432da07ecd9SMark Brown  * is intended for use with devices that require some time to quiesce.
2433da07ecd9SMark Brown  *
2434da07ecd9SMark Brown  * NOTE: this will only disable the regulator output if no other consumer
2435da07ecd9SMark Brown  * devices have it enabled, the regulator device supports disabling and
2436da07ecd9SMark Brown  * machine constraints permit this operation.
2437da07ecd9SMark Brown  */
2438da07ecd9SMark Brown int regulator_disable_deferred(struct regulator *regulator, int ms)
2439da07ecd9SMark Brown {
2440da07ecd9SMark Brown 	struct regulator_dev *rdev = regulator->rdev;
2441da07ecd9SMark Brown 
24426492bc1bSMark Brown 	if (regulator->always_on)
24436492bc1bSMark Brown 		return 0;
24446492bc1bSMark Brown 
24452b5a24a0SMark Brown 	if (!ms)
24462b5a24a0SMark Brown 		return regulator_disable(regulator);
24472b5a24a0SMark Brown 
2448da07ecd9SMark Brown 	mutex_lock(&rdev->mutex);
2449da07ecd9SMark Brown 	rdev->deferred_disables++;
2450c9ccaa0cSTirupathi Reddy 	mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2451c9ccaa0cSTirupathi Reddy 			 msecs_to_jiffies(ms));
2452da07ecd9SMark Brown 	mutex_unlock(&rdev->mutex);
2453da07ecd9SMark Brown 
2454aa59802dSMark Brown 	return 0;
2455da07ecd9SMark Brown }
2456da07ecd9SMark Brown EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2457da07ecd9SMark Brown 
2458414c70cbSLiam Girdwood static int _regulator_is_enabled(struct regulator_dev *rdev)
2459414c70cbSLiam Girdwood {
246065f73508SMark Brown 	/* A GPIO control always takes precedence */
24617b74d149SKim, Milo 	if (rdev->ena_pin)
246265f73508SMark Brown 		return rdev->ena_gpio_state;
246365f73508SMark Brown 
24649a7f6a4cSMark Brown 	/* If we don't know then assume that the regulator is always on */
24659332546fSMark Brown 	if (!rdev->desc->ops->is_enabled)
24669a7f6a4cSMark Brown 		return 1;
2467414c70cbSLiam Girdwood 
24689332546fSMark Brown 	return rdev->desc->ops->is_enabled(rdev);
2469414c70cbSLiam Girdwood }
2470414c70cbSLiam Girdwood 
24713a40cfc3SSascha Hauer static int _regulator_list_voltage(struct regulator *regulator,
24723a40cfc3SSascha Hauer 				    unsigned selector, int lock)
24733a40cfc3SSascha Hauer {
24743a40cfc3SSascha Hauer 	struct regulator_dev *rdev = regulator->rdev;
24753a40cfc3SSascha Hauer 	const struct regulator_ops *ops = rdev->desc->ops;
24763a40cfc3SSascha Hauer 	int ret;
24773a40cfc3SSascha Hauer 
24783a40cfc3SSascha Hauer 	if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
24793a40cfc3SSascha Hauer 		return rdev->desc->fixed_uV;
24803a40cfc3SSascha Hauer 
24813a40cfc3SSascha Hauer 	if (ops->list_voltage) {
24823a40cfc3SSascha Hauer 		if (selector >= rdev->desc->n_voltages)
24833a40cfc3SSascha Hauer 			return -EINVAL;
24843a40cfc3SSascha Hauer 		if (lock)
24853a40cfc3SSascha Hauer 			mutex_lock(&rdev->mutex);
24863a40cfc3SSascha Hauer 		ret = ops->list_voltage(rdev, selector);
24873a40cfc3SSascha Hauer 		if (lock)
24883a40cfc3SSascha Hauer 			mutex_unlock(&rdev->mutex);
2489fd086045SMatthias Kaehlcke 	} else if (rdev->is_switch && rdev->supply) {
24903a40cfc3SSascha Hauer 		ret = _regulator_list_voltage(rdev->supply, selector, lock);
24913a40cfc3SSascha Hauer 	} else {
24923a40cfc3SSascha Hauer 		return -EINVAL;
24933a40cfc3SSascha Hauer 	}
24943a40cfc3SSascha Hauer 
24953a40cfc3SSascha Hauer 	if (ret > 0) {
24963a40cfc3SSascha Hauer 		if (ret < rdev->constraints->min_uV)
24973a40cfc3SSascha Hauer 			ret = 0;
24983a40cfc3SSascha Hauer 		else if (ret > rdev->constraints->max_uV)
24993a40cfc3SSascha Hauer 			ret = 0;
25003a40cfc3SSascha Hauer 	}
25013a40cfc3SSascha Hauer 
25023a40cfc3SSascha Hauer 	return ret;
25033a40cfc3SSascha Hauer }
25043a40cfc3SSascha Hauer 
2505414c70cbSLiam Girdwood /**
2506414c70cbSLiam Girdwood  * regulator_is_enabled - is the regulator output enabled
2507414c70cbSLiam Girdwood  * @regulator: regulator source
2508414c70cbSLiam Girdwood  *
2509412aec61SDavid Brownell  * Returns positive if the regulator driver backing the source/client
2510412aec61SDavid Brownell  * has requested that the device be enabled, zero if it hasn't, else a
2511412aec61SDavid Brownell  * negative errno code.
2512412aec61SDavid Brownell  *
2513412aec61SDavid Brownell  * Note that the device backing this regulator handle can have multiple
2514412aec61SDavid Brownell  * users, so it might be enabled even if regulator_enable() was never
2515412aec61SDavid Brownell  * called for this particular source.
2516414c70cbSLiam Girdwood  */
2517414c70cbSLiam Girdwood int regulator_is_enabled(struct regulator *regulator)
2518414c70cbSLiam Girdwood {
25199332546fSMark Brown 	int ret;
25209332546fSMark Brown 
25216492bc1bSMark Brown 	if (regulator->always_on)
25226492bc1bSMark Brown 		return 1;
25236492bc1bSMark Brown 
25249332546fSMark Brown 	mutex_lock(&regulator->rdev->mutex);
25259332546fSMark Brown 	ret = _regulator_is_enabled(regulator->rdev);
25269332546fSMark Brown 	mutex_unlock(&regulator->rdev->mutex);
25279332546fSMark Brown 
25289332546fSMark Brown 	return ret;
2529414c70cbSLiam Girdwood }
2530414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_is_enabled);
2531414c70cbSLiam Girdwood 
2532414c70cbSLiam Girdwood /**
25334367cfdcSDavid Brownell  * regulator_count_voltages - count regulator_list_voltage() selectors
25344367cfdcSDavid Brownell  * @regulator: regulator source
25354367cfdcSDavid Brownell  *
25364367cfdcSDavid Brownell  * Returns number of selectors, or negative errno.  Selectors are
25374367cfdcSDavid Brownell  * numbered starting at zero, and typically correspond to bitfields
25384367cfdcSDavid Brownell  * in hardware registers.
25394367cfdcSDavid Brownell  */
25404367cfdcSDavid Brownell int regulator_count_voltages(struct regulator *regulator)
25414367cfdcSDavid Brownell {
25424367cfdcSDavid Brownell 	struct regulator_dev	*rdev = regulator->rdev;
25434367cfdcSDavid Brownell 
254426988efeSJavier Martinez Canillas 	if (rdev->desc->n_voltages)
254526988efeSJavier Martinez Canillas 		return rdev->desc->n_voltages;
254626988efeSJavier Martinez Canillas 
2547fd086045SMatthias Kaehlcke 	if (!rdev->is_switch || !rdev->supply)
254826988efeSJavier Martinez Canillas 		return -EINVAL;
254926988efeSJavier Martinez Canillas 
255026988efeSJavier Martinez Canillas 	return regulator_count_voltages(rdev->supply);
25514367cfdcSDavid Brownell }
25524367cfdcSDavid Brownell EXPORT_SYMBOL_GPL(regulator_count_voltages);
25534367cfdcSDavid Brownell 
25544367cfdcSDavid Brownell /**
25554367cfdcSDavid Brownell  * regulator_list_voltage - enumerate supported voltages
25564367cfdcSDavid Brownell  * @regulator: regulator source
25574367cfdcSDavid Brownell  * @selector: identify voltage to list
25584367cfdcSDavid Brownell  * Context: can sleep
25594367cfdcSDavid Brownell  *
25604367cfdcSDavid Brownell  * Returns a voltage that can be passed to @regulator_set_voltage(),
256188393161SThomas Weber  * zero if this selector code can't be used on this system, or a
25624367cfdcSDavid Brownell  * negative errno.
25634367cfdcSDavid Brownell  */
25644367cfdcSDavid Brownell int regulator_list_voltage(struct regulator *regulator, unsigned selector)
25654367cfdcSDavid Brownell {
25663a40cfc3SSascha Hauer 	return _regulator_list_voltage(regulator, selector, 1);
25674367cfdcSDavid Brownell }
25684367cfdcSDavid Brownell EXPORT_SYMBOL_GPL(regulator_list_voltage);
25694367cfdcSDavid Brownell 
25704367cfdcSDavid Brownell /**
257104eca28cSTuomas Tynkkynen  * regulator_get_regmap - get the regulator's register map
257204eca28cSTuomas Tynkkynen  * @regulator: regulator source
257304eca28cSTuomas Tynkkynen  *
257404eca28cSTuomas Tynkkynen  * Returns the register map for the given regulator, or an ERR_PTR value
257504eca28cSTuomas Tynkkynen  * if the regulator doesn't use regmap.
257604eca28cSTuomas Tynkkynen  */
257704eca28cSTuomas Tynkkynen struct regmap *regulator_get_regmap(struct regulator *regulator)
257804eca28cSTuomas Tynkkynen {
257904eca28cSTuomas Tynkkynen 	struct regmap *map = regulator->rdev->regmap;
258004eca28cSTuomas Tynkkynen 
258104eca28cSTuomas Tynkkynen 	return map ? map : ERR_PTR(-EOPNOTSUPP);
258204eca28cSTuomas Tynkkynen }
258304eca28cSTuomas Tynkkynen 
258404eca28cSTuomas Tynkkynen /**
258504eca28cSTuomas Tynkkynen  * regulator_get_hardware_vsel_register - get the HW voltage selector register
258604eca28cSTuomas Tynkkynen  * @regulator: regulator source
258704eca28cSTuomas Tynkkynen  * @vsel_reg: voltage selector register, output parameter
258804eca28cSTuomas Tynkkynen  * @vsel_mask: mask for voltage selector bitfield, output parameter
258904eca28cSTuomas Tynkkynen  *
259004eca28cSTuomas Tynkkynen  * Returns the hardware register offset and bitmask used for setting the
259104eca28cSTuomas Tynkkynen  * regulator voltage. This might be useful when configuring voltage-scaling
259204eca28cSTuomas Tynkkynen  * hardware or firmware that can make I2C requests behind the kernel's back,
259304eca28cSTuomas Tynkkynen  * for example.
259404eca28cSTuomas Tynkkynen  *
259504eca28cSTuomas Tynkkynen  * On success, the output parameters @vsel_reg and @vsel_mask are filled in
259604eca28cSTuomas Tynkkynen  * and 0 is returned, otherwise a negative errno is returned.
259704eca28cSTuomas Tynkkynen  */
259804eca28cSTuomas Tynkkynen int regulator_get_hardware_vsel_register(struct regulator *regulator,
259904eca28cSTuomas Tynkkynen 					 unsigned *vsel_reg,
260004eca28cSTuomas Tynkkynen 					 unsigned *vsel_mask)
260104eca28cSTuomas Tynkkynen {
260204eca28cSTuomas Tynkkynen 	struct regulator_dev *rdev = regulator->rdev;
260339f5460dSGuodong Xu 	const struct regulator_ops *ops = rdev->desc->ops;
260404eca28cSTuomas Tynkkynen 
260504eca28cSTuomas Tynkkynen 	if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
260604eca28cSTuomas Tynkkynen 		return -EOPNOTSUPP;
260704eca28cSTuomas Tynkkynen 
260804eca28cSTuomas Tynkkynen 	 *vsel_reg = rdev->desc->vsel_reg;
260904eca28cSTuomas Tynkkynen 	 *vsel_mask = rdev->desc->vsel_mask;
261004eca28cSTuomas Tynkkynen 
261104eca28cSTuomas Tynkkynen 	 return 0;
261204eca28cSTuomas Tynkkynen }
261304eca28cSTuomas Tynkkynen EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
261404eca28cSTuomas Tynkkynen 
261504eca28cSTuomas Tynkkynen /**
261604eca28cSTuomas Tynkkynen  * regulator_list_hardware_vsel - get the HW-specific register value for a selector
261704eca28cSTuomas Tynkkynen  * @regulator: regulator source
261804eca28cSTuomas Tynkkynen  * @selector: identify voltage to list
261904eca28cSTuomas Tynkkynen  *
262004eca28cSTuomas Tynkkynen  * Converts the selector to a hardware-specific voltage selector that can be
262104eca28cSTuomas Tynkkynen  * directly written to the regulator registers. The address of the voltage
262204eca28cSTuomas Tynkkynen  * register can be determined by calling @regulator_get_hardware_vsel_register.
262304eca28cSTuomas Tynkkynen  *
262404eca28cSTuomas Tynkkynen  * On error a negative errno is returned.
262504eca28cSTuomas Tynkkynen  */
262604eca28cSTuomas Tynkkynen int regulator_list_hardware_vsel(struct regulator *regulator,
262704eca28cSTuomas Tynkkynen 				 unsigned selector)
262804eca28cSTuomas Tynkkynen {
262904eca28cSTuomas Tynkkynen 	struct regulator_dev *rdev = regulator->rdev;
263039f5460dSGuodong Xu 	const struct regulator_ops *ops = rdev->desc->ops;
263104eca28cSTuomas Tynkkynen 
263204eca28cSTuomas Tynkkynen 	if (selector >= rdev->desc->n_voltages)
263304eca28cSTuomas Tynkkynen 		return -EINVAL;
263404eca28cSTuomas Tynkkynen 	if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
263504eca28cSTuomas Tynkkynen 		return -EOPNOTSUPP;
263604eca28cSTuomas Tynkkynen 
263704eca28cSTuomas Tynkkynen 	return selector;
263804eca28cSTuomas Tynkkynen }
263904eca28cSTuomas Tynkkynen EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
264004eca28cSTuomas Tynkkynen 
264104eca28cSTuomas Tynkkynen /**
26422a668a8bSPaul Walmsley  * regulator_get_linear_step - return the voltage step size between VSEL values
26432a668a8bSPaul Walmsley  * @regulator: regulator source
26442a668a8bSPaul Walmsley  *
26452a668a8bSPaul Walmsley  * Returns the voltage step size between VSEL values for linear
26462a668a8bSPaul Walmsley  * regulators, or return 0 if the regulator isn't a linear regulator.
26472a668a8bSPaul Walmsley  */
26482a668a8bSPaul Walmsley unsigned int regulator_get_linear_step(struct regulator *regulator)
26492a668a8bSPaul Walmsley {
26502a668a8bSPaul Walmsley 	struct regulator_dev *rdev = regulator->rdev;
26512a668a8bSPaul Walmsley 
26522a668a8bSPaul Walmsley 	return rdev->desc->uV_step;
26532a668a8bSPaul Walmsley }
26542a668a8bSPaul Walmsley EXPORT_SYMBOL_GPL(regulator_get_linear_step);
26552a668a8bSPaul Walmsley 
26562a668a8bSPaul Walmsley /**
2657a7a1ad90SMark Brown  * regulator_is_supported_voltage - check if a voltage range can be supported
2658a7a1ad90SMark Brown  *
2659a7a1ad90SMark Brown  * @regulator: Regulator to check.
2660a7a1ad90SMark Brown  * @min_uV: Minimum required voltage in uV.
2661a7a1ad90SMark Brown  * @max_uV: Maximum required voltage in uV.
2662a7a1ad90SMark Brown  *
2663a7a1ad90SMark Brown  * Returns a boolean or a negative error code.
2664a7a1ad90SMark Brown  */
2665a7a1ad90SMark Brown int regulator_is_supported_voltage(struct regulator *regulator,
2666a7a1ad90SMark Brown 				   int min_uV, int max_uV)
2667a7a1ad90SMark Brown {
2668c5f3939bSMark Brown 	struct regulator_dev *rdev = regulator->rdev;
2669a7a1ad90SMark Brown 	int i, voltages, ret;
2670a7a1ad90SMark Brown 
2671c5f3939bSMark Brown 	/* If we can't change voltage check the current voltage */
26728a34e979SWEN Pingbo 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2673c5f3939bSMark Brown 		ret = regulator_get_voltage(regulator);
2674c5f3939bSMark Brown 		if (ret >= 0)
26750d25d09dSJingoo Han 			return min_uV <= ret && ret <= max_uV;
2676c5f3939bSMark Brown 		else
2677c5f3939bSMark Brown 			return ret;
2678c5f3939bSMark Brown 	}
2679c5f3939bSMark Brown 
2680bd7a2b60SPawel Moll 	/* Any voltage within constrains range is fine? */
2681bd7a2b60SPawel Moll 	if (rdev->desc->continuous_voltage_range)
2682bd7a2b60SPawel Moll 		return min_uV >= rdev->constraints->min_uV &&
2683bd7a2b60SPawel Moll 				max_uV <= rdev->constraints->max_uV;
2684bd7a2b60SPawel Moll 
2685a7a1ad90SMark Brown 	ret = regulator_count_voltages(regulator);
2686a7a1ad90SMark Brown 	if (ret < 0)
2687a7a1ad90SMark Brown 		return ret;
2688a7a1ad90SMark Brown 	voltages = ret;
2689a7a1ad90SMark Brown 
2690a7a1ad90SMark Brown 	for (i = 0; i < voltages; i++) {
2691a7a1ad90SMark Brown 		ret = regulator_list_voltage(regulator, i);
2692a7a1ad90SMark Brown 
2693a7a1ad90SMark Brown 		if (ret >= min_uV && ret <= max_uV)
2694a7a1ad90SMark Brown 			return 1;
2695a7a1ad90SMark Brown 	}
2696a7a1ad90SMark Brown 
2697a7a1ad90SMark Brown 	return 0;
2698a7a1ad90SMark Brown }
2699a398eaa2SMark Brown EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2700a7a1ad90SMark Brown 
2701a204f41eSSascha Hauer static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2702a204f41eSSascha Hauer 				 int max_uV)
2703a204f41eSSascha Hauer {
2704a204f41eSSascha Hauer 	const struct regulator_desc *desc = rdev->desc;
2705a204f41eSSascha Hauer 
2706a204f41eSSascha Hauer 	if (desc->ops->map_voltage)
2707a204f41eSSascha Hauer 		return desc->ops->map_voltage(rdev, min_uV, max_uV);
2708a204f41eSSascha Hauer 
2709a204f41eSSascha Hauer 	if (desc->ops->list_voltage == regulator_list_voltage_linear)
2710a204f41eSSascha Hauer 		return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2711a204f41eSSascha Hauer 
2712a204f41eSSascha Hauer 	if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2713a204f41eSSascha Hauer 		return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2714a204f41eSSascha Hauer 
2715a204f41eSSascha Hauer 	return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2716a204f41eSSascha Hauer }
2717a204f41eSSascha Hauer 
27187179569aSHeiko Stübner static int _regulator_call_set_voltage(struct regulator_dev *rdev,
27197179569aSHeiko Stübner 				       int min_uV, int max_uV,
27207179569aSHeiko Stübner 				       unsigned *selector)
27217179569aSHeiko Stübner {
27227179569aSHeiko Stübner 	struct pre_voltage_change_data data;
27237179569aSHeiko Stübner 	int ret;
27247179569aSHeiko Stübner 
27257179569aSHeiko Stübner 	data.old_uV = _regulator_get_voltage(rdev);
27267179569aSHeiko Stübner 	data.min_uV = min_uV;
27277179569aSHeiko Stübner 	data.max_uV = max_uV;
27287179569aSHeiko Stübner 	ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
27297179569aSHeiko Stübner 				   &data);
27307179569aSHeiko Stübner 	if (ret & NOTIFY_STOP_MASK)
27317179569aSHeiko Stübner 		return -EINVAL;
27327179569aSHeiko Stübner 
27337179569aSHeiko Stübner 	ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
27347179569aSHeiko Stübner 	if (ret >= 0)
27357179569aSHeiko Stübner 		return ret;
27367179569aSHeiko Stübner 
27377179569aSHeiko Stübner 	_notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
27387179569aSHeiko Stübner 			     (void *)data.old_uV);
27397179569aSHeiko Stübner 
27407179569aSHeiko Stübner 	return ret;
27417179569aSHeiko Stübner }
27427179569aSHeiko Stübner 
27437179569aSHeiko Stübner static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
27447179569aSHeiko Stübner 					   int uV, unsigned selector)
27457179569aSHeiko Stübner {
27467179569aSHeiko Stübner 	struct pre_voltage_change_data data;
27477179569aSHeiko Stübner 	int ret;
27487179569aSHeiko Stübner 
27497179569aSHeiko Stübner 	data.old_uV = _regulator_get_voltage(rdev);
27507179569aSHeiko Stübner 	data.min_uV = uV;
27517179569aSHeiko Stübner 	data.max_uV = uV;
27527179569aSHeiko Stübner 	ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
27537179569aSHeiko Stübner 				   &data);
27547179569aSHeiko Stübner 	if (ret & NOTIFY_STOP_MASK)
27557179569aSHeiko Stübner 		return -EINVAL;
27567179569aSHeiko Stübner 
27577179569aSHeiko Stübner 	ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
27587179569aSHeiko Stübner 	if (ret >= 0)
27597179569aSHeiko Stübner 		return ret;
27607179569aSHeiko Stübner 
27617179569aSHeiko Stübner 	_notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
27627179569aSHeiko Stübner 			     (void *)data.old_uV);
27637179569aSHeiko Stübner 
27647179569aSHeiko Stübner 	return ret;
27657179569aSHeiko Stübner }
27667179569aSHeiko Stübner 
276773e705bfSMatthias Kaehlcke static int _regulator_set_voltage_time(struct regulator_dev *rdev,
276873e705bfSMatthias Kaehlcke 				       int old_uV, int new_uV)
276973e705bfSMatthias Kaehlcke {
277073e705bfSMatthias Kaehlcke 	unsigned int ramp_delay = 0;
277173e705bfSMatthias Kaehlcke 
277273e705bfSMatthias Kaehlcke 	if (rdev->constraints->ramp_delay)
277373e705bfSMatthias Kaehlcke 		ramp_delay = rdev->constraints->ramp_delay;
277473e705bfSMatthias Kaehlcke 	else if (rdev->desc->ramp_delay)
277573e705bfSMatthias Kaehlcke 		ramp_delay = rdev->desc->ramp_delay;
2776d6c1dc3fSLaxman Dewangan 	else if (rdev->constraints->settling_time)
2777d6c1dc3fSLaxman Dewangan 		return rdev->constraints->settling_time;
27783ffad468SMatthias Kaehlcke 	else if (rdev->constraints->settling_time_up &&
27793ffad468SMatthias Kaehlcke 		 (new_uV > old_uV))
27803ffad468SMatthias Kaehlcke 		return rdev->constraints->settling_time_up;
27813ffad468SMatthias Kaehlcke 	else if (rdev->constraints->settling_time_down &&
27823ffad468SMatthias Kaehlcke 		 (new_uV < old_uV))
27833ffad468SMatthias Kaehlcke 		return rdev->constraints->settling_time_down;
278473e705bfSMatthias Kaehlcke 
278573e705bfSMatthias Kaehlcke 	if (ramp_delay == 0) {
2786ba14fa1aSH. Nikolaus Schaller 		rdev_dbg(rdev, "ramp_delay not set\n");
278773e705bfSMatthias Kaehlcke 		return 0;
278873e705bfSMatthias Kaehlcke 	}
278973e705bfSMatthias Kaehlcke 
279073e705bfSMatthias Kaehlcke 	return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
279173e705bfSMatthias Kaehlcke }
279273e705bfSMatthias Kaehlcke 
279375790251SMark Brown static int _regulator_do_set_voltage(struct regulator_dev *rdev,
279475790251SMark Brown 				     int min_uV, int max_uV)
279575790251SMark Brown {
279675790251SMark Brown 	int ret;
279777af1b26SLinus Walleij 	int delay = 0;
2798e113d792SMark Brown 	int best_val = 0;
279975790251SMark Brown 	unsigned int selector;
2800eba41a5eSAxel Lin 	int old_selector = -1;
280157995a48SMatthias Kaehlcke 	const struct regulator_ops *ops = rdev->desc->ops;
280273e705bfSMatthias Kaehlcke 	int old_uV = _regulator_get_voltage(rdev);
280375790251SMark Brown 
280475790251SMark Brown 	trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
280575790251SMark Brown 
2806bf5892a8SMark Brown 	min_uV += rdev->constraints->uV_offset;
2807bf5892a8SMark Brown 	max_uV += rdev->constraints->uV_offset;
2808bf5892a8SMark Brown 
280977af1b26SLinus Walleij 	/*
281077af1b26SLinus Walleij 	 * If we can't obtain the old selector there is not enough
281177af1b26SLinus Walleij 	 * info to call set_voltage_time_sel().
281277af1b26SLinus Walleij 	 */
28138b7485efSAxel Lin 	if (_regulator_is_enabled(rdev) &&
281457995a48SMatthias Kaehlcke 	    ops->set_voltage_time_sel && ops->get_voltage_sel) {
281557995a48SMatthias Kaehlcke 		old_selector = ops->get_voltage_sel(rdev);
2816eba41a5eSAxel Lin 		if (old_selector < 0)
2817eba41a5eSAxel Lin 			return old_selector;
2818eba41a5eSAxel Lin 	}
281977af1b26SLinus Walleij 
282057995a48SMatthias Kaehlcke 	if (ops->set_voltage) {
28217179569aSHeiko Stübner 		ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
282275790251SMark Brown 						  &selector);
2823e113d792SMark Brown 
2824e113d792SMark Brown 		if (ret >= 0) {
282557995a48SMatthias Kaehlcke 			if (ops->list_voltage)
282657995a48SMatthias Kaehlcke 				best_val = ops->list_voltage(rdev,
2827e113d792SMark Brown 							     selector);
2828e113d792SMark Brown 			else
2829e113d792SMark Brown 				best_val = _regulator_get_voltage(rdev);
2830e113d792SMark Brown 		}
2831e113d792SMark Brown 
283257995a48SMatthias Kaehlcke 	} else if (ops->set_voltage_sel) {
2833a204f41eSSascha Hauer 		ret = regulator_map_voltage(rdev, min_uV, max_uV);
2834e843fc46SMark Brown 		if (ret >= 0) {
283557995a48SMatthias Kaehlcke 			best_val = ops->list_voltage(rdev, ret);
2836e113d792SMark Brown 			if (min_uV <= best_val && max_uV >= best_val) {
2837e843fc46SMark Brown 				selector = ret;
2838c66a566aSAxel Lin 				if (old_selector == selector)
2839c66a566aSAxel Lin 					ret = 0;
2840c66a566aSAxel Lin 				else
28417179569aSHeiko Stübner 					ret = _regulator_call_set_voltage_sel(
28427179569aSHeiko Stübner 						rdev, best_val, selector);
2843e113d792SMark Brown 			} else {
2844e113d792SMark Brown 				ret = -EINVAL;
2845e113d792SMark Brown 			}
2846e843fc46SMark Brown 		}
2847e8eef82bSMark Brown 	} else {
2848e8eef82bSMark Brown 		ret = -EINVAL;
2849e8eef82bSMark Brown 	}
2850e8eef82bSMark Brown 
285131dfe686SMatthias Kaehlcke 	if (ret)
285231dfe686SMatthias Kaehlcke 		goto out;
2853eba41a5eSAxel Lin 
285473e705bfSMatthias Kaehlcke 	if (ops->set_voltage_time_sel) {
285573e705bfSMatthias Kaehlcke 		/*
285673e705bfSMatthias Kaehlcke 		 * Call set_voltage_time_sel if successfully obtained
285773e705bfSMatthias Kaehlcke 		 * old_selector
285873e705bfSMatthias Kaehlcke 		 */
285973e705bfSMatthias Kaehlcke 		if (old_selector >= 0 && old_selector != selector)
286073e705bfSMatthias Kaehlcke 			delay = ops->set_voltage_time_sel(rdev, old_selector,
286173e705bfSMatthias Kaehlcke 							  selector);
286273e705bfSMatthias Kaehlcke 	} else {
286373e705bfSMatthias Kaehlcke 		if (old_uV != best_val) {
286473e705bfSMatthias Kaehlcke 			if (ops->set_voltage_time)
286573e705bfSMatthias Kaehlcke 				delay = ops->set_voltage_time(rdev, old_uV,
286673e705bfSMatthias Kaehlcke 							      best_val);
286773e705bfSMatthias Kaehlcke 			else
286873e705bfSMatthias Kaehlcke 				delay = _regulator_set_voltage_time(rdev,
286973e705bfSMatthias Kaehlcke 								    old_uV,
287073e705bfSMatthias Kaehlcke 								    best_val);
287173e705bfSMatthias Kaehlcke 		}
287273e705bfSMatthias Kaehlcke 	}
287373e705bfSMatthias Kaehlcke 
2874eba41a5eSAxel Lin 	if (delay < 0) {
287573e705bfSMatthias Kaehlcke 		rdev_warn(rdev, "failed to get delay: %d\n", delay);
2876eba41a5eSAxel Lin 		delay = 0;
2877e8eef82bSMark Brown 	}
287875790251SMark Brown 
287977af1b26SLinus Walleij 	/* Insert any necessary delays */
288077af1b26SLinus Walleij 	if (delay >= 1000) {
288177af1b26SLinus Walleij 		mdelay(delay / 1000);
288277af1b26SLinus Walleij 		udelay(delay % 1000);
288377af1b26SLinus Walleij 	} else if (delay) {
288477af1b26SLinus Walleij 		udelay(delay);
288577af1b26SLinus Walleij 	}
288677af1b26SLinus Walleij 
288731dfe686SMatthias Kaehlcke 	if (best_val >= 0) {
28882f6c797fSAxel Lin 		unsigned long data = best_val;
28892f6c797fSAxel Lin 
2890ded06a52SMark Brown 		_notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
28912f6c797fSAxel Lin 				     (void *)data);
28922f6c797fSAxel Lin 	}
2893ded06a52SMark Brown 
289431dfe686SMatthias Kaehlcke out:
2895eba41a5eSAxel Lin 	trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
289675790251SMark Brown 
289775790251SMark Brown 	return ret;
289875790251SMark Brown }
289975790251SMark Brown 
2900a9f226bcSSascha Hauer static int regulator_set_voltage_unlocked(struct regulator *regulator,
2901a9f226bcSSascha Hauer 					  int min_uV, int max_uV)
2902414c70cbSLiam Girdwood {
2903414c70cbSLiam Girdwood 	struct regulator_dev *rdev = regulator->rdev;
290495a3c23aSMark Brown 	int ret = 0;
290592d7a558SPaolo Pisati 	int old_min_uV, old_max_uV;
2906c00dc359SBjorn Andersson 	int current_uV;
2907fc42112cSSascha Hauer 	int best_supply_uV = 0;
2908fc42112cSSascha Hauer 	int supply_change_uV = 0;
2909414c70cbSLiam Girdwood 
291095a3c23aSMark Brown 	/* If we're setting the same range as last time the change
291195a3c23aSMark Brown 	 * should be a noop (some cpufreq implementations use the same
291295a3c23aSMark Brown 	 * voltage for multiple frequencies, for example).
291395a3c23aSMark Brown 	 */
291495a3c23aSMark Brown 	if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
291595a3c23aSMark Brown 		goto out;
291695a3c23aSMark Brown 
2917c00dc359SBjorn Andersson 	/* If we're trying to set a range that overlaps the current voltage,
2918d3fb9800SViresh Kumar 	 * return successfully even though the regulator does not support
2919c00dc359SBjorn Andersson 	 * changing the voltage.
2920c00dc359SBjorn Andersson 	 */
29218a34e979SWEN Pingbo 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2922c00dc359SBjorn Andersson 		current_uV = _regulator_get_voltage(rdev);
2923c00dc359SBjorn Andersson 		if (min_uV <= current_uV && current_uV <= max_uV) {
2924c00dc359SBjorn Andersson 			regulator->min_uV = min_uV;
2925c00dc359SBjorn Andersson 			regulator->max_uV = max_uV;
2926c00dc359SBjorn Andersson 			goto out;
2927c00dc359SBjorn Andersson 		}
2928c00dc359SBjorn Andersson 	}
2929c00dc359SBjorn Andersson 
2930414c70cbSLiam Girdwood 	/* sanity check */
2931e8eef82bSMark Brown 	if (!rdev->desc->ops->set_voltage &&
2932e8eef82bSMark Brown 	    !rdev->desc->ops->set_voltage_sel) {
2933414c70cbSLiam Girdwood 		ret = -EINVAL;
2934414c70cbSLiam Girdwood 		goto out;
2935414c70cbSLiam Girdwood 	}
2936414c70cbSLiam Girdwood 
2937414c70cbSLiam Girdwood 	/* constraints check */
2938414c70cbSLiam Girdwood 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2939414c70cbSLiam Girdwood 	if (ret < 0)
2940414c70cbSLiam Girdwood 		goto out;
294192d7a558SPaolo Pisati 
294292d7a558SPaolo Pisati 	/* restore original values in case of error */
294392d7a558SPaolo Pisati 	old_min_uV = regulator->min_uV;
294492d7a558SPaolo Pisati 	old_max_uV = regulator->max_uV;
2945414c70cbSLiam Girdwood 	regulator->min_uV = min_uV;
2946414c70cbSLiam Girdwood 	regulator->max_uV = max_uV;
29473a93f2a9SMark Brown 
294805fda3b1SThomas Petazzoni 	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
294905fda3b1SThomas Petazzoni 	if (ret < 0)
295092d7a558SPaolo Pisati 		goto out2;
295105fda3b1SThomas Petazzoni 
295243fc99f2SMark Brown 	if (rdev->supply &&
295343fc99f2SMark Brown 	    regulator_ops_is_valid(rdev->supply->rdev,
295443fc99f2SMark Brown 				   REGULATOR_CHANGE_VOLTAGE) &&
29552c2874b1STirupathi Reddy 	    (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
29562c2874b1STirupathi Reddy 					   rdev->desc->ops->get_voltage_sel))) {
2957fc42112cSSascha Hauer 		int current_supply_uV;
2958fc42112cSSascha Hauer 		int selector;
2959fc42112cSSascha Hauer 
2960fc42112cSSascha Hauer 		selector = regulator_map_voltage(rdev, min_uV, max_uV);
2961fc42112cSSascha Hauer 		if (selector < 0) {
2962fc42112cSSascha Hauer 			ret = selector;
2963fc42112cSSascha Hauer 			goto out2;
2964fc42112cSSascha Hauer 		}
2965fc42112cSSascha Hauer 
2966fc42112cSSascha Hauer 		best_supply_uV = _regulator_list_voltage(regulator, selector, 0);
2967fc42112cSSascha Hauer 		if (best_supply_uV < 0) {
2968fc42112cSSascha Hauer 			ret = best_supply_uV;
2969fc42112cSSascha Hauer 			goto out2;
2970fc42112cSSascha Hauer 		}
2971fc42112cSSascha Hauer 
2972fc42112cSSascha Hauer 		best_supply_uV += rdev->desc->min_dropout_uV;
2973fc42112cSSascha Hauer 
2974fc42112cSSascha Hauer 		current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
2975fc42112cSSascha Hauer 		if (current_supply_uV < 0) {
2976fc42112cSSascha Hauer 			ret = current_supply_uV;
2977fc42112cSSascha Hauer 			goto out2;
2978fc42112cSSascha Hauer 		}
2979fc42112cSSascha Hauer 
2980fc42112cSSascha Hauer 		supply_change_uV = best_supply_uV - current_supply_uV;
2981fc42112cSSascha Hauer 	}
2982fc42112cSSascha Hauer 
2983fc42112cSSascha Hauer 	if (supply_change_uV > 0) {
2984fc42112cSSascha Hauer 		ret = regulator_set_voltage_unlocked(rdev->supply,
2985fc42112cSSascha Hauer 				best_supply_uV, INT_MAX);
2986fc42112cSSascha Hauer 		if (ret) {
2987fc42112cSSascha Hauer 			dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
2988fc42112cSSascha Hauer 					ret);
2989fc42112cSSascha Hauer 			goto out2;
2990fc42112cSSascha Hauer 		}
2991fc42112cSSascha Hauer 	}
2992fc42112cSSascha Hauer 
299375790251SMark Brown 	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
299492d7a558SPaolo Pisati 	if (ret < 0)
299592d7a558SPaolo Pisati 		goto out2;
299602fa3ec0SMark Brown 
2997fc42112cSSascha Hauer 	if (supply_change_uV < 0) {
2998fc42112cSSascha Hauer 		ret = regulator_set_voltage_unlocked(rdev->supply,
2999fc42112cSSascha Hauer 				best_supply_uV, INT_MAX);
3000fc42112cSSascha Hauer 		if (ret)
3001fc42112cSSascha Hauer 			dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3002fc42112cSSascha Hauer 					ret);
3003fc42112cSSascha Hauer 		/* No need to fail here */
3004fc42112cSSascha Hauer 		ret = 0;
3005fc42112cSSascha Hauer 	}
3006fc42112cSSascha Hauer 
3007414c70cbSLiam Girdwood out:
3008414c70cbSLiam Girdwood 	return ret;
300992d7a558SPaolo Pisati out2:
301092d7a558SPaolo Pisati 	regulator->min_uV = old_min_uV;
301192d7a558SPaolo Pisati 	regulator->max_uV = old_max_uV;
3012a9f226bcSSascha Hauer 
3013a9f226bcSSascha Hauer 	return ret;
3014a9f226bcSSascha Hauer }
3015a9f226bcSSascha Hauer 
3016a9f226bcSSascha Hauer /**
3017a9f226bcSSascha Hauer  * regulator_set_voltage - set regulator output voltage
3018a9f226bcSSascha Hauer  * @regulator: regulator source
3019a9f226bcSSascha Hauer  * @min_uV: Minimum required voltage in uV
3020a9f226bcSSascha Hauer  * @max_uV: Maximum acceptable voltage in uV
3021a9f226bcSSascha Hauer  *
3022a9f226bcSSascha Hauer  * Sets a voltage regulator to the desired output voltage. This can be set
3023a9f226bcSSascha Hauer  * during any regulator state. IOW, regulator can be disabled or enabled.
3024a9f226bcSSascha Hauer  *
3025a9f226bcSSascha Hauer  * If the regulator is enabled then the voltage will change to the new value
3026a9f226bcSSascha Hauer  * immediately otherwise if the regulator is disabled the regulator will
3027a9f226bcSSascha Hauer  * output at the new voltage when enabled.
3028a9f226bcSSascha Hauer  *
3029a9f226bcSSascha Hauer  * NOTE: If the regulator is shared between several devices then the lowest
3030a9f226bcSSascha Hauer  * request voltage that meets the system constraints will be used.
3031a9f226bcSSascha Hauer  * Regulator system constraints must be set for this regulator before
3032a9f226bcSSascha Hauer  * calling this function otherwise this call will fail.
3033a9f226bcSSascha Hauer  */
3034a9f226bcSSascha Hauer int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3035a9f226bcSSascha Hauer {
3036a9f226bcSSascha Hauer 	int ret = 0;
3037a9f226bcSSascha Hauer 
3038fc42112cSSascha Hauer 	regulator_lock_supply(regulator->rdev);
3039a9f226bcSSascha Hauer 
3040a9f226bcSSascha Hauer 	ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV);
3041a9f226bcSSascha Hauer 
3042fc42112cSSascha Hauer 	regulator_unlock_supply(regulator->rdev);
3043a9f226bcSSascha Hauer 
304492d7a558SPaolo Pisati 	return ret;
3045414c70cbSLiam Girdwood }
3046414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_set_voltage);
3047414c70cbSLiam Girdwood 
3048606a2562SMark Brown /**
304988cd222bSLinus Walleij  * regulator_set_voltage_time - get raise/fall time
305088cd222bSLinus Walleij  * @regulator: regulator source
305188cd222bSLinus Walleij  * @old_uV: starting voltage in microvolts
305288cd222bSLinus Walleij  * @new_uV: target voltage in microvolts
305388cd222bSLinus Walleij  *
305488cd222bSLinus Walleij  * Provided with the starting and ending voltage, this function attempts to
305588cd222bSLinus Walleij  * calculate the time in microseconds required to rise or fall to this new
305688cd222bSLinus Walleij  * voltage.
305788cd222bSLinus Walleij  */
305888cd222bSLinus Walleij int regulator_set_voltage_time(struct regulator *regulator,
305988cd222bSLinus Walleij 			       int old_uV, int new_uV)
306088cd222bSLinus Walleij {
306188cd222bSLinus Walleij 	struct regulator_dev *rdev = regulator->rdev;
3062272e2315SGuodong Xu 	const struct regulator_ops *ops = rdev->desc->ops;
306388cd222bSLinus Walleij 	int old_sel = -1;
306488cd222bSLinus Walleij 	int new_sel = -1;
306588cd222bSLinus Walleij 	int voltage;
306688cd222bSLinus Walleij 	int i;
306788cd222bSLinus Walleij 
306873e705bfSMatthias Kaehlcke 	if (ops->set_voltage_time)
306973e705bfSMatthias Kaehlcke 		return ops->set_voltage_time(rdev, old_uV, new_uV);
307073e705bfSMatthias Kaehlcke 	else if (!ops->set_voltage_time_sel)
307173e705bfSMatthias Kaehlcke 		return _regulator_set_voltage_time(rdev, old_uV, new_uV);
307273e705bfSMatthias Kaehlcke 
307388cd222bSLinus Walleij 	/* Currently requires operations to do this */
307473e705bfSMatthias Kaehlcke 	if (!ops->list_voltage || !rdev->desc->n_voltages)
307588cd222bSLinus Walleij 		return -EINVAL;
307688cd222bSLinus Walleij 
307788cd222bSLinus Walleij 	for (i = 0; i < rdev->desc->n_voltages; i++) {
307888cd222bSLinus Walleij 		/* We only look for exact voltage matches here */
307988cd222bSLinus Walleij 		voltage = regulator_list_voltage(regulator, i);
308088cd222bSLinus Walleij 		if (voltage < 0)
308188cd222bSLinus Walleij 			return -EINVAL;
308288cd222bSLinus Walleij 		if (voltage == 0)
308388cd222bSLinus Walleij 			continue;
308488cd222bSLinus Walleij 		if (voltage == old_uV)
308588cd222bSLinus Walleij 			old_sel = i;
308688cd222bSLinus Walleij 		if (voltage == new_uV)
308788cd222bSLinus Walleij 			new_sel = i;
308888cd222bSLinus Walleij 	}
308988cd222bSLinus Walleij 
309088cd222bSLinus Walleij 	if (old_sel < 0 || new_sel < 0)
309188cd222bSLinus Walleij 		return -EINVAL;
309288cd222bSLinus Walleij 
309388cd222bSLinus Walleij 	return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
309488cd222bSLinus Walleij }
309588cd222bSLinus Walleij EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
309688cd222bSLinus Walleij 
309788cd222bSLinus Walleij /**
309898a175b6SYadwinder Singh Brar  * regulator_set_voltage_time_sel - get raise/fall time
3099296c6566SRandy Dunlap  * @rdev: regulator source device
310098a175b6SYadwinder Singh Brar  * @old_selector: selector for starting voltage
310198a175b6SYadwinder Singh Brar  * @new_selector: selector for target voltage
310298a175b6SYadwinder Singh Brar  *
310398a175b6SYadwinder Singh Brar  * Provided with the starting and target voltage selectors, this function
310498a175b6SYadwinder Singh Brar  * returns time in microseconds required to rise or fall to this new voltage
310598a175b6SYadwinder Singh Brar  *
3106f11d08c3SAxel Lin  * Drivers providing ramp_delay in regulation_constraints can use this as their
3107398715abSAxel Lin  * set_voltage_time_sel() operation.
310898a175b6SYadwinder Singh Brar  */
310998a175b6SYadwinder Singh Brar int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
311098a175b6SYadwinder Singh Brar 				   unsigned int old_selector,
311198a175b6SYadwinder Singh Brar 				   unsigned int new_selector)
311298a175b6SYadwinder Singh Brar {
3113f11d08c3SAxel Lin 	int old_volt, new_volt;
3114398715abSAxel Lin 
3115f11d08c3SAxel Lin 	/* sanity check */
3116f11d08c3SAxel Lin 	if (!rdev->desc->ops->list_voltage)
3117f11d08c3SAxel Lin 		return -EINVAL;
3118398715abSAxel Lin 
3119f11d08c3SAxel Lin 	old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3120f11d08c3SAxel Lin 	new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3121f11d08c3SAxel Lin 
312273e705bfSMatthias Kaehlcke 	if (rdev->desc->ops->set_voltage_time)
312373e705bfSMatthias Kaehlcke 		return rdev->desc->ops->set_voltage_time(rdev, old_volt,
312473e705bfSMatthias Kaehlcke 							 new_volt);
312573e705bfSMatthias Kaehlcke 	else
312673e705bfSMatthias Kaehlcke 		return _regulator_set_voltage_time(rdev, old_volt, new_volt);
312798a175b6SYadwinder Singh Brar }
3128b19dbf71SMark Brown EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
312998a175b6SYadwinder Singh Brar 
313098a175b6SYadwinder Singh Brar /**
3131606a2562SMark Brown  * regulator_sync_voltage - re-apply last regulator output voltage
3132606a2562SMark Brown  * @regulator: regulator source
3133606a2562SMark Brown  *
3134606a2562SMark Brown  * Re-apply the last configured voltage.  This is intended to be used
3135606a2562SMark Brown  * where some external control source the consumer is cooperating with
3136606a2562SMark Brown  * has caused the configured voltage to change.
3137606a2562SMark Brown  */
3138606a2562SMark Brown int regulator_sync_voltage(struct regulator *regulator)
3139606a2562SMark Brown {
3140606a2562SMark Brown 	struct regulator_dev *rdev = regulator->rdev;
3141606a2562SMark Brown 	int ret, min_uV, max_uV;
3142606a2562SMark Brown 
3143606a2562SMark Brown 	mutex_lock(&rdev->mutex);
3144606a2562SMark Brown 
3145606a2562SMark Brown 	if (!rdev->desc->ops->set_voltage &&
3146606a2562SMark Brown 	    !rdev->desc->ops->set_voltage_sel) {
3147606a2562SMark Brown 		ret = -EINVAL;
3148606a2562SMark Brown 		goto out;
3149606a2562SMark Brown 	}
3150606a2562SMark Brown 
3151606a2562SMark Brown 	/* This is only going to work if we've had a voltage configured. */
3152606a2562SMark Brown 	if (!regulator->min_uV && !regulator->max_uV) {
3153606a2562SMark Brown 		ret = -EINVAL;
3154606a2562SMark Brown 		goto out;
3155606a2562SMark Brown 	}
3156606a2562SMark Brown 
3157606a2562SMark Brown 	min_uV = regulator->min_uV;
3158606a2562SMark Brown 	max_uV = regulator->max_uV;
3159606a2562SMark Brown 
3160606a2562SMark Brown 	/* This should be a paranoia check... */
3161606a2562SMark Brown 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3162606a2562SMark Brown 	if (ret < 0)
3163606a2562SMark Brown 		goto out;
3164606a2562SMark Brown 
3165606a2562SMark Brown 	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
3166606a2562SMark Brown 	if (ret < 0)
3167606a2562SMark Brown 		goto out;
3168606a2562SMark Brown 
3169606a2562SMark Brown 	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3170606a2562SMark Brown 
3171606a2562SMark Brown out:
3172606a2562SMark Brown 	mutex_unlock(&rdev->mutex);
3173606a2562SMark Brown 	return ret;
3174606a2562SMark Brown }
3175606a2562SMark Brown EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3176606a2562SMark Brown 
3177414c70cbSLiam Girdwood static int _regulator_get_voltage(struct regulator_dev *rdev)
3178414c70cbSLiam Girdwood {
3179bf5892a8SMark Brown 	int sel, ret;
3180fef95019SMark Brown 	bool bypassed;
3181fef95019SMark Brown 
3182fef95019SMark Brown 	if (rdev->desc->ops->get_bypass) {
3183fef95019SMark Brown 		ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3184fef95019SMark Brown 		if (ret < 0)
3185fef95019SMark Brown 			return ret;
3186fef95019SMark Brown 		if (bypassed) {
3187fef95019SMark Brown 			/* if bypassed the regulator must have a supply */
318845389c47SJon Hunter 			if (!rdev->supply) {
318945389c47SJon Hunter 				rdev_err(rdev,
319045389c47SJon Hunter 					 "bypassed regulator has no supply!\n");
319145389c47SJon Hunter 				return -EPROBE_DEFER;
319245389c47SJon Hunter 			}
3193fef95019SMark Brown 
3194fef95019SMark Brown 			return _regulator_get_voltage(rdev->supply->rdev);
3195fef95019SMark Brown 		}
3196fef95019SMark Brown 	}
3197476c2d83SMark Brown 
3198476c2d83SMark Brown 	if (rdev->desc->ops->get_voltage_sel) {
3199476c2d83SMark Brown 		sel = rdev->desc->ops->get_voltage_sel(rdev);
3200476c2d83SMark Brown 		if (sel < 0)
3201476c2d83SMark Brown 			return sel;
3202bf5892a8SMark Brown 		ret = rdev->desc->ops->list_voltage(rdev, sel);
3203cb220d16SAxel Lin 	} else if (rdev->desc->ops->get_voltage) {
3204bf5892a8SMark Brown 		ret = rdev->desc->ops->get_voltage(rdev);
3205f7df20ecSMark Brown 	} else if (rdev->desc->ops->list_voltage) {
3206f7df20ecSMark Brown 		ret = rdev->desc->ops->list_voltage(rdev, 0);
32075a523605SLaxman Dewangan 	} else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
32085a523605SLaxman Dewangan 		ret = rdev->desc->fixed_uV;
3209e303996eSJavier Martinez Canillas 	} else if (rdev->supply) {
3210d9b96d35SMark Brown 		ret = _regulator_get_voltage(rdev->supply->rdev);
3211cb220d16SAxel Lin 	} else {
3212414c70cbSLiam Girdwood 		return -EINVAL;
3213cb220d16SAxel Lin 	}
3214bf5892a8SMark Brown 
3215cb220d16SAxel Lin 	if (ret < 0)
3216cb220d16SAxel Lin 		return ret;
3217bf5892a8SMark Brown 	return ret - rdev->constraints->uV_offset;
3218414c70cbSLiam Girdwood }
3219414c70cbSLiam Girdwood 
3220414c70cbSLiam Girdwood /**
3221414c70cbSLiam Girdwood  * regulator_get_voltage - get regulator output voltage
3222414c70cbSLiam Girdwood  * @regulator: regulator source
3223414c70cbSLiam Girdwood  *
3224414c70cbSLiam Girdwood  * This returns the current regulator voltage in uV.
3225414c70cbSLiam Girdwood  *
3226414c70cbSLiam Girdwood  * NOTE: If the regulator is disabled it will return the voltage value. This
3227414c70cbSLiam Girdwood  * function should not be used to determine regulator state.
3228414c70cbSLiam Girdwood  */
3229414c70cbSLiam Girdwood int regulator_get_voltage(struct regulator *regulator)
3230414c70cbSLiam Girdwood {
3231414c70cbSLiam Girdwood 	int ret;
3232414c70cbSLiam Girdwood 
3233d9b96d35SMark Brown 	regulator_lock_supply(regulator->rdev);
3234414c70cbSLiam Girdwood 
3235414c70cbSLiam Girdwood 	ret = _regulator_get_voltage(regulator->rdev);
3236414c70cbSLiam Girdwood 
3237d9b96d35SMark Brown 	regulator_unlock_supply(regulator->rdev);
3238414c70cbSLiam Girdwood 
3239414c70cbSLiam Girdwood 	return ret;
3240414c70cbSLiam Girdwood }
3241414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_voltage);
3242414c70cbSLiam Girdwood 
3243414c70cbSLiam Girdwood /**
3244414c70cbSLiam Girdwood  * regulator_set_current_limit - set regulator output current limit
3245414c70cbSLiam Girdwood  * @regulator: regulator source
3246ce0d10f8SCharles Keepax  * @min_uA: Minimum supported current in uA
3247414c70cbSLiam Girdwood  * @max_uA: Maximum supported current in uA
3248414c70cbSLiam Girdwood  *
3249414c70cbSLiam Girdwood  * Sets current sink to the desired output current. This can be set during
3250414c70cbSLiam Girdwood  * any regulator state. IOW, regulator can be disabled or enabled.
3251414c70cbSLiam Girdwood  *
3252414c70cbSLiam Girdwood  * If the regulator is enabled then the current will change to the new value
3253414c70cbSLiam Girdwood  * immediately otherwise if the regulator is disabled the regulator will
3254414c70cbSLiam Girdwood  * output at the new current when enabled.
3255414c70cbSLiam Girdwood  *
3256414c70cbSLiam Girdwood  * NOTE: Regulator system constraints must be set for this regulator before
3257414c70cbSLiam Girdwood  * calling this function otherwise this call will fail.
3258414c70cbSLiam Girdwood  */
3259414c70cbSLiam Girdwood int regulator_set_current_limit(struct regulator *regulator,
3260414c70cbSLiam Girdwood 			       int min_uA, int max_uA)
3261414c70cbSLiam Girdwood {
3262414c70cbSLiam Girdwood 	struct regulator_dev *rdev = regulator->rdev;
3263414c70cbSLiam Girdwood 	int ret;
3264414c70cbSLiam Girdwood 
3265414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
3266414c70cbSLiam Girdwood 
3267414c70cbSLiam Girdwood 	/* sanity check */
3268414c70cbSLiam Girdwood 	if (!rdev->desc->ops->set_current_limit) {
3269414c70cbSLiam Girdwood 		ret = -EINVAL;
3270414c70cbSLiam Girdwood 		goto out;
3271414c70cbSLiam Girdwood 	}
3272414c70cbSLiam Girdwood 
3273414c70cbSLiam Girdwood 	/* constraints check */
3274414c70cbSLiam Girdwood 	ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3275414c70cbSLiam Girdwood 	if (ret < 0)
3276414c70cbSLiam Girdwood 		goto out;
3277414c70cbSLiam Girdwood 
3278414c70cbSLiam Girdwood 	ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3279414c70cbSLiam Girdwood out:
3280414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
3281414c70cbSLiam Girdwood 	return ret;
3282414c70cbSLiam Girdwood }
3283414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3284414c70cbSLiam Girdwood 
3285414c70cbSLiam Girdwood static int _regulator_get_current_limit(struct regulator_dev *rdev)
3286414c70cbSLiam Girdwood {
3287414c70cbSLiam Girdwood 	int ret;
3288414c70cbSLiam Girdwood 
3289414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
3290414c70cbSLiam Girdwood 
3291414c70cbSLiam Girdwood 	/* sanity check */
3292414c70cbSLiam Girdwood 	if (!rdev->desc->ops->get_current_limit) {
3293414c70cbSLiam Girdwood 		ret = -EINVAL;
3294414c70cbSLiam Girdwood 		goto out;
3295414c70cbSLiam Girdwood 	}
3296414c70cbSLiam Girdwood 
3297414c70cbSLiam Girdwood 	ret = rdev->desc->ops->get_current_limit(rdev);
3298414c70cbSLiam Girdwood out:
3299414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
3300414c70cbSLiam Girdwood 	return ret;
3301414c70cbSLiam Girdwood }
3302414c70cbSLiam Girdwood 
3303414c70cbSLiam Girdwood /**
3304414c70cbSLiam Girdwood  * regulator_get_current_limit - get regulator output current
3305414c70cbSLiam Girdwood  * @regulator: regulator source
3306414c70cbSLiam Girdwood  *
3307414c70cbSLiam Girdwood  * This returns the current supplied by the specified current sink in uA.
3308414c70cbSLiam Girdwood  *
3309414c70cbSLiam Girdwood  * NOTE: If the regulator is disabled it will return the current value. This
3310414c70cbSLiam Girdwood  * function should not be used to determine regulator state.
3311414c70cbSLiam Girdwood  */
3312414c70cbSLiam Girdwood int regulator_get_current_limit(struct regulator *regulator)
3313414c70cbSLiam Girdwood {
3314414c70cbSLiam Girdwood 	return _regulator_get_current_limit(regulator->rdev);
3315414c70cbSLiam Girdwood }
3316414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3317414c70cbSLiam Girdwood 
3318414c70cbSLiam Girdwood /**
3319414c70cbSLiam Girdwood  * regulator_set_mode - set regulator operating mode
3320414c70cbSLiam Girdwood  * @regulator: regulator source
3321414c70cbSLiam Girdwood  * @mode: operating mode - one of the REGULATOR_MODE constants
3322414c70cbSLiam Girdwood  *
3323414c70cbSLiam Girdwood  * Set regulator operating mode to increase regulator efficiency or improve
3324414c70cbSLiam Girdwood  * regulation performance.
3325414c70cbSLiam Girdwood  *
3326414c70cbSLiam Girdwood  * NOTE: Regulator system constraints must be set for this regulator before
3327414c70cbSLiam Girdwood  * calling this function otherwise this call will fail.
3328414c70cbSLiam Girdwood  */
3329414c70cbSLiam Girdwood int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3330414c70cbSLiam Girdwood {
3331414c70cbSLiam Girdwood 	struct regulator_dev *rdev = regulator->rdev;
3332414c70cbSLiam Girdwood 	int ret;
3333500b4ac9SSundar R Iyer 	int regulator_curr_mode;
3334414c70cbSLiam Girdwood 
3335414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
3336414c70cbSLiam Girdwood 
3337414c70cbSLiam Girdwood 	/* sanity check */
3338414c70cbSLiam Girdwood 	if (!rdev->desc->ops->set_mode) {
3339414c70cbSLiam Girdwood 		ret = -EINVAL;
3340414c70cbSLiam Girdwood 		goto out;
3341414c70cbSLiam Girdwood 	}
3342414c70cbSLiam Girdwood 
3343500b4ac9SSundar R Iyer 	/* return if the same mode is requested */
3344500b4ac9SSundar R Iyer 	if (rdev->desc->ops->get_mode) {
3345500b4ac9SSundar R Iyer 		regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3346500b4ac9SSundar R Iyer 		if (regulator_curr_mode == mode) {
3347500b4ac9SSundar R Iyer 			ret = 0;
3348500b4ac9SSundar R Iyer 			goto out;
3349500b4ac9SSundar R Iyer 		}
3350500b4ac9SSundar R Iyer 	}
3351500b4ac9SSundar R Iyer 
3352414c70cbSLiam Girdwood 	/* constraints check */
335322c51b47SAxel Lin 	ret = regulator_mode_constrain(rdev, &mode);
3354414c70cbSLiam Girdwood 	if (ret < 0)
3355414c70cbSLiam Girdwood 		goto out;
3356414c70cbSLiam Girdwood 
3357414c70cbSLiam Girdwood 	ret = rdev->desc->ops->set_mode(rdev, mode);
3358414c70cbSLiam Girdwood out:
3359414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
3360414c70cbSLiam Girdwood 	return ret;
3361414c70cbSLiam Girdwood }
3362414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_set_mode);
3363414c70cbSLiam Girdwood 
3364414c70cbSLiam Girdwood static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3365414c70cbSLiam Girdwood {
3366414c70cbSLiam Girdwood 	int ret;
3367414c70cbSLiam Girdwood 
3368414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
3369414c70cbSLiam Girdwood 
3370414c70cbSLiam Girdwood 	/* sanity check */
3371414c70cbSLiam Girdwood 	if (!rdev->desc->ops->get_mode) {
3372414c70cbSLiam Girdwood 		ret = -EINVAL;
3373414c70cbSLiam Girdwood 		goto out;
3374414c70cbSLiam Girdwood 	}
3375414c70cbSLiam Girdwood 
3376414c70cbSLiam Girdwood 	ret = rdev->desc->ops->get_mode(rdev);
3377414c70cbSLiam Girdwood out:
3378414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
3379414c70cbSLiam Girdwood 	return ret;
3380414c70cbSLiam Girdwood }
3381414c70cbSLiam Girdwood 
3382414c70cbSLiam Girdwood /**
3383414c70cbSLiam Girdwood  * regulator_get_mode - get regulator operating mode
3384414c70cbSLiam Girdwood  * @regulator: regulator source
3385414c70cbSLiam Girdwood  *
3386414c70cbSLiam Girdwood  * Get the current regulator operating mode.
3387414c70cbSLiam Girdwood  */
3388414c70cbSLiam Girdwood unsigned int regulator_get_mode(struct regulator *regulator)
3389414c70cbSLiam Girdwood {
3390414c70cbSLiam Girdwood 	return _regulator_get_mode(regulator->rdev);
3391414c70cbSLiam Girdwood }
3392414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_mode);
3393414c70cbSLiam Girdwood 
33941b5b4221SAxel Haslam static int _regulator_get_error_flags(struct regulator_dev *rdev,
33951b5b4221SAxel Haslam 					unsigned int *flags)
33961b5b4221SAxel Haslam {
33971b5b4221SAxel Haslam 	int ret;
33981b5b4221SAxel Haslam 
33991b5b4221SAxel Haslam 	mutex_lock(&rdev->mutex);
34001b5b4221SAxel Haslam 
34011b5b4221SAxel Haslam 	/* sanity check */
34021b5b4221SAxel Haslam 	if (!rdev->desc->ops->get_error_flags) {
34031b5b4221SAxel Haslam 		ret = -EINVAL;
34041b5b4221SAxel Haslam 		goto out;
34051b5b4221SAxel Haslam 	}
34061b5b4221SAxel Haslam 
34071b5b4221SAxel Haslam 	ret = rdev->desc->ops->get_error_flags(rdev, flags);
34081b5b4221SAxel Haslam out:
34091b5b4221SAxel Haslam 	mutex_unlock(&rdev->mutex);
34101b5b4221SAxel Haslam 	return ret;
34111b5b4221SAxel Haslam }
34121b5b4221SAxel Haslam 
34131b5b4221SAxel Haslam /**
34141b5b4221SAxel Haslam  * regulator_get_error_flags - get regulator error information
34151b5b4221SAxel Haslam  * @regulator: regulator source
34161b5b4221SAxel Haslam  * @flags: pointer to store error flags
34171b5b4221SAxel Haslam  *
34181b5b4221SAxel Haslam  * Get the current regulator error information.
34191b5b4221SAxel Haslam  */
34201b5b4221SAxel Haslam int regulator_get_error_flags(struct regulator *regulator,
34211b5b4221SAxel Haslam 				unsigned int *flags)
34221b5b4221SAxel Haslam {
34231b5b4221SAxel Haslam 	return _regulator_get_error_flags(regulator->rdev, flags);
34241b5b4221SAxel Haslam }
34251b5b4221SAxel Haslam EXPORT_SYMBOL_GPL(regulator_get_error_flags);
34261b5b4221SAxel Haslam 
3427414c70cbSLiam Girdwood /**
3428e39ce48fSBjorn Andersson  * regulator_set_load - set regulator load
3429414c70cbSLiam Girdwood  * @regulator: regulator source
3430414c70cbSLiam Girdwood  * @uA_load: load current
3431414c70cbSLiam Girdwood  *
3432414c70cbSLiam Girdwood  * Notifies the regulator core of a new device load. This is then used by
3433414c70cbSLiam Girdwood  * DRMS (if enabled by constraints) to set the most efficient regulator
3434414c70cbSLiam Girdwood  * operating mode for the new regulator loading.
3435414c70cbSLiam Girdwood  *
3436414c70cbSLiam Girdwood  * Consumer devices notify their supply regulator of the maximum power
3437414c70cbSLiam Girdwood  * they will require (can be taken from device datasheet in the power
3438414c70cbSLiam Girdwood  * consumption tables) when they change operational status and hence power
3439414c70cbSLiam Girdwood  * state. Examples of operational state changes that can affect power
3440414c70cbSLiam Girdwood  * consumption are :-
3441414c70cbSLiam Girdwood  *
3442414c70cbSLiam Girdwood  *    o Device is opened / closed.
3443414c70cbSLiam Girdwood  *    o Device I/O is about to begin or has just finished.
3444414c70cbSLiam Girdwood  *    o Device is idling in between work.
3445414c70cbSLiam Girdwood  *
3446414c70cbSLiam Girdwood  * This information is also exported via sysfs to userspace.
3447414c70cbSLiam Girdwood  *
3448414c70cbSLiam Girdwood  * DRMS will sum the total requested load on the regulator and change
3449414c70cbSLiam Girdwood  * to the most efficient operating mode if platform constraints allow.
3450414c70cbSLiam Girdwood  *
3451e39ce48fSBjorn Andersson  * On error a negative errno is returned.
3452414c70cbSLiam Girdwood  */
3453e39ce48fSBjorn Andersson int regulator_set_load(struct regulator *regulator, int uA_load)
3454414c70cbSLiam Girdwood {
3455414c70cbSLiam Girdwood 	struct regulator_dev *rdev = regulator->rdev;
34568460ef38SBjorn Andersson 	int ret;
3457d92d95b6SStephen Boyd 
3458414c70cbSLiam Girdwood 	mutex_lock(&rdev->mutex);
3459414c70cbSLiam Girdwood 	regulator->uA_load = uA_load;
34608460ef38SBjorn Andersson 	ret = drms_uA_update(rdev);
3461414c70cbSLiam Girdwood 	mutex_unlock(&rdev->mutex);
34628460ef38SBjorn Andersson 
3463414c70cbSLiam Girdwood 	return ret;
3464414c70cbSLiam Girdwood }
3465e39ce48fSBjorn Andersson EXPORT_SYMBOL_GPL(regulator_set_load);
3466414c70cbSLiam Girdwood 
3467414c70cbSLiam Girdwood /**
3468f59c8f9fSMark Brown  * regulator_allow_bypass - allow the regulator to go into bypass mode
3469f59c8f9fSMark Brown  *
3470f59c8f9fSMark Brown  * @regulator: Regulator to configure
34719345dfb8SNishanth Menon  * @enable: enable or disable bypass mode
3472f59c8f9fSMark Brown  *
3473f59c8f9fSMark Brown  * Allow the regulator to go into bypass mode if all other consumers
3474f59c8f9fSMark Brown  * for the regulator also enable bypass mode and the machine
3475f59c8f9fSMark Brown  * constraints allow this.  Bypass mode means that the regulator is
3476f59c8f9fSMark Brown  * simply passing the input directly to the output with no regulation.
3477f59c8f9fSMark Brown  */
3478f59c8f9fSMark Brown int regulator_allow_bypass(struct regulator *regulator, bool enable)
3479f59c8f9fSMark Brown {
3480f59c8f9fSMark Brown 	struct regulator_dev *rdev = regulator->rdev;
3481f59c8f9fSMark Brown 	int ret = 0;
3482f59c8f9fSMark Brown 
3483f59c8f9fSMark Brown 	if (!rdev->desc->ops->set_bypass)
3484f59c8f9fSMark Brown 		return 0;
3485f59c8f9fSMark Brown 
34868a34e979SWEN Pingbo 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
3487f59c8f9fSMark Brown 		return 0;
3488f59c8f9fSMark Brown 
3489f59c8f9fSMark Brown 	mutex_lock(&rdev->mutex);
3490f59c8f9fSMark Brown 
3491f59c8f9fSMark Brown 	if (enable && !regulator->bypass) {
3492f59c8f9fSMark Brown 		rdev->bypass_count++;
3493f59c8f9fSMark Brown 
3494f59c8f9fSMark Brown 		if (rdev->bypass_count == rdev->open_count) {
3495f59c8f9fSMark Brown 			ret = rdev->desc->ops->set_bypass(rdev, enable);
3496f59c8f9fSMark Brown 			if (ret != 0)
3497f59c8f9fSMark Brown 				rdev->bypass_count--;
3498f59c8f9fSMark Brown 		}
3499f59c8f9fSMark Brown 
3500f59c8f9fSMark Brown 	} else if (!enable && regulator->bypass) {
3501f59c8f9fSMark Brown 		rdev->bypass_count--;
3502f59c8f9fSMark Brown 
3503f59c8f9fSMark Brown 		if (rdev->bypass_count != rdev->open_count) {
3504f59c8f9fSMark Brown 			ret = rdev->desc->ops->set_bypass(rdev, enable);
3505f59c8f9fSMark Brown 			if (ret != 0)
3506f59c8f9fSMark Brown 				rdev->bypass_count++;
3507f59c8f9fSMark Brown 		}
3508f59c8f9fSMark Brown 	}
3509f59c8f9fSMark Brown 
3510f59c8f9fSMark Brown 	if (ret == 0)
3511f59c8f9fSMark Brown 		regulator->bypass = enable;
3512f59c8f9fSMark Brown 
3513f59c8f9fSMark Brown 	mutex_unlock(&rdev->mutex);
3514f59c8f9fSMark Brown 
3515f59c8f9fSMark Brown 	return ret;
3516f59c8f9fSMark Brown }
3517f59c8f9fSMark Brown EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3518f59c8f9fSMark Brown 
3519f59c8f9fSMark Brown /**
3520414c70cbSLiam Girdwood  * regulator_register_notifier - register regulator event notifier
3521414c70cbSLiam Girdwood  * @regulator: regulator source
352269279fb9SMark Brown  * @nb: notifier block
3523414c70cbSLiam Girdwood  *
3524414c70cbSLiam Girdwood  * Register notifier block to receive regulator events.
3525414c70cbSLiam Girdwood  */
3526414c70cbSLiam Girdwood int regulator_register_notifier(struct regulator *regulator,
3527414c70cbSLiam Girdwood 			      struct notifier_block *nb)
3528414c70cbSLiam Girdwood {
3529414c70cbSLiam Girdwood 	return blocking_notifier_chain_register(&regulator->rdev->notifier,
3530414c70cbSLiam Girdwood 						nb);
3531414c70cbSLiam Girdwood }
3532414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_register_notifier);
3533414c70cbSLiam Girdwood 
3534414c70cbSLiam Girdwood /**
3535414c70cbSLiam Girdwood  * regulator_unregister_notifier - unregister regulator event notifier
3536414c70cbSLiam Girdwood  * @regulator: regulator source
353769279fb9SMark Brown  * @nb: notifier block
3538414c70cbSLiam Girdwood  *
3539414c70cbSLiam Girdwood  * Unregister regulator event notifier block.
3540414c70cbSLiam Girdwood  */
3541414c70cbSLiam Girdwood int regulator_unregister_notifier(struct regulator *regulator,
3542414c70cbSLiam Girdwood 				struct notifier_block *nb)
3543414c70cbSLiam Girdwood {
3544414c70cbSLiam Girdwood 	return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3545414c70cbSLiam Girdwood 						  nb);
3546414c70cbSLiam Girdwood }
3547414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3548414c70cbSLiam Girdwood 
3549b136fb44SJonathan Cameron /* notify regulator consumers and downstream regulator consumers.
3550b136fb44SJonathan Cameron  * Note mutex must be held by caller.
3551b136fb44SJonathan Cameron  */
35527179569aSHeiko Stübner static int _notifier_call_chain(struct regulator_dev *rdev,
3553414c70cbSLiam Girdwood 				  unsigned long event, void *data)
3554414c70cbSLiam Girdwood {
3555414c70cbSLiam Girdwood 	/* call rdev chain first */
35567179569aSHeiko Stübner 	return blocking_notifier_call_chain(&rdev->notifier, event, data);
3557414c70cbSLiam Girdwood }
3558414c70cbSLiam Girdwood 
3559414c70cbSLiam Girdwood /**
3560414c70cbSLiam Girdwood  * regulator_bulk_get - get multiple regulator consumers
3561414c70cbSLiam Girdwood  *
3562414c70cbSLiam Girdwood  * @dev:           Device to supply
3563414c70cbSLiam Girdwood  * @num_consumers: Number of consumers to register
3564414c70cbSLiam Girdwood  * @consumers:     Configuration of consumers; clients are stored here.
3565414c70cbSLiam Girdwood  *
3566414c70cbSLiam Girdwood  * @return 0 on success, an errno on failure.
3567414c70cbSLiam Girdwood  *
3568414c70cbSLiam Girdwood  * This helper function allows drivers to get several regulator
3569414c70cbSLiam Girdwood  * consumers in one operation.  If any of the regulators cannot be
3570414c70cbSLiam Girdwood  * acquired then any regulators that were allocated will be freed
3571414c70cbSLiam Girdwood  * before returning to the caller.
3572414c70cbSLiam Girdwood  */
3573414c70cbSLiam Girdwood int regulator_bulk_get(struct device *dev, int num_consumers,
3574414c70cbSLiam Girdwood 		       struct regulator_bulk_data *consumers)
3575414c70cbSLiam Girdwood {
3576414c70cbSLiam Girdwood 	int i;
3577414c70cbSLiam Girdwood 	int ret;
3578414c70cbSLiam Girdwood 
3579414c70cbSLiam Girdwood 	for (i = 0; i < num_consumers; i++)
3580414c70cbSLiam Girdwood 		consumers[i].consumer = NULL;
3581414c70cbSLiam Girdwood 
3582414c70cbSLiam Girdwood 	for (i = 0; i < num_consumers; i++) {
3583565f9b07SBjorn Andersson 		consumers[i].consumer = regulator_get(dev,
3584565f9b07SBjorn Andersson 						      consumers[i].supply);
3585414c70cbSLiam Girdwood 		if (IS_ERR(consumers[i].consumer)) {
3586414c70cbSLiam Girdwood 			ret = PTR_ERR(consumers[i].consumer);
35875b307627SMark Brown 			dev_err(dev, "Failed to get supply '%s': %d\n",
35885b307627SMark Brown 				consumers[i].supply, ret);
3589414c70cbSLiam Girdwood 			consumers[i].consumer = NULL;
3590414c70cbSLiam Girdwood 			goto err;
3591414c70cbSLiam Girdwood 		}
3592414c70cbSLiam Girdwood 	}
3593414c70cbSLiam Girdwood 
3594414c70cbSLiam Girdwood 	return 0;
3595414c70cbSLiam Girdwood 
3596414c70cbSLiam Girdwood err:
3597b29c7690SAxel Lin 	while (--i >= 0)
3598414c70cbSLiam Girdwood 		regulator_put(consumers[i].consumer);
3599414c70cbSLiam Girdwood 
3600414c70cbSLiam Girdwood 	return ret;
3601414c70cbSLiam Girdwood }
3602414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_bulk_get);
3603414c70cbSLiam Girdwood 
3604f21e0e81SMark Brown static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3605f21e0e81SMark Brown {
3606f21e0e81SMark Brown 	struct regulator_bulk_data *bulk = data;
3607f21e0e81SMark Brown 
3608f21e0e81SMark Brown 	bulk->ret = regulator_enable(bulk->consumer);
3609f21e0e81SMark Brown }
3610f21e0e81SMark Brown 
3611414c70cbSLiam Girdwood /**
3612414c70cbSLiam Girdwood  * regulator_bulk_enable - enable multiple regulator consumers
3613414c70cbSLiam Girdwood  *
3614414c70cbSLiam Girdwood  * @num_consumers: Number of consumers
3615414c70cbSLiam Girdwood  * @consumers:     Consumer data; clients are stored here.
3616414c70cbSLiam Girdwood  * @return         0 on success, an errno on failure
3617414c70cbSLiam Girdwood  *
3618414c70cbSLiam Girdwood  * This convenience API allows consumers to enable multiple regulator
3619414c70cbSLiam Girdwood  * clients in a single API call.  If any consumers cannot be enabled
3620414c70cbSLiam Girdwood  * then any others that were enabled will be disabled again prior to
3621414c70cbSLiam Girdwood  * return.
3622414c70cbSLiam Girdwood  */
3623414c70cbSLiam Girdwood int regulator_bulk_enable(int num_consumers,
3624414c70cbSLiam Girdwood 			  struct regulator_bulk_data *consumers)
3625414c70cbSLiam Girdwood {
36262955b47dSDan Williams 	ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3627414c70cbSLiam Girdwood 	int i;
3628f21e0e81SMark Brown 	int ret = 0;
3629414c70cbSLiam Girdwood 
36306492bc1bSMark Brown 	for (i = 0; i < num_consumers; i++) {
36316492bc1bSMark Brown 		if (consumers[i].consumer->always_on)
36326492bc1bSMark Brown 			consumers[i].ret = 0;
36336492bc1bSMark Brown 		else
3634f21e0e81SMark Brown 			async_schedule_domain(regulator_bulk_enable_async,
3635f21e0e81SMark Brown 					      &consumers[i], &async_domain);
36366492bc1bSMark Brown 	}
3637f21e0e81SMark Brown 
3638f21e0e81SMark Brown 	async_synchronize_full_domain(&async_domain);
3639f21e0e81SMark Brown 
3640f21e0e81SMark Brown 	/* If any consumer failed we need to unwind any that succeeded */
3641414c70cbSLiam Girdwood 	for (i = 0; i < num_consumers; i++) {
3642f21e0e81SMark Brown 		if (consumers[i].ret != 0) {
3643f21e0e81SMark Brown 			ret = consumers[i].ret;
3644414c70cbSLiam Girdwood 			goto err;
3645414c70cbSLiam Girdwood 		}
3646f21e0e81SMark Brown 	}
3647414c70cbSLiam Girdwood 
3648414c70cbSLiam Girdwood 	return 0;
3649414c70cbSLiam Girdwood 
3650414c70cbSLiam Girdwood err:
3651fbe31057SAndrzej Hajda 	for (i = 0; i < num_consumers; i++) {
3652fbe31057SAndrzej Hajda 		if (consumers[i].ret < 0)
3653fbe31057SAndrzej Hajda 			pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3654fbe31057SAndrzej Hajda 			       consumers[i].ret);
3655fbe31057SAndrzej Hajda 		else
3656414c70cbSLiam Girdwood 			regulator_disable(consumers[i].consumer);
3657fbe31057SAndrzej Hajda 	}
3658414c70cbSLiam Girdwood 
3659414c70cbSLiam Girdwood 	return ret;
3660414c70cbSLiam Girdwood }
3661414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3662414c70cbSLiam Girdwood 
3663414c70cbSLiam Girdwood /**
3664414c70cbSLiam Girdwood  * regulator_bulk_disable - disable multiple regulator consumers
3665414c70cbSLiam Girdwood  *
3666414c70cbSLiam Girdwood  * @num_consumers: Number of consumers
3667414c70cbSLiam Girdwood  * @consumers:     Consumer data; clients are stored here.
3668414c70cbSLiam Girdwood  * @return         0 on success, an errno on failure
3669414c70cbSLiam Girdwood  *
3670414c70cbSLiam Girdwood  * This convenience API allows consumers to disable multiple regulator
367149e22632SSylwester Nawrocki  * clients in a single API call.  If any consumers cannot be disabled
367249e22632SSylwester Nawrocki  * then any others that were disabled will be enabled again prior to
3673414c70cbSLiam Girdwood  * return.
3674414c70cbSLiam Girdwood  */
3675414c70cbSLiam Girdwood int regulator_bulk_disable(int num_consumers,
3676414c70cbSLiam Girdwood 			   struct regulator_bulk_data *consumers)
3677414c70cbSLiam Girdwood {
3678414c70cbSLiam Girdwood 	int i;
367901e86f49SMark Brown 	int ret, r;
3680414c70cbSLiam Girdwood 
368149e22632SSylwester Nawrocki 	for (i = num_consumers - 1; i >= 0; --i) {
3682414c70cbSLiam Girdwood 		ret = regulator_disable(consumers[i].consumer);
3683414c70cbSLiam Girdwood 		if (ret != 0)
3684414c70cbSLiam Girdwood 			goto err;
3685414c70cbSLiam Girdwood 	}
3686414c70cbSLiam Girdwood 
3687414c70cbSLiam Girdwood 	return 0;
3688414c70cbSLiam Girdwood 
3689414c70cbSLiam Girdwood err:
36905da84fd9SJoe Perches 	pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
369101e86f49SMark Brown 	for (++i; i < num_consumers; ++i) {
369201e86f49SMark Brown 		r = regulator_enable(consumers[i].consumer);
369301e86f49SMark Brown 		if (r != 0)
3694d1642ea7SDmitry Torokhov 			pr_err("Failed to re-enable %s: %d\n",
369501e86f49SMark Brown 			       consumers[i].supply, r);
369601e86f49SMark Brown 	}
3697414c70cbSLiam Girdwood 
3698414c70cbSLiam Girdwood 	return ret;
3699414c70cbSLiam Girdwood }
3700414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3701414c70cbSLiam Girdwood 
3702414c70cbSLiam Girdwood /**
3703e1de2f42SDonggeun Kim  * regulator_bulk_force_disable - force disable multiple regulator consumers
3704e1de2f42SDonggeun Kim  *
3705e1de2f42SDonggeun Kim  * @num_consumers: Number of consumers
3706e1de2f42SDonggeun Kim  * @consumers:     Consumer data; clients are stored here.
3707e1de2f42SDonggeun Kim  * @return         0 on success, an errno on failure
3708e1de2f42SDonggeun Kim  *
3709e1de2f42SDonggeun Kim  * This convenience API allows consumers to forcibly disable multiple regulator
3710e1de2f42SDonggeun Kim  * clients in a single API call.
3711e1de2f42SDonggeun Kim  * NOTE: This should be used for situations when device damage will
3712e1de2f42SDonggeun Kim  * likely occur if the regulators are not disabled (e.g. over temp).
3713e1de2f42SDonggeun Kim  * Although regulator_force_disable function call for some consumers can
3714e1de2f42SDonggeun Kim  * return error numbers, the function is called for all consumers.
3715e1de2f42SDonggeun Kim  */
3716e1de2f42SDonggeun Kim int regulator_bulk_force_disable(int num_consumers,
3717e1de2f42SDonggeun Kim 			   struct regulator_bulk_data *consumers)
3718e1de2f42SDonggeun Kim {
3719e1de2f42SDonggeun Kim 	int i;
3720b8c77ff6SDmitry Torokhov 	int ret = 0;
3721e1de2f42SDonggeun Kim 
3722b8c77ff6SDmitry Torokhov 	for (i = 0; i < num_consumers; i++) {
3723e1de2f42SDonggeun Kim 		consumers[i].ret =
3724e1de2f42SDonggeun Kim 			    regulator_force_disable(consumers[i].consumer);
3725e1de2f42SDonggeun Kim 
3726b8c77ff6SDmitry Torokhov 		/* Store first error for reporting */
3727b8c77ff6SDmitry Torokhov 		if (consumers[i].ret && !ret)
3728e1de2f42SDonggeun Kim 			ret = consumers[i].ret;
3729e1de2f42SDonggeun Kim 	}
3730e1de2f42SDonggeun Kim 
3731e1de2f42SDonggeun Kim 	return ret;
3732e1de2f42SDonggeun Kim }
3733e1de2f42SDonggeun Kim EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3734e1de2f42SDonggeun Kim 
3735e1de2f42SDonggeun Kim /**
3736414c70cbSLiam Girdwood  * regulator_bulk_free - free multiple regulator consumers
3737414c70cbSLiam Girdwood  *
3738414c70cbSLiam Girdwood  * @num_consumers: Number of consumers
3739414c70cbSLiam Girdwood  * @consumers:     Consumer data; clients are stored here.
3740414c70cbSLiam Girdwood  *
3741414c70cbSLiam Girdwood  * This convenience API allows consumers to free multiple regulator
3742414c70cbSLiam Girdwood  * clients in a single API call.
3743414c70cbSLiam Girdwood  */
3744414c70cbSLiam Girdwood void regulator_bulk_free(int num_consumers,
3745414c70cbSLiam Girdwood 			 struct regulator_bulk_data *consumers)
3746414c70cbSLiam Girdwood {
3747414c70cbSLiam Girdwood 	int i;
3748414c70cbSLiam Girdwood 
3749414c70cbSLiam Girdwood 	for (i = 0; i < num_consumers; i++) {
3750414c70cbSLiam Girdwood 		regulator_put(consumers[i].consumer);
3751414c70cbSLiam Girdwood 		consumers[i].consumer = NULL;
3752414c70cbSLiam Girdwood 	}
3753414c70cbSLiam Girdwood }
3754414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_bulk_free);
3755414c70cbSLiam Girdwood 
3756414c70cbSLiam Girdwood /**
3757414c70cbSLiam Girdwood  * regulator_notifier_call_chain - call regulator event notifier
375869279fb9SMark Brown  * @rdev: regulator source
3759414c70cbSLiam Girdwood  * @event: notifier block
376069279fb9SMark Brown  * @data: callback-specific data.
3761414c70cbSLiam Girdwood  *
3762414c70cbSLiam Girdwood  * Called by regulator drivers to notify clients a regulator event has
3763414c70cbSLiam Girdwood  * occurred. We also notify regulator clients downstream.
3764b136fb44SJonathan Cameron  * Note lock must be held by caller.
3765414c70cbSLiam Girdwood  */
3766414c70cbSLiam Girdwood int regulator_notifier_call_chain(struct regulator_dev *rdev,
3767414c70cbSLiam Girdwood 				  unsigned long event, void *data)
3768414c70cbSLiam Girdwood {
376970cfef26SKrzysztof Kozlowski 	lockdep_assert_held_once(&rdev->mutex);
377070cfef26SKrzysztof Kozlowski 
3771414c70cbSLiam Girdwood 	_notifier_call_chain(rdev, event, data);
3772414c70cbSLiam Girdwood 	return NOTIFY_DONE;
3773414c70cbSLiam Girdwood 
3774414c70cbSLiam Girdwood }
3775414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3776414c70cbSLiam Girdwood 
3777be721979SMark Brown /**
3778be721979SMark Brown  * regulator_mode_to_status - convert a regulator mode into a status
3779be721979SMark Brown  *
3780be721979SMark Brown  * @mode: Mode to convert
3781be721979SMark Brown  *
3782be721979SMark Brown  * Convert a regulator mode into a status.
3783be721979SMark Brown  */
3784be721979SMark Brown int regulator_mode_to_status(unsigned int mode)
3785be721979SMark Brown {
3786be721979SMark Brown 	switch (mode) {
3787be721979SMark Brown 	case REGULATOR_MODE_FAST:
3788be721979SMark Brown 		return REGULATOR_STATUS_FAST;
3789be721979SMark Brown 	case REGULATOR_MODE_NORMAL:
3790be721979SMark Brown 		return REGULATOR_STATUS_NORMAL;
3791be721979SMark Brown 	case REGULATOR_MODE_IDLE:
3792be721979SMark Brown 		return REGULATOR_STATUS_IDLE;
379303ffcf3dSKrystian Garbaciak 	case REGULATOR_MODE_STANDBY:
3794be721979SMark Brown 		return REGULATOR_STATUS_STANDBY;
3795be721979SMark Brown 	default:
37961beaf762SKrystian Garbaciak 		return REGULATOR_STATUS_UNDEFINED;
3797be721979SMark Brown 	}
3798be721979SMark Brown }
3799be721979SMark Brown EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3800be721979SMark Brown 
380139f802d6STakashi Iwai static struct attribute *regulator_dev_attrs[] = {
380239f802d6STakashi Iwai 	&dev_attr_name.attr,
380339f802d6STakashi Iwai 	&dev_attr_num_users.attr,
380439f802d6STakashi Iwai 	&dev_attr_type.attr,
380539f802d6STakashi Iwai 	&dev_attr_microvolts.attr,
380639f802d6STakashi Iwai 	&dev_attr_microamps.attr,
380739f802d6STakashi Iwai 	&dev_attr_opmode.attr,
380839f802d6STakashi Iwai 	&dev_attr_state.attr,
380939f802d6STakashi Iwai 	&dev_attr_status.attr,
381039f802d6STakashi Iwai 	&dev_attr_bypass.attr,
381139f802d6STakashi Iwai 	&dev_attr_requested_microamps.attr,
381239f802d6STakashi Iwai 	&dev_attr_min_microvolts.attr,
381339f802d6STakashi Iwai 	&dev_attr_max_microvolts.attr,
381439f802d6STakashi Iwai 	&dev_attr_min_microamps.attr,
381539f802d6STakashi Iwai 	&dev_attr_max_microamps.attr,
381639f802d6STakashi Iwai 	&dev_attr_suspend_standby_state.attr,
381739f802d6STakashi Iwai 	&dev_attr_suspend_mem_state.attr,
381839f802d6STakashi Iwai 	&dev_attr_suspend_disk_state.attr,
381939f802d6STakashi Iwai 	&dev_attr_suspend_standby_microvolts.attr,
382039f802d6STakashi Iwai 	&dev_attr_suspend_mem_microvolts.attr,
382139f802d6STakashi Iwai 	&dev_attr_suspend_disk_microvolts.attr,
382239f802d6STakashi Iwai 	&dev_attr_suspend_standby_mode.attr,
382339f802d6STakashi Iwai 	&dev_attr_suspend_mem_mode.attr,
382439f802d6STakashi Iwai 	&dev_attr_suspend_disk_mode.attr,
382539f802d6STakashi Iwai 	NULL
382639f802d6STakashi Iwai };
382739f802d6STakashi Iwai 
38287ad68e2fSDavid Brownell /*
38297ad68e2fSDavid Brownell  * To avoid cluttering sysfs (and memory) with useless state, only
38307ad68e2fSDavid Brownell  * create attributes that can be meaningfully displayed.
38317ad68e2fSDavid Brownell  */
383239f802d6STakashi Iwai static umode_t regulator_attr_is_visible(struct kobject *kobj,
383339f802d6STakashi Iwai 					 struct attribute *attr, int idx)
38347ad68e2fSDavid Brownell {
383539f802d6STakashi Iwai 	struct device *dev = kobj_to_dev(kobj);
383683080a14SGeliang Tang 	struct regulator_dev *rdev = dev_to_rdev(dev);
3837272e2315SGuodong Xu 	const struct regulator_ops *ops = rdev->desc->ops;
383839f802d6STakashi Iwai 	umode_t mode = attr->mode;
383939f802d6STakashi Iwai 
384039f802d6STakashi Iwai 	/* these three are always present */
384139f802d6STakashi Iwai 	if (attr == &dev_attr_name.attr ||
384239f802d6STakashi Iwai 	    attr == &dev_attr_num_users.attr ||
384339f802d6STakashi Iwai 	    attr == &dev_attr_type.attr)
384439f802d6STakashi Iwai 		return mode;
38457ad68e2fSDavid Brownell 
38467ad68e2fSDavid Brownell 	/* some attributes need specific methods to be displayed */
384739f802d6STakashi Iwai 	if (attr == &dev_attr_microvolts.attr) {
38484c78899bSMark Brown 		if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3849f2889e65SMark Brown 		    (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
38505a523605SLaxman Dewangan 		    (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
385139f802d6STakashi Iwai 		    (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
385239f802d6STakashi Iwai 			return mode;
385339f802d6STakashi Iwai 		return 0;
3854f59c8f9fSMark Brown 	}
38557ad68e2fSDavid Brownell 
385639f802d6STakashi Iwai 	if (attr == &dev_attr_microamps.attr)
385739f802d6STakashi Iwai 		return ops->get_current_limit ? mode : 0;
385839f802d6STakashi Iwai 
385939f802d6STakashi Iwai 	if (attr == &dev_attr_opmode.attr)
386039f802d6STakashi Iwai 		return ops->get_mode ? mode : 0;
386139f802d6STakashi Iwai 
386239f802d6STakashi Iwai 	if (attr == &dev_attr_state.attr)
386339f802d6STakashi Iwai 		return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
386439f802d6STakashi Iwai 
386539f802d6STakashi Iwai 	if (attr == &dev_attr_status.attr)
386639f802d6STakashi Iwai 		return ops->get_status ? mode : 0;
386739f802d6STakashi Iwai 
386839f802d6STakashi Iwai 	if (attr == &dev_attr_bypass.attr)
386939f802d6STakashi Iwai 		return ops->get_bypass ? mode : 0;
387039f802d6STakashi Iwai 
38717ad68e2fSDavid Brownell 	/* some attributes are type-specific */
387239f802d6STakashi Iwai 	if (attr == &dev_attr_requested_microamps.attr)
387339f802d6STakashi Iwai 		return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
38747ad68e2fSDavid Brownell 
38757ad68e2fSDavid Brownell 	/* constraints need specific supporting methods */
387639f802d6STakashi Iwai 	if (attr == &dev_attr_min_microvolts.attr ||
387739f802d6STakashi Iwai 	    attr == &dev_attr_max_microvolts.attr)
387839f802d6STakashi Iwai 		return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
387939f802d6STakashi Iwai 
388039f802d6STakashi Iwai 	if (attr == &dev_attr_min_microamps.attr ||
388139f802d6STakashi Iwai 	    attr == &dev_attr_max_microamps.attr)
388239f802d6STakashi Iwai 		return ops->set_current_limit ? mode : 0;
388339f802d6STakashi Iwai 
388439f802d6STakashi Iwai 	if (attr == &dev_attr_suspend_standby_state.attr ||
388539f802d6STakashi Iwai 	    attr == &dev_attr_suspend_mem_state.attr ||
388639f802d6STakashi Iwai 	    attr == &dev_attr_suspend_disk_state.attr)
388739f802d6STakashi Iwai 		return mode;
388839f802d6STakashi Iwai 
388939f802d6STakashi Iwai 	if (attr == &dev_attr_suspend_standby_microvolts.attr ||
389039f802d6STakashi Iwai 	    attr == &dev_attr_suspend_mem_microvolts.attr ||
389139f802d6STakashi Iwai 	    attr == &dev_attr_suspend_disk_microvolts.attr)
389239f802d6STakashi Iwai 		return ops->set_suspend_voltage ? mode : 0;
389339f802d6STakashi Iwai 
389439f802d6STakashi Iwai 	if (attr == &dev_attr_suspend_standby_mode.attr ||
389539f802d6STakashi Iwai 	    attr == &dev_attr_suspend_mem_mode.attr ||
389639f802d6STakashi Iwai 	    attr == &dev_attr_suspend_disk_mode.attr)
389739f802d6STakashi Iwai 		return ops->set_suspend_mode ? mode : 0;
389839f802d6STakashi Iwai 
389939f802d6STakashi Iwai 	return mode;
39007ad68e2fSDavid Brownell }
39017ad68e2fSDavid Brownell 
390239f802d6STakashi Iwai static const struct attribute_group regulator_dev_group = {
390339f802d6STakashi Iwai 	.attrs = regulator_dev_attrs,
390439f802d6STakashi Iwai 	.is_visible = regulator_attr_is_visible,
390539f802d6STakashi Iwai };
39067ad68e2fSDavid Brownell 
390739f802d6STakashi Iwai static const struct attribute_group *regulator_dev_groups[] = {
390839f802d6STakashi Iwai 	&regulator_dev_group,
390939f802d6STakashi Iwai 	NULL
391039f802d6STakashi Iwai };
391139f802d6STakashi Iwai 
391239f802d6STakashi Iwai static void regulator_dev_release(struct device *dev)
391339f802d6STakashi Iwai {
391439f802d6STakashi Iwai 	struct regulator_dev *rdev = dev_get_drvdata(dev);
391529f5f486SMark Brown 
391629f5f486SMark Brown 	kfree(rdev->constraints);
391729f5f486SMark Brown 	of_node_put(rdev->dev.of_node);
391839f802d6STakashi Iwai 	kfree(rdev);
39197ad68e2fSDavid Brownell }
39207ad68e2fSDavid Brownell 
392139f802d6STakashi Iwai static struct class regulator_class = {
392239f802d6STakashi Iwai 	.name = "regulator",
392339f802d6STakashi Iwai 	.dev_release = regulator_dev_release,
392439f802d6STakashi Iwai 	.dev_groups = regulator_dev_groups,
392539f802d6STakashi Iwai };
39267ad68e2fSDavid Brownell 
39271130e5b3SMark Brown static void rdev_init_debugfs(struct regulator_dev *rdev)
39281130e5b3SMark Brown {
3929a9eaa813SGuenter Roeck 	struct device *parent = rdev->dev.parent;
3930a9eaa813SGuenter Roeck 	const char *rname = rdev_get_name(rdev);
3931a9eaa813SGuenter Roeck 	char name[NAME_MAX];
3932a9eaa813SGuenter Roeck 
3933a9eaa813SGuenter Roeck 	/* Avoid duplicate debugfs directory names */
3934a9eaa813SGuenter Roeck 	if (parent && rname == rdev->desc->name) {
3935a9eaa813SGuenter Roeck 		snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3936a9eaa813SGuenter Roeck 			 rname);
3937a9eaa813SGuenter Roeck 		rname = name;
3938a9eaa813SGuenter Roeck 	}
3939a9eaa813SGuenter Roeck 
3940a9eaa813SGuenter Roeck 	rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
394124751434SStephen Boyd 	if (!rdev->debugfs) {
39421130e5b3SMark Brown 		rdev_warn(rdev, "Failed to create debugfs directory\n");
39431130e5b3SMark Brown 		return;
39441130e5b3SMark Brown 	}
39451130e5b3SMark Brown 
39461130e5b3SMark Brown 	debugfs_create_u32("use_count", 0444, rdev->debugfs,
39471130e5b3SMark Brown 			   &rdev->use_count);
39481130e5b3SMark Brown 	debugfs_create_u32("open_count", 0444, rdev->debugfs,
39491130e5b3SMark Brown 			   &rdev->open_count);
3950f59c8f9fSMark Brown 	debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3951f59c8f9fSMark Brown 			   &rdev->bypass_count);
39521130e5b3SMark Brown }
39531130e5b3SMark Brown 
39545e3ca2b3SJavier Martinez Canillas static int regulator_register_resolve_supply(struct device *dev, void *data)
39555e3ca2b3SJavier Martinez Canillas {
39567ddede6aSJon Hunter 	struct regulator_dev *rdev = dev_to_rdev(dev);
39577ddede6aSJon Hunter 
39587ddede6aSJon Hunter 	if (regulator_resolve_supply(rdev))
39597ddede6aSJon Hunter 		rdev_dbg(rdev, "unable to resolve supply\n");
39607ddede6aSJon Hunter 
39617ddede6aSJon Hunter 	return 0;
39625e3ca2b3SJavier Martinez Canillas }
39635e3ca2b3SJavier Martinez Canillas 
3964414c70cbSLiam Girdwood /**
3965414c70cbSLiam Girdwood  * regulator_register - register regulator
396669279fb9SMark Brown  * @regulator_desc: regulator to register
3967f47531b1SKrzysztof Kozlowski  * @cfg: runtime configuration for regulator
3968414c70cbSLiam Girdwood  *
3969414c70cbSLiam Girdwood  * Called by regulator drivers to register a regulator.
39700384618aSAxel Lin  * Returns a valid pointer to struct regulator_dev on success
39710384618aSAxel Lin  * or an ERR_PTR() on error.
3972414c70cbSLiam Girdwood  */
397365f26846SMark Brown struct regulator_dev *
397465f26846SMark Brown regulator_register(const struct regulator_desc *regulator_desc,
39751b3de223SKrzysztof Kozlowski 		   const struct regulator_config *cfg)
3976414c70cbSLiam Girdwood {
39779a8f5e07SMark Brown 	const struct regulation_constraints *constraints = NULL;
3978c172708dSMark Brown 	const struct regulator_init_data *init_data;
39791b3de223SKrzysztof Kozlowski 	struct regulator_config *config = NULL;
398072dca06fSAniroop Mathur 	static atomic_t regulator_no = ATOMIC_INIT(-1);
3981414c70cbSLiam Girdwood 	struct regulator_dev *rdev;
398232c8fad4SMark Brown 	struct device *dev;
3983a5766f11SLiam Girdwood 	int ret, i;
3984414c70cbSLiam Girdwood 
39851b3de223SKrzysztof Kozlowski 	if (regulator_desc == NULL || cfg == NULL)
3986414c70cbSLiam Girdwood 		return ERR_PTR(-EINVAL);
3987414c70cbSLiam Girdwood 
39881b3de223SKrzysztof Kozlowski 	dev = cfg->dev;
3989dcf70112SMark Brown 	WARN_ON(!dev);
399032c8fad4SMark Brown 
3991414c70cbSLiam Girdwood 	if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3992414c70cbSLiam Girdwood 		return ERR_PTR(-EINVAL);
3993414c70cbSLiam Girdwood 
3994cd78dfc6SDiego Liziero 	if (regulator_desc->type != REGULATOR_VOLTAGE &&
3995cd78dfc6SDiego Liziero 	    regulator_desc->type != REGULATOR_CURRENT)
3996414c70cbSLiam Girdwood 		return ERR_PTR(-EINVAL);
3997414c70cbSLiam Girdwood 
3998476c2d83SMark Brown 	/* Only one of each should be implemented */
3999476c2d83SMark Brown 	WARN_ON(regulator_desc->ops->get_voltage &&
4000476c2d83SMark Brown 		regulator_desc->ops->get_voltage_sel);
4001e8eef82bSMark Brown 	WARN_ON(regulator_desc->ops->set_voltage &&
4002e8eef82bSMark Brown 		regulator_desc->ops->set_voltage_sel);
4003476c2d83SMark Brown 
4004476c2d83SMark Brown 	/* If we're using selectors we must implement list_voltage. */
4005476c2d83SMark Brown 	if (regulator_desc->ops->get_voltage_sel &&
4006476c2d83SMark Brown 	    !regulator_desc->ops->list_voltage) {
4007476c2d83SMark Brown 		return ERR_PTR(-EINVAL);
4008476c2d83SMark Brown 	}
4009e8eef82bSMark Brown 	if (regulator_desc->ops->set_voltage_sel &&
4010e8eef82bSMark Brown 	    !regulator_desc->ops->list_voltage) {
4011e8eef82bSMark Brown 		return ERR_PTR(-EINVAL);
4012e8eef82bSMark Brown 	}
4013476c2d83SMark Brown 
4014414c70cbSLiam Girdwood 	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4015414c70cbSLiam Girdwood 	if (rdev == NULL)
4016414c70cbSLiam Girdwood 		return ERR_PTR(-ENOMEM);
4017414c70cbSLiam Girdwood 
40181b3de223SKrzysztof Kozlowski 	/*
40191b3de223SKrzysztof Kozlowski 	 * Duplicate the config so the driver could override it after
40201b3de223SKrzysztof Kozlowski 	 * parsing init data.
40211b3de223SKrzysztof Kozlowski 	 */
40221b3de223SKrzysztof Kozlowski 	config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
40231b3de223SKrzysztof Kozlowski 	if (config == NULL) {
40241b3de223SKrzysztof Kozlowski 		kfree(rdev);
40251b3de223SKrzysztof Kozlowski 		return ERR_PTR(-ENOMEM);
40261b3de223SKrzysztof Kozlowski 	}
40271b3de223SKrzysztof Kozlowski 
4028bfa21a0dSKrzysztof Kozlowski 	init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4029a0c7b164SMark Brown 					       &rdev->dev.of_node);
4030a0c7b164SMark Brown 	if (!init_data) {
4031a0c7b164SMark Brown 		init_data = config->init_data;
4032a0c7b164SMark Brown 		rdev->dev.of_node = of_node_get(config->of_node);
4033a0c7b164SMark Brown 	}
4034a0c7b164SMark Brown 
4035414c70cbSLiam Girdwood 	mutex_init(&rdev->mutex);
4036c172708dSMark Brown 	rdev->reg_data = config->driver_data;
4037414c70cbSLiam Girdwood 	rdev->owner = regulator_desc->owner;
4038414c70cbSLiam Girdwood 	rdev->desc = regulator_desc;
40393a4b0a07SMark Brown 	if (config->regmap)
404065b19ce6SMark Brown 		rdev->regmap = config->regmap;
404152b84dacSAnilKumar Ch 	else if (dev_get_regmap(dev, NULL))
40423a4b0a07SMark Brown 		rdev->regmap = dev_get_regmap(dev, NULL);
404352b84dacSAnilKumar Ch 	else if (dev->parent)
404452b84dacSAnilKumar Ch 		rdev->regmap = dev_get_regmap(dev->parent, NULL);
4045414c70cbSLiam Girdwood 	INIT_LIST_HEAD(&rdev->consumer_list);
4046414c70cbSLiam Girdwood 	INIT_LIST_HEAD(&rdev->list);
4047414c70cbSLiam Girdwood 	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4048da07ecd9SMark Brown 	INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4049414c70cbSLiam Girdwood 
4050a5766f11SLiam Girdwood 	/* preform any regulator specific init */
40519a8f5e07SMark Brown 	if (init_data && init_data->regulator_init) {
4052a5766f11SLiam Girdwood 		ret = init_data->regulator_init(rdev->reg_data);
40534fca9545SDavid Brownell 		if (ret < 0)
40544fca9545SDavid Brownell 			goto clean;
4055a5766f11SLiam Girdwood 	}
4056a5766f11SLiam Girdwood 
4057daad134dSKrzysztof Adamski 	if ((config->ena_gpio || config->ena_gpio_initialized) &&
4058daad134dSKrzysztof Adamski 	    gpio_is_valid(config->ena_gpio)) {
405945389c47SJon Hunter 		mutex_lock(&regulator_list_mutex);
4060daad134dSKrzysztof Adamski 		ret = regulator_ena_gpio_request(rdev, config);
406145389c47SJon Hunter 		mutex_unlock(&regulator_list_mutex);
4062daad134dSKrzysztof Adamski 		if (ret != 0) {
4063daad134dSKrzysztof Adamski 			rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4064daad134dSKrzysztof Adamski 				 config->ena_gpio, ret);
406532165230SKrzysztof Adamski 			goto clean;
4066daad134dSKrzysztof Adamski 		}
4067daad134dSKrzysztof Adamski 	}
4068daad134dSKrzysztof Adamski 
4069a5766f11SLiam Girdwood 	/* register with sysfs */
4070a5766f11SLiam Girdwood 	rdev->dev.class = &regulator_class;
4071a5766f11SLiam Girdwood 	rdev->dev.parent = dev;
407272dca06fSAniroop Mathur 	dev_set_name(&rdev->dev, "regulator.%lu",
407339138818SAniroop Mathur 		    (unsigned long) atomic_inc_return(&regulator_no));
4074a5766f11SLiam Girdwood 
407574f544c1SMike Rapoport 	/* set regulator constraints */
40769a8f5e07SMark Brown 	if (init_data)
40779a8f5e07SMark Brown 		constraints = &init_data->constraints;
40789a8f5e07SMark Brown 
40799a8f5e07SMark Brown 	if (init_data && init_data->supply_regulator)
40806261b06dSBjorn Andersson 		rdev->supply_name = init_data->supply_regulator;
408169511a45SRajendra Nayak 	else if (regulator_desc->supply_name)
40826261b06dSBjorn Andersson 		rdev->supply_name = regulator_desc->supply_name;
408369511a45SRajendra Nayak 
408445389c47SJon Hunter 	/*
408545389c47SJon Hunter 	 * Attempt to resolve the regulator supply, if specified,
408645389c47SJon Hunter 	 * but don't return an error if we fail because we will try
408745389c47SJon Hunter 	 * to resolve it again later as more regulators are added.
408845389c47SJon Hunter 	 */
408945389c47SJon Hunter 	if (regulator_resolve_supply(rdev))
409045389c47SJon Hunter 		rdev_dbg(rdev, "unable to resolve supply\n");
409145389c47SJon Hunter 
409245389c47SJon Hunter 	ret = set_machine_constraints(rdev, constraints);
409345389c47SJon Hunter 	if (ret < 0)
409445389c47SJon Hunter 		goto wash;
409545389c47SJon Hunter 
4096a5766f11SLiam Girdwood 	/* add consumers devices */
40979a8f5e07SMark Brown 	if (init_data) {
409845389c47SJon Hunter 		mutex_lock(&regulator_list_mutex);
4099a5766f11SLiam Girdwood 		for (i = 0; i < init_data->num_consumer_supplies; i++) {
4100a5766f11SLiam Girdwood 			ret = set_consumer_device_supply(rdev,
410140f9244fSMark Brown 				init_data->consumer_supplies[i].dev_name,
4102a5766f11SLiam Girdwood 				init_data->consumer_supplies[i].supply);
410323c2f041SMark Brown 			if (ret < 0) {
410445389c47SJon Hunter 				mutex_unlock(&regulator_list_mutex);
410523c2f041SMark Brown 				dev_err(dev, "Failed to set supply %s\n",
410623c2f041SMark Brown 					init_data->consumer_supplies[i].supply);
4107d4033b54SJani Nikula 				goto unset_supplies;
4108a5766f11SLiam Girdwood 			}
410923c2f041SMark Brown 		}
4110a2151374SJon Hunter 		mutex_unlock(&regulator_list_mutex);
41119a8f5e07SMark Brown 	}
4112a5766f11SLiam Girdwood 
4113fd086045SMatthias Kaehlcke 	if (!rdev->desc->ops->get_voltage &&
4114fd086045SMatthias Kaehlcke 	    !rdev->desc->ops->list_voltage &&
4115fd086045SMatthias Kaehlcke 	    !rdev->desc->fixed_uV)
4116fd086045SMatthias Kaehlcke 		rdev->is_switch = true;
4117fd086045SMatthias Kaehlcke 
4118c438b9d0SJon Hunter 	ret = device_register(&rdev->dev);
4119c438b9d0SJon Hunter 	if (ret != 0) {
4120c438b9d0SJon Hunter 		put_device(&rdev->dev);
4121c438b9d0SJon Hunter 		goto unset_supplies;
4122c438b9d0SJon Hunter 	}
4123c438b9d0SJon Hunter 
4124c438b9d0SJon Hunter 	dev_set_drvdata(&rdev->dev, rdev);
41251130e5b3SMark Brown 	rdev_init_debugfs(rdev);
41265e3ca2b3SJavier Martinez Canillas 
41275e3ca2b3SJavier Martinez Canillas 	/* try to resolve regulators supply since a new one was registered */
41285e3ca2b3SJavier Martinez Canillas 	class_for_each_device(&regulator_class, NULL, NULL,
41295e3ca2b3SJavier Martinez Canillas 			      regulator_register_resolve_supply);
41301b3de223SKrzysztof Kozlowski 	kfree(config);
4131414c70cbSLiam Girdwood 	return rdev;
41324fca9545SDavid Brownell 
4133d4033b54SJani Nikula unset_supplies:
413445389c47SJon Hunter 	mutex_lock(&regulator_list_mutex);
4135d4033b54SJani Nikula 	unset_regulator_supplies(rdev);
413645389c47SJon Hunter 	mutex_unlock(&regulator_list_mutex);
413732165230SKrzysztof Adamski wash:
4138469b640eSBoris Brezillon 	kfree(rdev->constraints);
413945389c47SJon Hunter 	mutex_lock(&regulator_list_mutex);
414032165230SKrzysztof Adamski 	regulator_ena_gpio_free(rdev);
414145389c47SJon Hunter 	mutex_unlock(&regulator_list_mutex);
41424fca9545SDavid Brownell clean:
41434fca9545SDavid Brownell 	kfree(rdev);
4144a2151374SJon Hunter 	kfree(config);
4145a2151374SJon Hunter 	return ERR_PTR(ret);
4146414c70cbSLiam Girdwood }
4147414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_register);
4148414c70cbSLiam Girdwood 
4149414c70cbSLiam Girdwood /**
4150414c70cbSLiam Girdwood  * regulator_unregister - unregister regulator
415169279fb9SMark Brown  * @rdev: regulator to unregister
4152414c70cbSLiam Girdwood  *
4153414c70cbSLiam Girdwood  * Called by regulator drivers to unregister a regulator.
4154414c70cbSLiam Girdwood  */
4155414c70cbSLiam Girdwood void regulator_unregister(struct regulator_dev *rdev)
4156414c70cbSLiam Girdwood {
4157414c70cbSLiam Girdwood 	if (rdev == NULL)
4158414c70cbSLiam Girdwood 		return;
4159414c70cbSLiam Girdwood 
4160891636eaSMark Brown 	if (rdev->supply) {
4161891636eaSMark Brown 		while (rdev->use_count--)
4162891636eaSMark Brown 			regulator_disable(rdev->supply);
4163e032b376SMark Brown 		regulator_put(rdev->supply);
4164891636eaSMark Brown 	}
4165414c70cbSLiam Girdwood 	mutex_lock(&regulator_list_mutex);
41661130e5b3SMark Brown 	debugfs_remove_recursive(rdev->debugfs);
416743829731STejun Heo 	flush_work(&rdev->disable_work.work);
41686bf87d17SMark Brown 	WARN_ON(rdev->open_count);
41690f1d747bSMike Rapoport 	unset_regulator_supplies(rdev);
4170414c70cbSLiam Girdwood 	list_del(&rdev->list);
4171f19b00daSKim, Milo 	regulator_ena_gpio_free(rdev);
41722c0a303aSMark Brown 	mutex_unlock(&regulator_list_mutex);
417358fb5cf5SLothar Waßmann 	device_unregister(&rdev->dev);
4174414c70cbSLiam Girdwood }
4175414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_unregister);
4176414c70cbSLiam Girdwood 
417785f3b431STomeu Vizoso static int _regulator_suspend_prepare(struct device *dev, void *data)
417885f3b431STomeu Vizoso {
417985f3b431STomeu Vizoso 	struct regulator_dev *rdev = dev_to_rdev(dev);
418085f3b431STomeu Vizoso 	const suspend_state_t *state = data;
418185f3b431STomeu Vizoso 	int ret;
418285f3b431STomeu Vizoso 
418385f3b431STomeu Vizoso 	mutex_lock(&rdev->mutex);
418485f3b431STomeu Vizoso 	ret = suspend_prepare(rdev, *state);
418585f3b431STomeu Vizoso 	mutex_unlock(&rdev->mutex);
418685f3b431STomeu Vizoso 
418785f3b431STomeu Vizoso 	return ret;
418885f3b431STomeu Vizoso }
418985f3b431STomeu Vizoso 
4190414c70cbSLiam Girdwood /**
4191cf7bbcdfSMark Brown  * regulator_suspend_prepare - prepare regulators for system wide suspend
4192414c70cbSLiam Girdwood  * @state: system suspend state
4193414c70cbSLiam Girdwood  *
4194414c70cbSLiam Girdwood  * Configure each regulator with it's suspend operating parameters for state.
4195414c70cbSLiam Girdwood  * This will usually be called by machine suspend code prior to supending.
4196414c70cbSLiam Girdwood  */
4197414c70cbSLiam Girdwood int regulator_suspend_prepare(suspend_state_t state)
4198414c70cbSLiam Girdwood {
4199414c70cbSLiam Girdwood 	/* ON is handled by regulator active state */
4200414c70cbSLiam Girdwood 	if (state == PM_SUSPEND_ON)
4201414c70cbSLiam Girdwood 		return -EINVAL;
4202414c70cbSLiam Girdwood 
420385f3b431STomeu Vizoso 	return class_for_each_device(&regulator_class, NULL, &state,
420485f3b431STomeu Vizoso 				     _regulator_suspend_prepare);
4205414c70cbSLiam Girdwood }
4206414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
4207414c70cbSLiam Girdwood 
420885f3b431STomeu Vizoso static int _regulator_suspend_finish(struct device *dev, void *data)
420985f3b431STomeu Vizoso {
421085f3b431STomeu Vizoso 	struct regulator_dev *rdev = dev_to_rdev(dev);
421185f3b431STomeu Vizoso 	int ret;
421285f3b431STomeu Vizoso 
421385f3b431STomeu Vizoso 	mutex_lock(&rdev->mutex);
421485f3b431STomeu Vizoso 	if (rdev->use_count > 0  || rdev->constraints->always_on) {
421585f3b431STomeu Vizoso 		if (!_regulator_is_enabled(rdev)) {
421685f3b431STomeu Vizoso 			ret = _regulator_do_enable(rdev);
421785f3b431STomeu Vizoso 			if (ret)
421885f3b431STomeu Vizoso 				dev_err(dev,
421985f3b431STomeu Vizoso 					"Failed to resume regulator %d\n",
422085f3b431STomeu Vizoso 					ret);
422185f3b431STomeu Vizoso 		}
422285f3b431STomeu Vizoso 	} else {
422385f3b431STomeu Vizoso 		if (!have_full_constraints())
422485f3b431STomeu Vizoso 			goto unlock;
422585f3b431STomeu Vizoso 		if (!_regulator_is_enabled(rdev))
422685f3b431STomeu Vizoso 			goto unlock;
422785f3b431STomeu Vizoso 
422885f3b431STomeu Vizoso 		ret = _regulator_do_disable(rdev);
422985f3b431STomeu Vizoso 		if (ret)
423085f3b431STomeu Vizoso 			dev_err(dev, "Failed to suspend regulator %d\n", ret);
423185f3b431STomeu Vizoso 	}
423285f3b431STomeu Vizoso unlock:
423385f3b431STomeu Vizoso 	mutex_unlock(&rdev->mutex);
423485f3b431STomeu Vizoso 
423585f3b431STomeu Vizoso 	/* Keep processing regulators in spite of any errors */
423685f3b431STomeu Vizoso 	return 0;
423785f3b431STomeu Vizoso }
423885f3b431STomeu Vizoso 
4239414c70cbSLiam Girdwood /**
42407a32b589SMyungJoo Ham  * regulator_suspend_finish - resume regulators from system wide suspend
42417a32b589SMyungJoo Ham  *
42427a32b589SMyungJoo Ham  * Turn on regulators that might be turned off by regulator_suspend_prepare
42437a32b589SMyungJoo Ham  * and that should be turned on according to the regulators properties.
42447a32b589SMyungJoo Ham  */
42457a32b589SMyungJoo Ham int regulator_suspend_finish(void)
42467a32b589SMyungJoo Ham {
424785f3b431STomeu Vizoso 	return class_for_each_device(&regulator_class, NULL, NULL,
424885f3b431STomeu Vizoso 				     _regulator_suspend_finish);
42497a32b589SMyungJoo Ham }
42507a32b589SMyungJoo Ham EXPORT_SYMBOL_GPL(regulator_suspend_finish);
42517a32b589SMyungJoo Ham 
42527a32b589SMyungJoo Ham /**
4253ca725561SMark Brown  * regulator_has_full_constraints - the system has fully specified constraints
4254ca725561SMark Brown  *
4255ca725561SMark Brown  * Calling this function will cause the regulator API to disable all
4256ca725561SMark Brown  * regulators which have a zero use count and don't have an always_on
4257ca725561SMark Brown  * constraint in a late_initcall.
4258ca725561SMark Brown  *
4259ca725561SMark Brown  * The intention is that this will become the default behaviour in a
4260ca725561SMark Brown  * future kernel release so users are encouraged to use this facility
4261ca725561SMark Brown  * now.
4262ca725561SMark Brown  */
4263ca725561SMark Brown void regulator_has_full_constraints(void)
4264ca725561SMark Brown {
4265ca725561SMark Brown 	has_full_constraints = 1;
4266ca725561SMark Brown }
4267ca725561SMark Brown EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4268ca725561SMark Brown 
4269ca725561SMark Brown /**
4270414c70cbSLiam Girdwood  * rdev_get_drvdata - get rdev regulator driver data
427169279fb9SMark Brown  * @rdev: regulator
4272414c70cbSLiam Girdwood  *
4273414c70cbSLiam Girdwood  * Get rdev regulator driver private data. This call can be used in the
4274414c70cbSLiam Girdwood  * regulator driver context.
4275414c70cbSLiam Girdwood  */
4276414c70cbSLiam Girdwood void *rdev_get_drvdata(struct regulator_dev *rdev)
4277414c70cbSLiam Girdwood {
4278414c70cbSLiam Girdwood 	return rdev->reg_data;
4279414c70cbSLiam Girdwood }
4280414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4281414c70cbSLiam Girdwood 
4282414c70cbSLiam Girdwood /**
4283414c70cbSLiam Girdwood  * regulator_get_drvdata - get regulator driver data
4284414c70cbSLiam Girdwood  * @regulator: regulator
4285414c70cbSLiam Girdwood  *
4286414c70cbSLiam Girdwood  * Get regulator driver private data. This call can be used in the consumer
4287414c70cbSLiam Girdwood  * driver context when non API regulator specific functions need to be called.
4288414c70cbSLiam Girdwood  */
4289414c70cbSLiam Girdwood void *regulator_get_drvdata(struct regulator *regulator)
4290414c70cbSLiam Girdwood {
4291414c70cbSLiam Girdwood 	return regulator->rdev->reg_data;
4292414c70cbSLiam Girdwood }
4293414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4294414c70cbSLiam Girdwood 
4295414c70cbSLiam Girdwood /**
4296414c70cbSLiam Girdwood  * regulator_set_drvdata - set regulator driver data
4297414c70cbSLiam Girdwood  * @regulator: regulator
4298414c70cbSLiam Girdwood  * @data: data
4299414c70cbSLiam Girdwood  */
4300414c70cbSLiam Girdwood void regulator_set_drvdata(struct regulator *regulator, void *data)
4301414c70cbSLiam Girdwood {
4302414c70cbSLiam Girdwood 	regulator->rdev->reg_data = data;
4303414c70cbSLiam Girdwood }
4304414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4305414c70cbSLiam Girdwood 
4306414c70cbSLiam Girdwood /**
4307414c70cbSLiam Girdwood  * regulator_get_id - get regulator ID
430869279fb9SMark Brown  * @rdev: regulator
4309414c70cbSLiam Girdwood  */
4310414c70cbSLiam Girdwood int rdev_get_id(struct regulator_dev *rdev)
4311414c70cbSLiam Girdwood {
4312414c70cbSLiam Girdwood 	return rdev->desc->id;
4313414c70cbSLiam Girdwood }
4314414c70cbSLiam Girdwood EXPORT_SYMBOL_GPL(rdev_get_id);
4315414c70cbSLiam Girdwood 
4316a5766f11SLiam Girdwood struct device *rdev_get_dev(struct regulator_dev *rdev)
4317a5766f11SLiam Girdwood {
4318a5766f11SLiam Girdwood 	return &rdev->dev;
4319a5766f11SLiam Girdwood }
4320a5766f11SLiam Girdwood EXPORT_SYMBOL_GPL(rdev_get_dev);
4321a5766f11SLiam Girdwood 
4322a5766f11SLiam Girdwood void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4323a5766f11SLiam Girdwood {
4324a5766f11SLiam Girdwood 	return reg_init_data->driver_data;
4325a5766f11SLiam Girdwood }
4326a5766f11SLiam Girdwood EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4327a5766f11SLiam Girdwood 
4328ba55a974SMark Brown #ifdef CONFIG_DEBUG_FS
4329dbc55955SHaishan Zhou static int supply_map_show(struct seq_file *sf, void *data)
4330ba55a974SMark Brown {
4331ba55a974SMark Brown 	struct regulator_map *map;
4332ba55a974SMark Brown 
4333ba55a974SMark Brown 	list_for_each_entry(map, &regulator_map_list, list) {
4334dbc55955SHaishan Zhou 		seq_printf(sf, "%s -> %s.%s\n",
4335ba55a974SMark Brown 				rdev_get_name(map->regulator), map->dev_name,
4336ba55a974SMark Brown 				map->supply);
4337ba55a974SMark Brown 	}
4338ba55a974SMark Brown 
4339dbc55955SHaishan Zhou 	return 0;
4340dbc55955SHaishan Zhou }
4341ba55a974SMark Brown 
4342dbc55955SHaishan Zhou static int supply_map_open(struct inode *inode, struct file *file)
4343dbc55955SHaishan Zhou {
4344dbc55955SHaishan Zhou 	return single_open(file, supply_map_show, inode->i_private);
4345ba55a974SMark Brown }
434624751434SStephen Boyd #endif
4347ba55a974SMark Brown 
4348ba55a974SMark Brown static const struct file_operations supply_map_fops = {
434924751434SStephen Boyd #ifdef CONFIG_DEBUG_FS
4350dbc55955SHaishan Zhou 	.open = supply_map_open,
4351dbc55955SHaishan Zhou 	.read = seq_read,
4352dbc55955SHaishan Zhou 	.llseek = seq_lseek,
4353dbc55955SHaishan Zhou 	.release = single_release,
4354ba55a974SMark Brown #endif
435524751434SStephen Boyd };
4356ba55a974SMark Brown 
43577c225ec9SHeiko Stübner #ifdef CONFIG_DEBUG_FS
435885f3b431STomeu Vizoso struct summary_data {
435985f3b431STomeu Vizoso 	struct seq_file *s;
436085f3b431STomeu Vizoso 	struct regulator_dev *parent;
436185f3b431STomeu Vizoso 	int level;
436285f3b431STomeu Vizoso };
436385f3b431STomeu Vizoso 
436485f3b431STomeu Vizoso static void regulator_summary_show_subtree(struct seq_file *s,
436585f3b431STomeu Vizoso 					   struct regulator_dev *rdev,
436685f3b431STomeu Vizoso 					   int level);
436785f3b431STomeu Vizoso 
436885f3b431STomeu Vizoso static int regulator_summary_show_children(struct device *dev, void *data)
436985f3b431STomeu Vizoso {
437085f3b431STomeu Vizoso 	struct regulator_dev *rdev = dev_to_rdev(dev);
437185f3b431STomeu Vizoso 	struct summary_data *summary_data = data;
437285f3b431STomeu Vizoso 
437385f3b431STomeu Vizoso 	if (rdev->supply && rdev->supply->rdev == summary_data->parent)
437485f3b431STomeu Vizoso 		regulator_summary_show_subtree(summary_data->s, rdev,
437585f3b431STomeu Vizoso 					       summary_data->level + 1);
437685f3b431STomeu Vizoso 
437785f3b431STomeu Vizoso 	return 0;
437885f3b431STomeu Vizoso }
437985f3b431STomeu Vizoso 
43807c225ec9SHeiko Stübner static void regulator_summary_show_subtree(struct seq_file *s,
43817c225ec9SHeiko Stübner 					   struct regulator_dev *rdev,
43827c225ec9SHeiko Stübner 					   int level)
43837c225ec9SHeiko Stübner {
43847c225ec9SHeiko Stübner 	struct regulation_constraints *c;
43857c225ec9SHeiko Stübner 	struct regulator *consumer;
438685f3b431STomeu Vizoso 	struct summary_data summary_data;
43877c225ec9SHeiko Stübner 
43887c225ec9SHeiko Stübner 	if (!rdev)
43897c225ec9SHeiko Stübner 		return;
43907c225ec9SHeiko Stübner 
43917c225ec9SHeiko Stübner 	seq_printf(s, "%*s%-*s %3d %4d %6d ",
43927c225ec9SHeiko Stübner 		   level * 3 + 1, "",
43937c225ec9SHeiko Stübner 		   30 - level * 3, rdev_get_name(rdev),
43947c225ec9SHeiko Stübner 		   rdev->use_count, rdev->open_count, rdev->bypass_count);
43957c225ec9SHeiko Stübner 
439623296099SHeiko Stübner 	seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
439723296099SHeiko Stübner 	seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
43987c225ec9SHeiko Stübner 
43997c225ec9SHeiko Stübner 	c = rdev->constraints;
44007c225ec9SHeiko Stübner 	if (c) {
44017c225ec9SHeiko Stübner 		switch (rdev->desc->type) {
44027c225ec9SHeiko Stübner 		case REGULATOR_VOLTAGE:
44037c225ec9SHeiko Stübner 			seq_printf(s, "%5dmV %5dmV ",
44047c225ec9SHeiko Stübner 				   c->min_uV / 1000, c->max_uV / 1000);
44057c225ec9SHeiko Stübner 			break;
44067c225ec9SHeiko Stübner 		case REGULATOR_CURRENT:
44077c225ec9SHeiko Stübner 			seq_printf(s, "%5dmA %5dmA ",
44087c225ec9SHeiko Stübner 				   c->min_uA / 1000, c->max_uA / 1000);
44097c225ec9SHeiko Stübner 			break;
44107c225ec9SHeiko Stübner 		}
44117c225ec9SHeiko Stübner 	}
44127c225ec9SHeiko Stübner 
44137c225ec9SHeiko Stübner 	seq_puts(s, "\n");
44147c225ec9SHeiko Stübner 
44157c225ec9SHeiko Stübner 	list_for_each_entry(consumer, &rdev->consumer_list, list) {
4416e42a46b6SLeonard Crestez 		if (consumer->dev && consumer->dev->class == &regulator_class)
44177c225ec9SHeiko Stübner 			continue;
44187c225ec9SHeiko Stübner 
44197c225ec9SHeiko Stübner 		seq_printf(s, "%*s%-*s ",
44207c225ec9SHeiko Stübner 			   (level + 1) * 3 + 1, "",
4421e42a46b6SLeonard Crestez 			   30 - (level + 1) * 3,
4422e42a46b6SLeonard Crestez 			   consumer->dev ? dev_name(consumer->dev) : "deviceless");
44237c225ec9SHeiko Stübner 
44247c225ec9SHeiko Stübner 		switch (rdev->desc->type) {
44257c225ec9SHeiko Stübner 		case REGULATOR_VOLTAGE:
442623296099SHeiko Stübner 			seq_printf(s, "%37dmV %5dmV",
44277c225ec9SHeiko Stübner 				   consumer->min_uV / 1000,
44287c225ec9SHeiko Stübner 				   consumer->max_uV / 1000);
44297c225ec9SHeiko Stübner 			break;
44307c225ec9SHeiko Stübner 		case REGULATOR_CURRENT:
44317c225ec9SHeiko Stübner 			break;
44327c225ec9SHeiko Stübner 		}
44337c225ec9SHeiko Stübner 
44347c225ec9SHeiko Stübner 		seq_puts(s, "\n");
44357c225ec9SHeiko Stübner 	}
44367c225ec9SHeiko Stübner 
443785f3b431STomeu Vizoso 	summary_data.s = s;
443885f3b431STomeu Vizoso 	summary_data.level = level;
443985f3b431STomeu Vizoso 	summary_data.parent = rdev;
44407c225ec9SHeiko Stübner 
444185f3b431STomeu Vizoso 	class_for_each_device(&regulator_class, NULL, &summary_data,
444285f3b431STomeu Vizoso 			      regulator_summary_show_children);
44437c225ec9SHeiko Stübner }
444485f3b431STomeu Vizoso 
444585f3b431STomeu Vizoso static int regulator_summary_show_roots(struct device *dev, void *data)
444685f3b431STomeu Vizoso {
444785f3b431STomeu Vizoso 	struct regulator_dev *rdev = dev_to_rdev(dev);
444885f3b431STomeu Vizoso 	struct seq_file *s = data;
444985f3b431STomeu Vizoso 
445085f3b431STomeu Vizoso 	if (!rdev->supply)
445185f3b431STomeu Vizoso 		regulator_summary_show_subtree(s, rdev, 0);
445285f3b431STomeu Vizoso 
445385f3b431STomeu Vizoso 	return 0;
44547c225ec9SHeiko Stübner }
44557c225ec9SHeiko Stübner 
44567c225ec9SHeiko Stübner static int regulator_summary_show(struct seq_file *s, void *data)
44577c225ec9SHeiko Stübner {
445823296099SHeiko Stübner 	seq_puts(s, " regulator                      use open bypass voltage current     min     max\n");
445923296099SHeiko Stübner 	seq_puts(s, "-------------------------------------------------------------------------------\n");
44607c225ec9SHeiko Stübner 
446185f3b431STomeu Vizoso 	class_for_each_device(&regulator_class, NULL, s,
446285f3b431STomeu Vizoso 			      regulator_summary_show_roots);
44637c225ec9SHeiko Stübner 
44647c225ec9SHeiko Stübner 	return 0;
44657c225ec9SHeiko Stübner }
44667c225ec9SHeiko Stübner 
44677c225ec9SHeiko Stübner static int regulator_summary_open(struct inode *inode, struct file *file)
44687c225ec9SHeiko Stübner {
44697c225ec9SHeiko Stübner 	return single_open(file, regulator_summary_show, inode->i_private);
44707c225ec9SHeiko Stübner }
44717c225ec9SHeiko Stübner #endif
44727c225ec9SHeiko Stübner 
44737c225ec9SHeiko Stübner static const struct file_operations regulator_summary_fops = {
44747c225ec9SHeiko Stübner #ifdef CONFIG_DEBUG_FS
44757c225ec9SHeiko Stübner 	.open		= regulator_summary_open,
44767c225ec9SHeiko Stübner 	.read		= seq_read,
44777c225ec9SHeiko Stübner 	.llseek		= seq_lseek,
44787c225ec9SHeiko Stübner 	.release	= single_release,
44797c225ec9SHeiko Stübner #endif
44807c225ec9SHeiko Stübner };
44817c225ec9SHeiko Stübner 
4482414c70cbSLiam Girdwood static int __init regulator_init(void)
4483414c70cbSLiam Girdwood {
448434abbd68SMark Brown 	int ret;
448534abbd68SMark Brown 
448634abbd68SMark Brown 	ret = class_register(&regulator_class);
448734abbd68SMark Brown 
44881130e5b3SMark Brown 	debugfs_root = debugfs_create_dir("regulator", NULL);
448924751434SStephen Boyd 	if (!debugfs_root)
44901130e5b3SMark Brown 		pr_warn("regulator: Failed to create debugfs directory\n");
4491ba55a974SMark Brown 
4492f4d562c6SMark Brown 	debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4493f4d562c6SMark Brown 			    &supply_map_fops);
44941130e5b3SMark Brown 
44957c225ec9SHeiko Stübner 	debugfs_create_file("regulator_summary", 0444, debugfs_root,
449685f3b431STomeu Vizoso 			    NULL, &regulator_summary_fops);
44977c225ec9SHeiko Stübner 
449834abbd68SMark Brown 	regulator_dummy_init();
449934abbd68SMark Brown 
450034abbd68SMark Brown 	return ret;
4501414c70cbSLiam Girdwood }
4502414c70cbSLiam Girdwood 
4503414c70cbSLiam Girdwood /* init early to allow our consumers to complete system booting */
4504414c70cbSLiam Girdwood core_initcall(regulator_init);
4505ca725561SMark Brown 
4506609ca5f3SMark Brown static int __init regulator_late_cleanup(struct device *dev, void *data)
4507ca725561SMark Brown {
4508609ca5f3SMark Brown 	struct regulator_dev *rdev = dev_to_rdev(dev);
4509609ca5f3SMark Brown 	const struct regulator_ops *ops = rdev->desc->ops;
4510609ca5f3SMark Brown 	struct regulation_constraints *c = rdev->constraints;
4511ca725561SMark Brown 	int enabled, ret;
4512ca725561SMark Brown 
451366fda75fSMarkus Pargmann 	if (c && c->always_on)
4514609ca5f3SMark Brown 		return 0;
4515ca725561SMark Brown 
45168a34e979SWEN Pingbo 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
4517609ca5f3SMark Brown 		return 0;
4518e9535834SMark Brown 
4519ca725561SMark Brown 	mutex_lock(&rdev->mutex);
4520ca725561SMark Brown 
4521ca725561SMark Brown 	if (rdev->use_count)
4522ca725561SMark Brown 		goto unlock;
4523ca725561SMark Brown 
4524ca725561SMark Brown 	/* If we can't read the status assume it's on. */
4525ca725561SMark Brown 	if (ops->is_enabled)
4526ca725561SMark Brown 		enabled = ops->is_enabled(rdev);
4527ca725561SMark Brown 	else
4528ca725561SMark Brown 		enabled = 1;
4529ca725561SMark Brown 
4530ca725561SMark Brown 	if (!enabled)
4531ca725561SMark Brown 		goto unlock;
4532ca725561SMark Brown 
453387b28417SMark Brown 	if (have_full_constraints()) {
4534609ca5f3SMark Brown 		/* We log since this may kill the system if it goes
4535609ca5f3SMark Brown 		 * wrong. */
45365da84fd9SJoe Perches 		rdev_info(rdev, "disabling\n");
453766fda75fSMarkus Pargmann 		ret = _regulator_do_disable(rdev);
45380d25d09dSJingoo Han 		if (ret != 0)
45395da84fd9SJoe Perches 			rdev_err(rdev, "couldn't disable: %d\n", ret);
4540ca725561SMark Brown 	} else {
4541ca725561SMark Brown 		/* The intention is that in future we will
4542ca725561SMark Brown 		 * assume that full constraints are provided
4543ca725561SMark Brown 		 * so warn even if we aren't going to do
4544ca725561SMark Brown 		 * anything here.
4545ca725561SMark Brown 		 */
45465da84fd9SJoe Perches 		rdev_warn(rdev, "incomplete constraints, leaving on\n");
4547ca725561SMark Brown 	}
4548ca725561SMark Brown 
4549ca725561SMark Brown unlock:
4550ca725561SMark Brown 	mutex_unlock(&rdev->mutex);
4551609ca5f3SMark Brown 
4552609ca5f3SMark Brown 	return 0;
4553ca725561SMark Brown }
4554ca725561SMark Brown 
4555609ca5f3SMark Brown static int __init regulator_init_complete(void)
4556609ca5f3SMark Brown {
4557609ca5f3SMark Brown 	/*
4558609ca5f3SMark Brown 	 * Since DT doesn't provide an idiomatic mechanism for
4559609ca5f3SMark Brown 	 * enabling full constraints and since it's much more natural
4560609ca5f3SMark Brown 	 * with DT to provide them just assume that a DT enabled
4561609ca5f3SMark Brown 	 * system has full constraints.
4562609ca5f3SMark Brown 	 */
4563609ca5f3SMark Brown 	if (of_have_populated_dt())
4564609ca5f3SMark Brown 		has_full_constraints = true;
4565609ca5f3SMark Brown 
45663827b64dSJavier Martinez Canillas 	/*
45673827b64dSJavier Martinez Canillas 	 * Regulators may had failed to resolve their input supplies
45683827b64dSJavier Martinez Canillas 	 * when were registered, either because the input supply was
45693827b64dSJavier Martinez Canillas 	 * not registered yet or because its parent device was not
45703827b64dSJavier Martinez Canillas 	 * bound yet. So attempt to resolve the input supplies for
45713827b64dSJavier Martinez Canillas 	 * pending regulators before trying to disable unused ones.
45723827b64dSJavier Martinez Canillas 	 */
45733827b64dSJavier Martinez Canillas 	class_for_each_device(&regulator_class, NULL, NULL,
45743827b64dSJavier Martinez Canillas 			      regulator_register_resolve_supply);
45753827b64dSJavier Martinez Canillas 
4576609ca5f3SMark Brown 	/* If we have a full configuration then disable any regulators
4577609ca5f3SMark Brown 	 * we have permission to change the status for and which are
4578609ca5f3SMark Brown 	 * not in use or always_on.  This is effectively the default
4579609ca5f3SMark Brown 	 * for DT and ACPI as they have full constraints.
4580609ca5f3SMark Brown 	 */
4581609ca5f3SMark Brown 	class_for_each_device(&regulator_class, NULL, NULL,
4582609ca5f3SMark Brown 			      regulator_late_cleanup);
4583ca725561SMark Brown 
4584ca725561SMark Brown 	return 0;
4585ca725561SMark Brown }
4586fd482a3eSSaravana Kannan late_initcall_sync(regulator_init_complete);
4587