| 1 | |
| 2 | |
| 3 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <[email protected]>
|
| 4 |
|
| 5 | #include <linux/module.h>
|
| 6 | #include <linux/clk-provider.h>
|
| 7 | #include <linux/slab.h>
|
| 8 | #include <linux/err.h>
|
| 9 | #include <linux/of.h>
|
| 10 | #include <linux/platform_device.h>
|
| 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 |
|
| 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);
|
| 26 | unsigned long long int rate;
|
| 27 |
|
| 28 | rate = (unsigned long long int)parent_rate * fix->mult;
|
| 29 | do_div(rate, fix->div);
|
| 30 | return (unsigned long)rate;
|
| 31 | }
|
| 32 |
|
| 33 | static int clk_factor_determine_rate(struct clk_hw *hw,
|
| 34 | struct clk_rate_request *req)
|
| 35 | {
|
| 36 | struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
|
| 37 |
|
| 38 | if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
|
| 39 | struct clk_hw *parent_hw = clk_hw_get_parent(hw);
|
| 40 | unsigned long best_parent;
|
| 41 |
|
| 42 | if (!parent_hw)
|
| 43 | return -EINVAL;
|
| 44 |
|
| 45 | best_parent = (req->rate / fix->mult) * fix->div;
|
| 46 | req->best_parent_rate = clk_hw_round_rate(parent_hw, best_parent);
|
| 47 | }
|
| 48 |
|
| 49 | req->rate = (req->best_parent_rate / fix->div) * fix->mult;
|
| 50 |
|
| 51 | return 0;
|
| 52 | }
|
| 53 |
|
| 54 | static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
|
| 55 | unsigned long parent_rate)
|
| 56 | {
|
| 57 | |
| 58 | * We must report success but we can do so unconditionally because
|
| 59 | * clk_factor_determine_rate returns values that ensure this call is a
|
| 60 | * nop.
|
| 61 |
|
| 62 |
|
| 63 | return 0;
|
| 64 | }
|
| 65 |
|
| 66 | static unsigned long clk_factor_recalc_accuracy(struct clk_hw *hw,
|
| 67 | unsigned long parent_accuracy)
|
| 68 | {
|
| 69 | struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
|
| 70 |
|
| 71 | if (fix->flags & CLK_FIXED_FACTOR_FIXED_ACCURACY)
|
| 72 | return fix->acc;
|
| 73 |
|
| 74 | return parent_accuracy;
|
| 75 | }
|
| 76 |
|
| 77 | const struct clk_ops clk_fixed_factor_ops = {
|
| 78 | .determine_rate = clk_factor_determine_rate,
|
| 79 | .set_rate = clk_factor_set_rate,
|
| 80 | .recalc_rate = clk_factor_recalc_rate,
|
| 81 | .recalc_accuracy = clk_factor_recalc_accuracy,
|
| 82 | };
|
| 83 | EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
|
| 84 |
|
| 85 | static void devm_clk_hw_register_fixed_factor_release(struct device *dev, void *res)
|
| 86 | {
|
| 87 | struct clk_fixed_factor *fix = res;
|
| 88 |
|
| 89 | |
| 90 | * We can not use clk_hw_unregister_fixed_factor, since it will kfree()
|
| 91 | * the hw, resulting in double free. Just unregister the hw and let
|
| 92 | * devres code kfree() it.
|
| 93 |
|
| 94 | clk_hw_unregister(&fix->hw);
|
| 95 | }
|
| 96 |
|
| 97 | struct clk_hw *
|
| 98 | __clk_hw_register_fixed_factor(struct device *dev, struct device_node *np,
|
| 99 | const char *name, const char *parent_name,
|
| 100 | const struct clk_hw *parent_hw, const struct clk_parent_data *pdata,
|
| 101 | unsigned long flags, unsigned int mult, unsigned int div,
|
| 102 | unsigned long acc, unsigned int fixflags, bool devm)
|
| 103 | {
|
| 104 | struct clk_fixed_factor *fix;
|
| 105 | struct clk_init_data init = { };
|
| 106 | struct clk_hw *hw;
|
| 107 | int ret;
|
| 108 |
|
| 109 |
|
| 110 | if (devm && !dev)
|
| 111 | return ERR_PTR(-EINVAL);
|
| 112 |
|
| 113 | if (devm)
|
| 114 | fix = devres_alloc(devm_clk_hw_register_fixed_factor_release,
|
| 115 | sizeof(*fix), GFP_KERNEL);
|
| 116 | else
|
| 117 | fix = kmalloc_obj(*fix);
|
| 118 | if (!fix)
|
| 119 | return ERR_PTR(-ENOMEM);
|
| 120 |
|
| 121 |
|
| 122 | fix->mult = mult;
|
| 123 | fix->div = div;
|
| 124 | fix->hw.init = &init;
|
| 125 | fix->acc = acc;
|
| 126 | fix->flags = fixflags;
|
| 127 |
|
| 128 | init.name = name;
|
| 129 | init.ops = &clk_fixed_factor_ops;
|
| 130 | init.flags = flags;
|
| 131 | init.parent_names = parent_name ? &parent_name : NULL;
|
| 132 | init.parent_hws = parent_hw ? &parent_hw : NULL;
|
| 133 | init.parent_data = pdata;
|
| 134 | if (parent_name || parent_hw || pdata)
|
| 135 | init.num_parents = 1;
|
| 136 | else
|
| 137 | init.num_parents = 0;
|
| 138 |
|
| 139 | hw = &fix->hw;
|
| 140 | if (dev)
|
| 141 | ret = clk_hw_register(dev, hw);
|
| 142 | else
|
| 143 | ret = of_clk_hw_register(np, hw);
|
| 144 | if (ret) {
|
| 145 | if (devm)
|
| 146 | devres_free(fix);
|
| 147 | else
|
| 148 | kfree(fix);
|
| 149 | hw = ERR_PTR(ret);
|
| 150 | } else if (devm)
|
| 151 | devres_add(dev, fix);
|
| 152 |
|
| 153 | return hw;
|
| 154 | }
|
| 155 | EXPORT_SYMBOL_GPL(__clk_hw_register_fixed_factor);
|
| 156 |
|
| 157 | |
| 158 | * devm_clk_hw_register_fixed_factor_index - Register a fixed factor clock with
|
| 159 | * parent from DT index
|
| 160 | * @dev: device that is registering this clock
|
| 161 | * @name: name of this clock
|
| 162 | * @index: index of phandle in @dev 'clocks' property
|
| 163 | * @flags: fixed factor flags
|
| 164 | * @mult: multiplier
|
| 165 | * @div: divider
|
| 166 | *
|
| 167 | * Return: Pointer to fixed factor clk_hw structure that was registered or
|
| 168 | * an error pointer.
|
| 169 |
|
| 170 | struct clk_hw *devm_clk_hw_register_fixed_factor_index(struct device *dev,
|
| 171 | const char *name, unsigned int index, unsigned long flags,
|
| 172 | unsigned int mult, unsigned int div)
|
| 173 | {
|
| 174 | const struct clk_parent_data pdata = { .index = index };
|
| 175 |
|
| 176 | return __clk_hw_register_fixed_factor(dev, NULL, name, NULL, NULL, &pdata,
|
| 177 | flags, mult, div, 0, 0, true);
|
| 178 | }
|
| 179 | EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor_index);
|
| 180 |
|
| 181 | struct clk_hw *clk_hw_register_fixed_factor_fwname(struct device *dev,
|
| 182 | struct device_node *np, const char *name, const char *fw_name,
|
| 183 | unsigned long flags, unsigned int mult, unsigned int div)
|
| 184 | {
|
| 185 | const struct clk_parent_data pdata = { .index = -1, .fw_name = fw_name };
|
| 186 |
|
| 187 | return __clk_hw_register_fixed_factor(dev, np, name, NULL, NULL,
|
| 188 | &pdata, flags, mult, div, 0, 0, false);
|
| 189 | }
|
| 190 | EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor_fwname);
|
| 191 |
|
| 192 | struct clk_hw *clk_hw_register_fixed_factor_with_accuracy_fwname(struct device *dev,
|
| 193 | struct device_node *np, const char *name, const char *fw_name,
|
| 194 | unsigned long flags, unsigned int mult, unsigned int div,
|
| 195 | unsigned long acc)
|
| 196 | {
|
| 197 | const struct clk_parent_data pdata = { .index = -1, .fw_name = fw_name };
|
| 198 |
|
| 199 | return __clk_hw_register_fixed_factor(dev, np, name, NULL, NULL,
|
| 200 | &pdata, flags, mult, div, acc,
|
| 201 | CLK_FIXED_FACTOR_FIXED_ACCURACY, false);
|
| 202 | }
|
| 203 | EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor_with_accuracy_fwname);
|
| 204 |
|
| 205 | struct clk_hw *clk_hw_register_fixed_factor_index(struct device *dev,
|
| 206 | const char *name, unsigned int index, unsigned long flags,
|
| 207 | unsigned int mult, unsigned int div)
|
| 208 | {
|
| 209 | const struct clk_parent_data pdata = { .index = index };
|
| 210 |
|
| 211 | return __clk_hw_register_fixed_factor(dev, NULL, name, NULL, NULL, &pdata,
|
| 212 | flags, mult, div, 0, 0, false);
|
| 213 | }
|
| 214 | EXPORT_SYMBOL_GPL(clk_hw_register_fixed_factor_index);
|
| 215 |
|
| 216 | struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
|
| 217 | const char *parent_name, unsigned long flags,
|
| 218 | unsigned int mult, unsigned int div)
|
| 219 | {
|
| 220 | struct clk_hw *hw;
|
| 221 |
|
| 222 | hw = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
|
| 223 | div);
|
| 224 | if (IS_ERR(hw))
|
| 225 | return ERR_CAST(hw);
|
| 226 | return hw->clk;
|
| 227 | }
|
| 228 | EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
|
| 229 |
|
| 230 | void clk_unregister_fixed_factor(struct clk *clk)
|
| 231 | {
|
| 232 | struct clk_hw *hw;
|
| 233 |
|
| 234 | hw = __clk_get_hw(clk);
|
| 235 | if (!hw)
|
| 236 | return;
|
| 237 |
|
| 238 | clk_unregister(clk);
|
| 239 | kfree(to_clk_fixed_factor(hw));
|
| 240 | }
|
| 241 | EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
|
| 242 |
|
| 243 | void clk_hw_unregister_fixed_factor(struct clk_hw *hw)
|
| 244 | {
|
| 245 | struct clk_fixed_factor *fix;
|
| 246 |
|
| 247 | fix = to_clk_fixed_factor(hw);
|
| 248 |
|
| 249 | clk_hw_unregister(hw);
|
| 250 | kfree(fix);
|
| 251 | }
|
| 252 | EXPORT_SYMBOL_GPL(clk_hw_unregister_fixed_factor);
|
| 253 |
|
| 254 | struct clk_hw *devm_clk_hw_register_fixed_factor_fwname(struct device *dev,
|
| 255 | struct device_node *np, const char *name, const char *fw_name,
|
| 256 | unsigned long flags, unsigned int mult, unsigned int div)
|
| 257 | {
|
| 258 | const struct clk_parent_data pdata = { .index = -1, .fw_name = fw_name };
|
| 259 |
|
| 260 | return __clk_hw_register_fixed_factor(dev, np, name, NULL, NULL,
|
| 261 | &pdata, flags, mult, div, 0, 0, true);
|
| 262 | }
|
| 263 | EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor_fwname);
|
| 264 |
|
| 265 | struct clk_hw *devm_clk_hw_register_fixed_factor_with_accuracy_fwname(struct device *dev,
|
| 266 | struct device_node *np, const char *name, const char *fw_name,
|
| 267 | unsigned long flags, unsigned int mult, unsigned int div,
|
| 268 | unsigned long acc)
|
| 269 | {
|
| 270 | const struct clk_parent_data pdata = { .index = -1, .fw_name = fw_name };
|
| 271 |
|
| 272 | return __clk_hw_register_fixed_factor(dev, np, name, NULL, NULL,
|
| 273 | &pdata, flags, mult, div, acc,
|
| 274 | CLK_FIXED_FACTOR_FIXED_ACCURACY, true);
|
| 275 | }
|
| 276 | EXPORT_SYMBOL_GPL(devm_clk_hw_register_fixed_factor_with_accuracy_fwname);
|
| 277 |
|
| 278 | #ifdef CONFIG_OF
|
| 279 | static struct clk_hw *_of_fixed_factor_clk_setup(struct device_node *node)
|
| 280 | {
|
| 281 | struct clk_hw *hw;
|
| 282 | const char *clk_name = node->name;
|
| 283 | const struct clk_parent_data pdata = { .index = 0 };
|
| 284 | u32 div, mult;
|
| 285 | int ret;
|
| 286 |
|
| 287 | if (of_property_read_u32(node, "clock-div", &div)) {
|
| 288 | pr_err("%s Fixed factor clock <%pOFn> must have a clock-div property\n",
|
| 289 | __func__, node);
|
| 290 | return ERR_PTR(-EIO);
|
| 291 | }
|
| 292 |
|
| 293 | if (of_property_read_u32(node, "clock-mult", &mult)) {
|
| 294 | pr_err("%s Fixed factor clock <%pOFn> must have a clock-mult property\n",
|
| 295 | __func__, node);
|
| 296 | return ERR_PTR(-EIO);
|
| 297 | }
|
| 298 |
|
| 299 | of_property_read_string(node, "clock-output-names", &clk_name);
|
| 300 |
|
| 301 | hw = __clk_hw_register_fixed_factor(NULL, node, clk_name, NULL, NULL,
|
| 302 | &pdata, 0, mult, div, 0, 0, false);
|
| 303 | if (IS_ERR(hw)) {
|
| 304 | |
| 305 | * Clear OF_POPULATED flag so that clock registration can be
|
| 306 | * attempted again from probe function.
|
| 307 |
|
| 308 | of_node_clear_flag(node, OF_POPULATED);
|
| 309 | return ERR_CAST(hw);
|
| 310 | }
|
| 311 |
|
| 312 | ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
|
| 313 | if (ret) {
|
| 314 | clk_hw_unregister_fixed_factor(hw);
|
| 315 | return ERR_PTR(ret);
|
| 316 | }
|
| 317 |
|
| 318 | return hw;
|
| 319 | }
|
| 320 |
|
| 321 | |
| 322 | * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
|
| 323 | * @node: device node for the clock
|
| 324 |
|
| 325 | void __init of_fixed_factor_clk_setup(struct device_node *node)
|
| 326 | {
|
| 327 | _of_fixed_factor_clk_setup(node);
|
| 328 | }
|
| 329 | CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
|
| 330 | of_fixed_factor_clk_setup);
|
| 331 |
|
| 332 | static void of_fixed_factor_clk_remove(struct platform_device *pdev)
|
| 333 | {
|
| 334 | struct clk_hw *clk = platform_get_drvdata(pdev);
|
| 335 |
|
| 336 | of_clk_del_provider(pdev->dev.of_node);
|
| 337 | clk_hw_unregister_fixed_factor(clk);
|
| 338 | }
|
| 339 |
|
| 340 | static int of_fixed_factor_clk_probe(struct platform_device *pdev)
|
| 341 | {
|
| 342 | struct clk_hw *clk;
|
| 343 |
|
| 344 | |
| 345 | * This function is not executed when of_fixed_factor_clk_setup
|
| 346 | * succeeded.
|
| 347 |
|
| 348 | clk = _of_fixed_factor_clk_setup(pdev->dev.of_node);
|
| 349 | if (IS_ERR(clk))
|
| 350 | return PTR_ERR(clk);
|
| 351 |
|
| 352 | platform_set_drvdata(pdev, clk);
|
| 353 |
|
| 354 | return 0;
|
| 355 | }
|
| 356 |
|
| 357 | static const struct of_device_id of_fixed_factor_clk_ids[] = {
|
| 358 | { .compatible = "fixed-factor-clock" },
|
| 359 | { }
|
| 360 | };
|
| 361 | MODULE_DEVICE_TABLE(of, of_fixed_factor_clk_ids);
|
| 362 |
|
| 363 | static struct platform_driver of_fixed_factor_clk_driver = {
|
| 364 | .driver = {
|
| 365 | .name = "of_fixed_factor_clk",
|
| 366 | .of_match_table = of_fixed_factor_clk_ids,
|
| 367 | },
|
| 368 | .probe = of_fixed_factor_clk_probe,
|
| 369 | .remove = of_fixed_factor_clk_remove,
|
| 370 | };
|
| 371 | builtin_platform_driver(of_fixed_factor_clk_driver);
|
| 372 | #endif
|