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