1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 JJ Hiblot <jjhiblot@ti.com>
9 #include <dm/device-internal.h>
12 #include <dm/uclass-internal.h>
14 static int bind_by_class_index(const char *uclass, int index,
17 static enum uclass_id uclass_id;
19 struct udevice *parent;
23 drv = lists_driver_lookup_name(drv_name);
25 printf("Cannot find driver '%s'\n", drv_name);
29 uclass_id = uclass_get_by_name(uclass);
30 if (uclass_id == UCLASS_INVALID) {
31 printf("%s is not a valid uclass\n", uclass);
35 ret = uclass_find_device(uclass_id, index, &parent);
37 printf("Cannot find device %d of class %s\n", index, uclass);
41 ret = device_bind_with_driver_data(parent, drv, drv->name, 0,
44 printf("Unable to bind. err:%d\n", ret);
51 static int find_dev(const char *uclass, int index, struct udevice **devp)
53 static enum uclass_id uclass_id;
56 uclass_id = uclass_get_by_name(uclass);
57 if (uclass_id == UCLASS_INVALID) {
58 printf("%s is not a valid uclass\n", uclass);
62 rc = uclass_find_device(uclass_id, index, devp);
64 printf("Cannot find device %d of class %s\n", index, uclass);
71 static int unbind_by_class_index(const char *uclass, int index)
76 ret = find_dev(uclass, index, &dev);
80 ret = device_remove(dev, DM_REMOVE_NORMAL);
82 printf("Unable to remove. err:%d\n", ret);
86 ret = device_unbind(dev);
88 printf("Unable to unbind. err:%d\n", ret);
95 static int unbind_child_by_class_index(const char *uclass, int index,
98 struct udevice *parent;
102 drv = lists_driver_lookup_name(drv_name);
104 printf("Cannot find driver '%s'\n", drv_name);
108 ret = find_dev(uclass, index, &parent);
112 ret = device_chld_remove(parent, drv, DM_REMOVE_NORMAL);
114 printf("Unable to remove all. err:%d\n", ret);
116 ret = device_chld_unbind(parent, drv);
118 printf("Unable to unbind all. err:%d\n", ret);
123 static int bind_by_node_path(const char *path, const char *drv_name)
126 struct udevice *parent = NULL;
131 drv = lists_driver_lookup_name(drv_name);
133 printf("%s is not a valid driver name\n", drv_name);
137 ofnode = ofnode_path(path);
138 if (!ofnode_valid(ofnode)) {
139 printf("%s is not a valid node path\n", path);
143 while (ofnode_valid(ofnode)) {
144 if (!device_find_global_by_ofnode(ofnode, &parent))
146 ofnode = ofnode_get_parent(ofnode);
150 printf("Cannot find a parent device for node path %s\n", path);
154 ofnode = ofnode_path(path);
155 ret = lists_bind_fdt(parent, ofnode, &dev, drv, false);
158 printf("Unable to bind. err:%d\n", ret);
165 static int unbind_by_node_path(const char *path)
171 ofnode = ofnode_path(path);
172 if (!ofnode_valid(ofnode)) {
173 printf("%s is not a valid node path\n", path);
177 ret = device_find_global_by_ofnode(ofnode, &dev);
180 printf("Cannot find a device with path %s\n", path);
184 ret = device_remove(dev, DM_REMOVE_NORMAL);
186 printf("Unable to remove. err:%d\n", ret);
190 ret = device_unbind(dev);
192 printf("Unable to unbind. err:%d\n", ret);
199 static int do_bind_unbind(struct cmd_tbl *cmdtp, int flag, int argc,
207 return CMD_RET_USAGE;
209 bind = (argv[0][0] == 'b');
210 by_node = (argv[1][0] == '/');
212 if (by_node && bind) {
214 return CMD_RET_USAGE;
215 ret = bind_by_node_path(argv[1], argv[2]);
216 } else if (by_node && !bind) {
218 return CMD_RET_USAGE;
219 ret = unbind_by_node_path(argv[1]);
220 } else if (!by_node && bind) {
221 int index = (argc > 2) ? dectoul(argv[2], NULL) : 0;
224 return CMD_RET_USAGE;
225 ret = bind_by_class_index(argv[1], index, argv[3]);
226 } else if (!by_node && !bind) {
227 int index = (argc > 2) ? dectoul(argv[2], NULL) : 0;
230 ret = unbind_by_class_index(argv[1], index);
232 ret = unbind_child_by_class_index(argv[1], index,
235 return CMD_RET_USAGE;
239 return CMD_RET_FAILURE;
241 return CMD_RET_SUCCESS;
245 bind, 4, 0, do_bind_unbind,
246 "Bind a device to a driver",
247 "<node path> <driver>\n"
248 "bind <class> <index> <driver>\n"
252 unbind, 4, 0, do_bind_unbind,
253 "Unbind a device from a driver",
255 "unbind <class> <index>\n"
256 "unbind <class> <index> <driver>\n"