Stephen Boyd | e1bd55e | 2018-12-11 09:57:48 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> |
| 4 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> |
| 5 | * |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 6 | * Fixed rate clock implementation |
| 7 | */ |
| 8 | |
| 9 | #include <linux/clk-provider.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/err.h> |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 14 | #include <linux/of.h> |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * DOC: basic fixed-rate clock that cannot gate |
| 19 | * |
| 20 | * Traits of this clock: |
| 21 | * prepare - clk_(un)prepare only ensures parents are prepared |
| 22 | * enable - clk_enable only ensures parents are enabled |
| 23 | * rate - rate is always a fixed value. No clk_set_rate support |
| 24 | * parent - fixed parent. No clk_set_parent support |
| 25 | */ |
| 26 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 27 | static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw, |
| 28 | unsigned long parent_rate) |
| 29 | { |
| 30 | return to_clk_fixed_rate(hw)->fixed_rate; |
| 31 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 32 | |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 33 | static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw, |
| 34 | unsigned long parent_accuracy) |
| 35 | { |
| 36 | return to_clk_fixed_rate(hw)->fixed_accuracy; |
| 37 | } |
| 38 | |
Shawn Guo | 822c250 | 2012-03-27 15:23:22 +0800 | [diff] [blame] | 39 | const struct clk_ops clk_fixed_rate_ops = { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 40 | .recalc_rate = clk_fixed_rate_recalc_rate, |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 41 | .recalc_accuracy = clk_fixed_rate_recalc_accuracy, |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 42 | }; |
| 43 | EXPORT_SYMBOL_GPL(clk_fixed_rate_ops); |
| 44 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 45 | /** |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 46 | * clk_hw_register_fixed_rate_with_accuracy - register fixed-rate clock with |
| 47 | * the clock framework |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 48 | * @dev: device that is registering this clock |
| 49 | * @name: name of this clock |
| 50 | * @parent_name: name of clock's parent |
| 51 | * @flags: framework-specific flags |
| 52 | * @fixed_rate: non-adjustable clock rate |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 53 | * @fixed_accuracy: non-adjustable clock rate |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 54 | */ |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 55 | struct clk_hw *clk_hw_register_fixed_rate_with_accuracy(struct device *dev, |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 56 | const char *name, const char *parent_name, unsigned long flags, |
| 57 | unsigned long fixed_rate, unsigned long fixed_accuracy) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 58 | { |
| 59 | struct clk_fixed_rate *fixed; |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 60 | struct clk_hw *hw; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 61 | struct clk_init_data init; |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 62 | int ret; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 63 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 64 | /* allocate fixed-rate clock */ |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 65 | fixed = kzalloc(sizeof(*fixed), GFP_KERNEL); |
| 66 | if (!fixed) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 67 | return ERR_PTR(-ENOMEM); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 68 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 69 | init.name = name; |
| 70 | init.ops = &clk_fixed_rate_ops; |
Stephen Boyd | 90b6c5c | 2019-04-25 10:57:37 -0700 | [diff] [blame] | 71 | init.flags = flags; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 72 | init.parent_names = (parent_name ? &parent_name: NULL); |
| 73 | init.num_parents = (parent_name ? 1 : 0); |
| 74 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 75 | /* struct clk_fixed_rate assignments */ |
| 76 | fixed->fixed_rate = fixed_rate; |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 77 | fixed->fixed_accuracy = fixed_accuracy; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 78 | fixed->hw.init = &init; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 79 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 80 | /* register the clock */ |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 81 | hw = &fixed->hw; |
| 82 | ret = clk_hw_register(dev, hw); |
| 83 | if (ret) { |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 84 | kfree(fixed); |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 85 | hw = ERR_PTR(ret); |
| 86 | } |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 87 | |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 88 | return hw; |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate_with_accuracy); |
| 91 | |
| 92 | struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev, |
| 93 | const char *name, const char *parent_name, unsigned long flags, |
| 94 | unsigned long fixed_rate, unsigned long fixed_accuracy) |
| 95 | { |
| 96 | struct clk_hw *hw; |
| 97 | |
| 98 | hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name, |
| 99 | flags, fixed_rate, fixed_accuracy); |
| 100 | if (IS_ERR(hw)) |
| 101 | return ERR_CAST(hw); |
| 102 | return hw->clk; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 103 | } |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 104 | EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy); |
| 105 | |
| 106 | /** |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 107 | * clk_hw_register_fixed_rate - register fixed-rate clock with the clock |
| 108 | * framework |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 109 | * @dev: device that is registering this clock |
| 110 | * @name: name of this clock |
| 111 | * @parent_name: name of clock's parent |
| 112 | * @flags: framework-specific flags |
| 113 | * @fixed_rate: non-adjustable clock rate |
| 114 | */ |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 115 | struct clk_hw *clk_hw_register_fixed_rate(struct device *dev, const char *name, |
| 116 | const char *parent_name, unsigned long flags, |
| 117 | unsigned long fixed_rate) |
| 118 | { |
| 119 | return clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name, |
| 120 | flags, fixed_rate, 0); |
| 121 | } |
| 122 | EXPORT_SYMBOL_GPL(clk_hw_register_fixed_rate); |
| 123 | |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 124 | struct clk *clk_register_fixed_rate(struct device *dev, const char *name, |
| 125 | const char *parent_name, unsigned long flags, |
| 126 | unsigned long fixed_rate) |
| 127 | { |
| 128 | return clk_register_fixed_rate_with_accuracy(dev, name, parent_name, |
| 129 | flags, fixed_rate, 0); |
| 130 | } |
Stephen Boyd | 389ae05 | 2013-07-24 17:43:29 -0700 | [diff] [blame] | 131 | EXPORT_SYMBOL_GPL(clk_register_fixed_rate); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 132 | |
Masahiro Yamada | 0b225e4 | 2016-01-06 13:25:10 +0900 | [diff] [blame] | 133 | void clk_unregister_fixed_rate(struct clk *clk) |
| 134 | { |
| 135 | struct clk_hw *hw; |
| 136 | |
| 137 | hw = __clk_get_hw(clk); |
| 138 | if (!hw) |
| 139 | return; |
| 140 | |
| 141 | clk_unregister(clk); |
| 142 | kfree(to_clk_fixed_rate(hw)); |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate); |
| 145 | |
Masahiro Yamada | 5244563 | 2016-05-22 14:33:35 +0900 | [diff] [blame] | 146 | void clk_hw_unregister_fixed_rate(struct clk_hw *hw) |
| 147 | { |
| 148 | struct clk_fixed_rate *fixed; |
| 149 | |
| 150 | fixed = to_clk_fixed_rate(hw); |
| 151 | |
| 152 | clk_hw_unregister(hw); |
| 153 | kfree(fixed); |
| 154 | } |
| 155 | EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate); |
| 156 | |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 157 | #ifdef CONFIG_OF |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 158 | static struct clk *_of_fixed_clk_setup(struct device_node *node) |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 159 | { |
| 160 | struct clk *clk; |
| 161 | const char *clk_name = node->name; |
| 162 | u32 rate; |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 163 | u32 accuracy = 0; |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 164 | int ret; |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 165 | |
| 166 | if (of_property_read_u32(node, "clock-frequency", &rate)) |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 167 | return ERR_PTR(-EIO); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 168 | |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 169 | of_property_read_u32(node, "clock-accuracy", &accuracy); |
| 170 | |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 171 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 172 | |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 173 | clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL, |
Stephen Boyd | d3781a7 | 2016-03-01 11:00:12 -0800 | [diff] [blame] | 174 | 0, rate, accuracy); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 175 | if (IS_ERR(clk)) |
| 176 | return clk; |
| 177 | |
| 178 | ret = of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 179 | if (ret) { |
| 180 | clk_unregister(clk); |
| 181 | return ERR_PTR(ret); |
| 182 | } |
| 183 | |
| 184 | return clk; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * of_fixed_clk_setup() - Setup function for simple fixed rate clock |
| 189 | */ |
Stephen Boyd | d336e9a | 2016-08-12 18:50:23 -0700 | [diff] [blame] | 190 | void __init of_fixed_clk_setup(struct device_node *node) |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 191 | { |
| 192 | _of_fixed_clk_setup(node); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 193 | } |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 194 | CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 195 | |
| 196 | static int of_fixed_clk_remove(struct platform_device *pdev) |
| 197 | { |
| 198 | struct clk *clk = platform_get_drvdata(pdev); |
| 199 | |
Alan Tull | 52091c2 | 2018-10-18 14:54:11 -0500 | [diff] [blame] | 200 | of_clk_del_provider(pdev->dev.of_node); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 201 | clk_unregister_fixed_rate(clk); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int of_fixed_clk_probe(struct platform_device *pdev) |
| 207 | { |
| 208 | struct clk *clk; |
| 209 | |
| 210 | /* |
| 211 | * This function is not executed when of_fixed_clk_setup |
| 212 | * succeeded. |
| 213 | */ |
| 214 | clk = _of_fixed_clk_setup(pdev->dev.of_node); |
| 215 | if (IS_ERR(clk)) |
| 216 | return PTR_ERR(clk); |
| 217 | |
| 218 | platform_set_drvdata(pdev, clk); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static const struct of_device_id of_fixed_clk_ids[] = { |
| 224 | { .compatible = "fixed-clock" }, |
| 225 | { } |
| 226 | }; |
| 227 | MODULE_DEVICE_TABLE(of, of_fixed_clk_ids); |
| 228 | |
| 229 | static struct platform_driver of_fixed_clk_driver = { |
| 230 | .driver = { |
| 231 | .name = "of_fixed_clk", |
| 232 | .of_match_table = of_fixed_clk_ids, |
| 233 | }, |
| 234 | .probe = of_fixed_clk_probe, |
| 235 | .remove = of_fixed_clk_remove, |
| 236 | }; |
| 237 | builtin_platform_driver(of_fixed_clk_driver); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 238 | #endif |