1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2017
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
7 #define LOG_CATEGORY UCLASS_SYSINFO
8
9 #include <common.h>
10 #include <dm.h>
11 #include <sysinfo.h>
12
13 struct sysinfo_priv {
14 bool detected;
15 };
16
sysinfo_get(struct udevice ** devp)17 int sysinfo_get(struct udevice **devp)
18 {
19 return uclass_first_device_err(UCLASS_SYSINFO, devp);
20 }
21
sysinfo_detect(struct udevice * dev)22 int sysinfo_detect(struct udevice *dev)
23 {
24 int ret;
25 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
26 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
27
28 if (!ops->detect)
29 return -ENOSYS;
30
31 ret = ops->detect(dev);
32 if (!ret)
33 priv->detected = true;
34
35 return ret;
36 }
37
sysinfo_get_fit_loadable(struct udevice * dev,int index,const char * type,const char ** strp)38 int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type,
39 const char **strp)
40 {
41 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
42 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
43
44 if (!priv->detected)
45 return -EPERM;
46
47 if (!ops->get_fit_loadable)
48 return -ENOSYS;
49
50 return ops->get_fit_loadable(dev, index, type, strp);
51 }
52
sysinfo_get_bool(struct udevice * dev,int id,bool * val)53 int sysinfo_get_bool(struct udevice *dev, int id, bool *val)
54 {
55 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
56 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
57
58 if (!priv->detected)
59 return -EPERM;
60
61 if (!ops->get_bool)
62 return -ENOSYS;
63
64 return ops->get_bool(dev, id, val);
65 }
66
sysinfo_get_int(struct udevice * dev,int id,int * val)67 int sysinfo_get_int(struct udevice *dev, int id, int *val)
68 {
69 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
70 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
71
72 if (!priv->detected)
73 return -EPERM;
74
75 if (!ops->get_int)
76 return -ENOSYS;
77
78 return ops->get_int(dev, id, val);
79 }
80
sysinfo_get_str(struct udevice * dev,int id,size_t size,char * val)81 int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val)
82 {
83 struct sysinfo_priv *priv = dev_get_uclass_priv(dev);
84 struct sysinfo_ops *ops = sysinfo_get_ops(dev);
85
86 if (!priv->detected)
87 return -EPERM;
88
89 if (!ops->get_str)
90 return -ENOSYS;
91
92 return ops->get_str(dev, id, size, val);
93 }
94
95 UCLASS_DRIVER(sysinfo) = {
96 .id = UCLASS_SYSINFO,
97 .name = "sysinfo",
98 .post_bind = dm_scan_fdt_dev,
99 .per_device_auto = sizeof(bool),
100 };
101