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> |
Jacek Anaszewski | 0471330 | 2014-08-07 05:10:22 -0700 | [diff] [blame] | 15 | #include <linux/leds.h> |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 16 | #include <linux/list.h> |
| 17 | #include <linux/module.h> |
Jacek Anaszewski | 0471330 | 2014-08-07 05:10:22 -0700 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 19 | #include <linux/rwsem.h> |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 20 | #include "leds.h" |
| 21 | |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 22 | DECLARE_RWSEM(leds_list_lock); |
Richard Purdie | c72a1d6 | 2006-03-31 02:31:04 -0800 | [diff] [blame] | 23 | EXPORT_SYMBOL_GPL(leds_list_lock); |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 24 | |
| 25 | LIST_HEAD(leds_list); |
| 26 | EXPORT_SYMBOL_GPL(leds_list); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 27 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 28 | static void led_set_software_blink(struct led_classdev *led_cdev, |
| 29 | unsigned long delay_on, |
| 30 | unsigned long delay_off) |
| 31 | { |
| 32 | int current_brightness; |
| 33 | |
| 34 | current_brightness = led_get_brightness(led_cdev); |
| 35 | if (current_brightness) |
| 36 | led_cdev->blink_brightness = current_brightness; |
| 37 | if (!led_cdev->blink_brightness) |
| 38 | led_cdev->blink_brightness = led_cdev->max_brightness; |
| 39 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 40 | led_cdev->blink_delay_on = delay_on; |
| 41 | led_cdev->blink_delay_off = delay_off; |
| 42 | |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 43 | /* never on - just set to off */ |
| 44 | if (!delay_on) { |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 45 | led_set_brightness_async(led_cdev, LED_OFF); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 46 | return; |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 47 | } |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 48 | |
| 49 | /* never off - just set to brightness */ |
| 50 | if (!delay_off) { |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 51 | led_set_brightness_async(led_cdev, led_cdev->blink_brightness); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 55 | mod_timer(&led_cdev->blink_timer, jiffies + 1); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
Bryan Wu | 20c0e6b | 2012-06-15 22:15:27 +0800 | [diff] [blame] | 59 | static void led_blink_setup(struct led_classdev *led_cdev, |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 60 | unsigned long *delay_on, |
| 61 | unsigned long *delay_off) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 62 | { |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 63 | if (!(led_cdev->flags & LED_BLINK_ONESHOT) && |
| 64 | led_cdev->blink_set && |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 65 | !led_cdev->blink_set(led_cdev, delay_on, delay_off)) |
| 66 | return; |
| 67 | |
| 68 | /* blink with 1 Hz as default if nothing specified */ |
| 69 | if (!*delay_on && !*delay_off) |
| 70 | *delay_on = *delay_off = 500; |
| 71 | |
| 72 | led_set_software_blink(led_cdev, *delay_on, *delay_off); |
| 73 | } |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 74 | |
| 75 | void led_blink_set(struct led_classdev *led_cdev, |
| 76 | unsigned long *delay_on, |
| 77 | unsigned long *delay_off) |
| 78 | { |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 79 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 80 | |
| 81 | led_cdev->flags &= ~LED_BLINK_ONESHOT; |
| 82 | led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; |
| 83 | |
| 84 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 85 | } |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 86 | EXPORT_SYMBOL(led_blink_set); |
| 87 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 88 | void led_blink_set_oneshot(struct led_classdev *led_cdev, |
| 89 | unsigned long *delay_on, |
| 90 | unsigned long *delay_off, |
| 91 | int invert) |
| 92 | { |
| 93 | if ((led_cdev->flags & LED_BLINK_ONESHOT) && |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 94 | timer_pending(&led_cdev->blink_timer)) |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 95 | return; |
| 96 | |
| 97 | led_cdev->flags |= LED_BLINK_ONESHOT; |
| 98 | led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; |
| 99 | |
| 100 | if (invert) |
| 101 | led_cdev->flags |= LED_BLINK_INVERT; |
| 102 | else |
| 103 | led_cdev->flags &= ~LED_BLINK_INVERT; |
| 104 | |
| 105 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 106 | } |
| 107 | EXPORT_SYMBOL(led_blink_set_oneshot); |
| 108 | |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 109 | void led_stop_software_blink(struct led_classdev *led_cdev) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 110 | { |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 111 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 112 | led_cdev->blink_delay_on = 0; |
| 113 | led_cdev->blink_delay_off = 0; |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 114 | } |
| 115 | EXPORT_SYMBOL_GPL(led_stop_software_blink); |
| 116 | |
| 117 | void led_set_brightness(struct led_classdev *led_cdev, |
| 118 | enum led_brightness brightness) |
| 119 | { |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 120 | int ret = 0; |
| 121 | |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 122 | /* delay brightness setting if need to stop soft-blink timer */ |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 123 | if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) { |
| 124 | led_cdev->delayed_set_value = brightness; |
| 125 | schedule_work(&led_cdev->set_brightness_work); |
| 126 | return; |
| 127 | } |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 128 | |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 129 | if (led_cdev->flags & SET_BRIGHTNESS_ASYNC) { |
| 130 | led_set_brightness_async(led_cdev, brightness); |
| 131 | return; |
| 132 | } else if (led_cdev->flags & SET_BRIGHTNESS_SYNC) |
| 133 | ret = led_set_brightness_sync(led_cdev, brightness); |
| 134 | else |
| 135 | ret = -EINVAL; |
| 136 | |
| 137 | if (ret < 0) |
| 138 | dev_dbg(led_cdev->dev, "Setting LED brightness failed (%d)\n", |
| 139 | ret); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 140 | } |
Shuah Khan | 19cd67e | 2012-06-14 04:34:30 +0800 | [diff] [blame] | 141 | EXPORT_SYMBOL(led_set_brightness); |
Jacek Anaszewski | 3ef7de5 | 2014-08-20 06:41:55 -0700 | [diff] [blame] | 142 | |
| 143 | int led_update_brightness(struct led_classdev *led_cdev) |
| 144 | { |
| 145 | int ret = 0; |
| 146 | |
| 147 | if (led_cdev->brightness_get) { |
| 148 | ret = led_cdev->brightness_get(led_cdev); |
| 149 | if (ret >= 0) { |
| 150 | led_cdev->brightness = ret; |
| 151 | return 0; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | EXPORT_SYMBOL(led_update_brightness); |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 158 | |
| 159 | /* Caller must ensure led_cdev->led_access held */ |
| 160 | void led_sysfs_disable(struct led_classdev *led_cdev) |
| 161 | { |
| 162 | lockdep_assert_held(&led_cdev->led_access); |
| 163 | |
| 164 | led_cdev->flags |= LED_SYSFS_DISABLE; |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(led_sysfs_disable); |
| 167 | |
| 168 | /* Caller must ensure led_cdev->led_access held */ |
| 169 | void led_sysfs_enable(struct led_classdev *led_cdev) |
| 170 | { |
| 171 | lockdep_assert_held(&led_cdev->led_access); |
| 172 | |
| 173 | led_cdev->flags &= ~LED_SYSFS_DISABLE; |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(led_sysfs_enable); |