2 * Backlight Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/backlight.h>
14 #include <linux/notifier.h>
15 #include <linux/ctype.h>
16 #include <linux/err.h>
18 #include <linux/slab.h>
20 #ifdef CONFIG_PMAC_BACKLIGHT
21 #include <asm/backlight.h>
24 static struct list_head backlight_dev_list
;
25 static struct mutex backlight_dev_list_mutex
;
27 static const char *const backlight_types
[] = {
28 [BACKLIGHT_RAW
] = "raw",
29 [BACKLIGHT_PLATFORM
] = "platform",
30 [BACKLIGHT_FIRMWARE
] = "firmware",
33 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
34 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
35 /* This callback gets called when something important happens inside a
36 * framebuffer driver. We're looking if that important event is blanking,
37 * and if it is, we're switching backlight power as well ...
39 static int fb_notifier_callback(struct notifier_block
*self
,
40 unsigned long event
, void *data
)
42 struct backlight_device
*bd
;
43 struct fb_event
*evdata
= data
;
45 /* If we aren't interested in this event, skip it immediately ... */
46 if (event
!= FB_EVENT_BLANK
&& event
!= FB_EVENT_CONBLANK
)
49 bd
= container_of(self
, struct backlight_device
, fb_notif
);
50 mutex_lock(&bd
->ops_lock
);
52 if (!bd
->ops
->check_fb
||
53 bd
->ops
->check_fb(bd
, evdata
->info
)) {
54 bd
->props
.fb_blank
= *(int *)evdata
->data
;
55 if (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
)
56 bd
->props
.state
&= ~BL_CORE_FBBLANK
;
58 bd
->props
.state
|= BL_CORE_FBBLANK
;
59 backlight_update_status(bd
);
61 mutex_unlock(&bd
->ops_lock
);
65 static int backlight_register_fb(struct backlight_device
*bd
)
67 memset(&bd
->fb_notif
, 0, sizeof(bd
->fb_notif
));
68 bd
->fb_notif
.notifier_call
= fb_notifier_callback
;
70 return fb_register_client(&bd
->fb_notif
);
73 static void backlight_unregister_fb(struct backlight_device
*bd
)
75 fb_unregister_client(&bd
->fb_notif
);
78 static inline int backlight_register_fb(struct backlight_device
*bd
)
83 static inline void backlight_unregister_fb(struct backlight_device
*bd
)
86 #endif /* CONFIG_FB */
88 static void backlight_generate_event(struct backlight_device
*bd
,
89 enum backlight_update_reason reason
)
94 case BACKLIGHT_UPDATE_SYSFS
:
95 envp
[0] = "SOURCE=sysfs";
97 case BACKLIGHT_UPDATE_HOTKEY
:
98 envp
[0] = "SOURCE=hotkey";
101 envp
[0] = "SOURCE=unknown";
105 kobject_uevent_env(&bd
->dev
.kobj
, KOBJ_CHANGE
, envp
);
106 sysfs_notify(&bd
->dev
.kobj
, NULL
, "actual_brightness");
109 static ssize_t
bl_power_show(struct device
*dev
, struct device_attribute
*attr
,
112 struct backlight_device
*bd
= to_backlight_device(dev
);
114 return sprintf(buf
, "%d\n", bd
->props
.power
);
117 static ssize_t
bl_power_store(struct device
*dev
, struct device_attribute
*attr
,
118 const char *buf
, size_t count
)
121 struct backlight_device
*bd
= to_backlight_device(dev
);
124 rc
= kstrtoul(buf
, 0, &power
);
129 mutex_lock(&bd
->ops_lock
);
131 pr_debug("set power to %lu\n", power
);
132 if (bd
->props
.power
!= power
) {
133 bd
->props
.power
= power
;
134 backlight_update_status(bd
);
138 mutex_unlock(&bd
->ops_lock
);
142 static DEVICE_ATTR_RW(bl_power
);
144 static ssize_t
brightness_show(struct device
*dev
,
145 struct device_attribute
*attr
, char *buf
)
147 struct backlight_device
*bd
= to_backlight_device(dev
);
149 return sprintf(buf
, "%d\n", bd
->props
.brightness
);
152 static ssize_t
brightness_store(struct device
*dev
,
153 struct device_attribute
*attr
, const char *buf
, size_t count
)
156 struct backlight_device
*bd
= to_backlight_device(dev
);
157 unsigned long brightness
;
159 rc
= kstrtoul(buf
, 0, &brightness
);
165 mutex_lock(&bd
->ops_lock
);
167 if (brightness
> bd
->props
.max_brightness
)
170 pr_debug("set brightness to %lu\n", brightness
);
171 bd
->props
.brightness
= brightness
;
172 backlight_update_status(bd
);
176 mutex_unlock(&bd
->ops_lock
);
178 backlight_generate_event(bd
, BACKLIGHT_UPDATE_SYSFS
);
182 static DEVICE_ATTR_RW(brightness
);
184 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
187 struct backlight_device
*bd
= to_backlight_device(dev
);
189 return sprintf(buf
, "%s\n", backlight_types
[bd
->props
.type
]);
191 static DEVICE_ATTR_RO(type
);
193 static ssize_t
max_brightness_show(struct device
*dev
,
194 struct device_attribute
*attr
, char *buf
)
196 struct backlight_device
*bd
= to_backlight_device(dev
);
198 return sprintf(buf
, "%d\n", bd
->props
.max_brightness
);
200 static DEVICE_ATTR_RO(max_brightness
);
202 static ssize_t
actual_brightness_show(struct device
*dev
,
203 struct device_attribute
*attr
, char *buf
)
206 struct backlight_device
*bd
= to_backlight_device(dev
);
208 mutex_lock(&bd
->ops_lock
);
209 if (bd
->ops
&& bd
->ops
->get_brightness
)
210 rc
= sprintf(buf
, "%d\n", bd
->ops
->get_brightness(bd
));
211 mutex_unlock(&bd
->ops_lock
);
215 static DEVICE_ATTR_RO(actual_brightness
);
217 static struct class *backlight_class
;
219 #ifdef CONFIG_PM_SLEEP
220 static int backlight_suspend(struct device
*dev
)
222 struct backlight_device
*bd
= to_backlight_device(dev
);
224 mutex_lock(&bd
->ops_lock
);
225 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
226 bd
->props
.state
|= BL_CORE_SUSPENDED
;
227 backlight_update_status(bd
);
229 mutex_unlock(&bd
->ops_lock
);
234 static int backlight_resume(struct device
*dev
)
236 struct backlight_device
*bd
= to_backlight_device(dev
);
238 mutex_lock(&bd
->ops_lock
);
239 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
240 bd
->props
.state
&= ~BL_CORE_SUSPENDED
;
241 backlight_update_status(bd
);
243 mutex_unlock(&bd
->ops_lock
);
249 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops
, backlight_suspend
,
252 static void bl_device_release(struct device
*dev
)
254 struct backlight_device
*bd
= to_backlight_device(dev
);
258 static struct attribute
*bl_device_attrs
[] = {
259 &dev_attr_bl_power
.attr
,
260 &dev_attr_brightness
.attr
,
261 &dev_attr_actual_brightness
.attr
,
262 &dev_attr_max_brightness
.attr
,
266 ATTRIBUTE_GROUPS(bl_device
);
269 * backlight_force_update - tell the backlight subsystem that hardware state
271 * @bd: the backlight device to update
273 * Updates the internal state of the backlight in response to a hardware event,
274 * and generate a uevent to notify userspace
276 void backlight_force_update(struct backlight_device
*bd
,
277 enum backlight_update_reason reason
)
279 mutex_lock(&bd
->ops_lock
);
280 if (bd
->ops
&& bd
->ops
->get_brightness
)
281 bd
->props
.brightness
= bd
->ops
->get_brightness(bd
);
282 mutex_unlock(&bd
->ops_lock
);
283 backlight_generate_event(bd
, reason
);
285 EXPORT_SYMBOL(backlight_force_update
);
288 * backlight_device_register - create and register a new object of
289 * backlight_device class.
290 * @name: the name of the new object(must be the same as the name of the
291 * respective framebuffer device).
292 * @parent: a pointer to the parent device
293 * @devdata: an optional pointer to be stored for private driver use. The
294 * methods may retrieve it by using bl_get_data(bd).
295 * @ops: the backlight operations structure.
297 * Creates and registers new backlight device. Returns either an
298 * ERR_PTR() or a pointer to the newly allocated device.
300 struct backlight_device
*backlight_device_register(const char *name
,
301 struct device
*parent
, void *devdata
, const struct backlight_ops
*ops
,
302 const struct backlight_properties
*props
)
304 struct backlight_device
*new_bd
;
307 pr_debug("backlight_device_register: name=%s\n", name
);
309 new_bd
= kzalloc(sizeof(struct backlight_device
), GFP_KERNEL
);
311 return ERR_PTR(-ENOMEM
);
313 mutex_init(&new_bd
->update_lock
);
314 mutex_init(&new_bd
->ops_lock
);
316 new_bd
->dev
.class = backlight_class
;
317 new_bd
->dev
.parent
= parent
;
318 new_bd
->dev
.release
= bl_device_release
;
319 dev_set_name(&new_bd
->dev
, "%s", name
);
320 dev_set_drvdata(&new_bd
->dev
, devdata
);
322 /* Set default properties */
324 memcpy(&new_bd
->props
, props
,
325 sizeof(struct backlight_properties
));
326 if (props
->type
<= 0 || props
->type
>= BACKLIGHT_TYPE_MAX
) {
327 WARN(1, "%s: invalid backlight type", name
);
328 new_bd
->props
.type
= BACKLIGHT_RAW
;
331 new_bd
->props
.type
= BACKLIGHT_RAW
;
334 rc
= device_register(&new_bd
->dev
);
340 rc
= backlight_register_fb(new_bd
);
342 device_unregister(&new_bd
->dev
);
348 #ifdef CONFIG_PMAC_BACKLIGHT
349 mutex_lock(&pmac_backlight_mutex
);
351 pmac_backlight
= new_bd
;
352 mutex_unlock(&pmac_backlight_mutex
);
355 mutex_lock(&backlight_dev_list_mutex
);
356 list_add(&new_bd
->entry
, &backlight_dev_list
);
357 mutex_unlock(&backlight_dev_list_mutex
);
361 EXPORT_SYMBOL(backlight_device_register
);
363 bool backlight_device_registered(enum backlight_type type
)
366 struct backlight_device
*bd
;
368 mutex_lock(&backlight_dev_list_mutex
);
369 list_for_each_entry(bd
, &backlight_dev_list
, entry
) {
370 if (bd
->props
.type
== type
) {
375 mutex_unlock(&backlight_dev_list_mutex
);
379 EXPORT_SYMBOL(backlight_device_registered
);
382 * backlight_device_unregister - unregisters a backlight device object.
383 * @bd: the backlight device object to be unregistered and freed.
385 * Unregisters a previously registered via backlight_device_register object.
387 void backlight_device_unregister(struct backlight_device
*bd
)
392 mutex_lock(&backlight_dev_list_mutex
);
393 list_del(&bd
->entry
);
394 mutex_unlock(&backlight_dev_list_mutex
);
396 #ifdef CONFIG_PMAC_BACKLIGHT
397 mutex_lock(&pmac_backlight_mutex
);
398 if (pmac_backlight
== bd
)
399 pmac_backlight
= NULL
;
400 mutex_unlock(&pmac_backlight_mutex
);
402 mutex_lock(&bd
->ops_lock
);
404 mutex_unlock(&bd
->ops_lock
);
406 backlight_unregister_fb(bd
);
407 device_unregister(&bd
->dev
);
409 EXPORT_SYMBOL(backlight_device_unregister
);
411 static void devm_backlight_device_release(struct device
*dev
, void *res
)
413 struct backlight_device
*backlight
= *(struct backlight_device
**)res
;
415 backlight_device_unregister(backlight
);
418 static int devm_backlight_device_match(struct device
*dev
, void *res
,
421 struct backlight_device
**r
= res
;
427 * devm_backlight_device_register - resource managed backlight_device_register()
428 * @dev: the device to register
429 * @name: the name of the device
430 * @parent: a pointer to the parent device
431 * @devdata: an optional pointer to be stored for private driver use
432 * @ops: the backlight operations structure
433 * @props: the backlight properties
435 * @return a struct backlight on success, or an ERR_PTR on error
437 * Managed backlight_device_register(). The backlight_device returned
438 * from this function are automatically freed on driver detach.
439 * See backlight_device_register() for more information.
441 struct backlight_device
*devm_backlight_device_register(struct device
*dev
,
442 const char *name
, struct device
*parent
, void *devdata
,
443 const struct backlight_ops
*ops
,
444 const struct backlight_properties
*props
)
446 struct backlight_device
**ptr
, *backlight
;
448 ptr
= devres_alloc(devm_backlight_device_release
, sizeof(*ptr
),
451 return ERR_PTR(-ENOMEM
);
453 backlight
= backlight_device_register(name
, parent
, devdata
, ops
,
455 if (!IS_ERR(backlight
)) {
457 devres_add(dev
, ptr
);
464 EXPORT_SYMBOL(devm_backlight_device_register
);
467 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
468 * @dev: the device to unregister
469 * @bd: the backlight device to unregister
471 * Deallocated a backlight allocated with devm_backlight_device_register().
472 * Normally this function will not need to be called and the resource management
473 * code will ensure that the resource is freed.
475 void devm_backlight_device_unregister(struct device
*dev
,
476 struct backlight_device
*bd
)
480 rc
= devres_release(dev
, devm_backlight_device_release
,
481 devm_backlight_device_match
, bd
);
484 EXPORT_SYMBOL(devm_backlight_device_unregister
);
487 static int of_parent_match(struct device
*dev
, const void *data
)
489 return dev
->parent
&& dev
->parent
->of_node
== data
;
493 * of_find_backlight_by_node() - find backlight device by device-tree node
494 * @node: device-tree node of the backlight device
496 * Returns a pointer to the backlight device corresponding to the given DT
497 * node or NULL if no such backlight device exists or if the device hasn't
500 * This function obtains a reference on the backlight device and it is the
501 * caller's responsibility to drop the reference by calling put_device() on
502 * the backlight device's .dev field.
504 struct backlight_device
*of_find_backlight_by_node(struct device_node
*node
)
508 dev
= class_find_device(backlight_class
, NULL
, node
, of_parent_match
);
510 return dev
? to_backlight_device(dev
) : NULL
;
512 EXPORT_SYMBOL(of_find_backlight_by_node
);
515 static void __exit
backlight_class_exit(void)
517 class_destroy(backlight_class
);
520 static int __init
backlight_class_init(void)
522 backlight_class
= class_create(THIS_MODULE
, "backlight");
523 if (IS_ERR(backlight_class
)) {
524 pr_warn("Unable to create backlight class; errno = %ld\n",
525 PTR_ERR(backlight_class
));
526 return PTR_ERR(backlight_class
);
529 backlight_class
->dev_groups
= bl_device_groups
;
530 backlight_class
->pm
= &backlight_class_dev_pm_ops
;
531 INIT_LIST_HEAD(&backlight_dev_list
);
532 mutex_init(&backlight_dev_list_mutex
);
537 * if this is compiled into the kernel, we need to ensure that the
538 * class is registered before users of the class try to register lcd's
540 postcore_initcall(backlight_class_init
);
541 module_exit(backlight_class_exit
);
543 MODULE_LICENSE("GPL");
544 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
545 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");