]> Git Repo - J-u-boot.git/blob - cmd/legacy_led.c
Merge patch series "Apply SoM overlays on phyCORE-AM6xx SoMs"
[J-u-boot.git] / cmd / legacy_led.c
1 // SPDX-License-Identifier: GPL-2.0+
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]>
10  */
11
12 #include <command.h>
13 #include <status_led.h>
14 #include <vsprintf.h>
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() */
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 */
22 };
23
24 typedef struct led_tbl_s led_tbl_t;
25
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 },
30 #endif
31 #ifdef CONFIG_LED_STATUS1
32         { "1", CONFIG_LED_STATUS_BIT1, NULL, NULL, NULL },
33 #endif
34 #ifdef CONFIG_LED_STATUS2
35         { "2", CONFIG_LED_STATUS_BIT2, NULL, NULL, NULL },
36 #endif
37 #ifdef CONFIG_LED_STATUS3
38         { "3", CONFIG_LED_STATUS_BIT3, NULL, NULL, NULL },
39 #endif
40 #ifdef CONFIG_LED_STATUS4
41         { "4", CONFIG_LED_STATUS_BIT4, NULL, NULL, NULL },
42 #endif
43 #ifdef CONFIG_LED_STATUS5
44         { "5", CONFIG_LED_STATUS_BIT5, NULL, NULL, NULL },
45 #endif
46 #endif
47 #ifdef CONFIG_LED_STATUS_GREEN
48         { "green", CONFIG_LED_STATUS_GREEN, green_led_off, green_led_on, NULL },
49 #endif
50 #ifdef CONFIG_LED_STATUS_YELLOW
51         { "yellow", CONFIG_LED_STATUS_YELLOW, yellow_led_off, yellow_led_on,
52           NULL },
53 #endif
54 #ifdef CONFIG_LED_STATUS_RED
55         { "red", CONFIG_LED_STATUS_RED, red_led_off, red_led_on, NULL },
56 #endif
57 #ifdef CONFIG_LED_STATUS_BLUE
58         { "blue", CONFIG_LED_STATUS_BLUE, blue_led_off, blue_led_on, NULL },
59 #endif
60         { NULL, 0, NULL, NULL, NULL }
61 };
62
63 enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE, LED_BLINK };
64
65 enum led_cmd get_led_cmd(char *var)
66 {
67         if (strcmp(var, "off") == 0)
68                 return LED_OFF;
69         if (strcmp(var, "on") == 0)
70                 return LED_ON;
71         if (strcmp(var, "toggle") == 0)
72                 return LED_TOGGLE;
73         if (strcmp(var, "blink") == 0)
74                 return LED_BLINK;
75
76         return -1;
77 }
78
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
87 int do_legacy_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
88 {
89         int i, match = 0;
90         enum led_cmd cmd;
91         int freq;
92
93         /* Validate arguments */
94         if ((argc < 3) || (argc > 4))
95                 return CMD_RET_USAGE;
96
97         cmd = get_led_cmd(argv[2]);
98         if (cmd < 0) {
99                 return CMD_RET_USAGE;
100         }
101
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)) {
105                         match = 1;
106                         switch (cmd) {
107                         case LED_ON:
108                                 if (led_commands[i].on)
109                                         led_commands[i].on();
110                                 else
111                                         __led_set(led_commands[i].mask,
112                                                           CONFIG_LED_STATUS_ON);
113                                 break;
114                         case LED_OFF:
115                                 if (led_commands[i].off)
116                                         led_commands[i].off();
117                                 else
118                                         __led_set(led_commands[i].mask,
119                                                   CONFIG_LED_STATUS_OFF);
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);
126                                 break;
127                         case LED_BLINK:
128                                 if (argc != 4)
129                                         return CMD_RET_USAGE;
130
131                                 freq = dectoul(argv[3], NULL);
132                                 __led_blink(led_commands[i].mask, freq);
133                         }
134                         /* Need to set only 1 led if led_name wasn't 'all' */
135                         if (strcmp("all", argv[1]) != 0)
136                                 break;
137                 }
138         }
139
140         /* If we ran out of matches, print Usage */
141         if (!match) {
142                 return CMD_RET_USAGE;
143         }
144
145         return 0;
146 }
147
148 U_BOOT_CMD(
149         led, 4, 1, do_legacy_led,
150         "["
151 #ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
152 #ifdef CONFIG_LED_STATUS0
153         "0|"
154 #endif
155 #ifdef CONFIG_LED_STATUS1
156         "1|"
157 #endif
158 #ifdef CONFIG_LED_STATUS2
159         "2|"
160 #endif
161 #ifdef CONFIG_LED_STATUS3
162         "3|"
163 #endif
164 #ifdef CONFIG_LED_STATUS4
165         "4|"
166 #endif
167 #ifdef CONFIG_LED_STATUS5
168         "5|"
169 #endif
170 #endif
171 #ifdef CONFIG_LED_STATUS_GREEN
172         "green|"
173 #endif
174 #ifdef CONFIG_LED_STATUS_YELLOW
175         "yellow|"
176 #endif
177 #ifdef CONFIG_LED_STATUS_RED
178         "red|"
179 #endif
180 #ifdef CONFIG_LED_STATUS_BLUE
181         "blue|"
182 #endif
183         "all] [on|off|toggle|blink] [blink-freq in ms]",
184         "[led_name] [on|off|toggle|blink] sets or clears led(s)"
185 );
This page took 0.034866 seconds and 4 git commands to generate.