1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 JJ Hiblot <jjhiblot@ti.com>
8 #include <dm/device-internal.h>
11 #include <dm/uclass-internal.h>
13 static int bind_by_class_seq(const char *uclass
, int seq
,
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_by_seq(uclass_id
, seq
, &parent
);
36 printf("Cannot find device %d of class %s\n", seq
, 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 seq
, 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_by_seq(uclass_id
, seq
, devp
);
63 printf("Cannot find device %d of class %s\n", seq
, uclass
);
70 static int unbind_by_class_seq(const char *uclass
, int seq
)
75 ret
= find_dev(uclass
, seq
, &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_seq(const char *uclass
, int seq
,
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
, seq
, &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
= lists_bind_fdt(parent
, ofnode
, &dev
, drv
, false);
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 seq
= (argc
> 2) ? dectoul(argv
[2], NULL
) : 0;
223 return CMD_RET_USAGE
;
224 ret
= bind_by_class_seq(argv
[1], seq
, argv
[3]);
225 } else if (!by_node
&& !bind
) {
226 int seq
= (argc
> 2) ? dectoul(argv
[2], NULL
) : 0;
229 ret
= unbind_by_class_seq(argv
[1], seq
);
231 ret
= unbind_child_by_class_seq(argv
[1], seq
,
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> <seq> <driver>\n"
248 "Use 'dm tree' to list all devices registered in the driver model,\n"
249 "their path, class, sequence and current driver.\n"
253 unbind
, 4, 0, do_bind_unbind
,
254 "Unbind a device from a driver",
256 "unbind <class> <seq>\n"
257 "unbind <class> <seq> <driver>\n"
258 "Use 'dm tree' to list all devices registered in the driver model,\n"
259 "their path, class, sequence and current driver.\n"