2 * Backlight Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
18 #include <linux/slab.h>
20 #ifdef CONFIG_PMAC_BACKLIGHT
21 #include <asm/backlight.h>
24 static struct list_head backlight_dev_list;
25 static struct mutex backlight_dev_list_mutex;
26 static struct blocking_notifier_head backlight_notifier;
28 static const char *const backlight_types[] = {
29 [BACKLIGHT_RAW] = "raw",
30 [BACKLIGHT_PLATFORM] = "platform",
31 [BACKLIGHT_FIRMWARE] = "firmware",
34 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
35 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
36 /* This callback gets called when something important happens inside a
37 * framebuffer driver. We're looking if that important event is blanking,
38 * and if it is and necessary, we're switching backlight power as well ...
40 static int fb_notifier_callback(struct notifier_block *self,
41 unsigned long event, void *data)
43 struct backlight_device *bd;
44 struct fb_event *evdata = data;
45 int node = evdata->info->node;
48 /* If we aren't interested in this event, skip it immediately ... */
49 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK &&
50 event != FB_EARLY_EVENT_BLANK)
53 bd = container_of(self, struct backlight_device, fb_notif);
54 mutex_lock(&bd->ops_lock);
56 if (!bd->ops->check_fb ||
57 bd->ops->check_fb(bd, evdata->info)) {
58 fb_blank = *(int *)evdata->data;
59 if (fb_blank == FB_BLANK_UNBLANK &&
60 !bd->fb_bl_on[node] &&
61 event != FB_EARLY_EVENT_BLANK) {
62 bd->fb_bl_on[node] = true;
63 if (!bd->use_count++) {
64 bd->props.state &= ~BL_CORE_FBBLANK;
65 bd->props.fb_blank = FB_BLANK_UNBLANK;
66 backlight_update_status(bd);
68 } else if (fb_blank != FB_BLANK_UNBLANK &&
70 event == FB_EARLY_EVENT_BLANK) {
71 bd->fb_bl_on[node] = false;
72 if (!(--bd->use_count)) {
73 bd->props.state |= BL_CORE_FBBLANK;
74 bd->props.fb_blank = fb_blank;
75 backlight_update_status(bd);
79 mutex_unlock(&bd->ops_lock);
83 static int backlight_register_fb(struct backlight_device *bd)
85 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
86 bd->fb_notif.notifier_call = fb_notifier_callback;
88 return fb_register_client(&bd->fb_notif);
91 static void backlight_unregister_fb(struct backlight_device *bd)
93 fb_unregister_client(&bd->fb_notif);
96 static inline int backlight_register_fb(struct backlight_device *bd)
101 static inline void backlight_unregister_fb(struct backlight_device *bd)
104 #endif /* CONFIG_FB */
106 static void backlight_generate_event(struct backlight_device *bd,
107 enum backlight_update_reason reason)
112 case BACKLIGHT_UPDATE_SYSFS:
113 envp[0] = "SOURCE=sysfs";
115 case BACKLIGHT_UPDATE_HOTKEY:
116 envp[0] = "SOURCE=hotkey";
119 envp[0] = "SOURCE=unknown";
123 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
124 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
127 static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
130 struct backlight_device *bd = to_backlight_device(dev);
132 return sprintf(buf, "%d\n", bd->props.power);
135 static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
136 const char *buf, size_t count)
139 struct backlight_device *bd = to_backlight_device(dev);
142 rc = kstrtoul(buf, 0, &power);
147 mutex_lock(&bd->ops_lock);
149 pr_debug("set power to %lu\n", power);
150 if (bd->props.power != power) {
151 bd->props.power = power;
152 backlight_update_status(bd);
156 mutex_unlock(&bd->ops_lock);
160 static DEVICE_ATTR_RW(bl_power);
162 static ssize_t brightness_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
165 struct backlight_device *bd = to_backlight_device(dev);
167 return sprintf(buf, "%d\n", bd->props.brightness);
170 int backlight_device_set_brightness(struct backlight_device *bd,
171 unsigned long brightness)
175 mutex_lock(&bd->ops_lock);
177 if (brightness > bd->props.max_brightness)
180 pr_debug("set brightness to %lu\n", brightness);
181 bd->props.brightness = brightness;
182 backlight_update_status(bd);
186 mutex_unlock(&bd->ops_lock);
188 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
192 EXPORT_SYMBOL(backlight_device_set_brightness);
194 static ssize_t brightness_store(struct device *dev,
195 struct device_attribute *attr, const char *buf, size_t count)
198 struct backlight_device *bd = to_backlight_device(dev);
199 unsigned long brightness;
201 rc = kstrtoul(buf, 0, &brightness);
205 rc = backlight_device_set_brightness(bd, brightness);
207 return rc ? rc : count;
209 static DEVICE_ATTR_RW(brightness);
211 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
214 struct backlight_device *bd = to_backlight_device(dev);
216 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
218 static DEVICE_ATTR_RO(type);
220 static ssize_t max_brightness_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
223 struct backlight_device *bd = to_backlight_device(dev);
225 return sprintf(buf, "%d\n", bd->props.max_brightness);
227 static DEVICE_ATTR_RO(max_brightness);
229 static ssize_t actual_brightness_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
233 struct backlight_device *bd = to_backlight_device(dev);
235 mutex_lock(&bd->ops_lock);
236 if (bd->ops && bd->ops->get_brightness)
237 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
239 rc = sprintf(buf, "%d\n", bd->props.brightness);
240 mutex_unlock(&bd->ops_lock);
244 static DEVICE_ATTR_RO(actual_brightness);
246 static struct class *backlight_class;
248 #ifdef CONFIG_PM_SLEEP
249 static int backlight_suspend(struct device *dev)
251 struct backlight_device *bd = to_backlight_device(dev);
253 mutex_lock(&bd->ops_lock);
254 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
255 bd->props.state |= BL_CORE_SUSPENDED;
256 backlight_update_status(bd);
258 mutex_unlock(&bd->ops_lock);
263 static int backlight_resume(struct device *dev)
265 struct backlight_device *bd = to_backlight_device(dev);
267 mutex_lock(&bd->ops_lock);
268 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
269 bd->props.state &= ~BL_CORE_SUSPENDED;
270 backlight_update_status(bd);
272 mutex_unlock(&bd->ops_lock);
278 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
281 static void bl_device_release(struct device *dev)
283 struct backlight_device *bd = to_backlight_device(dev);
287 static struct attribute *bl_device_attrs[] = {
288 &dev_attr_bl_power.attr,
289 &dev_attr_brightness.attr,
290 &dev_attr_actual_brightness.attr,
291 &dev_attr_max_brightness.attr,
295 ATTRIBUTE_GROUPS(bl_device);
298 * backlight_force_update - tell the backlight subsystem that hardware state
300 * @bd: the backlight device to update
302 * Updates the internal state of the backlight in response to a hardware event,
303 * and generate a uevent to notify userspace
305 void backlight_force_update(struct backlight_device *bd,
306 enum backlight_update_reason reason)
308 mutex_lock(&bd->ops_lock);
309 if (bd->ops && bd->ops->get_brightness)
310 bd->props.brightness = bd->ops->get_brightness(bd);
311 mutex_unlock(&bd->ops_lock);
312 backlight_generate_event(bd, reason);
314 EXPORT_SYMBOL(backlight_force_update);
317 * backlight_device_register - create and register a new object of
318 * backlight_device class.
319 * @name: the name of the new object(must be the same as the name of the
320 * respective framebuffer device).
321 * @parent: a pointer to the parent device
322 * @devdata: an optional pointer to be stored for private driver use. The
323 * methods may retrieve it by using bl_get_data(bd).
324 * @ops: the backlight operations structure.
326 * Creates and registers new backlight device. Returns either an
327 * ERR_PTR() or a pointer to the newly allocated device.
329 struct backlight_device *backlight_device_register(const char *name,
330 struct device *parent, void *devdata, const struct backlight_ops *ops,
331 const struct backlight_properties *props)
333 struct backlight_device *new_bd;
336 pr_debug("backlight_device_register: name=%s\n", name);
338 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
340 return ERR_PTR(-ENOMEM);
342 mutex_init(&new_bd->update_lock);
343 mutex_init(&new_bd->ops_lock);
345 new_bd->dev.class = backlight_class;
346 new_bd->dev.parent = parent;
347 new_bd->dev.release = bl_device_release;
348 dev_set_name(&new_bd->dev, "%s", name);
349 dev_set_drvdata(&new_bd->dev, devdata);
351 /* Set default properties */
353 memcpy(&new_bd->props, props,
354 sizeof(struct backlight_properties));
355 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
356 WARN(1, "%s: invalid backlight type", name);
357 new_bd->props.type = BACKLIGHT_RAW;
360 new_bd->props.type = BACKLIGHT_RAW;
363 rc = device_register(&new_bd->dev);
365 put_device(&new_bd->dev);
369 rc = backlight_register_fb(new_bd);
371 device_unregister(&new_bd->dev);
377 #ifdef CONFIG_PMAC_BACKLIGHT
378 mutex_lock(&pmac_backlight_mutex);
380 pmac_backlight = new_bd;
381 mutex_unlock(&pmac_backlight_mutex);
384 mutex_lock(&backlight_dev_list_mutex);
385 list_add(&new_bd->entry, &backlight_dev_list);
386 mutex_unlock(&backlight_dev_list_mutex);
388 blocking_notifier_call_chain(&backlight_notifier,
389 BACKLIGHT_REGISTERED, new_bd);
393 EXPORT_SYMBOL(backlight_device_register);
395 struct backlight_device *backlight_device_get_by_type(enum backlight_type type)
398 struct backlight_device *bd;
400 mutex_lock(&backlight_dev_list_mutex);
401 list_for_each_entry(bd, &backlight_dev_list, entry) {
402 if (bd->props.type == type) {
407 mutex_unlock(&backlight_dev_list_mutex);
409 return found ? bd : NULL;
411 EXPORT_SYMBOL(backlight_device_get_by_type);
414 * backlight_device_unregister - unregisters a backlight device object.
415 * @bd: the backlight device object to be unregistered and freed.
417 * Unregisters a previously registered via backlight_device_register object.
419 void backlight_device_unregister(struct backlight_device *bd)
424 mutex_lock(&backlight_dev_list_mutex);
425 list_del(&bd->entry);
426 mutex_unlock(&backlight_dev_list_mutex);
428 #ifdef CONFIG_PMAC_BACKLIGHT
429 mutex_lock(&pmac_backlight_mutex);
430 if (pmac_backlight == bd)
431 pmac_backlight = NULL;
432 mutex_unlock(&pmac_backlight_mutex);
435 blocking_notifier_call_chain(&backlight_notifier,
436 BACKLIGHT_UNREGISTERED, bd);
438 mutex_lock(&bd->ops_lock);
440 mutex_unlock(&bd->ops_lock);
442 backlight_unregister_fb(bd);
443 device_unregister(&bd->dev);
445 EXPORT_SYMBOL(backlight_device_unregister);
447 static void devm_backlight_device_release(struct device *dev, void *res)
449 struct backlight_device *backlight = *(struct backlight_device **)res;
451 backlight_device_unregister(backlight);
454 static int devm_backlight_device_match(struct device *dev, void *res,
457 struct backlight_device **r = res;
463 * backlight_register_notifier - get notified of backlight (un)registration
464 * @nb: notifier block with the notifier to call on backlight (un)registration
466 * @return 0 on success, otherwise a negative error code
468 * Register a notifier to get notified when backlight devices get registered
471 int backlight_register_notifier(struct notifier_block *nb)
473 return blocking_notifier_chain_register(&backlight_notifier, nb);
475 EXPORT_SYMBOL(backlight_register_notifier);
478 * backlight_unregister_notifier - unregister a backlight notifier
479 * @nb: notifier block to unregister
481 * @return 0 on success, otherwise a negative error code
483 * Register a notifier to get notified when backlight devices get registered
486 int backlight_unregister_notifier(struct notifier_block *nb)
488 return blocking_notifier_chain_unregister(&backlight_notifier, nb);
490 EXPORT_SYMBOL(backlight_unregister_notifier);
493 * devm_backlight_device_register - resource managed backlight_device_register()
494 * @dev: the device to register
495 * @name: the name of the device
496 * @parent: a pointer to the parent device
497 * @devdata: an optional pointer to be stored for private driver use
498 * @ops: the backlight operations structure
499 * @props: the backlight properties
501 * @return a struct backlight on success, or an ERR_PTR on error
503 * Managed backlight_device_register(). The backlight_device returned
504 * from this function are automatically freed on driver detach.
505 * See backlight_device_register() for more information.
507 struct backlight_device *devm_backlight_device_register(struct device *dev,
508 const char *name, struct device *parent, void *devdata,
509 const struct backlight_ops *ops,
510 const struct backlight_properties *props)
512 struct backlight_device **ptr, *backlight;
514 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
517 return ERR_PTR(-ENOMEM);
519 backlight = backlight_device_register(name, parent, devdata, ops,
521 if (!IS_ERR(backlight)) {
523 devres_add(dev, ptr);
530 EXPORT_SYMBOL(devm_backlight_device_register);
533 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
534 * @dev: the device to unregister
535 * @bd: the backlight device to unregister
537 * Deallocated a backlight allocated with devm_backlight_device_register().
538 * Normally this function will not need to be called and the resource management
539 * code will ensure that the resource is freed.
541 void devm_backlight_device_unregister(struct device *dev,
542 struct backlight_device *bd)
546 rc = devres_release(dev, devm_backlight_device_release,
547 devm_backlight_device_match, bd);
550 EXPORT_SYMBOL(devm_backlight_device_unregister);
553 static int of_parent_match(struct device *dev, const void *data)
555 return dev->parent && dev->parent->of_node == data;
559 * of_find_backlight_by_node() - find backlight device by device-tree node
560 * @node: device-tree node of the backlight device
562 * Returns a pointer to the backlight device corresponding to the given DT
563 * node or NULL if no such backlight device exists or if the device hasn't
566 * This function obtains a reference on the backlight device and it is the
567 * caller's responsibility to drop the reference by calling put_device() on
568 * the backlight device's .dev field.
570 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
574 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
576 return dev ? to_backlight_device(dev) : NULL;
578 EXPORT_SYMBOL(of_find_backlight_by_node);
581 static void __exit backlight_class_exit(void)
583 class_destroy(backlight_class);
586 static int __init backlight_class_init(void)
588 backlight_class = class_create(THIS_MODULE, "backlight");
589 if (IS_ERR(backlight_class)) {
590 pr_warn("Unable to create backlight class; errno = %ld\n",
591 PTR_ERR(backlight_class));
592 return PTR_ERR(backlight_class);
595 backlight_class->dev_groups = bl_device_groups;
596 backlight_class->pm = &backlight_class_dev_pm_ops;
597 INIT_LIST_HEAD(&backlight_dev_list);
598 mutex_init(&backlight_dev_list_mutex);
599 BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier);
605 * if this is compiled into the kernel, we need to ensure that the
606 * class is registered before users of the class try to register lcd's
608 postcore_initcall(backlight_class_init);
609 module_exit(backlight_class_exit);
611 MODULE_LICENSE("GPL");
612 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
613 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");