blob: 6d6475c32ee51612df97f7fcce8c952a004ccdeb [file] [log] [blame]
Mike Turquette9d9f78e2012-03-15 23:11:20 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Fixed rate clock implementation
10 */
11
12#include <linux/clk-provider.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/io.h>
16#include <linux/err.h>
Grant Likely015ba402012-04-07 21:39:39 -050017#include <linux/of.h>
Ricardo Ribalda Delgado435779f2016-07-05 18:23:34 +020018#include <linux/platform_device.h>
Mike Turquette9d9f78e2012-03-15 23:11:20 -070019
20/*
21 * DOC: basic fixed-rate clock that cannot gate
22 *
23 * Traits of this clock:
24 * prepare - clk_(un)prepare only ensures parents are prepared
25 * enable - clk_enable only ensures parents are enabled
26 * rate - rate is always a fixed value. No clk_set_rate support
27 * parent - fixed parent. No clk_set_parent support
28 */
29
Mike Turquette9d9f78e2012-03-15 23:11:20 -070030static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw,
31 unsigned long parent_rate)
32{
33 return to_clk_fixed_rate(hw)->fixed_rate;
34}
Mike Turquette9d9f78e2012-03-15 23:11:20 -070035
Boris BREZILLON0903ea62013-12-21 10:34:48 +010036static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw,
37 unsigned long parent_accuracy)
38{
39 return to_clk_fixed_rate(hw)->fixed_accuracy;
40}
41
Shawn Guo822c2502012-03-27 15:23:22 +080042const struct clk_ops clk_fixed_rate_ops = {
Mike Turquette9d9f78e2012-03-15 23:11:20 -070043 .recalc_rate = clk_fixed_rate_recalc_rate,
Boris BREZILLON0903ea62013-12-21 10:34:48 +010044 .recalc_accuracy = clk_fixed_rate_recalc_accuracy,
Mike Turquette9d9f78e2012-03-15 23:11:20 -070045};
46EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);
47
Mike Turquette27d54592012-03-26 17:51:03 -070048/**
Stephen Boyd26ef56b2016-02-07 00:34:13 -080049 * clk_hw_register_fixed_rate_with_accuracy - register fixed-rate clock with
50 * the clock framework
Mike Turquette27d54592012-03-26 17:51:03 -070051 * @dev: device that is registering this clock
52 * @name: name of this clock
53 * @parent_name: name of clock's parent
54 * @flags: framework-specific flags
55 * @fixed_rate: non-adjustable clock rate
Boris BREZILLON0903ea62013-12-21 10:34:48 +010056 * @fixed_accuracy: non-adjustable clock rate
Mike Turquette27d54592012-03-26 17:51:03 -070057 */
Stephen Boyd26ef56b2016-02-07 00:34:13 -080058struct clk_hw *clk_hw_register_fixed_rate_with_accuracy(struct device *dev,
Boris BREZILLON0903ea62013-12-21 10:34:48 +010059 const char *name, const char *parent_name, unsigned long flags,
60 unsigned long fixed_rate, unsigned long fixed_accuracy)
Mike Turquette9d9f78e2012-03-15 23:11:20 -070061{
62 struct clk_fixed_rate *fixed;
Stephen Boyd26ef56b2016-02-07 00:34:13 -080063 struct clk_hw *hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -070064 struct clk_init_data init;
Stephen Boyd26ef56b2016-02-07 00:34:13 -080065 int ret;
Mike Turquette9d9f78e2012-03-15 23:11:20 -070066
Mike Turquette27d54592012-03-26 17:51:03 -070067 /* allocate fixed-rate clock */
Stephen Boydd122db72015-05-14 16:47:10 -070068 fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
69 if (!fixed)
Mike Turquette9d9f78e2012-03-15 23:11:20 -070070 return ERR_PTR(-ENOMEM);
Mike Turquette9d9f78e2012-03-15 23:11:20 -070071
Saravana Kannan0197b3e2012-04-25 22:58:56 -070072 init.name = name;
73 init.ops = &clk_fixed_rate_ops;
Rajendra Nayakf7d8caa2012-06-01 14:02:47 +053074 init.flags = flags | CLK_IS_BASIC;
Saravana Kannan0197b3e2012-04-25 22:58:56 -070075 init.parent_names = (parent_name ? &parent_name: NULL);
76 init.num_parents = (parent_name ? 1 : 0);
77
Mike Turquette9d9f78e2012-03-15 23:11:20 -070078 /* struct clk_fixed_rate assignments */
79 fixed->fixed_rate = fixed_rate;
Boris BREZILLON0903ea62013-12-21 10:34:48 +010080 fixed->fixed_accuracy = fixed_accuracy;
Saravana Kannan0197b3e2012-04-25 22:58:56 -070081 fixed->hw.init = &init;
Mike Turquette9d9f78e2012-03-15 23:11:20 -070082
Mike Turquette27d54592012-03-26 17:51:03 -070083 /* register the clock */
Stephen Boyd26ef56b2016-02-07 00:34:13 -080084 hw = &fixed->hw;
85 ret = clk_hw_register(dev, hw);
86 if (ret) {
Mike Turquette27d54592012-03-26 17:51:03 -070087 kfree(fixed);
Stephen Boyd26ef56b2016-02-07 00:34:13 -080088 hw = ERR_PTR(ret);
89 }
Mike Turquette27d54592012-03-26 17:51:03 -070090
Stephen Boyd26ef56b2016-02-07 00:34:13 -080091 return hw;
92}
93EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate_with_accuracy);
94
95struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
96 const char *name, const char *parent_name, unsigned long flags,
97 unsigned long fixed_rate, unsigned long fixed_accuracy)
98{
99 struct clk_hw *hw;
100
101 hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
102 flags, fixed_rate, fixed_accuracy);
103 if (IS_ERR(hw))
104 return ERR_CAST(hw);
105 return hw->clk;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700106}
Boris BREZILLON0903ea62013-12-21 10:34:48 +0100107EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy);
108
109/**
Stephen Boyd26ef56b2016-02-07 00:34:13 -0800110 * clk_hw_register_fixed_rate - register fixed-rate clock with the clock
111 * framework
Boris BREZILLON0903ea62013-12-21 10:34:48 +0100112 * @dev: device that is registering this clock
113 * @name: name of this clock
114 * @parent_name: name of clock's parent
115 * @flags: framework-specific flags
116 * @fixed_rate: non-adjustable clock rate
117 */
Stephen Boyd26ef56b2016-02-07 00:34:13 -0800118struct clk_hw *clk_hw_register_fixed_rate(struct device *dev, const char *name,
119 const char *parent_name, unsigned long flags,
120 unsigned long fixed_rate)
121{
122 return clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name,
123 flags, fixed_rate, 0);
124}
125EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate);
126
Boris BREZILLON0903ea62013-12-21 10:34:48 +0100127struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
128 const char *parent_name, unsigned long flags,
129 unsigned long fixed_rate)
130{
131 return clk_register_fixed_rate_with_accuracy(dev, name, parent_name,
132 flags, fixed_rate, 0);
133}
Stephen Boyd389ae052013-07-24 17:43:29 -0700134EXPORT_SYMBOL_GPL(clk_register_fixed_rate);
Grant Likely015ba402012-04-07 21:39:39 -0500135
Masahiro Yamada0b225e42016-01-06 13:25:10 +0900136void clk_unregister_fixed_rate(struct clk *clk)
137{
138 struct clk_hw *hw;
139
140 hw = __clk_get_hw(clk);
141 if (!hw)
142 return;
143
144 clk_unregister(clk);
145 kfree(to_clk_fixed_rate(hw));
146}
147EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate);
148
Masahiro Yamada52445632016-05-22 14:33:35 +0900149void clk_hw_unregister_fixed_rate(struct clk_hw *hw)
150{
151 struct clk_fixed_rate *fixed;
152
153 fixed = to_clk_fixed_rate(hw);
154
155 clk_hw_unregister(hw);
156 kfree(fixed);
157}
158EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate);
159
Grant Likely015ba402012-04-07 21:39:39 -0500160#ifdef CONFIG_OF
Ricardo Ribalda Delgado435779f2016-07-05 18:23:34 +0200161static struct clk *_of_fixed_clk_setup(struct device_node *node)
Grant Likely015ba402012-04-07 21:39:39 -0500162{
163 struct clk *clk;
164 const char *clk_name = node->name;
165 u32 rate;
Boris BREZILLON0903ea62013-12-21 10:34:48 +0100166 u32 accuracy = 0;
Ricardo Ribalda Delgado435779f2016-07-05 18:23:34 +0200167 int ret;
Grant Likely015ba402012-04-07 21:39:39 -0500168
169 if (of_property_read_u32(node, "clock-frequency", &rate))
Ricardo Ribalda Delgado435779f2016-07-05 18:23:34 +0200170 return ERR_PTR(-EIO);
Grant Likely015ba402012-04-07 21:39:39 -0500171
Boris BREZILLON0903ea62013-12-21 10:34:48 +0100172 of_property_read_u32(node, "clock-accuracy", &accuracy);
173
Grant Likely015ba402012-04-07 21:39:39 -0500174 of_property_read_string(node, "clock-output-names", &clk_name);
175
Boris BREZILLON0903ea62013-12-21 10:34:48 +0100176 clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
Stephen Boydd3781a72016-03-01 11:00:12 -0800177 0, rate, accuracy);
Ricardo Ribalda Delgado435779f2016-07-05 18:23:34 +0200178 if (IS_ERR(clk))
179 return clk;
180
181 ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
182 if (ret) {
183 clk_unregister(clk);
184 return ERR_PTR(ret);
185 }
186
187 return clk;
188}
189
190/**
191 * of_fixed_clk_setup() - Setup function for simple fixed rate clock
192 */
Stephen Boydd336e9a2016-08-12 18:50:23 -0700193void __init of_fixed_clk_setup(struct device_node *node)
Ricardo Ribalda Delgado435779f2016-07-05 18:23:34 +0200194{
195 _of_fixed_clk_setup(node);
Grant Likely015ba402012-04-07 21:39:39 -0500196}
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +0530197CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);
Ricardo Ribalda Delgado435779f2016-07-05 18:23:34 +0200198
199static int of_fixed_clk_remove(struct platform_device *pdev)
200{
201 struct clk *clk = platform_get_drvdata(pdev);
202
Alan Tullcde06282018-10-18 14:54:11 -0500203 of_clk_del_provider(pdev->dev.of_node);
Ricardo Ribalda Delgado435779f2016-07-05 18:23:34 +0200204 clk_unregister_fixed_rate(clk);
205
206 return 0;
207}
208
209static int of_fixed_clk_probe(struct platform_device *pdev)
210{
211 struct clk *clk;
212
213 /*
214 * This function is not executed when of_fixed_clk_setup
215 * succeeded.
216 */
217 clk = _of_fixed_clk_setup(pdev->dev.of_node);
218 if (IS_ERR(clk))
219 return PTR_ERR(clk);
220
221 platform_set_drvdata(pdev, clk);
222
223 return 0;
224}
225
226static const struct of_device_id of_fixed_clk_ids[] = {
227 { .compatible = "fixed-clock" },
228 { }
229};
230MODULE_DEVICE_TABLE(of, of_fixed_clk_ids);
231
232static struct platform_driver of_fixed_clk_driver = {
233 .driver = {
234 .name = "of_fixed_clk",
235 .of_match_table = of_fixed_clk_ids,
236 },
237 .probe = of_fixed_clk_probe,
238 .remove = of_fixed_clk_remove,
239};
240builtin_platform_driver(of_fixed_clk_driver);
Grant Likely015ba402012-04-07 21:39:39 -0500241#endif