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 unsigned long clk_pwm_scaler_getrate(struct clk
*clk
)
78 unsigned long tcfg0
= __raw_readl(S3C2410_TCFG0
);
81 tcfg0
&= S3C2410_TCFG_PRESCALER1_MASK
;
82 tcfg0
>>= S3C2410_TCFG_PRESCALER1_SHIFT
;
84 tcfg0
&= S3C2410_TCFG_PRESCALER0_MASK
;
87 return clk_get_rate(clk
->parent
) / (tcfg0
+ 1);
90 /* TODO - add set rate calls. */
92 static struct clk clk_timer_scaler
[] = {
94 .name
= "pwm-scaler0",
96 .get_rate
= clk_pwm_scaler_getrate
,
99 .name
= "pwm-scaler1",
101 .get_rate
= clk_pwm_scaler_getrate
,
105 static struct clk clk_timer_tclk
[] = {
116 struct pwm_tdiv_clk
{
118 unsigned int divisor
;
121 static inline struct pwm_tdiv_clk
*to_tdiv(struct clk
*clk
)
123 return container_of(clk
, struct pwm_tdiv_clk
, clk
);
126 static unsigned long clk_pwm_tdiv_get_rate(struct clk
*clk
)
128 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
129 unsigned int divisor
;
131 tcfg1
>>= S3C2410_TCFG1_SHIFT(clk
->id
);
132 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
134 if (pwm_cfg_src_is_tclk(tcfg1
))
135 divisor
= to_tdiv(clk
)->divisor
;
137 divisor
= tcfg_to_divisor(tcfg1
);
139 return clk_get_rate(clk
->parent
) / divisor
;
142 static unsigned long clk_pwm_tdiv_round_rate(struct clk
*clk
,
145 unsigned long parent_rate
;
146 unsigned long divisor
;
148 parent_rate
= clk_get_rate(clk
->parent
);
149 divisor
= parent_rate
/ rate
;
151 if (divisor
<= 1 && pwm_tdiv_has_div1())
153 else if (divisor
<= 2)
155 else if (divisor
<= 4)
157 else if (divisor
<= 8)
162 return parent_rate
/ divisor
;
165 static unsigned long clk_pwm_tdiv_bits(struct pwm_tdiv_clk
*divclk
)
167 return pwm_tdiv_div_bits(divclk
->divisor
);
170 static void clk_pwm_tdiv_update(struct pwm_tdiv_clk
*divclk
)
172 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
173 unsigned long bits
= clk_pwm_tdiv_bits(divclk
);
175 unsigned long shift
= S3C2410_TCFG1_SHIFT(divclk
->clk
.id
);
177 local_irq_save(flags
);
179 tcfg1
= __raw_readl(S3C2410_TCFG1
);
180 tcfg1
&= ~(S3C2410_TCFG1_MUX_MASK
<< shift
);
181 tcfg1
|= bits
<< shift
;
182 __raw_writel(tcfg1
, S3C2410_TCFG1
);
184 local_irq_restore(flags
);
187 static int clk_pwm_tdiv_set_rate(struct clk
*clk
, unsigned long rate
)
189 struct pwm_tdiv_clk
*divclk
= to_tdiv(clk
);
190 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
191 unsigned long parent_rate
= clk_get_rate(clk
->parent
);
192 unsigned long divisor
;
194 tcfg1
>>= S3C2410_TCFG1_SHIFT(clk
->id
);
195 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
197 rate
= clk_round_rate(clk
, rate
);
198 divisor
= parent_rate
/ rate
;
203 divclk
->divisor
= divisor
;
205 /* Update the current MUX settings if we are currently
206 * selected as the clock source for this clock. */
208 if (!pwm_cfg_src_is_tclk(tcfg1
))
209 clk_pwm_tdiv_update(divclk
);
214 static struct pwm_tdiv_clk clk_timer_tdiv
[] = {
218 .parent
= &clk_timer_scaler
[0],
219 .get_rate
= clk_pwm_tdiv_get_rate
,
220 .set_rate
= clk_pwm_tdiv_set_rate
,
221 .round_rate
= clk_pwm_tdiv_round_rate
,
227 .parent
= &clk_timer_scaler
[0],
228 .get_rate
= clk_pwm_tdiv_get_rate
,
229 .set_rate
= clk_pwm_tdiv_set_rate
,
230 .round_rate
= clk_pwm_tdiv_round_rate
,
236 .parent
= &clk_timer_scaler
[1],
237 .get_rate
= clk_pwm_tdiv_get_rate
,
238 .set_rate
= clk_pwm_tdiv_set_rate
,
239 .round_rate
= clk_pwm_tdiv_round_rate
,
245 .parent
= &clk_timer_scaler
[1],
246 .get_rate
= clk_pwm_tdiv_get_rate
,
247 .set_rate
= clk_pwm_tdiv_set_rate
,
248 .round_rate
= clk_pwm_tdiv_round_rate
,
254 .parent
= &clk_timer_scaler
[1],
255 .get_rate
= clk_pwm_tdiv_get_rate
,
256 .set_rate
= clk_pwm_tdiv_set_rate
,
257 .round_rate
= clk_pwm_tdiv_round_rate
,
262 static int __init
clk_pwm_tdiv_register(unsigned int id
)
264 struct pwm_tdiv_clk
*divclk
= &clk_timer_tdiv
[id
];
265 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
267 tcfg1
>>= S3C2410_TCFG1_SHIFT(id
);
268 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
271 divclk
->divisor
= tcfg_to_divisor(tcfg1
);
273 return s3c24xx_register_clock(&divclk
->clk
);
276 static inline struct clk
*s3c24xx_pwmclk_tclk(unsigned int id
)
278 return (id
>= 2) ? &clk_timer_tclk
[1] : &clk_timer_tclk
[0];
281 static inline struct clk
*s3c24xx_pwmclk_tdiv(unsigned int id
)
283 return &clk_timer_tdiv
[id
].clk
;
286 static int clk_pwm_tin_set_parent(struct clk
*clk
, struct clk
*parent
)
288 unsigned int id
= clk
->id
;
292 unsigned long shift
= S3C2410_TCFG1_SHIFT(id
);
294 if (parent
== s3c24xx_pwmclk_tclk(id
))
295 bits
= S3C_TCFG1_MUX_TCLK
<< shift
;
296 else if (parent
== s3c24xx_pwmclk_tdiv(id
))
297 bits
= clk_pwm_tdiv_bits(to_tdiv(parent
)) << shift
;
301 clk
->parent
= parent
;
303 local_irq_save(flags
);
305 tcfg1
= __raw_readl(S3C2410_TCFG1
);
306 tcfg1
&= ~(S3C2410_TCFG1_MUX_MASK
<< shift
);
307 __raw_writel(tcfg1
| bits
, S3C2410_TCFG1
);
309 local_irq_restore(flags
);
314 static struct clk clk_tin
[] = {
318 .set_parent
= clk_pwm_tin_set_parent
,
323 .set_parent
= clk_pwm_tin_set_parent
,
328 .set_parent
= clk_pwm_tin_set_parent
,
333 .set_parent
= clk_pwm_tin_set_parent
,
338 .set_parent
= clk_pwm_tin_set_parent
,
342 static __init
int clk_pwm_tin_register(struct clk
*pwm
)
344 unsigned long tcfg1
= __raw_readl(S3C2410_TCFG1
);
345 unsigned int id
= pwm
->id
;
350 ret
= s3c24xx_register_clock(pwm
);
354 tcfg1
>>= S3C2410_TCFG1_SHIFT(id
);
355 tcfg1
&= S3C2410_TCFG1_MUX_MASK
;
357 if (pwm_cfg_src_is_tclk(tcfg1
))
358 parent
= s3c24xx_pwmclk_tclk(id
);
360 parent
= s3c24xx_pwmclk_tdiv(id
);
362 return clk_set_parent(pwm
, parent
);
365 static __init
int s3c24xx_pwmclk_init(void)
367 struct clk
*clk_timers
;
371 clk_timers
= clk_get(NULL
, "timers");
372 if (IS_ERR(clk_timers
)) {
373 printk(KERN_ERR
"%s: no parent clock\n", __func__
);
377 for (clk
= 0; clk
< ARRAY_SIZE(clk_timer_scaler
); clk
++) {
378 clk_timer_scaler
[clk
].parent
= clk_timers
;
379 ret
= s3c24xx_register_clock(&clk_timer_scaler
[clk
]);
381 printk(KERN_ERR
"error adding pwm scaler%d clock\n", clk
);
386 for (clk
= 0; clk
< ARRAY_SIZE(clk_timer_tclk
); clk
++) {
387 ret
= s3c24xx_register_clock(&clk_timer_tclk
[clk
]);
389 printk(KERN_ERR
"error adding pww tclk%d\n", clk
);
394 for (clk
= 0; clk
< ARRAY_SIZE(clk_timer_tdiv
); clk
++) {
395 ret
= clk_pwm_tdiv_register(clk
);
397 printk(KERN_ERR
"error adding pwm%d tdiv clock\n", clk
);
402 for (clk
= 0; clk
< ARRAY_SIZE(clk_tin
); clk
++) {
403 ret
= clk_pwm_tin_register(&clk_tin
[clk
]);
405 printk(KERN_ERR
"error adding pwm%d tin clock\n", clk
);
416 arch_initcall(s3c24xx_pwmclk_init
);