1 // SPDX-License-Identifier: GPL-2.0+
6 * Based on cmd_led.c patch from:
13 #include <status_led.h>
17 char *string; /* String for use in the command */
18 led_id_t mask; /* Mask used for calling __led_set() */
19 void (*off)(void); /* Optional function for turning LED off */
20 void (*on)(void); /* Optional function for turning LED on */
21 void (*toggle)(void);/* Optional function for toggling LED */
24 typedef struct led_tbl_s led_tbl_t;
26 static const led_tbl_t led_commands[] = {
27 #ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
28 #ifdef CONFIG_LED_STATUS0
29 { "0", CONFIG_LED_STATUS_BIT, NULL, NULL, NULL },
31 #ifdef CONFIG_LED_STATUS1
32 { "1", CONFIG_LED_STATUS_BIT1, NULL, NULL, NULL },
34 #ifdef CONFIG_LED_STATUS2
35 { "2", CONFIG_LED_STATUS_BIT2, NULL, NULL, NULL },
37 #ifdef CONFIG_LED_STATUS3
38 { "3", CONFIG_LED_STATUS_BIT3, NULL, NULL, NULL },
40 #ifdef CONFIG_LED_STATUS4
41 { "4", CONFIG_LED_STATUS_BIT4, NULL, NULL, NULL },
43 #ifdef CONFIG_LED_STATUS5
44 { "5", CONFIG_LED_STATUS_BIT5, NULL, NULL, NULL },
47 #ifdef CONFIG_LED_STATUS_GREEN
48 { "green", CONFIG_LED_STATUS_GREEN, green_led_off, green_led_on, NULL },
50 #ifdef CONFIG_LED_STATUS_YELLOW
51 { "yellow", CONFIG_LED_STATUS_YELLOW, yellow_led_off, yellow_led_on,
54 #ifdef CONFIG_LED_STATUS_RED
55 { "red", CONFIG_LED_STATUS_RED, red_led_off, red_led_on, NULL },
57 #ifdef CONFIG_LED_STATUS_BLUE
58 { "blue", CONFIG_LED_STATUS_BLUE, blue_led_off, blue_led_on, NULL },
60 { NULL, 0, NULL, NULL, NULL }
63 enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE, LED_BLINK };
65 enum led_cmd get_led_cmd(char *var)
67 if (strcmp(var, "off") == 0)
69 if (strcmp(var, "on") == 0)
71 if (strcmp(var, "toggle") == 0)
73 if (strcmp(var, "blink") == 0)
80 * LED drivers providing a blinking LED functionality, like the
81 * PCA9551, can override this empty weak function
83 void __weak __led_blink(led_id_t mask, int freq)
87 int do_legacy_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
93 /* Validate arguments */
94 if ((argc < 3) || (argc > 4))
97 cmd = get_led_cmd(argv[2]);
102 for (i = 0; led_commands[i].string; i++) {
103 if ((strcmp("all", argv[1]) == 0) ||
104 (strcmp(led_commands[i].string, argv[1]) == 0)) {
108 if (led_commands[i].on)
109 led_commands[i].on();
111 __led_set(led_commands[i].mask,
112 CONFIG_LED_STATUS_ON);
115 if (led_commands[i].off)
116 led_commands[i].off();
118 __led_set(led_commands[i].mask,
119 CONFIG_LED_STATUS_OFF);
122 if (led_commands[i].toggle)
123 led_commands[i].toggle();
125 __led_toggle(led_commands[i].mask);
129 return CMD_RET_USAGE;
131 freq = dectoul(argv[3], NULL);
132 __led_blink(led_commands[i].mask, freq);
134 /* Need to set only 1 led if led_name wasn't 'all' */
135 if (strcmp("all", argv[1]) != 0)
140 /* If we ran out of matches, print Usage */
142 return CMD_RET_USAGE;
149 led, 4, 1, do_legacy_led,
151 #ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
152 #ifdef CONFIG_LED_STATUS0
155 #ifdef CONFIG_LED_STATUS1
158 #ifdef CONFIG_LED_STATUS2
161 #ifdef CONFIG_LED_STATUS3
164 #ifdef CONFIG_LED_STATUS4
167 #ifdef CONFIG_LED_STATUS5
171 #ifdef CONFIG_LED_STATUS_GREEN
174 #ifdef CONFIG_LED_STATUS_YELLOW
177 #ifdef CONFIG_LED_STATUS_RED
180 #ifdef CONFIG_LED_STATUS_BLUE
183 "all] [on|off|toggle|blink] [blink-freq in ms]",
184 "[led_name] [on|off|toggle|blink] sets or clears led(s)"