summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/led_driver/led_gpio.c
blob: f4a2058f6c480674f725df4e9198fdb0ace6ebb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Copyright 2022 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * GPIO LED control.
 */

#include "ec_commands.h"
#include "led.h"
#include "util.h"

#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/logging/log.h>

#if DT_HAS_COMPAT_STATUS_OKAY(COMPAT_GPIO_LED)

LOG_MODULE_REGISTER(gpio_led, LOG_LEVEL_ERR);

/*
 * Struct defining LED GPIO pin and value to set.
 */
struct gpio_pin_t {
	enum gpio_signal signal;
	int val;
};

/*
 * Pin node contains LED color and array of GPIO pins
 * to alter in order to enable the given color.
 */
struct led_pins_node_t {
	/* Link between color and pins node */
	int led_color;

	/*
	 * Link between color and pins node in case of multiple LEDs
	 * Also required for ectool funcs support
	 */
	enum ec_led_id led_id;

	/* Brightness Range color, only used by ectool funcs for testing */
	enum ec_led_colors br_color;

	/* Array of GPIO pins to set to enable particular color */
	struct gpio_pin_t *gpio_pins;

	/* Number of pins per color */
	uint8_t pins_count;
};

#define SET_PIN(node_id, prop, i)					\
{									\
	.signal = GPIO_SIGNAL(DT_PHANDLE_BY_IDX(node_id, prop, i)),	\
	.val = DT_PHA_BY_IDX(node_id, prop, i, value)			\
},

#define SET_GPIO_PIN(node_id)						\
{									\
	DT_FOREACH_PROP_ELEM(node_id, led_pins, SET_PIN)		\
};

#define GEN_PINS_ARRAY(id)						\
struct gpio_pin_t PINS_ARRAY(id)[] = SET_GPIO_PIN(id)

DT_FOREACH_CHILD(GPIO_LED_PINS_NODE, GEN_PINS_ARRAY)

#define SET_PIN_NODE(node_id)						\
{									\
	.led_color = GET_PROP(node_id, led_color),			\
	.led_id = GET_PROP(node_id, led_id),				\
	.br_color = GET_PROP_NVE(node_id, br_color),			\
	.gpio_pins = PINS_ARRAY(node_id),				\
	.pins_count = DT_PROP_LEN(node_id, led_pins)			\
},

struct led_pins_node_t pins_node[] = {
	DT_FOREACH_CHILD(GPIO_LED_PINS_NODE, SET_PIN_NODE)
};

/*
 * Iterate through LED pins nodes to find the color matching node.
 * Set all the GPIO pins defined in the node to the defined value,
 * to enable the color.
 */
void led_set_color(enum led_color color, enum ec_led_id led_id)
{
	for (int i = 0; i < ARRAY_SIZE(pins_node); i++) {
		if ((pins_node[i].led_color == color) &&
		    (pins_node[i].led_id == led_id)) {
			for (int j = 0; j < pins_node[i].pins_count; j++) {
				gpio_pin_set_dt(gpio_get_dt_spec(
					pins_node[i].gpio_pins[j].signal),
					pins_node[i].gpio_pins[j].val);
			}
			break; /* Found the matching pin node, break here */
		}
	}
}

void led_get_brightness_range(enum ec_led_id led_id, uint8_t *brightness_range)
{
	for (int i = 0; i < ARRAY_SIZE(pins_node); i++) {
		int br_color = pins_node[i].br_color;

		if (br_color != -1)
			brightness_range[br_color] = 1;
	}
}

int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness)
{
	bool color_set = false;

	for (int i = 0; i < ARRAY_SIZE(pins_node); i++) {
		int br_color = pins_node[i].br_color;

		if ((br_color != -1) && (brightness[br_color] != 0)) {
			color_set = true;
			led_set_color(pins_node[i].led_color, led_id);
		}
	}

	/* If no color was set, turn off the LED */
	if (!color_set)
		led_set_color(LED_OFF, led_id);

	return EC_SUCCESS;
}

__override int led_is_supported(enum ec_led_id led_id)
{
	static int supported_leds = -1;

	if (supported_leds == -1) {
		supported_leds = 0;

		for (int i = 0; i < ARRAY_SIZE(pins_node); i++)
			supported_leds |= (1 << pins_node[i].led_id);
	}

	return ((1 << (int)led_id) & supported_leds);
}
#endif /* DT_HAS_COMPAT_STATUS_OKAY(COMPAT_GPIO_LED) */