1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 JJ Hiblot <jjhiblot@ti.com>
9 #include <dm/device-internal.h>
11 #include <dm/uclass-internal.h>
13 static int bind_by_class_index(const char *uclass, int index,
16 static enum uclass_id uclass_id;
18 struct udevice *parent;
22 drv = lists_driver_lookup_name(drv_name);
24 printf("Cannot find driver '%s'\n", drv_name);
28 uclass_id = uclass_get_by_name(uclass);
29 if (uclass_id == UCLASS_INVALID) {
30 printf("%s is not a valid uclass\n", uclass);
34 ret = uclass_find_device(uclass_id, index, &parent);
36 printf("Cannot find device %d of class %s\n", index, uclass);
40 ret = device_bind_with_driver_data(parent, drv, drv->name, 0,
43 printf("Unable to bind. err:%d\n", ret);
50 static int find_dev(const char *uclass, int index, struct udevice **devp)
52 static enum uclass_id uclass_id;
55 uclass_id = uclass_get_by_name(uclass);
56 if (uclass_id == UCLASS_INVALID) {
57 printf("%s is not a valid uclass\n", uclass);
61 rc = uclass_find_device(uclass_id, index, devp);
63 printf("Cannot find device %d of class %s\n", index, uclass);
70 static int unbind_by_class_index(const char *uclass, int index)
75 ret = find_dev(uclass, index, &dev);
79 ret = device_remove(dev, DM_REMOVE_NORMAL);
81 printf("Unable to remove. err:%d\n", ret);
85 ret = device_unbind(dev);
87 printf("Unable to unbind. err:%d\n", ret);
94 static int unbind_child_by_class_index(const char *uclass, int index,
97 struct udevice *parent;
101 drv = lists_driver_lookup_name(drv_name);
103 printf("Cannot find driver '%s'\n", drv_name);
107 ret = find_dev(uclass, index, &parent);
111 ret = device_chld_remove(parent, drv, DM_REMOVE_NORMAL);
113 printf("Unable to remove all. err:%d\n", ret);
115 ret = device_chld_unbind(parent, drv);
117 printf("Unable to unbind all. err:%d\n", ret);
122 static int bind_by_node_path(const char *path, const char *drv_name)
125 struct udevice *parent = NULL;
130 drv = lists_driver_lookup_name(drv_name);
132 printf("%s is not a valid driver name\n", drv_name);
136 ofnode = ofnode_path(path);
137 if (!ofnode_valid(ofnode)) {
138 printf("%s is not a valid node path\n", path);
142 while (ofnode_valid(ofnode)) {
143 if (!device_find_global_by_ofnode(ofnode, &parent))
145 ofnode = ofnode_get_parent(ofnode);
149 printf("Cannot find a parent device for node path %s\n", path);
153 ofnode = ofnode_path(path);
154 ret = device_bind_with_driver_data(parent, drv, ofnode_get_name(ofnode),
157 printf("Unable to bind. err:%d\n", ret);
164 static int unbind_by_node_path(const char *path)
170 ofnode = ofnode_path(path);
171 if (!ofnode_valid(ofnode)) {
172 printf("%s is not a valid node path\n", path);
176 ret = device_find_global_by_ofnode(ofnode, &dev);
179 printf("Cannot find a device with path %s\n", path);
183 ret = device_remove(dev, DM_REMOVE_NORMAL);
185 printf("Unable to remove. err:%d\n", ret);
189 ret = device_unbind(dev);
191 printf("Unable to unbind. err:%d\n", ret);
198 static int do_bind_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
206 return CMD_RET_USAGE;
208 bind = (argv[0][0] == 'b');
209 by_node = (argv[1][0] == '/');
211 if (by_node && bind) {
213 return CMD_RET_USAGE;
214 ret = bind_by_node_path(argv[1], argv[2]);
215 } else if (by_node && !bind) {
217 return CMD_RET_USAGE;
218 ret = unbind_by_node_path(argv[1]);
219 } else if (!by_node && bind) {
220 int index = (argc > 2) ? simple_strtoul(argv[2], NULL, 10) : 0;
223 return CMD_RET_USAGE;
224 ret = bind_by_class_index(argv[1], index, argv[3]);
225 } else if (!by_node && !bind) {
226 int index = (argc > 2) ? simple_strtoul(argv[2], NULL, 10) : 0;
229 ret = unbind_by_class_index(argv[1], index);
231 ret = unbind_child_by_class_index(argv[1], index,
234 return CMD_RET_USAGE;
238 return CMD_RET_FAILURE;
240 return CMD_RET_SUCCESS;
244 bind, 4, 0, do_bind_unbind,
245 "Bind a device to a driver",
246 "<node path> <driver>\n"
247 "bind <class> <index> <driver>\n"
251 unbind, 4, 0, do_bind_unbind,
252 "Unbind a device from a driver",
254 "unbind <class> <index>\n"
255 "unbind <class> <index> <driver>\n"