Lines Matching +full:pwm +full:- +full:offset
1 // SPDX-License-Identifier: GPL-2.0-only
15 #include <linux/pwm.h>
38 static int stmpe_24xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) in stmpe_24xx_pwm_enable() argument
44 ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS); in stmpe_24xx_pwm_enable()
46 dev_dbg(pwmchip_parent(chip), "error reading PWM#%u control\n", in stmpe_24xx_pwm_enable()
47 pwm->hwpwm); in stmpe_24xx_pwm_enable()
51 value = ret | BIT(pwm->hwpwm); in stmpe_24xx_pwm_enable()
53 ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value); in stmpe_24xx_pwm_enable()
55 dev_dbg(pwmchip_parent(chip), "error writing PWM#%u control\n", in stmpe_24xx_pwm_enable()
56 pwm->hwpwm); in stmpe_24xx_pwm_enable()
64 struct pwm_device *pwm) in stmpe_24xx_pwm_disable() argument
70 ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS); in stmpe_24xx_pwm_disable()
72 dev_dbg(pwmchip_parent(chip), "error reading PWM#%u control\n", in stmpe_24xx_pwm_disable()
73 pwm->hwpwm); in stmpe_24xx_pwm_disable()
77 value = ret & ~BIT(pwm->hwpwm); in stmpe_24xx_pwm_disable()
79 ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value); in stmpe_24xx_pwm_disable()
81 dev_dbg(pwmchip_parent(chip), "error writing PWM#%u control\n", in stmpe_24xx_pwm_disable()
82 pwm->hwpwm); in stmpe_24xx_pwm_disable()
86 /* STMPE 24xx PWM instructions */
97 static int stmpe_24xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, in stmpe_24xx_pwm_config() argument
107 u8 offset; in stmpe_24xx_pwm_config() local
111 if (pwm_is_enabled(pwm)) { in stmpe_24xx_pwm_config()
112 ret = stmpe_24xx_pwm_disable(chip, pwm); in stmpe_24xx_pwm_config()
116 /* Connect the PWM to the pin */ in stmpe_24xx_pwm_config()
117 pin = pwm->hwpwm; in stmpe_24xx_pwm_config()
120 if (stmpe_pwm->stmpe->partnum == STMPE2401 || in stmpe_24xx_pwm_config()
121 stmpe_pwm->stmpe->partnum == STMPE2403) in stmpe_24xx_pwm_config()
124 ret = stmpe_set_altfunc(stmpe_pwm->stmpe, BIT(pin), in stmpe_24xx_pwm_config()
127 dev_err(pwmchip_parent(chip), "unable to connect PWM#%u to pin\n", in stmpe_24xx_pwm_config()
128 pwm->hwpwm); in stmpe_24xx_pwm_config()
134 switch (pwm->hwpwm) { in stmpe_24xx_pwm_config()
136 offset = STMPE24XX_PWMIC0; in stmpe_24xx_pwm_config()
140 offset = STMPE24XX_PWMIC1; in stmpe_24xx_pwm_config()
144 offset = STMPE24XX_PWMIC2; in stmpe_24xx_pwm_config()
149 return -ENODEV; in stmpe_24xx_pwm_config()
152 dev_dbg(pwmchip_parent(chip), "PWM#%u: config duty %d ns, period %d ns\n", in stmpe_24xx_pwm_config()
153 pwm->hwpwm, duty_ns, period_ns); in stmpe_24xx_pwm_config()
156 if (stmpe_pwm->stmpe->partnum == STMPE2401) in stmpe_24xx_pwm_config()
159 if (stmpe_pwm->stmpe->partnum == STMPE2403) in stmpe_24xx_pwm_config()
162 stmpe_pwm->last_duty = 0x00; in stmpe_24xx_pwm_config()
164 if (stmpe_pwm->stmpe->partnum == STMPE2401) in stmpe_24xx_pwm_config()
167 if (stmpe_pwm->stmpe->partnum == STMPE2403) in stmpe_24xx_pwm_config()
170 stmpe_pwm->last_duty = 0xff; in stmpe_24xx_pwm_config()
172 u8 value, last = stmpe_pwm->last_duty; in stmpe_24xx_pwm_config()
178 * counter from the ramp, if this is >= PWM counter the output in stmpe_24xx_pwm_config()
182 * Prescale = 0 -> 2 kHz -> T = 1/f = 488281.25 ns in stmpe_24xx_pwm_config()
192 if (pwm_is_enabled(pwm)) in stmpe_24xx_pwm_config()
193 stmpe_24xx_pwm_enable(chip, pwm); in stmpe_24xx_pwm_config()
196 } else if (stmpe_pwm->stmpe->partnum == STMPE2403) { in stmpe_24xx_pwm_config()
197 /* STMPE2403 can simply set the right PWM value */ in stmpe_24xx_pwm_config()
200 } else if (stmpe_pwm->stmpe->partnum == STMPE2401) { in stmpe_24xx_pwm_config()
206 incdec = RAMPUP | (value - last); in stmpe_24xx_pwm_config()
209 incdec = RAMPDOWN | (last - value); in stmpe_24xx_pwm_config()
219 "PWM#%u: value = %02x, last_duty = %02x, program=%04x,%04x,%04x\n", in stmpe_24xx_pwm_config()
220 pwm->hwpwm, value, last, program[0], program[1], in stmpe_24xx_pwm_config()
222 stmpe_pwm->last_duty = value; in stmpe_24xx_pwm_config()
226 * We can write programs of up to 64 16-bit words into this channel. in stmpe_24xx_pwm_config()
233 ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value); in stmpe_24xx_pwm_config()
236 offset, ret); in stmpe_24xx_pwm_config()
242 ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value); in stmpe_24xx_pwm_config()
245 offset, ret); in stmpe_24xx_pwm_config()
250 /* If we were enabled, re-enable this PWM */ in stmpe_24xx_pwm_config()
251 if (pwm_is_enabled(pwm)) in stmpe_24xx_pwm_config()
252 stmpe_24xx_pwm_enable(chip, pwm); in stmpe_24xx_pwm_config()
257 dev_dbg(pwmchip_parent(chip), "programmed PWM#%u, %u bytes\n", pwm->hwpwm, i); in stmpe_24xx_pwm_config()
262 static int stmpe_24xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in stmpe_24xx_pwm_apply() argument
267 if (state->polarity != PWM_POLARITY_NORMAL) in stmpe_24xx_pwm_apply()
268 return -EINVAL; in stmpe_24xx_pwm_apply()
270 if (!state->enabled) { in stmpe_24xx_pwm_apply()
271 if (pwm->state.enabled) in stmpe_24xx_pwm_apply()
272 return stmpe_24xx_pwm_disable(chip, pwm); in stmpe_24xx_pwm_apply()
277 err = stmpe_24xx_pwm_config(chip, pwm, state->duty_cycle, state->period); in stmpe_24xx_pwm_apply()
281 if (!pwm->state.enabled) in stmpe_24xx_pwm_apply()
282 err = stmpe_24xx_pwm_enable(chip, pwm); in stmpe_24xx_pwm_apply()
293 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent); in stmpe_pwm_probe()
298 switch (stmpe->partnum) { in stmpe_pwm_probe()
303 return dev_err_probe(&pdev->dev, -ENODEV, in stmpe_pwm_probe()
306 return dev_err_probe(&pdev->dev, -ENODEV, in stmpe_pwm_probe()
307 "Unknown STMPE PWM\n"); in stmpe_pwm_probe()
310 chip = devm_pwmchip_alloc(&pdev->dev, 3, sizeof(*stmpe_pwm)); in stmpe_pwm_probe()
315 stmpe_pwm->stmpe = stmpe; in stmpe_pwm_probe()
317 chip->ops = &stmpe_24xx_pwm_ops; in stmpe_pwm_probe()
334 .name = "stmpe-pwm",