blob: 19d92d6441ac78424f2b2735a80da8901dd3e9ab [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glasscce3aed2015-06-23 15:38:45 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glasscce3aed2015-06-23 15:38:45 -06005 */
6
7#ifndef __LED_H
8#define __LED_H
9
Tom Rinic88e90a2024-07-30 12:36:22 -060010#include <stdbool.h>
11#include <cyclic.h>
Christian Marangiea7387e2024-10-01 14:24:36 +020012#include <dm/ofnode.h>
Tom Rinic88e90a2024-07-30 12:36:22 -060013
Simon Glass3ba929a2020-10-30 21:38:53 -060014struct udevice;
15
Marek Vasut03cadb62022-04-04 01:23:27 +020016enum led_state_t {
17 LEDST_OFF = 0,
18 LEDST_ON = 1,
19 LEDST_TOGGLE,
Marek Vasut03cadb62022-04-04 01:23:27 +020020 LEDST_BLINK,
Marek Vasut03cadb62022-04-04 01:23:27 +020021
22 LEDST_COUNT,
23};
24
Michael Polyntsov353deb92024-07-19 13:12:12 +040025enum led_sw_blink_state_t {
26 LED_SW_BLINK_ST_DISABLED,
27 LED_SW_BLINK_ST_NOT_READY,
28 LED_SW_BLINK_ST_OFF,
29 LED_SW_BLINK_ST_ON,
30};
31
32struct led_sw_blink {
33 enum led_sw_blink_state_t state;
34 struct udevice *dev;
35 struct cyclic_info cyclic;
36 const char cyclic_name[0];
37};
38
Simon Glasscce3aed2015-06-23 15:38:45 -060039/**
Simon Glassda01de92017-04-10 11:34:53 -060040 * struct led_uc_plat - Platform data the uclass stores about each device
Simon Glasscce3aed2015-06-23 15:38:45 -060041 *
42 * @label: LED label
Marek Vasut03cadb62022-04-04 01:23:27 +020043 * @default_state: LED default state
Simon Glasscce3aed2015-06-23 15:38:45 -060044 */
Simon Glassda01de92017-04-10 11:34:53 -060045struct led_uc_plat {
Simon Glasscce3aed2015-06-23 15:38:45 -060046 const char *label;
Marek Vasut03cadb62022-04-04 01:23:27 +020047 enum led_state_t default_state;
Michael Polyntsov353deb92024-07-19 13:12:12 +040048#ifdef CONFIG_LED_SW_BLINK
49 struct led_sw_blink *sw_blink;
50#endif
Simon Glasscce3aed2015-06-23 15:38:45 -060051};
52
Simon Glass3bd0c462017-04-10 11:34:57 -060053/**
54 * struct led_uc_priv - Private data the uclass stores about each device
55 *
Christian Marangiea7387e2024-10-01 14:24:36 +020056 * @boot_led_label: Boot LED label
Christian Marangi8be34e82024-10-01 14:24:38 +020057 * @activity_led_label: Activity LED label
Christian Marangiea7387e2024-10-01 14:24:36 +020058 * @boot_led_dev: Boot LED dev
Christian Marangi8be34e82024-10-01 14:24:38 +020059 * @activity_led_dev: Activity LED dev
Christian Marangiea7387e2024-10-01 14:24:36 +020060 * @boot_led_period: Boot LED blink period
Christian Marangi8be34e82024-10-01 14:24:38 +020061 * @activity_led_period: Activity LED blink period
Simon Glass3bd0c462017-04-10 11:34:57 -060062 */
63struct led_uc_priv {
Christian Marangiea7387e2024-10-01 14:24:36 +020064#ifdef CONFIG_LED_BOOT
65 const char *boot_led_label;
66 int boot_led_period;
67#endif
Christian Marangi8be34e82024-10-01 14:24:38 +020068#ifdef CONFIG_LED_ACTIVITY
69 const char *activity_led_label;
70 int activity_led_period;
71#endif
Simon Glass3bd0c462017-04-10 11:34:57 -060072};
73
Simon Glasscce3aed2015-06-23 15:38:45 -060074struct led_ops {
75 /**
Simon Glass6ca19772017-04-10 11:34:54 -060076 * set_state() - set the state of an LED
Simon Glasscce3aed2015-06-23 15:38:45 -060077 *
78 * @dev: LED device to change
Simon Glass6ca19772017-04-10 11:34:54 -060079 * @state: LED state to set
Simon Glasscce3aed2015-06-23 15:38:45 -060080 * @return 0 if OK, -ve on error
81 */
Simon Glass6ca19772017-04-10 11:34:54 -060082 int (*set_state)(struct udevice *dev, enum led_state_t state);
Simon Glassdc531662017-04-10 11:34:55 -060083
84 /**
85 * led_get_state() - get the state of an LED
86 *
87 * @dev: LED device to change
88 * @return LED state led_state_t, or -ve on error
89 */
90 enum led_state_t (*get_state)(struct udevice *dev);
Simon Glass3bd0c462017-04-10 11:34:57 -060091
92#ifdef CONFIG_LED_BLINK
93 /**
94 * led_set_period() - set the blink period of an LED
95 *
96 * Thie records the period if supported, or returns -ENOSYS if not.
97 * To start the LED blinking, use set_state().
98 *
99 * @dev: LED device to change
100 * @period_ms: LED blink period in milliseconds
101 * @return 0 if OK, -ve on error
102 */
103 int (*set_period)(struct udevice *dev, int period_ms);
104#endif
Simon Glasscce3aed2015-06-23 15:38:45 -0600105};
106
107#define led_get_ops(dev) ((struct led_ops *)(dev)->driver->ops)
108
109/**
110 * led_get_by_label() - Find an LED device by label
111 *
112 * @label: LED label to look up
113 * @devp: Returns the associated device, if found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100114 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glasscce3aed2015-06-23 15:38:45 -0600115 */
116int led_get_by_label(const char *label, struct udevice **devp);
117
118/**
Simon Glass6ca19772017-04-10 11:34:54 -0600119 * led_set_state() - set the state of an LED
Simon Glasscce3aed2015-06-23 15:38:45 -0600120 *
121 * @dev: LED device to change
Simon Glass6ca19772017-04-10 11:34:54 -0600122 * @state: LED state to set
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100123 * Return: 0 if OK, -ve on error
Simon Glasscce3aed2015-06-23 15:38:45 -0600124 */
Simon Glass6ca19772017-04-10 11:34:54 -0600125int led_set_state(struct udevice *dev, enum led_state_t state);
Simon Glasscce3aed2015-06-23 15:38:45 -0600126
Simon Glassdc531662017-04-10 11:34:55 -0600127/**
128 * led_get_state() - get the state of an LED
129 *
130 * @dev: LED device to change
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100131 * Return: LED state led_state_t, or -ve on error
Simon Glassdc531662017-04-10 11:34:55 -0600132 */
133enum led_state_t led_get_state(struct udevice *dev);
134
Simon Glass3bd0c462017-04-10 11:34:57 -0600135/**
136 * led_set_period() - set the blink period of an LED
137 *
138 * @dev: LED device to change
139 * @period_ms: LED blink period in milliseconds
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100140 * Return: 0 if OK, -ve on error
Simon Glass3bd0c462017-04-10 11:34:57 -0600141 */
142int led_set_period(struct udevice *dev, int period_ms);
143
Rasmus Villemoes49ba9f02023-11-17 12:38:08 +0100144/**
145 * led_bind_generic() - bind children of parent to given driver
146 *
147 * @parent: Top-level LED device
148 * @driver_name: Driver for handling individual child nodes
149 */
150int led_bind_generic(struct udevice *parent, const char *driver_name);
151
Michael Polyntsov353deb92024-07-19 13:12:12 +0400152/* Internal functions for software blinking. Do not use them in your code */
153int led_sw_set_period(struct udevice *dev, int period_ms);
154bool led_sw_is_blinking(struct udevice *dev);
155bool led_sw_on_state_change(struct udevice *dev, enum led_state_t state);
156
Christian Marangiea7387e2024-10-01 14:24:36 +0200157#ifdef CONFIG_LED_BOOT
158
159/**
160 * led_boot_on() - turn ON the designated LED for booting
161 *
162 * Return: 0 if OK, -ve on error
163 */
164int led_boot_on(void);
165
166/**
167 * led_boot_off() - turn OFF the designated LED for booting
168 *
169 * Return: 0 if OK, -ve on error
170 */
171int led_boot_off(void);
172
173#if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK)
174/**
175 * led_boot_blink() - turn ON the designated LED for booting
176 *
177 * Return: 0 if OK, -ve on error
178 */
179int led_boot_blink(void);
180
181#else
182/* If LED BLINK is not supported/enabled, fallback to LED ON */
183#define led_boot_blink led_boot_on
184#endif
185#else
186static inline int led_boot_on(void)
187{
188 return -ENOSYS;
189}
190
191static inline int led_boot_off(void)
192{
193 return -ENOSYS;
194}
195
196static inline int led_boot_blink(void)
197{
198 return -ENOSYS;
199}
200#endif
201
Christian Marangi8be34e82024-10-01 14:24:38 +0200202#ifdef CONFIG_LED_ACTIVITY
203
204/**
205 * led_activity_on() - turn ON the designated LED for activity
206 *
207 * Return: 0 if OK, -ve on error
208 */
209int led_activity_on(void);
210
211/**
212 * led_activity_off() - turn OFF the designated LED for activity
213 *
214 * Return: 0 if OK, -ve on error
215 */
216int led_activity_off(void);
217
218#if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK)
219/**
220 * led_activity_blink() - turn ON the designated LED for activity
221 *
222 * Return: 0 if OK, -ve on error
223 */
224int led_activity_blink(void);
225#else
226/* If LED BLINK is not supported/enabled, fallback to LED ON */
227#define led_activity_blink led_activity_on
228#endif
229#else
230static inline int led_activity_on(void)
231{
232 return -ENOSYS;
233}
234
235static inline int led_activity_off(void)
236{
237 return -ENOSYS;
238}
239
240static inline int led_activity_blink(void)
241{
242 return -ENOSYS;
243}
244#endif
245
Simon Glasscce3aed2015-06-23 15:38:45 -0600246#endif