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