Commit 69511a45 authored by Rajendra Nayak's avatar Rajendra Nayak Committed by Mark Brown

regulator: map consumer regulator based on device tree

Device nodes in DT can associate themselves with one or more
regulators/supply by providing a list of phandles (to regulator nodes)
and corresponding supply names.

For Example:
	devicenode: node@0x0 {
		...
		...
		vmmc-supply = <&regulator1>;
		vpll-supply = <&regulator2>;
	};

The driver would then do a regulator_get(dev, "vmmc"); to get
regulator1 and do a regulator_get(dev, "vpll"); to get
regulator2.

of_get_regulator() extracts the regulator node for a given
device, based on the supply name.

Use it to look up the regulator for a given consumer from device tree, during
a regulator_get(). If not found fallback and lookup through
the regulator_map_list instead.

Also, since the regulator dt nodes can use the same binding to
associate with a parent regulator/supply, allow the drivers to
specify a supply_name, which can then be used to lookup dt
to find the parent phandle.
Signed-off-by: default avatarRajendra Nayak <rnayak@ti.com>
Acked-by: default avatarGrant Likely <grant.likely@secretlab.ca>
Signed-off-by: default avatarMark Brown <broonie@opensource.wolfsonmicro.com>
parent 2c043bcb
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/suspend.h> #include <linux/suspend.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/of.h>
#include <linux/regulator/of_regulator.h>
#include <linux/regulator/consumer.h> #include <linux/regulator/consumer.h>
#include <linux/regulator/driver.h> #include <linux/regulator/driver.h>
#include <linux/regulator/machine.h> #include <linux/regulator/machine.h>
...@@ -132,6 +134,33 @@ static struct regulator *get_device_regulator(struct device *dev) ...@@ -132,6 +134,33 @@ static struct regulator *get_device_regulator(struct device *dev)
return NULL; return NULL;
} }
/**
* 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.
* retruns 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_warn(dev, "%s property in node %s references invalid phandle",
prop_name, dev->of_node->full_name);
return NULL;
}
return regnode;
}
/* Platform voltage constraint check */ /* Platform voltage constraint check */
static int regulator_check_voltage(struct regulator_dev *rdev, static int regulator_check_voltage(struct regulator_dev *rdev,
int *min_uV, int *max_uV) int *min_uV, int *max_uV)
...@@ -1148,6 +1177,30 @@ static int _regulator_get_enable_time(struct regulator_dev *rdev) ...@@ -1148,6 +1177,30 @@ static int _regulator_get_enable_time(struct regulator_dev *rdev)
return rdev->desc->ops->enable_time(rdev); return rdev->desc->ops->enable_time(rdev);
} }
static struct regulator_dev *regulator_dev_lookup(struct device *dev,
const char *supply)
{
struct regulator_dev *r;
struct device_node *node;
/* first do a dt based lookup */
if (dev && dev->of_node) {
node = of_get_regulator(dev, supply);
if (node)
list_for_each_entry(r, &regulator_list, list)
if (r->dev.parent &&
node == r->dev.of_node)
return r;
}
/* if not found, try doing it non-dt way */
list_for_each_entry(r, &regulator_list, list)
if (strcmp(rdev_get_name(r), supply) == 0)
return r;
return NULL;
}
/* Internal regulator request function */ /* Internal regulator request function */
static struct regulator *_regulator_get(struct device *dev, const char *id, static struct regulator *_regulator_get(struct device *dev, const char *id,
int exclusive) int exclusive)
...@@ -1168,6 +1221,10 @@ static struct regulator *_regulator_get(struct device *dev, const char *id, ...@@ -1168,6 +1221,10 @@ static struct regulator *_regulator_get(struct device *dev, const char *id,
mutex_lock(&regulator_list_mutex); mutex_lock(&regulator_list_mutex);
rdev = regulator_dev_lookup(dev, id);
if (rdev)
goto found;
list_for_each_entry(map, &regulator_map_list, list) { list_for_each_entry(map, &regulator_map_list, list) {
/* If the mapping has a device set up it must match */ /* If the mapping has a device set up it must match */
if (map->dev_name && if (map->dev_name &&
...@@ -2642,6 +2699,7 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, ...@@ -2642,6 +2699,7 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
static atomic_t regulator_no = ATOMIC_INIT(0); static atomic_t regulator_no = ATOMIC_INIT(0);
struct regulator_dev *rdev; struct regulator_dev *rdev;
int ret, i; int ret, i;
const char *supply = NULL;
if (regulator_desc == NULL) if (regulator_desc == NULL)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
...@@ -2718,21 +2776,18 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, ...@@ -2718,21 +2776,18 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
if (ret < 0) if (ret < 0)
goto scrub; goto scrub;
if (init_data->supply_regulator) { if (init_data->supply_regulator)
supply = init_data->supply_regulator;
else if (regulator_desc->supply_name)
supply = regulator_desc->supply_name;
if (supply) {
struct regulator_dev *r; struct regulator_dev *r;
int found = 0;
list_for_each_entry(r, &regulator_list, list) { r = regulator_dev_lookup(dev, supply);
if (strcmp(rdev_get_name(r),
init_data->supply_regulator) == 0) {
found = 1;
break;
}
}
if (!found) { if (!r) {
dev_err(dev, "Failed to find supply %s\n", dev_err(dev, "Failed to find supply %s\n", supply);
init_data->supply_regulator);
ret = -ENODEV; ret = -ENODEV;
goto scrub; goto scrub;
} }
......
...@@ -154,6 +154,7 @@ enum regulator_type { ...@@ -154,6 +154,7 @@ enum regulator_type {
* this type. * this type.
* *
* @name: Identifying name for the regulator. * @name: Identifying name for the regulator.
* @supply_name: Identifying the regulator supply
* @id: Numerical identifier for the regulator. * @id: Numerical identifier for the regulator.
* @n_voltages: Number of selectors available for ops.list_voltage(). * @n_voltages: Number of selectors available for ops.list_voltage().
* @ops: Regulator operations table. * @ops: Regulator operations table.
...@@ -163,6 +164,7 @@ enum regulator_type { ...@@ -163,6 +164,7 @@ enum regulator_type {
*/ */
struct regulator_desc { struct regulator_desc {
const char *name; const char *name;
const char *supply_name;
int id; int id;
unsigned n_voltages; unsigned n_voltages;
struct regulator_ops *ops; struct regulator_ops *ops;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment