Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * LED Class Core |
| 3 | * |
| 4 | * Copyright 2005-2006 Openedhand Ltd. |
| 5 | * |
| 6 | * Author: Richard Purdie <rpurdie@openedhand.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/module.h> |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 17 | #include <linux/rwsem.h> |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 18 | #include <linux/leds.h> |
| 19 | #include "leds.h" |
| 20 | |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 21 | DECLARE_RWSEM(leds_list_lock); |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 22 | EXPORT_SYMBOL_GPL(leds_list_lock); |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 23 | |
| 24 | LIST_HEAD(leds_list); |
| 25 | EXPORT_SYMBOL_GPL(leds_list); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 26 | |
| 27 | static void led_stop_software_blink(struct led_classdev *led_cdev) |
| 28 | { |
| 29 | /* deactivate previous settings */ |
| 30 | del_timer_sync(&led_cdev->blink_timer); |
| 31 | led_cdev->blink_delay_on = 0; |
| 32 | led_cdev->blink_delay_off = 0; |
| 33 | } |
| 34 | |
| 35 | static void led_set_software_blink(struct led_classdev *led_cdev, |
| 36 | unsigned long delay_on, |
| 37 | unsigned long delay_off) |
| 38 | { |
| 39 | int current_brightness; |
| 40 | |
| 41 | current_brightness = led_get_brightness(led_cdev); |
| 42 | if (current_brightness) |
| 43 | led_cdev->blink_brightness = current_brightness; |
| 44 | if (!led_cdev->blink_brightness) |
| 45 | led_cdev->blink_brightness = led_cdev->max_brightness; |
| 46 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 47 | led_cdev->blink_delay_on = delay_on; |
| 48 | led_cdev->blink_delay_off = delay_off; |
| 49 | |
| 50 | /* never on - don't blink */ |
| 51 | if (!delay_on) |
| 52 | return; |
| 53 | |
| 54 | /* never off - just set to brightness */ |
| 55 | if (!delay_off) { |
| 56 | led_set_brightness(led_cdev, led_cdev->blink_brightness); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | mod_timer(&led_cdev->blink_timer, jiffies + 1); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | void led_blink_set(struct led_classdev *led_cdev, |
| 65 | unsigned long *delay_on, |
| 66 | unsigned long *delay_off) |
| 67 | { |
| 68 | del_timer_sync(&led_cdev->blink_timer); |
| 69 | |
| 70 | if (led_cdev->blink_set && |
| 71 | !led_cdev->blink_set(led_cdev, delay_on, delay_off)) |
| 72 | return; |
| 73 | |
| 74 | /* blink with 1 Hz as default if nothing specified */ |
| 75 | if (!*delay_on && !*delay_off) |
| 76 | *delay_on = *delay_off = 500; |
| 77 | |
| 78 | led_set_software_blink(led_cdev, *delay_on, *delay_off); |
| 79 | } |
| 80 | EXPORT_SYMBOL(led_blink_set); |
| 81 | |
| 82 | void led_brightness_set(struct led_classdev *led_cdev, |
| 83 | enum led_brightness brightness) |
| 84 | { |
| 85 | led_stop_software_blink(led_cdev); |
| 86 | led_cdev->brightness_set(led_cdev, brightness); |
| 87 | } |
| 88 | EXPORT_SYMBOL(led_brightness_set); |