diff options
author | Viresh Kumar <viresh.kumar@st.com> | 2012-04-11 08:34:23 -0400 |
---|---|---|
committer | Arnd Bergmann <arnd@arndb.de> | 2012-05-12 15:19:27 -0400 |
commit | a45896bd3a4b7beb571fa704efa7c2782b791093 (patch) | |
tree | abbcf23625304e123839af8af6e5ba278d20797e /drivers/clk | |
parent | 270b9f421e66ee5d135c99ba1c2b883c7750ab6c (diff) |
SPEAr: clk: Add General Purpose Timer Synthesizer clock
All SPEAr SoC's contain GPT Synthesizers. Their Fout is derived from
following equations:
Fout= Fin/((2 ^ (N+1)) * (M+1))
This patch adds in support for this type of clock.
Signed-off-by: Viresh Kumar <viresh.kumar@st.com>
Reviewed-by: Mike Turquette <mturquette@linaro.org>
Diffstat (limited to 'drivers/clk')
-rw-r--r-- | drivers/clk/spear/Makefile | 2 | ||||
-rw-r--r-- | drivers/clk/spear/clk-gpt-synth.c | 154 | ||||
-rw-r--r-- | drivers/clk/spear/clk.h | 17 |
3 files changed, 172 insertions, 1 deletions
diff --git a/drivers/clk/spear/Makefile b/drivers/clk/spear/Makefile index 182703622195..9e64824a8000 100644 --- a/drivers/clk/spear/Makefile +++ b/drivers/clk/spear/Makefile | |||
@@ -2,4 +2,4 @@ | |||
2 | # SPEAr Clock specific Makefile | 2 | # SPEAr Clock specific Makefile |
3 | # | 3 | # |
4 | 4 | ||
5 | obj-y += clk.o clk-aux-synth.o clk-frac-synth.o clk-vco-pll.o | 5 | obj-y += clk.o clk-aux-synth.o clk-frac-synth.o clk-gpt-synth.o clk-vco-pll.o |
diff --git a/drivers/clk/spear/clk-gpt-synth.c b/drivers/clk/spear/clk-gpt-synth.c new file mode 100644 index 000000000000..b471c9762a97 --- /dev/null +++ b/drivers/clk/spear/clk-gpt-synth.c | |||
@@ -0,0 +1,154 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2012 ST Microelectronics | ||
3 | * Viresh Kumar <viresh.kumar@st.com> | ||
4 | * | ||
5 | * This file is licensed under the terms of the GNU General Public | ||
6 | * License version 2. This program is licensed "as is" without any | ||
7 | * warranty of any kind, whether express or implied. | ||
8 | * | ||
9 | * General Purpose Timer Synthesizer clock implementation | ||
10 | */ | ||
11 | |||
12 | #define pr_fmt(fmt) "clk-gpt-synth: " fmt | ||
13 | |||
14 | #include <linux/clk-provider.h> | ||
15 | #include <linux/slab.h> | ||
16 | #include <linux/io.h> | ||
17 | #include <linux/err.h> | ||
18 | #include "clk.h" | ||
19 | |||
20 | #define GPT_MSCALE_MASK 0xFFF | ||
21 | #define GPT_NSCALE_SHIFT 12 | ||
22 | #define GPT_NSCALE_MASK 0xF | ||
23 | |||
24 | /* | ||
25 | * DOC: General Purpose Timer Synthesizer clock | ||
26 | * | ||
27 | * Calculates gpt synth clk rate for different values of mscale and nscale | ||
28 | * | ||
29 | * Fout= Fin/((2 ^ (N+1)) * (M+1)) | ||
30 | */ | ||
31 | |||
32 | #define to_clk_gpt(_hw) container_of(_hw, struct clk_gpt, hw) | ||
33 | |||
34 | static unsigned long gpt_calc_rate(struct clk_hw *hw, unsigned long prate, | ||
35 | int index) | ||
36 | { | ||
37 | struct clk_gpt *gpt = to_clk_gpt(hw); | ||
38 | struct gpt_rate_tbl *rtbl = gpt->rtbl; | ||
39 | |||
40 | prate /= ((1 << (rtbl[index].nscale + 1)) * (rtbl[index].mscale + 1)); | ||
41 | |||
42 | return prate; | ||
43 | } | ||
44 | |||
45 | static long clk_gpt_round_rate(struct clk_hw *hw, unsigned long drate, | ||
46 | unsigned long *prate) | ||
47 | { | ||
48 | struct clk_gpt *gpt = to_clk_gpt(hw); | ||
49 | int unused; | ||
50 | |||
51 | return clk_round_rate_index(hw, drate, *prate, gpt_calc_rate, | ||
52 | gpt->rtbl_cnt, &unused); | ||
53 | } | ||
54 | |||
55 | static unsigned long clk_gpt_recalc_rate(struct clk_hw *hw, | ||
56 | unsigned long parent_rate) | ||
57 | { | ||
58 | struct clk_gpt *gpt = to_clk_gpt(hw); | ||
59 | unsigned long flags = 0; | ||
60 | unsigned int div = 1, val; | ||
61 | |||
62 | if (gpt->lock) | ||
63 | spin_lock_irqsave(gpt->lock, flags); | ||
64 | |||
65 | val = readl_relaxed(gpt->reg); | ||
66 | |||
67 | if (gpt->lock) | ||
68 | spin_unlock_irqrestore(gpt->lock, flags); | ||
69 | |||
70 | div += val & GPT_MSCALE_MASK; | ||
71 | div *= 1 << (((val >> GPT_NSCALE_SHIFT) & GPT_NSCALE_MASK) + 1); | ||
72 | |||
73 | if (!div) | ||
74 | return 0; | ||
75 | |||
76 | return parent_rate / div; | ||
77 | } | ||
78 | |||
79 | /* Configures new clock rate of gpt */ | ||
80 | static int clk_gpt_set_rate(struct clk_hw *hw, unsigned long drate, | ||
81 | unsigned long prate) | ||
82 | { | ||
83 | struct clk_gpt *gpt = to_clk_gpt(hw); | ||
84 | struct gpt_rate_tbl *rtbl = gpt->rtbl; | ||
85 | unsigned long flags = 0, val; | ||
86 | int i; | ||
87 | |||
88 | clk_round_rate_index(hw, drate, prate, gpt_calc_rate, gpt->rtbl_cnt, | ||
89 | &i); | ||
90 | |||
91 | if (gpt->lock) | ||
92 | spin_lock_irqsave(gpt->lock, flags); | ||
93 | |||
94 | val = readl(gpt->reg) & ~GPT_MSCALE_MASK; | ||
95 | val &= ~(GPT_NSCALE_MASK << GPT_NSCALE_SHIFT); | ||
96 | |||
97 | val |= rtbl[i].mscale & GPT_MSCALE_MASK; | ||
98 | val |= (rtbl[i].nscale & GPT_NSCALE_MASK) << GPT_NSCALE_SHIFT; | ||
99 | |||
100 | writel_relaxed(val, gpt->reg); | ||
101 | |||
102 | if (gpt->lock) | ||
103 | spin_unlock_irqrestore(gpt->lock, flags); | ||
104 | |||
105 | return 0; | ||
106 | } | ||
107 | |||
108 | static struct clk_ops clk_gpt_ops = { | ||
109 | .recalc_rate = clk_gpt_recalc_rate, | ||
110 | .round_rate = clk_gpt_round_rate, | ||
111 | .set_rate = clk_gpt_set_rate, | ||
112 | }; | ||
113 | |||
114 | struct clk *clk_register_gpt(const char *name, const char *parent_name, unsigned | ||
115 | long flags, void __iomem *reg, struct gpt_rate_tbl *rtbl, u8 | ||
116 | rtbl_cnt, spinlock_t *lock) | ||
117 | { | ||
118 | struct clk_init_data init; | ||
119 | struct clk_gpt *gpt; | ||
120 | struct clk *clk; | ||
121 | |||
122 | if (!name || !parent_name || !reg || !rtbl || !rtbl_cnt) { | ||
123 | pr_err("Invalid arguments passed"); | ||
124 | return ERR_PTR(-EINVAL); | ||
125 | } | ||
126 | |||
127 | gpt = kzalloc(sizeof(*gpt), GFP_KERNEL); | ||
128 | if (!gpt) { | ||
129 | pr_err("could not allocate gpt clk\n"); | ||
130 | return ERR_PTR(-ENOMEM); | ||
131 | } | ||
132 | |||
133 | /* struct clk_gpt assignments */ | ||
134 | gpt->reg = reg; | ||
135 | gpt->rtbl = rtbl; | ||
136 | gpt->rtbl_cnt = rtbl_cnt; | ||
137 | gpt->lock = lock; | ||
138 | gpt->hw.init = &init; | ||
139 | |||
140 | init.name = name; | ||
141 | init.ops = &clk_gpt_ops; | ||
142 | init.flags = flags; | ||
143 | init.parent_names = &parent_name; | ||
144 | init.num_parents = 1; | ||
145 | |||
146 | clk = clk_register(NULL, &gpt->hw); | ||
147 | if (!IS_ERR_OR_NULL(clk)) | ||
148 | return clk; | ||
149 | |||
150 | pr_err("clk register failed\n"); | ||
151 | kfree(gpt); | ||
152 | |||
153 | return NULL; | ||
154 | } | ||
diff --git a/drivers/clk/spear/clk.h b/drivers/clk/spear/clk.h index ac9030bbd6ce..3321c46a071c 100644 --- a/drivers/clk/spear/clk.h +++ b/drivers/clk/spear/clk.h | |||
@@ -68,6 +68,20 @@ struct clk_frac { | |||
68 | spinlock_t *lock; | 68 | spinlock_t *lock; |
69 | }; | 69 | }; |
70 | 70 | ||
71 | /* GPT clk */ | ||
72 | struct gpt_rate_tbl { | ||
73 | u16 mscale; | ||
74 | u16 nscale; | ||
75 | }; | ||
76 | |||
77 | struct clk_gpt { | ||
78 | struct clk_hw hw; | ||
79 | void __iomem *reg; | ||
80 | struct gpt_rate_tbl *rtbl; | ||
81 | u8 rtbl_cnt; | ||
82 | spinlock_t *lock; | ||
83 | }; | ||
84 | |||
71 | /* VCO-PLL clk */ | 85 | /* VCO-PLL clk */ |
72 | struct pll_rate_tbl { | 86 | struct pll_rate_tbl { |
73 | u8 mode; | 87 | u8 mode; |
@@ -103,6 +117,9 @@ struct clk *clk_register_aux(const char *aux_name, const char *gate_name, | |||
103 | struct clk *clk_register_frac(const char *name, const char *parent_name, | 117 | struct clk *clk_register_frac(const char *name, const char *parent_name, |
104 | unsigned long flags, void __iomem *reg, | 118 | unsigned long flags, void __iomem *reg, |
105 | struct frac_rate_tbl *rtbl, u8 rtbl_cnt, spinlock_t *lock); | 119 | struct frac_rate_tbl *rtbl, u8 rtbl_cnt, spinlock_t *lock); |
120 | struct clk *clk_register_gpt(const char *name, const char *parent_name, unsigned | ||
121 | long flags, void __iomem *reg, struct gpt_rate_tbl *rtbl, u8 | ||
122 | rtbl_cnt, spinlock_t *lock); | ||
106 | struct clk *clk_register_vco_pll(const char *vco_name, const char *pll_name, | 123 | struct clk *clk_register_vco_pll(const char *vco_name, const char *pll_name, |
107 | const char *vco_gate_name, const char *parent_name, | 124 | const char *vco_gate_name, const char *parent_name, |
108 | unsigned long flags, void __iomem *mode_reg, void __iomem | 125 | unsigned long flags, void __iomem *mode_reg, void __iomem |