1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007 Ben Dooks
4 * Copyright (c) 2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
6 * Copyright (c) 2013 Tomasz Figa <tomasz.figa@gmail.com>
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 * @variant: local copy of hardware variant data
73 * @inverter_mask: inverter status for all channels - one bit per channel
74 * @disabled_mask: disabled status for all channels - one bit per channel
75 * @base: base address of mapped PWM registers
76 * @base_clk: base clock used to drive the timers
77 * @tclk0: external clock 0 (can be ERR_PTR if not present)
78 * @tclk1: external clock 1 (can be ERR_PTR if not present)
79 * @channel: per channel driver data
81 struct samsung_pwm_chip
{
82 struct samsung_pwm_variant variant
;
90 struct samsung_pwm_channel channel
[SAMSUNG_PWM_NUM
];
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 pwmchip_get_drvdata(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
*our_chip
,
121 struct pwm_device
*pwm
)
123 unsigned int tcon_chan
= to_tcon_channel(pwm
->hwpwm
);
126 tcon
= readl(our_chip
->base
+ REG_TCON
);
127 tcon
|= TCON_MANUALUPDATE(tcon_chan
);
128 writel(tcon
, our_chip
->base
+ REG_TCON
);
130 tcon
&= ~TCON_MANUALUPDATE(tcon_chan
);
131 writel(tcon
, our_chip
->base
+ REG_TCON
);
134 static void pwm_samsung_set_divisor(struct samsung_pwm_chip
*our_chip
,
135 unsigned int channel
, u8 divisor
)
137 u8 shift
= TCFG1_SHIFT(channel
);
142 bits
= (fls(divisor
) - 1) - our_chip
->variant
.div_base
;
144 spin_lock_irqsave(&samsung_pwm_lock
, flags
);
146 reg
= readl(our_chip
->base
+ REG_TCFG1
);
147 reg
&= ~(TCFG1_MUX_MASK
<< shift
);
148 reg
|= bits
<< shift
;
149 writel(reg
, our_chip
->base
+ REG_TCFG1
);
151 spin_unlock_irqrestore(&samsung_pwm_lock
, flags
);
154 static int pwm_samsung_is_tdiv(struct samsung_pwm_chip
*our_chip
, unsigned int chan
)
156 struct samsung_pwm_variant
*variant
= &our_chip
->variant
;
159 reg
= readl(our_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
*our_chip
,
172 rate
= clk_get_rate(our_chip
->base_clk
);
174 reg
= readl(our_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 pwm_chip
*chip
,
183 unsigned int chan
, unsigned long freq
)
185 struct samsung_pwm_chip
*our_chip
= to_samsung_pwm_chip(chip
);
186 struct samsung_pwm_variant
*variant
= &our_chip
->variant
;
191 if (!pwm_samsung_is_tdiv(our_chip
, chan
)) {
192 clk
= (chan
< 2) ? our_chip
->tclk0
: our_chip
->tclk1
;
194 rate
= clk_get_rate(clk
);
199 dev_warn(pwmchip_parent(chip
),
200 "tclk of PWM %d is inoperational, using tdiv\n", chan
);
203 rate
= pwm_samsung_get_tin_rate(our_chip
, chan
);
204 dev_dbg(pwmchip_parent(chip
), "tin parent at %lu\n", rate
);
207 * Compare minimum PWM frequency that can be achieved with possible
208 * divider settings and choose the lowest divisor that can generate
209 * frequencies lower than requested.
211 if (variant
->bits
< 32) {
212 /* Only for s3c24xx */
213 for (div
= variant
->div_base
; div
< 4; ++div
)
214 if ((rate
>> (variant
->bits
+ div
)) < freq
)
218 * Other variants have enough counter bits to generate any
219 * requested rate, so no need to check higher divisors.
221 div
= variant
->div_base
;
224 pwm_samsung_set_divisor(our_chip
, chan
, BIT(div
));
229 static int pwm_samsung_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
231 struct samsung_pwm_chip
*our_chip
= to_samsung_pwm_chip(chip
);
233 if (!(our_chip
->variant
.output_mask
& BIT(pwm
->hwpwm
))) {
234 dev_warn(pwmchip_parent(chip
),
235 "tried to request PWM channel %d without output\n",
240 memset(&our_chip
->channel
[pwm
->hwpwm
], 0, sizeof(our_chip
->channel
[pwm
->hwpwm
]));
245 static int pwm_samsung_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
247 struct samsung_pwm_chip
*our_chip
= to_samsung_pwm_chip(chip
);
248 unsigned int tcon_chan
= to_tcon_channel(pwm
->hwpwm
);
252 spin_lock_irqsave(&samsung_pwm_lock
, flags
);
254 tcon
= readl(our_chip
->base
+ REG_TCON
);
256 tcon
&= ~TCON_START(tcon_chan
);
257 tcon
|= TCON_MANUALUPDATE(tcon_chan
);
258 writel(tcon
, our_chip
->base
+ REG_TCON
);
260 tcon
&= ~TCON_MANUALUPDATE(tcon_chan
);
261 tcon
|= TCON_START(tcon_chan
) | TCON_AUTORELOAD(tcon_chan
);
262 writel(tcon
, our_chip
->base
+ REG_TCON
);
264 our_chip
->disabled_mask
&= ~BIT(pwm
->hwpwm
);
266 spin_unlock_irqrestore(&samsung_pwm_lock
, flags
);
271 static void pwm_samsung_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
273 struct samsung_pwm_chip
*our_chip
= to_samsung_pwm_chip(chip
);
274 unsigned int tcon_chan
= to_tcon_channel(pwm
->hwpwm
);
278 spin_lock_irqsave(&samsung_pwm_lock
, flags
);
280 tcon
= readl(our_chip
->base
+ REG_TCON
);
281 tcon
&= ~TCON_AUTORELOAD(tcon_chan
);
282 writel(tcon
, our_chip
->base
+ REG_TCON
);
285 * In case the PWM is at 100% duty cycle, force a manual
286 * update to prevent the signal from staying high.
288 if (readl(our_chip
->base
+ REG_TCMPB(pwm
->hwpwm
)) == (u32
)-1U)
289 __pwm_samsung_manual_update(our_chip
, pwm
);
291 our_chip
->disabled_mask
|= BIT(pwm
->hwpwm
);
293 spin_unlock_irqrestore(&samsung_pwm_lock
, flags
);
296 static void pwm_samsung_manual_update(struct samsung_pwm_chip
*our_chip
,
297 struct pwm_device
*pwm
)
301 spin_lock_irqsave(&samsung_pwm_lock
, flags
);
303 __pwm_samsung_manual_update(our_chip
, pwm
);
305 spin_unlock_irqrestore(&samsung_pwm_lock
, flags
);
308 static int __pwm_samsung_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
309 int duty_ns
, int period_ns
, bool force_period
)
311 struct samsung_pwm_chip
*our_chip
= to_samsung_pwm_chip(chip
);
312 struct samsung_pwm_channel
*chan
= &our_chip
->channel
[pwm
->hwpwm
];
313 u32 tin_ns
= chan
->tin_ns
, tcnt
, tcmp
, oldtcmp
;
315 tcnt
= readl(our_chip
->base
+ REG_TCNTB(pwm
->hwpwm
));
316 oldtcmp
= readl(our_chip
->base
+ REG_TCMPB(pwm
->hwpwm
));
318 /* We need tick count for calculation, not last tick. */
321 /* Check to see if we are changing the clock rate of the PWM. */
322 if (chan
->period_ns
!= period_ns
|| force_period
) {
323 unsigned long tin_rate
;
326 period
= NSEC_PER_SEC
/ period_ns
;
328 dev_dbg(pwmchip_parent(chip
), "duty_ns=%d, period_ns=%d (%u)\n",
329 duty_ns
, period_ns
, period
);
331 tin_rate
= pwm_samsung_calc_tin(chip
, pwm
->hwpwm
, period
);
333 dev_dbg(pwmchip_parent(chip
), "tin_rate=%lu\n", tin_rate
);
335 tin_ns
= NSEC_PER_SEC
/ tin_rate
;
336 tcnt
= period_ns
/ tin_ns
;
339 /* Period is too short. */
343 /* Note that counters count down. */
344 tcmp
= duty_ns
/ tin_ns
;
346 /* 0% duty is not available */
352 /* Decrement to get tick numbers, instead of tick counts. */
354 /* -1UL will give 100% duty. */
357 dev_dbg(pwmchip_parent(chip
), "tin_ns=%u, tcmp=%u/%u\n", tin_ns
, tcmp
, tcnt
);
359 /* Update PWM registers. */
360 writel(tcnt
, our_chip
->base
+ REG_TCNTB(pwm
->hwpwm
));
361 writel(tcmp
, our_chip
->base
+ REG_TCMPB(pwm
->hwpwm
));
364 * In case the PWM is currently at 100% duty cycle, force a manual
365 * update to prevent the signal staying high if the PWM is disabled
366 * shortly afer this update (before it autoreloaded the new values).
368 if (oldtcmp
== (u32
) -1) {
369 dev_dbg(pwmchip_parent(chip
), "Forcing manual update");
370 pwm_samsung_manual_update(our_chip
, pwm
);
373 chan
->period_ns
= period_ns
;
374 chan
->tin_ns
= tin_ns
;
375 chan
->duty_ns
= duty_ns
;
380 static int pwm_samsung_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
381 int duty_ns
, int period_ns
)
383 return __pwm_samsung_config(chip
, pwm
, duty_ns
, period_ns
, false);
386 static void pwm_samsung_set_invert(struct samsung_pwm_chip
*our_chip
,
387 unsigned int channel
, bool invert
)
389 unsigned int tcon_chan
= to_tcon_channel(channel
);
393 spin_lock_irqsave(&samsung_pwm_lock
, flags
);
395 tcon
= readl(our_chip
->base
+ REG_TCON
);
398 our_chip
->inverter_mask
|= BIT(channel
);
399 tcon
|= TCON_INVERT(tcon_chan
);
401 our_chip
->inverter_mask
&= ~BIT(channel
);
402 tcon
&= ~TCON_INVERT(tcon_chan
);
405 writel(tcon
, our_chip
->base
+ REG_TCON
);
407 spin_unlock_irqrestore(&samsung_pwm_lock
, flags
);
410 static int pwm_samsung_set_polarity(struct pwm_chip
*chip
,
411 struct pwm_device
*pwm
,
412 enum pwm_polarity polarity
)
414 struct samsung_pwm_chip
*our_chip
= to_samsung_pwm_chip(chip
);
415 bool invert
= (polarity
== PWM_POLARITY_NORMAL
);
417 /* Inverted means normal in the hardware. */
418 pwm_samsung_set_invert(our_chip
, pwm
->hwpwm
, invert
);
423 static int pwm_samsung_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
424 const struct pwm_state
*state
)
426 int err
, enabled
= pwm
->state
.enabled
;
428 if (state
->polarity
!= pwm
->state
.polarity
) {
430 pwm_samsung_disable(chip
, pwm
);
434 err
= pwm_samsung_set_polarity(chip
, pwm
, state
->polarity
);
439 if (!state
->enabled
) {
441 pwm_samsung_disable(chip
, pwm
);
447 * We currently avoid using 64bit arithmetic by using the
448 * fact that anything faster than 1Hz is easily representable
451 if (state
->period
> NSEC_PER_SEC
)
454 err
= pwm_samsung_config(chip
, pwm
, state
->duty_cycle
, state
->period
);
458 if (!pwm
->state
.enabled
)
459 err
= pwm_samsung_enable(chip
, pwm
);
464 static const struct pwm_ops pwm_samsung_ops
= {
465 .request
= pwm_samsung_request
,
466 .apply
= pwm_samsung_apply
,
470 static const struct samsung_pwm_variant s3c24xx_variant
= {
473 .has_tint_cstat
= false,
477 static const struct samsung_pwm_variant s3c64xx_variant
= {
480 .has_tint_cstat
= true,
481 .tclk_mask
= BIT(7) | BIT(6) | BIT(5),
484 static const struct samsung_pwm_variant s5p64x0_variant
= {
487 .has_tint_cstat
= true,
491 static const struct samsung_pwm_variant s5pc100_variant
= {
494 .has_tint_cstat
= true,
498 static const struct of_device_id samsung_pwm_matches
[] = {
499 { .compatible
= "samsung,s3c2410-pwm", .data
= &s3c24xx_variant
},
500 { .compatible
= "samsung,s3c6400-pwm", .data
= &s3c64xx_variant
},
501 { .compatible
= "samsung,s5p6440-pwm", .data
= &s5p64x0_variant
},
502 { .compatible
= "samsung,s5pc100-pwm", .data
= &s5pc100_variant
},
503 { .compatible
= "samsung,exynos4210-pwm", .data
= &s5p64x0_variant
},
506 MODULE_DEVICE_TABLE(of
, samsung_pwm_matches
);
508 static int pwm_samsung_parse_dt(struct pwm_chip
*chip
)
510 struct samsung_pwm_chip
*our_chip
= to_samsung_pwm_chip(chip
);
511 struct device_node
*np
= pwmchip_parent(chip
)->of_node
;
512 const struct of_device_id
*match
;
515 match
= of_match_node(samsung_pwm_matches
, np
);
519 memcpy(&our_chip
->variant
, match
->data
, sizeof(our_chip
->variant
));
521 of_property_for_each_u32(np
, "samsung,pwm-outputs", val
) {
522 if (val
>= SAMSUNG_PWM_NUM
) {
523 dev_err(pwmchip_parent(chip
),
524 "%s: invalid channel index in samsung,pwm-outputs property\n",
528 our_chip
->variant
.output_mask
|= BIT(val
);
534 static int pwm_samsung_parse_dt(struct pwm_chip
*chip
)
540 static int pwm_samsung_probe(struct platform_device
*pdev
)
542 struct device
*dev
= &pdev
->dev
;
543 struct samsung_pwm_chip
*our_chip
;
544 struct pwm_chip
*chip
;
548 chip
= devm_pwmchip_alloc(&pdev
->dev
, SAMSUNG_PWM_NUM
, sizeof(*our_chip
));
550 return PTR_ERR(chip
);
551 our_chip
= to_samsung_pwm_chip(chip
);
553 chip
->ops
= &pwm_samsung_ops
;
554 our_chip
->inverter_mask
= BIT(SAMSUNG_PWM_NUM
) - 1;
556 if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
557 ret
= pwm_samsung_parse_dt(chip
);
561 if (!pdev
->dev
.platform_data
)
562 return dev_err_probe(&pdev
->dev
, -EINVAL
,
563 "no platform data specified\n");
565 memcpy(&our_chip
->variant
, pdev
->dev
.platform_data
,
566 sizeof(our_chip
->variant
));
569 our_chip
->base
= devm_platform_ioremap_resource(pdev
, 0);
570 if (IS_ERR(our_chip
->base
))
571 return PTR_ERR(our_chip
->base
);
573 our_chip
->base_clk
= devm_clk_get_enabled(&pdev
->dev
, "timers");
574 if (IS_ERR(our_chip
->base_clk
))
575 return dev_err_probe(dev
, PTR_ERR(our_chip
->base_clk
),
576 "failed to get timer base clk\n");
578 for (chan
= 0; chan
< SAMSUNG_PWM_NUM
; ++chan
)
579 if (our_chip
->variant
.output_mask
& BIT(chan
))
580 pwm_samsung_set_invert(our_chip
, chan
, true);
582 /* Following clocks are optional. */
583 our_chip
->tclk0
= devm_clk_get(&pdev
->dev
, "pwm-tclk0");
584 our_chip
->tclk1
= devm_clk_get(&pdev
->dev
, "pwm-tclk1");
586 platform_set_drvdata(pdev
, chip
);
588 ret
= devm_pwmchip_add(&pdev
->dev
, chip
);
590 return dev_err_probe(dev
, ret
, "failed to register PWM chip\n");
592 dev_dbg(dev
, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
593 clk_get_rate(our_chip
->base_clk
),
594 !IS_ERR(our_chip
->tclk0
) ? clk_get_rate(our_chip
->tclk0
) : 0,
595 !IS_ERR(our_chip
->tclk1
) ? clk_get_rate(our_chip
->tclk1
) : 0);
600 static int pwm_samsung_resume(struct device
*dev
)
602 struct pwm_chip
*chip
= dev_get_drvdata(dev
);
603 struct samsung_pwm_chip
*our_chip
= to_samsung_pwm_chip(chip
);
606 for (i
= 0; i
< SAMSUNG_PWM_NUM
; i
++) {
607 struct pwm_device
*pwm
= &chip
->pwms
[i
];
608 struct samsung_pwm_channel
*chan
= &our_chip
->channel
[i
];
610 if (!test_bit(PWMF_REQUESTED
, &pwm
->flags
))
613 if (our_chip
->variant
.output_mask
& BIT(i
))
614 pwm_samsung_set_invert(our_chip
, i
,
615 our_chip
->inverter_mask
& BIT(i
));
617 if (chan
->period_ns
) {
618 __pwm_samsung_config(chip
, pwm
, chan
->duty_ns
,
619 chan
->period_ns
, true);
620 /* needed to make PWM disable work on Odroid-XU3 */
621 pwm_samsung_manual_update(our_chip
, pwm
);
624 if (our_chip
->disabled_mask
& BIT(i
))
625 pwm_samsung_disable(chip
, pwm
);
627 pwm_samsung_enable(chip
, pwm
);
633 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops
, NULL
, pwm_samsung_resume
);
635 static struct platform_driver pwm_samsung_driver
= {
637 .name
= "samsung-pwm",
638 .pm
= pm_ptr(&pwm_samsung_pm_ops
),
639 .of_match_table
= of_match_ptr(samsung_pwm_matches
),
641 .probe
= pwm_samsung_probe
,
643 module_platform_driver(pwm_samsung_driver
);
645 MODULE_DESCRIPTION("Samsung Pulse Width Modulator driver");
646 MODULE_LICENSE("GPL");
647 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
648 MODULE_ALIAS("platform:samsung-pwm");