Simon Glass | 3a8ee3d | 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 | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_SYSINFO |
| 8 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <sysinfo.h> |
| 12 | |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 13 | struct sysinfo_priv { |
| 14 | bool detected; |
| 15 | }; |
| 16 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 17 | int sysinfo_get(struct udevice **devp) |
| 18 | { |
| 19 | return uclass_first_device_err(UCLASS_SYSINFO, devp); |
| 20 | } |
| 21 | |
| 22 | int sysinfo_detect(struct udevice *dev) |
| 23 | { |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 24 | int ret; |
| 25 | struct sysinfo_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 26 | struct sysinfo_ops *ops = sysinfo_get_ops(dev); |
| 27 | |
| 28 | if (!ops->detect) |
| 29 | return -ENOSYS; |
| 30 | |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 31 | ret = ops->detect(dev); |
| 32 | if (!ret) |
| 33 | priv->detected = true; |
| 34 | |
| 35 | return ret; |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | int sysinfo_get_fit_loadable(struct udevice *dev, int index, const char *type, |
| 39 | const char **strp) |
| 40 | { |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 41 | struct sysinfo_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 42 | struct sysinfo_ops *ops = sysinfo_get_ops(dev); |
| 43 | |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 44 | if (!priv->detected) |
| 45 | return -EPERM; |
| 46 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 47 | if (!ops->get_fit_loadable) |
| 48 | return -ENOSYS; |
| 49 | |
| 50 | return ops->get_fit_loadable(dev, index, type, strp); |
| 51 | } |
| 52 | |
| 53 | int sysinfo_get_bool(struct udevice *dev, int id, bool *val) |
| 54 | { |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 55 | struct sysinfo_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 56 | struct sysinfo_ops *ops = sysinfo_get_ops(dev); |
| 57 | |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 58 | if (!priv->detected) |
| 59 | return -EPERM; |
| 60 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 61 | if (!ops->get_bool) |
| 62 | return -ENOSYS; |
| 63 | |
| 64 | return ops->get_bool(dev, id, val); |
| 65 | } |
| 66 | |
| 67 | int sysinfo_get_int(struct udevice *dev, int id, int *val) |
| 68 | { |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 69 | struct sysinfo_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 70 | struct sysinfo_ops *ops = sysinfo_get_ops(dev); |
| 71 | |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 72 | if (!priv->detected) |
| 73 | return -EPERM; |
| 74 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 75 | if (!ops->get_int) |
| 76 | return -ENOSYS; |
| 77 | |
| 78 | return ops->get_int(dev, id, val); |
| 79 | } |
| 80 | |
| 81 | int sysinfo_get_str(struct udevice *dev, int id, size_t size, char *val) |
| 82 | { |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 83 | struct sysinfo_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 84 | struct sysinfo_ops *ops = sysinfo_get_ops(dev); |
| 85 | |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 86 | if (!priv->detected) |
| 87 | return -EPERM; |
| 88 | |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 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, |
Sean Anderson | 4d65c6b | 2021-04-20 10:50:56 -0400 | [diff] [blame] | 99 | .per_device_auto = sizeof(bool), |
Simon Glass | 3a8ee3d | 2020-11-05 06:32:05 -0700 | [diff] [blame] | 100 | }; |