diff mbox series

[4/6] regulator: core: Add external-output support

Message ID 20220504065252.6955-4-zev@bewilderbeest.net
State New
Headers show
Series regulator: core: Add support for external outputs | expand

Commit Message

Zev Weiss May 4, 2022, 6:52 a.m. UTC
Regulators feeding external outputs (i.e. supplying devices we don't
control) can be switched on and off by userspace via the 'state' sysfs
attribute, which is now (conditionally) writable.  They are also not
automatically disabled in regulator_late_cleanup(), since we have no
way of knowing that they're not actually in use.

Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
---
 .../ABI/testing/sysfs-class-regulator         |  4 ++
 drivers/regulator/core.c                      | 41 ++++++++++++++++---
 drivers/regulator/of_regulator.c              |  2 +
 include/linux/regulator/machine.h             |  1 +
 4 files changed, 43 insertions(+), 5 deletions(-)

Comments

Mark Brown May 4, 2022, 1:06 p.m. UTC | #1
On Tue, May 03, 2022 at 11:52:50PM -0700, Zev Weiss wrote:
> Regulators feeding external outputs (i.e. supplying devices we don't
> control) can be switched on and off by userspace via the 'state' sysfs
> attribute, which is now (conditionally) writable.  They are also not
> automatically disabled in regulator_late_cleanup(), since we have no
> way of knowing that they're not actually in use.

No, this is an abstraction failure.  Enabling and disabling a regulator
is something that should be handled in consumer drivers, just as with
every other use case there's no reason why the regulator should be
offering direct control to userspace.  Putting this directly in the
regulator will cause problems as soon as you for example have multiple
outputs supplied from a single regulator and can't be integrated with
any other control mechanisms.
diff mbox series

Patch

diff --git a/Documentation/ABI/testing/sysfs-class-regulator b/Documentation/ABI/testing/sysfs-class-regulator
index 475b9a372657..1c0451730df2 100644
--- a/Documentation/ABI/testing/sysfs-class-regulator
+++ b/Documentation/ABI/testing/sysfs-class-regulator
@@ -23,6 +23,10 @@  Description:
 		'unknown' means software cannot determine the state, or
 		the reported state is invalid.
 
+		For regulators supplying external outputs, 'enabled'
+		or 'disabled' can be written to this file to turn the
+		regulator on and off.
+
 		NOTE: this field can be used in conjunction with microvolts
 		or microamps to determine configured regulator output levels.
 
diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 9094bd8772e5..b7617926336f 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -83,6 +83,8 @@  struct regulator_supply_alias {
 
 static int _regulator_is_enabled(struct regulator_dev *rdev);
 static int _regulator_disable(struct regulator *regulator);
+static int _regulator_do_enable(struct regulator_dev *rdev);
+static int _regulator_do_disable(struct regulator_dev *rdev);
 static int _regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags);
 static int _regulator_get_current_limit(struct regulator_dev *rdev);
 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
@@ -667,7 +669,33 @@  static ssize_t state_show(struct device *dev,
 
 	return ret;
 }
-static DEVICE_ATTR_RO(state);
+
+static ssize_t state_store(struct device *dev, struct device_attribute *attr,
+			   const char *buf, size_t count)
+{
+	struct regulator_dev *rdev = dev_get_drvdata(dev);
+	bool enabled;
+	ssize_t ret = 0;
+
+	if (sysfs_streq(buf, "enabled"))
+		enabled = true;
+	else if (sysfs_streq(buf, "disabled"))
+		enabled = false;
+	else
+		return -EINVAL;
+
+	regulator_lock(rdev);
+	if (enabled != _regulator_is_enabled(rdev)) {
+		if (enabled)
+			ret = _regulator_do_enable(rdev);
+		else
+			ret = _regulator_do_disable(rdev);
+	}
+	regulator_unlock(rdev);
+
+	return ret ? : count;
+}
+static DEVICE_ATTR_RW(state);
 
 static ssize_t status_show(struct device *dev,
 			   struct device_attribute *attr, char *buf)
@@ -5051,8 +5079,11 @@  static umode_t regulator_attr_is_visible(struct kobject *kobj,
 	if (attr == &dev_attr_opmode.attr)
 		return ops->get_mode ? mode : 0;
 
-	if (attr == &dev_attr_state.attr)
+	if (attr == &dev_attr_state.attr) {
+		if (!(rdev->constraints && rdev->constraints->external_output))
+			mode &= ~0222;
 		return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
+	}
 
 	if (attr == &dev_attr_status.attr)
 		return ops->get_status ? mode : 0;
@@ -6062,7 +6093,7 @@  static int regulator_late_cleanup(struct device *dev, void *data)
 	struct regulation_constraints *c = rdev->constraints;
 	int ret;
 
-	if (c && c->always_on)
+	if (c && (c->always_on || c->external_output))
 		return 0;
 
 	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
@@ -6114,8 +6145,8 @@  static void regulator_init_complete_work_function(struct work_struct *work)
 
 	/* If we have a full configuration then disable any regulators
 	 * 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.
+	 * not in use, always_on, or external_output.  This is effectively
+	 * the default for DT and ACPI as they have full constraints.
 	 */
 	class_for_each_device(&regulator_class, NULL, NULL,
 			      regulator_late_cleanup);
diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c
index f54d4f176882..f48e6ea2b97e 100644
--- a/drivers/regulator/of_regulator.c
+++ b/drivers/regulator/of_regulator.c
@@ -96,6 +96,8 @@  static int of_get_regulation_constraints(struct device *dev,
 
 	constraints->name = of_get_property(np, "regulator-name", NULL);
 
+	constraints->external_output = of_property_read_bool(np, "regulator-external-output");
+
 	if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
 		constraints->min_uV = pval;
 
diff --git a/include/linux/regulator/machine.h b/include/linux/regulator/machine.h
index 621b7f4a3639..a38e7db9f82e 100644
--- a/include/linux/regulator/machine.h
+++ b/include/linux/regulator/machine.h
@@ -219,6 +219,7 @@  struct regulation_constraints {
 	unsigned over_voltage_detection:1; /* notify on over voltage */
 	unsigned under_voltage_detection:1; /* notify on under voltage */
 	unsigned over_temp_detection:1; /* notify on over temperature */
+	unsigned external_output:1; /* output supplies only external devices */
 };
 
 /**