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