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

pwm: imx1: Implement .apply callback

To eventually get rid of all legacy drivers convert this driver to the
modern world implementing .apply(). This just pushes down a slightly
optimized variant of how legacy drivers are handled in the core.

As a side effect this improves the behaviour for big duty cycles where
max * duty_ns overflowed before.

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
b23fd25e 9136a39e

+28 -5
+28 -5
drivers/pwm/pwm-imx1.c
··· 61 61 } 62 62 63 63 static int pwm_imx1_config(struct pwm_chip *chip, 64 - struct pwm_device *pwm, int duty_ns, int period_ns) 64 + struct pwm_device *pwm, u64 duty_ns, u64 period_ns) 65 65 { 66 66 struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip); 67 67 u32 max, p; ··· 84 84 * (/2 .. /16). 85 85 */ 86 86 max = readl(imx->mmio_base + MX1_PWMP); 87 - p = max * duty_ns / period_ns; 87 + p = mul_u64_u64_div_u64(max, duty_ns, period_ns); 88 88 89 89 writel(max - p, imx->mmio_base + MX1_PWMS); 90 90 ··· 120 120 pwm_imx1_clk_disable_unprepare(chip); 121 121 } 122 122 123 + static int pwm_imx1_apply(struct pwm_chip *chip, struct pwm_device *pwm, 124 + const struct pwm_state *state) 125 + { 126 + int err; 127 + 128 + if (state->polarity != PWM_POLARITY_NORMAL) 129 + return -EINVAL; 130 + 131 + if (!state->enabled) { 132 + if (pwm->state.enabled) 133 + pwm_imx1_disable(chip, pwm); 134 + 135 + return 0; 136 + } 137 + 138 + err = pwm_imx1_config(chip, pwm, state->duty_cycle, state->period); 139 + if (err) 140 + return err; 141 + 142 + if (!pwm->state.enabled) 143 + return pwm_imx1_enable(chip, pwm); 144 + 145 + return 0; 146 + } 147 + 123 148 static const struct pwm_ops pwm_imx1_ops = { 124 - .enable = pwm_imx1_enable, 125 - .disable = pwm_imx1_disable, 126 - .config = pwm_imx1_config, 149 + .apply = pwm_imx1_apply, 127 150 .owner = THIS_MODULE, 128 151 }; 129 152