Philippe Reynes | 325141a | 2020-07-24 18:19:47 +0200 | [diff] [blame] | 1 | // 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 Reynes | 325141a | 2020-07-24 18:19:47 +0200 | [diff] [blame] | 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)) { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 39 | struct button_uc_plat *plat = dev_get_uclass_plat(dev); |
Philippe Reynes | 325141a | 2020-07-24 18:19:47 +0200 | [diff] [blame] | 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 | |
Heinrich Schuchardt | a6bfd71 | 2020-09-14 12:50:56 +0200 | [diff] [blame] | 77 | return !ret; |
Philippe Reynes | 325141a | 2020-07-24 18:19:47 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | U_BOOT_CMD( |
Heinrich Schuchardt | a6bfd71 | 2020-09-14 12:50:56 +0200 | [diff] [blame] | 81 | button, 2, 1, do_button, |
Philippe Reynes | 325141a | 2020-07-24 18:19:47 +0200 | [diff] [blame] | 82 | "manage buttons", |
| 83 | "<button_label> \tGet button state\n" |
| 84 | "button list\t\tShow a list of buttons" |
| 85 | ); |