Skip to contentSkip to conversation

sunxi: Add option to prevent boot on plug-in

PATCH In review Andre Przywara 2 participants 5 messages Jan 21, 2022

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

v2 cover sunxi: Add option to prevent boot on plug-in

Sometimes it is desirable to prevent a board from automatically booting as soon as the power cable is plugged in. For boards with an AXP PMIC, we can actually query the power up source.

Add a AXP_DISABLE_BOOT_ON_POWERON Kconfig option to enable this behaviour.
Patch 1 prepares the PMIC header files to export the same symbol, which Chris' patch 2 then uses to do the actual work.

I rearranged Chris' code to get rid of the nasty #ifdef's. This function is already a "good" example of why #ifdefs are bad, we don't need to add to that. I will try to clean that up further, but for now this doesn't need to block this patch.

Cheers,
Andre.

Andre Przywara (1):
pmic: axp: define ALDO_IN startup bit

Chris Morgan (1):
sunxi: Add option to prevent booting on power plug-in

 arch/arm/mach-sunxi/Kconfig | 10 ++++++++++
 board/sunxi/board.c         | 11 +++++++++++
 include/axp152.h            |  2 ++
 include/axp209.h            |  3 ++-
 include/axp221.h            |  3 ++-
 include/axp305.h            |  3 +++
 include/axp809.h            |  3 ++-
 include/axp818.h            |  3 ++-
 8 files changed, 34 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 pmic: axp: define ALDO_IN startup bit

Most AXP PMICs feature a "startup source" register, which keeps information about how the PMIC started operation. Bit 0 in there means it has been started by "plugging in the power cable".

Define a symbol in each PMIC's header file to be able to use that register and bit later on.

---
 include/axp152.h | 2 ++
 include/axp209.h | 3 ++-
 include/axp221.h | 3 ++-
 include/axp305.h | 3 +++
 include/axp809.h | 3 ++-
 include/axp818.h | 3 ++-
 6 files changed, 13 insertions(+), 4 deletions(-)
include/axp152.h +2 -0
index 10d845fec4..bac6526a36 100644
@@ -16,6 +16,8 @@ enum axp152_reg {
 
 /* For axp_gpio.c */
 #ifdef CONFIG_AXP152_POWER
+#define AXP_POWER_STATUS		0x00
+#define AXP_POWER_STATUS_ALDO_IN		BIT(0)
 #define AXP_GPIO0_CTRL			0x90
 #define AXP_GPIO1_CTRL			0x91
 #define AXP_GPIO2_CTRL			0x92
include/axp209.h +2 -1
index 30399a8d62..414f88a32c 100644
@@ -76,7 +76,8 @@ enum axp209_reg {
 /* For axp_gpio.c */
 #ifdef CONFIG_AXP209_POWER
 #define AXP_POWER_STATUS		0x00
-#define AXP_POWER_STATUS_VBUS_PRESENT	BIT(5)
+#define AXP_POWER_STATUS_ALDO_IN		BIT(0)
+#define AXP_POWER_STATUS_VBUS_PRESENT		BIT(5)
 #define AXP_GPIO0_CTRL			0x90
 #define AXP_GPIO1_CTRL			0x92
 #define AXP_GPIO2_CTRL			0x93
include/axp221.h +2 -1
index a02e9b4f64..8dfcc5b5a2 100644
@@ -52,7 +52,8 @@
 /* For axp_gpio.c */
 #ifdef CONFIG_AXP221_POWER
 #define AXP_POWER_STATUS		0x00
-#define AXP_POWER_STATUS_VBUS_PRESENT		(1 << 5)
+#define AXP_POWER_STATUS_ALDO_IN		BIT(0)
+#define AXP_POWER_STATUS_VBUS_PRESENT		BIT(5)
 #define AXP_VBUS_IPSOUT			0x30
 #define AXP_VBUS_IPSOUT_DRIVEBUS		(1 << 2)
 #define AXP_MISC_CTRL			0x8f
include/axp305.h +3 -0
index 225c5040a3..0a42bc6804 100644
@@ -15,3 +15,6 @@ enum axp305_reg {
 #define AXP305_OUTPUT_CTRL1_DCDCD_EN	(1 << 3)
 
 #define AXP305_POWEROFF			(1 << 7)
+
+#define AXP_POWER_STATUS		0x00
+#define AXP_POWER_STATUS_ALDO_IN		BIT(0)
include/axp809.h +2 -1
index 430dbef622..8082e402e2 100644
@@ -46,7 +46,8 @@
 /* For axp_gpio.c */
 #ifdef CONFIG_AXP809_POWER
 #define AXP_POWER_STATUS		0x00
-#define AXP_POWER_STATUS_VBUS_PRESENT		(1 << 5)
+#define AXP_POWER_STATUS_ALDO_IN		BIT(0)
+#define AXP_POWER_STATUS_VBUS_PRESENT		BIT(5)
 #define AXP_VBUS_IPSOUT			0x30
 #define AXP_VBUS_IPSOUT_DRIVEBUS		(1 << 2)
 #define AXP_MISC_CTRL			0x8f
include/axp818.h +2 -1
index 8bac6b67ca..8ac517a2bf 100644
@@ -60,7 +60,8 @@
 /* For axp_gpio.c */
 #ifdef CONFIG_AXP818_POWER
 #define AXP_POWER_STATUS		0x00
-#define AXP_POWER_STATUS_VBUS_PRESENT		(1 << 5)
+#define AXP_POWER_STATUS_ALDO_IN		BIT(0)
+#define AXP_POWER_STATUS_VBUS_PRESENT		BIT(5)
 #define AXP_VBUS_IPSOUT			0x30
 #define AXP_VBUS_IPSOUT_DRIVEBUS		(1 << 2)
 #define AXP_MISC_CTRL			0x8f
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/2 sunxi: Add option to prevent booting on power plug-in

In reply to Andre Przywara

From: Chris Morgan <[email protected]>

For sunxi boards with the AXP209, AXP221, AXP809, and AXP818 PMICs (plus possibly others, I only confirmed the datasheets for these), it is sometimes desirable to not boot whenever the device is plugged in. An example would be when using the NTC CHIP inside a PocketCHIP.
This provides a configurable option to check if bit 0 of register 0 of the PMIC says it was powered because of a power button press (0) or a plug-in event (1). 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.

[Andre: reword to speak of boot, remove #ifdefs]

adapt board.c to generic AXP symbols

---
 arch/arm/mach-sunxi/Kconfig | 10 ++++++++++
 board/sunxi/board.c         | 11 +++++++++++
 2 files changed, 21 insertions(+)
arch/arm/mach-sunxi/Kconfig +10 -0
index 2c18cf02d1..d7f9a03152 100644
@@ -785,6 +785,16 @@ config AXP_GPIO
 	---help---
 	Say Y here to enable support for the gpio pins of the axp PMIC ICs.
 
+config AXP_DISABLE_BOOT_ON_POWERON
+	bool "Disable device boot on power plug-in"
+	depends on AXP209_POWER || AXP221_POWER || AXP809_POWER || AXP818_POWER
+	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 into the SPL 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 VIDEO_SUNXI
 	bool "Enable graphical uboot console on HDMI, LCD or VGA"
 	depends on !MACH_SUN8I_A83T
board/sunxi/board.c +11 -0
index fdbcd40269..3b5e7f3cdc 100644
@@ -27,6 +27,7 @@
 #include <asm/arch/dram.h>
 #include <asm/arch/mmc.h>
 #include <asm/arch/prcm.h>
+#include <asm/arch/pmic_bus.h>
 #include <asm/arch/spl.h>
 #include <asm/global_data.h>
 #include <linux/delay.h>
@@ -601,6 +602,16 @@ void sunxi_board_init(void)
 	defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
 	power_failed = axp_init();
 
+	if (IS_ENABLED(AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {
+		u8 boot_reason;
+
+		pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
+		if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
+			printf("Power on by plug-in, shutting down.\n");
+			pmic_bus_write(0x32, BIT(7));
+		}
+	}
+
 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
 	defined CONFIG_AXP818_POWER
 	power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Signature · 1 line
2.25.1
Tested-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] sunxi: Add option to prevent booting on power plug-in

Quoted 13 lines · Andre Przywara

From: Chris Morgan <[email protected]>

For sunxi boards with the AXP209, AXP221, AXP809, and AXP818 PMICs (plus possibly others, I only confirmed the datasheets for these), it is sometimes desirable to not boot whenever the device is plugged in. An example would be when using the NTC CHIP inside a PocketCHIP.
This provides a configurable option to check if bit 0 of register 0 of the PMIC says it was powered because of a power button press (0) or a plug-in event (1). 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.

Quoted 45 lines
[Andre: reword to speak of boot, remove #ifdefs]

adapt board.c to generic AXP symbols

---
 arch/arm/mach-sunxi/Kconfig | 10 ++++++++++
 board/sunxi/board.c         | 11 +++++++++++
 2 files changed, 21 insertions(+)
arch/arm/mach-sunxi/Kconfig +10 -0
index 2c18cf02d1..d7f9a03152 100644
@@ -785,6 +785,16 @@ config AXP_GPIO
 	---help---
 	Say Y here to enable support for the gpio pins of the axp PMIC ICs.
 
+config AXP_DISABLE_BOOT_ON_POWERON
+	bool "Disable device boot on power plug-in"
+	depends on AXP209_POWER || AXP221_POWER || AXP809_POWER || AXP818_POWER
+	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 into the SPL 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 VIDEO_SUNXI
 	bool "Enable graphical uboot console on HDMI, LCD or VGA"
 	depends on !MACH_SUN8I_A83T
board/sunxi/board.c +1 -0
index fdbcd40269..3b5e7f3cdc 100644
@@ -27,6 +27,7 @@
 #include <asm/arch/dram.h>
 #include <asm/arch/mmc.h>
 #include <asm/arch/prcm.h>
+#include <asm/arch/pmic_bus.h>
 #include <asm/arch/spl.h>
 #include <asm/global_data.h>
 #include <linux/delay.h>

board/sunxi/board.c void sunxi_board_init(void) · line 605

Quoted 4 lines
board/sunxi/board.c +1 -0
@@ -601,6 +602,16 @@ void sunxi_board_init(void)
 	defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
 	power_failed = axp_init();
 
+	if (IS_ENABLED(AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {

This doesn't work for me unless I change this to
IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON)

With this change though, it works just fine on my use case.

Quoted 15 lines
board/sunxi/board.c +9 -0
+		u8 boot_reason;
+
+		pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
+		if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
+			printf("Power on by plug-in, shutting down.\n");
+			pmic_bus_write(0x32, BIT(7));
+		}
+	}
+
 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
 	defined CONFIG_AXP818_POWER
 	power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v2 2/2] sunxi: Add option to prevent booting on power plug-in

On Fri, 21 Jan 2022 10:35:33 -0600
Chris Morgan <[email protected]> wrote:

Hi Chris,

Quoted 15 lines · Andre Przywara
Quoted 13 lines

From: Chris Morgan <[email protected]>

For sunxi boards with the AXP209, AXP221, AXP809, and AXP818 PMICs (plus possibly others, I only confirmed the datasheets for these), it is sometimes desirable to not boot whenever the device is plugged in. An example would be when using the NTC CHIP inside a PocketCHIP.
This provides a configurable option to check if bit 0 of register 0 of the PMIC says it was powered because of a power button press (0) or a plug-in event (1). 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.

Quoted 1 line

Thanks (although please refrain from using this tag if it's actually broken, as you point out below)

Quoted 49 lines
Quoted 49 lines
[Andre: reword to speak of boot, remove #ifdefs]

adapt board.c to generic AXP symbols

---
 arch/arm/mach-sunxi/Kconfig | 10 ++++++++++
 board/sunxi/board.c         | 11 +++++++++++
 2 files changed, 21 insertions(+)
arch/arm/mach-sunxi/Kconfig +10 -0
index 2c18cf02d1..d7f9a03152 100644
@@ -785,6 +785,16 @@ config AXP_GPIO
 	---help---
 	Say Y here to enable support for the gpio pins of the axp PMIC ICs.
 
+config AXP_DISABLE_BOOT_ON_POWERON
+	bool "Disable device boot on power plug-in"
+	depends on AXP209_POWER || AXP221_POWER || AXP809_POWER || AXP818_POWER
+	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 into the SPL 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 VIDEO_SUNXI
 	bool "Enable graphical uboot console on HDMI, LCD or VGA"
 	depends on !MACH_SUN8I_A83T
board/sunxi/board.c +1 -0
index fdbcd40269..3b5e7f3cdc 100644
@@ -27,6 +27,7 @@
 #include <asm/arch/dram.h>
 #include <asm/arch/mmc.h>
 #include <asm/arch/prcm.h>
+#include <asm/arch/pmic_bus.h>
 #include <asm/arch/spl.h>
 #include <asm/global_data.h>
 #include <linux/delay.h>
@@ -601,6 +602,16 @@ void sunxi_board_init(void)
 	defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
 	power_failed = axp_init();
 
Quoted 4 lines

board/sunxi/board.c void sunxi_board_init(void) · line 605

Quoted 1 line
board/sunxi/board.c +1 -0
+	if (IS_ENABLED(AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) {  

This doesn't work for me unless I change this to
IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON)

Argh, of course, the usual mix up between CONFIG_IS_ENABLED and IS_ENABLED ;-)
I guess you meant with the rest of the line (&& !power_failed) as well?

I will give it some testing tonight, then queue this, if nobody screams.

Cheers,
Andre.

Quoted 18 lines

With this change though, it works just fine on my use case.

Quoted 15 lines
board/sunxi/board.c +9 -0
+		u8 boot_reason;
+
+		pmic_bus_read(AXP_POWER_STATUS, &boot_reason);
+		if (boot_reason & AXP_POWER_STATUS_ALDO_IN) {
+			printf("Power on by plug-in, shutting down.\n");
+			pmic_bus_write(0x32, BIT(7));
+		}
+	}
+
 #if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
 	defined CONFIG_AXP818_POWER
 	power_failed |= axp_set_dcdc1(CONFIG_AXP_DCDC1_VOLT);
Signature · 1 line
2.25.1