blob: b0f1a61b1bb02cb813452b8ba696619645691f61 [file] [log] [blame]
Jason Kridnerb633f662011-04-18 17:22:44 -04001/*
2 * (C) Copyright 2010
3 * Jason Kridner <jkridner@beagleboard.org>
4 *
5 * Based on cmd_led.c patch from:
6 * http://www.mail-archive.com/u-boot@lists.denx.de/msg06873.html
7 * (C) Copyright 2008
8 * Ulf Samuelsson <ulf.samuelsson@atmel.com>
9 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +020010 * SPDX-License-Identifier: GPL-2.0+
Jason Kridnerb633f662011-04-18 17:22:44 -040011 */
12
13#include <common.h>
14#include <config.h>
15#include <command.h>
16#include <status_led.h>
17
18struct led_tbl_s {
19 char *string; /* String for use in the command */
20 led_id_t mask; /* Mask used for calling __led_set() */
Jason Kridner4086b512011-04-20 18:13:49 -050021 void (*off)(void); /* Optional function for turning LED off */
22 void (*on)(void); /* Optional function for turning LED on */
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050023 void (*toggle)(void);/* Optional function for toggling LED */
Jason Kridnerb633f662011-04-18 17:22:44 -040024};
25
26typedef struct led_tbl_s led_tbl_t;
27
28static const led_tbl_t led_commands[] = {
29#ifdef CONFIG_BOARD_SPECIFIC_LED
30#ifdef STATUS_LED_BIT
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050031 { "0", STATUS_LED_BIT, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040032#endif
33#ifdef STATUS_LED_BIT1
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050034 { "1", STATUS_LED_BIT1, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040035#endif
36#ifdef STATUS_LED_BIT2
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050037 { "2", STATUS_LED_BIT2, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040038#endif
39#ifdef STATUS_LED_BIT3
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050040 { "3", STATUS_LED_BIT3, NULL, NULL, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040041#endif
Stefan Roesea8eeaf22015-03-11 09:51:39 +010042#ifdef STATUS_LED_BIT4
43 { "4", STATUS_LED_BIT4, NULL, NULL, NULL },
44#endif
45#ifdef STATUS_LED_BIT5
46 { "5", STATUS_LED_BIT5, NULL, NULL, NULL },
47#endif
Jason Kridnerb633f662011-04-18 17:22:44 -040048#endif
49#ifdef STATUS_LED_GREEN
Jason Kridner2d3be7c2011-09-04 14:40:16 -040050 { "green", STATUS_LED_GREEN, green_led_off, green_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040051#endif
52#ifdef STATUS_LED_YELLOW
Jason Kridner2d3be7c2011-09-04 14:40:16 -040053 { "yellow", STATUS_LED_YELLOW, yellow_led_off, yellow_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040054#endif
55#ifdef STATUS_LED_RED
Jason Kridner2d3be7c2011-09-04 14:40:16 -040056 { "red", STATUS_LED_RED, red_led_off, red_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040057#endif
58#ifdef STATUS_LED_BLUE
Jason Kridner2d3be7c2011-09-04 14:40:16 -040059 { "blue", STATUS_LED_BLUE, blue_led_off, blue_led_on, NULL },
Jason Kridnerb633f662011-04-18 17:22:44 -040060#endif
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050061 { NULL, 0, NULL, NULL, NULL }
Jason Kridnerb633f662011-04-18 17:22:44 -040062};
63
Stefan Roesea8eeaf22015-03-11 09:51:39 +010064enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE, LED_BLINK };
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050065
66enum led_cmd get_led_cmd(char *var)
Jason Kridnerb633f662011-04-18 17:22:44 -040067{
Stefan Roesea8eeaf22015-03-11 09:51:39 +010068 if (strcmp(var, "off") == 0)
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050069 return LED_OFF;
Stefan Roesea8eeaf22015-03-11 09:51:39 +010070 if (strcmp(var, "on") == 0)
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050071 return LED_ON;
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050072 if (strcmp(var, "toggle") == 0)
73 return LED_TOGGLE;
Stefan Roesea8eeaf22015-03-11 09:51:39 +010074 if (strcmp(var, "blink") == 0)
75 return LED_BLINK;
76
Jason Kridnerb633f662011-04-18 17:22:44 -040077 return -1;
78}
79
Stefan Roesea8eeaf22015-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
Jason Kridnerb633f662011-04-18 17:22:44 -040088int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
89{
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050090 int i, match = 0;
91 enum led_cmd cmd;
Stefan Roesea8eeaf22015-03-11 09:51:39 +010092 int freq;
Jason Kridnerb633f662011-04-18 17:22:44 -040093
94 /* Validate arguments */
Stefan Roesea8eeaf22015-03-11 09:51:39 +010095 if ((argc < 3) || (argc > 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +000096 return CMD_RET_USAGE;
Jason Kridnerb633f662011-04-18 17:22:44 -040097
Joel A Fernandesb8bc8972011-08-11 23:16:53 -050098 cmd = get_led_cmd(argv[2]);
99 if (cmd < 0) {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000100 return CMD_RET_USAGE;
Jason Kridnerb633f662011-04-18 17:22:44 -0400101 }
102
103 for (i = 0; led_commands[i].string; i++) {
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200104 if ((strcmp("all", argv[1]) == 0) ||
Jason Kridnerb633f662011-04-18 17:22:44 -0400105 (strcmp(led_commands[i].string, argv[1]) == 0)) {
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500106 match = 1;
107 switch (cmd) {
108 case LED_ON:
109 if (led_commands[i].on)
Jason Kridnerb633f662011-04-18 17:22:44 -0400110 led_commands[i].on();
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500111 else
Otavio Salvadoraf730342013-02-23 02:43:07 +0000112 __led_set(led_commands[i].mask,
113 STATUS_LED_ON);
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500114 break;
115 case LED_OFF:
116 if (led_commands[i].off)
Jason Kridnerb633f662011-04-18 17:22:44 -0400117 led_commands[i].off();
Joel A Fernandesb8bc8972011-08-11 23:16:53 -0500118 else
Otavio Salvadoraf730342013-02-23 02:43:07 +0000119 __led_set(led_commands[i].mask,
120 STATUS_LED_OFF);
Joel A Fernandesb8bc8972011-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 Roesea8eeaf22015-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 Kridnerb633f662011-04-18 17:22:44 -0400134 }
Joel A Fernandesd604cda2011-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 Kridnerb633f662011-04-18 17:22:44 -0400138 }
139 }
140
141 /* If we ran out of matches, print Usage */
Jason Kridner95492d72011-03-08 20:42:01 -0600142 if (!match) {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000143 return CMD_RET_USAGE;
Jason Kridnerb633f662011-04-18 17:22:44 -0400144 }
145
146 return 0;
147}
148
149U_BOOT_CMD(
Stefan Roesea8eeaf22015-03-11 09:51:39 +0100150 led, 4, 1, do_led,
Robert P. J. Dayd4b901d2012-11-11 10:39:07 +0000151 "["
Jason Kridnerb633f662011-04-18 17:22:44 -0400152#ifdef CONFIG_BOARD_SPECIFIC_LED
153#ifdef STATUS_LED_BIT
154 "0|"
155#endif
156#ifdef STATUS_LED_BIT1
157 "1|"
158#endif
159#ifdef STATUS_LED_BIT2
160 "2|"
161#endif
162#ifdef STATUS_LED_BIT3
163 "3|"
164#endif
Stefan Roesea8eeaf22015-03-11 09:51:39 +0100165#ifdef STATUS_LED_BIT4
166 "4|"
167#endif
168#ifdef STATUS_LED_BIT5
169 "5|"
170#endif
Jason Kridnerb633f662011-04-18 17:22:44 -0400171#endif
172#ifdef STATUS_LED_GREEN
173 "green|"
174#endif
175#ifdef STATUS_LED_YELLOW
176 "yellow|"
177#endif
178#ifdef STATUS_LED_RED
179 "red|"
180#endif
181#ifdef STATUS_LED_BLUE
182 "blue|"
183#endif
Stefan Roesea8eeaf22015-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 Kridnerb633f662011-04-18 17:22:44 -0400186);