clk_kunit_helpers.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit helpers for clk providers and consumers
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/err.h>
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <kunit/clk.h>
  11. #include <kunit/resource.h>
  12. KUNIT_DEFINE_ACTION_WRAPPER(clk_disable_unprepare_wrapper,
  13. clk_disable_unprepare, struct clk *);
  14. /**
  15. * clk_prepare_enable_kunit() - Test managed clk_prepare_enable()
  16. * @test: The test context
  17. * @clk: clk to prepare and enable
  18. *
  19. * Return: 0 on success, or negative errno on failure.
  20. */
  21. int clk_prepare_enable_kunit(struct kunit *test, struct clk *clk)
  22. {
  23. int ret;
  24. ret = clk_prepare_enable(clk);
  25. if (ret)
  26. return ret;
  27. return kunit_add_action_or_reset(test, clk_disable_unprepare_wrapper,
  28. clk);
  29. }
  30. EXPORT_SYMBOL_GPL(clk_prepare_enable_kunit);
  31. KUNIT_DEFINE_ACTION_WRAPPER(clk_put_wrapper, clk_put, struct clk *);
  32. static struct clk *__clk_get_kunit(struct kunit *test, struct clk *clk)
  33. {
  34. int ret;
  35. if (IS_ERR(clk))
  36. return clk;
  37. ret = kunit_add_action_or_reset(test, clk_put_wrapper, clk);
  38. if (ret)
  39. return ERR_PTR(ret);
  40. return clk;
  41. }
  42. /**
  43. * clk_get_kunit() - Test managed clk_get()
  44. * @test: The test context
  45. * @dev: device for clock "consumer"
  46. * @con_id: clock consumer ID
  47. *
  48. * Just like clk_get(), except the clk is managed by the test case and is
  49. * automatically put with clk_put() after the test case concludes.
  50. *
  51. * Return: new clk consumer or ERR_PTR on failure.
  52. */
  53. struct clk *
  54. clk_get_kunit(struct kunit *test, struct device *dev, const char *con_id)
  55. {
  56. struct clk *clk;
  57. clk = clk_get(dev, con_id);
  58. return __clk_get_kunit(test, clk);
  59. }
  60. EXPORT_SYMBOL_GPL(clk_get_kunit);
  61. /**
  62. * of_clk_get_kunit() - Test managed of_clk_get()
  63. * @test: The test context
  64. * @np: device_node for clock "consumer"
  65. * @index: index in 'clocks' property of @np
  66. *
  67. * Just like of_clk_get(), except the clk is managed by the test case and is
  68. * automatically put with clk_put() after the test case concludes.
  69. *
  70. * Return: new clk consumer or ERR_PTR on failure.
  71. */
  72. struct clk *
  73. of_clk_get_kunit(struct kunit *test, struct device_node *np, int index)
  74. {
  75. struct clk *clk;
  76. clk = of_clk_get(np, index);
  77. return __clk_get_kunit(test, clk);
  78. }
  79. EXPORT_SYMBOL_GPL(of_clk_get_kunit);
  80. /**
  81. * clk_hw_get_clk_kunit() - Test managed clk_hw_get_clk()
  82. * @test: The test context
  83. * @hw: clk_hw associated with the clk being consumed
  84. * @con_id: connection ID string on device
  85. *
  86. * Just like clk_hw_get_clk(), except the clk is managed by the test case and
  87. * is automatically put with clk_put() after the test case concludes.
  88. *
  89. * Return: new clk consumer or ERR_PTR on failure.
  90. */
  91. struct clk *
  92. clk_hw_get_clk_kunit(struct kunit *test, struct clk_hw *hw, const char *con_id)
  93. {
  94. struct clk *clk;
  95. clk = clk_hw_get_clk(hw, con_id);
  96. return __clk_get_kunit(test, clk);
  97. }
  98. EXPORT_SYMBOL_GPL(clk_hw_get_clk_kunit);
  99. /**
  100. * clk_hw_get_clk_prepared_enabled_kunit() - Test managed clk_hw_get_clk() + clk_prepare_enable()
  101. * @test: The test context
  102. * @hw: clk_hw associated with the clk being consumed
  103. * @con_id: connection ID string on device
  104. *
  105. * Just like
  106. *
  107. * .. code-block:: c
  108. *
  109. * struct clk *clk = clk_hw_get_clk(...);
  110. * clk_prepare_enable(clk);
  111. *
  112. * except the clk is managed by the test case and is automatically disabled and
  113. * unprepared with clk_disable_unprepare() and put with clk_put() after the
  114. * test case concludes.
  115. *
  116. * Return: new clk consumer that is prepared and enabled or ERR_PTR on failure.
  117. */
  118. struct clk *
  119. clk_hw_get_clk_prepared_enabled_kunit(struct kunit *test, struct clk_hw *hw,
  120. const char *con_id)
  121. {
  122. int ret;
  123. struct clk *clk;
  124. clk = clk_hw_get_clk_kunit(test, hw, con_id);
  125. if (IS_ERR(clk))
  126. return clk;
  127. ret = clk_prepare_enable_kunit(test, clk);
  128. if (ret)
  129. return ERR_PTR(ret);
  130. return clk;
  131. }
  132. EXPORT_SYMBOL_GPL(clk_hw_get_clk_prepared_enabled_kunit);
  133. KUNIT_DEFINE_ACTION_WRAPPER(clk_hw_unregister_wrapper,
  134. clk_hw_unregister, struct clk_hw *);
  135. /**
  136. * clk_hw_register_kunit() - Test managed clk_hw_register()
  137. * @test: The test context
  138. * @dev: device that is registering this clock
  139. * @hw: link to hardware-specific clock data
  140. *
  141. * Just like clk_hw_register(), except the clk registration is managed by the
  142. * test case and is automatically unregistered after the test case concludes.
  143. *
  144. * Return: 0 on success or a negative errno value on failure.
  145. */
  146. int clk_hw_register_kunit(struct kunit *test, struct device *dev, struct clk_hw *hw)
  147. {
  148. int ret;
  149. ret = clk_hw_register(dev, hw);
  150. if (ret)
  151. return ret;
  152. return kunit_add_action_or_reset(test, clk_hw_unregister_wrapper, hw);
  153. }
  154. EXPORT_SYMBOL_GPL(clk_hw_register_kunit);
  155. /**
  156. * of_clk_hw_register_kunit() - Test managed of_clk_hw_register()
  157. * @test: The test context
  158. * @node: device_node of device that is registering this clock
  159. * @hw: link to hardware-specific clock data
  160. *
  161. * Just like of_clk_hw_register(), except the clk registration is managed by
  162. * the test case and is automatically unregistered after the test case
  163. * concludes.
  164. *
  165. * Return: 0 on success or a negative errno value on failure.
  166. */
  167. int of_clk_hw_register_kunit(struct kunit *test, struct device_node *node, struct clk_hw *hw)
  168. {
  169. int ret;
  170. ret = of_clk_hw_register(node, hw);
  171. if (ret)
  172. return ret;
  173. return kunit_add_action_or_reset(test, clk_hw_unregister_wrapper, hw);
  174. }
  175. EXPORT_SYMBOL_GPL(of_clk_hw_register_kunit);
  176. MODULE_LICENSE("GPL");
  177. MODULE_DESCRIPTION("KUnit helpers for clk providers and consumers");