1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007 Ben Dooks
4 * Copyright (c) 2008 Simtec Electronics
7 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
9 * PWM driver for Samsung SoCs
12 #include <linux/bitops.h>
13 #include <linux/clk.h>
14 #include <linux/export.h>
15 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pwm.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/time.h>
26 /* For struct samsung_timer_variant and samsung_pwm_lock. */
27 #include <clocksource/samsung_pwm.h>
29 #define REG_TCFG0 0x00
30 #define REG_TCFG1 0x04
33 #define REG_TCNTB(chan) (0x0c + ((chan) * 0xc))
34 #define REG_TCMPB(chan) (0x10 + ((chan) * 0xc))
36 #define TCFG0_PRESCALER_MASK 0xff
37 #define TCFG0_PRESCALER1_SHIFT 8
39 #define TCFG1_MUX_MASK 0xf
40 #define TCFG1_SHIFT(chan) (4 * (chan))
43 * Each channel occupies 4 bits in TCON register, but there is a gap of 4
44 * bits (one channel) after channel 0, so channels have different numbering
45 * when accessing TCON register. See to_tcon_channel() function.
47 * In addition, the location of autoreload bit for channel 4 (TCON channel 5)
48 * in its set of bits is 2 as opposed to 3 for other channels.
50 #define TCON_START(chan) BIT(4 * (chan) + 0)
51 #define TCON_MANUALUPDATE(chan) BIT(4 * (chan) + 1)
52 #define TCON_INVERT(chan) BIT(4 * (chan) + 2)
53 #define _TCON_AUTORELOAD(chan) BIT(4 * (chan) + 3)
54 #define _TCON_AUTORELOAD4(chan) BIT(4 * (chan) + 2)
55 #define TCON_AUTORELOAD(chan) \
56 ((chan < 5) ? _TCON_AUTORELOAD(chan) : _TCON_AUTORELOAD4(chan))
59 * struct samsung_pwm_channel - private data of PWM channel
60 * @period_ns: current period in nanoseconds programmed to the hardware
61 * @duty_ns: current duty time in nanoseconds programmed to the hardware
62 * @tin_ns: time of one timer tick in nanoseconds with current timer rate
64 struct samsung_pwm_channel {
71 * struct samsung_pwm_chip - private data of PWM chip
72 * @chip: generic PWM chip
73 * @variant: local copy of hardware variant data
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
77 * @base_clk: base clock used to drive the timers
78 * @tclk0: external clock 0 (can be ERR_PTR if not present)
79 * @tclk1: external clock 1 (can be ERR_PTR if not present)
81 struct samsung_pwm_chip {
83 struct samsung_pwm_variant variant;
93 #ifndef CONFIG_CLKSRC_SAMSUNG_PWM
95 * PWM block is shared between pwm-samsung and samsung_pwm_timer drivers
96 * and some registers need access synchronization. If both drivers are
97 * compiled in, the spinlock is defined in the clocksource driver,
98 * otherwise following definition is used.
100 * Currently we do not need any more complex synchronization method
101 * because all the supported SoCs contain only one instance of the PWM
102 * IP. Should this change, both drivers will need to be modified to
103 * properly synchronize accesses to particular instances.
105 static DEFINE_SPINLOCK(samsung_pwm_lock);
109 struct samsung_pwm_chip *to_samsung_pwm_chip(struct pwm_chip *chip)
111 return container_of(chip, struct samsung_pwm_chip, chip);
114 static inline unsigned int to_tcon_channel(unsigned int channel)
116 /* TCON register has a gap of 4 bits (1 channel) after channel 0 */
117 return (channel == 0) ? 0 : (channel + 1);
120 static void __pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
121 struct pwm_device *pwm)
123 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
126 tcon = readl(chip->base + REG_TCON);
127 tcon |= TCON_MANUALUPDATE(tcon_chan);
128 writel(tcon, chip->base + REG_TCON);
130 tcon &= ~TCON_MANUALUPDATE(tcon_chan);
131 writel(tcon, chip->base + REG_TCON);
134 static void pwm_samsung_set_divisor(struct samsung_pwm_chip *pwm,
135 unsigned int channel, u8 divisor)
137 u8 shift = TCFG1_SHIFT(channel);
142 bits = (fls(divisor) - 1) - pwm->variant.div_base;
144 spin_lock_irqsave(&samsung_pwm_lock, flags);
146 reg = readl(pwm->base + REG_TCFG1);
147 reg &= ~(TCFG1_MUX_MASK << shift);
148 reg |= bits << shift;
149 writel(reg, pwm->base + REG_TCFG1);
151 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
154 static int pwm_samsung_is_tdiv(struct samsung_pwm_chip *chip, unsigned int chan)
156 struct samsung_pwm_variant *variant = &chip->variant;
159 reg = readl(chip->base + REG_TCFG1);
160 reg >>= TCFG1_SHIFT(chan);
161 reg &= TCFG1_MUX_MASK;
163 return (BIT(reg) & variant->tclk_mask) == 0;
166 static unsigned long pwm_samsung_get_tin_rate(struct samsung_pwm_chip *chip,
172 rate = clk_get_rate(chip->base_clk);
174 reg = readl(chip->base + REG_TCFG0);
176 reg >>= TCFG0_PRESCALER1_SHIFT;
177 reg &= TCFG0_PRESCALER_MASK;
179 return rate / (reg + 1);
182 static unsigned long pwm_samsung_calc_tin(struct samsung_pwm_chip *chip,
183 unsigned int chan, unsigned long freq)
185 struct samsung_pwm_variant *variant = &chip->variant;
190 if (!pwm_samsung_is_tdiv(chip, chan)) {
191 clk = (chan < 2) ? chip->tclk0 : chip->tclk1;
193 rate = clk_get_rate(clk);
198 dev_warn(chip->chip.dev,
199 "tclk of PWM %d is inoperational, using tdiv\n", chan);
202 rate = pwm_samsung_get_tin_rate(chip, chan);
203 dev_dbg(chip->chip.dev, "tin parent at %lu\n", rate);
206 * Compare minimum PWM frequency that can be achieved with possible
207 * divider settings and choose the lowest divisor that can generate
208 * frequencies lower than requested.
210 if (variant->bits < 32) {
211 /* Only for s3c24xx */
212 for (div = variant->div_base; div < 4; ++div)
213 if ((rate >> (variant->bits + div)) < freq)
217 * Other variants have enough counter bits to generate any
218 * requested rate, so no need to check higher divisors.
220 div = variant->div_base;
223 pwm_samsung_set_divisor(chip, chan, BIT(div));
228 static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
230 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
231 struct samsung_pwm_channel *our_chan;
233 if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) {
235 "tried to request PWM channel %d without output\n",
240 our_chan = kzalloc(sizeof(*our_chan), GFP_KERNEL);
244 pwm_set_chip_data(pwm, our_chan);
249 static void pwm_samsung_free(struct pwm_chip *chip, struct pwm_device *pwm)
251 kfree(pwm_get_chip_data(pwm));
254 static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
256 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
257 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
261 spin_lock_irqsave(&samsung_pwm_lock, flags);
263 tcon = readl(our_chip->base + REG_TCON);
265 tcon &= ~TCON_START(tcon_chan);
266 tcon |= TCON_MANUALUPDATE(tcon_chan);
267 writel(tcon, our_chip->base + REG_TCON);
269 tcon &= ~TCON_MANUALUPDATE(tcon_chan);
270 tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
271 writel(tcon, our_chip->base + REG_TCON);
273 our_chip->disabled_mask &= ~BIT(pwm->hwpwm);
275 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
280 static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
282 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
283 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
287 spin_lock_irqsave(&samsung_pwm_lock, flags);
289 tcon = readl(our_chip->base + REG_TCON);
290 tcon &= ~TCON_AUTORELOAD(tcon_chan);
291 writel(tcon, our_chip->base + REG_TCON);
294 * In case the PWM is at 100% duty cycle, force a manual
295 * update to prevent the signal from staying high.
297 if (readl(our_chip->base + REG_TCMPB(pwm->hwpwm)) == (u32)-1U)
298 __pwm_samsung_manual_update(our_chip, pwm);
300 our_chip->disabled_mask |= BIT(pwm->hwpwm);
302 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
305 static void pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
306 struct pwm_device *pwm)
310 spin_lock_irqsave(&samsung_pwm_lock, flags);
312 __pwm_samsung_manual_update(chip, pwm);
314 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
317 static int __pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
318 int duty_ns, int period_ns, bool force_period)
320 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
321 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
322 u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
325 * We currently avoid using 64bit arithmetic by using the
326 * fact that anything faster than 1Hz is easily representable
329 if (period_ns > NSEC_PER_SEC)
332 tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
333 oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
335 /* We need tick count for calculation, not last tick. */
338 /* Check to see if we are changing the clock rate of the PWM. */
339 if (chan->period_ns != period_ns || force_period) {
340 unsigned long tin_rate;
343 period = NSEC_PER_SEC / period_ns;
345 dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n",
346 duty_ns, period_ns, period);
348 tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period);
350 dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate);
352 tin_ns = NSEC_PER_SEC / tin_rate;
353 tcnt = period_ns / tin_ns;
356 /* Period is too short. */
360 /* Note that counters count down. */
361 tcmp = duty_ns / tin_ns;
363 /* 0% duty is not available */
369 /* Decrement to get tick numbers, instead of tick counts. */
371 /* -1UL will give 100% duty. */
374 dev_dbg(our_chip->chip.dev,
375 "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
377 /* Update PWM registers. */
378 writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
379 writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
382 * In case the PWM is currently at 100% duty cycle, force a manual
383 * update to prevent the signal staying high if the PWM is disabled
384 * shortly afer this update (before it autoreloaded the new values).
386 if (oldtcmp == (u32) -1) {
387 dev_dbg(our_chip->chip.dev, "Forcing manual update");
388 pwm_samsung_manual_update(our_chip, pwm);
391 chan->period_ns = period_ns;
392 chan->tin_ns = tin_ns;
393 chan->duty_ns = duty_ns;
398 static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
399 int duty_ns, int period_ns)
401 return __pwm_samsung_config(chip, pwm, duty_ns, period_ns, false);
404 static void pwm_samsung_set_invert(struct samsung_pwm_chip *chip,
405 unsigned int channel, bool invert)
407 unsigned int tcon_chan = to_tcon_channel(channel);
411 spin_lock_irqsave(&samsung_pwm_lock, flags);
413 tcon = readl(chip->base + REG_TCON);
416 chip->inverter_mask |= BIT(channel);
417 tcon |= TCON_INVERT(tcon_chan);
419 chip->inverter_mask &= ~BIT(channel);
420 tcon &= ~TCON_INVERT(tcon_chan);
423 writel(tcon, chip->base + REG_TCON);
425 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
428 static int pwm_samsung_set_polarity(struct pwm_chip *chip,
429 struct pwm_device *pwm,
430 enum pwm_polarity polarity)
432 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
433 bool invert = (polarity == PWM_POLARITY_NORMAL);
435 /* Inverted means normal in the hardware. */
436 pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
441 static const struct pwm_ops pwm_samsung_ops = {
442 .request = pwm_samsung_request,
443 .free = pwm_samsung_free,
444 .enable = pwm_samsung_enable,
445 .disable = pwm_samsung_disable,
446 .config = pwm_samsung_config,
447 .set_polarity = pwm_samsung_set_polarity,
448 .owner = THIS_MODULE,
452 static const struct samsung_pwm_variant s3c24xx_variant = {
455 .has_tint_cstat = false,
459 static const struct samsung_pwm_variant s3c64xx_variant = {
462 .has_tint_cstat = true,
463 .tclk_mask = BIT(7) | BIT(6) | BIT(5),
466 static const struct samsung_pwm_variant s5p64x0_variant = {
469 .has_tint_cstat = true,
473 static const struct samsung_pwm_variant s5pc100_variant = {
476 .has_tint_cstat = true,
480 static const struct of_device_id samsung_pwm_matches[] = {
481 { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
482 { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
483 { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
484 { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
485 { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
488 MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
490 static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
492 struct device_node *np = chip->chip.dev->of_node;
493 const struct of_device_id *match;
494 struct property *prop;
498 match = of_match_node(samsung_pwm_matches, np);
502 memcpy(&chip->variant, match->data, sizeof(chip->variant));
504 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
505 if (val >= SAMSUNG_PWM_NUM) {
506 dev_err(chip->chip.dev,
507 "%s: invalid channel index in samsung,pwm-outputs property\n",
511 chip->variant.output_mask |= BIT(val);
517 static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
523 static int pwm_samsung_probe(struct platform_device *pdev)
525 struct device *dev = &pdev->dev;
526 struct samsung_pwm_chip *chip;
530 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
534 chip->chip.dev = &pdev->dev;
535 chip->chip.ops = &pwm_samsung_ops;
536 chip->chip.npwm = SAMSUNG_PWM_NUM;
537 chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
539 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
540 ret = pwm_samsung_parse_dt(chip);
544 if (!pdev->dev.platform_data) {
545 dev_err(&pdev->dev, "no platform data specified\n");
549 memcpy(&chip->variant, pdev->dev.platform_data,
550 sizeof(chip->variant));
553 chip->base = devm_platform_ioremap_resource(pdev, 0);
554 if (IS_ERR(chip->base))
555 return PTR_ERR(chip->base);
557 chip->base_clk = devm_clk_get(&pdev->dev, "timers");
558 if (IS_ERR(chip->base_clk)) {
559 dev_err(dev, "failed to get timer base clk\n");
560 return PTR_ERR(chip->base_clk);
563 ret = clk_prepare_enable(chip->base_clk);
565 dev_err(dev, "failed to enable base clock\n");
569 for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
570 if (chip->variant.output_mask & BIT(chan))
571 pwm_samsung_set_invert(chip, chan, true);
573 /* Following clocks are optional. */
574 chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
575 chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
577 platform_set_drvdata(pdev, chip);
579 ret = pwmchip_add(&chip->chip);
581 dev_err(dev, "failed to register PWM chip\n");
582 clk_disable_unprepare(chip->base_clk);
586 dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
587 clk_get_rate(chip->base_clk),
588 !IS_ERR(chip->tclk0) ? clk_get_rate(chip->tclk0) : 0,
589 !IS_ERR(chip->tclk1) ? clk_get_rate(chip->tclk1) : 0);
594 static int pwm_samsung_remove(struct platform_device *pdev)
596 struct samsung_pwm_chip *chip = platform_get_drvdata(pdev);
598 pwmchip_remove(&chip->chip);
600 clk_disable_unprepare(chip->base_clk);
605 #ifdef CONFIG_PM_SLEEP
606 static int pwm_samsung_resume(struct device *dev)
608 struct samsung_pwm_chip *our_chip = dev_get_drvdata(dev);
609 struct pwm_chip *chip = &our_chip->chip;
612 for (i = 0; i < SAMSUNG_PWM_NUM; i++) {
613 struct pwm_device *pwm = &chip->pwms[i];
614 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
619 if (our_chip->variant.output_mask & BIT(i))
620 pwm_samsung_set_invert(our_chip, i,
621 our_chip->inverter_mask & BIT(i));
623 if (chan->period_ns) {
624 __pwm_samsung_config(chip, pwm, chan->duty_ns,
625 chan->period_ns, true);
626 /* needed to make PWM disable work on Odroid-XU3 */
627 pwm_samsung_manual_update(our_chip, pwm);
630 if (our_chip->disabled_mask & BIT(i))
631 pwm_samsung_disable(chip, pwm);
633 pwm_samsung_enable(chip, pwm);
640 static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume);
642 static struct platform_driver pwm_samsung_driver = {
644 .name = "samsung-pwm",
645 .pm = &pwm_samsung_pm_ops,
646 .of_match_table = of_match_ptr(samsung_pwm_matches),
648 .probe = pwm_samsung_probe,
649 .remove = pwm_samsung_remove,
651 module_platform_driver(pwm_samsung_driver);
653 MODULE_LICENSE("GPL");
655 MODULE_ALIAS("platform:samsung-pwm");