Some LEDs can be driven by hardware (for example a LED connected to
an ethernet PHY or an ethernet switch can be configured to blink on
activity on the network, which in software is done by the netdev trigger).
To do such offloading, LED driver must support this and a supported
trigger must be used.
LED driver should declare the correct blink_mode supported and should set
the blink_mode parameter to one of LED_BLINK_HW_CONTROLLED or
LED_BLINK_SWHW_CONTROLLED.
The trigger will check this option and fail to activate if the blink_mode
is not supported.
By default if a LED driver doesn't declare blink_mode,
LED_BLINK_SW_CONTROLLED is assumed.
The LED must implement 3 main API:
- hw_control_status():
This asks the LED driver if hardware mode is enabled
or not.
- hw_control_start():
This will simply enable the hardware mode for the LED.
- hw_control_stop():
This will simply disable the hardware mode for the LED.
It's advised to the driver to put the LED in the old state
but this is not enforcerd and putting the LED off is also
accepted.
With LED_BLINK_HW_CONTROLLED blink_mode hw_control_status/start/stop is
optional and any software only trigger will reject activation as the LED
supports only hardware mode.
An additional config CONFIG_LEDS_HARDWARE_CONTROL is added to add support
for LEDs that can be controlled by hardware.
Cc: Marek BehĂșn <kabel@kernel.org>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
Documentation/leds/leds-class.rst | 34 +++++++++++++++++++++++++++
drivers/leds/Kconfig | 11 +++++++++
drivers/leds/led-class.c | 27 +++++++++++++++++++++
drivers/leds/led-triggers.c | 38 ++++++++++++++++++++++++++++++
include/linux/leds.h | 39 ++++++++++++++++++++++++++++++-
5 files changed, 148 insertions(+), 1 deletion(-)
@@ -169,6 +169,40 @@ Setting the brightness to zero with brightness_set() callback function
should completely turn off the LED and cancel the previously programmed
hardware blinking function, if any.
+Hardware driven LEDs
+===================================
+
+Some LEDs can be driven by hardware (for example a LED connected to
+an ethernet PHY or an ethernet switch can be configured to blink on activity on
+the network, which in software is done by the netdev trigger).
+
+To do such offloading, LED driver must support this and a supported trigger must
+be used.
+
+LED driver should declare the correct blink_mode supported and should set the
+blink_mode parameter to one of LED_BLINK_HW_CONTROLLED or LED_BLINK_SWHW_CONTROLLED.
+The trigger will check this option and fail to activate if the blink_mode is not
+supported.
+By default if a LED driver doesn't declare blink_mode, LED_BLINK_SW_CONTROLLED is
+assumed.
+
+The LED must implement 3 main API:
+
+- hw_control_status():
+ This asks the LED driver if hardware mode is enabled
+ or not.
+
+- hw_control_start():
+ This will simply enable the hardware mode for the LED.
+
+- hw_control_stop():
+ This will simply disable the hardware mode for the LED.
+ It's advised to the driver to put the LED in the old state
+ but this is not enforcerd and putting the LED off is also accepted.
+
+With LED_BLINK_HW_CONTROLLED blink_mode hw_control_status/start/stop is optional
+and any software only trigger will reject activation as the LED supports only
+hardware mode.
Known Issues
============
@@ -49,6 +49,17 @@ config LEDS_BRIGHTNESS_HW_CHANGED
See Documentation/ABI/testing/sysfs-class-led for details.
+config LEDS_HARDWARE_CONTROL
+ bool "LED Hardware Control support"
+ help
+ This option enabled Hardware control support used by leds that
+ can be driven in hardware by using supported triggers.
+
+ Hardware blink modes will be exposed by sysfs class in
+ /sys/class/leds based on the trigger currently active.
+
+ If unsure, say Y.
+
comment "LED drivers"
config LEDS_88PM860X
@@ -166,6 +166,27 @@ static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
}
#endif
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+static int led_classdev_check_blink_hw_mode_functions(struct led_classdev *led_cdev)
+{
+ int mode = led_cdev->blink_mode;
+
+ if (mode == LED_BLINK_SWHW_CONTROLLED &&
+ (!led_cdev->hw_control_status ||
+ !led_cdev->hw_control_start ||
+ !led_cdev->hw_control_stop))
+ return -EINVAL;
+
+ if (mode == LED_BLINK_SW_CONTROLLED &&
+ (led_cdev->hw_control_status ||
+ led_cdev->hw_control_start ||
+ led_cdev->hw_control_stop))
+ return -EINVAL;
+
+ return 0;
+}
+#endif
+
/**
* led_classdev_suspend - suspend an led_classdev.
* @led_cdev: the led_classdev to suspend.
@@ -466,6 +487,12 @@ int led_classdev_register_ext(struct device *parent,
if (ret < 0)
return ret;
+#ifdef CONFIG_LEDS_HARDWARE_CONTROL
+ ret = led_classdev_check_blink_hw_mode_functions(led_cdev);
+ if (ret < 0)
+ return ret;
+#endif
+
mutex_init(&led_cdev->led_access);
mutex_lock(&led_cdev->led_access);
led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
@@ -154,6 +154,38 @@ ssize_t led_trigger_read(struct file *filp, struct kobject *kobj,
}
EXPORT_SYMBOL_GPL(led_trigger_read);
+static bool led_trigger_is_supported(struct led_classdev *led_cdev,
+ struct led_trigger *trigger)
+{
+ switch (led_cdev->blink_mode) {
+ case LED_BLINK_SW_CONTROLLED:
+ return trigger->supported_blink_modes != LED_TRIGGER_HW_ONLY;
+
+ case LED_BLINK_HW_CONTROLLED:
+ return trigger->supported_blink_modes != LED_TRIGGER_SW_ONLY;
+
+ case LED_BLINK_SWHW_CONTROLLED:
+ return true;
+ }
+
+ return 1;
+}
+
+static void led_trigger_hw_mode_stop(struct led_classdev *led_cdev)
+{
+ /* check if LED is in HW block mode */
+ if (led_cdev->blink_mode == LED_BLINK_SW_CONTROLLED)
+ return;
+
+ /*
+ * We can assume these function are always present as
+ * for LED support hw blink mode they MUST be provided or register
+ * fail.
+ */
+ if (led_cdev->hw_control_status(led_cdev))
+ led_cdev->hw_control_stop(led_cdev);
+}
+
/* Caller must ensure led_cdev->trigger_lock held */
int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
{
@@ -179,6 +211,8 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
cancel_work_sync(&led_cdev->set_brightness_work);
led_stop_software_blink(led_cdev);
+ /* Disable hardware mode on trigger change if supported */
+ led_trigger_hw_mode_stop(led_cdev);
if (led_cdev->trigger->deactivate)
led_cdev->trigger->deactivate(led_cdev);
device_remove_groups(led_cdev->dev, led_cdev->trigger->groups);
@@ -188,6 +222,10 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
led_set_brightness(led_cdev, LED_OFF);
}
if (trig) {
+ /* Make sure the trigger support the LED blink mode */
+ if (!led_trigger_is_supported(led_cdev, trig))
+ return -EINVAL;
+
spin_lock(&trig->leddev_list_lock);
list_add_tail_rcu(&led_cdev->trig_list, &trig->led_cdevs);
spin_unlock(&trig->leddev_list_lock);
@@ -88,6 +88,12 @@ struct led_hw_trigger_type {
int dummy;
};
+enum led_blink_modes {
+ LED_BLINK_SW_CONTROLLED = 0x0,
+ LED_BLINK_HW_CONTROLLED,
+ LED_BLINK_SWHW_CONTROLLED,
+};
+
struct led_classdev {
const char *name;
unsigned int brightness;
@@ -175,6 +181,24 @@ struct led_classdev {
/* LEDs that have private triggers have this set */
struct led_hw_trigger_type *trigger_type;
+
+ /* This report the supported blink_mode. The driver should report the
+ * correct LED capabilities.
+ * With this set to LED_BLINK_HW_CONTROLLED, LED is always in offload
+ * mode and triggers can't be simulated by software.
+ * If the led is LED_BLINK_HW_CONTROLLED, status/start/stop function
+ * are optional.
+ * By default LED_BLINK_SW_CONTROLLED is set as blink_mode.
+ */
+ enum led_blink_modes blink_mode;
+ /* Ask the LED driver if hardware mode is enabled or not */
+ bool (*hw_control_status)(struct led_classdev *led_cdev);
+ /* Set LED in hardware mode */
+ int (*hw_control_start)(struct led_classdev *led_cdev);
+ /* Disable hardware mode for LED. It's advised to the LED driver to put it to
+ * the old status but that is not mandatory and also putting it off is accepted.
+ */
+ int (*hw_control_stop)(struct led_classdev *led_cdev);
#endif
#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
@@ -242,7 +266,6 @@ extern struct led_classdev *of_led_get(struct device_node *np, int index);
extern void led_put(struct led_classdev *led_cdev);
struct led_classdev *__must_check devm_of_led_get(struct device *dev,
int index);
-
/**
* led_blink_set - set blinking with software fallback
* @led_cdev: the LED to start blinking
@@ -377,12 +400,26 @@ static inline bool led_sysfs_is_disabled(struct led_classdev *led_cdev)
#define TRIG_NAME_MAX 50
+enum led_trigger_blink_supported_modes {
+ LED_TRIGGER_SW_ONLY = LED_BLINK_SW_CONTROLLED,
+ LED_TRIGGER_HW_ONLY = LED_BLINK_HW_CONTROLLED,
+ LED_TRIGGER_SWHW = LED_BLINK_SWHW_CONTROLLED,
+};
+
struct led_trigger {
/* Trigger Properties */
const char *name;
int (*activate)(struct led_classdev *led_cdev);
void (*deactivate)(struct led_classdev *led_cdev);
+ /* Declare if the Trigger supports hardware control to
+ * offload triggers or supports only software control.
+ * A trigger can also declare support for hardware control
+ * if its task is to only configure LED blink modes and expose
+ * them in sysfs.
+ */
+ enum led_trigger_blink_supported_modes supported_blink_modes;
+
/* LED-private triggers have this set */
struct led_hw_trigger_type *trigger_type;