Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 34 | struct led_tbl_s { |
| 35 | char *string; /* String for use in the command */ |
| 36 | led_id_t mask; /* Mask used for calling __led_set() */ |
Jason Kridner | 7514a7c | 2011-04-20 18:13:49 -0500 | [diff] [blame] | 37 | void (*off)(void); /* Optional function for turning LED off */ |
| 38 | void (*on)(void); /* Optional function for turning LED on */ |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 39 | void (*toggle)(void);/* Optional function for toggling LED */ |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | typedef struct led_tbl_s led_tbl_t; |
| 43 | |
| 44 | static const led_tbl_t led_commands[] = { |
| 45 | #ifdef CONFIG_BOARD_SPECIFIC_LED |
| 46 | #ifdef STATUS_LED_BIT |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 47 | { "0", STATUS_LED_BIT, NULL, NULL, NULL }, |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 48 | #endif |
| 49 | #ifdef STATUS_LED_BIT1 |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 50 | { "1", STATUS_LED_BIT1, NULL, NULL, NULL }, |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 51 | #endif |
| 52 | #ifdef STATUS_LED_BIT2 |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 53 | { "2", STATUS_LED_BIT2, NULL, NULL, NULL }, |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 54 | #endif |
| 55 | #ifdef STATUS_LED_BIT3 |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 56 | { "3", STATUS_LED_BIT3, NULL, NULL, NULL }, |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 57 | #endif |
| 58 | #endif |
| 59 | #ifdef STATUS_LED_GREEN |
Jason Kridner | aff0aa8 | 2011-09-04 14:40:16 -0400 | [diff] [blame] | 60 | { "green", STATUS_LED_GREEN, green_led_off, green_led_on, NULL }, |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 61 | #endif |
| 62 | #ifdef STATUS_LED_YELLOW |
Jason Kridner | aff0aa8 | 2011-09-04 14:40:16 -0400 | [diff] [blame] | 63 | { "yellow", STATUS_LED_YELLOW, yellow_led_off, yellow_led_on, NULL }, |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 64 | #endif |
| 65 | #ifdef STATUS_LED_RED |
Jason Kridner | aff0aa8 | 2011-09-04 14:40:16 -0400 | [diff] [blame] | 66 | { "red", STATUS_LED_RED, red_led_off, red_led_on, NULL }, |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 67 | #endif |
| 68 | #ifdef STATUS_LED_BLUE |
Jason Kridner | aff0aa8 | 2011-09-04 14:40:16 -0400 | [diff] [blame] | 69 | { "blue", STATUS_LED_BLUE, blue_led_off, blue_led_on, NULL }, |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 70 | #endif |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 71 | { NULL, 0, NULL, NULL, NULL } |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 72 | }; |
| 73 | |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 74 | enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE }; |
| 75 | |
| 76 | enum led_cmd get_led_cmd(char *var) |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 77 | { |
| 78 | if (strcmp(var, "off") == 0) { |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 79 | return LED_OFF; |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 80 | } |
| 81 | if (strcmp(var, "on") == 0) { |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 82 | return LED_ON; |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 83 | } |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 84 | if (strcmp(var, "toggle") == 0) |
| 85 | return LED_TOGGLE; |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 90 | { |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 91 | int i, match = 0; |
| 92 | enum led_cmd cmd; |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 93 | |
| 94 | /* Validate arguments */ |
| 95 | if ((argc != 3)) { |
| 96 | return cmd_usage(cmdtp); |
| 97 | } |
| 98 | |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 99 | cmd = get_led_cmd(argv[2]); |
| 100 | if (cmd < 0) { |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 101 | return cmd_usage(cmdtp); |
| 102 | } |
| 103 | |
| 104 | for (i = 0; led_commands[i].string; i++) { |
Wolfgang Denk | 80f7021 | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 105 | if ((strcmp("all", argv[1]) == 0) || |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 106 | (strcmp(led_commands[i].string, argv[1]) == 0)) { |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 107 | match = 1; |
| 108 | switch (cmd) { |
| 109 | case LED_ON: |
| 110 | if (led_commands[i].on) |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 111 | led_commands[i].on(); |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 112 | else |
| 113 | __led_set(led_commands[i].mask, 1); |
| 114 | break; |
| 115 | case LED_OFF: |
| 116 | if (led_commands[i].off) |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 117 | led_commands[i].off(); |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 118 | else |
| 119 | __led_set(led_commands[i].mask, 0); |
| 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); |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 126 | } |
Joel A Fernandes | b64bb20 | 2011-08-11 19:10:19 -0500 | [diff] [blame] | 127 | /* Need to set only 1 led if led_name wasn't 'all' */ |
| 128 | if (strcmp("all", argv[1]) != 0) |
| 129 | break; |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | /* If we ran out of matches, print Usage */ |
Jason Kridner | 1f44056 | 2011-03-08 20:42:01 -0600 | [diff] [blame] | 134 | if (!match) { |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 135 | return cmd_usage(cmdtp); |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | U_BOOT_CMD( |
| 142 | led, 3, 1, do_led, |
| 143 | "led\t- [" |
| 144 | #ifdef CONFIG_BOARD_SPECIFIC_LED |
| 145 | #ifdef STATUS_LED_BIT |
| 146 | "0|" |
| 147 | #endif |
| 148 | #ifdef STATUS_LED_BIT1 |
| 149 | "1|" |
| 150 | #endif |
| 151 | #ifdef STATUS_LED_BIT2 |
| 152 | "2|" |
| 153 | #endif |
| 154 | #ifdef STATUS_LED_BIT3 |
| 155 | "3|" |
| 156 | #endif |
| 157 | #endif |
| 158 | #ifdef STATUS_LED_GREEN |
| 159 | "green|" |
| 160 | #endif |
| 161 | #ifdef STATUS_LED_YELLOW |
| 162 | "yellow|" |
| 163 | #endif |
| 164 | #ifdef STATUS_LED_RED |
| 165 | "red|" |
| 166 | #endif |
| 167 | #ifdef STATUS_LED_BLUE |
| 168 | "blue|" |
| 169 | #endif |
Joel A Fernandes | 26bd918 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 170 | "all] [on|off|toggle]\n", |
| 171 | "led [led_name] [on|off|toggle] sets or clears led(s)\n" |
Jason Kridner | 931ced0 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 172 | ); |