leds: allow led-drivers to use a variable range of brightness values
authorGuennadi Liakhovetski <lg@denx.de>
Sat, 10 Jan 2009 18:54:39 +0000 (18:54 +0000)
committerRichard Purdie <rpurdie@linux.intel.com>
Mon, 6 Apr 2009 15:06:25 +0000 (16:06 +0100)
This patch allows drivers to override the default maximum brightness value
of 255.  We take care to preserve backwards-compatibility as much as
possible, so that user-space ABI doesn't change for existing drivers.
LED trigger code has also been updated to use the per-LED maximum.

Signed-off-by: Guennadi Liakhovetski <lg@denx.de>
Signed-off-by: Richard Purdie <rpurdie@linux.intel.com>
drivers/leds/led-class.c
drivers/leds/leds.h
drivers/leds/ledtrig-default-on.c
drivers/leds/ledtrig-heartbeat.c
drivers/leds/ledtrig-ide-disk.c
drivers/leds/ledtrig-timer.c
include/linux/leds.h

index 52f82e3ea13aae5b04a603938fba9dbbd0654712..f2cc13d76810f457c752d6f92f53759980f465ea 100644 (file)
@@ -64,7 +64,16 @@ static ssize_t led_brightness_store(struct device *dev,
        return ret;
 }
 
+static ssize_t led_max_brightness_show(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       struct led_classdev *led_cdev = dev_get_drvdata(dev);
+
+       return sprintf(buf, "%u\n", led_cdev->max_brightness);
+}
+
 static DEVICE_ATTR(brightness, 0644, led_brightness_show, led_brightness_store);
+static DEVICE_ATTR(max_brightness, 0444, led_max_brightness_show, NULL);
 #ifdef CONFIG_LEDS_TRIGGERS
 static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
 #endif
@@ -138,6 +147,13 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
        list_add_tail(&led_cdev->node, &leds_list);
        up_write(&leds_list_lock);
 
+       if (!led_cdev->max_brightness)
+               led_cdev->max_brightness = LED_FULL;
+
+       rc = device_create_file(led_cdev->dev, &dev_attr_max_brightness);
+       if (rc)
+               goto err_out_attr_max;
+
        led_update_brightness(led_cdev);
 
 #ifdef CONFIG_LEDS_TRIGGERS
@@ -155,9 +171,11 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
 
 #ifdef CONFIG_LEDS_TRIGGERS
 err_out_led_list:
+       device_remove_file(led_cdev->dev, &dev_attr_max_brightness);
+#endif
+err_out_attr_max:
        device_remove_file(led_cdev->dev, &dev_attr_brightness);
        list_del(&led_cdev->node);
-#endif
 err_out:
        device_unregister(led_cdev->dev);
        return rc;
@@ -172,6 +190,7 @@ EXPORT_SYMBOL_GPL(led_classdev_register);
  */
 void led_classdev_unregister(struct led_classdev *led_cdev)
 {
+       device_remove_file(led_cdev->dev, &dev_attr_max_brightness);
        device_remove_file(led_cdev->dev, &dev_attr_brightness);
 #ifdef CONFIG_LEDS_TRIGGERS
        device_remove_file(led_cdev->dev, &dev_attr_trigger);
index 5edbf52c4fa7e6834e9125855bd4c1801e2c2809..2dd8ecbfdc3155250d3608897db8d1d0047fc317 100644 (file)
@@ -20,8 +20,8 @@
 static inline void led_set_brightness(struct led_classdev *led_cdev,
                                        enum led_brightness value)
 {
-       if (value > LED_FULL)
-               value = LED_FULL;
+       if (value > led_cdev->max_brightness)
+               value = led_cdev->max_brightness;
        led_cdev->brightness = value;
        if (!(led_cdev->flags & LED_SUSPENDED))
                led_cdev->brightness_set(led_cdev, value);
index 92995e40cfa43fd6ba9ea1b454f5172044119193..a4ef54b9d508efffe60b0cd4db2213479680547e 100644 (file)
@@ -19,7 +19,7 @@
 
 static void defon_trig_activate(struct led_classdev *led_cdev)
 {
-       led_set_brightness(led_cdev, LED_FULL);
+       led_set_brightness(led_cdev, led_cdev->max_brightness);
 }
 
 static struct led_trigger defon_led_trigger = {
index 4bf8cec8b8c14173b68f78d4d3126648e2092051..c1c1ea6f817b1eb7ddb7f2433cbdc917e20d164e 100644 (file)
@@ -47,7 +47,7 @@ static void led_heartbeat_function(unsigned long data)
                        msecs_to_jiffies(heartbeat_data->period);
                delay = msecs_to_jiffies(70);
                heartbeat_data->phase++;
-               brightness = LED_FULL;
+               brightness = led_cdev->max_brightness;
                break;
        case 1:
                delay = heartbeat_data->period / 4 - msecs_to_jiffies(70);
@@ -56,7 +56,7 @@ static void led_heartbeat_function(unsigned long data)
        case 2:
                delay = msecs_to_jiffies(70);
                heartbeat_data->phase++;
-               brightness = LED_FULL;
+               brightness = led_cdev->max_brightness;
                break;
        default:
                delay = heartbeat_data->period - heartbeat_data->period / 4 -
index 883a577b1b97aaaa189a3d82fc461939ffa286da..ec099fcbcb00ecf01c4f8ef4313710230428b15b 100644 (file)
@@ -37,7 +37,8 @@ static void ledtrig_ide_timerfunc(unsigned long data)
 {
        if (ide_lastactivity != ide_activity) {
                ide_lastactivity = ide_activity;
-               led_trigger_event(ledtrig_ide, LED_FULL);
+               /* INT_MAX will set each LED to its maximum brightness */
+               led_trigger_event(ledtrig_ide, INT_MAX);
                mod_timer(&ledtrig_ide_timer, jiffies + msecs_to_jiffies(10));
        } else {
                led_trigger_event(ledtrig_ide, LED_OFF);
index 3d6531396dda094e41eb7f00b80d20acbbd244cd..3b83406de7520b8052b73906992f2fa34e1b3891 100644 (file)
@@ -166,7 +166,7 @@ static void timer_trig_activate(struct led_classdev *led_cdev)
 
        timer_data->brightness_on = led_get_brightness(led_cdev);
        if (timer_data->brightness_on == LED_OFF)
-               timer_data->brightness_on = LED_FULL;
+               timer_data->brightness_on = led_cdev->max_brightness;
        led_cdev->trigger_data = timer_data;
 
        init_timer(&timer_data->timer);
index 24489da701e331b1a4d1810b40afd7209775e265..17d277e0c4a645d6be61bfffb84539f36cacace5 100644 (file)
@@ -30,6 +30,7 @@ enum led_brightness {
 struct led_classdev {
        const char              *name;
        int                      brightness;
+       int                      max_brightness;
        int                      flags;
 
        /* Lower 16 bits reflect status */