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>
28 * The backlight core supports implementing backlight drivers.
30 * A backlight driver registers a driver using
31 * devm_backlight_device_register(). The properties of the backlight
32 * driver such as type and max_brightness must be specified.
33 * When the core detect changes in for example brightness or power state
34 * the update_status() operation is called. The backlight driver shall
35 * implement this operation and use it to adjust backlight.
37 * Several sysfs attributes are provided by the backlight core::
39 * - brightness R/W, set the requested brightness level
40 * - actual_brightness RO, the brightness level used by the HW
41 * - max_brightness RO, the maximum brightness level supported
43 * See Documentation/ABI/stable/sysfs-class-backlight for the full list.
45 * The backlight can be adjusted using the sysfs interface, and
46 * the backlight driver may also support adjusting backlight using
47 * a hot-key or some other platform or firmware specific way.
49 * The driver must implement the get_brightness() operation if
50 * the HW do not support all the levels that can be specified in
51 * brightness, thus providing user-space access to the actual level
52 * via the actual_brightness attribute.
54 * When the backlight changes this is reported to user-space using
55 * an uevent connected to the actual_brightness attribute.
56 * When brightness is set by platform specific means, for example
57 * a hot-key to adjust backlight, the driver must notify the backlight
58 * core that brightness has changed using backlight_force_update().
60 * The backlight driver core receives notifications from fbdev and
61 * if the event is FB_EVENT_BLANK and if the value of blank, from the
62 * FBIOBLANK ioctrl, results in a change in the backlight state the
63 * update_status() operation is called.
66 static struct list_head backlight_dev_list;
67 static struct mutex backlight_dev_list_mutex;
68 static struct blocking_notifier_head backlight_notifier;
70 static const char *const backlight_types[] = {
71 [BACKLIGHT_RAW] = "raw",
72 [BACKLIGHT_PLATFORM] = "platform",
73 [BACKLIGHT_FIRMWARE] = "firmware",
76 static const char *const backlight_scale_types[] = {
77 [BACKLIGHT_SCALE_UNKNOWN] = "unknown",
78 [BACKLIGHT_SCALE_LINEAR] = "linear",
79 [BACKLIGHT_SCALE_NON_LINEAR] = "non-linear",
82 #if defined(CONFIG_FB_CORE) || (defined(CONFIG_FB_CORE_MODULE) && \
83 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
85 * fb_notifier_callback
87 * This callback gets called when something important happens inside a
88 * framebuffer driver. The backlight core only cares about FB_BLANK_UNBLANK
89 * which is reported to the driver using backlight_update_status()
92 * There may be several fbdev's connected to the backlight device,
93 * in which case they are kept track of. A state change is only reported
94 * if there is a change in backlight for the specified fbdev.
96 static int fb_notifier_callback(struct notifier_block *self,
97 unsigned long event, void *data)
99 struct backlight_device *bd;
100 struct fb_event *evdata = data;
101 struct fb_info *info = evdata->info;
102 struct backlight_device *fb_bd = fb_bl_device(info);
103 int node = info->node;
106 /* If we aren't interested in this event, skip it immediately ... */
107 if (event != FB_EVENT_BLANK)
110 bd = container_of(self, struct backlight_device, fb_notif);
111 mutex_lock(&bd->ops_lock);
115 if (bd->ops->controls_device && !bd->ops->controls_device(bd, info->device))
117 if (fb_bd && fb_bd != bd)
120 fb_blank = *(int *)evdata->data;
121 if (fb_blank == FB_BLANK_UNBLANK && !bd->fb_bl_on[node]) {
122 bd->fb_bl_on[node] = true;
123 if (!bd->use_count++) {
124 bd->props.state &= ~BL_CORE_FBBLANK;
125 backlight_update_status(bd);
127 } else if (fb_blank != FB_BLANK_UNBLANK && bd->fb_bl_on[node]) {
128 bd->fb_bl_on[node] = false;
129 if (!(--bd->use_count)) {
130 bd->props.state |= BL_CORE_FBBLANK;
131 backlight_update_status(bd);
135 mutex_unlock(&bd->ops_lock);
139 static int backlight_register_fb(struct backlight_device *bd)
141 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
142 bd->fb_notif.notifier_call = fb_notifier_callback;
144 return fb_register_client(&bd->fb_notif);
147 static void backlight_unregister_fb(struct backlight_device *bd)
149 fb_unregister_client(&bd->fb_notif);
152 static inline int backlight_register_fb(struct backlight_device *bd)
157 static inline void backlight_unregister_fb(struct backlight_device *bd)
160 #endif /* CONFIG_FB_CORE */
162 static void backlight_generate_event(struct backlight_device *bd,
163 enum backlight_update_reason reason)
168 case BACKLIGHT_UPDATE_SYSFS:
169 envp[0] = "SOURCE=sysfs";
171 case BACKLIGHT_UPDATE_HOTKEY:
172 envp[0] = "SOURCE=hotkey";
175 envp[0] = "SOURCE=unknown";
179 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
180 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
183 static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
186 struct backlight_device *bd = to_backlight_device(dev);
188 return sprintf(buf, "%d\n", bd->props.power);
191 static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
192 const char *buf, size_t count)
195 struct backlight_device *bd = to_backlight_device(dev);
196 unsigned long power, old_power;
198 rc = kstrtoul(buf, 0, &power);
203 mutex_lock(&bd->ops_lock);
205 pr_debug("set power to %lu\n", power);
206 if (bd->props.power != power) {
207 old_power = bd->props.power;
208 bd->props.power = power;
209 rc = backlight_update_status(bd);
211 bd->props.power = old_power;
218 mutex_unlock(&bd->ops_lock);
222 static DEVICE_ATTR_RW(bl_power);
224 static ssize_t brightness_show(struct device *dev,
225 struct device_attribute *attr, char *buf)
227 struct backlight_device *bd = to_backlight_device(dev);
229 return sprintf(buf, "%d\n", bd->props.brightness);
232 int backlight_device_set_brightness(struct backlight_device *bd,
233 unsigned long brightness)
237 mutex_lock(&bd->ops_lock);
239 if (brightness > bd->props.max_brightness)
242 pr_debug("set brightness to %lu\n", brightness);
243 bd->props.brightness = brightness;
244 rc = backlight_update_status(bd);
247 mutex_unlock(&bd->ops_lock);
249 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
253 EXPORT_SYMBOL(backlight_device_set_brightness);
255 static ssize_t brightness_store(struct device *dev,
256 struct device_attribute *attr, const char *buf, size_t count)
259 struct backlight_device *bd = to_backlight_device(dev);
260 unsigned long brightness;
262 rc = kstrtoul(buf, 0, &brightness);
266 rc = backlight_device_set_brightness(bd, brightness);
268 return rc ? rc : count;
270 static DEVICE_ATTR_RW(brightness);
272 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
275 struct backlight_device *bd = to_backlight_device(dev);
277 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
279 static DEVICE_ATTR_RO(type);
281 static ssize_t max_brightness_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
284 struct backlight_device *bd = to_backlight_device(dev);
286 return sprintf(buf, "%d\n", bd->props.max_brightness);
288 static DEVICE_ATTR_RO(max_brightness);
290 static ssize_t actual_brightness_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
294 struct backlight_device *bd = to_backlight_device(dev);
296 mutex_lock(&bd->ops_lock);
297 if (bd->ops && bd->ops->get_brightness) {
298 rc = bd->ops->get_brightness(bd);
300 rc = sprintf(buf, "%d\n", rc);
302 rc = sprintf(buf, "%d\n", bd->props.brightness);
304 mutex_unlock(&bd->ops_lock);
308 static DEVICE_ATTR_RO(actual_brightness);
310 static ssize_t scale_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
313 struct backlight_device *bd = to_backlight_device(dev);
315 if (WARN_ON(bd->props.scale > BACKLIGHT_SCALE_NON_LINEAR))
316 return sprintf(buf, "unknown\n");
318 return sprintf(buf, "%s\n", backlight_scale_types[bd->props.scale]);
320 static DEVICE_ATTR_RO(scale);
322 #ifdef CONFIG_PM_SLEEP
323 static int backlight_suspend(struct device *dev)
325 struct backlight_device *bd = to_backlight_device(dev);
327 mutex_lock(&bd->ops_lock);
328 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
329 bd->props.state |= BL_CORE_SUSPENDED;
330 backlight_update_status(bd);
332 mutex_unlock(&bd->ops_lock);
337 static int backlight_resume(struct device *dev)
339 struct backlight_device *bd = to_backlight_device(dev);
341 mutex_lock(&bd->ops_lock);
342 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
343 bd->props.state &= ~BL_CORE_SUSPENDED;
344 backlight_update_status(bd);
346 mutex_unlock(&bd->ops_lock);
352 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
355 static void bl_device_release(struct device *dev)
357 struct backlight_device *bd = to_backlight_device(dev);
361 static struct attribute *bl_device_attrs[] = {
362 &dev_attr_bl_power.attr,
363 &dev_attr_brightness.attr,
364 &dev_attr_actual_brightness.attr,
365 &dev_attr_max_brightness.attr,
366 &dev_attr_scale.attr,
370 ATTRIBUTE_GROUPS(bl_device);
372 static const struct class backlight_class = {
374 .dev_groups = bl_device_groups,
375 .pm = &backlight_class_dev_pm_ops,
379 * backlight_force_update - tell the backlight subsystem that hardware state
381 * @bd: the backlight device to update
382 * @reason: reason for update
384 * Updates the internal state of the backlight in response to a hardware event,
385 * and generates an uevent to notify userspace. A backlight driver shall call
386 * backlight_force_update() when the backlight is changed using, for example,
387 * a hot-key. The updated brightness is read using get_brightness() and the
388 * brightness value is reported using an uevent.
390 void backlight_force_update(struct backlight_device *bd,
391 enum backlight_update_reason reason)
395 mutex_lock(&bd->ops_lock);
396 if (bd->ops && bd->ops->get_brightness) {
397 brightness = bd->ops->get_brightness(bd);
399 bd->props.brightness = brightness;
402 "Could not update brightness from device: %pe\n",
403 ERR_PTR(brightness));
405 mutex_unlock(&bd->ops_lock);
406 backlight_generate_event(bd, reason);
408 EXPORT_SYMBOL(backlight_force_update);
410 /* deprecated - use devm_backlight_device_register() */
411 struct backlight_device *backlight_device_register(const char *name,
412 struct device *parent, void *devdata, const struct backlight_ops *ops,
413 const struct backlight_properties *props)
415 struct backlight_device *new_bd;
418 pr_debug("backlight_device_register: name=%s\n", name);
420 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
422 return ERR_PTR(-ENOMEM);
424 mutex_init(&new_bd->update_lock);
425 mutex_init(&new_bd->ops_lock);
427 new_bd->dev.class = &backlight_class;
428 new_bd->dev.parent = parent;
429 new_bd->dev.release = bl_device_release;
430 dev_set_name(&new_bd->dev, "%s", name);
431 dev_set_drvdata(&new_bd->dev, devdata);
433 /* Set default properties */
435 memcpy(&new_bd->props, props,
436 sizeof(struct backlight_properties));
437 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
438 WARN(1, "%s: invalid backlight type", name);
439 new_bd->props.type = BACKLIGHT_RAW;
442 new_bd->props.type = BACKLIGHT_RAW;
445 rc = device_register(&new_bd->dev);
447 put_device(&new_bd->dev);
451 rc = backlight_register_fb(new_bd);
453 device_unregister(&new_bd->dev);
459 #ifdef CONFIG_PMAC_BACKLIGHT
460 mutex_lock(&pmac_backlight_mutex);
462 pmac_backlight = new_bd;
463 mutex_unlock(&pmac_backlight_mutex);
466 mutex_lock(&backlight_dev_list_mutex);
467 list_add(&new_bd->entry, &backlight_dev_list);
468 mutex_unlock(&backlight_dev_list_mutex);
470 blocking_notifier_call_chain(&backlight_notifier,
471 BACKLIGHT_REGISTERED, new_bd);
475 EXPORT_SYMBOL(backlight_device_register);
477 /** backlight_device_get_by_type - find first backlight device of a type
478 * @type: the type of backlight device
480 * Look up the first backlight device of the specified type
484 * Pointer to backlight device if any was found. Otherwise NULL.
486 struct backlight_device *backlight_device_get_by_type(enum backlight_type type)
489 struct backlight_device *bd;
491 mutex_lock(&backlight_dev_list_mutex);
492 list_for_each_entry(bd, &backlight_dev_list, entry) {
493 if (bd->props.type == type) {
498 mutex_unlock(&backlight_dev_list_mutex);
500 return found ? bd : NULL;
502 EXPORT_SYMBOL(backlight_device_get_by_type);
505 * backlight_device_get_by_name - Get backlight device by name
508 * This function looks up a backlight device by its name. It obtains a reference
509 * on the backlight device and it is the caller's responsibility to drop the
510 * reference by calling put_device().
513 * A pointer to the backlight device if found, otherwise NULL.
515 struct backlight_device *backlight_device_get_by_name(const char *name)
519 dev = class_find_device_by_name(&backlight_class, name);
521 return dev ? to_backlight_device(dev) : NULL;
523 EXPORT_SYMBOL(backlight_device_get_by_name);
525 /* deprecated - use devm_backlight_device_unregister() */
526 void backlight_device_unregister(struct backlight_device *bd)
531 mutex_lock(&backlight_dev_list_mutex);
532 list_del(&bd->entry);
533 mutex_unlock(&backlight_dev_list_mutex);
535 #ifdef CONFIG_PMAC_BACKLIGHT
536 mutex_lock(&pmac_backlight_mutex);
537 if (pmac_backlight == bd)
538 pmac_backlight = NULL;
539 mutex_unlock(&pmac_backlight_mutex);
542 blocking_notifier_call_chain(&backlight_notifier,
543 BACKLIGHT_UNREGISTERED, bd);
545 mutex_lock(&bd->ops_lock);
547 mutex_unlock(&bd->ops_lock);
549 backlight_unregister_fb(bd);
550 device_unregister(&bd->dev);
552 EXPORT_SYMBOL(backlight_device_unregister);
554 static void devm_backlight_device_release(struct device *dev, void *res)
556 struct backlight_device *backlight = *(struct backlight_device **)res;
558 backlight_device_unregister(backlight);
561 static int devm_backlight_device_match(struct device *dev, void *res,
564 struct backlight_device **r = res;
570 * backlight_register_notifier - get notified of backlight (un)registration
571 * @nb: notifier block with the notifier to call on backlight (un)registration
573 * Register a notifier to get notified when backlight devices get registered
578 * 0 on success, otherwise a negative error code
580 int backlight_register_notifier(struct notifier_block *nb)
582 return blocking_notifier_chain_register(&backlight_notifier, nb);
584 EXPORT_SYMBOL(backlight_register_notifier);
587 * backlight_unregister_notifier - unregister a backlight notifier
588 * @nb: notifier block to unregister
590 * Register a notifier to get notified when backlight devices get registered
595 * 0 on success, otherwise a negative error code
597 int backlight_unregister_notifier(struct notifier_block *nb)
599 return blocking_notifier_chain_unregister(&backlight_notifier, nb);
601 EXPORT_SYMBOL(backlight_unregister_notifier);
604 * devm_backlight_device_register - register a new backlight device
605 * @dev: the device to register
606 * @name: the name of the device
607 * @parent: a pointer to the parent device (often the same as @dev)
608 * @devdata: an optional pointer to be stored for private driver use
609 * @ops: the backlight operations structure
610 * @props: the backlight properties
612 * Creates and registers new backlight device. When a backlight device
613 * is registered the configuration must be specified in the @props
614 * parameter. See description of &backlight_properties.
618 * struct backlight on success, or an ERR_PTR on error
620 struct backlight_device *devm_backlight_device_register(struct device *dev,
621 const char *name, struct device *parent, void *devdata,
622 const struct backlight_ops *ops,
623 const struct backlight_properties *props)
625 struct backlight_device **ptr, *backlight;
627 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
630 return ERR_PTR(-ENOMEM);
632 backlight = backlight_device_register(name, parent, devdata, ops,
634 if (!IS_ERR(backlight)) {
636 devres_add(dev, ptr);
643 EXPORT_SYMBOL(devm_backlight_device_register);
646 * devm_backlight_device_unregister - unregister backlight device
647 * @dev: the device to unregister
648 * @bd: the backlight device to unregister
650 * Deallocates a backlight allocated with devm_backlight_device_register().
651 * Normally this function will not need to be called and the resource management
652 * code will ensure that the resources are freed.
654 void devm_backlight_device_unregister(struct device *dev,
655 struct backlight_device *bd)
659 rc = devres_release(dev, devm_backlight_device_release,
660 devm_backlight_device_match, bd);
663 EXPORT_SYMBOL(devm_backlight_device_unregister);
666 static int of_parent_match(struct device *dev, const void *data)
668 return dev->parent && dev->parent->of_node == data;
672 * of_find_backlight_by_node() - find backlight device by device-tree node
673 * @node: device-tree node of the backlight device
675 * Returns a pointer to the backlight device corresponding to the given DT
676 * node or NULL if no such backlight device exists or if the device hasn't
679 * This function obtains a reference on the backlight device and it is the
680 * caller's responsibility to drop the reference by calling put_device() on
681 * the backlight device's .dev field.
683 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
687 dev = class_find_device(&backlight_class, NULL, node, of_parent_match);
689 return dev ? to_backlight_device(dev) : NULL;
691 EXPORT_SYMBOL(of_find_backlight_by_node);
694 static struct backlight_device *of_find_backlight(struct device *dev)
696 struct backlight_device *bd = NULL;
697 struct device_node *np;
702 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
703 np = of_parse_phandle(dev->of_node, "backlight", 0);
705 bd = of_find_backlight_by_node(np);
708 return ERR_PTR(-EPROBE_DEFER);
715 static void devm_backlight_release(void *data)
717 struct backlight_device *bd = data;
719 put_device(&bd->dev);
723 * devm_of_find_backlight - find backlight for a device
726 * This function looks for a property named 'backlight' on the DT node
727 * connected to @dev and looks up the backlight device. The lookup is
728 * device managed so the reference to the backlight device is automatically
729 * dropped on driver detach.
733 * A pointer to the backlight device if found.
734 * Error pointer -EPROBE_DEFER if the DT property is set, but no backlight
735 * device is found. NULL if there's no backlight property.
737 struct backlight_device *devm_of_find_backlight(struct device *dev)
739 struct backlight_device *bd;
742 bd = of_find_backlight(dev);
743 if (IS_ERR_OR_NULL(bd))
745 ret = devm_add_action_or_reset(dev, devm_backlight_release, bd);
751 EXPORT_SYMBOL(devm_of_find_backlight);
753 static void __exit backlight_class_exit(void)
755 class_unregister(&backlight_class);
758 static int __init backlight_class_init(void)
762 ret = class_register(&backlight_class);
764 pr_warn("Unable to create backlight class; errno = %d\n", ret);
768 INIT_LIST_HEAD(&backlight_dev_list);
769 mutex_init(&backlight_dev_list_mutex);
770 BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier);
776 * if this is compiled into the kernel, we need to ensure that the
777 * class is registered before users of the class try to register lcd's
779 postcore_initcall(backlight_class_init);
780 module_exit(backlight_class_exit);
782 MODULE_LICENSE("GPL");
784 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");