Lines Matching +full:reset +full:- +full:controller

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Reset Controller framework
16 #include <linux/reset.h>
17 #include <linux/reset-controller.h>
27 * struct reset_control - a reset control
28 * @rcdev: a pointer to the reset controller device
29 * this reset control belongs to
30 * @list: list entry for the rcdev's reset controller list
31 * @id: ID of the reset controller in the reset
32 * controller device
36 * @array: Is this an array of reset controls (1)?
37 * @deassert_count: Number of times this reset line has been deasserted
38 * @triggered_count: Number of times this reset line has been reset. Currently
55 * struct reset_control_array - an array of reset controls
56 * @base: reset control for compatibility with reset control API functions
57 * @num_rstcs: number of reset controls
58 * @rstc: array of reset controls
68 if (rcdev->dev) in rcdev_name()
69 return dev_name(rcdev->dev); in rcdev_name()
71 if (rcdev->of_node) in rcdev_name()
72 return rcdev->of_node->full_name; in rcdev_name()
78 * of_reset_simple_xlate - translate reset_spec to the reset line number
79 * @rcdev: a pointer to the reset controller device
80 * @reset_spec: reset line specifier as found in the device tree
83 * :c:type:`reset_controller_dev` is not set. It is useful for all reset
84 * controllers with 1:1 mapping, where reset lines can be indexed by number
90 if (reset_spec->args[0] >= rcdev->nr_resets) in of_reset_simple_xlate()
91 return -EINVAL; in of_reset_simple_xlate()
93 return reset_spec->args[0]; in of_reset_simple_xlate()
97 * reset_controller_register - register a reset controller device
98 * @rcdev: a pointer to the initialized reset controller device
102 if (!rcdev->of_xlate) { in reset_controller_register()
103 rcdev->of_reset_n_cells = 1; in reset_controller_register()
104 rcdev->of_xlate = of_reset_simple_xlate; in reset_controller_register()
107 INIT_LIST_HEAD(&rcdev->reset_control_head); in reset_controller_register()
110 list_add(&rcdev->list, &reset_controller_list); in reset_controller_register()
118 * reset_controller_unregister - unregister a reset controller device
119 * @rcdev: a pointer to the reset controller device
124 list_del(&rcdev->list); in reset_controller_unregister()
135 * devm_reset_controller_register - resource managed reset_controller_register()
136 * @dev: device that is registering this reset controller
137 * @rcdev: a pointer to the initialized reset controller device
139 * Managed reset_controller_register(). For reset controllers registered by
152 return -ENOMEM; in devm_reset_controller_register()
168 * reset_controller_add_lookup - register a set of lookup entries
169 * @lookup: array of reset lookup entries
182 if (!entry->dev_id || !entry->provider) { in reset_controller_add_lookup()
183 pr_warn("%s(): reset lookup entry badly specified, skipping\n", in reset_controller_add_lookup()
188 list_add_tail(&entry->list, &reset_lookup_list); in reset_controller_add_lookup()
203 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_reset()
204 ret = reset_control_reset(resets->rstc[i]); in reset_control_array_reset()
217 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_rearm()
218 rstc = resets->rstc[i]; in reset_control_array_rearm()
224 return -EINVAL; in reset_control_array_rearm()
226 if (rstc->shared) { in reset_control_array_rearm()
227 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) in reset_control_array_rearm()
228 return -EINVAL; in reset_control_array_rearm()
230 if (!rstc->acquired) in reset_control_array_rearm()
231 return -EPERM; in reset_control_array_rearm()
235 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_rearm()
236 rstc = resets->rstc[i]; in reset_control_array_rearm()
238 if (rstc && rstc->shared) in reset_control_array_rearm()
239 WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0); in reset_control_array_rearm()
249 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_assert()
250 ret = reset_control_assert(resets->rstc[i]); in reset_control_array_assert()
258 while (i--) in reset_control_array_assert()
259 reset_control_deassert(resets->rstc[i]); in reset_control_array_assert()
267 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_deassert()
268 ret = reset_control_deassert(resets->rstc[i]); in reset_control_array_deassert()
276 while (i--) in reset_control_array_deassert()
277 reset_control_assert(resets->rstc[i]); in reset_control_array_deassert()
286 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_acquire()
287 err = reset_control_acquire(resets->rstc[i]); in reset_control_array_acquire()
295 while (i--) in reset_control_array_acquire()
296 reset_control_release(resets->rstc[i]); in reset_control_array_acquire()
305 for (i = 0; i < resets->num_rstcs; i++) in reset_control_array_release()
306 reset_control_release(resets->rstc[i]); in reset_control_array_release()
311 return rstc->array; in reset_control_is_array()
315 * reset_control_reset - reset the controlled device
316 * @rstc: reset controller
318 * On a shared reset line the actual reset pulse is only triggered once for the
320 * a no-op.
321 * Consumers must not use reset_control_(de)assert on shared reset lines when
324 * If rstc is NULL it is an optional reset and the function will just
335 return -EINVAL; in reset_control_reset()
340 if (!rstc->rcdev->ops->reset) in reset_control_reset()
341 return -ENOTSUPP; in reset_control_reset()
343 if (rstc->shared) { in reset_control_reset()
344 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) in reset_control_reset()
345 return -EINVAL; in reset_control_reset()
347 if (atomic_inc_return(&rstc->triggered_count) != 1) in reset_control_reset()
350 if (!rstc->acquired) in reset_control_reset()
351 return -EPERM; in reset_control_reset()
354 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); in reset_control_reset()
355 if (rstc->shared && ret) in reset_control_reset()
356 atomic_dec(&rstc->triggered_count); in reset_control_reset()
363 * reset_control_bulk_reset - reset the controlled devices in order
365 * @rstcs: array of struct reset_control_bulk_data with reset controls set
367 * Issue a reset on all provided reset controls, in order.
387 * reset_control_rearm - allow shared reset line to be re-triggered"
388 * @rstc: reset controller
390 * On a shared reset line the actual reset pulse is only triggered once for the
396 * Consumers must not use reset_control_(de)assert on shared reset lines when
407 return -EINVAL; in reset_control_rearm()
412 if (rstc->shared) { in reset_control_rearm()
413 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) in reset_control_rearm()
414 return -EINVAL; in reset_control_rearm()
416 WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0); in reset_control_rearm()
418 if (!rstc->acquired) in reset_control_rearm()
419 return -EPERM; in reset_control_rearm()
427 * reset_control_assert - asserts the reset line
428 * @rstc: reset controller
430 * Calling this on an exclusive reset controller guarantees that the reset
431 * will be asserted. When called on a shared reset controller the line may
434 * For shared reset controls a driver cannot expect the hw's registers and
435 * internal state to be reset, but must be prepared for this to happen.
436 * Consumers must not use reset_control_reset on shared reset lines when
439 * If rstc is NULL it is an optional reset and the function will just
448 return -EINVAL; in reset_control_assert()
453 if (rstc->shared) { in reset_control_assert()
454 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) in reset_control_assert()
455 return -EINVAL; in reset_control_assert()
457 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0)) in reset_control_assert()
458 return -EINVAL; in reset_control_assert()
460 if (atomic_dec_return(&rstc->deassert_count) != 0) in reset_control_assert()
464 * Shared reset controls allow the reset line to be in any state in reset_control_assert()
467 if (!rstc->rcdev->ops->assert) in reset_control_assert()
471 * If the reset controller does not implement .assert(), there in reset_control_assert()
472 * is no way to guarantee that the reset line is asserted after in reset_control_assert()
475 if (!rstc->rcdev->ops->assert) in reset_control_assert()
476 return -ENOTSUPP; in reset_control_assert()
478 if (!rstc->acquired) { in reset_control_assert()
479 WARN(1, "reset %s (ID: %u) is not acquired\n", in reset_control_assert()
480 rcdev_name(rstc->rcdev), rstc->id); in reset_control_assert()
481 return -EPERM; in reset_control_assert()
485 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); in reset_control_assert()
490 * reset_control_bulk_assert - asserts the reset lines in order
492 * @rstcs: array of struct reset_control_bulk_data with reset controls set
494 * Assert the reset lines for all provided reset controls, in order.
513 while (i--) in reset_control_bulk_assert()
520 * reset_control_deassert - deasserts the reset line
521 * @rstc: reset controller
523 * After calling this function, the reset is guaranteed to be deasserted.
524 * Consumers must not use reset_control_reset on shared reset lines when
527 * If rstc is NULL it is an optional reset and the function will just
536 return -EINVAL; in reset_control_deassert()
541 if (rstc->shared) { in reset_control_deassert()
542 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) in reset_control_deassert()
543 return -EINVAL; in reset_control_deassert()
545 if (atomic_inc_return(&rstc->deassert_count) != 1) in reset_control_deassert()
548 if (!rstc->acquired) { in reset_control_deassert()
549 WARN(1, "reset %s (ID: %u) is not acquired\n", in reset_control_deassert()
550 rcdev_name(rstc->rcdev), rstc->id); in reset_control_deassert()
551 return -EPERM; in reset_control_deassert()
556 * If the reset controller does not implement .deassert(), we assume in reset_control_deassert()
557 * that it handles self-deasserting reset lines via .reset(). In that in reset_control_deassert()
558 * case, the reset lines are deasserted by default. If that is not the in reset_control_deassert()
559 * case, the reset controller driver should implement .deassert() and in reset_control_deassert()
560 * return -ENOTSUPP. in reset_control_deassert()
562 if (!rstc->rcdev->ops->deassert) in reset_control_deassert()
565 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); in reset_control_deassert()
570 * reset_control_bulk_deassert - deasserts the reset lines in reverse order
572 * @rstcs: array of struct reset_control_bulk_data with reset controls set
574 * Deassert the reset lines for all provided reset controls, in reverse order.
584 for (i = num_rstcs - 1; i >= 0; i--) { in reset_control_bulk_deassert()
600 * reset_control_status - returns a negative errno if not supported, a
601 * positive value if the reset line is asserted, or zero if the reset
602 * line is not asserted or if the desc is NULL (optional reset).
603 * @rstc: reset controller
611 return -EINVAL; in reset_control_status()
613 if (rstc->rcdev->ops->status) in reset_control_status()
614 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); in reset_control_status()
616 return -ENOTSUPP; in reset_control_status()
621 * reset_control_acquire() - acquires a reset control for exclusive use
622 * @rstc: reset control
624 * This is used to explicitly acquire a reset control for exclusive use. Note
626 * second consumer to be able to control the reset, the first consumer has to
628 * reset_control_get_exclusive_released() to obtain an instance of the reset
629 * control. Such reset controls are not acquired by default.
631 * Consumers implementing shared access to an exclusive reset need to follow
633 * a reset they must acquire exclusive access using reset_control_acquire().
634 * After they are done operating the reset, they must release exclusive access
636 * access to the reset as long as another consumer hasn't released a reset.
648 return -EINVAL; in reset_control_acquire()
655 if (rstc->acquired) { in reset_control_acquire()
660 list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) { in reset_control_acquire()
661 if (rstc != rc && rstc->id == rc->id) { in reset_control_acquire()
662 if (rc->acquired) { in reset_control_acquire()
664 return -EBUSY; in reset_control_acquire()
669 rstc->acquired = true; in reset_control_acquire()
677 * reset_control_bulk_acquire - acquires reset controls for exclusive use
679 * @rstcs: array of struct reset_control_bulk_data with reset controls set
681 * This is used to explicitly acquire reset controls requested with
700 while (i--) in reset_control_bulk_acquire()
707 * reset_control_release() - releases exclusive access to a reset control
708 * @rstc: reset control
710 * Releases exclusive access right to a reset control previously obtained by a
724 rstc->acquired = false; in reset_control_release()
729 * reset_control_bulk_release() - releases exclusive access to reset controls
731 * @rstcs: array of struct reset_control_bulk_data with reset controls set
733 * Releases exclusive access right to reset controls previously obtained by a
756 list_for_each_entry(rstc, &rcdev->reset_control_head, list) { in __reset_control_get_internal()
757 if (rstc->id == index) { in __reset_control_get_internal()
761 * controlled reset line. in __reset_control_get_internal()
763 if (!rstc->shared && !shared && !acquired) in __reset_control_get_internal()
766 if (WARN_ON(!rstc->shared || !shared)) in __reset_control_get_internal()
767 return ERR_PTR(-EBUSY); in __reset_control_get_internal()
769 kref_get(&rstc->refcnt); in __reset_control_get_internal()
776 return ERR_PTR(-ENOMEM); in __reset_control_get_internal()
778 if (!try_module_get(rcdev->owner)) { in __reset_control_get_internal()
780 return ERR_PTR(-ENODEV); in __reset_control_get_internal()
783 rstc->rcdev = rcdev; in __reset_control_get_internal()
784 list_add(&rstc->list, &rcdev->reset_control_head); in __reset_control_get_internal()
785 rstc->id = index; in __reset_control_get_internal()
786 kref_init(&rstc->refcnt); in __reset_control_get_internal()
787 rstc->acquired = acquired; in __reset_control_get_internal()
788 rstc->shared = shared; in __reset_control_get_internal()
800 module_put(rstc->rcdev->owner); in __reset_control_release()
802 list_del(&rstc->list); in __reset_control_release()
813 kref_put(&rstc->refcnt, __reset_control_release); in __reset_control_put_internal()
827 return ERR_PTR(-EINVAL); in __of_reset_control_get()
831 "reset-names", id); in __of_reset_control_get()
832 if (index == -EILSEQ) in __of_reset_control_get()
835 return optional ? NULL : ERR_PTR(-ENOENT); in __of_reset_control_get()
838 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", in __of_reset_control_get()
840 if (ret == -EINVAL) in __of_reset_control_get()
848 if (args.np == r->of_node) { in __of_reset_control_get()
855 rstc = ERR_PTR(-EPROBE_DEFER); in __of_reset_control_get()
859 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { in __of_reset_control_get()
860 rstc = ERR_PTR(-EINVAL); in __of_reset_control_get()
864 rstc_id = rcdev->of_xlate(rcdev, &args); in __of_reset_control_get()
889 if (!rcdev->dev) in __reset_controller_by_name()
892 if (!strcmp(name, dev_name(rcdev->dev))) in __reset_controller_by_name()
911 if (strcmp(lookup->dev_id, dev_id)) in __reset_control_get_from_lookup()
914 if ((!con_id && !lookup->con_id) || in __reset_control_get_from_lookup()
915 ((con_id && lookup->con_id) && in __reset_control_get_from_lookup()
916 !strcmp(con_id, lookup->con_id))) { in __reset_control_get_from_lookup()
918 rcdev = __reset_controller_by_name(lookup->provider); in __reset_control_get_from_lookup()
922 /* Reset provider may not be ready yet. */ in __reset_control_get_from_lookup()
923 return ERR_PTR(-EPROBE_DEFER); in __reset_control_get_from_lookup()
927 lookup->index, in __reset_control_get_from_lookup()
937 return optional ? NULL : ERR_PTR(-ENOENT); in __reset_control_get_from_lookup()
947 return ERR_PTR(-EINVAL); in __reset_control_get()
949 if (dev->of_node) in __reset_control_get()
950 return __of_reset_control_get(dev->of_node, id, index, shared, in __reset_control_get()
977 while (i--) in __reset_control_bulk_get()
989 for (i = 0; i < resets->num_rstcs; i++) in reset_control_array_put()
990 __reset_control_put_internal(resets->rstc[i]); in reset_control_array_put()
996 * reset_control_put - free the reset controller
997 * @rstc: reset controller
1016 * reset_control_bulk_put - free the reset controllers
1018 * @rstcs: array of struct reset_control_bulk_data with reset controls set
1023 while (num_rstcs--) in reset_control_bulk_put()
1043 return ERR_PTR(-ENOMEM); in __devm_reset_control_get()
1067 reset_control_bulk_put(devres->num_rstcs, devres->rstcs); in devm_reset_control_bulk_release()
1080 return -ENOMEM; in __devm_reset_control_bulk_get()
1088 ptr->num_rstcs = num_rstcs; in __devm_reset_control_bulk_get()
1089 ptr->rstcs = rstcs; in __devm_reset_control_bulk_get()
1097 * __device_reset - find reset controller associated with the device
1098 * and perform reset
1099 * @dev: device to be reset by the controller
1100 * @optional: whether it is optional to reset the device
1103 * This is useful for the common case of devices with single, dedicated reset
1116 return optional ? 0 : -ENOENT; in __device_reset()
1119 return -EIO; in __device_reset()
1136 * APIs to manage an array of reset controls.
1140 * of_reset_control_get_count - Count number of resets available with a device
1144 * Returns positive reset count on success, or error number on failure and
1152 return -EINVAL; in of_reset_control_get_count()
1154 count = of_count_phandle_with_args(node, "resets", "#reset-cells"); in of_reset_control_get_count()
1156 count = -ENOENT; in of_reset_control_get_count()
1162 * of_reset_control_array_get - Get a list of reset controls using
1165 * @np: device node for the device that requests the reset controls array
1166 * @shared: whether reset controls are shared or not
1167 * @optional: whether it is optional to get the reset controls
1168 * @acquired: only one reset control may be acquired for a given controller
1187 return ERR_PTR(-ENOMEM); in of_reset_control_array_get()
1188 resets->num_rstcs = num; in of_reset_control_array_get()
1195 resets->rstc[i] = rstc; in of_reset_control_array_get()
1197 resets->base.array = true; in of_reset_control_array_get()
1199 return &resets->base; in of_reset_control_array_get()
1203 while (--i >= 0) in of_reset_control_array_get()
1204 __reset_control_put_internal(resets->rstc[i]); in of_reset_control_array_get()
1214 * devm_reset_control_array_get - Resource managed reset control array get
1216 * @dev: device that requests the list of reset controls
1217 * @shared: whether reset controls are shared or not
1218 * @optional: whether it is optional to get the reset controls
1220 * The reset control array APIs are intended for a list of resets
1234 return ERR_PTR(-ENOMEM); in devm_reset_control_array_get()
1236 rstc = of_reset_control_array_get(dev->of_node, shared, optional, true); in devm_reset_control_array_get()
1256 return -EINVAL; in reset_control_get_count_from_lookup()
1262 if (!strcmp(lookup->dev_id, dev_id)) in reset_control_get_count_from_lookup()
1269 count = -ENOENT; in reset_control_get_count_from_lookup()
1275 * reset_control_get_count - Count number of resets available with a device
1279 * Returns positive reset count on success, or error number on failure and
1284 if (dev->of_node) in reset_control_get_count()
1285 return of_reset_control_get_count(dev->of_node); in reset_control_get_count()