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 | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 11 | #include <common.h> |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 13 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Simon Glass | dd6ab88 | 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 | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 30 | if (!gd->dm_root) |
| 31 | return NULL; |
Simon Glass | dd6ab88 | 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 | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 37 | list_for_each_entry(uc, gd->uclass_root, sibling_node) { |
Simon Glass | dd6ab88 | 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 | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 49 | * Return: 0 on success, -ve on error |
Simon Glass | dd6ab88 | 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 | 1d06ced | 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 | 43313de | 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 | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 71 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 72 | uc = calloc(1, sizeof(*uc)); |
| 73 | if (!uc) |
| 74 | return -ENOMEM; |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 75 | if (uc_drv->priv_auto) { |
Simon Glass | 2543b16 | 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 | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 80 | ret = -ENOMEM; |
| 81 | goto fail_mem; |
| 82 | } |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 83 | uclass_set_priv(uc, ptr); |
Simon Glass | dd6ab88 | 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 | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 88 | list_add(&uc->sibling_node, DM_UCLASS_ROOT_NON_CONST); |
Simon Glass | dd6ab88 | 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 | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 100 | if (uc_drv->priv_auto) { |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 101 | free(uclass_get_priv(uc)); |
| 102 | uclass_set_priv(uc, NULL); |
Simon Glass | dd6ab88 | 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 | f33663d | 2015-04-19 07:20:58 -0600 | [diff] [blame] | 114 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 115 | int ret; |
| 116 | |
Simon Glass | f33663d | 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 | 42697f5 | 2020-03-28 14:03:48 -0600 | [diff] [blame] | 126 | ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 127 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 128 | return log_msg_ret("remove", ret); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 129 | ret = device_unbind(dev); |
| 130 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 131 | return log_msg_ret("unbind", ret); |
Simon Glass | dd6ab88 | 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 | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 138 | if (uc_drv->priv_auto) |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 139 | free(uclass_get_priv(uc)); |
Simon Glass | dd6ab88 | 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 | 8e9eacf | 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 | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 152 | *ucp = NULL; |
| 153 | uc = uclass_find(id); |
Simon Glass | 05693e9 | 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 | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 157 | return uclass_add(id, ucp); |
Simon Glass | 05693e9 | 2021-03-15 17:25:18 +1300 | [diff] [blame] | 158 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 159 | *ucp = uc; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Simon Glass | d19d073 | 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 | 96404c2 | 2020-12-22 19:30:26 -0700 | [diff] [blame] | 173 | void *uclass_get_priv(const struct uclass *uc) |
| 174 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 175 | return uc->priv_; |
Simon Glass | 96404c2 | 2020-12-22 19:30:26 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void uclass_set_priv(struct uclass *uc, void *priv) |
| 179 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 180 | uc->priv_ = priv; |
Simon Glass | 96404c2 | 2020-12-22 19:30:26 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Simon Glass | f1f519f | 2022-04-24 23:30:59 -0600 | [diff] [blame^] | 183 | enum uclass_id uclass_get_by_namelen(const char *name, int len) |
Simon Glass | 70e35b4 | 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 | f1f519f | 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 | 70e35b4 | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 192 | return i; |
| 193 | } |
| 194 | |
| 195 | return UCLASS_INVALID; |
| 196 | } |
| 197 | |
Simon Glass | 4b23e45 | 2021-10-23 17:26:08 -0600 | [diff] [blame] | 198 | enum uclass_id uclass_get_by_name(const char *name) |
| 199 | { |
Simon Glass | f1f519f | 2022-04-24 23:30:59 -0600 | [diff] [blame^] | 200 | return uclass_get_by_namelen(name, strlen(name)); |
Simon Glass | 4b23e45 | 2021-10-23 17:26:08 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Jean-Jacques Hiblot | c4f0248 | 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 | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 212 | uclass_foreach_dev(iter, uc) { |
Jean-Jacques Hiblot | c4f0248 | 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 | b74fcb4 | 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 | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 225 | { |
| 226 | struct uclass *uc; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 227 | struct udevice *dev; |
Simon Glass | dd6ab88 | 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 | 46766df | 2015-07-31 09:31:19 -0600 | [diff] [blame] | 234 | if (list_empty(&uc->dev_head)) |
| 235 | return -ENODEV; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 236 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 237 | uclass_foreach_dev(dev, uc) { |
Simon Glass | dd6ab88 | 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 | f9d156e | 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 | 0bb4427 | 2019-09-25 08:55:55 -0600 | [diff] [blame] | 257 | return 0; |
Przemyslaw Marczak | f9d156e | 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 | |
Przemyslaw Marczak | 2ffdf14 | 2015-04-15 13:07:22 +0200 | [diff] [blame] | 277 | int uclass_find_device_by_name(enum uclass_id id, const char *name, |
| 278 | struct udevice **devp) |
| 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 | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 291 | uclass_foreach_dev(dev, uc) { |
Peng Fan | 26d68b2 | 2019-05-22 07:08:14 +0000 | [diff] [blame] | 292 | if (!strcmp(dev->name, name)) { |
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 | b941f56 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 301 | int uclass_find_next_free_seq(struct uclass *uc) |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 302 | { |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 303 | struct udevice *dev; |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 304 | int max = -1; |
| 305 | |
Simon Glass | b941f56 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 306 | /* If using aliases, start with the highest alias value */ |
| 307 | if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) && |
| 308 | (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) |
| 309 | max = dev_read_alias_highest_id(uc->uc_drv->name); |
| 310 | |
| 311 | /* Avoid conflict with existing devices */ |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 312 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 313 | if (dev->seq_ > max) |
| 314 | max = dev->seq_; |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 315 | } |
Simon Glass | b941f56 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 316 | /* |
| 317 | * At this point, max will be -1 if there are no existing aliases or |
| 318 | * devices |
| 319 | */ |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 320 | |
| 321 | return max + 1; |
| 322 | } |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 323 | |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 324 | 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] | 325 | { |
| 326 | struct uclass *uc; |
| 327 | struct udevice *dev; |
| 328 | int ret; |
| 329 | |
| 330 | *devp = NULL; |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 331 | log_debug("%d\n", seq); |
| 332 | if (seq == -1) |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 333 | return -ENODEV; |
| 334 | ret = uclass_get(id, &uc); |
| 335 | if (ret) |
| 336 | return ret; |
| 337 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 338 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 339 | log_debug(" - %d '%s'\n", dev->seq_, dev->name); |
| 340 | if (dev->seq_ == seq) { |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 341 | *devp = dev; |
Kever Yang | 5f09a2d | 2019-10-22 15:39:47 +0800 | [diff] [blame] | 342 | log_debug(" - found\n"); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | } |
Kever Yang | 5f09a2d | 2019-10-22 15:39:47 +0800 | [diff] [blame] | 346 | log_debug(" - not found\n"); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 347 | |
| 348 | return -ENODEV; |
| 349 | } |
| 350 | |
Simon Glass | 96f0444 | 2016-01-21 19:43:57 -0700 | [diff] [blame] | 351 | int uclass_find_device_by_of_offset(enum uclass_id id, int node, |
| 352 | struct udevice **devp) |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 353 | { |
| 354 | struct uclass *uc; |
| 355 | struct udevice *dev; |
| 356 | int ret; |
| 357 | |
| 358 | *devp = NULL; |
| 359 | if (node < 0) |
| 360 | return -ENODEV; |
| 361 | ret = uclass_get(id, &uc); |
| 362 | if (ret) |
| 363 | return ret; |
| 364 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 365 | uclass_foreach_dev(dev, uc) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 366 | if (dev_of_offset(dev) == node) { |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 367 | *devp = dev; |
| 368 | return 0; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return -ENODEV; |
| 373 | } |
| 374 | |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 375 | int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node, |
| 376 | struct udevice **devp) |
| 377 | { |
| 378 | struct uclass *uc; |
| 379 | struct udevice *dev; |
| 380 | int ret; |
| 381 | |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 382 | 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] | 383 | *devp = NULL; |
| 384 | if (!ofnode_valid(node)) |
| 385 | return -ENODEV; |
| 386 | ret = uclass_get(id, &uc); |
| 387 | if (ret) |
| 388 | return ret; |
| 389 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 390 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 391 | log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n", |
| 392 | dev->name); |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 393 | if (ofnode_equal(dev_ofnode(dev), node)) { |
| 394 | *devp = dev; |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 395 | goto done; |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 396 | } |
| 397 | } |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 398 | ret = -ENODEV; |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 399 | |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 400 | done: |
| 401 | log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", |
| 402 | ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret); |
| 403 | return ret; |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 404 | } |
| 405 | |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 406 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | dc3efdc | 2018-11-18 08:14:30 -0700 | [diff] [blame] | 407 | int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent, |
| 408 | const char *name, struct udevice **devp) |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 409 | { |
| 410 | struct udevice *dev; |
| 411 | struct uclass *uc; |
| 412 | int find_phandle; |
| 413 | int ret; |
| 414 | |
| 415 | *devp = NULL; |
Simon Glass | bfb6e3d | 2017-05-18 20:09:12 -0600 | [diff] [blame] | 416 | find_phandle = dev_read_u32_default(parent, name, -1); |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 417 | if (find_phandle <= 0) |
| 418 | return -ENOENT; |
| 419 | ret = uclass_get(id, &uc); |
| 420 | if (ret) |
| 421 | return ret; |
| 422 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 423 | uclass_foreach_dev(dev, uc) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 424 | uint phandle; |
| 425 | |
Simon Glass | bfb6e3d | 2017-05-18 20:09:12 -0600 | [diff] [blame] | 426 | phandle = dev_read_phandle(dev); |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 427 | |
| 428 | if (phandle == find_phandle) { |
| 429 | *devp = dev; |
| 430 | return 0; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return -ENODEV; |
| 435 | } |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 436 | #endif |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 437 | |
Simon Glass | 32d8ab6 | 2016-07-17 15:23:15 -0600 | [diff] [blame] | 438 | int uclass_get_device_by_driver(enum uclass_id id, |
| 439 | const struct driver *find_drv, |
| 440 | struct udevice **devp) |
| 441 | { |
| 442 | struct udevice *dev; |
| 443 | struct uclass *uc; |
| 444 | int ret; |
| 445 | |
| 446 | ret = uclass_get(id, &uc); |
| 447 | if (ret) |
| 448 | return ret; |
| 449 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 450 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 32d8ab6 | 2016-07-17 15:23:15 -0600 | [diff] [blame] | 451 | if (dev->driver == find_drv) |
| 452 | return uclass_get_device_tail(dev, 0, devp); |
| 453 | } |
| 454 | |
| 455 | return -ENODEV; |
| 456 | } |
| 457 | |
Simon Glass | 98c9fb0 | 2017-04-23 20:10:43 -0600 | [diff] [blame] | 458 | 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] | 459 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 460 | if (ret) |
| 461 | return ret; |
| 462 | |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 463 | assert(dev); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 464 | ret = device_probe(dev); |
| 465 | if (ret) |
| 466 | return ret; |
| 467 | |
| 468 | *devp = dev; |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Simon Glass | 4b67cef | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 473 | int uclass_get_device(enum uclass_id id, int index, struct udevice **devp) |
| 474 | { |
| 475 | struct udevice *dev; |
| 476 | int ret; |
| 477 | |
| 478 | *devp = NULL; |
| 479 | ret = uclass_find_device(id, index, &dev); |
| 480 | return uclass_get_device_tail(dev, ret, devp); |
| 481 | } |
| 482 | |
Przemyslaw Marczak | a12a1f5 | 2015-04-15 13:07:23 +0200 | [diff] [blame] | 483 | int uclass_get_device_by_name(enum uclass_id id, const char *name, |
| 484 | struct udevice **devp) |
| 485 | { |
| 486 | struct udevice *dev; |
| 487 | int ret; |
| 488 | |
| 489 | *devp = NULL; |
| 490 | ret = uclass_find_device_by_name(id, name, &dev); |
| 491 | return uclass_get_device_tail(dev, ret, devp); |
| 492 | } |
| 493 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 494 | int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
| 495 | { |
| 496 | struct udevice *dev; |
| 497 | int ret; |
| 498 | |
| 499 | *devp = NULL; |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 500 | ret = uclass_find_device_by_seq(id, seq, &dev); |
| 501 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 502 | return uclass_get_device_tail(dev, ret, devp); |
| 503 | } |
| 504 | |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 505 | int uclass_get_device_by_of_offset(enum uclass_id id, int node, |
| 506 | struct udevice **devp) |
| 507 | { |
| 508 | struct udevice *dev; |
| 509 | int ret; |
| 510 | |
| 511 | *devp = NULL; |
| 512 | ret = uclass_find_device_by_of_offset(id, node, &dev); |
| 513 | return uclass_get_device_tail(dev, ret, devp); |
| 514 | } |
| 515 | |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 516 | int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node, |
| 517 | struct udevice **devp) |
| 518 | { |
| 519 | struct udevice *dev; |
| 520 | int ret; |
| 521 | |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 522 | 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] | 523 | *devp = NULL; |
| 524 | ret = uclass_find_device_by_ofnode(id, node, &dev); |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 525 | log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", |
| 526 | ofnode_get_name(node), dev ? dev->name : "(none)", ret); |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 527 | |
| 528 | return uclass_get_device_tail(dev, ret, devp); |
| 529 | } |
| 530 | |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 531 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Kever Yang | 66a0b5a | 2018-02-09 10:56:23 +0800 | [diff] [blame] | 532 | int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id, |
| 533 | struct udevice **devp) |
| 534 | { |
| 535 | struct udevice *dev; |
| 536 | struct uclass *uc; |
| 537 | int ret; |
| 538 | |
| 539 | *devp = NULL; |
| 540 | ret = uclass_get(id, &uc); |
| 541 | if (ret) |
| 542 | return ret; |
| 543 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 544 | uclass_foreach_dev(dev, uc) { |
Kever Yang | 66a0b5a | 2018-02-09 10:56:23 +0800 | [diff] [blame] | 545 | uint phandle; |
| 546 | |
| 547 | phandle = dev_read_phandle(dev); |
| 548 | |
| 549 | if (phandle == phandle_id) { |
| 550 | *devp = dev; |
| 551 | return uclass_get_device_tail(dev, ret, devp); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | return -ENODEV; |
| 556 | } |
| 557 | |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 558 | int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent, |
| 559 | const char *name, struct udevice **devp) |
| 560 | { |
| 561 | struct udevice *dev; |
| 562 | int ret; |
| 563 | |
| 564 | *devp = NULL; |
| 565 | ret = uclass_find_device_by_phandle(id, parent, name, &dev); |
| 566 | return uclass_get_device_tail(dev, ret, devp); |
| 567 | } |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 568 | #endif |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 569 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 570 | int uclass_first_device(enum uclass_id id, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 571 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 572 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 573 | int ret; |
| 574 | |
| 575 | *devp = NULL; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 576 | ret = uclass_find_first_device(id, &dev); |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 577 | if (!dev) |
| 578 | return 0; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 579 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Simon Glass | 832c3f0 | 2016-02-11 13:23:25 -0700 | [diff] [blame] | 582 | int uclass_first_device_err(enum uclass_id id, struct udevice **devp) |
| 583 | { |
| 584 | int ret; |
| 585 | |
| 586 | ret = uclass_first_device(id, devp); |
| 587 | if (ret) |
| 588 | return ret; |
| 589 | else if (!*devp) |
| 590 | return -ENODEV; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 595 | int uclass_next_device(struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 596 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 597 | struct udevice *dev = *devp; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 598 | int ret; |
| 599 | |
| 600 | *devp = NULL; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 601 | ret = uclass_find_next_device(&dev); |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 602 | if (!dev) |
| 603 | return 0; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 604 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Patrice Chotard | 4dff178 | 2018-10-24 14:10:15 +0200 | [diff] [blame] | 607 | int uclass_next_device_err(struct udevice **devp) |
| 608 | { |
| 609 | int ret; |
| 610 | |
| 611 | ret = uclass_next_device(devp); |
| 612 | if (ret) |
| 613 | return ret; |
| 614 | else if (!*devp) |
| 615 | return -ENODEV; |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
Simon Glass | 3ff83bc | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 620 | int uclass_first_device_check(enum uclass_id id, struct udevice **devp) |
| 621 | { |
| 622 | int ret; |
| 623 | |
| 624 | *devp = NULL; |
| 625 | ret = uclass_find_first_device(id, devp); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | if (!*devp) |
| 629 | return 0; |
| 630 | |
| 631 | return device_probe(*devp); |
| 632 | } |
| 633 | |
| 634 | int uclass_next_device_check(struct udevice **devp) |
| 635 | { |
| 636 | int ret; |
| 637 | |
| 638 | ret = uclass_find_next_device(devp); |
| 639 | if (ret) |
| 640 | return ret; |
| 641 | if (!*devp) |
| 642 | return 0; |
| 643 | |
| 644 | return device_probe(*devp); |
| 645 | } |
| 646 | |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 647 | int uclass_get_count(void) |
| 648 | { |
| 649 | const struct uclass *uc; |
| 650 | int count = 0; |
| 651 | |
| 652 | if (gd->dm_root) { |
| 653 | list_for_each_entry(uc, gd->uclass_root, sibling_node) |
| 654 | count++; |
| 655 | } |
| 656 | |
| 657 | return count; |
| 658 | } |
| 659 | |
Simon Glass | 35cb2a4 | 2020-02-06 09:54:50 -0700 | [diff] [blame] | 660 | int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data, |
| 661 | struct udevice **devp) |
| 662 | { |
| 663 | struct udevice *dev; |
| 664 | struct uclass *uc; |
| 665 | |
| 666 | uclass_id_foreach_dev(id, dev, uc) { |
| 667 | if (dev_get_driver_data(dev) == driver_data) { |
| 668 | *devp = dev; |
| 669 | |
| 670 | return device_probe(dev); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | return -ENODEV; |
| 675 | } |
| 676 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 677 | int uclass_bind_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 678 | { |
| 679 | struct uclass *uc; |
| 680 | int ret; |
| 681 | |
| 682 | uc = dev->uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 683 | list_add_tail(&dev->uclass_node, &uc->dev_head); |
| 684 | |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 685 | if (dev->parent) { |
| 686 | struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv; |
| 687 | |
| 688 | if (uc_drv->child_post_bind) { |
| 689 | ret = uc_drv->child_post_bind(dev); |
| 690 | if (ret) |
| 691 | goto err; |
| 692 | } |
| 693 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 694 | |
| 695 | return 0; |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 696 | err: |
| 697 | /* There is no need to undo the parent's post_bind call */ |
| 698 | list_del(&dev->uclass_node); |
| 699 | |
| 700 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 703 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Simon Glass | ce15bfb | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 704 | int uclass_pre_unbind_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 705 | { |
| 706 | struct uclass *uc; |
| 707 | int ret; |
| 708 | |
| 709 | uc = dev->uclass; |
| 710 | if (uc->uc_drv->pre_unbind) { |
| 711 | ret = uc->uc_drv->pre_unbind(dev); |
| 712 | if (ret) |
| 713 | return ret; |
| 714 | } |
| 715 | |
Simon Glass | ce15bfb | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | int uclass_unbind_device(struct udevice *dev) |
| 720 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 721 | list_del(&dev->uclass_node); |
Simon Glass | ce15bfb | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 722 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 723 | return 0; |
| 724 | } |
Simon Glass | 8914c8a | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 725 | #endif |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 726 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 727 | int uclass_pre_probe_device(struct udevice *dev) |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 728 | { |
| 729 | struct uclass_driver *uc_drv; |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 730 | int ret; |
| 731 | |
| 732 | uc_drv = dev->uclass->uc_drv; |
| 733 | if (uc_drv->pre_probe) { |
| 734 | ret = uc_drv->pre_probe(dev); |
| 735 | if (ret) |
| 736 | return ret; |
| 737 | } |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 738 | |
| 739 | if (!dev->parent) |
| 740 | return 0; |
| 741 | uc_drv = dev->parent->uclass->uc_drv; |
Simon Glass | 256d402 | 2019-09-25 08:55:47 -0600 | [diff] [blame] | 742 | if (uc_drv->child_pre_probe) { |
| 743 | ret = uc_drv->child_pre_probe(dev); |
| 744 | if (ret) |
| 745 | return ret; |
| 746 | } |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 751 | int uclass_post_probe_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 752 | { |
Bin Meng | 30569cf | 2018-10-15 02:20:57 -0700 | [diff] [blame] | 753 | struct uclass_driver *uc_drv; |
| 754 | int ret; |
| 755 | |
| 756 | if (dev->parent) { |
| 757 | uc_drv = dev->parent->uclass->uc_drv; |
| 758 | if (uc_drv->child_post_probe) { |
| 759 | ret = uc_drv->child_post_probe(dev); |
| 760 | if (ret) |
| 761 | return ret; |
| 762 | } |
| 763 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 764 | |
Bin Meng | 30569cf | 2018-10-15 02:20:57 -0700 | [diff] [blame] | 765 | uc_drv = dev->uclass->uc_drv; |
Simon Glass | 256d402 | 2019-09-25 08:55:47 -0600 | [diff] [blame] | 766 | if (uc_drv->post_probe) { |
| 767 | ret = uc_drv->post_probe(dev); |
| 768 | if (ret) |
| 769 | return ret; |
| 770 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 775 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 776 | int uclass_pre_remove_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 777 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 778 | struct uclass *uc; |
| 779 | int ret; |
| 780 | |
| 781 | uc = dev->uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 782 | if (uc->uc_drv->pre_remove) { |
| 783 | ret = uc->uc_drv->pre_remove(dev); |
| 784 | if (ret) |
| 785 | return ret; |
| 786 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 787 | |
| 788 | return 0; |
| 789 | } |
Simon Glass | 8914c8a | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 790 | #endif |
Jean-Jacques Hiblot | db97c7f | 2019-07-05 09:33:57 +0200 | [diff] [blame] | 791 | |
Vabhav Sharma | bcab47a | 2020-12-09 10:42:03 +0530 | [diff] [blame] | 792 | int uclass_probe_all(enum uclass_id id) |
| 793 | { |
| 794 | struct udevice *dev; |
| 795 | int ret; |
| 796 | |
| 797 | ret = uclass_first_device(id, &dev); |
| 798 | if (ret || !dev) |
| 799 | return ret; |
| 800 | |
| 801 | /* Scanning uclass to probe all devices */ |
| 802 | while (dev) { |
| 803 | ret = uclass_next_device(&dev); |
| 804 | if (ret) |
| 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
Simon Glass | dab7e0d | 2021-10-23 17:26:09 -0600 | [diff] [blame] | 811 | int uclass_id_count(enum uclass_id id) |
| 812 | { |
| 813 | struct udevice *dev; |
| 814 | struct uclass *uc; |
| 815 | int count = 0; |
| 816 | |
| 817 | uclass_id_foreach_dev(id, dev, uc) |
| 818 | count++; |
| 819 | |
| 820 | return count; |
| 821 | } |
| 822 | |
Jean-Jacques Hiblot | db97c7f | 2019-07-05 09:33:57 +0200 | [diff] [blame] | 823 | UCLASS_DRIVER(nop) = { |
| 824 | .id = UCLASS_NOP, |
| 825 | .name = "nop", |
| 826 | }; |