[PATCH v2 1/4] cmd: add temperature command

Currently, there is no way for users to check the readings from thermal sensors from U-boot console, only some boards print it during boot.
So, lets add a simple "temperature" command that allows listing thermal uclass devices and getting their value.
Note that the thermal devices are intenionally probed if list is used as almost always they will not get probed otherwise and there is no way for users to manually call probe on a certain device from console.
Assumption is made that temperature is returned in degrees C and not milidegrees like in Linux as this is what most drivers seem to return.
Signed-off-by: Robert Marko robert.marko@sartura.hr --- Changes in v2: * Drop <dm/uclass-internal.h> by using uclass_get_device_by_name() * Make the unit clear in help * Expand Kconfig boolean and help * Drop degree symbol as test doesnt work with it. --- cmd/Kconfig | 6 ++++ cmd/Makefile | 1 + cmd/temperature.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 cmd/temperature.c
diff --git a/cmd/Kconfig b/cmd/Kconfig index 3625ff2a50..ffbfed1f38 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1435,6 +1435,12 @@ config DEFAULT_SPI_MODE depends on CMD_SPI default 0
+config CMD_TEMPERATURE + bool "temperature - display the temperature from thermal sensors" + depends on DM_THERMAL + help + Provides a way to list thermal sensors and to get their readings. + config CMD_TSI148 bool "tsi148 - Command to access tsi148 device" help diff --git a/cmd/Makefile b/cmd/Makefile index 5e43a1e022..8874462f1a 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -152,6 +152,7 @@ obj-$(CONFIG_CMD_STRINGS) += strings.o obj-$(CONFIG_CMD_SMC) += smccc.o obj-$(CONFIG_CMD_SYSBOOT) += sysboot.o obj-$(CONFIG_CMD_STACKPROTECTOR_TEST) += stackprot_test.o +obj-$(CONFIG_CMD_TEMPERATURE) += temperature.o obj-$(CONFIG_CMD_TERMINAL) += terminal.o obj-$(CONFIG_CMD_TIME) += time.o obj-$(CONFIG_CMD_TIMER) += timer.o diff --git a/cmd/temperature.c b/cmd/temperature.c new file mode 100644 index 0000000000..420965de14 --- /dev/null +++ b/cmd/temperature.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +/* + * Copyright (c) 2022 Sartura Ltd. + * Written by Robert Marko robert.marko@sartura.hr + */ + +#include <common.h> +#include <command.h> +#include <dm.h> +#include <thermal.h> + +#define LIMIT_DEVNAME 30 + +static int do_get(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + struct udevice *dev; + int ret, temp; + + if (argc < 2) { + printf("thermal device not selected\n"); + return CMD_RET_FAILURE; + } + + ret = uclass_get_device_by_name(UCLASS_THERMAL, argv[1], &dev); + if (ret) { + printf("thermal device not found\n"); + return CMD_RET_FAILURE; + } + + ret = thermal_get_temp(dev, &temp); + if (ret) + return CMD_RET_FAILURE; + + printf("%s: %d C\n", dev->name, temp); + + return CMD_RET_SUCCESS; +} + +static int do_list(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + struct udevice *dev; + + printf("| %-*.*s| %-*.*s| %s\n", + LIMIT_DEVNAME, LIMIT_DEVNAME, "Device", + LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver", + "Parent"); + + uclass_foreach_dev_probe(UCLASS_THERMAL, dev) { + printf("| %-*.*s| %-*.*s| %s\n", + LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name, + LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name, + dev->parent->name); + } + + return CMD_RET_SUCCESS; +} + +static struct cmd_tbl temperature_subcmd[] = { + U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""), + U_BOOT_CMD_MKENT(get, 2, 1, do_get, "", ""), +}; + +static int do_temperature(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + struct cmd_tbl *cmd; + + argc--; + argv++; + + cmd = find_cmd_tbl(argv[0], temperature_subcmd, ARRAY_SIZE(temperature_subcmd)); + if (!cmd || argc > cmd->maxargs) + return CMD_RET_USAGE; + + return cmd->cmd(cmdtp, flag, argc, argv); +} + +U_BOOT_CMD(temperature, CONFIG_SYS_MAXARGS, 1, do_temperature, + "thermal sensor temperature", + "list\t\tshow list of temperature sensors\n" + "get [thermal device name]\tprint temperature in degrees C" +);

Add documentation for the temperature command.
Signed-off-by: Robert Marko robert.marko@sartura.hr --- doc/usage/cmd/temperature.rst | 50 +++++++++++++++++++++++++++++++++++ doc/usage/index.rst | 1 + 2 files changed, 51 insertions(+) create mode 100644 doc/usage/cmd/temperature.rst
diff --git a/doc/usage/cmd/temperature.rst b/doc/usage/cmd/temperature.rst new file mode 100644 index 0000000000..a5144ec50f --- /dev/null +++ b/doc/usage/cmd/temperature.rst @@ -0,0 +1,50 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +temperature command +=================== + +Synopsis +-------- + +:: + + temperature list + temperature get [thermal device name] + +Description +----------- + +The *temperature* command is used to list thermal sensors and get their +readings. + +The 'temperature list' command diplays the available thermal devices. + +The 'temperature get' command is used to get the reading in degrees C from +the desired device which is selected by passing its device name. + + thermal device name + device name of thermal sensor to select + +Example +------- + +:: + + + => temperature list + | Device | Driver | Parent + | tmon@610508110 | sparx5-temp | axi@600000000 + => + => temperature get tmon@610508110 + tmon@610508110: 42 C + +Configuration +------------- + +The *temperature* command is only available if CONFIG_CMD_TEMPERATURE=y. + +Return value +------------ + +The return value $? is set to 0 (true) if the command succeeded and to 1 (false) +otherwise. diff --git a/doc/usage/index.rst b/doc/usage/index.rst index 2ba8733b92..b6f307b2bd 100644 --- a/doc/usage/index.rst +++ b/doc/usage/index.rst @@ -62,6 +62,7 @@ Shell commands cmd/scp03 cmd/setexpr cmd/size + cmd/temperature cmd/true cmd/ums cmd/wdt

On Fri, 12 Aug 2022 at 12:16, Robert Marko robert.marko@sartura.hr wrote:
Add documentation for the temperature command.
Signed-off-by: Robert Marko robert.marko@sartura.hr
doc/usage/cmd/temperature.rst | 50 +++++++++++++++++++++++++++++++++++ doc/usage/index.rst | 1 + 2 files changed, 51 insertions(+) create mode 100644 doc/usage/cmd/temperature.rst
Reviewed-by: Simon Glass sjg@chromium.org

Provide a simple sandbox driver for the thermal uclass. It simply registers and returns 100 degrees C if requested.
Signed-off-by: Robert Marko robert.marko@sartura.hr --- arch/sandbox/dts/sandbox.dtsi | 4 ++++ arch/sandbox/dts/test.dts | 4 ++++ configs/sandbox_defconfig | 2 ++ drivers/thermal/Makefile | 1 + drivers/thermal/thermal_sandbox.c | 36 +++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 drivers/thermal/thermal_sandbox.c
diff --git a/arch/sandbox/dts/sandbox.dtsi b/arch/sandbox/dts/sandbox.dtsi index aa22b8765c..c24aee1d05 100644 --- a/arch/sandbox/dts/sandbox.dtsi +++ b/arch/sandbox/dts/sandbox.dtsi @@ -426,6 +426,10 @@ sandbox_tee { compatible = "sandbox,tee"; }; + + thermal { + compatible = "sandbox,thermal"; + }; };
&cros_ec { diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts index d1a8cc7bfb..f002233064 100644 --- a/arch/sandbox/dts/test.dts +++ b/arch/sandbox/dts/test.dts @@ -1668,6 +1668,10 @@ compatible = "sandbox,regmap_test"; }; }; + + thermal { + compatible = "sandbox,thermal"; + }; };
#include "sandbox_pmic.dtsi" diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig index eba7bcbb48..d4965248d4 100644 --- a/configs/sandbox_defconfig +++ b/configs/sandbox_defconfig @@ -77,6 +77,7 @@ CONFIG_CMD_PCI=y CONFIG_CMD_READ=y CONFIG_CMD_REMOTEPROC=y CONFIG_CMD_SPI=y +CONFIG_CMD_TEMPERATURE=y CONFIG_CMD_USB=y CONFIG_CMD_AXI=y CONFIG_CMD_SETEXPR_FMT=y @@ -282,6 +283,7 @@ CONFIG_SYSINFO=y CONFIG_SYSINFO_SANDBOX=y CONFIG_SYSINFO_GPIO=y CONFIG_SYSRESET=y +CONFIG_DM_THERMAL=y CONFIG_TIMER=y CONFIG_TIMER_EARLY=y CONFIG_SANDBOX_TIMER=y diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 6dda62bcd1..f2ee1df394 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -4,6 +4,7 @@ # Author: Nitin Garg nitin.garg@freescale.com
obj-$(CONFIG_DM_THERMAL) += thermal-uclass.o +obj-$(CONFIG_SANDBOX) += thermal_sandbox.o obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o obj-$(CONFIG_IMX_SCU_THERMAL) += imx_scu_thermal.o obj-$(CONFIG_SPARX5_THERMAL) += sparx5-temp.o diff --git a/drivers/thermal/thermal_sandbox.c b/drivers/thermal/thermal_sandbox.c new file mode 100644 index 0000000000..acc364feb0 --- /dev/null +++ b/drivers/thermal/thermal_sandbox.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2022 Sartura Ltd. + * Written by Robert Marko robert.marko@sartura.hr + * + * Sandbox driver for the thermal uclass. + */ + +#include <common.h> +#include <dm.h> +#include <thermal.h> + +int sandbox_thermal_get_temp(struct udevice *dev, int *temp) +{ + /* Simply return 100°C */ + *temp = 100; + + return 0; +} + +static const struct dm_thermal_ops sandbox_thermal_ops = { + .get_temp = sandbox_thermal_get_temp, +}; + +static const struct udevice_id sandbox_thermal_ids[] = { + { .compatible = "sandbox,thermal" }, + { } +}; + +U_BOOT_DRIVER(thermal_sandbox) = { + .name = "thermal-sandbox", + .id = UCLASS_THERMAL, + .of_match = sandbox_thermal_ids, + .ops = &sandbox_thermal_ops, + .flags = DM_FLAG_PRE_RELOC, +};

On Fri, 12 Aug 2022 at 12:16, Robert Marko robert.marko@sartura.hr wrote:
Provide a simple sandbox driver for the thermal uclass. It simply registers and returns 100 degrees C if requested.
Signed-off-by: Robert Marko robert.marko@sartura.hr
arch/sandbox/dts/sandbox.dtsi | 4 ++++ arch/sandbox/dts/test.dts | 4 ++++ configs/sandbox_defconfig | 2 ++ drivers/thermal/Makefile | 1 + drivers/thermal/thermal_sandbox.c | 36 +++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 drivers/thermal/thermal_sandbox.c
Reviewed-by: Simon Glass sjg@chromium.org

Add simple test for the temperature command.
Signed-off-by: Robert Marko robert.marko@sartura.hr --- test/cmd/Makefile | 1 + test/cmd/temperature.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/cmd/temperature.c
diff --git a/test/cmd/Makefile b/test/cmd/Makefile index c331757425..f9493357a5 100644 --- a/test/cmd/Makefile +++ b/test/cmd/Makefile @@ -13,3 +13,4 @@ obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o obj-$(CONFIG_CMD_PINMUX) += pinmux.o obj-$(CONFIG_CMD_PWM) += pwm.o obj-$(CONFIG_CMD_SETEXPR) += setexpr.o +obj-$(CONFIG_CMD_TEMPERATURE) += temperature.o diff --git a/test/cmd/temperature.c b/test/cmd/temperature.c new file mode 100644 index 0000000000..2a1ea0611d --- /dev/null +++ b/test/cmd/temperature.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Executes tests for temperature command + * + * Copyright (C) 2022 Sartura Ltd. + */ + +#include <common.h> +#include <command.h> +#include <dm.h> +#include <dm/test.h> +#include <test/test.h> +#include <test/ut.h> + +static int dm_test_cmd_temperature(struct unit_test_state *uts) +{ + struct udevice *dev; + + ut_assertok(uclass_get_device(UCLASS_THERMAL, 0, &dev)); + ut_assertnonnull(dev); + + ut_assertok(console_record_reset_enable()); + + /* Test that "temperature list" shows the sandbox device */ + ut_assertok(run_command("temperature list", 0)); + ut_assert_nextline("| Device | Driver | Parent"); + ut_assert_nextline("| thermal | thermal-sandbox | root_driver"); + ut_assert_console_end(); + + /* Test that "temperature get thermal" returns expected value */ + console_record_reset(); + ut_assertok(run_command("temperature get thermal", 0)); + ut_assert_nextline("thermal: 100 C"); + ut_assert_console_end(); + + return 0; +} + +DM_TEST(dm_test_cmd_temperature, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);

On Fri, 12 Aug 2022 at 12:16, Robert Marko robert.marko@sartura.hr wrote:
Add simple test for the temperature command.
Signed-off-by: Robert Marko robert.marko@sartura.hr
test/cmd/Makefile | 1 + test/cmd/temperature.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/cmd/temperature.c
Reviewed-by: Simon Glass sjg@chromium.org

On Fri, Aug 12, 2022 at 08:15:50PM +0200, Robert Marko wrote:
Add simple test for the temperature command.
Signed-off-by: Robert Marko robert.marko@sartura.hr Reviewed-by: Simon Glass sjg@chromium.org
test/cmd/Makefile | 1 + test/cmd/temperature.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/cmd/temperature.c
This causes failures such as: https://source.denx.de/u-boot/u-boot/-/jobs/490586 ---------------------------- Captured stdout setup ----------------------------- /u-boot Creating new bloblist size 400 at c000 Error binding driver 'thermal-sandbox': -96 Some drivers failed to bind initcall sequence 0000560e11b023a0 failed at call 000000000005a8ef (err=-96) ### ERROR ### Please RESET the board ###

On Sat, Sep 3, 2022 at 3:54 AM Tom Rini trini@konsulko.com wrote:
On Fri, Aug 12, 2022 at 08:15:50PM +0200, Robert Marko wrote:
Add simple test for the temperature command.
Signed-off-by: Robert Marko robert.marko@sartura.hr Reviewed-by: Simon Glass sjg@chromium.org
test/cmd/Makefile | 1 + test/cmd/temperature.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/cmd/temperature.c
This causes failures such as: https://source.denx.de/u-boot/u-boot/-/jobs/490586 ---------------------------- Captured stdout setup ----------------------------- /u-boot Creating new bloblist size 400 at c000 Error binding driver 'thermal-sandbox': -96 Some drivers failed to bind initcall sequence 0000560e11b023a0 failed at call 000000000005a8ef (err=-96) ### ERROR ### Please RESET the board ###
Ok, so its failing on SPL and flattree/noninstall variants, but why is the test even getting executed if CONFIG_CMD_TEMPERATURE is not set in their defconfig-s?
Regards, Robert
-- Tom

Hi Robert, H On Tue, 6 Sept 2022 at 04:50, Robert Marko robert.marko@sartura.hr wrote:
On Sat, Sep 3, 2022 at 3:54 AM Tom Rini trini@konsulko.com wrote:
On Fri, Aug 12, 2022 at 08:15:50PM +0200, Robert Marko wrote:
Add simple test for the temperature command.
Signed-off-by: Robert Marko robert.marko@sartura.hr Reviewed-by: Simon Glass sjg@chromium.org
test/cmd/Makefile | 1 + test/cmd/temperature.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/cmd/temperature.c
This causes failures such as: https://source.denx.de/u-boot/u-boot/-/jobs/490586 ---------------------------- Captured stdout setup ----------------------------- /u-boot Creating new bloblist size 400 at c000 Error binding driver 'thermal-sandbox': -96 Some drivers failed to bind initcall sequence 0000560e11b023a0 failed at call 000000000005a8ef (err=-96) ### ERROR ### Please RESET the board ###
Ok, so its failing on SPL and flattree/noninstall variants, but why is the test even getting executed if CONFIG_CMD_TEMPERATURE is not set in their defconfig-s?
It looks like you are enabling CMD_TEMPERATURE for sandbox_flattree. Error -96 means the uclass is missing, so make sure that is built.
For sandbox_spl the same applies. This is not about what happens in SPL itself, but what happens when sandbox SPL runs U-Boot proper.
Regards, Simon

On Tue, Sep 6, 2022 at 11:18 PM Simon Glass sjg@chromium.org wrote:
Hi Robert, H On Tue, 6 Sept 2022 at 04:50, Robert Marko robert.marko@sartura.hr wrote:
On Sat, Sep 3, 2022 at 3:54 AM Tom Rini trini@konsulko.com wrote:
On Fri, Aug 12, 2022 at 08:15:50PM +0200, Robert Marko wrote:
Add simple test for the temperature command.
Signed-off-by: Robert Marko robert.marko@sartura.hr Reviewed-by: Simon Glass sjg@chromium.org
test/cmd/Makefile | 1 + test/cmd/temperature.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/cmd/temperature.c
This causes failures such as: https://source.denx.de/u-boot/u-boot/-/jobs/490586 ---------------------------- Captured stdout setup ----------------------------- /u-boot Creating new bloblist size 400 at c000 Error binding driver 'thermal-sandbox': -96 Some drivers failed to bind initcall sequence 0000560e11b023a0 failed at call 000000000005a8ef (err=-96) ### ERROR ### Please RESET the board ###
Ok, so its failing on SPL and flattree/noninstall variants, but why is the test even getting executed if CONFIG_CMD_TEMPERATURE is not set in their defconfig-s?
It looks like you are enabling CMD_TEMPERATURE for sandbox_flattree. Error -96 means the uclass is missing, so make sure that is built.
For sandbox_spl the same applies. This is not about what happens in SPL itself, but what happens when sandbox SPL runs U-Boot proper.
Thanks,
I have sent a v3 yesterday that passes make check.
Regards, Robert
Regards, Simon

On Fri, 12 Aug 2022 at 12:16, Robert Marko robert.marko@sartura.hr wrote:
Currently, there is no way for users to check the readings from thermal sensors from U-boot console, only some boards print it during boot.
So, lets add a simple "temperature" command that allows listing thermal uclass devices and getting their value.
Note that the thermal devices are intenionally probed if list is used as almost always they will not get probed otherwise and there is no way for users to manually call probe on a certain device from console.
Assumption is made that temperature is returned in degrees C and not milidegrees like in Linux as this is what most drivers seem to return.
Signed-off-by: Robert Marko robert.marko@sartura.hr
Changes in v2:
- Drop <dm/uclass-internal.h> by using uclass_get_device_by_name()
- Make the unit clear in help
- Expand Kconfig boolean and help
- Drop degree symbol as test doesnt work with it.
cmd/Kconfig | 6 ++++ cmd/Makefile | 1 + cmd/temperature.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 cmd/temperature.c
Reviewed-by: Simon Glass sjg@chromium.org
participants (3)
-
Robert Marko
-
Simon Glass
-
Tom Rini