/src/u-boot/cmd/temperature.c
Line | Count | Source |
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 | 0 | #define LIMIT_DEVNAME 30 |
13 | | |
14 | | static int do_get(struct cmd_tbl *cmdtp, int flag, int argc, |
15 | | char *const argv[]) |
16 | 0 | { |
17 | 0 | struct udevice *dev; |
18 | 0 | int ret, temp; |
19 | |
|
20 | 0 | if (argc < 2) { |
21 | 0 | printf("thermal device not selected\n"); |
22 | 0 | return CMD_RET_FAILURE; |
23 | 0 | } |
24 | | |
25 | 0 | ret = uclass_get_device_by_name(UCLASS_THERMAL, argv[1], &dev); |
26 | 0 | if (ret) { |
27 | 0 | printf("thermal device not found\n"); |
28 | 0 | return CMD_RET_FAILURE; |
29 | 0 | } |
30 | | |
31 | 0 | ret = thermal_get_temp(dev, &temp); |
32 | 0 | if (ret) |
33 | 0 | return CMD_RET_FAILURE; |
34 | | |
35 | 0 | printf("%s: %d mC\n", dev->name, temp); |
36 | |
|
37 | 0 | return CMD_RET_SUCCESS; |
38 | 0 | } |
39 | | |
40 | | static int do_list(struct cmd_tbl *cmdtp, int flag, int argc, |
41 | | char *const argv[]) |
42 | 0 | { |
43 | 0 | struct udevice *dev; |
44 | |
|
45 | 0 | printf("| %-*.*s| %-*.*s| %s\n", |
46 | 0 | LIMIT_DEVNAME, LIMIT_DEVNAME, "Device", |
47 | 0 | LIMIT_DEVNAME, LIMIT_DEVNAME, "Driver", |
48 | 0 | "Parent"); |
49 | |
|
50 | 0 | uclass_foreach_dev_probe(UCLASS_THERMAL, dev) { |
51 | 0 | printf("| %-*.*s| %-*.*s| %s\n", |
52 | 0 | LIMIT_DEVNAME, LIMIT_DEVNAME, dev->name, |
53 | 0 | LIMIT_DEVNAME, LIMIT_DEVNAME, dev->driver->name, |
54 | 0 | dev->parent->name); |
55 | 0 | } |
56 | |
|
57 | 0 | return CMD_RET_SUCCESS; |
58 | 0 | } |
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 | | |
65 | | static int do_temperature(struct cmd_tbl *cmdtp, int flag, int argc, |
66 | | char *const argv[]) |
67 | 0 | { |
68 | 0 | struct cmd_tbl *cmd; |
69 | |
|
70 | 0 | argc--; |
71 | 0 | argv++; |
72 | |
|
73 | 0 | cmd = find_cmd_tbl(argv[0], temperature_subcmd, ARRAY_SIZE(temperature_subcmd)); |
74 | 0 | if (!cmd || argc > cmd->maxargs) |
75 | 0 | return CMD_RET_USAGE; |
76 | | |
77 | 0 | return cmd->cmd(cmdtp, flag, argc, argv); |
78 | 0 | } |
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 | | ); |