diff mbox series

[v2,2/2] rockchip: Add option to prevent booting on power plug-in

Message ID 20220527181820.24948-3-macroalpha82@gmail.com
State Accepted
Commit 30975fb73d51f65d95e9508e4af9c85badd5388c
Delegated to: Kever Yang
Headers show
Series Rockchip: Add option to prevent boot on plug-in | expand

Commit Message

Chris Morgan May 27, 2022, 6:18 p.m. UTC
From: Chris Morgan <macromorgan@hotmail.com>

For Rockchip boards with the all rk8xx series PMICs (excluding the
rk808), it is sometimes desirable to not boot whenever the device is
plugged in. An example would be for the Odroid Go Advance.

This provides a configurable option to check the PMIC says it was
powered because of a plug-in event. If the value is 1 and this option
is selected, the device shuts down shortly after printing a message
to console stating the reason why it's shutting down. Powering up the
board with the power button is not affected.

This patch parallels the work done in the following patch series:
https://lore.kernel.org/u-boot/20220121133732.2397273-1-andre.przywara@arm.com/

Signed-off-by: Chris Morgan <macromorgan@hotmail.com>
---
 arch/arm/mach-rockchip/Kconfig | 10 ++++++++++
 drivers/power/pmic/rk8xx.c     | 34 ++++++++++++++++++++++++++++++++++
 include/power/rk8xx_pmic.h     |  3 +++
 3 files changed, 47 insertions(+)

Comments

Jaehoon Chung May 30, 2022, 10:41 p.m. UTC | #1
On 5/28/22 03:18, Chris Morgan wrote:
> From: Chris Morgan <macromorgan@hotmail.com>
> 
> For Rockchip boards with the all rk8xx series PMICs (excluding the
> rk808), it is sometimes desirable to not boot whenever the device is
> plugged in. An example would be for the Odroid Go Advance.
> 
> This provides a configurable option to check the PMIC says it was
> powered because of a plug-in event. If the value is 1 and this option
> is selected, the device shuts down shortly after printing a message
> to console stating the reason why it's shutting down. Powering up the
> board with the power button is not affected.
> 
> This patch parallels the work done in the following patch series:
> https://lore.kernel.org/u-boot/20220121133732.2397273-1-andre.przywara@arm.com/
> 
> Signed-off-by: Chris Morgan <macromorgan@hotmail.com>

Reviewed-by: Jaehoon Chung <jh80.chung@samsung.com>

Best Regards,
Jaehoon Chung

> ---
>  arch/arm/mach-rockchip/Kconfig | 10 ++++++++++
>  drivers/power/pmic/rk8xx.c     | 34 ++++++++++++++++++++++++++++++++++
>  include/power/rk8xx_pmic.h     |  3 +++
>  3 files changed, 47 insertions(+)
> 
> diff --git a/arch/arm/mach-rockchip/Kconfig b/arch/arm/mach-rockchip/Kconfig
> index 18aff5480b..c561a77e6a 100644
> --- a/arch/arm/mach-rockchip/Kconfig
> +++ b/arch/arm/mach-rockchip/Kconfig
> @@ -361,6 +361,16 @@ config ROCKCHIP_BOOT_MODE_REG
>  	  The Soc will enter to different boot mode(defined in asm/arch-rockchip/boot_mode.h)
>  	  according to the value from this register.
>  
> +config ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON
> +	bool "Disable device boot on power plug-in"
> +	depends on PMIC_RK8XX
> +	default n
> +	---help---
> +	  Say Y here to prevent the device from booting up because of a plug-in
> +	  event. When set, the device will boot briefly to determine why it was
> +	  powered on, and if it was determined because of a plug-in event
> +	  instead of a button press event it will shut back off.
> +
>  config ROCKCHIP_STIMER
>  	bool "Rockchip STIMER support"
>  	default y
> diff --git a/drivers/power/pmic/rk8xx.c b/drivers/power/pmic/rk8xx.c
> index 1ffbecc02a..25ef621f8d 100644
> --- a/drivers/power/pmic/rk8xx.c
> +++ b/drivers/power/pmic/rk8xx.c
> @@ -51,6 +51,38 @@ U_BOOT_DRIVER(rk8xx_sysreset) = {
>  	.ops		= &rk8xx_sysreset_ops,
>  };
>  
> +/* In the event of a plug-in and the appropriate option has been
> + * selected, we simply shutdown instead of continue the normal boot
> + * process. Please note the rk808 is not supported as it doesn't
> + * have the appropriate register.
> + */
> +void rk8xx_off_for_plugin(struct udevice *dev)
> +{
> +	struct rk8xx_priv *priv = dev_get_priv(dev);
> +
> +	switch (priv->variant) {
> +	case RK805_ID:
> +	case RK816_ID:
> +	case RK818_ID:
> +		if (pmic_reg_read(dev, RK8XX_ON_SOURCE) & RK8XX_ON_PLUG_IN) {
> +			printf("Power Off due to plug-in event\n");
> +			pmic_clrsetbits(dev, REG_DEVCTRL, 0, BIT(0));
> +		}
> +		break;
> +	case RK809_ID:
> +	case RK817_ID:
> +		if (pmic_reg_read(dev, RK817_ON_SOURCE) & RK8XX_ON_PLUG_IN) {
> +			printf("Power Off due to plug-in event\n");
> +			pmic_clrsetbits(dev, RK817_REG_SYS_CFG3, 0,
> +					BIT(0));
> +		}
> +		break;
> +	default:
> +		printf("PMIC RK%x: Cannot read boot reason.\n",
> +		       priv->variant);
> +	}
> +}
> +
>  static struct reg_data rk817_init_reg[] = {
>  /* enable the under-voltage protection,
>   * the under-voltage protection will shutdown the LDO3 and reset the PMIC
> @@ -211,6 +243,8 @@ static int rk8xx_probe(struct udevice *dev)
>  		       pmic_reg_read(dev, on_source),
>  		       pmic_reg_read(dev, off_source));
>  	printf("\n");
> +	if (CONFIG_IS_ENABLED(ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON))
> +		rk8xx_off_for_plugin(dev);
>  
>  	return 0;
>  }
> diff --git a/include/power/rk8xx_pmic.h b/include/power/rk8xx_pmic.h
> index 8ff0af35c5..3cbfc02195 100644
> --- a/include/power/rk8xx_pmic.h
> +++ b/include/power/rk8xx_pmic.h
> @@ -214,6 +214,9 @@ enum {
>  #define RK817_ON_SOURCE		0xf5
>  #define RK817_OFF_SOURCE	0xf6
>  
> +#define RK8XX_ON_PWRON		BIT(7)
> +#define RK8XX_ON_PLUG_IN	BIT(6)
> +
>  struct reg_data {
>  	u8 reg;
>  	u8 val;
diff mbox series

Patch

diff --git a/arch/arm/mach-rockchip/Kconfig b/arch/arm/mach-rockchip/Kconfig
index 18aff5480b..c561a77e6a 100644
--- a/arch/arm/mach-rockchip/Kconfig
+++ b/arch/arm/mach-rockchip/Kconfig
@@ -361,6 +361,16 @@  config ROCKCHIP_BOOT_MODE_REG
 	  The Soc will enter to different boot mode(defined in asm/arch-rockchip/boot_mode.h)
 	  according to the value from this register.
 
+config ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON
+	bool "Disable device boot on power plug-in"
+	depends on PMIC_RK8XX
+	default n
+	---help---
+	  Say Y here to prevent the device from booting up because of a plug-in
+	  event. When set, the device will boot briefly to determine why it was
+	  powered on, and if it was determined because of a plug-in event
+	  instead of a button press event it will shut back off.
+
 config ROCKCHIP_STIMER
 	bool "Rockchip STIMER support"
 	default y
diff --git a/drivers/power/pmic/rk8xx.c b/drivers/power/pmic/rk8xx.c
index 1ffbecc02a..25ef621f8d 100644
--- a/drivers/power/pmic/rk8xx.c
+++ b/drivers/power/pmic/rk8xx.c
@@ -51,6 +51,38 @@  U_BOOT_DRIVER(rk8xx_sysreset) = {
 	.ops		= &rk8xx_sysreset_ops,
 };
 
+/* In the event of a plug-in and the appropriate option has been
+ * selected, we simply shutdown instead of continue the normal boot
+ * process. Please note the rk808 is not supported as it doesn't
+ * have the appropriate register.
+ */
+void rk8xx_off_for_plugin(struct udevice *dev)
+{
+	struct rk8xx_priv *priv = dev_get_priv(dev);
+
+	switch (priv->variant) {
+	case RK805_ID:
+	case RK816_ID:
+	case RK818_ID:
+		if (pmic_reg_read(dev, RK8XX_ON_SOURCE) & RK8XX_ON_PLUG_IN) {
+			printf("Power Off due to plug-in event\n");
+			pmic_clrsetbits(dev, REG_DEVCTRL, 0, BIT(0));
+		}
+		break;
+	case RK809_ID:
+	case RK817_ID:
+		if (pmic_reg_read(dev, RK817_ON_SOURCE) & RK8XX_ON_PLUG_IN) {
+			printf("Power Off due to plug-in event\n");
+			pmic_clrsetbits(dev, RK817_REG_SYS_CFG3, 0,
+					BIT(0));
+		}
+		break;
+	default:
+		printf("PMIC RK%x: Cannot read boot reason.\n",
+		       priv->variant);
+	}
+}
+
 static struct reg_data rk817_init_reg[] = {
 /* enable the under-voltage protection,
  * the under-voltage protection will shutdown the LDO3 and reset the PMIC
@@ -211,6 +243,8 @@  static int rk8xx_probe(struct udevice *dev)
 		       pmic_reg_read(dev, on_source),
 		       pmic_reg_read(dev, off_source));
 	printf("\n");
+	if (CONFIG_IS_ENABLED(ROCKCHIP_RK8XX_DISABLE_BOOT_ON_POWERON))
+		rk8xx_off_for_plugin(dev);
 
 	return 0;
 }
diff --git a/include/power/rk8xx_pmic.h b/include/power/rk8xx_pmic.h
index 8ff0af35c5..3cbfc02195 100644
--- a/include/power/rk8xx_pmic.h
+++ b/include/power/rk8xx_pmic.h
@@ -214,6 +214,9 @@  enum {
 #define RK817_ON_SOURCE		0xf5
 #define RK817_OFF_SOURCE	0xf6
 
+#define RK8XX_ON_PWRON		BIT(7)
+#define RK8XX_ON_PLUG_IN	BIT(6)
+
 struct reg_data {
 	u8 reg;
 	u8 val;