Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __LED_H |
| 8 | #define __LED_H |
| 9 | |
Tom Rini | c88e90a | 2024-07-30 12:36:22 -0600 | [diff] [blame] | 10 | #include <stdbool.h> |
| 11 | #include <cyclic.h> |
| 12 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | struct udevice; |
| 14 | |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 15 | enum led_state_t { |
| 16 | LEDST_OFF = 0, |
| 17 | LEDST_ON = 1, |
| 18 | LEDST_TOGGLE, |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 19 | LEDST_BLINK, |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 20 | |
| 21 | LEDST_COUNT, |
| 22 | }; |
| 23 | |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 24 | enum 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 | |
| 31 | struct 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 Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 38 | /** |
Simon Glass | da01de9 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 39 | * struct led_uc_plat - Platform data the uclass stores about each device |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 40 | * |
| 41 | * @label: LED label |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 42 | * @default_state: LED default state |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 43 | */ |
Simon Glass | da01de9 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 44 | struct led_uc_plat { |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 45 | const char *label; |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 46 | enum led_state_t default_state; |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 47 | #ifdef CONFIG_LED_SW_BLINK |
| 48 | struct led_sw_blink *sw_blink; |
| 49 | #endif |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 50 | }; |
| 51 | |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 52 | /** |
| 53 | * struct led_uc_priv - Private data the uclass stores about each device |
| 54 | * |
| 55 | * @period_ms: Flash period in milliseconds |
| 56 | */ |
| 57 | struct led_uc_priv { |
| 58 | int period_ms; |
| 59 | }; |
| 60 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 61 | struct led_ops { |
| 62 | /** |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 63 | * set_state() - set the state of an LED |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 64 | * |
| 65 | * @dev: LED device to change |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 66 | * @state: LED state to set |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 67 | * @return 0 if OK, -ve on error |
| 68 | */ |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 69 | int (*set_state)(struct udevice *dev, enum led_state_t state); |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 70 | |
| 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 Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 78 | |
| 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 Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 92 | }; |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 101 | * Return: 0 if found, -ENODEV if not found, other -ve on error |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 102 | */ |
| 103 | int led_get_by_label(const char *label, struct udevice **devp); |
| 104 | |
| 105 | /** |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 106 | * led_set_state() - set the state of an LED |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 107 | * |
| 108 | * @dev: LED device to change |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 109 | * @state: LED state to set |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 110 | * Return: 0 if OK, -ve on error |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 111 | */ |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 112 | int led_set_state(struct udevice *dev, enum led_state_t state); |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 113 | |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 114 | /** |
| 115 | * led_get_state() - get the state of an LED |
| 116 | * |
| 117 | * @dev: LED device to change |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 118 | * Return: LED state led_state_t, or -ve on error |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 119 | */ |
| 120 | enum led_state_t led_get_state(struct udevice *dev); |
| 121 | |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 122 | /** |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 127 | * Return: 0 if OK, -ve on error |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 128 | */ |
| 129 | int led_set_period(struct udevice *dev, int period_ms); |
| 130 | |
Rasmus Villemoes | 49ba9f0 | 2023-11-17 12:38:08 +0100 | [diff] [blame] | 131 | /** |
| 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 | */ |
| 137 | int led_bind_generic(struct udevice *parent, const char *driver_name); |
| 138 | |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 139 | /* Internal functions for software blinking. Do not use them in your code */ |
| 140 | int led_sw_set_period(struct udevice *dev, int period_ms); |
| 141 | bool led_sw_is_blinking(struct udevice *dev); |
| 142 | bool led_sw_on_state_change(struct udevice *dev, enum led_state_t state); |
| 143 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 144 | #endif |