regulator: core: Add ability to create a lookup alias for supply
RFC PATCH 1/1 regulator: core: Add ability to create a lookup alias for supply
These patches add the ability to create an alternative device on which a lookup for a certain supply should be conducted.
A common use-case for this would be devices that are logically represented as a collection of drivers within Linux but are are presented as a single device from device tree. It this case it is necessary for each sub device to locate their supply data on the main device.
---
Hi Mark,
Following our discussions around the Arizona regulator bindings here is an effort at putting a mechanism into the regulator core to alias registering of a supply from one device to another.
I have a couple of patches that update the Arizona driver to use this that I will send in if you think this looks along the lines of what you were suggesting.
Thanks,
Charles
drivers/regulator/core.c | 93 ++++++++++++++++++++++++++++++++++++ include/linux/regulator/consumer.h | 17 +++++++ 2 files changed, 110 insertions(+), 0 deletions(-)
drivers/regulator/core.c +93 -0
@@ -53,6 +53,7 @@ static DEFINE_MUTEX(regulator_list_mutex);static LIST_HEAD (regulator_list);static LIST_HEAD (regulator_map_list);static LIST_HEAD (regulator_ena_gpio_list); +static LIST_HEAD (regulator_supply_alias_list);static bool has_full_constraints;static struct dentry *debugfs_root;
@@ -82,6 +83,18 @@ struct regulator_enable_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 *dev; +const char *supply; +struct device *alias; +}; +static int _regulator_is_enabled (struct regulator_dev *rdev);static int _regulator_disable (struct regulator_dev *rdev);static int _regulator_get_voltage (struct regulator_dev *rdev);
@@ -1208,6 +1221,28 @@ static int _regulator_get_enable_time(struct regulator_dev *rdev)return rdev->desc->ops->enable_time (rdev); } +static inline int regulator_supply_alias_match (struct device *dev, +const char *supply, +struct regulator_supply_alias *map) +{ +return map->dev == dev &&strcmp (map->supply, supply) ==0 ; +} + +static struct device *regulator_supply_alias (struct device *dev, +const char *supply) +{ +struct regulator_supply_alias *map; + +list_for_each_entry (map, ®ulator_supply_alias_list, list) +if (regulator_supply_alias_match (dev, supply, map)) { +dev_dbg (dev,"Mapping supply %s to %s\n" , + supply,dev_name (map->alias)); +return map->alias; + } + +return dev; +} +static struct regulator_dev *regulator_dev_lookup (struct device *dev,const char *supply,int *ret)
@@ -1217,6 +1252,8 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,struct regulator_map *map;const char *devname =NULL ; + dev =regulator_supply_alias (dev, supply); +/* first do a dt based lookup */ if (dev && dev->of_node) { node =of_get_regulator (dev, supply);
@@ -1470,6 +1507,62 @@ void regulator_put(struct regulator *regulator) }EXPORT_SYMBOL_GPL (regulator_put); +/** +* 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: device that should be used to lookup the supply +* +* All lookups for id on dev will instead be conducted on for id on alias. +*/ +int regulator_register_supply_alias (struct device *dev,const char *id, +struct device *alias) +{ +struct regulator_supply_alias *map; + +list_for_each_entry (map, ®ulator_supply_alias_list, list) +if (regulator_supply_alias_match (dev, id, map)) +return -EEXIST ; + + map =kzalloc (sizeof (struct regulator_supply_alias ),GFP_KERNEL ); +if (!map) +return -ENOMEM ; + + map->dev = dev; + map->supply = id; + map->alias = alias; + +list_add (&map->list, ®ulator_supply_alias_list); + +pr_info ("Adding alias for supply %s: %s -> %s\n" , + id,dev_name (dev),dev_name (alias)); + +return 0 ; +} +EXPORT_SYMBOL_GPL (regulator_register_supply_alias); + +/** +* 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. +*/ +int regulator_unregister_supply_alias (struct device *dev,const char *id) +{ +struct regulator_supply_alias *map; + +list_for_each_entry (map, ®ulator_supply_alias_list, list) +if (regulator_supply_alias_match (dev, id, map)) { +list_del (&map->list); +kfree (map); +return 0 ; + } + +return -EINVAL ; +} +EXPORT_SYMBOL_GPL (regulator_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)
include/linux/regulator/consumer.h +17 -0
@@ -146,6 +146,10 @@ struct regulator *__must_check devm_regulator_get_optional(struct device *dev,void regulator_put (struct regulator *regulator);void devm_regulator_put (struct regulator *regulator); +int regulator_register_supply_alias (struct device *dev,const char *id, +struct device *alias); +int regulator_unregister_supply_alias (struct device *dev,const char *id); +/* regulator output control and status */ int __must_check regulator_enable (struct regulator *regulator);int regulator_disable (struct regulator *regulator);
@@ -250,6 +254,19 @@ static inline void devm_regulator_put(struct regulator *regulator) { } +static inline int regulator_register_supply_alias (struct device *dev, +const char *id, +struct device *alias) +{ +return 0 ; +} + +static inline int regulator_unregister_supply_alias (struct device *dev, +const char *id) +{ +return 0 ; +} +static inline int regulator_enable (struct regulator *regulator) {return 0 ;
Signature · 1 line
1.7.2.5
Mark Brown
maintainerQuoted 4 lines · Charles Keepax
Following our discussions around the Arizona regulator bindings here is an effort at putting a mechanism into the regulator core to alias registering of a supply from one device to another.
This seems basically fine, though I do have a few comments below.
Bulk versions of the API would also make sense - it's likely that more than one supply will need to be mapped - and please also add managed versions so people don't have to write cleanup code.
Quoted 3 lines
I have a couple of patches that update the Arizona driver to use this that I will send in if you think this looks along the lines of what you were suggesting.
This seems like someting that should be in the MFD core, or at least that there should be helpers in the core. Something like a set of mappings in the mfd_cell so it's all data to the drivers.
drivers/regulator/core.c struct regulator_enable_gpio { · line 95
Quoted 3 lines
drivers/regulator/core.c +3
-0+struct device *dev; +const char *supply; +struct device *alias;
Please use a more meaningful name for dev. It might also be worth allowing rewriting of the supply name since we'll probably find other applications for things like modular board designs where name rewriting is useful; since it's a function call with a parameter list it's harder to add later than it would be with a struct.
Quoted 3 lines · Mark Brown
Bulk versions of the API would also make sense - it's likely that more than one supply will need to be mapped - and please also add managed versions so people don't have to write cleanup code.
For the bulk API would you prefer taking a single src and alias device and a list of supplies, or lists for all three?
I would go for a list just for the supplies, as lists of devices would probably need to be filled in by a loop on the user end anyway negating the point of the bulk API some what and a lot of the use-cases will like have the same device on both sides for all the supplies.
Quoted 7 lines
Quoted 3 lines
I have a couple of patches that update the Arizona driver to use this that I will send in if you think this looks along the lines of what you were suggesting.
This seems like someting that should be in the MFD core, or at least that there should be helpers in the core. Something like a set of mappings in the
mfd_cellso it's all data to the drivers.
Looks like a simple enough addition to the MFD core, I will submit a patch doing it this way and see where we get to.
Quoted 5 lines
Please use a more meaningful name for dev. It might also be worth allowing rewriting of the supply name since we'll probably find other applications for things like modular board designs where name rewriting is useful; since it's a function call with a parameter list it's harder to add later than it would be with a struct.
Seems that it could be useful to change the supply name, I will add this in as well.
Thanks,
Charles
Mark Brown
maintainerQuoted 8 lines · Charles Keepax
For the bulk API would you prefer taking a single src and alias device and a list of supplies, or lists for all three?
I would go for a list just for the supplies, as lists of devices would probably need to be filled in by a loop on the user end anyway negating the point of the bulk API some what and a lot of the use-cases will like have the same device on both sides for all the supplies.
I think just the supplies is fine.