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