Skip to contentSkip to conversation

regulator: add support for GPIO power load switches

PATCH In review Bartosz Golaszewski 3 participants 7 messages Dec 16, 2016 – Dec 21, 2016

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

cover regulator: add support for GPIO power load switches

We would like to add support for GPIO-controlled power load switches (for example: the tps229* series from Texas Instruments). We use this chip to power-cycle devices whose power consumption is measured using baylibre-acme probes thus we need a way to control them from user space.

I initially submitted a series adding this support via the iio framework, but it was decided that iio is not the right interface for that.

This series extends the regulator core to support regulators controlled from user space and reuses the fixed regulator driver to support gpio power switches.

Bartosz Golaszewski (3):
regulator: add support for user space controlled regulators
doc: DT: add new compatible to fixed regulator's binding
regulator: fixed: add support for gpio power switches

 .../bindings/regulator/fixed-regulator.txt         |  4 ++-
 drivers/regulator/core.c                           | 38 +++++++++++++++++++++-
 drivers/regulator/fixed.c                          | 33 ++++++++++++++-----
 include/linux/regulator/driver.h                   |  5 +++
 4 files changed, 70 insertions(+), 10 deletions(-)
Signature · 6 lines
2.9.3

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH 1/3 regulator: add support for user space controlled regulators

Add a new flag to struct regulator_desc indicating whether a regulator can be controlled from user space and implement a routine in regulator core allowing to toggle the regulator state via the sysfs 'state' attribute.

This is useful for gpio power switches.

---
 drivers/regulator/core.c         | 38 +++++++++++++++++++++++++++++++++++++-
 include/linux/regulator/driver.h |  5 +++++
 2 files changed, 42 insertions(+), 1 deletion(-)
drivers/regulator/core.c +37 -1
index 5c1519b..f77de8f 100644
@@ -99,6 +99,7 @@ struct regulator_supply_alias {
 };
 
 static int _regulator_is_enabled(struct regulator_dev *rdev);
+static int _regulator_enable(struct regulator_dev *rdev);
 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);
@@ -401,7 +402,42 @@ static ssize_t regulator_state_show(struct device *dev,
 
 	return ret;
 }
-static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
+static ssize_t regulator_state_set(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t len)
+{
+	struct regulator_dev *rdev = dev_get_drvdata(dev);
+	bool enable;
+	ssize_t ret;
+
+	if (!rdev->desc->userspace_control)
+		return -EPERM;
+
+	if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1"))
+		enable = true;
+	else if (sysfs_streq(buf, "disabled\n") || sysfs_streq(buf, "0"))
+		enable = false;
+	else
+		return -EINVAL;
+
+	mutex_lock(&rdev->mutex);
+
+	if ((enable && _regulator_is_enabled(rdev)) ||
+	    (!enable && !_regulator_is_enabled(rdev))) {
+		mutex_unlock(&rdev->mutex);
+		return -EBUSY;
+	}
+
+	ret = enable ? _regulator_enable(rdev) : _regulator_disable(rdev);
+
+	mutex_unlock(&rdev->mutex);
+
+	if (ret)
+		return ret;
+
+	return len;
+}
+static DEVICE_ATTR(state, 0644, regulator_state_show, regulator_state_set);
 
 static ssize_t regulator_status_show(struct device *dev,
 				   struct device_attribute *attr, char *buf)
include/linux/regulator/driver.h +5 -0
index 37b5324..0e7ad95 100644
@@ -293,6 +293,9 @@ enum regulator_type {
  * @off_on_delay: guard time (in uS), before re-enabling a regulator
  *
  * @of_map_mode: Maps a hardware mode defined in a DeviceTree to a standard mode
+ *
+ * @userspace_control: A flag to indicate whether this regulator can be
+ *                     controlled from user-space.
  */
 struct regulator_desc {
 	const char *name;
@@ -347,6 +350,8 @@ struct regulator_desc {
 	unsigned int off_on_delay;
 
 	unsigned int (*of_map_mode)(unsigned int mode);
+
+	unsigned int userspace_control;
 };
 
 /**
Signature · 1 line
2.9.3
#3 lore raw

PATCH 2/3 doc: DT: add new compatible to fixed regulator's binding

In reply to Bartosz Golaszewski

Extend the fixed regulator's device tree bindings with a new compatible describing GPIO-driven power load switches.

---
 Documentation/devicetree/bindings/regulator/fixed-regulator.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/regulator/fixed-regulator.txt +3 -1
index 4fae41d..306931c 100644
@@ -1,7 +1,9 @@
 Fixed Voltage regulators
 
 Required properties:
-- compatible: Must be "regulator-fixed";
+- compatible:
+	"regulator-fixed" for regular fixed-regulators
+	"gpio-power-switch" for gpio-driven power load switches
 
 Optional properties:
 - gpio: gpio to use for enable control
Signature · 6 lines
2.9.3

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw

PATCH 3/3 regulator: fixed: add support for gpio power switches

In reply to Bartosz Golaszewski

The difference between a regular fixed regulator and a GPIO power load switch is the fact that the latter may be controlled from user space.

Reuse the fixed regulator driver to support power switches.

---
 drivers/regulator/fixed.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
drivers/regulator/fixed.c +25 -8
index 988a747..f125a3e 100644
@@ -31,6 +31,8 @@
 #include <linux/regulator/of_regulator.h>
 #include <linux/regulator/machine.h>
 
+#define REG_FIXED_POWER_SWITCH		BIT(0)
+
 struct fixed_voltage_data {
 	struct regulator_desc desc;
 	struct regulator_dev *dev;
@@ -97,11 +99,27 @@ of_get_fixed_voltage_config(struct device *dev,
 static struct regulator_ops fixed_voltage_ops = {
 };
 
+#if defined(CONFIG_OF)
+static const struct of_device_id fixed_of_match[] = {
+	{
+		.compatible = "regulator-fixed",
+	},
+	{
+		.compatible = "gpio-power-switch",
+		.data = (void *)REG_FIXED_POWER_SWITCH,
+	},
+	{},
+};
+MODULE_DEVICE_TABLE(of, fixed_of_match);
+#endif
+
 static int reg_fixed_voltage_probe(struct platform_device *pdev)
 {
 	struct fixed_voltage_config *config;
 	struct fixed_voltage_data *drvdata;
 	struct regulator_config cfg = { };
+	const struct of_device_id *of_id;
+	long flags = 0;
 	int ret;
 
 	drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
@@ -175,6 +193,13 @@ static int reg_fixed_voltage_probe(struct platform_device *pdev)
 	cfg.driver_data = drvdata;
 	cfg.of_node = pdev->dev.of_node;
 
+	of_id = of_match_node(fixed_of_match, pdev->dev.of_node);
+	if (of_id)
+		flags = (long)of_id->data;
+
+	if (flags & REG_FIXED_POWER_SWITCH)
+		drvdata->desc.userspace_control = 1;
+
 	drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
 					       &cfg);
 	if (IS_ERR(drvdata->dev)) {
@@ -191,14 +216,6 @@ static int reg_fixed_voltage_probe(struct platform_device *pdev)
 	return 0;
 }
 
-#if defined(CONFIG_OF)
-static const struct of_device_id fixed_of_match[] = {
-	{ .compatible = "regulator-fixed", },
-	{},
-};
-MODULE_DEVICE_TABLE(of, fixed_of_match);
-#endif
-
 static struct platform_driver regulator_fixed_voltage_driver = {
 	.probe		= reg_fixed_voltage_probe,
 	.driver		= {
Signature · 1 line
2.9.3
maintainer
#2 lore raw

Re: [PATCH 1/3] regulator: add support for user space controlled regulators

In reply to Bartosz Golaszewski
Quoted 4 lines · Bartosz Golaszewski

Add a new flag to struct regulator_desc indicating whether a regulator can be controlled from user space and implement a routine in regulator core allowing to toggle the regulator state via the sysfs 'state' attribute.

No, we've been through this repeatedly before - search the archives. Write a driver for your hardware which exposes a control for bouncing the power if that's something that makes sense in your application. Doing this at the regulator level is just far too likely to result in poorly integrated systems.

maintainer
#6 lore raw

Re: [PATCH 3/3] regulator: fixed: add support for gpio power switches

In reply to Bartosz Golaszewski
Quoted 2 lines · Bartosz Golaszewski

The difference between a regular fixed regulator and a GPIO power load switch is the fact that the latter may be controlled from user space.

This is not something I would understand or expect from the naming here. I would expect the difference between a regulator and a switch to be that the switch does no regulation, such things are fairly common in system design for example as part of the logic that switches between the different input power supplies available to the system to provide the root system power rail.

maintainer
#4 lore raw

Re: [PATCH 2/3] doc: DT: add new compatible to fixed regulator's binding

In reply to Bartosz Golaszewski
Quoted 13 lines · Bartosz Golaszewski

Extend the fixed regulator's device tree bindings with a new compatible describing GPIO-driven power load switches.

---
 Documentation/devicetree/bindings/regulator/fixed-regulator.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/regulator/fixed-regulator.txt +0 -0
index 4fae41d..306931c 100644

Documentation/devicetree/bindings/regulator/fixed-regulator.txt line 6

Quoted 7 lines
Documentation/devicetree/bindings/regulator/fixed-regulator.txt +3 -1
@@ -1,7 +1,9 @@
 Fixed Voltage regulators
 
 Required properties:
-- compatible: Must be "regulator-fixed";
+- compatible:
+	"regulator-fixed" for regular fixed-regulators
+	"gpio-power-switch" for gpio-driven power load switches

Probably add "... which do no regulation" along the lines of Mark's comment.

Quoted 6 lines
Documentation/devicetree/bindings/regulator/fixed-regulator.txt +0 -0
 Optional properties:
 - gpio: gpio to use for enable control
Signature · 1 line
2.9.3