U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/4] cmd: add temperature command
@ 2022-09-06 11:30 Robert Marko
  2022-09-06 11:30 ` [PATCH v3 2/4] doc: cmd: temperature: add documentation Robert Marko
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Robert Marko @ 2022-09-06 11:30 UTC (permalink / raw)
  To: sjg, pali, u-boot, trini; +Cc: luka.perkov, Robert Marko

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>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
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 8ea064b8d2..4376909ca8 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1445,6 +1445,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 6e87522b62..141e440d01 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"
+);
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread
* [PATCH v3 1/4] cmd: add temperature command
@ 2022-09-06 11:31 Robert Marko
  2022-09-06 11:31 ` [PATCH v3 2/4] doc: cmd: temperature: add documentation Robert Marko
  0 siblings, 1 reply; 9+ messages in thread
From: Robert Marko @ 2022-09-06 11:31 UTC (permalink / raw)
  To: sjg, pali, u-boot, trini; +Cc: luka.perkov, Robert Marko

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>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
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 8ea064b8d2..4376909ca8 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1445,6 +1445,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 6e87522b62..141e440d01 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"
+);
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-10-11 21:38 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-06 11:30 [PATCH v3 1/4] cmd: add temperature command Robert Marko
2022-09-06 11:30 ` [PATCH v3 2/4] doc: cmd: temperature: add documentation Robert Marko
2022-10-11 21:37   ` Tom Rini
2022-09-06 11:30 ` [PATCH v3 3/4] thermal: add sandbox driver Robert Marko
2022-10-11 21:37   ` Tom Rini
2022-09-06 11:30 ` [PATCH v3 4/4] test: cmd: add test for temperature command Robert Marko
2022-10-11 21:37   ` Tom Rini
2022-10-11 21:37 ` [PATCH v3 1/4] cmd: add " Tom Rini
  -- strict thread matches above, loose matches on Subject: below --
2022-09-06 11:31 Robert Marko
2022-09-06 11:31 ` [PATCH v3 2/4] doc: cmd: temperature: add documentation Robert Marko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox