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