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