Skip to contentSkip to conversation

pwm: img: Turn final 'else if' into 'else' in img_pwm_config

PATCH Applied Nathan Chancellor 4 participants 10 messages Mar 7, 2019 – Mar 20, 2019 PWM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pwm: img: Turn final 'else if' into 'else' in img_pwm_config

When building with -Wsometimes-uninitialized, Clang warns:

drivers/pwm/pwm-img.c:126:13: error: variable 'timebase' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

The final else if functions as an else; make that explicit so that Clang understands that timebase cannot be used uninitialized.

---
 drivers/pwm/pwm-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-img.c +1 -1
index 815f5333bb8f..1cc5fbe1e1d3 100644
@@ -123,7 +123,7 @@ static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	} else if (mul <= max_timebase * 512) {
 		div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
 		timebase = DIV_ROUND_UP(mul, 512);
-	} else if (mul > max_timebase * 512) {
+	} else {
 		dev_err(chip->dev,
 			"failed to configure timebase steps/divider value\n");
 		return -EINVAL;
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/pwm/pwm-img.c:126:13: error: variable 'timebase' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

The final else if functions as an else; make that explicit so that Clang understands that timebase cannot be used uninitialized.

A welcome simplification! Thank you.

Quoted 23 lines
---
 drivers/pwm/pwm-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-img.c +1 -2
index 815f5333bb8f..1cc5fbe1e1d3 100644
@@ -123,7 +123,7 @@ static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
        } else if (mul <= max_timebase * 512) {
                div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
                timebase = DIV_ROUND_UP(mul, 512);
-       } else if (mul > max_timebase * 512) {
+       } else {
                dev_err(chip->dev,
                        "failed to configure timebase steps/divider value\n");
                return -EINVAL;
--

2.21.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
In reply to Nathan Chancellor
Quoted 20 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/pwm/pwm-img.c:126:13: error: variable 'timebase' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

The final else if functions as an else; make that explicit so that Clang understands that timebase cannot be used uninitialized.

---
 drivers/pwm/pwm-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-img.c +0 -0
index 815f5333bb8f..1cc5fbe1e1d3 100644

drivers/pwm/pwm-img.c static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 129

Quoted 8 lines
drivers/pwm/pwm-img.c +1 -1
@@ -123,7 +123,7 @@ static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	} else if (mul <= max_timebase * 512) {
 		div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
 		timebase = DIV_ROUND_UP(mul, 512);
-	} else if (mul > max_timebase * 512) {
+	} else {
 		dev_err(chip->dev,
 			"failed to configure timebase steps/divider value\n");
 		return -EINVAL;

This can even be simplified further.

From the probe function we have:

	pwm_chip->max_period_ns = NSEC_PER_SEC * 512 * max_timebase / input_clk_hz

Then in img_pwm_config there is:

mul = ⎡ input_clk_hz / ⎡ NSEC_PER_SEC / period_ns⎤⎤

(Not sure this term is the best we can come up with. The rounding strategy looks strange because the first DIV_ROUND_UP makes mul smaller while the second makes it bigger. This is similar to

⎡ input_clk_hz * period_ns / NSEC_PER_SEC ⎤

which is probably more exact and cheaper to calculate.)

If we now had

mul > max_timebase * 512

this results in (apart from rounding errors):

input_clk_hz * period_ns / NSEC_PER_SEC > max_timebase * 512
<=> period_ns > max_timebase * 512 * NSEC_PER_SEC / input_clk_hz
<=> period_ns > pwm_chip->max_period_ns

This however is already ruled out by the first check in img_pwm_config().

So if the rounding would be fixed, the else is a dead branch and could be dropped completely.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#3 lore raw
Quoted 33 lines · Uwe Kleine-König
Quoted 20 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/pwm/pwm-img.c:126:13: error: variable 'timebase' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

The final else if functions as an else; make that explicit so that Clang understands that timebase cannot be used uninitialized.

---
 drivers/pwm/pwm-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-img.c +0 -0
index 815f5333bb8f..1cc5fbe1e1d3 100644

drivers/pwm/pwm-img.c static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 129

Quoted 8 lines
drivers/pwm/pwm-img.c +1 -1
@@ -123,7 +123,7 @@ static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
      } else if (mul <= max_timebase * 512) {
              div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
              timebase = DIV_ROUND_UP(mul, 512);
-     } else if (mul > max_timebase * 512) {
+     } else {
              dev_err(chip->dev,
                      "failed to configure timebase steps/divider value\n");
              return -EINVAL;

This can even be simplified further.

Quoted 3 lines

From the probe function we have:

        pwm_chip->max_period_ns = NSEC_PER_SEC * 512 * max_timebase / input_clk_hz

I had trouble verifying input_clk_hz in the above. The divisor is clk_get_rate(pwm->pwm_clk), but is it guaranteed to always be input_clk_hz? If so, where?

Quoted 28 lines

Then in img_pwm_config there is:

mul = ⎡ input_clk_hz / ⎡ NSEC_PER_SEC / period_ns⎤⎤

(Not sure this term is the best we can come up with. The rounding strategy looks strange because the first DIV_ROUND_UP makes mul smaller while the second makes it bigger. This is similar to

⎡ input_clk_hz * period_ns / NSEC_PER_SEC ⎤

which is probably more exact and cheaper to calculate.)

If we now had

mul > max_timebase * 512

this results in (apart from rounding errors):

input_clk_hz * period_ns / NSEC_PER_SEC > max_timebase * 512
<=> period_ns > max_timebase * 512 * NSEC_PER_SEC / input_clk_hz
<=> period_ns > pwm_chip->max_period_ns

This however is already ruled out by the first check in img_pwm_config().

So if the rounding would be fixed, the else is a dead branch and could be dropped completely.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#4 lore raw
Quoted 35 lines · Nick Desaulniers
Quoted 33 lines · Uwe Kleine-König
Quoted 20 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/pwm/pwm-img.c:126:13: error: variable 'timebase' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

The final else if functions as an else; make that explicit so that Clang understands that timebase cannot be used uninitialized.

---
 drivers/pwm/pwm-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-img.c +0 -0
index 815f5333bb8f..1cc5fbe1e1d3 100644

drivers/pwm/pwm-img.c static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 129

Quoted 8 lines
drivers/pwm/pwm-img.c +1 -1
@@ -123,7 +123,7 @@ static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
      } else if (mul <= max_timebase * 512) {
              div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
              timebase = DIV_ROUND_UP(mul, 512);
-     } else if (mul > max_timebase * 512) {
+     } else {
              dev_err(chip->dev,
                      "failed to configure timebase steps/divider value\n");
              return -EINVAL;

This can even be simplified further.

Quoted 7 lines
Quoted 3 lines

From the probe function we have:

        pwm_chip->max_period_ns = NSEC_PER_SEC * 512 * max_timebase / input_clk_hz

I had trouble verifying input_clk_hz in the above. The divisor is clk_get_rate(pwm->pwm_clk), but is it guaranteed to always be input_clk_hz? If so, where?

In the probe function it's called "clk_rate". We have:

	clk_rate = clk_get_rate(pwm->pwm_clk);
	...
	val = (u64)NSEC_PER_SEC * 512 * pwm->data->max_timebase;
	do_div(val, clk_rate);
	pwm->max_period_ns = val;

and in img_pwm_config we have:

	input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);

I used the name used in img_pwm_config with the intention that my reasoning is easier to understand, but obviously I failed.

This by the way highlights another patch opportunity: Unify the names that the same thing gets the same name in the different functions. This doesn't only affect "clk_rate" vs "input_clk_hz", but also "pwm_chip" vs. "pwm".

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#5 lore raw
Quoted 57 lines · Uwe Kleine-König
Quoted 35 lines · Nick Desaulniers
Quoted 33 lines · Uwe Kleine-König
Quoted 20 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/pwm/pwm-img.c:126:13: error: variable 'timebase' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

The final else if functions as an else; make that explicit so that Clang understands that timebase cannot be used uninitialized.

---
 drivers/pwm/pwm-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-img.c +0 -0
index 815f5333bb8f..1cc5fbe1e1d3 100644

drivers/pwm/pwm-img.c static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 129

Quoted 8 lines
drivers/pwm/pwm-img.c +1 -1
@@ -123,7 +123,7 @@ static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
      } else if (mul <= max_timebase * 512) {
              div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
              timebase = DIV_ROUND_UP(mul, 512);
-     } else if (mul > max_timebase * 512) {
+     } else {
              dev_err(chip->dev,
                      "failed to configure timebase steps/divider value\n");
              return -EINVAL;

This can even be simplified further.

Quoted 7 lines
Quoted 3 lines

From the probe function we have:

        pwm_chip->max_period_ns = NSEC_PER_SEC * 512 * max_timebase / input_clk_hz

I had trouble verifying input_clk_hz in the above. The divisor is clk_get_rate(pwm->pwm_clk), but is it guaranteed to always be input_clk_hz? If so, where?

In the probe function it's called "clk_rate". We have:

        clk_rate = clk_get_rate(pwm->pwm_clk);
        ...
        val = (u64)NSEC_PER_SEC * 512 * pwm->data->max_timebase;
        do_div(val, clk_rate);
        pwm->max_period_ns = val;

and in img_pwm_config we have:

        input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);
Quoted 7 lines

I used the name used in img_pwm_config with the intention that my reasoning is easier to understand, but obviously I failed.

This by the way highlights another patch opportunity: Unify the names that the same thing gets the same name in the different functions. This doesn't only affect "clk_rate" vs "input_clk_hz", but also "pwm_chip" vs. "pwm".

Sure, those seem like nice little cleanups that will help with maintainability. Would you mind sending a patch for those, and I'll review? Otherwise Nathan's patch exists and is ready to go.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#6 lore raw
Quoted 62 lines · Nick Desaulniers
Quoted 60 lines · Uwe Kleine-König
Quoted 35 lines · Nick Desaulniers
Quoted 33 lines · Uwe Kleine-König
Quoted 20 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/pwm/pwm-img.c:126:13: error: variable 'timebase' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

The final else if functions as an else; make that explicit so that Clang understands that timebase cannot be used uninitialized.

---
 drivers/pwm/pwm-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-img.c +0 -0
index 815f5333bb8f..1cc5fbe1e1d3 100644

drivers/pwm/pwm-img.c static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · line 129

Quoted 8 lines
drivers/pwm/pwm-img.c +1 -1
@@ -123,7 +123,7 @@ static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
      } else if (mul <= max_timebase * 512) {
              div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
              timebase = DIV_ROUND_UP(mul, 512);
-     } else if (mul > max_timebase * 512) {
+     } else {
              dev_err(chip->dev,
                      "failed to configure timebase steps/divider value\n");
              return -EINVAL;

This can even be simplified further.

Quoted 7 lines
Quoted 3 lines

From the probe function we have:

        pwm_chip->max_period_ns = NSEC_PER_SEC * 512 * max_timebase / input_clk_hz

I had trouble verifying input_clk_hz in the above. The divisor is clk_get_rate(pwm->pwm_clk), but is it guaranteed to always be input_clk_hz? If so, where?

In the probe function it's called "clk_rate". We have:

        clk_rate = clk_get_rate(pwm->pwm_clk);
        ...
        val = (u64)NSEC_PER_SEC * 512 * pwm->data->max_timebase;
        do_div(val, clk_rate);
        pwm->max_period_ns = val;

and in img_pwm_config we have:

        input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);

I used the name used in img_pwm_config with the intention that my reasoning is easier to understand, but obviously I failed.

Quoted 8 lines
Quoted 4 lines

This by the way highlights another patch opportunity: Unify the names that the same thing gets the same name in the different functions. This doesn't only affect "clk_rate" vs "input_clk_hz", but also "pwm_chip" vs. "pwm".

Sure, those seem like nice little cleanups that will help with maintainability. Would you mind sending a patch for those, and I'll review? Otherwise Nathan's patch exists and is ready to go.

I don't mind. But I would prefer to get some input from Thierry about how to round first. I also wonder if there is a publically available manual for this piece of hardware.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#7 lore raw
In reply to Nathan Chancellor

Hello Nathan,

the copy of my mail sent to [email protected] was not accepted. (So I dropped this address from the recipients for this reply.) Probably this list isn't open for non-members to post to. This is a bit annoying, so I ask you to either fix that or don't add this address to the recipients of your posts where non-members might answer.

Thanks
Uwe

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

Hello Nathan,

the copy of my mail sent to [email protected] was not accepted. (So I dropped this address from the recipients for this reply.) Probably this list isn't open for non-members to post to. This is a bit annoying, so I ask you to either fix that or don't add this address to the recipients of your posts where non-members might answer.

Ah, sorry, I did not realize that it would annoy people like this when I set it up. We've email vger post master to get us set up with a list. No response yet.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#9 lore raw
In reply to Nathan Chancellor
Quoted 6 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/pwm/pwm-img.c:126:13: error: variable 'timebase' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

Quoted 8 lines

The final else if functions as an else; make that explicit so that Clang understands that timebase cannot be used uninitialized.

---
 drivers/pwm/pwm-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Applied, thanks!

Thierry