1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /*
4 * Copyright (c) 2022 Sartura Ltd.
5 * Written by Robert Marko <robert.marko@sartura.hr>
6 */
7
8 #include <command.h>
9 #include <dm.h>
10 #include <thermal.h>
11
12 #define LIMIT_DEVNAME 30
13
do_get(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])14 static int do_get(struct cmd_tbl *cmdtp, int flag, int argc,
15 char *const argv[])
16 {
17 struct udevice *dev;
18 int ret, temp;
19
20 if (argc < 2) {
21 printf("thermal device not selected\n");
22 return CMD_RET_FAILURE;
23 }
24
25 ret = uclass_get_device_by_name(UCLASS_THERMAL, argv[1], &dev);
26 if (ret) {
27 printf("thermal device not found\n");
28 return CMD_RET_FAILURE;
29 }
30
31 ret = thermal_get_temp(dev, &temp);
32 if (ret)
33 return CMD_RET_FAILURE;
34
35 printf("%s: %d C\n", dev->name, temp);
36
37 return CMD_RET_SUCCESS;
38 }
39
do_list(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])40 static int do_list(struct cmd_tbl *cmdtp, int flag, int argc,
41 char *const argv[])
42 {
43 struct udevice *dev;
44
45 printf("| %-*.*s| %-*.*s| %s\n",
46 LIMIT_DEVNAME, LIMIT_DEVNAME, "Device",
47 LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver",
48 "Parent");
49
50 uclass_foreach_dev_probe(UCLASS_THERMAL, dev) {
51 printf("| %-*.*s| %-*.*s| %s\n",
52 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name,
53 LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name,
54 dev->parent->name);
55 }
56
57 return CMD_RET_SUCCESS;
58 }
59
60 static struct cmd_tbl temperature_subcmd[] = {
61 U_BOOT_CMD_MKENT(list, 1, 1, do_list, "", ""),
62 U_BOOT_CMD_MKENT(get, 2, 1, do_get, "", ""),
63 };
64
do_temperature(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])65 static int do_temperature(struct cmd_tbl *cmdtp, int flag, int argc,
66 char *const argv[])
67 {
68 struct cmd_tbl *cmd;
69
70 argc--;
71 argv++;
72
73 cmd = find_cmd_tbl(argv[0], temperature_subcmd, ARRAY_SIZE(temperature_subcmd));
74 if (!cmd || argc > cmd->maxargs)
75 return CMD_RET_USAGE;
76
77 return cmd->cmd(cmdtp, flag, argc, argv);
78 }
79
80 U_BOOT_CMD(temperature, CONFIG_SYS_MAXARGS, 1, do_temperature,
81 "thermal sensor temperature",
82 "list\t\tshow list of temperature sensors\n"
83 "get [thermal device name]\tprint temperature in degrees C"
84 );
85