mfd: rk808: Fix pointers and poweroff
v2 cover mfd: rk808: Fix pointers and poweroff
This patch is actually follow-up to:
[PATCH 1/1] mfd: rk808: Prepare rk8085 for poweroff
The patchset fixes poweroff function for boards with RK8085 PMU. During the preparation of v2 possible wrong pointer access was spot (pm_power_off), so one more patch was introduced in the series.
Stefan Mavrodiev (2):
mfd: rk808: Check pm_power_off pointer
mfd: rk808: Prepare rk805 for poweroff
drivers/mfd/rk808.c | 42 +++++++++++++++++++++++++++++++++------ include/linux/mfd/rk808.h | 2 ++ 2 files changed, 38 insertions(+), 6 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/2 mfd: rk808: Check pm_power_off pointer
The function pointer pm_power_off may point to function from other module (PSCI for example). If rk808 is removed, pm_power_off is overwritten to NULL and the system cannot be powered off.
This patch checks if pm_power_off points to a module function.
---
Changes in v2:
- Initial release actually
drivers/mfd/rk808.c | 13 +++++++------ include/linux/mfd/rk808.h | 1 + 2 files changed, 8 insertions(+), 6 deletions(-)
drivers/mfd/rk808.c +7 -6
@@ -438,7 +438,6 @@ static int rk808_probe(struct i2c_client *client,struct rk808 *rk808;const struct rk808_reg_data *pre_init_reg;const struct mfd_cell *cells; -void (*pm_pwroff_fn)(void );int nr_pre_init_regs;int nr_cells;int pm_off =0 , msb, lsb;
@@ -475,7 +474,7 @@ static int rk808_probe(struct i2c_client *client, nr_pre_init_regs =ARRAY_SIZE (rk805_pre_init_reg); cells = rk805s; nr_cells =ARRAY_SIZE (rk805s); - pm_pwroff_fn = rk805_device_shutdown; + rk808->pm_pwroff_fn = rk805_device_shutdown;break ;case RK808_ID : rk808->regmap_cfg = &rk808_regmap_config;
@@ -484,7 +483,7 @@ static int rk808_probe(struct i2c_client *client, nr_pre_init_regs =ARRAY_SIZE (rk808_pre_init_reg); cells = rk808s; nr_cells =ARRAY_SIZE (rk808s); - pm_pwroff_fn = rk808_device_shutdown; + rk808->pm_pwroff_fn = rk808_device_shutdown;break ;case RK818_ID : rk808->regmap_cfg = &rk818_regmap_config;
@@ -493,7 +492,7 @@ static int rk808_probe(struct i2c_client *client, nr_pre_init_regs =ARRAY_SIZE (rk818_pre_init_reg); cells = rk818s; nr_cells =ARRAY_SIZE (rk818s); - pm_pwroff_fn = rk818_device_shutdown; + rk808->pm_pwroff_fn = rk818_device_shutdown;break ;default : dev_err(&client->dev, "Unsupported RK8XX ID %lu\n",
@@ -548,7 +547,7 @@ static int rk808_probe(struct i2c_client *client, "rockchip,system-power-controller");if (pm_off && !pm_power_off) { rk808_i2c_client = client; - pm_power_off = pm_pwroff_fn; + pm_power_off = rk808->pm_pwroff_fn; }return 0 ;
@@ -563,7 +562,9 @@ static int rk808_remove(struct i2c_client *client)struct rk808 *rk808 =i2c_get_clientdata (client);regmap_del_irq_chip (client->irq, rk808->irq_data); - pm_power_off =NULL ; + +if (rk808->pm_pwroff_fn && pm_power_off == rk808->pm_pwroff_fn) + pm_power_off =NULL ;return 0 ; }
include/linux/mfd/rk808.h +1 -0
@@ -453,5 +453,6 @@ struct rk808 {long variant;const struct regmap_config *regmap_cfg;const struct regmap_irq_chip *regmap_irq_chip; + void (*pm_pwroff_fn)(void); };#endif /* __LINUX_REGULATOR_RK808_H */
Signature · 1 line
2.17.1
Re: [PATCH v2 1/2] mfd: rk808: Check pm_power_off pointer
Quoted 64 lines · Stefan Mavrodiev
The function pointer
pm_power_offmay point to function from other module (PSCI for example). If rk808 is removed,pm_power_offis overwritten to NULL and the system cannot be powered off.This patch checks if
pm_power_offpoints to a module function.Signed-off-by Stefan Mavrodiev <[email protected]>---
Changes in v2:
- Initial release actuallydrivers/mfd/rk808.c | 13 +++++++------ include/linux/mfd/rk808.h | 1 + 2 files changed, 8 insertions(+), 6 deletions(-)drivers/mfd/rk808.c +4
-5@@ -438,7 +438,6 @@ static int rk808_probe(struct i2c_client *client,struct rk808 *rk808;const struct rk808_reg_data *pre_init_reg;const struct mfd_cell *cells; -void (*pm_pwroff_fn)(void );int nr_pre_init_regs;int nr_cells;int pm_off =0 , msb, lsb;@@ -475,7 +474,7 @@ static int rk808_probe(struct i2c_client *client, nr_pre_init_regs =ARRAY_SIZE (rk805_pre_init_reg); cells = rk805s; nr_cells =ARRAY_SIZE (rk805s); - pm_pwroff_fn = rk805_device_shutdown; + rk808->pm_pwroff_fn = rk805_device_shutdown;break ;case RK808_ID : rk808->regmap_cfg = &rk808_regmap_config;@@ -484,7 +483,7 @@ static int rk808_probe(struct i2c_client *client, nr_pre_init_regs =ARRAY_SIZE (rk808_pre_init_reg); cells = rk808s; nr_cells =ARRAY_SIZE (rk808s); - pm_pwroff_fn = rk808_device_shutdown; + rk808->pm_pwroff_fn = rk808_device_shutdown;break ;case RK818_ID : rk808->regmap_cfg = &rk818_regmap_config;@@ -493,7 +492,7 @@ static int rk808_probe(struct i2c_client *client, nr_pre_init_regs =ARRAY_SIZE (rk818_pre_init_reg); cells = rk818s; nr_cells =ARRAY_SIZE (rk818s); - pm_pwroff_fn = rk818_device_shutdown; + rk808->pm_pwroff_fn = rk818_device_shutdown;break ;default : dev_err(&client->dev, "Unsupported RK8XX ID %lu\n",@@ -548,7 +547,7 @@ static int rk808_probe(struct i2c_client *client, "rockchip,system-power-controller");if (pm_off && !pm_power_off) { rk808_i2c_client = client; - pm_power_off = pm_pwroff_fn; + pm_power_off = rk808->pm_pwroff_fn; }return 0 ;
drivers/mfd/rk808.c static int rk808_remove(struct i2c_client *client) · line 567
Quoted 7 lines
drivers/mfd/rk808.c +3
-1@@ -563,7 +562,9 @@ static int rk808_remove(struct i2c_client *client)struct rk808 *rk808 =i2c_get_clientdata (client);regmap_del_irq_chip (client->irq, rk808->irq_data); - pm_power_off =NULL ; + +if (rk808->pm_pwroff_fn && pm_power_off == rk808->pm_pwroff_fn) + pm_power_off =NULL ;
The idea seems sound, but I think you should comment this statement.
Signature · 4 lines
Lee Jones [李琼斯] Linaro Services Technical Lead Linaro.org │ Open source software for ARM SoCs Follow Linaro: Facebook | Twitter | Blog
PATCH v2 2/2 mfd: rk808: Prepare rk805 for poweroff
In reply to Stefan MavrodievRK805 has SLEEP signal, which can put the device into SLEEP or OFF mode. The default is SLEEP mode.
However, when the kernel performs power-off (actually the ATF) the device will not go fully off and this will result in higher power consumption and inability to wake the device with RTC alarm.
The solution is to enable pm_power_off_prepare function, which will configure SLEEP pin for OFF function.
---
Changes for v2:
- Move pm_pwroff_prep_fn to header
- Check pm_power_off_prepare before make it NULL
drivers/mfd/rk808.c | 29 +++++++++++++++++++++++++++++ include/linux/mfd/rk808.h | 1 + 2 files changed, 30 insertions(+)
drivers/mfd/rk808.c +29 -0
@@ -387,6 +387,24 @@ static void rk805_device_shutdown(void)dev_err (&rk808_i2c_client->dev,"power off error!\n" ); } +static void rk805_device_shutdown_prepare (void ) +{ +int ret; +struct rk808 *rk808 =i2c_get_clientdata (rk808_i2c_client); + +if (!rk808) { +dev_warn (&rk808_i2c_client->dev, +"have no rk805, so do nothing here\n" ); +return ; + } + + ret =regmap_update_bits (rk808->regmap, +RK805_GPIO_IO_POL_REG , +SLP_SD_MSK ,SHUTDOWN_FUN ); +if (ret) +dev_err (&rk808_i2c_client->dev,"power off error!\n" ); +} +static void rk808_device_shutdown (void ) {int ret;
@@ -475,6 +493,7 @@ static int rk808_probe(struct i2c_client *client, cells = rk805s; nr_cells =ARRAY_SIZE (rk805s); rk808->pm_pwroff_fn = rk805_device_shutdown; + rk808->pm_pwroff_prep_fn = rk805_device_shutdown_prepare;break ;case RK808_ID : rk808->regmap_cfg = &rk808_regmap_config;
@@ -550,6 +569,12 @@ static int rk808_probe(struct i2c_client *client, pm_power_off = rk808->pm_pwroff_fn; } +if (pm_off && !pm_power_off_prepare) { +if (!rk808_i2c_client) + rk808_i2c_client = client; + pm_power_off_prepare = rk808->pm_pwroff_prep_fn; + } +return 0 ; err_irq:
@@ -566,6 +591,10 @@ static int rk808_remove(struct i2c_client *client)if (rk808->pm_pwroff_fn && pm_power_off == rk808->pm_pwroff_fn) pm_power_off =NULL ; +if (rk808->pm_pwroff_prep_fn && + pm_power_off_prepare == rk808->pm_pwroff_prep_fn) + pm_power_off_prepare =NULL ; +return 0 ; }
include/linux/mfd/rk808.h +1 -0
@@ -454,5 +454,6 @@ struct rk808 {const struct regmap_config *regmap_cfg;const struct regmap_irq_chip *regmap_irq_chip;void (*pm_pwroff_fn)(void ); + void (*pm_pwroff_prep_fn)(void); };#endif /* __LINUX_REGULATOR_RK808_H */
Signature · 1 line
2.17.1
Re: [PATCH v2 2/2] mfd: rk808: Prepare rk805 for poweroff
Quoted 26 lines · Stefan Mavrodiev
RK805 has SLEEP signal, which can put the device into SLEEP or OFF mode. The default is SLEEP mode.
However, when the kernel performs power-off (actually the ATF) the device will not go fully off and this will result in higher power consumption and inability to wake the device with RTC alarm.
The solution is to enable
pm_power_off_preparefunction, which will configure SLEEP pin for OFF function.Signed-off-by Stefan Mavrodiev <[email protected]>---
Changes for v2:
- Movepm_pwroff_prep_fnto header
- Checkpm_power_off_preparebefore make it NULLdrivers/mfd/rk808.c | 29 +++++++++++++++++++++++++++++ include/linux/mfd/rk808.h | 1 + 2 files changed, 30 insertions(+)drivers/mfd/rk808.c +0
-0@@ -387,6 +387,24 @@ static void rk805_device_shutdown(void)dev_err (&rk808_i2c_client->dev,"power off error!\n" );
drivers/mfd/rk808.c static void rk805_device_shutdown(void) · line 397
Quoted 10 lines
drivers/mfd/rk808.c +8
-0} +static void rk805_device_shutdown_prepare (void ) +{ +int ret; +struct rk808 *rk808 =i2c_get_clientdata (rk808_i2c_client); + +if (!rk808) { +dev_warn (&rk808_i2c_client->dev, +"have no rk805, so do nothing here\n" );
No need for this message I think.
If it's not available, just return.
drivers/mfd/rk808.c static void rk805_device_shutdown(void) · line 406
Quoted 9 lines
drivers/mfd/rk808.c +9
-0+return ; + } + + ret =regmap_update_bits (rk808->regmap, +RK805_GPIO_IO_POL_REG , +SLP_SD_MSK ,SHUTDOWN_FUN ); +if (ret) +dev_err (&rk808_i2c_client->dev,"power off error!\n" ); +}
"Failed to shutdown device"
Quoted 25 lines
drivers/mfd/rk808.c +7
-0static void rk808_device_shutdown (void ) {int ret;@@ -475,6 +493,7 @@ static int rk808_probe(struct i2c_client *client, cells = rk805s; nr_cells =ARRAY_SIZE (rk805s); rk808->pm_pwroff_fn = rk805_device_shutdown; + rk808->pm_pwroff_prep_fn = rk805_device_shutdown_prepare;break ;case RK808_ID : rk808->regmap_cfg = &rk808_regmap_config;@@ -550,6 +569,12 @@ static int rk808_probe(struct i2c_client *client, pm_power_off = rk808->pm_pwroff_fn; } +if (pm_off && !pm_power_off_prepare) { +if (!rk808_i2c_client) + rk808_i2c_client = client; + pm_power_off_prepare = rk808->pm_pwroff_prep_fn; + } +return 0 ; err_irq:
drivers/mfd/rk808.c static int rk808_remove(struct i2c_client *client) · line 596
Quoted 6 lines
drivers/mfd/rk808.c +3
-0@@ -566,6 +591,10 @@ static int rk808_remove(struct i2c_client *client)if (rk808->pm_pwroff_fn && pm_power_off == rk808->pm_pwroff_fn) pm_power_off =NULL ; +if (rk808->pm_pwroff_prep_fn && + pm_power_off_prepare == rk808->pm_pwroff_prep_fn) + pm_power_off_prepare =NULL ;
I think this block would benefit from a comment.
Quoted 14 lines
drivers/mfd/rk808.c +0
-0return 0 ; }include/linux/mfd/rk808.h +1
-0@@ -454,5 +454,6 @@ struct rk808 {const struct regmap_config *regmap_cfg;const struct regmap_irq_chip *regmap_irq_chip;void (*pm_pwroff_fn)(void ); + void (*pm_pwroff_prep_fn)(void); };#endif /* __LINUX_REGULATOR_RK808_H */
Signature · 4 lines
Lee Jones [李琼斯] Linaro Services Technical Lead Linaro.org │ Open source software for ARM SoCs Follow Linaro: Facebook | Twitter | Blog