pwm: meson: fix handling of period/duty if greater than UINT_MAX

[ Upstream commit 87a2cbf02d ]

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: b79c3670e1 ("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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Heiner Kallweit 2023-05-24 21:48:36 +02:00 committed by Greg Kroah-Hartman
parent bae3c43a9d
commit 7ac170d93b

View file

@ -163,8 +163,9 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
const struct pwm_state *state) const struct pwm_state *state)
{ {
struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm]; struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
unsigned int duty, period, pre_div, cnt, duty_cnt; unsigned int pre_div, cnt, duty_cnt;
unsigned long fin_freq; unsigned long fin_freq;
u64 duty, period;
duty = state->duty_cycle; duty = state->duty_cycle;
period = state->period; period = state->period;
@ -186,19 +187,19 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq); dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq);
pre_div = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * 0xffffLL); pre_div = div64_u64(fin_freq * period, NSEC_PER_SEC * 0xffffLL);
if (pre_div > MISC_CLK_DIV_MASK) { if (pre_div > MISC_CLK_DIV_MASK) {
dev_err(meson->chip.dev, "unable to get period pre_div\n"); dev_err(meson->chip.dev, "unable to get period pre_div\n");
return -EINVAL; return -EINVAL;
} }
cnt = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * (pre_div + 1)); cnt = div64_u64(fin_freq * period, NSEC_PER_SEC * (pre_div + 1));
if (cnt > 0xffff) { if (cnt > 0xffff) {
dev_err(meson->chip.dev, "unable to get period cnt\n"); dev_err(meson->chip.dev, "unable to get period cnt\n");
return -EINVAL; return -EINVAL;
} }
dev_dbg(meson->chip.dev, "period=%u pre_div=%u cnt=%u\n", period, dev_dbg(meson->chip.dev, "period=%llu pre_div=%u cnt=%u\n", period,
pre_div, cnt); pre_div, cnt);
if (duty == period) { if (duty == period) {
@ -211,14 +212,13 @@ static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
channel->lo = cnt; channel->lo = cnt;
} else { } else {
/* Then check is we can have the duty with the same pre_div */ /* Then check is we can have the duty with the same pre_div */
duty_cnt = div64_u64(fin_freq * (u64)duty, duty_cnt = div64_u64(fin_freq * duty, NSEC_PER_SEC * (pre_div + 1));
NSEC_PER_SEC * (pre_div + 1));
if (duty_cnt > 0xffff) { if (duty_cnt > 0xffff) {
dev_err(meson->chip.dev, "unable to get duty cycle\n"); dev_err(meson->chip.dev, "unable to get duty cycle\n");
return -EINVAL; return -EINVAL;
} }
dev_dbg(meson->chip.dev, "duty=%u pre_div=%u duty_cnt=%u\n", dev_dbg(meson->chip.dev, "duty=%llu pre_div=%u duty_cnt=%u\n",
duty, pre_div, duty_cnt); duty, pre_div, duty_cnt);
channel->pre_div = pre_div; channel->pre_div = pre_div;