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