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