clk-fixed-factor.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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>
  14. #include <linux/of.h>
  15. /*
  16. * DOC: basic fixed multiplier and divider clock that cannot gate
  17. *
  18. * Traits of this clock:
  19. * prepare - clk_prepare only ensures that parents are prepared
  20. * enable - clk_enable only ensures that parents are enabled
  21. * rate - rate is fixed. clk->rate = parent->rate / div * mult
  22. * parent - fixed parent. No clk_set_parent support
  23. */
  24. #define to_clk_fixed_factor(_hw) container_of(_hw, struct clk_fixed_factor, hw)
  25. static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
  26. unsigned long parent_rate)
  27. {
  28. struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
  29. unsigned long long int rate;
  30. rate = (unsigned long long int)parent_rate * fix->mult;
  31. do_div(rate, fix->div);
  32. return (unsigned long)rate;
  33. }
  34. static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
  35. unsigned long *prate)
  36. {
  37. struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
  38. if (__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT) {
  39. unsigned long best_parent;
  40. best_parent = (rate / fix->mult) * fix->div;
  41. *prate = __clk_round_rate(__clk_get_parent(hw->clk),
  42. best_parent);
  43. }
  44. return (*prate / fix->div) * fix->mult;
  45. }
  46. static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
  47. unsigned long parent_rate)
  48. {
  49. return 0;
  50. }
  51. struct clk_ops clk_fixed_factor_ops = {
  52. .round_rate = clk_factor_round_rate,
  53. .set_rate = clk_factor_set_rate,
  54. .recalc_rate = clk_factor_recalc_rate,
  55. };
  56. EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
  57. struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  58. const char *parent_name, unsigned long flags,
  59. unsigned int mult, unsigned int div)
  60. {
  61. struct clk_fixed_factor *fix;
  62. struct clk_init_data init;
  63. struct clk *clk;
  64. fix = kmalloc(sizeof(*fix), GFP_KERNEL);
  65. if (!fix) {
  66. pr_err("%s: could not allocate fixed factor clk\n", __func__);
  67. return ERR_PTR(-ENOMEM);
  68. }
  69. /* struct clk_fixed_factor assignments */
  70. fix->mult = mult;
  71. fix->div = div;
  72. fix->hw.init = &init;
  73. init.name = name;
  74. init.ops = &clk_fixed_factor_ops;
  75. init.flags = flags | CLK_IS_BASIC;
  76. init.parent_names = &parent_name;
  77. init.num_parents = 1;
  78. clk = clk_register(dev, &fix->hw);
  79. if (IS_ERR(clk))
  80. kfree(fix);
  81. return clk;
  82. }
  83. #ifdef CONFIG_OF
  84. /**
  85. * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
  86. */
  87. void __init of_fixed_factor_clk_setup(struct device_node *node)
  88. {
  89. struct clk *clk;
  90. const char *clk_name = node->name;
  91. const char *parent_name;
  92. u32 div, mult;
  93. if (of_property_read_u32(node, "clock-div", &div)) {
  94. pr_err("%s Fixed factor clock <%s> must have a clock-div property\n",
  95. __func__, node->name);
  96. return;
  97. }
  98. if (of_property_read_u32(node, "clock-mult", &mult)) {
  99. pr_err("%s Fixed factor clock <%s> must have a clokc-mult property\n",
  100. __func__, node->name);
  101. return;
  102. }
  103. of_property_read_string(node, "clock-output-names", &clk_name);
  104. parent_name = of_clk_get_parent_name(node, 0);
  105. clk = clk_register_fixed_factor(NULL, clk_name, parent_name, 0,
  106. mult, div);
  107. if (!IS_ERR(clk))
  108. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  109. }
  110. EXPORT_SYMBOL_GPL(of_fixed_factor_clk_setup);
  111. CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
  112. of_fixed_factor_clk_setup);
  113. #endif