blob: 4a660dfd1575046071cc48b1012caddd7e3a4aca [file] [log] [blame]
Simon Glass458b66a2020-11-05 06:32:05 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2017
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <sysinfo.h>
10
Sean Anderson6ccf10a2021-04-20 10:50:56 -040011struct sysinfo_priv {
12 bool detected;
13};
14
Simon Glass458b66a2020-11-05 06:32:05 -070015int sysinfo_get(struct udevice **devp)
16{
17 return uclass_first_device_err(UCLASS_SYSINFO, devp);
18}
19
20int sysinfo_detect(struct udevice *dev)
21{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040022 int ret;
23 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070024 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
25
26 if (!ops->detect)
27 return -ENOSYS;
28
Sean Anderson6ccf10a2021-04-20 10:50:56 -040029 ret = ops->detect(dev);
30 if (!ret)
31 priv->detected = true;
32
33 return ret;
Simon Glass458b66a2020-11-05 06:32:05 -070034}
35
36int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type,
37 const char **strp)
38{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040039 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070040 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
41
Sean Anderson6ccf10a2021-04-20 10:50:56 -040042 if (!priv->detected)
43 return -EPERM;
44
Simon Glass458b66a2020-11-05 06:32:05 -070045 if (!ops->get_fit_loadable)
46 return -ENOSYS;
47
48 return ops->get_fit_loadable(dev, index, type, strp);
49}
50
51int sysinfo_get_bool(struct udevice *dev, int id, bool *val)
52{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040053 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070054 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
55
Sean Anderson6ccf10a2021-04-20 10:50:56 -040056 if (!priv->detected)
57 return -EPERM;
58
Simon Glass458b66a2020-11-05 06:32:05 -070059 if (!ops->get_bool)
60 return -ENOSYS;
61
62 return ops->get_bool(dev, id, val);
63}
64
65int sysinfo_get_int(struct udevice *dev, int id, int *val)
66{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040067 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070068 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
69
Sean Anderson6ccf10a2021-04-20 10:50:56 -040070 if (!priv->detected)
71 return -EPERM;
72
Simon Glass458b66a2020-11-05 06:32:05 -070073 if (!ops->get_int)
74 return -ENOSYS;
75
76 return ops->get_int(dev, id, val);
77}
78
79int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val)
80{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040081 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070082 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
83
Sean Anderson6ccf10a2021-04-20 10:50:56 -040084 if (!priv->detected)
85 return -EPERM;
86
Simon Glass458b66a2020-11-05 06:32:05 -070087 if (!ops->get_str)
88 return -ENOSYS;
89
90 return ops->get_str(dev, id, size, val);
91}
92
93UCLASS_DRIVER(sysinfo) = {
94 .id = UCLASS_SYSINFO,
95 .name = "sysinfo",
96 .post_bind = dm_scan_fdt_dev,
Sean Anderson6ccf10a2021-04-20 10:50:56 -040097 .per_device_auto = sizeof(bool),
Simon Glass458b66a2020-11-05 06:32:05 -070098};