1 /* 2 * Backlight Lowlevel Control Abstraction 3 * 4 * Copyright (C) 2003,2004 Hewlett-Packard Company 5 * 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/device.h> 13 #include <linux/backlight.h> 14 #include <linux/notifier.h> 15 #include <linux/ctype.h> 16 #include <linux/err.h> 17 #include <linux/fb.h> 18 #include <linux/slab.h> 19 20 #ifdef CONFIG_PMAC_BACKLIGHT 21 #include <asm/backlight.h> 22 #endif 23 24 static struct list_head backlight_dev_list; 25 static struct mutex backlight_dev_list_mutex; 26 27 static const char *const backlight_types[] = { 28 [BACKLIGHT_RAW] = "raw", 29 [BACKLIGHT_PLATFORM] = "platform", 30 [BACKLIGHT_FIRMWARE] = "firmware", 31 }; 32 33 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \ 34 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)) 35 /* This callback gets called when something important happens inside a 36 * framebuffer driver. We're looking if that important event is blanking, 37 * and if it is and necessary, we're switching backlight power as well ... 38 */ 39 static int fb_notifier_callback(struct notifier_block *self, 40 unsigned long event, void *data) 41 { 42 struct backlight_device *bd; 43 struct fb_event *evdata = data; 44 int node = evdata->info->node; 45 int fb_blank = 0; 46 47 /* If we aren't interested in this event, skip it immediately ... */ 48 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK) 49 return 0; 50 51 bd = container_of(self, struct backlight_device, fb_notif); 52 mutex_lock(&bd->ops_lock); 53 if (bd->ops) 54 if (!bd->ops->check_fb || 55 bd->ops->check_fb(bd, evdata->info)) { 56 fb_blank = *(int *)evdata->data; 57 if (fb_blank == FB_BLANK_UNBLANK && 58 !bd->fb_bl_on[node]) { 59 bd->fb_bl_on[node] = true; 60 if (!bd->use_count++) { 61 bd->props.state &= ~BL_CORE_FBBLANK; 62 bd->props.fb_blank = FB_BLANK_UNBLANK; 63 backlight_update_status(bd); 64 } 65 } else if (fb_blank != FB_BLANK_UNBLANK && 66 bd->fb_bl_on[node]) { 67 bd->fb_bl_on[node] = false; 68 if (!(--bd->use_count)) { 69 bd->props.state |= BL_CORE_FBBLANK; 70 bd->props.fb_blank = fb_blank; 71 backlight_update_status(bd); 72 } 73 } 74 } 75 mutex_unlock(&bd->ops_lock); 76 return 0; 77 } 78 79 static int backlight_register_fb(struct backlight_device *bd) 80 { 81 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif)); 82 bd->fb_notif.notifier_call = fb_notifier_callback; 83 84 return fb_register_client(&bd->fb_notif); 85 } 86 87 static void backlight_unregister_fb(struct backlight_device *bd) 88 { 89 fb_unregister_client(&bd->fb_notif); 90 } 91 #else 92 static inline int backlight_register_fb(struct backlight_device *bd) 93 { 94 return 0; 95 } 96 97 static inline void backlight_unregister_fb(struct backlight_device *bd) 98 { 99 } 100 #endif /* CONFIG_FB */ 101 102 static void backlight_generate_event(struct backlight_device *bd, 103 enum backlight_update_reason reason) 104 { 105 char *envp[2]; 106 107 switch (reason) { 108 case BACKLIGHT_UPDATE_SYSFS: 109 envp[0] = "SOURCE=sysfs"; 110 break; 111 case BACKLIGHT_UPDATE_HOTKEY: 112 envp[0] = "SOURCE=hotkey"; 113 break; 114 default: 115 envp[0] = "SOURCE=unknown"; 116 break; 117 } 118 envp[1] = NULL; 119 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp); 120 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness"); 121 } 122 123 static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr, 124 char *buf) 125 { 126 struct backlight_device *bd = to_backlight_device(dev); 127 128 return sprintf(buf, "%d\n", bd->props.power); 129 } 130 131 static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr, 132 const char *buf, size_t count) 133 { 134 int rc; 135 struct backlight_device *bd = to_backlight_device(dev); 136 unsigned long power; 137 138 rc = kstrtoul(buf, 0, &power); 139 if (rc) 140 return rc; 141 142 rc = -ENXIO; 143 mutex_lock(&bd->ops_lock); 144 if (bd->ops) { 145 pr_debug("set power to %lu\n", power); 146 if (bd->props.power != power) { 147 bd->props.power = power; 148 backlight_update_status(bd); 149 } 150 rc = count; 151 } 152 mutex_unlock(&bd->ops_lock); 153 154 return rc; 155 } 156 static DEVICE_ATTR_RW(bl_power); 157 158 static ssize_t brightness_show(struct device *dev, 159 struct device_attribute *attr, char *buf) 160 { 161 struct backlight_device *bd = to_backlight_device(dev); 162 163 return sprintf(buf, "%d\n", bd->props.brightness); 164 } 165 166 static ssize_t brightness_store(struct device *dev, 167 struct device_attribute *attr, const char *buf, size_t count) 168 { 169 int rc; 170 struct backlight_device *bd = to_backlight_device(dev); 171 unsigned long brightness; 172 173 rc = kstrtoul(buf, 0, &brightness); 174 if (rc) 175 return rc; 176 177 rc = -ENXIO; 178 179 mutex_lock(&bd->ops_lock); 180 if (bd->ops) { 181 if (brightness > bd->props.max_brightness) 182 rc = -EINVAL; 183 else { 184 pr_debug("set brightness to %lu\n", brightness); 185 bd->props.brightness = brightness; 186 backlight_update_status(bd); 187 rc = count; 188 } 189 } 190 mutex_unlock(&bd->ops_lock); 191 192 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS); 193 194 return rc; 195 } 196 static DEVICE_ATTR_RW(brightness); 197 198 static ssize_t type_show(struct device *dev, struct device_attribute *attr, 199 char *buf) 200 { 201 struct backlight_device *bd = to_backlight_device(dev); 202 203 return sprintf(buf, "%s\n", backlight_types[bd->props.type]); 204 } 205 static DEVICE_ATTR_RO(type); 206 207 static ssize_t max_brightness_show(struct device *dev, 208 struct device_attribute *attr, char *buf) 209 { 210 struct backlight_device *bd = to_backlight_device(dev); 211 212 return sprintf(buf, "%d\n", bd->props.max_brightness); 213 } 214 static DEVICE_ATTR_RO(max_brightness); 215 216 static ssize_t actual_brightness_show(struct device *dev, 217 struct device_attribute *attr, char *buf) 218 { 219 int rc = -ENXIO; 220 struct backlight_device *bd = to_backlight_device(dev); 221 222 mutex_lock(&bd->ops_lock); 223 if (bd->ops && bd->ops->get_brightness) 224 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd)); 225 mutex_unlock(&bd->ops_lock); 226 227 return rc; 228 } 229 static DEVICE_ATTR_RO(actual_brightness); 230 231 static struct class *backlight_class; 232 233 #ifdef CONFIG_PM_SLEEP 234 static int backlight_suspend(struct device *dev) 235 { 236 struct backlight_device *bd = to_backlight_device(dev); 237 238 mutex_lock(&bd->ops_lock); 239 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) { 240 bd->props.state |= BL_CORE_SUSPENDED; 241 backlight_update_status(bd); 242 } 243 mutex_unlock(&bd->ops_lock); 244 245 return 0; 246 } 247 248 static int backlight_resume(struct device *dev) 249 { 250 struct backlight_device *bd = to_backlight_device(dev); 251 252 mutex_lock(&bd->ops_lock); 253 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) { 254 bd->props.state &= ~BL_CORE_SUSPENDED; 255 backlight_update_status(bd); 256 } 257 mutex_unlock(&bd->ops_lock); 258 259 return 0; 260 } 261 #endif 262 263 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend, 264 backlight_resume); 265 266 static void bl_device_release(struct device *dev) 267 { 268 struct backlight_device *bd = to_backlight_device(dev); 269 kfree(bd); 270 } 271 272 static struct attribute *bl_device_attrs[] = { 273 &dev_attr_bl_power.attr, 274 &dev_attr_brightness.attr, 275 &dev_attr_actual_brightness.attr, 276 &dev_attr_max_brightness.attr, 277 &dev_attr_type.attr, 278 NULL, 279 }; 280 ATTRIBUTE_GROUPS(bl_device); 281 282 /** 283 * backlight_force_update - tell the backlight subsystem that hardware state 284 * has changed 285 * @bd: the backlight device to update 286 * 287 * Updates the internal state of the backlight in response to a hardware event, 288 * and generate a uevent to notify userspace 289 */ 290 void backlight_force_update(struct backlight_device *bd, 291 enum backlight_update_reason reason) 292 { 293 mutex_lock(&bd->ops_lock); 294 if (bd->ops && bd->ops->get_brightness) 295 bd->props.brightness = bd->ops->get_brightness(bd); 296 mutex_unlock(&bd->ops_lock); 297 backlight_generate_event(bd, reason); 298 } 299 EXPORT_SYMBOL(backlight_force_update); 300 301 /** 302 * backlight_device_register - create and register a new object of 303 * backlight_device class. 304 * @name: the name of the new object(must be the same as the name of the 305 * respective framebuffer device). 306 * @parent: a pointer to the parent device 307 * @devdata: an optional pointer to be stored for private driver use. The 308 * methods may retrieve it by using bl_get_data(bd). 309 * @ops: the backlight operations structure. 310 * 311 * Creates and registers new backlight device. Returns either an 312 * ERR_PTR() or a pointer to the newly allocated device. 313 */ 314 struct backlight_device *backlight_device_register(const char *name, 315 struct device *parent, void *devdata, const struct backlight_ops *ops, 316 const struct backlight_properties *props) 317 { 318 struct backlight_device *new_bd; 319 int rc; 320 321 pr_debug("backlight_device_register: name=%s\n", name); 322 323 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL); 324 if (!new_bd) 325 return ERR_PTR(-ENOMEM); 326 327 mutex_init(&new_bd->update_lock); 328 mutex_init(&new_bd->ops_lock); 329 330 new_bd->dev.class = backlight_class; 331 new_bd->dev.parent = parent; 332 new_bd->dev.release = bl_device_release; 333 dev_set_name(&new_bd->dev, "%s", name); 334 dev_set_drvdata(&new_bd->dev, devdata); 335 336 /* Set default properties */ 337 if (props) { 338 memcpy(&new_bd->props, props, 339 sizeof(struct backlight_properties)); 340 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) { 341 WARN(1, "%s: invalid backlight type", name); 342 new_bd->props.type = BACKLIGHT_RAW; 343 } 344 } else { 345 new_bd->props.type = BACKLIGHT_RAW; 346 } 347 348 rc = device_register(&new_bd->dev); 349 if (rc) { 350 put_device(&new_bd->dev); 351 return ERR_PTR(rc); 352 } 353 354 rc = backlight_register_fb(new_bd); 355 if (rc) { 356 device_unregister(&new_bd->dev); 357 return ERR_PTR(rc); 358 } 359 360 new_bd->ops = ops; 361 362 #ifdef CONFIG_PMAC_BACKLIGHT 363 mutex_lock(&pmac_backlight_mutex); 364 if (!pmac_backlight) 365 pmac_backlight = new_bd; 366 mutex_unlock(&pmac_backlight_mutex); 367 #endif 368 369 mutex_lock(&backlight_dev_list_mutex); 370 list_add(&new_bd->entry, &backlight_dev_list); 371 mutex_unlock(&backlight_dev_list_mutex); 372 373 return new_bd; 374 } 375 EXPORT_SYMBOL(backlight_device_register); 376 377 bool backlight_device_registered(enum backlight_type type) 378 { 379 bool found = false; 380 struct backlight_device *bd; 381 382 mutex_lock(&backlight_dev_list_mutex); 383 list_for_each_entry(bd, &backlight_dev_list, entry) { 384 if (bd->props.type == type) { 385 found = true; 386 break; 387 } 388 } 389 mutex_unlock(&backlight_dev_list_mutex); 390 391 return found; 392 } 393 EXPORT_SYMBOL(backlight_device_registered); 394 395 /** 396 * backlight_device_unregister - unregisters a backlight device object. 397 * @bd: the backlight device object to be unregistered and freed. 398 * 399 * Unregisters a previously registered via backlight_device_register object. 400 */ 401 void backlight_device_unregister(struct backlight_device *bd) 402 { 403 if (!bd) 404 return; 405 406 mutex_lock(&backlight_dev_list_mutex); 407 list_del(&bd->entry); 408 mutex_unlock(&backlight_dev_list_mutex); 409 410 #ifdef CONFIG_PMAC_BACKLIGHT 411 mutex_lock(&pmac_backlight_mutex); 412 if (pmac_backlight == bd) 413 pmac_backlight = NULL; 414 mutex_unlock(&pmac_backlight_mutex); 415 #endif 416 mutex_lock(&bd->ops_lock); 417 bd->ops = NULL; 418 mutex_unlock(&bd->ops_lock); 419 420 backlight_unregister_fb(bd); 421 device_unregister(&bd->dev); 422 } 423 EXPORT_SYMBOL(backlight_device_unregister); 424 425 static void devm_backlight_device_release(struct device *dev, void *res) 426 { 427 struct backlight_device *backlight = *(struct backlight_device **)res; 428 429 backlight_device_unregister(backlight); 430 } 431 432 static int devm_backlight_device_match(struct device *dev, void *res, 433 void *data) 434 { 435 struct backlight_device **r = res; 436 437 return *r == data; 438 } 439 440 /** 441 * devm_backlight_device_register - resource managed backlight_device_register() 442 * @dev: the device to register 443 * @name: the name of the device 444 * @parent: a pointer to the parent device 445 * @devdata: an optional pointer to be stored for private driver use 446 * @ops: the backlight operations structure 447 * @props: the backlight properties 448 * 449 * @return a struct backlight on success, or an ERR_PTR on error 450 * 451 * Managed backlight_device_register(). The backlight_device returned 452 * from this function are automatically freed on driver detach. 453 * See backlight_device_register() for more information. 454 */ 455 struct backlight_device *devm_backlight_device_register(struct device *dev, 456 const char *name, struct device *parent, void *devdata, 457 const struct backlight_ops *ops, 458 const struct backlight_properties *props) 459 { 460 struct backlight_device **ptr, *backlight; 461 462 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr), 463 GFP_KERNEL); 464 if (!ptr) 465 return ERR_PTR(-ENOMEM); 466 467 backlight = backlight_device_register(name, parent, devdata, ops, 468 props); 469 if (!IS_ERR(backlight)) { 470 *ptr = backlight; 471 devres_add(dev, ptr); 472 } else { 473 devres_free(ptr); 474 } 475 476 return backlight; 477 } 478 EXPORT_SYMBOL(devm_backlight_device_register); 479 480 /** 481 * devm_backlight_device_unregister - resource managed backlight_device_unregister() 482 * @dev: the device to unregister 483 * @bd: the backlight device to unregister 484 * 485 * Deallocated a backlight allocated with devm_backlight_device_register(). 486 * Normally this function will not need to be called and the resource management 487 * code will ensure that the resource is freed. 488 */ 489 void devm_backlight_device_unregister(struct device *dev, 490 struct backlight_device *bd) 491 { 492 int rc; 493 494 rc = devres_release(dev, devm_backlight_device_release, 495 devm_backlight_device_match, bd); 496 WARN_ON(rc); 497 } 498 EXPORT_SYMBOL(devm_backlight_device_unregister); 499 500 #ifdef CONFIG_OF 501 static int of_parent_match(struct device *dev, const void *data) 502 { 503 return dev->parent && dev->parent->of_node == data; 504 } 505 506 /** 507 * of_find_backlight_by_node() - find backlight device by device-tree node 508 * @node: device-tree node of the backlight device 509 * 510 * Returns a pointer to the backlight device corresponding to the given DT 511 * node or NULL if no such backlight device exists or if the device hasn't 512 * been probed yet. 513 * 514 * This function obtains a reference on the backlight device and it is the 515 * caller's responsibility to drop the reference by calling put_device() on 516 * the backlight device's .dev field. 517 */ 518 struct backlight_device *of_find_backlight_by_node(struct device_node *node) 519 { 520 struct device *dev; 521 522 dev = class_find_device(backlight_class, NULL, node, of_parent_match); 523 524 return dev ? to_backlight_device(dev) : NULL; 525 } 526 EXPORT_SYMBOL(of_find_backlight_by_node); 527 #endif 528 529 static void __exit backlight_class_exit(void) 530 { 531 class_destroy(backlight_class); 532 } 533 534 static int __init backlight_class_init(void) 535 { 536 backlight_class = class_create(THIS_MODULE, "backlight"); 537 if (IS_ERR(backlight_class)) { 538 pr_warn("Unable to create backlight class; errno = %ld\n", 539 PTR_ERR(backlight_class)); 540 return PTR_ERR(backlight_class); 541 } 542 543 backlight_class->dev_groups = bl_device_groups; 544 backlight_class->pm = &backlight_class_dev_pm_ops; 545 INIT_LIST_HEAD(&backlight_dev_list); 546 mutex_init(&backlight_dev_list_mutex); 547 return 0; 548 } 549 550 /* 551 * if this is compiled into the kernel, we need to ensure that the 552 * class is registered before users of the class try to register lcd's 553 */ 554 postcore_initcall(backlight_class_init); 555 module_exit(backlight_class_exit); 556 557 MODULE_LICENSE("GPL"); 558 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); 559 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction"); 560