1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
12 #include <reset-uclass.h>
13 #include <dm/devres.h>
15 static inline struct reset_ops *reset_dev_ops(struct udevice *dev)
17 return (struct reset_ops *)dev->driver->ops;
20 static int reset_of_xlate_default(struct reset_ctl *reset_ctl,
21 struct ofnode_phandle_args *args)
23 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
25 if (args->args_count != 1) {
26 debug("Invaild args_count: %d\n", args->args_count);
30 reset_ctl->id = args->args[0];
35 static int reset_get_by_index_tail(int ret, ofnode node,
36 struct ofnode_phandle_args *args,
37 const char *list_name, int index,
38 struct reset_ctl *reset_ctl)
40 struct udevice *dev_reset;
41 struct reset_ops *ops;
44 reset_ctl->dev = NULL;
48 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args->node,
51 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
53 debug("%s %d\n", ofnode_get_name(args->node), args->args[0]);
56 ops = reset_dev_ops(dev_reset);
58 reset_ctl->dev = dev_reset;
60 ret = ops->of_xlate(reset_ctl, args);
62 ret = reset_of_xlate_default(reset_ctl, args);
64 debug("of_xlate() failed: %d\n", ret);
68 ret = ops->request(reset_ctl);
70 debug("ops->request() failed: %d\n", ret);
77 int reset_get_by_index(struct udevice *dev, int index,
78 struct reset_ctl *reset_ctl)
80 struct ofnode_phandle_args args;
83 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
86 return reset_get_by_index_tail(ret, dev_ofnode(dev), &args, "resets",
87 index > 0, reset_ctl);
90 int reset_get_by_index_nodev(ofnode node, int index,
91 struct reset_ctl *reset_ctl)
93 struct ofnode_phandle_args args;
96 ret = ofnode_parse_phandle_with_args(node, "resets", "#reset-cells", 0,
99 return reset_get_by_index_tail(ret, node, &args, "resets",
100 index > 0, reset_ctl);
103 int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
105 int i, ret, err, count;
109 count = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
113 bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
118 for (i = 0; i < count; i++) {
119 ret = reset_get_by_index(dev, i, &bulk->resets[i]);
129 err = reset_release_all(bulk->resets, bulk->count);
131 debug("%s: could release all resets for %p\n",
137 int reset_get_by_name(struct udevice *dev, const char *name,
138 struct reset_ctl *reset_ctl)
142 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
144 reset_ctl->dev = NULL;
146 index = dev_read_stringlist_search(dev, "reset-names", name);
148 debug("fdt_stringlist_search() failed: %d\n", index);
152 return reset_get_by_index(dev, index, reset_ctl);
155 int reset_request(struct reset_ctl *reset_ctl)
157 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
159 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
161 return ops->request(reset_ctl);
164 int reset_free(struct reset_ctl *reset_ctl)
166 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
168 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
170 return ops->rfree(reset_ctl);
173 int reset_assert(struct reset_ctl *reset_ctl)
175 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
177 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
179 return ops->rst_assert(reset_ctl);
182 int reset_assert_bulk(struct reset_ctl_bulk *bulk)
186 for (i = 0; i < bulk->count; i++) {
187 ret = reset_assert(&bulk->resets[i]);
195 int reset_deassert(struct reset_ctl *reset_ctl)
197 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
199 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
201 return ops->rst_deassert(reset_ctl);
204 int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
208 for (i = 0; i < bulk->count; i++) {
209 ret = reset_deassert(&bulk->resets[i]);
217 int reset_status(struct reset_ctl *reset_ctl)
219 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
221 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
223 return ops->rst_status(reset_ctl);
226 int reset_release_all(struct reset_ctl *reset_ctl, int count)
230 for (i = 0; i < count; i++) {
231 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
233 /* check if reset has been previously requested */
234 if (!reset_ctl[i].dev)
237 ret = reset_assert(&reset_ctl[i]);
241 ret = reset_free(&reset_ctl[i]);
249 UCLASS_DRIVER(reset) = {