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