1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
10 #include <reset-uclass.h>
12 static inline struct reset_ops *reset_dev_ops(struct udevice *dev)
14 return (struct reset_ops *)dev->driver->ops;
17 static int reset_of_xlate_default(struct reset_ctl *reset_ctl,
18 struct ofnode_phandle_args *args)
20 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
22 if (args->args_count != 1) {
23 debug("Invaild args_count: %d\n", args->args_count);
27 reset_ctl->id = args->args[0];
32 static int reset_get_by_index_tail(int ret, ofnode node,
33 struct ofnode_phandle_args *args,
34 const char *list_name, int index,
35 struct reset_ctl *reset_ctl)
37 struct udevice *dev_reset;
38 struct reset_ops *ops;
41 reset_ctl->dev = NULL;
45 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args->node,
48 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
50 debug("%s %d\n", ofnode_get_name(args->node), args->args[0]);
53 ops = reset_dev_ops(dev_reset);
55 reset_ctl->dev = dev_reset;
57 ret = ops->of_xlate(reset_ctl, args);
59 ret = reset_of_xlate_default(reset_ctl, args);
61 debug("of_xlate() failed: %d\n", ret);
65 ret = ops->request(reset_ctl);
67 debug("ops->request() failed: %d\n", ret);
74 int reset_get_by_index(struct udevice *dev, int index,
75 struct reset_ctl *reset_ctl)
77 struct ofnode_phandle_args args;
80 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
83 return reset_get_by_index_tail(ret, dev_ofnode(dev), &args, "resets",
84 index > 0, reset_ctl);
87 int reset_get_by_index_nodev(ofnode node, int index,
88 struct reset_ctl *reset_ctl)
90 struct ofnode_phandle_args args;
93 ret = ofnode_parse_phandle_with_args(node, "resets", "#reset-cells", 0,
96 return reset_get_by_index_tail(ret, node, &args, "resets",
97 index > 0, reset_ctl);
100 int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
102 int i, ret, err, count;
106 count = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
110 bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
115 for (i = 0; i < count; i++) {
116 ret = reset_get_by_index(dev, i, &bulk->resets[i]);
126 err = reset_release_all(bulk->resets, bulk->count);
128 debug("%s: could release all resets for %p\n",
134 int reset_get_by_name(struct udevice *dev, const char *name,
135 struct reset_ctl *reset_ctl)
139 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
141 reset_ctl->dev = NULL;
143 index = dev_read_stringlist_search(dev, "reset-names", name);
145 debug("fdt_stringlist_search() failed: %d\n", index);
149 return reset_get_by_index(dev, index, reset_ctl);
152 int reset_request(struct reset_ctl *reset_ctl)
154 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
156 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
158 return ops->request(reset_ctl);
161 int reset_free(struct reset_ctl *reset_ctl)
163 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
165 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
167 return ops->rfree(reset_ctl);
170 int reset_assert(struct reset_ctl *reset_ctl)
172 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
174 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
176 return ops->rst_assert(reset_ctl);
179 int reset_assert_bulk(struct reset_ctl_bulk *bulk)
183 for (i = 0; i < bulk->count; i++) {
184 ret = reset_assert(&bulk->resets[i]);
192 int reset_deassert(struct reset_ctl *reset_ctl)
194 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
196 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
198 return ops->rst_deassert(reset_ctl);
201 int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
205 for (i = 0; i < bulk->count; i++) {
206 ret = reset_deassert(&bulk->resets[i]);
214 int reset_status(struct reset_ctl *reset_ctl)
216 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
218 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
220 return ops->rst_status(reset_ctl);
223 int reset_release_all(struct reset_ctl *reset_ctl, int count)
227 for (i = 0; i < count; i++) {
228 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
230 /* check if reset has been previously requested */
231 if (!reset_ctl[i].dev)
234 ret = reset_assert(&reset_ctl[i]);
238 ret = reset_free(&reset_ctl[i]);
246 UCLASS_DRIVER(reset) = {