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 * @shared: Is this a shared (1), or an exclusive (0) reset_control?
38 * @deassert_cnt: Number of times this reset line has been deasserted
39 * @triggered_count: Number of times this reset line has been reset. Currently
40 * only used for shared resets, which means that the value
41 * will be either 0 or 1.
42 */
43 struct reset_control {
44 struct reset_controller_dev *rcdev;
45 struct list_head list;
46 unsigned int id;
47 struct kref refcnt;
48 bool shared;
49 bool array;
50 atomic_t deassert_count;
51 atomic_t triggered_count;
52 };
53
54 /**
55 * struct reset_control_array - an array of reset controls
56 * @base: reset control for compatibility with reset control API functions
57 * @num_rstcs: number of reset controls
58 * @rstc: array of reset controls
59 */
60 struct reset_control_array {
61 struct reset_control base;
62 unsigned int num_rstcs;
63 struct reset_control *rstc[];
64 };
65
66 /**
67 * of_reset_simple_xlate - translate reset_spec to the reset line number
68 * @rcdev: a pointer to the reset controller device
69 * @reset_spec: reset line specifier as found in the device tree
70 * @flags: a flags pointer to fill in (optional)
71 *
72 * This simple translation function should be used for reset controllers
73 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
74 */
of_reset_simple_xlate(struct reset_controller_dev * rcdev,const struct of_phandle_args * reset_spec)75 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
76 const struct of_phandle_args *reset_spec)
77 {
78 if (reset_spec->args[0] >= rcdev->nr_resets)
79 return -EINVAL;
80
81 return reset_spec->args[0];
82 }
83
84 /**
85 * reset_controller_register - register a reset controller device
86 * @rcdev: a pointer to the initialized reset controller device
87 */
reset_controller_register(struct reset_controller_dev * rcdev)88 int reset_controller_register(struct reset_controller_dev *rcdev)
89 {
90 if (!rcdev->of_xlate) {
91 rcdev->of_reset_n_cells = 1;
92 rcdev->of_xlate = of_reset_simple_xlate;
93 }
94
95 INIT_LIST_HEAD(&rcdev->reset_control_head);
96
97 mutex_lock(&reset_list_mutex);
98 list_add(&rcdev->list, &reset_controller_list);
99 mutex_unlock(&reset_list_mutex);
100
101 return 0;
102 }
103 EXPORT_SYMBOL_GPL(reset_controller_register);
104
105 /**
106 * reset_controller_unregister - unregister a reset controller device
107 * @rcdev: a pointer to the reset controller device
108 */
reset_controller_unregister(struct reset_controller_dev * rcdev)109 void reset_controller_unregister(struct reset_controller_dev *rcdev)
110 {
111 mutex_lock(&reset_list_mutex);
112 list_del(&rcdev->list);
113 mutex_unlock(&reset_list_mutex);
114 }
115 EXPORT_SYMBOL_GPL(reset_controller_unregister);
116
devm_reset_controller_release(struct device * dev,void * res)117 static void devm_reset_controller_release(struct device *dev, void *res)
118 {
119 reset_controller_unregister(*(struct reset_controller_dev **)res);
120 }
121
122 /**
123 * devm_reset_controller_register - resource managed reset_controller_register()
124 * @dev: device that is registering this reset controller
125 * @rcdev: a pointer to the initialized reset controller device
126 *
127 * Managed reset_controller_register(). For reset controllers registered by
128 * this function, reset_controller_unregister() is automatically called on
129 * driver detach. See reset_controller_register() for more information.
130 */
devm_reset_controller_register(struct device * dev,struct reset_controller_dev * rcdev)131 int devm_reset_controller_register(struct device *dev,
132 struct reset_controller_dev *rcdev)
133 {
134 struct reset_controller_dev **rcdevp;
135 int ret;
136
137 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
138 GFP_KERNEL);
139 if (!rcdevp)
140 return -ENOMEM;
141
142 ret = reset_controller_register(rcdev);
143 if (!ret) {
144 *rcdevp = rcdev;
145 devres_add(dev, rcdevp);
146 } else {
147 devres_free(rcdevp);
148 }
149
150 return ret;
151 }
152 EXPORT_SYMBOL_GPL(devm_reset_controller_register);
153
154 /**
155 * reset_controller_add_lookup - register a set of lookup entries
156 * @lookup: array of reset lookup entries
157 * @num_entries: number of entries in the lookup array
158 */
reset_controller_add_lookup(struct reset_control_lookup * lookup,unsigned int num_entries)159 void reset_controller_add_lookup(struct reset_control_lookup *lookup,
160 unsigned int num_entries)
161 {
162 struct reset_control_lookup *entry;
163 unsigned int i;
164
165 mutex_lock(&reset_lookup_mutex);
166 for (i = 0; i < num_entries; i++) {
167 entry = &lookup[i];
168
169 if (!entry->dev_id || !entry->provider) {
170 pr_warn("%s(): reset lookup entry badly specified, skipping\n",
171 __func__);
172 continue;
173 }
174
175 list_add_tail(&entry->list, &reset_lookup_list);
176 }
177 mutex_unlock(&reset_lookup_mutex);
178 }
179 EXPORT_SYMBOL_GPL(reset_controller_add_lookup);
180
181 static inline struct reset_control_array *
rstc_to_array(struct reset_control * rstc)182 rstc_to_array(struct reset_control *rstc) {
183 return container_of(rstc, struct reset_control_array, base);
184 }
185
reset_control_array_reset(struct reset_control_array * resets)186 static int reset_control_array_reset(struct reset_control_array *resets)
187 {
188 int ret, i;
189
190 for (i = 0; i < resets->num_rstcs; i++) {
191 ret = reset_control_reset(resets->rstc[i]);
192 if (ret)
193 return ret;
194 }
195
196 return 0;
197 }
198
reset_control_array_assert(struct reset_control_array * resets)199 static int reset_control_array_assert(struct reset_control_array *resets)
200 {
201 int ret, i;
202
203 for (i = 0; i < resets->num_rstcs; i++) {
204 ret = reset_control_assert(resets->rstc[i]);
205 if (ret)
206 goto err;
207 }
208
209 return 0;
210
211 err:
212 while (i--)
213 reset_control_deassert(resets->rstc[i]);
214 return ret;
215 }
216
reset_control_array_deassert(struct reset_control_array * resets)217 static int reset_control_array_deassert(struct reset_control_array *resets)
218 {
219 int ret, i;
220
221 for (i = 0; i < resets->num_rstcs; i++) {
222 ret = reset_control_deassert(resets->rstc[i]);
223 if (ret)
224 goto err;
225 }
226
227 return 0;
228
229 err:
230 while (i--)
231 reset_control_assert(resets->rstc[i]);
232 return ret;
233 }
234
reset_control_is_array(struct reset_control * rstc)235 static inline bool reset_control_is_array(struct reset_control *rstc)
236 {
237 return rstc->array;
238 }
239
240 /**
241 * reset_control_reset - reset the controlled device
242 * @rstc: reset controller
243 *
244 * On a shared reset line the actual reset pulse is only triggered once for the
245 * lifetime of the reset_control instance: for all but the first caller this is
246 * a no-op.
247 * Consumers must not use reset_control_(de)assert on shared reset lines when
248 * reset_control_reset has been used.
249 *
250 * If rstc is NULL it is an optional reset and the function will just
251 * return 0.
252 */
reset_control_reset(struct reset_control * rstc)253 int reset_control_reset(struct reset_control *rstc)
254 {
255 int ret;
256
257 if (!rstc)
258 return 0;
259
260 if (WARN_ON(IS_ERR(rstc)))
261 return -EINVAL;
262
263 if (reset_control_is_array(rstc))
264 return reset_control_array_reset(rstc_to_array(rstc));
265
266 if (!rstc->rcdev->ops->reset)
267 return -ENOTSUPP;
268
269 if (rstc->shared) {
270 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
271 return -EINVAL;
272
273 if (atomic_inc_return(&rstc->triggered_count) != 1)
274 return 0;
275 }
276
277 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
278 if (rstc->shared && ret)
279 atomic_dec(&rstc->triggered_count);
280
281 return ret;
282 }
283 EXPORT_SYMBOL_GPL(reset_control_reset);
284
285 /**
286 * reset_control_assert - asserts the reset line
287 * @rstc: reset controller
288 *
289 * Calling this on an exclusive reset controller guarantees that the reset
290 * will be asserted. When called on a shared reset controller the line may
291 * still be deasserted, as long as other users keep it so.
292 *
293 * For shared reset controls a driver cannot expect the hw's registers and
294 * internal state to be reset, but must be prepared for this to happen.
295 * Consumers must not use reset_control_reset on shared reset lines when
296 * reset_control_(de)assert has been used.
297 * return 0.
298 *
299 * If rstc is NULL it is an optional reset and the function will just
300 * return 0.
301 */
reset_control_assert(struct reset_control * rstc)302 int reset_control_assert(struct reset_control *rstc)
303 {
304 if (!rstc)
305 return 0;
306
307 if (WARN_ON(IS_ERR(rstc)))
308 return -EINVAL;
309
310 if (reset_control_is_array(rstc))
311 return reset_control_array_assert(rstc_to_array(rstc));
312
313 if (rstc->shared) {
314 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
315 return -EINVAL;
316
317 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
318 return -EINVAL;
319
320 if (atomic_dec_return(&rstc->deassert_count) != 0)
321 return 0;
322
323 /*
324 * Shared reset controls allow the reset line to be in any state
325 * after this call, so doing nothing is a valid option.
326 */
327 if (!rstc->rcdev->ops->assert)
328 return 0;
329 } else {
330 /*
331 * If the reset controller does not implement .assert(), there
332 * is no way to guarantee that the reset line is asserted after
333 * this call.
334 */
335 if (!rstc->rcdev->ops->assert)
336 return -ENOTSUPP;
337 }
338
339 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
340 }
341 EXPORT_SYMBOL_GPL(reset_control_assert);
342
343 /**
344 * reset_control_deassert - deasserts the reset line
345 * @rstc: reset controller
346 *
347 * After calling this function, the reset is guaranteed to be deasserted.
348 * Consumers must not use reset_control_reset on shared reset lines when
349 * reset_control_(de)assert has been used.
350 * return 0.
351 *
352 * If rstc is NULL it is an optional reset and the function will just
353 * return 0.
354 */
reset_control_deassert(struct reset_control * rstc)355 int reset_control_deassert(struct reset_control *rstc)
356 {
357 if (!rstc)
358 return 0;
359
360 if (WARN_ON(IS_ERR(rstc)))
361 return -EINVAL;
362
363 if (reset_control_is_array(rstc))
364 return reset_control_array_deassert(rstc_to_array(rstc));
365
366 if (rstc->shared) {
367 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
368 return -EINVAL;
369
370 if (atomic_inc_return(&rstc->deassert_count) != 1)
371 return 0;
372 }
373
374 /*
375 * If the reset controller does not implement .deassert(), we assume
376 * that it handles self-deasserting reset lines via .reset(). In that
377 * case, the reset lines are deasserted by default. If that is not the
378 * case, the reset controller driver should implement .deassert() and
379 * return -ENOTSUPP.
380 */
381 if (!rstc->rcdev->ops->deassert)
382 return 0;
383
384 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
385 }
386 EXPORT_SYMBOL_GPL(reset_control_deassert);
387
388 /**
389 * reset_control_status - returns a negative errno if not supported, a
390 * positive value if the reset line is asserted, or zero if the reset
391 * line is not asserted or if the desc is NULL (optional reset).
392 * @rstc: reset controller
393 */
reset_control_status(struct reset_control * rstc)394 int reset_control_status(struct reset_control *rstc)
395 {
396 if (!rstc)
397 return 0;
398
399 if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
400 return -EINVAL;
401
402 if (rstc->rcdev->ops->status)
403 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
404
405 return -ENOTSUPP;
406 }
407 EXPORT_SYMBOL_GPL(reset_control_status);
408
__reset_control_get_internal(struct reset_controller_dev * rcdev,unsigned int index,bool shared)409 static struct reset_control *__reset_control_get_internal(
410 struct reset_controller_dev *rcdev,
411 unsigned int index, bool shared)
412 {
413 struct reset_control *rstc;
414
415 lockdep_assert_held(&reset_list_mutex);
416
417 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
418 if (rstc->id == index) {
419 if (WARN_ON(!rstc->shared || !shared))
420 return ERR_PTR(-EBUSY);
421
422 kref_get(&rstc->refcnt);
423 return rstc;
424 }
425 }
426
427 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
428 if (!rstc)
429 return ERR_PTR(-ENOMEM);
430
431 if (!try_module_get(rcdev->owner)) {
432 kfree(rstc);
433 return ERR_PTR(-ENODEV);
434 }
435
436 rstc->rcdev = rcdev;
437 list_add(&rstc->list, &rcdev->reset_control_head);
438 rstc->id = index;
439 kref_init(&rstc->refcnt);
440 rstc->shared = shared;
441
442 return rstc;
443 }
444
__reset_control_release(struct kref * kref)445 static void __reset_control_release(struct kref *kref)
446 {
447 struct reset_control *rstc = container_of(kref, struct reset_control,
448 refcnt);
449
450 lockdep_assert_held(&reset_list_mutex);
451
452 module_put(rstc->rcdev->owner);
453
454 list_del(&rstc->list);
455 kfree(rstc);
456 }
457
__reset_control_put_internal(struct reset_control * rstc)458 static void __reset_control_put_internal(struct reset_control *rstc)
459 {
460 lockdep_assert_held(&reset_list_mutex);
461
462 kref_put(&rstc->refcnt, __reset_control_release);
463 }
464
__of_reset_control_get(struct device_node * node,const char * id,int index,bool shared,bool optional)465 struct reset_control *__of_reset_control_get(struct device_node *node,
466 const char *id, int index, bool shared,
467 bool optional)
468 {
469 struct reset_control *rstc;
470 struct reset_controller_dev *r, *rcdev;
471 struct of_phandle_args args;
472 int rstc_id;
473 int ret;
474
475 if (!node)
476 return ERR_PTR(-EINVAL);
477
478 if (id) {
479 index = of_property_match_string(node,
480 "reset-names", id);
481 if (index == -EILSEQ)
482 return ERR_PTR(index);
483 if (index < 0)
484 return optional ? NULL : ERR_PTR(-ENOENT);
485 }
486
487 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
488 index, &args);
489 if (ret == -EINVAL)
490 return ERR_PTR(ret);
491 if (ret)
492 return optional ? NULL : ERR_PTR(ret);
493
494 mutex_lock(&reset_list_mutex);
495 rcdev = NULL;
496 list_for_each_entry(r, &reset_controller_list, list) {
497 if (args.np == r->of_node) {
498 rcdev = r;
499 break;
500 }
501 }
502
503 if (!rcdev) {
504 rstc = ERR_PTR(-EPROBE_DEFER);
505 goto out;
506 }
507
508 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
509 rstc = ERR_PTR(-EINVAL);
510 goto out;
511 }
512
513 rstc_id = rcdev->of_xlate(rcdev, &args);
514 if (rstc_id < 0) {
515 rstc = ERR_PTR(rstc_id);
516 goto out;
517 }
518
519 /* reset_list_mutex also protects the rcdev's reset_control list */
520 rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
521
522 out:
523 mutex_unlock(&reset_list_mutex);
524 of_node_put(args.np);
525
526 return rstc;
527 }
528 EXPORT_SYMBOL_GPL(__of_reset_control_get);
529
530 static struct reset_controller_dev *
__reset_controller_by_name(const char * name)531 __reset_controller_by_name(const char *name)
532 {
533 struct reset_controller_dev *rcdev;
534
535 lockdep_assert_held(&reset_list_mutex);
536
537 list_for_each_entry(rcdev, &reset_controller_list, list) {
538 if (!rcdev->dev)
539 continue;
540
541 if (!strcmp(name, dev_name(rcdev->dev)))
542 return rcdev;
543 }
544
545 return NULL;
546 }
547
548 static struct reset_control *
__reset_control_get_from_lookup(struct device * dev,const char * con_id,bool shared,bool optional)549 __reset_control_get_from_lookup(struct device *dev, const char *con_id,
550 bool shared, bool optional)
551 {
552 const struct reset_control_lookup *lookup;
553 struct reset_controller_dev *rcdev;
554 const char *dev_id = dev_name(dev);
555 struct reset_control *rstc = NULL;
556
557 if (!dev)
558 return ERR_PTR(-EINVAL);
559
560 mutex_lock(&reset_lookup_mutex);
561
562 list_for_each_entry(lookup, &reset_lookup_list, list) {
563 if (strcmp(lookup->dev_id, dev_id))
564 continue;
565
566 if ((!con_id && !lookup->con_id) ||
567 ((con_id && lookup->con_id) &&
568 !strcmp(con_id, lookup->con_id))) {
569 mutex_lock(&reset_list_mutex);
570 rcdev = __reset_controller_by_name(lookup->provider);
571 if (!rcdev) {
572 mutex_unlock(&reset_list_mutex);
573 mutex_unlock(&reset_lookup_mutex);
574 /* Reset provider may not be ready yet. */
575 return ERR_PTR(-EPROBE_DEFER);
576 }
577
578 rstc = __reset_control_get_internal(rcdev,
579 lookup->index,
580 shared);
581 mutex_unlock(&reset_list_mutex);
582 break;
583 }
584 }
585
586 mutex_unlock(&reset_lookup_mutex);
587
588 if (!rstc)
589 return optional ? NULL : ERR_PTR(-ENOENT);
590
591 return rstc;
592 }
593
__reset_control_get(struct device * dev,const char * id,int index,bool shared,bool optional)594 struct reset_control *__reset_control_get(struct device *dev, const char *id,
595 int index, bool shared, bool optional)
596 {
597 if (dev->of_node)
598 return __of_reset_control_get(dev->of_node, id, index, shared,
599 optional);
600
601 return __reset_control_get_from_lookup(dev, id, shared, optional);
602 }
603 EXPORT_SYMBOL_GPL(__reset_control_get);
604
reset_control_array_put(struct reset_control_array * resets)605 static void reset_control_array_put(struct reset_control_array *resets)
606 {
607 int i;
608
609 mutex_lock(&reset_list_mutex);
610 for (i = 0; i < resets->num_rstcs; i++)
611 __reset_control_put_internal(resets->rstc[i]);
612 mutex_unlock(&reset_list_mutex);
613 kfree(resets);
614 }
615
616 /**
617 * reset_control_put - free the reset controller
618 * @rstc: reset controller
619 */
reset_control_put(struct reset_control * rstc)620 void reset_control_put(struct reset_control *rstc)
621 {
622 if (IS_ERR_OR_NULL(rstc))
623 return;
624
625 if (reset_control_is_array(rstc)) {
626 reset_control_array_put(rstc_to_array(rstc));
627 return;
628 }
629
630 mutex_lock(&reset_list_mutex);
631 __reset_control_put_internal(rstc);
632 mutex_unlock(&reset_list_mutex);
633 }
634 EXPORT_SYMBOL_GPL(reset_control_put);
635
devm_reset_control_release(struct device * dev,void * res)636 static void devm_reset_control_release(struct device *dev, void *res)
637 {
638 reset_control_put(*(struct reset_control **)res);
639 }
640
__devm_reset_control_get(struct device * dev,const char * id,int index,bool shared,bool optional)641 struct reset_control *__devm_reset_control_get(struct device *dev,
642 const char *id, int index, bool shared,
643 bool optional)
644 {
645 struct reset_control **ptr, *rstc;
646
647 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
648 GFP_KERNEL);
649 if (!ptr)
650 return ERR_PTR(-ENOMEM);
651
652 rstc = __reset_control_get(dev, id, index, shared, optional);
653 if (!IS_ERR(rstc)) {
654 *ptr = rstc;
655 devres_add(dev, ptr);
656 } else {
657 devres_free(ptr);
658 }
659
660 return rstc;
661 }
662 EXPORT_SYMBOL_GPL(__devm_reset_control_get);
663
664 /**
665 * device_reset - find reset controller associated with the device
666 * and perform reset
667 * @dev: device to be reset by the controller
668 * @optional: whether it is optional to reset the device
669 *
670 * Convenience wrapper for __reset_control_get() and reset_control_reset().
671 * This is useful for the common case of devices with single, dedicated reset
672 * lines.
673 */
__device_reset(struct device * dev,bool optional)674 int __device_reset(struct device *dev, bool optional)
675 {
676 struct reset_control *rstc;
677 int ret;
678
679 rstc = __reset_control_get(dev, NULL, 0, 0, optional);
680 if (IS_ERR(rstc))
681 return PTR_ERR(rstc);
682
683 ret = reset_control_reset(rstc);
684
685 reset_control_put(rstc);
686
687 return ret;
688 }
689 EXPORT_SYMBOL_GPL(__device_reset);
690
691 /**
692 * APIs to manage an array of reset controls.
693 */
694 /**
695 * of_reset_control_get_count - Count number of resets available with a device
696 *
697 * @node: device node that contains 'resets'.
698 *
699 * Returns positive reset count on success, or error number on failure and
700 * on count being zero.
701 */
of_reset_control_get_count(struct device_node * node)702 static int of_reset_control_get_count(struct device_node *node)
703 {
704 int count;
705
706 if (!node)
707 return -EINVAL;
708
709 count = of_count_phandle_with_args(node, "resets", "#reset-cells");
710 if (count == 0)
711 count = -ENOENT;
712
713 return count;
714 }
715
716 /**
717 * of_reset_control_array_get - Get a list of reset controls using
718 * device node.
719 *
720 * @np: device node for the device that requests the reset controls array
721 * @shared: whether reset controls are shared or not
722 * @optional: whether it is optional to get the reset controls
723 *
724 * Returns pointer to allocated reset_control_array on success or
725 * error on failure
726 */
727 struct reset_control *
of_reset_control_array_get(struct device_node * np,bool shared,bool optional)728 of_reset_control_array_get(struct device_node *np, bool shared, bool optional)
729 {
730 struct reset_control_array *resets;
731 struct reset_control *rstc;
732 int num, i;
733
734 num = of_reset_control_get_count(np);
735 if (num < 0)
736 return optional ? NULL : ERR_PTR(num);
737
738 resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL);
739 if (!resets)
740 return ERR_PTR(-ENOMEM);
741
742 for (i = 0; i < num; i++) {
743 rstc = __of_reset_control_get(np, NULL, i, shared, optional);
744 if (IS_ERR(rstc))
745 goto err_rst;
746 resets->rstc[i] = rstc;
747 }
748 resets->num_rstcs = num;
749 resets->base.array = true;
750
751 return &resets->base;
752
753 err_rst:
754 mutex_lock(&reset_list_mutex);
755 while (--i >= 0)
756 __reset_control_put_internal(resets->rstc[i]);
757 mutex_unlock(&reset_list_mutex);
758
759 kfree(resets);
760
761 return rstc;
762 }
763 EXPORT_SYMBOL_GPL(of_reset_control_array_get);
764
765 /**
766 * devm_reset_control_array_get - Resource managed reset control array get
767 *
768 * @dev: device that requests the list of reset controls
769 * @shared: whether reset controls are shared or not
770 * @optional: whether it is optional to get the reset controls
771 *
772 * The reset control array APIs are intended for a list of resets
773 * that just have to be asserted or deasserted, without any
774 * requirements on the order.
775 *
776 * Returns pointer to allocated reset_control_array on success or
777 * error on failure
778 */
779 struct reset_control *
devm_reset_control_array_get(struct device * dev,bool shared,bool optional)780 devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
781 {
782 struct reset_control **devres;
783 struct reset_control *rstc;
784
785 devres = devres_alloc(devm_reset_control_release, sizeof(*devres),
786 GFP_KERNEL);
787 if (!devres)
788 return ERR_PTR(-ENOMEM);
789
790 rstc = of_reset_control_array_get(dev->of_node, shared, optional);
791 if (IS_ERR(rstc)) {
792 devres_free(devres);
793 return rstc;
794 }
795
796 *devres = rstc;
797 devres_add(dev, devres);
798
799 return rstc;
800 }
801 EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
802