blob: 3e6db3f5b8eb5cef87031b399eae51d7a55466a6 [file] [log] [blame]
Philippe Reynesa558b182020-07-24 18:19:47 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com>
4 *
5 * Based on led.c
6 */
7
Philippe Reynesa558b182020-07-24 18:19:47 +02008#include <command.h>
9#include <dm.h>
10#include <button.h>
11#include <dm/uclass-internal.h>
12
13static const char *const state_label[] = {
14 [BUTTON_OFF] = "off",
15 [BUTTON_ON] = "on",
16};
17
18static 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
31static 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)) {
Simon Glass71fa5b42020-12-03 16:55:18 -070039 struct button_uc_plat *plat = dev_get_uclass_plat(dev);
Philippe Reynesa558b182020-07-24 18:19:47 +020040
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
56int 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
Heinrich Schuchardtadee5df2020-09-14 12:50:56 +020077 return !ret;
Philippe Reynesa558b182020-07-24 18:19:47 +020078}
79
80U_BOOT_CMD(
Heinrich Schuchardtadee5df2020-09-14 12:50:56 +020081 button, 2, 1, do_button,
Philippe Reynesa558b182020-07-24 18:19:47 +020082 "manage buttons",
83 "<button_label> \tGet button state\n"
84 "button list\t\tShow a list of buttons"
85);