Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
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.
8 *
9 * PWM driver for Samsung SoCs
10 */
11
12#include <linux/bitops.h>
13#include <linux/clk.h>
14#include <linux/export.h>
15#include <linux/err.h>
16#include <linux/io.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/of.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>
25
26/* For struct samsung_timer_variant and samsung_pwm_lock. */
27#include <clocksource/samsung_pwm.h>
28
29#define REG_TCFG0 0x00
30#define REG_TCFG1 0x04
31#define REG_TCON 0x08
32
33#define REG_TCNTB(chan) (0x0c + ((chan) * 0xc))
34#define REG_TCMPB(chan) (0x10 + ((chan) * 0xc))
35
36#define TCFG0_PRESCALER_MASK 0xff
37#define TCFG0_PRESCALER1_SHIFT 8
38
39#define TCFG1_MUX_MASK 0xf
40#define TCFG1_SHIFT(chan) (4 * (chan))
41
42/*
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.
46 *
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.
49 */
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))
57
58/**
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
63 */
64struct samsung_pwm_channel {
65 u32 period_ns;
66 u32 duty_ns;
67 u32 tin_ns;
68};
69
70/**
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 */
81struct samsung_pwm_chip {
82 struct pwm_chip chip;
83 struct samsung_pwm_variant variant;
84 u8 inverter_mask;
85 u8 disabled_mask;
86
87 void __iomem *base;
88 struct clk *base_clk;
89 struct clk *tclk0;
90 struct clk *tclk1;
91};
92
93#ifndef CONFIG_CLKSRC_SAMSUNG_PWM
94/*
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.
99 *
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.
104 */
105static DEFINE_SPINLOCK(samsung_pwm_lock);
106#endif
107
108static inline
109struct samsung_pwm_chip *to_samsung_pwm_chip(struct pwm_chip *chip)
110{
111 return container_of(chip, struct samsung_pwm_chip, chip);
112}
113
114static inline unsigned int to_tcon_channel(unsigned int channel)
115{
116 /* TCON register has a gap of 4 bits (1 channel) after channel 0 */
117 return (channel == 0) ? 0 : (channel + 1);
118}
119
120static void __pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
121 struct pwm_device *pwm)
122{
123 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
124 u32 tcon;
125
126 tcon = readl(chip->base + REG_TCON);
127 tcon |= TCON_MANUALUPDATE(tcon_chan);
128 writel(tcon, chip->base + REG_TCON);
129
130 tcon &= ~TCON_MANUALUPDATE(tcon_chan);
131 writel(tcon, chip->base + REG_TCON);
132}
133
134static void pwm_samsung_set_divisor(struct samsung_pwm_chip *pwm,
135 unsigned int channel, u8 divisor)
136{
137 u8 shift = TCFG1_SHIFT(channel);
138 unsigned long flags;
139 u32 reg;
140 u8 bits;
141
142 bits = (fls(divisor) - 1) - pwm->variant.div_base;
143
144 spin_lock_irqsave(&samsung_pwm_lock, flags);
145
146 reg = readl(pwm->base + REG_TCFG1);
147 reg &= ~(TCFG1_MUX_MASK << shift);
148 reg |= bits << shift;
149 writel(reg, pwm->base + REG_TCFG1);
150
151 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
152}
153
154static int pwm_samsung_is_tdiv(struct samsung_pwm_chip *chip, unsigned int chan)
155{
156 struct samsung_pwm_variant *variant = &chip->variant;
157 u32 reg;
158
159 reg = readl(chip->base + REG_TCFG1);
160 reg >>= TCFG1_SHIFT(chan);
161 reg &= TCFG1_MUX_MASK;
162
163 return (BIT(reg) & variant->tclk_mask) == 0;
164}
165
166static unsigned long pwm_samsung_get_tin_rate(struct samsung_pwm_chip *chip,
167 unsigned int chan)
168{
169 unsigned long rate;
170 u32 reg;
171
172 rate = clk_get_rate(chip->base_clk);
173
174 reg = readl(chip->base + REG_TCFG0);
175 if (chan >= 2)
176 reg >>= TCFG0_PRESCALER1_SHIFT;
177 reg &= TCFG0_PRESCALER_MASK;
178
179 return rate / (reg + 1);
180}
181
182static unsigned long pwm_samsung_calc_tin(struct samsung_pwm_chip *chip,
183 unsigned int chan, unsigned long freq)
184{
185 struct samsung_pwm_variant *variant = &chip->variant;
186 unsigned long rate;
187 struct clk *clk;
188 u8 div;
189
190 if (!pwm_samsung_is_tdiv(chip, chan)) {
191 clk = (chan < 2) ? chip->tclk0 : chip->tclk1;
192 if (!IS_ERR(clk)) {
193 rate = clk_get_rate(clk);
194 if (rate)
195 return rate;
196 }
197
198 dev_warn(chip->chip.dev,
199 "tclk of PWM %d is inoperational, using tdiv\n", chan);
200 }
201
202 rate = pwm_samsung_get_tin_rate(chip, chan);
203 dev_dbg(chip->chip.dev, "tin parent at %lu\n", rate);
204
205 /*
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.
209 */
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)
214 break;
215 } else {
216 /*
217 * Other variants have enough counter bits to generate any
218 * requested rate, so no need to check higher divisors.
219 */
220 div = variant->div_base;
221 }
222
223 pwm_samsung_set_divisor(chip, chan, BIT(div));
224
225 return rate >> div;
226}
227
228static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
229{
230 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
231 struct samsung_pwm_channel *our_chan;
232
233 if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) {
234 dev_warn(chip->dev,
235 "tried to request PWM channel %d without output\n",
236 pwm->hwpwm);
237 return -EINVAL;
238 }
239
240 our_chan = kzalloc(sizeof(*our_chan), GFP_KERNEL);
241 if (!our_chan)
242 return -ENOMEM;
243
244 pwm_set_chip_data(pwm, our_chan);
245
246 return 0;
247}
248
249static void pwm_samsung_free(struct pwm_chip *chip, struct pwm_device *pwm)
250{
251 kfree(pwm_get_chip_data(pwm));
252}
253
254static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
255{
256 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
257 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
258 unsigned long flags;
259 u32 tcon;
260
261 spin_lock_irqsave(&samsung_pwm_lock, flags);
262
263 tcon = readl(our_chip->base + REG_TCON);
264
265 tcon &= ~TCON_START(tcon_chan);
266 tcon |= TCON_MANUALUPDATE(tcon_chan);
267 writel(tcon, our_chip->base + REG_TCON);
268
269 tcon &= ~TCON_MANUALUPDATE(tcon_chan);
270 tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
271 writel(tcon, our_chip->base + REG_TCON);
272
273 our_chip->disabled_mask &= ~BIT(pwm->hwpwm);
274
275 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
276
277 return 0;
278}
279
280static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
281{
282 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
283 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
284 unsigned long flags;
285 u32 tcon;
286
287 spin_lock_irqsave(&samsung_pwm_lock, flags);
288
289 tcon = readl(our_chip->base + REG_TCON);
290 tcon &= ~TCON_AUTORELOAD(tcon_chan);
291 writel(tcon, our_chip->base + REG_TCON);
292
293 /*
294 * In case the PWM is at 100% duty cycle, force a manual
295 * update to prevent the signal from staying high.
296 */
297 if (readl(our_chip->base + REG_TCMPB(pwm->hwpwm)) == (u32)-1U)
298 __pwm_samsung_manual_update(our_chip, pwm);
299
300 our_chip->disabled_mask |= BIT(pwm->hwpwm);
301
302 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
303}
304
305static void pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
306 struct pwm_device *pwm)
307{
308 unsigned long flags;
309
310 spin_lock_irqsave(&samsung_pwm_lock, flags);
311
312 __pwm_samsung_manual_update(chip, pwm);
313
314 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
315}
316
317static int __pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
318 int duty_ns, int period_ns, bool force_period)
319{
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;
323
324 tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
325 oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
326
327 /* We need tick count for calculation, not last tick. */
328 ++tcnt;
329
330 /* Check to see if we are changing the clock rate of the PWM. */
331 if (chan->period_ns != period_ns || force_period) {
332 unsigned long tin_rate;
333 u32 period;
334
335 period = NSEC_PER_SEC / period_ns;
336
337 dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n",
338 duty_ns, period_ns, period);
339
340 tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period);
341
342 dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate);
343
344 tin_ns = NSEC_PER_SEC / tin_rate;
345 tcnt = period_ns / tin_ns;
346 }
347
348 /* Period is too short. */
349 if (tcnt <= 1)
350 return -ERANGE;
351
352 /* Note that counters count down. */
353 tcmp = duty_ns / tin_ns;
354
355 /* 0% duty is not available */
356 if (!tcmp)
357 ++tcmp;
358
359 tcmp = tcnt - tcmp;
360
361 /* Decrement to get tick numbers, instead of tick counts. */
362 --tcnt;
363 /* -1UL will give 100% duty. */
364 --tcmp;
365
366 dev_dbg(our_chip->chip.dev,
367 "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
368
369 /* Update PWM registers. */
370 writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
371 writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
372
373 /*
374 * In case the PWM is currently at 100% duty cycle, force a manual
375 * update to prevent the signal staying high if the PWM is disabled
376 * shortly afer this update (before it autoreloaded the new values).
377 */
378 if (oldtcmp == (u32) -1) {
379 dev_dbg(our_chip->chip.dev, "Forcing manual update");
380 pwm_samsung_manual_update(our_chip, pwm);
381 }
382
383 chan->period_ns = period_ns;
384 chan->tin_ns = tin_ns;
385 chan->duty_ns = duty_ns;
386
387 return 0;
388}
389
390static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
391 int duty_ns, int period_ns)
392{
393 return __pwm_samsung_config(chip, pwm, duty_ns, period_ns, false);
394}
395
396static void pwm_samsung_set_invert(struct samsung_pwm_chip *chip,
397 unsigned int channel, bool invert)
398{
399 unsigned int tcon_chan = to_tcon_channel(channel);
400 unsigned long flags;
401 u32 tcon;
402
403 spin_lock_irqsave(&samsung_pwm_lock, flags);
404
405 tcon = readl(chip->base + REG_TCON);
406
407 if (invert) {
408 chip->inverter_mask |= BIT(channel);
409 tcon |= TCON_INVERT(tcon_chan);
410 } else {
411 chip->inverter_mask &= ~BIT(channel);
412 tcon &= ~TCON_INVERT(tcon_chan);
413 }
414
415 writel(tcon, chip->base + REG_TCON);
416
417 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
418}
419
420static int pwm_samsung_set_polarity(struct pwm_chip *chip,
421 struct pwm_device *pwm,
422 enum pwm_polarity polarity)
423{
424 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
425 bool invert = (polarity == PWM_POLARITY_NORMAL);
426
427 /* Inverted means normal in the hardware. */
428 pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
429
430 return 0;
431}
432
433static int pwm_samsung_apply(struct pwm_chip *chip, struct pwm_device *pwm,
434 const struct pwm_state *state)
435{
436 int err, enabled = pwm->state.enabled;
437
438 if (state->polarity != pwm->state.polarity) {
439 if (enabled) {
440 pwm_samsung_disable(chip, pwm);
441 enabled = false;
442 }
443
444 err = pwm_samsung_set_polarity(chip, pwm, state->polarity);
445 if (err)
446 return err;
447 }
448
449 if (!state->enabled) {
450 if (enabled)
451 pwm_samsung_disable(chip, pwm);
452
453 return 0;
454 }
455
456 /*
457 * We currently avoid using 64bit arithmetic by using the
458 * fact that anything faster than 1Hz is easily representable
459 * by 32bits.
460 */
461 if (state->period > NSEC_PER_SEC)
462 return -ERANGE;
463
464 err = pwm_samsung_config(chip, pwm, state->duty_cycle, state->period);
465 if (err)
466 return err;
467
468 if (!pwm->state.enabled)
469 err = pwm_samsung_enable(chip, pwm);
470
471 return err;
472}
473
474static const struct pwm_ops pwm_samsung_ops = {
475 .request = pwm_samsung_request,
476 .free = pwm_samsung_free,
477 .apply = pwm_samsung_apply,
478 .owner = THIS_MODULE,
479};
480
481#ifdef CONFIG_OF
482static const struct samsung_pwm_variant s3c24xx_variant = {
483 .bits = 16,
484 .div_base = 1,
485 .has_tint_cstat = false,
486 .tclk_mask = BIT(4),
487};
488
489static const struct samsung_pwm_variant s3c64xx_variant = {
490 .bits = 32,
491 .div_base = 0,
492 .has_tint_cstat = true,
493 .tclk_mask = BIT(7) | BIT(6) | BIT(5),
494};
495
496static const struct samsung_pwm_variant s5p64x0_variant = {
497 .bits = 32,
498 .div_base = 0,
499 .has_tint_cstat = true,
500 .tclk_mask = 0,
501};
502
503static const struct samsung_pwm_variant s5pc100_variant = {
504 .bits = 32,
505 .div_base = 0,
506 .has_tint_cstat = true,
507 .tclk_mask = BIT(5),
508};
509
510static const struct of_device_id samsung_pwm_matches[] = {
511 { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
512 { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
513 { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
514 { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
515 { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
516 {},
517};
518MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
519
520static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
521{
522 struct device_node *np = chip->chip.dev->of_node;
523 const struct of_device_id *match;
524 struct property *prop;
525 const __be32 *cur;
526 u32 val;
527
528 match = of_match_node(samsung_pwm_matches, np);
529 if (!match)
530 return -ENODEV;
531
532 memcpy(&chip->variant, match->data, sizeof(chip->variant));
533
534 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
535 if (val >= SAMSUNG_PWM_NUM) {
536 dev_err(chip->chip.dev,
537 "%s: invalid channel index in samsung,pwm-outputs property\n",
538 __func__);
539 continue;
540 }
541 chip->variant.output_mask |= BIT(val);
542 }
543
544 return 0;
545}
546#else
547static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
548{
549 return -ENODEV;
550}
551#endif
552
553static int pwm_samsung_probe(struct platform_device *pdev)
554{
555 struct device *dev = &pdev->dev;
556 struct samsung_pwm_chip *chip;
557 unsigned int chan;
558 int ret;
559
560 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
561 if (chip == NULL)
562 return -ENOMEM;
563
564 chip->chip.dev = &pdev->dev;
565 chip->chip.ops = &pwm_samsung_ops;
566 chip->chip.npwm = SAMSUNG_PWM_NUM;
567 chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
568
569 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
570 ret = pwm_samsung_parse_dt(chip);
571 if (ret)
572 return ret;
573 } else {
574 if (!pdev->dev.platform_data) {
575 dev_err(&pdev->dev, "no platform data specified\n");
576 return -EINVAL;
577 }
578
579 memcpy(&chip->variant, pdev->dev.platform_data,
580 sizeof(chip->variant));
581 }
582
583 chip->base = devm_platform_ioremap_resource(pdev, 0);
584 if (IS_ERR(chip->base))
585 return PTR_ERR(chip->base);
586
587 chip->base_clk = devm_clk_get(&pdev->dev, "timers");
588 if (IS_ERR(chip->base_clk)) {
589 dev_err(dev, "failed to get timer base clk\n");
590 return PTR_ERR(chip->base_clk);
591 }
592
593 ret = clk_prepare_enable(chip->base_clk);
594 if (ret < 0) {
595 dev_err(dev, "failed to enable base clock\n");
596 return ret;
597 }
598
599 for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
600 if (chip->variant.output_mask & BIT(chan))
601 pwm_samsung_set_invert(chip, chan, true);
602
603 /* Following clocks are optional. */
604 chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
605 chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
606
607 platform_set_drvdata(pdev, chip);
608
609 ret = pwmchip_add(&chip->chip);
610 if (ret < 0) {
611 dev_err(dev, "failed to register PWM chip\n");
612 clk_disable_unprepare(chip->base_clk);
613 return ret;
614 }
615
616 dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
617 clk_get_rate(chip->base_clk),
618 !IS_ERR(chip->tclk0) ? clk_get_rate(chip->tclk0) : 0,
619 !IS_ERR(chip->tclk1) ? clk_get_rate(chip->tclk1) : 0);
620
621 return 0;
622}
623
624static int pwm_samsung_remove(struct platform_device *pdev)
625{
626 struct samsung_pwm_chip *chip = platform_get_drvdata(pdev);
627
628 pwmchip_remove(&chip->chip);
629
630 clk_disable_unprepare(chip->base_clk);
631
632 return 0;
633}
634
635#ifdef CONFIG_PM_SLEEP
636static int pwm_samsung_resume(struct device *dev)
637{
638 struct samsung_pwm_chip *our_chip = dev_get_drvdata(dev);
639 struct pwm_chip *chip = &our_chip->chip;
640 unsigned int i;
641
642 for (i = 0; i < SAMSUNG_PWM_NUM; i++) {
643 struct pwm_device *pwm = &chip->pwms[i];
644 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
645
646 if (!chan)
647 continue;
648
649 if (our_chip->variant.output_mask & BIT(i))
650 pwm_samsung_set_invert(our_chip, i,
651 our_chip->inverter_mask & BIT(i));
652
653 if (chan->period_ns) {
654 __pwm_samsung_config(chip, pwm, chan->duty_ns,
655 chan->period_ns, true);
656 /* needed to make PWM disable work on Odroid-XU3 */
657 pwm_samsung_manual_update(our_chip, pwm);
658 }
659
660 if (our_chip->disabled_mask & BIT(i))
661 pwm_samsung_disable(chip, pwm);
662 else
663 pwm_samsung_enable(chip, pwm);
664 }
665
666 return 0;
667}
668#endif
669
670static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume);
671
672static struct platform_driver pwm_samsung_driver = {
673 .driver = {
674 .name = "samsung-pwm",
675 .pm = &pwm_samsung_pm_ops,
676 .of_match_table = of_match_ptr(samsung_pwm_matches),
677 },
678 .probe = pwm_samsung_probe,
679 .remove = pwm_samsung_remove,
680};
681module_platform_driver(pwm_samsung_driver);
682
683MODULE_LICENSE("GPL");
684MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
685MODULE_ALIAS("platform:samsung-pwm");
1/*
2 * Copyright (c) 2007 Ben Dooks
3 * Copyright (c) 2008 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
5 * Copyright (c) 2013 Tomasz Figa <tomasz.figa@gmail.com>
6 *
7 * PWM driver for Samsung SoCs
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License.
12 */
13
14#include <linux/bitops.h>
15#include <linux/clk.h>
16#include <linux/export.h>
17#include <linux/err.h>
18#include <linux/io.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/platform_device.h>
23#include <linux/pwm.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
26#include <linux/time.h>
27
28/* For struct samsung_timer_variant and samsung_pwm_lock. */
29#include <clocksource/samsung_pwm.h>
30
31#define REG_TCFG0 0x00
32#define REG_TCFG1 0x04
33#define REG_TCON 0x08
34
35#define REG_TCNTB(chan) (0x0c + ((chan) * 0xc))
36#define REG_TCMPB(chan) (0x10 + ((chan) * 0xc))
37
38#define TCFG0_PRESCALER_MASK 0xff
39#define TCFG0_PRESCALER1_SHIFT 8
40
41#define TCFG1_MUX_MASK 0xf
42#define TCFG1_SHIFT(chan) (4 * (chan))
43
44/*
45 * Each channel occupies 4 bits in TCON register, but there is a gap of 4
46 * bits (one channel) after channel 0, so channels have different numbering
47 * when accessing TCON register. See to_tcon_channel() function.
48 *
49 * In addition, the location of autoreload bit for channel 4 (TCON channel 5)
50 * in its set of bits is 2 as opposed to 3 for other channels.
51 */
52#define TCON_START(chan) BIT(4 * (chan) + 0)
53#define TCON_MANUALUPDATE(chan) BIT(4 * (chan) + 1)
54#define TCON_INVERT(chan) BIT(4 * (chan) + 2)
55#define _TCON_AUTORELOAD(chan) BIT(4 * (chan) + 3)
56#define _TCON_AUTORELOAD4(chan) BIT(4 * (chan) + 2)
57#define TCON_AUTORELOAD(chan) \
58 ((chan < 5) ? _TCON_AUTORELOAD(chan) : _TCON_AUTORELOAD4(chan))
59
60/**
61 * struct samsung_pwm_channel - private data of PWM channel
62 * @period_ns: current period in nanoseconds programmed to the hardware
63 * @duty_ns: current duty time in nanoseconds programmed to the hardware
64 * @tin_ns: time of one timer tick in nanoseconds with current timer rate
65 */
66struct samsung_pwm_channel {
67 u32 period_ns;
68 u32 duty_ns;
69 u32 tin_ns;
70};
71
72/**
73 * struct samsung_pwm_chip - private data of PWM chip
74 * @chip: generic PWM chip
75 * @variant: local copy of hardware variant data
76 * @inverter_mask: inverter status for all channels - one bit per channel
77 * @base: base address of mapped PWM registers
78 * @base_clk: base clock used to drive the timers
79 * @tclk0: external clock 0 (can be ERR_PTR if not present)
80 * @tclk1: external clock 1 (can be ERR_PTR if not present)
81 */
82struct samsung_pwm_chip {
83 struct pwm_chip chip;
84 struct samsung_pwm_variant variant;
85 u8 inverter_mask;
86
87 void __iomem *base;
88 struct clk *base_clk;
89 struct clk *tclk0;
90 struct clk *tclk1;
91};
92
93#ifndef CONFIG_CLKSRC_SAMSUNG_PWM
94/*
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.
99 *
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.
104 */
105static DEFINE_SPINLOCK(samsung_pwm_lock);
106#endif
107
108static inline
109struct samsung_pwm_chip *to_samsung_pwm_chip(struct pwm_chip *chip)
110{
111 return container_of(chip, struct samsung_pwm_chip, chip);
112}
113
114static inline unsigned int to_tcon_channel(unsigned int channel)
115{
116 /* TCON register has a gap of 4 bits (1 channel) after channel 0 */
117 return (channel == 0) ? 0 : (channel + 1);
118}
119
120static void pwm_samsung_set_divisor(struct samsung_pwm_chip *pwm,
121 unsigned int channel, u8 divisor)
122{
123 u8 shift = TCFG1_SHIFT(channel);
124 unsigned long flags;
125 u32 reg;
126 u8 bits;
127
128 bits = (fls(divisor) - 1) - pwm->variant.div_base;
129
130 spin_lock_irqsave(&samsung_pwm_lock, flags);
131
132 reg = readl(pwm->base + REG_TCFG1);
133 reg &= ~(TCFG1_MUX_MASK << shift);
134 reg |= bits << shift;
135 writel(reg, pwm->base + REG_TCFG1);
136
137 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
138}
139
140static int pwm_samsung_is_tdiv(struct samsung_pwm_chip *chip, unsigned int chan)
141{
142 struct samsung_pwm_variant *variant = &chip->variant;
143 u32 reg;
144
145 reg = readl(chip->base + REG_TCFG1);
146 reg >>= TCFG1_SHIFT(chan);
147 reg &= TCFG1_MUX_MASK;
148
149 return (BIT(reg) & variant->tclk_mask) == 0;
150}
151
152static unsigned long pwm_samsung_get_tin_rate(struct samsung_pwm_chip *chip,
153 unsigned int chan)
154{
155 unsigned long rate;
156 u32 reg;
157
158 rate = clk_get_rate(chip->base_clk);
159
160 reg = readl(chip->base + REG_TCFG0);
161 if (chan >= 2)
162 reg >>= TCFG0_PRESCALER1_SHIFT;
163 reg &= TCFG0_PRESCALER_MASK;
164
165 return rate / (reg + 1);
166}
167
168static unsigned long pwm_samsung_calc_tin(struct samsung_pwm_chip *chip,
169 unsigned int chan, unsigned long freq)
170{
171 struct samsung_pwm_variant *variant = &chip->variant;
172 unsigned long rate;
173 struct clk *clk;
174 u8 div;
175
176 if (!pwm_samsung_is_tdiv(chip, chan)) {
177 clk = (chan < 2) ? chip->tclk0 : chip->tclk1;
178 if (!IS_ERR(clk)) {
179 rate = clk_get_rate(clk);
180 if (rate)
181 return rate;
182 }
183
184 dev_warn(chip->chip.dev,
185 "tclk of PWM %d is inoperational, using tdiv\n", chan);
186 }
187
188 rate = pwm_samsung_get_tin_rate(chip, chan);
189 dev_dbg(chip->chip.dev, "tin parent at %lu\n", rate);
190
191 /*
192 * Compare minimum PWM frequency that can be achieved with possible
193 * divider settings and choose the lowest divisor that can generate
194 * frequencies lower than requested.
195 */
196 if (variant->bits < 32) {
197 /* Only for s3c24xx */
198 for (div = variant->div_base; div < 4; ++div)
199 if ((rate >> (variant->bits + div)) < freq)
200 break;
201 } else {
202 /*
203 * Other variants have enough counter bits to generate any
204 * requested rate, so no need to check higher divisors.
205 */
206 div = variant->div_base;
207 }
208
209 pwm_samsung_set_divisor(chip, chan, BIT(div));
210
211 return rate >> div;
212}
213
214static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
215{
216 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
217 struct samsung_pwm_channel *our_chan;
218
219 if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) {
220 dev_warn(chip->dev,
221 "tried to request PWM channel %d without output\n",
222 pwm->hwpwm);
223 return -EINVAL;
224 }
225
226 our_chan = devm_kzalloc(chip->dev, sizeof(*our_chan), GFP_KERNEL);
227 if (!our_chan)
228 return -ENOMEM;
229
230 pwm_set_chip_data(pwm, our_chan);
231
232 return 0;
233}
234
235static void pwm_samsung_free(struct pwm_chip *chip, struct pwm_device *pwm)
236{
237 devm_kfree(chip->dev, pwm_get_chip_data(pwm));
238 pwm_set_chip_data(pwm, NULL);
239}
240
241static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
242{
243 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
244 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
245 unsigned long flags;
246 u32 tcon;
247
248 spin_lock_irqsave(&samsung_pwm_lock, flags);
249
250 tcon = readl(our_chip->base + REG_TCON);
251
252 tcon &= ~TCON_START(tcon_chan);
253 tcon |= TCON_MANUALUPDATE(tcon_chan);
254 writel(tcon, our_chip->base + REG_TCON);
255
256 tcon &= ~TCON_MANUALUPDATE(tcon_chan);
257 tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
258 writel(tcon, our_chip->base + REG_TCON);
259
260 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
261
262 return 0;
263}
264
265static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
266{
267 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
268 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
269 unsigned long flags;
270 u32 tcon;
271
272 spin_lock_irqsave(&samsung_pwm_lock, flags);
273
274 tcon = readl(our_chip->base + REG_TCON);
275 tcon &= ~TCON_AUTORELOAD(tcon_chan);
276 writel(tcon, our_chip->base + REG_TCON);
277
278 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
279}
280
281static void pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
282 struct pwm_device *pwm)
283{
284 unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
285 u32 tcon;
286 unsigned long flags;
287
288 spin_lock_irqsave(&samsung_pwm_lock, flags);
289
290 tcon = readl(chip->base + REG_TCON);
291 tcon |= TCON_MANUALUPDATE(tcon_chan);
292 writel(tcon, chip->base + REG_TCON);
293
294 tcon &= ~TCON_MANUALUPDATE(tcon_chan);
295 writel(tcon, chip->base + REG_TCON);
296
297 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
298}
299
300static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
301 int duty_ns, int period_ns)
302{
303 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
304 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
305 u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
306
307 /*
308 * We currently avoid using 64bit arithmetic by using the
309 * fact that anything faster than 1Hz is easily representable
310 * by 32bits.
311 */
312 if (period_ns > NSEC_PER_SEC)
313 return -ERANGE;
314
315 if (period_ns == chan->period_ns && duty_ns == chan->duty_ns)
316 return 0;
317
318 tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
319 oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
320
321 /* We need tick count for calculation, not last tick. */
322 ++tcnt;
323
324 /* Check to see if we are changing the clock rate of the PWM. */
325 if (chan->period_ns != period_ns) {
326 unsigned long tin_rate;
327 u32 period;
328
329 period = NSEC_PER_SEC / period_ns;
330
331 dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n",
332 duty_ns, period_ns, period);
333
334 tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period);
335
336 dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate);
337
338 tin_ns = NSEC_PER_SEC / tin_rate;
339 tcnt = period_ns / tin_ns;
340 }
341
342 /* Period is too short. */
343 if (tcnt <= 1)
344 return -ERANGE;
345
346 /* Note that counters count down. */
347 tcmp = duty_ns / tin_ns;
348
349 /* 0% duty is not available */
350 if (!tcmp)
351 ++tcmp;
352
353 tcmp = tcnt - tcmp;
354
355 /* Decrement to get tick numbers, instead of tick counts. */
356 --tcnt;
357 /* -1UL will give 100% duty. */
358 --tcmp;
359
360 dev_dbg(our_chip->chip.dev,
361 "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
362
363 /* Update PWM registers. */
364 writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
365 writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
366
367 /*
368 * In case the PWM is currently at 100% duty cycle, force a manual
369 * update to prevent the signal staying high if the PWM is disabled
370 * shortly afer this update (before it autoreloaded the new values).
371 */
372 if (oldtcmp == (u32) -1) {
373 dev_dbg(our_chip->chip.dev, "Forcing manual update");
374 pwm_samsung_manual_update(our_chip, pwm);
375 }
376
377 chan->period_ns = period_ns;
378 chan->tin_ns = tin_ns;
379 chan->duty_ns = duty_ns;
380
381 return 0;
382}
383
384static void pwm_samsung_set_invert(struct samsung_pwm_chip *chip,
385 unsigned int channel, bool invert)
386{
387 unsigned int tcon_chan = to_tcon_channel(channel);
388 unsigned long flags;
389 u32 tcon;
390
391 spin_lock_irqsave(&samsung_pwm_lock, flags);
392
393 tcon = readl(chip->base + REG_TCON);
394
395 if (invert) {
396 chip->inverter_mask |= BIT(channel);
397 tcon |= TCON_INVERT(tcon_chan);
398 } else {
399 chip->inverter_mask &= ~BIT(channel);
400 tcon &= ~TCON_INVERT(tcon_chan);
401 }
402
403 writel(tcon, chip->base + REG_TCON);
404
405 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
406}
407
408static int pwm_samsung_set_polarity(struct pwm_chip *chip,
409 struct pwm_device *pwm,
410 enum pwm_polarity polarity)
411{
412 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
413 bool invert = (polarity == PWM_POLARITY_NORMAL);
414
415 /* Inverted means normal in the hardware. */
416 pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
417
418 return 0;
419}
420
421static const struct pwm_ops pwm_samsung_ops = {
422 .request = pwm_samsung_request,
423 .free = pwm_samsung_free,
424 .enable = pwm_samsung_enable,
425 .disable = pwm_samsung_disable,
426 .config = pwm_samsung_config,
427 .set_polarity = pwm_samsung_set_polarity,
428 .owner = THIS_MODULE,
429};
430
431#ifdef CONFIG_OF
432static const struct samsung_pwm_variant s3c24xx_variant = {
433 .bits = 16,
434 .div_base = 1,
435 .has_tint_cstat = false,
436 .tclk_mask = BIT(4),
437};
438
439static const struct samsung_pwm_variant s3c64xx_variant = {
440 .bits = 32,
441 .div_base = 0,
442 .has_tint_cstat = true,
443 .tclk_mask = BIT(7) | BIT(6) | BIT(5),
444};
445
446static const struct samsung_pwm_variant s5p64x0_variant = {
447 .bits = 32,
448 .div_base = 0,
449 .has_tint_cstat = true,
450 .tclk_mask = 0,
451};
452
453static const struct samsung_pwm_variant s5pc100_variant = {
454 .bits = 32,
455 .div_base = 0,
456 .has_tint_cstat = true,
457 .tclk_mask = BIT(5),
458};
459
460static const struct of_device_id samsung_pwm_matches[] = {
461 { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
462 { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
463 { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
464 { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
465 { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
466 {},
467};
468MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
469
470static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
471{
472 struct device_node *np = chip->chip.dev->of_node;
473 const struct of_device_id *match;
474 struct property *prop;
475 const __be32 *cur;
476 u32 val;
477
478 match = of_match_node(samsung_pwm_matches, np);
479 if (!match)
480 return -ENODEV;
481
482 memcpy(&chip->variant, match->data, sizeof(chip->variant));
483
484 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
485 if (val >= SAMSUNG_PWM_NUM) {
486 dev_err(chip->chip.dev,
487 "%s: invalid channel index in samsung,pwm-outputs property\n",
488 __func__);
489 continue;
490 }
491 chip->variant.output_mask |= BIT(val);
492 }
493
494 return 0;
495}
496#else
497static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
498{
499 return -ENODEV;
500}
501#endif
502
503static int pwm_samsung_probe(struct platform_device *pdev)
504{
505 struct device *dev = &pdev->dev;
506 struct samsung_pwm_chip *chip;
507 struct resource *res;
508 unsigned int chan;
509 int ret;
510
511 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
512 if (chip == NULL)
513 return -ENOMEM;
514
515 chip->chip.dev = &pdev->dev;
516 chip->chip.ops = &pwm_samsung_ops;
517 chip->chip.base = -1;
518 chip->chip.npwm = SAMSUNG_PWM_NUM;
519 chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
520
521 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
522 ret = pwm_samsung_parse_dt(chip);
523 if (ret)
524 return ret;
525
526 chip->chip.of_xlate = of_pwm_xlate_with_flags;
527 chip->chip.of_pwm_n_cells = 3;
528 } else {
529 if (!pdev->dev.platform_data) {
530 dev_err(&pdev->dev, "no platform data specified\n");
531 return -EINVAL;
532 }
533
534 memcpy(&chip->variant, pdev->dev.platform_data,
535 sizeof(chip->variant));
536 }
537
538 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
539 chip->base = devm_ioremap_resource(&pdev->dev, res);
540 if (IS_ERR(chip->base))
541 return PTR_ERR(chip->base);
542
543 chip->base_clk = devm_clk_get(&pdev->dev, "timers");
544 if (IS_ERR(chip->base_clk)) {
545 dev_err(dev, "failed to get timer base clk\n");
546 return PTR_ERR(chip->base_clk);
547 }
548
549 ret = clk_prepare_enable(chip->base_clk);
550 if (ret < 0) {
551 dev_err(dev, "failed to enable base clock\n");
552 return ret;
553 }
554
555 for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
556 if (chip->variant.output_mask & BIT(chan))
557 pwm_samsung_set_invert(chip, chan, true);
558
559 /* Following clocks are optional. */
560 chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
561 chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
562
563 platform_set_drvdata(pdev, chip);
564
565 ret = pwmchip_add(&chip->chip);
566 if (ret < 0) {
567 dev_err(dev, "failed to register PWM chip\n");
568 clk_disable_unprepare(chip->base_clk);
569 return ret;
570 }
571
572 dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
573 clk_get_rate(chip->base_clk),
574 !IS_ERR(chip->tclk0) ? clk_get_rate(chip->tclk0) : 0,
575 !IS_ERR(chip->tclk1) ? clk_get_rate(chip->tclk1) : 0);
576
577 return 0;
578}
579
580static int pwm_samsung_remove(struct platform_device *pdev)
581{
582 struct samsung_pwm_chip *chip = platform_get_drvdata(pdev);
583 int ret;
584
585 ret = pwmchip_remove(&chip->chip);
586 if (ret < 0)
587 return ret;
588
589 clk_disable_unprepare(chip->base_clk);
590
591 return 0;
592}
593
594#ifdef CONFIG_PM_SLEEP
595static int pwm_samsung_suspend(struct device *dev)
596{
597 struct samsung_pwm_chip *chip = dev_get_drvdata(dev);
598 unsigned int i;
599
600 /*
601 * No one preserves these values during suspend so reset them.
602 * Otherwise driver leaves PWM unconfigured if same values are
603 * passed to pwm_config() next time.
604 */
605 for (i = 0; i < SAMSUNG_PWM_NUM; ++i) {
606 struct pwm_device *pwm = &chip->chip.pwms[i];
607 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
608
609 if (!chan)
610 continue;
611
612 chan->period_ns = 0;
613 chan->duty_ns = 0;
614 }
615
616 return 0;
617}
618
619static int pwm_samsung_resume(struct device *dev)
620{
621 struct samsung_pwm_chip *chip = dev_get_drvdata(dev);
622 unsigned int chan;
623
624 /*
625 * Inverter setting must be preserved across suspend/resume
626 * as nobody really seems to configure it more than once.
627 */
628 for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan) {
629 if (chip->variant.output_mask & BIT(chan))
630 pwm_samsung_set_invert(chip, chan,
631 chip->inverter_mask & BIT(chan));
632 }
633
634 return 0;
635}
636#endif
637
638static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, pwm_samsung_suspend,
639 pwm_samsung_resume);
640
641static struct platform_driver pwm_samsung_driver = {
642 .driver = {
643 .name = "samsung-pwm",
644 .pm = &pwm_samsung_pm_ops,
645 .of_match_table = of_match_ptr(samsung_pwm_matches),
646 },
647 .probe = pwm_samsung_probe,
648 .remove = pwm_samsung_remove,
649};
650module_platform_driver(pwm_samsung_driver);
651
652MODULE_LICENSE("GPL");
653MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
654MODULE_ALIAS("platform:samsung-pwm");