pwm: img: Turn final 'else if' into 'else' in img_pwm_config
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
@@ -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
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
Signed-off-by Nathan Chancellor <[email protected]>--- drivers/pwm/pwm-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/pwm/pwm-img.c +1
-2@@ -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
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.
Signed-off-by Nathan Chancellor <[email protected]>--- drivers/pwm/pwm-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/pwm/pwm-img.c +0
-0
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/ |
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.
Signed-off-by Nathan Chancellor <[email protected]>--- drivers/pwm/pwm-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/pwm/pwm-img.c +0
-0drivers/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_configthere 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_UPmakes 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* 512this 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_nsThis 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
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.
Signed-off-by Nathan Chancellor <[email protected]>--- drivers/pwm/pwm-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/pwm/pwm-img.c +0
-0drivers/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_hzI had trouble verifying
input_clk_hzin the above. The divisor isclk_get_rate(pwm->pwm_clk), but is it guaranteed to always beinput_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/ |
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.
Signed-off-by Nathan Chancellor <[email protected]>--- drivers/pwm/pwm-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/pwm/pwm-img.c +0
-0drivers/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_hzI had trouble verifying
input_clk_hzin the above. The divisor isclk_get_rate(pwm->pwm_clk), but is it guaranteed to always beinput_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_configwe have:input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);
Quoted 7 lines
I used the name used in
img_pwm_configwith 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
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.
Signed-off-by Nathan Chancellor <[email protected]>--- drivers/pwm/pwm-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/pwm/pwm-img.c +0
-0drivers/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_hzI had trouble verifying
input_clk_hzin the above. The divisor isclk_get_rate(pwm->pwm_clk), but is it guaranteed to always beinput_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_configwe have:input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);I used the name used in
img_pwm_configwith 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/ |
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/ |
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
Thierry Reding
AppliedQuoted 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.
Signed-off-by Nathan Chancellor <[email protected]>--- drivers/pwm/pwm-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Applied, thanks!
Thierry