2 * Reset Controller framework
4 * Copyright 2013 Philipp Zabel, Pengutronix
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 #include <linux/atomic.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
16 #include <linux/kref.h>
17 #include <linux/module.h>
19 #include <linux/reset.h>
20 #include <linux/reset-controller.h>
21 #include <linux/slab.h>
23 static DEFINE_MUTEX(reset_list_mutex);
24 static LIST_HEAD(reset_controller_list);
26 static DEFINE_MUTEX(reset_lookup_mutex);
27 static LIST_HEAD(reset_lookup_list);
30 * struct reset_control - a reset control
31 * @rcdev: a pointer to the reset controller device
32 * this reset control belongs to
33 * @list: list entry for the rcdev's reset controller list
34 * @id: ID of the reset controller in the reset
36 * @refcnt: Number of gets of this reset_control
37 * @acquired: Only one reset_control may be acquired for a given rcdev and id.
38 * @shared: Is this a shared (1), or an exclusive (0) reset_control?
39 * @deassert_cnt: Number of times this reset line has been deasserted
40 * @triggered_count: Number of times this reset line has been reset. Currently
41 * only used for shared resets, which means that the value
42 * will be either 0 or 1.
44 struct reset_control {
45 struct reset_controller_dev *rcdev;
46 struct list_head list;
52 atomic_t deassert_count;
53 atomic_t triggered_count;
57 * struct reset_control_array - an array of reset controls
58 * @base: reset control for compatibility with reset control API functions
59 * @num_rstcs: number of reset controls
60 * @rstc: array of reset controls
62 struct reset_control_array {
63 struct reset_control base;
64 unsigned int num_rstcs;
65 struct reset_control *rstc[];
68 static const char *rcdev_name(struct reset_controller_dev *rcdev)
71 return dev_name(rcdev->dev);
74 return rcdev->of_node->full_name;
80 * of_reset_simple_xlate - translate reset_spec to the reset line number
81 * @rcdev: a pointer to the reset controller device
82 * @reset_spec: reset line specifier as found in the device tree
83 * @flags: a flags pointer to fill in (optional)
85 * This simple translation function should be used for reset controllers
86 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
88 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
89 const struct of_phandle_args *reset_spec)
91 if (reset_spec->args[0] >= rcdev->nr_resets)
94 return reset_spec->args[0];
98 * reset_controller_register - register a reset controller device
99 * @rcdev: a pointer to the initialized reset controller device
101 int reset_controller_register(struct reset_controller_dev *rcdev)
103 if (!rcdev->of_xlate) {
104 rcdev->of_reset_n_cells = 1;
105 rcdev->of_xlate = of_reset_simple_xlate;
108 INIT_LIST_HEAD(&rcdev->reset_control_head);
110 mutex_lock(&reset_list_mutex);
111 list_add(&rcdev->list, &reset_controller_list);
112 mutex_unlock(&reset_list_mutex);
116 EXPORT_SYMBOL_GPL(reset_controller_register);
119 * reset_controller_unregister - unregister a reset controller device
120 * @rcdev: a pointer to the reset controller device
122 void reset_controller_unregister(struct reset_controller_dev *rcdev)
124 mutex_lock(&reset_list_mutex);
125 list_del(&rcdev->list);
126 mutex_unlock(&reset_list_mutex);
128 EXPORT_SYMBOL_GPL(reset_controller_unregister);
130 static void devm_reset_controller_release(struct device *dev, void *res)
132 reset_controller_unregister(*(struct reset_controller_dev **)res);
136 * devm_reset_controller_register - resource managed reset_controller_register()
137 * @dev: device that is registering this reset controller
138 * @rcdev: a pointer to the initialized reset controller device
140 * Managed reset_controller_register(). For reset controllers registered by
141 * this function, reset_controller_unregister() is automatically called on
142 * driver detach. See reset_controller_register() for more information.
144 int devm_reset_controller_register(struct device *dev,
145 struct reset_controller_dev *rcdev)
147 struct reset_controller_dev **rcdevp;
150 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
155 ret = reset_controller_register(rcdev);
158 devres_add(dev, rcdevp);
165 EXPORT_SYMBOL_GPL(devm_reset_controller_register);
168 * reset_controller_add_lookup - register a set of lookup entries
169 * @lookup: array of reset lookup entries
170 * @num_entries: number of entries in the lookup array
172 void reset_controller_add_lookup(struct reset_control_lookup *lookup,
173 unsigned int num_entries)
175 struct reset_control_lookup *entry;
178 mutex_lock(&reset_lookup_mutex);
179 for (i = 0; i < num_entries; i++) {
182 if (!entry->dev_id || !entry->provider) {
183 pr_warn("%s(): reset lookup entry badly specified, skipping\n",
188 list_add_tail(&entry->list, &reset_lookup_list);
190 mutex_unlock(&reset_lookup_mutex);
192 EXPORT_SYMBOL_GPL(reset_controller_add_lookup);
194 static inline struct reset_control_array *
195 rstc_to_array(struct reset_control *rstc) {
196 return container_of(rstc, struct reset_control_array, base);
199 static int reset_control_array_reset(struct reset_control_array *resets)
203 for (i = 0; i < resets->num_rstcs; i++) {
204 ret = reset_control_reset(resets->rstc[i]);
212 static int reset_control_array_assert(struct reset_control_array *resets)
216 for (i = 0; i < resets->num_rstcs; i++) {
217 ret = reset_control_assert(resets->rstc[i]);
226 reset_control_deassert(resets->rstc[i]);
230 static int reset_control_array_deassert(struct reset_control_array *resets)
234 for (i = 0; i < resets->num_rstcs; i++) {
235 ret = reset_control_deassert(resets->rstc[i]);
244 reset_control_assert(resets->rstc[i]);
248 static int reset_control_array_acquire(struct reset_control_array *resets)
253 for (i = 0; i < resets->num_rstcs; i++) {
254 err = reset_control_acquire(resets->rstc[i]);
263 reset_control_release(resets->rstc[i]);
268 static void reset_control_array_release(struct reset_control_array *resets)
272 for (i = 0; i < resets->num_rstcs; i++)
273 reset_control_release(resets->rstc[i]);
276 static inline bool reset_control_is_array(struct reset_control *rstc)
282 * reset_control_reset - reset the controlled device
283 * @rstc: reset controller
285 * On a shared reset line the actual reset pulse is only triggered once for the
286 * lifetime of the reset_control instance: for all but the first caller this is
288 * Consumers must not use reset_control_(de)assert on shared reset lines when
289 * reset_control_reset has been used.
291 * If rstc is NULL it is an optional reset and the function will just
294 int reset_control_reset(struct reset_control *rstc)
301 if (WARN_ON(IS_ERR(rstc)))
304 if (reset_control_is_array(rstc))
305 return reset_control_array_reset(rstc_to_array(rstc));
307 if (!rstc->rcdev->ops->reset)
311 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
314 if (atomic_inc_return(&rstc->triggered_count) != 1)
321 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
322 if (rstc->shared && ret)
323 atomic_dec(&rstc->triggered_count);
327 EXPORT_SYMBOL_GPL(reset_control_reset);
330 * reset_control_assert - asserts the reset line
331 * @rstc: reset controller
333 * Calling this on an exclusive reset controller guarantees that the reset
334 * will be asserted. When called on a shared reset controller the line may
335 * still be deasserted, as long as other users keep it so.
337 * For shared reset controls a driver cannot expect the hw's registers and
338 * internal state to be reset, but must be prepared for this to happen.
339 * Consumers must not use reset_control_reset on shared reset lines when
340 * reset_control_(de)assert has been used.
343 * If rstc is NULL it is an optional reset and the function will just
346 int reset_control_assert(struct reset_control *rstc)
351 if (WARN_ON(IS_ERR(rstc)))
354 if (reset_control_is_array(rstc))
355 return reset_control_array_assert(rstc_to_array(rstc));
358 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
361 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
364 if (atomic_dec_return(&rstc->deassert_count) != 0)
368 * Shared reset controls allow the reset line to be in any state
369 * after this call, so doing nothing is a valid option.
371 if (!rstc->rcdev->ops->assert)
375 * If the reset controller does not implement .assert(), there
376 * is no way to guarantee that the reset line is asserted after
379 if (!rstc->rcdev->ops->assert)
382 if (!rstc->acquired) {
383 WARN(1, "reset %s (ID: %u) is not acquired\n",
384 rcdev_name(rstc->rcdev), rstc->id);
389 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
391 EXPORT_SYMBOL_GPL(reset_control_assert);
394 * reset_control_deassert - deasserts the reset line
395 * @rstc: reset controller
397 * After calling this function, the reset is guaranteed to be deasserted.
398 * Consumers must not use reset_control_reset on shared reset lines when
399 * reset_control_(de)assert has been used.
402 * If rstc is NULL it is an optional reset and the function will just
405 int reset_control_deassert(struct reset_control *rstc)
410 if (WARN_ON(IS_ERR(rstc)))
413 if (reset_control_is_array(rstc))
414 return reset_control_array_deassert(rstc_to_array(rstc));
417 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
420 if (atomic_inc_return(&rstc->deassert_count) != 1)
423 if (!rstc->acquired) {
424 WARN(1, "reset %s (ID: %u) is not acquired\n",
425 rcdev_name(rstc->rcdev), rstc->id);
431 * If the reset controller does not implement .deassert(), we assume
432 * that it handles self-deasserting reset lines via .reset(). In that
433 * case, the reset lines are deasserted by default. If that is not the
434 * case, the reset controller driver should implement .deassert() and
437 if (!rstc->rcdev->ops->deassert)
440 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
442 EXPORT_SYMBOL_GPL(reset_control_deassert);
445 * reset_control_status - returns a negative errno if not supported, a
446 * positive value if the reset line is asserted, or zero if the reset
447 * line is not asserted or if the desc is NULL (optional reset).
448 * @rstc: reset controller
450 int reset_control_status(struct reset_control *rstc)
455 if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
458 if (rstc->rcdev->ops->status)
459 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
463 EXPORT_SYMBOL_GPL(reset_control_status);
466 * reset_control_acquire() - acquires a reset control for exclusive use
467 * @rstc: reset control
469 * This is used to explicitly acquire a reset control for exclusive use. Note
470 * that exclusive resets are requested as acquired by default. In order for a
471 * second consumer to be able to control the reset, the first consumer has to
472 * release it first. Typically the easiest way to achieve this is to call the
473 * reset_control_get_exclusive_released() to obtain an instance of the reset
474 * control. Such reset controls are not acquired by default.
476 * Consumers implementing shared access to an exclusive reset need to follow
477 * a specific protocol in order to work together. Before consumers can change
478 * a reset they must acquire exclusive access using reset_control_acquire().
479 * After they are done operating the reset, they must release exclusive access
480 * with a call to reset_control_release(). Consumers are not granted exclusive
481 * access to the reset as long as another consumer hasn't released a reset.
483 * See also: reset_control_release()
485 int reset_control_acquire(struct reset_control *rstc)
487 struct reset_control *rc;
492 if (WARN_ON(IS_ERR(rstc)))
495 if (reset_control_is_array(rstc))
496 return reset_control_array_acquire(rstc_to_array(rstc));
498 mutex_lock(&reset_list_mutex);
500 if (rstc->acquired) {
501 mutex_unlock(&reset_list_mutex);
505 list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) {
506 if (rstc != rc && rstc->id == rc->id) {
508 mutex_unlock(&reset_list_mutex);
514 rstc->acquired = true;
516 mutex_unlock(&reset_list_mutex);
519 EXPORT_SYMBOL_GPL(reset_control_acquire);
522 * reset_control_release() - releases exclusive access to a reset control
523 * @rstc: reset control
525 * Releases exclusive access right to a reset control previously obtained by a
526 * call to reset_control_acquire(). Until a consumer calls this function, no
527 * other consumers will be granted exclusive access.
529 * See also: reset_control_acquire()
531 void reset_control_release(struct reset_control *rstc)
533 if (!rstc || WARN_ON(IS_ERR(rstc)))
536 if (reset_control_is_array(rstc))
537 reset_control_array_release(rstc_to_array(rstc));
539 rstc->acquired = false;
541 EXPORT_SYMBOL_GPL(reset_control_release);
543 static struct reset_control *__reset_control_get_internal(
544 struct reset_controller_dev *rcdev,
545 unsigned int index, bool shared, bool acquired)
547 struct reset_control *rstc;
549 lockdep_assert_held(&reset_list_mutex);
551 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
552 if (rstc->id == index) {
554 * Allow creating a secondary exclusive reset_control
555 * that is initially not acquired for an already
556 * controlled reset line.
558 if (!rstc->shared && !shared && !acquired)
561 if (WARN_ON(!rstc->shared || !shared))
562 return ERR_PTR(-EBUSY);
564 kref_get(&rstc->refcnt);
569 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
571 return ERR_PTR(-ENOMEM);
573 try_module_get(rcdev->owner);
576 list_add(&rstc->list, &rcdev->reset_control_head);
578 kref_init(&rstc->refcnt);
579 rstc->acquired = acquired;
580 rstc->shared = shared;
585 static void __reset_control_release(struct kref *kref)
587 struct reset_control *rstc = container_of(kref, struct reset_control,
590 lockdep_assert_held(&reset_list_mutex);
592 module_put(rstc->rcdev->owner);
594 list_del(&rstc->list);
598 static void __reset_control_put_internal(struct reset_control *rstc)
600 lockdep_assert_held(&reset_list_mutex);
602 kref_put(&rstc->refcnt, __reset_control_release);
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)
609 struct reset_control *rstc;
610 struct reset_controller_dev *r, *rcdev;
611 struct of_phandle_args args;
616 return ERR_PTR(-EINVAL);
619 index = of_property_match_string(node,
621 if (index == -EILSEQ)
622 return ERR_PTR(index);
624 return optional ? NULL : ERR_PTR(-ENOENT);
627 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
632 return optional ? NULL : ERR_PTR(ret);
634 mutex_lock(&reset_list_mutex);
636 list_for_each_entry(r, &reset_controller_list, list) {
637 if (args.np == r->of_node) {
644 rstc = ERR_PTR(-EPROBE_DEFER);
648 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
649 rstc = ERR_PTR(-EINVAL);
653 rstc_id = rcdev->of_xlate(rcdev, &args);
655 rstc = ERR_PTR(rstc_id);
659 /* reset_list_mutex also protects the rcdev's reset_control list */
660 rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
663 mutex_unlock(&reset_list_mutex);
664 of_node_put(args.np);
668 EXPORT_SYMBOL_GPL(__of_reset_control_get);
670 static struct reset_controller_dev *
671 __reset_controller_by_name(const char *name)
673 struct reset_controller_dev *rcdev;
675 lockdep_assert_held(&reset_list_mutex);
677 list_for_each_entry(rcdev, &reset_controller_list, list) {
681 if (!strcmp(name, dev_name(rcdev->dev)))
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)
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;
698 return ERR_PTR(-EINVAL);
700 mutex_lock(&reset_lookup_mutex);
702 list_for_each_entry(lookup, &reset_lookup_list, list) {
703 if (strcmp(lookup->dev_id, dev_id))
706 if ((!con_id && !lookup->con_id) ||
707 ((con_id && lookup->con_id) &&
708 !strcmp(con_id, lookup->con_id))) {
709 mutex_lock(&reset_list_mutex);
710 rcdev = __reset_controller_by_name(lookup->provider);
712 mutex_unlock(&reset_list_mutex);
713 mutex_unlock(&reset_lookup_mutex);
714 /* Reset provider may not be ready yet. */
715 return ERR_PTR(-EPROBE_DEFER);
718 rstc = __reset_control_get_internal(rcdev,
721 mutex_unlock(&reset_list_mutex);
726 mutex_unlock(&reset_lookup_mutex);
729 return optional ? NULL : ERR_PTR(-ENOENT);
734 struct reset_control *__reset_control_get(struct device *dev, const char *id,
735 int index, bool shared, bool optional,
738 if (WARN_ON(shared && acquired))
739 return ERR_PTR(-EINVAL);
742 return __of_reset_control_get(dev->of_node, id, index, shared,
745 return __reset_control_get_from_lookup(dev, id, shared, optional,
748 EXPORT_SYMBOL_GPL(__reset_control_get);
750 static void reset_control_array_put(struct reset_control_array *resets)
754 mutex_lock(&reset_list_mutex);
755 for (i = 0; i < resets->num_rstcs; i++)
756 __reset_control_put_internal(resets->rstc[i]);
757 mutex_unlock(&reset_list_mutex);
761 * reset_control_put - free the reset controller
762 * @rstc: reset controller
764 void reset_control_put(struct reset_control *rstc)
766 if (IS_ERR_OR_NULL(rstc))
769 if (reset_control_is_array(rstc)) {
770 reset_control_array_put(rstc_to_array(rstc));
774 mutex_lock(&reset_list_mutex);
775 __reset_control_put_internal(rstc);
776 mutex_unlock(&reset_list_mutex);
778 EXPORT_SYMBOL_GPL(reset_control_put);
780 static void devm_reset_control_release(struct device *dev, void *res)
782 reset_control_put(*(struct reset_control **)res);
785 struct reset_control *__devm_reset_control_get(struct device *dev,
786 const char *id, int index, bool shared,
787 bool optional, bool acquired)
789 struct reset_control **ptr, *rstc;
791 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
794 return ERR_PTR(-ENOMEM);
796 rstc = __reset_control_get(dev, id, index, shared, optional, acquired);
799 devres_add(dev, ptr);
806 EXPORT_SYMBOL_GPL(__devm_reset_control_get);
809 * device_reset - find reset controller associated with the device
811 * @dev: device to be reset by the controller
812 * @optional: whether it is optional to reset the device
814 * Convenience wrapper for __reset_control_get() and reset_control_reset().
815 * This is useful for the common case of devices with single, dedicated reset
818 int __device_reset(struct device *dev, bool optional)
820 struct reset_control *rstc;
823 rstc = __reset_control_get(dev, NULL, 0, 0, optional, true);
825 return PTR_ERR(rstc);
827 ret = reset_control_reset(rstc);
829 reset_control_put(rstc);
833 EXPORT_SYMBOL_GPL(__device_reset);
836 * APIs to manage an array of reset controls.
839 * of_reset_control_get_count - Count number of resets available with a device
841 * @node: device node that contains 'resets'.
843 * Returns positive reset count on success, or error number on failure and
844 * on count being zero.
846 static int of_reset_control_get_count(struct device_node *node)
853 count = of_count_phandle_with_args(node, "resets", "#reset-cells");
861 * of_reset_control_array_get - Get a list of reset controls using
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
870 * Returns pointer to allocated reset_control_array on success or
873 struct reset_control *
874 of_reset_control_array_get(struct device_node *np, bool shared, bool optional,
877 struct reset_control_array *resets;
878 struct reset_control *rstc;
881 num = of_reset_control_get_count(np);
883 return optional ? NULL : ERR_PTR(num);
885 resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL);
887 return ERR_PTR(-ENOMEM);
889 for (i = 0; i < num; i++) {
890 rstc = __of_reset_control_get(np, NULL, i, shared, optional,
894 resets->rstc[i] = rstc;
896 resets->num_rstcs = num;
897 resets->base.array = true;
899 return &resets->base;
902 mutex_lock(&reset_list_mutex);
904 __reset_control_put_internal(resets->rstc[i]);
905 mutex_unlock(&reset_list_mutex);
911 EXPORT_SYMBOL_GPL(of_reset_control_array_get);
914 * devm_reset_control_array_get - Resource managed reset control array get
916 * @dev: device that requests the list of reset controls
917 * @shared: whether reset controls are shared or not
918 * @optional: whether it is optional to get the reset controls
920 * The reset control array APIs are intended for a list of resets
921 * that just have to be asserted or deasserted, without any
922 * requirements on the order.
924 * Returns pointer to allocated reset_control_array on success or
927 struct reset_control *
928 devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
930 struct reset_control **devres;
931 struct reset_control *rstc;
933 devres = devres_alloc(devm_reset_control_release, sizeof(*devres),
936 return ERR_PTR(-ENOMEM);
938 rstc = of_reset_control_array_get(dev->of_node, shared, optional, true);
945 devres_add(dev, devres);
949 EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
951 static int reset_control_get_count_from_lookup(struct device *dev)
953 const struct reset_control_lookup *lookup;
960 dev_id = dev_name(dev);
961 mutex_lock(&reset_lookup_mutex);
963 list_for_each_entry(lookup, &reset_lookup_list, list) {
964 if (!strcmp(lookup->dev_id, dev_id))
968 mutex_unlock(&reset_lookup_mutex);
977 * reset_control_get_count - Count number of resets available with a device
979 * @dev: device for which to return the number of resets
981 * Returns positive reset count on success, or error number on failure and
982 * on count being zero.
984 int reset_control_get_count(struct device *dev)
987 return of_reset_control_get_count(dev->of_node);
989 return reset_control_get_count_from_lookup(dev);
991 EXPORT_SYMBOL_GPL(reset_control_get_count);