GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / pwm / pwm-samsung.c
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  */
66 struct 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  */
82 struct 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  */
105 static DEFINE_SPINLOCK(samsung_pwm_lock);
106 #endif
107
108 static inline
109 struct samsung_pwm_chip *to_samsung_pwm_chip(struct pwm_chip *chip)
110 {
111         return container_of(chip, struct samsung_pwm_chip, chip);
112 }
113
114 static 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
120 static 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
140 static 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
152 static 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
168 static 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         for (div = variant->div_base; div < 4; ++div)
197                 if ((rate >> (variant->bits + div)) < freq)
198                         break;
199
200         pwm_samsung_set_divisor(chip, chan, BIT(div));
201
202         return rate >> div;
203 }
204
205 static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
206 {
207         struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
208         struct samsung_pwm_channel *our_chan;
209
210         if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) {
211                 dev_warn(chip->dev,
212                         "tried to request PWM channel %d without output\n",
213                         pwm->hwpwm);
214                 return -EINVAL;
215         }
216
217         our_chan = devm_kzalloc(chip->dev, sizeof(*our_chan), GFP_KERNEL);
218         if (!our_chan)
219                 return -ENOMEM;
220
221         pwm_set_chip_data(pwm, our_chan);
222
223         return 0;
224 }
225
226 static void pwm_samsung_free(struct pwm_chip *chip, struct pwm_device *pwm)
227 {
228         devm_kfree(chip->dev, pwm_get_chip_data(pwm));
229 }
230
231 static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
232 {
233         struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
234         unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
235         unsigned long flags;
236         u32 tcon;
237
238         spin_lock_irqsave(&samsung_pwm_lock, flags);
239
240         tcon = readl(our_chip->base + REG_TCON);
241
242         tcon &= ~TCON_START(tcon_chan);
243         tcon |= TCON_MANUALUPDATE(tcon_chan);
244         writel(tcon, our_chip->base + REG_TCON);
245
246         tcon &= ~TCON_MANUALUPDATE(tcon_chan);
247         tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
248         writel(tcon, our_chip->base + REG_TCON);
249
250         spin_unlock_irqrestore(&samsung_pwm_lock, flags);
251
252         return 0;
253 }
254
255 static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
256 {
257         struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
258         unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
259         unsigned long flags;
260         u32 tcon;
261
262         spin_lock_irqsave(&samsung_pwm_lock, flags);
263
264         tcon = readl(our_chip->base + REG_TCON);
265         tcon &= ~TCON_AUTORELOAD(tcon_chan);
266         writel(tcon, our_chip->base + REG_TCON);
267
268         spin_unlock_irqrestore(&samsung_pwm_lock, flags);
269 }
270
271 static void pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
272                                       struct pwm_device *pwm)
273 {
274         unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
275         u32 tcon;
276         unsigned long flags;
277
278         spin_lock_irqsave(&samsung_pwm_lock, flags);
279
280         tcon = readl(chip->base + REG_TCON);
281         tcon |= TCON_MANUALUPDATE(tcon_chan);
282         writel(tcon, chip->base + REG_TCON);
283
284         tcon &= ~TCON_MANUALUPDATE(tcon_chan);
285         writel(tcon, chip->base + REG_TCON);
286
287         spin_unlock_irqrestore(&samsung_pwm_lock, flags);
288 }
289
290 static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
291                               int duty_ns, int period_ns)
292 {
293         struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
294         struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
295         u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
296
297         /*
298          * We currently avoid using 64bit arithmetic by using the
299          * fact that anything faster than 1Hz is easily representable
300          * by 32bits.
301          */
302         if (period_ns > NSEC_PER_SEC)
303                 return -ERANGE;
304
305         if (period_ns == chan->period_ns && duty_ns == chan->duty_ns)
306                 return 0;
307
308         tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
309         oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
310
311         /* We need tick count for calculation, not last tick. */
312         ++tcnt;
313
314         /* Check to see if we are changing the clock rate of the PWM. */
315         if (chan->period_ns != period_ns) {
316                 unsigned long tin_rate;
317                 u32 period;
318
319                 period = NSEC_PER_SEC / period_ns;
320
321                 dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n",
322                                                 duty_ns, period_ns, period);
323
324                 tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period);
325
326                 dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate);
327
328                 tin_ns = NSEC_PER_SEC / tin_rate;
329                 tcnt = period_ns / tin_ns;
330         }
331
332         /* Period is too short. */
333         if (tcnt <= 1)
334                 return -ERANGE;
335
336         /* Note that counters count down. */
337         tcmp = duty_ns / tin_ns;
338
339         /* 0% duty is not available */
340         if (!tcmp)
341                 ++tcmp;
342
343         tcmp = tcnt - tcmp;
344
345         /* Decrement to get tick numbers, instead of tick counts. */
346         --tcnt;
347         /* -1UL will give 100% duty. */
348         --tcmp;
349
350         dev_dbg(our_chip->chip.dev,
351                                 "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
352
353         /* Update PWM registers. */
354         writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
355         writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
356
357         /*
358          * In case the PWM is currently at 100% duty cycle, force a manual
359          * update to prevent the signal staying high if the PWM is disabled
360          * shortly afer this update (before it autoreloaded the new values).
361          */
362         if (oldtcmp == (u32) -1) {
363                 dev_dbg(our_chip->chip.dev, "Forcing manual update");
364                 pwm_samsung_manual_update(our_chip, pwm);
365         }
366
367         chan->period_ns = period_ns;
368         chan->tin_ns = tin_ns;
369         chan->duty_ns = duty_ns;
370
371         return 0;
372 }
373
374 static void pwm_samsung_set_invert(struct samsung_pwm_chip *chip,
375                                    unsigned int channel, bool invert)
376 {
377         unsigned int tcon_chan = to_tcon_channel(channel);
378         unsigned long flags;
379         u32 tcon;
380
381         spin_lock_irqsave(&samsung_pwm_lock, flags);
382
383         tcon = readl(chip->base + REG_TCON);
384
385         if (invert) {
386                 chip->inverter_mask |= BIT(channel);
387                 tcon |= TCON_INVERT(tcon_chan);
388         } else {
389                 chip->inverter_mask &= ~BIT(channel);
390                 tcon &= ~TCON_INVERT(tcon_chan);
391         }
392
393         writel(tcon, chip->base + REG_TCON);
394
395         spin_unlock_irqrestore(&samsung_pwm_lock, flags);
396 }
397
398 static int pwm_samsung_set_polarity(struct pwm_chip *chip,
399                                     struct pwm_device *pwm,
400                                     enum pwm_polarity polarity)
401 {
402         struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
403         bool invert = (polarity == PWM_POLARITY_NORMAL);
404
405         /* Inverted means normal in the hardware. */
406         pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
407
408         return 0;
409 }
410
411 static const struct pwm_ops pwm_samsung_ops = {
412         .request        = pwm_samsung_request,
413         .free           = pwm_samsung_free,
414         .enable         = pwm_samsung_enable,
415         .disable        = pwm_samsung_disable,
416         .config         = pwm_samsung_config,
417         .set_polarity   = pwm_samsung_set_polarity,
418         .owner          = THIS_MODULE,
419 };
420
421 #ifdef CONFIG_OF
422 static const struct samsung_pwm_variant s3c24xx_variant = {
423         .bits           = 16,
424         .div_base       = 1,
425         .has_tint_cstat = false,
426         .tclk_mask      = BIT(4),
427 };
428
429 static const struct samsung_pwm_variant s3c64xx_variant = {
430         .bits           = 32,
431         .div_base       = 0,
432         .has_tint_cstat = true,
433         .tclk_mask      = BIT(7) | BIT(6) | BIT(5),
434 };
435
436 static const struct samsung_pwm_variant s5p64x0_variant = {
437         .bits           = 32,
438         .div_base       = 0,
439         .has_tint_cstat = true,
440         .tclk_mask      = 0,
441 };
442
443 static const struct samsung_pwm_variant s5pc100_variant = {
444         .bits           = 32,
445         .div_base       = 0,
446         .has_tint_cstat = true,
447         .tclk_mask      = BIT(5),
448 };
449
450 static const struct of_device_id samsung_pwm_matches[] = {
451         { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
452         { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
453         { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
454         { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
455         { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
456         {},
457 };
458 MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
459
460 static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
461 {
462         struct device_node *np = chip->chip.dev->of_node;
463         const struct of_device_id *match;
464         struct property *prop;
465         const __be32 *cur;
466         u32 val;
467
468         match = of_match_node(samsung_pwm_matches, np);
469         if (!match)
470                 return -ENODEV;
471
472         memcpy(&chip->variant, match->data, sizeof(chip->variant));
473
474         of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
475                 if (val >= SAMSUNG_PWM_NUM) {
476                         dev_err(chip->chip.dev,
477                                 "%s: invalid channel index in samsung,pwm-outputs property\n",
478                                                                 __func__);
479                         continue;
480                 }
481                 chip->variant.output_mask |= BIT(val);
482         }
483
484         return 0;
485 }
486 #else
487 static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
488 {
489         return -ENODEV;
490 }
491 #endif
492
493 static int pwm_samsung_probe(struct platform_device *pdev)
494 {
495         struct device *dev = &pdev->dev;
496         struct samsung_pwm_chip *chip;
497         struct resource *res;
498         unsigned int chan;
499         int ret;
500
501         chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
502         if (chip == NULL)
503                 return -ENOMEM;
504
505         chip->chip.dev = &pdev->dev;
506         chip->chip.ops = &pwm_samsung_ops;
507         chip->chip.base = -1;
508         chip->chip.npwm = SAMSUNG_PWM_NUM;
509         chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
510
511         if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
512                 ret = pwm_samsung_parse_dt(chip);
513                 if (ret)
514                         return ret;
515
516                 chip->chip.of_xlate = of_pwm_xlate_with_flags;
517                 chip->chip.of_pwm_n_cells = 3;
518         } else {
519                 if (!pdev->dev.platform_data) {
520                         dev_err(&pdev->dev, "no platform data specified\n");
521                         return -EINVAL;
522                 }
523
524                 memcpy(&chip->variant, pdev->dev.platform_data,
525                                                         sizeof(chip->variant));
526         }
527
528         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
529         chip->base = devm_ioremap_resource(&pdev->dev, res);
530         if (IS_ERR(chip->base))
531                 return PTR_ERR(chip->base);
532
533         chip->base_clk = devm_clk_get(&pdev->dev, "timers");
534         if (IS_ERR(chip->base_clk)) {
535                 dev_err(dev, "failed to get timer base clk\n");
536                 return PTR_ERR(chip->base_clk);
537         }
538
539         ret = clk_prepare_enable(chip->base_clk);
540         if (ret < 0) {
541                 dev_err(dev, "failed to enable base clock\n");
542                 return ret;
543         }
544
545         for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
546                 if (chip->variant.output_mask & BIT(chan))
547                         pwm_samsung_set_invert(chip, chan, true);
548
549         /* Following clocks are optional. */
550         chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
551         chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
552
553         platform_set_drvdata(pdev, chip);
554
555         ret = pwmchip_add(&chip->chip);
556         if (ret < 0) {
557                 dev_err(dev, "failed to register PWM chip\n");
558                 clk_disable_unprepare(chip->base_clk);
559                 return ret;
560         }
561
562         dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
563                 clk_get_rate(chip->base_clk),
564                 !IS_ERR(chip->tclk0) ? clk_get_rate(chip->tclk0) : 0,
565                 !IS_ERR(chip->tclk1) ? clk_get_rate(chip->tclk1) : 0);
566
567         return 0;
568 }
569
570 static int pwm_samsung_remove(struct platform_device *pdev)
571 {
572         struct samsung_pwm_chip *chip = platform_get_drvdata(pdev);
573         int ret;
574
575         ret = pwmchip_remove(&chip->chip);
576         if (ret < 0)
577                 return ret;
578
579         clk_disable_unprepare(chip->base_clk);
580
581         return 0;
582 }
583
584 #ifdef CONFIG_PM_SLEEP
585 static int pwm_samsung_suspend(struct device *dev)
586 {
587         struct samsung_pwm_chip *chip = dev_get_drvdata(dev);
588         unsigned int i;
589
590         /*
591          * No one preserves these values during suspend so reset them.
592          * Otherwise driver leaves PWM unconfigured if same values are
593          * passed to pwm_config() next time.
594          */
595         for (i = 0; i < SAMSUNG_PWM_NUM; ++i) {
596                 struct pwm_device *pwm = &chip->chip.pwms[i];
597                 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
598
599                 if (!chan)
600                         continue;
601
602                 chan->period_ns = 0;
603                 chan->duty_ns = 0;
604         }
605
606         return 0;
607 }
608
609 static int pwm_samsung_resume(struct device *dev)
610 {
611         struct samsung_pwm_chip *chip = dev_get_drvdata(dev);
612         unsigned int chan;
613
614         /*
615          * Inverter setting must be preserved across suspend/resume
616          * as nobody really seems to configure it more than once.
617          */
618         for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan) {
619                 if (chip->variant.output_mask & BIT(chan))
620                         pwm_samsung_set_invert(chip, chan,
621                                         chip->inverter_mask & BIT(chan));
622         }
623
624         return 0;
625 }
626 #endif
627
628 static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, pwm_samsung_suspend,
629                          pwm_samsung_resume);
630
631 static struct platform_driver pwm_samsung_driver = {
632         .driver         = {
633                 .name   = "samsung-pwm",
634                 .pm     = &pwm_samsung_pm_ops,
635                 .of_match_table = of_match_ptr(samsung_pwm_matches),
636         },
637         .probe          = pwm_samsung_probe,
638         .remove         = pwm_samsung_remove,
639 };
640 module_platform_driver(pwm_samsung_driver);
641
642 MODULE_LICENSE("GPL");
643 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
644 MODULE_ALIAS("platform:samsung-pwm");