Jason Kridner | b633f66 | 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 | * |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <config.h> |
| 15 | #include <command.h> |
| 16 | #include <status_led.h> |
| 17 | |
| 18 | struct led_tbl_s { |
| 19 | char *string; /* String for use in the command */ |
| 20 | led_id_t mask; /* Mask used for calling __led_set() */ |
Jason Kridner | 4086b51 | 2011-04-20 18:13:49 -0500 | [diff] [blame] | 21 | void (*off)(void); /* Optional function for turning LED off */ |
| 22 | void (*on)(void); /* Optional function for turning LED on */ |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 23 | void (*toggle)(void);/* Optional function for toggling LED */ |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | typedef struct led_tbl_s led_tbl_t; |
| 27 | |
| 28 | static const led_tbl_t led_commands[] = { |
| 29 | #ifdef CONFIG_BOARD_SPECIFIC_LED |
| 30 | #ifdef STATUS_LED_BIT |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 31 | { "0", STATUS_LED_BIT, NULL, NULL, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 32 | #endif |
| 33 | #ifdef STATUS_LED_BIT1 |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 34 | { "1", STATUS_LED_BIT1, NULL, NULL, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 35 | #endif |
| 36 | #ifdef STATUS_LED_BIT2 |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 37 | { "2", STATUS_LED_BIT2, NULL, NULL, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 38 | #endif |
| 39 | #ifdef STATUS_LED_BIT3 |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 40 | { "3", STATUS_LED_BIT3, NULL, NULL, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 41 | #endif |
| 42 | #endif |
| 43 | #ifdef STATUS_LED_GREEN |
Jason Kridner | 2d3be7c | 2011-09-04 14:40:16 -0400 | [diff] [blame] | 44 | { "green", STATUS_LED_GREEN, green_led_off, green_led_on, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 45 | #endif |
| 46 | #ifdef STATUS_LED_YELLOW |
Jason Kridner | 2d3be7c | 2011-09-04 14:40:16 -0400 | [diff] [blame] | 47 | { "yellow", STATUS_LED_YELLOW, yellow_led_off, yellow_led_on, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 48 | #endif |
| 49 | #ifdef STATUS_LED_RED |
Jason Kridner | 2d3be7c | 2011-09-04 14:40:16 -0400 | [diff] [blame] | 50 | { "red", STATUS_LED_RED, red_led_off, red_led_on, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 51 | #endif |
| 52 | #ifdef STATUS_LED_BLUE |
Jason Kridner | 2d3be7c | 2011-09-04 14:40:16 -0400 | [diff] [blame] | 53 | { "blue", STATUS_LED_BLUE, blue_led_off, blue_led_on, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 54 | #endif |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 55 | { NULL, 0, NULL, NULL, NULL } |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 56 | }; |
| 57 | |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 58 | enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE }; |
| 59 | |
| 60 | enum led_cmd get_led_cmd(char *var) |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 61 | { |
| 62 | if (strcmp(var, "off") == 0) { |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 63 | return LED_OFF; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 64 | } |
| 65 | if (strcmp(var, "on") == 0) { |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 66 | return LED_ON; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 67 | } |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 68 | if (strcmp(var, "toggle") == 0) |
| 69 | return LED_TOGGLE; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 74 | { |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 75 | int i, match = 0; |
| 76 | enum led_cmd cmd; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 77 | |
| 78 | /* Validate arguments */ |
| 79 | if ((argc != 3)) { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 80 | return CMD_RET_USAGE; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 83 | cmd = get_led_cmd(argv[2]); |
| 84 | if (cmd < 0) { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 85 | return CMD_RET_USAGE; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | for (i = 0; led_commands[i].string; i++) { |
Wolfgang Denk | cd6881b | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 89 | if ((strcmp("all", argv[1]) == 0) || |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 90 | (strcmp(led_commands[i].string, argv[1]) == 0)) { |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 91 | match = 1; |
| 92 | switch (cmd) { |
| 93 | case LED_ON: |
| 94 | if (led_commands[i].on) |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 95 | led_commands[i].on(); |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 96 | else |
Otavio Salvador | af73034 | 2013-02-23 02:43:07 +0000 | [diff] [blame] | 97 | __led_set(led_commands[i].mask, |
| 98 | STATUS_LED_ON); |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 99 | break; |
| 100 | case LED_OFF: |
| 101 | if (led_commands[i].off) |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 102 | led_commands[i].off(); |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 103 | else |
Otavio Salvador | af73034 | 2013-02-23 02:43:07 +0000 | [diff] [blame] | 104 | __led_set(led_commands[i].mask, |
| 105 | STATUS_LED_OFF); |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 106 | break; |
| 107 | case LED_TOGGLE: |
| 108 | if (led_commands[i].toggle) |
| 109 | led_commands[i].toggle(); |
| 110 | else |
| 111 | __led_toggle(led_commands[i].mask); |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 112 | } |
Joel A Fernandes | d604cda | 2011-08-11 19:10:19 -0500 | [diff] [blame] | 113 | /* Need to set only 1 led if led_name wasn't 'all' */ |
| 114 | if (strcmp("all", argv[1]) != 0) |
| 115 | break; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | /* If we ran out of matches, print Usage */ |
Jason Kridner | 95492d7 | 2011-03-08 20:42:01 -0600 | [diff] [blame] | 120 | if (!match) { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 121 | return CMD_RET_USAGE; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | U_BOOT_CMD( |
| 128 | led, 3, 1, do_led, |
Robert P. J. Day | d4b901d | 2012-11-11 10:39:07 +0000 | [diff] [blame] | 129 | "[" |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 130 | #ifdef CONFIG_BOARD_SPECIFIC_LED |
| 131 | #ifdef STATUS_LED_BIT |
| 132 | "0|" |
| 133 | #endif |
| 134 | #ifdef STATUS_LED_BIT1 |
| 135 | "1|" |
| 136 | #endif |
| 137 | #ifdef STATUS_LED_BIT2 |
| 138 | "2|" |
| 139 | #endif |
| 140 | #ifdef STATUS_LED_BIT3 |
| 141 | "3|" |
| 142 | #endif |
| 143 | #endif |
| 144 | #ifdef STATUS_LED_GREEN |
| 145 | "green|" |
| 146 | #endif |
| 147 | #ifdef STATUS_LED_YELLOW |
| 148 | "yellow|" |
| 149 | #endif |
| 150 | #ifdef STATUS_LED_RED |
| 151 | "red|" |
| 152 | #endif |
| 153 | #ifdef STATUS_LED_BLUE |
| 154 | "blue|" |
| 155 | #endif |
Robert P. J. Day | d4b901d | 2012-11-11 10:39:07 +0000 | [diff] [blame] | 156 | "all] [on|off|toggle]", |
| 157 | "[led_name] [on|off|toggle] sets or clears led(s)" |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 158 | ); |