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 [LEDST_BLINK] = "blink",
21 enum led_state_t get_led_cmd(char *var)
25 for (i = 0; i < LEDST_COUNT; i++) {
26 if (!strncmp(var, state_label[i], strlen(var)))
33 static int show_led_state(struct udevice *dev)
37 ret = led_get_state(dev);
38 if (ret >= LEDST_COUNT)
41 printf("%s\n", state_label[ret]);
46 static int list_leds(void)
51 for (uclass_find_first_device(UCLASS_LED, &dev);
53 uclass_find_next_device(&dev)) {
54 struct led_uc_plat *plat = dev_get_uclass_plat(dev);
58 printf("%-15s ", plat->label);
59 if (device_active(dev)) {
60 ret = show_led_state(dev);
62 printf("Error %d\n", ret);
64 printf("<inactive>\n");
71 int do_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
74 const char *led_label;
79 /* Validate arguments */
83 if (strncmp(led_label, "list", 4) == 0)
86 cmd = argc > 2 ? get_led_cmd(argv[2]) : LEDST_COUNT;
87 if (cmd == LEDST_BLINK) {
90 freq_ms = dectoul(argv[3], NULL);
92 ret = led_get_by_label(led_label, &dev);
94 printf("LED '%s' not found (err=%d)\n", led_label, ret);
95 return CMD_RET_FAILURE;
101 ret = led_set_state(dev, cmd);
104 ret = led_set_period(dev, freq_ms);
106 ret = led_set_state(dev, LEDST_BLINK);
109 printf("LED '%s': ", led_label);
110 ret = show_led_state(dev);
114 printf("LED '%s' operation failed (err=%d)\n", led_label, ret);
115 return CMD_RET_FAILURE;
121 #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK)
122 #define BLINK "|blink [blink-freq in ms]"
130 "<led_label> on|off|toggle" BLINK "\tChange LED state\n"
131 "led <led_label>\tGet LED state\n"
132 "led list\t\tshow a list of LEDs"