2 * r8a7790 Common Clock Framework support
4 * Copyright (C) 2013 Renesas Solutions Corp.
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
13 #include <linux/clk-provider.h>
14 #include <linux/init.h>
16 #include <linux/kernel.h>
18 #include <linux/of_address.h>
19 #include <linux/slab.h>
23 #define CPG_DIV6_CKSTP BIT(8)
24 #define CPG_DIV6_DIV(d) ((d) & 0x3f)
25 #define CPG_DIV6_DIV_MASK 0x3f
28 * struct div6_clock - CPG 6 bit divider clock
29 * @hw: handle between common and hardware-specific interfaces
30 * @reg: IO-remapped register
31 * @div: divisor value (1-64)
32 * @src_shift: Shift to access the register bits to select the parent clock
33 * @src_width: Number of register bits to select the parent clock (may be 0)
34 * @parents: Array to map from valid parent clocks indices to hardware indices
45 #define to_div6_clock(_hw) container_of(_hw, struct div6_clock, hw)
47 static int cpg_div6_clock_enable(struct clk_hw *hw)
49 struct div6_clock *clock = to_div6_clock(hw);
52 val = (clk_readl(clock->reg) & ~(CPG_DIV6_DIV_MASK | CPG_DIV6_CKSTP))
53 | CPG_DIV6_DIV(clock->div - 1);
54 clk_writel(val, clock->reg);
59 static void cpg_div6_clock_disable(struct clk_hw *hw)
61 struct div6_clock *clock = to_div6_clock(hw);
64 val = clk_readl(clock->reg);
65 val |= CPG_DIV6_CKSTP;
67 * DIV6 clocks require the divisor field to be non-zero when stopping
68 * the clock. However, some clocks (e.g. ZB on sh73a0) fail to be
69 * re-enabled later if the divisor field is changed when stopping the
72 if (!(val & CPG_DIV6_DIV_MASK))
73 val |= CPG_DIV6_DIV_MASK;
74 clk_writel(val, clock->reg);
77 static int cpg_div6_clock_is_enabled(struct clk_hw *hw)
79 struct div6_clock *clock = to_div6_clock(hw);
81 return !(clk_readl(clock->reg) & CPG_DIV6_CKSTP);
84 static unsigned long cpg_div6_clock_recalc_rate(struct clk_hw *hw,
85 unsigned long parent_rate)
87 struct div6_clock *clock = to_div6_clock(hw);
89 return parent_rate / clock->div;
92 static unsigned int cpg_div6_clock_calc_div(unsigned long rate,
93 unsigned long parent_rate)
100 div = DIV_ROUND_CLOSEST(parent_rate, rate);
101 return clamp_t(unsigned int, div, 1, 64);
104 static long cpg_div6_clock_round_rate(struct clk_hw *hw, unsigned long rate,
105 unsigned long *parent_rate)
107 unsigned int div = cpg_div6_clock_calc_div(rate, *parent_rate);
109 return *parent_rate / div;
112 static int cpg_div6_clock_set_rate(struct clk_hw *hw, unsigned long rate,
113 unsigned long parent_rate)
115 struct div6_clock *clock = to_div6_clock(hw);
116 unsigned int div = cpg_div6_clock_calc_div(rate, parent_rate);
121 val = clk_readl(clock->reg) & ~CPG_DIV6_DIV_MASK;
122 /* Only program the new divisor if the clock isn't stopped. */
123 if (!(val & CPG_DIV6_CKSTP))
124 clk_writel(val | CPG_DIV6_DIV(clock->div - 1), clock->reg);
129 static u8 cpg_div6_clock_get_parent(struct clk_hw *hw)
131 struct div6_clock *clock = to_div6_clock(hw);
135 if (clock->src_width == 0)
138 hw_index = (clk_readl(clock->reg) >> clock->src_shift) &
139 (BIT(clock->src_width) - 1);
140 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
141 if (clock->parents[i] == hw_index)
145 pr_err("%s: %s DIV6 clock set to invalid parent %u\n",
146 __func__, clk_hw_get_name(hw), hw_index);
150 static int cpg_div6_clock_set_parent(struct clk_hw *hw, u8 index)
152 struct div6_clock *clock = to_div6_clock(hw);
156 if (index >= clk_hw_get_num_parents(hw))
159 mask = ~((BIT(clock->src_width) - 1) << clock->src_shift);
160 hw_index = clock->parents[index];
162 clk_writel((clk_readl(clock->reg) & mask) |
163 (hw_index << clock->src_shift), clock->reg);
168 static const struct clk_ops cpg_div6_clock_ops = {
169 .enable = cpg_div6_clock_enable,
170 .disable = cpg_div6_clock_disable,
171 .is_enabled = cpg_div6_clock_is_enabled,
172 .get_parent = cpg_div6_clock_get_parent,
173 .set_parent = cpg_div6_clock_set_parent,
174 .recalc_rate = cpg_div6_clock_recalc_rate,
175 .round_rate = cpg_div6_clock_round_rate,
176 .set_rate = cpg_div6_clock_set_rate,
181 * cpg_div6_register - Register a DIV6 clock
182 * @name: Name of the DIV6 clock
183 * @num_parents: Number of parent clocks of the DIV6 clock (1, 4, or 8)
184 * @parent_names: Array containing the names of the parent clocks
185 * @reg: Mapped register used to control the DIV6 clock
187 struct clk * __init cpg_div6_register(const char *name,
188 unsigned int num_parents,
189 const char **parent_names,
192 unsigned int valid_parents;
193 struct clk_init_data init;
194 struct div6_clock *clock;
198 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
200 return ERR_PTR(-ENOMEM);
202 clock->parents = kmalloc_array(num_parents, sizeof(*clock->parents),
204 if (!clock->parents) {
205 clk = ERR_PTR(-ENOMEM);
212 * Read the divisor. Disabling the clock overwrites the divisor, so we
213 * need to cache its value for the enable operation.
215 clock->div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
217 switch (num_parents) {
219 /* fixed parent clock */
220 clock->src_shift = clock->src_width = 0;
223 /* clock with EXSRC bits 6-7 */
224 clock->src_shift = 6;
225 clock->src_width = 2;
228 /* VCLK with EXSRC bits 12-14 */
229 clock->src_shift = 12;
230 clock->src_width = 3;
233 pr_err("%s: invalid number of parents for DIV6 clock %s\n",
235 clk = ERR_PTR(-EINVAL);
239 /* Filter out invalid parents */
240 for (i = 0, valid_parents = 0; i < num_parents; i++) {
241 if (parent_names[i]) {
242 parent_names[valid_parents] = parent_names[i];
243 clock->parents[valid_parents] = i;
248 /* Register the clock. */
250 init.ops = &cpg_div6_clock_ops;
251 init.flags = CLK_IS_BASIC;
252 init.parent_names = parent_names;
253 init.num_parents = valid_parents;
255 clock->hw.init = &init;
257 clk = clk_register(NULL, &clock->hw);
264 kfree(clock->parents);
270 static void __init cpg_div6_clock_init(struct device_node *np)
272 unsigned int num_parents;
273 const char **parent_names;
274 const char *clk_name = np->name;
279 num_parents = of_clk_get_parent_count(np);
280 if (num_parents < 1) {
281 pr_err("%s: no parent found for %s DIV6 clock\n",
286 parent_names = kmalloc_array(num_parents, sizeof(*parent_names),
291 reg = of_iomap(np, 0);
293 pr_err("%s: failed to map %s DIV6 clock register\n",
298 /* Parse the DT properties. */
299 of_property_read_string(np, "clock-output-names", &clk_name);
301 for (i = 0; i < num_parents; i++)
302 parent_names[i] = of_clk_get_parent_name(np, i);
304 clk = cpg_div6_register(clk_name, num_parents, parent_names, reg);
306 pr_err("%s: failed to register %s DIV6 clock (%ld)\n",
307 __func__, np->name, PTR_ERR(clk));
311 of_clk_add_provider(np, of_clk_src_simple_get, clk);
321 CLK_OF_DECLARE(cpg_div6_clk, "renesas,cpg-div6-clock", cpg_div6_clock_init);