[PATCH v2] mfd: stpmic1: Attempt system shutdown twice in case PMIC is confused

Marek Vasut posted 1 patch 3 weeks, 2 days ago
There is a newer version of this series
drivers/mfd/stpmic1.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
[PATCH v2] mfd: stpmic1: Attempt system shutdown twice in case PMIC is confused
Posted by Marek Vasut 3 weeks, 2 days ago
Attempt to shut down again, in case the first attempt failed.
The STPMIC1 might get confused and the first regmap_update_bits()
returns with -ETIMEDOUT / -110 . If that or similar transient
failure occurs, try to shut down again. If the second attempt
fails, there is some bigger problem, report it to user.

Cc: stable@vger.kernel.org
Fixes: 6e9df38f359a ("mfd: stpmic1: Add PMIC poweroff via sys-off handler")
Signed-off-by: Marek Vasut <marex@nabladev.com>
---
Cc: Lee Jones <lee@kernel.org>
Cc: Pascal PAILLET-LME <p.paillet@st.com>
Cc: Paul Cercueil <paul@crapouillou.net>
Cc: Sean Nyekjaer <sean@geanix.com>
Cc: kernel@dh-electronics.com
---
V2: - Use a retry loop
    - Cc stable
---
 drivers/mfd/stpmic1.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/drivers/mfd/stpmic1.c b/drivers/mfd/stpmic1.c
index 081827bc05961..9caf2b8740829 100644
--- a/drivers/mfd/stpmic1.c
+++ b/drivers/mfd/stpmic1.c
@@ -121,9 +121,24 @@ static const struct regmap_irq_chip stpmic1_regmap_irq_chip = {
 static int stpmic1_power_off(struct sys_off_data *data)
 {
 	struct stpmic1 *ddata = data->cb_data;
-
-	regmap_update_bits(ddata->regmap, MAIN_CR,
-			   SOFTWARE_SWITCH_OFF, SOFTWARE_SWITCH_OFF);
+	int i, ret;
+
+	for (i = 0; i < 2; i++) {
+		ret = regmap_update_bits(ddata->regmap, MAIN_CR, SOFTWARE_SWITCH_OFF,
+					 SOFTWARE_SWITCH_OFF);
+		if (!ret)
+			return NOTIFY_DONE;
+
+		/*
+		 * Attempt to shut down again, in case the first attempt failed.
+		 * The STPMIC1 might get confused and the first regmap_update_bits()
+		 * returns with -ETIMEDOUT / -110 . If that or similar transient
+		 * failure occurs, try to shut down again. If the second attempt
+		 * fails, there is some bigger problem, report it to user.
+		 */
+		if (i)
+			dev_err(ddata->dev, "Failed to access PMIC I2C bus (%d)\n", ret);
+	}
 
 	return NOTIFY_DONE;
 }
-- 
2.51.0
Re: [PATCH v2] mfd: stpmic1: Attempt system shutdown twice in case PMIC is confused
Posted by Lee Jones 2 weeks, 4 days ago
On Thu, 15 Jan 2026, Marek Vasut wrote:

> Attempt to shut down again, in case the first attempt failed.
> The STPMIC1 might get confused and the first regmap_update_bits()
> returns with -ETIMEDOUT / -110 . If that or similar transient
> failure occurs, try to shut down again. If the second attempt
> fails, there is some bigger problem, report it to user.
> 
> Cc: stable@vger.kernel.org
> Fixes: 6e9df38f359a ("mfd: stpmic1: Add PMIC poweroff via sys-off handler")
> Signed-off-by: Marek Vasut <marex@nabladev.com>
> ---
> Cc: Lee Jones <lee@kernel.org>
> Cc: Pascal PAILLET-LME <p.paillet@st.com>
> Cc: Paul Cercueil <paul@crapouillou.net>
> Cc: Sean Nyekjaer <sean@geanix.com>
> Cc: kernel@dh-electronics.com
> ---
> V2: - Use a retry loop
>     - Cc stable
> ---
>  drivers/mfd/stpmic1.c | 21 ++++++++++++++++++---
>  1 file changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/mfd/stpmic1.c b/drivers/mfd/stpmic1.c
> index 081827bc05961..9caf2b8740829 100644
> --- a/drivers/mfd/stpmic1.c
> +++ b/drivers/mfd/stpmic1.c
> @@ -121,9 +121,24 @@ static const struct regmap_irq_chip stpmic1_regmap_irq_chip = {
>  static int stpmic1_power_off(struct sys_off_data *data)
>  {
>  	struct stpmic1 *ddata = data->cb_data;
> -
> -	regmap_update_bits(ddata->regmap, MAIN_CR,
> -			   SOFTWARE_SWITCH_OFF, SOFTWARE_SWITCH_OFF);
> +	int i, ret;
> +
> +	for (i = 0; i < 2; i++) {

  for (int retries = 0; retries < MAX_RETIRES; retries++)

> +		ret = regmap_update_bits(ddata->regmap, MAIN_CR, SOFTWARE_SWITCH_OFF,
> +					 SOFTWARE_SWITCH_OFF);
> +		if (!ret)
> +			return NOTIFY_DONE;
> +
> +		/*
> +		 * Attempt to shut down again, in case the first attempt failed.
> +		 * The STPMIC1 might get confused and the first regmap_update_bits()
> +		 * returns with -ETIMEDOUT / -110 . If that or similar transient
> +		 * failure occurs, try to shut down again. If the second attempt
> +		 * fails, there is some bigger problem, report it to user.
> +		 */
> +		if (i)
> +			dev_err(ddata->dev, "Failed to access PMIC I2C bus (%d)\n", ret);

Users aren't going to care if this works on the first or second attempt.

I would only issue the error once we've given up.

No return error in the fail case?

  define NOTIFY_DONE             0x0000          /* Don't care */

Harsh!

> +	}
>  
>  	return NOTIFY_DONE;
>  }
> -- 
> 2.51.0
> 

-- 
Lee Jones [李琼斯]
Re: [PATCH v2] mfd: stpmic1: Attempt system shutdown twice in case PMIC is confused
Posted by Marek Vasut 2 weeks, 2 days ago
On 1/20/26 6:18 PM, Lee Jones wrote:

>> +++ b/drivers/mfd/stpmic1.c
>> @@ -121,9 +121,24 @@ static const struct regmap_irq_chip stpmic1_regmap_irq_chip = {
>>   static int stpmic1_power_off(struct sys_off_data *data)
>>   {
>>   	struct stpmic1 *ddata = data->cb_data;
>> -
>> -	regmap_update_bits(ddata->regmap, MAIN_CR,
>> -			   SOFTWARE_SWITCH_OFF, SOFTWARE_SWITCH_OFF);
>> +	int i, ret;
>> +
>> +	for (i = 0; i < 2; i++) {
> 
>    for (int retries = 0; retries < MAX_RETIRES; retries++)
> 
>> +		ret = regmap_update_bits(ddata->regmap, MAIN_CR, SOFTWARE_SWITCH_OFF,
>> +					 SOFTWARE_SWITCH_OFF);
>> +		if (!ret)
>> +			return NOTIFY_DONE;
>> +
>> +		/*
>> +		 * Attempt to shut down again, in case the first attempt failed.
>> +		 * The STPMIC1 might get confused and the first regmap_update_bits()
>> +		 * returns with -ETIMEDOUT / -110 . If that or similar transient
>> +		 * failure occurs, try to shut down again. If the second attempt
>> +		 * fails, there is some bigger problem, report it to user.
>> +		 */
>> +		if (i)
>> +			dev_err(ddata->dev, "Failed to access PMIC I2C bus (%d)\n", ret);
> 
> Users aren't going to care if this works on the first or second attempt.
> 
> I would only issue the error once we've given up.
> 
> No return error in the fail case?
> 
>    define NOTIFY_DONE             0x0000          /* Don't care */
> 
> Harsh!
That is the only option, because there may be additional shutdown 
notifiers and those should also be run.

The rest is fixed in V3.