1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 JJ Hiblot <jjhiblot@ti.com>
8 #include <dm/device-internal.h>
10 #include <dm/uclass-internal.h>
12 static int bind_by_class_index(const char *uclass, int index,
15 static enum uclass_id uclass_id;
17 struct udevice *parent;
21 drv = lists_driver_lookup_name(drv_name);
23 printf("Cannot find driver '%s'\n", drv_name);
27 uclass_id = uclass_get_by_name(uclass);
28 if (uclass_id == UCLASS_INVALID) {
29 printf("%s is not a valid uclass\n", uclass);
33 ret = uclass_find_device(uclass_id, index, &parent);
35 printf("Cannot find device %d of class %s\n", index, uclass);
39 ret = device_bind_with_driver_data(parent, drv, drv->name, 0,
42 printf("Unable to bind. err:%d\n", ret);
49 static int find_dev(const char *uclass, int index, struct udevice **devp)
51 static enum uclass_id uclass_id;
54 uclass_id = uclass_get_by_name(uclass);
55 if (uclass_id == UCLASS_INVALID) {
56 printf("%s is not a valid uclass\n", uclass);
60 rc = uclass_find_device(uclass_id, index, devp);
62 printf("Cannot find device %d of class %s\n", index, uclass);
69 static int unbind_by_class_index(const char *uclass, int index)
74 ret = find_dev(uclass, index, &dev);
78 ret = device_remove(dev, DM_REMOVE_NORMAL);
80 printf("Unable to remove. err:%d\n", ret);
84 ret = device_unbind(dev);
86 printf("Unable to unbind. err:%d\n", ret);
93 static int unbind_child_by_class_index(const char *uclass, int index,
96 struct udevice *parent;
100 drv = lists_driver_lookup_name(drv_name);
102 printf("Cannot find driver '%s'\n", drv_name);
106 ret = find_dev(uclass, index, &parent);
110 ret = device_chld_remove(parent, drv, DM_REMOVE_NORMAL);
112 printf("Unable to remove all. err:%d\n", ret);
114 ret = device_chld_unbind(parent, drv);
116 printf("Unable to unbind all. err:%d\n", ret);
121 static int bind_by_node_path(const char *path, const char *drv_name)
124 struct udevice *parent = NULL;
129 drv = lists_driver_lookup_name(drv_name);
131 printf("%s is not a valid driver name\n", drv_name);
135 ofnode = ofnode_path(path);
136 if (!ofnode_valid(ofnode)) {
137 printf("%s is not a valid node path\n", path);
141 while (ofnode_valid(ofnode)) {
142 if (!device_find_global_by_ofnode(ofnode, &parent))
144 ofnode = ofnode_get_parent(ofnode);
148 printf("Cannot find a parent device for node path %s\n", path);
152 ofnode = ofnode_path(path);
153 ret = device_bind_with_driver_data(parent, drv, ofnode_get_name(ofnode),
156 printf("Unable to bind. err:%d\n", ret);
163 static int unbind_by_node_path(const char *path)
169 ofnode = ofnode_path(path);
170 if (!ofnode_valid(ofnode)) {
171 printf("%s is not a valid node path\n", path);
175 ret = device_find_global_by_ofnode(ofnode, &dev);
178 printf("Cannot find a device with path %s\n", path);
182 ret = device_remove(dev, DM_REMOVE_NORMAL);
184 printf("Unable to remove. err:%d\n", ret);
188 ret = device_unbind(dev);
190 printf("Unable to unbind. err:%d\n", ret);
197 static int do_bind_unbind(cmd_tbl_t *cmdtp, int flag, int argc,
205 return CMD_RET_USAGE;
207 bind = (argv[0][0] == 'b');
208 by_node = (argv[1][0] == '/');
210 if (by_node && bind) {
212 return CMD_RET_USAGE;
213 ret = bind_by_node_path(argv[1], argv[2]);
214 } else if (by_node && !bind) {
216 return CMD_RET_USAGE;
217 ret = unbind_by_node_path(argv[1]);
218 } else if (!by_node && bind) {
219 int index = (argc > 2) ? simple_strtoul(argv[2], NULL, 10) : 0;
222 return CMD_RET_USAGE;
223 ret = bind_by_class_index(argv[1], index, argv[3]);
224 } else if (!by_node && !bind) {
225 int index = (argc > 2) ? simple_strtoul(argv[2], NULL, 10) : 0;
228 ret = unbind_by_class_index(argv[1], index);
230 ret = unbind_child_by_class_index(argv[1], index,
233 return CMD_RET_USAGE;
237 return CMD_RET_FAILURE;
239 return CMD_RET_SUCCESS;
243 bind, 4, 0, do_bind_unbind,
244 "Bind a device to a driver",
245 "<node path> <driver>\n"
246 "bind <class> <index> <driver>\n"
250 unbind, 4, 0, do_bind_unbind,
251 "Unbind a device from a driver",
253 "unbind <class> <index>\n"
254 "unbind <class> <index> <driver>\n"