blob: f85ec8d1711fb7f36ac2e724308745dbf7b1ef47 [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>
Mike Turquette9d9f78e2012-03-15 23:11:20 -070018
19/*
20 * DOC: basic fixed-rate clock that cannot gate
21 *
22 * Traits of this clock:
23 * prepare - clk_(un)prepare only ensures parents are prepared
24 * enable - clk_enable only ensures parents are enabled
25 * rate - rate is always a fixed value. No clk_set_rate support
26 * parent - fixed parent. No clk_set_parent support
27 */
28
29#define to_clk_fixed_rate(_hw) container_of(_hw, struct clk_fixed_rate, hw)
30
31static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw,
32 unsigned long parent_rate)
33{
34 return to_clk_fixed_rate(hw)->fixed_rate;
35}
Mike Turquette9d9f78e2012-03-15 23:11:20 -070036
Boris BREZILLON0903ea62013-12-21 10:34:48 +010037static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw,
38 unsigned long parent_accuracy)
39{
40 return to_clk_fixed_rate(hw)->fixed_accuracy;
41}
42
Shawn Guo822c2502012-03-27 15:23:22 +080043const struct clk_ops clk_fixed_rate_ops = {
Mike Turquette9d9f78e2012-03-15 23:11:20 -070044 .recalc_rate = clk_fixed_rate_recalc_rate,
Boris BREZILLON0903ea62013-12-21 10:34:48 +010045 .recalc_accuracy = clk_fixed_rate_recalc_accuracy,
Mike Turquette9d9f78e2012-03-15 23:11:20 -070046};
47EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);
48
Mike Turquette27d54592012-03-26 17:51:03 -070049/**
Boris BREZILLON0903ea62013-12-21 10:34:48 +010050 * clk_register_fixed_rate_with_accuracy - register fixed-rate clock with the
51 * clock framework
Mike Turquette27d54592012-03-26 17:51:03 -070052 * @dev: device that is registering this clock
53 * @name: name of this clock
54 * @parent_name: name of clock's parent
55 * @flags: framework-specific flags
56 * @fixed_rate: non-adjustable clock rate
Boris BREZILLON0903ea62013-12-21 10:34:48 +010057 * @fixed_accuracy: non-adjustable clock rate
Mike Turquette27d54592012-03-26 17:51:03 -070058 */
Boris BREZILLON0903ea62013-12-21 10:34:48 +010059struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
60 const char *name, const char *parent_name, unsigned long flags,
61 unsigned long fixed_rate, unsigned long fixed_accuracy)
Mike Turquette9d9f78e2012-03-15 23:11:20 -070062{
63 struct clk_fixed_rate *fixed;
Mike Turquette27d54592012-03-26 17:51:03 -070064 struct clk *clk;
Saravana Kannan0197b3e2012-04-25 22:58:56 -070065 struct clk_init_data init;
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 */
Saravana Kannan0197b3e2012-04-25 22:58:56 -070084 clk = clk_register(dev, &fixed->hw);
Mike Turquette27d54592012-03-26 17:51:03 -070085 if (IS_ERR(clk))
86 kfree(fixed);
87
88 return clk;
Mike Turquette9d9f78e2012-03-15 23:11:20 -070089}
Boris BREZILLON0903ea62013-12-21 10:34:48 +010090EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy);
91
92/**
93 * clk_register_fixed_rate - register fixed-rate clock with the clock framework
94 * @dev: device that is registering this clock
95 * @name: name of this clock
96 * @parent_name: name of clock's parent
97 * @flags: framework-specific flags
98 * @fixed_rate: non-adjustable clock rate
99 */
100struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
101 const char *parent_name, unsigned long flags,
102 unsigned long fixed_rate)
103{
104 return clk_register_fixed_rate_with_accuracy(dev, name, parent_name,
105 flags, fixed_rate, 0);
106}
Stephen Boyd389ae052013-07-24 17:43:29 -0700107EXPORT_SYMBOL_GPL(clk_register_fixed_rate);
Grant Likely015ba402012-04-07 21:39:39 -0500108
109#ifdef CONFIG_OF
110/**
111 * of_fixed_clk_setup() - Setup function for simple fixed rate clock
112 */
Denis Efremove4eda8e2013-01-06 18:21:43 +0400113void of_fixed_clk_setup(struct device_node *node)
Grant Likely015ba402012-04-07 21:39:39 -0500114{
115 struct clk *clk;
116 const char *clk_name = node->name;
117 u32 rate;
Boris BREZILLON0903ea62013-12-21 10:34:48 +0100118 u32 accuracy = 0;
Grant Likely015ba402012-04-07 21:39:39 -0500119
120 if (of_property_read_u32(node, "clock-frequency", &rate))
121 return;
122
Boris BREZILLON0903ea62013-12-21 10:34:48 +0100123 of_property_read_u32(node, "clock-accuracy", &accuracy);
124
Grant Likely015ba402012-04-07 21:39:39 -0500125 of_property_read_string(node, "clock-output-names", &clk_name);
126
Boris BREZILLON0903ea62013-12-21 10:34:48 +0100127 clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
128 CLK_IS_ROOT, rate,
129 accuracy);
Wei Yongjuncdfed3b2012-09-21 14:35:18 +0800130 if (!IS_ERR(clk))
Grant Likely015ba402012-04-07 21:39:39 -0500131 of_clk_add_provider(node, of_clk_src_simple_get, clk);
132}
133EXPORT_SYMBOL_GPL(of_fixed_clk_setup);
Prashant Gaikwadf2f6c252013-01-04 12:30:52 +0530134CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);
Grant Likely015ba402012-04-07 21:39:39 -0500135#endif