Coverage Report

Created: 2026-11-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/drivers/led/led-gpio.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * gpio LED support for barebox
4
 *
5
 * (C) Copyright 2010 Sascha Hauer, Pengutronix
6
 */
7
#include <common.h>
8
#include <init.h>
9
#include <led.h>
10
#include <malloc.h>
11
#include <gpio.h>
12
#include <of_gpio.h>
13
14
static void led_gpio_set(struct led *led, unsigned int value)
15
0
{
16
0
  struct gpio_led *gpio_led = container_of(led, struct gpio_led, led);
17
18
0
  gpio_direction_output(gpio_led->gpio, !!value ^ gpio_led->active_low);
19
0
}
20
21
/**
22
 * led_gpio_register - register a gpio controlled LED
23
 * @param led The gpio LED
24
 *
25
 * This function registers a single gpio as a LED. led->gpio
26
 * should be initialized to the gpio to control.
27
 */
28
int led_gpio_register(struct gpio_led *led)
29
0
{
30
0
  int ret;
31
0
  const char *name = led->led.name;
32
33
0
  ret = gpio_request(led->gpio, name ? name : "led");
34
0
  if (ret)
35
0
    return ret;
36
37
0
  led->led.set = led_gpio_set;
38
0
  led->led.max_value = 1;
39
40
0
  ret = led_register(&led->led);
41
0
  if (ret)
42
0
    gpio_free(led->gpio);
43
44
0
  return ret;
45
0
}
46
47
/**
48
 * led_gpio_unregister - remove a gpio controlled LED from the framework
49
 * @param led The gpio LED
50
 */
51
void led_gpio_unregister(struct gpio_led *led)
52
0
{
53
0
  led_unregister(&led->led);
54
0
  gpio_free(led->gpio);
55
0
}
56
57
#ifdef CONFIG_LED_GPIO_BICOLOR
58
static void led_gpio_bicolor_set(struct led *led, unsigned int value)
59
0
{
60
0
  struct gpio_bicolor_led *bi = container_of(led, struct gpio_bicolor_led, led);
61
0
  int al = bi->active_low;
62
63
0
  switch (value) {
64
0
  case 0:
65
0
    gpio_direction_output(bi->gpio_c0, al);
66
0
    gpio_direction_output(bi->gpio_c1, al);
67
0
    break;
68
0
  case 1:
69
0
    gpio_direction_output(bi->gpio_c0, !al);
70
0
    gpio_direction_output(bi->gpio_c1, al);
71
0
    break;
72
0
  case 2:
73
0
    gpio_direction_output(bi->gpio_c0, al);
74
0
    gpio_direction_output(bi->gpio_c1, !al);
75
0
    break;
76
0
  }
77
0
}
78
79
/**
80
 * led_gpio_bicolor_register - register three gpios as a bicolor LED
81
 * @param led The gpio bicolor LED
82
 *
83
 * This function registers three gpios as a bicolor LED. led->gpio[rg]
84
 * should be initialized to the gpios to control.
85
 */
86
int led_gpio_bicolor_register(struct gpio_bicolor_led *led)
87
0
{
88
0
  int ret;
89
0
  const char *name = led->led.name;
90
91
0
  ret = gpio_request(led->gpio_c0, name ? name : "led_c0");
92
0
  if (ret)
93
0
    return ret;
94
95
0
  ret = gpio_request(led->gpio_c1, name ? name : "led_c1");
96
0
  if (ret)
97
0
    goto err_gpio_c0;
98
99
0
  led->led.set = led_gpio_bicolor_set;
100
0
  led->led.max_value = 2;
101
102
0
  ret = led_register(&led->led);
103
0
  if (ret)
104
0
    goto err_gpio_c1;
105
106
0
  return 0;
107
108
0
err_gpio_c1:
109
0
  gpio_free(led->gpio_c1);
110
0
err_gpio_c0:
111
0
  gpio_free(led->gpio_c0);
112
0
  return ret;
113
0
}
114
115
/**
116
 * led_gpio_bicolor_unregister - remove a gpio controlled bicolor LED from the framework
117
 * @param led The gpio LED
118
 */
119
void led_gpio_bicolor_unregister(struct gpio_bicolor_led *led)
120
0
{
121
0
  led_unregister(&led->led);
122
0
  gpio_free(led->gpio_c1);
123
0
  gpio_free(led->gpio_c0);
124
0
}
125
#endif
126
127
#ifdef CONFIG_LED_GPIO_RGB
128
129
static void led_gpio_rgb_set(struct led *led, unsigned int value)
130
0
{
131
0
  struct gpio_rgb_led *rgb = container_of(led, struct gpio_rgb_led, led);
132
0
  int al = rgb->active_low;
133
134
0
  gpio_direction_output(rgb->gpio_r, !!(value & 4) ^ al);
135
0
  gpio_direction_output(rgb->gpio_g, !!(value & 2) ^ al);
136
0
  gpio_direction_output(rgb->gpio_b, !!(value & 1) ^ al);
137
0
}
138
139
/**
140
 * led_gpio_rgb_register - register three gpios as a rgb LED
141
 * @param led The gpio rg LED
142
 *
143
 * This function registers three gpios as a rgb LED. led->gpio[rgb]
144
 * should be initialized to the gpios to control.
145
 */
146
int led_gpio_rgb_register(struct gpio_rgb_led *led)
147
0
{
148
0
  int ret;
149
0
  const char *name = led->led.name;
150
151
0
  ret = gpio_request(led->gpio_r, name ? name : "led_r");
152
0
  if (ret)
153
0
    return ret;
154
155
0
  ret = gpio_request(led->gpio_g, name ? name : "led_g");
156
0
  if (ret)
157
0
    goto err_gpio_r;
158
159
0
  ret = gpio_request(led->gpio_b, name ? name : "led_b");
160
0
  if (ret)
161
0
    goto err_gpio_g;
162
163
0
  led->led.set = led_gpio_rgb_set;
164
0
  led->led.max_value = 7;
165
166
0
  ret = led_register(&led->led);
167
0
  if (ret)
168
0
    goto err_gpio_b;
169
170
0
  return 0;
171
172
0
err_gpio_b:
173
0
  gpio_free(led->gpio_b);
174
0
err_gpio_g:
175
0
  gpio_free(led->gpio_g);
176
0
err_gpio_r:
177
0
  gpio_free(led->gpio_r);
178
0
  return ret;
179
0
}
180
181
/**
182
 * led_gpio_rgb_unregister - remove a gpio controlled rgb LED from the framework
183
 * @param led The gpio LED
184
 */
185
void led_gpio_rgb_unregister(struct gpio_rgb_led *led)
186
0
{
187
0
  led_unregister(&led->led);
188
0
  gpio_free(led->gpio_b);
189
0
  gpio_free(led->gpio_g);
190
0
  gpio_free(led->gpio_r);
191
0
}
192
#endif /* CONFIG_LED_GPIO_RGB */
193
194
#ifdef CONFIG_LED_GPIO_OF
195
static int led_gpio_of_probe(struct device *dev)
196
0
{
197
0
  struct device_node *child;
198
0
  struct gpio_led *leds;
199
0
  int num_leds;
200
0
  int ret = 0, n = 0;
201
202
0
  num_leds = of_get_child_count(dev->of_node);
203
0
  if (num_leds <= 0)
204
0
    return num_leds;
205
206
0
  leds = xzalloc(num_leds * sizeof(struct gpio_led));
207
208
0
  for_each_child_of_node(dev->of_node, child) {
209
0
    struct gpio_led *gled = &leds[n];
210
0
    const char *default_state;
211
0
    enum of_gpio_flags flags;
212
0
    int gpio;
213
0
    const char *label;
214
215
0
    gpio = of_get_named_gpio_flags(child, "gpios", 0, &flags);
216
0
    if (gpio < 0) {
217
0
      ret = dev_err_probe(dev, gpio, "getting gpio for %pOF\n", child);
218
0
      goto err;
219
0
    }
220
221
0
    if (of_property_read_string(child, "label", &label))
222
0
      label = child->name;
223
0
    gled->led.name = xstrdup(label);
224
225
0
    gled->gpio = gpio;
226
0
    gled->active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
227
228
0
    dev_dbg(dev, "register led %s on gpio%d, active_low = %d\n",
229
0
      gled->led.name, gled->gpio, gled->active_low);
230
231
0
    led_gpio_register(gled);
232
0
    led_of_parse_trigger(&gled->led, child);
233
234
0
    if (!of_property_read_string(child, "default-state", &default_state)) {
235
0
      if (!strcmp(default_state, "on"))
236
0
        led_gpio_set(&gled->led, 1);
237
0
      else if (!strcmp(default_state, "off"))
238
0
        led_gpio_set(&gled->led, 0);
239
0
    }
240
241
0
    n++;
242
0
  }
243
244
0
  return 0;
245
246
0
err:
247
0
  for (n = n - 1; n >= 0; n--)
248
0
    led_gpio_unregister(&leds[n]);
249
0
  free(leds);
250
0
  return ret;
251
0
}
252
253
static struct of_device_id led_gpio_of_ids[] = {
254
  { .compatible = "gpio-leds", },
255
  { }
256
};
257
MODULE_DEVICE_TABLE(of, led_gpio_of_ids);
258
259
static struct driver led_gpio_of_driver = {
260
  .name  = "gpio-leds",
261
  .probe = led_gpio_of_probe,
262
  .of_compatible = DRV_OF_COMPAT(led_gpio_of_ids),
263
};
264
device_platform_driver(led_gpio_of_driver);
265
266
#endif /* CONFIG LED_GPIO_OF */