linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support
@ 2023-10-10  8:59 Oleksij Rempel
  2023-10-10  8:59 ` [PATCH v1 2/3] regulator: fixed: add support for under-voltage IRQ Oleksij Rempel
                   ` (4 more replies)
  0 siblings, 5 replies; 15+ messages in thread
From: Oleksij Rempel @ 2023-10-10  8:59 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley
  Cc: Oleksij Rempel, kernel, linux-kernel, devicetree

Add under-voltage interrupt support. This can be used with simple
regulators having no other way to communicate an under-voltage event
except as by toggling some GPIO line.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>

diff --git a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
index ac0281b1cceb..0f8760ed2fb1 100644
--- a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
+++ b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
@@ -100,6 +100,14 @@ properties:
   vin-supply:
     description: Input supply phandle.

+  interrupts:
+    maxItems: 1
+    description:
+      Under-voltage interrupt
+
+  interrupt-names:
+    const: under-voltage
+
 required:
   - compatible
   - regulator-name
---
 .../devicetree/bindings/regulator/fixed-regulator.yaml    | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
index ac0281b1cceb..0f8760ed2fb1 100644
--- a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
+++ b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
@@ -100,6 +100,14 @@ properties:
   vin-supply:
     description: Input supply phandle.
 
+  interrupts:
+    maxItems: 1
+    description:
+      Under-voltage interrupt
+
+  interrupt-names:
+    const: under-voltage
+
 required:
   - compatible
   - regulator-name
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v1 2/3] regulator: fixed: add support for under-voltage IRQ
  2023-10-10  8:59 [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support Oleksij Rempel
@ 2023-10-10  8:59 ` Oleksij Rempel
  2023-10-10  8:59 ` [PATCH v1 3/3] regulator: fixed: forward under-voltage events Oleksij Rempel
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 15+ messages in thread
From: Oleksij Rempel @ 2023-10-10  8:59 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley
  Cc: Oleksij Rempel, kernel, linux-kernel, devicetree

Add interrupt support for under-voltage notification. This functionality
can be used on systems capable to detect under-voltage state and having
enough capacity to let the SoC do some emergency preparation.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/regulator/fixed.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/drivers/regulator/fixed.c b/drivers/regulator/fixed.c
index 55130efae9b8..493dd244e4f4 100644
--- a/drivers/regulator/fixed.c
+++ b/drivers/regulator/fixed.c
@@ -105,6 +105,35 @@ static int reg_is_enabled(struct regulator_dev *rdev)
 	return priv->enable_counter > 0;
 }
 
+static irqreturn_t reg_fixed_under_voltage_irq_handler(int irq, void *data)
+{
+	struct fixed_voltage_data *priv = data;
+	struct regulator_dev *rdev = priv->dev;
+
+	regulator_notifier_call_chain(rdev, REGULATOR_EVENT_UNDER_VOLTAGE, NULL);
+
+	return IRQ_HANDLED;
+}
+
+static int reg_fixed_get_irqs(struct device *dev,
+			      struct fixed_voltage_data *priv)
+{
+	const char *uv = "under-voltage";
+	int ret;
+
+	ret = fwnode_irq_get_byname(dev_fwnode(dev), "under-voltage");
+	/* This is optional IRQ. Ignore if not found */
+	if (ret < 0)
+		return 0;
+
+	ret = devm_request_threaded_irq(dev, ret, NULL,
+					reg_fixed_under_voltage_irq_handler,
+					IRQF_ONESHOT, uv, priv);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to request %s IRQ\n", uv);
+
+	return 0;
+}
 
 /**
  * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
@@ -294,6 +323,10 @@ static int reg_fixed_voltage_probe(struct platform_device *pdev)
 	dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
 		drvdata->desc.fixed_uV);
 
+	ret = reg_fixed_get_irqs(dev, drvdata);
+	if (ret)
+		return ret;
+
 	return 0;
 }
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-10  8:59 [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support Oleksij Rempel
  2023-10-10  8:59 ` [PATCH v1 2/3] regulator: fixed: add support for under-voltage IRQ Oleksij Rempel
@ 2023-10-10  8:59 ` Oleksij Rempel
  2023-10-10 12:19   ` Mark Brown
  2023-10-10 12:20 ` [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support Mark Brown
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 15+ messages in thread
From: Oleksij Rempel @ 2023-10-10  8:59 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley
  Cc: Oleksij Rempel, kernel, linux-kernel, devicetree

Add handler to forward under-voltage events.
On systems for more or less complicated regulator chains we need to
forward under-voltage events to actual driver which need to react on
them.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/regulator/fixed.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/drivers/regulator/fixed.c b/drivers/regulator/fixed.c
index 493dd244e4f4..99d37f1ebbc9 100644
--- a/drivers/regulator/fixed.c
+++ b/drivers/regulator/fixed.c
@@ -33,6 +33,7 @@
 struct fixed_voltage_data {
 	struct regulator_desc desc;
 	struct regulator_dev *dev;
+	struct notifier_block nb;
 
 	struct clk *enable_clock;
 	unsigned int enable_counter;
@@ -105,6 +106,39 @@ static int reg_is_enabled(struct regulator_dev *rdev)
 	return priv->enable_counter > 0;
 }
 
+static int reg_fixed_regulator_notifier(struct notifier_block *nb,
+					unsigned long event, void *data)
+{
+	struct fixed_voltage_data *priv =
+		container_of(nb, struct fixed_voltage_data, nb);
+	struct regulator_dev *rdev = priv->dev;
+
+	if (event != REGULATOR_EVENT_UNDER_VOLTAGE_WARN &&
+	    event != REGULATOR_EVENT_UNDER_VOLTAGE)
+		return NOTIFY_OK;
+
+	regulator_notifier_call_chain(rdev, event, NULL);
+
+	return NOTIFY_OK;
+}
+
+static int reg_fixed_register_reg_notifier(struct fixed_voltage_data *priv,
+					   struct device *dev)
+{
+	struct regulator_dev *rdev = priv->dev;
+	int ret;
+
+	if (!rdev->supply)
+		return 0;
+
+	priv->nb.notifier_call = reg_fixed_regulator_notifier;
+	ret = devm_regulator_register_notifier(rdev->supply, &priv->nb);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to register notifier\n");
+
+	return 0;
+}
+
 static irqreturn_t reg_fixed_under_voltage_irq_handler(int irq, void *data)
 {
 	struct fixed_voltage_data *priv = data;
@@ -327,7 +361,7 @@ static int reg_fixed_voltage_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	return 0;
+	return reg_fixed_register_reg_notifier(drvdata, dev);
 }
 
 #if defined(CONFIG_OF)
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-10  8:59 ` [PATCH v1 3/3] regulator: fixed: forward under-voltage events Oleksij Rempel
@ 2023-10-10 12:19   ` Mark Brown
  2023-10-10 12:55     ` Oleksij Rempel
  0 siblings, 1 reply; 15+ messages in thread
From: Mark Brown @ 2023-10-10 12:19 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: Liam Girdwood, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	kernel, linux-kernel, devicetree

[-- Attachment #1: Type: text/plain, Size: 1068 bytes --]

On Tue, Oct 10, 2023 at 10:59:06AM +0200, Oleksij Rempel wrote:
> Add handler to forward under-voltage events.
> On systems for more or less complicated regulator chains we need to
> forward under-voltage events to actual driver which need to react on
> them.

It isn't clear to me why this would be implemented in one specific
driver, nor why this would be done unconditionally.  Could you provide
some information on the problem you're trying to solve here?  This feels
like something that should be a core feature.

> +static int reg_fixed_regulator_notifier(struct notifier_block *nb,
> +					unsigned long event, void *data)
> +{
> +	struct fixed_voltage_data *priv =
> +		container_of(nb, struct fixed_voltage_data, nb);
> +	struct regulator_dev *rdev = priv->dev;
> +
> +	if (event != REGULATOR_EVENT_UNDER_VOLTAGE_WARN &&
> +	    event != REGULATOR_EVENT_UNDER_VOLTAGE)
> +		return NOTIFY_OK;
> +
> +	regulator_notifier_call_chain(rdev, event, NULL);

This would be better written as a switch statement for extensibility,
and it's not clear why the filtering?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support
  2023-10-10  8:59 [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support Oleksij Rempel
  2023-10-10  8:59 ` [PATCH v1 2/3] regulator: fixed: add support for under-voltage IRQ Oleksij Rempel
  2023-10-10  8:59 ` [PATCH v1 3/3] regulator: fixed: forward under-voltage events Oleksij Rempel
@ 2023-10-10 12:20 ` Mark Brown
  2023-10-10 18:13 ` Rob Herring
  2023-10-25 17:47 ` (subset) " Mark Brown
  4 siblings, 0 replies; 15+ messages in thread
From: Mark Brown @ 2023-10-10 12:20 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: Liam Girdwood, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	kernel, linux-kernel, devicetree

[-- Attachment #1: Type: text/plain, Size: 310 bytes --]

On Tue, Oct 10, 2023 at 10:59:04AM +0200, Oleksij Rempel wrote:
> Add under-voltage interrupt support. This can be used with simple
> regulators having no other way to communicate an under-voltage event
> except as by toggling some GPIO line.

This doesn't apply against current code, please check and resend.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-10 12:19   ` Mark Brown
@ 2023-10-10 12:55     ` Oleksij Rempel
  2023-10-10 13:39       ` Oleksij Rempel
  2023-10-10 17:19       ` Mark Brown
  0 siblings, 2 replies; 15+ messages in thread
From: Oleksij Rempel @ 2023-10-10 12:55 UTC (permalink / raw)
  To: Mark Brown
  Cc: Liam Girdwood, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	kernel, linux-kernel, devicetree

Hi,

On Tue, Oct 10, 2023 at 01:19:36PM +0100, Mark Brown wrote:
> On Tue, Oct 10, 2023 at 10:59:06AM +0200, Oleksij Rempel wrote:
> > Add handler to forward under-voltage events.
> > On systems for more or less complicated regulator chains we need to
> > forward under-voltage events to actual driver which need to react on
> > them.
> 
> It isn't clear to me why this would be implemented in one specific
> driver, nor why this would be done unconditionally.  Could you provide
> some information on the problem you're trying to solve here?

The hardware I am working with has an under-voltage sensor on the 24V
supply regulator and some backup capacitors to run SoC for 100ms. I want
to forward under-voltage events across a chain of different regulators
to a designated consumer. For instance, to the mmc driver, enabling it
to initiate shutdown before power loss occurs.  Additionally, a bit can
be set in the volatile memory of a scratch pad in an RTC clock to record
sudden power loss, which can be checked on the next system start.

> This feels like something that should be a core feature.

Agreed. I am relatively new to the regulator framework and am uncertain
about the optimal location for registering the event forwarding. Could
you advise on this?

> > +static int reg_fixed_regulator_notifier(struct notifier_block *nb,
> > +					unsigned long event, void *data)
> > +{
> > +	struct fixed_voltage_data *priv =
> > +		container_of(nb, struct fixed_voltage_data, nb);
> > +	struct regulator_dev *rdev = priv->dev;
> > +
> > +	if (event != REGULATOR_EVENT_UNDER_VOLTAGE_WARN &&
> > +	    event != REGULATOR_EVENT_UNDER_VOLTAGE)
> > +		return NOTIFY_OK;
> > +
> > +	regulator_notifier_call_chain(rdev, event, NULL);
> 
> This would be better written as a switch statement for extensibility,

ack.

> and it's not clear why the filtering?

I started with a conservative approach because I'm not sure about the
possible effects of forwarding all events. If forwarding all events is a
good idea, I can do it.

Regards,
Oleksij
-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-10 12:55     ` Oleksij Rempel
@ 2023-10-10 13:39       ` Oleksij Rempel
  2023-10-10 17:19       ` Mark Brown
  1 sibling, 0 replies; 15+ messages in thread
From: Oleksij Rempel @ 2023-10-10 13:39 UTC (permalink / raw)
  To: Mark Brown
  Cc: Liam Girdwood, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	kernel, linux-kernel, devicetree

On Tue, Oct 10, 2023 at 02:55:31PM +0200, Oleksij Rempel wrote:
> Hi,
> 
> On Tue, Oct 10, 2023 at 01:19:36PM +0100, Mark Brown wrote:
> > On Tue, Oct 10, 2023 at 10:59:06AM +0200, Oleksij Rempel wrote:
> > > Add handler to forward under-voltage events.
> > > On systems for more or less complicated regulator chains we need to
> > > forward under-voltage events to actual driver which need to react on
> > > them.
> > 
> > It isn't clear to me why this would be implemented in one specific
> > driver, nor why this would be done unconditionally.  Could you provide
> > some information on the problem you're trying to solve here?
> 
> The hardware I am working with has an under-voltage sensor on the 24V
> supply regulator and some backup capacitors to run SoC for 100ms. I want
> to forward under-voltage events across a chain of different regulators
> to a designated consumer. For instance, to the mmc driver, enabling it
> to initiate shutdown before power loss occurs.  Additionally, a bit can
> be set in the volatile memory of a scratch pad in an RTC clock to record
> sudden power loss, which can be checked on the next system start.

The bit picture of my HW may potentially be even more advanced:
- some regulator chain paths are disabled by the HW. With other words
  under-voltage event is converted to fail or disable. There is nothing
  what software can do, but it will be good to reflect it on the SW
  side for diagnostic.
- some paths can be disabled by software to get some more milliseconds
  of life and complete emergency shutdown task.


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-10 12:55     ` Oleksij Rempel
  2023-10-10 13:39       ` Oleksij Rempel
@ 2023-10-10 17:19       ` Mark Brown
  2023-10-11  7:59         ` Oleksij Rempel
  1 sibling, 1 reply; 15+ messages in thread
From: Mark Brown @ 2023-10-10 17:19 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: Liam Girdwood, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	kernel, linux-kernel, devicetree, Naresh Solanki, zev,
	Sebastian Reichel, linux-pm

[-- Attachment #1: Type: text/plain, Size: 2724 bytes --]

On Tue, Oct 10, 2023 at 02:55:31PM +0200, Oleksij Rempel wrote:

> The hardware I am working with has an under-voltage sensor on the 24V
> supply regulator and some backup capacitors to run SoC for 100ms. I want
> to forward under-voltage events across a chain of different regulators
> to a designated consumer. For instance, to the mmc driver, enabling it
> to initiate shutdown before power loss occurs.  Additionally, a bit can
> be set in the volatile memory of a scratch pad in an RTC clock to record
> sudden power loss, which can be checked on the next system start.

So it sounds like the underlying need is to flag the notifications from
one of the regulators as being system wide and then take action based on
those notifications somewhere basically disconnected?  That does seem
like a good use case.

The MMC doesn't specifically care that it is handling a regulator
notification, it more wants to know that the system is dying and doesn't
really care how we figured that out so if we can hook it into a system
level notificaiton it'd be happy and would also be able to handle other
critical faults.  I would have thought that we should have some
mechanisms for this already for RAS type stuff but I'm drawing a blank
on what it actually is if there is an existing abstraction.  It could
potentially go through userspace though there's latency concerns there
which might not be ideal, there should at least be some policy for
userspace.

For the regulator itself we probably want a way to identify regulators
as being system critical so they start notifying.  It would be tempting
to just do that by default but that would likely cause some issues for
example with regulators for things like SD cards which are more likely
to get hardware problems that don't comprimise the entire system.  We
could do that with DT, either a property or some sort of runtime
consumer, but it might be better to have a control in sysfs that
userspace can turn on?  OTOH the ability do something about this depends
on specific hardware design...

I've copied in Sebastian since this sounds like the sort of thing that
power supplies might have some kind of handling for, or at least if we
need to add something we should make it so that the power supplies can
be joined up to it.  I do see temperature and capacity alerts in the
sysfs ABI for power supplies, but nothing for voltage.

I've also coped in Naresh and Zev who've been discussing something
vaugely similar with userspace notifications for the userspace consumer
- it's not the same thing given that you don't specifically need
userspace to be involved here but it feels like it might have something
of a similar shape, or at least there might be some shared interest.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support
  2023-10-10  8:59 [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support Oleksij Rempel
                   ` (2 preceding siblings ...)
  2023-10-10 12:20 ` [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support Mark Brown
@ 2023-10-10 18:13 ` Rob Herring
  2023-10-25 17:47 ` (subset) " Mark Brown
  4 siblings, 0 replies; 15+ messages in thread
From: Rob Herring @ 2023-10-10 18:13 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: Liam Girdwood, Mark Brown, Krzysztof Kozlowski, Conor Dooley,
	kernel, linux-kernel, devicetree

On Tue, Oct 10, 2023 at 10:59:04AM +0200, Oleksij Rempel wrote:
> Add under-voltage interrupt support. This can be used with simple
> regulators having no other way to communicate an under-voltage event
> except as by toggling some GPIO line.
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> 
> diff --git a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
> index ac0281b1cceb..0f8760ed2fb1 100644
> --- a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
> +++ b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
> @@ -100,6 +100,14 @@ properties:
>    vin-supply:
>      description: Input supply phandle.
> 
> +  interrupts:
> +    maxItems: 1
> +    description:
> +      Under-voltage interrupt
> +
> +  interrupt-names:
> +    const: under-voltage

No need for a name. If there's ever a 2nd, it should be a specific 
binding, not fixed-regulator.

> +
>  required:
>    - compatible
>    - regulator-name
> ---
>  .../devicetree/bindings/regulator/fixed-regulator.yaml    | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
> index ac0281b1cceb..0f8760ed2fb1 100644
> --- a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
> +++ b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml

I think you've got some problems with your setup...

> @@ -100,6 +100,14 @@ properties:
>    vin-supply:
>      description: Input supply phandle.
>  
> +  interrupts:
> +    maxItems: 1
> +    description:
> +      Under-voltage interrupt
> +
> +  interrupt-names:
> +    const: under-voltage
> +
>  required:
>    - compatible
>    - regulator-name
> -- 
> 2.39.2
> 

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-10 17:19       ` Mark Brown
@ 2023-10-11  7:59         ` Oleksij Rempel
  2023-10-11 11:38           ` Mark Brown
  2023-10-21  0:26           ` Sebastian Reichel
  0 siblings, 2 replies; 15+ messages in thread
From: Oleksij Rempel @ 2023-10-11  7:59 UTC (permalink / raw)
  To: Mark Brown
  Cc: Liam Girdwood, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	kernel, linux-kernel, devicetree, Naresh Solanki, zev,
	Sebastian Reichel, linux-pm

On Tue, Oct 10, 2023 at 06:19:59PM +0100, Mark Brown wrote:
> On Tue, Oct 10, 2023 at 02:55:31PM +0200, Oleksij Rempel wrote:
> 
> > The hardware I am working with has an under-voltage sensor on the 24V
> > supply regulator and some backup capacitors to run SoC for 100ms. I want
> > to forward under-voltage events across a chain of different regulators
> > to a designated consumer. For instance, to the mmc driver, enabling it
> > to initiate shutdown before power loss occurs.  Additionally, a bit can
> > be set in the volatile memory of a scratch pad in an RTC clock to record
> > sudden power loss, which can be checked on the next system start.
> 
> So it sounds like the underlying need is to flag the notifications from
> one of the regulators as being system wide and then take action based on
> those notifications somewhere basically disconnected?  That does seem
> like a good use case.
> 
> The MMC doesn't specifically care that it is handling a regulator
> notification, it more wants to know that the system is dying and doesn't
> really care how we figured that out so if we can hook it into a system
> level notificaiton it'd be happy and would also be able to handle other
> critical faults.  I would have thought that we should have some
> mechanisms for this already for RAS type stuff but I'm drawing a blank
> on what it actually is if there is an existing abstraction.  It could
> potentially go through userspace though there's latency concerns there
> which might not be ideal, there should at least be some policy for
> userspace.

The project I'm working prefers reducing user space daemons to configure and
enforce RAS policies due to time and financial budget constraints. The customer
is inclined to invest only in essential infrastructure.

Configuration through the device tree and kernel defaults is preferable.
For instance, having a default kernel governor that doesn’t require user
space configuration aligns with the project’s objectives.

While a proper UAPI might not be implemented in the first run, the
design will allow for it to be added and extended by other projects in
the future.

> For the regulator itself we probably want a way to identify regulators
> as being system critical so they start notifying.  It would be tempting
> to just do that by default but that would likely cause some issues for
> example with regulators for things like SD cards which are more likely
> to get hardware problems that don't comprimise the entire system.  We
> could do that with DT, either a property or some sort of runtime
> consumer, but it might be better to have a control in sysfs that
> userspace can turn on?  OTOH the ability do something about this depends
> on specific hardware design...
> 
> I've copied in Sebastian since this sounds like the sort of thing that
> power supplies might have some kind of handling for, or at least if we
> need to add something we should make it so that the power supplies can
> be joined up to it.  I do see temperature and capacity alerts in the
> sysfs ABI for power supplies, but nothing for voltage.

Thank you for pointing towards the power supply framework. Given the hardware
design of my project, I can envision mapping the following states and
properties within this framework:

1. States:
   - POWER_SUPPLY_STATUS_FULL: When the capacitor is fully charged.
   - POWER_SUPPLY_STATUS_DISCHARGING: Triggered when an under-voltage event is
                                      detected.

2. Technology:
   - POWER_SUPPLY_TECHNOLOGY_CAPACITOR

3. Capacity Level:
   - Post under-voltage detection, the system would immediately transition to
     POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL state.

4. Properties:
   - POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 100ms, representing the time until
                                          complete power loss.
   - POWER_SUPPLY_TYPE_MAINS: Under normal operation.
   - POWER_SUPPLY_TYPE_BATTERY: Triggered when under-voltage is detected.

Considering the above mapping, my initial step would be to create a simple
regulator coupled (if regulator is still needed in this casr) with a Device
Tree (DT) based power supply driver.  This setup would align with the existing
power supply framework, with a notable extension being the system-wide
notification for emergency shutdown upon under-voltage detection.

> I've also coped in Naresh and Zev who've been discussing something
> vaugely similar with userspace notifications for the userspace consumer
> - it's not the same thing given that you don't specifically need
> userspace to be involved here but it feels like it might have something
> of a similar shape, or at least there might be some shared interest.

Regards,
Oleksij
-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-11  7:59         ` Oleksij Rempel
@ 2023-10-11 11:38           ` Mark Brown
  2023-10-12  7:08             ` Matti Vaittinen
  2023-10-21  0:26           ` Sebastian Reichel
  1 sibling, 1 reply; 15+ messages in thread
From: Mark Brown @ 2023-10-11 11:38 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: Liam Girdwood, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	kernel, linux-kernel, devicetree, Naresh Solanki, zev,
	Sebastian Reichel, linux-pm

[-- Attachment #1: Type: text/plain, Size: 2109 bytes --]

On Wed, Oct 11, 2023 at 09:59:31AM +0200, Oleksij Rempel wrote:

> Configuration through the device tree and kernel defaults is preferable.
> For instance, having a default kernel governor that doesn’t require user
> space configuration aligns with the project’s objectives.

That's policy though...

> 
> > For the regulator itself we probably want a way to identify regulators
> > as being system critical so they start notifying.  It would be tempting
> > to just do that by default but that would likely cause some issues for
> > example with regulators for things like SD cards which are more likely
> > to get hardware problems that don't comprimise the entire system.  We
> > could do that with DT, either a property or some sort of runtime
> > consumer, but it might be better to have a control in sysfs that
> > userspace can turn on?  OTOH the ability do something about this depends
> > on specific hardware design...
> > 
> > I've copied in Sebastian since this sounds like the sort of thing that
> > power supplies might have some kind of handling for, or at least if we
> > need to add something we should make it so that the power supplies can
> > be joined up to it.  I do see temperature and capacity alerts in the
> > sysfs ABI for power supplies, but nothing for voltage.
> 
> Thank you for pointing towards the power supply framework. Given the hardware
> design of my project, I can envision mapping the following states and
> properties within this framework:

There's also hw_failure_emergency_poweroff() which looks like exactly
what you're trying to trigger here.

> Considering the above mapping, my initial step would be to create a simple
> regulator coupled (if regulator is still needed in this casr) with a Device
> Tree (DT) based power supply driver.  This setup would align with the existing
> power supply framework, with a notable extension being the system-wide
> notification for emergency shutdown upon under-voltage detection.

It sounds like this is actually a regulator regardless of if it also
appears via some other API.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-11 11:38           ` Mark Brown
@ 2023-10-12  7:08             ` Matti Vaittinen
  2023-10-12 11:13               ` Mark Brown
  0 siblings, 1 reply; 15+ messages in thread
From: Matti Vaittinen @ 2023-10-12  7:08 UTC (permalink / raw)
  To: Mark Brown
  Cc: Oleksij Rempel, Liam Girdwood, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, kernel, linux-kernel, devicetree, Naresh Solanki,
	zev, Sebastian Reichel, linux-pm

[-- Attachment #1: Type: text/plain, Size: 3437 bytes --]

On Wed, Oct 11, 2023 at 12:38:19PM +0100, Mark Brown wrote:
> On Wed, Oct 11, 2023 at 09:59:31AM +0200, Oleksij Rempel wrote:
> 
> > Configuration through the device tree and kernel defaults is preferable.
> > For instance, having a default kernel governor that doesn’t require user
> > space configuration aligns with the project’s objectives.
> 
> That's policy though...
> 
> > 
> > > For the regulator itself we probably want a way to identify regulators
> > > as being system critical so they start notifying.  It would be tempting

Can the "criticality" could be determined by the severity (ERROR vs WARNING)?

> > > to just do that by default but that would likely cause some issues for
> > > example with regulators for things like SD cards which are more likely
> > > to get hardware problems that don't comprimise the entire system.  We

"comprimise the entire system" sounds (to my ears) exactly the
difference between WARNING and ERROR notifications.

> > > could do that with DT, either a property or some sort of runtime
> > > consumer, but it might be better to have a control in sysfs that
> > > userspace can turn on?  OTOH the ability do something about this depends
> > > on specific hardware design...
> > > 
> > > I've copied in Sebastian since this sounds like the sort of thing that
> > > power supplies might have some kind of handling for, or at least if we
> > > need to add something we should make it so that the power supplies can
> > > be joined up to it.  I do see temperature and capacity alerts in the
> > > sysfs ABI for power supplies, but nothing for voltage.
> > 
> > Thank you for pointing towards the power supply framework. Given the hardware
> > design of my project, I can envision mapping the following states and
> > properties within this framework:
> 
> There's also hw_failure_emergency_poweroff() which looks like exactly
> what you're trying to trigger here.

There is already a path from regulator notification handling to the
hw_failure_emergency_poweroff() - although only when handling the IRQs
fail and this failure is marked as fatal.

> > Considering the above mapping, my initial step would be to create a simple
> > regulator coupled (if regulator is still needed in this casr) with a Device
> > Tree (DT) based power supply driver.  This setup would align with the existing
> > power supply framework, with a notable extension being the system-wide
> > notification for emergency shutdown upon under-voltage detection.
> 
> It sounds like this is actually a regulator regardless of if it also
> appears via some other API.

I wonder if it would make sense to add a 'protector' in regulator core.
The 'protector' could register to listen the notifications from those
regulators which have some
'regulator-fatal-notifications = <list of notifications>;' -property
defined in device-tree.

In my eyes the device-tree is correct place for this information
because whether an "anomaly" in regulator output compromises the system
is a property of hardware.

Yours,
	-- Matti

-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-12  7:08             ` Matti Vaittinen
@ 2023-10-12 11:13               ` Mark Brown
  0 siblings, 0 replies; 15+ messages in thread
From: Mark Brown @ 2023-10-12 11:13 UTC (permalink / raw)
  To: Matti Vaittinen
  Cc: Oleksij Rempel, Liam Girdwood, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, kernel, linux-kernel, devicetree, Naresh Solanki,
	zev, Sebastian Reichel, linux-pm

[-- Attachment #1: Type: text/plain, Size: 294 bytes --]

On Thu, Oct 12, 2023 at 10:08:40AM +0300, Matti Vaittinen wrote:

> In my eyes the device-tree is correct place for this information
> because whether an "anomaly" in regulator output compromises the system
> is a property of hardware.

Yes, it's mainly the handling that has a policy element.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v1 3/3] regulator: fixed: forward under-voltage events
  2023-10-11  7:59         ` Oleksij Rempel
  2023-10-11 11:38           ` Mark Brown
@ 2023-10-21  0:26           ` Sebastian Reichel
  1 sibling, 0 replies; 15+ messages in thread
From: Sebastian Reichel @ 2023-10-21  0:26 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: Mark Brown, Liam Girdwood, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, kernel, linux-kernel, devicetree, Naresh Solanki,
	zev, linux-pm

Hi,

On Wed, Oct 11, 2023 at 09:59:31AM +0200, Oleksij Rempel wrote:
> On Tue, Oct 10, 2023 at 06:19:59PM +0100, Mark Brown wrote:
> > On Tue, Oct 10, 2023 at 02:55:31PM +0200, Oleksij Rempel wrote:
> > > The hardware I am working with has an under-voltage sensor on the 24V
> > > supply regulator and some backup capacitors to run SoC for 100ms. I want
> > > to forward under-voltage events across a chain of different regulators
> > > to a designated consumer. For instance, to the mmc driver, enabling it
> > > to initiate shutdown before power loss occurs.  Additionally, a bit can
> > > be set in the volatile memory of a scratch pad in an RTC clock to record
> > > sudden power loss, which can be checked on the next system start.
> > 
> > So it sounds like the underlying need is to flag the notifications from
> > one of the regulators as being system wide and then take action based on
> > those notifications somewhere basically disconnected?  That does seem
> > like a good use case.
> > 
> > The MMC doesn't specifically care that it is handling a regulator
> > notification, it more wants to know that the system is dying and doesn't
> > really care how we figured that out so if we can hook it into a system
> > level notificaiton it'd be happy and would also be able to handle other
> > critical faults.  I would have thought that we should have some
> > mechanisms for this already for RAS type stuff but I'm drawing a blank
> > on what it actually is if there is an existing abstraction.  It could
> > potentially go through userspace though there's latency concerns there
> > which might not be ideal, there should at least be some policy for
> > userspace.
> 
> The project I'm working prefers reducing user space daemons to configure and
> enforce RAS policies due to time and financial budget constraints. The customer
> is inclined to invest only in essential infrastructure.
> 
> Configuration through the device tree and kernel defaults is preferable.
> For instance, having a default kernel governor that doesn’t require user
> space configuration aligns with the project’s objectives.
> 
> While a proper UAPI might not be implemented in the first run, the
> design will allow for it to be added and extended by other projects in
> the future.
> 
> > For the regulator itself we probably want a way to identify regulators
> > as being system critical so they start notifying.  It would be tempting
> > to just do that by default but that would likely cause some issues for
> > example with regulators for things like SD cards which are more likely
> > to get hardware problems that don't comprimise the entire system.  We
> > could do that with DT, either a property or some sort of runtime
> > consumer, but it might be better to have a control in sysfs that
> > userspace can turn on?  OTOH the ability do something about this depends
> > on specific hardware design...
> > 
> > I've copied in Sebastian since this sounds like the sort of thing that
> > power supplies might have some kind of handling for, or at least if we
> > need to add something we should make it so that the power supplies can
> > be joined up to it.  I do see temperature and capacity alerts in the
> > sysfs ABI for power supplies, but nothing for voltage.
> 
> Thank you for pointing towards the power supply framework. Given the hardware
> design of my project, I can envision mapping the following states and
> properties within this framework:
> 
> 1. States:
>    - POWER_SUPPLY_STATUS_FULL: When the capacitor is fully charged.
>    - POWER_SUPPLY_STATUS_DISCHARGING: Triggered when an under-voltage event is
>                                       detected.
> 
> 2. Technology:
>    - POWER_SUPPLY_TECHNOLOGY_CAPACITOR
> 
> 3. Capacity Level:
>    - Post under-voltage detection, the system would immediately transition to
>      POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL state.
> 
> 4. Properties:
>    - POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 100ms, representing the time until
>                                           complete power loss.
>    - POWER_SUPPLY_TYPE_MAINS: Under normal operation.
>    - POWER_SUPPLY_TYPE_BATTERY: Triggered when under-voltage is detected.

I don't know if power-supply is the best fit for this, but if you
continue on this path:

POWER_SUPPLY_TYPE is supposed to be fixed. You either have a battery
or a charger. If you want to go the power-supply way, you need two
devices: One POWER_SUPPLY_TYPE_MAINS for the regulator charging the
capacitor and one POWER_SUPPLY_TYPE_BATTERY for the capacitor. The
MAINS device is important to keep power_supply_is_system_supplied()
working as expected.

Note, that there is no generic solution how to handle critical
battery events in the power-supply framework at the moment. On
Laptops userspace handles early poweroff based on the information
supplied by the kernel. Right now there is one phone battery driver
doing 'orderly_poweroff(true)' on critical battery state. That's
about it.

Greetings,

-- Sebastian

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: (subset) [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support
  2023-10-10  8:59 [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support Oleksij Rempel
                   ` (3 preceding siblings ...)
  2023-10-10 18:13 ` Rob Herring
@ 2023-10-25 17:47 ` Mark Brown
  4 siblings, 0 replies; 15+ messages in thread
From: Mark Brown @ 2023-10-25 17:47 UTC (permalink / raw)
  To: Liam Girdwood, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Oleksij Rempel
  Cc: kernel, linux-kernel, devicetree

On Tue, 10 Oct 2023 10:59:04 +0200, Oleksij Rempel wrote:
> Add under-voltage interrupt support. This can be used with simple
> regulators having no other way to communicate an under-voltage event
> except as by toggling some GPIO line.
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> 
> diff --git a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
> index ac0281b1cceb..0f8760ed2fb1 100644
> --- a/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
> +++ b/Documentation/devicetree/bindings/regulator/fixed-regulator.yaml
> @@ -100,6 +100,14 @@ properties:
>    vin-supply:
>      description: Input supply phandle.
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next

Thanks!

[1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support
      commit: 0ab1dc9c657f30434ca55a3dcc87e624af0b2116
[2/3] regulator: fixed: add support for under-voltage IRQ
      commit: ecb6f1f456144e9ade5a492192287decbeef4cfe

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-10-25 17:48 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-10  8:59 [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support Oleksij Rempel
2023-10-10  8:59 ` [PATCH v1 2/3] regulator: fixed: add support for under-voltage IRQ Oleksij Rempel
2023-10-10  8:59 ` [PATCH v1 3/3] regulator: fixed: forward under-voltage events Oleksij Rempel
2023-10-10 12:19   ` Mark Brown
2023-10-10 12:55     ` Oleksij Rempel
2023-10-10 13:39       ` Oleksij Rempel
2023-10-10 17:19       ` Mark Brown
2023-10-11  7:59         ` Oleksij Rempel
2023-10-11 11:38           ` Mark Brown
2023-10-12  7:08             ` Matti Vaittinen
2023-10-12 11:13               ` Mark Brown
2023-10-21  0:26           ` Sebastian Reichel
2023-10-10 12:20 ` [PATCH v1 1/3] regulator: dt-bindings: fixed-regulator: Add under-voltage interrupt support Mark Brown
2023-10-10 18:13 ` Rob Herring
2023-10-25 17:47 ` (subset) " Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).