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>
24 #include <mach/regs-clock.h>
25 #include <mach/regs-gpio.h>
27 #include <plat/clock.h>
30 #include <plat/regs-timer.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 inline unsigned long tcfg_to_divisor(unsigned long tcfg1)
173 return 1 << (1 + tcfg1);
176 static unsigned long clk_pwm_tdiv_get_rate(struct clk *clk)
178 unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
179 unsigned int divisor;
181 tcfg1 >>= S3C2410_TCFG1_SHIFT(clk->id);
182 tcfg1 &= S3C2410_TCFG1_MUX_MASK;
184 if (tcfg1 == S3C2410_TCFG1_MUX_TCLK)
185 divisor = to_tdiv(clk)->divisor;
187 divisor = tcfg_to_divisor(tcfg1);
189 return clk_get_rate(clk->parent) / divisor;
192 static unsigned long clk_pwm_tdiv_round_rate(struct clk *clk,
195 unsigned long parent_rate;
196 unsigned long divisor;
198 parent_rate = clk_get_rate(clk->parent);
199 divisor = parent_rate / rate;
203 else if (divisor <= 4)
205 else if (divisor <= 8)
210 return parent_rate / divisor;
213 static unsigned long clk_pwm_tdiv_bits(struct pwm_tdiv_clk *divclk)
217 switch (divclk->divisor) {
219 bits = S3C2410_TCFG1_MUX_DIV2;
222 bits = S3C2410_TCFG1_MUX_DIV4;
225 bits = S3C2410_TCFG1_MUX_DIV8;
229 bits = S3C2410_TCFG1_MUX_DIV16;
236 static void clk_pwm_tdiv_update(struct pwm_tdiv_clk *divclk)
238 unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
239 unsigned long bits = clk_pwm_tdiv_bits(divclk);
241 unsigned long shift = S3C2410_TCFG1_SHIFT(divclk->clk.id);
243 local_irq_save(flags);
245 tcfg1 = __raw_readl(S3C2410_TCFG1);
246 tcfg1 &= ~(S3C2410_TCFG1_MUX_MASK << shift);
247 tcfg1 |= bits << shift;
248 __raw_writel(tcfg1, S3C2410_TCFG1);
250 local_irq_restore(flags);
253 static int clk_pwm_tdiv_set_rate(struct clk *clk, unsigned long rate)
255 struct pwm_tdiv_clk *divclk = to_tdiv(clk);
256 unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
257 unsigned long parent_rate = clk_get_rate(clk->parent);
258 unsigned long divisor;
260 tcfg1 >>= S3C2410_TCFG1_SHIFT(clk->id);
261 tcfg1 &= S3C2410_TCFG1_MUX_MASK;
263 rate = clk_round_rate(clk, rate);
264 divisor = parent_rate / rate;
269 divclk->divisor = divisor;
271 /* Update the current MUX settings if we are currently
272 * selected as the clock source for this clock. */
274 if (tcfg1 != S3C2410_TCFG1_MUX_TCLK)
275 clk_pwm_tdiv_update(divclk);
280 static struct pwm_tdiv_clk clk_timer_tdiv[] = {
284 .parent = &clk_timer_scaler[0],
285 .get_rate = clk_pwm_tdiv_get_rate,
286 .set_rate = clk_pwm_tdiv_set_rate,
287 .round_rate = clk_pwm_tdiv_round_rate,
293 .parent = &clk_timer_scaler[0],
294 .get_rate = clk_pwm_tdiv_get_rate,
295 .set_rate = clk_pwm_tdiv_set_rate,
296 .round_rate = clk_pwm_tdiv_round_rate,
302 .parent = &clk_timer_scaler[1],
303 .get_rate = clk_pwm_tdiv_get_rate,
304 .set_rate = clk_pwm_tdiv_set_rate,
305 .round_rate = clk_pwm_tdiv_round_rate,
311 .parent = &clk_timer_scaler[1],
312 .get_rate = clk_pwm_tdiv_get_rate,
313 .set_rate = clk_pwm_tdiv_set_rate,
314 .round_rate = clk_pwm_tdiv_round_rate,
320 .parent = &clk_timer_scaler[1],
321 .get_rate = clk_pwm_tdiv_get_rate,
322 .set_rate = clk_pwm_tdiv_set_rate,
323 .round_rate = clk_pwm_tdiv_round_rate,
328 static int __init clk_pwm_tdiv_register(unsigned int id)
330 struct pwm_tdiv_clk *divclk = &clk_timer_tdiv[id];
331 unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
333 tcfg1 >>= S3C2410_TCFG1_SHIFT(id);
334 tcfg1 &= S3C2410_TCFG1_MUX_MASK;
337 divclk->divisor = tcfg_to_divisor(tcfg1);
339 return s3c24xx_register_clock(&divclk->clk);
342 static inline struct clk *s3c24xx_pwmclk_tclk(unsigned int id)
344 return (id >= 2) ? &clk_timer_tclk[1] : &clk_timer_tclk[0];
347 static inline struct clk *s3c24xx_pwmclk_tdiv(unsigned int id)
349 return &clk_timer_tdiv[id].clk;
352 static int clk_pwm_tin_set_parent(struct clk *clk, struct clk *parent)
354 unsigned int id = clk->id;
358 unsigned long shift = S3C2410_TCFG1_SHIFT(id);
360 if (parent == s3c24xx_pwmclk_tclk(id))
361 bits = S3C2410_TCFG1_MUX_TCLK << shift;
362 else if (parent == s3c24xx_pwmclk_tdiv(id))
363 bits = clk_pwm_tdiv_bits(to_tdiv(parent)) << shift;
367 clk->parent = parent;
369 local_irq_save(flags);
371 tcfg1 = __raw_readl(S3C2410_TCFG1);
372 tcfg1 &= ~(S3C2410_TCFG1_MUX_MASK << shift);
373 __raw_writel(tcfg1 | bits, S3C2410_TCFG1);
375 local_irq_restore(flags);
380 static struct clk clk_tin[] = {
384 .set_parent = clk_pwm_tin_set_parent,
389 .set_parent = clk_pwm_tin_set_parent,
394 .set_parent = clk_pwm_tin_set_parent,
399 .set_parent = clk_pwm_tin_set_parent,
404 .set_parent = clk_pwm_tin_set_parent,
408 static __init int clk_pwm_tin_register(struct clk *pwm)
410 unsigned long tcfg1 = __raw_readl(S3C2410_TCFG1);
411 unsigned int id = pwm->id;
416 ret = s3c24xx_register_clock(pwm);
420 tcfg1 >>= S3C2410_TCFG1_SHIFT(id);
421 tcfg1 &= S3C2410_TCFG1_MUX_MASK;
423 if (tcfg1 == S3C2410_TCFG1_MUX_TCLK)
424 parent = s3c24xx_pwmclk_tclk(id);
426 parent = s3c24xx_pwmclk_tdiv(id);
428 return clk_set_parent(pwm, parent);
431 static __init int s3c24xx_pwmclk_init(void)
433 struct clk *clk_timers;
437 clk_timers = clk_get(NULL, "timers");
438 if (IS_ERR(clk_timers)) {
439 printk(KERN_ERR "%s: no parent clock\n", __func__);
443 for (clk = 0; clk < ARRAY_SIZE(clk_timer_scaler); clk++) {
444 clk_timer_scaler[clk].parent = clk_timers;
445 ret = s3c24xx_register_clock(&clk_timer_scaler[clk]);
447 printk(KERN_ERR "error adding pwm scaler%d clock\n", clk);
452 for (clk = 0; clk < ARRAY_SIZE(clk_timer_tclk); clk++) {
453 ret = s3c24xx_register_clock(&clk_timer_tclk[clk]);
455 printk(KERN_ERR "error adding pww tclk%d\n", clk);
460 for (clk = 0; clk < ARRAY_SIZE(clk_timer_tdiv); clk++) {
461 ret = clk_pwm_tdiv_register(clk);
463 printk(KERN_ERR "error adding pwm%d tdiv clock\n", clk);
468 for (clk = 0; clk < ARRAY_SIZE(clk_tin); clk++) {
469 ret = clk_pwm_tin_register(&clk_tin[clk]);
471 printk(KERN_ERR "error adding pwm%d tin clock\n", clk);
482 arch_initcall(s3c24xx_pwmclk_init);