blob: 50de7e89d8f4c0533497fd907ef674c788cd2d5a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Kridnerb633f662011-04-18 17:22:44 -04002/*
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 Kridnerb633f662011-04-18 17:22:44 -040010 */
11
Jason Kridnerb633f662011-04-18 17:22:44 -040012#include <command.h>
13#include <status_led.h>
Tom Rini03de3052024-05-20 13:35:03 -060014#include <vsprintf.h>
Jason Kridnerb633f662011-04-18 17:22:44 -040015
16struct led_tbl_s {
17 char *string; /* String for use in the command */
18 led_id_t mask; /* Mask used for calling __led_set() */
Jason Kridner4086b512011-04-20 18:13:49 -050019 void (*off)(void); /* Optional function for turning LED off */
20 void (*on)(void); /* Optional function for turning LED on */
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050021 void (*toggle)(void);/* Optional function for toggling LED */
Jason Kridnerb633f662011-04-18 17:22:44 -040022};
23
24typedef struct led_tbl_s led_tbl_t;
25
26static const led_tbl_t led_commands[] = {
Uri Mashiach2d8d1902017-01-19 10:51:45 +020027#ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
28#ifdef CONFIG_LED_STATUS0
29 { "0", CONFIG_LED_STATUS_BIT, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040030#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +020031#ifdef CONFIG_LED_STATUS1
32 { "1", CONFIG_LED_STATUS_BIT1, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040033#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +020034#ifdef CONFIG_LED_STATUS2
35 { "2", CONFIG_LED_STATUS_BIT2, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040036#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +020037#ifdef CONFIG_LED_STATUS3
38 { "3", CONFIG_LED_STATUS_BIT3, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040039#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +020040#ifdef CONFIG_LED_STATUS4
41 { "4", CONFIG_LED_STATUS_BIT4, NULL, NULL, NULL },
Stefan Roesea8eeaf22015-03-11 09:51:39 +010042#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +020043#ifdef CONFIG_LED_STATUS5
44 { "5", CONFIG_LED_STATUS_BIT5, NULL, NULL, NULL },
Stefan Roesea8eeaf22015-03-11 09:51:39 +010045#endif
Jason Kridnerb633f662011-04-18 17:22:44 -040046#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +020047#ifdef CONFIG_LED_STATUS_GREEN
48 { "green", CONFIG_LED_STATUS_GREEN, green_led_off, green_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040049#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +020050#ifdef CONFIG_LED_STATUS_YELLOW
51 { "yellow", CONFIG_LED_STATUS_YELLOW, yellow_led_off, yellow_led_on,
52 NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040053#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +020054#ifdef CONFIG_LED_STATUS_RED
55 { "red", CONFIG_LED_STATUS_RED, red_led_off, red_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040056#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +020057#ifdef CONFIG_LED_STATUS_BLUE
58 { "blue", CONFIG_LED_STATUS_BLUE, blue_led_off, blue_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040059#endif
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050060 { NULL, 0, NULL, NULL, NULL }
Jason Kridnerb633f662011-04-18 17:22:44 -040061};
62
Stefan Roesea8eeaf22015-03-11 09:51:39 +010063enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE, LED_BLINK };
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050064
65enum led_cmd get_led_cmd(char *var)
Jason Kridnerb633f662011-04-18 17:22:44 -040066{
Stefan Roesea8eeaf22015-03-11 09:51:39 +010067 if (strcmp(var, "off") == 0)
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050068 return LED_OFF;
Stefan Roesea8eeaf22015-03-11 09:51:39 +010069 if (strcmp(var, "on") == 0)
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050070 return LED_ON;
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050071 if (strcmp(var, "toggle") == 0)
72 return LED_TOGGLE;
Stefan Roesea8eeaf22015-03-11 09:51:39 +010073 if (strcmp(var, "blink") == 0)
74 return LED_BLINK;
75
Jason Kridnerb633f662011-04-18 17:22:44 -040076 return -1;
77}
78
Stefan Roesea8eeaf22015-03-11 09:51:39 +010079/*
80 * LED drivers providing a blinking LED functionality, like the
81 * PCA9551, can override this empty weak function
82 */
83void __weak __led_blink(led_id_t mask, int freq)
84{
85}
86
Simon Glass09140112020-05-10 11:40:03 -060087int do_legacy_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Kridnerb633f662011-04-18 17:22:44 -040088{
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050089 int i, match = 0;
90 enum led_cmd cmd;
Stefan Roesea8eeaf22015-03-11 09:51:39 +010091 int freq;
Jason Kridnerb633f662011-04-18 17:22:44 -040092
93 /* Validate arguments */
Stefan Roesea8eeaf22015-03-11 09:51:39 +010094 if ((argc < 3) || (argc > 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +000095 return CMD_RET_USAGE;
Jason Kridnerb633f662011-04-18 17:22:44 -040096
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050097 cmd = get_led_cmd(argv[2]);
98 if (cmd < 0) {
Simon Glass4c12eeb2011-12-10 08:44:01 +000099 return CMD_RET_USAGE;
Jason Kridnerb633f662011-04-18 17:22:44 -0400100 }
101
102 for (i = 0; led_commands[i].string; i++) {
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200103 if ((strcmp("all", argv[1]) == 0) ||
Jason Kridnerb633f662011-04-18 17:22:44 -0400104 (strcmp(led_commands[i].string, argv[1]) == 0)) {
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500105 match = 1;
106 switch (cmd) {
107 case LED_ON:
108 if (led_commands[i].on)
Jason Kridnerb633f662011-04-18 17:22:44 -0400109 led_commands[i].on();
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500110 else
Otavio Salvadoraf730342013-02-23 02:43:07 +0000111 __led_set(led_commands[i].mask,
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200112 CONFIG_LED_STATUS_ON);
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500113 break;
114 case LED_OFF:
115 if (led_commands[i].off)
Jason Kridnerb633f662011-04-18 17:22:44 -0400116 led_commands[i].off();
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500117 else
Otavio Salvadoraf730342013-02-23 02:43:07 +0000118 __led_set(led_commands[i].mask,
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200119 CONFIG_LED_STATUS_OFF);
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500120 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 Roesea8eeaf22015-03-11 09:51:39 +0100126 break;
127 case LED_BLINK:
128 if (argc != 4)
129 return CMD_RET_USAGE;
130
Simon Glass0b1284e2021-07-24 09:03:30 -0600131 freq = dectoul(argv[3], NULL);
Stefan Roesea8eeaf22015-03-11 09:51:39 +0100132 __led_blink(led_commands[i].mask, freq);
Jason Kridnerb633f662011-04-18 17:22:44 -0400133 }
Joel A Fernandesd604cda2011-08-11 19:10:19 -0500134 /* Need to set only 1 led if led_name wasn't 'all' */
135 if (strcmp("all", argv[1]) != 0)
136 break;
Jason Kridnerb633f662011-04-18 17:22:44 -0400137 }
138 }
139
140 /* If we ran out of matches, print Usage */
Jason Kridner95492d72011-03-08 20:42:01 -0600141 if (!match) {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000142 return CMD_RET_USAGE;
Jason Kridnerb633f662011-04-18 17:22:44 -0400143 }
144
145 return 0;
146}
147
148U_BOOT_CMD(
Simon Glass21c34082017-04-10 11:34:58 -0600149 led, 4, 1, do_legacy_led,
Robert P. J. Dayd4b901d2012-11-11 10:39:07 +0000150 "["
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200151#ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
152#ifdef CONFIG_LED_STATUS0
Jason Kridnerb633f662011-04-18 17:22:44 -0400153 "0|"
154#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200155#ifdef CONFIG_LED_STATUS1
Jason Kridnerb633f662011-04-18 17:22:44 -0400156 "1|"
157#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200158#ifdef CONFIG_LED_STATUS2
Jason Kridnerb633f662011-04-18 17:22:44 -0400159 "2|"
160#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200161#ifdef CONFIG_LED_STATUS3
Jason Kridnerb633f662011-04-18 17:22:44 -0400162 "3|"
163#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200164#ifdef CONFIG_LED_STATUS4
Stefan Roesea8eeaf22015-03-11 09:51:39 +0100165 "4|"
166#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200167#ifdef CONFIG_LED_STATUS5
Stefan Roesea8eeaf22015-03-11 09:51:39 +0100168 "5|"
169#endif
Jason Kridnerb633f662011-04-18 17:22:44 -0400170#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200171#ifdef CONFIG_LED_STATUS_GREEN
Jason Kridnerb633f662011-04-18 17:22:44 -0400172 "green|"
173#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200174#ifdef CONFIG_LED_STATUS_YELLOW
Jason Kridnerb633f662011-04-18 17:22:44 -0400175 "yellow|"
176#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200177#ifdef CONFIG_LED_STATUS_RED
Jason Kridnerb633f662011-04-18 17:22:44 -0400178 "red|"
179#endif
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200180#ifdef CONFIG_LED_STATUS_BLUE
Jason Kridnerb633f662011-04-18 17:22:44 -0400181 "blue|"
182#endif
Stefan Roesea8eeaf22015-03-11 09:51:39 +0100183 "all] [on|off|toggle|blink] [blink-freq in ms]",
184 "[led_name] [on|off|toggle|blink] sets or clears led(s)"
Jason Kridnerb633f662011-04-18 17:22:44 -0400185);