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