leds: Add support for setting brightness in a synchronous way
authorJacek Anaszewski <j.anaszewski@samsung.com>
Fri, 14 Nov 2014 10:50:18 +0000 (02:50 -0800)
committerBryan Wu <cooloney@gmail.com>
Fri, 14 Nov 2014 22:29:35 +0000 (14:29 -0800)
There are use cases when setting a LED brightness has to
have immediate effect (e.g. setting a torch LED brightness).
This patch extends LED subsystem to support such operations.
The LED subsystem internal API __led_set_brightness is changed
to led_set_brightness_async and new led_set_brightness_sync API
is added.

Signed-off-by: Jacek Anaszewski <j.anaszewski@samsung.com>
Acked-by: Kyungmin Park <kyungmin.park@samsung.com>
Cc: Richard Purdie <rpurdie@rpsys.net>
Signed-off-by: Bryan Wu <cooloney@gmail.com>
drivers/leds/led-class.c
drivers/leds/led-core.c
drivers/leds/leds.h
drivers/leds/trigger/ledtrig-backlight.c
drivers/leds/trigger/ledtrig-default-on.c
drivers/leds/trigger/ledtrig-gpio.c
drivers/leds/trigger/ledtrig-heartbeat.c
drivers/leds/trigger/ledtrig-oneshot.c
drivers/leds/trigger/ledtrig-transient.c
include/linux/leds.h

index 65722de..dbeebac 100644 (file)
@@ -55,7 +55,7 @@ static ssize_t brightness_store(struct device *dev,
 
        if (state == LED_OFF)
                led_trigger_remove(led_cdev);
-       __led_set_brightness(led_cdev, state);
+       led_set_brightness(led_cdev, state);
 
        ret = size;
 unlock:
@@ -109,7 +109,7 @@ static void led_timer_function(unsigned long data)
        unsigned long delay;
 
        if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
-               __led_set_brightness(led_cdev, LED_OFF);
+               led_set_brightness_async(led_cdev, LED_OFF);
                return;
        }
 
@@ -132,7 +132,7 @@ static void led_timer_function(unsigned long data)
                delay = led_cdev->blink_delay_off;
        }
 
-       __led_set_brightness(led_cdev, brightness);
+       led_set_brightness_async(led_cdev, brightness);
 
        /* Return in next iteration if led is in one-shot mode and we are in
         * the final blink state so that the led is toggled each delay_on +
@@ -158,7 +158,7 @@ static void set_brightness_delayed(struct work_struct *ws)
 
        led_stop_software_blink(led_cdev);
 
-       __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
+       led_set_brightness_async(led_cdev, led_cdev->delayed_set_value);
 }
 
 /**
@@ -233,6 +233,8 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
        if (!led_cdev->max_brightness)
                led_cdev->max_brightness = LED_FULL;
 
+       led_cdev->flags |= SET_BRIGHTNESS_ASYNC;
+
        led_update_brightness(led_cdev);
 
        INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);
index be6d9fa..9886dac 100644 (file)
@@ -42,13 +42,13 @@ static void led_set_software_blink(struct led_classdev *led_cdev,
 
        /* never on - just set to off */
        if (!delay_on) {
-               __led_set_brightness(led_cdev, LED_OFF);
+               led_set_brightness_async(led_cdev, LED_OFF);
                return;
        }
 
        /* never off - just set to brightness */
        if (!delay_off) {
-               __led_set_brightness(led_cdev, led_cdev->blink_brightness);
+               led_set_brightness_async(led_cdev, led_cdev->blink_brightness);
                return;
        }
 
@@ -117,6 +117,8 @@ EXPORT_SYMBOL_GPL(led_stop_software_blink);
 void led_set_brightness(struct led_classdev *led_cdev,
                        enum led_brightness brightness)
 {
+       int ret = 0;
+
        /* delay brightness setting if need to stop soft-blink timer */
        if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) {
                led_cdev->delayed_set_value = brightness;
@@ -124,7 +126,17 @@ void led_set_brightness(struct led_classdev *led_cdev,
                return;
        }
 
-       __led_set_brightness(led_cdev, brightness);
+       if (led_cdev->flags & SET_BRIGHTNESS_ASYNC) {
+               led_set_brightness_async(led_cdev, brightness);
+               return;
+       } else if (led_cdev->flags & SET_BRIGHTNESS_SYNC)
+               ret = led_set_brightness_sync(led_cdev, brightness);
+       else
+               ret = -EINVAL;
+
+       if (ret < 0)
+               dev_dbg(led_cdev->dev, "Setting LED brightness failed (%d)\n",
+                       ret);
 }
 EXPORT_SYMBOL(led_set_brightness);
 
index 4c50365..2348dbd 100644 (file)
 #include <linux/rwsem.h>
 #include <linux/leds.h>
 
-static inline void __led_set_brightness(struct led_classdev *led_cdev,
+static inline void led_set_brightness_async(struct led_classdev *led_cdev,
                                        enum led_brightness value)
 {
-       if (value > led_cdev->max_brightness)
-               value = led_cdev->max_brightness;
-       led_cdev->brightness = value;
+       led_cdev->brightness = min(value, led_cdev->max_brightness);
+
        if (!(led_cdev->flags & LED_SUSPENDED))
                led_cdev->brightness_set(led_cdev, value);
 }
 
+static inline int led_set_brightness_sync(struct led_classdev *led_cdev,
+                                       enum led_brightness value)
+{
+       int ret = 0;
+
+       led_cdev->brightness = min(value, led_cdev->max_brightness);
+
+       if (!(led_cdev->flags & LED_SUSPENDED))
+               ret = led_cdev->brightness_set_sync(led_cdev,
+                                               led_cdev->brightness);
+       return ret;
+}
+
 static inline int led_get_brightness(struct led_classdev *led_cdev)
 {
        return led_cdev->brightness;
index 47e55aa..59eca17 100644 (file)
@@ -51,9 +51,9 @@ static int fb_notifier_callback(struct notifier_block *p,
 
        if ((n->old_status == UNBLANK) ^ n->invert) {
                n->brightness = led->brightness;
-               __led_set_brightness(led, LED_OFF);
+               led_set_brightness_async(led, LED_OFF);
        } else {
-               __led_set_brightness(led, n->brightness);
+               led_set_brightness_async(led, n->brightness);
        }
 
        n->old_status = new_status;
@@ -89,9 +89,9 @@ static ssize_t bl_trig_invert_store(struct device *dev,
 
        /* After inverting, we need to update the LED. */
        if ((n->old_status == BLANK) ^ n->invert)
-               __led_set_brightness(led, LED_OFF);
+               led_set_brightness_async(led, LED_OFF);
        else
-               __led_set_brightness(led, n->brightness);
+               led_set_brightness_async(led, n->brightness);
 
        return num;
 }
index 81a91be..6f38f88 100644 (file)
@@ -19,7 +19,7 @@
 
 static void defon_trig_activate(struct led_classdev *led_cdev)
 {
-       __led_set_brightness(led_cdev, led_cdev->max_brightness);
+       led_set_brightness_async(led_cdev, led_cdev->max_brightness);
 }
 
 static struct led_trigger defon_led_trigger = {
index c86c418..4cc7040 100644 (file)
@@ -54,12 +54,12 @@ static void gpio_trig_work(struct work_struct *work)
 
        if (tmp) {
                if (gpio_data->desired_brightness)
-                       __led_set_brightness(gpio_data->led,
+                       led_set_brightness_async(gpio_data->led,
                                           gpio_data->desired_brightness);
                else
-                       __led_set_brightness(gpio_data->led, LED_FULL);
+                       led_set_brightness_async(gpio_data->led, LED_FULL);
        } else {
-               __led_set_brightness(gpio_data->led, LED_OFF);
+               led_set_brightness_async(gpio_data->led, LED_OFF);
        }
 }
 
index 5c8464a..fea6871 100644 (file)
@@ -74,7 +74,7 @@ static void led_heartbeat_function(unsigned long data)
                break;
        }
 
-       __led_set_brightness(led_cdev, brightness);
+       led_set_brightness_async(led_cdev, brightness);
        mod_timer(&heartbeat_data->timer, jiffies + delay);
 }
 
index cb4c746..fbd02cd 100644 (file)
@@ -63,9 +63,9 @@ static ssize_t led_invert_store(struct device *dev,
        oneshot_data->invert = !!state;
 
        if (oneshot_data->invert)
-               __led_set_brightness(led_cdev, LED_FULL);
+               led_set_brightness_async(led_cdev, LED_FULL);
        else
-               __led_set_brightness(led_cdev, LED_OFF);
+               led_set_brightness_async(led_cdev, LED_OFF);
 
        return size;
 }
index e5abc00..3c34de4 100644 (file)
@@ -41,7 +41,7 @@ static void transient_timer_function(unsigned long data)
        struct transient_trig_data *transient_data = led_cdev->trigger_data;
 
        transient_data->activate = 0;
-       __led_set_brightness(led_cdev, transient_data->restore_state);
+       led_set_brightness_async(led_cdev, transient_data->restore_state);
 }
 
 static ssize_t transient_activate_show(struct device *dev,
@@ -72,7 +72,8 @@ static ssize_t transient_activate_store(struct device *dev,
        if (state == 0 && transient_data->activate == 1) {
                del_timer(&transient_data->timer);
                transient_data->activate = state;
-               __led_set_brightness(led_cdev, transient_data->restore_state);
+               led_set_brightness_async(led_cdev,
+                                       transient_data->restore_state);
                return size;
        }
 
@@ -80,7 +81,7 @@ static ssize_t transient_activate_store(struct device *dev,
        if (state == 1 && transient_data->activate == 0 &&
            transient_data->duration != 0) {
                transient_data->activate = state;
-               __led_set_brightness(led_cdev, transient_data->state);
+               led_set_brightness_async(led_cdev, transient_data->state);
                transient_data->restore_state =
                    (transient_data->state == LED_FULL) ? LED_OFF : LED_FULL;
                mod_timer(&transient_data->timer,
@@ -203,7 +204,8 @@ static void transient_trig_deactivate(struct led_classdev *led_cdev)
 
        if (led_cdev->activated) {
                del_timer_sync(&transient_data->timer);
-               __led_set_brightness(led_cdev, transient_data->restore_state);
+               led_set_brightness_async(led_cdev,
+                                       transient_data->restore_state);
                device_remove_file(led_cdev->dev, &dev_attr_activate);
                device_remove_file(led_cdev->dev, &dev_attr_duration);
                device_remove_file(led_cdev->dev, &dev_attr_state);
index 737f9b1..dfe70e9 100644 (file)
@@ -44,11 +44,19 @@ struct led_classdev {
 #define LED_BLINK_ONESHOT_STOP (1 << 18)
 #define LED_BLINK_INVERT       (1 << 19)
 #define LED_SYSFS_DISABLE      (1 << 20)
+#define SET_BRIGHTNESS_ASYNC   (1 << 21)
+#define SET_BRIGHTNESS_SYNC    (1 << 22)
 
        /* Set LED brightness level */
        /* Must not sleep, use a workqueue if needed */
        void            (*brightness_set)(struct led_classdev *led_cdev,
                                          enum led_brightness brightness);
+       /*
+        * Set LED brightness level immediately - it can block the caller for
+        * the time required for accessing a LED device register.
+        */
+       int             (*brightness_set_sync)(struct led_classdev *led_cdev,
+                                       enum led_brightness brightness);
        /* Get LED brightness level */
        enum led_brightness (*brightness_get)(struct led_classdev *led_cdev);