clk-fixed-rate.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #define to_clk_fixed_rate(_hw) container_of(_hw, struct clk_fixed_rate, hw)
  27. static unsigned long clk_fixed_rate_recalc_rate(struct clk_hw *hw,
  28. unsigned long parent_rate)
  29. {
  30. return to_clk_fixed_rate(hw)->fixed_rate;
  31. }
  32. static unsigned long clk_fixed_rate_recalc_accuracy(struct clk_hw *hw,
  33. unsigned long parent_accuracy)
  34. {
  35. return to_clk_fixed_rate(hw)->fixed_accuracy;
  36. }
  37. const struct clk_ops clk_fixed_rate_ops = {
  38. .recalc_rate = clk_fixed_rate_recalc_rate,
  39. .recalc_accuracy = clk_fixed_rate_recalc_accuracy,
  40. };
  41. EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);
  42. /**
  43. * clk_register_fixed_rate_with_accuracy - register fixed-rate clock with the
  44. * clock framework
  45. * @dev: device that is registering this clock
  46. * @name: name of this clock
  47. * @parent_name: name of clock's parent
  48. * @flags: framework-specific flags
  49. * @fixed_rate: non-adjustable clock rate
  50. * @fixed_accuracy: non-adjustable clock rate
  51. */
  52. struct clk *clk_register_fixed_rate_with_accuracy(struct device *dev,
  53. const char *name, const char *parent_name, unsigned long flags,
  54. unsigned long fixed_rate, unsigned long fixed_accuracy)
  55. {
  56. struct clk_fixed_rate *fixed;
  57. struct clk *clk;
  58. struct clk_init_data init;
  59. /* allocate fixed-rate clock */
  60. fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
  61. if (!fixed) {
  62. pr_err("%s: could not allocate fixed clk\n", __func__);
  63. return ERR_PTR(-ENOMEM);
  64. }
  65. init.name = name;
  66. init.ops = &clk_fixed_rate_ops;
  67. init.flags = flags | CLK_IS_BASIC;
  68. init.parent_names = (parent_name ? &parent_name: NULL);
  69. init.num_parents = (parent_name ? 1 : 0);
  70. /* struct clk_fixed_rate assignments */
  71. fixed->fixed_rate = fixed_rate;
  72. fixed->fixed_accuracy = fixed_accuracy;
  73. fixed->hw.init = &init;
  74. /* register the clock */
  75. clk = clk_register(dev, &fixed->hw);
  76. if (IS_ERR(clk))
  77. kfree(fixed);
  78. return clk;
  79. }
  80. EXPORT_SYMBOL_GPL(clk_register_fixed_rate_with_accuracy);
  81. /**
  82. * clk_register_fixed_rate - register fixed-rate clock with the clock framework
  83. * @dev: device that is registering this clock
  84. * @name: name of this clock
  85. * @parent_name: name of clock's parent
  86. * @flags: framework-specific flags
  87. * @fixed_rate: non-adjustable clock rate
  88. */
  89. struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  90. const char *parent_name, unsigned long flags,
  91. unsigned long fixed_rate)
  92. {
  93. return clk_register_fixed_rate_with_accuracy(dev, name, parent_name,
  94. flags, fixed_rate, 0);
  95. }
  96. EXPORT_SYMBOL_GPL(clk_register_fixed_rate);
  97. #ifdef CONFIG_OF
  98. /**
  99. * of_fixed_clk_setup() - Setup function for simple fixed rate clock
  100. */
  101. void of_fixed_clk_setup(struct device_node *node)
  102. {
  103. struct clk *clk;
  104. const char *clk_name = node->name;
  105. u32 rate;
  106. u32 accuracy = 0;
  107. if (of_property_read_u32(node, "clock-frequency", &rate))
  108. return;
  109. of_property_read_u32(node, "clock-accuracy", &accuracy);
  110. of_property_read_string(node, "clock-output-names", &clk_name);
  111. clk = clk_register_fixed_rate_with_accuracy(NULL, clk_name, NULL,
  112. CLK_IS_ROOT, rate,
  113. accuracy);
  114. if (!IS_ERR(clk))
  115. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  116. }
  117. EXPORT_SYMBOL_GPL(of_fixed_clk_setup);
  118. CLK_OF_DECLARE(fixed_clk, "fixed-clock", of_fixed_clk_setup);
  119. #endif