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 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 27 | static void led_set_software_blink(struct led_classdev *led_cdev, |
| 28 | unsigned long delay_on, |
| 29 | unsigned long delay_off) |
| 30 | { |
| 31 | int current_brightness; |
| 32 | |
| 33 | current_brightness = led_get_brightness(led_cdev); |
| 34 | if (current_brightness) |
| 35 | led_cdev->blink_brightness = current_brightness; |
| 36 | if (!led_cdev->blink_brightness) |
| 37 | led_cdev->blink_brightness = led_cdev->max_brightness; |
| 38 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 39 | led_cdev->blink_delay_on = delay_on; |
| 40 | led_cdev->blink_delay_off = delay_off; |
| 41 | |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 42 | /* never on - just set to off */ |
| 43 | if (!delay_on) { |
| 44 | __led_set_brightness(led_cdev, LED_OFF); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 45 | return; |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 46 | } |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 47 | |
| 48 | /* never off - just set to brightness */ |
| 49 | if (!delay_off) { |
Shuah Khan | 0da3e65 | 2012-06-13 10:01:37 +0800 | [diff] [blame] | 50 | __led_set_brightness(led_cdev, led_cdev->blink_brightness); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
| 54 | mod_timer(&led_cdev->blink_timer, jiffies + 1); |
| 55 | } |
| 56 | |
| 57 | |
Bryan Wu | 20c0e6b | 2012-06-15 22:15:27 +0800 | [diff] [blame] | 58 | static void led_blink_setup(struct led_classdev *led_cdev, |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 59 | unsigned long *delay_on, |
| 60 | unsigned long *delay_off) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 61 | { |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 62 | if (!(led_cdev->flags & LED_BLINK_ONESHOT) && |
| 63 | led_cdev->blink_set && |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 64 | !led_cdev->blink_set(led_cdev, delay_on, delay_off)) |
| 65 | return; |
| 66 | |
| 67 | /* blink with 1 Hz as default if nothing specified */ |
| 68 | if (!*delay_on && !*delay_off) |
| 69 | *delay_on = *delay_off = 500; |
| 70 | |
| 71 | led_set_software_blink(led_cdev, *delay_on, *delay_off); |
| 72 | } |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 73 | |
| 74 | void led_blink_set(struct led_classdev *led_cdev, |
| 75 | unsigned long *delay_on, |
| 76 | unsigned long *delay_off) |
| 77 | { |
| 78 | del_timer_sync(&led_cdev->blink_timer); |
| 79 | |
| 80 | led_cdev->flags &= ~LED_BLINK_ONESHOT; |
| 81 | led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; |
| 82 | |
| 83 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 84 | } |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 85 | EXPORT_SYMBOL(led_blink_set); |
| 86 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 87 | void led_blink_set_oneshot(struct led_classdev *led_cdev, |
| 88 | unsigned long *delay_on, |
| 89 | unsigned long *delay_off, |
| 90 | int invert) |
| 91 | { |
| 92 | if ((led_cdev->flags & LED_BLINK_ONESHOT) && |
| 93 | timer_pending(&led_cdev->blink_timer)) |
| 94 | return; |
| 95 | |
| 96 | led_cdev->flags |= LED_BLINK_ONESHOT; |
| 97 | led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; |
| 98 | |
| 99 | if (invert) |
| 100 | led_cdev->flags |= LED_BLINK_INVERT; |
| 101 | else |
| 102 | led_cdev->flags &= ~LED_BLINK_INVERT; |
| 103 | |
| 104 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 105 | } |
| 106 | EXPORT_SYMBOL(led_blink_set_oneshot); |
| 107 | |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 108 | void led_stop_software_blink(struct led_classdev *led_cdev) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 109 | { |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 110 | del_timer_sync(&led_cdev->blink_timer); |
| 111 | led_cdev->blink_delay_on = 0; |
| 112 | led_cdev->blink_delay_off = 0; |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 113 | } |
| 114 | EXPORT_SYMBOL_GPL(led_stop_software_blink); |
| 115 | |
| 116 | void led_set_brightness(struct led_classdev *led_cdev, |
| 117 | enum led_brightness brightness) |
| 118 | { |
| 119 | /* delay brightness setting if need to stop soft-blink timer */ |
| 120 | if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) { |
| 121 | led_cdev->delayed_set_value = brightness; |
| 122 | schedule_work(&led_cdev->set_brightness_work); |
| 123 | return; |
| 124 | } |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 125 | |
Shuah Khan | 0da3e65 | 2012-06-13 10:01:37 +0800 | [diff] [blame] | 126 | __led_set_brightness(led_cdev, brightness); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 127 | } |
Shuah Khan | 19cd67e | 2012-06-14 04:34:30 +0800 | [diff] [blame] | 128 | EXPORT_SYMBOL(led_set_brightness); |