clk-fixed-factor.c 3.7 KB

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