blob: 183dc0b42f7663769f56e4e8995edad342cf9960 [file] [log] [blame]
Jason Kridnerb633f662011-04-18 17:22:44 -04001/*
2 * (C) Copyright 2010
3 * Jason Kridner <jkridner@beagleboard.org>
4 *
5 * Based on cmd_led.c patch from:
6 * http://www.mail-archive.com/u-boot@lists.denx.de/msg06873.html
7 * (C) Copyright 2008
8 * Ulf Samuelsson <ulf.samuelsson@atmel.com>
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29#include <common.h>
30#include <config.h>
31#include <command.h>
32#include <status_led.h>
33
34struct led_tbl_s {
35 char *string; /* String for use in the command */
36 led_id_t mask; /* Mask used for calling __led_set() */
Jason Kridner4086b512011-04-20 18:13:49 -050037 void (*off)(void); /* Optional function for turning LED off */
38 void (*on)(void); /* Optional function for turning LED on */
Jason Kridnerb633f662011-04-18 17:22:44 -040039};
40
41typedef struct led_tbl_s led_tbl_t;
42
43static const led_tbl_t led_commands[] = {
44#ifdef CONFIG_BOARD_SPECIFIC_LED
45#ifdef STATUS_LED_BIT
46 { "0", STATUS_LED_BIT, NULL, NULL },
47#endif
48#ifdef STATUS_LED_BIT1
49 { "1", STATUS_LED_BIT1, NULL, NULL },
50#endif
51#ifdef STATUS_LED_BIT2
52 { "2", STATUS_LED_BIT2, NULL, NULL },
53#endif
54#ifdef STATUS_LED_BIT3
55 { "3", STATUS_LED_BIT3, NULL, NULL },
56#endif
57#endif
58#ifdef STATUS_LED_GREEN
59 { "green", STATUS_LED_GREEN, green_LED_off, green_LED_on },
60#endif
61#ifdef STATUS_LED_YELLOW
62 { "yellow", STATUS_LED_YELLOW, yellow_LED_off, yellow_LED_on },
63#endif
64#ifdef STATUS_LED_RED
65 { "red", STATUS_LED_RED, red_LED_off, red_LED_on },
66#endif
67#ifdef STATUS_LED_BLUE
68 { "blue", STATUS_LED_BLUE, blue_LED_off, blue_LED_on },
69#endif
70 { NULL, 0, NULL, NULL }
71};
72
73int str_onoff (char *var)
74{
75 if (strcmp(var, "off") == 0) {
76 return 0;
77 }
78 if (strcmp(var, "on") == 0) {
79 return 1;
80 }
81 return -1;
82}
83
84int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
85{
Jason Kridner95492d72011-03-08 20:42:01 -060086 int state, i, match = 0;
Jason Kridnerb633f662011-04-18 17:22:44 -040087
88 /* Validate arguments */
89 if ((argc != 3)) {
90 return cmd_usage(cmdtp);
91 }
92
93 state = str_onoff(argv[2]);
94 if (state < 0) {
95 return cmd_usage(cmdtp);
96 }
97
98 for (i = 0; led_commands[i].string; i++) {
Wolfgang Denkcd6881b2011-05-19 22:21:41 +020099 if ((strcmp("all", argv[1]) == 0) ||
Jason Kridnerb633f662011-04-18 17:22:44 -0400100 (strcmp(led_commands[i].string, argv[1]) == 0)) {
Jason Kridner95492d72011-03-08 20:42:01 -0600101 match = 1;
Jason Kridnerb633f662011-04-18 17:22:44 -0400102 if (led_commands[i].on) {
103 if (state) {
104 led_commands[i].on();
105 } else {
106 led_commands[i].off();
107 }
108 } else {
109 __led_set(led_commands[i].mask, state);
110 }
Joel A Fernandesd604cda2011-08-11 19:10:19 -0500111 /* Need to set only 1 led if led_name wasn't 'all' */
112 if (strcmp("all", argv[1]) != 0)
113 break;
Jason Kridnerb633f662011-04-18 17:22:44 -0400114 }
115 }
116
117 /* If we ran out of matches, print Usage */
Jason Kridner95492d72011-03-08 20:42:01 -0600118 if (!match) {
Jason Kridnerb633f662011-04-18 17:22:44 -0400119 return cmd_usage(cmdtp);
120 }
121
122 return 0;
123}
124
125U_BOOT_CMD(
126 led, 3, 1, do_led,
127 "led\t- ["
128#ifdef CONFIG_BOARD_SPECIFIC_LED
129#ifdef STATUS_LED_BIT
130 "0|"
131#endif
132#ifdef STATUS_LED_BIT1
133 "1|"
134#endif
135#ifdef STATUS_LED_BIT2
136 "2|"
137#endif
138#ifdef STATUS_LED_BIT3
139 "3|"
140#endif
141#endif
142#ifdef STATUS_LED_GREEN
143 "green|"
144#endif
145#ifdef STATUS_LED_YELLOW
146 "yellow|"
147#endif
148#ifdef STATUS_LED_RED
149 "red|"
150#endif
151#ifdef STATUS_LED_BLUE
152 "blue|"
153#endif
154 "all] [on|off]\n",
155 "led [led_name] [on|off] sets or clears led(s)\n"
156);