Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 1 | /* |
| 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 Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 17 | #include <linux/of.h> |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 18 | |
| 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 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 29 | static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw, |
| 30 | unsigned long parent_rate) |
| 31 | { |
| 32 | return to_clk_fixed_rate(hw)->fixed_rate; |
| 33 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 34 | |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 35 | static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw, |
| 36 | unsigned long parent_accuracy) |
| 37 | { |
| 38 | return to_clk_fixed_rate(hw)->fixed_accuracy; |
| 39 | } |
| 40 | |
Shawn Guo | 822c250 | 2012-03-27 15:23:22 +0800 | [diff] [blame] | 41 | const struct clk_ops clk_fixed_rate_ops = { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 42 | .recalc_rate = clk_fixed_rate_recalc_rate, |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 43 | .recalc_accuracy = clk_fixed_rate_recalc_accuracy, |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 44 | }; |
| 45 | EXPORT_SYMBOL_GPL(clk_fixed_rate_ops); |
| 46 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 47 | /** |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 48 | * clk_register_fixed_rate_with_accuracy - register fixed-rate clock with the |
| 49 | * clock framework |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 50 | * @dev: device that is registering this clock |
| 51 | * @name: name of this clock |
| 52 | * @parent_name: name of clock's parent |
| 53 | * @flags: framework-specific flags |
| 54 | * @fixed_rate: non-adjustable clock rate |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 55 | * @fixed_accuracy: non-adjustable clock rate |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 56 | */ |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 57 | struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev, |
| 58 | const char *name, const char *parent_name, unsigned long flags, |
| 59 | unsigned long fixed_rate, unsigned long fixed_accuracy) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 60 | { |
| 61 | struct clk_fixed_rate *fixed; |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 62 | struct clk *clk; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 63 | struct clk_init_data init; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 64 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 65 | /* allocate fixed-rate clock */ |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 66 | fixed = kzalloc(sizeof(*fixed), GFP_KERNEL); |
| 67 | if (!fixed) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 68 | return ERR_PTR(-ENOMEM); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 69 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 70 | init.name = name; |
| 71 | init.ops = &clk_fixed_rate_ops; |
Rajendra Nayak | f7d8caa | 2012-06-01 14:02:47 +0530 | [diff] [blame] | 72 | init.flags = flags | CLK_IS_BASIC; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 73 | init.parent_names = (parent_name ? &parent_name: NULL); |
| 74 | init.num_parents = (parent_name ? 1 : 0); |
| 75 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 76 | /* struct clk_fixed_rate assignments */ |
| 77 | fixed->fixed_rate = fixed_rate; |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 78 | fixed->fixed_accuracy = fixed_accuracy; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 79 | fixed->hw.init = &init; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 80 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 81 | /* register the clock */ |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 82 | clk = clk_register(dev, &fixed->hw); |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 83 | if (IS_ERR(clk)) |
| 84 | kfree(fixed); |
| 85 | |
| 86 | return clk; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 87 | } |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 88 | EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy); |
| 89 | |
| 90 | /** |
| 91 | * clk_register_fixed_rate - register fixed-rate clock with the clock framework |
| 92 | * @dev: device that is registering this clock |
| 93 | * @name: name of this clock |
| 94 | * @parent_name: name of clock's parent |
| 95 | * @flags: framework-specific flags |
| 96 | * @fixed_rate: non-adjustable clock rate |
| 97 | */ |
| 98 | struct clk *clk_register_fixed_rate(struct device *dev, const char *name, |
| 99 | const char *parent_name, unsigned long flags, |
| 100 | unsigned long fixed_rate) |
| 101 | { |
| 102 | return clk_register_fixed_rate_with_accuracy(dev, name, parent_name, |
| 103 | flags, fixed_rate, 0); |
| 104 | } |
Stephen Boyd | 389ae05 | 2013-07-24 17:43:29 -0700 | [diff] [blame] | 105 | EXPORT_SYMBOL_GPL(clk_register_fixed_rate); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 106 | |
Masahiro Yamada | 0b225e4 | 2016-01-06 13:25:10 +0900 | [diff] [blame] | 107 | void clk_unregister_fixed_rate(struct clk *clk) |
| 108 | { |
| 109 | struct clk_hw *hw; |
| 110 | |
| 111 | hw = __clk_get_hw(clk); |
| 112 | if (!hw) |
| 113 | return; |
| 114 | |
| 115 | clk_unregister(clk); |
| 116 | kfree(to_clk_fixed_rate(hw)); |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate); |
| 119 | |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 120 | #ifdef CONFIG_OF |
| 121 | /** |
| 122 | * of_fixed_clk_setup() - Setup function for simple fixed rate clock |
| 123 | */ |
Denis Efremov | e4eda8e | 2013-01-06 18:21:43 +0400 | [diff] [blame] | 124 | void of_fixed_clk_setup(struct device_node *node) |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 125 | { |
| 126 | struct clk *clk; |
| 127 | const char *clk_name = node->name; |
| 128 | u32 rate; |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 129 | u32 accuracy = 0; |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 130 | |
| 131 | if (of_property_read_u32(node, "clock-frequency", &rate)) |
| 132 | return; |
| 133 | |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 134 | of_property_read_u32(node, "clock-accuracy", &accuracy); |
| 135 | |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 136 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 137 | |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 138 | clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL, |
Stephen Boyd | d3781a7 | 2016-03-01 11:00:12 -0800 | [diff] [blame] | 139 | 0, rate, accuracy); |
Wei Yongjun | cdfed3b | 2012-09-21 14:35:18 +0800 | [diff] [blame] | 140 | if (!IS_ERR(clk)) |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 141 | of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 142 | } |
| 143 | EXPORT_SYMBOL_GPL(of_fixed_clk_setup); |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 144 | CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 145 | #endif |