clk-fixed-rate.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
  3. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Fixed rate clock implementation
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/err.h>
  16. #include <linux/of.h>
  17. /*
  18. * DOC: basic fixed-rate clock that cannot gate
  19. *
  20. * Traits of this clock:
  21. * prepare - clk_(un)prepare only ensures parents are prepared
  22. * enable - clk_enable only ensures parents are enabled
  23. * rate - rate is always a fixed value. No clk_set_rate support
  24. * parent - fixed parent. No clk_set_parent support
  25. */
  26. static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw,
  27. unsigned long parent_rate)
  28. {
  29. return to_clk_fixed_rate(hw)->fixed_rate;
  30. }
  31. static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw,
  32. unsigned long parent_accuracy)
  33. {
  34. return to_clk_fixed_rate(hw)->fixed_accuracy;
  35. }
  36. const struct clk_ops clk_fixed_rate_ops = {
  37. .recalc_rate = clk_fixed_rate_recalc_rate,
  38. .recalc_accuracy = clk_fixed_rate_recalc_accuracy,
  39. };
  40. EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);
  41. /**
  42. * clk_register_fixed_rate_with_accuracy - register fixed-rate clock with the
  43. * clock framework
  44. * @dev: device that is registering this clock
  45. * @name: name of this clock
  46. * @parent_name: name of clock's parent
  47. * @flags: framework-specific flags
  48. * @fixed_rate: non-adjustable clock rate
  49. * @fixed_accuracy: non-adjustable clock rate
  50. */
  51. struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
  52. const char *name, const char *parent_name, unsigned long flags,
  53. unsigned long fixed_rate, unsigned long fixed_accuracy)
  54. {
  55. struct clk_fixed_rate *fixed;
  56. struct clk *clk;
  57. struct clk_init_data init;
  58. /* allocate fixed-rate clock */
  59. fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
  60. if (!fixed)
  61. return ERR_PTR(-ENOMEM);
  62. init.name = name;
  63. init.ops = &clk_fixed_rate_ops;
  64. init.flags = flags | CLK_IS_BASIC;
  65. init.parent_names = (parent_name ? &parent_name: NULL);
  66. init.num_parents = (parent_name ? 1 : 0);
  67. /* struct clk_fixed_rate assignments */
  68. fixed->fixed_rate = fixed_rate;
  69. fixed->fixed_accuracy = fixed_accuracy;
  70. fixed->hw.init = &init;
  71. /* register the clock */
  72. clk = clk_register(dev, &fixed->hw);
  73. if (IS_ERR(clk))
  74. kfree(fixed);
  75. return clk;
  76. }
  77. EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy);
  78. /**
  79. * clk_register_fixed_rate - register fixed-rate clock with the clock framework
  80. * @dev: device that is registering this clock
  81. * @name: name of this clock
  82. * @parent_name: name of clock's parent
  83. * @flags: framework-specific flags
  84. * @fixed_rate: non-adjustable clock rate
  85. */
  86. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  87. const char *parent_name, unsigned long flags,
  88. unsigned long fixed_rate)
  89. {
  90. return clk_register_fixed_rate_with_accuracy(dev, name, parent_name,
  91. flags, fixed_rate, 0);
  92. }
  93. EXPORT_SYMBOL_GPL(clk_register_fixed_rate);
  94. void clk_unregister_fixed_rate(struct clk *clk)
  95. {
  96. struct clk_hw *hw;
  97. hw = __clk_get_hw(clk);
  98. if (!hw)
  99. return;
  100. clk_unregister(clk);
  101. kfree(to_clk_fixed_rate(hw));
  102. }
  103. EXPORT_SYMBOL_GPL(clk_unregister_fixed_rate);
  104. #ifdef CONFIG_OF
  105. /**
  106. * of_fixed_clk_setup() - Setup function for simple fixed rate clock
  107. */
  108. void of_fixed_clk_setup(struct device_node *node)
  109. {
  110. struct clk *clk;
  111. const char *clk_name = node->name;
  112. u32 rate;
  113. u32 accuracy = 0;
  114. if (of_property_read_u32(node, "clock-frequency", &rate))
  115. return;
  116. of_property_read_u32(node, "clock-accuracy", &accuracy);
  117. of_property_read_string(node, "clock-output-names", &clk_name);
  118. clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
  119. 0, rate, accuracy);
  120. if (!IS_ERR(clk))
  121. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  122. }
  123. EXPORT_SYMBOL_GPL(of_fixed_clk_setup);
  124. CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);
  125. #endif