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
|
/*
* drivers/pwm/s3c24xx-pwm.c
*
* Copyright (C) 2010 by Digi International Inc.
* All rights reserved.
*
* 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, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/slab.h>
#include <mach/gpio.h>
#include <plat/pwm.h>
#include <plat/regs-timer.h>
#define MAX_TIMER_COUNT 0xffff
struct s3c24xx_pwm {
struct pwm_device pwm;
spinlock_t lock;
struct s3c24xx_pwm_pdata *pdata;
};
static struct clk *clk_scaler[2];
/* Prescaler of each timer */
static int timers_prescaler[5] = {0, 0, 1, 1, 1};
#define pwm_tcon_start(pch) (1 << (pch->tcon_base + 0))
#define pwm_tcon_invert(pch) (1 << (pch->tcon_base + 2))
#define pwm_tcon_autoreload(pch) (1 << (pch->tcon_base + 3))
#define pwm_tcon_manulupdate(pch) (1 << (pch->tcon_base + 1))
static inline int pwm_is_tdiv(struct s3c24xx_pwm_channel *pch)
{
return clk_get_parent(pch->clk) == pch->clk_div;
}
/* @XXX: Return NULL by failures */
static inline struct s3c24xx_pwm_channel *s3c24xx_pwm_to_channel(struct pwm_channel *p)
{
struct s3c24xx_pwm *np;
struct s3c24xx_pwm_channel *retval;
if (!p) {
pr_err("%s: NULL pointer passed!\n", __func__);
return NULL;
}
np = container_of(p->pwm, struct s3c24xx_pwm, pwm);
if (!np) {
pr_err("%s: NO np found!\n", __func__);
return NULL;
}
if (!np->pdata) {
pr_err("%s: NO platform data found!\n", __func__);
return NULL;
}
if (!np->pdata->channels)
pr_err("NO channels passed?\n");
retval = np->pdata->channels + p->chan;
if (!retval)
pr_err("%s: Invalid channel number passed?\n", __func__);
return retval;
}
/* Starts PWM output */
static inline void __s3c24xx_pwm_start(struct pwm_channel *p)
{
unsigned long flags;
unsigned long tcon;
struct s3c24xx_pwm_channel *pch = s3c24xx_pwm_to_channel(p);
local_irq_save(flags);
tcon = __raw_readl(S3C2410_TCON);
tcon |= pwm_tcon_start(pch);
__raw_writel(tcon, S3C2410_TCON);
local_irq_restore(flags);
pch->running = 1;
}
/* Stops PWM output */
static inline void __s3c24xx_pwm_stop(struct pwm_channel *p)
{
unsigned long flags;
unsigned long tcon;
struct s3c24xx_pwm_channel *pch = s3c24xx_pwm_to_channel(p);
local_irq_save(flags);
tcon = __raw_readl(S3C2410_TCON);
tcon &= ~pwm_tcon_start(pch);
__raw_writel(tcon, S3C2410_TCON);
local_irq_restore(flags);
pch->running = 0;
}
/* Toggles PWM output polarity */
static inline void
__s3c24xx_pwm_config_polarity(struct pwm_channel *p, int polarity)
{
unsigned long flags;
unsigned long tcon;
struct s3c24xx_pwm_channel *pch = s3c24xx_pwm_to_channel(p);
local_irq_save(flags);
tcon = __raw_readl(S3C2410_TCON);
if (polarity) {
/* Don't use inverter */
tcon &= ~pwm_tcon_invert(pch);
}
else {
/* Use inverter */
tcon |= pwm_tcon_invert(pch);
}
__raw_writel(tcon, S3C2410_TCON);
local_irq_restore(flags);
}
static unsigned long pwm_calc_tin(struct s3c24xx_pwm_channel *pch, unsigned long freq)
{
unsigned long tin_parent_rate;
unsigned int div;
tin_parent_rate = clk_get_rate(clk_get_parent(pch->clk_div));
for (div = 2; div <= 16; div *= 2) {
if ((tin_parent_rate / (div << 16)) < freq)
return tin_parent_rate / div;
}
return tin_parent_rate / 16;
}
#define NS_IN_HZ (1000000000UL)
/*
* Figure to understand PWM concepts
* ------------------------- -----
* | | |
* | | |
* -------- -----------------
*
* ^ ^ ^
* |-- DUTY (polarity=1) ---| |
* | |
* |----------- PERIOD-----------------------|
*
*/
/* Configures duty_ticks */
static inline int
__s3c24xx_pwm_config_duty_ticks(struct pwm_channel *p, unsigned long duty_ticks)
{
unsigned long tcon;
unsigned long tcmp;
struct s3c24xx_pwm_channel *pch = s3c24xx_pwm_to_channel(p);
unsigned long flags;
if (duty_ticks > p->period_ticks)
return -ERANGE;
p->duty_ticks = duty_ticks;
/* Update the PWM register block. */
local_irq_save(flags);
tcmp = duty_ticks;
__raw_writel(tcmp, S3C2410_TCMPB(pch->timer));
tcon = __raw_readl(S3C2410_TCON);
tcon |= pwm_tcon_manulupdate(pch);
tcon |= pwm_tcon_autoreload(pch);
__raw_writel(tcon, S3C2410_TCON);
tcon &= ~pwm_tcon_manulupdate(pch);
__raw_writel(tcon, S3C2410_TCON);
local_irq_restore(flags);
return 0;
}
/* Configures period_ticks */
static inline int
__s3c24xx_pwm_config_period_ticks(struct pwm_channel *p, unsigned long period_ticks)
{
unsigned long period_ns;
unsigned long period;
unsigned long tin_rate;
unsigned long tcon;
unsigned long tcnt;
unsigned long flags;
unsigned long pclk_rate;
struct s3c24xx_pwm_channel *pch = s3c24xx_pwm_to_channel(p);
period_ns = pwm_ticks_to_ns(p, period_ticks);
if (!period_ns)
return -ERANGE;
period = NS_IN_HZ / period_ns;
/* Adjust prescaler (only applicable to prescaler 0 -timers 0 and 1-) */
if (0 == timers_prescaler[pch->timer]) {
pclk_rate = clk_get_rate(clk_get_parent(clk_scaler[0]));
#ifdef CONFIG_S3C2443_PWM_PRESCALER_0
/* Set prescaler to fixed prescaler set by user */
clk_set_rate(clk_get_parent(pch->clk_div), pclk_rate/(CONFIG_S3C2443_PWM_PRESCALER_0+1));
#endif
}
if (pwm_is_tdiv(pch)) {
tin_rate = pwm_calc_tin(pch, period);
clk_set_rate(pch->clk_div, tin_rate);
} else {
tin_rate = clk_get_rate(pch->clk);
}
/* After changing the period, the divisor might
* have changed, and with it, the tick, which must
* be now recalculated */
p->tick_hz = clk_get_rate(pch->clk);
/* Recalculate ticks with the new tick_hz, just in case */
tcnt = pwm_ns_to_ticks(p, period_ns);
if (tcnt > MAX_TIMER_COUNT)
tcnt = MAX_TIMER_COUNT;
/* Save new ticks */
p->period_ticks = tcnt;
/* Update the PWM register block. */
local_irq_save(flags);
__raw_writel(tcnt, S3C2410_TCNTB(pch->timer));
tcon = __raw_readl(S3C2410_TCON);
tcon |= pwm_tcon_manulupdate(pch);
tcon |= pwm_tcon_autoreload(pch);
__raw_writel(tcon, S3C2410_TCON);
tcon &= ~pwm_tcon_manulupdate(pch);
__raw_writel(tcon, S3C2410_TCON);
local_irq_restore(flags);
return 0;
}
/* No sleeping configuration */
static int s3c24xx_pwm_config_nosleep(struct pwm_channel *p, struct pwm_channel_config *c)
{
int ret = 0;
unsigned long flags;
unsigned long duty_ns;
spin_lock_irqsave(&p->lock, flags);
switch (c->config_mask) {
case PWM_CONFIG_DUTY_TICKS:
ret = __s3c24xx_pwm_config_duty_ticks(p, c->duty_ticks);
break;
case PWM_CONFIG_PERIOD_TICKS:
/* Save original duty cycle */
duty_ns = pwm_ticks_to_ns(p, p->duty_ticks);
ret = __s3c24xx_pwm_config_period_ticks(p, c->period_ticks);
if (!ret) {
/* If period has changed, reconfigure duty cycle */
__s3c24xx_pwm_config_duty_ticks(p, pwm_ns_to_ticks(p, duty_ns));
/* Do not bother to return error code if this fails or
* the set period function will be called again later */
}
break;
case PWM_CONFIG_STOP:
__s3c24xx_pwm_stop(p);
pr_debug("%s:%d stop\n", p->pwm->bus_id, p->chan);
break;
case PWM_CONFIG_START:
__s3c24xx_pwm_start(p);
pr_debug("%s:%d start\n", p->pwm->bus_id, p->chan);
break;
case PWM_CONFIG_POLARITY:
__s3c24xx_pwm_config_polarity(p, c->polarity);
break;
default:
ret = -EINVAL;
break;
}
spin_unlock_irqrestore(&p->lock, flags);
return ret;
}
/* PWM configuration */
static int s3c24xx_pwm_config(struct pwm_channel *p, struct pwm_channel_config *c)
{
struct s3c24xx_pwm_channel *pch = s3c24xx_pwm_to_channel(p);
/* We currently avoid using 64bit arithmetic by using the
* fact that anything faster than 1Hz is easily representable
* by 32bits. */
if (p->pwm->config_nosleep) {
if (!p->pwm->config_nosleep(p, c))
return 0;
}
might_sleep();
if (c->config_mask & PWM_CONFIG_PERIOD_TICKS) {
__s3c24xx_pwm_config_period_ticks(p, c->period_ticks);
if (!(c->config_mask & PWM_CONFIG_DUTY_TICKS)) {
__s3c24xx_pwm_config_duty_ticks(p, p->duty_ticks);
}
}
if (c->config_mask & PWM_CONFIG_DUTY_TICKS)
__s3c24xx_pwm_config_duty_ticks(p, c->duty_ticks);
if (c->config_mask & PWM_CONFIG_POLARITY)
__s3c24xx_pwm_config_polarity(p, c->polarity);
if ((c->config_mask & PWM_CONFIG_START)
|| (pch->running && !(c->config_mask & PWM_CONFIG_STOP)))
__s3c24xx_pwm_start(p);
pr_debug("%s:%d config_mask %x\n", p->pwm->bus_id, p->chan, c->config_mask);
return 0;
}
/* PWM request function */
static int s3c24xx_pwm_request(struct pwm_channel *p)
{
struct s3c24xx_pwm_channel *pch = s3c24xx_pwm_to_channel(p);
unsigned long flags;
char pwmgpio[20];
int ret;
spin_lock_irqsave(&p->lock, flags);
/* Determine if the timer is alredy enabled == requested */
if (pch->use_count == 0) {
pch->use_count = 1;
} else {
ret = -EBUSY;
goto gpio_err;
}
/* Request the GPIO */
sprintf(pwmgpio, "%s.%d", p->pwm->bus_id, p->chan);
ret = gpio_request(pch->gpio, pwmgpio);
if (ret) {
pr_err("Request of GPIO %i failure\n", pch->gpio);
goto gpio_err;
}
/* Configures GPIO for TOUTn function */
s3c2410_gpio_cfgpin(pch->gpio, S3C2410_GPIO_SFN2);
p->tick_hz = clk_get_rate(pch->clk);
__s3c24xx_pwm_stop(p);
spin_unlock_irqrestore(&p->lock, flags);
pr_debug("%s: tick_hz = %lu\n", __func__, p->tick_hz);
return 0;
gpio_err:
return ret;
}
/* Frees resources taken by PWM */
static void s3c24xx_pwm_free(struct pwm_channel *p)
{
struct s3c24xx_pwm_channel *pch = s3c24xx_pwm_to_channel(p);
pr_debug("%s\n", __func__);
if (pch->use_count) {
pch->use_count--;
} else {
printk(KERN_ERR "PWM channel %d already freed\n", pch->timer);
}
s3c2410_gpio_cfgpin(pch->gpio, S3C2410_GPIO_INPUT);
gpio_free(pch->gpio);
}
static int __devinit s3c24xx_pwmc_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct s3c24xx_pwm *np = NULL;
struct s3c24xx_pwm_pdata *pdata;
struct s3c24xx_pwm_channel *pch;
int ret;
int i;
pr_debug("Probing a new device (ID %i)\n", pdev->id);
pdata = pdev->dev.platform_data;
if (!pdata) {
pr_err("No platform data passed? Aborting.\n");
return -EINVAL;
}
/* Sanity check */
if (pdata->number_channels > S3C24XX_PWM_CHANNEL_MAX) {
pr_err("Invalid number '%i' of channels\n", pdata->number_channels);
return -EINVAL;
}
/* Initialize channels */
for (i = 0; i < pdata->number_channels; i++) {
pch = &pdata->channels[i];
/* calculate base of control bits in TCON */
pch->tcon_base = (i == 0) ? 0 : (i * 4) + 4;
pch->use_count = 0;
pch->running = 0;
/* Hack pdev->id to get the proper clock producer for
* each channel */
pdev->id = i;
pch->clk = clk_get(dev, "pwm-tin");
if (IS_ERR(pch->clk)) {
dev_err(dev, "failed to get pwm tin clk\n");
ret = PTR_ERR(pch->clk);
goto err_clk_tin;
}
pch->clk_div = clk_get(dev, "pwm-tdiv");
if (IS_ERR(pch->clk_div)) {
dev_err(dev, "failed to get pwm tdiv clk\n");
ret = PTR_ERR(pch->clk_div);
goto err_clk_tdiv;
}
}
/* Restore previously hacked pdev->id */
pdev->id = -1;
pr_debug("%i PWM channels registered.\n", pdata->number_channels);
np = kzalloc(sizeof(struct s3c24xx_pwm), GFP_KERNEL);
if (!np) {
ret = -ENOMEM;
goto err_clk_tdiv;
}
spin_lock_init(&np->lock);
np->pwm.bus_id = dev_name(&pdev->dev);
np->pwm.nchan = pdata->number_channels;
/* Copy the external platform data to our internal structure */
//memcpy(&np->pdata, pdata, sizeof(*pdata));
np->pwm.owner = THIS_MODULE;
np->pwm.request = s3c24xx_pwm_request;
np->pwm.free = s3c24xx_pwm_free;
np->pwm.config_nosleep = s3c24xx_pwm_config_nosleep;
np->pwm.config = s3c24xx_pwm_config;
ret = pwm_register(&np->pwm);
if (ret) {
pr_debug("Failure registering pwm\n");
goto err_clk_tdiv;
}
platform_set_drvdata(pdev, np);
/* @XXX: Probably not the best place for this assignment */
np->pdata = pdata;
return 0;
err_clk_tdiv:
pch = &pdata->channels[i];
clk_put(pch->clk);
err_clk_tin:
while (i > 0) {
i--;
pch = &pdata->channels[i];
clk_put(pch->clk_div);
clk_put(pch->clk);
}
kfree(np);
return ret;
}
static int __devexit s3c24xx_pwmc_remove(struct platform_device *pdev)
{
struct s3c24xx_pwm *np;
struct s3c24xx_pwm_pdata *pdata;
struct device *dev = &pdev->dev;
struct s3c24xx_pwm_channel *pch;
struct pwm_channel *p;
int ret;
u32 i;
pr_debug("Removing device (ID %i)\n", pdev->id);
pdata = pdev->dev.platform_data;
if (!pdata) {
pr_err("No platform data passed? Aborting.\n");
return -EINVAL;
}
np = platform_get_drvdata(pdev);
p = np->pwm.channels;
for(i = 0; i < pdata->number_channels; i++) {
pch = &pdata->channels[i];
if (pch->use_count) {
/* Stop running channels */
if (pch->running) {
__s3c24xx_pwm_stop(p + i);
}
/* Free requested channels */
s3c24xx_pwm_free(p + i);
}
/* Free clocks */
clk_put(pch->clk_div);
clk_put(pch->clk);
}
ret = pwm_unregister(&np->pwm);
if (ret) {
pr_debug("Failure unregistering pwm\n");
}
platform_set_drvdata(pdev, NULL);
kfree(np);
module_put(dev->driver->owner);
return 0;
}
static struct platform_driver s3c24xx_pwm_driver = {
.driver = {
.name = "s3c24xx-pwm",
.owner = THIS_MODULE,
},
.probe = s3c24xx_pwmc_probe,
.remove = __devexit_p(s3c24xx_pwmc_remove),
};
static char banner[] __initdata =
KERN_INFO "S3C24XX PWM driver\n";
static int __init s3c24xx_pwm_init(void)
{
int ret;
clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
return -EINVAL;
}
printk(banner);
ret = platform_driver_register(&s3c24xx_pwm_driver);
if (ret)
printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
return ret;
}
module_init(s3c24xx_pwm_init);
static void s3c24xx_pwm_exit(void)
{
platform_driver_unregister(&s3c24xx_pwm_driver);
}
module_exit(s3c24xx_pwm_exit);
MODULE_AUTHOR("Digi International Inc.");
MODULE_DESCRIPTION("Driver for s3c24xx PWM peripheral");
MODULE_LICENSE("GPL");
|