clk-fixed-factor.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
  25. unsigned long parent_rate)
  26. {
  27. struct clk_fixed_factor *fix = to_clk_fixed_factor(hw);
  28. unsigned long long int rate;
  29. rate = (unsigned long long int)parent_rate * fix->mult;
  30. do_div(rate, fix->div);
  31. return (unsigned long)rate;
  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. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  38. unsigned long best_parent;
  39. best_parent = (rate / fix->mult) * fix->div;
  40. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  41. }
  42. return (*prate / fix->div) * fix->mult;
  43. }
  44. static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
  45. unsigned long parent_rate)
  46. {
  47. /*
  48. * We must report success but we can do so unconditionally because
  49. * clk_factor_round_rate returns values that ensure this call is a
  50. * nop.
  51. */
  52. return 0;
  53. }
  54. const struct clk_ops clk_fixed_factor_ops = {
  55. .round_rate = clk_factor_round_rate,
  56. .set_rate = clk_factor_set_rate,
  57. .recalc_rate = clk_factor_recalc_rate,
  58. };
  59. EXPORT_SYMBOL_GPL(clk_fixed_factor_ops);
  60. struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
  61. const char *parent_name, unsigned long flags,
  62. unsigned int mult, unsigned int div)
  63. {
  64. struct clk_fixed_factor *fix;
  65. struct clk_init_data init;
  66. struct clk *clk;
  67. fix = kmalloc(sizeof(*fix), GFP_KERNEL);
  68. if (!fix)
  69. return ERR_PTR(-ENOMEM);
  70. /* struct clk_fixed_factor assignments */
  71. fix->mult = mult;
  72. fix->div = div;
  73. fix->hw.init = &init;
  74. init.name = name;
  75. init.ops = &clk_fixed_factor_ops;
  76. init.flags = flags | CLK_IS_BASIC;
  77. init.parent_names = &parent_name;
  78. init.num_parents = 1;
  79. clk = clk_register(dev, &fix->hw);
  80. if (IS_ERR(clk))
  81. kfree(fix);
  82. return clk;
  83. }
  84. EXPORT_SYMBOL_GPL(clk_register_fixed_factor);
  85. void clk_unregister_fixed_factor(struct clk *clk)
  86. {
  87. struct clk_hw *hw;
  88. hw = __clk_get_hw(clk);
  89. if (!hw)
  90. return;
  91. clk_unregister(clk);
  92. kfree(to_clk_fixed_factor(hw));
  93. }
  94. EXPORT_SYMBOL_GPL(clk_unregister_fixed_factor);
  95. #ifdef CONFIG_OF
  96. /**
  97. * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
  98. */
  99. void __init of_fixed_factor_clk_setup(struct device_node *node)
  100. {
  101. struct clk *clk;
  102. const char *clk_name = node->name;
  103. const char *parent_name;
  104. u32 div, mult;
  105. if (of_property_read_u32(node, "clock-div", &div)) {
  106. pr_err("%s Fixed factor clock <%s> must have a clock-div property\n",
  107. __func__, node->name);
  108. return;
  109. }
  110. if (of_property_read_u32(node, "clock-mult", &mult)) {
  111. pr_err("%s Fixed factor clock <%s> must have a clock-mult property\n",
  112. __func__, node->name);
  113. return;
  114. }
  115. of_property_read_string(node, "clock-output-names", &clk_name);
  116. parent_name = of_clk_get_parent_name(node, 0);
  117. clk = clk_register_fixed_factor(NULL, clk_name, parent_name, 0,
  118. mult, div);
  119. if (!IS_ERR(clk))
  120. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  121. }
  122. EXPORT_SYMBOL_GPL(of_fixed_factor_clk_setup);
  123. CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock",
  124. of_fixed_factor_clk_setup);
  125. #endif