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