blob: 91fb856ee5989c43e6dfb866168b0487ebc3b919 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassfc8a5292017-04-10 11:34:59 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassfc8a5292017-04-10 11:34:59 -06005 */
6
Simon Glassfc8a5292017-04-10 11:34:59 -06007#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
14static const char *const state_label[] = {
15 [LEDST_OFF] = "off",
16 [LEDST_ON] = "on",
17 [LEDST_TOGGLE] = "toggle",
Simon Glassfc8a5292017-04-10 11:34:59 -060018 [LEDST_BLINK] = "blink",
Simon Glassfc8a5292017-04-10 11:34:59 -060019};
20
21enum 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
33static 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
46static 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)) {
Simon Glass71fa5b42020-12-03 16:55:18 -070054 struct led_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glassfc8a5292017-04-10 11:34:59 -060055
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
Simon Glassed38aef2020-05-10 11:40:03 -060071int do_led(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glassfc8a5292017-04-10 11:34:59 -060072{
73 enum led_state_t cmd;
74 const char *led_label;
75 struct udevice *dev;
Simon Glassfc8a5292017-04-10 11:34:59 -060076 int freq_ms = 0;
Simon Glassfc8a5292017-04-10 11:34:59 -060077 int ret;
78
79 /* Validate arguments */
80 if (argc < 2)
81 return CMD_RET_USAGE;
82 led_label = argv[1];
Heiko Schocher741f2692019-05-27 08:14:16 +020083 if (strncmp(led_label, "list", 4) == 0)
Simon Glassfc8a5292017-04-10 11:34:59 -060084 return list_leds();
85
86 cmd = argc > 2 ? get_led_cmd(argv[2]) : LEDST_COUNT;
Simon Glassfc8a5292017-04-10 11:34:59 -060087 if (cmd == LEDST_BLINK) {
88 if (argc < 4)
89 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -060090 freq_ms = dectoul(argv[3], NULL);
Simon Glassfc8a5292017-04-10 11:34:59 -060091 }
Simon Glassfc8a5292017-04-10 11:34:59 -060092 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;
Simon Glassfc8a5292017-04-10 11:34:59 -0600103 case LEDST_BLINK:
104 ret = led_set_period(dev, freq_ms);
105 if (!ret)
106 ret = led_set_state(dev, LEDST_BLINK);
107 break;
Simon Glassfc8a5292017-04-10 11:34:59 -0600108 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
Mikhail Kshevetskiy97613b62024-08-14 14:08:30 +0400121#if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK)
Simon Glassfc8a5292017-04-10 11:34:59 -0600122#define BLINK "|blink [blink-freq in ms]"
123#else
124#define BLINK ""
125#endif
126
127U_BOOT_CMD(
128 led, 4, 1, do_led,
129 "manage LEDs",
130 "<led_label> on|off|toggle" BLINK "\tChange LED state\n"
Anatolij Gustschin486201d2019-06-17 15:33:55 +0200131 "led <led_label>\tGet LED state\n"
Simon Glassfc8a5292017-04-10 11:34:59 -0600132 "led list\t\tshow a list of LEDs"
133);