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> |
| 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 |
| 48 | * @return 0 on success, -ve on error |
| 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) { |
Masahiro Yamada | 1d06ced | 2015-07-07 18:51:32 +0900 | [diff] [blame] | 62 | debug("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 | |
| 148 | *ucp = NULL; |
| 149 | uc = uclass_find(id); |
| 150 | if (!uc) |
| 151 | return uclass_add(id, ucp); |
| 152 | *ucp = uc; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Simon Glass | d19d073 | 2016-10-05 20:42:13 -0600 | [diff] [blame] | 157 | const char *uclass_get_name(enum uclass_id id) |
| 158 | { |
| 159 | struct uclass *uc; |
| 160 | |
| 161 | if (uclass_get(id, &uc)) |
| 162 | return NULL; |
| 163 | return uc->uc_drv->name; |
| 164 | } |
| 165 | |
Simon Glass | 96404c2 | 2020-12-22 19:30:26 -0700 | [diff] [blame] | 166 | void *uclass_get_priv(const struct uclass *uc) |
| 167 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 168 | return uc->priv_; |
Simon Glass | 96404c2 | 2020-12-22 19:30:26 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void uclass_set_priv(struct uclass *uc, void *priv) |
| 172 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 173 | uc->priv_ = priv; |
Simon Glass | 96404c2 | 2020-12-22 19:30:26 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Simon Glass | 70e35b4 | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 176 | enum uclass_id uclass_get_by_name(const char *name) |
| 177 | { |
| 178 | int i; |
| 179 | |
| 180 | for (i = 0; i < UCLASS_COUNT; i++) { |
| 181 | struct uclass_driver *uc_drv = lists_uclass_lookup(i); |
| 182 | |
| 183 | if (uc_drv && !strcmp(uc_drv->name, name)) |
| 184 | return i; |
| 185 | } |
| 186 | |
| 187 | return UCLASS_INVALID; |
| 188 | } |
| 189 | |
Jean-Jacques Hiblot | c4f0248 | 2018-08-09 16:17:42 +0200 | [diff] [blame] | 190 | int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp) |
| 191 | { |
| 192 | struct udevice *iter; |
| 193 | struct uclass *uc = dev->uclass; |
| 194 | int i = 0; |
| 195 | |
| 196 | if (list_empty(&uc->dev_head)) |
| 197 | return -ENODEV; |
| 198 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 199 | uclass_foreach_dev(iter, uc) { |
Jean-Jacques Hiblot | c4f0248 | 2018-08-09 16:17:42 +0200 | [diff] [blame] | 200 | if (iter == dev) { |
| 201 | if (ucp) |
| 202 | *ucp = uc; |
| 203 | return i; |
| 204 | } |
| 205 | i++; |
| 206 | } |
| 207 | |
| 208 | return -ENODEV; |
| 209 | } |
| 210 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 211 | 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] | 212 | { |
| 213 | struct uclass *uc; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 214 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 215 | int ret; |
| 216 | |
| 217 | *devp = NULL; |
| 218 | ret = uclass_get(id, &uc); |
| 219 | if (ret) |
| 220 | return ret; |
Simon Glass | 46766df | 2015-07-31 09:31:19 -0600 | [diff] [blame] | 221 | if (list_empty(&uc->dev_head)) |
| 222 | return -ENODEV; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 223 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 224 | uclass_foreach_dev(dev, uc) { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 225 | if (!index--) { |
| 226 | *devp = dev; |
| 227 | return 0; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return -ENODEV; |
| 232 | } |
| 233 | |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 234 | int uclass_find_first_device(enum uclass_id id, struct udevice **devp) |
| 235 | { |
| 236 | struct uclass *uc; |
| 237 | int ret; |
| 238 | |
| 239 | *devp = NULL; |
| 240 | ret = uclass_get(id, &uc); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | if (list_empty(&uc->dev_head)) |
Simon Glass | 0bb4427 | 2019-09-25 08:55:55 -0600 | [diff] [blame] | 244 | return 0; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 245 | |
| 246 | *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | int uclass_find_next_device(struct udevice **devp) |
| 252 | { |
| 253 | struct udevice *dev = *devp; |
| 254 | |
| 255 | *devp = NULL; |
| 256 | if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head)) |
| 257 | return 0; |
| 258 | |
| 259 | *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Przemyslaw Marczak | 2ffdf14 | 2015-04-15 13:07:22 +0200 | [diff] [blame] | 264 | int uclass_find_device_by_name(enum uclass_id id, const char *name, |
| 265 | struct udevice **devp) |
| 266 | { |
| 267 | struct uclass *uc; |
| 268 | struct udevice *dev; |
| 269 | int ret; |
| 270 | |
| 271 | *devp = NULL; |
| 272 | if (!name) |
| 273 | return -EINVAL; |
| 274 | ret = uclass_get(id, &uc); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 278 | uclass_foreach_dev(dev, uc) { |
Peng Fan | 26d68b2 | 2019-05-22 07:08:14 +0000 | [diff] [blame] | 279 | if (!strcmp(dev->name, name)) { |
Przemyslaw Marczak | 2ffdf14 | 2015-04-15 13:07:22 +0200 | [diff] [blame] | 280 | *devp = dev; |
| 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return -ENODEV; |
| 286 | } |
| 287 | |
Simon Glass | b941f56 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 288 | int uclass_find_next_free_seq(struct uclass *uc) |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 289 | { |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 290 | struct udevice *dev; |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 291 | int max = -1; |
| 292 | |
Simon Glass | b941f56 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 293 | /* If using aliases, start with the highest alias value */ |
| 294 | if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) && |
| 295 | (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) |
| 296 | max = dev_read_alias_highest_id(uc->uc_drv->name); |
| 297 | |
| 298 | /* Avoid conflict with existing devices */ |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 299 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 300 | if (dev->seq_ > max) |
| 301 | max = dev->seq_; |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 302 | } |
Simon Glass | b941f56 | 2020-12-16 21:20:30 -0700 | [diff] [blame] | 303 | /* |
| 304 | * At this point, max will be -1 if there are no existing aliases or |
| 305 | * devices |
| 306 | */ |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 307 | |
| 308 | return max + 1; |
| 309 | } |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 310 | |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 311 | 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] | 312 | { |
| 313 | struct uclass *uc; |
| 314 | struct udevice *dev; |
| 315 | int ret; |
| 316 | |
| 317 | *devp = NULL; |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 318 | log_debug("%d\n", seq); |
| 319 | if (seq == -1) |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 320 | return -ENODEV; |
| 321 | ret = uclass_get(id, &uc); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 325 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 326 | log_debug(" - %d '%s'\n", dev->seq_, dev->name); |
| 327 | if (dev->seq_ == seq) { |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 328 | *devp = dev; |
Kever Yang | 5f09a2d | 2019-10-22 15:39:47 +0800 | [diff] [blame] | 329 | log_debug(" - found\n"); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | } |
Kever Yang | 5f09a2d | 2019-10-22 15:39:47 +0800 | [diff] [blame] | 333 | log_debug(" - not found\n"); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 334 | |
| 335 | return -ENODEV; |
| 336 | } |
| 337 | |
Simon Glass | 96f0444 | 2016-01-21 19:43:57 -0700 | [diff] [blame] | 338 | int uclass_find_device_by_of_offset(enum uclass_id id, int node, |
| 339 | struct udevice **devp) |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 340 | { |
| 341 | struct uclass *uc; |
| 342 | struct udevice *dev; |
| 343 | int ret; |
| 344 | |
| 345 | *devp = NULL; |
| 346 | if (node < 0) |
| 347 | return -ENODEV; |
| 348 | ret = uclass_get(id, &uc); |
| 349 | if (ret) |
| 350 | return ret; |
| 351 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 352 | uclass_foreach_dev(dev, uc) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 353 | if (dev_of_offset(dev) == node) { |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 354 | *devp = dev; |
| 355 | return 0; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return -ENODEV; |
| 360 | } |
| 361 | |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 362 | int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node, |
| 363 | struct udevice **devp) |
| 364 | { |
| 365 | struct uclass *uc; |
| 366 | struct udevice *dev; |
| 367 | int ret; |
| 368 | |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 369 | 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] | 370 | *devp = NULL; |
| 371 | if (!ofnode_valid(node)) |
| 372 | return -ENODEV; |
| 373 | ret = uclass_get(id, &uc); |
| 374 | if (ret) |
| 375 | return ret; |
| 376 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 377 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 378 | log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n", |
| 379 | dev->name); |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 380 | if (ofnode_equal(dev_ofnode(dev), node)) { |
| 381 | *devp = dev; |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 382 | goto done; |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 383 | } |
| 384 | } |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 385 | ret = -ENODEV; |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 386 | |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 387 | done: |
| 388 | log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", |
| 389 | ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret); |
| 390 | return ret; |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 391 | } |
| 392 | |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 393 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | dc3efdc | 2018-11-18 08:14:30 -0700 | [diff] [blame] | 394 | int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent, |
| 395 | const char *name, struct udevice **devp) |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 396 | { |
| 397 | struct udevice *dev; |
| 398 | struct uclass *uc; |
| 399 | int find_phandle; |
| 400 | int ret; |
| 401 | |
| 402 | *devp = NULL; |
Simon Glass | bfb6e3d | 2017-05-18 20:09:12 -0600 | [diff] [blame] | 403 | find_phandle = dev_read_u32_default(parent, name, -1); |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 404 | if (find_phandle <= 0) |
| 405 | return -ENOENT; |
| 406 | ret = uclass_get(id, &uc); |
| 407 | if (ret) |
| 408 | return ret; |
| 409 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 410 | uclass_foreach_dev(dev, uc) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 411 | uint phandle; |
| 412 | |
Simon Glass | bfb6e3d | 2017-05-18 20:09:12 -0600 | [diff] [blame] | 413 | phandle = dev_read_phandle(dev); |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 414 | |
| 415 | if (phandle == find_phandle) { |
| 416 | *devp = dev; |
| 417 | return 0; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return -ENODEV; |
| 422 | } |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 423 | #endif |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 424 | |
Simon Glass | 32d8ab6 | 2016-07-17 15:23:15 -0600 | [diff] [blame] | 425 | int uclass_get_device_by_driver(enum uclass_id id, |
| 426 | const struct driver *find_drv, |
| 427 | struct udevice **devp) |
| 428 | { |
| 429 | struct udevice *dev; |
| 430 | struct uclass *uc; |
| 431 | int ret; |
| 432 | |
| 433 | ret = uclass_get(id, &uc); |
| 434 | if (ret) |
| 435 | return ret; |
| 436 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 437 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 32d8ab6 | 2016-07-17 15:23:15 -0600 | [diff] [blame] | 438 | if (dev->driver == find_drv) |
| 439 | return uclass_get_device_tail(dev, 0, devp); |
| 440 | } |
| 441 | |
| 442 | return -ENODEV; |
| 443 | } |
| 444 | |
Simon Glass | 98c9fb0 | 2017-04-23 20:10:43 -0600 | [diff] [blame] | 445 | 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] | 446 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 447 | if (ret) |
| 448 | return ret; |
| 449 | |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 450 | assert(dev); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 451 | ret = device_probe(dev); |
| 452 | if (ret) |
| 453 | return ret; |
| 454 | |
| 455 | *devp = dev; |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Simon Glass | 4b67cef | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 460 | int uclass_get_device(enum uclass_id id, int index, struct udevice **devp) |
| 461 | { |
| 462 | struct udevice *dev; |
| 463 | int ret; |
| 464 | |
| 465 | *devp = NULL; |
| 466 | ret = uclass_find_device(id, index, &dev); |
| 467 | return uclass_get_device_tail(dev, ret, devp); |
| 468 | } |
| 469 | |
Przemyslaw Marczak | a12a1f5 | 2015-04-15 13:07:23 +0200 | [diff] [blame] | 470 | int uclass_get_device_by_name(enum uclass_id id, const char *name, |
| 471 | struct udevice **devp) |
| 472 | { |
| 473 | struct udevice *dev; |
| 474 | int ret; |
| 475 | |
| 476 | *devp = NULL; |
| 477 | ret = uclass_find_device_by_name(id, name, &dev); |
| 478 | return uclass_get_device_tail(dev, ret, devp); |
| 479 | } |
| 480 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 481 | int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
| 482 | { |
| 483 | struct udevice *dev; |
| 484 | int ret; |
| 485 | |
| 486 | *devp = NULL; |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 487 | ret = uclass_find_device_by_seq(id, seq, &dev); |
| 488 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 489 | return uclass_get_device_tail(dev, ret, devp); |
| 490 | } |
| 491 | |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 492 | int uclass_get_device_by_of_offset(enum uclass_id id, int node, |
| 493 | struct udevice **devp) |
| 494 | { |
| 495 | struct udevice *dev; |
| 496 | int ret; |
| 497 | |
| 498 | *devp = NULL; |
| 499 | ret = uclass_find_device_by_of_offset(id, node, &dev); |
| 500 | return uclass_get_device_tail(dev, ret, devp); |
| 501 | } |
| 502 | |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 503 | int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node, |
| 504 | struct udevice **devp) |
| 505 | { |
| 506 | struct udevice *dev; |
| 507 | int ret; |
| 508 | |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 509 | 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] | 510 | *devp = NULL; |
| 511 | ret = uclass_find_device_by_ofnode(id, node, &dev); |
Simon Glass | 5ab533c | 2018-06-11 13:07:19 -0600 | [diff] [blame] | 512 | log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", |
| 513 | ofnode_get_name(node), dev ? dev->name : "(none)", ret); |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 514 | |
| 515 | return uclass_get_device_tail(dev, ret, devp); |
| 516 | } |
| 517 | |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 518 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Kever Yang | 66a0b5a | 2018-02-09 10:56:23 +0800 | [diff] [blame] | 519 | int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id, |
| 520 | struct udevice **devp) |
| 521 | { |
| 522 | struct udevice *dev; |
| 523 | struct uclass *uc; |
| 524 | int ret; |
| 525 | |
| 526 | *devp = NULL; |
| 527 | ret = uclass_get(id, &uc); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
Liviu Dudau | 6f38622 | 2018-09-28 14:12:55 +0100 | [diff] [blame] | 531 | uclass_foreach_dev(dev, uc) { |
Kever Yang | 66a0b5a | 2018-02-09 10:56:23 +0800 | [diff] [blame] | 532 | uint phandle; |
| 533 | |
| 534 | phandle = dev_read_phandle(dev); |
| 535 | |
| 536 | if (phandle == phandle_id) { |
| 537 | *devp = dev; |
| 538 | return uclass_get_device_tail(dev, ret, devp); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | return -ENODEV; |
| 543 | } |
| 544 | |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 545 | int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent, |
| 546 | const char *name, struct udevice **devp) |
| 547 | { |
| 548 | struct udevice *dev; |
| 549 | int ret; |
| 550 | |
| 551 | *devp = NULL; |
| 552 | ret = uclass_find_device_by_phandle(id, parent, name, &dev); |
| 553 | return uclass_get_device_tail(dev, ret, devp); |
| 554 | } |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 555 | #endif |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 556 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 557 | int uclass_first_device(enum uclass_id id, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 558 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 559 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 560 | int ret; |
| 561 | |
| 562 | *devp = NULL; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 563 | ret = uclass_find_first_device(id, &dev); |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 564 | if (!dev) |
| 565 | return 0; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 566 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Simon Glass | 832c3f0 | 2016-02-11 13:23:25 -0700 | [diff] [blame] | 569 | int uclass_first_device_err(enum uclass_id id, struct udevice **devp) |
| 570 | { |
| 571 | int ret; |
| 572 | |
| 573 | ret = uclass_first_device(id, devp); |
| 574 | if (ret) |
| 575 | return ret; |
| 576 | else if (!*devp) |
| 577 | return -ENODEV; |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 582 | int uclass_next_device(struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 583 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 584 | struct udevice *dev = *devp; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 585 | int ret; |
| 586 | |
| 587 | *devp = NULL; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 588 | ret = uclass_find_next_device(&dev); |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 589 | if (!dev) |
| 590 | return 0; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 591 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Patrice Chotard | 4dff178 | 2018-10-24 14:10:15 +0200 | [diff] [blame] | 594 | int uclass_next_device_err(struct udevice **devp) |
| 595 | { |
| 596 | int ret; |
| 597 | |
| 598 | ret = uclass_next_device(devp); |
| 599 | if (ret) |
| 600 | return ret; |
| 601 | else if (!*devp) |
| 602 | return -ENODEV; |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
Simon Glass | 3ff83bc | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 607 | int uclass_first_device_check(enum uclass_id id, struct udevice **devp) |
| 608 | { |
| 609 | int ret; |
| 610 | |
| 611 | *devp = NULL; |
| 612 | ret = uclass_find_first_device(id, devp); |
| 613 | if (ret) |
| 614 | return ret; |
| 615 | if (!*devp) |
| 616 | return 0; |
| 617 | |
| 618 | return device_probe(*devp); |
| 619 | } |
| 620 | |
| 621 | int uclass_next_device_check(struct udevice **devp) |
| 622 | { |
| 623 | int ret; |
| 624 | |
| 625 | ret = uclass_find_next_device(devp); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | if (!*devp) |
| 629 | return 0; |
| 630 | |
| 631 | return device_probe(*devp); |
| 632 | } |
| 633 | |
Simon Glass | 35cb2a4 | 2020-02-06 09:54:50 -0700 | [diff] [blame] | 634 | int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data, |
| 635 | struct udevice **devp) |
| 636 | { |
| 637 | struct udevice *dev; |
| 638 | struct uclass *uc; |
| 639 | |
| 640 | uclass_id_foreach_dev(id, dev, uc) { |
| 641 | if (dev_get_driver_data(dev) == driver_data) { |
| 642 | *devp = dev; |
| 643 | |
| 644 | return device_probe(dev); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return -ENODEV; |
| 649 | } |
| 650 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 651 | int uclass_bind_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 652 | { |
| 653 | struct uclass *uc; |
| 654 | int ret; |
| 655 | |
| 656 | uc = dev->uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 657 | list_add_tail(&dev->uclass_node, &uc->dev_head); |
| 658 | |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 659 | if (dev->parent) { |
| 660 | struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv; |
| 661 | |
| 662 | if (uc_drv->child_post_bind) { |
| 663 | ret = uc_drv->child_post_bind(dev); |
| 664 | if (ret) |
| 665 | goto err; |
| 666 | } |
| 667 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 668 | |
| 669 | return 0; |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 670 | err: |
| 671 | /* There is no need to undo the parent's post_bind call */ |
| 672 | list_del(&dev->uclass_node); |
| 673 | |
| 674 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 677 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 678 | int uclass_unbind_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 679 | { |
| 680 | struct uclass *uc; |
| 681 | int ret; |
| 682 | |
| 683 | uc = dev->uclass; |
| 684 | if (uc->uc_drv->pre_unbind) { |
| 685 | ret = uc->uc_drv->pre_unbind(dev); |
| 686 | if (ret) |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | list_del(&dev->uclass_node); |
| 691 | return 0; |
| 692 | } |
Simon Glass | 8914c8a | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 693 | #endif |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 694 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 695 | int uclass_pre_probe_device(struct udevice *dev) |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 696 | { |
| 697 | struct uclass_driver *uc_drv; |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 698 | int ret; |
| 699 | |
| 700 | uc_drv = dev->uclass->uc_drv; |
| 701 | if (uc_drv->pre_probe) { |
| 702 | ret = uc_drv->pre_probe(dev); |
| 703 | if (ret) |
| 704 | return ret; |
| 705 | } |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 706 | |
| 707 | if (!dev->parent) |
| 708 | return 0; |
| 709 | uc_drv = dev->parent->uclass->uc_drv; |
Simon Glass | 256d402 | 2019-09-25 08:55:47 -0600 | [diff] [blame] | 710 | if (uc_drv->child_pre_probe) { |
| 711 | ret = uc_drv->child_pre_probe(dev); |
| 712 | if (ret) |
| 713 | return ret; |
| 714 | } |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 719 | int uclass_post_probe_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 720 | { |
Bin Meng | 30569cf | 2018-10-15 02:20:57 -0700 | [diff] [blame] | 721 | struct uclass_driver *uc_drv; |
| 722 | int ret; |
| 723 | |
| 724 | if (dev->parent) { |
| 725 | uc_drv = dev->parent->uclass->uc_drv; |
| 726 | if (uc_drv->child_post_probe) { |
| 727 | ret = uc_drv->child_post_probe(dev); |
| 728 | if (ret) |
| 729 | return ret; |
| 730 | } |
| 731 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 732 | |
Bin Meng | 30569cf | 2018-10-15 02:20:57 -0700 | [diff] [blame] | 733 | uc_drv = dev->uclass->uc_drv; |
Simon Glass | 256d402 | 2019-09-25 08:55:47 -0600 | [diff] [blame] | 734 | if (uc_drv->post_probe) { |
| 735 | ret = uc_drv->post_probe(dev); |
| 736 | if (ret) |
| 737 | return ret; |
| 738 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 743 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 744 | int uclass_pre_remove_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 745 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 746 | struct uclass *uc; |
| 747 | int ret; |
| 748 | |
| 749 | uc = dev->uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 750 | if (uc->uc_drv->pre_remove) { |
| 751 | ret = uc->uc_drv->pre_remove(dev); |
| 752 | if (ret) |
| 753 | return ret; |
| 754 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 755 | |
| 756 | return 0; |
| 757 | } |
Simon Glass | 8914c8a | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 758 | #endif |
Jean-Jacques Hiblot | db97c7f | 2019-07-05 09:33:57 +0200 | [diff] [blame] | 759 | |
Vabhav Sharma | bcab47a | 2020-12-09 10:42:03 +0530 | [diff] [blame] | 760 | int uclass_probe_all(enum uclass_id id) |
| 761 | { |
| 762 | struct udevice *dev; |
| 763 | int ret; |
| 764 | |
| 765 | ret = uclass_first_device(id, &dev); |
| 766 | if (ret || !dev) |
| 767 | return ret; |
| 768 | |
| 769 | /* Scanning uclass to probe all devices */ |
| 770 | while (dev) { |
| 771 | ret = uclass_next_device(&dev); |
| 772 | if (ret) |
| 773 | return ret; |
| 774 | } |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
Jean-Jacques Hiblot | db97c7f | 2019-07-05 09:33:57 +0200 | [diff] [blame] | 779 | UCLASS_DRIVER(nop) = { |
| 780 | .id = UCLASS_NOP, |
| 781 | .name = "nop", |
| 782 | }; |