1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Reset Controller framework
5 * Copyright 2013 Philipp Zabel, Pengutronix
7 #include <linux/atomic.h>
8 #include <linux/device.h>
10 #include <linux/export.h>
11 #include <linux/kernel.h>
12 #include <linux/kref.h>
13 #include <linux/module.h>
15 #include <linux/reset.h>
16 #include <linux/reset-controller.h>
17 #include <linux/slab.h>
19 static DEFINE_MUTEX(reset_list_mutex);
20 static LIST_HEAD(reset_controller_list);
22 static DEFINE_MUTEX(reset_lookup_mutex);
23 static LIST_HEAD(reset_lookup_list);
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
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.
41 struct reset_control {
42 struct reset_controller_dev *rcdev;
43 struct list_head list;
49 atomic_t deassert_count;
50 atomic_t triggered_count;
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
59 struct reset_control_array {
60 struct reset_control base;
61 unsigned int num_rstcs;
62 struct reset_control *rstc[];
65 static const char *rcdev_name(struct reset_controller_dev *rcdev)
68 return dev_name(rcdev->dev);
71 return rcdev->of_node->full_name;
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
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
86 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
87 const struct of_phandle_args *reset_spec)
89 if (reset_spec->args[0] >= rcdev->nr_resets)
92 return reset_spec->args[0];
96 * reset_controller_register - register a reset controller device
97 * @rcdev: a pointer to the initialized reset controller device
99 int reset_controller_register(struct reset_controller_dev *rcdev)
101 if (!rcdev->of_xlate) {
102 rcdev->of_reset_n_cells = 1;
103 rcdev->of_xlate = of_reset_simple_xlate;
106 INIT_LIST_HEAD(&rcdev->reset_control_head);
108 mutex_lock(&reset_list_mutex);
109 list_add(&rcdev->list, &reset_controller_list);
110 mutex_unlock(&reset_list_mutex);
114 EXPORT_SYMBOL_GPL(reset_controller_register);
117 * reset_controller_unregister - unregister a reset controller device
118 * @rcdev: a pointer to the reset controller device
120 void reset_controller_unregister(struct reset_controller_dev *rcdev)
122 mutex_lock(&reset_list_mutex);
123 list_del(&rcdev->list);
124 mutex_unlock(&reset_list_mutex);
126 EXPORT_SYMBOL_GPL(reset_controller_unregister);
128 static void devm_reset_controller_release(struct device *dev, void *res)
130 reset_controller_unregister(*(struct reset_controller_dev **)res);
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
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.
142 int devm_reset_controller_register(struct device *dev,
143 struct reset_controller_dev *rcdev)
145 struct reset_controller_dev **rcdevp;
148 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
153 ret = reset_controller_register(rcdev);
160 devres_add(dev, rcdevp);
164 EXPORT_SYMBOL_GPL(devm_reset_controller_register);
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
171 void reset_controller_add_lookup(struct reset_control_lookup *lookup,
172 unsigned int num_entries)
174 struct reset_control_lookup *entry;
177 mutex_lock(&reset_lookup_mutex);
178 for (i = 0; i < num_entries; i++) {
181 if (!entry->dev_id || !entry->provider) {
182 pr_warn("%s(): reset lookup entry badly specified, skipping\n",
187 list_add_tail(&entry->list, &reset_lookup_list);
189 mutex_unlock(&reset_lookup_mutex);
191 EXPORT_SYMBOL_GPL(reset_controller_add_lookup);
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);
198 static int reset_control_array_reset(struct reset_control_array *resets)
202 for (i = 0; i < resets->num_rstcs; i++) {
203 ret = reset_control_reset(resets->rstc[i]);
211 static int reset_control_array_assert(struct reset_control_array *resets)
215 for (i = 0; i < resets->num_rstcs; i++) {
216 ret = reset_control_assert(resets->rstc[i]);
225 reset_control_deassert(resets->rstc[i]);
229 static int reset_control_array_deassert(struct reset_control_array *resets)
233 for (i = 0; i < resets->num_rstcs; i++) {
234 ret = reset_control_deassert(resets->rstc[i]);
243 reset_control_assert(resets->rstc[i]);
247 static int reset_control_array_acquire(struct reset_control_array *resets)
252 for (i = 0; i < resets->num_rstcs; i++) {
253 err = reset_control_acquire(resets->rstc[i]);
262 reset_control_release(resets->rstc[i]);
267 static void reset_control_array_release(struct reset_control_array *resets)
271 for (i = 0; i < resets->num_rstcs; i++)
272 reset_control_release(resets->rstc[i]);
275 static inline bool reset_control_is_array(struct reset_control *rstc)
281 * reset_control_reset - reset the controlled device
282 * @rstc: reset controller
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
287 * Consumers must not use reset_control_(de)assert on shared reset lines when
288 * reset_control_reset has been used.
290 * If rstc is NULL it is an optional reset and the function will just
293 int reset_control_reset(struct reset_control *rstc)
300 if (WARN_ON(IS_ERR(rstc)))
303 if (reset_control_is_array(rstc))
304 return reset_control_array_reset(rstc_to_array(rstc));
306 if (!rstc->rcdev->ops->reset)
310 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
313 if (atomic_inc_return(&rstc->triggered_count) != 1)
320 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
321 if (rstc->shared && ret)
322 atomic_dec(&rstc->triggered_count);
326 EXPORT_SYMBOL_GPL(reset_control_reset);
329 * reset_control_assert - asserts the reset line
330 * @rstc: reset controller
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.
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.
341 * If rstc is NULL it is an optional reset and the function will just
344 int reset_control_assert(struct reset_control *rstc)
349 if (WARN_ON(IS_ERR(rstc)))
352 if (reset_control_is_array(rstc))
353 return reset_control_array_assert(rstc_to_array(rstc));
356 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
359 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
362 if (atomic_dec_return(&rstc->deassert_count) != 0)
366 * Shared reset controls allow the reset line to be in any state
367 * after this call, so doing nothing is a valid option.
369 if (!rstc->rcdev->ops->assert)
373 * If the reset controller does not implement .assert(), there
374 * is no way to guarantee that the reset line is asserted after
377 if (!rstc->rcdev->ops->assert)
380 if (!rstc->acquired) {
381 WARN(1, "reset %s (ID: %u) is not acquired\n",
382 rcdev_name(rstc->rcdev), rstc->id);
387 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
389 EXPORT_SYMBOL_GPL(reset_control_assert);
392 * reset_control_deassert - deasserts the reset line
393 * @rstc: reset controller
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.
399 * If rstc is NULL it is an optional reset and the function will just
402 int reset_control_deassert(struct reset_control *rstc)
407 if (WARN_ON(IS_ERR(rstc)))
410 if (reset_control_is_array(rstc))
411 return reset_control_array_deassert(rstc_to_array(rstc));
414 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
417 if (atomic_inc_return(&rstc->deassert_count) != 1)
420 if (!rstc->acquired) {
421 WARN(1, "reset %s (ID: %u) is not acquired\n",
422 rcdev_name(rstc->rcdev), rstc->id);
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
434 if (!rstc->rcdev->ops->deassert)
437 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
439 EXPORT_SYMBOL_GPL(reset_control_deassert);
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
447 int reset_control_status(struct reset_control *rstc)
452 if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
455 if (rstc->rcdev->ops->status)
456 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
460 EXPORT_SYMBOL_GPL(reset_control_status);
463 * reset_control_acquire() - acquires a reset control for exclusive use
464 * @rstc: reset control
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.
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.
480 * See also: reset_control_release()
482 int reset_control_acquire(struct reset_control *rstc)
484 struct reset_control *rc;
489 if (WARN_ON(IS_ERR(rstc)))
492 if (reset_control_is_array(rstc))
493 return reset_control_array_acquire(rstc_to_array(rstc));
495 mutex_lock(&reset_list_mutex);
497 if (rstc->acquired) {
498 mutex_unlock(&reset_list_mutex);
502 list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) {
503 if (rstc != rc && rstc->id == rc->id) {
505 mutex_unlock(&reset_list_mutex);
511 rstc->acquired = true;
513 mutex_unlock(&reset_list_mutex);
516 EXPORT_SYMBOL_GPL(reset_control_acquire);
519 * reset_control_release() - releases exclusive access to a reset control
520 * @rstc: reset control
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.
526 * See also: reset_control_acquire()
528 void reset_control_release(struct reset_control *rstc)
530 if (!rstc || WARN_ON(IS_ERR(rstc)))
533 if (reset_control_is_array(rstc))
534 reset_control_array_release(rstc_to_array(rstc));
536 rstc->acquired = false;
538 EXPORT_SYMBOL_GPL(reset_control_release);
540 static struct reset_control *__reset_control_get_internal(
541 struct reset_controller_dev *rcdev,
542 unsigned int index, bool shared, bool acquired)
544 struct reset_control *rstc;
546 lockdep_assert_held(&reset_list_mutex);
548 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
549 if (rstc->id == index) {
551 * Allow creating a secondary exclusive reset_control
552 * that is initially not acquired for an already
553 * controlled reset line.
555 if (!rstc->shared && !shared && !acquired)
558 if (WARN_ON(!rstc->shared || !shared))
559 return ERR_PTR(-EBUSY);
561 kref_get(&rstc->refcnt);
566 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
568 return ERR_PTR(-ENOMEM);
570 try_module_get(rcdev->owner);
573 list_add(&rstc->list, &rcdev->reset_control_head);
575 kref_init(&rstc->refcnt);
576 rstc->acquired = acquired;
577 rstc->shared = shared;
582 static void __reset_control_release(struct kref *kref)
584 struct reset_control *rstc = container_of(kref, struct reset_control,
587 lockdep_assert_held(&reset_list_mutex);
589 module_put(rstc->rcdev->owner);
591 list_del(&rstc->list);
595 static void __reset_control_put_internal(struct reset_control *rstc)
597 lockdep_assert_held(&reset_list_mutex);
599 kref_put(&rstc->refcnt, __reset_control_release);
602 struct reset_control *__of_reset_control_get(struct device_node *node,
603 const char *id, int index, bool shared,
604 bool optional, bool acquired)
606 struct reset_control *rstc;
607 struct reset_controller_dev *r, *rcdev;
608 struct of_phandle_args args;
613 return ERR_PTR(-EINVAL);
616 index = of_property_match_string(node,
618 if (index == -EILSEQ)
619 return ERR_PTR(index);
621 return optional ? NULL : ERR_PTR(-ENOENT);
624 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
629 return optional ? NULL : ERR_PTR(ret);
631 mutex_lock(&reset_list_mutex);
633 list_for_each_entry(r, &reset_controller_list, list) {
634 if (args.np == r->of_node) {
641 rstc = ERR_PTR(-EPROBE_DEFER);
645 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
646 rstc = ERR_PTR(-EINVAL);
650 rstc_id = rcdev->of_xlate(rcdev, &args);
652 rstc = ERR_PTR(rstc_id);
656 /* reset_list_mutex also protects the rcdev's reset_control list */
657 rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
660 mutex_unlock(&reset_list_mutex);
661 of_node_put(args.np);
665 EXPORT_SYMBOL_GPL(__of_reset_control_get);
667 static struct reset_controller_dev *
668 __reset_controller_by_name(const char *name)
670 struct reset_controller_dev *rcdev;
672 lockdep_assert_held(&reset_list_mutex);
674 list_for_each_entry(rcdev, &reset_controller_list, list) {
678 if (!strcmp(name, dev_name(rcdev->dev)))
685 static struct reset_control *
686 __reset_control_get_from_lookup(struct device *dev, const char *con_id,
687 bool shared, bool optional, bool acquired)
689 const struct reset_control_lookup *lookup;
690 struct reset_controller_dev *rcdev;
691 const char *dev_id = dev_name(dev);
692 struct reset_control *rstc = NULL;
694 mutex_lock(&reset_lookup_mutex);
696 list_for_each_entry(lookup, &reset_lookup_list, list) {
697 if (strcmp(lookup->dev_id, dev_id))
700 if ((!con_id && !lookup->con_id) ||
701 ((con_id && lookup->con_id) &&
702 !strcmp(con_id, lookup->con_id))) {
703 mutex_lock(&reset_list_mutex);
704 rcdev = __reset_controller_by_name(lookup->provider);
706 mutex_unlock(&reset_list_mutex);
707 mutex_unlock(&reset_lookup_mutex);
708 /* Reset provider may not be ready yet. */
709 return ERR_PTR(-EPROBE_DEFER);
712 rstc = __reset_control_get_internal(rcdev,
715 mutex_unlock(&reset_list_mutex);
720 mutex_unlock(&reset_lookup_mutex);
723 return optional ? NULL : ERR_PTR(-ENOENT);
728 struct reset_control *__reset_control_get(struct device *dev, const char *id,
729 int index, bool shared, bool optional,
732 if (WARN_ON(shared && acquired))
733 return ERR_PTR(-EINVAL);
736 return __of_reset_control_get(dev->of_node, id, index, shared,
739 return __reset_control_get_from_lookup(dev, id, shared, optional,
742 EXPORT_SYMBOL_GPL(__reset_control_get);
744 static void reset_control_array_put(struct reset_control_array *resets)
748 mutex_lock(&reset_list_mutex);
749 for (i = 0; i < resets->num_rstcs; i++)
750 __reset_control_put_internal(resets->rstc[i]);
751 mutex_unlock(&reset_list_mutex);
756 * reset_control_put - free the reset controller
757 * @rstc: reset controller
759 void reset_control_put(struct reset_control *rstc)
761 if (IS_ERR_OR_NULL(rstc))
764 if (reset_control_is_array(rstc)) {
765 reset_control_array_put(rstc_to_array(rstc));
769 mutex_lock(&reset_list_mutex);
770 __reset_control_put_internal(rstc);
771 mutex_unlock(&reset_list_mutex);
773 EXPORT_SYMBOL_GPL(reset_control_put);
775 static void devm_reset_control_release(struct device *dev, void *res)
777 reset_control_put(*(struct reset_control **)res);
780 struct reset_control *__devm_reset_control_get(struct device *dev,
781 const char *id, int index, bool shared,
782 bool optional, bool acquired)
784 struct reset_control **ptr, *rstc;
786 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
789 return ERR_PTR(-ENOMEM);
791 rstc = __reset_control_get(dev, id, index, shared, optional, acquired);
792 if (IS_ERR_OR_NULL(rstc)) {
798 devres_add(dev, ptr);
802 EXPORT_SYMBOL_GPL(__devm_reset_control_get);
805 * device_reset - find reset controller associated with the device
807 * @dev: device to be reset by the controller
808 * @optional: whether it is optional to reset the device
810 * Convenience wrapper for __reset_control_get() and reset_control_reset().
811 * This is useful for the common case of devices with single, dedicated reset
814 int __device_reset(struct device *dev, bool optional)
816 struct reset_control *rstc;
819 rstc = __reset_control_get(dev, NULL, 0, 0, optional, true);
821 return PTR_ERR(rstc);
823 ret = reset_control_reset(rstc);
825 reset_control_put(rstc);
829 EXPORT_SYMBOL_GPL(__device_reset);
832 * APIs to manage an array of reset controls.
836 * of_reset_control_get_count - Count number of resets available with a device
838 * @node: device node that contains 'resets'.
840 * Returns positive reset count on success, or error number on failure and
841 * on count being zero.
843 static int of_reset_control_get_count(struct device_node *node)
850 count = of_count_phandle_with_args(node, "resets", "#reset-cells");
858 * of_reset_control_array_get - Get a list of reset controls using
861 * @np: device node for the device that requests the reset controls array
862 * @shared: whether reset controls are shared or not
863 * @optional: whether it is optional to get the reset controls
864 * @acquired: only one reset control may be acquired for a given controller
867 * Returns pointer to allocated reset_control on success or error on failure
869 struct reset_control *
870 of_reset_control_array_get(struct device_node *np, bool shared, bool optional,
873 struct reset_control_array *resets;
874 struct reset_control *rstc;
877 num = of_reset_control_get_count(np);
879 return optional ? NULL : ERR_PTR(num);
881 resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL);
883 return ERR_PTR(-ENOMEM);
885 for (i = 0; i < num; i++) {
886 rstc = __of_reset_control_get(np, NULL, i, shared, optional,
890 resets->rstc[i] = rstc;
892 resets->num_rstcs = num;
893 resets->base.array = true;
895 return &resets->base;
898 mutex_lock(&reset_list_mutex);
900 __reset_control_put_internal(resets->rstc[i]);
901 mutex_unlock(&reset_list_mutex);
907 EXPORT_SYMBOL_GPL(of_reset_control_array_get);
910 * devm_reset_control_array_get - Resource managed reset control array get
912 * @dev: device that requests the list of reset controls
913 * @shared: whether reset controls are shared or not
914 * @optional: whether it is optional to get the reset controls
916 * The reset control array APIs are intended for a list of resets
917 * that just have to be asserted or deasserted, without any
918 * requirements on the order.
920 * Returns pointer to allocated reset_control on success or error on failure
922 struct reset_control *
923 devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
925 struct reset_control **ptr, *rstc;
927 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
930 return ERR_PTR(-ENOMEM);
932 rstc = of_reset_control_array_get(dev->of_node, shared, optional, true);
933 if (IS_ERR_OR_NULL(rstc)) {
939 devres_add(dev, ptr);
943 EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
945 static int reset_control_get_count_from_lookup(struct device *dev)
947 const struct reset_control_lookup *lookup;
954 dev_id = dev_name(dev);
955 mutex_lock(&reset_lookup_mutex);
957 list_for_each_entry(lookup, &reset_lookup_list, list) {
958 if (!strcmp(lookup->dev_id, dev_id))
962 mutex_unlock(&reset_lookup_mutex);
971 * reset_control_get_count - Count number of resets available with a device
973 * @dev: device for which to return the number of resets
975 * Returns positive reset count on success, or error number on failure and
976 * on count being zero.
978 int reset_control_get_count(struct device *dev)
981 return of_reset_control_get_count(dev->of_node);
983 return reset_control_get_count_from_lookup(dev);
985 EXPORT_SYMBOL_GPL(reset_control_get_count);