blob: 84f79fae00698e2e70199090919b1981bafd6a1f [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 */
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050039 void (*toggle)(void);/* Optional function for toggling LED */
Jason Kridnerb633f662011-04-18 17:22:44 -040040};
41
42typedef struct led_tbl_s led_tbl_t;
43
44static const led_tbl_t led_commands[] = {
45#ifdef CONFIG_BOARD_SPECIFIC_LED
46#ifdef STATUS_LED_BIT
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050047 { "0", STATUS_LED_BIT, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040048#endif
49#ifdef STATUS_LED_BIT1
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050050 { "1", STATUS_LED_BIT1, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040051#endif
52#ifdef STATUS_LED_BIT2
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050053 { "2", STATUS_LED_BIT2, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040054#endif
55#ifdef STATUS_LED_BIT3
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050056 { "3", STATUS_LED_BIT3, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040057#endif
58#endif
59#ifdef STATUS_LED_GREEN
Jason Kridner2d3be7c2011-09-04 14:40:16 -040060 { "green", STATUS_LED_GREEN, green_led_off, green_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040061#endif
62#ifdef STATUS_LED_YELLOW
Jason Kridner2d3be7c2011-09-04 14:40:16 -040063 { "yellow", STATUS_LED_YELLOW, yellow_led_off, yellow_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040064#endif
65#ifdef STATUS_LED_RED
Jason Kridner2d3be7c2011-09-04 14:40:16 -040066 { "red", STATUS_LED_RED, red_led_off, red_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040067#endif
68#ifdef STATUS_LED_BLUE
Jason Kridner2d3be7c2011-09-04 14:40:16 -040069 { "blue", STATUS_LED_BLUE, blue_led_off, blue_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040070#endif
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050071 { NULL, 0, NULL, NULL, NULL }
Jason Kridnerb633f662011-04-18 17:22:44 -040072};
73
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050074enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE };
75
76enum led_cmd get_led_cmd(char *var)
Jason Kridnerb633f662011-04-18 17:22:44 -040077{
78 if (strcmp(var, "off") == 0) {
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050079 return LED_OFF;
Jason Kridnerb633f662011-04-18 17:22:44 -040080 }
81 if (strcmp(var, "on") == 0) {
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050082 return LED_ON;
Jason Kridnerb633f662011-04-18 17:22:44 -040083 }
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050084 if (strcmp(var, "toggle") == 0)
85 return LED_TOGGLE;
Jason Kridnerb633f662011-04-18 17:22:44 -040086 return -1;
87}
88
89int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
90{
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050091 int i, match = 0;
92 enum led_cmd cmd;
Jason Kridnerb633f662011-04-18 17:22:44 -040093
94 /* Validate arguments */
95 if ((argc != 3)) {
Simon Glass4c12eeb2011-12-10 08:44:01 +000096 return CMD_RET_USAGE;
Jason Kridnerb633f662011-04-18 17:22:44 -040097 }
98
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050099 cmd = get_led_cmd(argv[2]);
100 if (cmd < 0) {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000101 return CMD_RET_USAGE;
Jason Kridnerb633f662011-04-18 17:22:44 -0400102 }
103
104 for (i = 0; led_commands[i].string; i++) {
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200105 if ((strcmp("all", argv[1]) == 0) ||
Jason Kridnerb633f662011-04-18 17:22:44 -0400106 (strcmp(led_commands[i].string, argv[1]) == 0)) {
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500107 match = 1;
108 switch (cmd) {
109 case LED_ON:
110 if (led_commands[i].on)
Jason Kridnerb633f662011-04-18 17:22:44 -0400111 led_commands[i].on();
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500112 else
Otavio Salvadoraf730342013-02-23 02:43:07 +0000113 __led_set(led_commands[i].mask,
114 STATUS_LED_ON);
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500115 break;
116 case LED_OFF:
117 if (led_commands[i].off)
Jason Kridnerb633f662011-04-18 17:22:44 -0400118 led_commands[i].off();
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500119 else
Otavio Salvadoraf730342013-02-23 02:43:07 +0000120 __led_set(led_commands[i].mask,
121 STATUS_LED_OFF);
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500122 break;
123 case LED_TOGGLE:
124 if (led_commands[i].toggle)
125 led_commands[i].toggle();
126 else
127 __led_toggle(led_commands[i].mask);
Jason Kridnerb633f662011-04-18 17:22:44 -0400128 }
Joel A Fernandesd604cda2011-08-11 19:10:19 -0500129 /* Need to set only 1 led if led_name wasn't 'all' */
130 if (strcmp("all", argv[1]) != 0)
131 break;
Jason Kridnerb633f662011-04-18 17:22:44 -0400132 }
133 }
134
135 /* If we ran out of matches, print Usage */
Jason Kridner95492d72011-03-08 20:42:01 -0600136 if (!match) {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000137 return CMD_RET_USAGE;
Jason Kridnerb633f662011-04-18 17:22:44 -0400138 }
139
140 return 0;
141}
142
143U_BOOT_CMD(
144 led, 3, 1, do_led,
Robert P. J. Dayd4b901d2012-11-11 10:39:07 +0000145 "["
Jason Kridnerb633f662011-04-18 17:22:44 -0400146#ifdef CONFIG_BOARD_SPECIFIC_LED
147#ifdef STATUS_LED_BIT
148 "0|"
149#endif
150#ifdef STATUS_LED_BIT1
151 "1|"
152#endif
153#ifdef STATUS_LED_BIT2
154 "2|"
155#endif
156#ifdef STATUS_LED_BIT3
157 "3|"
158#endif
159#endif
160#ifdef STATUS_LED_GREEN
161 "green|"
162#endif
163#ifdef STATUS_LED_YELLOW
164 "yellow|"
165#endif
166#ifdef STATUS_LED_RED
167 "red|"
168#endif
169#ifdef STATUS_LED_BLUE
170 "blue|"
171#endif
Robert P. J. Dayd4b901d2012-11-11 10:39:07 +0000172 "all] [on|off|toggle]",
173 "[led_name] [on|off|toggle] sets or clears led(s)"
Jason Kridnerb633f662011-04-18 17:22:44 -0400174);