1 // SPDX-License-Identifier: GPL-2.0-only
3 * Backlight Lowlevel Control Abstraction
5 * Copyright (C) 2003,2004 Hewlett-Packard Company
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/backlight.h>
15 #include <linux/notifier.h>
16 #include <linux/ctype.h>
17 #include <linux/err.h>
19 #include <linux/slab.h>
21 #ifdef CONFIG_PMAC_BACKLIGHT
22 #include <asm/backlight.h>
25 static struct list_head backlight_dev_list
;
26 static struct mutex backlight_dev_list_mutex
;
27 static struct blocking_notifier_head backlight_notifier
;
29 static const char *const backlight_types
[] = {
30 [BACKLIGHT_RAW
] = "raw",
31 [BACKLIGHT_PLATFORM
] = "platform",
32 [BACKLIGHT_FIRMWARE
] = "firmware",
35 static const char *const backlight_scale_types
[] = {
36 [BACKLIGHT_SCALE_UNKNOWN
] = "unknown",
37 [BACKLIGHT_SCALE_LINEAR
] = "linear",
38 [BACKLIGHT_SCALE_NON_LINEAR
] = "non-linear",
41 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
42 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
43 /* This callback gets called when something important happens inside a
44 * framebuffer driver. We're looking if that important event is blanking,
45 * and if it is and necessary, we're switching backlight power as well ...
47 static int fb_notifier_callback(struct notifier_block
*self
,
48 unsigned long event
, void *data
)
50 struct backlight_device
*bd
;
51 struct fb_event
*evdata
= data
;
52 int node
= evdata
->info
->node
;
55 /* If we aren't interested in this event, skip it immediately ... */
56 if (event
!= FB_EVENT_BLANK
)
59 bd
= container_of(self
, struct backlight_device
, fb_notif
);
60 mutex_lock(&bd
->ops_lock
);
62 if (!bd
->ops
->check_fb
||
63 bd
->ops
->check_fb(bd
, evdata
->info
)) {
64 fb_blank
= *(int *)evdata
->data
;
65 if (fb_blank
== FB_BLANK_UNBLANK
&&
66 !bd
->fb_bl_on
[node
]) {
67 bd
->fb_bl_on
[node
] = true;
68 if (!bd
->use_count
++) {
69 bd
->props
.state
&= ~BL_CORE_FBBLANK
;
70 bd
->props
.fb_blank
= FB_BLANK_UNBLANK
;
71 backlight_update_status(bd
);
73 } else if (fb_blank
!= FB_BLANK_UNBLANK
&&
75 bd
->fb_bl_on
[node
] = false;
76 if (!(--bd
->use_count
)) {
77 bd
->props
.state
|= BL_CORE_FBBLANK
;
78 bd
->props
.fb_blank
= fb_blank
;
79 backlight_update_status(bd
);
83 mutex_unlock(&bd
->ops_lock
);
87 static int backlight_register_fb(struct backlight_device
*bd
)
89 memset(&bd
->fb_notif
, 0, sizeof(bd
->fb_notif
));
90 bd
->fb_notif
.notifier_call
= fb_notifier_callback
;
92 return fb_register_client(&bd
->fb_notif
);
95 static void backlight_unregister_fb(struct backlight_device
*bd
)
97 fb_unregister_client(&bd
->fb_notif
);
100 static inline int backlight_register_fb(struct backlight_device
*bd
)
105 static inline void backlight_unregister_fb(struct backlight_device
*bd
)
108 #endif /* CONFIG_FB */
110 static void backlight_generate_event(struct backlight_device
*bd
,
111 enum backlight_update_reason reason
)
116 case BACKLIGHT_UPDATE_SYSFS
:
117 envp
[0] = "SOURCE=sysfs";
119 case BACKLIGHT_UPDATE_HOTKEY
:
120 envp
[0] = "SOURCE=hotkey";
123 envp
[0] = "SOURCE=unknown";
127 kobject_uevent_env(&bd
->dev
.kobj
, KOBJ_CHANGE
, envp
);
128 sysfs_notify(&bd
->dev
.kobj
, NULL
, "actual_brightness");
131 static ssize_t
bl_power_show(struct device
*dev
, struct device_attribute
*attr
,
134 struct backlight_device
*bd
= to_backlight_device(dev
);
136 return sprintf(buf
, "%d\n", bd
->props
.power
);
139 static ssize_t
bl_power_store(struct device
*dev
, struct device_attribute
*attr
,
140 const char *buf
, size_t count
)
143 struct backlight_device
*bd
= to_backlight_device(dev
);
144 unsigned long power
, old_power
;
146 rc
= kstrtoul(buf
, 0, &power
);
151 mutex_lock(&bd
->ops_lock
);
153 pr_debug("set power to %lu\n", power
);
154 if (bd
->props
.power
!= power
) {
155 old_power
= bd
->props
.power
;
156 bd
->props
.power
= power
;
157 rc
= backlight_update_status(bd
);
159 bd
->props
.power
= old_power
;
166 mutex_unlock(&bd
->ops_lock
);
170 static DEVICE_ATTR_RW(bl_power
);
172 static ssize_t
brightness_show(struct device
*dev
,
173 struct device_attribute
*attr
, char *buf
)
175 struct backlight_device
*bd
= to_backlight_device(dev
);
177 return sprintf(buf
, "%d\n", bd
->props
.brightness
);
180 int backlight_device_set_brightness(struct backlight_device
*bd
,
181 unsigned long brightness
)
185 mutex_lock(&bd
->ops_lock
);
187 if (brightness
> bd
->props
.max_brightness
)
190 pr_debug("set brightness to %lu\n", brightness
);
191 bd
->props
.brightness
= brightness
;
192 rc
= backlight_update_status(bd
);
195 mutex_unlock(&bd
->ops_lock
);
197 backlight_generate_event(bd
, BACKLIGHT_UPDATE_SYSFS
);
201 EXPORT_SYMBOL(backlight_device_set_brightness
);
203 static ssize_t
brightness_store(struct device
*dev
,
204 struct device_attribute
*attr
, const char *buf
, size_t count
)
207 struct backlight_device
*bd
= to_backlight_device(dev
);
208 unsigned long brightness
;
210 rc
= kstrtoul(buf
, 0, &brightness
);
214 rc
= backlight_device_set_brightness(bd
, brightness
);
216 return rc
? rc
: count
;
218 static DEVICE_ATTR_RW(brightness
);
220 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
223 struct backlight_device
*bd
= to_backlight_device(dev
);
225 return sprintf(buf
, "%s\n", backlight_types
[bd
->props
.type
]);
227 static DEVICE_ATTR_RO(type
);
229 static ssize_t
max_brightness_show(struct device
*dev
,
230 struct device_attribute
*attr
, char *buf
)
232 struct backlight_device
*bd
= to_backlight_device(dev
);
234 return sprintf(buf
, "%d\n", bd
->props
.max_brightness
);
236 static DEVICE_ATTR_RO(max_brightness
);
238 static ssize_t
actual_brightness_show(struct device
*dev
,
239 struct device_attribute
*attr
, char *buf
)
242 struct backlight_device
*bd
= to_backlight_device(dev
);
244 mutex_lock(&bd
->ops_lock
);
245 if (bd
->ops
&& bd
->ops
->get_brightness
)
246 rc
= sprintf(buf
, "%d\n", bd
->ops
->get_brightness(bd
));
248 rc
= sprintf(buf
, "%d\n", bd
->props
.brightness
);
249 mutex_unlock(&bd
->ops_lock
);
253 static DEVICE_ATTR_RO(actual_brightness
);
255 static ssize_t
scale_show(struct device
*dev
,
256 struct device_attribute
*attr
, char *buf
)
258 struct backlight_device
*bd
= to_backlight_device(dev
);
260 if (WARN_ON(bd
->props
.scale
> BACKLIGHT_SCALE_NON_LINEAR
))
261 return sprintf(buf
, "unknown\n");
263 return sprintf(buf
, "%s\n", backlight_scale_types
[bd
->props
.scale
]);
265 static DEVICE_ATTR_RO(scale
);
267 static struct class *backlight_class
;
269 #ifdef CONFIG_PM_SLEEP
270 static int backlight_suspend(struct device
*dev
)
272 struct backlight_device
*bd
= to_backlight_device(dev
);
274 mutex_lock(&bd
->ops_lock
);
275 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
276 bd
->props
.state
|= BL_CORE_SUSPENDED
;
277 backlight_update_status(bd
);
279 mutex_unlock(&bd
->ops_lock
);
284 static int backlight_resume(struct device
*dev
)
286 struct backlight_device
*bd
= to_backlight_device(dev
);
288 mutex_lock(&bd
->ops_lock
);
289 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
290 bd
->props
.state
&= ~BL_CORE_SUSPENDED
;
291 backlight_update_status(bd
);
293 mutex_unlock(&bd
->ops_lock
);
299 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops
, backlight_suspend
,
302 static void bl_device_release(struct device
*dev
)
304 struct backlight_device
*bd
= to_backlight_device(dev
);
308 static struct attribute
*bl_device_attrs
[] = {
309 &dev_attr_bl_power
.attr
,
310 &dev_attr_brightness
.attr
,
311 &dev_attr_actual_brightness
.attr
,
312 &dev_attr_max_brightness
.attr
,
313 &dev_attr_scale
.attr
,
317 ATTRIBUTE_GROUPS(bl_device
);
320 * backlight_force_update - tell the backlight subsystem that hardware state
322 * @bd: the backlight device to update
324 * Updates the internal state of the backlight in response to a hardware event,
325 * and generate a uevent to notify userspace
327 void backlight_force_update(struct backlight_device
*bd
,
328 enum backlight_update_reason reason
)
330 mutex_lock(&bd
->ops_lock
);
331 if (bd
->ops
&& bd
->ops
->get_brightness
)
332 bd
->props
.brightness
= bd
->ops
->get_brightness(bd
);
333 mutex_unlock(&bd
->ops_lock
);
334 backlight_generate_event(bd
, reason
);
336 EXPORT_SYMBOL(backlight_force_update
);
339 * backlight_device_register - create and register a new object of
340 * backlight_device class.
341 * @name: the name of the new object(must be the same as the name of the
342 * respective framebuffer device).
343 * @parent: a pointer to the parent device
344 * @devdata: an optional pointer to be stored for private driver use. The
345 * methods may retrieve it by using bl_get_data(bd).
346 * @ops: the backlight operations structure.
348 * Creates and registers new backlight device. Returns either an
349 * ERR_PTR() or a pointer to the newly allocated device.
351 struct backlight_device
*backlight_device_register(const char *name
,
352 struct device
*parent
, void *devdata
, const struct backlight_ops
*ops
,
353 const struct backlight_properties
*props
)
355 struct backlight_device
*new_bd
;
358 pr_debug("backlight_device_register: name=%s\n", name
);
360 new_bd
= kzalloc(sizeof(struct backlight_device
), GFP_KERNEL
);
362 return ERR_PTR(-ENOMEM
);
364 mutex_init(&new_bd
->update_lock
);
365 mutex_init(&new_bd
->ops_lock
);
367 new_bd
->dev
.class = backlight_class
;
368 new_bd
->dev
.parent
= parent
;
369 new_bd
->dev
.release
= bl_device_release
;
370 dev_set_name(&new_bd
->dev
, "%s", name
);
371 dev_set_drvdata(&new_bd
->dev
, devdata
);
373 /* Set default properties */
375 memcpy(&new_bd
->props
, props
,
376 sizeof(struct backlight_properties
));
377 if (props
->type
<= 0 || props
->type
>= BACKLIGHT_TYPE_MAX
) {
378 WARN(1, "%s: invalid backlight type", name
);
379 new_bd
->props
.type
= BACKLIGHT_RAW
;
382 new_bd
->props
.type
= BACKLIGHT_RAW
;
385 rc
= device_register(&new_bd
->dev
);
387 put_device(&new_bd
->dev
);
391 rc
= backlight_register_fb(new_bd
);
393 device_unregister(&new_bd
->dev
);
399 #ifdef CONFIG_PMAC_BACKLIGHT
400 mutex_lock(&pmac_backlight_mutex
);
402 pmac_backlight
= new_bd
;
403 mutex_unlock(&pmac_backlight_mutex
);
406 mutex_lock(&backlight_dev_list_mutex
);
407 list_add(&new_bd
->entry
, &backlight_dev_list
);
408 mutex_unlock(&backlight_dev_list_mutex
);
410 blocking_notifier_call_chain(&backlight_notifier
,
411 BACKLIGHT_REGISTERED
, new_bd
);
415 EXPORT_SYMBOL(backlight_device_register
);
417 struct backlight_device
*backlight_device_get_by_type(enum backlight_type type
)
420 struct backlight_device
*bd
;
422 mutex_lock(&backlight_dev_list_mutex
);
423 list_for_each_entry(bd
, &backlight_dev_list
, entry
) {
424 if (bd
->props
.type
== type
) {
429 mutex_unlock(&backlight_dev_list_mutex
);
431 return found
? bd
: NULL
;
433 EXPORT_SYMBOL(backlight_device_get_by_type
);
436 * backlight_device_unregister - unregisters a backlight device object.
437 * @bd: the backlight device object to be unregistered and freed.
439 * Unregisters a previously registered via backlight_device_register object.
441 void backlight_device_unregister(struct backlight_device
*bd
)
446 mutex_lock(&backlight_dev_list_mutex
);
447 list_del(&bd
->entry
);
448 mutex_unlock(&backlight_dev_list_mutex
);
450 #ifdef CONFIG_PMAC_BACKLIGHT
451 mutex_lock(&pmac_backlight_mutex
);
452 if (pmac_backlight
== bd
)
453 pmac_backlight
= NULL
;
454 mutex_unlock(&pmac_backlight_mutex
);
457 blocking_notifier_call_chain(&backlight_notifier
,
458 BACKLIGHT_UNREGISTERED
, bd
);
460 mutex_lock(&bd
->ops_lock
);
462 mutex_unlock(&bd
->ops_lock
);
464 backlight_unregister_fb(bd
);
465 device_unregister(&bd
->dev
);
467 EXPORT_SYMBOL(backlight_device_unregister
);
469 static void devm_backlight_device_release(struct device
*dev
, void *res
)
471 struct backlight_device
*backlight
= *(struct backlight_device
**)res
;
473 backlight_device_unregister(backlight
);
476 static int devm_backlight_device_match(struct device
*dev
, void *res
,
479 struct backlight_device
**r
= res
;
485 * backlight_register_notifier - get notified of backlight (un)registration
486 * @nb: notifier block with the notifier to call on backlight (un)registration
488 * @return 0 on success, otherwise a negative error code
490 * Register a notifier to get notified when backlight devices get registered
493 int backlight_register_notifier(struct notifier_block
*nb
)
495 return blocking_notifier_chain_register(&backlight_notifier
, nb
);
497 EXPORT_SYMBOL(backlight_register_notifier
);
500 * backlight_unregister_notifier - unregister a backlight notifier
501 * @nb: notifier block to unregister
503 * @return 0 on success, otherwise a negative error code
505 * Register a notifier to get notified when backlight devices get registered
508 int backlight_unregister_notifier(struct notifier_block
*nb
)
510 return blocking_notifier_chain_unregister(&backlight_notifier
, nb
);
512 EXPORT_SYMBOL(backlight_unregister_notifier
);
515 * devm_backlight_device_register - resource managed backlight_device_register()
516 * @dev: the device to register
517 * @name: the name of the device
518 * @parent: a pointer to the parent device
519 * @devdata: an optional pointer to be stored for private driver use
520 * @ops: the backlight operations structure
521 * @props: the backlight properties
523 * @return a struct backlight on success, or an ERR_PTR on error
525 * Managed backlight_device_register(). The backlight_device returned
526 * from this function are automatically freed on driver detach.
527 * See backlight_device_register() for more information.
529 struct backlight_device
*devm_backlight_device_register(struct device
*dev
,
530 const char *name
, struct device
*parent
, void *devdata
,
531 const struct backlight_ops
*ops
,
532 const struct backlight_properties
*props
)
534 struct backlight_device
**ptr
, *backlight
;
536 ptr
= devres_alloc(devm_backlight_device_release
, sizeof(*ptr
),
539 return ERR_PTR(-ENOMEM
);
541 backlight
= backlight_device_register(name
, parent
, devdata
, ops
,
543 if (!IS_ERR(backlight
)) {
545 devres_add(dev
, ptr
);
552 EXPORT_SYMBOL(devm_backlight_device_register
);
555 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
556 * @dev: the device to unregister
557 * @bd: the backlight device to unregister
559 * Deallocated a backlight allocated with devm_backlight_device_register().
560 * Normally this function will not need to be called and the resource management
561 * code will ensure that the resource is freed.
563 void devm_backlight_device_unregister(struct device
*dev
,
564 struct backlight_device
*bd
)
568 rc
= devres_release(dev
, devm_backlight_device_release
,
569 devm_backlight_device_match
, bd
);
572 EXPORT_SYMBOL(devm_backlight_device_unregister
);
575 static int of_parent_match(struct device
*dev
, const void *data
)
577 return dev
->parent
&& dev
->parent
->of_node
== data
;
581 * of_find_backlight_by_node() - find backlight device by device-tree node
582 * @node: device-tree node of the backlight device
584 * Returns a pointer to the backlight device corresponding to the given DT
585 * node or NULL if no such backlight device exists or if the device hasn't
588 * This function obtains a reference on the backlight device and it is the
589 * caller's responsibility to drop the reference by calling put_device() on
590 * the backlight device's .dev field.
592 struct backlight_device
*of_find_backlight_by_node(struct device_node
*node
)
596 dev
= class_find_device(backlight_class
, NULL
, node
, of_parent_match
);
598 return dev
? to_backlight_device(dev
) : NULL
;
600 EXPORT_SYMBOL(of_find_backlight_by_node
);
604 * of_find_backlight - Get backlight device
607 * This function looks for a property named 'backlight' on the DT node
608 * connected to @dev and looks up the backlight device.
610 * Call backlight_put() to drop the reference on the backlight device.
613 * A pointer to the backlight device if found.
614 * Error pointer -EPROBE_DEFER if the DT property is set, but no backlight
616 * NULL if there's no backlight property.
618 struct backlight_device
*of_find_backlight(struct device
*dev
)
620 struct backlight_device
*bd
= NULL
;
621 struct device_node
*np
;
626 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
627 np
= of_parse_phandle(dev
->of_node
, "backlight", 0);
629 bd
= of_find_backlight_by_node(np
);
632 return ERR_PTR(-EPROBE_DEFER
);
634 * Note: gpio_backlight uses brightness as
635 * power state during probe
637 if (!bd
->props
.brightness
)
638 bd
->props
.brightness
= bd
->props
.max_brightness
;
644 EXPORT_SYMBOL(of_find_backlight
);
646 static void devm_backlight_release(void *data
)
652 * devm_of_find_backlight - Resource-managed of_find_backlight()
655 * Device managed version of of_find_backlight().
656 * The reference on the backlight device is automatically
657 * dropped on driver detach.
659 struct backlight_device
*devm_of_find_backlight(struct device
*dev
)
661 struct backlight_device
*bd
;
664 bd
= of_find_backlight(dev
);
665 if (IS_ERR_OR_NULL(bd
))
667 ret
= devm_add_action(dev
, devm_backlight_release
, bd
);
674 EXPORT_SYMBOL(devm_of_find_backlight
);
676 static void __exit
backlight_class_exit(void)
678 class_destroy(backlight_class
);
681 static int __init
backlight_class_init(void)
683 backlight_class
= class_create(THIS_MODULE
, "backlight");
684 if (IS_ERR(backlight_class
)) {
685 pr_warn("Unable to create backlight class; errno = %ld\n",
686 PTR_ERR(backlight_class
));
687 return PTR_ERR(backlight_class
);
690 backlight_class
->dev_groups
= bl_device_groups
;
691 backlight_class
->pm
= &backlight_class_dev_pm_ops
;
692 INIT_LIST_HEAD(&backlight_dev_list
);
693 mutex_init(&backlight_dev_list_mutex
);
694 BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier
);
700 * if this is compiled into the kernel, we need to ensure that the
701 * class is registered before users of the class try to register lcd's
703 postcore_initcall(backlight_class_init
);
704 module_exit(backlight_class_exit
);
706 MODULE_LICENSE("GPL");
707 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
708 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");