Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / video / backlight / backlight.c
blobf699e5827ccb3a41360a01a69dd2ac501d765510
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Backlight Lowlevel Control Abstraction
5 * Copyright (C) 2003,2004 Hewlett-Packard Company
7 */
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>
18 #include <linux/fb.h>
19 #include <linux/slab.h>
21 #ifdef CONFIG_PMAC_BACKLIGHT
22 #include <asm/backlight.h>
23 #endif
25 /**
26 * DOC: overview
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;
69 static const char *const backlight_types[] = {
70 [BACKLIGHT_RAW] = "raw",
71 [BACKLIGHT_PLATFORM] = "platform",
72 [BACKLIGHT_FIRMWARE] = "firmware",
75 static const char *const backlight_scale_types[] = {
76 [BACKLIGHT_SCALE_UNKNOWN] = "unknown",
77 [BACKLIGHT_SCALE_LINEAR] = "linear",
78 [BACKLIGHT_SCALE_NON_LINEAR] = "non-linear",
81 #if defined(CONFIG_FB_CORE) || (defined(CONFIG_FB_CORE_MODULE) && \
82 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
84 * fb_notifier_callback
86 * This callback gets called when something important happens inside a
87 * framebuffer driver. The backlight core only cares about FB_BLANK_UNBLANK
88 * which is reported to the driver using backlight_update_status()
89 * as a state change.
91 * There may be several fbdev's connected to the backlight device,
92 * in which case they are kept track of. A state change is only reported
93 * if there is a change in backlight for the specified fbdev.
95 static int fb_notifier_callback(struct notifier_block *self,
96 unsigned long event, void *data)
98 struct backlight_device *bd;
99 struct fb_event *evdata = data;
100 struct fb_info *info = evdata->info;
101 struct backlight_device *fb_bd = fb_bl_device(info);
102 int node = info->node;
103 int fb_blank = 0;
105 /* If we aren't interested in this event, skip it immediately ... */
106 if (event != FB_EVENT_BLANK)
107 return 0;
109 bd = container_of(self, struct backlight_device, fb_notif);
110 mutex_lock(&bd->ops_lock);
112 if (!bd->ops)
113 goto out;
114 if (bd->ops->controls_device && !bd->ops->controls_device(bd, info->device))
115 goto out;
116 if (fb_bd && fb_bd != bd)
117 goto out;
119 fb_blank = *(int *)evdata->data;
120 if (fb_blank == FB_BLANK_UNBLANK && !bd->fb_bl_on[node]) {
121 bd->fb_bl_on[node] = true;
122 if (!bd->use_count++) {
123 bd->props.state &= ~BL_CORE_FBBLANK;
124 backlight_update_status(bd);
126 } else if (fb_blank != FB_BLANK_UNBLANK && bd->fb_bl_on[node]) {
127 bd->fb_bl_on[node] = false;
128 if (!(--bd->use_count)) {
129 bd->props.state |= BL_CORE_FBBLANK;
130 backlight_update_status(bd);
133 out:
134 mutex_unlock(&bd->ops_lock);
135 return 0;
138 static int backlight_register_fb(struct backlight_device *bd)
140 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
141 bd->fb_notif.notifier_call = fb_notifier_callback;
143 return fb_register_client(&bd->fb_notif);
146 static void backlight_unregister_fb(struct backlight_device *bd)
148 fb_unregister_client(&bd->fb_notif);
150 #else
151 static inline int backlight_register_fb(struct backlight_device *bd)
153 return 0;
156 static inline void backlight_unregister_fb(struct backlight_device *bd)
159 #endif /* CONFIG_FB_CORE */
161 static void backlight_generate_event(struct backlight_device *bd,
162 enum backlight_update_reason reason)
164 char *envp[2];
166 switch (reason) {
167 case BACKLIGHT_UPDATE_SYSFS:
168 envp[0] = "SOURCE=sysfs";
169 break;
170 case BACKLIGHT_UPDATE_HOTKEY:
171 envp[0] = "SOURCE=hotkey";
172 break;
173 default:
174 envp[0] = "SOURCE=unknown";
175 break;
177 envp[1] = NULL;
178 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
179 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
182 static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
183 char *buf)
185 struct backlight_device *bd = to_backlight_device(dev);
187 return sprintf(buf, "%d\n", bd->props.power);
190 static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t count)
193 int rc;
194 struct backlight_device *bd = to_backlight_device(dev);
195 unsigned long power, old_power;
197 rc = kstrtoul(buf, 0, &power);
198 if (rc)
199 return rc;
201 rc = -ENXIO;
202 mutex_lock(&bd->ops_lock);
203 if (bd->ops) {
204 pr_debug("set power to %lu\n", power);
205 if (bd->props.power != power) {
206 old_power = bd->props.power;
207 bd->props.power = power;
208 rc = backlight_update_status(bd);
209 if (rc)
210 bd->props.power = old_power;
211 else
212 rc = count;
213 } else {
214 rc = count;
217 mutex_unlock(&bd->ops_lock);
219 return rc;
221 static DEVICE_ATTR_RW(bl_power);
223 static ssize_t 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.brightness);
231 int backlight_device_set_brightness(struct backlight_device *bd,
232 unsigned long brightness)
234 int rc = -ENXIO;
236 mutex_lock(&bd->ops_lock);
237 if (bd->ops) {
238 if (brightness > bd->props.max_brightness)
239 rc = -EINVAL;
240 else {
241 pr_debug("set brightness to %lu\n", brightness);
242 bd->props.brightness = brightness;
243 rc = backlight_update_status(bd);
246 mutex_unlock(&bd->ops_lock);
248 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
250 return rc;
252 EXPORT_SYMBOL(backlight_device_set_brightness);
254 static ssize_t brightness_store(struct device *dev,
255 struct device_attribute *attr, const char *buf, size_t count)
257 int rc;
258 struct backlight_device *bd = to_backlight_device(dev);
259 unsigned long brightness;
261 rc = kstrtoul(buf, 0, &brightness);
262 if (rc)
263 return rc;
265 rc = backlight_device_set_brightness(bd, brightness);
267 return rc ? rc : count;
269 static DEVICE_ATTR_RW(brightness);
271 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
272 char *buf)
274 struct backlight_device *bd = to_backlight_device(dev);
276 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
278 static DEVICE_ATTR_RO(type);
280 static ssize_t max_brightness_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
283 struct backlight_device *bd = to_backlight_device(dev);
285 return sprintf(buf, "%d\n", bd->props.max_brightness);
287 static DEVICE_ATTR_RO(max_brightness);
289 static ssize_t actual_brightness_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 int rc = -ENXIO;
293 struct backlight_device *bd = to_backlight_device(dev);
295 mutex_lock(&bd->ops_lock);
296 if (bd->ops && bd->ops->get_brightness) {
297 rc = bd->ops->get_brightness(bd);
298 if (rc >= 0)
299 rc = sprintf(buf, "%d\n", rc);
300 } else {
301 rc = sprintf(buf, "%d\n", bd->props.brightness);
303 mutex_unlock(&bd->ops_lock);
305 return rc;
307 static DEVICE_ATTR_RO(actual_brightness);
309 static ssize_t scale_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 struct backlight_device *bd = to_backlight_device(dev);
314 if (WARN_ON(bd->props.scale > BACKLIGHT_SCALE_NON_LINEAR))
315 return sprintf(buf, "unknown\n");
317 return sprintf(buf, "%s\n", backlight_scale_types[bd->props.scale]);
319 static DEVICE_ATTR_RO(scale);
321 #ifdef CONFIG_PM_SLEEP
322 static int backlight_suspend(struct device *dev)
324 struct backlight_device *bd = to_backlight_device(dev);
326 mutex_lock(&bd->ops_lock);
327 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
328 bd->props.state |= BL_CORE_SUSPENDED;
329 backlight_update_status(bd);
331 mutex_unlock(&bd->ops_lock);
333 return 0;
336 static int backlight_resume(struct device *dev)
338 struct backlight_device *bd = to_backlight_device(dev);
340 mutex_lock(&bd->ops_lock);
341 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
342 bd->props.state &= ~BL_CORE_SUSPENDED;
343 backlight_update_status(bd);
345 mutex_unlock(&bd->ops_lock);
347 return 0;
349 #endif
351 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
352 backlight_resume);
354 static void bl_device_release(struct device *dev)
356 struct backlight_device *bd = to_backlight_device(dev);
357 kfree(bd);
360 static struct attribute *bl_device_attrs[] = {
361 &dev_attr_bl_power.attr,
362 &dev_attr_brightness.attr,
363 &dev_attr_actual_brightness.attr,
364 &dev_attr_max_brightness.attr,
365 &dev_attr_scale.attr,
366 &dev_attr_type.attr,
367 NULL,
369 ATTRIBUTE_GROUPS(bl_device);
371 static const struct class backlight_class = {
372 .name = "backlight",
373 .dev_groups = bl_device_groups,
374 .pm = &backlight_class_dev_pm_ops,
378 * backlight_force_update - tell the backlight subsystem that hardware state
379 * has changed
380 * @bd: the backlight device to update
381 * @reason: reason for update
383 * Updates the internal state of the backlight in response to a hardware event,
384 * and generates an uevent to notify userspace. A backlight driver shall call
385 * backlight_force_update() when the backlight is changed using, for example,
386 * a hot-key. The updated brightness is read using get_brightness() and the
387 * brightness value is reported using an uevent.
389 void backlight_force_update(struct backlight_device *bd,
390 enum backlight_update_reason reason)
392 int brightness;
394 mutex_lock(&bd->ops_lock);
395 if (bd->ops && bd->ops->get_brightness) {
396 brightness = bd->ops->get_brightness(bd);
397 if (brightness >= 0)
398 bd->props.brightness = brightness;
399 else
400 dev_err(&bd->dev,
401 "Could not update brightness from device: %pe\n",
402 ERR_PTR(brightness));
404 mutex_unlock(&bd->ops_lock);
405 backlight_generate_event(bd, reason);
407 EXPORT_SYMBOL(backlight_force_update);
409 /* deprecated - use devm_backlight_device_register() */
410 struct backlight_device *backlight_device_register(const char *name,
411 struct device *parent, void *devdata, const struct backlight_ops *ops,
412 const struct backlight_properties *props)
414 struct backlight_device *new_bd;
415 int rc;
417 pr_debug("backlight_device_register: name=%s\n", name);
419 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
420 if (!new_bd)
421 return ERR_PTR(-ENOMEM);
423 mutex_init(&new_bd->update_lock);
424 mutex_init(&new_bd->ops_lock);
426 new_bd->dev.class = &backlight_class;
427 new_bd->dev.parent = parent;
428 new_bd->dev.release = bl_device_release;
429 dev_set_name(&new_bd->dev, "%s", name);
430 dev_set_drvdata(&new_bd->dev, devdata);
432 /* Set default properties */
433 if (props) {
434 memcpy(&new_bd->props, props,
435 sizeof(struct backlight_properties));
436 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
437 WARN(1, "%s: invalid backlight type", name);
438 new_bd->props.type = BACKLIGHT_RAW;
440 } else {
441 new_bd->props.type = BACKLIGHT_RAW;
444 rc = device_register(&new_bd->dev);
445 if (rc) {
446 put_device(&new_bd->dev);
447 return ERR_PTR(rc);
450 rc = backlight_register_fb(new_bd);
451 if (rc) {
452 device_unregister(&new_bd->dev);
453 return ERR_PTR(rc);
456 new_bd->ops = ops;
458 #ifdef CONFIG_PMAC_BACKLIGHT
459 mutex_lock(&pmac_backlight_mutex);
460 if (!pmac_backlight)
461 pmac_backlight = new_bd;
462 mutex_unlock(&pmac_backlight_mutex);
463 #endif
465 mutex_lock(&backlight_dev_list_mutex);
466 list_add(&new_bd->entry, &backlight_dev_list);
467 mutex_unlock(&backlight_dev_list_mutex);
469 return new_bd;
471 EXPORT_SYMBOL(backlight_device_register);
473 /** backlight_device_get_by_type - find first backlight device of a type
474 * @type: the type of backlight device
476 * Look up the first backlight device of the specified type
478 * RETURNS:
480 * Pointer to backlight device if any was found. Otherwise NULL.
482 struct backlight_device *backlight_device_get_by_type(enum backlight_type type)
484 bool found = false;
485 struct backlight_device *bd;
487 mutex_lock(&backlight_dev_list_mutex);
488 list_for_each_entry(bd, &backlight_dev_list, entry) {
489 if (bd->props.type == type) {
490 found = true;
491 break;
494 mutex_unlock(&backlight_dev_list_mutex);
496 return found ? bd : NULL;
498 EXPORT_SYMBOL(backlight_device_get_by_type);
501 * backlight_device_get_by_name - Get backlight device by name
502 * @name: Device name
504 * This function looks up a backlight device by its name. It obtains a reference
505 * on the backlight device and it is the caller's responsibility to drop the
506 * reference by calling put_device().
508 * Returns:
509 * A pointer to the backlight device if found, otherwise NULL.
511 struct backlight_device *backlight_device_get_by_name(const char *name)
513 struct device *dev;
515 dev = class_find_device_by_name(&backlight_class, name);
517 return dev ? to_backlight_device(dev) : NULL;
519 EXPORT_SYMBOL(backlight_device_get_by_name);
521 /* deprecated - use devm_backlight_device_unregister() */
522 void backlight_device_unregister(struct backlight_device *bd)
524 if (!bd)
525 return;
527 mutex_lock(&backlight_dev_list_mutex);
528 list_del(&bd->entry);
529 mutex_unlock(&backlight_dev_list_mutex);
531 #ifdef CONFIG_PMAC_BACKLIGHT
532 mutex_lock(&pmac_backlight_mutex);
533 if (pmac_backlight == bd)
534 pmac_backlight = NULL;
535 mutex_unlock(&pmac_backlight_mutex);
536 #endif
538 mutex_lock(&bd->ops_lock);
539 bd->ops = NULL;
540 mutex_unlock(&bd->ops_lock);
542 backlight_unregister_fb(bd);
543 device_unregister(&bd->dev);
545 EXPORT_SYMBOL(backlight_device_unregister);
547 static void devm_backlight_device_release(struct device *dev, void *res)
549 struct backlight_device *backlight = *(struct backlight_device **)res;
551 backlight_device_unregister(backlight);
554 static int devm_backlight_device_match(struct device *dev, void *res,
555 void *data)
557 struct backlight_device **r = res;
559 return *r == data;
563 * devm_backlight_device_register - register a new backlight device
564 * @dev: the device to register
565 * @name: the name of the device
566 * @parent: a pointer to the parent device (often the same as @dev)
567 * @devdata: an optional pointer to be stored for private driver use
568 * @ops: the backlight operations structure
569 * @props: the backlight properties
571 * Creates and registers new backlight device. When a backlight device
572 * is registered the configuration must be specified in the @props
573 * parameter. See description of &backlight_properties.
575 * RETURNS:
577 * struct backlight on success, or an ERR_PTR on error
579 struct backlight_device *devm_backlight_device_register(struct device *dev,
580 const char *name, struct device *parent, void *devdata,
581 const struct backlight_ops *ops,
582 const struct backlight_properties *props)
584 struct backlight_device **ptr, *backlight;
586 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
587 GFP_KERNEL);
588 if (!ptr)
589 return ERR_PTR(-ENOMEM);
591 backlight = backlight_device_register(name, parent, devdata, ops,
592 props);
593 if (!IS_ERR(backlight)) {
594 *ptr = backlight;
595 devres_add(dev, ptr);
596 } else {
597 devres_free(ptr);
600 return backlight;
602 EXPORT_SYMBOL(devm_backlight_device_register);
605 * devm_backlight_device_unregister - unregister backlight device
606 * @dev: the device to unregister
607 * @bd: the backlight device to unregister
609 * Deallocates a backlight allocated with devm_backlight_device_register().
610 * Normally this function will not need to be called and the resource management
611 * code will ensure that the resources are freed.
613 void devm_backlight_device_unregister(struct device *dev,
614 struct backlight_device *bd)
616 int rc;
618 rc = devres_release(dev, devm_backlight_device_release,
619 devm_backlight_device_match, bd);
620 WARN_ON(rc);
622 EXPORT_SYMBOL(devm_backlight_device_unregister);
624 #ifdef CONFIG_OF
625 static int of_parent_match(struct device *dev, const void *data)
627 return dev->parent && dev->parent->of_node == data;
631 * of_find_backlight_by_node() - find backlight device by device-tree node
632 * @node: device-tree node of the backlight device
634 * Returns a pointer to the backlight device corresponding to the given DT
635 * node or NULL if no such backlight device exists or if the device hasn't
636 * been probed yet.
638 * This function obtains a reference on the backlight device and it is the
639 * caller's responsibility to drop the reference by calling put_device() on
640 * the backlight device's .dev field.
642 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
644 struct device *dev;
646 dev = class_find_device(&backlight_class, NULL, node, of_parent_match);
648 return dev ? to_backlight_device(dev) : NULL;
650 EXPORT_SYMBOL(of_find_backlight_by_node);
651 #endif
653 static struct backlight_device *of_find_backlight(struct device *dev)
655 struct backlight_device *bd = NULL;
656 struct device_node *np;
658 if (!dev)
659 return NULL;
661 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
662 np = of_parse_phandle(dev->of_node, "backlight", 0);
663 if (np) {
664 bd = of_find_backlight_by_node(np);
665 of_node_put(np);
666 if (!bd)
667 return ERR_PTR(-EPROBE_DEFER);
671 return bd;
674 static void devm_backlight_release(void *data)
676 struct backlight_device *bd = data;
678 put_device(&bd->dev);
682 * devm_of_find_backlight - find backlight for a device
683 * @dev: the device
685 * This function looks for a property named 'backlight' on the DT node
686 * connected to @dev and looks up the backlight device. The lookup is
687 * device managed so the reference to the backlight device is automatically
688 * dropped on driver detach.
690 * RETURNS:
692 * A pointer to the backlight device if found.
693 * Error pointer -EPROBE_DEFER if the DT property is set, but no backlight
694 * device is found. NULL if there's no backlight property.
696 struct backlight_device *devm_of_find_backlight(struct device *dev)
698 struct backlight_device *bd;
699 int ret;
701 bd = of_find_backlight(dev);
702 if (IS_ERR_OR_NULL(bd))
703 return bd;
704 ret = devm_add_action_or_reset(dev, devm_backlight_release, bd);
705 if (ret)
706 return ERR_PTR(ret);
708 return bd;
710 EXPORT_SYMBOL(devm_of_find_backlight);
712 static void __exit backlight_class_exit(void)
714 class_unregister(&backlight_class);
717 static int __init backlight_class_init(void)
719 int ret;
721 ret = class_register(&backlight_class);
722 if (ret) {
723 pr_warn("Unable to create backlight class; errno = %d\n", ret);
724 return ret;
727 INIT_LIST_HEAD(&backlight_dev_list);
728 mutex_init(&backlight_dev_list_mutex);
730 return 0;
734 * if this is compiled into the kernel, we need to ensure that the
735 * class is registered before users of the class try to register lcd's
737 postcore_initcall(backlight_class_init);
738 module_exit(backlight_class_exit);
740 MODULE_LICENSE("GPL");
741 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
742 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");