1 /* linux/arch/arm/plat-s3c24xx/pwm-clock.c
3 * Copyright (c) 2007 Simtec Electronics
4 * Copyright (c) 2007, 2008 Ben Dooks
5 * Ben Dooks <ben-linux@fluff.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License.
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/clk.h>
18 #include <linux/err.h>
21 #include <mach/hardware.h>
25 #include <plat/clock.h>
28 #include <plat/regs-timer.h>
30 /* Each of the timers 0 through 5 go through the following
31 * clock tree, with the inputs depending on the timers.
33 * pclk ---- [ prescaler 0 ] -+---> timer 0
36 * pclk ---- [ prescaler 1 ] -+---> timer 2
40 * Which are fed into the timers as so:
42 * prescaled 0 ---- [ div 2,4,8,16 ] ---\
44 * tclk 0 ------------------------------/
46 * prescaled 0 ---- [ div 2,4,8,16 ] ---\
48 * tclk 0 ------------------------------/
51 * prescaled 1 ---- [ div 2,4,8,16 ] ---\
53 * tclk 1 ------------------------------/
55 * prescaled 1 ---- [ div 2,4,8,16 ] ---\
57 * tclk 1 ------------------------------/
59 * prescaled 1 ---- [ div 2,4,8, 16 ] --\
61 * tclk 1 ------------------------------/
63 * Since the mux and the divider are tied together in the
64 * same register space, it is impossible to set the parent
65 * and the rate at the same time. To avoid this, we add an
66 * intermediate 'prescaled-and-divided' clock to select
67 * as the parent for the timer input clock called tdiv.
69 * prescaled clk --> pwm-tdiv ---\
71 * tclk -------------------------/
74 static unsigned long clk_pwm_scaler_getrate(struct clk
*clk
)
76 unsigned long tcfg0
= __raw_readl(S3C2410_TCFG0
);
79 tcfg0
&= S3C2410_TCFG_PRESCALER1_MASK
;
80 tcfg0
>>= S3C2410_TCFG_PRESCALER1_SHIFT
;
82 tcfg0
&= S3C2410_TCFG_PRESCALER0_MASK
;
85 return clk_get_rate(clk
->parent
) / (tcfg0
+ 1);
88 /* TODO - add set rate calls. */
90 static struct clk clk_timer_scaler
[] = {
92 .name
= "pwm-scaler0",
94 .get_rate
= clk_pwm_scaler_getrate
,
97 .name
= "pwm-scaler1",
99 .get_rate
= clk_pwm_scaler_getrate
,
103 static struct clk clk_timer_tclk
[] = {
114 struct pwm_tdiv_clk
{
116 unsigned int divisor
;
119 static inline struct pwm_tdiv_clk
*to_tdiv(struct clk
*clk
)
121 return container_of(clk
, struct pwm_tdiv_clk
, clk
);
124 static inline unsigned long tcfg_to_divisor(unsigned long tcfg1
)
126 return 1 << (1 + tcfg1
);
129 static unsigned long clk_pwm_tdiv_get_rate(struct clk
*clk
)
131 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
132 unsigned int divisor
;
134 tcfg1
>>= S3C2410_TCFG1_SHIFT(clk
->id
);
135 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
137 if (tcfg1
== S3C2410_TCFG1_MUX_TCLK
)
138 divisor
= to_tdiv(clk
)->divisor
;
140 divisor
= tcfg_to_divisor(tcfg1
);
142 return clk_get_rate(clk
->parent
) / divisor
;
145 static unsigned long clk_pwm_tdiv_round_rate(struct clk
*clk
,
148 unsigned long parent_rate
;
149 unsigned long divisor
;
151 parent_rate
= clk_get_rate(clk
->parent
);
152 divisor
= parent_rate
/ rate
;
156 else if (divisor
<= 4)
158 else if (divisor
<= 8)
163 return parent_rate
/ divisor
;
166 static unsigned long clk_pwm_tdiv_bits(struct pwm_tdiv_clk
*divclk
)
170 switch (divclk
->divisor
) {
172 bits
= S3C2410_TCFG1_MUX_DIV2
;
175 bits
= S3C2410_TCFG1_MUX_DIV4
;
178 bits
= S3C2410_TCFG1_MUX_DIV8
;
182 bits
= S3C2410_TCFG1_MUX_DIV16
;
189 static void clk_pwm_tdiv_update(struct pwm_tdiv_clk
*divclk
)
191 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
192 unsigned long bits
= clk_pwm_tdiv_bits(divclk
);
194 unsigned long shift
= S3C2410_TCFG1_SHIFT(divclk
->clk
.id
);
196 local_irq_save(flags
);
198 tcfg1
= __raw_readl(S3C2410_TCFG1
);
199 tcfg1
&= ~(S3C2410_TCFG1_MUX_MASK
<< shift
);
200 tcfg1
|= bits
<< shift
;
201 __raw_writel(tcfg1
, S3C2410_TCFG1
);
203 local_irq_restore(flags
);
206 static int clk_pwm_tdiv_set_rate(struct clk
*clk
, unsigned long rate
)
208 struct pwm_tdiv_clk
*divclk
= to_tdiv(clk
);
209 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
210 unsigned long parent_rate
= clk_get_rate(clk
->parent
);
211 unsigned long divisor
;
213 tcfg1
>>= S3C2410_TCFG1_SHIFT(clk
->id
);
214 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
216 rate
= clk_round_rate(clk
, rate
);
217 divisor
= parent_rate
/ rate
;
222 divclk
->divisor
= divisor
;
224 /* Update the current MUX settings if we are currently
225 * selected as the clock source for this clock. */
227 if (tcfg1
!= S3C2410_TCFG1_MUX_TCLK
)
228 clk_pwm_tdiv_update(divclk
);
233 static struct pwm_tdiv_clk clk_timer_tdiv
[] = {
237 .parent
= &clk_timer_scaler
[0],
238 .get_rate
= clk_pwm_tdiv_get_rate
,
239 .set_rate
= clk_pwm_tdiv_set_rate
,
240 .round_rate
= clk_pwm_tdiv_round_rate
,
246 .parent
= &clk_timer_scaler
[0],
247 .get_rate
= clk_pwm_tdiv_get_rate
,
248 .set_rate
= clk_pwm_tdiv_set_rate
,
249 .round_rate
= clk_pwm_tdiv_round_rate
,
255 .parent
= &clk_timer_scaler
[1],
256 .get_rate
= clk_pwm_tdiv_get_rate
,
257 .set_rate
= clk_pwm_tdiv_set_rate
,
258 .round_rate
= clk_pwm_tdiv_round_rate
,
264 .parent
= &clk_timer_scaler
[1],
265 .get_rate
= clk_pwm_tdiv_get_rate
,
266 .set_rate
= clk_pwm_tdiv_set_rate
,
267 .round_rate
= clk_pwm_tdiv_round_rate
,
273 .parent
= &clk_timer_scaler
[1],
274 .get_rate
= clk_pwm_tdiv_get_rate
,
275 .set_rate
= clk_pwm_tdiv_set_rate
,
276 .round_rate
= clk_pwm_tdiv_round_rate
,
281 static int __init
clk_pwm_tdiv_register(unsigned int id
)
283 struct pwm_tdiv_clk
*divclk
= &clk_timer_tdiv
[id
];
284 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
286 tcfg1
>>= S3C2410_TCFG1_SHIFT(id
);
287 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
290 divclk
->divisor
= tcfg_to_divisor(tcfg1
);
292 return s3c24xx_register_clock(&divclk
->clk
);
295 static inline struct clk
*s3c24xx_pwmclk_tclk(unsigned int id
)
297 return (id
>= 2) ? &clk_timer_tclk
[1] : &clk_timer_tclk
[0];
300 static inline struct clk
*s3c24xx_pwmclk_tdiv(unsigned int id
)
302 return &clk_timer_tdiv
[id
].clk
;
305 static int clk_pwm_tin_set_parent(struct clk
*clk
, struct clk
*parent
)
307 unsigned int id
= clk
->id
;
311 unsigned long shift
= S3C2410_TCFG1_SHIFT(id
);
313 if (parent
== s3c24xx_pwmclk_tclk(id
))
314 bits
= S3C2410_TCFG1_MUX_TCLK
<< shift
;
315 else if (parent
== s3c24xx_pwmclk_tdiv(id
))
316 bits
= clk_pwm_tdiv_bits(to_tdiv(parent
)) << shift
;
320 clk
->parent
= parent
;
322 local_irq_save(flags
);
324 tcfg1
= __raw_readl(S3C2410_TCFG1
);
325 tcfg1
&= ~(S3C2410_TCFG1_MUX_MASK
<< shift
);
326 __raw_writel(tcfg1
| bits
, S3C2410_TCFG1
);
328 local_irq_restore(flags
);
333 static struct clk clk_tin
[] = {
337 .set_parent
= clk_pwm_tin_set_parent
,
342 .set_parent
= clk_pwm_tin_set_parent
,
347 .set_parent
= clk_pwm_tin_set_parent
,
352 .set_parent
= clk_pwm_tin_set_parent
,
357 .set_parent
= clk_pwm_tin_set_parent
,
361 static __init
int clk_pwm_tin_register(struct clk
*pwm
)
363 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
364 unsigned int id
= pwm
->id
;
369 ret
= s3c24xx_register_clock(pwm
);
373 tcfg1
>>= S3C2410_TCFG1_SHIFT(id
);
374 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
376 if (tcfg1
== S3C2410_TCFG1_MUX_TCLK
)
377 parent
= s3c24xx_pwmclk_tclk(id
);
379 parent
= s3c24xx_pwmclk_tdiv(id
);
381 return clk_set_parent(pwm
, parent
);
384 static __init
int s3c24xx_pwmclk_init(void)
386 struct clk
*clk_timers
;
390 clk_timers
= clk_get(NULL
, "timers");
391 if (IS_ERR(clk_timers
)) {
392 printk(KERN_ERR
"%s: no parent clock\n", __func__
);
396 for (clk
= 0; clk
< ARRAY_SIZE(clk_timer_scaler
); clk
++) {
397 clk_timer_scaler
[clk
].parent
= clk_timers
;
398 ret
= s3c24xx_register_clock(&clk_timer_scaler
[clk
]);
400 printk(KERN_ERR
"error adding pwm scaler%d clock\n", clk
);
405 for (clk
= 0; clk
< ARRAY_SIZE(clk_timer_tclk
); clk
++) {
406 ret
= s3c24xx_register_clock(&clk_timer_tclk
[clk
]);
408 printk(KERN_ERR
"error adding pww tclk%d\n", clk
);
413 for (clk
= 0; clk
< ARRAY_SIZE(clk_timer_tdiv
); clk
++) {
414 ret
= clk_pwm_tdiv_register(clk
);
416 printk(KERN_ERR
"error adding pwm%d tdiv clock\n", clk
);
421 for (clk
= 0; clk
< ARRAY_SIZE(clk_tin
); clk
++) {
422 ret
= clk_pwm_tin_register(&clk_tin
[clk
]);
424 printk(KERN_ERR
"error adding pwm%d tin clock\n", clk
);
435 arch_initcall(s3c24xx_pwmclk_init
);