2 * clkgen-mux.c: ST GEN-MUX Clock driver
4 * Copyright (C) 2014 STMicroelectronics (R&D) Limited
6 * Authors: Stephen Gallimore <stephen.gallimore@st.com>
7 * Pankaj Dev <pankaj.dev@st.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
16 #include <linux/slab.h>
17 #include <linux/of_address.h>
18 #include <linux/clk-provider.h>
20 static DEFINE_SPINLOCK(clkgena_divmux_lock);
22 static const char ** __init clkgen_mux_get_parents(struct device_node *np,
28 nparents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
29 if (WARN_ON(nparents <= 0))
30 return ERR_PTR(-EINVAL);
32 parents = kzalloc(nparents * sizeof(const char *), GFP_KERNEL);
34 return ERR_PTR(-ENOMEM);
36 for (i = 0; i < nparents; i++)
37 parents[i] = of_clk_get_parent_name(np, i);
39 *num_parents = nparents;
44 * DOC: Clock mux with a programmable divider on each of its three inputs.
45 * The mux has an input setting which effectively gates its output.
47 * Traits of this clock:
48 * prepare - clk_(un)prepare only ensures parent is (un)prepared
49 * enable - clk_enable and clk_disable are functional & control gating
50 * rate - set rate is supported
51 * parent - set/get parent
56 struct clkgena_divmux {
58 /* Subclassed mux and divider structures */
60 struct clk_divider div[NUM_INPUTS];
61 /* Enable/running feedback register bits for each input */
62 void __iomem *feedback_reg[NUM_INPUTS];
68 #define to_clkgena_divmux(_hw) container_of(_hw, struct clkgena_divmux, hw)
70 struct clkgena_divmux_data {
75 int div_offsets[NUM_INPUTS];
76 int fb_offsets[NUM_INPUTS];
80 #define CKGAX_CLKOPSRC_SWITCH_OFF 0x3
82 static int clkgena_divmux_is_running(struct clkgena_divmux *mux)
84 u32 regval = readl(mux->feedback_reg[mux->muxsel]);
85 u32 running = regval & BIT(mux->feedback_bit_idx);
89 static int clkgena_divmux_enable(struct clk_hw *hw)
91 struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
92 struct clk_hw *mux_hw = &genamux->mux.hw;
93 unsigned long timeout;
96 mux_hw->clk = hw->clk;
98 ret = clk_mux_ops.set_parent(mux_hw, genamux->muxsel);
102 timeout = jiffies + msecs_to_jiffies(10);
104 while (!clkgena_divmux_is_running(genamux)) {
105 if (time_after(jiffies, timeout))
113 static void clkgena_divmux_disable(struct clk_hw *hw)
115 struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
116 struct clk_hw *mux_hw = &genamux->mux.hw;
118 mux_hw->clk = hw->clk;
120 clk_mux_ops.set_parent(mux_hw, CKGAX_CLKOPSRC_SWITCH_OFF);
123 static int clkgena_divmux_is_enabled(struct clk_hw *hw)
125 struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
126 struct clk_hw *mux_hw = &genamux->mux.hw;
128 mux_hw->clk = hw->clk;
130 return (s8)clk_mux_ops.get_parent(mux_hw) > 0;
133 u8 clkgena_divmux_get_parent(struct clk_hw *hw)
135 struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
136 struct clk_hw *mux_hw = &genamux->mux.hw;
138 mux_hw->clk = hw->clk;
140 genamux->muxsel = clk_mux_ops.get_parent(mux_hw);
141 if ((s8)genamux->muxsel < 0) {
142 pr_debug("%s: %s: Invalid parent, setting to default.\n",
143 __func__, __clk_get_name(hw->clk));
147 return genamux->muxsel;
150 static int clkgena_divmux_set_parent(struct clk_hw *hw, u8 index)
152 struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
154 if (index >= CKGAX_CLKOPSRC_SWITCH_OFF)
157 genamux->muxsel = index;
160 * If the mux is already enabled, call enable directly to set the
161 * new mux position and wait for it to start running again. Otherwise
164 if (clkgena_divmux_is_enabled(hw))
165 clkgena_divmux_enable(hw);
170 unsigned long clkgena_divmux_recalc_rate(struct clk_hw *hw,
171 unsigned long parent_rate)
173 struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
174 struct clk_hw *div_hw = &genamux->div[genamux->muxsel].hw;
176 div_hw->clk = hw->clk;
178 return clk_divider_ops.recalc_rate(div_hw, parent_rate);
181 static int clkgena_divmux_set_rate(struct clk_hw *hw, unsigned long rate,
182 unsigned long parent_rate)
184 struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
185 struct clk_hw *div_hw = &genamux->div[genamux->muxsel].hw;
187 div_hw->clk = hw->clk;
189 return clk_divider_ops.set_rate(div_hw, rate, parent_rate);
192 static long clkgena_divmux_round_rate(struct clk_hw *hw, unsigned long rate,
193 unsigned long *prate)
195 struct clkgena_divmux *genamux = to_clkgena_divmux(hw);
196 struct clk_hw *div_hw = &genamux->div[genamux->muxsel].hw;
198 div_hw->clk = hw->clk;
200 return clk_divider_ops.round_rate(div_hw, rate, prate);
203 static const struct clk_ops clkgena_divmux_ops = {
204 .enable = clkgena_divmux_enable,
205 .disable = clkgena_divmux_disable,
206 .is_enabled = clkgena_divmux_is_enabled,
207 .get_parent = clkgena_divmux_get_parent,
208 .set_parent = clkgena_divmux_set_parent,
209 .round_rate = clkgena_divmux_round_rate,
210 .recalc_rate = clkgena_divmux_recalc_rate,
211 .set_rate = clkgena_divmux_set_rate,
215 * clk_register_genamux - register a genamux clock with the clock framework
217 struct clk *clk_register_genamux(const char *name,
218 const char **parent_names, u8 num_parents,
220 const struct clkgena_divmux_data *muxdata,
224 * Fixed constants across all ClockgenA variants
226 const int mux_width = 2;
227 const int divider_width = 5;
228 struct clkgena_divmux *genamux;
230 struct clk_init_data init;
233 genamux = kzalloc(sizeof(*genamux), GFP_KERNEL);
235 return ERR_PTR(-ENOMEM);
238 init.ops = &clkgena_divmux_ops;
239 init.flags = CLK_IS_BASIC;
240 init.parent_names = parent_names;
241 init.num_parents = num_parents;
243 genamux->mux.lock = &clkgena_divmux_lock;
244 genamux->mux.mask = BIT(mux_width) - 1;
245 genamux->mux.shift = muxdata->mux_start_bit + (idx * mux_width);
246 if (genamux->mux.shift > 31) {
248 * We have spilled into the second mux register so
249 * adjust the register address and the bit shift accordingly
251 genamux->mux.reg = reg + muxdata->mux_offset2;
252 genamux->mux.shift -= 32;
254 genamux->mux.reg = reg + muxdata->mux_offset;
257 for (i = 0; i < NUM_INPUTS; i++) {
259 * Divider config for each input
261 void __iomem *divbase = reg + muxdata->div_offsets[i];
262 genamux->div[i].width = divider_width;
263 genamux->div[i].reg = divbase + (idx * sizeof(u32));
266 * Mux enabled/running feedback register for each input.
268 genamux->feedback_reg[i] = reg + muxdata->fb_offsets[i];
271 genamux->feedback_bit_idx = muxdata->fb_start_bit_idx + idx;
272 genamux->hw.init = &init;
274 clk = clk_register(NULL, &genamux->hw);
280 pr_debug("%s: parent %s rate %lu\n",
282 __clk_get_name(clk_get_parent(clk)),
288 static struct clkgena_divmux_data st_divmux_c65hs = {
292 .div_offsets = { 0x800, 0x900, 0xb00 },
293 .fb_offsets = { 0x18, 0x1c, 0x20 },
294 .fb_start_bit_idx = 0,
297 static struct clkgena_divmux_data st_divmux_c65ls = {
302 .div_offsets = { 0x810, 0xa10, 0xb10 },
303 .fb_offsets = { 0x18, 0x1c, 0x20 },
304 .fb_start_bit_idx = 4,
307 static struct clkgena_divmux_data st_divmux_c32odf0 = {
311 .div_offsets = { 0x800, 0x900, 0xa60 },
312 .fb_offsets = { 0x2c, 0x24, 0x28 },
313 .fb_start_bit_idx = 0,
316 static struct clkgena_divmux_data st_divmux_c32odf1 = {
320 .div_offsets = { 0x820, 0x980, 0xa80 },
321 .fb_offsets = { 0x2c, 0x24, 0x28 },
322 .fb_start_bit_idx = 8,
325 static struct clkgena_divmux_data st_divmux_c32odf2 = {
329 .div_offsets = { 0x840, 0xa20, 0xb10 },
330 .fb_offsets = { 0x2c, 0x24, 0x28 },
331 .fb_start_bit_idx = 16,
334 static struct clkgena_divmux_data st_divmux_c32odf3 = {
338 .div_offsets = { 0x860, 0xa40, 0xb30 },
339 .fb_offsets = { 0x2c, 0x24, 0x28 },
340 .fb_start_bit_idx = 24,
343 static struct of_device_id clkgena_divmux_of_match[] = {
345 .compatible = "st,clkgena-divmux-c65-hs",
346 .data = &st_divmux_c65hs,
349 .compatible = "st,clkgena-divmux-c65-ls",
350 .data = &st_divmux_c65ls,
353 .compatible = "st,clkgena-divmux-c32-odf0",
354 .data = &st_divmux_c32odf0,
357 .compatible = "st,clkgena-divmux-c32-odf1",
358 .data = &st_divmux_c32odf1,
361 .compatible = "st,clkgena-divmux-c32-odf2",
362 .data = &st_divmux_c32odf2,
365 .compatible = "st,clkgena-divmux-c32-odf3",
366 .data = &st_divmux_c32odf3,
371 static void __iomem * __init clkgen_get_register_base(
372 struct device_node *np)
374 struct device_node *pnode;
375 void __iomem *reg = NULL;
377 pnode = of_get_parent(np);
381 reg = of_iomap(pnode, 0);
387 void __init st_of_clkgena_divmux_setup(struct device_node *np)
389 const struct of_device_id *match;
390 const struct clkgena_divmux_data *data;
391 struct clk_onecell_data *clk_data;
393 const char **parents;
394 int num_parents = 0, i;
396 match = of_match_node(clkgena_divmux_of_match, np);
400 data = (struct clkgena_divmux_data *)match->data;
402 reg = clkgen_get_register_base(np);
406 parents = clkgen_mux_get_parents(np, &num_parents);
410 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
414 clk_data->clk_num = data->num_outputs;
415 clk_data->clks = kzalloc(clk_data->clk_num * sizeof(struct clk *),
421 for (i = 0; i < clk_data->clk_num; i++) {
423 const char *clk_name;
425 if (of_property_read_string_index(np, "clock-output-names",
430 * If we read an empty clock name then the output is unused
432 if (*clk_name == '\0')
435 clk = clk_register_genamux(clk_name, parents, num_parents,
441 clk_data->clks[i] = clk;
446 of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
450 kfree(clk_data->clks);
455 CLK_OF_DECLARE(clkgenadivmux, "st,clkgena-divmux", st_of_clkgena_divmux_setup);
457 struct clkgena_prediv_data {
460 struct clk_div_table *table;
463 static struct clk_div_table prediv_table16[] = {
464 { .val = 0, .div = 1 },
465 { .val = 1, .div = 16 },
469 static struct clkgena_prediv_data prediv_c65_data = {
472 .table = prediv_table16,
475 static struct clkgena_prediv_data prediv_c32_data = {
478 .table = prediv_table16,
481 static struct of_device_id clkgena_prediv_of_match[] = {
482 { .compatible = "st,clkgena-prediv-c65", .data = &prediv_c65_data },
483 { .compatible = "st,clkgena-prediv-c32", .data = &prediv_c32_data },
487 void __init st_of_clkgena_prediv_setup(struct device_node *np)
489 const struct of_device_id *match;
491 const char *parent_name, *clk_name;
493 struct clkgena_prediv_data *data;
495 match = of_match_node(clkgena_prediv_of_match, np);
497 pr_err("%s: No matching data\n", __func__);
501 data = (struct clkgena_prediv_data *)match->data;
503 reg = clkgen_get_register_base(np);
507 parent_name = of_clk_get_parent_name(np, 0);
511 if (of_property_read_string_index(np, "clock-output-names",
515 clk = clk_register_divider_table(NULL, clk_name, parent_name, 0,
516 reg + data->offset, data->shift, 1,
517 0, data->table, NULL);
521 of_clk_add_provider(np, of_clk_src_simple_get, clk);
522 pr_debug("%s: parent %s rate %u\n",
524 __clk_get_name(clk_get_parent(clk)),
525 (unsigned int)clk_get_rate(clk));
529 CLK_OF_DECLARE(clkgenaprediv, "st,clkgena-prediv", st_of_clkgena_prediv_setup);