2 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Standard functionality for the common clock API.
10 #include <linux/module.h>
11 #include <linux/clk-provider.h>
12 #include <linux/slab.h>
13 #include <linux/err.h>
17 * DOC: basic fixed multiplier and divider clock that cannot gate
19 * Traits of this clock:
20 * prepare - clk_prepare only ensures that parents are prepared
21 * enable - clk_enable only ensures that parents are enabled
22 * rate - rate is fixed. clk->rate = parent->rate / div * mult
23 * parent - fixed parent. No clk_set_parent support
26 static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
27 unsigned long parent_rate)
29 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
30 unsigned long long int rate;
32 rate = (unsigned long long int)parent_rate * fix->mult;
33 do_div(rate, fix->div);
34 return (unsigned long)rate;
37 static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
40 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
42 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
43 unsigned long best_parent;
45 best_parent = (rate / fix->mult) * fix->div;
46 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
49 return (*prate / fix->div) * fix->mult;
52 static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
53 unsigned long parent_rate)
56 * We must report success but we can do so unconditionally because
57 * clk_factor_round_rate returns values that ensure this call is a
64 const struct clk_ops clk_fixed_factor_ops = {
65 .round_rate = clk_factor_round_rate,
66 .set_rate = clk_factor_set_rate,
67 .recalc_rate = clk_factor_recalc_rate,
69 EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
71 struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
72 const char *parent_name, unsigned long flags,
73 unsigned int mult, unsigned int div)
75 struct clk_fixed_factor *fix;
76 struct clk_init_data init;
79 fix = kmalloc(sizeof(*fix), GFP_KERNEL);
81 return ERR_PTR(-ENOMEM);
83 /* struct clk_fixed_factor assignments */
89 init.ops = &clk_fixed_factor_ops;
90 init.flags = flags | CLK_IS_BASIC;
91 init.parent_names = &parent_name;
94 clk = clk_register(dev, &fix->hw);
101 EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
105 * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
107 void __init of_fixed_factor_clk_setup(struct device_node *node)
110 const char *clk_name = node->name;
111 const char *parent_name;
114 if (of_property_read_u32(node, "clock-div", &div)) {
115 pr_err("%s Fixed factor clock <%s> must have a clock-div property\n",
116 __func__, node->name);
120 if (of_property_read_u32(node, "clock-mult", &mult)) {
121 pr_err("%s Fixed factor clock <%s> must have a clock-mult property\n",
122 __func__, node->name);
126 of_property_read_string(node, "clock-output-names", &clk_name);
127 parent_name = of_clk_get_parent_name(node, 0);
129 clk = clk_register_fixed_factor(NULL, clk_name, parent_name, 0,
132 of_clk_add_provider(node, of_clk_src_simple_get, clk);
134 EXPORT_SYMBOL_GPL(of_fixed_factor_clk_setup);
135 CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
136 of_fixed_factor_clk_setup);