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