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/leds.h> 16 #include <linux/list.h> 17 #include <linux/module.h> 18 #include <linux/mutex.h> 19 #include <linux/of.h> 20 #include <linux/rwsem.h> 21 #include <linux/slab.h> 22 #include "leds.h" 23 24 DECLARE_RWSEM(leds_list_lock); 25 EXPORT_SYMBOL_GPL(leds_list_lock); 26 27 LIST_HEAD(leds_list); 28 EXPORT_SYMBOL_GPL(leds_list); 29 30 static int __led_set_brightness(struct led_classdev *led_cdev, 31 enum led_brightness value) 32 { 33 if (!led_cdev->brightness_set) 34 return -ENOTSUPP; 35 36 led_cdev->brightness_set(led_cdev, value); 37 38 return 0; 39 } 40 41 static int __led_set_brightness_blocking(struct led_classdev *led_cdev, 42 enum led_brightness value) 43 { 44 if (!led_cdev->brightness_set_blocking) 45 return -ENOTSUPP; 46 47 return led_cdev->brightness_set_blocking(led_cdev, value); 48 } 49 50 static void led_timer_function(struct timer_list *t) 51 { 52 struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer); 53 unsigned long brightness; 54 unsigned long delay; 55 56 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) { 57 led_set_brightness_nosleep(led_cdev, LED_OFF); 58 clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 59 return; 60 } 61 62 if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP, 63 &led_cdev->work_flags)) { 64 clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 65 return; 66 } 67 68 brightness = led_get_brightness(led_cdev); 69 if (!brightness) { 70 /* Time to switch the LED on. */ 71 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE, 72 &led_cdev->work_flags)) 73 brightness = led_cdev->new_blink_brightness; 74 else 75 brightness = led_cdev->blink_brightness; 76 delay = led_cdev->blink_delay_on; 77 } else { 78 /* Store the current brightness value to be able 79 * to restore it when the delay_off period is over. 80 */ 81 led_cdev->blink_brightness = brightness; 82 brightness = LED_OFF; 83 delay = led_cdev->blink_delay_off; 84 } 85 86 led_set_brightness_nosleep(led_cdev, brightness); 87 88 /* Return in next iteration if led is in one-shot mode and we are in 89 * the final blink state so that the led is toggled each delay_on + 90 * delay_off milliseconds in worst case. 91 */ 92 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) { 93 if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) { 94 if (brightness) 95 set_bit(LED_BLINK_ONESHOT_STOP, 96 &led_cdev->work_flags); 97 } else { 98 if (!brightness) 99 set_bit(LED_BLINK_ONESHOT_STOP, 100 &led_cdev->work_flags); 101 } 102 } 103 104 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay)); 105 } 106 107 static void set_brightness_delayed(struct work_struct *ws) 108 { 109 struct led_classdev *led_cdev = 110 container_of(ws, struct led_classdev, set_brightness_work); 111 int ret = 0; 112 113 if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) { 114 led_cdev->delayed_set_value = LED_OFF; 115 led_stop_software_blink(led_cdev); 116 } 117 118 ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value); 119 if (ret == -ENOTSUPP) 120 ret = __led_set_brightness_blocking(led_cdev, 121 led_cdev->delayed_set_value); 122 if (ret < 0 && 123 /* LED HW might have been unplugged, therefore don't warn */ 124 !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) && 125 (led_cdev->flags & LED_HW_PLUGGABLE))) 126 dev_err(led_cdev->dev, 127 "Setting an LED's brightness failed (%d)\n", ret); 128 } 129 130 static void led_set_software_blink(struct led_classdev *led_cdev, 131 unsigned long delay_on, 132 unsigned long delay_off) 133 { 134 int current_brightness; 135 136 current_brightness = led_get_brightness(led_cdev); 137 if (current_brightness) 138 led_cdev->blink_brightness = current_brightness; 139 if (!led_cdev->blink_brightness) 140 led_cdev->blink_brightness = led_cdev->max_brightness; 141 142 led_cdev->blink_delay_on = delay_on; 143 led_cdev->blink_delay_off = delay_off; 144 145 /* never on - just set to off */ 146 if (!delay_on) { 147 led_set_brightness_nosleep(led_cdev, LED_OFF); 148 return; 149 } 150 151 /* never off - just set to brightness */ 152 if (!delay_off) { 153 led_set_brightness_nosleep(led_cdev, 154 led_cdev->blink_brightness); 155 return; 156 } 157 158 set_bit(LED_BLINK_SW, &led_cdev->work_flags); 159 mod_timer(&led_cdev->blink_timer, jiffies + 1); 160 } 161 162 163 static void led_blink_setup(struct led_classdev *led_cdev, 164 unsigned long *delay_on, 165 unsigned long *delay_off) 166 { 167 /* 168 * If "set brightness to 0" is pending in workqueue, we don't 169 * want that to be reordered after blink_set() 170 */ 171 flush_work(&led_cdev->set_brightness_work); 172 if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && 173 led_cdev->blink_set && 174 !led_cdev->blink_set(led_cdev, delay_on, delay_off)) 175 return; 176 177 /* blink with 1 Hz as default if nothing specified */ 178 if (!*delay_on && !*delay_off) 179 *delay_on = *delay_off = 500; 180 181 led_set_software_blink(led_cdev, *delay_on, *delay_off); 182 } 183 184 void led_init_core(struct led_classdev *led_cdev) 185 { 186 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed); 187 188 timer_setup(&led_cdev->blink_timer, led_timer_function, 0); 189 } 190 EXPORT_SYMBOL_GPL(led_init_core); 191 192 void led_blink_set(struct led_classdev *led_cdev, 193 unsigned long *delay_on, 194 unsigned long *delay_off) 195 { 196 del_timer_sync(&led_cdev->blink_timer); 197 198 clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 199 clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); 200 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); 201 202 led_blink_setup(led_cdev, delay_on, delay_off); 203 } 204 EXPORT_SYMBOL_GPL(led_blink_set); 205 206 void led_blink_set_oneshot(struct led_classdev *led_cdev, 207 unsigned long *delay_on, 208 unsigned long *delay_off, 209 int invert) 210 { 211 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && 212 timer_pending(&led_cdev->blink_timer)) 213 return; 214 215 set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags); 216 clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags); 217 218 if (invert) 219 set_bit(LED_BLINK_INVERT, &led_cdev->work_flags); 220 else 221 clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags); 222 223 led_blink_setup(led_cdev, delay_on, delay_off); 224 } 225 EXPORT_SYMBOL_GPL(led_blink_set_oneshot); 226 227 void led_stop_software_blink(struct led_classdev *led_cdev) 228 { 229 del_timer_sync(&led_cdev->blink_timer); 230 led_cdev->blink_delay_on = 0; 231 led_cdev->blink_delay_off = 0; 232 clear_bit(LED_BLINK_SW, &led_cdev->work_flags); 233 } 234 EXPORT_SYMBOL_GPL(led_stop_software_blink); 235 236 void led_set_brightness(struct led_classdev *led_cdev, 237 enum led_brightness brightness) 238 { 239 /* 240 * If software blink is active, delay brightness setting 241 * until the next timer tick. 242 */ 243 if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) { 244 /* 245 * If we need to disable soft blinking delegate this to the 246 * work queue task to avoid problems in case we are called 247 * from hard irq context. 248 */ 249 if (brightness == LED_OFF) { 250 set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags); 251 schedule_work(&led_cdev->set_brightness_work); 252 } else { 253 set_bit(LED_BLINK_BRIGHTNESS_CHANGE, 254 &led_cdev->work_flags); 255 led_cdev->new_blink_brightness = brightness; 256 } 257 return; 258 } 259 260 led_set_brightness_nosleep(led_cdev, brightness); 261 } 262 EXPORT_SYMBOL_GPL(led_set_brightness); 263 264 void led_set_brightness_nopm(struct led_classdev *led_cdev, 265 enum led_brightness value) 266 { 267 /* Use brightness_set op if available, it is guaranteed not to sleep */ 268 if (!__led_set_brightness(led_cdev, value)) 269 return; 270 271 /* If brightness setting can sleep, delegate it to a work queue task */ 272 led_cdev->delayed_set_value = value; 273 schedule_work(&led_cdev->set_brightness_work); 274 } 275 EXPORT_SYMBOL_GPL(led_set_brightness_nopm); 276 277 void led_set_brightness_nosleep(struct led_classdev *led_cdev, 278 enum led_brightness value) 279 { 280 led_cdev->brightness = min(value, led_cdev->max_brightness); 281 282 if (led_cdev->flags & LED_SUSPENDED) 283 return; 284 285 led_set_brightness_nopm(led_cdev, led_cdev->brightness); 286 } 287 EXPORT_SYMBOL_GPL(led_set_brightness_nosleep); 288 289 int led_set_brightness_sync(struct led_classdev *led_cdev, 290 enum led_brightness value) 291 { 292 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) 293 return -EBUSY; 294 295 led_cdev->brightness = min(value, led_cdev->max_brightness); 296 297 if (led_cdev->flags & LED_SUSPENDED) 298 return 0; 299 300 return __led_set_brightness_blocking(led_cdev, led_cdev->brightness); 301 } 302 EXPORT_SYMBOL_GPL(led_set_brightness_sync); 303 304 int led_update_brightness(struct led_classdev *led_cdev) 305 { 306 int ret = 0; 307 308 if (led_cdev->brightness_get) { 309 ret = led_cdev->brightness_get(led_cdev); 310 if (ret >= 0) { 311 led_cdev->brightness = ret; 312 return 0; 313 } 314 } 315 316 return ret; 317 } 318 EXPORT_SYMBOL_GPL(led_update_brightness); 319 320 u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size) 321 { 322 struct device_node *np = dev_of_node(led_cdev->dev); 323 u32 *pattern; 324 int count; 325 326 if (!np) 327 return NULL; 328 329 count = of_property_count_u32_elems(np, "led-pattern"); 330 if (count < 0) 331 return NULL; 332 333 pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL); 334 if (!pattern) 335 return NULL; 336 337 if (of_property_read_u32_array(np, "led-pattern", pattern, count)) { 338 kfree(pattern); 339 return NULL; 340 } 341 342 *size = count; 343 344 return pattern; 345 } 346 EXPORT_SYMBOL_GPL(led_get_default_pattern); 347 348 /* Caller must ensure led_cdev->led_access held */ 349 void led_sysfs_disable(struct led_classdev *led_cdev) 350 { 351 lockdep_assert_held(&led_cdev->led_access); 352 353 led_cdev->flags |= LED_SYSFS_DISABLE; 354 } 355 EXPORT_SYMBOL_GPL(led_sysfs_disable); 356 357 /* Caller must ensure led_cdev->led_access held */ 358 void led_sysfs_enable(struct led_classdev *led_cdev) 359 { 360 lockdep_assert_held(&led_cdev->led_access); 361 362 led_cdev->flags &= ~LED_SYSFS_DISABLE; 363 } 364 EXPORT_SYMBOL_GPL(led_sysfs_enable); 365