All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zev Weiss <zev@bewilderbeest.net>
To: Mark Brown <broonie@kernel.org>, Liam Girdwood <lgirdwood@gmail.com>
Cc: Zev Weiss <zev@bewilderbeest.net>,
	linux-kernel@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	openbmc@lists.ozlabs.org
Subject: [PATCH 4/6] regulator: core: Add external-output support
Date: Tue,  3 May 2022 23:52:50 -0700	[thread overview]
Message-ID: <20220504065252.6955-4-zev@bewilderbeest.net> (raw)
In-Reply-To: <20220504065252.6955-1-zev@bewilderbeest.net>

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(-)

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 */
 };
 
 /**
-- 
2.36.0


WARNING: multiple messages have this Message-ID (diff)
From: Zev Weiss <zev@bewilderbeest.net>
To: Mark Brown <broonie@kernel.org>, Liam Girdwood <lgirdwood@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	openbmc@lists.ozlabs.org, linux-kernel@vger.kernel.org,
	Zev Weiss <zev@bewilderbeest.net>
Subject: [PATCH 4/6] regulator: core: Add external-output support
Date: Tue,  3 May 2022 23:52:50 -0700	[thread overview]
Message-ID: <20220504065252.6955-4-zev@bewilderbeest.net> (raw)
In-Reply-To: <20220504065252.6955-1-zev@bewilderbeest.net>

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(-)

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 */
 };
 
 /**
-- 
2.36.0


  parent reply	other threads:[~2022-05-04  6:53 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-04  6:52 [PATCH 1/6] dt-bindings: regulator: Add regulator-external-output property Zev Weiss
2022-05-04  6:52 ` Zev Weiss
2022-05-04  6:52 ` [PATCH 2/6] dt-bindings: regulator: Add reg-external-output binding Zev Weiss
2022-05-04  6:52   ` Zev Weiss
2022-05-04 12:55   ` Mark Brown
2022-05-04 12:55     ` Mark Brown
2022-05-04 20:33     ` Zev Weiss
2022-05-04 20:33       ` Zev Weiss
2022-05-04 20:49       ` Mark Brown
2022-05-04 20:49         ` Mark Brown
2022-05-04 21:35         ` Zev Weiss
2022-05-04 21:35           ` Zev Weiss
2022-05-05 12:05           ` Mark Brown
2022-05-05 12:05             ` Mark Brown
2022-05-05  8:33       ` Krzysztof Kozlowski
2022-05-04  6:52 ` [PATCH 3/6] regulator: core: Add error flags to sysfs attributes Zev Weiss
2022-05-04  6:52   ` Zev Weiss
2022-05-04  6:52 ` Zev Weiss [this message]
2022-05-04  6:52   ` [PATCH 4/6] regulator: core: Add external-output support Zev Weiss
2022-05-04 13:06   ` Mark Brown
2022-05-04 13:06     ` Mark Brown
2022-05-04  6:52 ` [PATCH 5/6] regulator: core: Add external get type Zev Weiss
2022-05-04  6:52   ` Zev Weiss
2022-05-04  6:52 ` [PATCH 6/6] regulator: core: Add external-consumer driver Zev Weiss
2022-05-04  6:52   ` Zev Weiss
2022-05-04 12:36 ` [PATCH 1/6] dt-bindings: regulator: Add regulator-external-output property Mark Brown
2022-05-04 12:36   ` Mark Brown
2022-05-04 20:54 ` (subset) " Mark Brown
2022-05-04 20:54   ` Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220504065252.6955-4-zev@bewilderbeest.net \
    --to=zev@bewilderbeest.net \
    --cc=broonie@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=openbmc@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.