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 | |
Stephen Boyd | 38d1e38 | 2019-08-30 08:09:15 -0700 | [diff] [blame] | 27 | #define to_clk_fixed_rate(_hw) container_of(_hw, struct clk_fixed_rate, hw) |
| 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 | |
Stephen Boyd | 2d34f09 | 2019-08-30 08:09:17 -0700 | [diff] [blame^] | 47 | struct clk_hw *__clk_hw_register_fixed_rate(struct device *dev, |
| 48 | struct device_node *np, const char *name, |
| 49 | const char *parent_name, const struct clk_hw *parent_hw, |
| 50 | const struct clk_parent_data *parent_data, unsigned long flags, |
| 51 | unsigned long fixed_rate, unsigned long fixed_accuracy, |
| 52 | unsigned long clk_fixed_flags) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 53 | { |
| 54 | struct clk_fixed_rate *fixed; |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 55 | struct clk_hw *hw; |
Manivannan Sadhasivam | cc819cf | 2019-11-15 21:58:55 +0530 | [diff] [blame] | 56 | struct clk_init_data init = {}; |
Stephen Boyd | 2d34f09 | 2019-08-30 08:09:17 -0700 | [diff] [blame^] | 57 | int ret = -EINVAL; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 58 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 59 | /* allocate fixed-rate clock */ |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 60 | fixed = kzalloc(sizeof(*fixed), GFP_KERNEL); |
| 61 | if (!fixed) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 62 | return ERR_PTR(-ENOMEM); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 63 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 64 | init.name = name; |
| 65 | init.ops = &clk_fixed_rate_ops; |
Stephen Boyd | 90b6c5c | 2019-04-25 10:57:37 -0700 | [diff] [blame] | 66 | init.flags = flags; |
Stephen Boyd | 2d34f09 | 2019-08-30 08:09:17 -0700 | [diff] [blame^] | 67 | init.parent_names = parent_name ? &parent_name : NULL; |
| 68 | init.parent_hws = parent_hw ? &parent_hw : NULL; |
| 69 | init.parent_data = parent_data; |
| 70 | if (parent_name || parent_hw || parent_data) |
| 71 | init.num_parents = 1; |
| 72 | else |
| 73 | init.num_parents = 0; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 74 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 75 | /* struct clk_fixed_rate assignments */ |
Stephen Boyd | 2d34f09 | 2019-08-30 08:09:17 -0700 | [diff] [blame^] | 76 | fixed->flags = clk_fixed_flags; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 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 */ |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 82 | hw = &fixed->hw; |
Stephen Boyd | 2d34f09 | 2019-08-30 08:09:17 -0700 | [diff] [blame^] | 83 | if (dev || !np) |
| 84 | ret = clk_hw_register(dev, hw); |
| 85 | else if (np) |
| 86 | ret = of_clk_hw_register(np, hw); |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 87 | if (ret) { |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 88 | kfree(fixed); |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 89 | hw = ERR_PTR(ret); |
| 90 | } |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 91 | |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 92 | return hw; |
| 93 | } |
Stephen Boyd | 2d34f09 | 2019-08-30 08:09:17 -0700 | [diff] [blame^] | 94 | EXPORT_SYMBOL_GPL(__clk_hw_register_fixed_rate); |
Stephen Boyd | 26ef56b | 2016-02-07 00:34:13 -0800 | [diff] [blame] | 95 | |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 96 | struct clk *clk_register_fixed_rate(struct device *dev, const char *name, |
| 97 | const char *parent_name, unsigned long flags, |
| 98 | unsigned long fixed_rate) |
| 99 | { |
Stephen Boyd | 576859d | 2019-08-30 08:09:14 -0700 | [diff] [blame] | 100 | struct clk_hw *hw; |
| 101 | |
| 102 | hw = clk_hw_register_fixed_rate_with_accuracy(dev, name, parent_name, |
| 103 | flags, fixed_rate, 0); |
| 104 | if (IS_ERR(hw)) |
| 105 | return ERR_CAST(hw); |
| 106 | return hw->clk; |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 107 | } |
Stephen Boyd | 389ae05 | 2013-07-24 17:43:29 -0700 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(clk_register_fixed_rate); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 109 | |
Masahiro Yamada | 0b225e4 | 2016-01-06 13:25:10 +0900 | [diff] [blame] | 110 | void clk_unregister_fixed_rate(struct clk *clk) |
| 111 | { |
| 112 | struct clk_hw *hw; |
| 113 | |
| 114 | hw = __clk_get_hw(clk); |
| 115 | if (!hw) |
| 116 | return; |
| 117 | |
| 118 | clk_unregister(clk); |
| 119 | kfree(to_clk_fixed_rate(hw)); |
| 120 | } |
| 121 | EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate); |
| 122 | |
Masahiro Yamada | 5244563 | 2016-05-22 14:33:35 +0900 | [diff] [blame] | 123 | void clk_hw_unregister_fixed_rate(struct clk_hw *hw) |
| 124 | { |
| 125 | struct clk_fixed_rate *fixed; |
| 126 | |
| 127 | fixed = to_clk_fixed_rate(hw); |
| 128 | |
| 129 | clk_hw_unregister(hw); |
| 130 | kfree(fixed); |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_rate); |
| 133 | |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 134 | #ifdef CONFIG_OF |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 135 | static struct clk_hw *_of_fixed_clk_setup(struct device_node *node) |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 136 | { |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 137 | struct clk_hw *hw; |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 138 | const char *clk_name = node->name; |
| 139 | u32 rate; |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 140 | u32 accuracy = 0; |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 141 | int ret; |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 142 | |
| 143 | if (of_property_read_u32(node, "clock-frequency", &rate)) |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 144 | return ERR_PTR(-EIO); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 145 | |
Boris BREZILLON | 0903ea6 | 2013-12-21 10:34:48 +0100 | [diff] [blame] | 146 | of_property_read_u32(node, "clock-accuracy", &accuracy); |
| 147 | |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 148 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 149 | |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 150 | hw = clk_hw_register_fixed_rate_with_accuracy(NULL, clk_name, NULL, |
Stephen Boyd | d3781a7 | 2016-03-01 11:00:12 -0800 | [diff] [blame] | 151 | 0, rate, accuracy); |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 152 | if (IS_ERR(hw)) |
| 153 | return hw; |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 154 | |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 155 | ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 156 | if (ret) { |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 157 | clk_hw_unregister_fixed_rate(hw); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 158 | return ERR_PTR(ret); |
| 159 | } |
| 160 | |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 161 | return hw; |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * of_fixed_clk_setup() - Setup function for simple fixed rate clock |
| 166 | */ |
Stephen Boyd | d336e9a | 2016-08-12 18:50:23 -0700 | [diff] [blame] | 167 | void __init of_fixed_clk_setup(struct device_node *node) |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 168 | { |
| 169 | _of_fixed_clk_setup(node); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 170 | } |
Prashant Gaikwad | f2f6c25 | 2013-01-04 12:30:52 +0530 | [diff] [blame] | 171 | CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 172 | |
| 173 | static int of_fixed_clk_remove(struct platform_device *pdev) |
| 174 | { |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 175 | struct clk_hw *hw = platform_get_drvdata(pdev); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 176 | |
Alan Tull | 52091c2 | 2018-10-18 14:54:11 -0500 | [diff] [blame] | 177 | of_clk_del_provider(pdev->dev.of_node); |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 178 | clk_hw_unregister_fixed_rate(hw); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int of_fixed_clk_probe(struct platform_device *pdev) |
| 184 | { |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 185 | struct clk_hw *hw; |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * This function is not executed when of_fixed_clk_setup |
| 189 | * succeeded. |
| 190 | */ |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 191 | hw = _of_fixed_clk_setup(pdev->dev.of_node); |
| 192 | if (IS_ERR(hw)) |
| 193 | return PTR_ERR(hw); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 194 | |
Stephen Boyd | 34e0183 | 2019-08-30 08:09:13 -0700 | [diff] [blame] | 195 | platform_set_drvdata(pdev, hw); |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static const struct of_device_id of_fixed_clk_ids[] = { |
| 201 | { .compatible = "fixed-clock" }, |
| 202 | { } |
| 203 | }; |
Ricardo Ribalda Delgado | 435779f | 2016-07-05 18:23:34 +0200 | [diff] [blame] | 204 | |
| 205 | static struct platform_driver of_fixed_clk_driver = { |
| 206 | .driver = { |
| 207 | .name = "of_fixed_clk", |
| 208 | .of_match_table = of_fixed_clk_ids, |
| 209 | }, |
| 210 | .probe = of_fixed_clk_probe, |
| 211 | .remove = of_fixed_clk_remove, |
| 212 | }; |
| 213 | builtin_platform_driver(of_fixed_clk_driver); |
Grant Likely | 015ba40 | 2012-04-07 21:39:39 -0500 | [diff] [blame] | 214 | #endif |