summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/led_driver/led.h
blob: b8cedf5af7a2ee0ef139bf180833716a42ee1830 (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
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef __CROS_EC_LED_H__
#define __CROS_EC_LED_H__

#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/pwm.h>

#define COMPAT_GPIO_LED cros_ec_gpio_led_pins
#define COMPAT_PWM_LED cros_ec_pwm_led_pins

#define PINS_NODE(id) DT_CAT(PIN_NODE_, id)
#define PINS_ARRAY(id) DT_CAT(PINS_ARRAY_, id)

/*
 * Return string-token if the property exists, otherwise return 0
 */
#define GET_PROP(id, prop)                      \
	COND_CODE_1(DT_NODE_HAS_PROP(id, prop), \
		    (DT_STRING_UPPER_TOKEN(id, prop)), (0))

/*
 * Return string-token if the property exists, otherwise return -1
 */
#define GET_PROP_NVE(id, prop)                  \
	COND_CODE_1(DT_NODE_HAS_PROP(id, prop), \
		    (DT_STRING_UPPER_TOKEN(id, prop)), (-1))

#define LED_ENUM(id, enum_name) DT_STRING_TOKEN(id, enum_name)
#define LED_ENUM_WITH_COMMA(id, enum_name)           \
	COND_CODE_1(DT_NODE_HAS_PROP(id, enum_name), \
		    (LED_ENUM(id, enum_name), ), ())

#define GPIO_LED_PINS_NODE DT_PATH(gpio_led_pins)
#define PWM_LED_PINS_NODE DT_PATH(pwm_led_pins)

enum led_color {
	LED_OFF,
	LED_RED,
	LED_GREEN,
	LED_BLUE,
	LED_YELLOW,
	LED_WHITE,
	LED_AMBER,
	LED_COLOR_COUNT /* Number of colors, not a color itself */
};

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

/*
 * Struct defining LED PWM pin and duty cycle to set.
 */
struct pwm_pin_t {
	struct pwm_dt_spec pwm;
	uint32_t pulse_ns; /* PWM Duty cycle ns */
};

/*
 * Pin node contains LED color and array of gpio/pwm pins
 * to alter in order to enable the given color.
 */
struct led_pins_node_t {
	/*
	 * Link between color and pins node. Only used to support
	 * ectool functionality.
	 */
	int led_color;

	/*
	 * Link between color and pins node. Only used to support
	 * ectool functionality.
	 */
	enum ec_led_id led_id;

	/* Brightness Range color, only used to support ectool functionality */
	enum ec_led_colors br_color;

#if DT_HAS_COMPAT_STATUS_OKAY(COMPAT_GPIO_LED)
	/* Array of GPIO pins to set to enable particular color */
	struct gpio_pin_t *gpio_pins;
#endif

#if DT_HAS_COMPAT_STATUS_OKAY(COMPAT_PWM_LED)
	/* Array of PWM pins to set to enable particular color */
	struct pwm_pin_t *pwm_pins;
#endif

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

/**
 * Set LED color using color enum
 *
 * @param color		LED Color to enable
 * @param led_id	LED ID to set the color for
 */
void led_set_color(enum led_color color, enum ec_led_id led_id);

/**
 * Set LED color using pins node
 *
 * @param *pins_node	Pins node to enable the color corresponding
 *			to the node.
 */
void led_set_color_with_node(const struct led_pins_node_t *pins_node);

#ifdef TEST_BUILD
const struct led_pins_node_t *led_get_node(enum led_color color,
					   enum ec_led_id led_id);

enum power_state get_chipset_state(void);
#endif /* TEST_BUILD */

#endif /* __CROS_EC_LED_H__ */