blob: 7d40d8db58e0c4b412f187b66bbf0b093253909f [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
12#include <common.h>
13#include <config.h>
14#include <command.h>
15#include <status_led.h>
16
17struct led_tbl_s {
18 char *string; /* String for use in the command */
19 led_id_t mask; /* Mask used for calling __led_set() */
Jason Kridner7514a7c2011-04-20 18:13:49 -050020 void (*off)(void); /* Optional function for turning LED off */
21 void (*on)(void); /* Optional function for turning LED on */
Joel A Fernandes26bd9182011-08-11 23:16:53 -050022 void (*toggle)(void);/* Optional function for toggling LED */
Jason Kridner931ced02011-04-18 17:22:44 -040023};
24
25typedef struct led_tbl_s led_tbl_t;
26
27static const led_tbl_t led_commands[] = {
Uri Mashiach4892d392017-01-19 10:51:45 +020028#ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
29#ifdef CONFIG_LED_STATUS0
30 { "0", CONFIG_LED_STATUS_BIT, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040031#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020032#ifdef CONFIG_LED_STATUS1
33 { "1", CONFIG_LED_STATUS_BIT1, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040034#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020035#ifdef CONFIG_LED_STATUS2
36 { "2", CONFIG_LED_STATUS_BIT2, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040037#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020038#ifdef CONFIG_LED_STATUS3
39 { "3", CONFIG_LED_STATUS_BIT3, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040040#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020041#ifdef CONFIG_LED_STATUS4
42 { "4", CONFIG_LED_STATUS_BIT4, NULL, NULL, NULL },
Stefan Roese448e9df2015-03-11 09:51:39 +010043#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020044#ifdef CONFIG_LED_STATUS5
45 { "5", CONFIG_LED_STATUS_BIT5, NULL, NULL, NULL },
Stefan Roese448e9df2015-03-11 09:51:39 +010046#endif
Jason Kridner931ced02011-04-18 17:22:44 -040047#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020048#ifdef CONFIG_LED_STATUS_GREEN
49 { "green", CONFIG_LED_STATUS_GREEN, green_led_off, green_led_on, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040050#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020051#ifdef CONFIG_LED_STATUS_YELLOW
52 { "yellow", CONFIG_LED_STATUS_YELLOW, yellow_led_off, yellow_led_on,
53 NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040054#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020055#ifdef CONFIG_LED_STATUS_RED
56 { "red", CONFIG_LED_STATUS_RED, red_led_off, red_led_on, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040057#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020058#ifdef CONFIG_LED_STATUS_BLUE
59 { "blue", CONFIG_LED_STATUS_BLUE, blue_led_off, blue_led_on, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040060#endif
Joel A Fernandes26bd9182011-08-11 23:16:53 -050061 { NULL, 0, NULL, NULL, NULL }
Jason Kridner931ced02011-04-18 17:22:44 -040062};
63
Stefan Roese448e9df2015-03-11 09:51:39 +010064enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE, LED_BLINK };
Joel A Fernandes26bd9182011-08-11 23:16:53 -050065
66enum led_cmd get_led_cmd(char *var)
Jason Kridner931ced02011-04-18 17:22:44 -040067{
Stefan Roese448e9df2015-03-11 09:51:39 +010068 if (strcmp(var, "off") == 0)
Joel A Fernandes26bd9182011-08-11 23:16:53 -050069 return LED_OFF;
Stefan Roese448e9df2015-03-11 09:51:39 +010070 if (strcmp(var, "on") == 0)
Joel A Fernandes26bd9182011-08-11 23:16:53 -050071 return LED_ON;
Joel A Fernandes26bd9182011-08-11 23:16:53 -050072 if (strcmp(var, "toggle") == 0)
73 return LED_TOGGLE;
Stefan Roese448e9df2015-03-11 09:51:39 +010074 if (strcmp(var, "blink") == 0)
75 return LED_BLINK;
76
Jason Kridner931ced02011-04-18 17:22:44 -040077 return -1;
78}
79
Stefan Roese448e9df2015-03-11 09:51:39 +010080/*
81 * LED drivers providing a blinking LED functionality, like the
82 * PCA9551, can override this empty weak function
83 */
84void __weak __led_blink(led_id_t mask, int freq)
85{
86}
87
Simon Glass4c818462017-04-10 11:34:58 -060088int do_legacy_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Jason Kridner931ced02011-04-18 17:22:44 -040089{
Joel A Fernandes26bd9182011-08-11 23:16:53 -050090 int i, match = 0;
91 enum led_cmd cmd;
Stefan Roese448e9df2015-03-11 09:51:39 +010092 int freq;
Jason Kridner931ced02011-04-18 17:22:44 -040093
94 /* Validate arguments */
Stefan Roese448e9df2015-03-11 09:51:39 +010095 if ((argc < 3) || (argc > 4))
Simon Glassa06dfc72011-12-10 08:44:01 +000096 return CMD_RET_USAGE;
Jason Kridner931ced02011-04-18 17:22:44 -040097
Joel A Fernandes26bd9182011-08-11 23:16:53 -050098 cmd = get_led_cmd(argv[2]);
99 if (cmd < 0) {
Simon Glassa06dfc72011-12-10 08:44:01 +0000100 return CMD_RET_USAGE;
Jason Kridner931ced02011-04-18 17:22:44 -0400101 }
102
103 for (i = 0; led_commands[i].string; i++) {
Wolfgang Denk80f70212011-05-19 22:21:41 +0200104 if ((strcmp("all", argv[1]) == 0) ||
Jason Kridner931ced02011-04-18 17:22:44 -0400105 (strcmp(led_commands[i].string, argv[1]) == 0)) {
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500106 match = 1;
107 switch (cmd) {
108 case LED_ON:
109 if (led_commands[i].on)
Jason Kridner931ced02011-04-18 17:22:44 -0400110 led_commands[i].on();
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500111 else
Otavio Salvadorfbd76e82013-02-23 02:43:07 +0000112 __led_set(led_commands[i].mask,
Uri Mashiach4892d392017-01-19 10:51:45 +0200113 CONFIG_LED_STATUS_ON);
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500114 break;
115 case LED_OFF:
116 if (led_commands[i].off)
Jason Kridner931ced02011-04-18 17:22:44 -0400117 led_commands[i].off();
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500118 else
Otavio Salvadorfbd76e82013-02-23 02:43:07 +0000119 __led_set(led_commands[i].mask,
Uri Mashiach4892d392017-01-19 10:51:45 +0200120 CONFIG_LED_STATUS_OFF);
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500121 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 Roese448e9df2015-03-11 09:51:39 +0100127 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 Kridner931ced02011-04-18 17:22:44 -0400134 }
Joel A Fernandesb64bb202011-08-11 19:10:19 -0500135 /* Need to set only 1 led if led_name wasn't 'all' */
136 if (strcmp("all", argv[1]) != 0)
137 break;
Jason Kridner931ced02011-04-18 17:22:44 -0400138 }
139 }
140
141 /* If we ran out of matches, print Usage */
Jason Kridner1f440562011-03-08 20:42:01 -0600142 if (!match) {
Simon Glassa06dfc72011-12-10 08:44:01 +0000143 return CMD_RET_USAGE;
Jason Kridner931ced02011-04-18 17:22:44 -0400144 }
145
146 return 0;
147}
148
149U_BOOT_CMD(
Simon Glass4c818462017-04-10 11:34:58 -0600150 led, 4, 1, do_legacy_led,
Robert P. J. Day8ce43a92012-11-11 10:39:07 +0000151 "["
Uri Mashiach4892d392017-01-19 10:51:45 +0200152#ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
153#ifdef CONFIG_LED_STATUS0
Jason Kridner931ced02011-04-18 17:22:44 -0400154 "0|"
155#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200156#ifdef CONFIG_LED_STATUS1
Jason Kridner931ced02011-04-18 17:22:44 -0400157 "1|"
158#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200159#ifdef CONFIG_LED_STATUS2
Jason Kridner931ced02011-04-18 17:22:44 -0400160 "2|"
161#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200162#ifdef CONFIG_LED_STATUS3
Jason Kridner931ced02011-04-18 17:22:44 -0400163 "3|"
164#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200165#ifdef CONFIG_LED_STATUS4
Stefan Roese448e9df2015-03-11 09:51:39 +0100166 "4|"
167#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200168#ifdef CONFIG_LED_STATUS5
Stefan Roese448e9df2015-03-11 09:51:39 +0100169 "5|"
170#endif
Jason Kridner931ced02011-04-18 17:22:44 -0400171#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200172#ifdef CONFIG_LED_STATUS_GREEN
Jason Kridner931ced02011-04-18 17:22:44 -0400173 "green|"
174#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200175#ifdef CONFIG_LED_STATUS_YELLOW
Jason Kridner931ced02011-04-18 17:22:44 -0400176 "yellow|"
177#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200178#ifdef CONFIG_LED_STATUS_RED
Jason Kridner931ced02011-04-18 17:22:44 -0400179 "red|"
180#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200181#ifdef CONFIG_LED_STATUS_BLUE
Jason Kridner931ced02011-04-18 17:22:44 -0400182 "blue|"
183#endif
Stefan Roese448e9df2015-03-11 09:51:39 +0100184 "all] [on|off|toggle|blink] [blink-freq in ms]",
185 "[led_name] [on|off|toggle|blink] sets or clears led(s)"
Jason Kridner931ced02011-04-18 17:22:44 -0400186);