Message ID | 20240204212043.2951852-2-u.kleine-koenig@pengutronix.de |
---|---|
State | Accepted |
Headers | show |
Series | pwm: sti: Fix capture for st,pwm-num-chan < st,capture-num-chan | expand |
Hello, On Sun, Feb 04, 2024 at 10:20:43PM +0100, Uwe Kleine-König wrote: > The driver only used the number of pwm channels to set the pwm_chip's > npwm member. The result is that if there are more capture channels than > PWM channels specified in the device tree, only a part of the capture > channel is usable. Fix that by passing the bigger channel count to the > pwm framework. This makes it possible that the .apply() callback is > called with .hwpwm >= pwm_num_devs, catch that case and return an error > code. > > Fixes: c97267ae831d ("pwm: sti: Add PWM capture callback") > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Applied to https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next Best regards Uwe
diff --git a/drivers/pwm/pwm-sti.c b/drivers/pwm/pwm-sti.c index 6cf55cf34d39..69b1113c6b82 100644 --- a/drivers/pwm/pwm-sti.c +++ b/drivers/pwm/pwm-sti.c @@ -395,8 +395,17 @@ static int sti_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm, static int sti_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_state *state) { + struct sti_pwm_chip *pc = to_sti_pwmchip(chip); + struct sti_pwm_compat_data *cdata = pc->cdata; + struct device *dev = pc->dev; int err; + if (pwm->hwpwm >= cdata->pwm_num_devs) { + dev_err(dev, "device %u is not valid for pwm mode\n", + pwm->hwpwm); + return -EINVAL; + } + if (state->polarity != PWM_POLARITY_NORMAL) return -EINVAL; @@ -646,7 +655,7 @@ static int sti_pwm_probe(struct platform_device *pdev) pc->chip.dev = dev; pc->chip.ops = &sti_pwm_ops; - pc->chip.npwm = pc->cdata->pwm_num_devs; + pc->chip.npwm = max(cdata->pwm_num_devs, cdata->cpt_num_devs); for (i = 0; i < cdata->cpt_num_devs; i++) { struct sti_cpt_ddata *ddata = &cdata->ddata[i];
The driver only used the number of pwm channels to set the pwm_chip's npwm member. The result is that if there are more capture channels than PWM channels specified in the device tree, only a part of the capture channel is usable. Fix that by passing the bigger channel count to the pwm framework. This makes it possible that the .apply() callback is called with .hwpwm >= pwm_num_devs, catch that case and return an error code. Fixes: c97267ae831d ("pwm: sti: Add PWM capture callback") Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> --- Hello, the code that is fixed here was actually introduced a bit earlier than the commit specified in the fixes line. See the commits immediately proceeding the pointed out commit. This problem only gets relevant once the capture callback is actually available, so the blamed commit is correct. In this regard the commit log of 85a834c42a49 ("pwm: sti: It's now valid for number of PWM channels to be zero") is bogus however. Best regards Uwe drivers/pwm/pwm-sti.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)