Skip to content

Commit 72d6b24

Browse files
Uwe Kleine-Königthierryreding
authored andcommitted
pwm: iqs620a: Fix overflow and optimize calculations
If state->duty_cycle is 0x100000000000000, the previous calculation of duty_scale overflows and yields a duty cycle ratio of 0% instead of 100%. Fix this by clamping the requested duty cycle to the maximal possible duty cycle first. This way it is possible to use a native integer division instead of a (depending on the architecture) more expensive 64bit division. With this change in place duty_scale cannot be bigger than 256 which allows to simplify the calculation of duty_val. Fixes: 6f0841a ("pwm: Add support for Azoteq IQS620A PWM generator") Tested-by: Jeff LaBundy <jeff@labundy.com> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
1 parent 11be938 commit 72d6b24

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎drivers/pwm/pwm-iqs620a.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
4646
{
4747
struct iqs620_pwm_private *iqs620_pwm;
4848
struct iqs62x_core *iqs62x;
49-
u64 duty_scale;
49+
unsigned int duty_cycle;
50+
unsigned int duty_scale;
5051
int ret;
5152

5253
if (state->polarity != PWM_POLARITY_NORMAL)
@@ -70,7 +71,8 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
7071
* For lower duty cycles (e.g. 0), the PWM output is simply disabled to
7172
* allow an external pull-down resistor to hold the GPIO3/LTX pin low.
7273
*/
73-
duty_scale = div_u64(state->duty_cycle * 256, IQS620_PWM_PERIOD_NS);
74+
duty_cycle = min_t(u64, state->duty_cycle, IQS620_PWM_PERIOD_NS);
75+
duty_scale = duty_cycle * 256 / IQS620_PWM_PERIOD_NS;
7476

7577
mutex_lock(&iqs620_pwm->lock);
7678

@@ -82,7 +84,7 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
8284
}
8385

8486
if (duty_scale) {
85-
u8 duty_val = min_t(u64, duty_scale - 1, 0xff);
87+
u8 duty_val = duty_scale - 1;
8688

8789
ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
8890
duty_val);

0 commit comments

Comments
 (0)