xref: /openbmc/linux/drivers/reset/core.c (revision 4a075bd4)
1 /*
2  * Reset Controller framework
3  *
4  * Copyright 2013 Philipp Zabel, Pengutronix
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 #include <linux/atomic.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
16 #include <linux/kref.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/reset.h>
20 #include <linux/reset-controller.h>
21 #include <linux/slab.h>
22 
23 static DEFINE_MUTEX(reset_list_mutex);
24 static LIST_HEAD(reset_controller_list);
25 
26 static DEFINE_MUTEX(reset_lookup_mutex);
27 static LIST_HEAD(reset_lookup_list);
28 
29 /**
30  * struct reset_control - a reset control
31  * @rcdev: a pointer to the reset controller device
32  *         this reset control belongs to
33  * @list: list entry for the rcdev's reset controller list
34  * @id: ID of the reset controller in the reset
35  *      controller device
36  * @refcnt: Number of gets of this reset_control
37  * @acquired: Only one reset_control may be acquired for a given rcdev and id.
38  * @shared: Is this a shared (1), or an exclusive (0) reset_control?
39  * @deassert_cnt: Number of times this reset line has been deasserted
40  * @triggered_count: Number of times this reset line has been reset. Currently
41  *                   only used for shared resets, which means that the value
42  *                   will be either 0 or 1.
43  */
44 struct reset_control {
45 	struct reset_controller_dev *rcdev;
46 	struct list_head list;
47 	unsigned int id;
48 	struct kref refcnt;
49 	bool acquired;
50 	bool shared;
51 	bool array;
52 	atomic_t deassert_count;
53 	atomic_t triggered_count;
54 };
55 
56 /**
57  * struct reset_control_array - an array of reset controls
58  * @base: reset control for compatibility with reset control API functions
59  * @num_rstcs: number of reset controls
60  * @rstc: array of reset controls
61  */
62 struct reset_control_array {
63 	struct reset_control base;
64 	unsigned int num_rstcs;
65 	struct reset_control *rstc[];
66 };
67 
68 static const char *rcdev_name(struct reset_controller_dev *rcdev)
69 {
70 	if (rcdev->dev)
71 		return dev_name(rcdev->dev);
72 
73 	if (rcdev->of_node)
74 		return rcdev->of_node->full_name;
75 
76 	return NULL;
77 }
78 
79 /**
80  * of_reset_simple_xlate - translate reset_spec to the reset line number
81  * @rcdev: a pointer to the reset controller device
82  * @reset_spec: reset line specifier as found in the device tree
83  * @flags: a flags pointer to fill in (optional)
84  *
85  * This simple translation function should be used for reset controllers
86  * with 1:1 mapping, where reset lines can be indexed by number without gaps.
87  */
88 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
89 			  const struct of_phandle_args *reset_spec)
90 {
91 	if (reset_spec->args[0] >= rcdev->nr_resets)
92 		return -EINVAL;
93 
94 	return reset_spec->args[0];
95 }
96 
97 /**
98  * reset_controller_register - register a reset controller device
99  * @rcdev: a pointer to the initialized reset controller device
100  */
101 int reset_controller_register(struct reset_controller_dev *rcdev)
102 {
103 	if (!rcdev->of_xlate) {
104 		rcdev->of_reset_n_cells = 1;
105 		rcdev->of_xlate = of_reset_simple_xlate;
106 	}
107 
108 	INIT_LIST_HEAD(&rcdev->reset_control_head);
109 
110 	mutex_lock(&reset_list_mutex);
111 	list_add(&rcdev->list, &reset_controller_list);
112 	mutex_unlock(&reset_list_mutex);
113 
114 	return 0;
115 }
116 EXPORT_SYMBOL_GPL(reset_controller_register);
117 
118 /**
119  * reset_controller_unregister - unregister a reset controller device
120  * @rcdev: a pointer to the reset controller device
121  */
122 void reset_controller_unregister(struct reset_controller_dev *rcdev)
123 {
124 	mutex_lock(&reset_list_mutex);
125 	list_del(&rcdev->list);
126 	mutex_unlock(&reset_list_mutex);
127 }
128 EXPORT_SYMBOL_GPL(reset_controller_unregister);
129 
130 static void devm_reset_controller_release(struct device *dev, void *res)
131 {
132 	reset_controller_unregister(*(struct reset_controller_dev **)res);
133 }
134 
135 /**
136  * devm_reset_controller_register - resource managed reset_controller_register()
137  * @dev: device that is registering this reset controller
138  * @rcdev: a pointer to the initialized reset controller device
139  *
140  * Managed reset_controller_register(). For reset controllers registered by
141  * this function, reset_controller_unregister() is automatically called on
142  * driver detach. See reset_controller_register() for more information.
143  */
144 int devm_reset_controller_register(struct device *dev,
145 				   struct reset_controller_dev *rcdev)
146 {
147 	struct reset_controller_dev **rcdevp;
148 	int ret;
149 
150 	rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
151 			      GFP_KERNEL);
152 	if (!rcdevp)
153 		return -ENOMEM;
154 
155 	ret = reset_controller_register(rcdev);
156 	if (!ret) {
157 		*rcdevp = rcdev;
158 		devres_add(dev, rcdevp);
159 	} else {
160 		devres_free(rcdevp);
161 	}
162 
163 	return ret;
164 }
165 EXPORT_SYMBOL_GPL(devm_reset_controller_register);
166 
167 /**
168  * reset_controller_add_lookup - register a set of lookup entries
169  * @lookup: array of reset lookup entries
170  * @num_entries: number of entries in the lookup array
171  */
172 void reset_controller_add_lookup(struct reset_control_lookup *lookup,
173 				 unsigned int num_entries)
174 {
175 	struct reset_control_lookup *entry;
176 	unsigned int i;
177 
178 	mutex_lock(&reset_lookup_mutex);
179 	for (i = 0; i < num_entries; i++) {
180 		entry = &lookup[i];
181 
182 		if (!entry->dev_id || !entry->provider) {
183 			pr_warn("%s(): reset lookup entry badly specified, skipping\n",
184 				__func__);
185 			continue;
186 		}
187 
188 		list_add_tail(&entry->list, &reset_lookup_list);
189 	}
190 	mutex_unlock(&reset_lookup_mutex);
191 }
192 EXPORT_SYMBOL_GPL(reset_controller_add_lookup);
193 
194 static inline struct reset_control_array *
195 rstc_to_array(struct reset_control *rstc) {
196 	return container_of(rstc, struct reset_control_array, base);
197 }
198 
199 static int reset_control_array_reset(struct reset_control_array *resets)
200 {
201 	int ret, i;
202 
203 	for (i = 0; i < resets->num_rstcs; i++) {
204 		ret = reset_control_reset(resets->rstc[i]);
205 		if (ret)
206 			return ret;
207 	}
208 
209 	return 0;
210 }
211 
212 static int reset_control_array_assert(struct reset_control_array *resets)
213 {
214 	int ret, i;
215 
216 	for (i = 0; i < resets->num_rstcs; i++) {
217 		ret = reset_control_assert(resets->rstc[i]);
218 		if (ret)
219 			goto err;
220 	}
221 
222 	return 0;
223 
224 err:
225 	while (i--)
226 		reset_control_deassert(resets->rstc[i]);
227 	return ret;
228 }
229 
230 static int reset_control_array_deassert(struct reset_control_array *resets)
231 {
232 	int ret, i;
233 
234 	for (i = 0; i < resets->num_rstcs; i++) {
235 		ret = reset_control_deassert(resets->rstc[i]);
236 		if (ret)
237 			goto err;
238 	}
239 
240 	return 0;
241 
242 err:
243 	while (i--)
244 		reset_control_assert(resets->rstc[i]);
245 	return ret;
246 }
247 
248 static int reset_control_array_acquire(struct reset_control_array *resets)
249 {
250 	unsigned int i;
251 	int err;
252 
253 	for (i = 0; i < resets->num_rstcs; i++) {
254 		err = reset_control_acquire(resets->rstc[i]);
255 		if (err < 0)
256 			goto release;
257 	}
258 
259 	return 0;
260 
261 release:
262 	while (i--)
263 		reset_control_release(resets->rstc[i]);
264 
265 	return err;
266 }
267 
268 static void reset_control_array_release(struct reset_control_array *resets)
269 {
270 	unsigned int i;
271 
272 	for (i = 0; i < resets->num_rstcs; i++)
273 		reset_control_release(resets->rstc[i]);
274 }
275 
276 static inline bool reset_control_is_array(struct reset_control *rstc)
277 {
278 	return rstc->array;
279 }
280 
281 /**
282  * reset_control_reset - reset the controlled device
283  * @rstc: reset controller
284  *
285  * On a shared reset line the actual reset pulse is only triggered once for the
286  * lifetime of the reset_control instance: for all but the first caller this is
287  * a no-op.
288  * Consumers must not use reset_control_(de)assert on shared reset lines when
289  * reset_control_reset has been used.
290  *
291  * If rstc is NULL it is an optional reset and the function will just
292  * return 0.
293  */
294 int reset_control_reset(struct reset_control *rstc)
295 {
296 	int ret;
297 
298 	if (!rstc)
299 		return 0;
300 
301 	if (WARN_ON(IS_ERR(rstc)))
302 		return -EINVAL;
303 
304 	if (reset_control_is_array(rstc))
305 		return reset_control_array_reset(rstc_to_array(rstc));
306 
307 	if (!rstc->rcdev->ops->reset)
308 		return -ENOTSUPP;
309 
310 	if (rstc->shared) {
311 		if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
312 			return -EINVAL;
313 
314 		if (atomic_inc_return(&rstc->triggered_count) != 1)
315 			return 0;
316 	} else {
317 		if (!rstc->acquired)
318 			return -EPERM;
319 	}
320 
321 	ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
322 	if (rstc->shared && ret)
323 		atomic_dec(&rstc->triggered_count);
324 
325 	return ret;
326 }
327 EXPORT_SYMBOL_GPL(reset_control_reset);
328 
329 /**
330  * reset_control_assert - asserts the reset line
331  * @rstc: reset controller
332  *
333  * Calling this on an exclusive reset controller guarantees that the reset
334  * will be asserted. When called on a shared reset controller the line may
335  * still be deasserted, as long as other users keep it so.
336  *
337  * For shared reset controls a driver cannot expect the hw's registers and
338  * internal state to be reset, but must be prepared for this to happen.
339  * Consumers must not use reset_control_reset on shared reset lines when
340  * reset_control_(de)assert has been used.
341  * return 0.
342  *
343  * If rstc is NULL it is an optional reset and the function will just
344  * return 0.
345  */
346 int reset_control_assert(struct reset_control *rstc)
347 {
348 	if (!rstc)
349 		return 0;
350 
351 	if (WARN_ON(IS_ERR(rstc)))
352 		return -EINVAL;
353 
354 	if (reset_control_is_array(rstc))
355 		return reset_control_array_assert(rstc_to_array(rstc));
356 
357 	if (rstc->shared) {
358 		if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
359 			return -EINVAL;
360 
361 		if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
362 			return -EINVAL;
363 
364 		if (atomic_dec_return(&rstc->deassert_count) != 0)
365 			return 0;
366 
367 		/*
368 		 * Shared reset controls allow the reset line to be in any state
369 		 * after this call, so doing nothing is a valid option.
370 		 */
371 		if (!rstc->rcdev->ops->assert)
372 			return 0;
373 	} else {
374 		/*
375 		 * If the reset controller does not implement .assert(), there
376 		 * is no way to guarantee that the reset line is asserted after
377 		 * this call.
378 		 */
379 		if (!rstc->rcdev->ops->assert)
380 			return -ENOTSUPP;
381 
382 		if (!rstc->acquired) {
383 			WARN(1, "reset %s (ID: %u) is not acquired\n",
384 			     rcdev_name(rstc->rcdev), rstc->id);
385 			return -EPERM;
386 		}
387 	}
388 
389 	return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
390 }
391 EXPORT_SYMBOL_GPL(reset_control_assert);
392 
393 /**
394  * reset_control_deassert - deasserts the reset line
395  * @rstc: reset controller
396  *
397  * After calling this function, the reset is guaranteed to be deasserted.
398  * Consumers must not use reset_control_reset on shared reset lines when
399  * reset_control_(de)assert has been used.
400  * return 0.
401  *
402  * If rstc is NULL it is an optional reset and the function will just
403  * return 0.
404  */
405 int reset_control_deassert(struct reset_control *rstc)
406 {
407 	if (!rstc)
408 		return 0;
409 
410 	if (WARN_ON(IS_ERR(rstc)))
411 		return -EINVAL;
412 
413 	if (reset_control_is_array(rstc))
414 		return reset_control_array_deassert(rstc_to_array(rstc));
415 
416 	if (rstc->shared) {
417 		if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
418 			return -EINVAL;
419 
420 		if (atomic_inc_return(&rstc->deassert_count) != 1)
421 			return 0;
422 	} else {
423 		if (!rstc->acquired) {
424 			WARN(1, "reset %s (ID: %u) is not acquired\n",
425 			     rcdev_name(rstc->rcdev), rstc->id);
426 			return -EPERM;
427 		}
428 	}
429 
430 	/*
431 	 * If the reset controller does not implement .deassert(), we assume
432 	 * that it handles self-deasserting reset lines via .reset(). In that
433 	 * case, the reset lines are deasserted by default. If that is not the
434 	 * case, the reset controller driver should implement .deassert() and
435 	 * return -ENOTSUPP.
436 	 */
437 	if (!rstc->rcdev->ops->deassert)
438 		return 0;
439 
440 	return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
441 }
442 EXPORT_SYMBOL_GPL(reset_control_deassert);
443 
444 /**
445  * reset_control_status - returns a negative errno if not supported, a
446  * positive value if the reset line is asserted, or zero if the reset
447  * line is not asserted or if the desc is NULL (optional reset).
448  * @rstc: reset controller
449  */
450 int reset_control_status(struct reset_control *rstc)
451 {
452 	if (!rstc)
453 		return 0;
454 
455 	if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
456 		return -EINVAL;
457 
458 	if (rstc->rcdev->ops->status)
459 		return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
460 
461 	return -ENOTSUPP;
462 }
463 EXPORT_SYMBOL_GPL(reset_control_status);
464 
465 /**
466  * reset_control_acquire() - acquires a reset control for exclusive use
467  * @rstc: reset control
468  *
469  * This is used to explicitly acquire a reset control for exclusive use. Note
470  * that exclusive resets are requested as acquired by default. In order for a
471  * second consumer to be able to control the reset, the first consumer has to
472  * release it first. Typically the easiest way to achieve this is to call the
473  * reset_control_get_exclusive_released() to obtain an instance of the reset
474  * control. Such reset controls are not acquired by default.
475  *
476  * Consumers implementing shared access to an exclusive reset need to follow
477  * a specific protocol in order to work together. Before consumers can change
478  * a reset they must acquire exclusive access using reset_control_acquire().
479  * After they are done operating the reset, they must release exclusive access
480  * with a call to reset_control_release(). Consumers are not granted exclusive
481  * access to the reset as long as another consumer hasn't released a reset.
482  *
483  * See also: reset_control_release()
484  */
485 int reset_control_acquire(struct reset_control *rstc)
486 {
487 	struct reset_control *rc;
488 
489 	if (!rstc)
490 		return 0;
491 
492 	if (WARN_ON(IS_ERR(rstc)))
493 		return -EINVAL;
494 
495 	if (reset_control_is_array(rstc))
496 		return reset_control_array_acquire(rstc_to_array(rstc));
497 
498 	mutex_lock(&reset_list_mutex);
499 
500 	if (rstc->acquired) {
501 		mutex_unlock(&reset_list_mutex);
502 		return 0;
503 	}
504 
505 	list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) {
506 		if (rstc != rc && rstc->id == rc->id) {
507 			if (rc->acquired) {
508 				mutex_unlock(&reset_list_mutex);
509 				return -EBUSY;
510 			}
511 		}
512 	}
513 
514 	rstc->acquired = true;
515 
516 	mutex_unlock(&reset_list_mutex);
517 	return 0;
518 }
519 EXPORT_SYMBOL_GPL(reset_control_acquire);
520 
521 /**
522  * reset_control_release() - releases exclusive access to a reset control
523  * @rstc: reset control
524  *
525  * Releases exclusive access right to a reset control previously obtained by a
526  * call to reset_control_acquire(). Until a consumer calls this function, no
527  * other consumers will be granted exclusive access.
528  *
529  * See also: reset_control_acquire()
530  */
531 void reset_control_release(struct reset_control *rstc)
532 {
533 	if (!rstc || WARN_ON(IS_ERR(rstc)))
534 		return;
535 
536 	if (reset_control_is_array(rstc))
537 		reset_control_array_release(rstc_to_array(rstc));
538 	else
539 		rstc->acquired = false;
540 }
541 EXPORT_SYMBOL_GPL(reset_control_release);
542 
543 static struct reset_control *__reset_control_get_internal(
544 				struct reset_controller_dev *rcdev,
545 				unsigned int index, bool shared, bool acquired)
546 {
547 	struct reset_control *rstc;
548 
549 	lockdep_assert_held(&reset_list_mutex);
550 
551 	list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
552 		if (rstc->id == index) {
553 			/*
554 			 * Allow creating a secondary exclusive reset_control
555 			 * that is initially not acquired for an already
556 			 * controlled reset line.
557 			 */
558 			if (!rstc->shared && !shared && !acquired)
559 				break;
560 
561 			if (WARN_ON(!rstc->shared || !shared))
562 				return ERR_PTR(-EBUSY);
563 
564 			kref_get(&rstc->refcnt);
565 			return rstc;
566 		}
567 	}
568 
569 	rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
570 	if (!rstc)
571 		return ERR_PTR(-ENOMEM);
572 
573 	try_module_get(rcdev->owner);
574 
575 	rstc->rcdev = rcdev;
576 	list_add(&rstc->list, &rcdev->reset_control_head);
577 	rstc->id = index;
578 	kref_init(&rstc->refcnt);
579 	rstc->acquired = acquired;
580 	rstc->shared = shared;
581 
582 	return rstc;
583 }
584 
585 static void __reset_control_release(struct kref *kref)
586 {
587 	struct reset_control *rstc = container_of(kref, struct reset_control,
588 						  refcnt);
589 
590 	lockdep_assert_held(&reset_list_mutex);
591 
592 	module_put(rstc->rcdev->owner);
593 
594 	list_del(&rstc->list);
595 	kfree(rstc);
596 }
597 
598 static void __reset_control_put_internal(struct reset_control *rstc)
599 {
600 	lockdep_assert_held(&reset_list_mutex);
601 
602 	kref_put(&rstc->refcnt, __reset_control_release);
603 }
604 
605 struct reset_control *__of_reset_control_get(struct device_node *node,
606 				     const char *id, int index, bool shared,
607 				     bool optional, bool acquired)
608 {
609 	struct reset_control *rstc;
610 	struct reset_controller_dev *r, *rcdev;
611 	struct of_phandle_args args;
612 	int rstc_id;
613 	int ret;
614 
615 	if (!node)
616 		return ERR_PTR(-EINVAL);
617 
618 	if (id) {
619 		index = of_property_match_string(node,
620 						 "reset-names", id);
621 		if (index == -EILSEQ)
622 			return ERR_PTR(index);
623 		if (index < 0)
624 			return optional ? NULL : ERR_PTR(-ENOENT);
625 	}
626 
627 	ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
628 					 index, &args);
629 	if (ret == -EINVAL)
630 		return ERR_PTR(ret);
631 	if (ret)
632 		return optional ? NULL : ERR_PTR(ret);
633 
634 	mutex_lock(&reset_list_mutex);
635 	rcdev = NULL;
636 	list_for_each_entry(r, &reset_controller_list, list) {
637 		if (args.np == r->of_node) {
638 			rcdev = r;
639 			break;
640 		}
641 	}
642 
643 	if (!rcdev) {
644 		rstc = ERR_PTR(-EPROBE_DEFER);
645 		goto out;
646 	}
647 
648 	if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
649 		rstc = ERR_PTR(-EINVAL);
650 		goto out;
651 	}
652 
653 	rstc_id = rcdev->of_xlate(rcdev, &args);
654 	if (rstc_id < 0) {
655 		rstc = ERR_PTR(rstc_id);
656 		goto out;
657 	}
658 
659 	/* reset_list_mutex also protects the rcdev's reset_control list */
660 	rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
661 
662 out:
663 	mutex_unlock(&reset_list_mutex);
664 	of_node_put(args.np);
665 
666 	return rstc;
667 }
668 EXPORT_SYMBOL_GPL(__of_reset_control_get);
669 
670 static struct reset_controller_dev *
671 __reset_controller_by_name(const char *name)
672 {
673 	struct reset_controller_dev *rcdev;
674 
675 	lockdep_assert_held(&reset_list_mutex);
676 
677 	list_for_each_entry(rcdev, &reset_controller_list, list) {
678 		if (!rcdev->dev)
679 			continue;
680 
681 		if (!strcmp(name, dev_name(rcdev->dev)))
682 			return rcdev;
683 	}
684 
685 	return NULL;
686 }
687 
688 static struct reset_control *
689 __reset_control_get_from_lookup(struct device *dev, const char *con_id,
690 				bool shared, bool optional, bool acquired)
691 {
692 	const struct reset_control_lookup *lookup;
693 	struct reset_controller_dev *rcdev;
694 	const char *dev_id = dev_name(dev);
695 	struct reset_control *rstc = NULL;
696 
697 	if (!dev)
698 		return ERR_PTR(-EINVAL);
699 
700 	mutex_lock(&reset_lookup_mutex);
701 
702 	list_for_each_entry(lookup, &reset_lookup_list, list) {
703 		if (strcmp(lookup->dev_id, dev_id))
704 			continue;
705 
706 		if ((!con_id && !lookup->con_id) ||
707 		    ((con_id && lookup->con_id) &&
708 		     !strcmp(con_id, lookup->con_id))) {
709 			mutex_lock(&reset_list_mutex);
710 			rcdev = __reset_controller_by_name(lookup->provider);
711 			if (!rcdev) {
712 				mutex_unlock(&reset_list_mutex);
713 				mutex_unlock(&reset_lookup_mutex);
714 				/* Reset provider may not be ready yet. */
715 				return ERR_PTR(-EPROBE_DEFER);
716 			}
717 
718 			rstc = __reset_control_get_internal(rcdev,
719 							    lookup->index,
720 							    shared, acquired);
721 			mutex_unlock(&reset_list_mutex);
722 			break;
723 		}
724 	}
725 
726 	mutex_unlock(&reset_lookup_mutex);
727 
728 	if (!rstc)
729 		return optional ? NULL : ERR_PTR(-ENOENT);
730 
731 	return rstc;
732 }
733 
734 struct reset_control *__reset_control_get(struct device *dev, const char *id,
735 					  int index, bool shared, bool optional,
736 					  bool acquired)
737 {
738 	if (WARN_ON(shared && acquired))
739 		return ERR_PTR(-EINVAL);
740 
741 	if (dev->of_node)
742 		return __of_reset_control_get(dev->of_node, id, index, shared,
743 					      optional, acquired);
744 
745 	return __reset_control_get_from_lookup(dev, id, shared, optional,
746 					       acquired);
747 }
748 EXPORT_SYMBOL_GPL(__reset_control_get);
749 
750 static void reset_control_array_put(struct reset_control_array *resets)
751 {
752 	int i;
753 
754 	mutex_lock(&reset_list_mutex);
755 	for (i = 0; i < resets->num_rstcs; i++)
756 		__reset_control_put_internal(resets->rstc[i]);
757 	mutex_unlock(&reset_list_mutex);
758 }
759 
760 /**
761  * reset_control_put - free the reset controller
762  * @rstc: reset controller
763  */
764 void reset_control_put(struct reset_control *rstc)
765 {
766 	if (IS_ERR_OR_NULL(rstc))
767 		return;
768 
769 	if (reset_control_is_array(rstc)) {
770 		reset_control_array_put(rstc_to_array(rstc));
771 		return;
772 	}
773 
774 	mutex_lock(&reset_list_mutex);
775 	__reset_control_put_internal(rstc);
776 	mutex_unlock(&reset_list_mutex);
777 }
778 EXPORT_SYMBOL_GPL(reset_control_put);
779 
780 static void devm_reset_control_release(struct device *dev, void *res)
781 {
782 	reset_control_put(*(struct reset_control **)res);
783 }
784 
785 struct reset_control *__devm_reset_control_get(struct device *dev,
786 				     const char *id, int index, bool shared,
787 				     bool optional, bool acquired)
788 {
789 	struct reset_control **ptr, *rstc;
790 
791 	ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
792 			   GFP_KERNEL);
793 	if (!ptr)
794 		return ERR_PTR(-ENOMEM);
795 
796 	rstc = __reset_control_get(dev, id, index, shared, optional, acquired);
797 	if (!IS_ERR(rstc)) {
798 		*ptr = rstc;
799 		devres_add(dev, ptr);
800 	} else {
801 		devres_free(ptr);
802 	}
803 
804 	return rstc;
805 }
806 EXPORT_SYMBOL_GPL(__devm_reset_control_get);
807 
808 /**
809  * device_reset - find reset controller associated with the device
810  *                and perform reset
811  * @dev: device to be reset by the controller
812  * @optional: whether it is optional to reset the device
813  *
814  * Convenience wrapper for __reset_control_get() and reset_control_reset().
815  * This is useful for the common case of devices with single, dedicated reset
816  * lines.
817  */
818 int __device_reset(struct device *dev, bool optional)
819 {
820 	struct reset_control *rstc;
821 	int ret;
822 
823 	rstc = __reset_control_get(dev, NULL, 0, 0, optional, true);
824 	if (IS_ERR(rstc))
825 		return PTR_ERR(rstc);
826 
827 	ret = reset_control_reset(rstc);
828 
829 	reset_control_put(rstc);
830 
831 	return ret;
832 }
833 EXPORT_SYMBOL_GPL(__device_reset);
834 
835 /**
836  * APIs to manage an array of reset controls.
837  */
838 /**
839  * of_reset_control_get_count - Count number of resets available with a device
840  *
841  * @node: device node that contains 'resets'.
842  *
843  * Returns positive reset count on success, or error number on failure and
844  * on count being zero.
845  */
846 static int of_reset_control_get_count(struct device_node *node)
847 {
848 	int count;
849 
850 	if (!node)
851 		return -EINVAL;
852 
853 	count = of_count_phandle_with_args(node, "resets", "#reset-cells");
854 	if (count == 0)
855 		count = -ENOENT;
856 
857 	return count;
858 }
859 
860 /**
861  * of_reset_control_array_get - Get a list of reset controls using
862  *				device node.
863  *
864  * @np: device node for the device that requests the reset controls array
865  * @shared: whether reset controls are shared or not
866  * @optional: whether it is optional to get the reset controls
867  * @acquired: only one reset control may be acquired for a given controller
868  *            and ID
869  *
870  * Returns pointer to allocated reset_control_array on success or
871  * error on failure
872  */
873 struct reset_control *
874 of_reset_control_array_get(struct device_node *np, bool shared, bool optional,
875 			   bool acquired)
876 {
877 	struct reset_control_array *resets;
878 	struct reset_control *rstc;
879 	int num, i;
880 
881 	num = of_reset_control_get_count(np);
882 	if (num < 0)
883 		return optional ? NULL : ERR_PTR(num);
884 
885 	resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL);
886 	if (!resets)
887 		return ERR_PTR(-ENOMEM);
888 
889 	for (i = 0; i < num; i++) {
890 		rstc = __of_reset_control_get(np, NULL, i, shared, optional,
891 					      acquired);
892 		if (IS_ERR(rstc))
893 			goto err_rst;
894 		resets->rstc[i] = rstc;
895 	}
896 	resets->num_rstcs = num;
897 	resets->base.array = true;
898 
899 	return &resets->base;
900 
901 err_rst:
902 	mutex_lock(&reset_list_mutex);
903 	while (--i >= 0)
904 		__reset_control_put_internal(resets->rstc[i]);
905 	mutex_unlock(&reset_list_mutex);
906 
907 	kfree(resets);
908 
909 	return rstc;
910 }
911 EXPORT_SYMBOL_GPL(of_reset_control_array_get);
912 
913 /**
914  * devm_reset_control_array_get - Resource managed reset control array get
915  *
916  * @dev: device that requests the list of reset controls
917  * @shared: whether reset controls are shared or not
918  * @optional: whether it is optional to get the reset controls
919  *
920  * The reset control array APIs are intended for a list of resets
921  * that just have to be asserted or deasserted, without any
922  * requirements on the order.
923  *
924  * Returns pointer to allocated reset_control_array on success or
925  * error on failure
926  */
927 struct reset_control *
928 devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
929 {
930 	struct reset_control **devres;
931 	struct reset_control *rstc;
932 
933 	devres = devres_alloc(devm_reset_control_release, sizeof(*devres),
934 			      GFP_KERNEL);
935 	if (!devres)
936 		return ERR_PTR(-ENOMEM);
937 
938 	rstc = of_reset_control_array_get(dev->of_node, shared, optional, true);
939 	if (IS_ERR(rstc)) {
940 		devres_free(devres);
941 		return rstc;
942 	}
943 
944 	*devres = rstc;
945 	devres_add(dev, devres);
946 
947 	return rstc;
948 }
949 EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
950 
951 static int reset_control_get_count_from_lookup(struct device *dev)
952 {
953 	const struct reset_control_lookup *lookup;
954 	const char *dev_id;
955 	int count = 0;
956 
957 	if (!dev)
958 		return -EINVAL;
959 
960 	dev_id = dev_name(dev);
961 	mutex_lock(&reset_lookup_mutex);
962 
963 	list_for_each_entry(lookup, &reset_lookup_list, list) {
964 		if (!strcmp(lookup->dev_id, dev_id))
965 			count++;
966 	}
967 
968 	mutex_unlock(&reset_lookup_mutex);
969 
970 	if (count == 0)
971 		count = -ENOENT;
972 
973 	return count;
974 }
975 
976 /**
977  * reset_control_get_count - Count number of resets available with a device
978  *
979  * @dev: device for which to return the number of resets
980  *
981  * Returns positive reset count on success, or error number on failure and
982  * on count being zero.
983  */
984 int reset_control_get_count(struct device *dev)
985 {
986 	if (dev->of_node)
987 		return of_reset_control_get_count(dev->of_node);
988 
989 	return reset_control_get_count_from_lookup(dev);
990 }
991 EXPORT_SYMBOL_GPL(reset_control_get_count);
992