1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 #include <dm/uclass-internal.h>
13 #define LED_TOGGLE LEDST_COUNT
15 static const char *const state_label[] = {
18 [LEDST_TOGGLE] = "toggle",
19 #ifdef CONFIG_LED_BLINK
20 [LEDST_BLINK] = "blink",
24 enum led_state_t get_led_cmd(char *var)
28 for (i = 0; i < LEDST_COUNT; i++) {
29 if (!strncmp(var, state_label[i], strlen(var)))
36 static int show_led_state(struct udevice *dev)
40 ret = led_get_state(dev);
41 if (ret >= LEDST_COUNT)
44 printf("%s\n", state_label[ret]);
49 static int list_leds(void)
54 for (uclass_find_first_device(UCLASS_LED, &dev);
56 uclass_find_next_device(&dev)) {
57 struct led_uc_plat *plat = dev_get_uclass_plat(dev);
61 printf("%-15s ", plat->label);
62 if (device_active(dev)) {
63 ret = show_led_state(dev);
65 printf("Error %d\n", ret);
67 printf("<inactive>\n");
74 int do_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
77 const char *led_label;
79 #ifdef CONFIG_LED_BLINK
84 /* Validate arguments */
88 if (strncmp(led_label, "list", 4) == 0)
91 cmd = argc > 2 ? get_led_cmd(argv[2]) : LEDST_COUNT;
92 #ifdef CONFIG_LED_BLINK
93 if (cmd == LEDST_BLINK) {
96 freq_ms = dectoul(argv[3], NULL);
99 ret = led_get_by_label(led_label, &dev);
101 printf("LED '%s' not found (err=%d)\n", led_label, ret);
102 return CMD_RET_FAILURE;
108 ret = led_set_state(dev, cmd);
110 #ifdef CONFIG_LED_BLINK
112 ret = led_set_period(dev, freq_ms);
114 ret = led_set_state(dev, LEDST_BLINK);
118 printf("LED '%s': ", led_label);
119 ret = show_led_state(dev);
123 printf("LED '%s' operation failed (err=%d)\n", led_label, ret);
124 return CMD_RET_FAILURE;
130 #ifdef CONFIG_LED_BLINK
131 #define BLINK "|blink [blink-freq in ms]"
139 "<led_label> on|off|toggle" BLINK "\tChange LED state\n"
140 "led <led_label>\tGet LED state\n"
141 "led list\t\tshow a list of LEDs"