Skip to contentSkip to conversation

pwm: iqs620a: Correct a stale state variable

PATCH Superseded Jeff LaBundy 2 participants 5 messages Jan 15, 2021 – Jan 19, 2021 PWM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pwm: iqs620a: Correct a stale state variable

If duty cycle is first set to a value that is sufficiently high to enable the output (e.g. 10000 ns) but then lowered to a value that is quantized to zero (e.g. 1000 ns), the output is disabled as the device cannot drive a constant zero (as expected).

However if the device is later re-initialized due to watchdog bite, the output is re-enabled at the next-to-last duty cycle (10000 ns). This is because the iqs620_pwm->out_en flag unconditionally tracks state->enabled instead of what was actually written to the device.

To solve this problem, force the iqs620_pwm->out_en flag to follow the IQS620_PWR_SETTINGS_PWM_OUT field instead, as was the original design intent.

Fixes 6f0841a8197b ("pwm: Add support for Azoteq IQS620A PWM generator")
---
 drivers/pwm/pwm-iqs620a.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pwm/pwm-iqs620a.c +4 -2
index 5ede825..5eb8fa4 100644
@@ -79,6 +79,8 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0);
 		if (ret)
 			goto err_mutex;
+
+		iqs620_pwm->out_en = false;
 	}
 
 	if (duty_scale) {
@@ -97,9 +99,9 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
 		if (ret)
 			goto err_mutex;
-	}
 
-	iqs620_pwm->out_en = state->enabled;
+		iqs620_pwm->out_en = true;
+	}
 
 err_mutex:
 	mutex_unlock(&iqs620_pwm->lock);
Signature · 1 line
2.7.4
#1 lore raw
Quoted 34 lines · Jeff LaBundy

If duty cycle is first set to a value that is sufficiently high to enable the output (e.g. 10000 ns) but then lowered to a value that is quantized to zero (e.g. 1000 ns), the output is disabled as the device cannot drive a constant zero (as expected).

However if the device is later re-initialized due to watchdog bite, the output is re-enabled at the next-to-last duty cycle (10000 ns). This is because the iqs620_pwm->out_en flag unconditionally tracks state->enabled instead of what was actually written to the device.

To solve this problem, force the iqs620_pwm->out_en flag to follow the IQS620_PWR_SETTINGS_PWM_OUT field instead, as was the original design intent.

Fixes 6f0841a8197b ("pwm: Add support for Azoteq IQS620A PWM generator")
---
 drivers/pwm/pwm-iqs620a.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pwm/pwm-iqs620a.c +2 -0
index 5ede825..5eb8fa4 100644
@@ -79,6 +79,8 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0);
 		if (ret)
 			goto err_mutex;
+
+		iqs620_pwm->out_en = false;
 	}
 
 	if (duty_scale) {

drivers/pwm/pwm-iqs620a.c static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 104

Quoted 8 lines
drivers/pwm/pwm-iqs620a.c +2 -2
@@ -97,9 +99,9 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
 		if (ret)
 			goto err_mutex;
-	}
 
-	iqs620_pwm->out_en = state->enabled;
+		iqs620_pwm->out_en = true;
+	}

I got the problem and I agree it needs fixing. Are you aware you change the semantic of out_en here and so the behaviour of .get_state()? IMHO the change is fine however, and unless I miss something this patch makes the comment in iqs620_pwm_get_state true.

Other than that I wonder if it would make more sense to track duty_scale in the driver struct instead of duty_val and out_en.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#2 lore raw

Hi Uwe,

Thank you for taking a look; actually I came across this problem while testing your patch, so I owe you even more gratitude :)

Quoted 40 lines · Uwe Kleine-König
Quoted 39 lines · Jeff LaBundy

If duty cycle is first set to a value that is sufficiently high to enable the output (e.g. 10000 ns) but then lowered to a value that is quantized to zero (e.g. 1000 ns), the output is disabled as the device cannot drive a constant zero (as expected).

However if the device is later re-initialized due to watchdog bite, the output is re-enabled at the next-to-last duty cycle (10000 ns). This is because the iqs620_pwm->out_en flag unconditionally tracks state->enabled instead of what was actually written to the device.

To solve this problem, force the iqs620_pwm->out_en flag to follow the IQS620_PWR_SETTINGS_PWM_OUT field instead, as was the original design intent.

Fixes 6f0841a8197b ("pwm: Add support for Azoteq IQS620A PWM generator")
---
 drivers/pwm/pwm-iqs620a.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pwm/pwm-iqs620a.c +2 -1
index 5ede825..5eb8fa4 100644
@@ -79,6 +79,8 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0);
 		if (ret)
 			goto err_mutex;
+
+		iqs620_pwm->out_en = false;
 	}
 
 	if (duty_scale) {
@@ -97,9 +99,9 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
 		if (ret)
 			goto err_mutex;
-	}
 
Quoted 8 lines

drivers/pwm/pwm-iqs620a.c static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 104

Quoted 3 lines
drivers/pwm/pwm-iqs620a.c +2 -1
-	iqs620_pwm->out_en = state->enabled;
+		iqs620_pwm->out_en = true;
+	}

I got the problem and I agree it needs fixing. Are you aware you change the semantic of out_en here and so the behaviour of .get_state()? IMHO the change is fine however, and unless I miss something this patch makes the comment in iqs620_pwm_get_state true.

Agreed on all counts; in fact I saw this as an improvement because the get_state callback now reflects the actual state of the hardware under all circumstances.

As you mention, the comment in iqs620_pwm_get_state() is fully correct now too. Previously it was incorrect in this particular corner case.

Quoted 3 lines

Other than that I wonder if it would make more sense to track duty_scale in the driver struct instead of duty_val and out_en.

You would still have to cache state->enabled because it's required for decoding duty_scale at the time it was cached, so there is not much to gain. I prefer this solution because the get_state callback is correct across all cases now, and the change is small.

Quoted 7 lines

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

Kind regards,
Jeff LaBundy

#3 lore raw

Hello Jeff,

Quoted 2 lines · Jeff LaBundy

Thank you for taking a look; actually I came across this problem while testing your patch, so I owe you even more gratitude :)

:-)

Quoted 50 lines · Uwe Kleine-König
Quoted 40 lines · Jeff LaBundy
Quoted 39 lines

If duty cycle is first set to a value that is sufficiently high to enable the output (e.g. 10000 ns) but then lowered to a value that is quantized to zero (e.g. 1000 ns), the output is disabled as the device cannot drive a constant zero (as expected).

However if the device is later re-initialized due to watchdog bite, the output is re-enabled at the next-to-last duty cycle (10000 ns). This is because the iqs620_pwm->out_en flag unconditionally tracks state->enabled instead of what was actually written to the device.

To solve this problem, force the iqs620_pwm->out_en flag to follow the IQS620_PWR_SETTINGS_PWM_OUT field instead, as was the original design intent.

Fixes 6f0841a8197b ("pwm: Add support for Azoteq IQS620A PWM generator")
---
 drivers/pwm/pwm-iqs620a.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pwm/pwm-iqs620a.c +2 -1
index 5ede825..5eb8fa4 100644
@@ -79,6 +79,8 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0);
 		if (ret)
 			goto err_mutex;
+
+		iqs620_pwm->out_en = false;
 	}
 
 	if (duty_scale) {
@@ -97,9 +99,9 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
 		if (ret)
 			goto err_mutex;
-	}
 
Quoted 8 lines

drivers/pwm/pwm-iqs620a.c static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 104

Quoted 3 lines
drivers/pwm/pwm-iqs620a.c +2 -1
-	iqs620_pwm->out_en = state->enabled;
+		iqs620_pwm->out_en = true;
+	}

I got the problem and I agree it needs fixing. Are you aware you change the semantic of out_en here and so the behaviour of .get_state()? IMHO the change is fine however, and unless I miss something this patch makes the comment in iqs620_pwm_get_state true.

Quoted 6 lines

Agreed on all counts; in fact I saw this as an improvement because the get_state callback now reflects the actual state of the hardware under all circumstances.

As you mention, the comment in iqs620_pwm_get_state() is fully correct now too. Previously it was incorrect in this particular corner case.

ok, I wondered if I missed something.

Quoted 7 lines
Quoted 2 lines

Other than that I wonder if it would make more sense to track duty_scale in the driver struct instead of duty_val and out_en.

You would still have to cache state->enabled because it's required for decoding duty_scale at the time it was cached, so there is not much to gain. I prefer this solution because the get_state callback is correct across all cases now, and the change is small.

Can't we cope for this by just doing

	if (!state->enabled)
		duty_scale = 0;

?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#4 lore raw

Hi Uwe,

Quoted 75 lines · Uwe Kleine-König

Hello Jeff,

Quoted 2 lines · Jeff LaBundy

Thank you for taking a look; actually I came across this problem while testing your patch, so I owe you even more gratitude :)

:-)

Quoted 50 lines · Uwe Kleine-König
Quoted 40 lines · Jeff LaBundy
Quoted 39 lines

If duty cycle is first set to a value that is sufficiently high to enable the output (e.g. 10000 ns) but then lowered to a value that is quantized to zero (e.g. 1000 ns), the output is disabled as the device cannot drive a constant zero (as expected).

However if the device is later re-initialized due to watchdog bite, the output is re-enabled at the next-to-last duty cycle (10000 ns). This is because the iqs620_pwm->out_en flag unconditionally tracks state->enabled instead of what was actually written to the device.

To solve this problem, force the iqs620_pwm->out_en flag to follow the IQS620_PWR_SETTINGS_PWM_OUT field instead, as was the original design intent.

Fixes 6f0841a8197b ("pwm: Add support for Azoteq IQS620A PWM generator")
---
 drivers/pwm/pwm-iqs620a.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pwm/pwm-iqs620a.c +2 -1
index 5ede825..5eb8fa4 100644
@@ -79,6 +79,8 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0);
 		if (ret)
 			goto err_mutex;
+
+		iqs620_pwm->out_en = false;
 	}
 
 	if (duty_scale) {
@@ -97,9 +99,9 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 					 IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
 		if (ret)
 			goto err_mutex;
-	}
 
Quoted 8 lines

drivers/pwm/pwm-iqs620a.c static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 104

Quoted 3 lines
drivers/pwm/pwm-iqs620a.c +2 -1
-	iqs620_pwm->out_en = state->enabled;
+		iqs620_pwm->out_en = true;
+	}

I got the problem and I agree it needs fixing. Are you aware you change the semantic of out_en here and so the behaviour of .get_state()? IMHO the change is fine however, and unless I miss something this patch makes the comment in iqs620_pwm_get_state true.

Quoted 6 lines

Agreed on all counts; in fact I saw this as an improvement because the get_state callback now reflects the actual state of the hardware under all circumstances.

As you mention, the comment in iqs620_pwm_get_state() is fully correct now too. Previously it was incorrect in this particular corner case.

ok, I wondered if I missed something.

Quoted 7 lines
Quoted 2 lines

Other than that I wonder if it would make more sense to track duty_scale in the driver struct instead of duty_val and out_en.

You would still have to cache state->enabled because it's required for decoding duty_scale at the time it was cached, so there is not much to gain. I prefer this solution because the get_state callback is correct across all cases now, and the change is small.

Quoted 6 lines

Can't we cope for this by just doing

	if (!state->enabled)
		duty_scale = 0;

?

This is an excellent idea, and simplifies the driver significantly. I'll send a v2 shortly.

Quoted 7 lines

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

Kind regards,
Jeff LaBundy