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