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 | |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 28 | static void led_timer_function(unsigned long data) |
| 29 | { |
| 30 | struct led_classdev *led_cdev = (void *)data; |
| 31 | unsigned long brightness; |
| 32 | unsigned long delay; |
| 33 | |
| 34 | if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) { |
| 35 | led_set_brightness_async(led_cdev, LED_OFF); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) { |
| 40 | led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | brightness = led_get_brightness(led_cdev); |
| 45 | if (!brightness) { |
| 46 | /* Time to switch the LED on. */ |
| 47 | if (led_cdev->delayed_set_value) { |
| 48 | led_cdev->blink_brightness = |
| 49 | led_cdev->delayed_set_value; |
| 50 | led_cdev->delayed_set_value = 0; |
| 51 | } |
| 52 | brightness = led_cdev->blink_brightness; |
| 53 | delay = led_cdev->blink_delay_on; |
| 54 | } else { |
| 55 | /* Store the current brightness value to be able |
| 56 | * to restore it when the delay_off period is over. |
| 57 | */ |
| 58 | led_cdev->blink_brightness = brightness; |
| 59 | brightness = LED_OFF; |
| 60 | delay = led_cdev->blink_delay_off; |
| 61 | } |
| 62 | |
| 63 | led_set_brightness_async(led_cdev, brightness); |
| 64 | |
| 65 | /* Return in next iteration if led is in one-shot mode and we are in |
| 66 | * the final blink state so that the led is toggled each delay_on + |
| 67 | * delay_off milliseconds in worst case. |
| 68 | */ |
| 69 | if (led_cdev->flags & LED_BLINK_ONESHOT) { |
| 70 | if (led_cdev->flags & LED_BLINK_INVERT) { |
| 71 | if (brightness) |
| 72 | led_cdev->flags |= LED_BLINK_ONESHOT_STOP; |
| 73 | } else { |
| 74 | if (!brightness) |
| 75 | led_cdev->flags |= LED_BLINK_ONESHOT_STOP; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay)); |
| 80 | } |
| 81 | |
| 82 | static void set_brightness_delayed(struct work_struct *ws) |
| 83 | { |
| 84 | struct led_classdev *led_cdev = |
| 85 | container_of(ws, struct led_classdev, set_brightness_work); |
| 86 | |
| 87 | led_stop_software_blink(led_cdev); |
| 88 | |
| 89 | led_set_brightness_async(led_cdev, led_cdev->delayed_set_value); |
| 90 | } |
| 91 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 92 | static void led_set_software_blink(struct led_classdev *led_cdev, |
| 93 | unsigned long delay_on, |
| 94 | unsigned long delay_off) |
| 95 | { |
| 96 | int current_brightness; |
| 97 | |
| 98 | current_brightness = led_get_brightness(led_cdev); |
| 99 | if (current_brightness) |
| 100 | led_cdev->blink_brightness = current_brightness; |
| 101 | if (!led_cdev->blink_brightness) |
| 102 | led_cdev->blink_brightness = led_cdev->max_brightness; |
| 103 | |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 104 | led_cdev->blink_delay_on = delay_on; |
| 105 | led_cdev->blink_delay_off = delay_off; |
| 106 | |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 107 | /* never on - just set to off */ |
| 108 | if (!delay_on) { |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 109 | led_set_brightness_async(led_cdev, LED_OFF); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 110 | return; |
Stefan Sørensen | 8d82fef | 2014-02-04 00:11:42 -0800 | [diff] [blame] | 111 | } |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 112 | |
| 113 | /* never off - just set to brightness */ |
| 114 | if (!delay_off) { |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 115 | led_set_brightness_async(led_cdev, led_cdev->blink_brightness); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 119 | mod_timer(&led_cdev->blink_timer, jiffies + 1); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
Bryan Wu | 20c0e6b | 2012-06-15 22:15:27 +0800 | [diff] [blame] | 123 | static void led_blink_setup(struct led_classdev *led_cdev, |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 124 | unsigned long *delay_on, |
| 125 | unsigned long *delay_off) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 126 | { |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 127 | if (!(led_cdev->flags & LED_BLINK_ONESHOT) && |
| 128 | led_cdev->blink_set && |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 129 | !led_cdev->blink_set(led_cdev, delay_on, delay_off)) |
| 130 | return; |
| 131 | |
| 132 | /* blink with 1 Hz as default if nothing specified */ |
| 133 | if (!*delay_on && !*delay_off) |
| 134 | *delay_on = *delay_off = 500; |
| 135 | |
| 136 | led_set_software_blink(led_cdev, *delay_on, *delay_off); |
| 137 | } |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 138 | |
Jacek Anaszewski | 757b06a | 2015-09-28 14:38:04 +0200 | [diff] [blame] | 139 | void led_init_core(struct led_classdev *led_cdev) |
| 140 | { |
| 141 | INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed); |
| 142 | |
| 143 | setup_timer(&led_cdev->blink_timer, led_timer_function, |
| 144 | (unsigned long)led_cdev); |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(led_init_core); |
| 147 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 148 | void led_blink_set(struct led_classdev *led_cdev, |
| 149 | unsigned long *delay_on, |
| 150 | unsigned long *delay_off) |
| 151 | { |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 152 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 153 | |
| 154 | led_cdev->flags &= ~LED_BLINK_ONESHOT; |
| 155 | led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; |
| 156 | |
| 157 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 158 | } |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 159 | EXPORT_SYMBOL(led_blink_set); |
| 160 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 161 | void led_blink_set_oneshot(struct led_classdev *led_cdev, |
| 162 | unsigned long *delay_on, |
| 163 | unsigned long *delay_off, |
| 164 | int invert) |
| 165 | { |
| 166 | if ((led_cdev->flags & LED_BLINK_ONESHOT) && |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 167 | timer_pending(&led_cdev->blink_timer)) |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 168 | return; |
| 169 | |
| 170 | led_cdev->flags |= LED_BLINK_ONESHOT; |
| 171 | led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; |
| 172 | |
| 173 | if (invert) |
| 174 | led_cdev->flags |= LED_BLINK_INVERT; |
| 175 | else |
| 176 | led_cdev->flags &= ~LED_BLINK_INVERT; |
| 177 | |
| 178 | led_blink_setup(led_cdev, delay_on, delay_off); |
| 179 | } |
| 180 | EXPORT_SYMBOL(led_blink_set_oneshot); |
| 181 | |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 182 | void led_stop_software_blink(struct led_classdev *led_cdev) |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 183 | { |
Jiri Kosina | 9067359 | 2014-09-02 02:03:12 -0700 | [diff] [blame] | 184 | del_timer_sync(&led_cdev->blink_timer); |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 185 | led_cdev->blink_delay_on = 0; |
| 186 | led_cdev->blink_delay_off = 0; |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 187 | } |
| 188 | EXPORT_SYMBOL_GPL(led_stop_software_blink); |
| 189 | |
| 190 | void led_set_brightness(struct led_classdev *led_cdev, |
| 191 | enum led_brightness brightness) |
| 192 | { |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 193 | int ret = 0; |
| 194 | |
Stas Sergeev | 76931ed | 2015-05-14 08:24:02 -0700 | [diff] [blame] | 195 | /* delay brightness if soft-blink is active */ |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 196 | if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) { |
| 197 | led_cdev->delayed_set_value = brightness; |
Stas Sergeev | 76931ed | 2015-05-14 08:24:02 -0700 | [diff] [blame] | 198 | if (brightness == LED_OFF) |
| 199 | schedule_work(&led_cdev->set_brightness_work); |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 200 | return; |
| 201 | } |
Fabio Baltieri | 4378648 | 2012-06-07 03:12:34 +0800 | [diff] [blame] | 202 | |
Jacek Anaszewski | 4d71a4a | 2014-11-14 02:50:18 -0800 | [diff] [blame] | 203 | if (led_cdev->flags & SET_BRIGHTNESS_ASYNC) { |
| 204 | led_set_brightness_async(led_cdev, brightness); |
| 205 | return; |
| 206 | } else if (led_cdev->flags & SET_BRIGHTNESS_SYNC) |
| 207 | ret = led_set_brightness_sync(led_cdev, brightness); |
| 208 | else |
| 209 | ret = -EINVAL; |
| 210 | |
| 211 | if (ret < 0) |
| 212 | dev_dbg(led_cdev->dev, "Setting LED brightness failed (%d)\n", |
| 213 | ret); |
Bryan Wu | a403d93 | 2012-03-23 15:02:14 -0700 | [diff] [blame] | 214 | } |
Shuah Khan | 19cd67e | 2012-06-14 04:34:30 +0800 | [diff] [blame] | 215 | EXPORT_SYMBOL(led_set_brightness); |
Jacek Anaszewski | 3ef7de5 | 2014-08-20 06:41:55 -0700 | [diff] [blame] | 216 | |
| 217 | int led_update_brightness(struct led_classdev *led_cdev) |
| 218 | { |
| 219 | int ret = 0; |
| 220 | |
| 221 | if (led_cdev->brightness_get) { |
| 222 | ret = led_cdev->brightness_get(led_cdev); |
| 223 | if (ret >= 0) { |
| 224 | led_cdev->brightness = ret; |
| 225 | return 0; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | EXPORT_SYMBOL(led_update_brightness); |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 232 | |
| 233 | /* Caller must ensure led_cdev->led_access held */ |
| 234 | void led_sysfs_disable(struct led_classdev *led_cdev) |
| 235 | { |
| 236 | lockdep_assert_held(&led_cdev->led_access); |
| 237 | |
| 238 | led_cdev->flags |= LED_SYSFS_DISABLE; |
| 239 | } |
| 240 | EXPORT_SYMBOL_GPL(led_sysfs_disable); |
| 241 | |
| 242 | /* Caller must ensure led_cdev->led_access held */ |
| 243 | void led_sysfs_enable(struct led_classdev *led_cdev) |
| 244 | { |
| 245 | lockdep_assert_held(&led_cdev->led_access); |
| 246 | |
| 247 | led_cdev->flags &= ~LED_SYSFS_DISABLE; |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(led_sysfs_enable); |