1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Google, Inc
10 #include <dm/uclass-internal.h>
12 #define LED_TOGGLE LEDST_COUNT
14 static const char *const state_label[] = {
17 [LEDST_TOGGLE] = "toggle",
18 #ifdef CONFIG_LED_BLINK
19 [LEDST_BLINK] = "blink",
23 enum led_state_t get_led_cmd(char *var)
27 for (i = 0; i < LEDST_COUNT; i++) {
28 if (!strncmp(var, state_label[i], strlen(var)))
35 static int show_led_state(struct udevice *dev)
39 ret = led_get_state(dev);
40 if (ret >= LEDST_COUNT)
43 printf("%s\n", state_label[ret]);
48 static int list_leds(void)
53 for (uclass_find_first_device(UCLASS_LED, &dev);
55 uclass_find_next_device(&dev)) {
56 struct led_uc_plat *plat = dev_get_uclass_plat(dev);
60 printf("%-15s ", plat->label);
61 if (device_active(dev)) {
62 ret = show_led_state(dev);
64 printf("Error %d\n", ret);
66 printf("<inactive>\n");
73 int do_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
76 const char *led_label;
78 #ifdef CONFIG_LED_BLINK
83 /* Validate arguments */
87 if (strncmp(led_label, "list", 4) == 0)
90 cmd = argc > 2 ? get_led_cmd(argv[2]) : LEDST_COUNT;
91 #ifdef CONFIG_LED_BLINK
92 if (cmd == LEDST_BLINK) {
95 freq_ms = dectoul(argv[3], NULL);
98 ret = led_get_by_label(led_label, &dev);
100 printf("LED '%s' not found (err=%d)\n", led_label, ret);
101 return CMD_RET_FAILURE;
107 ret = led_set_state(dev, cmd);
109 #ifdef CONFIG_LED_BLINK
111 ret = led_set_period(dev, freq_ms);
113 ret = led_set_state(dev, LEDST_BLINK);
117 printf("LED '%s': ", led_label);
118 ret = show_led_state(dev);
122 printf("LED '%s' operation failed (err=%d)\n", led_label, ret);
123 return CMD_RET_FAILURE;
129 #ifdef CONFIG_LED_BLINK
130 #define BLINK "|blink [blink-freq in ms]"
138 "<led_label> on|off|toggle" BLINK "\tChange LED state\n"
139 "led <led_label>\tGet LED state\n"
140 "led list\t\tshow a list of LEDs"