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_get_by_name - Get backlight device by name
439 * This function looks up a backlight device by its name. It obtains a reference
440 * on the backlight device and it is the caller's responsibility to drop the
441 * reference by calling backlight_put().
444 * A pointer to the backlight device if found, otherwise NULL.
446 struct backlight_device *backlight_device_get_by_name(const char *name)
450 dev = class_find_device_by_name(backlight_class, name);
452 return dev ? to_backlight_device(dev) : NULL;
454 EXPORT_SYMBOL(backlight_device_get_by_name);
457 * backlight_device_unregister - unregisters a backlight device object.
458 * @bd: the backlight device object to be unregistered and freed.
460 * Unregisters a previously registered via backlight_device_register object.
462 void backlight_device_unregister(struct backlight_device *bd)
467 mutex_lock(&backlight_dev_list_mutex);
468 list_del(&bd->entry);
469 mutex_unlock(&backlight_dev_list_mutex);
471 #ifdef CONFIG_PMAC_BACKLIGHT
472 mutex_lock(&pmac_backlight_mutex);
473 if (pmac_backlight == bd)
474 pmac_backlight = NULL;
475 mutex_unlock(&pmac_backlight_mutex);
478 blocking_notifier_call_chain(&backlight_notifier,
479 BACKLIGHT_UNREGISTERED, bd);
481 mutex_lock(&bd->ops_lock);
483 mutex_unlock(&bd->ops_lock);
485 backlight_unregister_fb(bd);
486 device_unregister(&bd->dev);
488 EXPORT_SYMBOL(backlight_device_unregister);
490 static void devm_backlight_device_release(struct device *dev, void *res)
492 struct backlight_device *backlight = *(struct backlight_device **)res;
494 backlight_device_unregister(backlight);
497 static int devm_backlight_device_match(struct device *dev, void *res,
500 struct backlight_device **r = res;
506 * backlight_register_notifier - get notified of backlight (un)registration
507 * @nb: notifier block with the notifier to call on backlight (un)registration
509 * @return 0 on success, otherwise a negative error code
511 * Register a notifier to get notified when backlight devices get registered
514 int backlight_register_notifier(struct notifier_block *nb)
516 return blocking_notifier_chain_register(&backlight_notifier, nb);
518 EXPORT_SYMBOL(backlight_register_notifier);
521 * backlight_unregister_notifier - unregister a backlight notifier
522 * @nb: notifier block to unregister
524 * @return 0 on success, otherwise a negative error code
526 * Register a notifier to get notified when backlight devices get registered
529 int backlight_unregister_notifier(struct notifier_block *nb)
531 return blocking_notifier_chain_unregister(&backlight_notifier, nb);
533 EXPORT_SYMBOL(backlight_unregister_notifier);
536 * devm_backlight_device_register - resource managed backlight_device_register()
537 * @dev: the device to register
538 * @name: the name of the device
539 * @parent: a pointer to the parent device
540 * @devdata: an optional pointer to be stored for private driver use
541 * @ops: the backlight operations structure
542 * @props: the backlight properties
544 * @return a struct backlight on success, or an ERR_PTR on error
546 * Managed backlight_device_register(). The backlight_device returned
547 * from this function are automatically freed on driver detach.
548 * See backlight_device_register() for more information.
550 struct backlight_device *devm_backlight_device_register(struct device *dev,
551 const char *name, struct device *parent, void *devdata,
552 const struct backlight_ops *ops,
553 const struct backlight_properties *props)
555 struct backlight_device **ptr, *backlight;
557 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
560 return ERR_PTR(-ENOMEM);
562 backlight = backlight_device_register(name, parent, devdata, ops,
564 if (!IS_ERR(backlight)) {
566 devres_add(dev, ptr);
573 EXPORT_SYMBOL(devm_backlight_device_register);
576 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
577 * @dev: the device to unregister
578 * @bd: the backlight device to unregister
580 * Deallocated a backlight allocated with devm_backlight_device_register().
581 * Normally this function will not need to be called and the resource management
582 * code will ensure that the resource is freed.
584 void devm_backlight_device_unregister(struct device *dev,
585 struct backlight_device *bd)
589 rc = devres_release(dev, devm_backlight_device_release,
590 devm_backlight_device_match, bd);
593 EXPORT_SYMBOL(devm_backlight_device_unregister);
596 static int of_parent_match(struct device *dev, const void *data)
598 return dev->parent && dev->parent->of_node == data;
602 * of_find_backlight_by_node() - find backlight device by device-tree node
603 * @node: device-tree node of the backlight device
605 * Returns a pointer to the backlight device corresponding to the given DT
606 * node or NULL if no such backlight device exists or if the device hasn't
609 * This function obtains a reference on the backlight device and it is the
610 * caller's responsibility to drop the reference by calling put_device() on
611 * the backlight device's .dev field.
613 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
617 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
619 return dev ? to_backlight_device(dev) : NULL;
621 EXPORT_SYMBOL(of_find_backlight_by_node);
625 * of_find_backlight - Get backlight device
628 * This function looks for a property named 'backlight' on the DT node
629 * connected to @dev and looks up the backlight device.
631 * Call backlight_put() to drop the reference on the backlight device.
634 * A pointer to the backlight device if found.
635 * Error pointer -EPROBE_DEFER if the DT property is set, but no backlight
637 * NULL if there's no backlight property.
639 struct backlight_device *of_find_backlight(struct device *dev)
641 struct backlight_device *bd = NULL;
642 struct device_node *np;
647 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
648 np = of_parse_phandle(dev->of_node, "backlight", 0);
650 bd = of_find_backlight_by_node(np);
653 return ERR_PTR(-EPROBE_DEFER);
655 * Note: gpio_backlight uses brightness as
656 * power state during probe
658 if (!bd->props.brightness)
659 bd->props.brightness = bd->props.max_brightness;
665 EXPORT_SYMBOL(of_find_backlight);
667 static void devm_backlight_release(void *data)
673 * devm_of_find_backlight - Resource-managed of_find_backlight()
676 * Device managed version of of_find_backlight().
677 * The reference on the backlight device is automatically
678 * dropped on driver detach.
680 struct backlight_device *devm_of_find_backlight(struct device *dev)
682 struct backlight_device *bd;
685 bd = of_find_backlight(dev);
686 if (IS_ERR_OR_NULL(bd))
688 ret = devm_add_action(dev, devm_backlight_release, bd);
695 EXPORT_SYMBOL(devm_of_find_backlight);
697 static void __exit backlight_class_exit(void)
699 class_destroy(backlight_class);
702 static int __init backlight_class_init(void)
704 backlight_class = class_create(THIS_MODULE, "backlight");
705 if (IS_ERR(backlight_class)) {
706 pr_warn("Unable to create backlight class; errno = %ld\n",
707 PTR_ERR(backlight_class));
708 return PTR_ERR(backlight_class);
711 backlight_class->dev_groups = bl_device_groups;
712 backlight_class->pm = &backlight_class_dev_pm_ops;
713 INIT_LIST_HEAD(&backlight_dev_list);
714 mutex_init(&backlight_dev_list_mutex);
715 BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier);
721 * if this is compiled into the kernel, we need to ensure that the
722 * class is registered before users of the class try to register lcd's
724 postcore_initcall(backlight_class_init);
725 module_exit(backlight_class_exit);
727 MODULE_LICENSE("GPL");
729 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");