Lines Matching +full:gpio +full:- +full:reset

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Reset Controller framework
14 #include <linux/gpio/driver.h>
15 #include <linux/gpio/machine.h>
21 #include <linux/reset.h>
22 #include <linux/reset-controller.h>
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
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
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
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
88 if (rcdev->dev) in rcdev_name()
89 return dev_name(rcdev->dev); in rcdev_name()
91 if (rcdev->of_node) in rcdev_name()
92 return rcdev->of_node->full_name; in rcdev_name()
94 if (rcdev->of_args) in rcdev_name()
95 return rcdev->of_args->np->full_name; in rcdev_name()
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
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
113 if (reset_spec->args[0] >= rcdev->nr_resets) in of_reset_simple_xlate()
114 return -EINVAL; in of_reset_simple_xlate()
116 return reset_spec->args[0]; in of_reset_simple_xlate()
120 * reset_controller_register - register a reset controller device
121 * @rcdev: a pointer to the initialized reset controller device
125 if (rcdev->of_node && rcdev->of_args) in reset_controller_register()
126 return -EINVAL; in reset_controller_register()
128 if (!rcdev->of_xlate) { in reset_controller_register()
129 rcdev->of_reset_n_cells = 1; in reset_controller_register()
130 rcdev->of_xlate = of_reset_simple_xlate; in reset_controller_register()
133 INIT_LIST_HEAD(&rcdev->reset_control_head); in reset_controller_register()
136 list_add(&rcdev->list, &reset_controller_list); in reset_controller_register()
144 * reset_controller_unregister - unregister a reset controller device
145 * @rcdev: a pointer to the reset controller device
150 list_del(&rcdev->list); in reset_controller_unregister()
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
165 * Managed reset_controller_register(). For reset controllers registered by
178 return -ENOMEM; in devm_reset_controller_register()
194 * reset_controller_add_lookup - register a set of lookup entries
195 * @lookup: array of reset lookup entries
208 if (!entry->dev_id || !entry->provider) { in reset_controller_add_lookup()
209 pr_warn("%s(): reset lookup entry badly specified, skipping\n", in reset_controller_add_lookup()
214 list_add_tail(&entry->list, &reset_lookup_list); in reset_controller_add_lookup()
229 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_reset()
230 ret = reset_control_reset(resets->rstc[i]); in reset_control_array_reset()
243 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_rearm()
244 rstc = resets->rstc[i]; in reset_control_array_rearm()
250 return -EINVAL; in reset_control_array_rearm()
252 if (rstc->shared) { in reset_control_array_rearm()
253 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) in reset_control_array_rearm()
254 return -EINVAL; in reset_control_array_rearm()
256 if (!rstc->acquired) in reset_control_array_rearm()
257 return -EPERM; in reset_control_array_rearm()
261 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_rearm()
262 rstc = resets->rstc[i]; in reset_control_array_rearm()
264 if (rstc && rstc->shared) in reset_control_array_rearm()
265 WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0); in reset_control_array_rearm()
275 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_assert()
276 ret = reset_control_assert(resets->rstc[i]); in reset_control_array_assert()
284 while (i--) in reset_control_array_assert()
285 reset_control_deassert(resets->rstc[i]); in reset_control_array_assert()
293 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_deassert()
294 ret = reset_control_deassert(resets->rstc[i]); in reset_control_array_deassert()
302 while (i--) in reset_control_array_deassert()
303 reset_control_assert(resets->rstc[i]); in reset_control_array_deassert()
312 for (i = 0; i < resets->num_rstcs; i++) { in reset_control_array_acquire()
313 err = reset_control_acquire(resets->rstc[i]); in reset_control_array_acquire()
321 while (i--) in reset_control_array_acquire()
322 reset_control_release(resets->rstc[i]); in reset_control_array_acquire()
331 for (i = 0; i < resets->num_rstcs; i++) in reset_control_array_release()
332 reset_control_release(resets->rstc[i]); in reset_control_array_release()
337 return rstc->array; in reset_control_is_array()
341 * reset_control_reset - reset the controlled device
342 * @rstc: reset controller
344 * On a shared reset line the actual reset pulse is only triggered once for the
346 * a no-op.
347 * Consumers must not use reset_control_(de)assert on shared reset lines when
350 * If rstc is NULL it is an optional reset and the function will just
361 return -EINVAL; in reset_control_reset()
366 if (!rstc->rcdev->ops->reset) in reset_control_reset()
367 return -ENOTSUPP; in reset_control_reset()
369 if (rstc->shared) { in reset_control_reset()
370 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) in reset_control_reset()
371 return -EINVAL; in reset_control_reset()
373 if (atomic_inc_return(&rstc->triggered_count) != 1) in reset_control_reset()
376 if (!rstc->acquired) in reset_control_reset()
377 return -EPERM; in reset_control_reset()
380 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id); in reset_control_reset()
381 if (rstc->shared && ret) in reset_control_reset()
382 atomic_dec(&rstc->triggered_count); in reset_control_reset()
389 * reset_control_bulk_reset - reset the controlled devices in order
391 * @rstcs: array of struct reset_control_bulk_data with reset controls set
393 * Issue a reset on all provided reset controls, in order.
413 * reset_control_rearm - allow shared reset line to be re-triggered"
414 * @rstc: reset controller
416 * On a shared reset line the actual reset pulse is only triggered once for the
422 * Consumers must not use reset_control_(de)assert on shared reset lines when
433 return -EINVAL; in reset_control_rearm()
438 if (rstc->shared) { in reset_control_rearm()
439 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0)) in reset_control_rearm()
440 return -EINVAL; in reset_control_rearm()
442 WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0); in reset_control_rearm()
444 if (!rstc->acquired) in reset_control_rearm()
445 return -EPERM; in reset_control_rearm()
453 * reset_control_assert - asserts the reset line
454 * @rstc: reset controller
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
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
465 * If rstc is NULL it is an optional reset and the function will just
474 return -EINVAL; in reset_control_assert()
479 if (rstc->shared) { in reset_control_assert()
480 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) in reset_control_assert()
481 return -EINVAL; in reset_control_assert()
483 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0)) in reset_control_assert()
484 return -EINVAL; in reset_control_assert()
486 if (atomic_dec_return(&rstc->deassert_count) != 0) in reset_control_assert()
490 * Shared reset controls allow the reset line to be in any state in reset_control_assert()
493 if (!rstc->rcdev->ops->assert) in reset_control_assert()
497 * If the reset controller does not implement .assert(), there in reset_control_assert()
498 * is no way to guarantee that the reset line is asserted after in reset_control_assert()
501 if (!rstc->rcdev->ops->assert) in reset_control_assert()
502 return -ENOTSUPP; in reset_control_assert()
504 if (!rstc->acquired) { in reset_control_assert()
505 WARN(1, "reset %s (ID: %u) is not acquired\n", in reset_control_assert()
506 rcdev_name(rstc->rcdev), rstc->id); in reset_control_assert()
507 return -EPERM; in reset_control_assert()
511 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id); in reset_control_assert()
516 * reset_control_bulk_assert - asserts the reset lines in order
518 * @rstcs: array of struct reset_control_bulk_data with reset controls set
520 * Assert the reset lines for all provided reset controls, in order.
539 while (i--) in reset_control_bulk_assert()
546 * reset_control_deassert - deasserts the reset line
547 * @rstc: reset controller
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
553 * If rstc is NULL it is an optional reset and the function will just
562 return -EINVAL; in reset_control_deassert()
567 if (rstc->shared) { in reset_control_deassert()
568 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0)) in reset_control_deassert()
569 return -EINVAL; in reset_control_deassert()
571 if (atomic_inc_return(&rstc->deassert_count) != 1) in reset_control_deassert()
574 if (!rstc->acquired) { in reset_control_deassert()
575 WARN(1, "reset %s (ID: %u) is not acquired\n", in reset_control_deassert()
576 rcdev_name(rstc->rcdev), rstc->id); in reset_control_deassert()
577 return -EPERM; in reset_control_deassert()
582 * If the reset controller does not implement .deassert(), we assume in reset_control_deassert()
583 * that it handles self-deasserting reset lines via .reset(). In that in reset_control_deassert()
584 * case, the reset lines are deasserted by default. If that is not the in reset_control_deassert()
585 * case, the reset controller driver should implement .deassert() and in reset_control_deassert()
586 * return -ENOTSUPP. in reset_control_deassert()
588 if (!rstc->rcdev->ops->deassert) in reset_control_deassert()
591 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id); in reset_control_deassert()
596 * reset_control_bulk_deassert - deasserts the reset lines in reverse order
598 * @rstcs: array of struct reset_control_bulk_data with reset controls set
600 * Deassert the reset lines for all provided reset controls, in reverse order.
610 for (i = num_rstcs - 1; i >= 0; i--) { in reset_control_bulk_deassert()
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
637 return -EINVAL; in reset_control_status()
639 if (rstc->rcdev->ops->status) in reset_control_status()
640 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id); in reset_control_status()
642 return -ENOTSUPP; in reset_control_status()
647 * reset_control_acquire() - acquires a reset control for exclusive use
648 * @rstc: reset control
650 * This is used to explicitly acquire a reset control for exclusive use. Note
652 * second consumer to be able to control the reset, the first consumer has to
654 * reset_control_get_exclusive_released() to obtain an instance of the reset
655 * control. Such reset controls are not acquired by default.
657 * Consumers implementing shared access to an exclusive reset need to follow
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
662 * access to the reset as long as another consumer hasn't released a reset.
674 return -EINVAL; in reset_control_acquire()
681 if (rstc->acquired) { in reset_control_acquire()
686 list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) { in reset_control_acquire()
687 if (rstc != rc && rstc->id == rc->id) { in reset_control_acquire()
688 if (rc->acquired) { in reset_control_acquire()
690 return -EBUSY; in reset_control_acquire()
695 rstc->acquired = true; in reset_control_acquire()
703 * reset_control_bulk_acquire - acquires reset controls for exclusive use
705 * @rstcs: array of struct reset_control_bulk_data with reset controls set
707 * This is used to explicitly acquire reset controls requested with
726 while (i--) in reset_control_bulk_acquire()
733 * reset_control_release() - releases exclusive access to a reset control
734 * @rstc: reset control
736 * Releases exclusive access right to a reset control previously obtained by a
750 rstc->acquired = false; in reset_control_release()
755 * reset_control_bulk_release() - releases exclusive access to reset controls
757 * @rstcs: array of struct reset_control_bulk_data with reset controls set
759 * Releases exclusive access right to reset controls previously obtained by a
782 list_for_each_entry(rstc, &rcdev->reset_control_head, list) { in __reset_control_get_internal()
783 if (rstc->id == index) { in __reset_control_get_internal()
787 * controlled reset line. in __reset_control_get_internal()
789 if (!rstc->shared && !shared && !acquired) in __reset_control_get_internal()
792 if (WARN_ON(!rstc->shared || !shared)) in __reset_control_get_internal()
793 return ERR_PTR(-EBUSY); in __reset_control_get_internal()
795 kref_get(&rstc->refcnt); in __reset_control_get_internal()
802 return ERR_PTR(-ENOMEM); in __reset_control_get_internal()
804 if (!try_module_get(rcdev->owner)) { in __reset_control_get_internal()
806 return ERR_PTR(-ENODEV); in __reset_control_get_internal()
809 rstc->rcdev = rcdev; in __reset_control_get_internal()
810 list_add(&rstc->list, &rcdev->reset_control_head); in __reset_control_get_internal()
811 rstc->id = index; in __reset_control_get_internal()
812 kref_init(&rstc->refcnt); in __reset_control_get_internal()
813 rstc->acquired = acquired; in __reset_control_get_internal()
814 rstc->shared = shared; in __reset_control_get_internal()
815 get_device(rcdev->dev); in __reset_control_get_internal()
827 module_put(rstc->rcdev->owner); in __reset_control_release()
829 list_del(&rstc->list); in __reset_control_release()
830 put_device(rstc->rcdev->dev); in __reset_control_release()
841 kref_put(&rstc->refcnt, __reset_control_release); in __reset_control_put_internal()
845 unsigned int gpio, in __reset_add_reset_gpio_lookup() argument
853 * Later we map GPIO flags between OF and Linux, however not all in __reset_add_reset_gpio_lookup()
854 * constants from include/dt-bindings/gpio/gpio.h and in __reset_add_reset_gpio_lookup()
855 * include/linux/gpio/machine.h match each other. in __reset_add_reset_gpio_lookup()
858 pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n", in __reset_add_reset_gpio_lookup()
859 of_flags, gpio); in __reset_add_reset_gpio_lookup()
860 return -EINVAL; in __reset_add_reset_gpio_lookup()
865 return -EPROBE_DEFER; in __reset_add_reset_gpio_lookup()
869 return -EINVAL; in __reset_add_reset_gpio_lookup()
873 return -ENOMEM; in __reset_add_reset_gpio_lookup()
879 return -ENOMEM; in __reset_add_reset_gpio_lookup()
881 lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id); in __reset_add_reset_gpio_lookup()
882 if (!lookup->dev_id) in __reset_add_reset_gpio_lookup()
883 return -ENOMEM; in __reset_add_reset_gpio_lookup()
887 lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset", in __reset_add_reset_gpio_lookup()
897 * @args: phandle to the GPIO provider with all the args like GPIO number
906 * Currently only #gpio-cells=2 is supported with the meaning of: in __reset_add_reset_gpio_device()
907 * args[0]: GPIO number in __reset_add_reset_gpio_device()
908 * args[1]: GPIO flags in __reset_add_reset_gpio_device()
911 if (args->args_count != 2) in __reset_add_reset_gpio_device()
912 return -ENOENT; in __reset_add_reset_gpio_device()
915 * Registering reset-gpio device might cause immediate in __reset_add_reset_gpio_device()
916 * bind, resulting in its probe() registering new reset controller thus in __reset_add_reset_gpio_device()
924 if (args->np == rgpio_dev->of_args.np) { in __reset_add_reset_gpio_device()
925 if (of_phandle_args_equal(args, &rgpio_dev->of_args)) in __reset_add_reset_gpio_device()
937 ret = -ENOMEM; in __reset_add_reset_gpio_device()
941 ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0], in __reset_add_reset_gpio_device()
942 args->args[1]); in __reset_add_reset_gpio_device()
946 rgpio_dev->of_args = *args; in __reset_add_reset_gpio_device()
952 of_node_get(rgpio_dev->of_args.np); in __reset_add_reset_gpio_device()
953 pdev = platform_device_register_data(NULL, "reset-gpio", id, in __reset_add_reset_gpio_device()
954 &rgpio_dev->of_args, in __reset_add_reset_gpio_device()
955 sizeof(rgpio_dev->of_args)); in __reset_add_reset_gpio_device()
960 list_add(&rgpio_dev->list, &reset_gpio_lookup_list); in __reset_add_reset_gpio_device()
965 of_node_put(rgpio_dev->of_args.np); in __reset_add_reset_gpio_device()
983 if (rcdev->of_args && of_phandle_args_equal(args, in __reset_find_rcdev()
984 rcdev->of_args)) in __reset_find_rcdev()
987 if (args->np == rcdev->of_node) in __reset_find_rcdev()
1007 return ERR_PTR(-EINVAL); in __of_reset_control_get()
1011 "reset-names", id); in __of_reset_control_get()
1012 if (index == -EILSEQ) in __of_reset_control_get()
1015 return optional ? NULL : ERR_PTR(-ENOENT); in __of_reset_control_get()
1018 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", in __of_reset_control_get()
1020 if (ret == -EINVAL) in __of_reset_control_get()
1027 * There can be only one reset-gpio for regular devices, so in __of_reset_control_get()
1028 * don't bother with the "reset-gpios" phandle index. in __of_reset_control_get()
1030 ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells", in __of_reset_control_get()
1047 rstc = ERR_PTR(-EPROBE_DEFER); in __of_reset_control_get()
1051 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { in __of_reset_control_get()
1052 rstc = ERR_PTR(-EINVAL); in __of_reset_control_get()
1056 rstc_id = rcdev->of_xlate(rcdev, &args); in __of_reset_control_get()
1082 if (!rcdev->dev) in __reset_controller_by_name()
1085 if (!strcmp(name, dev_name(rcdev->dev))) in __reset_controller_by_name()
1104 if (strcmp(lookup->dev_id, dev_id)) in __reset_control_get_from_lookup()
1107 if ((!con_id && !lookup->con_id) || in __reset_control_get_from_lookup()
1108 ((con_id && lookup->con_id) && in __reset_control_get_from_lookup()
1109 !strcmp(con_id, lookup->con_id))) { in __reset_control_get_from_lookup()
1111 rcdev = __reset_controller_by_name(lookup->provider); in __reset_control_get_from_lookup()
1115 /* Reset provider may not be ready yet. */ in __reset_control_get_from_lookup()
1116 return ERR_PTR(-EPROBE_DEFER); in __reset_control_get_from_lookup()
1120 lookup->index, in __reset_control_get_from_lookup()
1130 return optional ? NULL : ERR_PTR(-ENOENT); in __reset_control_get_from_lookup()
1140 return ERR_PTR(-EINVAL); in __reset_control_get()
1142 if (dev->of_node) in __reset_control_get()
1143 return __of_reset_control_get(dev->of_node, id, index, shared, in __reset_control_get()
1170 while (i--) in __reset_control_bulk_get()
1182 for (i = 0; i < resets->num_rstcs; i++) in reset_control_array_put()
1183 __reset_control_put_internal(resets->rstc[i]); in reset_control_array_put()
1189 * reset_control_put - free the reset controller
1190 * @rstc: reset controller
1209 * reset_control_bulk_put - free the reset controllers
1211 * @rstcs: array of struct reset_control_bulk_data with reset controls set
1216 while (num_rstcs--) in reset_control_bulk_put()
1236 return ERR_PTR(-ENOMEM); in __devm_reset_control_get()
1260 reset_control_bulk_put(devres->num_rstcs, devres->rstcs); in devm_reset_control_bulk_release()
1273 return -ENOMEM; in __devm_reset_control_bulk_get()
1281 ptr->num_rstcs = num_rstcs; in __devm_reset_control_bulk_get()
1282 ptr->rstcs = rstcs; in __devm_reset_control_bulk_get()
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
1296 * This is useful for the common case of devices with single, dedicated reset
1309 return optional ? 0 : -ENOENT; in __device_reset()
1312 return -EIO; in __device_reset()
1329 * APIs to manage an array of reset controls.
1333 * of_reset_control_get_count - Count number of resets available with a device
1337 * Returns positive reset count on success, or error number on failure and
1345 return -EINVAL; in of_reset_control_get_count()
1347 count = of_count_phandle_with_args(node, "resets", "#reset-cells"); in of_reset_control_get_count()
1349 count = -ENOENT; in of_reset_control_get_count()
1355 * of_reset_control_array_get - Get a list of reset controls using
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
1380 return ERR_PTR(-ENOMEM); in of_reset_control_array_get()
1381 resets->num_rstcs = num; in of_reset_control_array_get()
1388 resets->rstc[i] = rstc; in of_reset_control_array_get()
1390 resets->base.array = true; in of_reset_control_array_get()
1392 return &resets->base; in of_reset_control_array_get()
1396 while (--i >= 0) in of_reset_control_array_get()
1397 __reset_control_put_internal(resets->rstc[i]); in of_reset_control_array_get()
1407 * devm_reset_control_array_get - Resource managed reset control array get
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
1413 * The reset control array APIs are intended for a list of resets
1427 return ERR_PTR(-ENOMEM); in devm_reset_control_array_get()
1429 rstc = of_reset_control_array_get(dev->of_node, shared, optional, true); in devm_reset_control_array_get()
1449 return -EINVAL; in reset_control_get_count_from_lookup()
1455 if (!strcmp(lookup->dev_id, dev_id)) in reset_control_get_count_from_lookup()
1462 count = -ENOENT; in reset_control_get_count_from_lookup()
1468 * reset_control_get_count - Count number of resets available with a device
1472 * Returns positive reset count on success, or error number on failure and
1477 if (dev->of_node) in reset_control_get_count()
1478 return of_reset_control_get_count(dev->of_node); in reset_control_get_count()