Linux Audio

Check our new training course

Loading...
v6.13.7
  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 * @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
 80 */
 81struct samsung_pwm_chip {
 
 82	struct samsung_pwm_variant variant;
 83	u8 inverter_mask;
 84	u8 disabled_mask;
 85
 86	void __iomem *base;
 87	struct clk *base_clk;
 88	struct clk *tclk0;
 89	struct clk *tclk1;
 90	struct samsung_pwm_channel channel[SAMSUNG_PWM_NUM];
 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 pwmchip_get_drvdata(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 *our_chip,
121				      struct pwm_device *pwm)
122{
123	unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
124	u32 tcon;
125
126	tcon = readl(our_chip->base + REG_TCON);
127	tcon |= TCON_MANUALUPDATE(tcon_chan);
128	writel(tcon, our_chip->base + REG_TCON);
129
130	tcon &= ~TCON_MANUALUPDATE(tcon_chan);
131	writel(tcon, our_chip->base + REG_TCON);
132}
133
134static void pwm_samsung_set_divisor(struct samsung_pwm_chip *our_chip,
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) - our_chip->variant.div_base;
143
144	spin_lock_irqsave(&samsung_pwm_lock, flags);
145
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);
150
151	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
152}
153
154static int pwm_samsung_is_tdiv(struct samsung_pwm_chip *our_chip, unsigned int chan)
155{
156	struct samsung_pwm_variant *variant = &our_chip->variant;
157	u32 reg;
158
159	reg = readl(our_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 *our_chip,
167					      unsigned int chan)
168{
169	unsigned long rate;
170	u32 reg;
171
172	rate = clk_get_rate(our_chip->base_clk);
173
174	reg = readl(our_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 pwm_chip *chip,
183					  unsigned int chan, unsigned long freq)
184{
185	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
186	struct samsung_pwm_variant *variant = &our_chip->variant;
187	unsigned long rate;
188	struct clk *clk;
189	u8 div;
190
191	if (!pwm_samsung_is_tdiv(our_chip, chan)) {
192		clk = (chan < 2) ? our_chip->tclk0 : our_chip->tclk1;
193		if (!IS_ERR(clk)) {
194			rate = clk_get_rate(clk);
195			if (rate)
196				return rate;
197		}
198
199		dev_warn(pwmchip_parent(chip),
200			"tclk of PWM %d is inoperational, using tdiv\n", chan);
201	}
202
203	rate = pwm_samsung_get_tin_rate(our_chip, chan);
204	dev_dbg(pwmchip_parent(chip), "tin parent at %lu\n", rate);
205
206	/*
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.
210	 */
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)
215				break;
216	} else {
217		/*
218		 * Other variants have enough counter bits to generate any
219		 * requested rate, so no need to check higher divisors.
220		 */
221		div = variant->div_base;
222	}
223
224	pwm_samsung_set_divisor(our_chip, chan, BIT(div));
225
226	return rate >> div;
227}
228
229static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
230{
231	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
232
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",
236			pwm->hwpwm);
237		return -EINVAL;
238	}
239
240	memset(&our_chip->channel[pwm->hwpwm], 0, sizeof(our_chip->channel[pwm->hwpwm]));
241
242	return 0;
243}
244
245static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
246{
247	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
248	unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
249	unsigned long flags;
250	u32 tcon;
251
252	spin_lock_irqsave(&samsung_pwm_lock, flags);
253
254	tcon = readl(our_chip->base + REG_TCON);
255
256	tcon &= ~TCON_START(tcon_chan);
257	tcon |= TCON_MANUALUPDATE(tcon_chan);
258	writel(tcon, our_chip->base + REG_TCON);
259
260	tcon &= ~TCON_MANUALUPDATE(tcon_chan);
261	tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
262	writel(tcon, our_chip->base + REG_TCON);
263
264	our_chip->disabled_mask &= ~BIT(pwm->hwpwm);
265
266	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
267
268	return 0;
269}
270
271static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
272{
273	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
274	unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
275	unsigned long flags;
276	u32 tcon;
277
278	spin_lock_irqsave(&samsung_pwm_lock, flags);
279
280	tcon = readl(our_chip->base + REG_TCON);
281	tcon &= ~TCON_AUTORELOAD(tcon_chan);
282	writel(tcon, our_chip->base + REG_TCON);
283
284	/*
285	 * In case the PWM is at 100% duty cycle, force a manual
286	 * update to prevent the signal from staying high.
287	 */
288	if (readl(our_chip->base + REG_TCMPB(pwm->hwpwm)) == (u32)-1U)
289		__pwm_samsung_manual_update(our_chip, pwm);
290
291	our_chip->disabled_mask |= BIT(pwm->hwpwm);
292
293	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
294}
295
296static void pwm_samsung_manual_update(struct samsung_pwm_chip *our_chip,
297				      struct pwm_device *pwm)
298{
299	unsigned long flags;
300
301	spin_lock_irqsave(&samsung_pwm_lock, flags);
302
303	__pwm_samsung_manual_update(our_chip, pwm);
304
305	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
306}
307
308static int __pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
309				int duty_ns, int period_ns, bool force_period)
310{
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;
314
315	tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
316	oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
317
318	/* We need tick count for calculation, not last tick. */
319	++tcnt;
320
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;
324		u32 period;
325
326		period = NSEC_PER_SEC / period_ns;
327
328		dev_dbg(pwmchip_parent(chip), "duty_ns=%d, period_ns=%d (%u)\n",
329						duty_ns, period_ns, period);
330
331		tin_rate = pwm_samsung_calc_tin(chip, pwm->hwpwm, period);
332
333		dev_dbg(pwmchip_parent(chip), "tin_rate=%lu\n", tin_rate);
334
335		tin_ns = NSEC_PER_SEC / tin_rate;
336		tcnt = period_ns / tin_ns;
337	}
338
339	/* Period is too short. */
340	if (tcnt <= 1)
341		return -ERANGE;
342
343	/* Note that counters count down. */
344	tcmp = duty_ns / tin_ns;
345
346	/* 0% duty is not available */
347	if (!tcmp)
348		++tcmp;
349
350	tcmp = tcnt - tcmp;
351
352	/* Decrement to get tick numbers, instead of tick counts. */
353	--tcnt;
354	/* -1UL will give 100% duty. */
355	--tcmp;
356
357	dev_dbg(pwmchip_parent(chip), "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
 
358
359	/* Update PWM registers. */
360	writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
361	writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
362
363	/*
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).
367	 */
368	if (oldtcmp == (u32) -1) {
369		dev_dbg(pwmchip_parent(chip), "Forcing manual update");
370		pwm_samsung_manual_update(our_chip, pwm);
371	}
372
373	chan->period_ns = period_ns;
374	chan->tin_ns = tin_ns;
375	chan->duty_ns = duty_ns;
376
377	return 0;
378}
379
380static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
381			      int duty_ns, int period_ns)
382{
383	return __pwm_samsung_config(chip, pwm, duty_ns, period_ns, false);
384}
385
386static void pwm_samsung_set_invert(struct samsung_pwm_chip *our_chip,
387				   unsigned int channel, bool invert)
388{
389	unsigned int tcon_chan = to_tcon_channel(channel);
390	unsigned long flags;
391	u32 tcon;
392
393	spin_lock_irqsave(&samsung_pwm_lock, flags);
394
395	tcon = readl(our_chip->base + REG_TCON);
396
397	if (invert) {
398		our_chip->inverter_mask |= BIT(channel);
399		tcon |= TCON_INVERT(tcon_chan);
400	} else {
401		our_chip->inverter_mask &= ~BIT(channel);
402		tcon &= ~TCON_INVERT(tcon_chan);
403	}
404
405	writel(tcon, our_chip->base + REG_TCON);
406
407	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
408}
409
410static int pwm_samsung_set_polarity(struct pwm_chip *chip,
411				    struct pwm_device *pwm,
412				    enum pwm_polarity polarity)
413{
414	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
415	bool invert = (polarity == PWM_POLARITY_NORMAL);
416
417	/* Inverted means normal in the hardware. */
418	pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
419
420	return 0;
421}
422
423static int pwm_samsung_apply(struct pwm_chip *chip, struct pwm_device *pwm,
424			     const struct pwm_state *state)
425{
426	int err, enabled = pwm->state.enabled;
427
428	if (state->polarity != pwm->state.polarity) {
429		if (enabled) {
430			pwm_samsung_disable(chip, pwm);
431			enabled = false;
432		}
433
434		err = pwm_samsung_set_polarity(chip, pwm, state->polarity);
435		if (err)
436			return err;
437	}
438
439	if (!state->enabled) {
440		if (enabled)
441			pwm_samsung_disable(chip, pwm);
442
443		return 0;
444	}
445
446	/*
447	 * We currently avoid using 64bit arithmetic by using the
448	 * fact that anything faster than 1Hz is easily representable
449	 * by 32bits.
450	 */
451	if (state->period > NSEC_PER_SEC)
452		return -ERANGE;
453
454	err = pwm_samsung_config(chip, pwm, state->duty_cycle, state->period);
455	if (err)
456		return err;
457
458	if (!pwm->state.enabled)
459		err = pwm_samsung_enable(chip, pwm);
460
461	return err;
462}
463
464static const struct pwm_ops pwm_samsung_ops = {
465	.request	= pwm_samsung_request,
466	.apply		= pwm_samsung_apply,
467};
468
469#ifdef CONFIG_OF
470static const struct samsung_pwm_variant s3c24xx_variant = {
471	.bits		= 16,
472	.div_base	= 1,
473	.has_tint_cstat	= false,
474	.tclk_mask	= BIT(4),
475};
476
477static const struct samsung_pwm_variant s3c64xx_variant = {
478	.bits		= 32,
479	.div_base	= 0,
480	.has_tint_cstat	= true,
481	.tclk_mask	= BIT(7) | BIT(6) | BIT(5),
482};
483
484static const struct samsung_pwm_variant s5p64x0_variant = {
485	.bits		= 32,
486	.div_base	= 0,
487	.has_tint_cstat	= true,
488	.tclk_mask	= 0,
489};
490
491static const struct samsung_pwm_variant s5pc100_variant = {
492	.bits		= 32,
493	.div_base	= 0,
494	.has_tint_cstat	= true,
495	.tclk_mask	= BIT(5),
496};
497
498static 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 },
504	{},
505};
506MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
507
508static int pwm_samsung_parse_dt(struct pwm_chip *chip)
509{
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;
 
 
513	u32 val;
514
515	match = of_match_node(samsung_pwm_matches, np);
516	if (!match)
517		return -ENODEV;
518
519	memcpy(&our_chip->variant, match->data, sizeof(our_chip->variant));
520
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",
525								__func__);
526			continue;
527		}
528		our_chip->variant.output_mask |= BIT(val);
529	}
530
531	return 0;
532}
533#else
534static int pwm_samsung_parse_dt(struct pwm_chip *chip)
535{
536	return -ENODEV;
537}
538#endif
539
540static int pwm_samsung_probe(struct platform_device *pdev)
541{
542	struct device *dev = &pdev->dev;
543	struct samsung_pwm_chip *our_chip;
544	struct pwm_chip *chip;
545	unsigned int chan;
546	int ret;
547
548	chip = devm_pwmchip_alloc(&pdev->dev, SAMSUNG_PWM_NUM, sizeof(*our_chip));
549	if (IS_ERR(chip))
550		return PTR_ERR(chip);
551	our_chip = to_samsung_pwm_chip(chip);
552
553	chip->ops = &pwm_samsung_ops;
 
554	our_chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
555
556	if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
557		ret = pwm_samsung_parse_dt(chip);
558		if (ret)
559			return ret;
560	} else {
561		if (!pdev->dev.platform_data)
562			return dev_err_probe(&pdev->dev, -EINVAL,
563					     "no platform data specified\n");
 
564
565		memcpy(&our_chip->variant, pdev->dev.platform_data,
566							sizeof(our_chip->variant));
567	}
568
569	our_chip->base = devm_platform_ioremap_resource(pdev, 0);
570	if (IS_ERR(our_chip->base))
571		return PTR_ERR(our_chip->base);
572
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");
 
 
 
 
 
 
 
577
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);
581
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");
585
586	platform_set_drvdata(pdev, chip);
587
588	ret = devm_pwmchip_add(&pdev->dev, chip);
589	if (ret < 0)
590		return dev_err_probe(dev, ret, "failed to register PWM chip\n");
 
 
 
591
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);
596
597	return 0;
598}
599
 
 
 
 
 
 
 
 
 
600static int pwm_samsung_resume(struct device *dev)
601{
602	struct pwm_chip *chip = dev_get_drvdata(dev);
603	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
604	unsigned int i;
605
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];
609
610		if (!test_bit(PWMF_REQUESTED, &pwm->flags))
611			continue;
612
613		if (our_chip->variant.output_mask & BIT(i))
614			pwm_samsung_set_invert(our_chip, i,
615					our_chip->inverter_mask & BIT(i));
616
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);
622		}
623
624		if (our_chip->disabled_mask & BIT(i))
625			pwm_samsung_disable(chip, pwm);
626		else
627			pwm_samsung_enable(chip, pwm);
628	}
629
630	return 0;
631}
632
633static DEFINE_SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume);
634
635static struct platform_driver pwm_samsung_driver = {
636	.driver		= {
637		.name	= "samsung-pwm",
638		.pm	= pm_ptr(&pwm_samsung_pm_ops),
639		.of_match_table = of_match_ptr(samsung_pwm_matches),
640	},
641	.probe		= pwm_samsung_probe,
 
642};
643module_platform_driver(pwm_samsung_driver);
644
645MODULE_DESCRIPTION("Samsung Pulse Width Modulator driver");
646MODULE_LICENSE("GPL");
647MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
648MODULE_ALIAS("platform:samsung-pwm");
v6.8
  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 * @channel:		per channel driver data
 81 */
 82struct samsung_pwm_chip {
 83	struct pwm_chip chip;
 84	struct samsung_pwm_variant variant;
 85	u8 inverter_mask;
 86	u8 disabled_mask;
 87
 88	void __iomem *base;
 89	struct clk *base_clk;
 90	struct clk *tclk0;
 91	struct clk *tclk1;
 92	struct samsung_pwm_channel channel[SAMSUNG_PWM_NUM];
 93};
 94
 95#ifndef CONFIG_CLKSRC_SAMSUNG_PWM
 96/*
 97 * PWM block is shared between pwm-samsung and samsung_pwm_timer drivers
 98 * and some registers need access synchronization. If both drivers are
 99 * compiled in, the spinlock is defined in the clocksource driver,
100 * otherwise following definition is used.
101 *
102 * Currently we do not need any more complex synchronization method
103 * because all the supported SoCs contain only one instance of the PWM
104 * IP. Should this change, both drivers will need to be modified to
105 * properly synchronize accesses to particular instances.
106 */
107static DEFINE_SPINLOCK(samsung_pwm_lock);
108#endif
109
110static inline
111struct samsung_pwm_chip *to_samsung_pwm_chip(struct pwm_chip *chip)
112{
113	return container_of(chip, struct samsung_pwm_chip, chip);
114}
115
116static inline unsigned int to_tcon_channel(unsigned int channel)
117{
118	/* TCON register has a gap of 4 bits (1 channel) after channel 0 */
119	return (channel == 0) ? 0 : (channel + 1);
120}
121
122static void __pwm_samsung_manual_update(struct samsung_pwm_chip *our_chip,
123				      struct pwm_device *pwm)
124{
125	unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
126	u32 tcon;
127
128	tcon = readl(our_chip->base + REG_TCON);
129	tcon |= TCON_MANUALUPDATE(tcon_chan);
130	writel(tcon, our_chip->base + REG_TCON);
131
132	tcon &= ~TCON_MANUALUPDATE(tcon_chan);
133	writel(tcon, our_chip->base + REG_TCON);
134}
135
136static void pwm_samsung_set_divisor(struct samsung_pwm_chip *our_chip,
137				    unsigned int channel, u8 divisor)
138{
139	u8 shift = TCFG1_SHIFT(channel);
140	unsigned long flags;
141	u32 reg;
142	u8 bits;
143
144	bits = (fls(divisor) - 1) - our_chip->variant.div_base;
145
146	spin_lock_irqsave(&samsung_pwm_lock, flags);
147
148	reg = readl(our_chip->base + REG_TCFG1);
149	reg &= ~(TCFG1_MUX_MASK << shift);
150	reg |= bits << shift;
151	writel(reg, our_chip->base + REG_TCFG1);
152
153	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
154}
155
156static int pwm_samsung_is_tdiv(struct samsung_pwm_chip *our_chip, unsigned int chan)
157{
158	struct samsung_pwm_variant *variant = &our_chip->variant;
159	u32 reg;
160
161	reg = readl(our_chip->base + REG_TCFG1);
162	reg >>= TCFG1_SHIFT(chan);
163	reg &= TCFG1_MUX_MASK;
164
165	return (BIT(reg) & variant->tclk_mask) == 0;
166}
167
168static unsigned long pwm_samsung_get_tin_rate(struct samsung_pwm_chip *our_chip,
169					      unsigned int chan)
170{
171	unsigned long rate;
172	u32 reg;
173
174	rate = clk_get_rate(our_chip->base_clk);
175
176	reg = readl(our_chip->base + REG_TCFG0);
177	if (chan >= 2)
178		reg >>= TCFG0_PRESCALER1_SHIFT;
179	reg &= TCFG0_PRESCALER_MASK;
180
181	return rate / (reg + 1);
182}
183
184static unsigned long pwm_samsung_calc_tin(struct samsung_pwm_chip *our_chip,
185					  unsigned int chan, unsigned long freq)
186{
 
187	struct samsung_pwm_variant *variant = &our_chip->variant;
188	unsigned long rate;
189	struct clk *clk;
190	u8 div;
191
192	if (!pwm_samsung_is_tdiv(our_chip, chan)) {
193		clk = (chan < 2) ? our_chip->tclk0 : our_chip->tclk1;
194		if (!IS_ERR(clk)) {
195			rate = clk_get_rate(clk);
196			if (rate)
197				return rate;
198		}
199
200		dev_warn(our_chip->chip.dev,
201			"tclk of PWM %d is inoperational, using tdiv\n", chan);
202	}
203
204	rate = pwm_samsung_get_tin_rate(our_chip, chan);
205	dev_dbg(our_chip->chip.dev, "tin parent at %lu\n", rate);
206
207	/*
208	 * Compare minimum PWM frequency that can be achieved with possible
209	 * divider settings and choose the lowest divisor that can generate
210	 * frequencies lower than requested.
211	 */
212	if (variant->bits < 32) {
213		/* Only for s3c24xx */
214		for (div = variant->div_base; div < 4; ++div)
215			if ((rate >> (variant->bits + div)) < freq)
216				break;
217	} else {
218		/*
219		 * Other variants have enough counter bits to generate any
220		 * requested rate, so no need to check higher divisors.
221		 */
222		div = variant->div_base;
223	}
224
225	pwm_samsung_set_divisor(our_chip, chan, BIT(div));
226
227	return rate >> div;
228}
229
230static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
231{
232	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
233
234	if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) {
235		dev_warn(chip->dev,
236			"tried to request PWM channel %d without output\n",
237			pwm->hwpwm);
238		return -EINVAL;
239	}
240
241	memset(&our_chip->channel[pwm->hwpwm], 0, sizeof(our_chip->channel[pwm->hwpwm]));
242
243	return 0;
244}
245
246static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
247{
248	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
249	unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
250	unsigned long flags;
251	u32 tcon;
252
253	spin_lock_irqsave(&samsung_pwm_lock, flags);
254
255	tcon = readl(our_chip->base + REG_TCON);
256
257	tcon &= ~TCON_START(tcon_chan);
258	tcon |= TCON_MANUALUPDATE(tcon_chan);
259	writel(tcon, our_chip->base + REG_TCON);
260
261	tcon &= ~TCON_MANUALUPDATE(tcon_chan);
262	tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
263	writel(tcon, our_chip->base + REG_TCON);
264
265	our_chip->disabled_mask &= ~BIT(pwm->hwpwm);
266
267	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
268
269	return 0;
270}
271
272static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
273{
274	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
275	unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
276	unsigned long flags;
277	u32 tcon;
278
279	spin_lock_irqsave(&samsung_pwm_lock, flags);
280
281	tcon = readl(our_chip->base + REG_TCON);
282	tcon &= ~TCON_AUTORELOAD(tcon_chan);
283	writel(tcon, our_chip->base + REG_TCON);
284
285	/*
286	 * In case the PWM is at 100% duty cycle, force a manual
287	 * update to prevent the signal from staying high.
288	 */
289	if (readl(our_chip->base + REG_TCMPB(pwm->hwpwm)) == (u32)-1U)
290		__pwm_samsung_manual_update(our_chip, pwm);
291
292	our_chip->disabled_mask |= BIT(pwm->hwpwm);
293
294	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
295}
296
297static void pwm_samsung_manual_update(struct samsung_pwm_chip *our_chip,
298				      struct pwm_device *pwm)
299{
300	unsigned long flags;
301
302	spin_lock_irqsave(&samsung_pwm_lock, flags);
303
304	__pwm_samsung_manual_update(our_chip, pwm);
305
306	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
307}
308
309static int __pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
310				int duty_ns, int period_ns, bool force_period)
311{
312	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
313	struct samsung_pwm_channel *chan = &our_chip->channel[pwm->hwpwm];
314	u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
315
316	tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
317	oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
318
319	/* We need tick count for calculation, not last tick. */
320	++tcnt;
321
322	/* Check to see if we are changing the clock rate of the PWM. */
323	if (chan->period_ns != period_ns || force_period) {
324		unsigned long tin_rate;
325		u32 period;
326
327		period = NSEC_PER_SEC / period_ns;
328
329		dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n",
330						duty_ns, period_ns, period);
331
332		tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period);
333
334		dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate);
335
336		tin_ns = NSEC_PER_SEC / tin_rate;
337		tcnt = period_ns / tin_ns;
338	}
339
340	/* Period is too short. */
341	if (tcnt <= 1)
342		return -ERANGE;
343
344	/* Note that counters count down. */
345	tcmp = duty_ns / tin_ns;
346
347	/* 0% duty is not available */
348	if (!tcmp)
349		++tcmp;
350
351	tcmp = tcnt - tcmp;
352
353	/* Decrement to get tick numbers, instead of tick counts. */
354	--tcnt;
355	/* -1UL will give 100% duty. */
356	--tcmp;
357
358	dev_dbg(our_chip->chip.dev,
359				"tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
360
361	/* Update PWM registers. */
362	writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
363	writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
364
365	/*
366	 * In case the PWM is currently at 100% duty cycle, force a manual
367	 * update to prevent the signal staying high if the PWM is disabled
368	 * shortly afer this update (before it autoreloaded the new values).
369	 */
370	if (oldtcmp == (u32) -1) {
371		dev_dbg(our_chip->chip.dev, "Forcing manual update");
372		pwm_samsung_manual_update(our_chip, pwm);
373	}
374
375	chan->period_ns = period_ns;
376	chan->tin_ns = tin_ns;
377	chan->duty_ns = duty_ns;
378
379	return 0;
380}
381
382static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
383			      int duty_ns, int period_ns)
384{
385	return __pwm_samsung_config(chip, pwm, duty_ns, period_ns, false);
386}
387
388static void pwm_samsung_set_invert(struct samsung_pwm_chip *our_chip,
389				   unsigned int channel, bool invert)
390{
391	unsigned int tcon_chan = to_tcon_channel(channel);
392	unsigned long flags;
393	u32 tcon;
394
395	spin_lock_irqsave(&samsung_pwm_lock, flags);
396
397	tcon = readl(our_chip->base + REG_TCON);
398
399	if (invert) {
400		our_chip->inverter_mask |= BIT(channel);
401		tcon |= TCON_INVERT(tcon_chan);
402	} else {
403		our_chip->inverter_mask &= ~BIT(channel);
404		tcon &= ~TCON_INVERT(tcon_chan);
405	}
406
407	writel(tcon, our_chip->base + REG_TCON);
408
409	spin_unlock_irqrestore(&samsung_pwm_lock, flags);
410}
411
412static int pwm_samsung_set_polarity(struct pwm_chip *chip,
413				    struct pwm_device *pwm,
414				    enum pwm_polarity polarity)
415{
416	struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
417	bool invert = (polarity == PWM_POLARITY_NORMAL);
418
419	/* Inverted means normal in the hardware. */
420	pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
421
422	return 0;
423}
424
425static int pwm_samsung_apply(struct pwm_chip *chip, struct pwm_device *pwm,
426			     const struct pwm_state *state)
427{
428	int err, enabled = pwm->state.enabled;
429
430	if (state->polarity != pwm->state.polarity) {
431		if (enabled) {
432			pwm_samsung_disable(chip, pwm);
433			enabled = false;
434		}
435
436		err = pwm_samsung_set_polarity(chip, pwm, state->polarity);
437		if (err)
438			return err;
439	}
440
441	if (!state->enabled) {
442		if (enabled)
443			pwm_samsung_disable(chip, pwm);
444
445		return 0;
446	}
447
448	/*
449	 * We currently avoid using 64bit arithmetic by using the
450	 * fact that anything faster than 1Hz is easily representable
451	 * by 32bits.
452	 */
453	if (state->period > NSEC_PER_SEC)
454		return -ERANGE;
455
456	err = pwm_samsung_config(chip, pwm, state->duty_cycle, state->period);
457	if (err)
458		return err;
459
460	if (!pwm->state.enabled)
461		err = pwm_samsung_enable(chip, pwm);
462
463	return err;
464}
465
466static const struct pwm_ops pwm_samsung_ops = {
467	.request	= pwm_samsung_request,
468	.apply		= pwm_samsung_apply,
469};
470
471#ifdef CONFIG_OF
472static const struct samsung_pwm_variant s3c24xx_variant = {
473	.bits		= 16,
474	.div_base	= 1,
475	.has_tint_cstat	= false,
476	.tclk_mask	= BIT(4),
477};
478
479static const struct samsung_pwm_variant s3c64xx_variant = {
480	.bits		= 32,
481	.div_base	= 0,
482	.has_tint_cstat	= true,
483	.tclk_mask	= BIT(7) | BIT(6) | BIT(5),
484};
485
486static const struct samsung_pwm_variant s5p64x0_variant = {
487	.bits		= 32,
488	.div_base	= 0,
489	.has_tint_cstat	= true,
490	.tclk_mask	= 0,
491};
492
493static const struct samsung_pwm_variant s5pc100_variant = {
494	.bits		= 32,
495	.div_base	= 0,
496	.has_tint_cstat	= true,
497	.tclk_mask	= BIT(5),
498};
499
500static const struct of_device_id samsung_pwm_matches[] = {
501	{ .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
502	{ .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
503	{ .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
504	{ .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
505	{ .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
506	{},
507};
508MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
509
510static int pwm_samsung_parse_dt(struct samsung_pwm_chip *our_chip)
511{
512	struct device_node *np = our_chip->chip.dev->of_node;
 
513	const struct of_device_id *match;
514	struct property *prop;
515	const __be32 *cur;
516	u32 val;
517
518	match = of_match_node(samsung_pwm_matches, np);
519	if (!match)
520		return -ENODEV;
521
522	memcpy(&our_chip->variant, match->data, sizeof(our_chip->variant));
523
524	of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
525		if (val >= SAMSUNG_PWM_NUM) {
526			dev_err(our_chip->chip.dev,
527				"%s: invalid channel index in samsung,pwm-outputs property\n",
528								__func__);
529			continue;
530		}
531		our_chip->variant.output_mask |= BIT(val);
532	}
533
534	return 0;
535}
536#else
537static int pwm_samsung_parse_dt(struct samsung_pwm_chip *our_chip)
538{
539	return -ENODEV;
540}
541#endif
542
543static int pwm_samsung_probe(struct platform_device *pdev)
544{
545	struct device *dev = &pdev->dev;
546	struct samsung_pwm_chip *our_chip;
 
547	unsigned int chan;
548	int ret;
549
550	our_chip = devm_kzalloc(&pdev->dev, sizeof(*our_chip), GFP_KERNEL);
551	if (our_chip == NULL)
552		return -ENOMEM;
553
554	our_chip->chip.dev = &pdev->dev;
555	our_chip->chip.ops = &pwm_samsung_ops;
556	our_chip->chip.npwm = SAMSUNG_PWM_NUM;
557	our_chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
558
559	if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
560		ret = pwm_samsung_parse_dt(our_chip);
561		if (ret)
562			return ret;
563	} else {
564		if (!pdev->dev.platform_data) {
565			dev_err(&pdev->dev, "no platform data specified\n");
566			return -EINVAL;
567		}
568
569		memcpy(&our_chip->variant, pdev->dev.platform_data,
570							sizeof(our_chip->variant));
571	}
572
573	our_chip->base = devm_platform_ioremap_resource(pdev, 0);
574	if (IS_ERR(our_chip->base))
575		return PTR_ERR(our_chip->base);
576
577	our_chip->base_clk = devm_clk_get(&pdev->dev, "timers");
578	if (IS_ERR(our_chip->base_clk)) {
579		dev_err(dev, "failed to get timer base clk\n");
580		return PTR_ERR(our_chip->base_clk);
581	}
582
583	ret = clk_prepare_enable(our_chip->base_clk);
584	if (ret < 0) {
585		dev_err(dev, "failed to enable base clock\n");
586		return ret;
587	}
588
589	for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
590		if (our_chip->variant.output_mask & BIT(chan))
591			pwm_samsung_set_invert(our_chip, chan, true);
592
593	/* Following clocks are optional. */
594	our_chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
595	our_chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
596
597	platform_set_drvdata(pdev, our_chip);
598
599	ret = pwmchip_add(&our_chip->chip);
600	if (ret < 0) {
601		dev_err(dev, "failed to register PWM chip\n");
602		clk_disable_unprepare(our_chip->base_clk);
603		return ret;
604	}
605
606	dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
607		clk_get_rate(our_chip->base_clk),
608		!IS_ERR(our_chip->tclk0) ? clk_get_rate(our_chip->tclk0) : 0,
609		!IS_ERR(our_chip->tclk1) ? clk_get_rate(our_chip->tclk1) : 0);
610
611	return 0;
612}
613
614static void pwm_samsung_remove(struct platform_device *pdev)
615{
616	struct samsung_pwm_chip *our_chip = platform_get_drvdata(pdev);
617
618	pwmchip_remove(&our_chip->chip);
619
620	clk_disable_unprepare(our_chip->base_clk);
621}
622
623static int pwm_samsung_resume(struct device *dev)
624{
625	struct samsung_pwm_chip *our_chip = dev_get_drvdata(dev);
626	struct pwm_chip *chip = &our_chip->chip;
627	unsigned int i;
628
629	for (i = 0; i < SAMSUNG_PWM_NUM; i++) {
630		struct pwm_device *pwm = &chip->pwms[i];
631		struct samsung_pwm_channel *chan = &our_chip->channel[i];
632
633		if (!test_bit(PWMF_REQUESTED, &pwm->flags))
634			continue;
635
636		if (our_chip->variant.output_mask & BIT(i))
637			pwm_samsung_set_invert(our_chip, i,
638					our_chip->inverter_mask & BIT(i));
639
640		if (chan->period_ns) {
641			__pwm_samsung_config(chip, pwm, chan->duty_ns,
642					     chan->period_ns, true);
643			/* needed to make PWM disable work on Odroid-XU3 */
644			pwm_samsung_manual_update(our_chip, pwm);
645		}
646
647		if (our_chip->disabled_mask & BIT(i))
648			pwm_samsung_disable(chip, pwm);
649		else
650			pwm_samsung_enable(chip, pwm);
651	}
652
653	return 0;
654}
655
656static DEFINE_SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume);
657
658static struct platform_driver pwm_samsung_driver = {
659	.driver		= {
660		.name	= "samsung-pwm",
661		.pm	= pm_ptr(&pwm_samsung_pm_ops),
662		.of_match_table = of_match_ptr(samsung_pwm_matches),
663	},
664	.probe		= pwm_samsung_probe,
665	.remove_new	= pwm_samsung_remove,
666};
667module_platform_driver(pwm_samsung_driver);
668
 
669MODULE_LICENSE("GPL");
670MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
671MODULE_ALIAS("platform:samsung-pwm");