]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
b633f66f JK |
2 | /* |
3 | * (C) Copyright 2010 | |
4 | * Jason Kridner <[email protected]> | |
5 | * | |
6 | * Based on cmd_led.c patch from: | |
7 | * http://www.mail-archive.com/[email protected]/msg06873.html | |
8 | * (C) Copyright 2008 | |
9 | * Ulf Samuelsson <[email protected]> | |
b633f66f JK |
10 | */ |
11 | ||
b633f66f JK |
12 | #include <command.h> |
13 | #include <status_led.h> | |
03de305e | 14 | #include <vsprintf.h> |
b633f66f JK |
15 | |
16 | struct led_tbl_s { | |
17 | char *string; /* String for use in the command */ | |
18 | led_id_t mask; /* Mask used for calling __led_set() */ | |
4086b51c JK |
19 | void (*off)(void); /* Optional function for turning LED off */ |
20 | void (*on)(void); /* Optional function for turning LED on */ | |
b8bc8973 | 21 | void (*toggle)(void);/* Optional function for toggling LED */ |
b633f66f JK |
22 | }; |
23 | ||
24 | typedef struct led_tbl_s led_tbl_t; | |
25 | ||
26 | static const led_tbl_t led_commands[] = { | |
2d8d190c UM |
27 | #ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC |
28 | #ifdef CONFIG_LED_STATUS0 | |
29 | { "0", CONFIG_LED_STATUS_BIT, NULL, NULL, NULL }, | |
b633f66f | 30 | #endif |
2d8d190c UM |
31 | #ifdef CONFIG_LED_STATUS1 |
32 | { "1", CONFIG_LED_STATUS_BIT1, NULL, NULL, NULL }, | |
b633f66f | 33 | #endif |
2d8d190c UM |
34 | #ifdef CONFIG_LED_STATUS2 |
35 | { "2", CONFIG_LED_STATUS_BIT2, NULL, NULL, NULL }, | |
b633f66f | 36 | #endif |
2d8d190c UM |
37 | #ifdef CONFIG_LED_STATUS3 |
38 | { "3", CONFIG_LED_STATUS_BIT3, NULL, NULL, NULL }, | |
b633f66f | 39 | #endif |
2d8d190c UM |
40 | #ifdef CONFIG_LED_STATUS4 |
41 | { "4", CONFIG_LED_STATUS_BIT4, NULL, NULL, NULL }, | |
a8eeaf2f | 42 | #endif |
2d8d190c UM |
43 | #ifdef CONFIG_LED_STATUS5 |
44 | { "5", CONFIG_LED_STATUS_BIT5, NULL, NULL, NULL }, | |
a8eeaf2f | 45 | #endif |
b633f66f | 46 | #endif |
2d8d190c UM |
47 | #ifdef CONFIG_LED_STATUS_GREEN |
48 | { "green", CONFIG_LED_STATUS_GREEN, green_led_off, green_led_on, NULL }, | |
b633f66f | 49 | #endif |
2d8d190c UM |
50 | #ifdef CONFIG_LED_STATUS_YELLOW |
51 | { "yellow", CONFIG_LED_STATUS_YELLOW, yellow_led_off, yellow_led_on, | |
52 | NULL }, | |
b633f66f | 53 | #endif |
2d8d190c UM |
54 | #ifdef CONFIG_LED_STATUS_RED |
55 | { "red", CONFIG_LED_STATUS_RED, red_led_off, red_led_on, NULL }, | |
b633f66f | 56 | #endif |
2d8d190c UM |
57 | #ifdef CONFIG_LED_STATUS_BLUE |
58 | { "blue", CONFIG_LED_STATUS_BLUE, blue_led_off, blue_led_on, NULL }, | |
b633f66f | 59 | #endif |
b8bc8973 | 60 | { NULL, 0, NULL, NULL, NULL } |
b633f66f JK |
61 | }; |
62 | ||
a8eeaf2f | 63 | enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE, LED_BLINK }; |
b8bc8973 JF |
64 | |
65 | enum led_cmd get_led_cmd(char *var) | |
b633f66f | 66 | { |
a8eeaf2f | 67 | if (strcmp(var, "off") == 0) |
b8bc8973 | 68 | return LED_OFF; |
a8eeaf2f | 69 | if (strcmp(var, "on") == 0) |
b8bc8973 | 70 | return LED_ON; |
b8bc8973 JF |
71 | if (strcmp(var, "toggle") == 0) |
72 | return LED_TOGGLE; | |
a8eeaf2f SR |
73 | if (strcmp(var, "blink") == 0) |
74 | return LED_BLINK; | |
75 | ||
b633f66f JK |
76 | return -1; |
77 | } | |
78 | ||
a8eeaf2f SR |
79 | /* |
80 | * LED drivers providing a blinking LED functionality, like the | |
81 | * PCA9551, can override this empty weak function | |
82 | */ | |
83 | void __weak __led_blink(led_id_t mask, int freq) | |
84 | { | |
85 | } | |
86 | ||
09140113 | 87 | int do_legacy_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
b633f66f | 88 | { |
b8bc8973 JF |
89 | int i, match = 0; |
90 | enum led_cmd cmd; | |
a8eeaf2f | 91 | int freq; |
b633f66f JK |
92 | |
93 | /* Validate arguments */ | |
a8eeaf2f | 94 | if ((argc < 3) || (argc > 4)) |
4c12eeb8 | 95 | return CMD_RET_USAGE; |
b633f66f | 96 | |
b8bc8973 JF |
97 | cmd = get_led_cmd(argv[2]); |
98 | if (cmd < 0) { | |
4c12eeb8 | 99 | return CMD_RET_USAGE; |
b633f66f JK |
100 | } |
101 | ||
102 | for (i = 0; led_commands[i].string; i++) { | |
cd6881b5 | 103 | if ((strcmp("all", argv[1]) == 0) || |
b633f66f | 104 | (strcmp(led_commands[i].string, argv[1]) == 0)) { |
b8bc8973 JF |
105 | match = 1; |
106 | switch (cmd) { | |
107 | case LED_ON: | |
108 | if (led_commands[i].on) | |
b633f66f | 109 | led_commands[i].on(); |
b8bc8973 | 110 | else |
af73034c | 111 | __led_set(led_commands[i].mask, |
2d8d190c | 112 | CONFIG_LED_STATUS_ON); |
b8bc8973 JF |
113 | break; |
114 | case LED_OFF: | |
115 | if (led_commands[i].off) | |
b633f66f | 116 | led_commands[i].off(); |
b8bc8973 | 117 | else |
af73034c | 118 | __led_set(led_commands[i].mask, |
2d8d190c | 119 | CONFIG_LED_STATUS_OFF); |
b8bc8973 JF |
120 | break; |
121 | case LED_TOGGLE: | |
122 | if (led_commands[i].toggle) | |
123 | led_commands[i].toggle(); | |
124 | else | |
125 | __led_toggle(led_commands[i].mask); | |
a8eeaf2f SR |
126 | break; |
127 | case LED_BLINK: | |
128 | if (argc != 4) | |
129 | return CMD_RET_USAGE; | |
130 | ||
0b1284eb | 131 | freq = dectoul(argv[3], NULL); |
a8eeaf2f | 132 | __led_blink(led_commands[i].mask, freq); |
b633f66f | 133 | } |
d604cda3 JF |
134 | /* Need to set only 1 led if led_name wasn't 'all' */ |
135 | if (strcmp("all", argv[1]) != 0) | |
136 | break; | |
b633f66f JK |
137 | } |
138 | } | |
139 | ||
140 | /* If we ran out of matches, print Usage */ | |
95492d78 | 141 | if (!match) { |
4c12eeb8 | 142 | return CMD_RET_USAGE; |
b633f66f JK |
143 | } |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | U_BOOT_CMD( | |
21c34084 | 149 | led, 4, 1, do_legacy_led, |
d4b901dd | 150 | "[" |
2d8d190c UM |
151 | #ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC |
152 | #ifdef CONFIG_LED_STATUS0 | |
b633f66f JK |
153 | "0|" |
154 | #endif | |
2d8d190c | 155 | #ifdef CONFIG_LED_STATUS1 |
b633f66f JK |
156 | "1|" |
157 | #endif | |
2d8d190c | 158 | #ifdef CONFIG_LED_STATUS2 |
b633f66f JK |
159 | "2|" |
160 | #endif | |
2d8d190c | 161 | #ifdef CONFIG_LED_STATUS3 |
b633f66f JK |
162 | "3|" |
163 | #endif | |
2d8d190c | 164 | #ifdef CONFIG_LED_STATUS4 |
a8eeaf2f SR |
165 | "4|" |
166 | #endif | |
2d8d190c | 167 | #ifdef CONFIG_LED_STATUS5 |
a8eeaf2f SR |
168 | "5|" |
169 | #endif | |
b633f66f | 170 | #endif |
2d8d190c | 171 | #ifdef CONFIG_LED_STATUS_GREEN |
b633f66f JK |
172 | "green|" |
173 | #endif | |
2d8d190c | 174 | #ifdef CONFIG_LED_STATUS_YELLOW |
b633f66f JK |
175 | "yellow|" |
176 | #endif | |
2d8d190c | 177 | #ifdef CONFIG_LED_STATUS_RED |
b633f66f JK |
178 | "red|" |
179 | #endif | |
2d8d190c | 180 | #ifdef CONFIG_LED_STATUS_BLUE |
b633f66f JK |
181 | "blue|" |
182 | #endif | |
a8eeaf2f SR |
183 | "all] [on|off|toggle|blink] [blink-freq in ms]", |
184 | "[led_name] [on|off|toggle|blink] sets or clears led(s)" | |
b633f66f | 185 | ); |