2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-02-26 22:59:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Google, Inc
|
|
|
|
*
|
|
|
|
* (C) Copyright 2012
|
|
|
|
* Pavel Herrmann <morpheus.ibis@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2019-10-22 07:39:47 +00:00
|
|
|
#define LOG_CATEGORY LOGC_DM
|
|
|
|
|
2014-02-26 22:59:18 +00:00
|
|
|
#include <common.h>
|
2017-05-19 02:09:09 +00:00
|
|
|
#include <dm.h>
|
2014-02-26 22:59:18 +00:00
|
|
|
#include <errno.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2014-02-26 22:59:18 +00:00
|
|
|
#include <malloc.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2014-02-26 22:59:18 +00:00
|
|
|
#include <dm/device.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/lists.h>
|
|
|
|
#include <dm/uclass.h>
|
|
|
|
#include <dm/uclass-internal.h>
|
|
|
|
#include <dm/util.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
struct uclass *uclass_find(enum uclass_id key)
|
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
|
2014-07-23 12:55:15 +00:00
|
|
|
if (!gd->dm_root)
|
|
|
|
return NULL;
|
2014-02-26 22:59:18 +00:00
|
|
|
/*
|
|
|
|
* TODO(sjg@chromium.org): Optimise this, perhaps moving the found
|
|
|
|
* node to the start of the list, or creating a linear array mapping
|
|
|
|
* id to node.
|
|
|
|
*/
|
2020-12-19 17:40:17 +00:00
|
|
|
list_for_each_entry(uc, gd->uclass_root, sibling_node) {
|
2014-02-26 22:59:18 +00:00
|
|
|
if (uc->uc_drv->id == key)
|
|
|
|
return uc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* uclass_add() - Create new uclass in list
|
|
|
|
* @id: Id number to create
|
|
|
|
* @ucp: Returns pointer to uclass, or NULL on error
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 on success, -ve on error
|
2014-02-26 22:59:18 +00:00
|
|
|
*
|
|
|
|
* The new uclass is added to the list. There must be only one uclass for
|
|
|
|
* each id.
|
|
|
|
*/
|
|
|
|
static int uclass_add(enum uclass_id id, struct uclass **ucp)
|
|
|
|
{
|
|
|
|
struct uclass_driver *uc_drv;
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*ucp = NULL;
|
|
|
|
uc_drv = lists_uclass_lookup(id);
|
|
|
|
if (!uc_drv) {
|
2015-07-07 09:51:32 +00:00
|
|
|
debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
|
|
|
|
id);
|
2015-08-30 22:55:16 +00:00
|
|
|
/*
|
|
|
|
* Use a strange error to make this case easier to find. When
|
|
|
|
* a uclass is not available it can prevent driver model from
|
|
|
|
* starting up and this failure is otherwise hard to debug.
|
|
|
|
*/
|
|
|
|
return -EPFNOSUPPORT;
|
2014-02-26 22:59:18 +00:00
|
|
|
}
|
|
|
|
uc = calloc(1, sizeof(*uc));
|
|
|
|
if (!uc)
|
|
|
|
return -ENOMEM;
|
2020-12-03 23:55:17 +00:00
|
|
|
if (uc_drv->priv_auto) {
|
2020-12-23 02:30:29 +00:00
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = calloc(1, uc_drv->priv_auto);
|
|
|
|
if (!ptr) {
|
2014-02-26 22:59:18 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_mem;
|
|
|
|
}
|
2020-12-23 02:30:29 +00:00
|
|
|
uclass_set_priv(uc, ptr);
|
2014-02-26 22:59:18 +00:00
|
|
|
}
|
|
|
|
uc->uc_drv = uc_drv;
|
|
|
|
INIT_LIST_HEAD(&uc->sibling_node);
|
|
|
|
INIT_LIST_HEAD(&uc->dev_head);
|
2020-12-19 17:40:17 +00:00
|
|
|
list_add(&uc->sibling_node, DM_UCLASS_ROOT_NON_CONST);
|
2014-02-26 22:59:18 +00:00
|
|
|
|
|
|
|
if (uc_drv->init) {
|
|
|
|
ret = uc_drv->init(uc);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucp = uc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
2020-12-03 23:55:17 +00:00
|
|
|
if (uc_drv->priv_auto) {
|
2020-12-23 02:30:29 +00:00
|
|
|
free(uclass_get_priv(uc));
|
|
|
|
uclass_set_priv(uc, NULL);
|
2014-02-26 22:59:18 +00:00
|
|
|
}
|
|
|
|
list_del(&uc->sibling_node);
|
|
|
|
fail_mem:
|
|
|
|
free(uc);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uclass_destroy(struct uclass *uc)
|
|
|
|
{
|
|
|
|
struct uclass_driver *uc_drv;
|
2015-04-19 13:20:58 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:18 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-04-19 13:20:58 +00:00
|
|
|
/*
|
|
|
|
* We cannot use list_for_each_entry_safe() here. If a device in this
|
|
|
|
* uclass has a child device also in this uclass, it will be also be
|
|
|
|
* unbound (by the recursion in the call to device_unbind() below).
|
|
|
|
* We can loop until the list is empty.
|
|
|
|
*/
|
|
|
|
while (!list_empty(&uc->dev_head)) {
|
|
|
|
dev = list_first_entry(&uc->dev_head, struct udevice,
|
|
|
|
uclass_node);
|
2020-03-28 20:03:48 +00:00
|
|
|
ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD);
|
2014-02-26 22:59:18 +00:00
|
|
|
if (ret)
|
2020-03-28 20:03:47 +00:00
|
|
|
return log_msg_ret("remove", ret);
|
2014-02-26 22:59:18 +00:00
|
|
|
ret = device_unbind(dev);
|
|
|
|
if (ret)
|
2020-03-28 20:03:47 +00:00
|
|
|
return log_msg_ret("unbind", ret);
|
2014-02-26 22:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uc_drv = uc->uc_drv;
|
|
|
|
if (uc_drv->destroy)
|
|
|
|
uc_drv->destroy(uc);
|
|
|
|
list_del(&uc->sibling_node);
|
2020-12-03 23:55:17 +00:00
|
|
|
if (uc_drv->priv_auto)
|
2020-12-23 02:30:29 +00:00
|
|
|
free(uclass_get_priv(uc));
|
2014-02-26 22:59:18 +00:00
|
|
|
free(uc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uclass_get(enum uclass_id id, struct uclass **ucp)
|
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
|
2021-08-01 18:05:23 +00:00
|
|
|
/* Immediately fail if driver model is not set up */
|
|
|
|
if (!gd->uclass_root)
|
|
|
|
return -EDEADLK;
|
2014-02-26 22:59:18 +00:00
|
|
|
*ucp = NULL;
|
|
|
|
uc = uclass_find(id);
|
2021-03-15 04:25:18 +00:00
|
|
|
if (!uc) {
|
|
|
|
if (CONFIG_IS_ENABLED(OF_PLATDATA_INST))
|
|
|
|
return -ENOENT;
|
2014-02-26 22:59:18 +00:00
|
|
|
return uclass_add(id, ucp);
|
2021-03-15 04:25:18 +00:00
|
|
|
}
|
2014-02-26 22:59:18 +00:00
|
|
|
*ucp = uc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 02:42:13 +00:00
|
|
|
const char *uclass_get_name(enum uclass_id id)
|
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
|
|
|
|
if (uclass_get(id, &uc))
|
|
|
|
return NULL;
|
|
|
|
return uc->uc_drv->name;
|
|
|
|
}
|
|
|
|
|
2020-12-23 02:30:26 +00:00
|
|
|
void *uclass_get_priv(const struct uclass *uc)
|
|
|
|
{
|
2020-12-23 02:30:30 +00:00
|
|
|
return uc->priv_;
|
2020-12-23 02:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void uclass_set_priv(struct uclass *uc, void *priv)
|
|
|
|
{
|
2020-12-23 02:30:30 +00:00
|
|
|
uc->priv_ = priv;
|
2020-12-23 02:30:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 05:30:59 +00:00
|
|
|
enum uclass_id uclass_get_by_namelen(const char *name, int len)
|
2017-12-28 20:14:15 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < UCLASS_COUNT; i++) {
|
|
|
|
struct uclass_driver *uc_drv = lists_uclass_lookup(i);
|
|
|
|
|
2022-04-25 05:30:59 +00:00
|
|
|
if (uc_drv && !strncmp(uc_drv->name, name, len) &&
|
|
|
|
strlen(uc_drv->name) == len)
|
2017-12-28 20:14:15 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UCLASS_INVALID;
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:26:08 +00:00
|
|
|
enum uclass_id uclass_get_by_name(const char *name)
|
|
|
|
{
|
2022-04-25 05:30:59 +00:00
|
|
|
return uclass_get_by_namelen(name, strlen(name));
|
2021-10-23 23:26:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 14:17:42 +00:00
|
|
|
int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
|
|
|
|
{
|
|
|
|
struct udevice *iter;
|
|
|
|
struct uclass *uc = dev->uclass;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (list_empty(&uc->dev_head))
|
|
|
|
return -ENODEV;
|
|
|
|
|
2018-09-28 13:12:55 +00:00
|
|
|
uclass_foreach_dev(iter, uc) {
|
2018-08-09 14:17:42 +00:00
|
|
|
if (iter == dev) {
|
|
|
|
if (ucp)
|
|
|
|
*ucp = uc;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
|
|
|
struct uclass *uc;
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:18 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
ret = uclass_get(id, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-07-31 15:31:19 +00:00
|
|
|
if (list_empty(&uc->dev_head))
|
|
|
|
return -ENODEV;
|
2014-02-26 22:59:18 +00:00
|
|
|
|
2018-09-28 13:12:55 +00:00
|
|
|
uclass_foreach_dev(dev, uc) {
|
2014-02-26 22:59:18 +00:00
|
|
|
if (!index--) {
|
|
|
|
*devp = dev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2015-04-15 11:07:17 +00:00
|
|
|
int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
ret = uclass_get(id, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (list_empty(&uc->dev_head))
|
2019-09-25 14:55:55 +00:00
|
|
|
return 0;
|
2015-04-15 11:07:17 +00:00
|
|
|
|
|
|
|
*devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uclass_find_next_device(struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev = *devp;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-25 05:31:00 +00:00
|
|
|
int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len,
|
|
|
|
struct udevice **devp)
|
2015-04-15 11:07:22 +00:00
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
if (!name)
|
|
|
|
return -EINVAL;
|
|
|
|
ret = uclass_get(id, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-09-28 13:12:55 +00:00
|
|
|
uclass_foreach_dev(dev, uc) {
|
2022-04-25 05:31:00 +00:00
|
|
|
if (!strncmp(dev->name, name, len) &&
|
|
|
|
strlen(dev->name) == len) {
|
2015-04-15 11:07:22 +00:00
|
|
|
*devp = dev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2022-04-25 05:31:00 +00:00
|
|
|
int uclass_find_device_by_name(enum uclass_id id, const char *name,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
return uclass_find_device_by_namelen(id, name, strlen(name), devp);
|
|
|
|
}
|
|
|
|
|
2020-12-17 04:20:30 +00:00
|
|
|
int uclass_find_next_free_seq(struct uclass *uc)
|
2018-12-07 13:50:39 +00:00
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int max = -1;
|
|
|
|
|
2020-12-17 04:20:30 +00:00
|
|
|
/* If using aliases, start with the highest alias value */
|
|
|
|
if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
|
|
|
|
(uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
|
|
|
|
max = dev_read_alias_highest_id(uc->uc_drv->name);
|
|
|
|
|
|
|
|
/* Avoid conflict with existing devices */
|
2018-12-07 13:50:39 +00:00
|
|
|
list_for_each_entry(dev, &uc->dev_head, uclass_node) {
|
2020-12-19 17:40:09 +00:00
|
|
|
if (dev->seq_ > max)
|
|
|
|
max = dev->seq_;
|
2018-12-07 13:50:39 +00:00
|
|
|
}
|
2020-12-17 04:20:30 +00:00
|
|
|
/*
|
|
|
|
* At this point, max will be -1 if there are no existing aliases or
|
|
|
|
* devices
|
|
|
|
*/
|
2018-12-07 13:50:39 +00:00
|
|
|
|
|
|
|
return max + 1;
|
|
|
|
}
|
|
|
|
|
2020-12-17 04:20:29 +00:00
|
|
|
int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
|
2014-07-23 12:55:12 +00:00
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
2020-12-17 04:20:29 +00:00
|
|
|
log_debug("%d\n", seq);
|
|
|
|
if (seq == -1)
|
2014-07-23 12:55:12 +00:00
|
|
|
return -ENODEV;
|
|
|
|
ret = uclass_get(id, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-09-28 13:12:55 +00:00
|
|
|
uclass_foreach_dev(dev, uc) {
|
2020-12-19 17:40:09 +00:00
|
|
|
log_debug(" - %d '%s'\n", dev->seq_, dev->name);
|
|
|
|
if (dev->seq_ == seq) {
|
2014-07-23 12:55:12 +00:00
|
|
|
*devp = dev;
|
2019-10-22 07:39:47 +00:00
|
|
|
log_debug(" - found\n");
|
2014-07-23 12:55:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2019-10-22 07:39:47 +00:00
|
|
|
log_debug(" - not found\n");
|
2014-07-23 12:55:12 +00:00
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2016-01-22 02:43:57 +00:00
|
|
|
int uclass_find_device_by_of_offset(enum uclass_id id, int node,
|
|
|
|
struct udevice **devp)
|
2014-07-23 12:55:14 +00:00
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
if (node < 0)
|
|
|
|
return -ENODEV;
|
|
|
|
ret = uclass_get(id, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-09-28 13:12:55 +00:00
|
|
|
uclass_foreach_dev(dev, uc) {
|
2017-01-17 23:52:55 +00:00
|
|
|
if (dev_of_offset(dev) == node) {
|
2014-07-23 12:55:14 +00:00
|
|
|
*devp = dev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:09:09 +00:00
|
|
|
int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
2018-06-11 19:07:19 +00:00
|
|
|
log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
|
2017-05-19 02:09:09 +00:00
|
|
|
*devp = NULL;
|
|
|
|
if (!ofnode_valid(node))
|
|
|
|
return -ENODEV;
|
|
|
|
ret = uclass_get(id, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-09-28 13:12:55 +00:00
|
|
|
uclass_foreach_dev(dev, uc) {
|
2018-06-11 19:07:19 +00:00
|
|
|
log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
|
|
|
|
dev->name);
|
2017-05-19 02:09:09 +00:00
|
|
|
if (ofnode_equal(dev_ofnode(dev), node)) {
|
|
|
|
*devp = dev;
|
2018-06-11 19:07:19 +00:00
|
|
|
goto done;
|
2017-05-19 02:09:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 19:07:19 +00:00
|
|
|
ret = -ENODEV;
|
2017-05-19 02:09:09 +00:00
|
|
|
|
2018-06-11 19:07:19 +00:00
|
|
|
done:
|
|
|
|
log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
|
|
|
|
ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
|
|
|
|
return ret;
|
2017-05-19 02:09:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 13:24:03 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_REAL)
|
2023-04-13 15:16:11 +00:00
|
|
|
static int uclass_find_device_by_phandle_id(enum uclass_id id,
|
|
|
|
uint find_phandle,
|
|
|
|
struct udevice **devp)
|
2015-07-03 00:15:38 +00:00
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get(id, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-09-28 13:12:55 +00:00
|
|
|
uclass_foreach_dev(dev, uc) {
|
2017-01-17 23:52:55 +00:00
|
|
|
uint phandle;
|
|
|
|
|
2017-05-19 02:09:12 +00:00
|
|
|
phandle = dev_read_phandle(dev);
|
2015-07-03 00:15:38 +00:00
|
|
|
|
|
|
|
if (phandle == find_phandle) {
|
|
|
|
*devp = dev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2023-04-13 15:16:11 +00:00
|
|
|
|
|
|
|
int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
|
|
|
|
const char *name, struct udevice **devp)
|
|
|
|
{
|
|
|
|
int find_phandle;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
find_phandle = dev_read_u32_default(parent, name, -1);
|
|
|
|
if (find_phandle <= 0)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
return uclass_find_device_by_phandle_id(id, find_phandle, devp);
|
|
|
|
}
|
2015-12-20 02:38:55 +00:00
|
|
|
#endif
|
2015-07-03 00:15:38 +00:00
|
|
|
|
2016-07-17 21:23:15 +00:00
|
|
|
int uclass_get_device_by_driver(enum uclass_id id,
|
|
|
|
const struct driver *find_drv,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get(id, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-09-28 13:12:55 +00:00
|
|
|
uclass_foreach_dev(dev, uc) {
|
2016-07-17 21:23:15 +00:00
|
|
|
if (dev->driver == find_drv)
|
|
|
|
return uclass_get_device_tail(dev, 0, devp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2017-04-24 02:10:43 +00:00
|
|
|
int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-04-25 04:33:07 +00:00
|
|
|
assert(dev);
|
2014-02-26 22:59:18 +00:00
|
|
|
ret = device_probe(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*devp = dev;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-23 12:55:08 +00:00
|
|
|
int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
ret = uclass_find_device(id, index, &dev);
|
|
|
|
return uclass_get_device_tail(dev, ret, devp);
|
|
|
|
}
|
|
|
|
|
2015-04-15 11:07:23 +00:00
|
|
|
int uclass_get_device_by_name(enum uclass_id id, const char *name,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
ret = uclass_find_device_by_name(id, name, &dev);
|
|
|
|
return uclass_get_device_tail(dev, ret, devp);
|
|
|
|
}
|
|
|
|
|
2014-07-23 12:55:12 +00:00
|
|
|
int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
2020-12-17 04:20:29 +00:00
|
|
|
ret = uclass_find_device_by_seq(id, seq, &dev);
|
|
|
|
|
2014-07-23 12:55:12 +00:00
|
|
|
return uclass_get_device_tail(dev, ret, devp);
|
|
|
|
}
|
|
|
|
|
2014-07-23 12:55:14 +00:00
|
|
|
int uclass_get_device_by_of_offset(enum uclass_id id, int node,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
ret = uclass_find_device_by_of_offset(id, node, &dev);
|
|
|
|
return uclass_get_device_tail(dev, ret, devp);
|
|
|
|
}
|
|
|
|
|
2017-05-19 02:09:09 +00:00
|
|
|
int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
2018-06-11 19:07:19 +00:00
|
|
|
log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
|
2017-05-19 02:09:09 +00:00
|
|
|
*devp = NULL;
|
|
|
|
ret = uclass_find_device_by_ofnode(id, node, &dev);
|
2018-06-11 19:07:19 +00:00
|
|
|
log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
|
|
|
|
ofnode_get_name(node), dev ? dev->name : "(none)", ret);
|
2017-05-19 02:09:09 +00:00
|
|
|
|
|
|
|
return uclass_get_device_tail(dev, ret, devp);
|
|
|
|
}
|
|
|
|
|
2023-04-13 15:16:11 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_REAL)
|
2018-02-09 02:56:23 +00:00
|
|
|
int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
2023-04-13 15:16:11 +00:00
|
|
|
ret = uclass_find_device_by_phandle_id(id, phandle_id, &dev);
|
|
|
|
return uclass_get_device_tail(dev, ret, devp);
|
2018-02-09 02:56:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:15:38 +00:00
|
|
|
int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
|
|
|
|
const char *name, struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
ret = uclass_find_device_by_phandle(id, parent, name, &dev);
|
|
|
|
return uclass_get_device_tail(dev, ret, devp);
|
|
|
|
}
|
2015-12-20 02:38:55 +00:00
|
|
|
#endif
|
2015-07-03 00:15:38 +00:00
|
|
|
|
2022-10-12 19:58:09 +00:00
|
|
|
/*
|
|
|
|
* Starting from the given device @dev, return pointer to the first device in
|
|
|
|
* the uclass that probes successfully in @devp.
|
|
|
|
*/
|
|
|
|
static void _uclass_next_device(struct udevice *dev, struct udevice **devp)
|
|
|
|
{
|
|
|
|
for (; dev; uclass_find_next_device(&dev)) {
|
|
|
|
if (!device_probe(dev))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*devp = dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uclass_first_device(enum uclass_id id, struct udevice **devp)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev;
|
2014-02-26 22:59:18 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-04-15 11:07:17 +00:00
|
|
|
ret = uclass_find_first_device(id, &dev);
|
2022-10-12 19:58:09 +00:00
|
|
|
_uclass_next_device(dev, devp);
|
2014-02-26 22:59:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 19:58:09 +00:00
|
|
|
void uclass_next_device(struct udevice **devp)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
2014-05-22 10:43:05 +00:00
|
|
|
struct udevice *dev = *devp;
|
2014-02-26 22:59:18 +00:00
|
|
|
|
2022-10-12 19:58:09 +00:00
|
|
|
uclass_find_next_device(&dev);
|
|
|
|
_uclass_next_device(dev, devp);
|
2014-02-26 22:59:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 19:58:06 +00:00
|
|
|
int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_first_device_check(id, devp);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else if (!*devp)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:10:15 +00:00
|
|
|
int uclass_next_device_err(struct udevice **devp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-10-12 19:58:06 +00:00
|
|
|
ret = uclass_next_device_check(devp);
|
2018-10-24 12:10:15 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else if (!*devp)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-24 02:10:45 +00:00
|
|
|
int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
ret = uclass_find_first_device(id, devp);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (!*devp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return device_probe(*devp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int uclass_next_device_check(struct udevice **devp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_find_next_device(devp);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (!*devp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return device_probe(*devp);
|
|
|
|
}
|
|
|
|
|
2021-12-17 03:59:32 +00:00
|
|
|
int uclass_get_count(void)
|
|
|
|
{
|
|
|
|
const struct uclass *uc;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (gd->dm_root) {
|
|
|
|
list_for_each_entry(uc, gd->uclass_root, sibling_node)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:54:50 +00:00
|
|
|
int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
struct uclass *uc;
|
|
|
|
|
|
|
|
uclass_id_foreach_dev(id, dev, uc) {
|
|
|
|
if (dev_get_driver_data(dev) == driver_data) {
|
|
|
|
*devp = dev;
|
|
|
|
|
|
|
|
return device_probe(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
int uclass_bind_device(struct udevice *dev)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
uc = dev->uclass;
|
|
|
|
list_add_tail(&dev->uclass_node, &uc->dev_head);
|
|
|
|
|
2015-01-25 15:27:08 +00:00
|
|
|
if (dev->parent) {
|
|
|
|
struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
|
|
|
|
|
|
|
|
if (uc_drv->child_post_bind) {
|
|
|
|
ret = uc_drv->child_post_bind(dev);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 22:59:18 +00:00
|
|
|
|
|
|
|
return 0;
|
2015-01-25 15:27:08 +00:00
|
|
|
err:
|
|
|
|
/* There is no need to undo the parent's post_bind call */
|
|
|
|
list_del(&dev->uclass_node);
|
|
|
|
|
|
|
|
return ret;
|
2014-02-26 22:59:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 22:31:52 +00:00
|
|
|
#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
|
2021-10-23 23:26:05 +00:00
|
|
|
int uclass_pre_unbind_device(struct udevice *dev)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
uc = dev->uclass;
|
|
|
|
if (uc->uc_drv->pre_unbind) {
|
|
|
|
ret = uc->uc_drv->pre_unbind(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:26:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uclass_unbind_device(struct udevice *dev)
|
|
|
|
{
|
2014-02-26 22:59:18 +00:00
|
|
|
list_del(&dev->uclass_node);
|
2021-10-23 23:26:05 +00:00
|
|
|
|
2014-02-26 22:59:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-02-28 05:06:31 +00:00
|
|
|
#endif
|
2014-02-26 22:59:18 +00:00
|
|
|
|
2015-03-05 19:25:22 +00:00
|
|
|
int uclass_pre_probe_device(struct udevice *dev)
|
2015-01-25 15:27:10 +00:00
|
|
|
{
|
|
|
|
struct uclass_driver *uc_drv;
|
2015-03-05 19:25:22 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
uc_drv = dev->uclass->uc_drv;
|
|
|
|
if (uc_drv->pre_probe) {
|
|
|
|
ret = uc_drv->pre_probe(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-25 15:27:10 +00:00
|
|
|
|
|
|
|
if (!dev->parent)
|
|
|
|
return 0;
|
|
|
|
uc_drv = dev->parent->uclass->uc_drv;
|
2019-09-25 14:55:47 +00:00
|
|
|
if (uc_drv->child_pre_probe) {
|
|
|
|
ret = uc_drv->child_pre_probe(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-25 15:27:10 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-22 10:43:05 +00:00
|
|
|
int uclass_post_probe_device(struct udevice *dev)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
2018-10-15 09:20:57 +00:00
|
|
|
struct uclass_driver *uc_drv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (dev->parent) {
|
|
|
|
uc_drv = dev->parent->uclass->uc_drv;
|
|
|
|
if (uc_drv->child_post_probe) {
|
|
|
|
ret = uc_drv->child_post_probe(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 22:59:18 +00:00
|
|
|
|
2018-10-15 09:20:57 +00:00
|
|
|
uc_drv = dev->uclass->uc_drv;
|
2019-09-25 14:55:47 +00:00
|
|
|
if (uc_drv->post_probe) {
|
|
|
|
ret = uc_drv->post_probe(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2014-02-26 22:59:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-11 22:31:52 +00:00
|
|
|
#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
|
2014-05-22 10:43:05 +00:00
|
|
|
int uclass_pre_remove_device(struct udevice *dev)
|
2014-02-26 22:59:18 +00:00
|
|
|
{
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
uc = dev->uclass;
|
|
|
|
if (uc->uc_drv->pre_remove) {
|
|
|
|
ret = uc->uc_drv->pre_remove(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-28 05:06:31 +00:00
|
|
|
#endif
|
2019-07-05 07:33:57 +00:00
|
|
|
|
2020-12-09 05:12:03 +00:00
|
|
|
int uclass_probe_all(enum uclass_id id)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
2022-10-12 19:57:50 +00:00
|
|
|
int ret, err;
|
2020-12-09 05:12:03 +00:00
|
|
|
|
2022-10-12 19:57:50 +00:00
|
|
|
err = uclass_first_device_check(id, &dev);
|
2020-12-09 05:12:03 +00:00
|
|
|
|
|
|
|
/* Scanning uclass to probe all devices */
|
|
|
|
while (dev) {
|
2022-10-12 19:57:50 +00:00
|
|
|
ret = uclass_next_device_check(&dev);
|
2020-12-09 05:12:03 +00:00
|
|
|
if (ret)
|
2022-10-12 19:57:50 +00:00
|
|
|
err = ret;
|
2020-12-09 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 19:57:50 +00:00
|
|
|
return err;
|
2020-12-09 05:12:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 23:26:09 +00:00
|
|
|
int uclass_id_count(enum uclass_id id)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
struct uclass *uc;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
uclass_id_foreach_dev(id, dev, uc)
|
|
|
|
count++;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-07-05 07:33:57 +00:00
|
|
|
UCLASS_DRIVER(nop) = {
|
|
|
|
.id = UCLASS_NOP,
|
|
|
|
.name = "nop",
|
|
|
|
};
|