blob: d77d1e3ee44251677be768697a66c7b6b988052c [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
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_SYSINFO
8
Simon Glass458b66a2020-11-05 06:32:05 -07009#include <dm.h>
10#include <sysinfo.h>
11
Sean Anderson6ccf10a2021-04-20 10:50:56 -040012struct sysinfo_priv {
13 bool detected;
14};
15
Simon Glass458b66a2020-11-05 06:32:05 -070016int sysinfo_get(struct udevice **devp)
17{
Michal Suchanek4cd455e2022-09-25 13:08:16 +020018 int ret = uclass_first_device_err(UCLASS_SYSINFO, devp);
19
20 /*
21 * There is some very dodgy error handling in gazerbeam,
22 * do not return a device on error.
23 */
24 if (ret)
25 *devp = NULL;
26 return ret;
Simon Glass458b66a2020-11-05 06:32:05 -070027}
28
29int sysinfo_detect(struct udevice *dev)
30{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040031 int ret;
32 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070033 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
34
35 if (!ops->detect)
36 return -ENOSYS;
37
Sean Anderson6ccf10a2021-04-20 10:50:56 -040038 ret = ops->detect(dev);
39 if (!ret)
40 priv->detected = true;
41
42 return ret;
Simon Glass458b66a2020-11-05 06:32:05 -070043}
44
45int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type,
46 const char **strp)
47{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040048 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070049 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
50
Sean Anderson6ccf10a2021-04-20 10:50:56 -040051 if (!priv->detected)
52 return -EPERM;
53
Simon Glass458b66a2020-11-05 06:32:05 -070054 if (!ops->get_fit_loadable)
55 return -ENOSYS;
56
57 return ops->get_fit_loadable(dev, index, type, strp);
58}
59
60int sysinfo_get_bool(struct udevice *dev, int id, bool *val)
61{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040062 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070063 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
64
Sean Anderson6ccf10a2021-04-20 10:50:56 -040065 if (!priv->detected)
66 return -EPERM;
67
Simon Glass458b66a2020-11-05 06:32:05 -070068 if (!ops->get_bool)
69 return -ENOSYS;
70
71 return ops->get_bool(dev, id, val);
72}
73
74int sysinfo_get_int(struct udevice *dev, int id, int *val)
75{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040076 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070077 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
78
Sean Anderson6ccf10a2021-04-20 10:50:56 -040079 if (!priv->detected)
80 return -EPERM;
81
Simon Glass458b66a2020-11-05 06:32:05 -070082 if (!ops->get_int)
83 return -ENOSYS;
84
85 return ops->get_int(dev, id, val);
86}
87
88int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val)
89{
Sean Anderson6ccf10a2021-04-20 10:50:56 -040090 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
Simon Glass458b66a2020-11-05 06:32:05 -070091 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
92
Sean Anderson6ccf10a2021-04-20 10:50:56 -040093 if (!priv->detected)
94 return -EPERM;
95
Simon Glass458b66a2020-11-05 06:32:05 -070096 if (!ops->get_str)
97 return -ENOSYS;
98
99 return ops->get_str(dev, id, size, val);
100}
101
102UCLASS_DRIVER(sysinfo) = {
103 .id = UCLASS_SYSINFO,
104 .name = "sysinfo",
105 .post_bind = dm_scan_fdt_dev,
Sean Anderson6ccf10a2021-04-20 10:50:56 -0400106 .per_device_auto = sizeof(bool),
Simon Glass458b66a2020-11-05 06:32:05 -0700107};