blob: 0a4ff55fad0407716a7c63e22a57c7e263b43d17 [file] [log] [blame]
Thomas Gleixner84a14ae2019-05-28 09:57:07 -07001// SPDX-License-Identifier: GPL-2.0-only
Tomasz Figa11ad39e2013-04-06 02:40:36 +02002/*
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>
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +02007 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
Tomasz Figa11ad39e2013-04-06 02:40:36 +02008 *
9 * PWM driver for Samsung SoCs
Tomasz Figa11ad39e2013-04-06 02:40:36 +020010 */
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>
Sachin Kamatc3bdfe12013-09-27 16:53:24 +053019#include <linux/of.h>
Tomasz Figa11ad39e2013-04-06 02:40:36 +020020#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
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +020075 * @disabled_mask: disabled status for all channels - one bit per channel
Tomasz Figa11ad39e2013-04-06 02:40:36 +020076 * @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;
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +020085 u8 disabled_mask;
Tomasz Figa11ad39e2013-04-06 02:40:36 +020086
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
Mårten Lindahl5d82e662021-09-09 12:10:42 +0200120static 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
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200134static 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 */
Seung-Woo Kim04d68de2016-08-16 23:22:01 +0900210 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 }
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200222
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
Uwe Kleine-Königb05232d2019-03-25 10:49:34 +0100240 our_chan = kzalloc(sizeof(*our_chan), GFP_KERNEL);
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200241 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{
Uwe Kleine-Königb05232d2019-03-25 10:49:34 +0100251 kfree(pwm_get_chip_data(pwm));
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200252}
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
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200273 our_chip->disabled_mask &= ~BIT(pwm->hwpwm);
274
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200275 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
Mårten Lindahl5d82e662021-09-09 12:10:42 +0200293 /*
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
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200300 our_chip->disabled_mask |= BIT(pwm->hwpwm);
301
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200302 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
303}
304
Sjoerd Simons4a1c6832015-03-05 09:14:03 +0100305static void pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
306 struct pwm_device *pwm)
307{
Sjoerd Simons4a1c6832015-03-05 09:14:03 +0100308 unsigned long flags;
309
310 spin_lock_irqsave(&samsung_pwm_lock, flags);
311
Mårten Lindahl5d82e662021-09-09 12:10:42 +0200312 __pwm_samsung_manual_update(chip, pwm);
Sjoerd Simons4a1c6832015-03-05 09:14:03 +0100313
314 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
315}
316
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200317static int __pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
318 int duty_ns, int period_ns, bool force_period)
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200319{
320 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
321 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
Sjoerd Simons4a1c6832015-03-05 09:14:03 +0100322 u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200323
324 /*
325 * We currently avoid using 64bit arithmetic by using the
326 * fact that anything faster than 1Hz is easily representable
327 * by 32bits.
328 */
329 if (period_ns > NSEC_PER_SEC)
330 return -ERANGE;
331
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200332 tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
Sjoerd Simons4a1c6832015-03-05 09:14:03 +0100333 oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200334
335 /* We need tick count for calculation, not last tick. */
336 ++tcnt;
337
338 /* Check to see if we are changing the clock rate of the PWM. */
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200339 if (chan->period_ns != period_ns || force_period) {
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200340 unsigned long tin_rate;
341 u32 period;
342
343 period = NSEC_PER_SEC / period_ns;
344
345 dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n",
346 duty_ns, period_ns, period);
347
348 tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period);
349
350 dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate);
351
352 tin_ns = NSEC_PER_SEC / tin_rate;
353 tcnt = period_ns / tin_ns;
354 }
355
356 /* Period is too short. */
357 if (tcnt <= 1)
358 return -ERANGE;
359
360 /* Note that counters count down. */
361 tcmp = duty_ns / tin_ns;
362
363 /* 0% duty is not available */
364 if (!tcmp)
365 ++tcmp;
366
367 tcmp = tcnt - tcmp;
368
369 /* Decrement to get tick numbers, instead of tick counts. */
370 --tcnt;
371 /* -1UL will give 100% duty. */
372 --tcmp;
373
374 dev_dbg(our_chip->chip.dev,
375 "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
376
377 /* Update PWM registers. */
378 writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
379 writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
380
Sjoerd Simons4a1c6832015-03-05 09:14:03 +0100381 /*
382 * In case the PWM is currently at 100% duty cycle, force a manual
383 * update to prevent the signal staying high if the PWM is disabled
384 * shortly afer this update (before it autoreloaded the new values).
385 */
386 if (oldtcmp == (u32) -1) {
387 dev_dbg(our_chip->chip.dev, "Forcing manual update");
388 pwm_samsung_manual_update(our_chip, pwm);
389 }
390
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200391 chan->period_ns = period_ns;
392 chan->tin_ns = tin_ns;
393 chan->duty_ns = duty_ns;
394
395 return 0;
396}
397
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200398static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
399 int duty_ns, int period_ns)
400{
401 return __pwm_samsung_config(chip, pwm, duty_ns, period_ns, false);
402}
403
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200404static void pwm_samsung_set_invert(struct samsung_pwm_chip *chip,
405 unsigned int channel, bool invert)
406{
407 unsigned int tcon_chan = to_tcon_channel(channel);
408 unsigned long flags;
409 u32 tcon;
410
411 spin_lock_irqsave(&samsung_pwm_lock, flags);
412
413 tcon = readl(chip->base + REG_TCON);
414
415 if (invert) {
416 chip->inverter_mask |= BIT(channel);
417 tcon |= TCON_INVERT(tcon_chan);
418 } else {
419 chip->inverter_mask &= ~BIT(channel);
420 tcon &= ~TCON_INVERT(tcon_chan);
421 }
422
423 writel(tcon, chip->base + REG_TCON);
424
425 spin_unlock_irqrestore(&samsung_pwm_lock, flags);
426}
427
428static int pwm_samsung_set_polarity(struct pwm_chip *chip,
429 struct pwm_device *pwm,
430 enum pwm_polarity polarity)
431{
432 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
433 bool invert = (polarity == PWM_POLARITY_NORMAL);
434
435 /* Inverted means normal in the hardware. */
436 pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
437
438 return 0;
439}
440
441static const struct pwm_ops pwm_samsung_ops = {
442 .request = pwm_samsung_request,
443 .free = pwm_samsung_free,
444 .enable = pwm_samsung_enable,
445 .disable = pwm_samsung_disable,
446 .config = pwm_samsung_config,
447 .set_polarity = pwm_samsung_set_polarity,
448 .owner = THIS_MODULE,
449};
450
451#ifdef CONFIG_OF
452static const struct samsung_pwm_variant s3c24xx_variant = {
453 .bits = 16,
454 .div_base = 1,
455 .has_tint_cstat = false,
456 .tclk_mask = BIT(4),
457};
458
459static const struct samsung_pwm_variant s3c64xx_variant = {
460 .bits = 32,
461 .div_base = 0,
462 .has_tint_cstat = true,
463 .tclk_mask = BIT(7) | BIT(6) | BIT(5),
464};
465
466static const struct samsung_pwm_variant s5p64x0_variant = {
467 .bits = 32,
468 .div_base = 0,
469 .has_tint_cstat = true,
470 .tclk_mask = 0,
471};
472
473static const struct samsung_pwm_variant s5pc100_variant = {
474 .bits = 32,
475 .div_base = 0,
476 .has_tint_cstat = true,
477 .tclk_mask = BIT(5),
478};
479
480static const struct of_device_id samsung_pwm_matches[] = {
481 { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
482 { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
483 { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
484 { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
485 { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
486 {},
487};
Javier Martinez Canillascccb9452015-05-14 02:32:31 +0200488MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200489
490static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
491{
492 struct device_node *np = chip->chip.dev->of_node;
493 const struct of_device_id *match;
494 struct property *prop;
495 const __be32 *cur;
496 u32 val;
497
498 match = of_match_node(samsung_pwm_matches, np);
499 if (!match)
500 return -ENODEV;
501
502 memcpy(&chip->variant, match->data, sizeof(chip->variant));
503
504 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
505 if (val >= SAMSUNG_PWM_NUM) {
506 dev_err(chip->chip.dev,
507 "%s: invalid channel index in samsung,pwm-outputs property\n",
508 __func__);
509 continue;
510 }
511 chip->variant.output_mask |= BIT(val);
512 }
513
514 return 0;
515}
516#else
517static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
518{
519 return -ENODEV;
520}
521#endif
522
523static int pwm_samsung_probe(struct platform_device *pdev)
524{
525 struct device *dev = &pdev->dev;
526 struct samsung_pwm_chip *chip;
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200527 unsigned int chan;
528 int ret;
529
530 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
531 if (chip == NULL)
532 return -ENOMEM;
533
534 chip->chip.dev = &pdev->dev;
535 chip->chip.ops = &pwm_samsung_ops;
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200536 chip->chip.npwm = SAMSUNG_PWM_NUM;
537 chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
538
539 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
540 ret = pwm_samsung_parse_dt(chip);
541 if (ret)
542 return ret;
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200543 } else {
544 if (!pdev->dev.platform_data) {
545 dev_err(&pdev->dev, "no platform data specified\n");
546 return -EINVAL;
547 }
548
549 memcpy(&chip->variant, pdev->dev.platform_data,
550 sizeof(chip->variant));
551 }
552
Yangtao Licc1cc4b2019-12-29 08:05:55 +0000553 chip->base = devm_platform_ioremap_resource(pdev, 0);
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200554 if (IS_ERR(chip->base))
555 return PTR_ERR(chip->base);
556
557 chip->base_clk = devm_clk_get(&pdev->dev, "timers");
558 if (IS_ERR(chip->base_clk)) {
559 dev_err(dev, "failed to get timer base clk\n");
560 return PTR_ERR(chip->base_clk);
561 }
562
563 ret = clk_prepare_enable(chip->base_clk);
564 if (ret < 0) {
565 dev_err(dev, "failed to enable base clock\n");
566 return ret;
567 }
568
569 for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
570 if (chip->variant.output_mask & BIT(chan))
571 pwm_samsung_set_invert(chip, chan, true);
572
573 /* Following clocks are optional. */
574 chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
575 chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
576
577 platform_set_drvdata(pdev, chip);
578
579 ret = pwmchip_add(&chip->chip);
580 if (ret < 0) {
581 dev_err(dev, "failed to register PWM chip\n");
582 clk_disable_unprepare(chip->base_clk);
583 return ret;
584 }
585
586 dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
587 clk_get_rate(chip->base_clk),
588 !IS_ERR(chip->tclk0) ? clk_get_rate(chip->tclk0) : 0,
589 !IS_ERR(chip->tclk1) ? clk_get_rate(chip->tclk1) : 0);
590
591 return 0;
592}
593
594static int pwm_samsung_remove(struct platform_device *pdev)
595{
596 struct samsung_pwm_chip *chip = platform_get_drvdata(pdev);
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200597
Uwe Kleine-König4e334972021-07-07 18:28:32 +0200598 pwmchip_remove(&chip->chip);
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200599
600 clk_disable_unprepare(chip->base_clk);
601
602 return 0;
603}
604
605#ifdef CONFIG_PM_SLEEP
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200606static int pwm_samsung_resume(struct device *dev)
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200607{
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200608 struct samsung_pwm_chip *our_chip = dev_get_drvdata(dev);
609 struct pwm_chip *chip = &our_chip->chip;
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200610 unsigned int i;
611
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200612 for (i = 0; i < SAMSUNG_PWM_NUM; i++) {
613 struct pwm_device *pwm = &chip->pwms[i];
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200614 struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
615
616 if (!chan)
617 continue;
618
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200619 if (our_chip->variant.output_mask & BIT(i))
620 pwm_samsung_set_invert(our_chip, i,
621 our_chip->inverter_mask & BIT(i));
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200622
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200623 if (chan->period_ns) {
624 __pwm_samsung_config(chip, pwm, chan->duty_ns,
625 chan->period_ns, true);
626 /* needed to make PWM disable work on Odroid-XU3 */
627 pwm_samsung_manual_update(our_chip, pwm);
628 }
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200629
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200630 if (our_chip->disabled_mask & BIT(i))
631 pwm_samsung_disable(chip, pwm);
632 else
633 pwm_samsung_enable(chip, pwm);
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200634 }
635
636 return 0;
637}
638#endif
639
Bartlomiej Zolnierkiewicz08a4d8e2017-04-24 12:01:08 +0200640static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume);
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200641
642static struct platform_driver pwm_samsung_driver = {
643 .driver = {
644 .name = "samsung-pwm",
Tomasz Figa11ad39e2013-04-06 02:40:36 +0200645 .pm = &pwm_samsung_pm_ops,
646 .of_match_table = of_match_ptr(samsung_pwm_matches),
647 },
648 .probe = pwm_samsung_probe,
649 .remove = pwm_samsung_remove,
650};
651module_platform_driver(pwm_samsung_driver);
652
653MODULE_LICENSE("GPL");
654MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
655MODULE_ALIAS("platform:samsung-pwm");