clk-gate-zynqmp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 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. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  37. ret = eemi_ops->clock_enable(clk_id);
  38. if (ret)
  39. pr_warn_once("%s() clock enabled failed for %s, ret = %d\n",
  40. __func__, clk_name, ret);
  41. return ret;
  42. }
  43. /*
  44. * zynqmp_clk_gate_disable() - Disable clock
  45. * @hw: handle between common and hardware-specific interfaces
  46. */
  47. static void zynqmp_clk_gate_disable(struct clk_hw *hw)
  48. {
  49. struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(hw);
  50. const char *clk_name = clk_hw_get_name(hw);
  51. u32 clk_id = gate->clk_id;
  52. int ret;
  53. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  54. ret = eemi_ops->clock_disable(clk_id);
  55. if (ret)
  56. pr_warn_once("%s() clock disable failed for %s, ret = %d\n",
  57. __func__, clk_name, ret);
  58. }
  59. /**
  60. * zynqmp_clk_gate_is_enable() - Check clock state
  61. * @hw: handle between common and hardware-specific interfaces
  62. *
  63. * Return: 1 if enabled, 0 if disabled else error code
  64. */
  65. static int zynqmp_clk_gate_is_enabled(struct clk_hw *hw)
  66. {
  67. struct zynqmp_clk_gate *gate = to_zynqmp_clk_gate(hw);
  68. const char *clk_name = clk_hw_get_name(hw);
  69. u32 clk_id = gate->clk_id;
  70. int state, ret;
  71. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  72. ret = eemi_ops->clock_getstate(clk_id, &state);
  73. if (ret) {
  74. pr_warn_once("%s() clock get state failed for %s, ret = %d\n",
  75. __func__, clk_name, ret);
  76. return -EIO;
  77. }
  78. return state ? 1 : 0;
  79. }
  80. static const struct clk_ops zynqmp_clk_gate_ops = {
  81. .enable = zynqmp_clk_gate_enable,
  82. .disable = zynqmp_clk_gate_disable,
  83. .is_enabled = zynqmp_clk_gate_is_enabled,
  84. };
  85. /**
  86. * zynqmp_clk_register_gate() - Register a gate clock with the clock framework
  87. * @name: Name of this clock
  88. * @clk_id: Id of this clock
  89. * @parents: Name of this clock's parents
  90. * @num_parents: Number of parents
  91. * @nodes: Clock topology node
  92. *
  93. * Return: clock hardware of the registered clock gate
  94. */
  95. struct clk_hw *zynqmp_clk_register_gate(const char *name, u32 clk_id,
  96. const char * const *parents,
  97. u8 num_parents,
  98. const struct clock_topology *nodes)
  99. {
  100. struct zynqmp_clk_gate *gate;
  101. struct clk_hw *hw;
  102. int ret;
  103. struct clk_init_data init;
  104. /* allocate the gate */
  105. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  106. if (!gate)
  107. return ERR_PTR(-ENOMEM);
  108. init.name = name;
  109. init.ops = &zynqmp_clk_gate_ops;
  110. init.flags = nodes->flag;
  111. init.parent_names = parents;
  112. init.num_parents = 1;
  113. /* struct clk_gate assignments */
  114. gate->flags = nodes->type_flag;
  115. gate->hw.init = &init;
  116. gate->clk_id = clk_id;
  117. hw = &gate->hw;
  118. ret = clk_hw_register(NULL, hw);
  119. if (ret) {
  120. kfree(gate);
  121. hw = ERR_PTR(ret);
  122. }
  123. return hw;
  124. }