2 * Copyright (c) 2017 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/uclass-internal.h>
14 #define LED_TOGGLE LEDST_COUNT
16 static const char *const state_label[] = {
19 [LEDST_TOGGLE] = "toggle",
20 #ifdef CONFIG_LED_BLINK
21 [LEDST_BLINK] = "blink",
25 enum led_state_t get_led_cmd(char *var)
29 for (i = 0; i < LEDST_COUNT; i++) {
30 if (!strncmp(var, state_label[i], strlen(var)))
37 static int show_led_state(struct udevice *dev)
41 ret = led_get_state(dev);
42 if (ret >= LEDST_COUNT)
45 printf("%s\n", state_label[ret]);
50 static int list_leds(void)
55 for (uclass_find_first_device(UCLASS_LED, &dev);
57 uclass_find_next_device(&dev)) {
58 struct led_uc_plat *plat = dev_get_uclass_platdata(dev);
62 printf("%-15s ", plat->label);
63 if (device_active(dev)) {
64 ret = show_led_state(dev);
66 printf("Error %d\n", ret);
68 printf("<inactive>\n");
75 int do_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
78 const char *led_label;
80 #ifdef CONFIG_LED_BLINK
85 /* Validate arguments */
89 if (*led_label == 'l')
92 cmd = argc > 2 ? get_led_cmd(argv[2]) : LEDST_COUNT;
93 #ifdef CONFIG_LED_BLINK
94 if (cmd == LEDST_BLINK) {
97 freq_ms = simple_strtoul(argv[3], NULL, 10);
100 ret = led_get_by_label(led_label, &dev);
102 printf("LED '%s' not found (err=%d)\n", led_label, ret);
103 return CMD_RET_FAILURE;
109 ret = led_set_state(dev, cmd);
111 #ifdef CONFIG_LED_BLINK
113 ret = led_set_period(dev, freq_ms);
115 ret = led_set_state(dev, LEDST_BLINK);
119 printf("LED '%s': ", led_label);
120 ret = show_led_state(dev);
124 printf("LED '%s' operation failed (err=%d)\n", led_label, ret);
125 return CMD_RET_FAILURE;
131 #ifdef CONFIG_LED_BLINK
132 #define BLINK "|blink [blink-freq in ms]"
140 "<led_label> on|off|toggle" BLINK "\tChange LED state\n"
141 "led [<led_label>\tGet LED state\n"
142 "led list\t\tshow a list of LEDs"