Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 7 | #include <command.h> |
| 8 | #include <dm.h> |
| 9 | #include <led.h> |
| 10 | #include <dm/uclass-internal.h> |
| 11 | |
| 12 | #define LED_TOGGLE LEDST_COUNT |
| 13 | |
| 14 | static const char *const state_label[] = { |
| 15 | [LEDST_OFF] = "off", |
| 16 | [LEDST_ON] = "on", |
| 17 | [LEDST_TOGGLE] = "toggle", |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 18 | [LEDST_BLINK] = "blink", |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | enum led_state_t get_led_cmd(char *var) |
| 22 | { |
| 23 | int i; |
| 24 | |
| 25 | for (i = 0; i < LEDST_COUNT; i++) { |
| 26 | if (!strncmp(var, state_label[i], strlen(var))) |
| 27 | return i; |
| 28 | } |
| 29 | |
| 30 | return -1; |
| 31 | } |
| 32 | |
| 33 | static int show_led_state(struct udevice *dev) |
| 34 | { |
| 35 | int ret; |
| 36 | |
| 37 | ret = led_get_state(dev); |
| 38 | if (ret >= LEDST_COUNT) |
| 39 | ret = -EINVAL; |
| 40 | if (ret >= 0) |
| 41 | printf("%s\n", state_label[ret]); |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static int list_leds(void) |
| 47 | { |
| 48 | struct udevice *dev; |
| 49 | int ret; |
| 50 | |
| 51 | for (uclass_find_first_device(UCLASS_LED, &dev); |
| 52 | dev; |
| 53 | uclass_find_next_device(&dev)) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 54 | struct led_uc_plat *plat = dev_get_uclass_plat(dev); |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 55 | |
| 56 | if (!plat->label) |
| 57 | continue; |
| 58 | printf("%-15s ", plat->label); |
| 59 | if (device_active(dev)) { |
| 60 | ret = show_led_state(dev); |
| 61 | if (ret < 0) |
| 62 | printf("Error %d\n", ret); |
| 63 | } else { |
| 64 | printf("<inactive>\n"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 71 | int do_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 72 | { |
| 73 | enum led_state_t cmd; |
| 74 | const char *led_label; |
| 75 | struct udevice *dev; |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 76 | int freq_ms = 0; |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 77 | int ret; |
| 78 | |
| 79 | /* Validate arguments */ |
| 80 | if (argc < 2) |
| 81 | return CMD_RET_USAGE; |
| 82 | led_label = argv[1]; |
Heiko Schocher | 741f269 | 2019-05-27 08:14:16 +0200 | [diff] [blame] | 83 | if (strncmp(led_label, "list", 4) == 0) |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 84 | return list_leds(); |
| 85 | |
| 86 | cmd = argc > 2 ? get_led_cmd(argv[2]) : LEDST_COUNT; |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 87 | if (cmd == LEDST_BLINK) { |
| 88 | if (argc < 4) |
| 89 | return CMD_RET_USAGE; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 90 | freq_ms = dectoul(argv[3], NULL); |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 91 | } |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 92 | ret = led_get_by_label(led_label, &dev); |
| 93 | if (ret) { |
| 94 | printf("LED '%s' not found (err=%d)\n", led_label, ret); |
| 95 | return CMD_RET_FAILURE; |
| 96 | } |
| 97 | switch (cmd) { |
| 98 | case LEDST_OFF: |
| 99 | case LEDST_ON: |
| 100 | case LEDST_TOGGLE: |
| 101 | ret = led_set_state(dev, cmd); |
| 102 | break; |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 103 | case LEDST_BLINK: |
| 104 | ret = led_set_period(dev, freq_ms); |
| 105 | if (!ret) |
| 106 | ret = led_set_state(dev, LEDST_BLINK); |
| 107 | break; |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 108 | case LEDST_COUNT: |
| 109 | printf("LED '%s': ", led_label); |
| 110 | ret = show_led_state(dev); |
| 111 | break; |
| 112 | } |
| 113 | if (ret < 0) { |
| 114 | printf("LED '%s' operation failed (err=%d)\n", led_label, ret); |
| 115 | return CMD_RET_FAILURE; |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Mikhail Kshevetskiy | 97613b6 | 2024-08-14 14:08:30 +0400 | [diff] [blame] | 121 | #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK) |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 122 | #define BLINK "|blink [blink-freq in ms]" |
| 123 | #else |
| 124 | #define BLINK "" |
| 125 | #endif |
| 126 | |
| 127 | U_BOOT_CMD( |
| 128 | led, 4, 1, do_led, |
| 129 | "manage LEDs", |
| 130 | "<led_label> on|off|toggle" BLINK "\tChange LED state\n" |
Anatolij Gustschin | 486201d | 2019-06-17 15:33:55 +0200 | [diff] [blame] | 131 | "led <led_label>\tGet LED state\n" |
Simon Glass | fc8a529 | 2017-04-10 11:34:59 -0600 | [diff] [blame] | 132 | "led list\t\tshow a list of LEDs" |
| 133 | ); |