linux-leds.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Lin <dtwlin@google.com>
To: corbet@lwn.net, rpurdie@rpsys.net, jacek.anaszewski@gmail.com,
	pavel@ucw.cz, hdegoede@redhat.com, gregkh@linuxfoundation.org
Cc: robh@kernel.org, romlem@google.com, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org,
	David Lin <dtwlin@google.com>
Subject: [PATCH v2 3/3] led: ledtrig-transient: add support for hrtimer
Date: Wed, 13 Sep 2017 10:54:00 -0700	[thread overview]
Message-ID: <20170913175400.42744-4-dtwlin@google.com> (raw)
In-Reply-To: <20170913175400.42744-1-dtwlin@google.com>

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


  parent reply	other threads:[~2017-09-13 17:54 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-13 17:53 [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer David Lin
2017-09-13 17:53 ` [PATCH v2 1/3] leds: Replace flags bit shift with BIT() macros David Lin
2017-09-14 19:43   ` Jacek Anaszewski
2017-09-13 17:53 ` [PATCH v2 2/3] leds: Add the LED_BRIGHTNESS_FAST flag David Lin
2017-09-13 17:54 ` David Lin [this message]
2017-09-13 20:20 ` [PATCH v2 0/3] led: ledtrig-transient: add support for hrtimer Pavel Machek
2017-09-13 21:20   ` David Lin
2017-09-13 21:34     ` Pavel Machek
2017-09-14 17:31       ` David Lin
2017-09-14 19:42         ` Pavel Machek
2017-09-14 19:31   ` Jacek Anaszewski
2017-09-14 19:38     ` David Lin
2017-09-14 20:03       ` Jacek Anaszewski
2017-09-14 20:58     ` Vibrations in input vs. LED was " Pavel Machek
2017-09-15 18:34       ` Dmitry Torokhov
2017-09-15 21:55         ` Jacek Anaszewski
2017-09-15 22:30           ` Dmitry Torokhov
2017-09-17 16:41             ` Jacek Anaszewski
2017-09-17 18:22               ` Pavel Machek
2017-09-17 21:15                 ` Pavel Machek
2017-09-18 20:50                 ` Jacek Anaszewski
2017-09-18 22:29                   ` Dmitry Torokhov
2017-09-19 20:45                     ` Jacek Anaszewski
2017-09-19 21:07                       ` Dmitry Torokhov
2017-09-20 19:31                         ` Jacek Anaszewski
2017-09-20 11:29                       ` Pavel Machek
2017-09-20 20:08                         ` Jacek Anaszewski
2017-10-06 11:48                           ` Pavel Machek
2017-10-06 20:57                             ` Jacek Anaszewski
2017-09-20 11:26                   ` Pavel Machek
2017-09-28  5:03             ` David Lin
2017-09-28  5:43               ` Dmitry Torokhov
2017-09-28 19:22                 ` David Lin
2017-10-05  0:40                   ` Dmitry Torokhov
2017-09-16 12:59           ` Pavel Machek
2017-09-15 21:55       ` Jacek Anaszewski
2017-09-16  1:58         ` Pavel Machek
2017-09-17 16:41           ` Jacek Anaszewski
2017-09-17 17:50             ` Pavel Machek
2017-09-18 20:43               ` Jacek Anaszewski
2017-09-20 11:15                 ` Pavel Machek
2017-09-20 18:44                   ` Jacek Anaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170913175400.42744-4-dtwlin@google.com \
    --to=dtwlin@google.com \
    --cc=corbet@lwn.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=hdegoede@redhat.com \
    --cc=jacek.anaszewski@gmail.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@ucw.cz \
    --cc=robh@kernel.org \
    --cc=romlem@google.com \
    --cc=rpurdie@rpsys.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).