blob: 4db3be2140776a27e6e6207c9fb446702d338000 [file] [log] [blame]
Sascha Hauerf0948f52012-05-03 15:36:14 +05301/*
2 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
3 *
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.
7 *
8 * Standard functionality for the common clock API.
9 */
10#include <linux/module.h>
11#include <linux/clk-provider.h>
12#include <linux/slab.h>
13#include <linux/err.h>
Gregory CLEMENT79b16642013-04-12 13:57:44 +020014#include <linux/of.h>
Sascha Hauerf0948f52012-05-03 15:36:14 +053015
16/*
17 * DOC: basic fixed multiplier and divider clock that cannot gate
18 *
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
24 */
25
Sascha Hauerf0948f52012-05-03 15:36:14 +053026static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
27 unsigned long parent_rate)
28{
29 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
Haojian Zhuangbab53302012-12-03 16:14:37 +080030 unsigned long long int rate;
Sascha Hauerf0948f52012-05-03 15:36:14 +053031
Haojian Zhuangbab53302012-12-03 16:14:37 +080032 rate = (unsigned long long int)parent_rate * fix->mult;
33 do_div(rate, fix->div);
34 return (unsigned long)rate;
Sascha Hauerf0948f52012-05-03 15:36:14 +053035}
36
37static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
38 unsigned long *prate)
39{
40 struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
41
Stephen Boyd98d8a602015-06-29 16:56:30 -070042 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
Sascha Hauerf0948f52012-05-03 15:36:14 +053043 unsigned long best_parent;
44
45 best_parent = (rate / fix->mult) * fix->div;
Stephen Boyd2f508a92015-07-30 17:20:57 -070046 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
Sascha Hauerf0948f52012-05-03 15:36:14 +053047 }
48
49 return (*prate / fix->div) * fix->mult;
50}
51
52static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
53 unsigned long parent_rate)
54{
Daniel Thompson3037e9e2015-06-10 21:04:54 +010055 /*
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
58 * nop.
59 */
60
Sascha Hauerf0948f52012-05-03 15:36:14 +053061 return 0;
62}
63
Daniel Thompson3037e9e2015-06-10 21:04:54 +010064const struct clk_ops clk_fixed_factor_ops = {
Sascha Hauerf0948f52012-05-03 15:36:14 +053065 .round_rate = clk_factor_round_rate,
66 .set_rate = clk_factor_set_rate,
67 .recalc_rate = clk_factor_recalc_rate,
68};
69EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
70
Stephen Boyd0759ac82016-02-07 00:11:06 -080071struct clk_hw *clk_hw_register_fixed_factor(struct device *dev,
72 const char *name, const char *parent_name, unsigned long flags,
Sascha Hauerf0948f52012-05-03 15:36:14 +053073 unsigned int mult, unsigned int div)
74{
75 struct clk_fixed_factor *fix;
76 struct clk_init_data init;
Stephen Boyd0759ac82016-02-07 00:11:06 -080077 struct clk_hw *hw;
78 int ret;
Sascha Hauerf0948f52012-05-03 15:36:14 +053079
80 fix = kmalloc(sizeof(*fix), GFP_KERNEL);
Stephen Boydd122db72015-05-14 16:47:10 -070081 if (!fix)
Sascha Hauerf0948f52012-05-03 15:36:14 +053082 return ERR_PTR(-ENOMEM);
Sascha Hauerf0948f52012-05-03 15:36:14 +053083
84 /* struct clk_fixed_factor assignments */
85 fix->mult = mult;
86 fix->div = div;
87 fix->hw.init = &init;
88
89 init.name = name;
90 init.ops = &clk_fixed_factor_ops;
Rajendra Nayakf7d8caa2012-06-01 14:02:47 +053091 init.flags = flags | CLK_IS_BASIC;
Sascha Hauerf0948f52012-05-03 15:36:14 +053092 init.parent_names = &parent_name;
93 init.num_parents = 1;
94
Stephen Boyd0759ac82016-02-07 00:11:06 -080095 hw = &fix->hw;
96 ret = clk_hw_register(dev, hw);
97 if (ret) {
Sascha Hauerf0948f52012-05-03 15:36:14 +053098 kfree(fix);
Stephen Boyd0759ac82016-02-07 00:11:06 -080099 hw = ERR_PTR(ret);
100 }
Sascha Hauerf0948f52012-05-03 15:36:14 +0530101
Stephen Boyd0759ac82016-02-07 00:11:06 -0800102 return hw;
103}
104EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor);
105
106struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
107 const char *parent_name, unsigned long flags,
108 unsigned int mult, unsigned int div)
109{
110 struct clk_hw *hw;
111
112 hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
113 div);
114 if (IS_ERR(hw))
115 return ERR_CAST(hw);
116 return hw->clk;
Sascha Hauerf0948f52012-05-03 15:36:14 +0530117}
Mike Turquette5cfe10b2013-08-15 19:06:29 -0700118EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
119
Masahiro Yamadacbf95912016-01-06 13:25:09 +0900120void clk_unregister_fixed_factor(struct clk *clk)
121{
122 struct clk_hw *hw;
123
124 hw = __clk_get_hw(clk);
125 if (!hw)
126 return;
127
128 clk_unregister(clk);
129 kfree(to_clk_fixed_factor(hw));
130}
131EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
132
Stephen Boyd0759ac82016-02-07 00:11:06 -0800133void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
134{
135 struct clk_fixed_factor *fix;
136
137 fix = to_clk_fixed_factor(hw);
138
139 clk_hw_unregister(hw);
140 kfree(fix);
141}
142EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
143
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200144#ifdef CONFIG_OF
Maxime Riparde6cbf992016-06-22 11:15:54 +0200145static const struct of_device_id set_rate_parent_matches[] = {
146 { .compatible = "allwinner,sun4i-a10-pll3-2x-clk" },
147 { /* Sentinel */ },
148};
149
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200150/**
151 * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
152 */
153void __init of_fixed_factor_clk_setup(struct device_node *node)
154{
155 struct clk *clk;
156 const char *clk_name = node->name;
157 const char *parent_name;
Maxime Riparde6cbf992016-06-22 11:15:54 +0200158 unsigned long flags = 0;
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200159 u32 div, mult;
160
161 if (of_property_read_u32(node, "clock-div", &div)) {
162 pr_err("%s Fixed factor clock <%s> must have a clock-div property\n",
163 __func__, node->name);
164 return;
165 }
166
167 if (of_property_read_u32(node, "clock-mult", &mult)) {
Ezequiel Garciafe2fd5c2013-09-25 16:08:46 -0300168 pr_err("%s Fixed factor clock <%s> must have a clock-mult property\n",
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200169 __func__, node->name);
170 return;
171 }
172
173 of_property_read_string(node, "clock-output-names", &clk_name);
174 parent_name = of_clk_get_parent_name(node, 0);
175
Maxime Riparde6cbf992016-06-22 11:15:54 +0200176 if (of_match_node(set_rate_parent_matches, node))
177 flags |= CLK_SET_RATE_PARENT;
178
179 clk = clk_register_fixed_factor(NULL, clk_name, parent_name, flags,
Gregory CLEMENT79b16642013-04-12 13:57:44 +0200180 mult, div);
181 if (!IS_ERR(clk))
182 of_clk_add_provider(node, of_clk_src_simple_get, clk);
183}
184EXPORT_SYMBOL_GPL(of_fixed_factor_clk_setup);
185CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
186 of_fixed_factor_clk_setup);
187#endif