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/log2.h>
18 #include <linux/clk.h>
19 #include <linux/err.h>
22 #include <mach/hardware.h>
26 #include <plat/clock.h>
29 #include <plat/regs-timer.h>
30 #include <mach/pwm-clock.h>
32 /* Each of the timers 0 through 5 go through the following
33 * clock tree, with the inputs depending on the timers.
35 * pclk ---- [ prescaler 0 ] -+---> timer 0
38 * pclk ---- [ prescaler 1 ] -+---> timer 2
42 * Which are fed into the timers as so:
44 * prescaled 0 ---- [ div 2,4,8,16 ] ---\
46 * tclk 0 ------------------------------/
48 * prescaled 0 ---- [ div 2,4,8,16 ] ---\
50 * tclk 0 ------------------------------/
53 * prescaled 1 ---- [ div 2,4,8,16 ] ---\
55 * tclk 1 ------------------------------/
57 * prescaled 1 ---- [ div 2,4,8,16 ] ---\
59 * tclk 1 ------------------------------/
61 * prescaled 1 ---- [ div 2,4,8, 16 ] --\
63 * tclk 1 ------------------------------/
65 * Since the mux and the divider are tied together in the
66 * same register space, it is impossible to set the parent
67 * and the rate at the same time. To avoid this, we add an
68 * intermediate 'prescaled-and-divided' clock to select
69 * as the parent for the timer input clock called tdiv.
71 * prescaled clk --> pwm-tdiv ---\
73 * tclk -------------------------/
76 static struct clk clk_timer_scaler
[];
78 static unsigned long clk_pwm_scaler_get_rate(struct clk
*clk
)
80 unsigned long tcfg0
= __raw_readl(S3C2410_TCFG0
);
82 if (clk
== &clk_timer_scaler
[1]) {
83 tcfg0
&= S3C2410_TCFG_PRESCALER1_MASK
;
84 tcfg0
>>= S3C2410_TCFG_PRESCALER1_SHIFT
;
86 tcfg0
&= S3C2410_TCFG_PRESCALER0_MASK
;
89 return clk_get_rate(clk
->parent
) / (tcfg0
+ 1);
92 static unsigned long clk_pwm_scaler_round_rate(struct clk
*clk
,
95 unsigned long parent_rate
= clk_get_rate(clk
->parent
);
96 unsigned long divisor
= parent_rate
/ rate
;
100 else if (divisor
< 2)
103 return parent_rate
/ divisor
;
106 static int clk_pwm_scaler_set_rate(struct clk
*clk
, unsigned long rate
)
108 unsigned long round
= clk_pwm_scaler_round_rate(clk
, rate
);
110 unsigned long divisor
;
113 divisor
= clk_get_rate(clk
->parent
) / round
;
116 local_irq_save(flags
);
117 tcfg0
= __raw_readl(S3C2410_TCFG0
);
119 if (clk
== &clk_timer_scaler
[1]) {
120 tcfg0
&= ~S3C2410_TCFG_PRESCALER1_MASK
;
121 tcfg0
|= divisor
<< S3C2410_TCFG_PRESCALER1_SHIFT
;
123 tcfg0
&= ~S3C2410_TCFG_PRESCALER0_MASK
;
127 __raw_writel(tcfg0
, S3C2410_TCFG0
);
128 local_irq_restore(flags
);
133 static struct clk clk_timer_scaler
[] = {
135 .name
= "pwm-scaler0",
137 .get_rate
= clk_pwm_scaler_get_rate
,
138 .set_rate
= clk_pwm_scaler_set_rate
,
139 .round_rate
= clk_pwm_scaler_round_rate
,
142 .name
= "pwm-scaler1",
144 .get_rate
= clk_pwm_scaler_get_rate
,
145 .set_rate
= clk_pwm_scaler_set_rate
,
146 .round_rate
= clk_pwm_scaler_round_rate
,
150 static struct clk clk_timer_tclk
[] = {
161 struct pwm_tdiv_clk
{
163 unsigned int divisor
;
166 static inline struct pwm_tdiv_clk
*to_tdiv(struct clk
*clk
)
168 return container_of(clk
, struct pwm_tdiv_clk
, clk
);
171 static unsigned long clk_pwm_tdiv_get_rate(struct clk
*clk
)
173 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
174 unsigned int divisor
;
176 tcfg1
>>= S3C2410_TCFG1_SHIFT(clk
->id
);
177 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
179 if (pwm_cfg_src_is_tclk(tcfg1
))
180 divisor
= to_tdiv(clk
)->divisor
;
182 divisor
= tcfg_to_divisor(tcfg1
);
184 return clk_get_rate(clk
->parent
) / divisor
;
187 static unsigned long clk_pwm_tdiv_round_rate(struct clk
*clk
,
190 unsigned long parent_rate
;
191 unsigned long divisor
;
193 parent_rate
= clk_get_rate(clk
->parent
);
194 divisor
= parent_rate
/ rate
;
196 if (divisor
<= 1 && pwm_tdiv_has_div1())
198 else if (divisor
<= 2)
200 else if (divisor
<= 4)
202 else if (divisor
<= 8)
207 return parent_rate
/ divisor
;
210 static unsigned long clk_pwm_tdiv_bits(struct pwm_tdiv_clk
*divclk
)
212 return pwm_tdiv_div_bits(divclk
->divisor
);
215 static void clk_pwm_tdiv_update(struct pwm_tdiv_clk
*divclk
)
217 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
218 unsigned long bits
= clk_pwm_tdiv_bits(divclk
);
220 unsigned long shift
= S3C2410_TCFG1_SHIFT(divclk
->clk
.id
);
222 local_irq_save(flags
);
224 tcfg1
= __raw_readl(S3C2410_TCFG1
);
225 tcfg1
&= ~(S3C2410_TCFG1_MUX_MASK
<< shift
);
226 tcfg1
|= bits
<< shift
;
227 __raw_writel(tcfg1
, S3C2410_TCFG1
);
229 local_irq_restore(flags
);
232 static int clk_pwm_tdiv_set_rate(struct clk
*clk
, unsigned long rate
)
234 struct pwm_tdiv_clk
*divclk
= to_tdiv(clk
);
235 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
236 unsigned long parent_rate
= clk_get_rate(clk
->parent
);
237 unsigned long divisor
;
239 tcfg1
>>= S3C2410_TCFG1_SHIFT(clk
->id
);
240 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
242 rate
= clk_round_rate(clk
, rate
);
243 divisor
= parent_rate
/ rate
;
248 divclk
->divisor
= divisor
;
250 /* Update the current MUX settings if we are currently
251 * selected as the clock source for this clock. */
253 if (!pwm_cfg_src_is_tclk(tcfg1
))
254 clk_pwm_tdiv_update(divclk
);
259 static struct pwm_tdiv_clk clk_timer_tdiv
[] = {
263 .parent
= &clk_timer_scaler
[0],
264 .get_rate
= clk_pwm_tdiv_get_rate
,
265 .set_rate
= clk_pwm_tdiv_set_rate
,
266 .round_rate
= clk_pwm_tdiv_round_rate
,
272 .parent
= &clk_timer_scaler
[0],
273 .get_rate
= clk_pwm_tdiv_get_rate
,
274 .set_rate
= clk_pwm_tdiv_set_rate
,
275 .round_rate
= clk_pwm_tdiv_round_rate
,
281 .parent
= &clk_timer_scaler
[1],
282 .get_rate
= clk_pwm_tdiv_get_rate
,
283 .set_rate
= clk_pwm_tdiv_set_rate
,
284 .round_rate
= clk_pwm_tdiv_round_rate
,
290 .parent
= &clk_timer_scaler
[1],
291 .get_rate
= clk_pwm_tdiv_get_rate
,
292 .set_rate
= clk_pwm_tdiv_set_rate
,
293 .round_rate
= clk_pwm_tdiv_round_rate
,
299 .parent
= &clk_timer_scaler
[1],
300 .get_rate
= clk_pwm_tdiv_get_rate
,
301 .set_rate
= clk_pwm_tdiv_set_rate
,
302 .round_rate
= clk_pwm_tdiv_round_rate
,
307 static int __init
clk_pwm_tdiv_register(unsigned int id
)
309 struct pwm_tdiv_clk
*divclk
= &clk_timer_tdiv
[id
];
310 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
312 tcfg1
>>= S3C2410_TCFG1_SHIFT(id
);
313 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
316 divclk
->divisor
= tcfg_to_divisor(tcfg1
);
318 return s3c24xx_register_clock(&divclk
->clk
);
321 static inline struct clk
*s3c24xx_pwmclk_tclk(unsigned int id
)
323 return (id
>= 2) ? &clk_timer_tclk
[1] : &clk_timer_tclk
[0];
326 static inline struct clk
*s3c24xx_pwmclk_tdiv(unsigned int id
)
328 return &clk_timer_tdiv
[id
].clk
;
331 static int clk_pwm_tin_set_parent(struct clk
*clk
, struct clk
*parent
)
333 unsigned int id
= clk
->id
;
337 unsigned long shift
= S3C2410_TCFG1_SHIFT(id
);
339 if (parent
== s3c24xx_pwmclk_tclk(id
))
340 bits
= S3C_TCFG1_MUX_TCLK
<< shift
;
341 else if (parent
== s3c24xx_pwmclk_tdiv(id
))
342 bits
= clk_pwm_tdiv_bits(to_tdiv(parent
)) << shift
;
346 clk
->parent
= parent
;
348 local_irq_save(flags
);
350 tcfg1
= __raw_readl(S3C2410_TCFG1
);
351 tcfg1
&= ~(S3C2410_TCFG1_MUX_MASK
<< shift
);
352 __raw_writel(tcfg1
| bits
, S3C2410_TCFG1
);
354 local_irq_restore(flags
);
359 static struct clk clk_tin
[] = {
363 .set_parent
= clk_pwm_tin_set_parent
,
368 .set_parent
= clk_pwm_tin_set_parent
,
373 .set_parent
= clk_pwm_tin_set_parent
,
378 .set_parent
= clk_pwm_tin_set_parent
,
383 .set_parent
= clk_pwm_tin_set_parent
,
387 static __init
int clk_pwm_tin_register(struct clk
*pwm
)
389 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
390 unsigned int id
= pwm
->id
;
395 ret
= s3c24xx_register_clock(pwm
);
399 tcfg1
>>= S3C2410_TCFG1_SHIFT(id
);
400 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
402 if (pwm_cfg_src_is_tclk(tcfg1
))
403 parent
= s3c24xx_pwmclk_tclk(id
);
405 parent
= s3c24xx_pwmclk_tdiv(id
);
407 return clk_set_parent(pwm
, parent
);
411 * s3c_pwmclk_init() - initialise pwm clocks
413 * Initialise and register the clocks which provide the inputs for the
416 * Note, this call is required by the time core, so must be called after
417 * the base clocks are added and before any of the initcalls are run.
419 __init
void s3c_pwmclk_init(void)
421 struct clk
*clk_timers
;
425 clk_timers
= clk_get(NULL
, "timers");
426 if (IS_ERR(clk_timers
)) {
427 printk(KERN_ERR
"%s: no parent clock\n", __func__
);
431 for (clk
= 0; clk
< ARRAY_SIZE(clk_timer_scaler
); clk
++) {
432 clk_timer_scaler
[clk
].parent
= clk_timers
;
433 ret
= s3c24xx_register_clock(&clk_timer_scaler
[clk
]);
435 printk(KERN_ERR
"error adding pwm scaler%d clock\n", clk
);
440 for (clk
= 0; clk
< ARRAY_SIZE(clk_timer_tclk
); clk
++) {
441 ret
= s3c24xx_register_clock(&clk_timer_tclk
[clk
]);
443 printk(KERN_ERR
"error adding pww tclk%d\n", clk
);
448 for (clk
= 0; clk
< ARRAY_SIZE(clk_timer_tdiv
); clk
++) {
449 ret
= clk_pwm_tdiv_register(clk
);
451 printk(KERN_ERR
"error adding pwm%d tdiv clock\n", clk
);
456 for (clk
= 0; clk
< ARRAY_SIZE(clk_tin
); clk
++) {
457 ret
= clk_pwm_tin_register(&clk_tin
[clk
]);
459 printk(KERN_ERR
"error adding pwm%d tin clock\n", clk
);