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)
80 * @channel: per channel driver data
82 struct samsung_pwm_chip {
84 struct samsung_pwm_variant variant;
92 struct samsung_pwm_channel channel[SAMSUNG_PWM_NUM];
95 #ifndef CONFIG_CLKSRC_SAMSUNG_PWM
97 * PWM block is shared between pwm-samsung and samsung_pwm_timer drivers
98 * and some registers need access synchronization. If both drivers are
99 * compiled in, the spinlock is defined in the clocksource driver,
100 * otherwise following definition is used.
102 * Currently we do not need any more complex synchronization method
103 * because all the supported SoCs contain only one instance of the PWM
104 * IP. Should this change, both drivers will need to be modified to
105 * properly synchronize accesses to particular instances.
107 static DEFINE_SPINLOCK(samsung_pwm_lock);
111 struct samsung_pwm_chip *to_samsung_pwm_chip(struct pwm_chip *chip)
113 return container_of(chip, struct samsung_pwm_chip, chip);
116 static inline unsigned int to_tcon_channel(unsigned int channel)
118 /* TCON register has a gap of 4 bits (1 channel) after channel 0 */
119 return (channel == 0) ? 0 : (channel + 1);
122 static void __pwm_samsung_manual_update(struct samsung_pwm_chip *our_chip,
123 struct pwm_device *pwm)
125 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
128 tcon = readl(our_chip->base + REG_TCON);
129 tcon |= TCON_MANUALUPDATE(tcon_chan);
130 writel(tcon, our_chip->base + REG_TCON);
132 tcon &= ~TCON_MANUALUPDATE(tcon_chan);
133 writel(tcon, our_chip->base + REG_TCON);
136 static void pwm_samsung_set_divisor(struct samsung_pwm_chip *our_chip,
137 unsigned int channel, u8 divisor)
139 u8 shift = TCFG1_SHIFT(channel);
144 bits = (fls(divisor) - 1) - our_chip->variant.div_base;
146 spin_lock_irqsave(&samsung_pwm_lock, flags);
148 reg = readl(our_chip->base + REG_TCFG1);
149 reg &= ~(TCFG1_MUX_MASK << shift);
150 reg |= bits << shift;
151 writel(reg, our_chip->base + REG_TCFG1);
153 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
156 static int pwm_samsung_is_tdiv(struct samsung_pwm_chip *our_chip, unsigned int chan)
158 struct samsung_pwm_variant *variant = &our_chip->variant;
161 reg = readl(our_chip->base + REG_TCFG1);
162 reg >>= TCFG1_SHIFT(chan);
163 reg &= TCFG1_MUX_MASK;
165 return (BIT(reg) & variant->tclk_mask) == 0;
168 static unsigned long pwm_samsung_get_tin_rate(struct samsung_pwm_chip *our_chip,
174 rate = clk_get_rate(our_chip->base_clk);
176 reg = readl(our_chip->base + REG_TCFG0);
178 reg >>= TCFG0_PRESCALER1_SHIFT;
179 reg &= TCFG0_PRESCALER_MASK;
181 return rate / (reg + 1);
184 static unsigned long pwm_samsung_calc_tin(struct samsung_pwm_chip *our_chip,
185 unsigned int chan, unsigned long freq)
187 struct samsung_pwm_variant *variant = &our_chip->variant;
192 if (!pwm_samsung_is_tdiv(our_chip, chan)) {
193 clk = (chan < 2) ? our_chip->tclk0 : our_chip->tclk1;
195 rate = clk_get_rate(clk);
200 dev_warn(our_chip->chip.dev,
201 "tclk of PWM %d is inoperational, using tdiv\n", chan);
204 rate = pwm_samsung_get_tin_rate(our_chip, chan);
205 dev_dbg(our_chip->chip.dev, "tin parent at %lu\n", rate);
208 * Compare minimum PWM frequency that can be achieved with possible
209 * divider settings and choose the lowest divisor that can generate
210 * frequencies lower than requested.
212 if (variant->bits < 32) {
213 /* Only for s3c24xx */
214 for (div = variant->div_base; div < 4; ++div)
215 if ((rate >> (variant->bits + div)) < freq)
219 * Other variants have enough counter bits to generate any
220 * requested rate, so no need to check higher divisors.
222 div = variant->div_base;
225 pwm_samsung_set_divisor(our_chip, chan, BIT(div));
230 static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
232 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
234 if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) {
236 "tried to request PWM channel %d without output\n",
241 memset(&our_chip->channel[pwm->hwpwm], 0, sizeof(our_chip->channel[pwm->hwpwm]));
246 static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
248 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
249 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
253 spin_lock_irqsave(&samsung_pwm_lock, flags);
255 tcon = readl(our_chip->base + REG_TCON);
257 tcon &= ~TCON_START(tcon_chan);
258 tcon |= TCON_MANUALUPDATE(tcon_chan);
259 writel(tcon, our_chip->base + REG_TCON);
261 tcon &= ~TCON_MANUALUPDATE(tcon_chan);
262 tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
263 writel(tcon, our_chip->base + REG_TCON);
265 our_chip->disabled_mask &= ~BIT(pwm->hwpwm);
267 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
272 static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
274 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
275 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
279 spin_lock_irqsave(&samsung_pwm_lock, flags);
281 tcon = readl(our_chip->base + REG_TCON);
282 tcon &= ~TCON_AUTORELOAD(tcon_chan);
283 writel(tcon, our_chip->base + REG_TCON);
286 * In case the PWM is at 100% duty cycle, force a manual
287 * update to prevent the signal from staying high.
289 if (readl(our_chip->base + REG_TCMPB(pwm->hwpwm)) == (u32)-1U)
290 __pwm_samsung_manual_update(our_chip, pwm);
292 our_chip->disabled_mask |= BIT(pwm->hwpwm);
294 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
297 static void pwm_samsung_manual_update(struct samsung_pwm_chip *our_chip,
298 struct pwm_device *pwm)
302 spin_lock_irqsave(&samsung_pwm_lock, flags);
304 __pwm_samsung_manual_update(our_chip, pwm);
306 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
309 static int __pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
310 int duty_ns, int period_ns, bool force_period)
312 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
313 struct samsung_pwm_channel *chan = &our_chip->channel[pwm->hwpwm];
314 u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
316 tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
317 oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
319 /* We need tick count for calculation, not last tick. */
322 /* Check to see if we are changing the clock rate of the PWM. */
323 if (chan->period_ns != period_ns || force_period) {
324 unsigned long tin_rate;
327 period = NSEC_PER_SEC / period_ns;
329 dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n",
330 duty_ns, period_ns, period);
332 tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period);
334 dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate);
336 tin_ns = NSEC_PER_SEC / tin_rate;
337 tcnt = period_ns / tin_ns;
340 /* Period is too short. */
344 /* Note that counters count down. */
345 tcmp = duty_ns / tin_ns;
347 /* 0% duty is not available */
353 /* Decrement to get tick numbers, instead of tick counts. */
355 /* -1UL will give 100% duty. */
358 dev_dbg(our_chip->chip.dev,
359 "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
361 /* Update PWM registers. */
362 writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
363 writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
366 * In case the PWM is currently at 100% duty cycle, force a manual
367 * update to prevent the signal staying high if the PWM is disabled
368 * shortly afer this update (before it autoreloaded the new values).
370 if (oldtcmp == (u32) -1) {
371 dev_dbg(our_chip->chip.dev, "Forcing manual update");
372 pwm_samsung_manual_update(our_chip, pwm);
375 chan->period_ns = period_ns;
376 chan->tin_ns = tin_ns;
377 chan->duty_ns = duty_ns;
382 static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
383 int duty_ns, int period_ns)
385 return __pwm_samsung_config(chip, pwm, duty_ns, period_ns, false);
388 static void pwm_samsung_set_invert(struct samsung_pwm_chip *our_chip,
389 unsigned int channel, bool invert)
391 unsigned int tcon_chan = to_tcon_channel(channel);
395 spin_lock_irqsave(&samsung_pwm_lock, flags);
397 tcon = readl(our_chip->base + REG_TCON);
400 our_chip->inverter_mask |= BIT(channel);
401 tcon |= TCON_INVERT(tcon_chan);
403 our_chip->inverter_mask &= ~BIT(channel);
404 tcon &= ~TCON_INVERT(tcon_chan);
407 writel(tcon, our_chip->base + REG_TCON);
409 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
412 static int pwm_samsung_set_polarity(struct pwm_chip *chip,
413 struct pwm_device *pwm,
414 enum pwm_polarity polarity)
416 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
417 bool invert = (polarity == PWM_POLARITY_NORMAL);
419 /* Inverted means normal in the hardware. */
420 pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
425 static int pwm_samsung_apply(struct pwm_chip *chip, struct pwm_device *pwm,
426 const struct pwm_state *state)
428 int err, enabled = pwm->state.enabled;
430 if (state->polarity != pwm->state.polarity) {
432 pwm_samsung_disable(chip, pwm);
436 err = pwm_samsung_set_polarity(chip, pwm, state->polarity);
441 if (!state->enabled) {
443 pwm_samsung_disable(chip, pwm);
449 * We currently avoid using 64bit arithmetic by using the
450 * fact that anything faster than 1Hz is easily representable
453 if (state->period > NSEC_PER_SEC)
456 err = pwm_samsung_config(chip, pwm, state->duty_cycle, state->period);
460 if (!pwm->state.enabled)
461 err = pwm_samsung_enable(chip, pwm);
466 static const struct pwm_ops pwm_samsung_ops = {
467 .request = pwm_samsung_request,
468 .apply = pwm_samsung_apply,
472 static const struct samsung_pwm_variant s3c24xx_variant = {
475 .has_tint_cstat = false,
479 static const struct samsung_pwm_variant s3c64xx_variant = {
482 .has_tint_cstat = true,
483 .tclk_mask = BIT(7) | BIT(6) | BIT(5),
486 static const struct samsung_pwm_variant s5p64x0_variant = {
489 .has_tint_cstat = true,
493 static const struct samsung_pwm_variant s5pc100_variant = {
496 .has_tint_cstat = true,
500 static const struct of_device_id samsung_pwm_matches[] = {
501 { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
502 { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
503 { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
504 { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
505 { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
508 MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
510 static int pwm_samsung_parse_dt(struct samsung_pwm_chip *our_chip)
512 struct device_node *np = our_chip->chip.dev->of_node;
513 const struct of_device_id *match;
514 struct property *prop;
518 match = of_match_node(samsung_pwm_matches, np);
522 memcpy(&our_chip->variant, match->data, sizeof(our_chip->variant));
524 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
525 if (val >= SAMSUNG_PWM_NUM) {
526 dev_err(our_chip->chip.dev,
527 "%s: invalid channel index in samsung,pwm-outputs property\n",
531 our_chip->variant.output_mask |= BIT(val);
537 static int pwm_samsung_parse_dt(struct samsung_pwm_chip *our_chip)
543 static int pwm_samsung_probe(struct platform_device *pdev)
545 struct device *dev = &pdev->dev;
546 struct samsung_pwm_chip *our_chip;
550 our_chip = devm_kzalloc(&pdev->dev, sizeof(*our_chip), GFP_KERNEL);
551 if (our_chip == NULL)
554 our_chip->chip.dev = &pdev->dev;
555 our_chip->chip.ops = &pwm_samsung_ops;
556 our_chip->chip.npwm = SAMSUNG_PWM_NUM;
557 our_chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
559 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
560 ret = pwm_samsung_parse_dt(our_chip);
564 if (!pdev->dev.platform_data) {
565 dev_err(&pdev->dev, "no platform data specified\n");
569 memcpy(&our_chip->variant, pdev->dev.platform_data,
570 sizeof(our_chip->variant));
573 our_chip->base = devm_platform_ioremap_resource(pdev, 0);
574 if (IS_ERR(our_chip->base))
575 return PTR_ERR(our_chip->base);
577 our_chip->base_clk = devm_clk_get(&pdev->dev, "timers");
578 if (IS_ERR(our_chip->base_clk)) {
579 dev_err(dev, "failed to get timer base clk\n");
580 return PTR_ERR(our_chip->base_clk);
583 ret = clk_prepare_enable(our_chip->base_clk);
585 dev_err(dev, "failed to enable base clock\n");
589 for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
590 if (our_chip->variant.output_mask & BIT(chan))
591 pwm_samsung_set_invert(our_chip, chan, true);
593 /* Following clocks are optional. */
594 our_chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
595 our_chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
597 platform_set_drvdata(pdev, our_chip);
599 ret = pwmchip_add(&our_chip->chip);
601 dev_err(dev, "failed to register PWM chip\n");
602 clk_disable_unprepare(our_chip->base_clk);
606 dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
607 clk_get_rate(our_chip->base_clk),
608 !IS_ERR(our_chip->tclk0) ? clk_get_rate(our_chip->tclk0) : 0,
609 !IS_ERR(our_chip->tclk1) ? clk_get_rate(our_chip->tclk1) : 0);
614 static void pwm_samsung_remove(struct platform_device *pdev)
616 struct samsung_pwm_chip *our_chip = platform_get_drvdata(pdev);
618 pwmchip_remove(&our_chip->chip);
620 clk_disable_unprepare(our_chip->base_clk);
623 #ifdef CONFIG_PM_SLEEP
624 static int pwm_samsung_resume(struct device *dev)
626 struct samsung_pwm_chip *our_chip = dev_get_drvdata(dev);
627 struct pwm_chip *chip = &our_chip->chip;
630 for (i = 0; i < SAMSUNG_PWM_NUM; i++) {
631 struct pwm_device *pwm = &chip->pwms[i];
632 struct samsung_pwm_channel *chan = &our_chip->channel[i];
634 if (!test_bit(PWMF_REQUESTED, &pwm->flags))
637 if (our_chip->variant.output_mask & BIT(i))
638 pwm_samsung_set_invert(our_chip, i,
639 our_chip->inverter_mask & BIT(i));
641 if (chan->period_ns) {
642 __pwm_samsung_config(chip, pwm, chan->duty_ns,
643 chan->period_ns, true);
644 /* needed to make PWM disable work on Odroid-XU3 */
645 pwm_samsung_manual_update(our_chip, pwm);
648 if (our_chip->disabled_mask & BIT(i))
649 pwm_samsung_disable(chip, pwm);
651 pwm_samsung_enable(chip, pwm);
658 static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume);
660 static struct platform_driver pwm_samsung_driver = {
662 .name = "samsung-pwm",
663 .pm = &pwm_samsung_pm_ops,
664 .of_match_table = of_match_ptr(samsung_pwm_matches),
666 .probe = pwm_samsung_probe,
667 .remove_new = pwm_samsung_remove,
669 module_platform_driver(pwm_samsung_driver);
671 MODULE_LICENSE("GPL");
673 MODULE_ALIAS("platform:samsung-pwm");