]> Git Repo - J-u-boot.git/blob - cmd/led.c
Merge commit '0344c602eadc0802776b65ff90f0a02c856cf53c' as 'lib/mbedtls/external...
[J-u-boot.git] / cmd / led.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2017 Google, Inc
4  * Written by Simon Glass <[email protected]>
5  */
6
7 #include <command.h>
8 #include <dm.h>
9 #include <led.h>
10 #include <dm/uclass-internal.h>
11
12 #define LED_TOGGLE LEDST_COUNT
13
14 static const char *const state_label[] = {
15         [LEDST_OFF]     = "off",
16         [LEDST_ON]      = "on",
17         [LEDST_TOGGLE]  = "toggle",
18         [LEDST_BLINK]   = "blink",
19 };
20
21 enum led_state_t get_led_cmd(char *var)
22 {
23         int i;
24
25         for (i = 0; i < LEDST_COUNT; i++) {
26                 if (!strncmp(var, state_label[i], strlen(var)))
27                         return i;
28         }
29
30         return -1;
31 }
32
33 static int show_led_state(struct udevice *dev)
34 {
35         int ret;
36
37         ret = led_get_state(dev);
38         if (ret >= LEDST_COUNT)
39                 ret = -EINVAL;
40         if (ret >= 0)
41                 printf("%s\n", state_label[ret]);
42
43         return ret;
44 }
45
46 static int list_leds(void)
47 {
48         struct udevice *dev;
49         int ret;
50
51         for (uclass_find_first_device(UCLASS_LED, &dev);
52              dev;
53              uclass_find_next_device(&dev)) {
54                 struct led_uc_plat *plat = dev_get_uclass_plat(dev);
55
56                 if (!plat->label)
57                         continue;
58                 printf("%-15s ", plat->label);
59                 if (device_active(dev)) {
60                         ret = show_led_state(dev);
61                         if (ret < 0)
62                                 printf("Error %d\n", ret);
63                 } else {
64                         printf("<inactive>\n");
65                 }
66         }
67
68         return 0;
69 }
70
71 int do_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
72 {
73         enum led_state_t cmd;
74         const char *led_label;
75         struct udevice *dev;
76         int freq_ms = 0;
77         int ret;
78
79         /* Validate arguments */
80         if (argc < 2)
81                 return CMD_RET_USAGE;
82         led_label = argv[1];
83         if (strncmp(led_label, "list", 4) == 0)
84                 return list_leds();
85
86         cmd = argc > 2 ? get_led_cmd(argv[2]) : LEDST_COUNT;
87         if (cmd == LEDST_BLINK) {
88                 if (argc < 4)
89                         return CMD_RET_USAGE;
90                 freq_ms = dectoul(argv[3], NULL);
91         }
92         ret = led_get_by_label(led_label, &dev);
93         if (ret) {
94                 printf("LED '%s' not found (err=%d)\n", led_label, ret);
95                 return CMD_RET_FAILURE;
96         }
97         switch (cmd) {
98         case LEDST_OFF:
99         case LEDST_ON:
100         case LEDST_TOGGLE:
101                 ret = led_set_state(dev, cmd);
102                 break;
103         case LEDST_BLINK:
104                 ret = led_set_period(dev, freq_ms);
105                 if (!ret)
106                         ret = led_set_state(dev, LEDST_BLINK);
107                 break;
108         case LEDST_COUNT:
109                 printf("LED '%s': ", led_label);
110                 ret = show_led_state(dev);
111                 break;
112         }
113         if (ret < 0) {
114                 printf("LED '%s' operation failed (err=%d)\n", led_label, ret);
115                 return CMD_RET_FAILURE;
116         }
117
118         return 0;
119 }
120
121 #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK)
122 #define BLINK "|blink [blink-freq in ms]"
123 #else
124 #define BLINK ""
125 #endif
126
127 U_BOOT_CMD(
128         led, 4, 1, do_led,
129         "manage LEDs",
130         "<led_label> on|off|toggle" BLINK "\tChange LED state\n"
131         "led <led_label>\tGet LED state\n"
132         "led list\t\tshow a list of LEDs"
133 );
This page took 0.03212 seconds and 4 git commands to generate.