[RFC PATCH v4 5/8] leds: trigger: netdev: add hardware control support

From: Ansuel Smith
Date: Wed Nov 10 2021 - 20:35:41 EST


Add hardware control support for the Netdev trigger.
The trigger on config change will check if the requested trigger can set
to blink mode using LED hardware mode and if every blink mode is supported,
the trigger will enable hardware mode with the requested configuration.
If there is at least one trigger that is not supported and can't run in
hardware mode, then software mode will be used instead.

Signed-off-by: Ansuel Smith <ansuelsmth@xxxxxxxxx>
---
drivers/leds/trigger/ledtrig-netdev.c | 30 +++++++++++++++++++++++++--
include/linux/leds.h | 3 +++
2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/drivers/leds/trigger/ledtrig-netdev.c b/drivers/leds/trigger/ledtrig-netdev.c
index 01e4544fa7b0..74c9a6ecfbbf 100644
--- a/drivers/leds/trigger/ledtrig-netdev.c
+++ b/drivers/leds/trigger/ledtrig-netdev.c
@@ -44,9 +44,31 @@ enum netdev_led_attr {

static void set_baseline_state(struct led_netdev_data *trigger_data)
{
- int current_brightness;
+ int current_brightness, can_offload;
struct led_classdev *led_cdev = trigger_data->led_cdev;

+ if (LED_HARDWARE_CONTROLLED & led_cdev->flags) {
+ /* Check if blink mode can he set in hardware mode.
+ * The LED driver will chose a interval based on the trigger_data
+ * and its implementation.
+ */
+ can_offload = led_cdev->blink_set(led_cdev, 0, 0);
+
+ /* If blink_set doesn't return error we can run in hardware mode
+ * So actually activate it.
+ */
+ if (!can_offload) {
+ led_cdev->hw_control_start(led_cdev);
+ return;
+ }
+ }
+
+ /* If LED supports only hardware mode and we reach this point,
+ * then skip any software handling.
+ */
+ if (!(LED_SOFTWARE_CONTROLLED & led_cdev->flags))
+ return;
+
current_brightness = led_cdev->brightness;
if (current_brightness)
led_cdev->blink_brightness = current_brightness;
@@ -395,8 +417,11 @@ static int netdev_trig_activate(struct led_classdev *led_cdev)

rc = register_netdevice_notifier(&trigger_data->notifier);
if (rc)
- kfree(trigger_data);
+ goto err;

+ return 0;
+err:
+ kfree(trigger_data);
return rc;
}

@@ -416,6 +441,7 @@ static void netdev_trig_deactivate(struct led_classdev *led_cdev)

static struct led_trigger netdev_led_trigger = {
.name = "netdev",
+ .supported_blink_modes = SOFTWARE_HARDWARE,
.activate = netdev_trig_activate,
.deactivate = netdev_trig_deactivate,
.groups = netdev_trig_groups,
diff --git a/include/linux/leds.h b/include/linux/leds.h
index bc3c54eb269a..dd41acd564a3 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -17,6 +17,9 @@
#include <linux/spinlock.h>
#include <linux/timer.h>
#include <linux/workqueue.h>
+#ifdef CONFIG_LEDS_TRIGGER_NETDEV
+#include <linux/netdevice.h>
+#endif

struct device;
struct led_pattern;
--
2.32.0