Lines Matching +full:pwm +full:- +full:0

1 // SPDX-License-Identifier: GPL-2.0-only
15 #include <linux/pwm.h>
18 #define STMPE24XX_PWMCS 0x30
19 #define PWMCS_EN_PWM0 BIT(0)
22 #define STMPE24XX_PWMIC0 0x38
23 #define STMPE24XX_PWMIC1 0x39
24 #define STMPE24XX_PWMIC2 0x3a
39 static int stmpe_24xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) in stmpe_24xx_pwm_enable() argument
45 ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS); in stmpe_24xx_pwm_enable()
46 if (ret < 0) { in stmpe_24xx_pwm_enable()
47 dev_err(chip->dev, "error reading PWM#%u control\n", in stmpe_24xx_pwm_enable()
48 pwm->hwpwm); in stmpe_24xx_pwm_enable()
52 value = ret | BIT(pwm->hwpwm); in stmpe_24xx_pwm_enable()
54 ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value); in stmpe_24xx_pwm_enable()
56 dev_err(chip->dev, "error writing PWM#%u control\n", in stmpe_24xx_pwm_enable()
57 pwm->hwpwm); in stmpe_24xx_pwm_enable()
61 return 0; in stmpe_24xx_pwm_enable()
65 struct pwm_device *pwm) in stmpe_24xx_pwm_disable() argument
71 ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS); in stmpe_24xx_pwm_disable()
72 if (ret < 0) { in stmpe_24xx_pwm_disable()
73 dev_err(chip->dev, "error reading PWM#%u control\n", in stmpe_24xx_pwm_disable()
74 pwm->hwpwm); in stmpe_24xx_pwm_disable()
78 value = ret & ~BIT(pwm->hwpwm); in stmpe_24xx_pwm_disable()
80 ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value); in stmpe_24xx_pwm_disable()
82 dev_err(chip->dev, "error writing PWM#%u control\n", in stmpe_24xx_pwm_disable()
83 pwm->hwpwm); in stmpe_24xx_pwm_disable()
87 /* STMPE 24xx PWM instructions */
88 #define SMAX 0x007f
89 #define SMIN 0x00ff
90 #define GTS 0x0000
92 #define RAMPUP 0x0000
98 static int stmpe_24xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, in stmpe_24xx_pwm_config() argument
112 if (pwm_is_enabled(pwm)) { in stmpe_24xx_pwm_config()
113 ret = stmpe_24xx_pwm_disable(chip, pwm); in stmpe_24xx_pwm_config()
117 /* Connect the PWM to the pin */ in stmpe_24xx_pwm_config()
118 pin = pwm->hwpwm; in stmpe_24xx_pwm_config()
121 if (stmpe_pwm->stmpe->partnum == STMPE2401 || in stmpe_24xx_pwm_config()
122 stmpe_pwm->stmpe->partnum == STMPE2403) in stmpe_24xx_pwm_config()
125 ret = stmpe_set_altfunc(stmpe_pwm->stmpe, BIT(pin), in stmpe_24xx_pwm_config()
128 dev_err(chip->dev, "unable to connect PWM#%u to pin\n", in stmpe_24xx_pwm_config()
129 pwm->hwpwm); in stmpe_24xx_pwm_config()
135 switch (pwm->hwpwm) { in stmpe_24xx_pwm_config()
136 case 0: in stmpe_24xx_pwm_config()
150 return -ENODEV; in stmpe_24xx_pwm_config()
153 dev_dbg(chip->dev, "PWM#%u: config duty %d ns, period %d ns\n", in stmpe_24xx_pwm_config()
154 pwm->hwpwm, duty_ns, period_ns); in stmpe_24xx_pwm_config()
156 if (duty_ns == 0) { in stmpe_24xx_pwm_config()
157 if (stmpe_pwm->stmpe->partnum == STMPE2401) in stmpe_24xx_pwm_config()
158 program[0] = SMAX; /* off all the time */ in stmpe_24xx_pwm_config()
160 if (stmpe_pwm->stmpe->partnum == STMPE2403) in stmpe_24xx_pwm_config()
161 program[0] = LOAD | 0xff; /* LOAD 0xff */ in stmpe_24xx_pwm_config()
163 stmpe_pwm->last_duty = 0x00; in stmpe_24xx_pwm_config()
165 if (stmpe_pwm->stmpe->partnum == STMPE2401) in stmpe_24xx_pwm_config()
166 program[0] = SMIN; /* on all the time */ in stmpe_24xx_pwm_config()
168 if (stmpe_pwm->stmpe->partnum == STMPE2403) in stmpe_24xx_pwm_config()
169 program[0] = LOAD | 0x00; /* LOAD 0x00 */ in stmpe_24xx_pwm_config()
171 stmpe_pwm->last_duty = 0xff; in stmpe_24xx_pwm_config()
173 u8 value, last = stmpe_pwm->last_duty; in stmpe_24xx_pwm_config()
177 * Counter goes from 0x00 to 0xff repeatedly at 32768 Hz, in stmpe_24xx_pwm_config()
179 * counter from the ramp, if this is >= PWM counter the output in stmpe_24xx_pwm_config()
183 * Prescale = 0 -> 2 kHz -> T = 1/f = 488281.25 ns in stmpe_24xx_pwm_config()
186 /* Scale to 0..0xff */ in stmpe_24xx_pwm_config()
193 if (pwm_is_enabled(pwm)) in stmpe_24xx_pwm_config()
194 stmpe_24xx_pwm_enable(chip, pwm); in stmpe_24xx_pwm_config()
196 return 0; in stmpe_24xx_pwm_config()
197 } else if (stmpe_pwm->stmpe->partnum == STMPE2403) { in stmpe_24xx_pwm_config()
198 /* STMPE2403 can simply set the right PWM value */ in stmpe_24xx_pwm_config()
199 program[0] = LOAD | value; in stmpe_24xx_pwm_config()
200 program[1] = 0x0000; in stmpe_24xx_pwm_config()
201 } else if (stmpe_pwm->stmpe->partnum == STMPE2401) { in stmpe_24xx_pwm_config()
203 u16 incdec = 0x0000; in stmpe_24xx_pwm_config()
207 incdec = RAMPUP | (value - last); in stmpe_24xx_pwm_config()
210 incdec = RAMPDOWN | (last - value); in stmpe_24xx_pwm_config()
213 program[0] = PRESCALE_512 | STEPTIME_1 | incdec; in stmpe_24xx_pwm_config()
215 /* Loop eternally to 0x00 */ in stmpe_24xx_pwm_config()
219 dev_dbg(chip->dev, in stmpe_24xx_pwm_config()
220 "PWM#%u: value = %02x, last_duty = %02x, program=%04x,%04x,%04x\n", in stmpe_24xx_pwm_config()
221 pwm->hwpwm, value, last, program[0], program[1], in stmpe_24xx_pwm_config()
223 stmpe_pwm->last_duty = value; in stmpe_24xx_pwm_config()
227 * We can write programs of up to 64 16-bit words into this channel. in stmpe_24xx_pwm_config()
229 for (i = 0; i < ARRAY_SIZE(program); i++) { in stmpe_24xx_pwm_config()
232 value = (program[i] >> 8) & 0xff; in stmpe_24xx_pwm_config()
234 ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value); in stmpe_24xx_pwm_config()
236 dev_err(chip->dev, "error writing register %02x: %d\n", in stmpe_24xx_pwm_config()
241 value = program[i] & 0xff; in stmpe_24xx_pwm_config()
243 ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value); in stmpe_24xx_pwm_config()
245 dev_err(chip->dev, "error writing register %02x: %d\n", in stmpe_24xx_pwm_config()
251 /* If we were enabled, re-enable this PWM */ in stmpe_24xx_pwm_config()
252 if (pwm_is_enabled(pwm)) in stmpe_24xx_pwm_config()
253 stmpe_24xx_pwm_enable(chip, pwm); in stmpe_24xx_pwm_config()
258 dev_dbg(chip->dev, "programmed PWM#%u, %u bytes\n", pwm->hwpwm, i); in stmpe_24xx_pwm_config()
260 return 0; in stmpe_24xx_pwm_config()
263 static int stmpe_24xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in stmpe_24xx_pwm_apply() argument
268 if (state->polarity != PWM_POLARITY_NORMAL) in stmpe_24xx_pwm_apply()
269 return -EINVAL; in stmpe_24xx_pwm_apply()
271 if (!state->enabled) { in stmpe_24xx_pwm_apply()
272 if (pwm->state.enabled) in stmpe_24xx_pwm_apply()
273 return stmpe_24xx_pwm_disable(chip, pwm); in stmpe_24xx_pwm_apply()
275 return 0; in stmpe_24xx_pwm_apply()
278 err = stmpe_24xx_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period); in stmpe_24xx_pwm_apply()
282 if (!pwm->state.enabled) in stmpe_24xx_pwm_apply()
283 err = stmpe_24xx_pwm_enable(chip, pwm); in stmpe_24xx_pwm_apply()
295 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent); in stmpe_pwm_probe()
299 stmpe_pwm = devm_kzalloc(&pdev->dev, sizeof(*stmpe_pwm), GFP_KERNEL); in stmpe_pwm_probe()
301 return -ENOMEM; in stmpe_pwm_probe()
303 stmpe_pwm->stmpe = stmpe; in stmpe_pwm_probe()
304 stmpe_pwm->chip.dev = &pdev->dev; in stmpe_pwm_probe()
306 if (stmpe->partnum == STMPE2401 || stmpe->partnum == STMPE2403) { in stmpe_pwm_probe()
307 stmpe_pwm->chip.ops = &stmpe_24xx_pwm_ops; in stmpe_pwm_probe()
308 stmpe_pwm->chip.npwm = 3; in stmpe_pwm_probe()
310 if (stmpe->partnum == STMPE1601) in stmpe_pwm_probe()
311 dev_err(&pdev->dev, "STMPE1601 not yet supported\n"); in stmpe_pwm_probe()
313 dev_err(&pdev->dev, "Unknown STMPE PWM\n"); in stmpe_pwm_probe()
315 return -ENODEV; in stmpe_pwm_probe()
322 ret = pwmchip_add(&stmpe_pwm->chip); in stmpe_pwm_probe()
328 return 0; in stmpe_pwm_probe()
333 .name = "stmpe-pwm",