Skip to contentSkip to conversation

mfd: tps65219: Add support for soft shutdown via sys-off API

PATCH In review Jerome Neanne 4 participants 6 messages Feb 3, 2023 – Mar 3, 2023

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

PATCH 1/1 mfd: tps65219: Add support for soft shutdown via sys-off API

Use new API for power-off mode support:

sys-off API allows support of shutdown handler and restart handler.

Shutdown was not supported before that enhancement. This is required for platform that are not using PSCI.

Test:
- restart:

  # reboot
  Default is cold reset:
  # cat /sys/kernel/reboot/mode
  Switch boot mode to warm reset:
  # echo warm > /sys/kernel/reboot/mode
- power-off:
  # halt

Tested on AM62-SP-SK board.

---
 drivers/mfd/tps65219.c | 45 +++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)
drivers/mfd/tps65219.c +34 -11
index 0e402fda206b..c134f3f6e202 100644
@@ -25,25 +25,34 @@ static int tps65219_cold_reset(struct tps65219 *tps)
 				  TPS65219_MFP_COLD_RESET_I2C_CTRL_MASK);
 }
 
-static int tps65219_restart(struct notifier_block *this,
-			    unsigned long reboot_mode, void *cmd)
+static int tps65219_soft_shutdown(struct tps65219 *tps)
 {
-	struct tps65219 *tps;
+	return regmap_update_bits(tps->regmap, TPS65219_REG_MFP_CTRL,
+				  TPS65219_MFP_I2C_OFF_REQ_MASK,
+				  TPS65219_MFP_I2C_OFF_REQ_MASK);
+}
 
-	tps = container_of(this, struct tps65219, nb);
+static int tps65219_power_off_handler(struct sys_off_data *data)
+{
+	tps65219_soft_shutdown(data->cb_data);
+	return NOTIFY_DONE;
+}
 
+static int tps65219_restart(struct tps65219 *tps,
+			    unsigned long reboot_mode)
+{
 	if (reboot_mode == REBOOT_WARM)
 		tps65219_warm_reset(tps);
 	else
 		tps65219_cold_reset(tps);
-
 	return NOTIFY_DONE;
 }
 
-static struct notifier_block pmic_rst_restart_nb = {
-	.notifier_call = tps65219_restart,
-	.priority = 200,
-};
+static int tps65219_restart_handler(struct sys_off_data *data)
+{
+	tps65219_restart(data->cb_data, data->mode);
+	return NOTIFY_DONE;
+}
 
 static const struct resource tps65219_pwrbutton_resources[] = {
 	DEFINE_RES_IRQ_NAMED(TPS65219_INT_PB_FALLING_EDGE_DETECT, "falling"),
@@ -269,13 +278,27 @@ static int tps65219_probe(struct i2c_client *client)
 		}
 	}
 
-	tps->nb = pmic_rst_restart_nb;
-	ret = register_restart_handler(&tps->nb);
+	ret = devm_register_sys_off_handler(tps->dev,
+					    SYS_OFF_MODE_RESTART,
+					    SYS_OFF_PRIO_HIGH,
+					    tps65219_restart_handler,
+					    tps);
+
 	if (ret) {
 		dev_err(tps->dev, "cannot register restart handler, %d\n", ret);
 		return ret;
 	}
 
+	ret = devm_register_sys_off_handler(tps->dev,
+					    SYS_OFF_MODE_POWER_OFF,
+					    SYS_OFF_PRIO_DEFAULT,
+					    tps65219_power_off_handler,
+					    tps);
+	if (ret) {
+		dev_err(tps->dev, "failed to register sys-off handler: %d\n",
+			ret);
+		return ret;
+	}
 	return 0;
 }
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 19 lines · Jerome Neanne

Use new API for power-off mode support:

sys-off API allows support of shutdown handler and restart handler.

Shutdown was not supported before that enhancement. This is required for platform that are not using PSCI.

Test:
- restart:

  # reboot
  Default is cold reset:
  # cat /sys/kernel/reboot/mode
  Switch boot mode to warm reset:
  # echo warm > /sys/kernel/reboot/mode
- power-off:
  # halt
Quoted 4 lines

Tested on AM62-SP-SK board.

A review from Andrew would be helpful here.

Quoted 85 lines
---
 drivers/mfd/tps65219.c | 45 +++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)
drivers/mfd/tps65219.c +34 -11
index 0e402fda206b..c134f3f6e202 100644
@@ -25,25 +25,34 @@ static int tps65219_cold_reset(struct tps65219 *tps)
 				  TPS65219_MFP_COLD_RESET_I2C_CTRL_MASK);
 }
 
-static int tps65219_restart(struct notifier_block *this,
-			    unsigned long reboot_mode, void *cmd)
+static int tps65219_soft_shutdown(struct tps65219 *tps)
 {
-	struct tps65219 *tps;
+	return regmap_update_bits(tps->regmap, TPS65219_REG_MFP_CTRL,
+				  TPS65219_MFP_I2C_OFF_REQ_MASK,
+				  TPS65219_MFP_I2C_OFF_REQ_MASK);
+}
 
-	tps = container_of(this, struct tps65219, nb);
+static int tps65219_power_off_handler(struct sys_off_data *data)
+{
+	tps65219_soft_shutdown(data->cb_data);
+	return NOTIFY_DONE;
+}
 
+static int tps65219_restart(struct tps65219 *tps,
+			    unsigned long reboot_mode)
+{
 	if (reboot_mode == REBOOT_WARM)
 		tps65219_warm_reset(tps);
 	else
 		tps65219_cold_reset(tps);
-
 	return NOTIFY_DONE;
 }
 
-static struct notifier_block pmic_rst_restart_nb = {
-	.notifier_call = tps65219_restart,
-	.priority = 200,
-};
+static int tps65219_restart_handler(struct sys_off_data *data)
+{
+	tps65219_restart(data->cb_data, data->mode);
+	return NOTIFY_DONE;
+}
 
 static const struct resource tps65219_pwrbutton_resources[] = {
 	DEFINE_RES_IRQ_NAMED(TPS65219_INT_PB_FALLING_EDGE_DETECT, "falling"),
@@ -269,13 +278,27 @@ static int tps65219_probe(struct i2c_client *client)
 		}
 	}
 
-	tps->nb = pmic_rst_restart_nb;
-	ret = register_restart_handler(&tps->nb);
+	ret = devm_register_sys_off_handler(tps->dev,
+					    SYS_OFF_MODE_RESTART,
+					    SYS_OFF_PRIO_HIGH,
+					    tps65219_restart_handler,
+					    tps);
+
 	if (ret) {
 		dev_err(tps->dev, "cannot register restart handler, %d\n", ret);
 		return ret;
 	}
 
+	ret = devm_register_sys_off_handler(tps->dev,
+					    SYS_OFF_MODE_POWER_OFF,
+					    SYS_OFF_PRIO_DEFAULT,
+					    tps65219_power_off_handler,
+					    tps);
+	if (ret) {
+		dev_err(tps->dev, "failed to register sys-off handler: %d\n",
+			ret);
+		return ret;
+	}
 	return 0;
 }
 
Signature · 1 line
2.34.1
Signature · 1 line
Lee Jones [李琼斯]
Reviewed-by
#2 lore raw
Quoted 11 lines · Lee Jones
Quoted 9 lines · Jerome Neanne

Use new API for power-off mode support:

sys-off API allows support of shutdown handler and restart handler.

Shutdown was not supported before that enhancement. This is required for platform that are not using PSCI.

Not sure what platform doesn't have PSCI off, since you tested on AM62-SK I'm guessing you manually disabled the PSCI off for testing?

Anyway I don't see any huge issues with the code itself, small comment below.

Quoted 73 lines
Quoted 10 lines

Test:
- restart:

   # reboot
   Default is cold reset:
   # cat /sys/kernel/reboot/mode
   Switch boot mode to warm reset:
   # echo warm > /sys/kernel/reboot/mode
- power-off:
   # halt
Quoted 4 lines

Tested on AM62-SP-SK board.

A review from Andrew would be helpful here.

Quoted 56 lines
---
  drivers/mfd/tps65219.c | 45 +++++++++++++++++++++++++++++++-----------
  1 file changed, 34 insertions(+), 11 deletions(-)
drivers/mfd/tps65219.c +18 -9
index 0e402fda206b..c134f3f6e202 100644
@@ -25,25 +25,34 @@ static int tps65219_cold_reset(struct tps65219 *tps)
  				  TPS65219_MFP_COLD_RESET_I2C_CTRL_MASK);
  }
 
-static int tps65219_restart(struct notifier_block *this,
-			    unsigned long reboot_mode, void *cmd)
+static int tps65219_soft_shutdown(struct tps65219 *tps)
  {
-	struct tps65219 *tps;
+	return regmap_update_bits(tps->regmap, TPS65219_REG_MFP_CTRL,
+				  TPS65219_MFP_I2C_OFF_REQ_MASK,
+				  TPS65219_MFP_I2C_OFF_REQ_MASK);
+}
 
-	tps = container_of(this, struct tps65219, nb);
+static int tps65219_power_off_handler(struct sys_off_data *data)
+{
+	tps65219_soft_shutdown(data->cb_data);
+	return NOTIFY_DONE;
+}
 
+static int tps65219_restart(struct tps65219 *tps,
+			    unsigned long reboot_mode)
+{
  	if (reboot_mode == REBOOT_WARM)
  		tps65219_warm_reset(tps);
  	else
  		tps65219_cold_reset(tps);
-
  	return NOTIFY_DONE;
  }
 
-static struct notifier_block pmic_rst_restart_nb = {
-	.notifier_call = tps65219_restart,
-	.priority = 200,
-};
+static int tps65219_restart_handler(struct sys_off_data *data)
+{
+	tps65219_restart(data->cb_data, data->mode);
+	return NOTIFY_DONE;
+}
 
  static const struct resource tps65219_pwrbutton_resources[] = {
  	DEFINE_RES_IRQ_NAMED(TPS65219_INT_PB_FALLING_EDGE_DETECT, "falling"),
@@ -269,13 +278,27 @@ static int tps65219_probe(struct i2c_client *client)
  		}
  	}
 
Quoted 5 lines

drivers/mfd/tps65219.c static int tps65219_probe(struct i2c_client *client) · line 283

Quoted 5 lines
drivers/mfd/tps65219.c +3 -2
-	tps->nb = pmic_rst_restart_nb;
-	ret = register_restart_handler(&tps->nb);
+	ret = devm_register_sys_off_handler(tps->dev,
+					    SYS_OFF_MODE_RESTART,
+					    SYS_OFF_PRIO_HIGH,

Why not default prio? SYS_OFF_PRIO_DEFAULT

Then you can use this new helper devm_register_restart_handler()

Quoted 8 lines
Quoted 8 lines
drivers/mfd/tps65219.c +3 -0
+					    tps65219_restart_handler,
+					    tps);
+
  	if (ret) {
  		dev_err(tps->dev, "cannot register restart handler, %d\n", ret);
  		return ret;
  	}
 
Quoted 5 lines

drivers/mfd/tps65219.c static int tps65219_probe(struct i2c_client *client) · line 296

Quoted 5 lines
drivers/mfd/tps65219.c +5 -0
+	ret = devm_register_sys_off_handler(tps->dev,
+					    SYS_OFF_MODE_POWER_OFF,
+					    SYS_OFF_PRIO_DEFAULT,
+					    tps65219_power_off_handler,
+					    tps);

devm_register_power_off_handler()?

Otherwise I see no major issues,

Andrew

Quoted 12 lines
Quoted 11 lines
drivers/mfd/tps65219.c +5 -0
+	if (ret) {
+		dev_err(tps->dev, "failed to register sys-off handler: %d\n",
+			ret);
+		return ret;
+	}
  	return 0;
  }
 
Signature · 1 line
2.34.1
#3 lore raw
Quoted 85 lines · Andrew Davis
Quoted 11 lines · Lee Jones
Quoted 9 lines · Jerome Neanne

Use new API for power-off mode support:

sys-off API allows support of shutdown handler and restart handler.

Shutdown was not supported before that enhancement. This is required for platform that are not using PSCI.

Not sure what platform doesn't have PSCI off, since you tested on AM62-SK I'm guessing you manually disabled the PSCI off for testing?

Anyway I don't see any huge issues with the code itself, small comment below.

Quoted 67 lines
Quoted 10 lines

Test:
- restart:

   # reboot
   Default is cold reset:
   # cat /sys/kernel/reboot/mode
   Switch boot mode to warm reset:
   # echo warm > /sys/kernel/reboot/mode
- power-off:
   # halt
Quoted 4 lines

Tested on AM62-SP-SK board.

A review from Andrew would be helpful here.

Quoted 50 lines

---
  drivers/mfd/tps65219.c | 45 +++++++++++++++++++++++++++++++-----------

  1 file changed, 34 insertions(+), 11 deletions(-)
drivers/mfd/tps65219.c +13 -4
index 0e402fda206b..c134f3f6e202 100644
@@ -25,25 +25,34 @@ static int tps65219_cold_reset(struct tps65219 *tps)
                     TPS65219_MFP_COLD_RESET_I2C_CTRL_MASK);
   }
-static int tps65219_restart(struct notifier_block *this,
-                unsigned long reboot_mode, void *cmd)
+static int tps65219_soft_shutdown(struct tps65219 *tps)
   {
-    struct tps65219 *tps;
+    return regmap_update_bits(tps->regmap, TPS65219_REG_MFP_CTRL,
+                  TPS65219_MFP_I2C_OFF_REQ_MASK,
+                  TPS65219_MFP_I2C_OFF_REQ_MASK);
+}
-    tps = container_of(this, struct tps65219, nb);
+static int tps65219_power_off_handler(struct sys_off_data *data)
+{
+    tps65219_soft_shutdown(data->cb_data);
+    return NOTIFY_DONE;
+}
+static int tps65219_restart(struct tps65219 *tps,
+                unsigned long reboot_mode)
+{
       if (reboot_mode == REBOOT_WARM)
           tps65219_warm_reset(tps);

      else
          tps65219_cold_reset(tps);
-
      return NOTIFY_DONE;
  }

drivers/mfd/tps65219.c +5 -4
-static struct notifier_block pmic_rst_restart_nb = {
-    .notifier_call = tps65219_restart,
-    .priority = 200,
-};
+static int tps65219_restart_handler(struct sys_off_data *data)
+{
+    tps65219_restart(data->cb_data, data->mode);
+    return NOTIFY_DONE;
+}

  static const struct resource tps65219_pwrbutton_resources[] = { DEFINE_RES_IRQ_NAMED(TPS65219_INT_PB_FALLING_EDGE_DETECT, "falling"),

drivers/mfd/tps65219.c +0 -0
@@ -269,13 +278,27 @@ static int tps65219_probe(struct i2c_client 
 *client)
           }
Quoted 8 lines
Quoted 6 lines

drivers/mfd/tps65219.c static int tps65219_probe(struct i2c_client *client) · line 283

Quoted 6 lines
drivers/mfd/tps65219.c +3 -2
       }
-    tps->nb = pmic_rst_restart_nb;
-    ret = register_restart_handler(&tps->nb);
+    ret = devm_register_sys_off_handler(tps->dev,
+                        SYS_OFF_MODE_RESTART,
+                        SYS_OFF_PRIO_HIGH,

Why not default prio? SYS_OFF_PRIO_DEFAULT

I'm not completely clear about PRIO recommendations. Will follow your suggestion.

Quoted 2 lines

Then you can use this new helper devm_register_restart_handler()

Sure!

Quoted 15 lines
Quoted 13 lines

drivers/mfd/tps65219.c static int tps65219_probe(struct i2c_client *client) · line 296

Quoted 13 lines
drivers/mfd/tps65219.c +8 -0
+                        tps65219_restart_handler,
+                        tps);
+
       if (ret) {
           dev_err(tps->dev, "cannot register restart handler, %d\n", ret);
           return ret;
       }
+    ret = devm_register_sys_off_handler(tps->dev,
+                        SYS_OFF_MODE_POWER_OFF,
+                        SYS_OFF_PRIO_DEFAULT,
+                        tps65219_power_off_handler,
+                        tps);
Quoted 2 lines

devm_register_power_off_handler()?

Oh yes, right, this is solving the PRIO question by construction. This is definitely a better option

Quoted 17 lines

Otherwise I see no major issues,

Andrew

Quoted 11 lines
Quoted 10 lines
drivers/mfd/tps65219.c +5 -0
+    if (ret) {
+        dev_err(tps->dev, "failed to register sys-off handler: %d\n",
+            ret);
+        return ret;
+    }
       return 0;
   }
Signature · 1 line
2.34.1
#4 lore raw
In reply to Jerome Neanne
Quoted 19 lines · Jerome Neanne

Use new API for power-off mode support:

sys-off API allows support of shutdown handler and restart handler.

Shutdown was not supported before that enhancement. This is required for platform that are not using PSCI.

Test:
- restart:

  # reboot
  Default is cold reset:
  # cat /sys/kernel/reboot/mode
  Switch boot mode to warm reset:
  # echo warm > /sys/kernel/reboot/mode
- power-off:
  # halt
Quoted 2 lines

Tested on AM62-SP-SK board.

There is no -SP-SK that I am aware of.. Do you mean -LP-SK? [...]

Signature · 2 lines
Regards
Vignesh
#5 lore raw
Quoted 22 lines · Vignesh Raghavendra
Quoted 19 lines · Jerome Neanne

Use new API for power-off mode support:

sys-off API allows support of shutdown handler and restart handler.

Shutdown was not supported before that enhancement. This is required for platform that are not using PSCI.

Test:
- restart:

   # reboot
   Default is cold reset:
   # cat /sys/kernel/reboot/mode
   Switch boot mode to warm reset:
   # echo warm > /sys/kernel/reboot/mode
- power-off:
   # halt
Quoted 4 lines
Quoted 2 lines

Tested on AM62-SP-SK board.

There is no -SP-SK that I am aware of.. Do you mean -LP-SK?

You are right, this is a typo. It's LP-SK

Quoted 2 lines

[...]