xref: /linux/drivers/reset/core.c (revision c76350e7add86344beae4cd69fffdf63284a4bf5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Reset Controller framework
4  *
5  * Copyright 2013 Philipp Zabel, Pengutronix
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/atomic.h>
10 #include <linux/auxiliary_bus.h>
11 #include <linux/cleanup.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/fwnode.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/gpio/machine.h>
18 #include <linux/gpio/property.h>
19 #include <linux/idr.h>
20 #include <linux/kernel.h>
21 #include <linux/kref.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/property.h>
25 #include <linux/reset.h>
26 #include <linux/reset-controller.h>
27 #include <linux/slab.h>
28 #include <linux/srcu.h>
29 
30 static DEFINE_MUTEX(reset_list_mutex);
31 static LIST_HEAD(reset_controller_list);
32 
33 /* Protects reset_gpio_lookup_list */
34 static DEFINE_MUTEX(reset_gpio_lookup_mutex);
35 static LIST_HEAD(reset_gpio_lookup_list);
36 static DEFINE_IDA(reset_gpio_ida);
37 
38 /**
39  * struct reset_control - a reset control
40  * @rcdev: a pointer to the reset controller device
41  *         this reset control belongs to
42  * @srcu: protects the rcdev pointer from removal during consumer access
43  * @list: list entry for the rcdev's reset controller list
44  * @id: ID of the reset controller in the reset
45  *      controller device
46  * @refcnt: Number of gets of this reset_control
47  * @acquired: Only one reset_control may be acquired for a given rcdev and id.
48  * @shared: Is this a shared (1), or an exclusive (0) reset_control?
49  * @array: Is this an array of reset controls (1)?
50  * @deassert_count: Number of times this reset line has been deasserted
51  * @triggered_count: Number of times this reset line has been reset. Currently
52  *                   only used for shared resets, which means that the value
53  *                   will be either 0 or 1.
54  * @lock: serializes the internals of reset_control_acquire()
55  */
56 struct reset_control {
57 	struct reset_controller_dev __rcu *rcdev;
58 	struct srcu_struct srcu;
59 	struct list_head list;
60 	unsigned int id;
61 	struct kref refcnt;
62 	bool acquired;
63 	bool shared;
64 	bool array;
65 	atomic_t deassert_count;
66 	atomic_t triggered_count;
67 	struct mutex lock;
68 };
69 
70 /**
71  * struct reset_control_array - an array of reset controls
72  * @base: reset control for compatibility with reset control API functions
73  * @num_rstcs: number of reset controls
74  * @rstc: array of reset controls
75  */
76 struct reset_control_array {
77 	struct reset_control base;
78 	unsigned int num_rstcs;
79 	struct reset_control *rstc[] __counted_by(num_rstcs);
80 };
81 
82 /**
83  * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices
84  * @ref_args: Reference to the reset controller with all the args like GPIO number
85  * @swnode: Software node containing the reference to the GPIO provider
86  * @list: list entry for the reset_gpio_lookup_list
87  * @adev: Auxiliary device representing the reset controller
88  */
89 struct reset_gpio_lookup {
90 	struct fwnode_reference_args ref_args;
91 	struct fwnode_handle *swnode;
92 	struct list_head list;
93 	struct auxiliary_device adev;
94 };
95 
96 static const char *rcdev_name(struct reset_controller_dev *rcdev)
97 {
98 	if (rcdev->dev)
99 		return dev_name(rcdev->dev);
100 
101 	if (rcdev->fwnode)
102 		return fwnode_get_name(rcdev->fwnode);
103 
104 	return NULL;
105 }
106 
107 /**
108  * fwnode_reset_simple_xlate - translate reset_spec to the reset line number
109  * @rcdev: a pointer to the reset controller device
110  * @reset_spec: reset line specifier as found in firmware
111  *
112  * This static translation function is used by default if neither fwnode_xlate
113  * not of_xlate in :c:type:`reset_controller_dev` is not set. It is useful for
114  * all reset controllers with 1:1 mapping, where reset lines can be indexed by
115  * number without gaps.
116  */
117 static int fwnode_reset_simple_xlate(struct reset_controller_dev *rcdev,
118 				     const struct fwnode_reference_args *reset_spec)
119 {
120 	if (reset_spec->args[0] >= rcdev->nr_resets)
121 		return -EINVAL;
122 
123 	return reset_spec->args[0];
124 }
125 
126 /**
127  * reset_controller_register - register a reset controller device
128  * @rcdev: a pointer to the initialized reset controller device
129  */
130 int reset_controller_register(struct reset_controller_dev *rcdev)
131 {
132 	if ((rcdev->of_node && rcdev->fwnode) || (rcdev->of_xlate && rcdev->fwnode_xlate))
133 		return -EINVAL;
134 
135 	if (!rcdev->of_node && !rcdev->fwnode) {
136 		rcdev->fwnode = dev_fwnode(rcdev->dev);
137 		if (!rcdev->fwnode)
138 			return -EINVAL;
139 	}
140 
141 	if (rcdev->of_node) {
142 		rcdev->fwnode = of_fwnode_handle(rcdev->of_node);
143 		rcdev->fwnode_reset_n_cells = rcdev->of_reset_n_cells;
144 	}
145 
146 	if (rcdev->fwnode && !rcdev->fwnode_xlate) {
147 		rcdev->fwnode_reset_n_cells = 1;
148 		rcdev->fwnode_xlate = fwnode_reset_simple_xlate;
149 	}
150 
151 	INIT_LIST_HEAD(&rcdev->reset_control_head);
152 	mutex_init(&rcdev->lock);
153 
154 	guard(mutex)(&reset_list_mutex);
155 
156 	list_add(&rcdev->list, &reset_controller_list);
157 
158 	return 0;
159 }
160 EXPORT_SYMBOL_GPL(reset_controller_register);
161 
162 static void reset_controller_remove(struct reset_controller_dev *rcdev,
163 				    struct reset_control *rstc)
164 {
165 	lockdep_assert_held(&rcdev->lock);
166 
167 	list_del(&rstc->list);
168 	module_put(rcdev->owner);
169 	put_device(rcdev->dev);
170 }
171 
172 /**
173  * reset_controller_unregister - unregister a reset controller device
174  * @rcdev: a pointer to the reset controller device
175  */
176 void reset_controller_unregister(struct reset_controller_dev *rcdev)
177 {
178 	struct reset_control *rstc, *pos;
179 
180 	scoped_guard(mutex, &reset_list_mutex)
181 		list_del(&rcdev->list);
182 
183 	scoped_guard(mutex, &rcdev->lock) {
184 		/*
185 		 * Numb but don't free the remaining reset control handles that are
186 		 * still held by consumers.
187 		 */
188 		list_for_each_entry_safe(rstc, pos, &rcdev->reset_control_head, list) {
189 			rcu_assign_pointer(rstc->rcdev, NULL);
190 			synchronize_srcu(&rstc->srcu);
191 			reset_controller_remove(rcdev, rstc);
192 		}
193 	}
194 
195 	mutex_destroy(&rcdev->lock);
196 }
197 EXPORT_SYMBOL_GPL(reset_controller_unregister);
198 
199 static void devm_reset_controller_release(struct device *dev, void *res)
200 {
201 	reset_controller_unregister(*(struct reset_controller_dev **)res);
202 }
203 
204 /**
205  * devm_reset_controller_register - resource managed reset_controller_register()
206  * @dev: device that is registering this reset controller
207  * @rcdev: a pointer to the initialized reset controller device
208  *
209  * Managed reset_controller_register(). For reset controllers registered by
210  * this function, reset_controller_unregister() is automatically called on
211  * driver detach. See reset_controller_register() for more information.
212  */
213 int devm_reset_controller_register(struct device *dev,
214 				   struct reset_controller_dev *rcdev)
215 {
216 	struct reset_controller_dev **rcdevp;
217 	int ret;
218 
219 	rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
220 			      GFP_KERNEL);
221 	if (!rcdevp)
222 		return -ENOMEM;
223 
224 	ret = reset_controller_register(rcdev);
225 	if (ret) {
226 		devres_free(rcdevp);
227 		return ret;
228 	}
229 
230 	*rcdevp = rcdev;
231 	devres_add(dev, rcdevp);
232 
233 	return ret;
234 }
235 EXPORT_SYMBOL_GPL(devm_reset_controller_register);
236 
237 static inline struct reset_control_array *
238 rstc_to_array(struct reset_control *rstc) {
239 	return container_of(rstc, struct reset_control_array, base);
240 }
241 
242 static int reset_control_array_reset(struct reset_control_array *resets)
243 {
244 	int ret, i;
245 
246 	for (i = 0; i < resets->num_rstcs; i++) {
247 		ret = reset_control_reset(resets->rstc[i]);
248 		if (ret)
249 			return ret;
250 	}
251 
252 	return 0;
253 }
254 
255 static int reset_control_array_rearm(struct reset_control_array *resets)
256 {
257 	struct reset_control *rstc;
258 	int i;
259 
260 	for (i = 0; i < resets->num_rstcs; i++) {
261 		rstc = resets->rstc[i];
262 
263 		if (!rstc)
264 			continue;
265 
266 		if (WARN_ON(IS_ERR(rstc)))
267 			return -EINVAL;
268 
269 		if (rstc->shared) {
270 			if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
271 				return -EINVAL;
272 		} else {
273 			if (!rstc->acquired)
274 				return -EPERM;
275 		}
276 	}
277 
278 	for (i = 0; i < resets->num_rstcs; i++) {
279 		rstc = resets->rstc[i];
280 
281 		if (rstc && rstc->shared)
282 			WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0);
283 	}
284 
285 	return 0;
286 }
287 
288 static int reset_control_array_assert(struct reset_control_array *resets)
289 {
290 	int ret, i;
291 
292 	for (i = 0; i < resets->num_rstcs; i++) {
293 		ret = reset_control_assert(resets->rstc[i]);
294 		if (ret)
295 			goto err;
296 	}
297 
298 	return 0;
299 
300 err:
301 	while (i--)
302 		reset_control_deassert(resets->rstc[i]);
303 	return ret;
304 }
305 
306 static int reset_control_array_deassert(struct reset_control_array *resets)
307 {
308 	int ret, i;
309 
310 	for (i = 0; i < resets->num_rstcs; i++) {
311 		ret = reset_control_deassert(resets->rstc[i]);
312 		if (ret)
313 			goto err;
314 	}
315 
316 	return 0;
317 
318 err:
319 	while (i--)
320 		reset_control_assert(resets->rstc[i]);
321 	return ret;
322 }
323 
324 static int reset_control_array_acquire(struct reset_control_array *resets)
325 {
326 	unsigned int i;
327 	int err;
328 
329 	for (i = 0; i < resets->num_rstcs; i++) {
330 		err = reset_control_acquire(resets->rstc[i]);
331 		if (err < 0)
332 			goto release;
333 	}
334 
335 	return 0;
336 
337 release:
338 	while (i--)
339 		reset_control_release(resets->rstc[i]);
340 
341 	return err;
342 }
343 
344 static void reset_control_array_release(struct reset_control_array *resets)
345 {
346 	unsigned int i;
347 
348 	for (i = 0; i < resets->num_rstcs; i++)
349 		reset_control_release(resets->rstc[i]);
350 }
351 
352 static inline bool reset_control_is_array(struct reset_control *rstc)
353 {
354 	return rstc->array;
355 }
356 
357 /**
358  * reset_control_reset - reset the controlled device
359  * @rstc: reset controller
360  *
361  * On a shared reset line the actual reset pulse is only triggered once for the
362  * lifetime of the reset_control instance: for all but the first caller this is
363  * a no-op.
364  * Consumers must not use reset_control_(de)assert on shared reset lines when
365  * reset_control_reset has been used.
366  *
367  * If rstc is NULL it is an optional reset and the function will just
368  * return 0.
369  */
370 int reset_control_reset(struct reset_control *rstc)
371 {
372 	struct reset_controller_dev *rcdev;
373 	int ret;
374 
375 	if (!rstc)
376 		return 0;
377 
378 	if (WARN_ON(IS_ERR(rstc)))
379 		return -EINVAL;
380 
381 	if (reset_control_is_array(rstc))
382 		return reset_control_array_reset(rstc_to_array(rstc));
383 
384 	guard(srcu)(&rstc->srcu);
385 
386 	rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu);
387 	if (!rcdev)
388 		return -ENODEV;
389 
390 	if (!rcdev->ops->reset)
391 		return -ENOTSUPP;
392 
393 	if (rstc->shared) {
394 		if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
395 			return -EINVAL;
396 
397 		if (atomic_inc_return(&rstc->triggered_count) != 1)
398 			return 0;
399 	} else {
400 		if (!rstc->acquired)
401 			return -EPERM;
402 	}
403 
404 	ret = rcdev->ops->reset(rcdev, rstc->id);
405 	if (rstc->shared && ret)
406 		atomic_dec(&rstc->triggered_count);
407 
408 	return ret;
409 }
410 EXPORT_SYMBOL_GPL(reset_control_reset);
411 
412 /**
413  * reset_control_bulk_reset - reset the controlled devices in order
414  * @num_rstcs: number of entries in rstcs array
415  * @rstcs: array of struct reset_control_bulk_data with reset controls set
416  *
417  * Issue a reset on all provided reset controls, in order.
418  *
419  * See also: reset_control_reset()
420  */
421 int reset_control_bulk_reset(int num_rstcs,
422 			     struct reset_control_bulk_data *rstcs)
423 {
424 	int ret, i;
425 
426 	for (i = 0; i < num_rstcs; i++) {
427 		ret = reset_control_reset(rstcs[i].rstc);
428 		if (ret)
429 			return ret;
430 	}
431 
432 	return 0;
433 }
434 EXPORT_SYMBOL_GPL(reset_control_bulk_reset);
435 
436 /**
437  * reset_control_rearm - allow shared reset line to be re-triggered"
438  * @rstc: reset controller
439  *
440  * On a shared reset line the actual reset pulse is only triggered once for the
441  * lifetime of the reset_control instance, except if this call is used.
442  *
443  * Calls to this function must be balanced with calls to reset_control_reset,
444  * a warning is thrown in case triggered_count ever dips below 0.
445  *
446  * Consumers must not use reset_control_(de)assert on shared reset lines when
447  * reset_control_reset or reset_control_rearm have been used.
448  *
449  * If rstc is NULL the function will just return 0.
450  */
451 int reset_control_rearm(struct reset_control *rstc)
452 {
453 	if (!rstc)
454 		return 0;
455 
456 	if (WARN_ON(IS_ERR(rstc)))
457 		return -EINVAL;
458 
459 	if (reset_control_is_array(rstc))
460 		return reset_control_array_rearm(rstc_to_array(rstc));
461 
462 	if (rstc->shared) {
463 		if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
464 			return -EINVAL;
465 
466 		WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0);
467 	} else {
468 		if (!rstc->acquired)
469 			return -EPERM;
470 	}
471 
472 	return 0;
473 }
474 EXPORT_SYMBOL_GPL(reset_control_rearm);
475 
476 /**
477  * reset_control_assert - asserts the reset line
478  * @rstc: reset controller
479  *
480  * Calling this on an exclusive reset controller guarantees that the reset
481  * will be asserted. When called on a shared reset controller the line may
482  * still be deasserted, as long as other users keep it so.
483  *
484  * For shared reset controls a driver cannot expect the hw's registers and
485  * internal state to be reset, but must be prepared for this to happen.
486  * Consumers must not use reset_control_reset on shared reset lines when
487  * reset_control_(de)assert has been used.
488  *
489  * If rstc is NULL it is an optional reset and the function will just
490  * return 0.
491  */
492 int reset_control_assert(struct reset_control *rstc)
493 {
494 	struct reset_controller_dev *rcdev;
495 
496 	if (!rstc)
497 		return 0;
498 
499 	if (WARN_ON(IS_ERR(rstc)))
500 		return -EINVAL;
501 
502 	if (reset_control_is_array(rstc))
503 		return reset_control_array_assert(rstc_to_array(rstc));
504 
505 	guard(srcu)(&rstc->srcu);
506 
507 	rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu);
508 	if (!rcdev)
509 		return -ENODEV;
510 
511 	if (rstc->shared) {
512 		if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
513 			return -EINVAL;
514 
515 		if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
516 			return -EINVAL;
517 
518 		if (atomic_dec_return(&rstc->deassert_count) != 0)
519 			return 0;
520 
521 		/*
522 		 * Shared reset controls allow the reset line to be in any state
523 		 * after this call, so doing nothing is a valid option.
524 		 */
525 		if (!rcdev->ops->assert)
526 			return 0;
527 	} else {
528 		/*
529 		 * If the reset controller does not implement .assert(), there
530 		 * is no way to guarantee that the reset line is asserted after
531 		 * this call.
532 		 */
533 		if (!rcdev->ops->assert)
534 			return -ENOTSUPP;
535 
536 		if (!rstc->acquired) {
537 			WARN(1, "reset %s (ID: %u) is not acquired\n",
538 			     rcdev_name(rcdev), rstc->id);
539 			return -EPERM;
540 		}
541 	}
542 
543 	return rcdev->ops->assert(rcdev, rstc->id);
544 }
545 EXPORT_SYMBOL_GPL(reset_control_assert);
546 
547 /**
548  * reset_control_bulk_assert - asserts the reset lines in order
549  * @num_rstcs: number of entries in rstcs array
550  * @rstcs: array of struct reset_control_bulk_data with reset controls set
551  *
552  * Assert the reset lines for all provided reset controls, in order.
553  * If an assertion fails, already asserted resets are deasserted again.
554  *
555  * See also: reset_control_assert()
556  */
557 int reset_control_bulk_assert(int num_rstcs,
558 			      struct reset_control_bulk_data *rstcs)
559 {
560 	int ret, i;
561 
562 	for (i = 0; i < num_rstcs; i++) {
563 		ret = reset_control_assert(rstcs[i].rstc);
564 		if (ret)
565 			goto err;
566 	}
567 
568 	return 0;
569 
570 err:
571 	while (i--)
572 		reset_control_deassert(rstcs[i].rstc);
573 	return ret;
574 }
575 EXPORT_SYMBOL_GPL(reset_control_bulk_assert);
576 
577 /**
578  * reset_control_deassert - deasserts the reset line
579  * @rstc: reset controller
580  *
581  * After calling this function, the reset is guaranteed to be deasserted.
582  * Consumers must not use reset_control_reset on shared reset lines when
583  * reset_control_(de)assert has been used.
584  *
585  * If rstc is NULL it is an optional reset and the function will just
586  * return 0.
587  */
588 int reset_control_deassert(struct reset_control *rstc)
589 {
590 	struct reset_controller_dev *rcdev;
591 
592 	if (!rstc)
593 		return 0;
594 
595 	if (WARN_ON(IS_ERR(rstc)))
596 		return -EINVAL;
597 
598 	if (reset_control_is_array(rstc))
599 		return reset_control_array_deassert(rstc_to_array(rstc));
600 
601 	guard(srcu)(&rstc->srcu);
602 
603 	rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu);
604 	if (!rcdev)
605 		return -ENODEV;
606 
607 	if (rstc->shared) {
608 		if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
609 			return -EINVAL;
610 
611 		if (atomic_inc_return(&rstc->deassert_count) != 1)
612 			return 0;
613 	} else {
614 		if (!rstc->acquired) {
615 			WARN(1, "reset %s (ID: %u) is not acquired\n",
616 			     rcdev_name(rcdev), rstc->id);
617 			return -EPERM;
618 		}
619 	}
620 
621 	/*
622 	 * If the reset controller does not implement .deassert(), we assume
623 	 * that it handles self-deasserting reset lines via .reset(). In that
624 	 * case, the reset lines are deasserted by default. If that is not the
625 	 * case, the reset controller driver should implement .deassert() and
626 	 * return -ENOTSUPP.
627 	 */
628 	if (!rcdev->ops->deassert)
629 		return 0;
630 
631 	return rcdev->ops->deassert(rcdev, rstc->id);
632 }
633 EXPORT_SYMBOL_GPL(reset_control_deassert);
634 
635 /**
636  * reset_control_bulk_deassert - deasserts the reset lines in reverse order
637  * @num_rstcs: number of entries in rstcs array
638  * @rstcs: array of struct reset_control_bulk_data with reset controls set
639  *
640  * Deassert the reset lines for all provided reset controls, in reverse order.
641  * If a deassertion fails, already deasserted resets are asserted again.
642  *
643  * See also: reset_control_deassert()
644  */
645 int reset_control_bulk_deassert(int num_rstcs,
646 				struct reset_control_bulk_data *rstcs)
647 {
648 	int ret, i;
649 
650 	for (i = num_rstcs - 1; i >= 0; i--) {
651 		ret = reset_control_deassert(rstcs[i].rstc);
652 		if (ret)
653 			goto err;
654 	}
655 
656 	return 0;
657 
658 err:
659 	while (i < num_rstcs)
660 		reset_control_assert(rstcs[i++].rstc);
661 	return ret;
662 }
663 EXPORT_SYMBOL_GPL(reset_control_bulk_deassert);
664 
665 /**
666  * reset_control_status - returns a negative errno if not supported, a
667  * positive value if the reset line is asserted, or zero if the reset
668  * line is not asserted or if the desc is NULL (optional reset).
669  * @rstc: reset controller
670  */
671 int reset_control_status(struct reset_control *rstc)
672 {
673 	struct reset_controller_dev *rcdev;
674 
675 	if (!rstc)
676 		return 0;
677 
678 	if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
679 		return -EINVAL;
680 
681 	guard(srcu)(&rstc->srcu);
682 
683 	rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu);
684 	if (!rcdev)
685 		return -ENODEV;
686 
687 	if (rcdev->ops->status)
688 		return rcdev->ops->status(rcdev, rstc->id);
689 
690 	return -ENOTSUPP;
691 }
692 EXPORT_SYMBOL_GPL(reset_control_status);
693 
694 /**
695  * reset_control_acquire() - acquires a reset control for exclusive use
696  * @rstc: reset control
697  *
698  * This is used to explicitly acquire a reset control for exclusive use. Note
699  * that exclusive resets are requested as acquired by default. In order for a
700  * second consumer to be able to control the reset, the first consumer has to
701  * release it first. Typically the easiest way to achieve this is to call the
702  * reset_control_get_exclusive_released() to obtain an instance of the reset
703  * control. Such reset controls are not acquired by default.
704  *
705  * Consumers implementing shared access to an exclusive reset need to follow
706  * a specific protocol in order to work together. Before consumers can change
707  * a reset they must acquire exclusive access using reset_control_acquire().
708  * After they are done operating the reset, they must release exclusive access
709  * with a call to reset_control_release(). Consumers are not granted exclusive
710  * access to the reset as long as another consumer hasn't released a reset.
711  *
712  * See also: reset_control_release()
713  */
714 int reset_control_acquire(struct reset_control *rstc)
715 {
716 	struct reset_controller_dev *rcdev;
717 	struct reset_control *rc;
718 
719 	if (!rstc)
720 		return 0;
721 
722 	if (WARN_ON(IS_ERR(rstc)))
723 		return -EINVAL;
724 
725 	if (reset_control_is_array(rstc))
726 		return reset_control_array_acquire(rstc_to_array(rstc));
727 
728 	guard(mutex)(&rstc->lock);
729 
730 	if (rstc->acquired)
731 		return 0;
732 
733 	guard(srcu)(&rstc->srcu);
734 
735 	rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu);
736 	if (!rcdev)
737 		return -ENODEV;
738 
739 	scoped_guard(mutex, &rcdev->lock) {
740 		list_for_each_entry(rc, &rcdev->reset_control_head, list) {
741 			if (rstc != rc && rstc->id == rc->id) {
742 				if (rc->acquired)
743 					return -EBUSY;
744 			}
745 		}
746 	}
747 
748 	rstc->acquired = true;
749 
750 	return 0;
751 }
752 EXPORT_SYMBOL_GPL(reset_control_acquire);
753 
754 /**
755  * reset_control_bulk_acquire - acquires reset controls for exclusive use
756  * @num_rstcs: number of entries in rstcs array
757  * @rstcs: array of struct reset_control_bulk_data with reset controls set
758  *
759  * This is used to explicitly acquire reset controls requested with
760  * reset_control_bulk_get_exclusive_release() for temporary exclusive use.
761  *
762  * See also: reset_control_acquire(), reset_control_bulk_release()
763  */
764 int reset_control_bulk_acquire(int num_rstcs,
765 			       struct reset_control_bulk_data *rstcs)
766 {
767 	int ret, i;
768 
769 	for (i = 0; i < num_rstcs; i++) {
770 		ret = reset_control_acquire(rstcs[i].rstc);
771 		if (ret)
772 			goto err;
773 	}
774 
775 	return 0;
776 
777 err:
778 	while (i--)
779 		reset_control_release(rstcs[i].rstc);
780 	return ret;
781 }
782 EXPORT_SYMBOL_GPL(reset_control_bulk_acquire);
783 
784 /**
785  * reset_control_release() - releases exclusive access to a reset control
786  * @rstc: reset control
787  *
788  * Releases exclusive access right to a reset control previously obtained by a
789  * call to reset_control_acquire(). Until a consumer calls this function, no
790  * other consumers will be granted exclusive access.
791  *
792  * See also: reset_control_acquire()
793  */
794 void reset_control_release(struct reset_control *rstc)
795 {
796 	if (!rstc || WARN_ON(IS_ERR(rstc)))
797 		return;
798 
799 	if (reset_control_is_array(rstc))
800 		reset_control_array_release(rstc_to_array(rstc));
801 	else
802 		rstc->acquired = false;
803 }
804 EXPORT_SYMBOL_GPL(reset_control_release);
805 
806 /**
807  * reset_control_bulk_release() - releases exclusive access to reset controls
808  * @num_rstcs: number of entries in rstcs array
809  * @rstcs: array of struct reset_control_bulk_data with reset controls set
810  *
811  * Releases exclusive access right to reset controls previously obtained by a
812  * call to reset_control_bulk_acquire().
813  *
814  * See also: reset_control_release(), reset_control_bulk_acquire()
815  */
816 void reset_control_bulk_release(int num_rstcs,
817 				struct reset_control_bulk_data *rstcs)
818 {
819 	int i;
820 
821 	for (i = 0; i < num_rstcs; i++)
822 		reset_control_release(rstcs[i].rstc);
823 }
824 EXPORT_SYMBOL_GPL(reset_control_bulk_release);
825 
826 static struct reset_control *
827 __reset_control_get_internal(struct reset_controller_dev *rcdev,
828 			     unsigned int index, enum reset_control_flags flags)
829 {
830 	bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED;
831 	bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED;
832 	struct reset_control *rstc;
833 	int ret;
834 
835 	lockdep_assert_held(&rcdev->lock);
836 
837 	/* Expect callers to filter out OPTIONAL and DEASSERTED bits */
838 	if (WARN_ON(flags & ~(RESET_CONTROL_FLAGS_BIT_SHARED |
839 			      RESET_CONTROL_FLAGS_BIT_ACQUIRED)))
840 		return ERR_PTR(-EINVAL);
841 
842 	list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
843 		if (rstc->id == index) {
844 			/*
845 			 * Allow creating a secondary exclusive reset_control
846 			 * that is initially not acquired for an already
847 			 * controlled reset line.
848 			 */
849 			if (!rstc->shared && !shared && !acquired)
850 				break;
851 
852 			if (WARN_ON(!rstc->shared || !shared))
853 				return ERR_PTR(-EBUSY);
854 
855 			kref_get(&rstc->refcnt);
856 			return rstc;
857 		}
858 	}
859 
860 	rstc = kzalloc_obj(*rstc);
861 	if (!rstc)
862 		return ERR_PTR(-ENOMEM);
863 
864 	ret = init_srcu_struct(&rstc->srcu);
865 	if (ret) {
866 		kfree(rstc);
867 		return ERR_PTR(ret);
868 	}
869 
870 	if (!try_module_get(rcdev->owner)) {
871 		cleanup_srcu_struct(&rstc->srcu);
872 		kfree(rstc);
873 		return ERR_PTR(-ENODEV);
874 	}
875 
876 	rcu_assign_pointer(rstc->rcdev, rcdev);
877 	list_add(&rstc->list, &rcdev->reset_control_head);
878 	rstc->id = index;
879 	kref_init(&rstc->refcnt);
880 	mutex_init(&rstc->lock);
881 	rstc->acquired = acquired;
882 	rstc->shared = shared;
883 	get_device(rcdev->dev);
884 
885 	return rstc;
886 }
887 
888 static void __reset_control_release(struct kref *kref)
889 {
890 	struct reset_control *rstc = container_of(kref, struct reset_control,
891 						  refcnt);
892 	struct reset_controller_dev *rcdev;
893 
894 	lockdep_assert_held(&rstc->srcu);
895 
896 	rcdev = rcu_replace_pointer(rstc->rcdev, NULL, true);
897 	if (rcdev) {
898 		lockdep_assert_held(&rcdev->lock);
899 		reset_controller_remove(rcdev, rstc);
900 	}
901 
902 	mutex_destroy(&rstc->lock);
903 }
904 
905 static void reset_control_put_internal(struct reset_control *rstc)
906 {
907 	struct reset_controller_dev *rcdev;
908 	int ret = 0;
909 
910 	if (IS_ERR_OR_NULL(rstc))
911 		return;
912 
913 	scoped_guard(srcu, &rstc->srcu) {
914 		rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu);
915 		if (!rcdev)
916 			/* Already released. */
917 			return;
918 
919 		guard(mutex)(&rcdev->lock);
920 		ret = kref_put(&rstc->refcnt, __reset_control_release);
921 	}
922 
923 	if (ret) {
924 		synchronize_srcu(&rstc->srcu);
925 		cleanup_srcu_struct(&rstc->srcu);
926 		kfree(rstc);
927 	}
928 }
929 
930 static void reset_gpio_aux_device_release(struct device *dev)
931 {
932 	WARN(1, "reset-gpio device %s should never have been removed", dev_name(dev));
933 }
934 
935 static int reset_create_gpio_aux_device(struct reset_gpio_lookup *rgpio_dev,
936 					struct device *parent)
937 {
938 	struct auxiliary_device *adev = &rgpio_dev->adev;
939 	int ret, id;
940 
941 	id = ida_alloc(&reset_gpio_ida, GFP_KERNEL);
942 	if (id < 0)
943 		return -ENOMEM;
944 
945 	adev->id = id;
946 	adev->name = "gpio";
947 	adev->dev.parent = parent;
948 	adev->dev.platform_data = &rgpio_dev->ref_args;
949 	adev->dev.release = reset_gpio_aux_device_release;
950 	device_set_node(&adev->dev, rgpio_dev->swnode);
951 
952 	ret = auxiliary_device_init(adev);
953 	if (ret) {
954 		ida_free(&reset_gpio_ida, id);
955 		return ret;
956 	}
957 
958 	ret = __auxiliary_device_add(adev, "reset");
959 	if (ret) {
960 		auxiliary_device_uninit(adev);
961 		ida_free(&reset_gpio_ida, id);
962 		return ret;
963 	}
964 
965 	return 0;
966 }
967 
968 static void reset_gpio_add_devlink(struct fwnode_handle *fwnode,
969 				   struct reset_gpio_lookup *rgpio_dev)
970 {
971 	struct device *consumer;
972 
973 	/*
974 	 * We must use get_dev_from_fwnode() and not ref_find_device_by_node()
975 	 * because the latter only considers the platform bus while we want to
976 	 * get consumers of any kind that can be associated with firmware
977 	 * nodes: auxiliary, soundwire, etc.
978 	 */
979 	consumer = get_dev_from_fwnode(fwnode);
980 	if (consumer) {
981 		if (!device_link_add(consumer, &rgpio_dev->adev.dev,
982 				     DL_FLAG_AUTOREMOVE_CONSUMER))
983 			pr_warn("Failed to create a device link between reset-gpio and its consumer");
984 
985 		put_device(consumer);
986 	}
987 	/*
988 	 * else { }
989 	 *
990 	 * TODO: If ever there's a case where we need to support shared
991 	 * reset-gpios retrieved from a device node for which there's no
992 	 * device present yet, this is where we'd set up a notifier waiting
993 	 * for the device to appear in the system. This would be a lot of code
994 	 * that would go unused for now so let's cross that bridge when and if
995 	 * we get there.
996 	 */
997 }
998 
999 /* TODO: move it out into drivers/base/ */
1000 static bool fwnode_reference_args_equal(const struct fwnode_reference_args *left,
1001 					const struct fwnode_reference_args *right)
1002 {
1003 	return left->fwnode == right->fwnode && left->nargs == right->nargs &&
1004 	       !memcmp(left->args, right->args, sizeof(left->args[0]) * left->nargs);
1005 }
1006 
1007 /*
1008  * @np: OF-node associated with the consumer
1009  * @args: Reference to the GPIO provider with all the args like GPIO number
1010  */
1011 static int __reset_add_reset_gpio_device(struct fwnode_handle *fwnode,
1012 					 const struct fwnode_reference_args *args)
1013 {
1014 	struct property_entry properties[3] = { };
1015 	unsigned int offset, flags, lflags;
1016 	struct reset_gpio_lookup *rgpio_dev;
1017 	struct device *parent;
1018 	int ret, prop = 0;
1019 
1020 	/*
1021 	 * Currently only #gpio-cells=2 is supported with the meaning of:
1022 	 * args[0]: GPIO number
1023 	 * args[1]: GPIO flags
1024 	 * TODO: Handle other cases.
1025 	 */
1026 	if (args->nargs != 2)
1027 		return -ENOENT;
1028 
1029 	/*
1030 	 * Registering reset-gpio device might cause immediate
1031 	 * bind, resulting in its probe() registering new reset controller thus
1032 	 * taking reset_list_mutex lock via reset_controller_register().
1033 	 */
1034 	lockdep_assert_not_held(&reset_list_mutex);
1035 
1036 	offset = args->args[0];
1037 	flags = args->args[1];
1038 
1039 	/*
1040 	 * Later we map GPIO flags between OF and Linux, however not all
1041 	 * constants from include/dt-bindings/gpio/gpio.h and
1042 	 * include/linux/gpio/machine.h match each other.
1043 	 *
1044 	 * FIXME: Find a better way of translating OF flags to GPIO lookup
1045 	 * flags.
1046 	 */
1047 	if (flags > GPIO_ACTIVE_LOW) {
1048 		pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n",
1049 		       flags, offset);
1050 		return -EINVAL;
1051 	}
1052 
1053 	struct gpio_device *gdev __free(gpio_device_put) =
1054 			gpio_device_find_by_fwnode(args->fwnode);
1055 	if (!gdev)
1056 		return -EPROBE_DEFER;
1057 
1058 	guard(mutex)(&reset_gpio_lookup_mutex);
1059 
1060 	list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) {
1061 		if (fwnode_reference_args_equal(args, &rgpio_dev->ref_args)) {
1062 			/*
1063 			 * Already on the list, create the device link
1064 			 * and stop here.
1065 			 */
1066 			reset_gpio_add_devlink(fwnode, rgpio_dev);
1067 			return 0;
1068 		}
1069 	}
1070 
1071 	lflags = GPIO_PERSISTENT | (flags & GPIO_ACTIVE_LOW);
1072 	parent = gpio_device_to_device(gdev);
1073 	properties[prop++] = PROPERTY_ENTRY_STRING("compatible", "reset-gpio");
1074 	properties[prop++] = PROPERTY_ENTRY_GPIO("reset-gpios", parent->fwnode, offset, lflags);
1075 
1076 	/* Not freed on success, because it is persisent subsystem data. */
1077 	rgpio_dev = kzalloc_obj(*rgpio_dev);
1078 	if (!rgpio_dev)
1079 		return -ENOMEM;
1080 
1081 	rgpio_dev->ref_args = *args;
1082 	/*
1083 	 * We keep the fwnode_handle reference, but ref_args.fwnode is put at
1084 	 * the end of __fwnode_reset_control_get(), so get it one more time.
1085 	 * Hold reference as long as rgpio_dev memory is valid.
1086 	 */
1087 	fwnode_handle_get(rgpio_dev->ref_args.fwnode);
1088 
1089 	rgpio_dev->swnode = fwnode_create_software_node(properties, NULL);
1090 	if (IS_ERR(rgpio_dev->swnode)) {
1091 		ret = PTR_ERR(rgpio_dev->swnode);
1092 		goto err_put_fwnode;
1093 	}
1094 
1095 	ret = reset_create_gpio_aux_device(rgpio_dev, parent);
1096 	if (ret)
1097 		goto err_del_swnode;
1098 
1099 	reset_gpio_add_devlink(fwnode, rgpio_dev);
1100 	list_add(&rgpio_dev->list, &reset_gpio_lookup_list);
1101 
1102 	return 0;
1103 
1104 err_del_swnode:
1105 	fwnode_remove_software_node(rgpio_dev->swnode);
1106 err_put_fwnode:
1107 	fwnode_handle_put(rgpio_dev->ref_args.fwnode);
1108 	kfree(rgpio_dev);
1109 
1110 	return ret;
1111 }
1112 
1113 static struct reset_controller_dev *
1114 __reset_find_rcdev(const struct fwnode_reference_args *args, bool gpio_fallback)
1115 {
1116 	struct fwnode_reference_args *rc_args;
1117 	struct reset_controller_dev *rcdev;
1118 
1119 	lockdep_assert_held(&reset_list_mutex);
1120 
1121 	list_for_each_entry(rcdev, &reset_controller_list, list) {
1122 		if (gpio_fallback && rcdev->dev &&
1123 		    device_is_compatible(rcdev->dev, "reset-gpio")) {
1124 			rc_args = dev_get_platdata(rcdev->dev);
1125 
1126 			if (fwnode_reference_args_equal(args, rc_args))
1127 				return rcdev;
1128 		} else {
1129 			if (args->fwnode == rcdev->fwnode)
1130 				return rcdev;
1131 		}
1132 	}
1133 
1134 	return NULL;
1135 }
1136 
1137 struct reset_control *
1138 __fwnode_reset_control_get(struct fwnode_handle *fwnode, const char *id, int index,
1139 			   enum reset_control_flags flags)
1140 {
1141 	bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL;
1142 	bool gpio_fallback = false;
1143 	struct reset_control *rstc = ERR_PTR(-EINVAL);
1144 	struct reset_controller_dev *rcdev;
1145 	struct fwnode_reference_args args;
1146 	struct of_phandle_args of_args;
1147 	int rstc_id = -EINVAL;
1148 	int ret;
1149 
1150 	if (!fwnode)
1151 		return ERR_PTR(-EINVAL);
1152 
1153 	if (id) {
1154 		index = fwnode_property_match_string(fwnode, "reset-names", id);
1155 		if (index == -EILSEQ)
1156 			return ERR_PTR(index);
1157 		if (index < 0)
1158 			return optional ? NULL : ERR_PTR(-ENOENT);
1159 	}
1160 
1161 	ret = fwnode_property_get_reference_args(fwnode, "resets", "#reset-cells",
1162 						 0, index, &args);
1163 	if (ret == -EINVAL)
1164 		return ERR_PTR(ret);
1165 	if (ret) {
1166 		if (!IS_ENABLED(CONFIG_RESET_GPIO))
1167 			return optional ? NULL : ERR_PTR(ret);
1168 
1169 		/*
1170 		 * There can be only one reset-gpio for regular devices, so
1171 		 * don't bother with the "reset-gpios" phandle index.
1172 		 */
1173 		ret = fwnode_property_get_reference_args(fwnode, "reset-gpios",
1174 							 "#gpio-cells", 0, 0, &args);
1175 		if (ret)
1176 			return optional ? NULL : ERR_PTR(ret);
1177 
1178 		gpio_fallback = true;
1179 
1180 		ret = __reset_add_reset_gpio_device(fwnode, &args);
1181 		if (ret) {
1182 			fwnode_handle_put(args.fwnode);
1183 			return ERR_PTR(ret);
1184 		}
1185 	}
1186 
1187 	guard(mutex)(&reset_list_mutex);
1188 
1189 	rcdev = __reset_find_rcdev(&args, gpio_fallback);
1190 	if (!rcdev) {
1191 		rstc = ERR_PTR(-EPROBE_DEFER);
1192 		goto out_put;
1193 	}
1194 
1195 	if (WARN_ON(args.nargs != rcdev->fwnode_reset_n_cells)) {
1196 		rstc = ERR_PTR(-EINVAL);
1197 		goto out_put;
1198 	}
1199 
1200 	if (rcdev->of_xlate && is_of_node(fwnode)) {
1201 		ret = of_parse_phandle_with_args(to_of_node(fwnode),
1202 					 gpio_fallback ? "reset-gpios" : "resets",
1203 					 gpio_fallback ? "#gpio-cells" : "#reset-cells",
1204 					 gpio_fallback ? 0 : index,
1205 					 &of_args);
1206 		if (ret) {
1207 			rstc = ERR_PTR(ret);
1208 			goto out_put;
1209 		}
1210 
1211 		rstc_id = rcdev->of_xlate(rcdev, &of_args);
1212 		of_node_put(of_args.np);
1213 	} else if (rcdev->fwnode_xlate) {
1214 		rstc_id = rcdev->fwnode_xlate(rcdev, &args);
1215 	}
1216 	if (rstc_id < 0) {
1217 		rstc = ERR_PTR(rstc_id);
1218 			goto out_put;
1219 	}
1220 
1221 	flags &= ~RESET_CONTROL_FLAGS_BIT_OPTIONAL;
1222 
1223 	scoped_guard(mutex, &rcdev->lock)
1224 		rstc = __reset_control_get_internal(rcdev, rstc_id, flags);
1225 
1226 out_put:
1227 	fwnode_handle_put(args.fwnode);
1228 
1229 	return rstc;
1230 }
1231 EXPORT_SYMBOL_GPL(__fwnode_reset_control_get);
1232 
1233 struct reset_control *__reset_control_get(struct device *dev, const char *id,
1234 					  int index, enum reset_control_flags flags)
1235 {
1236 	bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED;
1237 	bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED;
1238 	bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL;
1239 	struct fwnode_handle *fwnode = dev_fwnode(dev);
1240 
1241 	if (WARN_ON(shared && acquired))
1242 		return ERR_PTR(-EINVAL);
1243 
1244 	if (fwnode)
1245 		return __fwnode_reset_control_get(fwnode, id, index, flags);
1246 
1247 	return optional ? NULL : ERR_PTR(-ENOENT);
1248 }
1249 EXPORT_SYMBOL_GPL(__reset_control_get);
1250 
1251 int __reset_control_bulk_get(struct device *dev, int num_rstcs,
1252 			     struct reset_control_bulk_data *rstcs,
1253 			     enum reset_control_flags flags)
1254 {
1255 	int ret, i;
1256 
1257 	for (i = 0; i < num_rstcs; i++) {
1258 		rstcs[i].rstc = __reset_control_get(dev, rstcs[i].id, 0, flags);
1259 		if (IS_ERR(rstcs[i].rstc)) {
1260 			ret = PTR_ERR(rstcs[i].rstc);
1261 			goto err;
1262 		}
1263 	}
1264 
1265 	return 0;
1266 
1267 err:
1268 	while (i--)
1269 		reset_control_put_internal(rstcs[i].rstc);
1270 
1271 	return ret;
1272 }
1273 EXPORT_SYMBOL_GPL(__reset_control_bulk_get);
1274 
1275 static void reset_control_array_put(struct reset_control_array *resets)
1276 {
1277 	int i;
1278 
1279 	for (i = 0; i < resets->num_rstcs; i++)
1280 		reset_control_put_internal(resets->rstc[i]);
1281 	kfree(resets);
1282 }
1283 
1284 /**
1285  * reset_control_put - free the reset controller
1286  * @rstc: reset controller
1287  */
1288 void reset_control_put(struct reset_control *rstc)
1289 {
1290 	if (IS_ERR_OR_NULL(rstc))
1291 		return;
1292 
1293 	if (reset_control_is_array(rstc)) {
1294 		reset_control_array_put(rstc_to_array(rstc));
1295 		return;
1296 	}
1297 
1298 	reset_control_put_internal(rstc);
1299 }
1300 EXPORT_SYMBOL_GPL(reset_control_put);
1301 
1302 /**
1303  * reset_control_bulk_put - free the reset controllers
1304  * @num_rstcs: number of entries in rstcs array
1305  * @rstcs: array of struct reset_control_bulk_data with reset controls set
1306  */
1307 void reset_control_bulk_put(int num_rstcs, struct reset_control_bulk_data *rstcs)
1308 {
1309 	while (num_rstcs--)
1310 		reset_control_put_internal(rstcs[num_rstcs].rstc);
1311 }
1312 EXPORT_SYMBOL_GPL(reset_control_bulk_put);
1313 
1314 static void devm_reset_control_release(struct device *dev, void *res)
1315 {
1316 	reset_control_put(*(struct reset_control **)res);
1317 }
1318 
1319 static void devm_reset_control_release_deasserted(struct device *dev, void *res)
1320 {
1321 	struct reset_control *rstc = *(struct reset_control **)res;
1322 
1323 	reset_control_assert(rstc);
1324 	reset_control_put(rstc);
1325 }
1326 
1327 struct reset_control *
1328 __devm_reset_control_get(struct device *dev, const char *id, int index,
1329 			 enum reset_control_flags flags)
1330 {
1331 	struct reset_control **ptr, *rstc;
1332 	bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED;
1333 
1334 	ptr = devres_alloc(deasserted ? devm_reset_control_release_deasserted :
1335 			   devm_reset_control_release, sizeof(*ptr),
1336 			   GFP_KERNEL);
1337 	if (!ptr)
1338 		return ERR_PTR(-ENOMEM);
1339 
1340 	flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED;
1341 
1342 	rstc = __reset_control_get(dev, id, index, flags);
1343 	if (IS_ERR_OR_NULL(rstc)) {
1344 		devres_free(ptr);
1345 		return rstc;
1346 	}
1347 
1348 	if (deasserted) {
1349 		int ret;
1350 
1351 		ret = reset_control_deassert(rstc);
1352 		if (ret) {
1353 			reset_control_put(rstc);
1354 			devres_free(ptr);
1355 			return ERR_PTR(ret);
1356 		}
1357 	}
1358 
1359 	*ptr = rstc;
1360 	devres_add(dev, ptr);
1361 
1362 	return rstc;
1363 }
1364 EXPORT_SYMBOL_GPL(__devm_reset_control_get);
1365 
1366 struct reset_control_bulk_devres {
1367 	int num_rstcs;
1368 	struct reset_control_bulk_data *rstcs;
1369 };
1370 
1371 static void devm_reset_control_bulk_release(struct device *dev, void *res)
1372 {
1373 	struct reset_control_bulk_devres *devres = res;
1374 
1375 	reset_control_bulk_put(devres->num_rstcs, devres->rstcs);
1376 }
1377 
1378 static void devm_reset_control_bulk_release_deasserted(struct device *dev, void *res)
1379 {
1380 	struct reset_control_bulk_devres *devres = res;
1381 
1382 	reset_control_bulk_assert(devres->num_rstcs, devres->rstcs);
1383 	reset_control_bulk_put(devres->num_rstcs, devres->rstcs);
1384 }
1385 
1386 int __devm_reset_control_bulk_get(struct device *dev, int num_rstcs,
1387 				  struct reset_control_bulk_data *rstcs,
1388 				  enum reset_control_flags flags)
1389 {
1390 	struct reset_control_bulk_devres *ptr;
1391 	bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED;
1392 	int ret;
1393 
1394 	ptr = devres_alloc(deasserted ? devm_reset_control_bulk_release_deasserted :
1395 			   devm_reset_control_bulk_release, sizeof(*ptr),
1396 			   GFP_KERNEL);
1397 	if (!ptr)
1398 		return -ENOMEM;
1399 
1400 	flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED;
1401 
1402 	ret = __reset_control_bulk_get(dev, num_rstcs, rstcs, flags);
1403 	if (ret < 0) {
1404 		devres_free(ptr);
1405 		return ret;
1406 	}
1407 
1408 	if (deasserted) {
1409 		ret = reset_control_bulk_deassert(num_rstcs, rstcs);
1410 		if (ret) {
1411 			reset_control_bulk_put(num_rstcs, rstcs);
1412 			devres_free(ptr);
1413 			return ret;
1414 		}
1415 	}
1416 
1417 	ptr->num_rstcs = num_rstcs;
1418 	ptr->rstcs = rstcs;
1419 	devres_add(dev, ptr);
1420 
1421 	return 0;
1422 }
1423 EXPORT_SYMBOL_GPL(__devm_reset_control_bulk_get);
1424 
1425 /**
1426  * __device_reset - find reset controller associated with the device
1427  *                  and perform reset
1428  * @dev: device to be reset by the controller
1429  * @optional: whether it is optional to reset the device
1430  *
1431  * Convenience wrapper for __reset_control_get() and reset_control_reset().
1432  * This is useful for the common case of devices with single, dedicated reset
1433  * lines. _RST firmware method will be called for devices with ACPI.
1434  */
1435 int __device_reset(struct device *dev, bool optional)
1436 {
1437 	enum reset_control_flags flags;
1438 	struct reset_control *rstc;
1439 	int ret;
1440 
1441 #ifdef CONFIG_ACPI
1442 	acpi_handle handle = ACPI_HANDLE(dev);
1443 
1444 	if (handle) {
1445 		if (!acpi_has_method(handle, "_RST"))
1446 			return optional ? 0 : -ENOENT;
1447 		if (ACPI_FAILURE(acpi_evaluate_object(handle, "_RST", NULL,
1448 						      NULL)))
1449 			return -EIO;
1450 	}
1451 #endif
1452 
1453 	flags = optional ? RESET_CONTROL_OPTIONAL_EXCLUSIVE : RESET_CONTROL_EXCLUSIVE;
1454 	rstc = __reset_control_get(dev, NULL, 0, flags);
1455 	if (IS_ERR(rstc))
1456 		return PTR_ERR(rstc);
1457 
1458 	ret = reset_control_reset(rstc);
1459 
1460 	reset_control_put(rstc);
1461 
1462 	return ret;
1463 }
1464 EXPORT_SYMBOL_GPL(__device_reset);
1465 
1466 /*
1467  * APIs to manage an array of reset controls.
1468  */
1469 
1470 /**
1471  * fwnode_reset_control_get_count - Count number of resets available with a device
1472  *
1473  * @fwnode: firmware node that contains 'resets'.
1474  *
1475  * Returns positive reset count on success, or error number on failure and
1476  * on count being zero.
1477  */
1478 static int fwnode_reset_control_get_count(struct fwnode_handle *fwnode)
1479 {
1480 	struct fwnode_reference_args args;
1481 	int count = 0, ret;
1482 
1483 	if (!fwnode)
1484 		return -EINVAL;
1485 
1486 	for (;;) {
1487 		ret = fwnode_property_get_reference_args(fwnode, "resets", "#reset-cells",
1488 							 0, count, &args);
1489 		if (ret) {
1490 			if (ret == -ENOENT)
1491 				break;
1492 
1493 			return ret;
1494 		}
1495 
1496 		fwnode_handle_put(args.fwnode);
1497 		count++;
1498 	}
1499 
1500 	if (count == 0)
1501 		count = -ENOENT;
1502 
1503 	return count;
1504 }
1505 
1506 /**
1507  * fwnode_reset_control_array_get - Get a list of reset controls using
1508  *                                  a firmware node.
1509  *
1510  * @fwnode: firmware node for the device that requests the reset controls array
1511  * @flags: whether reset controls are shared, optional, acquired
1512  *
1513  * Returns pointer to allocated reset_control on success or error on failure
1514  */
1515 struct reset_control *
1516 fwnode_reset_control_array_get(struct fwnode_handle *fwnode,
1517 			       enum reset_control_flags flags)
1518 {
1519 	bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL;
1520 	struct reset_control_array *resets;
1521 	struct reset_control *rstc;
1522 	int num, i;
1523 
1524 	num = fwnode_reset_control_get_count(fwnode);
1525 	if (num < 0)
1526 		return optional ? NULL : ERR_PTR(num);
1527 
1528 	resets = kzalloc_flex(*resets, rstc, num);
1529 	if (!resets)
1530 		return ERR_PTR(-ENOMEM);
1531 	resets->num_rstcs = num;
1532 
1533 	for (i = 0; i < num; i++) {
1534 		rstc = __fwnode_reset_control_get(fwnode, NULL, i, flags);
1535 		if (IS_ERR(rstc))
1536 			goto err_rst;
1537 		resets->rstc[i] = rstc;
1538 	}
1539 	resets->base.array = true;
1540 
1541 	return &resets->base;
1542 
1543 err_rst:
1544 	while (--i >= 0)
1545 		reset_control_put_internal(resets->rstc[i]);
1546 
1547 	kfree(resets);
1548 
1549 	return rstc;
1550 }
1551 EXPORT_SYMBOL_GPL(fwnode_reset_control_array_get);
1552 
1553 /**
1554  * devm_reset_control_array_get - Resource managed reset control array get
1555  *
1556  * @dev: device that requests the list of reset controls
1557  * @flags: whether reset controls are shared, optional, acquired
1558  *
1559  * The reset control array APIs are intended for a list of resets
1560  * that just have to be asserted or deasserted, without any
1561  * requirements on the order.
1562  *
1563  * Returns pointer to allocated reset_control on success or error on failure
1564  */
1565 struct reset_control *
1566 devm_reset_control_array_get(struct device *dev, enum reset_control_flags flags)
1567 {
1568 	struct reset_control **ptr, *rstc;
1569 
1570 	ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
1571 			   GFP_KERNEL);
1572 	if (!ptr)
1573 		return ERR_PTR(-ENOMEM);
1574 
1575 	rstc = fwnode_reset_control_array_get(dev_fwnode(dev), flags);
1576 	if (IS_ERR_OR_NULL(rstc)) {
1577 		devres_free(ptr);
1578 		return rstc;
1579 	}
1580 
1581 	*ptr = rstc;
1582 	devres_add(dev, ptr);
1583 
1584 	return rstc;
1585 }
1586 EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
1587 
1588 /**
1589  * reset_control_get_count - Count number of resets available with a device
1590  *
1591  * @dev: device for which to return the number of resets
1592  *
1593  * Returns positive reset count on success, or error number on failure and
1594  * on count being zero.
1595  */
1596 int reset_control_get_count(struct device *dev)
1597 {
1598 	struct fwnode_handle *fwnode = dev_fwnode(dev);
1599 
1600 	if (fwnode)
1601 		return fwnode_reset_control_get_count(fwnode);
1602 
1603 	return -ENOENT;
1604 }
1605 EXPORT_SYMBOL_GPL(reset_control_get_count);
1606