1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #ifndef __LED_H
8 #define __LED_H
9 
10 struct udevice;
11 
12 /**
13  * struct led_uc_plat - Platform data the uclass stores about each device
14  *
15  * @label:	LED label
16  */
17 struct led_uc_plat {
18 	const char *label;
19 };
20 
21 /**
22  * struct led_uc_priv - Private data the uclass stores about each device
23  *
24  * @period_ms:	Flash period in milliseconds
25  */
26 struct led_uc_priv {
27 	int period_ms;
28 };
29 
30 enum led_state_t {
31 	LEDST_OFF = 0,
32 	LEDST_ON = 1,
33 	LEDST_TOGGLE,
34 #ifdef CONFIG_LED_BLINK
35 	LEDST_BLINK,
36 #endif
37 
38 	LEDST_COUNT,
39 };
40 
41 struct led_ops {
42 	/**
43 	 * set_state() - set the state of an LED
44 	 *
45 	 * @dev:	LED device to change
46 	 * @state:	LED state to set
47 	 * @return 0 if OK, -ve on error
48 	 */
49 	int (*set_state)(struct udevice *dev, enum led_state_t state);
50 
51 	/**
52 	 * led_get_state() - get the state of an LED
53 	 *
54 	 * @dev:	LED device to change
55 	 * @return LED state led_state_t, or -ve on error
56 	 */
57 	enum led_state_t (*get_state)(struct udevice *dev);
58 
59 #ifdef CONFIG_LED_BLINK
60 	/**
61 	 * led_set_period() - set the blink period of an LED
62 	 *
63 	 * Thie records the period if supported, or returns -ENOSYS if not.
64 	 * To start the LED blinking, use set_state().
65 	 *
66 	 * @dev:	LED device to change
67 	 * @period_ms:	LED blink period in milliseconds
68 	 * @return 0 if OK, -ve on error
69 	 */
70 	int (*set_period)(struct udevice *dev, int period_ms);
71 #endif
72 };
73 
74 #define led_get_ops(dev)	((struct led_ops *)(dev)->driver->ops)
75 
76 /**
77  * led_get_by_label() - Find an LED device by label
78  *
79  * @label:	LED label to look up
80  * @devp:	Returns the associated device, if found
81  * @return 0 if found, -ENODEV if not found, other -ve on error
82  */
83 int led_get_by_label(const char *label, struct udevice **devp);
84 
85 /**
86  * led_set_state() - set the state of an LED
87  *
88  * @dev:	LED device to change
89  * @state:	LED state to set
90  * @return 0 if OK, -ve on error
91  */
92 int led_set_state(struct udevice *dev, enum led_state_t state);
93 
94 /**
95  * led_get_state() - get the state of an LED
96  *
97  * @dev:	LED device to change
98  * @return LED state led_state_t, or -ve on error
99  */
100 enum led_state_t led_get_state(struct udevice *dev);
101 
102 /**
103  * led_set_period() - set the blink period of an LED
104  *
105  * @dev:	LED device to change
106  * @period_ms:	LED blink period in milliseconds
107  * @return 0 if OK, -ve on error
108  */
109 int led_set_period(struct udevice *dev, int period_ms);
110 
111 /**
112  * led_default_state() - set the default state for all the LED
113  *
114  * This enables all leds which have default state.
115  * see Documentation/devicetree/bindings/leds/common.txt
116  *
117  */
118 int led_default_state(void);
119 
120 #endif
121