1/* 2 * LED Class Core 3 * 4 * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu> 5 * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 12#include <linux/ctype.h> 13#include <linux/device.h> 14#include <linux/err.h> 15#include <linux/init.h> 16#include <linux/kernel.h> 17#include <linux/leds.h> 18#include <linux/list.h> 19#include <linux/module.h> 20#include <linux/slab.h> 21#include <linux/spinlock.h> 22#include <linux/timer.h> 23#include "leds.h" 24 25static struct class *leds_class; 26 27static ssize_t brightness_show(struct device *dev, 28 struct device_attribute *attr, char *buf) 29{ 30 struct led_classdev *led_cdev = dev_get_drvdata(dev); 31 32 /* no lock needed for this */ 33 led_update_brightness(led_cdev); 34 35 return sprintf(buf, "%u\n", led_cdev->brightness); 36} 37 38static ssize_t brightness_store(struct device *dev, 39 struct device_attribute *attr, const char *buf, size_t size) 40{ 41 struct led_classdev *led_cdev = dev_get_drvdata(dev); 42 unsigned long state; 43 ssize_t ret; 44 45 mutex_lock(&led_cdev->led_access); 46 47 if (led_sysfs_is_disabled(led_cdev)) { 48 ret = -EBUSY; 49 goto unlock; 50 } 51 52 ret = kstrtoul(buf, 10, &state); 53 if (ret) 54 goto unlock; 55 56 if (state == LED_OFF) 57 led_trigger_remove(led_cdev); 58 led_set_brightness(led_cdev, state); 59 60 ret = size; 61unlock: 62 mutex_unlock(&led_cdev->led_access); 63 return ret; 64} 65static DEVICE_ATTR_RW(brightness); 66 67static ssize_t max_brightness_show(struct device *dev, 68 struct device_attribute *attr, char *buf) 69{ 70 struct led_classdev *led_cdev = dev_get_drvdata(dev); 71 72 return sprintf(buf, "%u\n", led_cdev->max_brightness); 73} 74static DEVICE_ATTR_RO(max_brightness); 75 76#ifdef CONFIG_LEDS_TRIGGERS 77static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store); 78static struct attribute *led_trigger_attrs[] = { 79 &dev_attr_trigger.attr, 80 NULL, 81}; 82static const struct attribute_group led_trigger_group = { 83 .attrs = led_trigger_attrs, 84}; 85#endif 86 87static struct attribute *led_class_attrs[] = { 88 &dev_attr_brightness.attr, 89 &dev_attr_max_brightness.attr, 90 NULL, 91}; 92 93static const struct attribute_group led_group = { 94 .attrs = led_class_attrs, 95}; 96 97static const struct attribute_group *led_groups[] = { 98 &led_group, 99#ifdef CONFIG_LEDS_TRIGGERS 100 &led_trigger_group, 101#endif 102 NULL, 103}; 104 105static void led_timer_function(unsigned long data) 106{ 107 struct led_classdev *led_cdev = (void *)data; 108 unsigned long brightness; 109 unsigned long delay; 110 111 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) { 112 led_set_brightness_async(led_cdev, LED_OFF); 113 return; 114 } 115 116 if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) { 117 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP; 118 return; 119 } 120 121 brightness = led_get_brightness(led_cdev); 122 if (!brightness) { 123 /* Time to switch the LED on. */ 124 brightness = led_cdev->blink_brightness; 125 delay = led_cdev->blink_delay_on; 126 } else { 127 /* Store the current brightness value to be able 128 * to restore it when the delay_off period is over. 129 */ 130 led_cdev->blink_brightness = brightness; 131 brightness = LED_OFF; 132 delay = led_cdev->blink_delay_off; 133 } 134 135 led_set_brightness_async(led_cdev, brightness); 136 137 /* Return in next iteration if led is in one-shot mode and we are in 138 * the final blink state so that the led is toggled each delay_on + 139 * delay_off milliseconds in worst case. 140 */ 141 if (led_cdev->flags & LED_BLINK_ONESHOT) { 142 if (led_cdev->flags & LED_BLINK_INVERT) { 143 if (brightness) 144 led_cdev->flags |= LED_BLINK_ONESHOT_STOP; 145 } else { 146 if (!brightness) 147 led_cdev->flags |= LED_BLINK_ONESHOT_STOP; 148 } 149 } 150 151 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay)); 152} 153 154static void set_brightness_delayed(struct work_struct *ws) 155{ 156 struct led_classdev *led_cdev = 157 container_of(ws, struct led_classdev, set_brightness_work); 158 159 led_stop_software_blink(led_cdev); 160 161 led_set_brightness_async(led_cdev, led_cdev->delayed_set_value); 162} 163 164/** 165 * led_classdev_suspend - suspend an led_classdev. 166 * @led_cdev: the led_classdev to suspend. 167 */ 168void led_classdev_suspend(struct led_classdev *led_cdev) 169{ 170 led_cdev->flags |= LED_SUSPENDED; 171 led_cdev->brightness_set(led_cdev, 0); 172} 173EXPORT_SYMBOL_GPL(led_classdev_suspend); 174 175/** 176 * led_classdev_resume - resume an led_classdev. 177 * @led_cdev: the led_classdev to resume. 178 */ 179void led_classdev_resume(struct led_classdev *led_cdev) 180{ 181 led_cdev->brightness_set(led_cdev, led_cdev->brightness); 182 183 if (led_cdev->flash_resume) 184 led_cdev->flash_resume(led_cdev); 185 186 led_cdev->flags &= ~LED_SUSPENDED; 187} 188EXPORT_SYMBOL_GPL(led_classdev_resume); 189 190#ifdef CONFIG_PM_SLEEP 191static int led_suspend(struct device *dev) 192{ 193 struct led_classdev *led_cdev = dev_get_drvdata(dev); 194 195 if (led_cdev->flags & LED_CORE_SUSPENDRESUME) 196 led_classdev_suspend(led_cdev); 197 198 return 0; 199} 200 201static int led_resume(struct device *dev) 202{ 203 struct led_classdev *led_cdev = dev_get_drvdata(dev); 204 205 if (led_cdev->flags & LED_CORE_SUSPENDRESUME) 206 led_classdev_resume(led_cdev); 207 208 return 0; 209} 210#endif 211 212static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume); 213 214static int match_name(struct device *dev, const void *data) 215{ 216 if (!dev_name(dev)) 217 return 0; 218 return !strcmp(dev_name(dev), (char *)data); 219} 220 221static int led_classdev_next_name(const char *init_name, char *name, 222 size_t len) 223{ 224 unsigned int i = 0; 225 int ret = 0; 226 struct device *dev; 227 228 strlcpy(name, init_name, len); 229 230 while ((ret < len) && 231 (dev = class_find_device(leds_class, NULL, name, match_name))) { 232 put_device(dev); 233 ret = snprintf(name, len, "%s_%u", init_name, ++i); 234 } 235 236 if (ret >= len) 237 return -ENOMEM; 238 239 return i; 240} 241 242/** 243 * led_classdev_register - register a new object of led_classdev class. 244 * @parent: The device to register. 245 * @led_cdev: the led_classdev structure for this device. 246 */ 247int led_classdev_register(struct device *parent, struct led_classdev *led_cdev) 248{ 249 char name[64]; 250 int ret; 251 252 ret = led_classdev_next_name(led_cdev->name, name, sizeof(name)); 253 if (ret < 0) 254 return ret; 255 256 led_cdev->dev = device_create_with_groups(leds_class, parent, 0, 257 led_cdev, led_cdev->groups, "%s", name); 258 if (IS_ERR(led_cdev->dev)) 259 return PTR_ERR(led_cdev->dev); 260 261 if (ret) 262 dev_warn(parent, "Led %s renamed to %s due to name collision", 263 led_cdev->name, dev_name(led_cdev->dev)); 264 265#ifdef CONFIG_LEDS_TRIGGERS 266 init_rwsem(&led_cdev->trigger_lock); 267#endif 268 mutex_init(&led_cdev->led_access); 269 /* add to the list of leds */ 270 down_write(&leds_list_lock); 271 list_add_tail(&led_cdev->node, &leds_list); 272 up_write(&leds_list_lock); 273 274 if (!led_cdev->max_brightness) 275 led_cdev->max_brightness = LED_FULL; 276 277 led_cdev->flags |= SET_BRIGHTNESS_ASYNC; 278 279 led_update_brightness(led_cdev); 280 281 INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed); 282 283 setup_timer(&led_cdev->blink_timer, led_timer_function, 284 (unsigned long)led_cdev); 285 286#ifdef CONFIG_LEDS_TRIGGERS 287 led_trigger_set_default(led_cdev); 288#endif 289 290 dev_dbg(parent, "Registered led device: %s\n", 291 led_cdev->name); 292 293 return 0; 294} 295EXPORT_SYMBOL_GPL(led_classdev_register); 296 297/** 298 * led_classdev_unregister - unregisters a object of led_properties class. 299 * @led_cdev: the led device to unregister 300 * 301 * Unregisters a previously registered via led_classdev_register object. 302 */ 303void led_classdev_unregister(struct led_classdev *led_cdev) 304{ 305#ifdef CONFIG_LEDS_TRIGGERS 306 down_write(&led_cdev->trigger_lock); 307 if (led_cdev->trigger) 308 led_trigger_set(led_cdev, NULL); 309 up_write(&led_cdev->trigger_lock); 310#endif 311 312 cancel_work_sync(&led_cdev->set_brightness_work); 313 314 /* Stop blinking */ 315 led_stop_software_blink(led_cdev); 316 led_set_brightness(led_cdev, LED_OFF); 317 318 device_unregister(led_cdev->dev); 319 320 down_write(&leds_list_lock); 321 list_del(&led_cdev->node); 322 up_write(&leds_list_lock); 323 324 mutex_destroy(&led_cdev->led_access); 325} 326EXPORT_SYMBOL_GPL(led_classdev_unregister); 327 328static void devm_led_classdev_release(struct device *dev, void *res) 329{ 330 led_classdev_unregister(*(struct led_classdev **)res); 331} 332 333/** 334 * devm_led_classdev_register - resource managed led_classdev_register() 335 * @parent: The device to register. 336 * @led_cdev: the led_classdev structure for this device. 337 */ 338int devm_led_classdev_register(struct device *parent, 339 struct led_classdev *led_cdev) 340{ 341 struct led_classdev **dr; 342 int rc; 343 344 dr = devres_alloc(devm_led_classdev_release, sizeof(*dr), GFP_KERNEL); 345 if (!dr) 346 return -ENOMEM; 347 348 rc = led_classdev_register(parent, led_cdev); 349 if (rc) { 350 devres_free(dr); 351 return rc; 352 } 353 354 *dr = led_cdev; 355 devres_add(parent, dr); 356 357 return 0; 358} 359EXPORT_SYMBOL_GPL(devm_led_classdev_register); 360 361static int devm_led_classdev_match(struct device *dev, void *res, void *data) 362{ 363 struct led_cdev **p = res; 364 365 if (WARN_ON(!p || !*p)) 366 return 0; 367 368 return *p == data; 369} 370 371/** 372 * devm_led_classdev_unregister() - resource managed led_classdev_unregister() 373 * @parent: The device to unregister. 374 * @led_cdev: the led_classdev structure for this device. 375 */ 376void devm_led_classdev_unregister(struct device *dev, 377 struct led_classdev *led_cdev) 378{ 379 WARN_ON(devres_release(dev, 380 devm_led_classdev_release, 381 devm_led_classdev_match, led_cdev)); 382} 383EXPORT_SYMBOL_GPL(devm_led_classdev_unregister); 384 385static int __init leds_init(void) 386{ 387 leds_class = class_create(THIS_MODULE, "leds"); 388 if (IS_ERR(leds_class)) 389 return PTR_ERR(leds_class); 390 leds_class->pm = &leds_class_dev_pm_ops; 391 leds_class->dev_groups = led_groups; 392 return 0; 393} 394 395static void __exit leds_exit(void) 396{ 397 class_destroy(leds_class); 398} 399 400subsys_initcall(leds_init); 401module_exit(leds_exit); 402 403MODULE_AUTHOR("John Lenz, Richard Purdie"); 404MODULE_LICENSE("GPL"); 405MODULE_DESCRIPTION("LED Class Interface"); 406