Introduce power off call chain API
Subsystems (2)
cover Introduce power off call chain API
Introduce power off call chain API that is inspired by the restart API. It allows to have multiple power off handlers invoked along the chain until system is powered off. For the starter this series converts couple NVIDIA Tegra drivers to the new API. Existing pm_power_off() method stays around and may be removed once all users will adopt the new API.
There were couple attempts in the past to add power off API from Guenter Roeck and Thierry Reding, but they were never completed. This is a somewhat simplified version which doesn't try to convert whole kernel to the new API at once, but solves immediate practical problem that we have on Nexus 7 Android tablet where device needs to chain power off methods.
Dmitry Osipenko (6):
notifier: Add blocking_notifier_call_chain_empty()
kernel: Add power off handler call chain API
kernel: Add devm_register_restart_handler()
mfd: max77620: Use power off call chain API soc/tegra: pmc: Use devm_register_restart_handler() soc/tegra: pmc: Add power off handler
drivers/mfd/max77620.c | 22 ++++-- drivers/soc/tegra/pmc.c | 62 ++++++++++++---- include/linux/mfd/max77620.h | 2 + include/linux/notifier.h | 2 + include/linux/reboot.h | 7 ++ kernel/notifier.c | 14 ++++ kernel/reboot.c | 138 ++++++++++++++++++++++++++++++++++- 7 files changed, 225 insertions(+), 22 deletions(-)
Signature · 1 line
2.32.0
PATCH 1/6 notifier: Add blocking_notifier_call_chain_empty()
Add blocking_notifier_call_chain_empty() that returns true if call chain is empty and false otherwise.
--- include/linux/notifier.h | 2 ++ kernel/notifier.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+)
include/linux/notifier.h +2 -0
@@ -173,6 +173,8 @@ extern int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nhextern int raw_notifier_call_chain_robust (struct raw_notifier_head *nh,unsigned long val_up,unsigned long val_down,void *v); +extern bool blocking_notifier_call_chain_empty (struct blocking_notifier_head *nh); +#define NOTIFY_DONE 0x0000/* Don't care */ #define NOTIFY_OK 0x0001/* Suits me */ #define NOTIFY_STOP_MASK 0x8000/* Don't call further */
kernel/notifier.c +14 -0
@@ -322,6 +322,20 @@ int blocking_notifier_call_chain(struct blocking_notifier_head *nh, }EXPORT_SYMBOL_GPL (blocking_notifier_call_chain); +/** +* blocking_notifier_call_chain_empty - Check whether notifier chain is empty +* @nh: Pointer to head of the blocking notifier chain +* +* Checks whether notifier chain is empty. +* +* Returns true is notifier chain is empty, false otherwise. +*/ +bool blocking_notifier_call_chain_empty (struct blocking_notifier_head *nh) +{ +return !rcu_access_pointer (nh->head); +} +EXPORT_SYMBOL_GPL (blocking_notifier_call_chain_empty); +/* * Raw notifier chain routines. There is no protection; * the caller must provide it. Use at your own risk!
Signature · 1 line
2.32.0
PATCH 2/6 kernel: Add power off handler call chain API
In reply to Dmitry OsipenkoSoC platforms often have multiple options for performing system's power off. Meanwhile kernel is limited to a single option. Add power off handler call chain API, which is inspired by the restart API. The old pm_power_off method will be kept around till all drivers are converted to the new API.
--- include/linux/reboot.h | 5 ++ kernel/reboot.c | 111 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 1 deletion(-)
include/linux/reboot.h +5 -0
@@ -49,6 +49,11 @@ extern int register_restart_handler(struct notifier_block *);extern int unregister_restart_handler (struct notifier_block *);extern void do_kernel_restart (char *cmd); +extern int register_poweroff_handler (struct notifier_block *); +extern int unregister_poweroff_handler (struct notifier_block *); + +extern int devm_register_poweroff_handler (struct device *,struct notifier_block *); +/* * Architecture-specific implementations of sys_reboot commands. */
kernel/reboot.c +110 -1
129 lines not on this page · raw message
Signature · 1 line
2.32.0
PATCH 3/6 kernel: Add devm_register_restart_handler()
In reply to Dmitry OsipenkoAdd resource-managed variant of register_restart_handler().
--- include/linux/reboot.h | 2 ++ kernel/reboot.c | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+)
include/linux/reboot.h +2 -0
@@ -49,6 +49,8 @@ extern int register_restart_handler(struct notifier_block *);extern int unregister_restart_handler (struct notifier_block *);extern void do_kernel_restart (char *cmd); +extern int devm_register_restart_handler (struct device *,struct notifier_block *); +extern int register_poweroff_handler (struct notifier_block *);extern int unregister_poweroff_handler (struct notifier_block *);
kernel/reboot.c +27 -0
@@ -200,6 +200,33 @@ int unregister_restart_handler(struct notifier_block *nb) }EXPORT_SYMBOL (unregister_restart_handler); +static void devm_unregister_restart_handler (struct device *dev,void *res) +{ +WARN_ON (unregister_restart_handler (*(struct notifier_block **)res)); +} + +int devm_register_restart_handler (struct device *dev,struct notifier_block *nb) +{ +struct notifier_block **rcnb; +int ret; + + rcnb =devres_alloc (devm_unregister_restart_handler, +sizeof (*rcnb),GFP_KERNEL ); +if (!rcnb) +return -ENOMEM ; + + ret =register_restart_handler (nb); +if (!ret) { + *rcnb = nb; +devres_add (dev, rcnb); + }else { +devres_free (rcnb); + } + +return ret; +} +EXPORT_SYMBOL (devm_register_restart_handler); +/** * do_kernel_restart - Execute kernel restart handler call chain *
Signature · 1 line
2.32.0
PATCH 4/6 mfd: max77620: Use power off call chain API
In reply to Dmitry OsipenkoUse new power off call chain API which allows multiple power off handlers to coexist. Nexus 7 Android tablet can be powered off using MAX77663 PMIC and using a special bootloader command. At first the bootloader option should be tried, it will have a higher priority than the PMIC.
--- drivers/mfd/max77620.c | 22 +++++++++++++++------- include/linux/mfd/max77620.h | 2 ++ 2 files changed, 17 insertions(+), 7 deletions(-)
drivers/mfd/max77620.c +15 -7
@@ -31,11 +31,10 @@#include <linux/init.h> #include <linux/of.h> #include <linux/of_device.h> +#include <linux/reboot.h> #include <linux/regmap.h> #include <linux/slab.h> -static struct max77620_chip *max77620_scratch; -static const struct resource gpio_resources[] = {DEFINE_RES_IRQ (MAX77620_IRQ_TOP_GPIO ), };
@@ -483,13 +482,17 @@ static int max77620_read_es_version(struct max77620_chip *chip)return ret; } -static void max77620_pm_power_off (void ) +static int max77620_pm_power_off (struct notifier_block *nb, +unsigned long reboot_mode,void *data) { -struct max77620_chip *chip = max77620_scratch; + struct max77620_chip *chip = container_of(nb, struct max77620_chip, + pm_off_nb);regmap_update_bits (chip->rmap,MAX77620_REG_ONOFFCNFG1 ,MAX77620_ONOFFCNFG1_SFT_RST ,MAX77620_ONOFFCNFG1_SFT_RST ); + +return NOTIFY_DONE ; } static int max77620_probe(struct i2c_client *client,
@@ -566,9 +569,14 @@ static int max77620_probe(struct i2c_client *client, } pm_off =of_device_is_system_power_controller (client->dev.of_node); -if (pm_off && !pm_power_off) { - max77620_scratch = chip; - pm_power_off = max77620_pm_power_off; +if (pm_off) { + chip->pm_off_nb.notifier_call = max77620_pm_power_off; + chip->pm_off_nb.priority =128 ; + + ret =devm_register_poweroff_handler (chip->dev, &chip->pm_off_nb); +if (ret <0 ) +dev_err (chip->dev, +"Failed to register poweroff handler: %d\n" , ret); }return 0 ;
include/linux/mfd/max77620.h +2 -0
@@ -8,6 +8,7 @@#ifndef _MFD_MAX77620_H_#define _MFD_MAX77620_H_ +#include <linux/notifier.h> #include <linux/types.h> /* GLOBAL, PMIC, GPIO, FPS, ONOFFC, CID Registers */
@@ -327,6 +328,7 @@ enum max77620_chip_id {struct max77620_chip {struct device *dev;struct regmap *rmap; +struct notifier_block pm_off_nb;int chip_irq;
Signature · 1 line
2.32.0
Re: [PATCH v1 4/6] mfd: max77620: Use power off call chain API
Quoted 10 lines · Dmitry Osipenko
Use new power off call chain API which allows multiple power off handlers to coexist. Nexus 7 Android tablet can be powered off using MAX77663 PMIC and using a special bootloader command. At first the bootloader option should be tried, it will have a higher priority than the PMIC.
Signed-off-by Dmitry Osipenko <[email protected]>--- drivers/mfd/max77620.c | 22 +++++++++++++++------- include/linux/mfd/max77620.h | 2 ++ 2 files changed, 17 insertions(+), 7 deletions(-)
I don't have a problem with the approach in general.
I guess it's up to the relevant maintainers to decide.
Quoted 75 lines
drivers/mfd/max77620.c +15
-7@@ -31,11 +31,10 @@#include <linux/init.h> #include <linux/of.h> #include <linux/of_device.h> +#include <linux/reboot.h> #include <linux/regmap.h> #include <linux/slab.h> -static struct max77620_chip *max77620_scratch; -static const struct resource gpio_resources[] = {DEFINE_RES_IRQ (MAX77620_IRQ_TOP_GPIO ), };@@ -483,13 +482,17 @@ static int max77620_read_es_version(struct max77620_chip *chip)return ret; } -static void max77620_pm_power_off (void ) +static int max77620_pm_power_off (struct notifier_block *nb, +unsigned long reboot_mode,void *data) { -struct max77620_chip *chip = max77620_scratch; + struct max77620_chip *chip = container_of(nb, struct max77620_chip, + pm_off_nb);regmap_update_bits (chip->rmap,MAX77620_REG_ONOFFCNFG1 ,MAX77620_ONOFFCNFG1_SFT_RST ,MAX77620_ONOFFCNFG1_SFT_RST ); + +return NOTIFY_DONE ; } static int max77620_probe(struct i2c_client *client,@@ -566,9 +569,14 @@ static int max77620_probe(struct i2c_client *client, } pm_off =of_device_is_system_power_controller (client->dev.of_node); -if (pm_off && !pm_power_off) { - max77620_scratch = chip; - pm_power_off = max77620_pm_power_off; +if (pm_off) { + chip->pm_off_nb.notifier_call = max77620_pm_power_off; + chip->pm_off_nb.priority =128 ; + + ret =devm_register_poweroff_handler (chip->dev, &chip->pm_off_nb); +if (ret <0 ) +dev_err (chip->dev, +"Failed to register poweroff handler: %d\n" , ret); }return 0 ;include/linux/mfd/max77620.h +2
-0@@ -8,6 +8,7 @@#ifndef _MFD_MAX77620_H_#define _MFD_MAX77620_H_ +#include <linux/notifier.h> #include <linux/types.h> /* GLOBAL, PMIC, GPIO, FPS, ONOFFC, CID Registers */ @@ -327,6 +328,7 @@ enum max77620_chip_id {struct max77620_chip {struct device *dev;struct regmap *rmap; +struct notifier_block pm_off_nb;int chip_irq;
Signature · 4 lines
Lee Jones [李琼斯] Senior Technical Lead - Developer Services Linaro.org │ Open source software for Arm SoCs Follow Linaro: Facebook | Twitter | Blog
Re: [PATCH v1 4/6] mfd: max77620: Use power off call chain API
19.10.2021 18:31, Lee Jones пишет:
Quoted 7 lines · Dmitry Osipenko
Quoted 6 lines
Use new power off call chain API which allows multiple power off handlers to coexist. Nexus 7 Android tablet can be powered off using MAX77663 PMIC and using a special bootloader command. At first the bootloader option should be tried, it will have a higher priority than the PMIC.
Quoted 9 lines
Quoted 5 lines
Signed-off-by Dmitry Osipenko <[email protected]>--- drivers/mfd/max77620.c | 22 +++++++++++++++------- include/linux/mfd/max77620.h | 2 ++ 2 files changed, 17 insertions(+), 7 deletions(-)I don't have a problem with the approach in general.
I guess it's up to the relevant maintainers to decide.
Thank you for taking a look at this. Guenter Roeck gave me advice based on his previous experience of working on this topic and I'm now in a process of finalizing v2 that will be a more comprehensive and nicer solution. So you will need to take another look soon, thanks.
PATCH 5/6 soc/tegra: pmc: Use devm_register_restart_handler()
In reply to Dmitry OsipenkoUse resource-managed variant of register_restart_handler() to make code cleaner.
--- drivers/soc/tegra/pmc.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
drivers/soc/tegra/pmc.c +9 -11
@@ -2859,6 +2859,13 @@ static int tegra_pmc_probe(struct platform_device *pdev) pmc->clk =NULL ; } + err =devm_register_restart_handler (&pdev->dev, &tegra_pmc_restart_handler); +if (err) { +dev_err (&pdev->dev,"unable to register restart handler, %d\n" , + err); +return err; + } +/* * PCLK clock rate can't be retrieved using CLK API because it * causes lockup if CPU enters LP2 idle state from some other
@@ -2890,20 +2897,13 @@ static int tegra_pmc_probe(struct platform_device *pdev) goto cleanup_sysfs; } - err =register_restart_handler (&tegra_pmc_restart_handler); -if (err) { -dev_err (&pdev->dev,"unable to register restart handler, %d\n" , - err); - goto cleanup_debugfs; - } - err =tegra_pmc_pinctrl_init (pmc);if (err) - goto cleanup_restart_handler; + goto cleanup_debugfs; err =tegra_pmc_regmap_init (pmc);if (err <0 ) - goto cleanup_restart_handler; + goto cleanup_debugfs; err =tegra_powergate_init (pmc, pdev->dev.of_node);if (err <0 )
@@ -2926,8 +2926,6 @@ static int tegra_pmc_probe(struct platform_device *pdev) cleanup_powergates:tegra_powergate_remove_all (pdev->dev.of_node); -cleanup_restart_handler: -unregister_restart_handler (&tegra_pmc_restart_handler); cleanup_debugfs:debugfs_remove (pmc->debugfs); cleanup_sysfs:
Signature · 1 line
2.32.0
PATCH 6/6 soc/tegra: pmc: Add power off handler
In reply to Dmitry OsipenkoNexus 7 Android tablet can be turned off using a special bootloader command which is conveyed to bootloader by putting magic value into specific scratch register and then rebooting normally. This power off method should be invoked if USB cable is connected. Bootloader then will display battery status and power off the device. This behaviour is borrowed from downstream kernel and matches user expectations, otherwise it looks like device got hung during power off.
--- drivers/soc/tegra/pmc.c | 42 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-)
drivers/soc/tegra/pmc.c +39 -3
@@ -39,6 +39,7 @@#include <linux/platform_device.h> #include <linux/pm_domain.h> #include <linux/pm_opp.h> +#include <linux/power_supply.h> #include <linux/reboot.h> #include <linux/regmap.h> #include <linux/reset.h>
@@ -107,6 +108,7 @@#define PMC_USB_DEBOUNCE_DEL 0xec#define PMC_USB_AO 0xf0 +#define PMC_SCRATCH37 0x130#define PMC_SCRATCH41 0x140#define PMC_WAKE2_MASK 0x160
@@ -1063,10 +1065,8 @@ int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)return tegra_powergate_remove_clamping (id); } -static int tegra_pmc_restart_notify (struct notifier_block *this, -unsigned long action,void *data) +static void tegra_pmc_restart (const char *cmd) { -const char *cmd = data;u32 value; value =tegra_pmc_scratch_readl (pmc, pmc->soc->regs->scratch0);
@@ -1089,6 +1089,12 @@ static int tegra_pmc_restart_notify(struct notifier_block *this, value =tegra_pmc_readl (pmc,PMC_CNTRL ); value |=PMC_CNTRL_MAIN_RST ;tegra_pmc_writel (pmc, value,PMC_CNTRL ); +} + +static int tegra_pmc_restart_notify (struct notifier_block *this, +unsigned long action,void *data) +{ +tegra_pmc_restart (data);return NOTIFY_DONE ; }
@@ -1098,6 +1104,29 @@ static struct notifier_block tegra_pmc_restart_handler = { .priority = 128, }; +static int tegra_pmc_poweroff_notify (struct notifier_block *this, +unsigned long action,void *data) +{ +/* +* Reboot Nexus 7 into special bootloader mode if USB cable is +* connected in order to display battery status and power off. +*/ +if (of_machine_is_compatible ("asus,grouper" ) && +power_supply_is_system_supplied ()) { +const u32 go_to_charger_mode =0xa5a55a5a ; + +tegra_pmc_writel (pmc, go_to_charger_mode,PMC_SCRATCH37 ); +tegra_pmc_restart (NULL ); + } + +return NOTIFY_DONE ; +} + +static struct notifier_block tegra_pmc_poweroff_handler = { + .notifier_call = tegra_pmc_poweroff_notify, + .priority =200 , +}; +static int powergate_show (struct seq_file *s,void *data) {unsigned int i;
@@ -2866,6 +2895,13 @@ static int tegra_pmc_probe(struct platform_device *pdev)return err; } + err =devm_register_poweroff_handler (&pdev->dev, &tegra_pmc_poweroff_handler); +if (err) { +dev_err (&pdev->dev,"unable to register poweroff handler, %d\n" , + err); +return err; + } +/* * PCLK clock rate can't be retrieved using CLK API because it * causes lockup if CPU enters LP2 idle state from some other
Signature · 1 line
2.32.0
Quoted 3 lines · Dmitry Osipenko
Introduce power off call chain API that is inspired by the restart API. It allows to have multiple power off handlers invoked along the chain
allows multiple
Quoted 1 line
until system is powered off. For the starter this series converts couple
the system
a couple
Quoted 2 lines
NVIDIA Tegra drivers to the new API. Existing
pm_power_off()method stays around and may be removed once all users will adopt the new API.
users adopt
Quoted 2 lines
There were couple attempts in the past to add power off API from
a couple
Quoted 3 lines
Guenter Roeck and Thierry Reding, but they were never completed. This is a somewhat simplified version which doesn't try to convert whole kernel to the new API at once, but solves immediate practical problem that we
problems
Quoted 1 line
have on Nexus 7 Android tablet where device needs to chain power off
tablets where the device
Quoted 1 line
methods.
Immediate question here is how do you see the plan of spreading this. I.o.w. can you put an explanation that you have checked, let's say
Quoted 1 line
80% current users, and they may be converted like [example
placeholder] without any special tricks?
Signature · 2 lines
With Best Regards, Andy Shevchenko
07.10.2021 10:18, Andy Shevchenko пишет:
Quoted 26 lines · Dmitry Osipenko
Quoted 3 lines
Introduce power off call chain API that is inspired by the restart API. It allows to have multiple power off handlers invoked along the chain
allows multiple
Quoted 1 line
until system is powered off. For the starter this series converts couple
the system
a coupleQuoted 2 lines
NVIDIA Tegra drivers to the new API. Existing
pm_power_off()method stays around and may be removed once all users will adopt the new API.users adopt
Quoted 2 lines
There were couple attempts in the past to add power off API from
a couple
Quoted 3 lines
Guenter Roeck and Thierry Reding, but they were never completed. This is a somewhat simplified version which doesn't try to convert whole kernel to the new API at once, but solves immediate practical problem that we
Quoted 5 lines
problems
Quoted 1 line
have on Nexus 7 Android tablet where device needs to chain power off
tablets where the device
Thank you for the corrections, so far there is one problem and one tablet :)
Quoted 4 lines
Immediate question here is how do you see the plan of spreading this. I.o.w. can you put an explanation that you have checked, let's say
Quoted 1 line
80% current users, and they may be converted like [example
placeholder] without any special tricks?
The rough plan is:
1. Add new API.
2. Convert drivers to the new API per subsystem.
3. Expose do_kernel_restart().
4. Replace pm_power_off() with do_kernel_poweroff() per arch/, making power off similar to the restart that uses do_kernel_restart().
5. Remove do_kernel_restart() from kernel/reboot.c
Majority of pm_power_off() users shouldn't need the chaining and pm_power_off() doesn't conflict with the new API, so there is no need to rush the conversion.
The single-link chain users could be converted to the new API directly, this will remove some global variables. But at first should be better to gain more users who actually need the chained power off since they may have very specific requirements not covered by the current variant of the API and will be easier to evolve API with less users.
Quoted 2 lines · Dmitry Osipenko
07.10.2021 10:18, Andy Shevchenko пишет:
Quoted 1 line · Dmitry Osipenko
...
Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
This
is a somewhat simplified version which doesn't try to convert whole kernel to the new API at once, but solves immediate practical problem that weproblems
Quoted 1 line
have on Nexus 7 Android tablet where device needs to chain power off
tablets where the device
Thank you for the corrections, so far there is one problem and one tablet :)
Then use "the Nexus 7 Android tablet" :-)
Quoted 9 lines
Quoted 4 lines
Immediate question here is how do you see the plan of spreading this. I.o.w. can you put an explanation that you have checked, let's say
Quoted 1 line
80% current users, and they may be converted like [example
placeholder] without any special tricks?
The rough plan is:
1. Add new API.
2. Convert drivers to the new API per subsystem.
I would suggest to show that you are actually into it by converting a couple of the subsystems for the starter.
Quoted 9 lines
3. Expose
do_kernel_restart().
4. Replacepm_power_off()withdo_kernel_poweroff()perarch/, making power off similar to the restart that usesdo_kernel_restart().
5. Removedo_kernel_restart()fromkernel/reboot.cMajority of
pm_power_off()users shouldn't need the chaining andpm_power_off()doesn't conflict with the new API, so there is no need to rush the conversion.
Quoted 5 lines
The single-link chain users could be converted to the new API directly, this will remove some global variables. But at first should be better to gain more users who actually need the chained power off since they may have very specific requirements not covered by the current variant of the API and will be easier to evolve API with less users.
All above in one or another form should be in cover letter.
Signature · 2 lines
With Best Regards, Andy Shevchenko
07.10.2021 12:11, Andy Shevchenko пишет:
Quoted 25 lines · Dmitry Osipenko
Quoted 2 lines
07.10.2021 10:18, Andy Shevchenko пишет:
Quoted 1 line · Dmitry Osipenko
...
Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
This
is a somewhat simplified version which doesn't try to convert whole kernel to the new API at once, but solves immediate practical problem that weproblems
Quoted 1 line
have on Nexus 7 Android tablet where device needs to chain power off
tablets where the device
Thank you for the corrections, so far there is one problem and one tablet :)
Then use "the Nexus 7 Android tablet" :-)
Quoted 5 lines
Quoted 4 lines
Immediate question here is how do you see the plan of spreading this. I.o.w. can you put an explanation that you have checked, let's say
Quoted 1 line
80% current users, and they may be converted like [example
placeholder] without any special tricks?
Quoted 7 lines
Quoted 4 lines
The rough plan is:
1. Add new API.
2. Convert drivers to the new API per subsystem.I would suggest to show that you are actually into it by converting a couple of the subsystems for the starter.
Noted
Quoted 9 lines
Quoted 9 lines
3. Expose
do_kernel_restart().
4. Replacepm_power_off()withdo_kernel_poweroff()perarch/, making power off similar to the restart that usesdo_kernel_restart().
5. Removedo_kernel_restart()fromkernel/reboot.cMajority of
pm_power_off()users shouldn't need the chaining andpm_power_off()doesn't conflict with the new API, so there is no need to rush the conversion.
Quoted 7 lines
Quoted 5 lines
The single-link chain users could be converted to the new API directly, this will remove some global variables. But at first should be better to gain more users who actually need the chained power off since they may have very specific requirements not covered by the current variant of the API and will be easier to evolve API with less users.
All above in one or another form should be in cover letter.
Alright, apparently I overestimated a tad awareness about the current status. I'll extend the cover letter next time, thanks.