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