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