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