Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
| 4 | * |
| 5 | * (C) Copyright 2012 |
| 6 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Kever Yang | 97b5f9d | 2019-10-22 15:39:47 +0800 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_DM |
| 10 | |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 11 | #include <common.h> |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 13 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 17 | #include <dm/device.h> |
| 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/lists.h> |
| 20 | #include <dm/uclass.h> |
| 21 | #include <dm/uclass-internal.h> |
| 22 | #include <dm/util.h> |
| 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
| 26 | struct uclass *uclass_find(enum uclass_id key) |
| 27 | { |
| 28 | struct uclass *uc; |
| 29 | |
Simon Glass | c910e2e | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 30 | if (!gd->dm_root) |
| 31 | return NULL; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 32 | /* |
| 33 | * TODO(sjg@chromium.org): Optimise this, perhaps moving the found |
| 34 | * node to the start of the list, or creating a linear array mapping |
| 35 | * id to node. |
| 36 | */ |
Simon Glass | 8a71553 | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 37 | list_for_each_entry(uc, gd->uclass_root, sibling_node) { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 38 | if (uc->uc_drv->id == key) |
| 39 | return uc; |
| 40 | } |
| 41 | |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * uclass_add() - Create new uclass in list |
| 47 | * @id: Id number to create |
| 48 | * @ucp: Returns pointer to uclass, or NULL on error |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 49 | * Return: 0 on success, -ve on error |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 50 | * |
| 51 | * The new uclass is added to the list. There must be only one uclass for |
| 52 | * each id. |
| 53 | */ |
| 54 | static int uclass_add(enum uclass_id id, struct uclass **ucp) |
| 55 | { |
| 56 | struct uclass_driver *uc_drv; |
| 57 | struct uclass *uc; |
| 58 | int ret; |
| 59 | |
| 60 | *ucp = NULL; |
| 61 | uc_drv = lists_uclass_lookup(id); |
| 62 | if (!uc_drv) { |
Masahiro Yamada | 643e690 | 2015-07-07 18:51:32 +0900 | [diff] [blame] | 63 | debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n", |
| 64 | id); |
Simon Glass | 3346c87 | 2015-08-30 16:55:16 -0600 | [diff] [blame] | 65 | /* |
| 66 | * Use a strange error to make this case easier to find. When |
| 67 | * a uclass is not available it can prevent driver model from |
| 68 | * starting up and this failure is otherwise hard to debug. |
| 69 | */ |
| 70 | return -EPFNOSUPPORT; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 71 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 72 | uc = calloc(1, sizeof(*uc)); |
| 73 | if (!uc) |
| 74 | return -ENOMEM; |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 75 | if (uc_drv->priv_auto) { |
Simon Glass | 89ba6d5 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 76 | void *ptr; |
| 77 | |
| 78 | ptr = calloc(1, uc_drv->priv_auto); |
| 79 | if (!ptr) { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 80 | ret = -ENOMEM; |
| 81 | goto fail_mem; |
| 82 | } |
Simon Glass | 89ba6d5 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 83 | uclass_set_priv(uc, ptr); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 84 | } |
| 85 | uc->uc_drv = uc_drv; |
| 86 | INIT_LIST_HEAD(&uc->sibling_node); |
| 87 | INIT_LIST_HEAD(&uc->dev_head); |
Simon Glass | 8a71553 | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 88 | list_add(&uc->sibling_node, DM_UCLASS_ROOT_NON_CONST); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 89 | |
| 90 | if (uc_drv->init) { |
| 91 | ret = uc_drv->init(uc); |
| 92 | if (ret) |
| 93 | goto fail; |
| 94 | } |
| 95 | |
| 96 | *ucp = uc; |
| 97 | |
| 98 | return 0; |
| 99 | fail: |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 100 | if (uc_drv->priv_auto) { |
Simon Glass | 89ba6d5 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 101 | free(uclass_get_priv(uc)); |
| 102 | uclass_set_priv(uc, NULL); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 103 | } |
| 104 | list_del(&uc->sibling_node); |
| 105 | fail_mem: |
| 106 | free(uc); |
| 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | int uclass_destroy(struct uclass *uc) |
| 112 | { |
| 113 | struct uclass_driver *uc_drv; |
Simon Glass | 07d260e | 2015-04-19 07:20:58 -0600 | [diff] [blame] | 114 | struct udevice *dev; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 115 | int ret; |
| 116 | |
Simon Glass | 07d260e | 2015-04-19 07:20:58 -0600 | [diff] [blame] | 117 | /* |
| 118 | * We cannot use list_for_each_entry_safe() here. If a device in this |
| 119 | * uclass has a child device also in this uclass, it will be also be |
| 120 | * unbound (by the recursion in the call to device_unbind() below). |
| 121 | * We can loop until the list is empty. |
| 122 | */ |
| 123 | while (!list_empty(&uc->dev_head)) { |
| 124 | dev = list_first_entry(&uc->dev_head, struct udevice, |
| 125 | uclass_node); |
Simon Glass | ced1080 | 2020-03-28 14:03:48 -0600 | [diff] [blame] | 126 | ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 127 | if (ret) |
Simon Glass | 8474da9 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 128 | return log_msg_ret("remove", ret); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 129 | ret = device_unbind(dev); |
| 130 | if (ret) |
Simon Glass | 8474da9 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 131 | return log_msg_ret("unbind", ret); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | uc_drv = uc->uc_drv; |
| 135 | if (uc_drv->destroy) |
| 136 | uc_drv->destroy(uc); |
| 137 | list_del(&uc->sibling_node); |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 138 | if (uc_drv->priv_auto) |
Simon Glass | 89ba6d5 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 139 | free(uclass_get_priv(uc)); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 140 | free(uc); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int uclass_get(enum uclass_id id, struct uclass **ucp) |
| 146 | { |
| 147 | struct uclass *uc; |
| 148 | |
Simon Glass | 1e9ced2 | 2021-08-01 12:05:23 -0600 | [diff] [blame] | 149 | /* Immediately fail if driver model is not set up */ |
| 150 | if (!gd->uclass_root) |
| 151 | return -EDEADLK; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 152 | *ucp = NULL; |
| 153 | uc = uclass_find(id); |
Simon Glass | 91bcfdf | 2021-03-15 17:25:18 +1300 | [diff] [blame] | 154 | if (!uc) { |
| 155 | if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) |
| 156 | return -ENOENT; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 157 | return uclass_add(id, ucp); |
Simon Glass | 91bcfdf | 2021-03-15 17:25:18 +1300 | [diff] [blame] | 158 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 159 | *ucp = uc; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Simon Glass | 0a5f6f8 | 2016-10-05 20:42:13 -0600 | [diff] [blame] | 164 | const char *uclass_get_name(enum uclass_id id) |
| 165 | { |
| 166 | struct uclass *uc; |
| 167 | |
| 168 | if (uclass_get(id, &uc)) |
| 169 | return NULL; |
| 170 | return uc->uc_drv->name; |
| 171 | } |
| 172 | |
Simon Glass | 8064739 | 2020-12-22 19:30:26 -0700 | [diff] [blame] | 173 | void *uclass_get_priv(const struct uclass *uc) |
| 174 | { |
Simon Glass | fb8c9fb | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 175 | return uc->priv_; |
Simon Glass | 8064739 | 2020-12-22 19:30:26 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void uclass_set_priv(struct uclass *uc, void *priv) |
| 179 | { |
Simon Glass | fb8c9fb | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 180 | uc->priv_ = priv; |
Simon Glass | 8064739 | 2020-12-22 19:30:26 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Simon Glass | 6aa4fe3 | 2022-04-24 23:30:59 -0600 | [diff] [blame] | 183 | enum uclass_id uclass_get_by_namelen(const char *name, int len) |
Simon Glass | 6e43d1b | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 184 | { |
| 185 | int i; |
| 186 | |
| 187 | for (i = 0; i < UCLASS_COUNT; i++) { |
| 188 | struct uclass_driver *uc_drv = lists_uclass_lookup(i); |
| 189 | |
Simon Glass | 6aa4fe3 | 2022-04-24 23:30:59 -0600 | [diff] [blame] | 190 | if (uc_drv && !strncmp(uc_drv->name, name, len) && |
| 191 | strlen(uc_drv->name) == len) |
Simon Glass | 6e43d1b | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 192 | return i; |
| 193 | } |
| 194 | |
| 195 | return UCLASS_INVALID; |
| 196 | } |
| 197 | |
Simon Glass | 4b03017 | 2021-10-23 17:26:08 -0600 | [diff] [blame] | 198 | enum uclass_id uclass_get_by_name(const char *name) |
| 199 | { |
Simon Glass | 6aa4fe3 | 2022-04-24 23:30:59 -0600 | [diff] [blame] | 200 | return uclass_get_by_namelen(name, strlen(name)); |
Simon Glass | 4b03017 | 2021-10-23 17:26:08 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Jean-Jacques Hiblot | e7c8656 | 2018-08-09 16:17:42 +0200 | [diff] [blame] | 203 | int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp) |
| 204 | { |
| 205 | struct udevice *iter; |
| 206 | struct uclass *uc = dev->uclass; |
| 207 | int i = 0; |
| 208 | |
| 209 | if (list_empty(&uc->dev_head)) |
| 210 | return -ENODEV; |
| 211 | |
Liviu Dudau | 81f351d | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 212 | uclass_foreach_dev(iter, uc) { |
Jean-Jacques Hiblot | e7c8656 | 2018-08-09 16:17:42 +0200 | [diff] [blame] | 213 | if (iter == dev) { |
| 214 | if (ucp) |
| 215 | *ucp = uc; |
| 216 | return i; |
| 217 | } |
| 218 | i++; |
| 219 | } |
| 220 | |
| 221 | return -ENODEV; |
| 222 | } |
| 223 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 224 | int uclass_find_device(enum uclass_id id, int index, struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 225 | { |
| 226 | struct uclass *uc; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 227 | struct udevice *dev; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 228 | int ret; |
| 229 | |
| 230 | *devp = NULL; |
| 231 | ret = uclass_get(id, &uc); |
| 232 | if (ret) |
| 233 | return ret; |
Simon Glass | 2fda14a | 2015-07-31 09:31:19 -0600 | [diff] [blame] | 234 | if (list_empty(&uc->dev_head)) |
| 235 | return -ENODEV; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 236 | |
Liviu Dudau | 81f351d | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 237 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 238 | if (!index--) { |
| 239 | *devp = dev; |
| 240 | return 0; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return -ENODEV; |
| 245 | } |
| 246 | |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 247 | int uclass_find_first_device(enum uclass_id id, struct udevice **devp) |
| 248 | { |
| 249 | struct uclass *uc; |
| 250 | int ret; |
| 251 | |
| 252 | *devp = NULL; |
| 253 | ret = uclass_get(id, &uc); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | if (list_empty(&uc->dev_head)) |
Simon Glass | 4805a7a | 2019-09-25 08:55:55 -0600 | [diff] [blame] | 257 | return 0; |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 258 | |
| 259 | *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | int uclass_find_next_device(struct udevice **devp) |
| 265 | { |
| 266 | struct udevice *dev = *devp; |
| 267 | |
| 268 | *devp = NULL; |
| 269 | if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head)) |
| 270 | return 0; |
| 271 | |
| 272 | *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node); |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Simon Glass | 4e0710a | 2022-04-24 23:31:00 -0600 | [diff] [blame] | 277 | int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len, |
| 278 | struct udevice **devp) |
Przemyslaw Marczak | e0735a4 | 2015-04-15 13:07:22 +0200 | [diff] [blame] | 279 | { |
| 280 | struct uclass *uc; |
| 281 | struct udevice *dev; |
| 282 | int ret; |
| 283 | |
| 284 | *devp = NULL; |
| 285 | if (!name) |
| 286 | return -EINVAL; |
| 287 | ret = uclass_get(id, &uc); |
| 288 | if (ret) |
| 289 | return ret; |
| 290 | |
Liviu Dudau | 81f351d | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 291 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 4e0710a | 2022-04-24 23:31:00 -0600 | [diff] [blame] | 292 | if (!strncmp(dev->name, name, len) && |
| 293 | strlen(dev->name) == len) { |
Przemyslaw Marczak | e0735a4 | 2015-04-15 13:07:22 +0200 | [diff] [blame] | 294 | *devp = dev; |
| 295 | return 0; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return -ENODEV; |
| 300 | } |
| 301 | |
Simon Glass | 4e0710a | 2022-04-24 23:31:00 -0600 | [diff] [blame] | 302 | int uclass_find_device_by_name(enum uclass_id id, const char *name, |
| 303 | struct udevice **devp) |
| 304 | { |
| 305 | return uclass_find_device_by_namelen(id, name, strlen(name), devp); |
| 306 | } |
| 307 | |
Simon Glass | a133e21 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 308 | int uclass_find_next_free_seq(struct uclass *uc) |
Jean-Jacques Hiblot | 3542ff2 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 309 | { |
Jean-Jacques Hiblot | 3542ff2 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 310 | struct udevice *dev; |
Jean-Jacques Hiblot | 3542ff2 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 311 | int max = -1; |
| 312 | |
Simon Glass | a133e21 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 313 | /* If using aliases, start with the highest alias value */ |
| 314 | if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) && |
| 315 | (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) |
| 316 | max = dev_read_alias_highest_id(uc->uc_drv->name); |
Jean-Jacques Hiblot | 3542ff2 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 317 | |
Simon Glass | a133e21 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 318 | /* Avoid conflict with existing devices */ |
| 319 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | 2462139 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 320 | if (dev->seq_ > max) |
| 321 | max = dev->seq_; |
Simon Glass | a133e21 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 322 | } |
| 323 | /* |
| 324 | * At this point, max will be -1 if there are no existing aliases or |
| 325 | * devices |
| 326 | */ |
Jean-Jacques Hiblot | 3542ff2 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 327 | |
| 328 | return max + 1; |
| 329 | } |
Jean-Jacques Hiblot | 3542ff2 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 330 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 331 | int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 332 | { |
| 333 | struct uclass *uc; |
| 334 | struct udevice *dev; |
| 335 | int ret; |
| 336 | |
| 337 | *devp = NULL; |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 338 | log_debug("%d\n", seq); |
| 339 | if (seq == -1) |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 340 | return -ENODEV; |
| 341 | ret = uclass_get(id, &uc); |
| 342 | if (ret) |
| 343 | return ret; |
| 344 | |
Liviu Dudau | 81f351d | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 345 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 2462139 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 346 | log_debug(" - %d '%s'\n", dev->seq_, dev->name); |
| 347 | if (dev->seq_ == seq) { |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 348 | *devp = dev; |
Kever Yang | 97b5f9d | 2019-10-22 15:39:47 +0800 | [diff] [blame] | 349 | log_debug(" - found\n"); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | } |
Kever Yang | 97b5f9d | 2019-10-22 15:39:47 +0800 | [diff] [blame] | 353 | log_debug(" - not found\n"); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 354 | |
| 355 | return -ENODEV; |
| 356 | } |
| 357 | |
Simon Glass | 1b30d61 | 2016-01-21 19:43:57 -0700 | [diff] [blame] | 358 | int uclass_find_device_by_of_offset(enum uclass_id id, int node, |
| 359 | struct udevice **devp) |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 360 | { |
| 361 | struct uclass *uc; |
| 362 | struct udevice *dev; |
| 363 | int ret; |
| 364 | |
| 365 | *devp = NULL; |
| 366 | if (node < 0) |
| 367 | return -ENODEV; |
| 368 | ret = uclass_get(id, &uc); |
| 369 | if (ret) |
| 370 | return ret; |
| 371 | |
Liviu Dudau | 81f351d | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 372 | uclass_foreach_dev(dev, uc) { |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 373 | if (dev_of_offset(dev) == node) { |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 374 | *devp = dev; |
| 375 | return 0; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return -ENODEV; |
| 380 | } |
| 381 | |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 382 | int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node, |
| 383 | struct udevice **devp) |
| 384 | { |
| 385 | struct uclass *uc; |
| 386 | struct udevice *dev; |
| 387 | int ret; |
| 388 | |
Simon Glass | 31e60ff | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 389 | log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node)); |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 390 | *devp = NULL; |
| 391 | if (!ofnode_valid(node)) |
| 392 | return -ENODEV; |
| 393 | ret = uclass_get(id, &uc); |
| 394 | if (ret) |
| 395 | return ret; |
| 396 | |
Liviu Dudau | 81f351d | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 397 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 31e60ff | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 398 | log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n", |
| 399 | dev->name); |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 400 | if (ofnode_equal(dev_ofnode(dev), node)) { |
| 401 | *devp = dev; |
Simon Glass | 31e60ff | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 402 | goto done; |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 403 | } |
| 404 | } |
Simon Glass | 31e60ff | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 405 | ret = -ENODEV; |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 406 | |
Simon Glass | 31e60ff | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 407 | done: |
| 408 | log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", |
| 409 | ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret); |
| 410 | return ret; |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 411 | } |
| 412 | |
Simon Glass | 414cc15 | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 413 | #if CONFIG_IS_ENABLED(OF_REAL) |
Rasmus Villemoes | 6ebb357 | 2023-04-13 17:16:11 +0200 | [diff] [blame] | 414 | static int uclass_find_device_by_phandle_id(enum uclass_id id, |
| 415 | uint find_phandle, |
| 416 | struct udevice **devp) |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 417 | { |
| 418 | struct udevice *dev; |
| 419 | struct uclass *uc; |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 420 | int ret; |
| 421 | |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 422 | ret = uclass_get(id, &uc); |
| 423 | if (ret) |
| 424 | return ret; |
| 425 | |
Liviu Dudau | 81f351d | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 426 | uclass_foreach_dev(dev, uc) { |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 427 | uint phandle; |
| 428 | |
Simon Glass | a40cc8e | 2017-05-18 20:09:12 -0600 | [diff] [blame] | 429 | phandle = dev_read_phandle(dev); |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 430 | |
| 431 | if (phandle == find_phandle) { |
| 432 | *devp = dev; |
| 433 | return 0; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return -ENODEV; |
| 438 | } |
Rasmus Villemoes | 6ebb357 | 2023-04-13 17:16:11 +0200 | [diff] [blame] | 439 | |
| 440 | int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent, |
| 441 | const char *name, struct udevice **devp) |
| 442 | { |
| 443 | int find_phandle; |
| 444 | |
| 445 | *devp = NULL; |
| 446 | find_phandle = dev_read_u32_default(parent, name, -1); |
| 447 | if (find_phandle <= 0) |
| 448 | return -ENOENT; |
| 449 | |
| 450 | return uclass_find_device_by_phandle_id(id, find_phandle, devp); |
| 451 | } |
Simon Glass | c275dfe | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 452 | #endif |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 453 | |
Simon Glass | c57f806 | 2016-07-17 15:23:15 -0600 | [diff] [blame] | 454 | int uclass_get_device_by_driver(enum uclass_id id, |
| 455 | const struct driver *find_drv, |
| 456 | struct udevice **devp) |
| 457 | { |
| 458 | struct udevice *dev; |
| 459 | struct uclass *uc; |
| 460 | int ret; |
| 461 | |
| 462 | ret = uclass_get(id, &uc); |
| 463 | if (ret) |
| 464 | return ret; |
| 465 | |
Liviu Dudau | 81f351d | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 466 | uclass_foreach_dev(dev, uc) { |
Simon Glass | c57f806 | 2016-07-17 15:23:15 -0600 | [diff] [blame] | 467 | if (dev->driver == find_drv) |
| 468 | return uclass_get_device_tail(dev, 0, devp); |
| 469 | } |
| 470 | |
| 471 | return -ENODEV; |
| 472 | } |
| 473 | |
Simon Glass | 30a570a | 2017-04-23 20:10:43 -0600 | [diff] [blame] | 474 | int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 475 | { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 476 | if (ret) |
| 477 | return ret; |
| 478 | |
Simon Glass | f66529f | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 479 | assert(dev); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 480 | ret = device_probe(dev); |
| 481 | if (ret) |
| 482 | return ret; |
| 483 | |
| 484 | *devp = dev; |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Simon Glass | 9ca296a | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 489 | int uclass_get_device(enum uclass_id id, int index, struct udevice **devp) |
| 490 | { |
| 491 | struct udevice *dev; |
| 492 | int ret; |
| 493 | |
| 494 | *devp = NULL; |
| 495 | ret = uclass_find_device(id, index, &dev); |
| 496 | return uclass_get_device_tail(dev, ret, devp); |
| 497 | } |
| 498 | |
Przemyslaw Marczak | b7af1a2 | 2015-04-15 13:07:23 +0200 | [diff] [blame] | 499 | int uclass_get_device_by_name(enum uclass_id id, const char *name, |
| 500 | struct udevice **devp) |
| 501 | { |
| 502 | struct udevice *dev; |
| 503 | int ret; |
| 504 | |
| 505 | *devp = NULL; |
| 506 | ret = uclass_find_device_by_name(id, name, &dev); |
| 507 | return uclass_get_device_tail(dev, ret, devp); |
| 508 | } |
| 509 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 510 | int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
| 511 | { |
| 512 | struct udevice *dev; |
| 513 | int ret; |
| 514 | |
| 515 | *devp = NULL; |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 516 | ret = uclass_find_device_by_seq(id, seq, &dev); |
| 517 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 518 | return uclass_get_device_tail(dev, ret, devp); |
| 519 | } |
| 520 | |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 521 | int uclass_get_device_by_of_offset(enum uclass_id id, int node, |
| 522 | struct udevice **devp) |
| 523 | { |
| 524 | struct udevice *dev; |
| 525 | int ret; |
| 526 | |
| 527 | *devp = NULL; |
| 528 | ret = uclass_find_device_by_of_offset(id, node, &dev); |
| 529 | return uclass_get_device_tail(dev, ret, devp); |
| 530 | } |
| 531 | |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 532 | int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node, |
| 533 | struct udevice **devp) |
| 534 | { |
| 535 | struct udevice *dev; |
| 536 | int ret; |
| 537 | |
Simon Glass | 31e60ff | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 538 | log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node)); |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 539 | *devp = NULL; |
| 540 | ret = uclass_find_device_by_ofnode(id, node, &dev); |
Simon Glass | 31e60ff | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 541 | log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", |
| 542 | ofnode_get_name(node), dev ? dev->name : "(none)", ret); |
Simon Glass | 40bb637 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 543 | |
| 544 | return uclass_get_device_tail(dev, ret, devp); |
| 545 | } |
| 546 | |
Rasmus Villemoes | 6ebb357 | 2023-04-13 17:16:11 +0200 | [diff] [blame] | 547 | #if CONFIG_IS_ENABLED(OF_REAL) |
Rasmus Villemoes | ca031c0 | 2023-04-13 17:17:03 +0200 | [diff] [blame] | 548 | int uclass_get_device_by_of_path(enum uclass_id id, const char *path, |
| 549 | struct udevice **devp) |
| 550 | { |
| 551 | return uclass_get_device_by_ofnode(id, ofnode_path(path), devp); |
| 552 | } |
| 553 | |
Kever Yang | d255fad | 2018-02-09 10:56:23 +0800 | [diff] [blame] | 554 | int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id, |
| 555 | struct udevice **devp) |
| 556 | { |
| 557 | struct udevice *dev; |
Kever Yang | d255fad | 2018-02-09 10:56:23 +0800 | [diff] [blame] | 558 | int ret; |
| 559 | |
| 560 | *devp = NULL; |
Rasmus Villemoes | 6ebb357 | 2023-04-13 17:16:11 +0200 | [diff] [blame] | 561 | ret = uclass_find_device_by_phandle_id(id, phandle_id, &dev); |
| 562 | return uclass_get_device_tail(dev, ret, devp); |
Kever Yang | d255fad | 2018-02-09 10:56:23 +0800 | [diff] [blame] | 563 | } |
| 564 | |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 565 | int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent, |
| 566 | const char *name, struct udevice **devp) |
| 567 | { |
| 568 | struct udevice *dev; |
| 569 | int ret; |
| 570 | |
| 571 | *devp = NULL; |
| 572 | ret = uclass_find_device_by_phandle(id, parent, name, &dev); |
| 573 | return uclass_get_device_tail(dev, ret, devp); |
| 574 | } |
Simon Glass | c275dfe | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 575 | #endif |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 576 | |
Michal Suchanek | f219547 | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 577 | /* |
| 578 | * Starting from the given device @dev, return pointer to the first device in |
| 579 | * the uclass that probes successfully in @devp. |
| 580 | */ |
| 581 | static void _uclass_next_device(struct udevice *dev, struct udevice **devp) |
| 582 | { |
| 583 | for (; dev; uclass_find_next_device(&dev)) { |
| 584 | if (!device_probe(dev)) |
| 585 | break; |
| 586 | } |
| 587 | *devp = dev; |
| 588 | } |
| 589 | |
| 590 | void uclass_first_device(enum uclass_id id, struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 591 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 592 | struct udevice *dev; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 593 | int ret; |
| 594 | |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 595 | ret = uclass_find_first_device(id, &dev); |
Michal Suchanek | f219547 | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 596 | _uclass_next_device(dev, devp); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Michal Suchanek | f219547 | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 599 | void uclass_next_device(struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 600 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 601 | struct udevice *dev = *devp; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 602 | |
Michal Suchanek | f219547 | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 603 | uclass_find_next_device(&dev); |
| 604 | _uclass_next_device(dev, devp); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Michal Suchanek | e44d7e7 | 2022-10-12 21:58:06 +0200 | [diff] [blame] | 607 | int uclass_first_device_err(enum uclass_id id, struct udevice **devp) |
| 608 | { |
| 609 | int ret; |
| 610 | |
| 611 | ret = uclass_first_device_check(id, devp); |
| 612 | if (ret) |
| 613 | return ret; |
| 614 | else if (!*devp) |
| 615 | return -ENODEV; |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
Patrice Chotard | f6abd53 | 2018-10-24 14:10:15 +0200 | [diff] [blame] | 620 | int uclass_next_device_err(struct udevice **devp) |
| 621 | { |
| 622 | int ret; |
| 623 | |
Michal Suchanek | e44d7e7 | 2022-10-12 21:58:06 +0200 | [diff] [blame] | 624 | ret = uclass_next_device_check(devp); |
Patrice Chotard | f6abd53 | 2018-10-24 14:10:15 +0200 | [diff] [blame] | 625 | if (ret) |
| 626 | return ret; |
| 627 | else if (!*devp) |
| 628 | return -ENODEV; |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 633 | int uclass_first_device_check(enum uclass_id id, struct udevice **devp) |
| 634 | { |
| 635 | int ret; |
| 636 | |
| 637 | *devp = NULL; |
| 638 | ret = uclass_find_first_device(id, devp); |
| 639 | if (ret) |
| 640 | return ret; |
| 641 | if (!*devp) |
| 642 | return 0; |
| 643 | |
| 644 | return device_probe(*devp); |
| 645 | } |
| 646 | |
| 647 | int uclass_next_device_check(struct udevice **devp) |
| 648 | { |
| 649 | int ret; |
| 650 | |
| 651 | ret = uclass_find_next_device(devp); |
| 652 | if (ret) |
| 653 | return ret; |
| 654 | if (!*devp) |
| 655 | return 0; |
| 656 | |
| 657 | return device_probe(*devp); |
| 658 | } |
| 659 | |
Simon Glass | 6476c4d | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 660 | int uclass_get_count(void) |
| 661 | { |
| 662 | const struct uclass *uc; |
| 663 | int count = 0; |
| 664 | |
| 665 | if (gd->dm_root) { |
| 666 | list_for_each_entry(uc, gd->uclass_root, sibling_node) |
| 667 | count++; |
| 668 | } |
| 669 | |
| 670 | return count; |
| 671 | } |
| 672 | |
Simon Glass | 5016234 | 2020-02-06 09:54:50 -0700 | [diff] [blame] | 673 | int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data, |
| 674 | struct udevice **devp) |
| 675 | { |
| 676 | struct udevice *dev; |
| 677 | struct uclass *uc; |
| 678 | |
| 679 | uclass_id_foreach_dev(id, dev, uc) { |
| 680 | if (dev_get_driver_data(dev) == driver_data) { |
| 681 | *devp = dev; |
| 682 | |
| 683 | return device_probe(dev); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | return -ENODEV; |
| 688 | } |
| 689 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 690 | int uclass_bind_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 691 | { |
| 692 | struct uclass *uc; |
| 693 | int ret; |
| 694 | |
| 695 | uc = dev->uclass; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 696 | list_add_tail(&dev->uclass_node, &uc->dev_head); |
| 697 | |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 698 | if (dev->parent) { |
| 699 | struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv; |
| 700 | |
| 701 | if (uc_drv->child_post_bind) { |
| 702 | ret = uc_drv->child_post_bind(dev); |
| 703 | if (ret) |
| 704 | goto err; |
| 705 | } |
| 706 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 707 | |
| 708 | return 0; |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 709 | err: |
| 710 | /* There is no need to undo the parent's post_bind call */ |
| 711 | list_del(&dev->uclass_node); |
| 712 | |
| 713 | return ret; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Masahiro Yamada | 0a5804b | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 716 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Simon Glass | 8044318 | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 717 | int uclass_pre_unbind_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 718 | { |
| 719 | struct uclass *uc; |
| 720 | int ret; |
| 721 | |
| 722 | uc = dev->uclass; |
| 723 | if (uc->uc_drv->pre_unbind) { |
| 724 | ret = uc->uc_drv->pre_unbind(dev); |
| 725 | if (ret) |
| 726 | return ret; |
| 727 | } |
| 728 | |
Simon Glass | 8044318 | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | int uclass_unbind_device(struct udevice *dev) |
| 733 | { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 734 | list_del(&dev->uclass_node); |
Simon Glass | 8044318 | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 735 | |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 736 | return 0; |
| 737 | } |
Simon Glass | 7f9875e | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 738 | #endif |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 739 | |
Simon Glass | 02c07b3 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 740 | int uclass_pre_probe_device(struct udevice *dev) |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 741 | { |
| 742 | struct uclass_driver *uc_drv; |
Simon Glass | 02c07b3 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 743 | int ret; |
| 744 | |
| 745 | uc_drv = dev->uclass->uc_drv; |
| 746 | if (uc_drv->pre_probe) { |
| 747 | ret = uc_drv->pre_probe(dev); |
| 748 | if (ret) |
| 749 | return ret; |
| 750 | } |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 751 | |
| 752 | if (!dev->parent) |
| 753 | return 0; |
| 754 | uc_drv = dev->parent->uclass->uc_drv; |
Simon Glass | c8b31cc | 2019-09-25 08:55:47 -0600 | [diff] [blame] | 755 | if (uc_drv->child_pre_probe) { |
| 756 | ret = uc_drv->child_pre_probe(dev); |
| 757 | if (ret) |
| 758 | return ret; |
| 759 | } |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 764 | int uclass_post_probe_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 765 | { |
Bin Meng | 651d0c0 | 2018-10-15 02:20:57 -0700 | [diff] [blame] | 766 | struct uclass_driver *uc_drv; |
| 767 | int ret; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 768 | |
Bin Meng | 651d0c0 | 2018-10-15 02:20:57 -0700 | [diff] [blame] | 769 | if (dev->parent) { |
| 770 | uc_drv = dev->parent->uclass->uc_drv; |
| 771 | if (uc_drv->child_post_probe) { |
| 772 | ret = uc_drv->child_post_probe(dev); |
| 773 | if (ret) |
| 774 | return ret; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | uc_drv = dev->uclass->uc_drv; |
Simon Glass | c8b31cc | 2019-09-25 08:55:47 -0600 | [diff] [blame] | 779 | if (uc_drv->post_probe) { |
| 780 | ret = uc_drv->post_probe(dev); |
| 781 | if (ret) |
| 782 | return ret; |
| 783 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 784 | |
| 785 | return 0; |
| 786 | } |
| 787 | |
Masahiro Yamada | 0a5804b | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 788 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 789 | int uclass_pre_remove_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 790 | { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 791 | struct uclass *uc; |
| 792 | int ret; |
| 793 | |
| 794 | uc = dev->uclass; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 795 | if (uc->uc_drv->pre_remove) { |
| 796 | ret = uc->uc_drv->pre_remove(dev); |
| 797 | if (ret) |
| 798 | return ret; |
| 799 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 800 | |
| 801 | return 0; |
| 802 | } |
Simon Glass | 7f9875e | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 803 | #endif |
Jean-Jacques Hiblot | 07e3371 | 2019-07-05 09:33:57 +0200 | [diff] [blame] | 804 | |
Vabhav Sharma | a59153d | 2020-12-09 10:42:03 +0530 | [diff] [blame] | 805 | int uclass_probe_all(enum uclass_id id) |
| 806 | { |
| 807 | struct udevice *dev; |
Michal Suchanek | dfecd63 | 2022-10-12 21:57:50 +0200 | [diff] [blame] | 808 | int ret, err; |
Vabhav Sharma | a59153d | 2020-12-09 10:42:03 +0530 | [diff] [blame] | 809 | |
Michal Suchanek | dfecd63 | 2022-10-12 21:57:50 +0200 | [diff] [blame] | 810 | err = uclass_first_device_check(id, &dev); |
Vabhav Sharma | a59153d | 2020-12-09 10:42:03 +0530 | [diff] [blame] | 811 | |
| 812 | /* Scanning uclass to probe all devices */ |
| 813 | while (dev) { |
Michal Suchanek | dfecd63 | 2022-10-12 21:57:50 +0200 | [diff] [blame] | 814 | ret = uclass_next_device_check(&dev); |
Vabhav Sharma | a59153d | 2020-12-09 10:42:03 +0530 | [diff] [blame] | 815 | if (ret) |
Michal Suchanek | dfecd63 | 2022-10-12 21:57:50 +0200 | [diff] [blame] | 816 | err = ret; |
Vabhav Sharma | a59153d | 2020-12-09 10:42:03 +0530 | [diff] [blame] | 817 | } |
| 818 | |
Michal Suchanek | dfecd63 | 2022-10-12 21:57:50 +0200 | [diff] [blame] | 819 | return err; |
Vabhav Sharma | a59153d | 2020-12-09 10:42:03 +0530 | [diff] [blame] | 820 | } |
| 821 | |
Simon Glass | 29fe555 | 2021-10-23 17:26:09 -0600 | [diff] [blame] | 822 | int uclass_id_count(enum uclass_id id) |
| 823 | { |
| 824 | struct udevice *dev; |
| 825 | struct uclass *uc; |
| 826 | int count = 0; |
| 827 | |
| 828 | uclass_id_foreach_dev(id, dev, uc) |
| 829 | count++; |
| 830 | |
| 831 | return count; |
| 832 | } |
| 833 | |
Jean-Jacques Hiblot | 07e3371 | 2019-07-05 09:33:57 +0200 | [diff] [blame] | 834 | UCLASS_DRIVER(nop) = { |
| 835 | .id = UCLASS_NOP, |
| 836 | .name = "nop", |
| 837 | }; |