clk-fixed-factor.c 3.7 KB

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