Stephen Boyd | e1bd55e | 2018-12-11 09:57:48 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 4 | */ |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/clk-provider.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/err.h> |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 9 | #include <linux/of.h> |
Ricardo Ribalda Delgado | 971451b | 2016-07-05 18:23:33 +0200 | [diff] [blame] | 10 | #include <linux/platform_device.h> |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * DOC: basic fixed multiplier and divider clock that cannot gate |
| 14 | * |
| 15 | * Traits of this clock: |
| 16 | * prepare - clk_prepare only ensures that parents are prepared |
| 17 | * enable - clk_enable only ensures that parents are enabled |
| 18 | * rate - rate is fixed. clk->rate = parent->rate / div * mult |
| 19 | * parent - fixed parent. No clk_set_parent support |
| 20 | */ |
| 21 | |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 22 | static unsigned long clk_factor_recalc_rate(struct clk_hw *hw, |
| 23 | unsigned long parent_rate) |
| 24 | { |
| 25 | struct clk_fixed_factor *fix = to_clk_fixed_factor(hw); |
Haojian Zhuang | bab5330 | 2012-12-03 16:14:37 +0800 | [diff] [blame] | 26 | unsigned long long int rate; |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 27 | |
Haojian Zhuang | bab5330 | 2012-12-03 16:14:37 +0800 | [diff] [blame] | 28 | rate = (unsigned long long int)parent_rate * fix->mult; |
| 29 | do_div(rate, fix->div); |
| 30 | return (unsigned long)rate; |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate, |
| 34 | unsigned long *prate) |
| 35 | { |
| 36 | struct clk_fixed_factor *fix = to_clk_fixed_factor(hw); |
| 37 | |
Stephen Boyd | 98d8a60 | 2015-06-29 16:56:30 -0700 | [diff] [blame] | 38 | if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) { |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 39 | unsigned long best_parent; |
| 40 | |
| 41 | best_parent = (rate / fix->mult) * fix->div; |
Stephen Boyd | 2f508a9 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 42 | *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent); |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | return (*prate / fix->div) * fix->mult; |
| 46 | } |
| 47 | |
| 48 | static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate, |
| 49 | unsigned long parent_rate) |
| 50 | { |
Daniel Thompson | 3037e9e | 2015-06-10 21:04:54 +0100 | [diff] [blame] | 51 | /* |
| 52 | * We must report success but we can do so unconditionally because |
| 53 | * clk_factor_round_rate returns values that ensure this call is a |
| 54 | * nop. |
| 55 | */ |
| 56 | |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Daniel Thompson | 3037e9e | 2015-06-10 21:04:54 +0100 | [diff] [blame] | 60 | const struct clk_ops clk_fixed_factor_ops = { |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 61 | .round_rate = clk_factor_round_rate, |
| 62 | .set_rate = clk_factor_set_rate, |
| 63 | .recalc_rate = clk_factor_recalc_rate, |
| 64 | }; |
| 65 | EXPORT_SYMBOL_GPL(clk_fixed_factor_ops); |
| 66 | |
Stephen Boyd | 0759ac8 | 2016-02-07 00:11:06 -0800 | [diff] [blame] | 67 | struct clk_hw *clk_hw_register_fixed_factor(struct device *dev, |
| 68 | const char *name, const char *parent_name, unsigned long flags, |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 69 | unsigned int mult, unsigned int div) |
| 70 | { |
| 71 | struct clk_fixed_factor *fix; |
| 72 | struct clk_init_data init; |
Stephen Boyd | 0759ac8 | 2016-02-07 00:11:06 -0800 | [diff] [blame] | 73 | struct clk_hw *hw; |
| 74 | int ret; |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 75 | |
| 76 | fix = kmalloc(sizeof(*fix), GFP_KERNEL); |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 77 | if (!fix) |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 78 | return ERR_PTR(-ENOMEM); |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 79 | |
| 80 | /* struct clk_fixed_factor assignments */ |
| 81 | fix->mult = mult; |
| 82 | fix->div = div; |
| 83 | fix->hw.init = &init; |
| 84 | |
| 85 | init.name = name; |
| 86 | init.ops = &clk_fixed_factor_ops; |
Rajendra Nayak | f7d8caa | 2012-06-01 14:02:47 +0530 | [diff] [blame] | 87 | init.flags = flags | CLK_IS_BASIC; |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 88 | init.parent_names = &parent_name; |
| 89 | init.num_parents = 1; |
| 90 | |
Stephen Boyd | 0759ac8 | 2016-02-07 00:11:06 -0800 | [diff] [blame] | 91 | hw = &fix->hw; |
| 92 | ret = clk_hw_register(dev, hw); |
| 93 | if (ret) { |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 94 | kfree(fix); |
Stephen Boyd | 0759ac8 | 2016-02-07 00:11:06 -0800 | [diff] [blame] | 95 | hw = ERR_PTR(ret); |
| 96 | } |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 97 | |
Stephen Boyd | 0759ac8 | 2016-02-07 00:11:06 -0800 | [diff] [blame] | 98 | return hw; |
| 99 | } |
| 100 | EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor); |
| 101 | |
| 102 | struct clk *clk_register_fixed_factor(struct device *dev, const char *name, |
| 103 | const char *parent_name, unsigned long flags, |
| 104 | unsigned int mult, unsigned int div) |
| 105 | { |
| 106 | struct clk_hw *hw; |
| 107 | |
| 108 | hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult, |
| 109 | div); |
| 110 | if (IS_ERR(hw)) |
| 111 | return ERR_CAST(hw); |
| 112 | return hw->clk; |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 113 | } |
Mike Turquette | 5cfe10b | 2013-08-15 19:06:29 -0700 | [diff] [blame] | 114 | EXPORT_SYMBOL_GPL(clk_register_fixed_factor); |
| 115 | |
Masahiro Yamada | cbf9591 | 2016-01-06 13:25:09 +0900 | [diff] [blame] | 116 | void clk_unregister_fixed_factor(struct clk *clk) |
| 117 | { |
| 118 | struct clk_hw *hw; |
| 119 | |
| 120 | hw = __clk_get_hw(clk); |
| 121 | if (!hw) |
| 122 | return; |
| 123 | |
| 124 | clk_unregister(clk); |
| 125 | kfree(to_clk_fixed_factor(hw)); |
| 126 | } |
| 127 | EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor); |
| 128 | |
Stephen Boyd | 0759ac8 | 2016-02-07 00:11:06 -0800 | [diff] [blame] | 129 | void clk_hw_unregister_fixed_factor(struct clk_hw *hw) |
| 130 | { |
| 131 | struct clk_fixed_factor *fix; |
| 132 | |
| 133 | fix = to_clk_fixed_factor(hw); |
| 134 | |
| 135 | clk_hw_unregister(hw); |
| 136 | kfree(fix); |
| 137 | } |
| 138 | EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor); |
| 139 | |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 140 | #ifdef CONFIG_OF |
Maxime Ripard | e6cbf99 | 2016-06-22 11:15:54 +0200 | [diff] [blame] | 141 | static const struct of_device_id set_rate_parent_matches[] = { |
| 142 | { .compatible = "allwinner,sun4i-a10-pll3-2x-clk" }, |
| 143 | { /* Sentinel */ }, |
| 144 | }; |
| 145 | |
Ricardo Ribalda Delgado | 971451b | 2016-07-05 18:23:33 +0200 | [diff] [blame] | 146 | static struct clk *_of_fixed_factor_clk_setup(struct device_node *node) |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 147 | { |
| 148 | struct clk *clk; |
| 149 | const char *clk_name = node->name; |
| 150 | const char *parent_name; |
Maxime Ripard | e6cbf99 | 2016-06-22 11:15:54 +0200 | [diff] [blame] | 151 | unsigned long flags = 0; |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 152 | u32 div, mult; |
Ricardo Ribalda Delgado | 971451b | 2016-07-05 18:23:33 +0200 | [diff] [blame] | 153 | int ret; |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 154 | |
| 155 | if (of_property_read_u32(node, "clock-div", &div)) { |
Rob Herring | e665f02 | 2018-08-28 10:44:29 -0500 | [diff] [blame] | 156 | pr_err("%s Fixed factor clock <%pOFn> must have a clock-div property\n", |
| 157 | __func__, node); |
Ricardo Ribalda Delgado | 971451b | 2016-07-05 18:23:33 +0200 | [diff] [blame] | 158 | return ERR_PTR(-EIO); |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if (of_property_read_u32(node, "clock-mult", &mult)) { |
Rob Herring | e665f02 | 2018-08-28 10:44:29 -0500 | [diff] [blame] | 162 | pr_err("%s Fixed factor clock <%pOFn> must have a clock-mult property\n", |
| 163 | __func__, node); |
Ricardo Ribalda Delgado | 971451b | 2016-07-05 18:23:33 +0200 | [diff] [blame] | 164 | return ERR_PTR(-EIO); |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 168 | parent_name = of_clk_get_parent_name(node, 0); |
| 169 | |
Maxime Ripard | e6cbf99 | 2016-06-22 11:15:54 +0200 | [diff] [blame] | 170 | if (of_match_node(set_rate_parent_matches, node)) |
| 171 | flags |= CLK_SET_RATE_PARENT; |
| 172 | |
| 173 | clk = clk_register_fixed_factor(NULL, clk_name, parent_name, flags, |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 174 | mult, div); |
Rajan Vaja | f6dab42 | 2018-07-17 06:17:00 -0700 | [diff] [blame] | 175 | if (IS_ERR(clk)) { |
| 176 | /* |
| 177 | * If parent clock is not registered, registration would fail. |
| 178 | * Clear OF_POPULATED flag so that clock registration can be |
| 179 | * attempted again from probe function. |
| 180 | */ |
| 181 | of_node_clear_flag(node, OF_POPULATED); |
Ricardo Ribalda Delgado | 971451b | 2016-07-05 18:23:33 +0200 | [diff] [blame] | 182 | return clk; |
Rajan Vaja | f6dab42 | 2018-07-17 06:17:00 -0700 | [diff] [blame] | 183 | } |
Ricardo Ribalda Delgado | 971451b | 2016-07-05 18:23:33 +0200 | [diff] [blame] | 184 | |
| 185 | ret = of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 186 | if (ret) { |
| 187 | clk_unregister(clk); |
| 188 | return ERR_PTR(ret); |
| 189 | } |
| 190 | |
| 191 | return clk; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock |
| 196 | */ |
| 197 | void __init of_fixed_factor_clk_setup(struct device_node *node) |
| 198 | { |
| 199 | _of_fixed_factor_clk_setup(node); |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 200 | } |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 201 | CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock", |
| 202 | of_fixed_factor_clk_setup); |
Ricardo Ribalda Delgado | 971451b | 2016-07-05 18:23:33 +0200 | [diff] [blame] | 203 | |
| 204 | static int of_fixed_factor_clk_remove(struct platform_device *pdev) |
| 205 | { |
| 206 | struct clk *clk = platform_get_drvdata(pdev); |
| 207 | |
Ricardo Ribalda Delgado | f98e8a5 | 2018-11-01 14:15:49 +0100 | [diff] [blame] | 208 | of_clk_del_provider(pdev->dev.of_node); |
Ricardo Ribalda Delgado | 971451b | 2016-07-05 18:23:33 +0200 | [diff] [blame] | 209 | clk_unregister_fixed_factor(clk); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int of_fixed_factor_clk_probe(struct platform_device *pdev) |
| 215 | { |
| 216 | struct clk *clk; |
| 217 | |
| 218 | /* |
| 219 | * This function is not executed when of_fixed_factor_clk_setup |
| 220 | * succeeded. |
| 221 | */ |
| 222 | clk = _of_fixed_factor_clk_setup(pdev->dev.of_node); |
| 223 | if (IS_ERR(clk)) |
| 224 | return PTR_ERR(clk); |
| 225 | |
| 226 | platform_set_drvdata(pdev, clk); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static const struct of_device_id of_fixed_factor_clk_ids[] = { |
| 232 | { .compatible = "fixed-factor-clock" }, |
| 233 | { } |
| 234 | }; |
| 235 | MODULE_DEVICE_TABLE(of, of_fixed_factor_clk_ids); |
| 236 | |
| 237 | static struct platform_driver of_fixed_factor_clk_driver = { |
| 238 | .driver = { |
| 239 | .name = "of_fixed_factor_clk", |
| 240 | .of_match_table = of_fixed_factor_clk_ids, |
| 241 | }, |
| 242 | .probe = of_fixed_factor_clk_probe, |
| 243 | .remove = of_fixed_factor_clk_remove, |
| 244 | }; |
| 245 | builtin_platform_driver(of_fixed_factor_clk_driver); |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 246 | #endif |