xref: /linux-6.15/include/linux/reset.h (revision c84b0326)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_RESET_H_
3 #define _LINUX_RESET_H_
4 
5 #include <linux/types.h>
6 
7 struct device;
8 struct device_node;
9 struct reset_control;
10 
11 #ifdef CONFIG_RESET_CONTROLLER
12 
13 int reset_control_reset(struct reset_control *rstc);
14 int reset_control_assert(struct reset_control *rstc);
15 int reset_control_deassert(struct reset_control *rstc);
16 int reset_control_status(struct reset_control *rstc);
17 int reset_control_acquire(struct reset_control *rstc);
18 void reset_control_release(struct reset_control *rstc);
19 
20 struct reset_control *__of_reset_control_get(struct device_node *node,
21 				     const char *id, int index, bool shared,
22 				     bool optional, bool acquired);
23 struct reset_control *__reset_control_get(struct device *dev, const char *id,
24 					  int index, bool shared,
25 					  bool optional, bool acquired);
26 void reset_control_put(struct reset_control *rstc);
27 int __device_reset(struct device *dev, bool optional);
28 struct reset_control *__devm_reset_control_get(struct device *dev,
29 				     const char *id, int index, bool shared,
30 				     bool optional, bool acquired);
31 
32 struct reset_control *devm_reset_control_array_get(struct device *dev,
33 						   bool shared, bool optional);
34 struct reset_control *of_reset_control_array_get(struct device_node *np,
35 						 bool shared, bool optional);
36 
37 int reset_control_get_count(struct device *dev);
38 
39 #else
40 
41 static inline int reset_control_reset(struct reset_control *rstc)
42 {
43 	return 0;
44 }
45 
46 static inline int reset_control_assert(struct reset_control *rstc)
47 {
48 	return 0;
49 }
50 
51 static inline int reset_control_deassert(struct reset_control *rstc)
52 {
53 	return 0;
54 }
55 
56 static inline int reset_control_status(struct reset_control *rstc)
57 {
58 	return 0;
59 }
60 
61 static inline int reset_control_acquire(struct reset_control *rstc)
62 {
63 	return 0;
64 }
65 
66 static inline void reset_control_release(struct reset_control *rstc)
67 {
68 }
69 
70 static inline void reset_control_put(struct reset_control *rstc)
71 {
72 }
73 
74 static inline int __device_reset(struct device *dev, bool optional)
75 {
76 	return optional ? 0 : -ENOTSUPP;
77 }
78 
79 static inline struct reset_control *__of_reset_control_get(
80 					struct device_node *node,
81 					const char *id, int index, bool shared,
82 					bool optional, bool acquired)
83 {
84 	return optional ? NULL : ERR_PTR(-ENOTSUPP);
85 }
86 
87 static inline struct reset_control *__reset_control_get(
88 					struct device *dev, const char *id,
89 					int index, bool shared, bool optional,
90 					bool acquired)
91 {
92 	return optional ? NULL : ERR_PTR(-ENOTSUPP);
93 }
94 
95 static inline struct reset_control *__devm_reset_control_get(
96 					struct device *dev, const char *id,
97 					int index, bool shared, bool optional,
98 					bool acquired)
99 {
100 	return optional ? NULL : ERR_PTR(-ENOTSUPP);
101 }
102 
103 static inline struct reset_control *
104 devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
105 {
106 	return optional ? NULL : ERR_PTR(-ENOTSUPP);
107 }
108 
109 static inline struct reset_control *
110 of_reset_control_array_get(struct device_node *np, bool shared, bool optional)
111 {
112 	return optional ? NULL : ERR_PTR(-ENOTSUPP);
113 }
114 
115 static inline int reset_control_get_count(struct device *dev)
116 {
117 	return -ENOENT;
118 }
119 
120 #endif /* CONFIG_RESET_CONTROLLER */
121 
122 static inline int __must_check device_reset(struct device *dev)
123 {
124 	return __device_reset(dev, false);
125 }
126 
127 static inline int device_reset_optional(struct device *dev)
128 {
129 	return __device_reset(dev, true);
130 }
131 
132 /**
133  * reset_control_get_exclusive - Lookup and obtain an exclusive reference
134  *                               to a reset controller.
135  * @dev: device to be reset by the controller
136  * @id: reset line name
137  *
138  * Returns a struct reset_control or IS_ERR() condition containing errno.
139  * If this function is called more than once for the same reset_control it will
140  * return -EBUSY.
141  *
142  * See reset_control_get_shared for details on shared references to
143  * reset-controls.
144  *
145  * Use of id names is optional.
146  */
147 static inline struct reset_control *
148 __must_check reset_control_get_exclusive(struct device *dev, const char *id)
149 {
150 	return __reset_control_get(dev, id, 0, false, false, true);
151 }
152 
153 /**
154  * reset_control_get_exclusive_released - Lookup and obtain a temoprarily
155  *                                        exclusive reference to a reset
156  *                                        controller.
157  * @dev: device to be reset by the controller
158  * @id: reset line name
159  *
160  * Returns a struct reset_control or IS_ERR() condition containing errno.
161  * reset-controls returned by this function must be acquired via
162  * reset_control_acquire() before they can be used and should be released
163  * via reset_control_release() afterwards.
164  *
165  * Use of id names is optional.
166  */
167 static inline struct reset_control *
168 __must_check reset_control_get_exclusive_released(struct device *dev,
169 						  const char *id)
170 {
171 	return __reset_control_get(dev, id, 0, false, false, false);
172 }
173 
174 /**
175  * reset_control_get_shared - Lookup and obtain a shared reference to a
176  *                            reset controller.
177  * @dev: device to be reset by the controller
178  * @id: reset line name
179  *
180  * Returns a struct reset_control or IS_ERR() condition containing errno.
181  * This function is intended for use with reset-controls which are shared
182  * between hardware blocks.
183  *
184  * When a reset-control is shared, the behavior of reset_control_assert /
185  * deassert is changed, the reset-core will keep track of a deassert_count
186  * and only (re-)assert the reset after reset_control_assert has been called
187  * as many times as reset_control_deassert was called. Also see the remark
188  * about shared reset-controls in the reset_control_assert docs.
189  *
190  * Calling reset_control_assert without first calling reset_control_deassert
191  * is not allowed on a shared reset control. Calling reset_control_reset is
192  * also not allowed on a shared reset control.
193  *
194  * Use of id names is optional.
195  */
196 static inline struct reset_control *reset_control_get_shared(
197 					struct device *dev, const char *id)
198 {
199 	return __reset_control_get(dev, id, 0, true, false, false);
200 }
201 
202 static inline struct reset_control *reset_control_get_optional_exclusive(
203 					struct device *dev, const char *id)
204 {
205 	return __reset_control_get(dev, id, 0, false, true, true);
206 }
207 
208 static inline struct reset_control *reset_control_get_optional_shared(
209 					struct device *dev, const char *id)
210 {
211 	return __reset_control_get(dev, id, 0, true, true, false);
212 }
213 
214 /**
215  * of_reset_control_get_exclusive - Lookup and obtain an exclusive reference
216  *                                  to a reset controller.
217  * @node: device to be reset by the controller
218  * @id: reset line name
219  *
220  * Returns a struct reset_control or IS_ERR() condition containing errno.
221  *
222  * Use of id names is optional.
223  */
224 static inline struct reset_control *of_reset_control_get_exclusive(
225 				struct device_node *node, const char *id)
226 {
227 	return __of_reset_control_get(node, id, 0, false, false, true);
228 }
229 
230 /**
231  * of_reset_control_get_shared - Lookup and obtain a shared reference
232  *                               to a reset controller.
233  * @node: device to be reset by the controller
234  * @id: reset line name
235  *
236  * When a reset-control is shared, the behavior of reset_control_assert /
237  * deassert is changed, the reset-core will keep track of a deassert_count
238  * and only (re-)assert the reset after reset_control_assert has been called
239  * as many times as reset_control_deassert was called. Also see the remark
240  * about shared reset-controls in the reset_control_assert docs.
241  *
242  * Calling reset_control_assert without first calling reset_control_deassert
243  * is not allowed on a shared reset control. Calling reset_control_reset is
244  * also not allowed on a shared reset control.
245  * Returns a struct reset_control or IS_ERR() condition containing errno.
246  *
247  * Use of id names is optional.
248  */
249 static inline struct reset_control *of_reset_control_get_shared(
250 				struct device_node *node, const char *id)
251 {
252 	return __of_reset_control_get(node, id, 0, true, false, false);
253 }
254 
255 /**
256  * of_reset_control_get_exclusive_by_index - Lookup and obtain an exclusive
257  *                                           reference to a reset controller
258  *                                           by index.
259  * @node: device to be reset by the controller
260  * @index: index of the reset controller
261  *
262  * This is to be used to perform a list of resets for a device or power domain
263  * in whatever order. Returns a struct reset_control or IS_ERR() condition
264  * containing errno.
265  */
266 static inline struct reset_control *of_reset_control_get_exclusive_by_index(
267 					struct device_node *node, int index)
268 {
269 	return __of_reset_control_get(node, NULL, index, false, false, true);
270 }
271 
272 /**
273  * of_reset_control_get_shared_by_index - Lookup and obtain a shared
274  *                                        reference to a reset controller
275  *                                        by index.
276  * @node: device to be reset by the controller
277  * @index: index of the reset controller
278  *
279  * When a reset-control is shared, the behavior of reset_control_assert /
280  * deassert is changed, the reset-core will keep track of a deassert_count
281  * and only (re-)assert the reset after reset_control_assert has been called
282  * as many times as reset_control_deassert was called. Also see the remark
283  * about shared reset-controls in the reset_control_assert docs.
284  *
285  * Calling reset_control_assert without first calling reset_control_deassert
286  * is not allowed on a shared reset control. Calling reset_control_reset is
287  * also not allowed on a shared reset control.
288  * Returns a struct reset_control or IS_ERR() condition containing errno.
289  *
290  * This is to be used to perform a list of resets for a device or power domain
291  * in whatever order. Returns a struct reset_control or IS_ERR() condition
292  * containing errno.
293  */
294 static inline struct reset_control *of_reset_control_get_shared_by_index(
295 					struct device_node *node, int index)
296 {
297 	return __of_reset_control_get(node, NULL, index, true, false, false);
298 }
299 
300 /**
301  * devm_reset_control_get_exclusive - resource managed
302  *                                    reset_control_get_exclusive()
303  * @dev: device to be reset by the controller
304  * @id: reset line name
305  *
306  * Managed reset_control_get_exclusive(). For reset controllers returned
307  * from this function, reset_control_put() is called automatically on driver
308  * detach.
309  *
310  * See reset_control_get_exclusive() for more information.
311  */
312 static inline struct reset_control *
313 __must_check devm_reset_control_get_exclusive(struct device *dev,
314 					      const char *id)
315 {
316 	return __devm_reset_control_get(dev, id, 0, false, false, true);
317 }
318 
319 /**
320  * devm_reset_control_get_exclusive_released - resource managed
321  *                                             reset_control_get_exclusive_released()
322  * @dev: device to be reset by the controller
323  * @id: reset line name
324  *
325  * Managed reset_control_get_exclusive_released(). For reset controllers
326  * returned from this function, reset_control_put() is called automatically on
327  * driver detach.
328  *
329  * See reset_control_get_exclusive_released() for more information.
330  */
331 static inline struct reset_control *
332 __must_check devm_reset_control_get_exclusive_released(struct device *dev,
333 						       const char *id)
334 {
335 	return __devm_reset_control_get(dev, id, 0, false, false, false);
336 }
337 
338 /**
339  * devm_reset_control_get_shared - resource managed reset_control_get_shared()
340  * @dev: device to be reset by the controller
341  * @id: reset line name
342  *
343  * Managed reset_control_get_shared(). For reset controllers returned from
344  * this function, reset_control_put() is called automatically on driver detach.
345  * See reset_control_get_shared() for more information.
346  */
347 static inline struct reset_control *devm_reset_control_get_shared(
348 					struct device *dev, const char *id)
349 {
350 	return __devm_reset_control_get(dev, id, 0, true, false, false);
351 }
352 
353 static inline struct reset_control *devm_reset_control_get_optional_exclusive(
354 					struct device *dev, const char *id)
355 {
356 	return __devm_reset_control_get(dev, id, 0, false, true, true);
357 }
358 
359 static inline struct reset_control *devm_reset_control_get_optional_shared(
360 					struct device *dev, const char *id)
361 {
362 	return __devm_reset_control_get(dev, id, 0, true, true, false);
363 }
364 
365 /**
366  * devm_reset_control_get_exclusive_by_index - resource managed
367  *                                             reset_control_get_exclusive()
368  * @dev: device to be reset by the controller
369  * @index: index of the reset controller
370  *
371  * Managed reset_control_get_exclusive(). For reset controllers returned from
372  * this function, reset_control_put() is called automatically on driver
373  * detach.
374  *
375  * See reset_control_get_exclusive() for more information.
376  */
377 static inline struct reset_control *
378 devm_reset_control_get_exclusive_by_index(struct device *dev, int index)
379 {
380 	return __devm_reset_control_get(dev, NULL, index, false, false, true);
381 }
382 
383 /**
384  * devm_reset_control_get_shared_by_index - resource managed
385  *                                          reset_control_get_shared
386  * @dev: device to be reset by the controller
387  * @index: index of the reset controller
388  *
389  * Managed reset_control_get_shared(). For reset controllers returned from
390  * this function, reset_control_put() is called automatically on driver detach.
391  * See reset_control_get_shared() for more information.
392  */
393 static inline struct reset_control *
394 devm_reset_control_get_shared_by_index(struct device *dev, int index)
395 {
396 	return __devm_reset_control_get(dev, NULL, index, true, false, false);
397 }
398 
399 /*
400  * TEMPORARY calls to use during transition:
401  *
402  *   of_reset_control_get() => of_reset_control_get_exclusive()
403  *
404  * These inline function calls will be removed once all consumers
405  * have been moved over to the new explicit API.
406  */
407 static inline struct reset_control *of_reset_control_get(
408 				struct device_node *node, const char *id)
409 {
410 	return of_reset_control_get_exclusive(node, id);
411 }
412 
413 static inline struct reset_control *of_reset_control_get_by_index(
414 				struct device_node *node, int index)
415 {
416 	return of_reset_control_get_exclusive_by_index(node, index);
417 }
418 
419 static inline struct reset_control *devm_reset_control_get(
420 				struct device *dev, const char *id)
421 {
422 	return devm_reset_control_get_exclusive(dev, id);
423 }
424 
425 static inline struct reset_control *devm_reset_control_get_optional(
426 				struct device *dev, const char *id)
427 {
428 	return devm_reset_control_get_optional_exclusive(dev, id);
429 
430 }
431 
432 static inline struct reset_control *devm_reset_control_get_by_index(
433 				struct device *dev, int index)
434 {
435 	return devm_reset_control_get_exclusive_by_index(dev, index);
436 }
437 
438 /*
439  * APIs to manage a list of reset controllers
440  */
441 static inline struct reset_control *
442 devm_reset_control_array_get_exclusive(struct device *dev)
443 {
444 	return devm_reset_control_array_get(dev, false, false);
445 }
446 
447 static inline struct reset_control *
448 devm_reset_control_array_get_shared(struct device *dev)
449 {
450 	return devm_reset_control_array_get(dev, true, false);
451 }
452 
453 static inline struct reset_control *
454 devm_reset_control_array_get_optional_exclusive(struct device *dev)
455 {
456 	return devm_reset_control_array_get(dev, false, true);
457 }
458 
459 static inline struct reset_control *
460 devm_reset_control_array_get_optional_shared(struct device *dev)
461 {
462 	return devm_reset_control_array_get(dev, true, true);
463 }
464 
465 static inline struct reset_control *
466 of_reset_control_array_get_exclusive(struct device_node *node)
467 {
468 	return of_reset_control_array_get(node, false, false);
469 }
470 
471 static inline struct reset_control *
472 of_reset_control_array_get_shared(struct device_node *node)
473 {
474 	return of_reset_control_array_get(node, true, false);
475 }
476 
477 static inline struct reset_control *
478 of_reset_control_array_get_optional_exclusive(struct device_node *node)
479 {
480 	return of_reset_control_array_get(node, false, true);
481 }
482 
483 static inline struct reset_control *
484 of_reset_control_array_get_optional_shared(struct device_node *node)
485 {
486 	return of_reset_control_array_get(node, true, true);
487 }
488 #endif
489