Skip to contentSkip to conversation

mfd: rk808: add reboot support to rk808.c

PATCH Superseded Peter Geis 3 participants 6 messages Dec 17, 2021 – Dec 20, 2021

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Peter Geis

author
#0 lore raw

PATCH v2 1/1 mfd: rk808: add reboot support to rk808.c

This adds reboot support to the rk808o pmic driver and enables it for the rk809 and rk817 devices.
This only enables if the rockchip,system-power-controller flag is set.

---
This patch was created to address issues with psci-reset on rk356x chips. Until the rk356x series ATF open source code is released so we can fix the issue at the source, this is the only way to ensure reliable resetting on devices using these chips.

After testing the rk808 (thanks Robin!), it was found DEV_OFF_RST does not reset the PMIC to a power on state. Since the rk805 and rk818 match this register layout, I'm removing support for all three in the v2. It may be possible to add support to them using an RTC wakeup, but that has not been explored and is outside the scope of this patch.

Changelog:
V2:
- Squash the patch from Frank Wunderlich for rk809 support.
- Remove support for the rk805, rk808, and rk818 devices.
- Only register the reset handler for supported devices.
- Remove unnecessary dev_err and dev_warn statements.
- Register the reset handler directly

 drivers/mfd/rk808.c       | 43 +++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/rk808.h |  1 +
 2 files changed, 44 insertions(+)
drivers/mfd/rk808.c +43 -0
index b181fe401330..6fa7fbf9b695 100644
@@ -19,6 +19,7 @@
 #include <linux/module.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/reboot.h>
 
 struct rk808_reg_data {
 	int addr;
@@ -543,6 +544,7 @@ static void rk808_pm_power_off(void)
 		reg = RK808_DEVCTRL_REG,
 		bit = DEV_OFF_RST;
 		break;
+	case RK809_ID:
 	case RK817_ID:
 		reg = RK817_SYS_CFG(3);
 		bit = DEV_OFF;
@@ -559,6 +561,34 @@ static void rk808_pm_power_off(void)
 		dev_err(&rk808_i2c_client->dev, "Failed to shutdown device!\n");
 }
 
+static int rk808_restart_notify(struct notifier_block *this, unsigned long mode, void *cmd)
+{
+	int ret;
+	unsigned int reg, bit;
+	struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
+
+	switch (rk808->variant) {
+	case RK809_ID:
+	case RK817_ID:
+		reg = RK817_SYS_CFG(3);
+		bit = DEV_RST;
+		break;
+
+	default:
+		return NOTIFY_DONE;
+	}
+	ret = regmap_update_bits(rk808->regmap, reg, bit, bit);
+	if (ret)
+		dev_err(&rk808_i2c_client->dev, "Failed to restart device!\n");
+
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block rk808_restart_handler = {
+	.notifier_call = rk808_restart_notify,
+	.priority = 255,
+};
+
 static void rk8xx_shutdown(struct i2c_client *client)
 {
 	struct rk808 *rk808 = i2c_get_clientdata(client);
@@ -727,6 +757,19 @@ static int rk808_probe(struct i2c_client *client,
 	if (of_property_read_bool(np, "rockchip,system-power-controller")) {
 		rk808_i2c_client = client;
 		pm_power_off = rk808_pm_power_off;
+
+		switch (rk808->variant) {
+		case RK809_ID:
+		case RK817_ID:
+			ret = register_restart_handler(&rk808_restart_handler);
+			break;
+		default:
+			dev_info(&client->dev, "pmic controlled board reset not supported\n");
+			break;
+		}
+
+		if (ret)
+			dev_err(&client->dev, "failed to register restart handler, %d\n", ret);
 	}
 
 	return 0;
include/linux/mfd/rk808.h +1 -0
index a96e6d43ca06..58602032e642 100644
@@ -373,6 +373,7 @@ enum rk805_reg {
 #define SWITCH2_EN	BIT(6)
 #define SWITCH1_EN	BIT(5)
 #define DEV_OFF_RST	BIT(3)
+#define DEV_RST		BIT(2)
 #define DEV_OFF		BIT(0)
 #define RTC_STOP	BIT(0)
 
Signature · 1 line
2.25.1
#1 lore raw

17.12.2021 17:55, Peter Geis пишет:

Quoted 29 lines

This adds reboot support to the rk808o pmic driver and enables it for the rk809 and rk817 devices.
This only enables if the rockchip,system-power-controller flag is set.

---
This patch was created to address issues with psci-reset on rk356x chips. Until the rk356x series ATF open source code is released so we can fix the issue at the source, this is the only way to ensure reliable resetting on devices using these chips.

After testing the rk808 (thanks Robin!), it was found DEV_OFF_RST does not reset the PMIC to a power on state. Since the rk805 and rk818 match this register layout, I'm removing support for all three in the v2. It may be possible to add support to them using an RTC wakeup, but that has not been explored and is outside the scope of this patch.

Changelog:
V2:
- Squash the patch from Frank Wunderlich for rk809 support.
- Remove support for the rk805, rk808, and rk818 devices.
- Only register the reset handler for supported devices.
- Remove unnecessary dev_err and dev_warn statements.
- Register the reset handler directly

 drivers/mfd/rk808.c       | 43 +++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/rk808.h |  1 +
 2 files changed, 44 insertions(+)

...

drivers/mfd/rk808.c static void rk808_pm_power_off(void) · line 590

Quoted 4 lines
drivers/mfd/rk808.c +4 -0
+static struct notifier_block rk808_restart_handler = {
+	.notifier_call = rk808_restart_notify,
+	.priority = 255,
+};

Hello!

Please use the default 128 priority if there are no other conflicting handlers on this RK.

drivers/mfd/rk808.c static int rk808_probe(struct i2c_client *client, · line 764

Quoted 12 lines

static void rk8xx_shutdown(struct i2c_client *client) {

drivers/mfd/rk808.c +5 -0
 	struct rk808 *rk808 = i2c_get_clientdata(client);
@@ -727,6 +757,19 @@ static int rk808_probe(struct i2c_client *client,
 	if (of_property_read_bool(np, "rockchip,system-power-controller")) {
 		rk808_i2c_client = client;
 		pm_power_off = rk808_pm_power_off;
+
+		switch (rk808->variant) {
+		case RK809_ID:
+		case RK817_ID:
+			ret = register_restart_handler(&rk808_restart_handler);

There is no corresponding unregister_restart_handler(), which you should add to rk808_remove(). Otherwise kernel will crash on reboot if you'll unload this driver module.

drivers/mfd/rk808.c static int rk808_probe(struct i2c_client *client, · line 767

Quoted 3 lines
drivers/mfd/rk808.c +3 -0
+			break;
+		default:
+			dev_info(&client->dev, "pmic controlled board reset not supported\n");

I'd set ret=0 explicitly here. Later on somebody may change the code and ret won't be zero anymore, this is not an uncommon trouble in kernel.

Quoted 5 lines
drivers/mfd/rk808.c +5 -0
+			break;
+		}
+
+		if (ret)
+			dev_err(&client->dev, "failed to register restart handler, %d\n", ret);
author
#2 lore raw
Quoted 39 lines · Dmitry Osipenko

17.12.2021 17:55, Peter Geis пишет:

Quoted 29 lines

This adds reboot support to the rk808o pmic driver and enables it for the rk809 and rk817 devices.
This only enables if the rockchip,system-power-controller flag is set.

---
This patch was created to address issues with psci-reset on rk356x chips. Until the rk356x series ATF open source code is released so we can fix the issue at the source, this is the only way to ensure reliable resetting on devices using these chips.

After testing the rk808 (thanks Robin!), it was found DEV_OFF_RST does not reset the PMIC to a power on state. Since the rk805 and rk818 match this register layout, I'm removing support for all three in the v2. It may be possible to add support to them using an RTC wakeup, but that has not been explored and is outside the scope of this patch.

Changelog:
V2:
- Squash the patch from Frank Wunderlich for rk809 support.
- Remove support for the rk805, rk808, and rk818 devices.
- Only register the reset handler for supported devices.
- Remove unnecessary dev_err and dev_warn statements.
- Register the reset handler directly

 drivers/mfd/rk808.c       | 43 +++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/rk808.h |  1 +
 2 files changed, 44 insertions(+)

...

drivers/mfd/rk808.c static void rk808_pm_power_off(void) · line 590

Quoted 4 lines
drivers/mfd/rk808.c +4 -0
+static struct notifier_block rk808_restart_handler = {
+     .notifier_call = rk808_restart_notify,
+     .priority = 255,
+};

Hello!

Quoted 2 lines

Please use the default 128 priority if there are no other conflicting handlers on this RK.

Unfortunately the psci-reboot handler is set to 129, I'll adjust this to 192 which is in line with other PMIC reboot drivers.

Quoted 17 lines

drivers/mfd/rk808.c static int rk808_probe(struct i2c_client *client, · line 764

Quoted 13 lines

static void rk8xx_shutdown(struct i2c_client *client)

 {
      struct rk808 *rk808 = i2c_get_clientdata(client);
drivers/mfd/rk808.c +5 -0
@@ -727,6 +757,19 @@ static int rk808_probe(struct i2c_client *client,
      if (of_property_read_bool(np, "rockchip,system-power-controller")) {
              rk808_i2c_client = client;
              pm_power_off = rk808_pm_power_off;
+
+             switch (rk808->variant) {
+             case RK809_ID:
+             case RK817_ID:
+                     ret = register_restart_handler(&rk808_restart_handler);

There is no corresponding unregister_restart_handler(), which you should add to rk808_remove(). Otherwise kernel will crash on reboot if you'll unload this driver module.

Thanks, added!

Quoted 7 lines

drivers/mfd/rk808.c static int rk808_probe(struct i2c_client *client, · line 767

Quoted 4 lines
drivers/mfd/rk808.c +3 -0
+                     break;
+             default:
+                     dev_info(&client->dev, "pmic controlled board reset not supported\n");

I'd set ret=0 explicitly here. Later on somebody may change the code and ret won't be zero anymore, this is not an uncommon trouble in kernel.

It took me a moment to see the logic here, but I understand it now.

Quoted 9 lines

drivers/mfd/rk808.c static int rk808_probe(struct i2c_client *client, · line 772

Quoted 6 lines
drivers/mfd/rk808.c +5 -0
+                     break;
+             }
+
+             if (ret)
+                     dev_err(&client->dev, "failed to register restart handler, %d\n", ret);

Thank you for the review.

#3 lore raw

17.12.2021 21:16, Peter Geis пишет:

Quoted 3 lines
Quoted 3 lines

drivers/mfd/rk808.c static int rk808_probe(struct i2c_client *client, · line 767

Quoted 3 lines
drivers/mfd/rk808.c +3 -0
+                     break;
+             default:
+                     dev_info(&client->dev, "pmic controlled board reset not supported\n");

I'd change this dev_info to dev_dbg to not clutter KMSG.

Quoted 4 lines
Quoted 2 lines

I'd set ret=0 explicitly here. Later on somebody may change the code and ret won't be zero anymore, this is not an uncommon trouble in kernel.

It took me a moment to see the logic here, but I understand it now.

Could be even better to place the error message simply right after the register_restart_handler().

author
#4 lore raw
Quoted 7 lines · Dmitry Osipenko

17.12.2021 21:16, Peter Geis пишет:

Quoted 3 lines
Quoted 3 lines

drivers/mfd/rk808.c static int rk808_probe(struct i2c_client *client, · line 767

Quoted 3 lines
drivers/mfd/rk808.c +3 -0
+                     break;
+             default:
+                     dev_info(&client->dev, "pmic controlled board reset not supported\n");

I'd change this dev_info to dev_dbg to not clutter KMSG.

I'd prefer to leave this as info, since the device is designated as the system power controller but it is only capable of powering down the system, not rebooting it.
But on second thought, anyone who's making these changes would be investigating the driver anyway.
So I'll change it to dev_dbg.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I'd set ret=0 explicitly here. Later on somebody may change the code and ret won't be zero anymore, this is not an uncommon trouble in kernel.

It took me a moment to see the logic here, but I understand it now.

Could be even better to place the error message simply right after the register_restart_handler().

Good point, thanks

#5 lore raw
Quoted 7 lines · Peter Geis
Quoted 6 lines · Dmitry Osipenko

17.12.2021 21:16, Peter Geis пишет:

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/mfd/rk808.c +3 -0
+                     break;
+             default:
+                     dev_info(&client->dev, "pmic controlled board reset not supported\n");
Quoted 8 lines
Quoted 1 line

I'd change this dev_info to dev_dbg to not clutter KMSG.

I'd prefer to leave this as info, since the device is designated as the system power controller but it is only capable of powering down the system, not rebooting it.
But on second thought, anyone who's making these changes would be investigating the driver anyway.
So I'll change it to dev_dbg.

Indeed, this is the expected case for RK808, which has to be system-power-controller if you want shutdown to actually power off.

Cheers,
Robin.