]>
Commit | Line | Data |
---|---|---|
325141a6 PR |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Copyright (C) 2020 Philippe Reynes <[email protected]> | |
4 | * | |
5 | * Based on led.c | |
6 | */ | |
7 | ||
325141a6 PR |
8 | #include <command.h> |
9 | #include <dm.h> | |
10 | #include <button.h> | |
11 | #include <dm/uclass-internal.h> | |
12 | ||
13 | static const char *const state_label[] = { | |
14 | [BUTTON_OFF] = "off", | |
15 | [BUTTON_ON] = "on", | |
16 | }; | |
17 | ||
18 | static int show_button_state(struct udevice *dev) | |
19 | { | |
20 | int ret; | |
21 | ||
22 | ret = button_get_state(dev); | |
23 | if (ret >= BUTTON_COUNT) | |
24 | ret = -EINVAL; | |
25 | if (ret >= 0) | |
26 | printf("%s\n", state_label[ret]); | |
27 | ||
28 | return ret; | |
29 | } | |
30 | ||
31 | static int list_buttons(void) | |
32 | { | |
33 | struct udevice *dev; | |
34 | int ret; | |
35 | ||
36 | for (uclass_find_first_device(UCLASS_BUTTON, &dev); | |
37 | dev; | |
38 | uclass_find_next_device(&dev)) { | |
caa4daa2 | 39 | struct button_uc_plat *plat = dev_get_uclass_plat(dev); |
325141a6 PR |
40 | |
41 | if (!plat->label) | |
42 | continue; | |
43 | printf("%-15s ", plat->label); | |
44 | if (device_active(dev)) { | |
45 | ret = show_button_state(dev); | |
46 | if (ret < 0) | |
47 | printf("Error %d\n", ret); | |
48 | } else { | |
49 | printf("<inactive>\n"); | |
50 | } | |
51 | } | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
56 | int do_button(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) | |
57 | { | |
58 | const char *button_label; | |
59 | struct udevice *dev; | |
60 | int ret; | |
61 | ||
62 | /* Validate arguments */ | |
63 | if (argc < 2) | |
64 | return CMD_RET_USAGE; | |
65 | button_label = argv[1]; | |
66 | if (strncmp(button_label, "list", 4) == 0) | |
67 | return list_buttons(); | |
68 | ||
69 | ret = button_get_by_label(button_label, &dev); | |
70 | if (ret) { | |
71 | printf("Button '%s' not found (err=%d)\n", button_label, ret); | |
72 | return CMD_RET_FAILURE; | |
73 | } | |
74 | ||
75 | ret = show_button_state(dev); | |
76 | ||
a6bfd71a | 77 | return !ret; |
325141a6 PR |
78 | } |
79 | ||
80 | U_BOOT_CMD( | |
a6bfd71a | 81 | button, 2, 1, do_button, |
325141a6 PR |
82 | "manage buttons", |
83 | "<button_label> \tGet button state\n" | |
84 | "button list\t\tShow a list of buttons" | |
85 | ); |