Skip to content

Commit 87a2cbf

Browse files
hkallweitthierryreding
authored andcommitted
pwm: meson: fix handling of period/duty if greater than UINT_MAX
state->period/duty are of type u64, and if their value is greater than UINT_MAX, then the cast to uint will cause problems. Fix this by changing the type of the respective local variables to u64. Fixes: b79c367 ("pwm: meson: Don't duplicate the polarity internally") Cc: stable@vger.kernel.org Suggested-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Reviewed-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com> Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
1 parent 6b9352f commit 87a2cbf

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

drivers/pwm/pwm-meson.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
156156
const struct pwm_state *state)
157157
{
158158
struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
159-
unsigned int duty, period, pre_div, cnt, duty_cnt;
159+
unsigned int pre_div, cnt, duty_cnt;
160160
unsigned long fin_freq;
161+
u64 duty, period;
161162

162163
duty = state->duty_cycle;
163164
period = state->period;
@@ -179,19 +180,19 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
179180

180181
dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq);
181182

182-
pre_div = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * 0xffffLL);
183+
pre_div = div64_u64(fin_freq * period, NSEC_PER_SEC * 0xffffLL);
183184
if (pre_div > MISC_CLK_DIV_MASK) {
184185
dev_err(meson->chip.dev, "unable to get period pre_div\n");
185186
return -EINVAL;
186187
}
187188

188-
cnt = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * (pre_div + 1));
189+
cnt = div64_u64(fin_freq * period, NSEC_PER_SEC * (pre_div + 1));
189190
if (cnt > 0xffff) {
190191
dev_err(meson->chip.dev, "unable to get period cnt\n");
191192
return -EINVAL;
192193
}
193194

194-
dev_dbg(meson->chip.dev, "period=%u pre_div=%u cnt=%u\n", period,
195+
dev_dbg(meson->chip.dev, "period=%llu pre_div=%u cnt=%u\n", period,
195196
pre_div, cnt);
196197

197198
if (duty == period) {
@@ -204,14 +205,13 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
204205
channel->lo = cnt;
205206
} else {
206207
/* Then check is we can have the duty with the same pre_div */
207-
duty_cnt = div64_u64(fin_freq * (u64)duty,
208-
NSEC_PER_SEC * (pre_div + 1));
208+
duty_cnt = div64_u64(fin_freq * duty, NSEC_PER_SEC * (pre_div + 1));
209209
if (duty_cnt > 0xffff) {
210210
dev_err(meson->chip.dev, "unable to get duty cycle\n");
211211
return -EINVAL;
212212
}
213213

214-
dev_dbg(meson->chip.dev, "duty=%u pre_div=%u duty_cnt=%u\n",
214+
dev_dbg(meson->chip.dev, "duty=%llu pre_div=%u duty_cnt=%u\n",
215215
duty, pre_div, duty_cnt);
216216

217217
channel->pre_div = pre_div;

0 commit comments

Comments
 (0)