Lines Matching +full:s3c6400 +full:- +full:pwm

1 // SPDX-License-Identifier: GPL-2.0-only
5 * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
9 * PWM driver for Samsung SoCs
21 #include <linux/pwm.h>
59 * struct samsung_pwm_channel - private data of PWM channel
71 * struct samsung_pwm_chip - private data of PWM chip
72 * @chip: generic PWM chip
74 * @inverter_mask: inverter status for all channels - one bit per channel
75 * @disabled_mask: disabled status for all channels - one bit per channel
76 * @base: base address of mapped PWM registers
95 * PWM block is shared between pwm-samsung and samsung_pwm_timer drivers
101 * because all the supported SoCs contain only one instance of the PWM
120 static void pwm_samsung_set_divisor(struct samsung_pwm_chip *pwm, in pwm_samsung_set_divisor() argument
128 bits = (fls(divisor) - 1) - pwm->variant.div_base; in pwm_samsung_set_divisor()
132 reg = readl(pwm->base + REG_TCFG1); in pwm_samsung_set_divisor()
135 writel(reg, pwm->base + REG_TCFG1); in pwm_samsung_set_divisor()
142 struct samsung_pwm_variant *variant = &chip->variant; in pwm_samsung_is_tdiv()
145 reg = readl(chip->base + REG_TCFG1); in pwm_samsung_is_tdiv()
149 return (BIT(reg) & variant->tclk_mask) == 0; in pwm_samsung_is_tdiv()
158 rate = clk_get_rate(chip->base_clk); in pwm_samsung_get_tin_rate()
160 reg = readl(chip->base + REG_TCFG0); in pwm_samsung_get_tin_rate()
171 struct samsung_pwm_variant *variant = &chip->variant; in pwm_samsung_calc_tin()
177 clk = (chan < 2) ? chip->tclk0 : chip->tclk1; in pwm_samsung_calc_tin()
184 dev_warn(chip->chip.dev, in pwm_samsung_calc_tin()
185 "tclk of PWM %d is inoperational, using tdiv\n", chan); in pwm_samsung_calc_tin()
189 dev_dbg(chip->chip.dev, "tin parent at %lu\n", rate); in pwm_samsung_calc_tin()
192 * Compare minimum PWM frequency that can be achieved with possible in pwm_samsung_calc_tin()
196 if (variant->bits < 32) { in pwm_samsung_calc_tin()
198 for (div = variant->div_base; div < 4; ++div) in pwm_samsung_calc_tin()
199 if ((rate >> (variant->bits + div)) < freq) in pwm_samsung_calc_tin()
206 div = variant->div_base; in pwm_samsung_calc_tin()
214 static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm) in pwm_samsung_request() argument
219 if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) { in pwm_samsung_request()
220 dev_warn(chip->dev, in pwm_samsung_request()
221 "tried to request PWM channel %d without output\n", in pwm_samsung_request()
222 pwm->hwpwm); in pwm_samsung_request()
223 return -EINVAL; in pwm_samsung_request()
228 return -ENOMEM; in pwm_samsung_request()
230 pwm_set_chip_data(pwm, our_chan); in pwm_samsung_request()
235 static void pwm_samsung_free(struct pwm_chip *chip, struct pwm_device *pwm) in pwm_samsung_free() argument
237 kfree(pwm_get_chip_data(pwm)); in pwm_samsung_free()
240 static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm) in pwm_samsung_enable() argument
243 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm); in pwm_samsung_enable()
249 tcon = readl(our_chip->base + REG_TCON); in pwm_samsung_enable()
253 writel(tcon, our_chip->base + REG_TCON); in pwm_samsung_enable()
257 writel(tcon, our_chip->base + REG_TCON); in pwm_samsung_enable()
259 our_chip->disabled_mask &= ~BIT(pwm->hwpwm); in pwm_samsung_enable()
266 static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm) in pwm_samsung_disable() argument
269 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm); in pwm_samsung_disable()
275 tcon = readl(our_chip->base + REG_TCON); in pwm_samsung_disable()
277 writel(tcon, our_chip->base + REG_TCON); in pwm_samsung_disable()
279 our_chip->disabled_mask |= BIT(pwm->hwpwm); in pwm_samsung_disable()
285 struct pwm_device *pwm) in pwm_samsung_manual_update() argument
287 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm); in pwm_samsung_manual_update()
293 tcon = readl(chip->base + REG_TCON); in pwm_samsung_manual_update()
295 writel(tcon, chip->base + REG_TCON); in pwm_samsung_manual_update()
298 writel(tcon, chip->base + REG_TCON); in pwm_samsung_manual_update()
303 static int __pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm, in __pwm_samsung_config() argument
307 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm); in __pwm_samsung_config()
308 u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp; in __pwm_samsung_config()
316 return -ERANGE; in __pwm_samsung_config()
318 tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm)); in __pwm_samsung_config()
319 oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm)); in __pwm_samsung_config()
324 /* Check to see if we are changing the clock rate of the PWM. */ in __pwm_samsung_config()
325 if (chan->period_ns != period_ns || force_period) { in __pwm_samsung_config()
331 dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n", in __pwm_samsung_config()
334 tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period); in __pwm_samsung_config()
336 dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate); in __pwm_samsung_config()
344 return -ERANGE; in __pwm_samsung_config()
353 tcmp = tcnt - tcmp; in __pwm_samsung_config()
356 --tcnt; in __pwm_samsung_config()
357 /* -1UL will give 100% duty. */ in __pwm_samsung_config()
358 --tcmp; in __pwm_samsung_config()
360 dev_dbg(our_chip->chip.dev, in __pwm_samsung_config()
363 /* Update PWM registers. */ in __pwm_samsung_config()
364 writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm)); in __pwm_samsung_config()
365 writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm)); in __pwm_samsung_config()
368 * In case the PWM is currently at 100% duty cycle, force a manual in __pwm_samsung_config()
369 * update to prevent the signal staying high if the PWM is disabled in __pwm_samsung_config()
372 if (oldtcmp == (u32) -1) { in __pwm_samsung_config()
373 dev_dbg(our_chip->chip.dev, "Forcing manual update"); in __pwm_samsung_config()
374 pwm_samsung_manual_update(our_chip, pwm); in __pwm_samsung_config()
377 chan->period_ns = period_ns; in __pwm_samsung_config()
378 chan->tin_ns = tin_ns; in __pwm_samsung_config()
379 chan->duty_ns = duty_ns; in __pwm_samsung_config()
384 static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm, in pwm_samsung_config() argument
387 return __pwm_samsung_config(chip, pwm, duty_ns, period_ns, false); in pwm_samsung_config()
399 tcon = readl(chip->base + REG_TCON); in pwm_samsung_set_invert()
402 chip->inverter_mask |= BIT(channel); in pwm_samsung_set_invert()
405 chip->inverter_mask &= ~BIT(channel); in pwm_samsung_set_invert()
409 writel(tcon, chip->base + REG_TCON); in pwm_samsung_set_invert()
415 struct pwm_device *pwm, in pwm_samsung_set_polarity() argument
422 pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert); in pwm_samsung_set_polarity()
467 { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
468 { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
469 { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
470 { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
471 { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
478 struct device_node *np = chip->chip.dev->of_node; in pwm_samsung_parse_dt()
486 return -ENODEV; in pwm_samsung_parse_dt()
488 memcpy(&chip->variant, match->data, sizeof(chip->variant)); in pwm_samsung_parse_dt()
490 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) { in pwm_samsung_parse_dt()
492 dev_err(chip->chip.dev, in pwm_samsung_parse_dt()
493 "%s: invalid channel index in samsung,pwm-outputs property\n", in pwm_samsung_parse_dt()
497 chip->variant.output_mask |= BIT(val); in pwm_samsung_parse_dt()
505 return -ENODEV; in pwm_samsung_parse_dt()
511 struct device *dev = &pdev->dev; in pwm_samsung_probe()
517 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); in pwm_samsung_probe()
519 return -ENOMEM; in pwm_samsung_probe()
521 chip->chip.dev = &pdev->dev; in pwm_samsung_probe()
522 chip->chip.ops = &pwm_samsung_ops; in pwm_samsung_probe()
523 chip->chip.base = -1; in pwm_samsung_probe()
524 chip->chip.npwm = SAMSUNG_PWM_NUM; in pwm_samsung_probe()
525 chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1; in pwm_samsung_probe()
527 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) { in pwm_samsung_probe()
532 chip->chip.of_xlate = of_pwm_xlate_with_flags; in pwm_samsung_probe()
533 chip->chip.of_pwm_n_cells = 3; in pwm_samsung_probe()
535 if (!pdev->dev.platform_data) { in pwm_samsung_probe()
536 dev_err(&pdev->dev, "no platform data specified\n"); in pwm_samsung_probe()
537 return -EINVAL; in pwm_samsung_probe()
540 memcpy(&chip->variant, pdev->dev.platform_data, in pwm_samsung_probe()
541 sizeof(chip->variant)); in pwm_samsung_probe()
545 chip->base = devm_ioremap_resource(&pdev->dev, res); in pwm_samsung_probe()
546 if (IS_ERR(chip->base)) in pwm_samsung_probe()
547 return PTR_ERR(chip->base); in pwm_samsung_probe()
549 chip->base_clk = devm_clk_get(&pdev->dev, "timers"); in pwm_samsung_probe()
550 if (IS_ERR(chip->base_clk)) { in pwm_samsung_probe()
552 return PTR_ERR(chip->base_clk); in pwm_samsung_probe()
555 ret = clk_prepare_enable(chip->base_clk); in pwm_samsung_probe()
562 if (chip->variant.output_mask & BIT(chan)) in pwm_samsung_probe()
566 chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0"); in pwm_samsung_probe()
567 chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1"); in pwm_samsung_probe()
571 ret = pwmchip_add(&chip->chip); in pwm_samsung_probe()
573 dev_err(dev, "failed to register PWM chip\n"); in pwm_samsung_probe()
574 clk_disable_unprepare(chip->base_clk); in pwm_samsung_probe()
579 clk_get_rate(chip->base_clk), in pwm_samsung_probe()
580 !IS_ERR(chip->tclk0) ? clk_get_rate(chip->tclk0) : 0, in pwm_samsung_probe()
581 !IS_ERR(chip->tclk1) ? clk_get_rate(chip->tclk1) : 0); in pwm_samsung_probe()
591 ret = pwmchip_remove(&chip->chip); in pwm_samsung_remove()
595 clk_disable_unprepare(chip->base_clk); in pwm_samsung_remove()
604 struct pwm_chip *chip = &our_chip->chip; in pwm_samsung_resume()
608 struct pwm_device *pwm = &chip->pwms[i]; in pwm_samsung_resume() local
609 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm); in pwm_samsung_resume()
614 if (our_chip->variant.output_mask & BIT(i)) in pwm_samsung_resume()
616 our_chip->inverter_mask & BIT(i)); in pwm_samsung_resume()
618 if (chan->period_ns) { in pwm_samsung_resume()
619 __pwm_samsung_config(chip, pwm, chan->duty_ns, in pwm_samsung_resume()
620 chan->period_ns, true); in pwm_samsung_resume()
621 /* needed to make PWM disable work on Odroid-XU3 */ in pwm_samsung_resume()
622 pwm_samsung_manual_update(our_chip, pwm); in pwm_samsung_resume()
625 if (our_chip->disabled_mask & BIT(i)) in pwm_samsung_resume()
626 pwm_samsung_disable(chip, pwm); in pwm_samsung_resume()
628 pwm_samsung_enable(chip, pwm); in pwm_samsung_resume()
639 .name = "samsung-pwm",
650 MODULE_ALIAS("platform:samsung-pwm");