diff mbox series

[net-next,v3,08/13] leds: trigger: netdev: add support for LED hw control

Message ID 20230527112854.2366-9-ansuelsmth@gmail.com
State New
Headers show
Series leds: introduce new LED hw control APIs | expand

Commit Message

Christian Marangi May 27, 2023, 11:28 a.m. UTC
Add support for LED hw control for the netdev trigger.

The trigger on calling set_baseline_state to configure a new mode, will
do various check to verify if hw control can be used for the requested
mode in can_hw_control() function.

It will first check if the LED driver supports hw control for the netdev
trigger, then will use hw_control_is_supported() and finally will call
hw_control_set() to apply the requested mode.

To use such mode, interval MUST be set to the default value and net_dev
MUST be set. If one of these 2 value are not valid, hw control will
never be used and normal software fallback is used.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/leds/trigger/ledtrig-netdev.c | 39 ++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

Comments

Andrew Lunn May 29, 2023, 3:45 p.m. UTC | #1
>  static bool can_hw_control(struct led_netdev_data *trigger_data)
>  {
> +	unsigned int interval = atomic_read(&trigger_data->interval);
>  	struct led_classdev *led_cdev = trigger_data->led_cdev;
> +	unsigned long default_interval = msecs_to_jiffies(50);

nitpick:

We have 50 in netdev_trig_activate(). Now it is used twice, it would
be nice to replace it with a #define. I doubt it will ever get
changed, but we do want them to be identical.

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

    Andrew
Christian Marangi May 29, 2023, 3:52 p.m. UTC | #2
On Mon, May 29, 2023 at 05:45:45PM +0200, Andrew Lunn wrote:
> >  static bool can_hw_control(struct led_netdev_data *trigger_data)
> >  {
> > +	unsigned int interval = atomic_read(&trigger_data->interval);
> >  	struct led_classdev *led_cdev = trigger_data->led_cdev;
> > +	unsigned long default_interval = msecs_to_jiffies(50);
> 
> nitpick:
> 
> We have 50 in netdev_trig_activate(). Now it is used twice, it would
> be nice to replace it with a #define. I doubt it will ever get
> changed, but we do want them to be identical.
> 
> Reviewed-by: Andrew Lunn <andrew@lunn.ch>
>

Good idea. I will move the value to a define in v4, guess it's ok to
keep the review tag for this simple change?
diff mbox series

Patch

diff --git a/drivers/leds/trigger/ledtrig-netdev.c b/drivers/leds/trigger/ledtrig-netdev.c
index cb2ec33abc4e..8d6381415208 100644
--- a/drivers/leds/trigger/ledtrig-netdev.c
+++ b/drivers/leds/trigger/ledtrig-netdev.c
@@ -68,6 +68,13 @@  static void set_baseline_state(struct led_netdev_data *trigger_data)
 	int current_brightness;
 	struct led_classdev *led_cdev = trigger_data->led_cdev;
 
+	/* Already validated, hw control is possible with the requested mode */
+	if (trigger_data->hw_control) {
+		led_cdev->hw_control_set(led_cdev, trigger_data->mode);
+
+		return;
+	}
+
 	current_brightness = led_cdev->brightness;
 	if (current_brightness)
 		led_cdev->blink_brightness = current_brightness;
@@ -103,12 +110,42 @@  static bool supports_hw_control(struct led_classdev *led_cdev)
 
 static bool can_hw_control(struct led_netdev_data *trigger_data)
 {
+	unsigned int interval = atomic_read(&trigger_data->interval);
 	struct led_classdev *led_cdev = trigger_data->led_cdev;
+	unsigned long default_interval = msecs_to_jiffies(50);
+	int ret;
 
 	if (!supports_hw_control(led_cdev))
 		return false;
 
-	return false;
+	/*
+	 * Interval must be set to the default
+	 * value. Any different value is rejected if in hw
+	 * control.
+	 */
+	if (interval != default_interval)
+		return false;
+
+	/*
+	 * net_dev must be set with hw control, otherwise no
+	 * blinking can be happening and there is nothing to
+	 * offloaded.
+	 */
+	if (!trigger_data->net_dev)
+		return false;
+
+	/* Check if the requested mode is supported */
+	ret = led_cdev->hw_control_is_supported(led_cdev, trigger_data->mode);
+	/* Fall back to software blinking if not supported */
+	if (ret == -EOPNOTSUPP)
+		return false;
+	if (ret) {
+		dev_warn(led_cdev->dev,
+			 "Current mode check failed with error %d\n", ret);
+		return false;
+	}
+
+	return true;
 }
 
 static ssize_t device_name_show(struct device *dev,