summaryrefstree
path: root/kernel/drivers/pwm/pwm-samsung.c
blob: d7af903ff2b508c792d9b2dafed876604ee2feba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/* drivers/pwm/pwm-samsung.c
 *
 * Copyright (c) 2007 Ben Dooks
 * Copyright (c) 2008 Simtec Electronics
 *	Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
 *
 * S3C series PWM device core
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
*/

#define pr_fmt(fmt) "pwm-samsung: " fmt

#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/pwm.h>
#include <linux/of.h>
#include <linux/spinlock.h>

#include <mach/exynos-pm.h>
#define NPWM				4

#define REG_TCFG0			0x00
#define REG_TCFG1			0x04
#define REG_TCON			0x08
#define REG_TINT_CSTAT			0x44

#define REG_TCNTB(chan)			(0x0c + 12 * (chan))
#define REG_TCMPB(chan)			(0x10 + 12 * (chan))

#define to_s3c_chip(chip)		container_of(chip, struct s3c_chip, chip)

#define pwm_dbg(_pwm, msg...)		dev_dbg(&(_pwm)->pdev->dev, msg)

#define pwm_tcon_start(pwm)		(1 << (pwm->tcon_base + 0))
#define pwm_tcon_invert(pwm)		(1 << (pwm->tcon_base + 2))
#define pwm_tcon_autoreload(pwm)	(1 << (pwm->tcon_base + 3))
#define pwm_tcon_manulupdate(pwm)	(1 << (pwm->tcon_base + 1))

#define pwm_is_s3c24xx(s3c)		(s3c->pwm_type == S3C24XX_PWM)

#define NS_IN_HZ			(1000000000UL)

enum duty_cycle {
	DUTY_CYCLE_ZERO,
	DUTY_CYCLE_PULSE,
	DUTY_CYCLE_FULL,
};

enum s3c_pwm_type {
	S3C24XX_PWM = 0,
	S3C64XX_PWM,
};

struct s3c_pwm_device {
	struct clk		*clk_div;
	struct clk		*clk_tin;

	unsigned int		period_ns;
	unsigned int		duty_ns;

	unsigned char		tcon_base;
	unsigned char		running;
	unsigned char		requested;
	unsigned char		pwm_id;

	enum duty_cycle		duty_cycle;
	struct pwm_device	*pwm;
};

struct s3c_chip {
	struct platform_device	*pdev;
	struct clk		*clk;
	void __iomem		*reg_base;
	struct pwm_chip		chip;
	struct s3c_pwm_device	*s3c_pwm[NPWM];
	unsigned int		pwm_type;
	unsigned int		reg_tcfg0;
	unsigned int		clk_ctrl;
	unsigned int		need_hw_init;
};

static DEFINE_SPINLOCK(pwm_spinlock);
static int pwm_enable_cnt = 0;
static struct s3c_chip *g_s3c;

static void s3c_pwm_init(struct s3c_chip *s3c, struct s3c_pwm_device *s3c_pwm);
static void s3c_pwm_save(struct s3c_chip *s3c);
static void s3c_pwm_restore(struct s3c_chip *s3c);

static inline int pwm_is_tdiv(struct s3c_pwm_device *s3c_pwm)
{
	return clk_get_parent(s3c_pwm->clk_tin) == s3c_pwm->clk_div;
}

int pwm_check_enable_cnt(void)
{
	return pwm_enable_cnt;
}
EXPORT_SYMBOL_GPL(pwm_check_enable_cnt);

static int s3c_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct s3c_chip *s3c = to_s3c_chip(chip);
	struct s3c_pwm_device *s3c_pwm = pwm_get_chip_data(pwm);
	void __iomem *reg_base = s3c->reg_base;
	unsigned long flags;
	unsigned long tcon;

	spin_lock_irqsave(&pwm_spinlock, flags);

	if (!pwm_enable_cnt && s3c->clk_ctrl)
		clk_enable(s3c->clk);

	if (s3c->need_hw_init)
		s3c_pwm_restore(s3c);

	if (pwm_is_s3c24xx(s3c)) {
		tcon = __raw_readl(reg_base + REG_TCON);
		tcon |= pwm_tcon_start(s3c_pwm);
		__raw_writel(tcon, reg_base + REG_TCON);
	} else {
		tcon = __raw_readl(reg_base + REG_TCON);
		if (!(tcon & pwm_tcon_start(s3c_pwm))) {
			tcon |= pwm_tcon_manulupdate(s3c_pwm);
			__raw_writel(tcon, reg_base + REG_TCON);

			tcon &= ~pwm_tcon_manulupdate(s3c_pwm);
			if (s3c_pwm->duty_cycle == DUTY_CYCLE_ZERO)
				tcon &= ~pwm_tcon_autoreload(s3c_pwm);
			else
				tcon |= pwm_tcon_autoreload(s3c_pwm);
			tcon |= pwm_tcon_start(s3c_pwm);
			__raw_writel(tcon, reg_base + REG_TCON);
		} else if (!(tcon & pwm_tcon_autoreload(s3c_pwm)) &&
			   s3c_pwm->duty_cycle != DUTY_CYCLE_ZERO) {
			tcon |= pwm_tcon_manulupdate(s3c_pwm);
			__raw_writel(tcon, reg_base + REG_TCON);

			tcon &= ~pwm_tcon_manulupdate(s3c_pwm);
			tcon |= pwm_tcon_autoreload(s3c_pwm);
			__raw_writel(tcon, reg_base + REG_TCON);
		}
	}

	s3c_pwm->running = 1;
	pwm_enable_cnt++;

	spin_unlock_irqrestore(&pwm_spinlock, flags);
	return 0;
}

static void s3c_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct s3c_chip *s3c = to_s3c_chip(chip);
	struct s3c_pwm_device *s3c_pwm = pwm_get_chip_data(pwm);
	void __iomem *reg_base = s3c->reg_base;
	unsigned long flags;
	unsigned long tcon;

	spin_lock_irqsave(&pwm_spinlock, flags);

	if (s3c->need_hw_init)
		s3c_pwm_restore(s3c);

	if (pwm_is_s3c24xx(s3c)) {
		tcon = __raw_readl(reg_base + REG_TCON);
		tcon &= ~pwm_tcon_start(s3c_pwm);
		__raw_writel(tcon, reg_base + REG_TCON);
	} else {
		tcon = __raw_readl(reg_base + REG_TCON);
		tcon &= ~pwm_tcon_autoreload(s3c_pwm);
		__raw_writel(tcon, reg_base + REG_TCON);
	}

	s3c_pwm->running = 0;
	pwm_enable_cnt--;
	if (!pwm_enable_cnt && s3c->clk_ctrl)
		clk_disable(s3c->clk);

	spin_unlock_irqrestore(&pwm_spinlock, flags);
}

static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
{
	struct s3c_pwm_device *s3c_pwm = pwm_get_chip_data(pwm);
	unsigned long tin_parent_rate;
	unsigned int div;

	tin_parent_rate = clk_get_rate(clk_get_parent(s3c_pwm->clk_div));
	clk_set_rate(clk_get_parent(s3c_pwm->clk_div),tin_parent_rate);

	for (div = 2; div <= 16; div *= 2) {
		if ((tin_parent_rate / (div << 16)) < freq)
			return tin_parent_rate / div;
	}

	return tin_parent_rate / 16;
}

static int s3c_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
		int duty_ns, int period_ns)
{
	struct s3c_chip *s3c = to_s3c_chip(chip);
	struct s3c_pwm_device *s3c_pwm = pwm_get_chip_data(pwm);
	void __iomem *reg_base = s3c->reg_base;
	unsigned long tin_rate;
	unsigned long tin_ns;
	unsigned long period;
	unsigned long flags;
	unsigned long tcon;
	unsigned long tcnt;
	long tcmp;
	enum duty_cycle duty_cycle;
	unsigned int id = pwm->pwm;
	int ret = 0;

	/* We currently avoid using 64bit arithmetic by using the
	 * fact that anything faster than 1Hz is easily representable
	 * by 32bits. */

	if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
		return -ERANGE;

	if (duty_ns > period_ns)
		return -EINVAL;

	if (period_ns == s3c_pwm->period_ns &&
	    duty_ns == s3c_pwm->duty_ns)
		return 0;

	period = NS_IN_HZ / period_ns;

	clk_enable(s3c->clk);
	/* Check to see if we are changing the clock rate of the PWM */
	if (s3c_pwm->period_ns != period_ns && pwm_is_tdiv(s3c_pwm)) {
		tin_rate = pwm_calc_tin(pwm, period);
		clk_set_rate(s3c_pwm->clk_div, tin_rate);
		tin_rate = clk_get_rate(s3c_pwm->clk_div);
		s3c_pwm->period_ns = period_ns;
		pwm_dbg(s3c, "tin_rate=%lu\n", tin_rate);
	} else {
		tin_rate = clk_get_rate(s3c_pwm->clk_tin);
	}

	if(!tin_rate) {
		ret = -EFAULT;
		goto out;
	}

	/* Note, counters count down */
	tin_ns = NS_IN_HZ / tin_rate;

	tcnt = DIV_ROUND_CLOSEST(period_ns, tin_ns);
	tcmp = DIV_ROUND_CLOSEST(duty_ns, tin_ns);

	if (tcnt <= 1) {
		/* Too small to generate a pulse */
		ret = -ERANGE;
		goto out;
	}

	pwm_dbg(s3c, "duty_ns=%d, period_ns=%d (%lu)\n",
		duty_ns, period_ns, period);

	if (tcmp == 0)
		duty_cycle = DUTY_CYCLE_ZERO;
	else if (tcmp == tcnt)
		duty_cycle = DUTY_CYCLE_FULL;
	else
		duty_cycle = DUTY_CYCLE_PULSE;

	tcmp = tcnt - tcmp;
	/* the pwm hw only checks the compare register after a decrement,
	   so the pin never toggles if tcmp = tcnt */
	if (tcmp == tcnt)
		tcmp--;

	/*
	 * PWM counts 1 hidden tick at the end of each period on S3C64XX and
	 * EXYNOS series, so tcmp and tcnt should be subtracted 1.
	 */
	if (!pwm_is_s3c24xx(s3c)) {
		tcnt--;
		/*
		 * tcmp can be -1. It appears 100% duty cycle and PWM never
		 * toggles when TCMPB is set to 0xFFFFFFFF (-1).
		 */
		tcmp--;
	}

	pwm_dbg(s3c, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);

	/* Update the PWM register block. */
	spin_lock_irqsave(&pwm_spinlock, flags);
	if (s3c->need_hw_init)
		s3c_pwm_restore(s3c);

	__raw_writel(tcmp, reg_base + REG_TCMPB(id));
	__raw_writel(tcnt, reg_base + REG_TCNTB(id));

	if (pwm_is_s3c24xx(s3c)) {
		tcon = __raw_readl(reg_base + REG_TCON);
		tcon |= pwm_tcon_manulupdate(s3c_pwm);
		tcon |= pwm_tcon_autoreload(s3c_pwm);
		__raw_writel(tcon, reg_base + REG_TCON);

		tcon &= ~pwm_tcon_manulupdate(s3c_pwm);
		__raw_writel(tcon, reg_base + REG_TCON);
	} else {
		tcon = __raw_readl(reg_base + REG_TCON);
		if (s3c_pwm->running == 1 &&
		    tcon & pwm_tcon_start(s3c_pwm) &&
		    s3c_pwm->duty_cycle != duty_cycle) {
			if (duty_cycle == DUTY_CYCLE_ZERO) {
				tcon |= pwm_tcon_manulupdate(s3c_pwm);
				__raw_writel(tcon, reg_base + REG_TCON);

				tcon &= ~pwm_tcon_manulupdate(s3c_pwm);
				tcon &= ~pwm_tcon_autoreload(s3c_pwm);
			} else {
				tcon |= pwm_tcon_autoreload(s3c_pwm);
			}
			__raw_writel(tcon, reg_base + REG_TCON);
		}
	}
	s3c_pwm->duty_ns = duty_ns;
	s3c_pwm->period_ns = period_ns;
	s3c_pwm->duty_cycle = duty_cycle;
	spin_unlock_irqrestore(&pwm_spinlock, flags);
out:
	clk_disable(s3c->clk);

	return ret;
}

static void s3c_pwm_init(struct s3c_chip *s3c, struct s3c_pwm_device *s3c_pwm)
{
	void __iomem *reg_base = s3c->reg_base;
	unsigned long tcon;
	unsigned int id = s3c_pwm->pwm_id;

	if (pwm_is_s3c24xx(s3c)) {
		tcon = __raw_readl(reg_base + REG_TCON);
		tcon |= pwm_tcon_invert(s3c_pwm);
		__raw_writel(tcon, reg_base + REG_TCON);
	} else {
		__raw_writel(0, reg_base + REG_TCMPB(id));
		__raw_writel(0, reg_base + REG_TCNTB(id));

		tcon = __raw_readl(reg_base + REG_TCON);
		tcon |= pwm_tcon_invert(s3c_pwm);
		tcon |= pwm_tcon_manulupdate(s3c_pwm);
		tcon &= ~pwm_tcon_autoreload(s3c_pwm);
		tcon &= ~pwm_tcon_start(s3c_pwm);
		__raw_writel(tcon, reg_base + REG_TCON);

		tcon &= ~pwm_tcon_manulupdate(s3c_pwm);
		__raw_writel(tcon, reg_base + REG_TCON);
	}
}

static int s3c_pwm_request(struct pwm_chip *chip,
				 struct pwm_device *pwm)
{
	struct s3c_chip *s3c = to_s3c_chip(chip);
	struct s3c_pwm_device *s3c_pwm;
	struct device *dev = chip->dev;
	void __iomem *reg_base = s3c->reg_base;
	unsigned char clk_tin_name[16];
	unsigned char clk_tdiv_name[16];
	static struct clk *clk_timers;
	unsigned int id = pwm->pwm;
	unsigned long flags;
	int ret;

	s3c_pwm = devm_kzalloc(chip->dev, sizeof(*s3c_pwm), GFP_KERNEL);
	if (!s3c_pwm)
		return -ENOMEM;

	/* calculate base of control bits in TCON */
	s3c_pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
	s3c_pwm->pwm_id = id;

	s3c_pwm->pwm = pwm;
	pwm_set_chip_data(pwm, s3c_pwm);

	snprintf(clk_tin_name, sizeof(clk_tin_name), "pwm-tin%d", id);
	clk_timers = devm_clk_get(dev, clk_tin_name);
	if (IS_ERR(clk_timers)) {
		dev_err(dev, "failed to get pwm tin clk\n");
		ret = PTR_ERR(clk_timers);
		goto err;
	}
	s3c_pwm->clk_tin = clk_timers;

	snprintf(clk_tdiv_name, sizeof(clk_tdiv_name), "pwm-tdiv%d", id);
	clk_timers = devm_clk_get(dev, clk_tdiv_name);
	if (IS_ERR(clk_timers)) {
		dev_err(dev, "failed to get pwm tdiv clk\n");
		ret = PTR_ERR(clk_timers);
		goto err;
	}
	s3c_pwm->clk_div = clk_timers;

	spin_lock_irqsave(&pwm_spinlock, flags);
	clk_enable(s3c->clk);
	s3c_pwm_init(s3c, s3c_pwm);
	clk_disable(s3c->clk);
	spin_unlock_irqrestore(&pwm_spinlock, flags);

	s3c->s3c_pwm[id] = s3c_pwm;

	pwm_dbg(s3c, "config bits %02x\n",
		(__raw_readl(reg_base + REG_TCON) >> s3c_pwm->tcon_base) & 0x0f);
	return 0;

err:
	devm_kfree(chip->dev, s3c_pwm);
	return ret;
}

static void s3c_pwm_free(struct pwm_chip *chip,
				struct pwm_device *pwm)
{
	struct s3c_chip *s3c = to_s3c_chip(chip);
	struct s3c_pwm_device *s3c_pwm = pwm_get_chip_data(pwm);
	unsigned int id = pwm->pwm;

	s3c->s3c_pwm[id] = NULL;
	devm_kfree(chip->dev, s3c_pwm);
}

static struct pwm_ops s3c_pwm_ops = {
	.request = s3c_pwm_request,
	.free = s3c_pwm_free,
	.enable = s3c_pwm_enable,
	.disable = s3c_pwm_disable,
	.config = s3c_pwm_config,
	.owner = THIS_MODULE,
};

static int s3c_pwm_clk_init(struct platform_device *pdev,
				struct s3c_chip *s3c)
{
	struct device *dev = &pdev->dev;
	static struct clk *clk_scaler[2];

	s3c->clk = devm_clk_get(dev, "gate_timers");
	if (IS_ERR(s3c->clk)) {
		pr_err("no parent clock\n");
		return -EINVAL;
	}

	clk_prepare_enable(s3c->clk);

	clk_scaler[0] = devm_clk_get(dev, "pwm-scaler0");
	clk_scaler[1] = devm_clk_get(dev, "pwm-scaler1");

	if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
		pr_err("failed to get scaler clocks\n");
		return -EINVAL;
	}

	return 0;
}

#ifdef CONFIG_OF
static struct of_device_id s3c_pwm_match[] = {
	{
		.compatible = "samsung,s3c2410-pwm",
		.data = (void *)S3C24XX_PWM,
	},
	{
		.compatible = "samsung,s3c6400-pwm",
		.data = (void *)S3C64XX_PWM,
	},
	{ }
};
#endif

static inline int s3c_pwm_get_driver_data(struct device *dev)
{
#ifdef CONFIG_OF
	if (dev->of_node) {
		const struct of_device_id *match;
		match = of_match_node(s3c_pwm_match, dev->of_node);
		return (long)match->data;
	}
#endif
	return 0;
}

#ifdef CONFIG_CPU_IDLE
static int pwm_samsung_notifier(struct notifier_block *nb,
				unsigned long cmd, void *v)
{
	switch (cmd) {
	case LPA_ENTER:
		if (!g_s3c->need_hw_init)
			s3c_pwm_save(g_s3c);
		break;
	case LPA_EXIT:
			g_s3c->need_hw_init = 1;
		break;
	}

	return NOTIFY_OK;
}

static struct notifier_block pwm_samsung_notifier_block = {
	.notifier_call = pwm_samsung_notifier,
};
#endif

static int s3c_pwm_probe(struct platform_device *pdev)
{
	static struct resource	*pwm_mem;
	struct device *dev = &pdev->dev;
	struct s3c_chip *s3c;
	int ret;

	pwm_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (pwm_mem == NULL) {
		dev_err(dev, "no memory resource specified\n");
		return -ENOENT;
	}

	s3c = devm_kzalloc(&pdev->dev, sizeof(*s3c), GFP_KERNEL);
	if (s3c == NULL) {
		dev_err(dev, "failed to allocate pwm_device\n");
		ret = -ENOMEM;
		return ret;
	}

	s3c->reg_base = devm_ioremap_resource(dev, pwm_mem);
	if (!s3c->reg_base) {
		dev_err(dev, "failed to map pwm registers\n");
		ret = PTR_ERR(s3c->reg_base);
		return ret;
	}

	ret = s3c_pwm_clk_init(pdev, s3c);
	if (ret < 0) {
		dev_err(dev, "failed to pwm clock init\n");
		return ret;
	}

	s3c->pdev = pdev;
	s3c->chip.dev = &pdev->dev;
	s3c->chip.ops = &s3c_pwm_ops;
	s3c->chip.of_xlate = of_pwm_xlate_with_flags;
	s3c->chip.of_pwm_n_cells = 3;
	s3c->chip.base = -1;
	s3c->chip.npwm = NPWM;
	s3c->pwm_type = s3c_pwm_get_driver_data(dev);
#ifdef CONFIG_OF
	if (dev->of_node)
		s3c->clk_ctrl = !!of_get_property(dev->of_node,
						"clock-control", NULL);
#endif
	if (s3c->clk_ctrl)
		clk_disable(s3c->clk);

	g_s3c = s3c;
	ret = pwmchip_add(&s3c->chip);
	if (ret < 0) {
		dev_err(dev, "failed to register pwm\n");
		return ret;
	}

	platform_set_drvdata(pdev, s3c);
	return 0;
}

static int s3c_pwm_remove(struct platform_device *pdev)
{
	struct s3c_chip *s3c = platform_get_drvdata(pdev);
	int err;

	err = pwmchip_remove(&s3c->chip);
	if (err < 0)
		return err;

	clk_disable_unprepare(s3c->clk);

	return 0;
}

static void s3c_pwm_save(struct s3c_chip *s3c)
{
	struct s3c_pwm_device *s3c_pwm;
	void __iomem *reg_base = s3c->reg_base;
	unsigned long tcon;
	unsigned char i;

	if (!pwm_enable_cnt && s3c->clk_ctrl && !s3c->need_hw_init)
		clk_enable(s3c->clk);

	if (!pwm_is_s3c24xx(s3c)) {
		for (i = 0; i < NPWM; i++) {
			if (s3c->s3c_pwm[i] == NULL)
				continue;
			s3c_pwm = s3c->s3c_pwm[i];
			if (s3c_pwm->running == 0) {
				tcon = __raw_readl(reg_base + REG_TCON);
				if (s3c_pwm->duty_cycle == DUTY_CYCLE_ZERO) {
					tcon |= pwm_tcon_manulupdate(s3c_pwm);
				} else if (s3c_pwm->duty_cycle == DUTY_CYCLE_FULL) {
					tcon &= pwm_tcon_invert(s3c_pwm);
					tcon |= pwm_tcon_manulupdate(s3c_pwm);
				}
				tcon &= ~pwm_tcon_start(s3c_pwm);
				__raw_writel(tcon, reg_base + REG_TCON);
			}
			/* No one preserve these values during suspend so reset them
			 * Otherwise driver leaves PWM unconfigured if same values
			 * passed to pwm_config
			 */
			s3c_pwm->duty_ns = -1;
			s3c_pwm->period_ns = -1;
		}
	}
	/* Save pwm registers*/
	if (pwm_enable_cnt)
	s3c->reg_tcfg0 = __raw_readl(s3c->reg_base + REG_TCFG0);

	clk_disable(s3c->clk);
}

static void s3c_pwm_restore(struct s3c_chip *s3c)
{
	struct s3c_pwm_device *s3c_pwm;
	unsigned char i;

	clk_enable(s3c->clk);

	/* Restore pwm registers*/
	if (pwm_enable_cnt)
	__raw_writel(s3c->reg_tcfg0, s3c->reg_base + REG_TCFG0);
	s3c->need_hw_init = 0;

	for (i = 0; i < NPWM; i++) {
		if (s3c->s3c_pwm[i] == NULL)
			continue;
		s3c_pwm = s3c->s3c_pwm[i];
		s3c_pwm_init(s3c, s3c_pwm);
	}
}

#ifdef CONFIG_PM_SLEEP
static int s3c_pwm_suspend(struct device *dev)
{
	struct s3c_chip *s3c = dev_get_drvdata(dev);

	if(!s3c->need_hw_init)
		s3c_pwm_save(s3c);

	return 0;
}

static int s3c_pwm_resume(struct device *dev)
{
	struct s3c_chip *s3c = dev_get_drvdata(dev);

	s3c_pwm_restore(s3c);

	if (!pwm_enable_cnt && s3c->clk_ctrl && !s3c->need_hw_init)
		clk_disable(s3c->clk);

	return 0;
}
#endif

static SIMPLE_DEV_PM_OPS(s3c_pwm_pm_ops, s3c_pwm_suspend,
			s3c_pwm_resume);

static struct platform_driver s3c_pwm_driver = {
	.driver		= {
		.name	= "s3c24xx-pwm",
		.owner	= THIS_MODULE,
		.pm	= &s3c_pwm_pm_ops,
#ifdef CONFIG_OF
		.of_match_table	= of_match_ptr(s3c_pwm_match),
#endif
	},
	.probe		= s3c_pwm_probe,
	.remove		= s3c_pwm_remove,
};

static int __init pwm_init(void)
{
	int ret;

#ifdef CONFIG_CPU_IDLE
	exynos_pm_register_notifier(&pwm_samsung_notifier_block);
#endif
	ret = platform_driver_register(&s3c_pwm_driver);
	if (ret)
		pr_err("failed to add pwm driver\n");

	return ret;
}

arch_initcall(pwm_init);