blob: 951a5e242f264ac7ecbe87347d078d7c9b531700 [file] [log] [blame]
Jason Kridner931ced02011-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 Denkbd8ec7e2013-10-07 13:07:26 +020010 * SPDX-License-Identifier: GPL-2.0+
Jason Kridner931ced02011-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 Kridner7514a7c2011-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 Fernandes26bd9182011-08-11 23:16:53 -050023 void (*toggle)(void);/* Optional function for toggling LED */
Jason Kridner931ced02011-04-18 17:22:44 -040024};
25
26typedef struct led_tbl_s led_tbl_t;
27
28static const led_tbl_t led_commands[] = {
Uri Mashiach4892d392017-01-19 10:51:45 +020029#ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
30#ifdef CONFIG_LED_STATUS0
31 { "0", CONFIG_LED_STATUS_BIT, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040032#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020033#ifdef CONFIG_LED_STATUS1
34 { "1", CONFIG_LED_STATUS_BIT1, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040035#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020036#ifdef CONFIG_LED_STATUS2
37 { "2", CONFIG_LED_STATUS_BIT2, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040038#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020039#ifdef CONFIG_LED_STATUS3
40 { "3", CONFIG_LED_STATUS_BIT3, NULL, NULL, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040041#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020042#ifdef CONFIG_LED_STATUS4
43 { "4", CONFIG_LED_STATUS_BIT4, NULL, NULL, NULL },
Stefan Roese448e9df2015-03-11 09:51:39 +010044#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020045#ifdef CONFIG_LED_STATUS5
46 { "5", CONFIG_LED_STATUS_BIT5, NULL, NULL, NULL },
Stefan Roese448e9df2015-03-11 09:51:39 +010047#endif
Jason Kridner931ced02011-04-18 17:22:44 -040048#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020049#ifdef CONFIG_LED_STATUS_GREEN
50 { "green", CONFIG_LED_STATUS_GREEN, green_led_off, green_led_on, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040051#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020052#ifdef CONFIG_LED_STATUS_YELLOW
53 { "yellow", CONFIG_LED_STATUS_YELLOW, yellow_led_off, yellow_led_on,
54 NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040055#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020056#ifdef CONFIG_LED_STATUS_RED
57 { "red", CONFIG_LED_STATUS_RED, red_led_off, red_led_on, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040058#endif
Uri Mashiach4892d392017-01-19 10:51:45 +020059#ifdef CONFIG_LED_STATUS_BLUE
60 { "blue", CONFIG_LED_STATUS_BLUE, blue_led_off, blue_led_on, NULL },
Jason Kridner931ced02011-04-18 17:22:44 -040061#endif
Joel A Fernandes26bd9182011-08-11 23:16:53 -050062 { NULL, 0, NULL, NULL, NULL }
Jason Kridner931ced02011-04-18 17:22:44 -040063};
64
Stefan Roese448e9df2015-03-11 09:51:39 +010065enum led_cmd { LED_ON, LED_OFF, LED_TOGGLE, LED_BLINK };
Joel A Fernandes26bd9182011-08-11 23:16:53 -050066
67enum led_cmd get_led_cmd(char *var)
Jason Kridner931ced02011-04-18 17:22:44 -040068{
Stefan Roese448e9df2015-03-11 09:51:39 +010069 if (strcmp(var, "off") == 0)
Joel A Fernandes26bd9182011-08-11 23:16:53 -050070 return LED_OFF;
Stefan Roese448e9df2015-03-11 09:51:39 +010071 if (strcmp(var, "on") == 0)
Joel A Fernandes26bd9182011-08-11 23:16:53 -050072 return LED_ON;
Joel A Fernandes26bd9182011-08-11 23:16:53 -050073 if (strcmp(var, "toggle") == 0)
74 return LED_TOGGLE;
Stefan Roese448e9df2015-03-11 09:51:39 +010075 if (strcmp(var, "blink") == 0)
76 return LED_BLINK;
77
Jason Kridner931ced02011-04-18 17:22:44 -040078 return -1;
79}
80
Stefan Roese448e9df2015-03-11 09:51:39 +010081/*
82 * LED drivers providing a blinking LED functionality, like the
83 * PCA9551, can override this empty weak function
84 */
85void __weak __led_blink(led_id_t mask, int freq)
86{
87}
88
Jason Kridner931ced02011-04-18 17:22:44 -040089int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
90{
Joel A Fernandes26bd9182011-08-11 23:16:53 -050091 int i, match = 0;
92 enum led_cmd cmd;
Stefan Roese448e9df2015-03-11 09:51:39 +010093 int freq;
Jason Kridner931ced02011-04-18 17:22:44 -040094
95 /* Validate arguments */
Stefan Roese448e9df2015-03-11 09:51:39 +010096 if ((argc < 3) || (argc > 4))
Simon Glassa06dfc72011-12-10 08:44:01 +000097 return CMD_RET_USAGE;
Jason Kridner931ced02011-04-18 17:22:44 -040098
Joel A Fernandes26bd9182011-08-11 23:16:53 -050099 cmd = get_led_cmd(argv[2]);
100 if (cmd < 0) {
Simon Glassa06dfc72011-12-10 08:44:01 +0000101 return CMD_RET_USAGE;
Jason Kridner931ced02011-04-18 17:22:44 -0400102 }
103
104 for (i = 0; led_commands[i].string; i++) {
Wolfgang Denk80f70212011-05-19 22:21:41 +0200105 if ((strcmp("all", argv[1]) == 0) ||
Jason Kridner931ced02011-04-18 17:22:44 -0400106 (strcmp(led_commands[i].string, argv[1]) == 0)) {
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500107 match = 1;
108 switch (cmd) {
109 case LED_ON:
110 if (led_commands[i].on)
Jason Kridner931ced02011-04-18 17:22:44 -0400111 led_commands[i].on();
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500112 else
Otavio Salvadorfbd76e82013-02-23 02:43:07 +0000113 __led_set(led_commands[i].mask,
Uri Mashiach4892d392017-01-19 10:51:45 +0200114 CONFIG_LED_STATUS_ON);
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500115 break;
116 case LED_OFF:
117 if (led_commands[i].off)
Jason Kridner931ced02011-04-18 17:22:44 -0400118 led_commands[i].off();
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500119 else
Otavio Salvadorfbd76e82013-02-23 02:43:07 +0000120 __led_set(led_commands[i].mask,
Uri Mashiach4892d392017-01-19 10:51:45 +0200121 CONFIG_LED_STATUS_OFF);
Joel A Fernandes26bd9182011-08-11 23:16:53 -0500122 break;
123 case LED_TOGGLE:
124 if (led_commands[i].toggle)
125 led_commands[i].toggle();
126 else
127 __led_toggle(led_commands[i].mask);
Stefan Roese448e9df2015-03-11 09:51:39 +0100128 break;
129 case LED_BLINK:
130 if (argc != 4)
131 return CMD_RET_USAGE;
132
133 freq = simple_strtoul(argv[3], NULL, 10);
134 __led_blink(led_commands[i].mask, freq);
Jason Kridner931ced02011-04-18 17:22:44 -0400135 }
Joel A Fernandesb64bb202011-08-11 19:10:19 -0500136 /* Need to set only 1 led if led_name wasn't 'all' */
137 if (strcmp("all", argv[1]) != 0)
138 break;
Jason Kridner931ced02011-04-18 17:22:44 -0400139 }
140 }
141
142 /* If we ran out of matches, print Usage */
Jason Kridner1f440562011-03-08 20:42:01 -0600143 if (!match) {
Simon Glassa06dfc72011-12-10 08:44:01 +0000144 return CMD_RET_USAGE;
Jason Kridner931ced02011-04-18 17:22:44 -0400145 }
146
147 return 0;
148}
149
150U_BOOT_CMD(
Stefan Roese448e9df2015-03-11 09:51:39 +0100151 led, 4, 1, do_led,
Robert P. J. Day8ce43a92012-11-11 10:39:07 +0000152 "["
Uri Mashiach4892d392017-01-19 10:51:45 +0200153#ifdef CONFIG_LED_STATUS_BOARD_SPECIFIC
154#ifdef CONFIG_LED_STATUS0
Jason Kridner931ced02011-04-18 17:22:44 -0400155 "0|"
156#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200157#ifdef CONFIG_LED_STATUS1
Jason Kridner931ced02011-04-18 17:22:44 -0400158 "1|"
159#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200160#ifdef CONFIG_LED_STATUS2
Jason Kridner931ced02011-04-18 17:22:44 -0400161 "2|"
162#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200163#ifdef CONFIG_LED_STATUS3
Jason Kridner931ced02011-04-18 17:22:44 -0400164 "3|"
165#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200166#ifdef CONFIG_LED_STATUS4
Stefan Roese448e9df2015-03-11 09:51:39 +0100167 "4|"
168#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200169#ifdef CONFIG_LED_STATUS5
Stefan Roese448e9df2015-03-11 09:51:39 +0100170 "5|"
171#endif
Jason Kridner931ced02011-04-18 17:22:44 -0400172#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200173#ifdef CONFIG_LED_STATUS_GREEN
Jason Kridner931ced02011-04-18 17:22:44 -0400174 "green|"
175#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200176#ifdef CONFIG_LED_STATUS_YELLOW
Jason Kridner931ced02011-04-18 17:22:44 -0400177 "yellow|"
178#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200179#ifdef CONFIG_LED_STATUS_RED
Jason Kridner931ced02011-04-18 17:22:44 -0400180 "red|"
181#endif
Uri Mashiach4892d392017-01-19 10:51:45 +0200182#ifdef CONFIG_LED_STATUS_BLUE
Jason Kridner931ced02011-04-18 17:22:44 -0400183 "blue|"
184#endif
Stefan Roese448e9df2015-03-11 09:51:39 +0100185 "all] [on|off|toggle|blink] [blink-freq in ms]",
186 "[led_name] [on|off|toggle|blink] sets or clears led(s)"
Jason Kridner931ced02011-04-18 17:22:44 -0400187);