clk-gate-zynqmp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Zynq UltraScale+ MPSoC clock controller
  4. *
  5. * Copyright (C) 2016-2018 Xilinx
  6. *
  7. * Gated clock implementation
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/slab.h>
  11. #include "clk-zynqmp.h"
  12. /**
  13. * struct zynqmp_clk_gate - gating clock
  14. * @hw: handle between common and hardware-specific interfaces
  15. * @flags: hardware-specific flags
  16. * @clk_id: Id of clock
  17. */
  18. struct zynqmp_clk_gate {
  19. struct clk_hw hw;
  20. u8 flags;
  21. u32 clk_id;
  22. };
  23. #define to_zynqmp_clk_gate(_hw) container_of(_hw, struct zynqmp_clk_gate, hw)
  24. /**
  25. * zynqmp_clk_gate_enable() - Enable clock
  26. * @hw: handle between common and hardware-specific interfaces
  27. *
  28. * Return: 0 on success else error code
  29. */
  30. static int zynqmp_clk_gate_enable(struct clk_hw *hw)
  31. {
  32. struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(hw);
  33. const char *clk_name = clk_hw_get_name(hw);
  34. u32 clk_id = gate->clk_id;
  35. int ret;
  36. ret = zynqmp_pm_clock_enable(clk_id);
  37. if (ret)
  38. pr_debug("%s() clock enable failed for %s (id %d), ret = %d\n",
  39. __func__, clk_name, clk_id, ret);
  40. return ret;
  41. }
  42. /*
  43. * zynqmp_clk_gate_disable() - Disable clock
  44. * @hw: handle between common and hardware-specific interfaces
  45. */
  46. static void zynqmp_clk_gate_disable(struct clk_hw *hw)
  47. {
  48. struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(hw);
  49. const char *clk_name = clk_hw_get_name(hw);
  50. u32 clk_id = gate->clk_id;
  51. int ret;
  52. ret = zynqmp_pm_clock_disable(clk_id);
  53. if (ret)
  54. pr_debug("%s() clock disable failed for %s (id %d), ret = %d\n",
  55. __func__, clk_name, clk_id, ret);
  56. }
  57. /**
  58. * zynqmp_clk_gate_is_enabled() - Check clock state
  59. * @hw: handle between common and hardware-specific interfaces
  60. *
  61. * Return: 1 if enabled, 0 if disabled else error code
  62. */
  63. static int zynqmp_clk_gate_is_enabled(struct clk_hw *hw)
  64. {
  65. struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(hw);
  66. const char *clk_name = clk_hw_get_name(hw);
  67. u32 clk_id = gate->clk_id;
  68. int state, ret;
  69. ret = zynqmp_pm_clock_getstate(clk_id, &state);
  70. if (ret) {
  71. pr_debug("%s() clock get state failed for %s, ret = %d\n",
  72. __func__, clk_name, ret);
  73. return -EIO;
  74. }
  75. return state ? 1 : 0;
  76. }
  77. static const struct clk_ops zynqmp_clk_gate_ops = {
  78. .enable = zynqmp_clk_gate_enable,
  79. .disable = zynqmp_clk_gate_disable,
  80. .is_enabled = zynqmp_clk_gate_is_enabled,
  81. };
  82. /**
  83. * zynqmp_clk_register_gate() - Register a gate clock with the clock framework
  84. * @name: Name of this clock
  85. * @clk_id: Id of this clock
  86. * @parents: Name of this clock's parents
  87. * @num_parents: Number of parents
  88. * @nodes: Clock topology node
  89. *
  90. * Return: clock hardware of the registered clock gate
  91. */
  92. struct clk_hw *zynqmp_clk_register_gate(const char *name, u32 clk_id,
  93. const char * const *parents,
  94. u8 num_parents,
  95. const struct clock_topology *nodes)
  96. {
  97. struct zynqmp_clk_gate *gate;
  98. struct clk_hw *hw;
  99. int ret;
  100. struct clk_init_data init;
  101. /* allocate the gate */
  102. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  103. if (!gate)
  104. return ERR_PTR(-ENOMEM);
  105. init.name = name;
  106. init.ops = &zynqmp_clk_gate_ops;
  107. init.flags = zynqmp_clk_map_common_ccf_flags(nodes->flag);
  108. init.parent_names = parents;
  109. init.num_parents = 1;
  110. /* struct clk_gate assignments */
  111. gate->flags = nodes->type_flag;
  112. gate->hw.init = &init;
  113. gate->clk_id = clk_id;
  114. hw = &gate->hw;
  115. ret = clk_hw_register(NULL, hw);
  116. if (ret) {
  117. kfree(gate);
  118. hw = ERR_PTR(ret);
  119. }
  120. return hw;
  121. }