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