[2/2] mfd: stpmic1: add pmic poweroff via sys-off handler

Message ID 20230428112847.2146348-2-sean@geanix.com
State New
Headers
Series [1/2] mfd: stpmic1: fixup main control register and bits naming |

Commit Message

Sean Nyekjaer April 28, 2023, 11:28 a.m. UTC
  Use devm_register_sys_off_handler() that allows to register multiple
power-off handlers.

This can be enabled by adding "st,pmic-poweroff" to device-tree.

Signed-off-by: Sean Nyekjaer <sean@geanix.com>
---
 drivers/mfd/stpmic1.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
  

Comments

Sean Nyekjaer May 2, 2023, 6:17 a.m. UTC | #1
> On 28 Apr 2023, at 13.28, Sean Nyekjaer <sean@geanix.com> wrote:
> 
> Use devm_register_sys_off_handler() that allows to register multiple
> power-off handlers.
> 
> This can be enabled by adding "st,pmic-poweroff" to device-tree.
> 
> Signed-off-by: Sean Nyekjaer <sean@geanix.com>
> ---

Oh, I forgot the device-tree documentation.
Will add that after review…

/Sean

> drivers/mfd/stpmic1.c | 34 ++++++++++++++++++++++++++++++++++
> 1 file changed, 34 insertions(+)
> 
> diff --git a/drivers/mfd/stpmic1.c b/drivers/mfd/stpmic1.c
> index 4c9b18d9dec8..c628141eb03c 100644
> --- a/drivers/mfd/stpmic1.c
> +++ b/drivers/mfd/stpmic1.c
> @@ -7,6 +7,7 @@
> #include <linux/mfd/core.h>
> #include <linux/mfd/stpmic1.h>
> #include <linux/module.h>
> +#include <linux/reboot.h>
> #include <linux/of.h>
> #include <linux/of_irq.h>
> #include <linux/of_platform.h>
> @@ -117,6 +118,33 @@ static const struct regmap_irq_chip stpmic1_regmap_irq_chip = {
> .num_irqs = ARRAY_SIZE(stpmic1_irqs),
> };
> 
> +static int stpmic1_power_off(struct sys_off_data *data)
> +{
> + struct stpmic1 *ddata = data->cb_data;
> +
> + regmap_update_bits(ddata->regmap, MAIN_CR,
> +   SOFTWARE_SWITCH_OFF, SOFTWARE_SWITCH_OFF);
> +
> + return NOTIFY_DONE;
> +}
> +
> +static int stpmic1_power_off_prepare_init(struct stpmic1 *ddata)
> +{
> + int ret;
> +
> + ret = devm_register_sys_off_handler(ddata->dev,
> +    SYS_OFF_MODE_POWER_OFF,
> +    SYS_OFF_PRIO_DEFAULT,
> +    stpmic1_power_off,
> +    ddata);
> + if (ret) {
> + dev_err(ddata->dev, "failed to register sys-off handler: %d\n", ret);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> static int stpmic1_probe(struct i2c_client *i2c)
> {
> struct stpmic1 *ddata;
> @@ -159,6 +187,12 @@ static int stpmic1_probe(struct i2c_client *i2c)
> return ret;
> }
> 
> + if (of_property_read_bool(i2c->dev.of_node,  "st,pmic-poweroff")) {
> + ret = stpmic1_power_off_prepare_init(ddata);
> + if (ret)
> + return ret;
> + }
> +
> return devm_of_platform_populate(dev);
> }
> 
> -- 
> 2.40.0
>
  
Lee Jones May 15, 2023, 12:46 p.m. UTC | #2
On Fri, 28 Apr 2023, Sean Nyekjaer wrote:

> Use devm_register_sys_off_handler() that allows to register multiple
> power-off handlers.
> 
> This can be enabled by adding "st,pmic-poweroff" to device-tree.
> 
> Signed-off-by: Sean Nyekjaer <sean@geanix.com>
> ---
>  drivers/mfd/stpmic1.c | 34 ++++++++++++++++++++++++++++++++++
>  1 file changed, 34 insertions(+)

You need to submit the Device Tree binding with this patch.

> diff --git a/drivers/mfd/stpmic1.c b/drivers/mfd/stpmic1.c
> index 4c9b18d9dec8..c628141eb03c 100644
> --- a/drivers/mfd/stpmic1.c
> +++ b/drivers/mfd/stpmic1.c
> @@ -7,6 +7,7 @@
>  #include <linux/mfd/core.h>
>  #include <linux/mfd/stpmic1.h>
>  #include <linux/module.h>
> +#include <linux/reboot.h>
>  #include <linux/of.h>
>  #include <linux/of_irq.h>
>  #include <linux/of_platform.h>
> @@ -117,6 +118,33 @@ static const struct regmap_irq_chip stpmic1_regmap_irq_chip = {
>  	.num_irqs = ARRAY_SIZE(stpmic1_irqs),
>  };
>  
> +static int stpmic1_power_off(struct sys_off_data *data)
> +{
> +	struct stpmic1 *ddata = data->cb_data;
> +
> +	regmap_update_bits(ddata->regmap, MAIN_CR,
> +			   SOFTWARE_SWITCH_OFF, SOFTWARE_SWITCH_OFF);
> +
> +	return NOTIFY_DONE;
> +}
> +
> +static int stpmic1_power_off_prepare_init(struct stpmic1 *ddata)

This function looks superfluous.

Why not just call devm_register_sys_off_handler() inside the if?

> +{
> +	int ret;
> +
> +	ret = devm_register_sys_off_handler(ddata->dev,
> +					    SYS_OFF_MODE_POWER_OFF,
> +					    SYS_OFF_PRIO_DEFAULT,
> +					    stpmic1_power_off,
> +					    ddata);
> +	if (ret) {
> +		dev_err(ddata->dev, "failed to register sys-off handler: %d\n", ret);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
>  static int stpmic1_probe(struct i2c_client *i2c)
>  {
>  	struct stpmic1 *ddata;
> @@ -159,6 +187,12 @@ static int stpmic1_probe(struct i2c_client *i2c)
>  		return ret;
>  	}
>  
> +	if (of_property_read_bool(i2c->dev.of_node,  "st,pmic-poweroff")) {
> +		ret = stpmic1_power_off_prepare_init(ddata);
> +		if (ret)
> +			return ret;
> +	}
> +
>  	return devm_of_platform_populate(dev);
>  }
>  
> -- 
> 2.40.0
>
  

Patch

diff --git a/drivers/mfd/stpmic1.c b/drivers/mfd/stpmic1.c
index 4c9b18d9dec8..c628141eb03c 100644
--- a/drivers/mfd/stpmic1.c
+++ b/drivers/mfd/stpmic1.c
@@ -7,6 +7,7 @@ 
 #include <linux/mfd/core.h>
 #include <linux/mfd/stpmic1.h>
 #include <linux/module.h>
+#include <linux/reboot.h>
 #include <linux/of.h>
 #include <linux/of_irq.h>
 #include <linux/of_platform.h>
@@ -117,6 +118,33 @@  static const struct regmap_irq_chip stpmic1_regmap_irq_chip = {
 	.num_irqs = ARRAY_SIZE(stpmic1_irqs),
 };
 
+static int stpmic1_power_off(struct sys_off_data *data)
+{
+	struct stpmic1 *ddata = data->cb_data;
+
+	regmap_update_bits(ddata->regmap, MAIN_CR,
+			   SOFTWARE_SWITCH_OFF, SOFTWARE_SWITCH_OFF);
+
+	return NOTIFY_DONE;
+}
+
+static int stpmic1_power_off_prepare_init(struct stpmic1 *ddata)
+{
+	int ret;
+
+	ret = devm_register_sys_off_handler(ddata->dev,
+					    SYS_OFF_MODE_POWER_OFF,
+					    SYS_OFF_PRIO_DEFAULT,
+					    stpmic1_power_off,
+					    ddata);
+	if (ret) {
+		dev_err(ddata->dev, "failed to register sys-off handler: %d\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
 static int stpmic1_probe(struct i2c_client *i2c)
 {
 	struct stpmic1 *ddata;
@@ -159,6 +187,12 @@  static int stpmic1_probe(struct i2c_client *i2c)
 		return ret;
 	}
 
+	if (of_property_read_bool(i2c->dev.of_node,  "st,pmic-poweroff")) {
+		ret = stpmic1_power_off_prepare_init(ddata);
+		if (ret)
+			return ret;
+	}
+
 	return devm_of_platform_populate(dev);
 }