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
)
52 bd
= container_of(self
, struct backlight_device
, fb_notif
);
53 mutex_lock(&bd
->ops_lock
);
55 if (!bd
->ops
->check_fb
||
56 bd
->ops
->check_fb(bd
, evdata
->info
)) {
57 fb_blank
= *(int *)evdata
->data
;
58 if (fb_blank
== FB_BLANK_UNBLANK
&&
59 !bd
->fb_bl_on
[node
]) {
60 bd
->fb_bl_on
[node
] = true;
61 if (!bd
->use_count
++) {
62 bd
->props
.state
&= ~BL_CORE_FBBLANK
;
63 bd
->props
.fb_blank
= FB_BLANK_UNBLANK
;
64 backlight_update_status(bd
);
66 } else if (fb_blank
!= FB_BLANK_UNBLANK
&&
68 bd
->fb_bl_on
[node
] = false;
69 if (!(--bd
->use_count
)) {
70 bd
->props
.state
|= BL_CORE_FBBLANK
;
71 bd
->props
.fb_blank
= fb_blank
;
72 backlight_update_status(bd
);
76 mutex_unlock(&bd
->ops_lock
);
80 static int backlight_register_fb(struct backlight_device
*bd
)
82 memset(&bd
->fb_notif
, 0, sizeof(bd
->fb_notif
));
83 bd
->fb_notif
.notifier_call
= fb_notifier_callback
;
85 return fb_register_client(&bd
->fb_notif
);
88 static void backlight_unregister_fb(struct backlight_device
*bd
)
90 fb_unregister_client(&bd
->fb_notif
);
93 static inline int backlight_register_fb(struct backlight_device
*bd
)
98 static inline void backlight_unregister_fb(struct backlight_device
*bd
)
101 #endif /* CONFIG_FB */
103 static void backlight_generate_event(struct backlight_device
*bd
,
104 enum backlight_update_reason reason
)
109 case BACKLIGHT_UPDATE_SYSFS
:
110 envp
[0] = "SOURCE=sysfs";
112 case BACKLIGHT_UPDATE_HOTKEY
:
113 envp
[0] = "SOURCE=hotkey";
116 envp
[0] = "SOURCE=unknown";
120 kobject_uevent_env(&bd
->dev
.kobj
, KOBJ_CHANGE
, envp
);
121 sysfs_notify(&bd
->dev
.kobj
, NULL
, "actual_brightness");
124 static ssize_t
bl_power_show(struct device
*dev
, struct device_attribute
*attr
,
127 struct backlight_device
*bd
= to_backlight_device(dev
);
129 return sprintf(buf
, "%d\n", bd
->props
.power
);
132 static ssize_t
bl_power_store(struct device
*dev
, struct device_attribute
*attr
,
133 const char *buf
, size_t count
)
136 struct backlight_device
*bd
= to_backlight_device(dev
);
139 rc
= kstrtoul(buf
, 0, &power
);
144 mutex_lock(&bd
->ops_lock
);
146 pr_debug("set power to %lu\n", power
);
147 if (bd
->props
.power
!= power
) {
148 bd
->props
.power
= power
;
149 backlight_update_status(bd
);
153 mutex_unlock(&bd
->ops_lock
);
157 static DEVICE_ATTR_RW(bl_power
);
159 static ssize_t
brightness_show(struct device
*dev
,
160 struct device_attribute
*attr
, char *buf
)
162 struct backlight_device
*bd
= to_backlight_device(dev
);
164 return sprintf(buf
, "%d\n", bd
->props
.brightness
);
167 static ssize_t
brightness_store(struct device
*dev
,
168 struct device_attribute
*attr
, const char *buf
, size_t count
)
171 struct backlight_device
*bd
= to_backlight_device(dev
);
172 unsigned long brightness
;
174 rc
= kstrtoul(buf
, 0, &brightness
);
180 mutex_lock(&bd
->ops_lock
);
182 if (brightness
> bd
->props
.max_brightness
)
185 pr_debug("set brightness to %lu\n", brightness
);
186 bd
->props
.brightness
= brightness
;
187 backlight_update_status(bd
);
191 mutex_unlock(&bd
->ops_lock
);
193 backlight_generate_event(bd
, BACKLIGHT_UPDATE_SYSFS
);
197 static DEVICE_ATTR_RW(brightness
);
199 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
202 struct backlight_device
*bd
= to_backlight_device(dev
);
204 return sprintf(buf
, "%s\n", backlight_types
[bd
->props
.type
]);
206 static DEVICE_ATTR_RO(type
);
208 static ssize_t
max_brightness_show(struct device
*dev
,
209 struct device_attribute
*attr
, char *buf
)
211 struct backlight_device
*bd
= to_backlight_device(dev
);
213 return sprintf(buf
, "%d\n", bd
->props
.max_brightness
);
215 static DEVICE_ATTR_RO(max_brightness
);
217 static ssize_t
actual_brightness_show(struct device
*dev
,
218 struct device_attribute
*attr
, char *buf
)
221 struct backlight_device
*bd
= to_backlight_device(dev
);
223 mutex_lock(&bd
->ops_lock
);
224 if (bd
->ops
&& bd
->ops
->get_brightness
)
225 rc
= sprintf(buf
, "%d\n", bd
->ops
->get_brightness(bd
));
226 mutex_unlock(&bd
->ops_lock
);
230 static DEVICE_ATTR_RO(actual_brightness
);
232 static struct class *backlight_class
;
234 #ifdef CONFIG_PM_SLEEP
235 static int backlight_suspend(struct device
*dev
)
237 struct backlight_device
*bd
= to_backlight_device(dev
);
239 mutex_lock(&bd
->ops_lock
);
240 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
241 bd
->props
.state
|= BL_CORE_SUSPENDED
;
242 backlight_update_status(bd
);
244 mutex_unlock(&bd
->ops_lock
);
249 static int backlight_resume(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
);
264 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops
, backlight_suspend
,
267 static void bl_device_release(struct device
*dev
)
269 struct backlight_device
*bd
= to_backlight_device(dev
);
273 static struct attribute
*bl_device_attrs
[] = {
274 &dev_attr_bl_power
.attr
,
275 &dev_attr_brightness
.attr
,
276 &dev_attr_actual_brightness
.attr
,
277 &dev_attr_max_brightness
.attr
,
281 ATTRIBUTE_GROUPS(bl_device
);
284 * backlight_force_update - tell the backlight subsystem that hardware state
286 * @bd: the backlight device to update
288 * Updates the internal state of the backlight in response to a hardware event,
289 * and generate a uevent to notify userspace
291 void backlight_force_update(struct backlight_device
*bd
,
292 enum backlight_update_reason reason
)
294 mutex_lock(&bd
->ops_lock
);
295 if (bd
->ops
&& bd
->ops
->get_brightness
)
296 bd
->props
.brightness
= bd
->ops
->get_brightness(bd
);
297 mutex_unlock(&bd
->ops_lock
);
298 backlight_generate_event(bd
, reason
);
300 EXPORT_SYMBOL(backlight_force_update
);
303 * backlight_device_register - create and register a new object of
304 * backlight_device class.
305 * @name: the name of the new object(must be the same as the name of the
306 * respective framebuffer device).
307 * @parent: a pointer to the parent device
308 * @devdata: an optional pointer to be stored for private driver use. The
309 * methods may retrieve it by using bl_get_data(bd).
310 * @ops: the backlight operations structure.
312 * Creates and registers new backlight device. Returns either an
313 * ERR_PTR() or a pointer to the newly allocated device.
315 struct backlight_device
*backlight_device_register(const char *name
,
316 struct device
*parent
, void *devdata
, const struct backlight_ops
*ops
,
317 const struct backlight_properties
*props
)
319 struct backlight_device
*new_bd
;
322 pr_debug("backlight_device_register: name=%s\n", name
);
324 new_bd
= kzalloc(sizeof(struct backlight_device
), GFP_KERNEL
);
326 return ERR_PTR(-ENOMEM
);
328 mutex_init(&new_bd
->update_lock
);
329 mutex_init(&new_bd
->ops_lock
);
331 new_bd
->dev
.class = backlight_class
;
332 new_bd
->dev
.parent
= parent
;
333 new_bd
->dev
.release
= bl_device_release
;
334 dev_set_name(&new_bd
->dev
, "%s", name
);
335 dev_set_drvdata(&new_bd
->dev
, devdata
);
337 /* Set default properties */
339 memcpy(&new_bd
->props
, props
,
340 sizeof(struct backlight_properties
));
341 if (props
->type
<= 0 || props
->type
>= BACKLIGHT_TYPE_MAX
) {
342 WARN(1, "%s: invalid backlight type", name
);
343 new_bd
->props
.type
= BACKLIGHT_RAW
;
346 new_bd
->props
.type
= BACKLIGHT_RAW
;
349 rc
= device_register(&new_bd
->dev
);
351 put_device(&new_bd
->dev
);
355 rc
= backlight_register_fb(new_bd
);
357 device_unregister(&new_bd
->dev
);
363 #ifdef CONFIG_PMAC_BACKLIGHT
364 mutex_lock(&pmac_backlight_mutex
);
366 pmac_backlight
= new_bd
;
367 mutex_unlock(&pmac_backlight_mutex
);
370 mutex_lock(&backlight_dev_list_mutex
);
371 list_add(&new_bd
->entry
, &backlight_dev_list
);
372 mutex_unlock(&backlight_dev_list_mutex
);
374 blocking_notifier_call_chain(&backlight_notifier
,
375 BACKLIGHT_REGISTERED
, new_bd
);
379 EXPORT_SYMBOL(backlight_device_register
);
381 bool backlight_device_registered(enum backlight_type type
)
384 struct backlight_device
*bd
;
386 mutex_lock(&backlight_dev_list_mutex
);
387 list_for_each_entry(bd
, &backlight_dev_list
, entry
) {
388 if (bd
->props
.type
== type
) {
393 mutex_unlock(&backlight_dev_list_mutex
);
397 EXPORT_SYMBOL(backlight_device_registered
);
400 * backlight_device_unregister - unregisters a backlight device object.
401 * @bd: the backlight device object to be unregistered and freed.
403 * Unregisters a previously registered via backlight_device_register object.
405 void backlight_device_unregister(struct backlight_device
*bd
)
410 mutex_lock(&backlight_dev_list_mutex
);
411 list_del(&bd
->entry
);
412 mutex_unlock(&backlight_dev_list_mutex
);
414 #ifdef CONFIG_PMAC_BACKLIGHT
415 mutex_lock(&pmac_backlight_mutex
);
416 if (pmac_backlight
== bd
)
417 pmac_backlight
= NULL
;
418 mutex_unlock(&pmac_backlight_mutex
);
421 blocking_notifier_call_chain(&backlight_notifier
,
422 BACKLIGHT_UNREGISTERED
, bd
);
424 mutex_lock(&bd
->ops_lock
);
426 mutex_unlock(&bd
->ops_lock
);
428 backlight_unregister_fb(bd
);
429 device_unregister(&bd
->dev
);
431 EXPORT_SYMBOL(backlight_device_unregister
);
433 static void devm_backlight_device_release(struct device
*dev
, void *res
)
435 struct backlight_device
*backlight
= *(struct backlight_device
**)res
;
437 backlight_device_unregister(backlight
);
440 static int devm_backlight_device_match(struct device
*dev
, void *res
,
443 struct backlight_device
**r
= res
;
449 * backlight_register_notifier - get notified of backlight (un)registration
450 * @nb: notifier block with the notifier to call on backlight (un)registration
452 * @return 0 on success, otherwise a negative error code
454 * Register a notifier to get notified when backlight devices get registered
457 int backlight_register_notifier(struct notifier_block
*nb
)
459 return blocking_notifier_chain_register(&backlight_notifier
, nb
);
461 EXPORT_SYMBOL(backlight_register_notifier
);
464 * backlight_unregister_notifier - unregister a backlight notifier
465 * @nb: notifier block to unregister
467 * @return 0 on success, otherwise a negative error code
469 * Register a notifier to get notified when backlight devices get registered
472 int backlight_unregister_notifier(struct notifier_block
*nb
)
474 return blocking_notifier_chain_unregister(&backlight_notifier
, nb
);
476 EXPORT_SYMBOL(backlight_unregister_notifier
);
479 * devm_backlight_device_register - resource managed backlight_device_register()
480 * @dev: the device to register
481 * @name: the name of the device
482 * @parent: a pointer to the parent device
483 * @devdata: an optional pointer to be stored for private driver use
484 * @ops: the backlight operations structure
485 * @props: the backlight properties
487 * @return a struct backlight on success, or an ERR_PTR on error
489 * Managed backlight_device_register(). The backlight_device returned
490 * from this function are automatically freed on driver detach.
491 * See backlight_device_register() for more information.
493 struct backlight_device
*devm_backlight_device_register(struct device
*dev
,
494 const char *name
, struct device
*parent
, void *devdata
,
495 const struct backlight_ops
*ops
,
496 const struct backlight_properties
*props
)
498 struct backlight_device
**ptr
, *backlight
;
500 ptr
= devres_alloc(devm_backlight_device_release
, sizeof(*ptr
),
503 return ERR_PTR(-ENOMEM
);
505 backlight
= backlight_device_register(name
, parent
, devdata
, ops
,
507 if (!IS_ERR(backlight
)) {
509 devres_add(dev
, ptr
);
516 EXPORT_SYMBOL(devm_backlight_device_register
);
519 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
520 * @dev: the device to unregister
521 * @bd: the backlight device to unregister
523 * Deallocated a backlight allocated with devm_backlight_device_register().
524 * Normally this function will not need to be called and the resource management
525 * code will ensure that the resource is freed.
527 void devm_backlight_device_unregister(struct device
*dev
,
528 struct backlight_device
*bd
)
532 rc
= devres_release(dev
, devm_backlight_device_release
,
533 devm_backlight_device_match
, bd
);
536 EXPORT_SYMBOL(devm_backlight_device_unregister
);
539 static int of_parent_match(struct device
*dev
, const void *data
)
541 return dev
->parent
&& dev
->parent
->of_node
== data
;
545 * of_find_backlight_by_node() - find backlight device by device-tree node
546 * @node: device-tree node of the backlight device
548 * Returns a pointer to the backlight device corresponding to the given DT
549 * node or NULL if no such backlight device exists or if the device hasn't
552 * This function obtains a reference on the backlight device and it is the
553 * caller's responsibility to drop the reference by calling put_device() on
554 * the backlight device's .dev field.
556 struct backlight_device
*of_find_backlight_by_node(struct device_node
*node
)
560 dev
= class_find_device(backlight_class
, NULL
, node
, of_parent_match
);
562 return dev
? to_backlight_device(dev
) : NULL
;
564 EXPORT_SYMBOL(of_find_backlight_by_node
);
567 static void __exit
backlight_class_exit(void)
569 class_destroy(backlight_class
);
572 static int __init
backlight_class_init(void)
574 backlight_class
= class_create(THIS_MODULE
, "backlight");
575 if (IS_ERR(backlight_class
)) {
576 pr_warn("Unable to create backlight class; errno = %ld\n",
577 PTR_ERR(backlight_class
));
578 return PTR_ERR(backlight_class
);
581 backlight_class
->dev_groups
= bl_device_groups
;
582 backlight_class
->pm
= &backlight_class_dev_pm_ops
;
583 INIT_LIST_HEAD(&backlight_dev_list
);
584 mutex_init(&backlight_dev_list_mutex
);
585 BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier
);
591 * if this is compiled into the kernel, we need to ensure that the
592 * class is registered before users of the class try to register lcd's
594 postcore_initcall(backlight_class_init
);
595 module_exit(backlight_class_exit
);
597 MODULE_LICENSE("GPL");
598 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
599 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");