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[] = { |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 29 | #ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC |
| 30 | #ifdef CONFIG_LED_STATUS0 |
| 31 | { "0", CONFIG_LED_STATUS_BIT, NULL, NULL, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 32 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 33 | #ifdef CONFIG_LED_STATUS1 |
| 34 | { "1", CONFIG_LED_STATUS_BIT1, NULL, NULL, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 35 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 36 | #ifdef CONFIG_LED_STATUS2 |
| 37 | { "2", CONFIG_LED_STATUS_BIT2, NULL, NULL, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 38 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 39 | #ifdef CONFIG_LED_STATUS3 |
| 40 | { "3", CONFIG_LED_STATUS_BIT3, NULL, NULL, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 41 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 42 | #ifdef CONFIG_LED_STATUS4 |
| 43 | { "4", CONFIG_LED_STATUS_BIT4, NULL, NULL, NULL }, |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 44 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_LED_STATUS5 |
| 46 | { "5", CONFIG_LED_STATUS_BIT5, NULL, NULL, NULL }, |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 47 | #endif |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 48 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 49 | #ifdef CONFIG_LED_STATUS_GREEN |
| 50 | { "green", CONFIG_LED_STATUS_GREEN, green_led_off, green_led_on, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 51 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 52 | #ifdef CONFIG_LED_STATUS_YELLOW |
| 53 | { "yellow", CONFIG_LED_STATUS_YELLOW, yellow_led_off, yellow_led_on, |
| 54 | NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 55 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 56 | #ifdef CONFIG_LED_STATUS_RED |
| 57 | { "red", CONFIG_LED_STATUS_RED, red_led_off, red_led_on, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 58 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 59 | #ifdef CONFIG_LED_STATUS_BLUE |
| 60 | { "blue", CONFIG_LED_STATUS_BLUE, blue_led_off, blue_led_on, NULL }, |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 61 | #endif |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 62 | { NULL, 0, NULL, NULL, NULL } |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 65 | enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE, LED_BLINK }; |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 66 | |
| 67 | enum led_cmd get_led_cmd(char *var) |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 68 | { |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 69 | if (strcmp(var, "off") == 0) |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 70 | return LED_OFF; |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 71 | if (strcmp(var, "on") == 0) |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 72 | return LED_ON; |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 73 | if (strcmp(var, "toggle") == 0) |
| 74 | return LED_TOGGLE; |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 75 | if (strcmp(var, "blink") == 0) |
| 76 | return LED_BLINK; |
| 77 | |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 81 | /* |
| 82 | * LED drivers providing a blinking LED functionality, like the |
| 83 | * PCA9551, can override this empty weak function |
| 84 | */ |
| 85 | void __weak __led_blink(led_id_t mask, int freq) |
| 86 | { |
| 87 | } |
| 88 | |
Simon Glass | 21c3408 | 2017-04-10 11:34:58 -0600 | [diff] [blame] | 89 | int do_legacy_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 90 | { |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 91 | int i, match = 0; |
| 92 | enum led_cmd cmd; |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 93 | int freq; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 94 | |
| 95 | /* Validate arguments */ |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 96 | if ((argc < 3) || (argc > 4)) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 97 | return CMD_RET_USAGE; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 98 | |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 99 | cmd = get_led_cmd(argv[2]); |
| 100 | if (cmd < 0) { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 101 | return CMD_RET_USAGE; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | for (i = 0; led_commands[i].string; i++) { |
Wolfgang Denk | cd6881b | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 105 | if ((strcmp("all", argv[1]) == 0) || |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 106 | (strcmp(led_commands[i].string, argv[1]) == 0)) { |
Joel A Fernandes | b8bc897 | 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 | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 111 | led_commands[i].on(); |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 112 | else |
Otavio Salvador | af73034 | 2013-02-23 02:43:07 +0000 | [diff] [blame] | 113 | __led_set(led_commands[i].mask, |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 114 | CONFIG_LED_STATUS_ON); |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 115 | break; |
| 116 | case LED_OFF: |
| 117 | if (led_commands[i].off) |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 118 | led_commands[i].off(); |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 119 | else |
Otavio Salvador | af73034 | 2013-02-23 02:43:07 +0000 | [diff] [blame] | 120 | __led_set(led_commands[i].mask, |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 121 | CONFIG_LED_STATUS_OFF); |
Joel A Fernandes | b8bc897 | 2011-08-11 23:16:53 -0500 | [diff] [blame] | 122 | 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); |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 128 | break; |
| 129 | case LED_BLINK: |
| 130 | if (argc != 4) |
| 131 | return CMD_RET_USAGE; |
| 132 | |
| 133 | freq = simple_strtoul(argv[3], NULL, 10); |
| 134 | __led_blink(led_commands[i].mask, freq); |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 135 | } |
Joel A Fernandes | d604cda | 2011-08-11 19:10:19 -0500 | [diff] [blame] | 136 | /* Need to set only 1 led if led_name wasn't 'all' */ |
| 137 | if (strcmp("all", argv[1]) != 0) |
| 138 | break; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /* If we ran out of matches, print Usage */ |
Jason Kridner | 95492d7 | 2011-03-08 20:42:01 -0600 | [diff] [blame] | 143 | if (!match) { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 144 | return CMD_RET_USAGE; |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | U_BOOT_CMD( |
Simon Glass | 21c3408 | 2017-04-10 11:34:58 -0600 | [diff] [blame] | 151 | led, 4, 1, do_legacy_led, |
Robert P. J. Day | d4b901d | 2012-11-11 10:39:07 +0000 | [diff] [blame] | 152 | "[" |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 153 | #ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC |
| 154 | #ifdef CONFIG_LED_STATUS0 |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 155 | "0|" |
| 156 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 157 | #ifdef CONFIG_LED_STATUS1 |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 158 | "1|" |
| 159 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 160 | #ifdef CONFIG_LED_STATUS2 |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 161 | "2|" |
| 162 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 163 | #ifdef CONFIG_LED_STATUS3 |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 164 | "3|" |
| 165 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 166 | #ifdef CONFIG_LED_STATUS4 |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 167 | "4|" |
| 168 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 169 | #ifdef CONFIG_LED_STATUS5 |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 170 | "5|" |
| 171 | #endif |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 172 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 173 | #ifdef CONFIG_LED_STATUS_GREEN |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 174 | "green|" |
| 175 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 176 | #ifdef CONFIG_LED_STATUS_YELLOW |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 177 | "yellow|" |
| 178 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 179 | #ifdef CONFIG_LED_STATUS_RED |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 180 | "red|" |
| 181 | #endif |
Uri Mashiach | 2d8d190 | 2017-01-19 10:51:45 +0200 | [diff] [blame] | 182 | #ifdef CONFIG_LED_STATUS_BLUE |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 183 | "blue|" |
| 184 | #endif |
Stefan Roese | a8eeaf2 | 2015-03-11 09:51:39 +0100 | [diff] [blame] | 185 | "all] [on|off|toggle|blink] [blink-freq in ms]", |
| 186 | "[led_name] [on|off|toggle|blink] sets or clears led(s)" |
Jason Kridner | b633f66 | 2011-04-18 17:22:44 -0400 | [diff] [blame] | 187 | ); |