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