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