Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1731756

[PATCH v2 3/3] led: ledtrig-transient: add support for hrtimer

From David Lin <dtwlin@google.com>
Newsgroups linux.kernel
Subject [PATCH v2 3/3] led: ledtrig-transient: add support for hrtimer
Date 2017-09-13 20:00 +0200
Message-ID <upk78-227-7@gated-at.bofh.it> (permalink)
References <upk77-227-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


This patch adds a hrtimer to ledtrig-transient so that when driver is
registered with LED_BRIGHTNESS_FAST, the hrtimer is used for the better
time accuracy in handling the duration.

Signed-off-by: David Lin <dtwlin@google.com>
---
 drivers/leds/trigger/ledtrig-transient.c | 59 +++++++++++++++++++++++++++++---
 1 file changed, 54 insertions(+), 5 deletions(-)

diff --git a/drivers/leds/trigger/ledtrig-transient.c b/drivers/leds/trigger/ledtrig-transient.c
index 7e6011bd3646..7d2ce757b39d 100644
--- a/drivers/leds/trigger/ledtrig-transient.c
+++ b/drivers/leds/trigger/ledtrig-transient.c
@@ -24,15 +24,18 @@
 #include <linux/device.h>
 #include <linux/slab.h>
 #include <linux/timer.h>
+#include <linux/hrtimer.h>
 #include <linux/leds.h>
 #include "../leds.h"
 
 struct transient_trig_data {
+	struct led_classdev *led_cdev;
 	int activate;
 	int state;
 	int restore_state;
 	unsigned long duration;
 	struct timer_list timer;
+	struct hrtimer hrtimer;
 };
 
 static void transient_timer_function(unsigned long data)
@@ -44,6 +47,54 @@ static void transient_timer_function(unsigned long data)
 	led_set_brightness_nosleep(led_cdev, transient_data->restore_state);
 }
 
+static enum hrtimer_restart transient_hrtimer_function(struct hrtimer *timer)
+{
+	struct transient_trig_data *transient_data =
+		container_of(timer, struct transient_trig_data, hrtimer);
+
+	transient_timer_function((unsigned long)transient_data->led_cdev);
+
+	return HRTIMER_NORESTART;
+}
+
+static void transient_timer_setup(struct led_classdev *led_cdev)
+{
+	struct transient_trig_data *tdata = led_cdev->trigger_data;
+
+	if (led_cdev->flags & LED_BRIGHTNESS_FAST) {
+		tdata->led_cdev = led_cdev;
+		hrtimer_init(&tdata->hrtimer, CLOCK_MONOTONIC,
+			     HRTIMER_MODE_REL);
+		tdata->hrtimer.function = transient_hrtimer_function;
+	} else {
+		setup_timer(&tdata->timer, transient_timer_function,
+			    (unsigned long)led_cdev);
+	}
+}
+
+static void transient_timer_start(struct led_classdev *led_cdev)
+{
+	struct transient_trig_data *tdata = led_cdev->trigger_data;
+
+	if (led_cdev->flags & LED_BRIGHTNESS_FAST) {
+		hrtimer_start(&tdata->hrtimer, ms_to_ktime(tdata->duration),
+			      HRTIMER_MODE_REL);
+	} else {
+		mod_timer(&tdata->timer,
+			  jiffies + msecs_to_jiffies(tdata->duration));
+	}
+}
+
+static void transient_timer_cancel(struct led_classdev *led_cdev)
+{
+	struct transient_trig_data *tdata = led_cdev->trigger_data;
+
+	if (led_cdev->flags & LED_BRIGHTNESS_FAST)
+		hrtimer_cancel(&tdata->hrtimer);
+	else
+		del_timer_sync(&tdata->timer);
+}
+
 static ssize_t transient_activate_show(struct device *dev,
 		struct device_attribute *attr, char *buf)
 {
@@ -70,7 +121,7 @@ static ssize_t transient_activate_store(struct device *dev,
 
 	/* cancel the running timer */
 	if (state == 0 && transient_data->activate == 1) {
-		del_timer(&transient_data->timer);
+		transient_timer_cancel(led_cdev);
 		transient_data->activate = state;
 		led_set_brightness_nosleep(led_cdev,
 					transient_data->restore_state);
@@ -84,8 +135,7 @@ static ssize_t transient_activate_store(struct device *dev,
 		led_set_brightness_nosleep(led_cdev, transient_data->state);
 		transient_data->restore_state =
 		    (transient_data->state == LED_FULL) ? LED_OFF : LED_FULL;
-		mod_timer(&transient_data->timer,
-			  jiffies + msecs_to_jiffies(transient_data->duration));
+		transient_timer_start(led_cdev);
 	}
 
 	/* state == 0 && transient_data->activate == 0
@@ -182,8 +232,7 @@ static void transient_trig_activate(struct led_classdev *led_cdev)
 	if (rc)
 		goto err_out_state;
 
-	setup_timer(&tdata->timer, transient_timer_function,
-		    (unsigned long) led_cdev);
+	transient_timer_setup(led_cdev);
 	led_cdev->activated = true;
 
 	return;
-- 
2.14.1.581.gf28d330327-goog

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer David Lin <dtwlin@google.com> - 2017-09-13 20:00 +0200
  [PATCH v2 2/3] leds: Add the LED_BRIGHTNESS_FAST flag David Lin <dtwlin@google.com> - 2017-09-13 20:00 +0200
  [PATCH v2 3/3] led: ledtrig-transient: add support for hrtimer David Lin <dtwlin@google.com> - 2017-09-13 20:00 +0200
  [PATCH v2 1/3] leds: Replace flags bit shift with BIT() macros David Lin <dtwlin@google.com> - 2017-09-13 20:00 +0200
    Re: [PATCH v2 1/3] leds: Replace flags bit shift with BIT() macros Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-14 21:50 +0200
  Re: [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-13 22:30 +0200
    Re: [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer David Lin <dtwlin@google.com> - 2017-09-13 23:30 +0200
      Re: [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-13 23:40 +0200
        Re: [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer David Lin <dtwlin@google.com> - 2017-09-14 19:40 +0200
          Re: [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-14 21:50 +0200
    Re: [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-14 21:40 +0200
      Re: [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer David Lin <dtwlin@google.com> - 2017-09-14 21:40 +0200
        Re: [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-14 22:10 +0200
      Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-14 23:00 +0200
        Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2017-09-15 20:40 +0200
          Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-16 00:00 +0200
            Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2017-09-16 00:40 +0200
              Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-17 18:50 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-17 20:30 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-17 23:20 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-18 23:00 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2017-09-19 00:30 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-19 22:50 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Dmitry Torokhov <dmitry.torokhov@gmail.com> - 2017-09-19 23:10 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-20 21:40 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-20 13:30 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-20 22:10 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-20 13:30 +0200
            Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-16 15:10 +0200
        Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-16 00:00 +0200
          Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-16 14:40 +0200
            Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-17 18:50 +0200
              Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-17 20:00 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-18 22:50 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Pavel Machek <pavel@ucw.cz> - 2017-09-20 13:20 +0200
                Re: Vibrations in input vs. LED was Re: [PATCH v2 0/3] led:  ledtrig-transient: add support for hrtimer Jacek Anaszewski <jacek.anaszewski@gmail.com> - 2017-09-20 20:50 +0200

csiph-web