diff options
Diffstat (limited to 'drivers/regulator/core.c')
| -rw-r--r-- | drivers/regulator/core.c | 2234 | 
1 files changed, 1737 insertions, 497 deletions
diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index f1d10c974cd..4c1f999041d 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -15,25 +15,48 @@  #include <linux/kernel.h>  #include <linux/init.h> +#include <linux/debugfs.h>  #include <linux/device.h>  #include <linux/slab.h> +#include <linux/async.h>  #include <linux/err.h>  #include <linux/mutex.h>  #include <linux/suspend.h>  #include <linux/delay.h> +#include <linux/gpio.h> +#include <linux/of.h> +#include <linux/regmap.h> +#include <linux/regulator/of_regulator.h>  #include <linux/regulator/consumer.h>  #include <linux/regulator/driver.h>  #include <linux/regulator/machine.h> +#include <linux/module.h> -#include "dummy.h" +#define CREATE_TRACE_POINTS +#include <trace/events/regulator.h> -#define REGULATOR_VERSION "0.5" +#include "dummy.h" +#include "internal.h" + +#define rdev_crit(rdev, fmt, ...)					\ +	pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) +#define rdev_err(rdev, fmt, ...)					\ +	pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) +#define rdev_warn(rdev, fmt, ...)					\ +	pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) +#define rdev_info(rdev, fmt, ...)					\ +	pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) +#define rdev_dbg(rdev, fmt, ...)					\ +	pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)  static DEFINE_MUTEX(regulator_list_mutex);  static LIST_HEAD(regulator_list);  static LIST_HEAD(regulator_map_list); -static int has_full_constraints; -static bool board_wants_dummy_regulator; +static LIST_HEAD(regulator_ena_gpio_list); +static LIST_HEAD(regulator_supply_alias_list); +static bool has_full_constraints; + +static struct dentry *debugfs_root;  /*   * struct regulator_map @@ -48,29 +71,43 @@ struct regulator_map {  };  /* - * struct regulator + * struct regulator_enable_gpio   * - * One for each consumer device. + * Management for shared enable GPIO pin   */ -struct regulator { -	struct device *dev; +struct regulator_enable_gpio {  	struct list_head list; -	int uA_load; -	int min_uV; -	int max_uV; -	char *supply_name; -	struct device_attribute dev_attr; -	struct regulator_dev *rdev; +	int gpio; +	u32 enable_count;	/* a number of enabled shared GPIO */ +	u32 request_count;	/* a number of requested shared GPIO */ +	unsigned int ena_gpio_invert:1; +}; + +/* + * struct regulator_supply_alias + * + * Used to map lookups for a supply onto an alternative device. + */ +struct regulator_supply_alias { +	struct list_head list; +	struct device *src_dev; +	const char *src_supply; +	struct device *alias_dev; +	const char *alias_supply;  };  static int _regulator_is_enabled(struct regulator_dev *rdev); -static int _regulator_disable(struct regulator_dev *rdev, -		struct regulator_dev **supply_rdev_ptr); +static int _regulator_disable(struct regulator_dev *rdev);  static int _regulator_get_voltage(struct regulator_dev *rdev);  static int _regulator_get_current_limit(struct regulator_dev *rdev);  static unsigned int _regulator_get_mode(struct regulator_dev *rdev);  static void _notifier_call_chain(struct regulator_dev *rdev,  				  unsigned long event, void *data); +static int _regulator_do_set_voltage(struct regulator_dev *rdev, +				     int min_uV, int max_uV); +static struct regulator *create_regulator(struct regulator_dev *rdev, +					  struct device *dev, +					  const char *supply_name);  static const char *rdev_get_name(struct regulator_dev *rdev)  { @@ -82,26 +119,47 @@ static const char *rdev_get_name(struct regulator_dev *rdev)  		return "";  } -/* gets the regulator for a given consumer device */ -static struct regulator *get_device_regulator(struct device *dev) +static bool have_full_constraints(void)  { -	struct regulator *regulator = NULL; -	struct regulator_dev *rdev; +	return has_full_constraints || of_have_populated_dt(); +} -	mutex_lock(®ulator_list_mutex); -	list_for_each_entry(rdev, ®ulator_list, list) { -		mutex_lock(&rdev->mutex); -		list_for_each_entry(regulator, &rdev->consumer_list, list) { -			if (regulator->dev == dev) { -				mutex_unlock(&rdev->mutex); -				mutex_unlock(®ulator_list_mutex); -				return regulator; -			} -		} -		mutex_unlock(&rdev->mutex); +/** + * of_get_regulator - get a regulator device node based on supply name + * @dev: Device pointer for the consumer (of regulator) device + * @supply: regulator supply name + * + * Extract the regulator device node corresponding to the supply name. + * returns the device node corresponding to the regulator if found, else + * returns NULL. + */ +static struct device_node *of_get_regulator(struct device *dev, const char *supply) +{ +	struct device_node *regnode = NULL; +	char prop_name[32]; /* 32 is max size of property name */ + +	dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); + +	snprintf(prop_name, 32, "%s-supply", supply); +	regnode = of_parse_phandle(dev->of_node, prop_name, 0); + +	if (!regnode) { +		dev_dbg(dev, "Looking up %s property in node %s failed", +				prop_name, dev->of_node->full_name); +		return NULL;  	} -	mutex_unlock(®ulator_list_mutex); -	return NULL; +	return regnode; +} + +static int _regulator_can_change_status(struct regulator_dev *rdev) +{ +	if (!rdev->constraints) +		return 0; + +	if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS) +		return 1; +	else +		return 0;  }  /* Platform voltage constraint check */ @@ -111,13 +169,11 @@ static int regulator_check_voltage(struct regulator_dev *rdev,  	BUG_ON(*min_uV > *max_uV);  	if (!rdev->constraints) { -		printk(KERN_ERR "%s: no constraints for %s\n", __func__, -		       rdev_get_name(rdev)); +		rdev_err(rdev, "no constraints\n");  		return -ENODEV;  	}  	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { -		printk(KERN_ERR "%s: operation not allowed for %s\n", -		       __func__, rdev_get_name(rdev)); +		rdev_err(rdev, "operation not allowed\n");  		return -EPERM;  	} @@ -126,8 +182,42 @@ static int regulator_check_voltage(struct regulator_dev *rdev,  	if (*min_uV < rdev->constraints->min_uV)  		*min_uV = rdev->constraints->min_uV; -	if (*min_uV > *max_uV) +	if (*min_uV > *max_uV) { +		rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", +			 *min_uV, *max_uV); +		return -EINVAL; +	} + +	return 0; +} + +/* Make sure we select a voltage that suits the needs of all + * regulator consumers + */ +static int regulator_check_consumers(struct regulator_dev *rdev, +				     int *min_uV, int *max_uV) +{ +	struct regulator *regulator; + +	list_for_each_entry(regulator, &rdev->consumer_list, list) { +		/* +		 * Assume consumers that didn't say anything are OK +		 * with anything in the constraint range. +		 */ +		if (!regulator->min_uV && !regulator->max_uV) +			continue; + +		if (*max_uV > regulator->max_uV) +			*max_uV = regulator->max_uV; +		if (*min_uV < regulator->min_uV) +			*min_uV = regulator->min_uV; +	} + +	if (*min_uV > *max_uV) { +		rdev_err(rdev, "Restricting voltage, %u-%uuV\n", +			*min_uV, *max_uV);  		return -EINVAL; +	}  	return 0;  } @@ -139,13 +229,11 @@ static int regulator_check_current_limit(struct regulator_dev *rdev,  	BUG_ON(*min_uA > *max_uA);  	if (!rdev->constraints) { -		printk(KERN_ERR "%s: no constraints for %s\n", __func__, -		       rdev_get_name(rdev)); +		rdev_err(rdev, "no constraints\n");  		return -ENODEV;  	}  	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) { -		printk(KERN_ERR "%s: operation not allowed for %s\n", -		       __func__, rdev_get_name(rdev)); +		rdev_err(rdev, "operation not allowed\n");  		return -EPERM;  	} @@ -154,71 +242,64 @@ static int regulator_check_current_limit(struct regulator_dev *rdev,  	if (*min_uA < rdev->constraints->min_uA)  		*min_uA = rdev->constraints->min_uA; -	if (*min_uA > *max_uA) +	if (*min_uA > *max_uA) { +		rdev_err(rdev, "unsupportable current range: %d-%duA\n", +			 *min_uA, *max_uA);  		return -EINVAL; +	}  	return 0;  }  /* operating mode constraint check */ -static int regulator_check_mode(struct regulator_dev *rdev, int mode) +static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)  { -	switch (mode) { +	switch (*mode) {  	case REGULATOR_MODE_FAST:  	case REGULATOR_MODE_NORMAL:  	case REGULATOR_MODE_IDLE:  	case REGULATOR_MODE_STANDBY:  		break;  	default: +		rdev_err(rdev, "invalid mode %x specified\n", *mode);  		return -EINVAL;  	}  	if (!rdev->constraints) { -		printk(KERN_ERR "%s: no constraints for %s\n", __func__, -		       rdev_get_name(rdev)); +		rdev_err(rdev, "no constraints\n");  		return -ENODEV;  	}  	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) { -		printk(KERN_ERR "%s: operation not allowed for %s\n", -		       __func__, rdev_get_name(rdev)); +		rdev_err(rdev, "operation not allowed\n");  		return -EPERM;  	} -	if (!(rdev->constraints->valid_modes_mask & mode)) { -		printk(KERN_ERR "%s: invalid mode %x for %s\n", -		       __func__, mode, rdev_get_name(rdev)); -		return -EINVAL; + +	/* The modes are bitmasks, the most power hungry modes having +	 * the lowest values. If the requested mode isn't supported +	 * try higher modes. */ +	while (*mode) { +		if (rdev->constraints->valid_modes_mask & *mode) +			return 0; +		*mode /= 2;  	} -	return 0; + +	return -EINVAL;  }  /* dynamic regulator mode switching constraint check */  static int regulator_check_drms(struct regulator_dev *rdev)  {  	if (!rdev->constraints) { -		printk(KERN_ERR "%s: no constraints for %s\n", __func__, -		       rdev_get_name(rdev)); +		rdev_err(rdev, "no constraints\n");  		return -ENODEV;  	}  	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) { -		printk(KERN_ERR "%s: operation not allowed for %s\n", -		       __func__, rdev_get_name(rdev)); +		rdev_err(rdev, "operation not allowed\n");  		return -EPERM;  	}  	return 0;  } -static ssize_t device_requested_uA_show(struct device *dev, -			     struct device_attribute *attr, char *buf) -{ -	struct regulator *regulator; - -	regulator = get_device_regulator(dev); -	if (regulator == NULL) -		return 0; - -	return sprintf(buf, "%d\n", regulator->uA_load); -} -  static ssize_t regulator_uV_show(struct device *dev,  				struct device_attribute *attr, char *buf)  { @@ -242,13 +323,14 @@ static ssize_t regulator_uA_show(struct device *dev,  }  static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); -static ssize_t regulator_name_show(struct device *dev, -			     struct device_attribute *attr, char *buf) +static ssize_t name_show(struct device *dev, struct device_attribute *attr, +			 char *buf)  {  	struct regulator_dev *rdev = dev_get_drvdata(dev);  	return sprintf(buf, "%s\n", rdev_get_name(rdev));  } +static DEVICE_ATTR_RO(name);  static ssize_t regulator_print_opmode(char *buf, int mode)  { @@ -331,6 +413,12 @@ static ssize_t regulator_status_show(struct device *dev,  	case REGULATOR_STATUS_STANDBY:  		label = "standby";  		break; +	case REGULATOR_STATUS_BYPASS: +		label = "bypass"; +		break; +	case REGULATOR_STATUS_UNDEFINED: +		label = "undefined"; +		break;  	default:  		return -ERANGE;  	} @@ -402,15 +490,16 @@ static ssize_t regulator_total_uA_show(struct device *dev,  }  static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); -static ssize_t regulator_num_users_show(struct device *dev, -				      struct device_attribute *attr, char *buf) +static ssize_t num_users_show(struct device *dev, struct device_attribute *attr, +			      char *buf)  {  	struct regulator_dev *rdev = dev_get_drvdata(dev);  	return sprintf(buf, "%d\n", rdev->use_count);  } +static DEVICE_ATTR_RO(num_users); -static ssize_t regulator_type_show(struct device *dev, -				  struct device_attribute *attr, char *buf) +static ssize_t type_show(struct device *dev, struct device_attribute *attr, +			 char *buf)  {  	struct regulator_dev *rdev = dev_get_drvdata(dev); @@ -422,6 +511,7 @@ static ssize_t regulator_type_show(struct device *dev,  	}  	return sprintf(buf, "unknown\n");  } +static DEVICE_ATTR_RO(type);  static ssize_t regulator_suspend_mem_uV_show(struct device *dev,  				struct device_attribute *attr, char *buf) @@ -519,17 +609,39 @@ static ssize_t regulator_suspend_standby_state_show(struct device *dev,  static DEVICE_ATTR(suspend_standby_state, 0444,  		regulator_suspend_standby_state_show, NULL); +static ssize_t regulator_bypass_show(struct device *dev, +				     struct device_attribute *attr, char *buf) +{ +	struct regulator_dev *rdev = dev_get_drvdata(dev); +	const char *report; +	bool bypass; +	int ret; + +	ret = rdev->desc->ops->get_bypass(rdev, &bypass); + +	if (ret != 0) +		report = "unknown"; +	else if (bypass) +		report = "enabled"; +	else +		report = "disabled"; + +	return sprintf(buf, "%s\n", report); +} +static DEVICE_ATTR(bypass, 0444, +		   regulator_bypass_show, NULL);  /*   * These are the only attributes are present for all regulators.   * Other attributes are a function of regulator functionality.   */ -static struct device_attribute regulator_dev_attrs[] = { -	__ATTR(name, 0444, regulator_name_show, NULL), -	__ATTR(num_users, 0444, regulator_num_users_show, NULL), -	__ATTR(type, 0444, regulator_type_show, NULL), -	__ATTR_NULL, +static struct attribute *regulator_dev_attrs[] = { +	&dev_attr_name.attr, +	&dev_attr_num_users.attr, +	&dev_attr_type.attr, +	NULL,  }; +ATTRIBUTE_GROUPS(regulator_dev);  static void regulator_dev_release(struct device *dev)  { @@ -540,7 +652,7 @@ static void regulator_dev_release(struct device *dev)  static struct class regulator_class = {  	.name = "regulator",  	.dev_release = regulator_dev_release, -	.dev_attrs = regulator_dev_attrs, +	.dev_groups = regulator_dev_groups,  };  /* Calculate the new optimum regulator operating mode based on the new total @@ -553,18 +665,21 @@ static void drms_uA_update(struct regulator_dev *rdev)  	err = regulator_check_drms(rdev);  	if (err < 0 || !rdev->desc->ops->get_optimum_mode || -	    !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode) +	    (!rdev->desc->ops->get_voltage && +	     !rdev->desc->ops->get_voltage_sel) || +	    !rdev->desc->ops->set_mode)  		return;  	/* get output voltage */ -	output_uV = rdev->desc->ops->get_voltage(rdev); +	output_uV = _regulator_get_voltage(rdev);  	if (output_uV <= 0)  		return;  	/* get input voltage */ -	if (rdev->supply && rdev->supply->desc->ops->get_voltage) -		input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); -	else +	input_uV = 0; +	if (rdev->supply) +		input_uV = regulator_get_voltage(rdev->supply); +	if (input_uV <= 0)  		input_uV = rdev->constraints->input_uV;  	if (input_uV <= 0)  		return; @@ -578,7 +693,7 @@ static void drms_uA_update(struct regulator_dev *rdev)  						  output_uV, current_uA);  	/* check the new mode is allowed */ -	err = regulator_check_mode(rdev, mode); +	err = regulator_mode_constrain(rdev, &mode);  	if (err == 0)  		rdev->desc->ops->set_mode(rdev, mode);  } @@ -587,48 +702,39 @@ static int suspend_set_state(struct regulator_dev *rdev,  	struct regulator_state *rstate)  {  	int ret = 0; -	bool can_set_state; - -	can_set_state = rdev->desc->ops->set_suspend_enable && -		rdev->desc->ops->set_suspend_disable;  	/* If we have no suspend mode configration don't set anything; -	 * only warn if the driver actually makes the suspend mode -	 * configurable. +	 * only warn if the driver implements set_suspend_voltage or +	 * set_suspend_mode callback.  	 */  	if (!rstate->enabled && !rstate->disabled) { -		if (can_set_state) -			printk(KERN_WARNING "%s: No configuration for %s\n", -			       __func__, rdev_get_name(rdev)); +		if (rdev->desc->ops->set_suspend_voltage || +		    rdev->desc->ops->set_suspend_mode) +			rdev_warn(rdev, "No configuration\n");  		return 0;  	}  	if (rstate->enabled && rstate->disabled) { -		printk(KERN_ERR "%s: invalid configuration for %s\n", -		       __func__, rdev_get_name(rdev)); +		rdev_err(rdev, "invalid configuration\n");  		return -EINVAL;  	} -	if (!can_set_state) { -		printk(KERN_ERR "%s: no way to set suspend state\n", -			__func__); -		return -EINVAL; -	} - -	if (rstate->enabled) +	if (rstate->enabled && rdev->desc->ops->set_suspend_enable)  		ret = rdev->desc->ops->set_suspend_enable(rdev); -	else +	else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)  		ret = rdev->desc->ops->set_suspend_disable(rdev); +	else /* OK if set_suspend_enable or set_suspend_disable is NULL */ +		ret = 0; +  	if (ret < 0) { -		printk(KERN_ERR "%s: failed to enabled/disable\n", __func__); +		rdev_err(rdev, "failed to enabled/disable\n");  		return ret;  	}  	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {  		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);  		if (ret < 0) { -			printk(KERN_ERR "%s: failed to set voltage\n", -				__func__); +			rdev_err(rdev, "failed to set voltage\n");  			return ret;  		}  	} @@ -636,7 +742,7 @@ static int suspend_set_state(struct regulator_dev *rdev,  	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {  		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);  		if (ret < 0) { -			printk(KERN_ERR "%s: failed to set mode\n", __func__); +			rdev_err(rdev, "failed to set mode\n");  			return ret;  		}  	} @@ -688,6 +794,10 @@ static void print_constraints(struct regulator_dev *rdev)  			count += sprintf(buf + count, "at %d mV ", ret / 1000);  	} +	if (constraints->uV_offset) +		count += sprintf(buf, "%dmV offset ", +				 constraints->uV_offset / 1000); +  	if (constraints->min_uA && constraints->max_uA) {  		if (constraints->min_uA == constraints->max_uA)  			count += sprintf(buf + count, "%d mA ", @@ -714,29 +824,43 @@ static void print_constraints(struct regulator_dev *rdev)  	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)  		count += sprintf(buf + count, "standby"); -	printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf); +	if (!count) +		sprintf(buf, "no parameters"); + +	rdev_info(rdev, "%s\n", buf); + +	if ((constraints->min_uV != constraints->max_uV) && +	    !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) +		rdev_warn(rdev, +			  "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");  }  static int machine_constraints_voltage(struct regulator_dev *rdev,  	struct regulation_constraints *constraints)  {  	struct regulator_ops *ops = rdev->desc->ops; -	const char *name = rdev_get_name(rdev);  	int ret;  	/* do we need to apply the constraint voltage */  	if (rdev->constraints->apply_uV && -		rdev->constraints->min_uV == rdev->constraints->max_uV && -		ops->set_voltage) { -		ret = ops->set_voltage(rdev, -			rdev->constraints->min_uV, rdev->constraints->max_uV); +	    rdev->constraints->min_uV == rdev->constraints->max_uV) { +		int current_uV = _regulator_get_voltage(rdev); +		if (current_uV < 0) { +			rdev_err(rdev, "failed to get the current voltage\n"); +			return current_uV; +		} +		if (current_uV < rdev->constraints->min_uV || +		    current_uV > rdev->constraints->max_uV) { +			ret = _regulator_do_set_voltage( +				rdev, rdev->constraints->min_uV, +				rdev->constraints->max_uV);  			if (ret < 0) { -				printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n", -				       __func__, -				       rdev->constraints->min_uV, name); -				rdev->constraints = NULL; +				rdev_err(rdev, +					"failed to apply %duV constraint\n", +					rdev->constraints->min_uV);  				return ret;  			} +		}  	}  	/* constrain machine-level voltage specs to fit @@ -765,8 +889,7 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,  		/* else require explicit machine-level constraints */  		if (cmin <= 0 || cmax <= 0 || cmax < cmin) { -			pr_err("%s: %s '%s' voltage constraints\n", -				       __func__, "invalid", name); +			rdev_err(rdev, "invalid voltage constraints\n");  			return -EINVAL;  		} @@ -787,22 +910,21 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,  		/* final: [min_uV..max_uV] valid iff constraints valid */  		if (max_uV < min_uV) { -			pr_err("%s: %s '%s' voltage constraints\n", -				       __func__, "unsupportable", name); +			rdev_err(rdev, +				 "unsupportable voltage constraints %u-%uuV\n", +				 min_uV, max_uV);  			return -EINVAL;  		}  		/* use regulator's subset of machine constraints */  		if (constraints->min_uV < min_uV) { -			pr_debug("%s: override '%s' %s, %d -> %d\n", -				       __func__, name, "min_uV", -					constraints->min_uV, min_uV); +			rdev_dbg(rdev, "override min_uV, %d -> %d\n", +				 constraints->min_uV, min_uV);  			constraints->min_uV = min_uV;  		}  		if (constraints->max_uV > max_uV) { -			pr_debug("%s: override '%s' %s, %d -> %d\n", -				       __func__, name, "max_uV", -					constraints->max_uV, max_uV); +			rdev_dbg(rdev, "override max_uV, %d -> %d\n", +				 constraints->max_uV, max_uV);  			constraints->max_uV = max_uV;  		}  	} @@ -810,6 +932,38 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,  	return 0;  } +static int machine_constraints_current(struct regulator_dev *rdev, +	struct regulation_constraints *constraints) +{ +	struct regulator_ops *ops = rdev->desc->ops; +	int ret; + +	if (!constraints->min_uA && !constraints->max_uA) +		return 0; + +	if (constraints->min_uA > constraints->max_uA) { +		rdev_err(rdev, "Invalid current constraints\n"); +		return -EINVAL; +	} + +	if (!ops->set_current_limit || !ops->get_current_limit) { +		rdev_warn(rdev, "Operation of current configuration missing\n"); +		return 0; +	} + +	/* Set regulator current in constraints range */ +	ret = ops->set_current_limit(rdev, constraints->min_uA, +			constraints->max_uA); +	if (ret < 0) { +		rdev_err(rdev, "Failed to set current constraint, %d\n", ret); +		return ret; +	} + +	return 0; +} + +static int _regulator_do_enable(struct regulator_dev *rdev); +  /**   * set_machine_constraints - sets regulator constraints   * @rdev: regulator source @@ -822,44 +976,47 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,   * set_mode.   */  static int set_machine_constraints(struct regulator_dev *rdev, -	struct regulation_constraints *constraints) +	const struct regulation_constraints *constraints)  {  	int ret = 0; -	const char *name;  	struct regulator_ops *ops = rdev->desc->ops; -	rdev->constraints = constraints; +	if (constraints) +		rdev->constraints = kmemdup(constraints, sizeof(*constraints), +					    GFP_KERNEL); +	else +		rdev->constraints = kzalloc(sizeof(*constraints), +					    GFP_KERNEL); +	if (!rdev->constraints) +		return -ENOMEM; -	name = rdev_get_name(rdev); +	ret = machine_constraints_voltage(rdev, rdev->constraints); +	if (ret != 0) +		goto out; -	ret = machine_constraints_voltage(rdev, constraints); +	ret = machine_constraints_current(rdev, rdev->constraints);  	if (ret != 0)  		goto out;  	/* do we need to setup our suspend state */ -	if (constraints->initial_state) { -		ret = suspend_prepare(rdev, constraints->initial_state); +	if (rdev->constraints->initial_state) { +		ret = suspend_prepare(rdev, rdev->constraints->initial_state);  		if (ret < 0) { -			printk(KERN_ERR "%s: failed to set suspend state for %s\n", -			       __func__, name); -			rdev->constraints = NULL; +			rdev_err(rdev, "failed to set suspend state\n");  			goto out;  		}  	} -	if (constraints->initial_mode) { +	if (rdev->constraints->initial_mode) {  		if (!ops->set_mode) { -			printk(KERN_ERR "%s: no set_mode operation for %s\n", -			       __func__, name); +			rdev_err(rdev, "no set_mode operation\n");  			ret = -EINVAL;  			goto out;  		} -		ret = ops->set_mode(rdev, constraints->initial_mode); +		ret = ops->set_mode(rdev, rdev->constraints->initial_mode);  		if (ret < 0) { -			printk(KERN_ERR -			       "%s: failed to set initial mode for %s: %d\n", -			       __func__, name, ret); +			rdev_err(rdev, "failed to set initial mode: %d\n", ret);  			goto out;  		}  	} @@ -867,18 +1024,28 @@ static int set_machine_constraints(struct regulator_dev *rdev,  	/* If the constraints say the regulator should be on at this point  	 * and we have control then make sure it is enabled.  	 */ -	if ((constraints->always_on || constraints->boot_on) && ops->enable) { -		ret = ops->enable(rdev); +	if (rdev->constraints->always_on || rdev->constraints->boot_on) { +		ret = _regulator_do_enable(rdev); +		if (ret < 0 && ret != -EINVAL) { +			rdev_err(rdev, "failed to enable\n"); +			goto out; +		} +	} + +	if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) +		&& ops->set_ramp_delay) { +		ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);  		if (ret < 0) { -			printk(KERN_ERR "%s: failed to enable %s\n", -			       __func__, name); -			rdev->constraints = NULL; +			rdev_err(rdev, "failed to set ramp_delay\n");  			goto out;  		}  	}  	print_constraints(rdev); +	return 0;  out: +	kfree(rdev->constraints); +	rdev->constraints = NULL;  	return ret;  } @@ -892,28 +1059,25 @@ out:   * core if it's child is enabled.   */  static int set_supply(struct regulator_dev *rdev, -	struct regulator_dev *supply_rdev) +		      struct regulator_dev *supply_rdev)  {  	int err; -	err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj, -				"supply"); -	if (err) { -		printk(KERN_ERR -		       "%s: could not add device link %s err %d\n", -		       __func__, supply_rdev->dev.kobj.name, err); -		       goto out; +	rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev)); + +	rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); +	if (rdev->supply == NULL) { +		err = -ENOMEM; +		return err;  	} -	rdev->supply = supply_rdev; -	list_add(&rdev->slist, &supply_rdev->supply_list); -out: -	return err; +	supply_rdev->open_count++; + +	return 0;  }  /** - * set_consumer_device_supply: Bind a regulator to a symbolic supply + * set_consumer_device_supply - Bind a regulator to a symbolic supply   * @rdev:         regulator source - * @consumer_dev: device the supply applies to   * @consumer_dev_name: dev_name() string for device supply applies to   * @supply:       symbolic name for supply   * @@ -921,22 +1085,14 @@ out:   * sources to symbolic names for supplies for use by devices.  Devices   * should use these symbolic names to request regulators, avoiding the   * need to provide board-specific regulator names as platform data. - * - * Only one of consumer_dev and consumer_dev_name may be specified.   */  static int set_consumer_device_supply(struct regulator_dev *rdev, -	struct device *consumer_dev, const char *consumer_dev_name, -	const char *supply) +				      const char *consumer_dev_name, +				      const char *supply)  {  	struct regulator_map *node;  	int has_dev; -	if (consumer_dev && consumer_dev_name) -		return -EINVAL; - -	if (!consumer_dev_name && consumer_dev) -		consumer_dev_name = dev_name(consumer_dev); -  	if (supply == NULL)  		return -EINVAL; @@ -956,11 +1112,12 @@ static int set_consumer_device_supply(struct regulator_dev *rdev,  		if (strcmp(node->supply, supply) != 0)  			continue; -		dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n", -				dev_name(&node->regulator->dev), -				node->regulator->desc->name, -				supply, -				dev_name(&rdev->dev), rdev_get_name(rdev)); +		pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n", +			 consumer_dev_name, +			 dev_name(&node->regulator->dev), +			 node->regulator->desc->name, +			 supply, +			 dev_name(&rdev->dev), rdev_get_name(rdev));  		return -EBUSY;  	} @@ -996,7 +1153,7 @@ static void unset_regulator_supplies(struct regulator_dev *rdev)  	}  } -#define REG_STR_SIZE	32 +#define REG_STR_SIZE	64  static struct regulator *create_regulator(struct regulator_dev *rdev,  					  struct device *dev, @@ -1015,55 +1172,55 @@ static struct regulator *create_regulator(struct regulator_dev *rdev,  	list_add(®ulator->list, &rdev->consumer_list);  	if (dev) { -		/* create a 'requested_microamps_name' sysfs entry */ -		size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s", -			supply_name); -		if (size >= REG_STR_SIZE) -			goto overflow_err; -  		regulator->dev = dev; -		sysfs_attr_init(®ulator->dev_attr.attr); -		regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL); -		if (regulator->dev_attr.attr.name == NULL) -			goto attr_name_err; - -		regulator->dev_attr.attr.mode = 0444; -		regulator->dev_attr.show = device_requested_uA_show; -		err = device_create_file(dev, ®ulator->dev_attr); -		if (err < 0) { -			printk(KERN_WARNING "%s: could not add regulator_dev" -				" load sysfs\n", __func__); -			goto attr_name_err; -		} -		/* also add a link to the device sysfs entry */ +		/* Add a link to the device sysfs entry */  		size = scnprintf(buf, REG_STR_SIZE, "%s-%s",  				 dev->kobj.name, supply_name);  		if (size >= REG_STR_SIZE) -			goto attr_err; +			goto overflow_err;  		regulator->supply_name = kstrdup(buf, GFP_KERNEL);  		if (regulator->supply_name == NULL) -			goto attr_err; +			goto overflow_err;  		err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,  					buf);  		if (err) { -			printk(KERN_WARNING -			       "%s: could not add device link %s err %d\n", -			       __func__, dev->kobj.name, err); -			device_remove_file(dev, ®ulator->dev_attr); -			goto link_name_err; +			rdev_warn(rdev, "could not add device link %s err %d\n", +				  dev->kobj.name, err); +			/* non-fatal */  		} +	} else { +		regulator->supply_name = kstrdup(supply_name, GFP_KERNEL); +		if (regulator->supply_name == NULL) +			goto overflow_err; +	} + +	regulator->debugfs = debugfs_create_dir(regulator->supply_name, +						rdev->debugfs); +	if (!regulator->debugfs) { +		rdev_warn(rdev, "Failed to create debugfs directory\n"); +	} else { +		debugfs_create_u32("uA_load", 0444, regulator->debugfs, +				   ®ulator->uA_load); +		debugfs_create_u32("min_uV", 0444, regulator->debugfs, +				   ®ulator->min_uV); +		debugfs_create_u32("max_uV", 0444, regulator->debugfs, +				   ®ulator->max_uV);  	} + +	/* +	 * Check now if the regulator is an always on regulator - if +	 * it is then we don't need to do nearly so much work for +	 * enable/disable calls. +	 */ +	if (!_regulator_can_change_status(rdev) && +	    _regulator_is_enabled(rdev)) +		regulator->always_on = true; +  	mutex_unlock(&rdev->mutex);  	return regulator; -link_name_err: -	kfree(regulator->supply_name); -attr_err: -	device_remove_file(regulator->dev, ®ulator->dev_attr); -attr_name_err: -	kfree(regulator->dev_attr.attr.name);  overflow_err:  	list_del(®ulator->list);  	kfree(regulator); @@ -1073,30 +1230,78 @@ overflow_err:  static int _regulator_get_enable_time(struct regulator_dev *rdev)  { +	if (rdev->constraints && rdev->constraints->enable_time) +		return rdev->constraints->enable_time;  	if (!rdev->desc->ops->enable_time) -		return 0; +		return rdev->desc->enable_time;  	return rdev->desc->ops->enable_time(rdev);  } -/* Internal regulator request function */ -static struct regulator *_regulator_get(struct device *dev, const char *id, -					int exclusive) +static struct regulator_supply_alias *regulator_find_supply_alias( +		struct device *dev, const char *supply)  { -	struct regulator_dev *rdev; +	struct regulator_supply_alias *map; + +	list_for_each_entry(map, ®ulator_supply_alias_list, list) +		if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0) +			return map; + +	return NULL; +} + +static void regulator_supply_alias(struct device **dev, const char **supply) +{ +	struct regulator_supply_alias *map; + +	map = regulator_find_supply_alias(*dev, *supply); +	if (map) { +		dev_dbg(*dev, "Mapping supply %s to %s,%s\n", +				*supply, map->alias_supply, +				dev_name(map->alias_dev)); +		*dev = map->alias_dev; +		*supply = map->alias_supply; +	} +} + +static struct regulator_dev *regulator_dev_lookup(struct device *dev, +						  const char *supply, +						  int *ret) +{ +	struct regulator_dev *r; +	struct device_node *node;  	struct regulator_map *map; -	struct regulator *regulator = ERR_PTR(-ENODEV);  	const char *devname = NULL; -	int ret; -	if (id == NULL) { -		printk(KERN_ERR "regulator: get() with no identifier\n"); -		return regulator; +	regulator_supply_alias(&dev, &supply); + +	/* first do a dt based lookup */ +	if (dev && dev->of_node) { +		node = of_get_regulator(dev, supply); +		if (node) { +			list_for_each_entry(r, ®ulator_list, list) +				if (r->dev.parent && +					node == r->dev.of_node) +					return r; +			*ret = -EPROBE_DEFER; +			return NULL; +		} else { +			/* +			 * If we couldn't even get the node then it's +			 * not just that the device didn't register +			 * yet, there's no node and we'll never +			 * succeed. +			 */ +			*ret = -ENODEV; +		}  	} +	/* if not found, try doing it non-dt way */  	if (dev)  		devname = dev_name(dev); -	mutex_lock(®ulator_list_mutex); +	list_for_each_entry(r, ®ulator_list, list) +		if (strcmp(rdev_get_name(r), supply) == 0) +			return r;  	list_for_each_entry(map, ®ulator_map_list, list) {  		/* If the mapping has a device set up it must match */ @@ -1104,31 +1309,68 @@ static struct regulator *_regulator_get(struct device *dev, const char *id,  		    (!devname || strcmp(map->dev_name, devname)))  			continue; -		if (strcmp(map->supply, id) == 0) { -			rdev = map->regulator; -			goto found; -		} +		if (strcmp(map->supply, supply) == 0) +			return map->regulator;  	} -	if (board_wants_dummy_regulator) { -		rdev = dummy_regulator_rdev; -		goto found; + +	return NULL; +} + +/* Internal regulator request function */ +static struct regulator *_regulator_get(struct device *dev, const char *id, +					bool exclusive, bool allow_dummy) +{ +	struct regulator_dev *rdev; +	struct regulator *regulator = ERR_PTR(-EPROBE_DEFER); +	const char *devname = NULL; +	int ret; + +	if (id == NULL) { +		pr_err("get() with no identifier\n"); +		return ERR_PTR(-EINVAL);  	} -#ifdef CONFIG_REGULATOR_DUMMY +	if (dev) +		devname = dev_name(dev); + +	if (have_full_constraints()) +		ret = -ENODEV; +	else +		ret = -EPROBE_DEFER; + +	mutex_lock(®ulator_list_mutex); + +	rdev = regulator_dev_lookup(dev, id, &ret); +	if (rdev) +		goto found; + +	regulator = ERR_PTR(ret); + +	/* +	 * If we have return value from dev_lookup fail, we do not expect to +	 * succeed, so, quit with appropriate error value +	 */ +	if (ret && ret != -ENODEV) +		goto out; +  	if (!devname)  		devname = "deviceless"; -	/* If the board didn't flag that it was fully constrained then -	 * substitute in a dummy regulator so consumers can continue. +	/* +	 * Assume that a regulator is physically present and enabled +	 * even if it isn't hooked up and just provide a dummy.  	 */ -	if (!has_full_constraints) { -		pr_warning("%s supply %s not found, using dummy regulator\n", -			   devname, id); +	if (have_full_constraints() && allow_dummy) { +		pr_warn("%s supply %s not found, using dummy regulator\n", +			devname, id); +  		rdev = dummy_regulator_rdev;  		goto found; +	/* Don't log an error when called from regulator_get_optional() */ +	} else if (!have_full_constraints() || exclusive) { +		dev_warn(dev, "dummy supplies not allowed\n");  	} -#endif  	mutex_unlock(®ulator_list_mutex);  	return regulator; @@ -1151,6 +1393,7 @@ found:  	if (regulator == NULL) {  		regulator = ERR_PTR(-ENOMEM);  		module_put(rdev->owner); +		goto out;  	}  	rdev->open_count++; @@ -1185,7 +1428,7 @@ out:   */  struct regulator *regulator_get(struct device *dev, const char *id)  { -	return _regulator_get(dev, id, 0); +	return _regulator_get(dev, id, false, true);  }  EXPORT_SYMBOL_GPL(regulator_get); @@ -1196,9 +1439,9 @@ EXPORT_SYMBOL_GPL(regulator_get);   *   * Returns a struct regulator corresponding to the regulator producer,   * or IS_ERR() condition containing errno.  Other consumers will be - * unable to obtain this reference is held and the use count for the - * regulator will be initialised to reflect the current state of the - * regulator. + * unable to obtain this regulator while this reference is held and the + * use count for the regulator will be initialised to reflect the current + * state of the regulator.   *   * This is intended for use by consumers which cannot tolerate shared   * use of the regulator such as those which need to force the @@ -1212,35 +1455,52 @@ EXPORT_SYMBOL_GPL(regulator_get);   */  struct regulator *regulator_get_exclusive(struct device *dev, const char *id)  { -	return _regulator_get(dev, id, 1); +	return _regulator_get(dev, id, true, false);  }  EXPORT_SYMBOL_GPL(regulator_get_exclusive);  /** - * regulator_put - "free" the regulator source - * @regulator: regulator source + * regulator_get_optional - obtain optional access to a regulator. + * @dev: device for regulator "consumer" + * @id: Supply name or regulator ID.   * - * Note: drivers must ensure that all regulator_enable calls made on this - * regulator source are balanced by regulator_disable calls prior to calling - * this function. + * Returns a struct regulator corresponding to the regulator producer, + * or IS_ERR() condition containing errno. + * + * This is intended for use by consumers for devices which can have + * some supplies unconnected in normal use, such as some MMC devices. + * It can allow the regulator core to provide stub supplies for other + * supplies requested using normal regulator_get() calls without + * disrupting the operation of drivers that can handle absent + * supplies. + * + * Use of supply names configured via regulator_set_device_supply() is + * strongly encouraged.  It is recommended that the supply name used + * should match the name used for the supply and/or the relevant + * device pins in the datasheet.   */ -void regulator_put(struct regulator *regulator) +struct regulator *regulator_get_optional(struct device *dev, const char *id) +{ +	return _regulator_get(dev, id, false, false); +} +EXPORT_SYMBOL_GPL(regulator_get_optional); + +/* Locks held by regulator_put() */ +static void _regulator_put(struct regulator *regulator)  {  	struct regulator_dev *rdev;  	if (regulator == NULL || IS_ERR(regulator))  		return; -	mutex_lock(®ulator_list_mutex);  	rdev = regulator->rdev; +	debugfs_remove_recursive(regulator->debugfs); +  	/* remove any sysfs entries */ -	if (regulator->dev) { +	if (regulator->dev)  		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); -		kfree(regulator->supply_name); -		device_remove_file(regulator->dev, ®ulator->dev_attr); -		kfree(regulator->dev_attr.attr.name); -	} +	kfree(regulator->supply_name);  	list_del(®ulator->list);  	kfree(regulator); @@ -1248,36 +1508,329 @@ void regulator_put(struct regulator *regulator)  	rdev->exclusive = 0;  	module_put(rdev->owner); +} + +/** + * regulator_put - "free" the regulator source + * @regulator: regulator source + * + * Note: drivers must ensure that all regulator_enable calls made on this + * regulator source are balanced by regulator_disable calls prior to calling + * this function. + */ +void regulator_put(struct regulator *regulator) +{ +	mutex_lock(®ulator_list_mutex); +	_regulator_put(regulator);  	mutex_unlock(®ulator_list_mutex);  }  EXPORT_SYMBOL_GPL(regulator_put); -static int _regulator_can_change_status(struct regulator_dev *rdev) +/** + * regulator_register_supply_alias - Provide device alias for supply lookup + * + * @dev: device that will be given as the regulator "consumer" + * @id: Supply name or regulator ID + * @alias_dev: device that should be used to lookup the supply + * @alias_id: Supply name or regulator ID that should be used to lookup the + * supply + * + * All lookups for id on dev will instead be conducted for alias_id on + * alias_dev. + */ +int regulator_register_supply_alias(struct device *dev, const char *id, +				    struct device *alias_dev, +				    const char *alias_id)  { -	if (!rdev->constraints) -		return 0; +	struct regulator_supply_alias *map; -	if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS) -		return 1; -	else -		return 0; +	map = regulator_find_supply_alias(dev, id); +	if (map) +		return -EEXIST; + +	map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL); +	if (!map) +		return -ENOMEM; + +	map->src_dev = dev; +	map->src_supply = id; +	map->alias_dev = alias_dev; +	map->alias_supply = alias_id; + +	list_add(&map->list, ®ulator_supply_alias_list); + +	pr_info("Adding alias for supply %s,%s -> %s,%s\n", +		id, dev_name(dev), alias_id, dev_name(alias_dev)); + +	return 0;  } +EXPORT_SYMBOL_GPL(regulator_register_supply_alias); -/* locks held by regulator_enable() */ -static int _regulator_enable(struct regulator_dev *rdev) +/** + * regulator_unregister_supply_alias - Remove device alias + * + * @dev: device that will be given as the regulator "consumer" + * @id: Supply name or regulator ID + * + * Remove a lookup alias if one exists for id on dev. + */ +void regulator_unregister_supply_alias(struct device *dev, const char *id) +{ +	struct regulator_supply_alias *map; + +	map = regulator_find_supply_alias(dev, id); +	if (map) { +		list_del(&map->list); +		kfree(map); +	} +} +EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias); + +/** + * regulator_bulk_register_supply_alias - register multiple aliases + * + * @dev: device that will be given as the regulator "consumer" + * @id: List of supply names or regulator IDs + * @alias_dev: device that should be used to lookup the supply + * @alias_id: List of supply names or regulator IDs that should be used to + * lookup the supply + * @num_id: Number of aliases to register + * + * @return 0 on success, an errno on failure. + * + * This helper function allows drivers to register several supply + * aliases in one operation.  If any of the aliases cannot be + * registered any aliases that were registered will be removed + * before returning to the caller. + */ +int regulator_bulk_register_supply_alias(struct device *dev, +					 const char *const *id, +					 struct device *alias_dev, +					 const char *const *alias_id, +					 int num_id) +{ +	int i; +	int ret; + +	for (i = 0; i < num_id; ++i) { +		ret = regulator_register_supply_alias(dev, id[i], alias_dev, +						      alias_id[i]); +		if (ret < 0) +			goto err; +	} + +	return 0; + +err: +	dev_err(dev, +		"Failed to create supply alias %s,%s -> %s,%s\n", +		id[i], dev_name(dev), alias_id[i], dev_name(alias_dev)); + +	while (--i >= 0) +		regulator_unregister_supply_alias(dev, id[i]); + +	return ret; +} +EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias); + +/** + * regulator_bulk_unregister_supply_alias - unregister multiple aliases + * + * @dev: device that will be given as the regulator "consumer" + * @id: List of supply names or regulator IDs + * @num_id: Number of aliases to unregister + * + * This helper function allows drivers to unregister several supply + * aliases in one operation. + */ +void regulator_bulk_unregister_supply_alias(struct device *dev, +					    const char *const *id, +					    int num_id) +{ +	int i; + +	for (i = 0; i < num_id; ++i) +		regulator_unregister_supply_alias(dev, id[i]); +} +EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias); + + +/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */ +static int regulator_ena_gpio_request(struct regulator_dev *rdev, +				const struct regulator_config *config) +{ +	struct regulator_enable_gpio *pin; +	int ret; + +	list_for_each_entry(pin, ®ulator_ena_gpio_list, list) { +		if (pin->gpio == config->ena_gpio) { +			rdev_dbg(rdev, "GPIO %d is already used\n", +				config->ena_gpio); +			goto update_ena_gpio_to_rdev; +		} +	} + +	ret = gpio_request_one(config->ena_gpio, +				GPIOF_DIR_OUT | config->ena_gpio_flags, +				rdev_get_name(rdev)); +	if (ret) +		return ret; + +	pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL); +	if (pin == NULL) { +		gpio_free(config->ena_gpio); +		return -ENOMEM; +	} + +	pin->gpio = config->ena_gpio; +	pin->ena_gpio_invert = config->ena_gpio_invert; +	list_add(&pin->list, ®ulator_ena_gpio_list); + +update_ena_gpio_to_rdev: +	pin->request_count++; +	rdev->ena_pin = pin; +	return 0; +} + +static void regulator_ena_gpio_free(struct regulator_dev *rdev) +{ +	struct regulator_enable_gpio *pin, *n; + +	if (!rdev->ena_pin) +		return; + +	/* Free the GPIO only in case of no use */ +	list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) { +		if (pin->gpio == rdev->ena_pin->gpio) { +			if (pin->request_count <= 1) { +				pin->request_count = 0; +				gpio_free(pin->gpio); +				list_del(&pin->list); +				kfree(pin); +			} else { +				pin->request_count--; +			} +		} +	} +} + +/** + * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control + * @rdev: regulator_dev structure + * @enable: enable GPIO at initial use? + * + * GPIO is enabled in case of initial use. (enable_count is 0) + * GPIO is disabled when it is not shared any more. (enable_count <= 1) + */ +static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable) +{ +	struct regulator_enable_gpio *pin = rdev->ena_pin; + +	if (!pin) +		return -EINVAL; + +	if (enable) { +		/* Enable GPIO at initial use */ +		if (pin->enable_count == 0) +			gpio_set_value_cansleep(pin->gpio, +						!pin->ena_gpio_invert); + +		pin->enable_count++; +	} else { +		if (pin->enable_count > 1) { +			pin->enable_count--; +			return 0; +		} + +		/* Disable GPIO if not used */ +		if (pin->enable_count <= 1) { +			gpio_set_value_cansleep(pin->gpio, +						pin->ena_gpio_invert); +			pin->enable_count = 0; +		} +	} + +	return 0; +} + +static int _regulator_do_enable(struct regulator_dev *rdev)  {  	int ret, delay; -	/* do we need to enable the supply regulator first */ -	if (rdev->supply) { -		ret = _regulator_enable(rdev->supply); -		if (ret < 0) { -			printk(KERN_ERR "%s: failed to enable %s: %d\n", -			       __func__, rdev_get_name(rdev), ret); +	/* Query before enabling in case configuration dependent.  */ +	ret = _regulator_get_enable_time(rdev); +	if (ret >= 0) { +		delay = ret; +	} else { +		rdev_warn(rdev, "enable_time() failed: %d\n", ret); +		delay = 0; +	} + +	trace_regulator_enable(rdev_get_name(rdev)); + +	if (rdev->ena_pin) { +		ret = regulator_ena_gpio_ctrl(rdev, true); +		if (ret < 0)  			return ret; +		rdev->ena_gpio_state = 1; +	} else if (rdev->desc->ops->enable) { +		ret = rdev->desc->ops->enable(rdev); +		if (ret < 0) +			return ret; +	} else { +		return -EINVAL; +	} + +	/* Allow the regulator to ramp; it would be useful to extend +	 * this for bulk operations so that the regulators can ramp +	 * together.  */ +	trace_regulator_enable_delay(rdev_get_name(rdev)); + +	/* +	 * Delay for the requested amount of time as per the guidelines in: +	 * +	 *     Documentation/timers/timers-howto.txt +	 * +	 * The assumption here is that regulators will never be enabled in +	 * atomic context and therefore sleeping functions can be used. +	 */ +	if (delay) { +		unsigned int ms = delay / 1000; +		unsigned int us = delay % 1000; + +		if (ms > 0) { +			/* +			 * For small enough values, handle super-millisecond +			 * delays in the usleep_range() call below. +			 */ +			if (ms < 20) +				us += ms * 1000; +			else +				msleep(ms);  		} + +		/* +		 * Give the scheduler some room to coalesce with any other +		 * wakeup sources. For delays shorter than 10 us, don't even +		 * bother setting up high-resolution timers and just busy- +		 * loop. +		 */ +		if (us >= 10) +			usleep_range(us, us + 100); +		else +			udelay(us);  	} +	trace_regulator_enable_complete(rdev_get_name(rdev)); + +	return 0; +} + +/* locks held by regulator_enable() */ +static int _regulator_enable(struct regulator_dev *rdev) +{ +	int ret; +  	/* check voltage and requested load before enabling */  	if (rdev->constraints &&  	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) @@ -1290,37 +1843,12 @@ static int _regulator_enable(struct regulator_dev *rdev)  			if (!_regulator_can_change_status(rdev))  				return -EPERM; -			if (!rdev->desc->ops->enable) -				return -EINVAL; - -			/* Query before enabling in case configuration -			 * dependant.  */ -			ret = _regulator_get_enable_time(rdev); -			if (ret >= 0) { -				delay = ret; -			} else { -				printk(KERN_WARNING -					"%s: enable_time() failed for %s: %d\n", -					__func__, rdev_get_name(rdev), -					ret); -				delay = 0; -			} - -			/* Allow the regulator to ramp; it would be useful -			 * to extend this for bulk operations so that the -			 * regulators can ramp together.  */ -			ret = rdev->desc->ops->enable(rdev); +			ret = _regulator_do_enable(rdev);  			if (ret < 0)  				return ret; -			if (delay >= 1000) -				mdelay(delay / 1000); -			else if (delay) -				udelay(delay); -  		} else if (ret < 0) { -			printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n", -			       __func__, rdev_get_name(rdev), ret); +			rdev_err(rdev, "is_enabled() failed: %d\n", ret);  			return ret;  		}  		/* Fallthrough on positive return values - already enabled */ @@ -1347,22 +1875,56 @@ int regulator_enable(struct regulator *regulator)  	struct regulator_dev *rdev = regulator->rdev;  	int ret = 0; +	if (regulator->always_on) +		return 0; + +	if (rdev->supply) { +		ret = regulator_enable(rdev->supply); +		if (ret != 0) +			return ret; +	} +  	mutex_lock(&rdev->mutex);  	ret = _regulator_enable(rdev);  	mutex_unlock(&rdev->mutex); + +	if (ret != 0 && rdev->supply) +		regulator_disable(rdev->supply); +  	return ret;  }  EXPORT_SYMBOL_GPL(regulator_enable); +static int _regulator_do_disable(struct regulator_dev *rdev) +{ +	int ret; + +	trace_regulator_disable(rdev_get_name(rdev)); + +	if (rdev->ena_pin) { +		ret = regulator_ena_gpio_ctrl(rdev, false); +		if (ret < 0) +			return ret; +		rdev->ena_gpio_state = 0; + +	} else if (rdev->desc->ops->disable) { +		ret = rdev->desc->ops->disable(rdev); +		if (ret != 0) +			return ret; +	} + +	trace_regulator_disable_complete(rdev_get_name(rdev)); + +	return 0; +} +  /* locks held by regulator_disable() */ -static int _regulator_disable(struct regulator_dev *rdev, -		struct regulator_dev **supply_rdev_ptr) +static int _regulator_disable(struct regulator_dev *rdev)  {  	int ret = 0;  	if (WARN(rdev->use_count <= 0, -			"unbalanced disables for %s\n", -			rdev_get_name(rdev))) +		 "unbalanced disables for %s\n", rdev_get_name(rdev)))  		return -EIO;  	/* are we the last user and permitted to disable ? */ @@ -1370,22 +1932,16 @@ static int _regulator_disable(struct regulator_dev *rdev,  	    (rdev->constraints && !rdev->constraints->always_on)) {  		/* we are last user */ -		if (_regulator_can_change_status(rdev) && -		    rdev->desc->ops->disable) { -			ret = rdev->desc->ops->disable(rdev); +		if (_regulator_can_change_status(rdev)) { +			ret = _regulator_do_disable(rdev);  			if (ret < 0) { -				printk(KERN_ERR "%s: failed to disable %s\n", -				       __func__, rdev_get_name(rdev)); +				rdev_err(rdev, "failed to disable\n");  				return ret;  			} -  			_notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, -					     NULL); +					NULL);  		} -		/* decrease our supplies ref count and disable if required */ -		*supply_rdev_ptr = rdev->supply; -  		rdev->use_count = 0;  	} else if (rdev->use_count > 1) { @@ -1396,6 +1952,7 @@ static int _regulator_disable(struct regulator_dev *rdev,  		rdev->use_count--;  	} +  	return ret;  } @@ -1414,51 +1971,37 @@ static int _regulator_disable(struct regulator_dev *rdev,  int regulator_disable(struct regulator *regulator)  {  	struct regulator_dev *rdev = regulator->rdev; -	struct regulator_dev *supply_rdev = NULL;  	int ret = 0; +	if (regulator->always_on) +		return 0; +  	mutex_lock(&rdev->mutex); -	ret = _regulator_disable(rdev, &supply_rdev); +	ret = _regulator_disable(rdev);  	mutex_unlock(&rdev->mutex); -	/* decrease our supplies ref count and disable if required */ -	while (supply_rdev != NULL) { -		rdev = supply_rdev; - -		mutex_lock(&rdev->mutex); -		_regulator_disable(rdev, &supply_rdev); -		mutex_unlock(&rdev->mutex); -	} +	if (ret == 0 && rdev->supply) +		regulator_disable(rdev->supply);  	return ret;  }  EXPORT_SYMBOL_GPL(regulator_disable);  /* locks held by regulator_force_disable() */ -static int _regulator_force_disable(struct regulator_dev *rdev, -		struct regulator_dev **supply_rdev_ptr) +static int _regulator_force_disable(struct regulator_dev *rdev)  {  	int ret = 0; -	/* force disable */ -	if (rdev->desc->ops->disable) { -		/* ah well, who wants to live forever... */ -		ret = rdev->desc->ops->disable(rdev); -		if (ret < 0) { -			printk(KERN_ERR "%s: failed to force disable %s\n", -			       __func__, rdev_get_name(rdev)); -			return ret; -		} -		/* notify other consumers that power has been forced off */ -		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | -			REGULATOR_EVENT_DISABLE, NULL); +	ret = _regulator_do_disable(rdev); +	if (ret < 0) { +		rdev_err(rdev, "failed to force disable\n"); +		return ret;  	} -	/* decrease our supplies ref count and disable if required */ -	*supply_rdev_ptr = rdev->supply; +	_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | +			REGULATOR_EVENT_DISABLE, NULL); -	rdev->use_count = 0; -	return ret; +	return 0;  }  /** @@ -1472,23 +2015,97 @@ static int _regulator_force_disable(struct regulator_dev *rdev,   */  int regulator_force_disable(struct regulator *regulator)  { -	struct regulator_dev *supply_rdev = NULL; +	struct regulator_dev *rdev = regulator->rdev;  	int ret; -	mutex_lock(®ulator->rdev->mutex); +	mutex_lock(&rdev->mutex);  	regulator->uA_load = 0; -	ret = _regulator_force_disable(regulator->rdev, &supply_rdev); -	mutex_unlock(®ulator->rdev->mutex); +	ret = _regulator_force_disable(regulator->rdev); +	mutex_unlock(&rdev->mutex); -	if (supply_rdev) -		regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev))); +	if (rdev->supply) +		while (rdev->open_count--) +			regulator_disable(rdev->supply);  	return ret;  }  EXPORT_SYMBOL_GPL(regulator_force_disable); +static void regulator_disable_work(struct work_struct *work) +{ +	struct regulator_dev *rdev = container_of(work, struct regulator_dev, +						  disable_work.work); +	int count, i, ret; + +	mutex_lock(&rdev->mutex); + +	BUG_ON(!rdev->deferred_disables); + +	count = rdev->deferred_disables; +	rdev->deferred_disables = 0; + +	for (i = 0; i < count; i++) { +		ret = _regulator_disable(rdev); +		if (ret != 0) +			rdev_err(rdev, "Deferred disable failed: %d\n", ret); +	} + +	mutex_unlock(&rdev->mutex); + +	if (rdev->supply) { +		for (i = 0; i < count; i++) { +			ret = regulator_disable(rdev->supply); +			if (ret != 0) { +				rdev_err(rdev, +					 "Supply disable failed: %d\n", ret); +			} +		} +	} +} + +/** + * regulator_disable_deferred - disable regulator output with delay + * @regulator: regulator source + * @ms: miliseconds until the regulator is disabled + * + * Execute regulator_disable() on the regulator after a delay.  This + * is intended for use with devices that require some time to quiesce. + * + * NOTE: this will only disable the regulator output if no other consumer + * devices have it enabled, the regulator device supports disabling and + * machine constraints permit this operation. + */ +int regulator_disable_deferred(struct regulator *regulator, int ms) +{ +	struct regulator_dev *rdev = regulator->rdev; +	int ret; + +	if (regulator->always_on) +		return 0; + +	if (!ms) +		return regulator_disable(regulator); + +	mutex_lock(&rdev->mutex); +	rdev->deferred_disables++; +	mutex_unlock(&rdev->mutex); + +	ret = queue_delayed_work(system_power_efficient_wq, +				 &rdev->disable_work, +				 msecs_to_jiffies(ms)); +	if (ret < 0) +		return ret; +	else +		return 0; +} +EXPORT_SYMBOL_GPL(regulator_disable_deferred); +  static int _regulator_is_enabled(struct regulator_dev *rdev)  { +	/* A GPIO control always takes precedence */ +	if (rdev->ena_pin) +		return rdev->ena_gpio_state; +  	/* If we don't know then assume that the regulator is always on */  	if (!rdev->desc->ops->is_enabled)  		return 1; @@ -1512,6 +2129,9 @@ int regulator_is_enabled(struct regulator *regulator)  {  	int ret; +	if (regulator->always_on) +		return 1; +  	mutex_lock(®ulator->rdev->mutex);  	ret = _regulator_is_enabled(regulator->rdev);  	mutex_unlock(®ulator->rdev->mutex); @@ -1521,6 +2141,34 @@ int regulator_is_enabled(struct regulator *regulator)  EXPORT_SYMBOL_GPL(regulator_is_enabled);  /** + * regulator_can_change_voltage - check if regulator can change voltage + * @regulator: regulator source + * + * Returns positive if the regulator driver backing the source/client + * can change its voltage, false otherwise. Useful for detecting fixed + * or dummy regulators and disabling voltage change logic in the client + * driver. + */ +int regulator_can_change_voltage(struct regulator *regulator) +{ +	struct regulator_dev	*rdev = regulator->rdev; + +	if (rdev->constraints && +	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { +		if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1) +			return 1; + +		if (rdev->desc->continuous_voltage_range && +		    rdev->constraints->min_uV && rdev->constraints->max_uV && +		    rdev->constraints->min_uV != rdev->constraints->max_uV) +			return 1; +	} + +	return 0; +} +EXPORT_SYMBOL_GPL(regulator_can_change_voltage); + +/**   * regulator_count_voltages - count regulator_list_voltage() selectors   * @regulator: regulator source   * @@ -1552,6 +2200,9 @@ int regulator_list_voltage(struct regulator *regulator, unsigned selector)  	struct regulator_ops	*ops = rdev->desc->ops;  	int			ret; +	if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) +		return rdev->desc->fixed_uV; +  	if (!ops->list_voltage || selector >= rdev->desc->n_voltages)  		return -EINVAL; @@ -1571,6 +2222,21 @@ int regulator_list_voltage(struct regulator *regulator, unsigned selector)  EXPORT_SYMBOL_GPL(regulator_list_voltage);  /** + * regulator_get_linear_step - return the voltage step size between VSEL values + * @regulator: regulator source + * + * Returns the voltage step size between VSEL values for linear + * regulators, or return 0 if the regulator isn't a linear regulator. + */ +unsigned int regulator_get_linear_step(struct regulator *regulator) +{ +	struct regulator_dev *rdev = regulator->rdev; + +	return rdev->desc->uV_step; +} +EXPORT_SYMBOL_GPL(regulator_get_linear_step); + +/**   * regulator_is_supported_voltage - check if a voltage range can be supported   *   * @regulator: Regulator to check. @@ -1582,8 +2248,23 @@ EXPORT_SYMBOL_GPL(regulator_list_voltage);  int regulator_is_supported_voltage(struct regulator *regulator,  				   int min_uV, int max_uV)  { +	struct regulator_dev *rdev = regulator->rdev;  	int i, voltages, ret; +	/* If we can't change voltage check the current voltage */ +	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { +		ret = regulator_get_voltage(regulator); +		if (ret >= 0) +			return min_uV <= ret && ret <= max_uV; +		else +			return ret; +	} + +	/* Any voltage within constrains range is fine? */ +	if (rdev->desc->continuous_voltage_range) +		return min_uV >= rdev->constraints->min_uV && +				max_uV <= rdev->constraints->max_uV; +  	ret = regulator_count_voltages(regulator);  	if (ret < 0)  		return ret; @@ -1598,6 +2279,113 @@ int regulator_is_supported_voltage(struct regulator *regulator,  	return 0;  } +EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); + +static int _regulator_do_set_voltage(struct regulator_dev *rdev, +				     int min_uV, int max_uV) +{ +	int ret; +	int delay = 0; +	int best_val = 0; +	unsigned int selector; +	int old_selector = -1; + +	trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); + +	min_uV += rdev->constraints->uV_offset; +	max_uV += rdev->constraints->uV_offset; + +	/* +	 * If we can't obtain the old selector there is not enough +	 * info to call set_voltage_time_sel(). +	 */ +	if (_regulator_is_enabled(rdev) && +	    rdev->desc->ops->set_voltage_time_sel && +	    rdev->desc->ops->get_voltage_sel) { +		old_selector = rdev->desc->ops->get_voltage_sel(rdev); +		if (old_selector < 0) +			return old_selector; +	} + +	if (rdev->desc->ops->set_voltage) { +		ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, +						   &selector); + +		if (ret >= 0) { +			if (rdev->desc->ops->list_voltage) +				best_val = rdev->desc->ops->list_voltage(rdev, +									 selector); +			else +				best_val = _regulator_get_voltage(rdev); +		} + +	} else if (rdev->desc->ops->set_voltage_sel) { +		if (rdev->desc->ops->map_voltage) { +			ret = rdev->desc->ops->map_voltage(rdev, min_uV, +							   max_uV); +		} else { +			if (rdev->desc->ops->list_voltage == +			    regulator_list_voltage_linear) +				ret = regulator_map_voltage_linear(rdev, +								min_uV, max_uV); +			else if (rdev->desc->ops->list_voltage == +				 regulator_list_voltage_linear_range) +				ret = regulator_map_voltage_linear_range(rdev, +								min_uV, max_uV); +			else +				ret = regulator_map_voltage_iterate(rdev, +								min_uV, max_uV); +		} + +		if (ret >= 0) { +			best_val = rdev->desc->ops->list_voltage(rdev, ret); +			if (min_uV <= best_val && max_uV >= best_val) { +				selector = ret; +				if (old_selector == selector) +					ret = 0; +				else +					ret = rdev->desc->ops->set_voltage_sel( +								rdev, ret); +			} else { +				ret = -EINVAL; +			} +		} +	} else { +		ret = -EINVAL; +	} + +	/* Call set_voltage_time_sel if successfully obtained old_selector */ +	if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0 +		&& old_selector != selector) { + +		delay = rdev->desc->ops->set_voltage_time_sel(rdev, +						old_selector, selector); +		if (delay < 0) { +			rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", +				  delay); +			delay = 0; +		} + +		/* Insert any necessary delays */ +		if (delay >= 1000) { +			mdelay(delay / 1000); +			udelay(delay % 1000); +		} else if (delay) { +			udelay(delay); +		} +	} + +	if (ret == 0 && best_val >= 0) { +		unsigned long data = best_val; + +		_notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, +				     (void *)data); +	} + +	trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val); + +	return ret; +}  /**   * regulator_set_voltage - set regulator output voltage @@ -1620,12 +2408,35 @@ int regulator_is_supported_voltage(struct regulator *regulator,  int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)  {  	struct regulator_dev *rdev = regulator->rdev; -	int ret; +	int ret = 0; +	int old_min_uV, old_max_uV; +	int current_uV;  	mutex_lock(&rdev->mutex); +	/* If we're setting the same range as last time the change +	 * should be a noop (some cpufreq implementations use the same +	 * voltage for multiple frequencies, for example). +	 */ +	if (regulator->min_uV == min_uV && regulator->max_uV == max_uV) +		goto out; + +	/* If we're trying to set a range that overlaps the current voltage, +	 * return succesfully even though the regulator does not support +	 * changing the voltage. +	 */ +	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { +		current_uV = _regulator_get_voltage(rdev); +		if (min_uV <= current_uV && current_uV <= max_uV) { +			regulator->min_uV = min_uV; +			regulator->max_uV = max_uV; +			goto out; +		} +	} +  	/* sanity check */ -	if (!rdev->desc->ops->set_voltage) { +	if (!rdev->desc->ops->set_voltage && +	    !rdev->desc->ops->set_voltage_sel) {  		ret = -EINVAL;  		goto out;  	} @@ -1634,24 +2445,186 @@ int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)  	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);  	if (ret < 0)  		goto out; + +	/* restore original values in case of error */ +	old_min_uV = regulator->min_uV; +	old_max_uV = regulator->max_uV;  	regulator->min_uV = min_uV;  	regulator->max_uV = max_uV; -	ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV); + +	ret = regulator_check_consumers(rdev, &min_uV, &max_uV); +	if (ret < 0) +		goto out2; + +	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); +	if (ret < 0) +		goto out2;  out: -	_notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL); +	mutex_unlock(&rdev->mutex); +	return ret; +out2: +	regulator->min_uV = old_min_uV; +	regulator->max_uV = old_max_uV;  	mutex_unlock(&rdev->mutex);  	return ret;  }  EXPORT_SYMBOL_GPL(regulator_set_voltage); -static int _regulator_get_voltage(struct regulator_dev *rdev) +/** + * regulator_set_voltage_time - get raise/fall time + * @regulator: regulator source + * @old_uV: starting voltage in microvolts + * @new_uV: target voltage in microvolts + * + * Provided with the starting and ending voltage, this function attempts to + * calculate the time in microseconds required to rise or fall to this new + * voltage. + */ +int regulator_set_voltage_time(struct regulator *regulator, +			       int old_uV, int new_uV)  { +	struct regulator_dev	*rdev = regulator->rdev; +	struct regulator_ops	*ops = rdev->desc->ops; +	int old_sel = -1; +	int new_sel = -1; +	int voltage; +	int i; + +	/* Currently requires operations to do this */ +	if (!ops->list_voltage || !ops->set_voltage_time_sel +	    || !rdev->desc->n_voltages) +		return -EINVAL; + +	for (i = 0; i < rdev->desc->n_voltages; i++) { +		/* We only look for exact voltage matches here */ +		voltage = regulator_list_voltage(regulator, i); +		if (voltage < 0) +			return -EINVAL; +		if (voltage == 0) +			continue; +		if (voltage == old_uV) +			old_sel = i; +		if (voltage == new_uV) +			new_sel = i; +	} + +	if (old_sel < 0 || new_sel < 0) +		return -EINVAL; + +	return ops->set_voltage_time_sel(rdev, old_sel, new_sel); +} +EXPORT_SYMBOL_GPL(regulator_set_voltage_time); + +/** + * regulator_set_voltage_time_sel - get raise/fall time + * @rdev: regulator source device + * @old_selector: selector for starting voltage + * @new_selector: selector for target voltage + * + * Provided with the starting and target voltage selectors, this function + * returns time in microseconds required to rise or fall to this new voltage + * + * Drivers providing ramp_delay in regulation_constraints can use this as their + * set_voltage_time_sel() operation. + */ +int regulator_set_voltage_time_sel(struct regulator_dev *rdev, +				   unsigned int old_selector, +				   unsigned int new_selector) +{ +	unsigned int ramp_delay = 0; +	int old_volt, new_volt; + +	if (rdev->constraints->ramp_delay) +		ramp_delay = rdev->constraints->ramp_delay; +	else if (rdev->desc->ramp_delay) +		ramp_delay = rdev->desc->ramp_delay; + +	if (ramp_delay == 0) { +		rdev_warn(rdev, "ramp_delay not set\n"); +		return 0; +	} +  	/* sanity check */ -	if (rdev->desc->ops->get_voltage) -		return rdev->desc->ops->get_voltage(rdev); -	else +	if (!rdev->desc->ops->list_voltage) +		return -EINVAL; + +	old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); +	new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); + +	return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay); +} +EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel); + +/** + * regulator_sync_voltage - re-apply last regulator output voltage + * @regulator: regulator source + * + * Re-apply the last configured voltage.  This is intended to be used + * where some external control source the consumer is cooperating with + * has caused the configured voltage to change. + */ +int regulator_sync_voltage(struct regulator *regulator) +{ +	struct regulator_dev *rdev = regulator->rdev; +	int ret, min_uV, max_uV; + +	mutex_lock(&rdev->mutex); + +	if (!rdev->desc->ops->set_voltage && +	    !rdev->desc->ops->set_voltage_sel) { +		ret = -EINVAL; +		goto out; +	} + +	/* This is only going to work if we've had a voltage configured. */ +	if (!regulator->min_uV && !regulator->max_uV) { +		ret = -EINVAL; +		goto out; +	} + +	min_uV = regulator->min_uV; +	max_uV = regulator->max_uV; + +	/* This should be a paranoia check... */ +	ret = regulator_check_voltage(rdev, &min_uV, &max_uV); +	if (ret < 0) +		goto out; + +	ret = regulator_check_consumers(rdev, &min_uV, &max_uV); +	if (ret < 0) +		goto out; + +	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); + +out: +	mutex_unlock(&rdev->mutex); +	return ret; +} +EXPORT_SYMBOL_GPL(regulator_sync_voltage); + +static int _regulator_get_voltage(struct regulator_dev *rdev) +{ +	int sel, ret; + +	if (rdev->desc->ops->get_voltage_sel) { +		sel = rdev->desc->ops->get_voltage_sel(rdev); +		if (sel < 0) +			return sel; +		ret = rdev->desc->ops->list_voltage(rdev, sel); +	} else if (rdev->desc->ops->get_voltage) { +		ret = rdev->desc->ops->get_voltage(rdev); +	} else if (rdev->desc->ops->list_voltage) { +		ret = rdev->desc->ops->list_voltage(rdev, 0); +	} else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { +		ret = rdev->desc->fixed_uV; +	} else {  		return -EINVAL; +	} + +	if (ret < 0) +		return ret; +	return ret - rdev->constraints->uV_offset;  }  /** @@ -1680,7 +2653,7 @@ EXPORT_SYMBOL_GPL(regulator_get_voltage);  /**   * regulator_set_current_limit - set regulator output current limit   * @regulator: regulator source - * @min_uA: Minimuum supported current in uA + * @min_uA: Minimum supported current in uA   * @max_uA: Maximum supported current in uA   *   * Sets current sink to the desired output current. This can be set during @@ -1787,7 +2760,7 @@ int regulator_set_mode(struct regulator *regulator, unsigned int mode)  	}  	/* constraints check */ -	ret = regulator_check_mode(rdev, mode); +	ret = regulator_mode_constrain(rdev, &mode);  	if (ret < 0)  		goto out; @@ -1858,37 +2831,49 @@ int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)  {  	struct regulator_dev *rdev = regulator->rdev;  	struct regulator *consumer; -	int ret, output_uV, input_uV, total_uA_load = 0; +	int ret, output_uV, input_uV = 0, total_uA_load = 0;  	unsigned int mode; +	if (rdev->supply) +		input_uV = regulator_get_voltage(rdev->supply); +  	mutex_lock(&rdev->mutex); +	/* +	 * first check to see if we can set modes at all, otherwise just +	 * tell the consumer everything is OK. +	 */  	regulator->uA_load = uA_load;  	ret = regulator_check_drms(rdev); -	if (ret < 0) +	if (ret < 0) { +		ret = 0;  		goto out; -	ret = -EINVAL; +	} -	/* sanity check */  	if (!rdev->desc->ops->get_optimum_mode)  		goto out; +	/* +	 * we can actually do this so any errors are indicators of +	 * potential real failure. +	 */ +	ret = -EINVAL; + +	if (!rdev->desc->ops->set_mode) +		goto out; +  	/* get output voltage */ -	output_uV = rdev->desc->ops->get_voltage(rdev); +	output_uV = _regulator_get_voltage(rdev);  	if (output_uV <= 0) { -		printk(KERN_ERR "%s: invalid output voltage found for %s\n", -			__func__, rdev_get_name(rdev)); +		rdev_err(rdev, "invalid output voltage found\n");  		goto out;  	} -	/* get input voltage */ -	if (rdev->supply && rdev->supply->desc->ops->get_voltage) -		input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); -	else +	/* No supply? Use constraint voltage */ +	if (input_uV <= 0)  		input_uV = rdev->constraints->input_uV;  	if (input_uV <= 0) { -		printk(KERN_ERR "%s: invalid input voltage found for %s\n", -			__func__, rdev_get_name(rdev)); +		rdev_err(rdev, "invalid input voltage found\n");  		goto out;  	} @@ -1899,18 +2884,16 @@ int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)  	mode = rdev->desc->ops->get_optimum_mode(rdev,  						 input_uV, output_uV,  						 total_uA_load); -	ret = regulator_check_mode(rdev, mode); +	ret = regulator_mode_constrain(rdev, &mode);  	if (ret < 0) { -		printk(KERN_ERR "%s: failed to get optimum mode for %s @" -			" %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev), -			total_uA_load, input_uV, output_uV); +		rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", +			 total_uA_load, input_uV, output_uV);  		goto out;  	}  	ret = rdev->desc->ops->set_mode(rdev, mode);  	if (ret < 0) { -		printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n", -			__func__, mode, rdev_get_name(rdev)); +		rdev_err(rdev, "failed to set optimum mode %x\n", mode);  		goto out;  	}  	ret = mode; @@ -1921,6 +2904,59 @@ out:  EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);  /** + * regulator_allow_bypass - allow the regulator to go into bypass mode + * + * @regulator: Regulator to configure + * @enable: enable or disable bypass mode + * + * Allow the regulator to go into bypass mode if all other consumers + * for the regulator also enable bypass mode and the machine + * constraints allow this.  Bypass mode means that the regulator is + * simply passing the input directly to the output with no regulation. + */ +int regulator_allow_bypass(struct regulator *regulator, bool enable) +{ +	struct regulator_dev *rdev = regulator->rdev; +	int ret = 0; + +	if (!rdev->desc->ops->set_bypass) +		return 0; + +	if (rdev->constraints && +	    !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS)) +		return 0; + +	mutex_lock(&rdev->mutex); + +	if (enable && !regulator->bypass) { +		rdev->bypass_count++; + +		if (rdev->bypass_count == rdev->open_count) { +			ret = rdev->desc->ops->set_bypass(rdev, enable); +			if (ret != 0) +				rdev->bypass_count--; +		} + +	} else if (!enable && regulator->bypass) { +		rdev->bypass_count--; + +		if (rdev->bypass_count != rdev->open_count) { +			ret = rdev->desc->ops->set_bypass(rdev, enable); +			if (ret != 0) +				rdev->bypass_count++; +		} +	} + +	if (ret == 0) +		regulator->bypass = enable; + +	mutex_unlock(&rdev->mutex); + +	return ret; +} +EXPORT_SYMBOL_GPL(regulator_allow_bypass); + +/**   * regulator_register_notifier - register regulator event notifier   * @regulator: regulator source   * @nb: notifier block @@ -1956,17 +2992,8 @@ EXPORT_SYMBOL_GPL(regulator_unregister_notifier);  static void _notifier_call_chain(struct regulator_dev *rdev,  				  unsigned long event, void *data)  { -	struct regulator_dev *_rdev; -  	/* call rdev chain first */ -	blocking_notifier_call_chain(&rdev->notifier, event, NULL); - -	/* now notify regulator we supply */ -	list_for_each_entry(_rdev, &rdev->supply_list, slist) { -		mutex_lock(&_rdev->mutex); -		_notifier_call_chain(_rdev, event, data); -		mutex_unlock(&_rdev->mutex); -	} +	blocking_notifier_call_chain(&rdev->notifier, event, data);  }  /** @@ -2007,13 +3034,20 @@ int regulator_bulk_get(struct device *dev, int num_consumers,  	return 0;  err: -	for (i = 0; i < num_consumers && consumers[i].consumer; i++) +	while (--i >= 0)  		regulator_put(consumers[i].consumer);  	return ret;  }  EXPORT_SYMBOL_GPL(regulator_bulk_get); +static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) +{ +	struct regulator_bulk_data *bulk = data; + +	bulk->ret = regulator_enable(bulk->consumer); +} +  /**   * regulator_bulk_enable - enable multiple regulator consumers   * @@ -2029,21 +3063,38 @@ EXPORT_SYMBOL_GPL(regulator_bulk_get);  int regulator_bulk_enable(int num_consumers,  			  struct regulator_bulk_data *consumers)  { +	ASYNC_DOMAIN_EXCLUSIVE(async_domain);  	int i; -	int ret; +	int ret = 0;  	for (i = 0; i < num_consumers; i++) { -		ret = regulator_enable(consumers[i].consumer); -		if (ret != 0) +		if (consumers[i].consumer->always_on) +			consumers[i].ret = 0; +		else +			async_schedule_domain(regulator_bulk_enable_async, +					      &consumers[i], &async_domain); +	} + +	async_synchronize_full_domain(&async_domain); + +	/* If any consumer failed we need to unwind any that succeeded */ +	for (i = 0; i < num_consumers; i++) { +		if (consumers[i].ret != 0) { +			ret = consumers[i].ret;  			goto err; +		}  	}  	return 0;  err: -	printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret); -	for (--i; i >= 0; --i) -		regulator_disable(consumers[i].consumer); +	for (i = 0; i < num_consumers; i++) { +		if (consumers[i].ret < 0) +			pr_err("Failed to enable %s: %d\n", consumers[i].supply, +			       consumers[i].ret); +		else +			regulator_disable(consumers[i].consumer); +	}  	return ret;  } @@ -2057,17 +3108,17 @@ EXPORT_SYMBOL_GPL(regulator_bulk_enable);   * @return         0 on success, an errno on failure   *   * This convenience API allows consumers to disable multiple regulator - * clients in a single API call.  If any consumers cannot be enabled - * then any others that were disabled will be disabled again prior to + * clients in a single API call.  If any consumers cannot be disabled + * then any others that were disabled will be enabled again prior to   * return.   */  int regulator_bulk_disable(int num_consumers,  			   struct regulator_bulk_data *consumers)  {  	int i; -	int ret; +	int ret, r; -	for (i = 0; i < num_consumers; i++) { +	for (i = num_consumers - 1; i >= 0; --i) {  		ret = regulator_disable(consumers[i].consumer);  		if (ret != 0)  			goto err; @@ -2076,16 +3127,56 @@ int regulator_bulk_disable(int num_consumers,  	return 0;  err: -	printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply, -	       ret); -	for (--i; i >= 0; --i) -		regulator_enable(consumers[i].consumer); +	pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); +	for (++i; i < num_consumers; ++i) { +		r = regulator_enable(consumers[i].consumer); +		if (r != 0) +			pr_err("Failed to reename %s: %d\n", +			       consumers[i].supply, r); +	}  	return ret;  }  EXPORT_SYMBOL_GPL(regulator_bulk_disable);  /** + * regulator_bulk_force_disable - force disable multiple regulator consumers + * + * @num_consumers: Number of consumers + * @consumers:     Consumer data; clients are stored here. + * @return         0 on success, an errno on failure + * + * This convenience API allows consumers to forcibly disable multiple regulator + * clients in a single API call. + * NOTE: This should be used for situations when device damage will + * likely occur if the regulators are not disabled (e.g. over temp). + * Although regulator_force_disable function call for some consumers can + * return error numbers, the function is called for all consumers. + */ +int regulator_bulk_force_disable(int num_consumers, +			   struct regulator_bulk_data *consumers) +{ +	int i; +	int ret; + +	for (i = 0; i < num_consumers; i++) +		consumers[i].ret = +			    regulator_force_disable(consumers[i].consumer); + +	for (i = 0; i < num_consumers; i++) { +		if (consumers[i].ret != 0) { +			ret = consumers[i].ret; +			goto out; +		} +	} + +	return 0; +out: +	return ret; +} +EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); + +/**   * regulator_bulk_free - free multiple regulator consumers   *   * @num_consumers: Number of consumers @@ -2141,10 +3232,10 @@ int regulator_mode_to_status(unsigned int mode)  		return REGULATOR_STATUS_NORMAL;  	case REGULATOR_MODE_IDLE:  		return REGULATOR_STATUS_IDLE; -	case REGULATOR_STATUS_STANDBY: +	case REGULATOR_MODE_STANDBY:  		return REGULATOR_STATUS_STANDBY;  	default: -		return 0; +		return REGULATOR_STATUS_UNDEFINED;  	}  }  EXPORT_SYMBOL_GPL(regulator_mode_to_status); @@ -2160,7 +3251,10 @@ static int add_regulator_attributes(struct regulator_dev *rdev)  	int			status = 0;  	/* some attributes need specific methods to be displayed */ -	if (ops->get_voltage) { +	if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || +	    (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || +	    (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || +		(rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1))) {  		status = device_create_file(dev, &dev_attr_microvolts);  		if (status < 0)  			return status; @@ -2175,7 +3269,7 @@ static int add_regulator_attributes(struct regulator_dev *rdev)  		if (status < 0)  			return status;  	} -	if (ops->is_enabled) { +	if (rdev->ena_pin || ops->is_enabled) {  		status = device_create_file(dev, &dev_attr_state);  		if (status < 0)  			return status; @@ -2185,6 +3279,11 @@ static int add_regulator_attributes(struct regulator_dev *rdev)  		if (status < 0)  			return status;  	} +	if (ops->get_bypass) { +		status = device_create_file(dev, &dev_attr_bypass); +		if (status < 0) +			return status; +	}  	/* some attributes are type-specific */  	if (rdev->desc->type == REGULATOR_CURRENT) { @@ -2201,7 +3300,7 @@ static int add_regulator_attributes(struct regulator_dev *rdev)  		return status;  	/* constraints need specific supporting methods */ -	if (ops->set_voltage) { +	if (ops->set_voltage || ops->set_voltage_sel) {  		status = device_create_file(dev, &dev_attr_min_microvolts);  		if (status < 0)  			return status; @@ -2218,10 +3317,6 @@ static int add_regulator_attributes(struct regulator_dev *rdev)  			return status;  	} -	/* suspend mode constraints need multiple supporting methods */ -	if (!(ops->set_suspend_enable && ops->set_suspend_disable)) -		return status; -  	status = device_create_file(dev, &dev_attr_suspend_standby_state);  	if (status < 0)  		return status; @@ -2265,27 +3360,49 @@ static int add_regulator_attributes(struct regulator_dev *rdev)  	return status;  } +static void rdev_init_debugfs(struct regulator_dev *rdev) +{ +	rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root); +	if (!rdev->debugfs) { +		rdev_warn(rdev, "Failed to create debugfs directory\n"); +		return; +	} + +	debugfs_create_u32("use_count", 0444, rdev->debugfs, +			   &rdev->use_count); +	debugfs_create_u32("open_count", 0444, rdev->debugfs, +			   &rdev->open_count); +	debugfs_create_u32("bypass_count", 0444, rdev->debugfs, +			   &rdev->bypass_count); +} +  /**   * regulator_register - register regulator   * @regulator_desc: regulator to register - * @dev: struct device for the regulator - * @init_data: platform provided init data, passed through by driver - * @driver_data: private regulator data + * @config: runtime configuration for regulator   *   * Called by regulator drivers to register a regulator. - * Returns 0 on success. + * Returns a valid pointer to struct regulator_dev on success + * or an ERR_PTR() on error.   */ -struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, -	struct device *dev, struct regulator_init_data *init_data, -	void *driver_data) +struct regulator_dev * +regulator_register(const struct regulator_desc *regulator_desc, +		   const struct regulator_config *config)  { +	const struct regulation_constraints *constraints = NULL; +	const struct regulator_init_data *init_data;  	static atomic_t regulator_no = ATOMIC_INIT(0);  	struct regulator_dev *rdev; +	struct device *dev;  	int ret, i; +	const char *supply = NULL; -	if (regulator_desc == NULL) +	if (regulator_desc == NULL || config == NULL)  		return ERR_PTR(-EINVAL); +	dev = config->dev; +	WARN_ON(!dev); +  	if (regulator_desc->name == NULL || regulator_desc->ops == NULL)  		return ERR_PTR(-EINVAL); @@ -2293,8 +3410,23 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,  	    regulator_desc->type != REGULATOR_CURRENT)  		return ERR_PTR(-EINVAL); -	if (!init_data) +	/* Only one of each should be implemented */ +	WARN_ON(regulator_desc->ops->get_voltage && +		regulator_desc->ops->get_voltage_sel); +	WARN_ON(regulator_desc->ops->set_voltage && +		regulator_desc->ops->set_voltage_sel); + +	/* If we're using selectors we must implement list_voltage. */ +	if (regulator_desc->ops->get_voltage_sel && +	    !regulator_desc->ops->list_voltage) { +		return ERR_PTR(-EINVAL); +	} +	if (regulator_desc->ops->set_voltage_sel && +	    !regulator_desc->ops->list_voltage) {  		return ERR_PTR(-EINVAL); +	} + +	init_data = config->init_data;  	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);  	if (rdev == NULL) @@ -2303,17 +3435,22 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,  	mutex_lock(®ulator_list_mutex);  	mutex_init(&rdev->mutex); -	rdev->reg_data = driver_data; +	rdev->reg_data = config->driver_data;  	rdev->owner = regulator_desc->owner;  	rdev->desc = regulator_desc; +	if (config->regmap) +		rdev->regmap = config->regmap; +	else if (dev_get_regmap(dev, NULL)) +		rdev->regmap = dev_get_regmap(dev, NULL); +	else if (dev->parent) +		rdev->regmap = dev_get_regmap(dev->parent, NULL);  	INIT_LIST_HEAD(&rdev->consumer_list); -	INIT_LIST_HEAD(&rdev->supply_list);  	INIT_LIST_HEAD(&rdev->list); -	INIT_LIST_HEAD(&rdev->slist);  	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); +	INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);  	/* preform any regulator specific init */ -	if (init_data->regulator_init) { +	if (init_data && init_data->regulator_init) {  		ret = init_data->regulator_init(rdev->reg_data);  		if (ret < 0)  			goto clean; @@ -2321,6 +3458,7 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,  	/* register with sysfs */  	rdev->dev.class = ®ulator_class; +	rdev->dev.of_node = of_node_get(config->of_node);  	rdev->dev.parent = dev;  	dev_set_name(&rdev->dev, "regulator.%d",  		     atomic_inc_return(®ulator_no) - 1); @@ -2332,8 +3470,26 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,  	dev_set_drvdata(&rdev->dev, rdev); +	if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) { +		ret = regulator_ena_gpio_request(rdev, config); +		if (ret != 0) { +			rdev_err(rdev, "Failed to request enable GPIO%d: %d\n", +				 config->ena_gpio, ret); +			goto wash; +		} + +		if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH) +			rdev->ena_gpio_state = 1; + +		if (config->ena_gpio_invert) +			rdev->ena_gpio_state = !rdev->ena_gpio_state; +	} +  	/* set regulator constraints */ -	ret = set_machine_constraints(rdev, &init_data->constraints); +	if (init_data) +		constraints = &init_data->constraints; + +	ret = set_machine_constraints(rdev, constraints);  	if (ret < 0)  		goto scrub; @@ -2342,55 +3498,59 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,  	if (ret < 0)  		goto scrub; -	/* set supply regulator if it exists */ -	if (init_data->supply_regulator && init_data->supply_regulator_dev) { -		dev_err(dev, -			"Supply regulator specified by both name and dev\n"); -		goto scrub; -	} +	if (init_data && init_data->supply_regulator) +		supply = init_data->supply_regulator; +	else if (regulator_desc->supply_name) +		supply = regulator_desc->supply_name; -	if (init_data->supply_regulator) { +	if (supply) {  		struct regulator_dev *r; -		int found = 0; -		list_for_each_entry(r, ®ulator_list, list) { -			if (strcmp(rdev_get_name(r), -				   init_data->supply_regulator) == 0) { -				found = 1; -				break; -			} -		} +		r = regulator_dev_lookup(dev, supply, &ret); -		if (!found) { -			dev_err(dev, "Failed to find supply %s\n", -				init_data->supply_regulator); +		if (ret == -ENODEV) { +			/* +			 * No supply was specified for this regulator and +			 * there will never be one. +			 */ +			ret = 0; +			goto add_dev; +		} else if (!r) { +			dev_err(dev, "Failed to find supply %s\n", supply); +			ret = -EPROBE_DEFER;  			goto scrub;  		}  		ret = set_supply(rdev, r);  		if (ret < 0)  			goto scrub; -	} -	if (init_data->supply_regulator_dev) { -		dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n"); -		ret = set_supply(rdev, -			dev_get_drvdata(init_data->supply_regulator_dev)); -		if (ret < 0) -			goto scrub; +		/* Enable supply if rail is enabled */ +		if (_regulator_is_enabled(rdev)) { +			ret = regulator_enable(rdev->supply); +			if (ret < 0) +				goto scrub; +		}  	} +add_dev:  	/* add consumers devices */ -	for (i = 0; i < init_data->num_consumer_supplies; i++) { -		ret = set_consumer_device_supply(rdev, -			init_data->consumer_supplies[i].dev, -			init_data->consumer_supplies[i].dev_name, -			init_data->consumer_supplies[i].supply); -		if (ret < 0) -			goto unset_supplies; +	if (init_data) { +		for (i = 0; i < init_data->num_consumer_supplies; i++) { +			ret = set_consumer_device_supply(rdev, +				init_data->consumer_supplies[i].dev_name, +				init_data->consumer_supplies[i].supply); +			if (ret < 0) { +				dev_err(dev, "Failed to set supply %s\n", +					init_data->consumer_supplies[i].supply); +				goto unset_supplies; +			} +		}  	}  	list_add(&rdev->list, ®ulator_list); + +	rdev_init_debugfs(rdev);  out:  	mutex_unlock(®ulator_list_mutex);  	return rdev; @@ -2399,6 +3559,11 @@ unset_supplies:  	unset_regulator_supplies(rdev);  scrub: +	if (rdev->supply) +		_regulator_put(rdev->supply); +	regulator_ena_gpio_free(rdev); +	kfree(rdev->constraints); +wash:  	device_unregister(&rdev->dev);  	/* device core frees rdev */  	rdev = ERR_PTR(ret); @@ -2422,12 +3587,20 @@ void regulator_unregister(struct regulator_dev *rdev)  	if (rdev == NULL)  		return; +	if (rdev->supply) { +		while (rdev->use_count--) +			regulator_disable(rdev->supply); +		regulator_put(rdev->supply); +	}  	mutex_lock(®ulator_list_mutex); +	debugfs_remove_recursive(rdev->debugfs); +	flush_work(&rdev->disable_work.work);  	WARN_ON(rdev->open_count);  	unset_regulator_supplies(rdev);  	list_del(&rdev->list); -	if (rdev->supply) -		sysfs_remove_link(&rdev->dev.kobj, "supply"); +	kfree(rdev->constraints); +	regulator_ena_gpio_free(rdev); +	of_node_put(rdev->dev.of_node);  	device_unregister(&rdev->dev);  	mutex_unlock(®ulator_list_mutex);  } @@ -2457,8 +3630,7 @@ int regulator_suspend_prepare(suspend_state_t state)  		mutex_unlock(&rdev->mutex);  		if (ret < 0) { -			printk(KERN_ERR "%s: failed to prepare %s\n", -				__func__, rdev_get_name(rdev)); +			rdev_err(rdev, "failed to prepare\n");  			goto out;  		}  	} @@ -2469,6 +3641,42 @@ out:  EXPORT_SYMBOL_GPL(regulator_suspend_prepare);  /** + * regulator_suspend_finish - resume regulators from system wide suspend + * + * Turn on regulators that might be turned off by regulator_suspend_prepare + * and that should be turned on according to the regulators properties. + */ +int regulator_suspend_finish(void) +{ +	struct regulator_dev *rdev; +	int ret = 0, error; + +	mutex_lock(®ulator_list_mutex); +	list_for_each_entry(rdev, ®ulator_list, list) { +		mutex_lock(&rdev->mutex); +		if (rdev->use_count > 0  || rdev->constraints->always_on) { +			error = _regulator_do_enable(rdev); +			if (error) +				ret = error; +		} else { +			if (!have_full_constraints()) +				goto unlock; +			if (!_regulator_is_enabled(rdev)) +				goto unlock; + +			error = _regulator_do_disable(rdev); +			if (error) +				ret = error; +		} +unlock: +		mutex_unlock(&rdev->mutex); +	} +	mutex_unlock(®ulator_list_mutex); +	return ret; +} +EXPORT_SYMBOL_GPL(regulator_suspend_finish); + +/**   * regulator_has_full_constraints - the system has fully specified constraints   *   * Calling this function will cause the regulator API to disable all @@ -2486,22 +3694,6 @@ void regulator_has_full_constraints(void)  EXPORT_SYMBOL_GPL(regulator_has_full_constraints);  /** - * regulator_use_dummy_regulator - Provide a dummy regulator when none is found - * - * Calling this function will cause the regulator API to provide a - * dummy regulator to consumers if no physical regulator is found, - * allowing most consumers to proceed as though a regulator were - * configured.  This allows systems such as those with software - * controllable regulators for the CPU core only to be brought up more - * readily. - */ -void regulator_use_dummy_regulator(void) -{ -	board_wants_dummy_regulator = true; -} -EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator); - -/**   * rdev_get_drvdata - get rdev regulator driver data   * @rdev: regulator   * @@ -2560,14 +3752,58 @@ void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)  }  EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); +#ifdef CONFIG_DEBUG_FS +static ssize_t supply_map_read_file(struct file *file, char __user *user_buf, +				    size_t count, loff_t *ppos) +{ +	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); +	ssize_t len, ret = 0; +	struct regulator_map *map; + +	if (!buf) +		return -ENOMEM; + +	list_for_each_entry(map, ®ulator_map_list, list) { +		len = snprintf(buf + ret, PAGE_SIZE - ret, +			       "%s -> %s.%s\n", +			       rdev_get_name(map->regulator), map->dev_name, +			       map->supply); +		if (len >= 0) +			ret += len; +		if (ret > PAGE_SIZE) { +			ret = PAGE_SIZE; +			break; +		} +	} + +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); + +	kfree(buf); + +	return ret; +} +#endif + +static const struct file_operations supply_map_fops = { +#ifdef CONFIG_DEBUG_FS +	.read = supply_map_read_file, +	.llseek = default_llseek, +#endif +}; +  static int __init regulator_init(void)  {  	int ret; -	printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION); -  	ret = class_register(®ulator_class); +	debugfs_root = debugfs_create_dir("regulator", NULL); +	if (!debugfs_root) +		pr_warn("regulator: Failed to create debugfs directory\n"); + +	debugfs_create_file("supply_map", 0444, debugfs_root, NULL, +			    &supply_map_fops); +  	regulator_dummy_init();  	return ret; @@ -2582,21 +3818,31 @@ static int __init regulator_init_complete(void)  	struct regulator_ops *ops;  	struct regulation_constraints *c;  	int enabled, ret; -	const char *name; + +	/* +	 * Since DT doesn't provide an idiomatic mechanism for +	 * enabling full constraints and since it's much more natural +	 * with DT to provide them just assume that a DT enabled +	 * system has full constraints. +	 */ +	if (of_have_populated_dt()) +		has_full_constraints = true;  	mutex_lock(®ulator_list_mutex);  	/* If we have a full configuration then disable any regulators -	 * which are not in use or always_on.  This will become the -	 * default behaviour in the future. +	 * we have permission to change the status for and which are +	 * not in use or always_on.  This is effectively the default +	 * for DT and ACPI as they have full constraints.  	 */  	list_for_each_entry(rdev, ®ulator_list, list) {  		ops = rdev->desc->ops;  		c = rdev->constraints; -		name = rdev_get_name(rdev); +		if (c && c->always_on) +			continue; -		if (!ops->disable || (c && c->always_on)) +		if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))  			continue;  		mutex_lock(&rdev->mutex); @@ -2613,26 +3859,20 @@ static int __init regulator_init_complete(void)  		if (!enabled)  			goto unlock; -		if (has_full_constraints) { +		if (have_full_constraints()) {  			/* We log since this may kill the system if it  			 * goes wrong. */ -			printk(KERN_INFO "%s: disabling %s\n", -			       __func__, name); -			ret = ops->disable(rdev); -			if (ret != 0) { -				printk(KERN_ERR -				       "%s: couldn't disable %s: %d\n", -				       __func__, name, ret); -			} +			rdev_info(rdev, "disabling\n"); +			ret = _regulator_do_disable(rdev); +			if (ret != 0) +				rdev_err(rdev, "couldn't disable: %d\n", ret);  		} else {  			/* The intention is that in future we will  			 * assume that full constraints are provided  			 * so warn even if we aren't going to do  			 * anything here.  			 */ -			printk(KERN_WARNING -			       "%s: incomplete constraints, leaving %s on\n", -			       __func__, name); +			rdev_warn(rdev, "incomplete constraints, leaving on\n");  		}  unlock: @@ -2643,4 +3883,4 @@ unlock:  	return 0;  } -late_initcall(regulator_init_complete); +late_initcall_sync(regulator_init_complete);  | 
