blob: 99f93c5ef86d5afc22dedd975251260d307a5593 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass59171122015-06-23 15:38:45 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass59171122015-06-23 15:38:45 -06005 */
6
7#ifndef __LED_H
8#define __LED_H
9
Tom Rini8877bc52024-07-30 12:36:22 -060010#include <stdbool.h>
11#include <cyclic.h>
12
Simon Glass401d1c42020-10-30 21:38:53 -060013struct udevice;
14
Simon Glassddae9fc2017-04-10 11:34:54 -060015enum led_state_t {
16 LEDST_OFF = 0,
17 LEDST_ON = 1,
Simon Glass9413ad42017-04-10 11:34:56 -060018 LEDST_TOGGLE,
Simon Glass53378da2017-04-10 11:34:57 -060019 LEDST_BLINK,
Simon Glassddae9fc2017-04-10 11:34:54 -060020
21 LEDST_COUNT,
22};
23
Michael Polyntsovb557f552024-07-19 13:12:12 +040024enum led_sw_blink_state_t {
25 LED_SW_BLINK_ST_DISABLED,
26 LED_SW_BLINK_ST_NOT_READY,
27 LED_SW_BLINK_ST_OFF,
28 LED_SW_BLINK_ST_ON,
29};
30
31struct led_sw_blink {
32 enum led_sw_blink_state_t state;
33 struct udevice *dev;
34 struct cyclic_info cyclic;
35 const char cyclic_name[0];
36};
37
Marek Vasut72675b02022-04-04 01:23:27 +020038/**
39 * struct led_uc_plat - Platform data the uclass stores about each device
40 *
41 * @label: LED label
42 * @default_state: LED default state
43 */
44struct led_uc_plat {
45 const char *label;
46 enum led_state_t default_state;
Michael Polyntsovb557f552024-07-19 13:12:12 +040047#ifdef CONFIG_LED_SW_BLINK
48 struct led_sw_blink *sw_blink;
49#endif
Marek Vasut72675b02022-04-04 01:23:27 +020050};
51
52/**
53 * struct led_uc_priv - Private data the uclass stores about each device
54 *
55 * @period_ms: Flash period in milliseconds
56 */
57struct led_uc_priv {
58 int period_ms;
59};
60
Simon Glass59171122015-06-23 15:38:45 -060061struct led_ops {
62 /**
Simon Glassddae9fc2017-04-10 11:34:54 -060063 * set_state() - set the state of an LED
Simon Glass59171122015-06-23 15:38:45 -060064 *
65 * @dev: LED device to change
Simon Glassddae9fc2017-04-10 11:34:54 -060066 * @state: LED state to set
Simon Glass59171122015-06-23 15:38:45 -060067 * @return 0 if OK, -ve on error
68 */
Simon Glassddae9fc2017-04-10 11:34:54 -060069 int (*set_state)(struct udevice *dev, enum led_state_t state);
Simon Glass8f4b6122017-04-10 11:34:55 -060070
71 /**
72 * led_get_state() - get the state of an LED
73 *
74 * @dev: LED device to change
75 * @return LED state led_state_t, or -ve on error
76 */
77 enum led_state_t (*get_state)(struct udevice *dev);
Simon Glass53378da2017-04-10 11:34:57 -060078
79#ifdef CONFIG_LED_BLINK
80 /**
81 * led_set_period() - set the blink period of an LED
82 *
83 * Thie records the period if supported, or returns -ENOSYS if not.
84 * To start the LED blinking, use set_state().
85 *
86 * @dev: LED device to change
87 * @period_ms: LED blink period in milliseconds
88 * @return 0 if OK, -ve on error
89 */
90 int (*set_period)(struct udevice *dev, int period_ms);
91#endif
Simon Glass59171122015-06-23 15:38:45 -060092};
93
94#define led_get_ops(dev) ((struct led_ops *)(dev)->driver->ops)
95
96/**
97 * led_get_by_label() - Find an LED device by label
98 *
99 * @label: LED label to look up
100 * @devp: Returns the associated device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100101 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glass59171122015-06-23 15:38:45 -0600102 */
103int led_get_by_label(const char *label, struct udevice **devp);
104
105/**
Simon Glassddae9fc2017-04-10 11:34:54 -0600106 * led_set_state() - set the state of an LED
Simon Glass59171122015-06-23 15:38:45 -0600107 *
108 * @dev: LED device to change
Simon Glassddae9fc2017-04-10 11:34:54 -0600109 * @state: LED state to set
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100110 * Return: 0 if OK, -ve on error
Simon Glass59171122015-06-23 15:38:45 -0600111 */
Simon Glassddae9fc2017-04-10 11:34:54 -0600112int led_set_state(struct udevice *dev, enum led_state_t state);
Simon Glass59171122015-06-23 15:38:45 -0600113
Simon Glass8f4b6122017-04-10 11:34:55 -0600114/**
115 * led_get_state() - get the state of an LED
116 *
117 * @dev: LED device to change
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100118 * Return: LED state led_state_t, or -ve on error
Simon Glass8f4b6122017-04-10 11:34:55 -0600119 */
120enum led_state_t led_get_state(struct udevice *dev);
121
Simon Glass53378da2017-04-10 11:34:57 -0600122/**
123 * led_set_period() - set the blink period of an LED
124 *
125 * @dev: LED device to change
126 * @period_ms: LED blink period in milliseconds
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100127 * Return: 0 if OK, -ve on error
Simon Glass53378da2017-04-10 11:34:57 -0600128 */
129int led_set_period(struct udevice *dev, int period_ms);
130
Rasmus Villemoes3bf05152023-11-17 12:38:08 +0100131/**
132 * led_bind_generic() - bind children of parent to given driver
133 *
134 * @parent: Top-level LED device
135 * @driver_name: Driver for handling individual child nodes
136 */
137int led_bind_generic(struct udevice *parent, const char *driver_name);
138
Michael Polyntsovb557f552024-07-19 13:12:12 +0400139/* Internal functions for software blinking. Do not use them in your code */
140int led_sw_set_period(struct udevice *dev, int period_ms);
141bool led_sw_is_blinking(struct udevice *dev);
142bool led_sw_on_state_change(struct udevice *dev, enum led_state_t state);
143
Simon Glass59171122015-06-23 15:38:45 -0600144#endif