module: Convert symbol namespace to string literal
[linux.git] / drivers / clk / clk_kunit_helpers.c
blob68a28e70bb61cde465a9a3cf6bb539c06d04beff
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>
11 #include <kunit/clk.h>
12 #include <kunit/resource.h>
14 KUNIT_DEFINE_ACTION_WRAPPER(clk_disable_unprepare_wrapper,
15 clk_disable_unprepare, struct clk *);
16 /**
17 * clk_prepare_enable_kunit() - Test managed clk_prepare_enable()
18 * @test: The test context
19 * @clk: clk to prepare and enable
21 * Return: 0 on success, or negative errno on failure.
23 int clk_prepare_enable_kunit(struct kunit *test, struct clk *clk)
25 int ret;
27 ret = clk_prepare_enable(clk);
28 if (ret)
29 return ret;
31 return kunit_add_action_or_reset(test, clk_disable_unprepare_wrapper,
32 clk);
34 EXPORT_SYMBOL_GPL(clk_prepare_enable_kunit);
36 KUNIT_DEFINE_ACTION_WRAPPER(clk_put_wrapper, clk_put, struct clk *);
38 static struct clk *__clk_get_kunit(struct kunit *test, struct clk *clk)
40 int ret;
42 if (IS_ERR(clk))
43 return clk;
45 ret = kunit_add_action_or_reset(test, clk_put_wrapper, clk);
46 if (ret)
47 return ERR_PTR(ret);
49 return clk;
52 /**
53 * clk_get_kunit() - Test managed clk_get()
54 * @test: The test context
55 * @dev: device for clock "consumer"
56 * @con_id: clock consumer ID
58 * Just like clk_get(), except the clk is managed by the test case and is
59 * automatically put with clk_put() after the test case concludes.
61 * Return: new clk consumer or ERR_PTR on failure.
63 struct clk *
64 clk_get_kunit(struct kunit *test, struct device *dev, const char *con_id)
66 struct clk *clk;
68 clk = clk_get(dev, con_id);
70 return __clk_get_kunit(test, clk);
72 EXPORT_SYMBOL_GPL(clk_get_kunit);
74 /**
75 * of_clk_get_kunit() - Test managed of_clk_get()
76 * @test: The test context
77 * @np: device_node for clock "consumer"
78 * @index: index in 'clocks' property of @np
80 * Just like of_clk_get(), except the clk is managed by the test case and is
81 * automatically put with clk_put() after the test case concludes.
83 * Return: new clk consumer or ERR_PTR on failure.
85 struct clk *
86 of_clk_get_kunit(struct kunit *test, struct device_node *np, int index)
88 struct clk *clk;
90 clk = of_clk_get(np, index);
92 return __clk_get_kunit(test, clk);
94 EXPORT_SYMBOL_GPL(of_clk_get_kunit);
96 /**
97 * clk_hw_get_clk_kunit() - Test managed clk_hw_get_clk()
98 * @test: The test context
99 * @hw: clk_hw associated with the clk being consumed
100 * @con_id: connection ID string on device
102 * Just like clk_hw_get_clk(), except the clk is managed by the test case and
103 * is automatically put with clk_put() after the test case concludes.
105 * Return: new clk consumer or ERR_PTR on failure.
107 struct clk *
108 clk_hw_get_clk_kunit(struct kunit *test, struct clk_hw *hw, const char *con_id)
110 struct clk *clk;
112 clk = clk_hw_get_clk(hw, con_id);
114 return __clk_get_kunit(test, clk);
116 EXPORT_SYMBOL_GPL(clk_hw_get_clk_kunit);
119 * clk_hw_get_clk_prepared_enabled_kunit() - Test managed clk_hw_get_clk() + clk_prepare_enable()
120 * @test: The test context
121 * @hw: clk_hw associated with the clk being consumed
122 * @con_id: connection ID string on device
124 * Just like
126 * .. code-block:: c
128 * struct clk *clk = clk_hw_get_clk(...);
129 * clk_prepare_enable(clk);
131 * except the clk is managed by the test case and is automatically disabled and
132 * unprepared with clk_disable_unprepare() and put with clk_put() after the
133 * test case concludes.
135 * Return: new clk consumer that is prepared and enabled or ERR_PTR on failure.
137 struct clk *
138 clk_hw_get_clk_prepared_enabled_kunit(struct kunit *test, struct clk_hw *hw,
139 const char *con_id)
141 int ret;
142 struct clk *clk;
144 clk = clk_hw_get_clk_kunit(test, hw, con_id);
145 if (IS_ERR(clk))
146 return clk;
148 ret = clk_prepare_enable_kunit(test, clk);
149 if (ret)
150 return ERR_PTR(ret);
152 return clk;
154 EXPORT_SYMBOL_GPL(clk_hw_get_clk_prepared_enabled_kunit);
156 KUNIT_DEFINE_ACTION_WRAPPER(clk_hw_unregister_wrapper,
157 clk_hw_unregister, struct clk_hw *);
160 * clk_hw_register_kunit() - Test managed clk_hw_register()
161 * @test: The test context
162 * @dev: device that is registering this clock
163 * @hw: link to hardware-specific clock data
165 * Just like clk_hw_register(), except the clk registration is managed by the
166 * test case and is automatically unregistered after the test case concludes.
168 * Return: 0 on success or a negative errno value on failure.
170 int clk_hw_register_kunit(struct kunit *test, struct device *dev, struct clk_hw *hw)
172 int ret;
174 ret = clk_hw_register(dev, hw);
175 if (ret)
176 return ret;
178 return kunit_add_action_or_reset(test, clk_hw_unregister_wrapper, hw);
180 EXPORT_SYMBOL_GPL(clk_hw_register_kunit);
183 * of_clk_hw_register_kunit() - Test managed of_clk_hw_register()
184 * @test: The test context
185 * @node: device_node of device that is registering this clock
186 * @hw: link to hardware-specific clock data
188 * Just like of_clk_hw_register(), except the clk registration is managed by
189 * the test case and is automatically unregistered after the test case
190 * concludes.
192 * Return: 0 on success or a negative errno value on failure.
194 int of_clk_hw_register_kunit(struct kunit *test, struct device_node *node, struct clk_hw *hw)
196 int ret;
198 ret = of_clk_hw_register(node, hw);
199 if (ret)
200 return ret;
202 return kunit_add_action_or_reset(test, clk_hw_unregister_wrapper, hw);
204 EXPORT_SYMBOL_GPL(of_clk_hw_register_kunit);
206 KUNIT_DEFINE_ACTION_WRAPPER(of_clk_del_provider_wrapper,
207 of_clk_del_provider, struct device_node *);
210 * of_clk_add_hw_provider_kunit() - Test managed of_clk_add_hw_provider()
211 * @test: The test context
212 * @np: Device node pointer associated with clock provider
213 * @get: Callback for decoding clk_hw
214 * @data: Context pointer for @get callback.
216 * Just like of_clk_add_hw_provider(), except the clk_hw provider is managed by
217 * the test case and is automatically unregistered after the test case
218 * concludes.
220 * Return: 0 on success or a negative errno value on failure.
222 int of_clk_add_hw_provider_kunit(struct kunit *test, struct device_node *np,
223 struct clk_hw *(*get)(struct of_phandle_args *clkspec, void *data),
224 void *data)
226 int ret;
228 ret = of_clk_add_hw_provider(np, get, data);
229 if (ret)
230 return ret;
232 return kunit_add_action_or_reset(test, of_clk_del_provider_wrapper, np);
234 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider_kunit);
236 MODULE_LICENSE("GPL");
237 MODULE_DESCRIPTION("KUnit helpers for clk providers and consumers");