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 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
36 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
37 /* This callback gets called when something important happens inside a
38 * framebuffer driver. We're looking if that important event is blanking,
39 * and if it is and necessary, we're switching backlight power as well ...
41 static int fb_notifier_callback(struct notifier_block
*self
,
42 unsigned long event
, void *data
)
44 struct backlight_device
*bd
;
45 struct fb_event
*evdata
= data
;
46 int node
= evdata
->info
->node
;
49 /* If we aren't interested in this event, skip it immediately ... */
50 if (event
!= FB_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 bd
->fb_bl_on
[node
] = true;
62 if (!bd
->use_count
++) {
63 bd
->props
.state
&= ~BL_CORE_FBBLANK
;
64 bd
->props
.fb_blank
= FB_BLANK_UNBLANK
;
65 backlight_update_status(bd
);
67 } else if (fb_blank
!= FB_BLANK_UNBLANK
&&
69 bd
->fb_bl_on
[node
] = false;
70 if (!(--bd
->use_count
)) {
71 bd
->props
.state
|= BL_CORE_FBBLANK
;
72 bd
->props
.fb_blank
= fb_blank
;
73 backlight_update_status(bd
);
77 mutex_unlock(&bd
->ops_lock
);
81 static int backlight_register_fb(struct backlight_device
*bd
)
83 memset(&bd
->fb_notif
, 0, sizeof(bd
->fb_notif
));
84 bd
->fb_notif
.notifier_call
= fb_notifier_callback
;
86 return fb_register_client(&bd
->fb_notif
);
89 static void backlight_unregister_fb(struct backlight_device
*bd
)
91 fb_unregister_client(&bd
->fb_notif
);
94 static inline int backlight_register_fb(struct backlight_device
*bd
)
99 static inline void backlight_unregister_fb(struct backlight_device
*bd
)
102 #endif /* CONFIG_FB */
104 static void backlight_generate_event(struct backlight_device
*bd
,
105 enum backlight_update_reason reason
)
110 case BACKLIGHT_UPDATE_SYSFS
:
111 envp
[0] = "SOURCE=sysfs";
113 case BACKLIGHT_UPDATE_HOTKEY
:
114 envp
[0] = "SOURCE=hotkey";
117 envp
[0] = "SOURCE=unknown";
121 kobject_uevent_env(&bd
->dev
.kobj
, KOBJ_CHANGE
, envp
);
122 sysfs_notify(&bd
->dev
.kobj
, NULL
, "actual_brightness");
125 static ssize_t
bl_power_show(struct device
*dev
, struct device_attribute
*attr
,
128 struct backlight_device
*bd
= to_backlight_device(dev
);
130 return sprintf(buf
, "%d\n", bd
->props
.power
);
133 static ssize_t
bl_power_store(struct device
*dev
, struct device_attribute
*attr
,
134 const char *buf
, size_t count
)
137 struct backlight_device
*bd
= to_backlight_device(dev
);
138 unsigned long power
, old_power
;
140 rc
= kstrtoul(buf
, 0, &power
);
145 mutex_lock(&bd
->ops_lock
);
147 pr_debug("set power to %lu\n", power
);
148 if (bd
->props
.power
!= power
) {
149 old_power
= bd
->props
.power
;
150 bd
->props
.power
= power
;
151 rc
= backlight_update_status(bd
);
153 bd
->props
.power
= old_power
;
160 mutex_unlock(&bd
->ops_lock
);
164 static DEVICE_ATTR_RW(bl_power
);
166 static ssize_t
brightness_show(struct device
*dev
,
167 struct device_attribute
*attr
, char *buf
)
169 struct backlight_device
*bd
= to_backlight_device(dev
);
171 return sprintf(buf
, "%d\n", bd
->props
.brightness
);
174 int backlight_device_set_brightness(struct backlight_device
*bd
,
175 unsigned long brightness
)
179 mutex_lock(&bd
->ops_lock
);
181 if (brightness
> bd
->props
.max_brightness
)
184 pr_debug("set brightness to %lu\n", brightness
);
185 bd
->props
.brightness
= brightness
;
186 rc
= backlight_update_status(bd
);
189 mutex_unlock(&bd
->ops_lock
);
191 backlight_generate_event(bd
, BACKLIGHT_UPDATE_SYSFS
);
195 EXPORT_SYMBOL(backlight_device_set_brightness
);
197 static ssize_t
brightness_store(struct device
*dev
,
198 struct device_attribute
*attr
, const char *buf
, size_t count
)
201 struct backlight_device
*bd
= to_backlight_device(dev
);
202 unsigned long brightness
;
204 rc
= kstrtoul(buf
, 0, &brightness
);
208 rc
= backlight_device_set_brightness(bd
, brightness
);
210 return rc
? rc
: count
;
212 static DEVICE_ATTR_RW(brightness
);
214 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
217 struct backlight_device
*bd
= to_backlight_device(dev
);
219 return sprintf(buf
, "%s\n", backlight_types
[bd
->props
.type
]);
221 static DEVICE_ATTR_RO(type
);
223 static ssize_t
max_brightness_show(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
226 struct backlight_device
*bd
= to_backlight_device(dev
);
228 return sprintf(buf
, "%d\n", bd
->props
.max_brightness
);
230 static DEVICE_ATTR_RO(max_brightness
);
232 static ssize_t
actual_brightness_show(struct device
*dev
,
233 struct device_attribute
*attr
, char *buf
)
236 struct backlight_device
*bd
= to_backlight_device(dev
);
238 mutex_lock(&bd
->ops_lock
);
239 if (bd
->ops
&& bd
->ops
->get_brightness
)
240 rc
= sprintf(buf
, "%d\n", bd
->ops
->get_brightness(bd
));
242 rc
= sprintf(buf
, "%d\n", bd
->props
.brightness
);
243 mutex_unlock(&bd
->ops_lock
);
247 static DEVICE_ATTR_RO(actual_brightness
);
249 static struct class *backlight_class
;
251 #ifdef CONFIG_PM_SLEEP
252 static int backlight_suspend(struct device
*dev
)
254 struct backlight_device
*bd
= to_backlight_device(dev
);
256 mutex_lock(&bd
->ops_lock
);
257 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
258 bd
->props
.state
|= BL_CORE_SUSPENDED
;
259 backlight_update_status(bd
);
261 mutex_unlock(&bd
->ops_lock
);
266 static int backlight_resume(struct device
*dev
)
268 struct backlight_device
*bd
= to_backlight_device(dev
);
270 mutex_lock(&bd
->ops_lock
);
271 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
272 bd
->props
.state
&= ~BL_CORE_SUSPENDED
;
273 backlight_update_status(bd
);
275 mutex_unlock(&bd
->ops_lock
);
281 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops
, backlight_suspend
,
284 static void bl_device_release(struct device
*dev
)
286 struct backlight_device
*bd
= to_backlight_device(dev
);
290 static struct attribute
*bl_device_attrs
[] = {
291 &dev_attr_bl_power
.attr
,
292 &dev_attr_brightness
.attr
,
293 &dev_attr_actual_brightness
.attr
,
294 &dev_attr_max_brightness
.attr
,
298 ATTRIBUTE_GROUPS(bl_device
);
301 * backlight_force_update - tell the backlight subsystem that hardware state
303 * @bd: the backlight device to update
305 * Updates the internal state of the backlight in response to a hardware event,
306 * and generate a uevent to notify userspace
308 void backlight_force_update(struct backlight_device
*bd
,
309 enum backlight_update_reason reason
)
311 mutex_lock(&bd
->ops_lock
);
312 if (bd
->ops
&& bd
->ops
->get_brightness
)
313 bd
->props
.brightness
= bd
->ops
->get_brightness(bd
);
314 mutex_unlock(&bd
->ops_lock
);
315 backlight_generate_event(bd
, reason
);
317 EXPORT_SYMBOL(backlight_force_update
);
320 * backlight_device_register - create and register a new object of
321 * backlight_device class.
322 * @name: the name of the new object(must be the same as the name of the
323 * respective framebuffer device).
324 * @parent: a pointer to the parent device
325 * @devdata: an optional pointer to be stored for private driver use. The
326 * methods may retrieve it by using bl_get_data(bd).
327 * @ops: the backlight operations structure.
329 * Creates and registers new backlight device. Returns either an
330 * ERR_PTR() or a pointer to the newly allocated device.
332 struct backlight_device
*backlight_device_register(const char *name
,
333 struct device
*parent
, void *devdata
, const struct backlight_ops
*ops
,
334 const struct backlight_properties
*props
)
336 struct backlight_device
*new_bd
;
339 pr_debug("backlight_device_register: name=%s\n", name
);
341 new_bd
= kzalloc(sizeof(struct backlight_device
), GFP_KERNEL
);
343 return ERR_PTR(-ENOMEM
);
345 mutex_init(&new_bd
->update_lock
);
346 mutex_init(&new_bd
->ops_lock
);
348 new_bd
->dev
.class = backlight_class
;
349 new_bd
->dev
.parent
= parent
;
350 new_bd
->dev
.release
= bl_device_release
;
351 dev_set_name(&new_bd
->dev
, "%s", name
);
352 dev_set_drvdata(&new_bd
->dev
, devdata
);
354 /* Set default properties */
356 memcpy(&new_bd
->props
, props
,
357 sizeof(struct backlight_properties
));
358 if (props
->type
<= 0 || props
->type
>= BACKLIGHT_TYPE_MAX
) {
359 WARN(1, "%s: invalid backlight type", name
);
360 new_bd
->props
.type
= BACKLIGHT_RAW
;
363 new_bd
->props
.type
= BACKLIGHT_RAW
;
366 rc
= device_register(&new_bd
->dev
);
368 put_device(&new_bd
->dev
);
372 rc
= backlight_register_fb(new_bd
);
374 device_unregister(&new_bd
->dev
);
380 #ifdef CONFIG_PMAC_BACKLIGHT
381 mutex_lock(&pmac_backlight_mutex
);
383 pmac_backlight
= new_bd
;
384 mutex_unlock(&pmac_backlight_mutex
);
387 mutex_lock(&backlight_dev_list_mutex
);
388 list_add(&new_bd
->entry
, &backlight_dev_list
);
389 mutex_unlock(&backlight_dev_list_mutex
);
391 blocking_notifier_call_chain(&backlight_notifier
,
392 BACKLIGHT_REGISTERED
, new_bd
);
396 EXPORT_SYMBOL(backlight_device_register
);
398 struct backlight_device
*backlight_device_get_by_type(enum backlight_type type
)
401 struct backlight_device
*bd
;
403 mutex_lock(&backlight_dev_list_mutex
);
404 list_for_each_entry(bd
, &backlight_dev_list
, entry
) {
405 if (bd
->props
.type
== type
) {
410 mutex_unlock(&backlight_dev_list_mutex
);
412 return found
? bd
: NULL
;
414 EXPORT_SYMBOL(backlight_device_get_by_type
);
417 * backlight_device_unregister - unregisters a backlight device object.
418 * @bd: the backlight device object to be unregistered and freed.
420 * Unregisters a previously registered via backlight_device_register object.
422 void backlight_device_unregister(struct backlight_device
*bd
)
427 mutex_lock(&backlight_dev_list_mutex
);
428 list_del(&bd
->entry
);
429 mutex_unlock(&backlight_dev_list_mutex
);
431 #ifdef CONFIG_PMAC_BACKLIGHT
432 mutex_lock(&pmac_backlight_mutex
);
433 if (pmac_backlight
== bd
)
434 pmac_backlight
= NULL
;
435 mutex_unlock(&pmac_backlight_mutex
);
438 blocking_notifier_call_chain(&backlight_notifier
,
439 BACKLIGHT_UNREGISTERED
, bd
);
441 mutex_lock(&bd
->ops_lock
);
443 mutex_unlock(&bd
->ops_lock
);
445 backlight_unregister_fb(bd
);
446 device_unregister(&bd
->dev
);
448 EXPORT_SYMBOL(backlight_device_unregister
);
450 static void devm_backlight_device_release(struct device
*dev
, void *res
)
452 struct backlight_device
*backlight
= *(struct backlight_device
**)res
;
454 backlight_device_unregister(backlight
);
457 static int devm_backlight_device_match(struct device
*dev
, void *res
,
460 struct backlight_device
**r
= res
;
466 * backlight_register_notifier - get notified of backlight (un)registration
467 * @nb: notifier block with the notifier to call on backlight (un)registration
469 * @return 0 on success, otherwise a negative error code
471 * Register a notifier to get notified when backlight devices get registered
474 int backlight_register_notifier(struct notifier_block
*nb
)
476 return blocking_notifier_chain_register(&backlight_notifier
, nb
);
478 EXPORT_SYMBOL(backlight_register_notifier
);
481 * backlight_unregister_notifier - unregister a backlight notifier
482 * @nb: notifier block to unregister
484 * @return 0 on success, otherwise a negative error code
486 * Register a notifier to get notified when backlight devices get registered
489 int backlight_unregister_notifier(struct notifier_block
*nb
)
491 return blocking_notifier_chain_unregister(&backlight_notifier
, nb
);
493 EXPORT_SYMBOL(backlight_unregister_notifier
);
496 * devm_backlight_device_register - resource managed backlight_device_register()
497 * @dev: the device to register
498 * @name: the name of the device
499 * @parent: a pointer to the parent device
500 * @devdata: an optional pointer to be stored for private driver use
501 * @ops: the backlight operations structure
502 * @props: the backlight properties
504 * @return a struct backlight on success, or an ERR_PTR on error
506 * Managed backlight_device_register(). The backlight_device returned
507 * from this function are automatically freed on driver detach.
508 * See backlight_device_register() for more information.
510 struct backlight_device
*devm_backlight_device_register(struct device
*dev
,
511 const char *name
, struct device
*parent
, void *devdata
,
512 const struct backlight_ops
*ops
,
513 const struct backlight_properties
*props
)
515 struct backlight_device
**ptr
, *backlight
;
517 ptr
= devres_alloc(devm_backlight_device_release
, sizeof(*ptr
),
520 return ERR_PTR(-ENOMEM
);
522 backlight
= backlight_device_register(name
, parent
, devdata
, ops
,
524 if (!IS_ERR(backlight
)) {
526 devres_add(dev
, ptr
);
533 EXPORT_SYMBOL(devm_backlight_device_register
);
536 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
537 * @dev: the device to unregister
538 * @bd: the backlight device to unregister
540 * Deallocated a backlight allocated with devm_backlight_device_register().
541 * Normally this function will not need to be called and the resource management
542 * code will ensure that the resource is freed.
544 void devm_backlight_device_unregister(struct device
*dev
,
545 struct backlight_device
*bd
)
549 rc
= devres_release(dev
, devm_backlight_device_release
,
550 devm_backlight_device_match
, bd
);
553 EXPORT_SYMBOL(devm_backlight_device_unregister
);
556 static int of_parent_match(struct device
*dev
, const void *data
)
558 return dev
->parent
&& dev
->parent
->of_node
== data
;
562 * of_find_backlight_by_node() - find backlight device by device-tree node
563 * @node: device-tree node of the backlight device
565 * Returns a pointer to the backlight device corresponding to the given DT
566 * node or NULL if no such backlight device exists or if the device hasn't
569 * This function obtains a reference on the backlight device and it is the
570 * caller's responsibility to drop the reference by calling put_device() on
571 * the backlight device's .dev field.
573 struct backlight_device
*of_find_backlight_by_node(struct device_node
*node
)
577 dev
= class_find_device(backlight_class
, NULL
, node
, of_parent_match
);
579 return dev
? to_backlight_device(dev
) : NULL
;
581 EXPORT_SYMBOL(of_find_backlight_by_node
);
585 * of_find_backlight - Get backlight device
588 * This function looks for a property named 'backlight' on the DT node
589 * connected to @dev and looks up the backlight device.
591 * Call backlight_put() to drop the reference on the backlight device.
594 * A pointer to the backlight device if found.
595 * Error pointer -EPROBE_DEFER if the DT property is set, but no backlight
597 * NULL if there's no backlight property.
599 struct backlight_device
*of_find_backlight(struct device
*dev
)
601 struct backlight_device
*bd
= NULL
;
602 struct device_node
*np
;
607 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
608 np
= of_parse_phandle(dev
->of_node
, "backlight", 0);
610 bd
= of_find_backlight_by_node(np
);
613 return ERR_PTR(-EPROBE_DEFER
);
615 * Note: gpio_backlight uses brightness as
616 * power state during probe
618 if (!bd
->props
.brightness
)
619 bd
->props
.brightness
= bd
->props
.max_brightness
;
625 EXPORT_SYMBOL(of_find_backlight
);
627 static void devm_backlight_release(void *data
)
633 * devm_of_find_backlight - Resource-managed of_find_backlight()
636 * Device managed version of of_find_backlight().
637 * The reference on the backlight device is automatically
638 * dropped on driver detach.
640 struct backlight_device
*devm_of_find_backlight(struct device
*dev
)
642 struct backlight_device
*bd
;
645 bd
= of_find_backlight(dev
);
646 if (IS_ERR_OR_NULL(bd
))
648 ret
= devm_add_action(dev
, devm_backlight_release
, bd
);
655 EXPORT_SYMBOL(devm_of_find_backlight
);
657 static void __exit
backlight_class_exit(void)
659 class_destroy(backlight_class
);
662 static int __init
backlight_class_init(void)
664 backlight_class
= class_create(THIS_MODULE
, "backlight");
665 if (IS_ERR(backlight_class
)) {
666 pr_warn("Unable to create backlight class; errno = %ld\n",
667 PTR_ERR(backlight_class
));
668 return PTR_ERR(backlight_class
);
671 backlight_class
->dev_groups
= bl_device_groups
;
672 backlight_class
->pm
= &backlight_class_dev_pm_ops
;
673 INIT_LIST_HEAD(&backlight_dev_list
);
674 mutex_init(&backlight_dev_list_mutex
);
675 BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier
);
681 * if this is compiled into the kernel, we need to ensure that the
682 * class is registered before users of the class try to register lcd's
684 postcore_initcall(backlight_class_init
);
685 module_exit(backlight_class_exit
);
687 MODULE_LICENSE("GPL");
688 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
689 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");