blob: 99f93c5ef86d5afc22dedd975251260d307a5593 [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>
12
Simon Glass3ba929a2020-10-30 21:38:53 -060013struct udevice;
14
Marek Vasut03cadb62022-04-04 01:23:27 +020015enum led_state_t {
16 LEDST_OFF = 0,
17 LEDST_ON = 1,
18 LEDST_TOGGLE,
Marek Vasut03cadb62022-04-04 01:23:27 +020019 LEDST_BLINK,
Marek Vasut03cadb62022-04-04 01:23:27 +020020
21 LEDST_COUNT,
22};
23
Michael Polyntsov353deb92024-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
Simon Glasscce3aed2015-06-23 15:38:45 -060038/**
Simon Glassda01de92017-04-10 11:34:53 -060039 * struct led_uc_plat - Platform data the uclass stores about each device
Simon Glasscce3aed2015-06-23 15:38:45 -060040 *
41 * @label: LED label
Marek Vasut03cadb62022-04-04 01:23:27 +020042 * @default_state: LED default state
Simon Glasscce3aed2015-06-23 15:38:45 -060043 */
Simon Glassda01de92017-04-10 11:34:53 -060044struct led_uc_plat {
Simon Glasscce3aed2015-06-23 15:38:45 -060045 const char *label;
Marek Vasut03cadb62022-04-04 01:23:27 +020046 enum led_state_t default_state;
Michael Polyntsov353deb92024-07-19 13:12:12 +040047#ifdef CONFIG_LED_SW_BLINK
48 struct led_sw_blink *sw_blink;
49#endif
Simon Glasscce3aed2015-06-23 15:38:45 -060050};
51
Simon Glass3bd0c462017-04-10 11:34:57 -060052/**
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 Glasscce3aed2015-06-23 15:38:45 -060061struct led_ops {
62 /**
Simon Glass6ca19772017-04-10 11:34:54 -060063 * set_state() - set the state of an LED
Simon Glasscce3aed2015-06-23 15:38:45 -060064 *
65 * @dev: LED device to change
Simon Glass6ca19772017-04-10 11:34:54 -060066 * @state: LED state to set
Simon Glasscce3aed2015-06-23 15:38:45 -060067 * @return 0 if OK, -ve on error
68 */
Simon Glass6ca19772017-04-10 11:34:54 -060069 int (*set_state)(struct udevice *dev, enum led_state_t state);
Simon Glassdc531662017-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 Glass3bd0c462017-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 Glasscce3aed2015-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 Schuchardt47b4c022022-01-19 18:05:50 +0100101 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glasscce3aed2015-06-23 15:38:45 -0600102 */
103int led_get_by_label(const char *label, struct udevice **devp);
104
105/**
Simon Glass6ca19772017-04-10 11:34:54 -0600106 * led_set_state() - set the state of an LED
Simon Glasscce3aed2015-06-23 15:38:45 -0600107 *
108 * @dev: LED device to change
Simon Glass6ca19772017-04-10 11:34:54 -0600109 * @state: LED state to set
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100110 * Return: 0 if OK, -ve on error
Simon Glasscce3aed2015-06-23 15:38:45 -0600111 */
Simon Glass6ca19772017-04-10 11:34:54 -0600112int led_set_state(struct udevice *dev, enum led_state_t state);
Simon Glasscce3aed2015-06-23 15:38:45 -0600113
Simon Glassdc531662017-04-10 11:34:55 -0600114/**
115 * led_get_state() - get the state of an LED
116 *
117 * @dev: LED device to change
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100118 * Return: LED state led_state_t, or -ve on error
Simon Glassdc531662017-04-10 11:34:55 -0600119 */
120enum led_state_t led_get_state(struct udevice *dev);
121
Simon Glass3bd0c462017-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 Schuchardt47b4c022022-01-19 18:05:50 +0100127 * Return: 0 if OK, -ve on error
Simon Glass3bd0c462017-04-10 11:34:57 -0600128 */
129int led_set_period(struct udevice *dev, int period_ms);
130
Rasmus Villemoes49ba9f02023-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 Polyntsov353deb92024-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 Glasscce3aed2015-06-23 15:38:45 -0600144#endif