Skip to contentSkip to conversation

PATCH In review Gregory CLEMENT 2 participants 9 messages Dec 26, 2014 – Jan 6, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Subject: [PATCH 0/2] regulator: Add the device tree version to the regulator_get family

Hi,

Currently it is not possible to associate a regulator to a child node which is not a device. The several ports of an ahci controller are represented as subnodes but they are not created as devices. In order to be able to associate each port with a regulator the framework API needs to be extended.

The second patch adds the device tree version (of_) for each member of the regulator_get family: normal, exclusive, optional and all of the manageable version.The of_regulator_get* functions allow using a device node to get the regulator instead using the device object.

The first patch is not related to the second one, but it is little improvement.

Gregory

Gregory CLEMENT (2):
regulator: core: Add a sanity check on the regulator_ enable/disable
functions
regulator: core: Add the device tree version to the regulator_get
family

 drivers/regulator/core.c           | 127 +++++++++++++++++++++++++++++++++----
 drivers/regulator/devres.c         |  70 ++++++++++++++++++--
 include/linux/regulator/consumer.h |  20 ++++++
 3 files changed, 198 insertions(+), 19 deletions(-)
Signature · 1 line
1.9.1
#7 lore raw

PATCH 2/2 regulator: core: Add the device tree version to the regulator_get family

This patch adds the device tree version (of_) for each member of the regulator_get family: normal, exclusive, optional and all of the manageable version.

The of_regulator_get* functions allow using a device node to get the regulator instead using the device object. It is needed for the regulator associated to a child node which is not a device, it is the case of the SATA ports of an ahci controller for instance.

---
 drivers/regulator/core.c           | 114 +++++++++++++++++++++++++++++++++----
 drivers/regulator/devres.c         |  70 ++++++++++++++++++++---
 include/linux/regulator/consumer.h |  20 +++++++
 3 files changed, 187 insertions(+), 17 deletions(-)
drivers/regulator/core.c +104 -10
index de29399b5430..74167b98797a 100644
@@ -127,26 +127,37 @@ static bool have_full_constraints(void)
 
 /**
  * of_get_regulator - get a regulator device node based on supply name
+ * and on device node if provided
+ *
  * @dev: Device pointer for the consumer (of regulator) device
+ * @np: Device tree node pointer on the node containing the regulator
  * @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)
+static struct device_node *of_get_regulator_by_node(struct device *dev,
+						    const char *supply,
+						    struct device_node *np)
 {
 	struct device_node *regnode = NULL;
 	char prop_name[32]; /* 32 is max size of property name */
+	struct device_node *node;
+
+	if (np)
+		node = np;
+	else
+		node = dev->of_node;
 
 	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);
+	regnode = of_parse_phandle(node, prop_name, 0);
 
 	if (!regnode) {
 		dev_dbg(dev, "Looking up %s property in node %s failed",
-				prop_name, dev->of_node->full_name);
+				prop_name, node->full_name);
 		return NULL;
 	}
 	return regnode;
@@ -1268,6 +1279,7 @@ static void regulator_supply_alias(struct device **dev, const char **supply)
 
 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
 						  const char *supply,
+						  struct device_node *np,
 						  int *ret)
 {
 	struct regulator_dev *r;
@@ -1278,8 +1290,8 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
 	regulator_supply_alias(&dev, &supply);
 
 	/* first do a dt based lookup */
-	if (dev && dev->of_node) {
-		node = of_get_regulator(dev, supply);
+	if ((dev && dev->of_node) || np) {
+		node = of_get_regulator_by_node(dev, supply, np);
 		if (node) {
 			list_for_each_entry(r, &regulator_list, list)
 				if (r->dev.parent &&
@@ -1322,6 +1334,7 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
 
 /* Internal regulator request function */
 static struct regulator *_regulator_get(struct device *dev, const char *id,
+					struct device_node *node,
 					bool exclusive, bool allow_dummy)
 {
 	struct regulator_dev *rdev;
@@ -1344,7 +1357,7 @@ static struct regulator *_regulator_get(struct device *dev, const char *id,
 
 	mutex_lock(&regulator_list_mutex);
 
-	rdev = regulator_dev_lookup(dev, id, &ret);
+	rdev = regulator_dev_lookup(dev, id, node, &ret);
 	if (rdev)
 		goto found;
 
@@ -1431,7 +1444,7 @@ out:
  */
 struct regulator *regulator_get(struct device *dev, const char *id)
 {
-	return _regulator_get(dev, id, false, true);
+	return _regulator_get(dev, id, NULL, false, true);
 }
 EXPORT_SYMBOL_GPL(regulator_get);
 
@@ -1458,7 +1471,7 @@ EXPORT_SYMBOL_GPL(regulator_get);
  */
 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
 {
-	return _regulator_get(dev, id, true, false);
+	return _regulator_get(dev, id, NULL, true, false);
 }
 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
 
@@ -1484,10 +1497,91 @@ EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  */
 struct regulator *regulator_get_optional(struct device *dev, const char *id)
 {
-	return _regulator_get(dev, id, false, false);
+	return _regulator_get(dev, id, NULL, false, false);
 }
 EXPORT_SYMBOL_GPL(regulator_get_optional);
 
+/**
+ * of_regulator_get - lookup and obtain a reference to a regulator.
+ * @dev: device for regulator "consumer"
+ * @node: device node for which to get the regulator
+ * @id: Supply name or regulator ID.
+ *
+ * Returns a struct regulator corresponding to the regulator producer,
+ * or IS_ERR() condition containing errno.
+ *
+ * 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.
+ */
+struct regulator *of_regulator_get(struct device *dev,
+				   const char *id,
+				struct device_node *node)
+{
+	return _regulator_get(dev, id, node, false, true);
+}
+EXPORT_SYMBOL_GPL(of_regulator_get);
+
+/**
+ * of_regulator_get_exclusive - obtain exclusive access to a regulator.
+ * @dev: device for regulator "consumer"
+ * @node: device node for which to get the regulator
+ * @id: Supply name or regulator ID.
+ *
+ * Returns a struct regulator corresponding to the regulator producer,
+ * or IS_ERR() condition containing errno.  Other consumers will be
+ * 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
+ * regulator off for correct operation of the hardware they are
+ * controlling.
+ *
+ * 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.
+ */
+struct regulator *of_regulator_get_exclusive(struct device *dev,
+					     const char *id,
+					     struct device_node *node)
+{
+	return _regulator_get(dev, id, node, true, false);
+}
+EXPORT_SYMBOL_GPL(of_regulator_get_exclusive);
+
+/**
+ * of_regulator_get_optional - obtain optional access to a regulator.
+ * @dev: device for regulator "consumer"
+ * @node: device node for which to get the regulator
+ * @id: Supply name or regulator ID.
+ *
+ * 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.
+ */
+struct regulator *of_regulator_get_optional(struct device *dev,
+					    const char *id,
+					    struct device_node *node)
+{
+	return _regulator_get(dev, id, node, false, false);
+}
+EXPORT_SYMBOL_GPL(of_regulator_get_optional);
+
 /* Locks held by regulator_put() */
 static void _regulator_put(struct regulator *regulator)
 {
@@ -3714,7 +3808,7 @@ regulator_register(const struct regulator_desc *regulator_desc,
 	if (supply) {
 		struct regulator_dev *r;
 
-		r = regulator_dev_lookup(dev, supply, &ret);
+		r = regulator_dev_lookup(dev, supply, NULL, &ret);
 
 		if (ret == -ENODEV) {
 			/*
drivers/regulator/devres.c +63 -7

107 lines not on this page · raw message

include/linux/regulator/consumer.h +20 -0
index d17e1ff7ad01..1c88ce1aaa3f 100644
@@ -40,6 +40,7 @@
 struct device;
 struct notifier_block;
 struct regmap;
+struct device_node;
 
 /*
  * Regulator operating modes.
@@ -162,14 +163,33 @@ struct regulator *__must_check regulator_get(struct device *dev,
 					     const char *id);
 struct regulator *__must_check devm_regulator_get(struct device *dev,
 					     const char *id);
+struct regulator *__must_check of_regulator_get(struct device *dev,
+						const char *id,
+						struct device_node *node);
+struct regulator *__must_check devm_of_regulator_get(struct device *dev,
+						const char *id,
+						struct device_node *node);
 struct regulator *__must_check regulator_get_exclusive(struct device *dev,
 						       const char *id);
 struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev,
 							const char *id);
+struct regulator *__must_check of_regulator_get_exclusive(struct device *dev,
+						      const char *id,
+						      struct device_node *node);
+struct regulator *__must_check devm_of_regulator_get_exclusive(struct device *dev,
+						      const char *id,
+						      struct device_node *node);
 struct regulator *__must_check regulator_get_optional(struct device *dev,
 						      const char *id);
 struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
 							   const char *id);
+struct regulator *__must_check of_regulator_get_optional(struct device *dev,
+						      const char *id,
+						      struct device_node *node);
+struct regulator *__must_check devm_of_regulator_get_optional(struct device *dev,
+						      const char *id,
+						      struct device_node *node);
+
 void regulator_put(struct regulator *regulator);
 void devm_regulator_put(struct regulator *regulator);
 
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/2 regulator: core: Add a sanity check on the regulator_ enable/disable functions

In reply to Gregory CLEMENT

These two functions use the pointer passed in parameter without any check. By adding a NULL pointer check, it allows using those functions from a driver in a more generic way. It is useful especially for the disable case if the regulator is optional.

---
 drivers/regulator/core.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/regulator/core.c +11 -2
index e225711bb8bc..de29399b5430 100644
@@ -1911,12 +1911,16 @@ static int _regulator_enable(struct regulator_dev *rdev)
  */
 int regulator_enable(struct regulator *regulator)
 {
-	struct regulator_dev *rdev = regulator->rdev;
+	struct regulator_dev *rdev;
 	int ret = 0;
 
+	if (!regulator)
+		return 0;
+
 	if (regulator->always_on)
 		return 0;
 
+	rdev = regulator->rdev;
 	if (rdev->supply) {
 		ret = regulator_enable(rdev->supply);
 		if (ret != 0)
@@ -2024,12 +2028,17 @@ static int _regulator_disable(struct regulator_dev *rdev)
  */
 int regulator_disable(struct regulator *regulator)
 {
-	struct regulator_dev *rdev = regulator->rdev;
+	struct regulator_dev *rdev;
 	int ret = 0;
 
+	if (!regulator)
+		return 0;
+
 	if (regulator->always_on)
 		return 0;
 
+	rdev = regulator->rdev;
+
 	mutex_lock(&rdev->mutex);
 	ret = _regulator_disable(rdev);
 	mutex_unlock(&rdev->mutex);
Signature · 1 line
1.9.1
maintainer
#2 lore raw

Re: [PATCH 1/2] regulator: core: Add a sanity check on the regulator_ enable/disable functions

Quoted 4 lines · Gregory CLEMENT

These two functions use the pointer passed in parameter without any check. By adding a NULL pointer check, it allows using those functions from a driver in a more generic way. It is useful especially for the disable case if the regulator is optional.

No, especially in the case of regulator_enable() this is deliberate - we're trying to ensure that if people are using regulators they're being careful about it, checking error codes and so on. I'd really want to see some persuasive use case for this. What you're saying here sounds like the consumer shouldn't be treating the regulator as optional at all but should instead be using a normal regulator.

maintainer
#8 lore raw

Re: [PATCH 2/2] regulator: core: Add the device tree version to the regulator_get family

In reply to Gregory CLEMENT
Quoted 3 lines · Gregory CLEMENT

This patch adds the device tree version (of_) for each member of the regulator_get family: normal, exclusive, optional and all of the manageable version.

I'm really not keen on this - this sort of firmware specific interface is just going to encourage the sort of problematic Linux MFD in DT stuff people seem so enthusiastic about and needlessly DT specific code.

Quoted 4 lines

The of_regulator_get* functions allow using a device node to get the regulator instead using the device object. It is needed for the regulator associated to a child node which is not a device, it is the case of the SATA ports of an ahci controller for instance.

It would be much better to provide a way of mapping the supplies to the regulator using the normal APIs, for example by making the child nodes devices (I'm not seeing anything that says why that's not possible and it sounds like they're supposed to be physically separate devices which sound like good candidates for being struct device) or by providing some other alternative mapping mechanism which isn't DT specific.

Or take another look at what the bindings are doing to avoid the requirement for this, I'd have expected these new bindings to be part of the review here...

#3 lore raw

Re: [PATCH 1/2] regulator: core: Add a sanity check on the regulator_ enable/disable functions

In reply to Mark Brown

Hi Mark,

Quoted 9 lines · Mark Brown
Quoted 4 lines · Gregory CLEMENT

These two functions use the pointer passed in parameter without any check. By adding a NULL pointer check, it allows using those functions from a driver in a more generic way. It is useful especially for the disable case if the regulator is optional.

No, especially in the case of regulator_enable() this is deliberate - we're trying to ensure that if people are using regulators they're being careful about it, checking error codes and so on. I'd really want to

OK so at least we should check that the pointer is not NULL before using it and inform the user of it by using a WARNING() or even a BUG() instead of just let the kernel crash latter.

Quoted 4 lines

see some persuasive use case for this. What you're saying here sounds like the consumer shouldn't be treating the regulator as optional at all but should instead be using a normal regulator.

Being able to deal with NULL pointer in the disable function is convenient and is done in other similar subsystems such as phy or clk for example. Instead of having a check on the NULL pointer in each driver, it seems more logical to do it directly in the disable function.

Thanks for you review,

Gregory

Signature · 4 lines
Gregory Clement, Free Electrons
Kernel, drivers, real-time and embedded Linux
development, consulting, training and support.
http://free-electrons.com
maintainer
#4 lore raw

Re: [PATCH 1/2] regulator: core: Add a sanity check on the regulator_ enable/disable functions

Quoted 5 lines · Gregory CLEMENT

Hi Mark,

Quoted 1 line · Mark Brown

On Fri, Dec 26, 2014 at 06:26:38PM +0100, Gregory CLEMENT wrote:

Quoted 7 lines
Quoted 3 lines

No, especially in the case of regulator_enable() this is deliberate - we're trying to ensure that if people are using regulators they're being careful about it, checking error codes and so on. I'd really want to

OK so at least we should check that the pointer is not NULL before using it and inform the user of it by using a WARNING() or even a BUG() instead of just let the kernel crash latter.

Just crashing on the NULL is just about as good in terms of discoverabilty and any consumer that is assuming NULL is not a valid regulator is buggy in any case, any non-error pointer could be a valid regulator as far as users are concerned.

Quoted 8 lines
Quoted 3 lines

see some persuasive use case for this. What you're saying here sounds like the consumer shouldn't be treating the regulator as optional at all but should instead be using a normal regulator.

Being able to deal with NULL pointer in the disable function is convenient and is done in other similar subsystems such as phy or clk for example. Instead of having a check on the NULL pointer in each driver, it seems more logical to do it directly in the disable function.

This really only applies if it's likely that some thing that always gets used if it's there might be missing which isn't the case for regulators, it's not at all common to have power supplies that might be missing and if they are missing NULL isn't a good way to track them.

If you're having problems with this and need workarounds in the core to make your driver code look OK that sounds like things are working since it sounds like the driver code is probably abusing the API here.

#5 lore raw

Re: [PATCH 1/2] regulator: core: Add a sanity check on the regulator_ enable/disable functions

Hi Mark,

Quoted 23 lines · Mark Brown
Quoted 5 lines · Gregory CLEMENT

Hi Mark,

Quoted 1 line · Mark Brown

On Fri, Dec 26, 2014 at 06:26:38PM +0100, Gregory CLEMENT wrote:

Quoted 7 lines
Quoted 3 lines

No, especially in the case of regulator_enable() this is deliberate - we're trying to ensure that if people are using regulators they're being careful about it, checking error codes and so on. I'd really want to

OK so at least we should check that the pointer is not NULL before using it and inform the user of it by using a WARNING() or even a BUG() instead of just let the kernel crash latter.

Just crashing on the NULL is just about as good in terms of discoverabilty and any consumer that is assuming NULL is not a valid regulator is buggy in any case, any non-error pointer could be a valid regulator as far as users are concerned.

Quoted 3 lines
Quoted 3 lines

see some persuasive use case for this. What you're saying here sounds like the consumer shouldn't be treating the regulator as optional at all but should instead be using a normal regulator.

Quoted 8 lines
Quoted 4 lines

Being able to deal with NULL pointer in the disable function is convenient and is done in other similar subsystems such as phy or clk for example. Instead of having a check on the NULL pointer in each driver, it seems more logical to do it directly in the disable function.

This really only applies if it's likely that some thing that always gets used if it's there might be missing which isn't the case for regulators, it's not at all common to have power supplies that might be missing and

Well the pattern the following pattern is very common in the drivers using the regulator:

if (!IS_ERR(regulator_pointer)

	regulator_disable(regulator_pointer);

So for me it was a good hint that we can factorize it.

Quoted 5 lines

if they are missing NULL isn't a good way to track them.

If you're having problems with this and need workarounds in the core to make your driver code look OK that sounds like things are working since it sounds like the driver code is probably abusing the API here.

I don't _need_ it at all. It was just an improvement but if you don't want it, I am fine with it.

Thanks,

Gregory

Signature · 4 lines
Gregory Clement, Free Electrons
Kernel, drivers, real-time and embedded Linux
development, consulting, training and support.
http://free-electrons.com
maintainer
#6 lore raw

Re: [PATCH 1/2] regulator: core: Add a sanity check on the regulator_ enable/disable functions

Quoted 6 lines · Gregory CLEMENT
Quoted 3 lines · Mark Brown

This really only applies if it's likely that some thing that always gets used if it's there might be missing which isn't the case for regulators, it's not at all common to have power supplies that might be missing and

Quoted 7 lines

Well the pattern the following pattern is very common in the drivers using the regulator:

if (!IS_ERR(regulator_pointer)

	regulator_disable(regulator_pointer);

So for me it was a good hint that we can factorize it.

It is? It shouldn't be...