xref: /linux-6.15/include/linux/reset.h (revision 0ca10b60)
1 #ifndef _LINUX_RESET_H_
2 #define _LINUX_RESET_H_
3 
4 #include <linux/device.h>
5 
6 struct reset_control;
7 
8 #ifdef CONFIG_RESET_CONTROLLER
9 
10 int reset_control_reset(struct reset_control *rstc);
11 int reset_control_assert(struct reset_control *rstc);
12 int reset_control_deassert(struct reset_control *rstc);
13 int reset_control_status(struct reset_control *rstc);
14 
15 struct reset_control *__of_reset_control_get(struct device_node *node,
16 				     const char *id, int index, bool shared,
17 				     bool optional);
18 void reset_control_put(struct reset_control *rstc);
19 struct reset_control *__devm_reset_control_get(struct device *dev,
20 				     const char *id, int index, bool shared,
21 				     bool optional);
22 
23 int __must_check device_reset(struct device *dev);
24 
25 static inline int device_reset_optional(struct device *dev)
26 {
27 	return device_reset(dev);
28 }
29 
30 #else
31 
32 static inline int reset_control_reset(struct reset_control *rstc)
33 {
34 	return 0;
35 }
36 
37 static inline int reset_control_assert(struct reset_control *rstc)
38 {
39 	return 0;
40 }
41 
42 static inline int reset_control_deassert(struct reset_control *rstc)
43 {
44 	return 0;
45 }
46 
47 static inline int reset_control_status(struct reset_control *rstc)
48 {
49 	return 0;
50 }
51 
52 static inline void reset_control_put(struct reset_control *rstc)
53 {
54 }
55 
56 static inline int __must_check device_reset(struct device *dev)
57 {
58 	WARN_ON(1);
59 	return -ENOTSUPP;
60 }
61 
62 static inline int device_reset_optional(struct device *dev)
63 {
64 	return -ENOTSUPP;
65 }
66 
67 static inline struct reset_control *__of_reset_control_get(
68 					struct device_node *node,
69 					const char *id, int index, bool shared,
70 					bool optional)
71 {
72 	return optional ? NULL : ERR_PTR(-ENOTSUPP);
73 }
74 
75 static inline struct reset_control *__devm_reset_control_get(
76 					struct device *dev, const char *id,
77 					int index, bool shared, bool optional)
78 {
79 	return optional ? NULL : ERR_PTR(-ENOTSUPP);
80 }
81 
82 #endif /* CONFIG_RESET_CONTROLLER */
83 
84 /**
85  * reset_control_get_exclusive - Lookup and obtain an exclusive reference
86  *                               to a reset controller.
87  * @dev: device to be reset by the controller
88  * @id: reset line name
89  *
90  * Returns a struct reset_control or IS_ERR() condition containing errno.
91  * If this function is called more then once for the same reset_control it will
92  * return -EBUSY.
93  *
94  * See reset_control_get_shared for details on shared references to
95  * reset-controls.
96  *
97  * Use of id names is optional.
98  */
99 static inline struct reset_control *
100 __must_check reset_control_get_exclusive(struct device *dev, const char *id)
101 {
102 #ifndef CONFIG_RESET_CONTROLLER
103 	WARN_ON(1);
104 #endif
105 	return __of_reset_control_get(dev ? dev->of_node : NULL, id, 0, false,
106 									false);
107 }
108 
109 /**
110  * reset_control_get_shared - Lookup and obtain a shared reference to a
111  *                            reset controller.
112  * @dev: device to be reset by the controller
113  * @id: reset line name
114  *
115  * Returns a struct reset_control or IS_ERR() condition containing errno.
116  * This function is intended for use with reset-controls which are shared
117  * between hardware-blocks.
118  *
119  * When a reset-control is shared, the behavior of reset_control_assert /
120  * deassert is changed, the reset-core will keep track of a deassert_count
121  * and only (re-)assert the reset after reset_control_assert has been called
122  * as many times as reset_control_deassert was called. Also see the remark
123  * about shared reset-controls in the reset_control_assert docs.
124  *
125  * Calling reset_control_assert without first calling reset_control_deassert
126  * is not allowed on a shared reset control. Calling reset_control_reset is
127  * also not allowed on a shared reset control.
128  *
129  * Use of id names is optional.
130  */
131 static inline struct reset_control *reset_control_get_shared(
132 					struct device *dev, const char *id)
133 {
134 	return __of_reset_control_get(dev ? dev->of_node : NULL, id, 0, true,
135 									false);
136 }
137 
138 static inline struct reset_control *reset_control_get_optional_exclusive(
139 					struct device *dev, const char *id)
140 {
141 	return __of_reset_control_get(dev ? dev->of_node : NULL, id, 0, false,
142 									true);
143 }
144 
145 static inline struct reset_control *reset_control_get_optional_shared(
146 					struct device *dev, const char *id)
147 {
148 	return __of_reset_control_get(dev ? dev->of_node : NULL, id, 0, true,
149 									true);
150 }
151 
152 /**
153  * of_reset_control_get_exclusive - Lookup and obtain an exclusive reference
154  *                                  to a reset controller.
155  * @node: device to be reset by the controller
156  * @id: reset line name
157  *
158  * Returns a struct reset_control or IS_ERR() condition containing errno.
159  *
160  * Use of id names is optional.
161  */
162 static inline struct reset_control *of_reset_control_get_exclusive(
163 				struct device_node *node, const char *id)
164 {
165 	return __of_reset_control_get(node, id, 0, false, false);
166 }
167 
168 /**
169  * of_reset_control_get_shared - Lookup and obtain an shared reference
170  *                               to a reset controller.
171  * @node: device to be reset by the controller
172  * @id: reset line name
173  *
174  * When a reset-control is shared, the behavior of reset_control_assert /
175  * deassert is changed, the reset-core will keep track of a deassert_count
176  * and only (re-)assert the reset after reset_control_assert has been called
177  * as many times as reset_control_deassert was called. Also see the remark
178  * about shared reset-controls in the reset_control_assert docs.
179  *
180  * Calling reset_control_assert without first calling reset_control_deassert
181  * is not allowed on a shared reset control. Calling reset_control_reset is
182  * also not allowed on a shared reset control.
183  * Returns a struct reset_control or IS_ERR() condition containing errno.
184  *
185  * Use of id names is optional.
186  */
187 static inline struct reset_control *of_reset_control_get_shared(
188 				struct device_node *node, const char *id)
189 {
190 	return __of_reset_control_get(node, id, 0, true, false);
191 }
192 
193 /**
194  * of_reset_control_get_exclusive_by_index - Lookup and obtain an exclusive
195  *                                           reference to a reset controller
196  *                                           by index.
197  * @node: device to be reset by the controller
198  * @index: index of the reset controller
199  *
200  * This is to be used to perform a list of resets for a device or power domain
201  * in whatever order. Returns a struct reset_control or IS_ERR() condition
202  * containing errno.
203  */
204 static inline struct reset_control *of_reset_control_get_exclusive_by_index(
205 					struct device_node *node, int index)
206 {
207 	return __of_reset_control_get(node, NULL, index, false, false);
208 }
209 
210 /**
211  * of_reset_control_get_shared_by_index - Lookup and obtain an shared
212  *                                        reference to a reset controller
213  *                                        by index.
214  * @node: device to be reset by the controller
215  * @index: index of the reset controller
216  *
217  * When a reset-control is shared, the behavior of reset_control_assert /
218  * deassert is changed, the reset-core will keep track of a deassert_count
219  * and only (re-)assert the reset after reset_control_assert has been called
220  * as many times as reset_control_deassert was called. Also see the remark
221  * about shared reset-controls in the reset_control_assert docs.
222  *
223  * Calling reset_control_assert without first calling reset_control_deassert
224  * is not allowed on a shared reset control. Calling reset_control_reset is
225  * also not allowed on a shared reset control.
226  * Returns a struct reset_control or IS_ERR() condition containing errno.
227  *
228  * This is to be used to perform a list of resets for a device or power domain
229  * in whatever order. Returns a struct reset_control or IS_ERR() condition
230  * containing errno.
231  */
232 static inline struct reset_control *of_reset_control_get_shared_by_index(
233 					struct device_node *node, int index)
234 {
235 	return __of_reset_control_get(node, NULL, index, true, false);
236 }
237 
238 /**
239  * devm_reset_control_get_exclusive - resource managed
240  *                                    reset_control_get_exclusive()
241  * @dev: device to be reset by the controller
242  * @id: reset line name
243  *
244  * Managed reset_control_get_exclusive(). For reset controllers returned
245  * from this function, reset_control_put() is called automatically on driver
246  * detach.
247  *
248  * See reset_control_get_exclusive() for more information.
249  */
250 static inline struct reset_control *
251 __must_check devm_reset_control_get_exclusive(struct device *dev,
252 					      const char *id)
253 {
254 #ifndef CONFIG_RESET_CONTROLLER
255 	WARN_ON(1);
256 #endif
257 	return __devm_reset_control_get(dev, id, 0, false, false);
258 }
259 
260 /**
261  * devm_reset_control_get_shared - resource managed reset_control_get_shared()
262  * @dev: device to be reset by the controller
263  * @id: reset line name
264  *
265  * Managed reset_control_get_shared(). For reset controllers returned from
266  * this function, reset_control_put() is called automatically on driver detach.
267  * See reset_control_get_shared() for more information.
268  */
269 static inline struct reset_control *devm_reset_control_get_shared(
270 					struct device *dev, const char *id)
271 {
272 	return __devm_reset_control_get(dev, id, 0, true, false);
273 }
274 
275 static inline struct reset_control *devm_reset_control_get_optional_exclusive(
276 					struct device *dev, const char *id)
277 {
278 	return __devm_reset_control_get(dev, id, 0, false, true);
279 }
280 
281 static inline struct reset_control *devm_reset_control_get_optional_shared(
282 					struct device *dev, const char *id)
283 {
284 	return __devm_reset_control_get(dev, id, 0, true, true);
285 }
286 
287 /**
288  * devm_reset_control_get_exclusive_by_index - resource managed
289  *                                             reset_control_get_exclusive()
290  * @dev: device to be reset by the controller
291  * @index: index of the reset controller
292  *
293  * Managed reset_control_get_exclusive(). For reset controllers returned from
294  * this function, reset_control_put() is called automatically on driver
295  * detach.
296  *
297  * See reset_control_get_exclusive() for more information.
298  */
299 static inline struct reset_control *
300 devm_reset_control_get_exclusive_by_index(struct device *dev, int index)
301 {
302 	return __devm_reset_control_get(dev, NULL, index, false, false);
303 }
304 
305 /**
306  * devm_reset_control_get_shared_by_index - resource managed
307  * reset_control_get_shared
308  * @dev: device to be reset by the controller
309  * @index: index of the reset controller
310  *
311  * Managed reset_control_get_shared(). For reset controllers returned from
312  * this function, reset_control_put() is called automatically on driver detach.
313  * See reset_control_get_shared() for more information.
314  */
315 static inline struct reset_control *
316 devm_reset_control_get_shared_by_index(struct device *dev, int index)
317 {
318 	return __devm_reset_control_get(dev, NULL, index, true, false);
319 }
320 
321 /*
322  * TEMPORARY calls to use during transition:
323  *
324  *   of_reset_control_get() => of_reset_control_get_exclusive()
325  *
326  * These inline function calls will be removed once all consumers
327  * have been moved over to the new explicit API.
328  */
329 static inline struct reset_control *reset_control_get(
330 				struct device *dev, const char *id)
331 {
332 	return reset_control_get_exclusive(dev, id);
333 }
334 
335 static inline struct reset_control *reset_control_get_optional(
336 					struct device *dev, const char *id)
337 {
338 	return reset_control_get_optional_exclusive(dev, id);
339 }
340 
341 static inline struct reset_control *of_reset_control_get(
342 				struct device_node *node, const char *id)
343 {
344 	return of_reset_control_get_exclusive(node, id);
345 }
346 
347 static inline struct reset_control *of_reset_control_get_by_index(
348 				struct device_node *node, int index)
349 {
350 	return of_reset_control_get_exclusive_by_index(node, index);
351 }
352 
353 static inline struct reset_control *devm_reset_control_get(
354 				struct device *dev, const char *id)
355 {
356 	return devm_reset_control_get_exclusive(dev, id);
357 }
358 
359 static inline struct reset_control *devm_reset_control_get_optional(
360 				struct device *dev, const char *id)
361 {
362 	return devm_reset_control_get_optional_exclusive(dev, id);
363 
364 }
365 
366 static inline struct reset_control *devm_reset_control_get_by_index(
367 				struct device *dev, int index)
368 {
369 	return devm_reset_control_get_exclusive_by_index(dev, index);
370 }
371 #endif
372