blob: 50de7e89d8f4c0533497fd907ef674c788cd2d5a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Kridner931ced02011-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 Kridner931ced02011-04-18 17:22:44 -040010 */
11
Jason Kridner931ced02011-04-18 17:22:44 -040012#include <command.h>
13#include <status_led.h>
Tom Rini5c1444f2024-04-27 08:10:59 -060014#include <vsprintf.h>
Jason Kridner931ced02011-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 Kridner7514a7c2011-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 Fernandes26bd9182011-08-11 23:16:53 -050021 void (*toggle)(void);/* Optional function for toggling LED */
Jason Kridner931ced02011-04-18 17:22:44 -040022};
23
24typedef struct led_tbl_s led_tbl_t;
25
26static const led_tbl_t led_commands[] = {
Uri Mashiach4892d392017-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 Kridner931ced02011-04-18 17:22:44 -040030#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020031#ifdef CONFIG_LED_STATUS1
32 { "1", CONFIG_LED_STATUS_BIT1, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040033#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020034#ifdef CONFIG_LED_STATUS2
35 { "2", CONFIG_LED_STATUS_BIT2, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040036#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020037#ifdef CONFIG_LED_STATUS3
38 { "3", CONFIG_LED_STATUS_BIT3, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040039#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020040#ifdef CONFIG_LED_STATUS4
41 { "4", CONFIG_LED_STATUS_BIT4, NULL, NULL, NULL },
Stefan Roese448e9df2015-03-11 09:51:39 +010042#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020043#ifdef CONFIG_LED_STATUS5
44 { "5", CONFIG_LED_STATUS_BIT5, NULL, NULL, NULL },
Stefan Roese448e9df2015-03-11 09:51:39 +010045#endif
Jason Kridner931ced02011-04-18 17:22:44 -040046#endif
Uri Mashiach4892d392017-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 Kridner931ced02011-04-18 17:22:44 -040049#endif
Uri Mashiach4892d392017-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 Kridner931ced02011-04-18 17:22:44 -040053#endif
Uri Mashiach4892d392017-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 Kridner931ced02011-04-18 17:22:44 -040056#endif
Uri Mashiach4892d392017-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 Kridner931ced02011-04-18 17:22:44 -040059#endif
Joel A Fernandes26bd9182011-08-11 23:16:53 -050060 { NULL, 0, NULL, NULL, NULL }
Jason Kridner931ced02011-04-18 17:22:44 -040061};
62
Stefan Roese448e9df2015-03-11 09:51:39 +010063enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE, LED_BLINK };
Joel A Fernandes26bd9182011-08-11 23:16:53 -050064
65enum led_cmd get_led_cmd(char *var)
Jason Kridner931ced02011-04-18 17:22:44 -040066{
Stefan Roese448e9df2015-03-11 09:51:39 +010067 if (strcmp(var, "off") == 0)
Joel A Fernandes26bd9182011-08-11 23:16:53 -050068 return LED_OFF;
Stefan Roese448e9df2015-03-11 09:51:39 +010069 if (strcmp(var, "on") == 0)
Joel A Fernandes26bd9182011-08-11 23:16:53 -050070 return LED_ON;
Joel A Fernandes26bd9182011-08-11 23:16:53 -050071 if (strcmp(var, "toggle") == 0)
72 return LED_TOGGLE;
Stefan Roese448e9df2015-03-11 09:51:39 +010073 if (strcmp(var, "blink") == 0)
74 return LED_BLINK;
75
Jason Kridner931ced02011-04-18 17:22:44 -040076 return -1;
77}
78
Stefan Roese448e9df2015-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 Glassed38aef2020-05-10 11:40:03 -060087int do_legacy_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Jason Kridner931ced02011-04-18 17:22:44 -040088{
Joel A Fernandes26bd9182011-08-11 23:16:53 -050089 int i, match = 0;
90 enum led_cmd cmd;
Stefan Roese448e9df2015-03-11 09:51:39 +010091 int freq;
Jason Kridner931ced02011-04-18 17:22:44 -040092
93 /* Validate arguments */
Stefan Roese448e9df2015-03-11 09:51:39 +010094 if ((argc < 3) || (argc > 4))
Simon Glassa06dfc72011-12-10 08:44:01 +000095 return CMD_RET_USAGE;
Jason Kridner931ced02011-04-18 17:22:44 -040096
Joel A Fernandes26bd9182011-08-11 23:16:53 -050097 cmd = get_led_cmd(argv[2]);
98 if (cmd < 0) {
Simon Glassa06dfc72011-12-10 08:44:01 +000099 return CMD_RET_USAGE;
Jason Kridner931ced02011-04-18 17:22:44 -0400100 }
101
102 for (i = 0; led_commands[i].string; i++) {
Wolfgang Denk80f70212011-05-19 22:21:41 +0200103 if ((strcmp("all", argv[1]) == 0) ||
Jason Kridner931ced02011-04-18 17:22:44 -0400104 (strcmp(led_commands[i].string, argv[1]) == 0)) {
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500105 match = 1;
106 switch (cmd) {
107 case LED_ON:
108 if (led_commands[i].on)
Jason Kridner931ced02011-04-18 17:22:44 -0400109 led_commands[i].on();
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500110 else
Otavio Salvadorfbd76e82013-02-23 02:43:07 +0000111 __led_set(led_commands[i].mask,
Uri Mashiach4892d392017-01-19 10:51:45 +0200112 CONFIG_LED_STATUS_ON);
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500113 break;
114 case LED_OFF:
115 if (led_commands[i].off)
Jason Kridner931ced02011-04-18 17:22:44 -0400116 led_commands[i].off();
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500117 else
Otavio Salvadorfbd76e82013-02-23 02:43:07 +0000118 __led_set(led_commands[i].mask,
Uri Mashiach4892d392017-01-19 10:51:45 +0200119 CONFIG_LED_STATUS_OFF);
Joel A Fernandes26bd9182011-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 Roese448e9df2015-03-11 09:51:39 +0100126 break;
127 case LED_BLINK:
128 if (argc != 4)
129 return CMD_RET_USAGE;
130
Simon Glassff9b9032021-07-24 09:03:30 -0600131 freq = dectoul(argv[3], NULL);
Stefan Roese448e9df2015-03-11 09:51:39 +0100132 __led_blink(led_commands[i].mask, freq);
Jason Kridner931ced02011-04-18 17:22:44 -0400133 }
Joel A Fernandesb64bb202011-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 Kridner931ced02011-04-18 17:22:44 -0400137 }
138 }
139
140 /* If we ran out of matches, print Usage */
Jason Kridner1f440562011-03-08 20:42:01 -0600141 if (!match) {
Simon Glassa06dfc72011-12-10 08:44:01 +0000142 return CMD_RET_USAGE;
Jason Kridner931ced02011-04-18 17:22:44 -0400143 }
144
145 return 0;
146}
147
148U_BOOT_CMD(
Simon Glass4c818462017-04-10 11:34:58 -0600149 led, 4, 1, do_legacy_led,
Robert P. J. Day8ce43a92012-11-11 10:39:07 +0000150 "["
Uri Mashiach4892d392017-01-19 10:51:45 +0200151#ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
152#ifdef CONFIG_LED_STATUS0
Jason Kridner931ced02011-04-18 17:22:44 -0400153 "0|"
154#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200155#ifdef CONFIG_LED_STATUS1
Jason Kridner931ced02011-04-18 17:22:44 -0400156 "1|"
157#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200158#ifdef CONFIG_LED_STATUS2
Jason Kridner931ced02011-04-18 17:22:44 -0400159 "2|"
160#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200161#ifdef CONFIG_LED_STATUS3
Jason Kridner931ced02011-04-18 17:22:44 -0400162 "3|"
163#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200164#ifdef CONFIG_LED_STATUS4
Stefan Roese448e9df2015-03-11 09:51:39 +0100165 "4|"
166#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200167#ifdef CONFIG_LED_STATUS5
Stefan Roese448e9df2015-03-11 09:51:39 +0100168 "5|"
169#endif
Jason Kridner931ced02011-04-18 17:22:44 -0400170#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200171#ifdef CONFIG_LED_STATUS_GREEN
Jason Kridner931ced02011-04-18 17:22:44 -0400172 "green|"
173#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200174#ifdef CONFIG_LED_STATUS_YELLOW
Jason Kridner931ced02011-04-18 17:22:44 -0400175 "yellow|"
176#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200177#ifdef CONFIG_LED_STATUS_RED
Jason Kridner931ced02011-04-18 17:22:44 -0400178 "red|"
179#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200180#ifdef CONFIG_LED_STATUS_BLUE
Jason Kridner931ced02011-04-18 17:22:44 -0400181 "blue|"
182#endif
Stefan Roese448e9df2015-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 Kridner931ced02011-04-18 17:22:44 -0400185);