Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pwm: atmel: Improve duty cycle calculation in .apply()

In the calculation of the register value determining the duty cycle the
requested period is used instead of the actually implemented period which
results in suboptimal settings.

The following example assumes an input clock of 133333333 Hz on one of
the SoCs with 16 bit period.

When the following state is to be applied:

.period = 414727681
.duty_cycle = 652806

the following register values used to be calculated:

PRES = 10
CPRD = 54000
CDTY = 53916

which yields an actual duty cycle of a bit more than 645120 ns.

The setting

PRES = 10
CPRD = 54000
CDTY = 53915

however yields a duty of 652800 ns which is between the current result
and the requested value and so is a better approximation.

The reason for this error is that for the calculation of CDTY the
requested period was used instead of the actually implemented one.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>

authored by

Uwe Kleine-König and committed by
Thierry Reding
8035e6c6 453e8b3d

+16 -7
+16 -7
drivers/pwm/pwm-atmel.c
··· 124 124 } 125 125 126 126 static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip, 127 + unsigned long clkrate, 127 128 const struct pwm_state *state, 128 129 unsigned long *cprd, u32 *pres) 129 130 { ··· 133 132 int shift; 134 133 135 134 /* Calculate the period cycles and prescale value */ 136 - cycles *= clk_get_rate(atmel_pwm->clk); 135 + cycles *= clkrate; 137 136 do_div(cycles, NSEC_PER_SEC); 138 137 139 138 /* ··· 159 158 } 160 159 161 160 static void atmel_pwm_calculate_cdty(const struct pwm_state *state, 162 - unsigned long cprd, unsigned long *cdty) 161 + unsigned long clkrate, unsigned long cprd, 162 + u32 pres, unsigned long *cdty) 163 163 { 164 164 unsigned long long cycles = state->duty_cycle; 165 165 166 - cycles *= cprd; 167 - do_div(cycles, state->period); 166 + cycles *= clkrate; 167 + do_div(cycles, NSEC_PER_SEC); 168 + cycles >>= pres; 168 169 *cdty = cprd - cycles; 169 170 } 170 171 ··· 247 244 pwm_get_state(pwm, &cstate); 248 245 249 246 if (state->enabled) { 247 + unsigned long clkrate = clk_get_rate(atmel_pwm->clk); 248 + 250 249 if (cstate.enabled && 251 250 cstate.polarity == state->polarity && 252 251 cstate.period == state->period) { 252 + u32 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR); 253 + 253 254 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, 254 255 atmel_pwm->data->regs.period); 255 - atmel_pwm_calculate_cdty(state, cprd, &cdty); 256 + pres = cmr & PWM_CMR_CPRE_MSK; 257 + 258 + atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty); 256 259 atmel_pwm_update_cdty(chip, pwm, cdty); 257 260 return 0; 258 261 } 259 262 260 - ret = atmel_pwm_calculate_cprd_and_pres(chip, state, &cprd, 263 + ret = atmel_pwm_calculate_cprd_and_pres(chip, clkrate, state, &cprd, 261 264 &pres); 262 265 if (ret) { 263 266 dev_err(chip->dev, ··· 271 262 return ret; 272 263 } 273 264 274 - atmel_pwm_calculate_cdty(state, cprd, &cdty); 265 + atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty); 275 266 276 267 if (cstate.enabled) { 277 268 atmel_pwm_disable(chip, pwm, false);