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 const char *const backlight_types
[] = {
25 [BACKLIGHT_RAW
] = "raw",
26 [BACKLIGHT_PLATFORM
] = "platform",
27 [BACKLIGHT_FIRMWARE
] = "firmware",
30 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
31 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
32 /* This callback gets called when something important happens inside a
33 * framebuffer driver. We're looking if that important event is blanking,
34 * and if it is, we're switching backlight power as well ...
36 static int fb_notifier_callback(struct notifier_block
*self
,
37 unsigned long event
, void *data
)
39 struct backlight_device
*bd
;
40 struct fb_event
*evdata
= data
;
42 /* If we aren't interested in this event, skip it immediately ... */
43 if (event
!= FB_EVENT_BLANK
&& event
!= FB_EVENT_CONBLANK
)
46 bd
= container_of(self
, struct backlight_device
, fb_notif
);
47 mutex_lock(&bd
->ops_lock
);
49 if (!bd
->ops
->check_fb
||
50 bd
->ops
->check_fb(bd
, evdata
->info
)) {
51 bd
->props
.fb_blank
= *(int *)evdata
->data
;
52 if (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
)
53 bd
->props
.state
&= ~BL_CORE_FBBLANK
;
55 bd
->props
.state
|= BL_CORE_FBBLANK
;
56 backlight_update_status(bd
);
58 mutex_unlock(&bd
->ops_lock
);
62 static int backlight_register_fb(struct backlight_device
*bd
)
64 memset(&bd
->fb_notif
, 0, sizeof(bd
->fb_notif
));
65 bd
->fb_notif
.notifier_call
= fb_notifier_callback
;
67 return fb_register_client(&bd
->fb_notif
);
70 static void backlight_unregister_fb(struct backlight_device
*bd
)
72 fb_unregister_client(&bd
->fb_notif
);
75 static inline int backlight_register_fb(struct backlight_device
*bd
)
80 static inline void backlight_unregister_fb(struct backlight_device
*bd
)
83 #endif /* CONFIG_FB */
85 static void backlight_generate_event(struct backlight_device
*bd
,
86 enum backlight_update_reason reason
)
91 case BACKLIGHT_UPDATE_SYSFS
:
92 envp
[0] = "SOURCE=sysfs";
94 case BACKLIGHT_UPDATE_HOTKEY
:
95 envp
[0] = "SOURCE=hotkey";
98 envp
[0] = "SOURCE=unknown";
102 kobject_uevent_env(&bd
->dev
.kobj
, KOBJ_CHANGE
, envp
);
103 sysfs_notify(&bd
->dev
.kobj
, NULL
, "actual_brightness");
106 static ssize_t
bl_power_show(struct device
*dev
, struct device_attribute
*attr
,
109 struct backlight_device
*bd
= to_backlight_device(dev
);
111 return sprintf(buf
, "%d\n", bd
->props
.power
);
114 static ssize_t
bl_power_store(struct device
*dev
, struct device_attribute
*attr
,
115 const char *buf
, size_t count
)
118 struct backlight_device
*bd
= to_backlight_device(dev
);
121 rc
= kstrtoul(buf
, 0, &power
);
126 mutex_lock(&bd
->ops_lock
);
128 pr_debug("set power to %lu\n", power
);
129 if (bd
->props
.power
!= power
) {
130 bd
->props
.power
= power
;
131 backlight_update_status(bd
);
135 mutex_unlock(&bd
->ops_lock
);
139 static DEVICE_ATTR_RW(bl_power
);
141 static ssize_t
brightness_show(struct device
*dev
,
142 struct device_attribute
*attr
, char *buf
)
144 struct backlight_device
*bd
= to_backlight_device(dev
);
146 return sprintf(buf
, "%d\n", bd
->props
.brightness
);
149 static ssize_t
brightness_store(struct device
*dev
,
150 struct device_attribute
*attr
, const char *buf
, size_t count
)
153 struct backlight_device
*bd
= to_backlight_device(dev
);
154 unsigned long brightness
;
156 rc
= kstrtoul(buf
, 0, &brightness
);
162 mutex_lock(&bd
->ops_lock
);
164 if (brightness
> bd
->props
.max_brightness
)
167 pr_debug("set brightness to %lu\n", brightness
);
168 bd
->props
.brightness
= brightness
;
169 backlight_update_status(bd
);
173 mutex_unlock(&bd
->ops_lock
);
175 backlight_generate_event(bd
, BACKLIGHT_UPDATE_SYSFS
);
179 static DEVICE_ATTR_RW(brightness
);
181 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
184 struct backlight_device
*bd
= to_backlight_device(dev
);
186 return sprintf(buf
, "%s\n", backlight_types
[bd
->props
.type
]);
188 static DEVICE_ATTR_RO(type
);
190 static ssize_t
max_brightness_show(struct device
*dev
,
191 struct device_attribute
*attr
, char *buf
)
193 struct backlight_device
*bd
= to_backlight_device(dev
);
195 return sprintf(buf
, "%d\n", bd
->props
.max_brightness
);
197 static DEVICE_ATTR_RO(max_brightness
);
199 static ssize_t
actual_brightness_show(struct device
*dev
,
200 struct device_attribute
*attr
, char *buf
)
203 struct backlight_device
*bd
= to_backlight_device(dev
);
205 mutex_lock(&bd
->ops_lock
);
206 if (bd
->ops
&& bd
->ops
->get_brightness
)
207 rc
= sprintf(buf
, "%d\n", bd
->ops
->get_brightness(bd
));
208 mutex_unlock(&bd
->ops_lock
);
212 static DEVICE_ATTR_RO(actual_brightness
);
214 static struct class *backlight_class
;
216 #ifdef CONFIG_PM_SLEEP
217 static int backlight_suspend(struct device
*dev
)
219 struct backlight_device
*bd
= to_backlight_device(dev
);
221 mutex_lock(&bd
->ops_lock
);
222 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
223 bd
->props
.state
|= BL_CORE_SUSPENDED
;
224 backlight_update_status(bd
);
226 mutex_unlock(&bd
->ops_lock
);
231 static int backlight_resume(struct device
*dev
)
233 struct backlight_device
*bd
= to_backlight_device(dev
);
235 mutex_lock(&bd
->ops_lock
);
236 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
237 bd
->props
.state
&= ~BL_CORE_SUSPENDED
;
238 backlight_update_status(bd
);
240 mutex_unlock(&bd
->ops_lock
);
246 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops
, backlight_suspend
,
249 static void bl_device_release(struct device
*dev
)
251 struct backlight_device
*bd
= to_backlight_device(dev
);
255 static struct attribute
*bl_device_attrs
[] = {
256 &dev_attr_bl_power
.attr
,
257 &dev_attr_brightness
.attr
,
258 &dev_attr_actual_brightness
.attr
,
259 &dev_attr_max_brightness
.attr
,
263 ATTRIBUTE_GROUPS(bl_device
);
266 * backlight_force_update - tell the backlight subsystem that hardware state
268 * @bd: the backlight device to update
270 * Updates the internal state of the backlight in response to a hardware event,
271 * and generate a uevent to notify userspace
273 void backlight_force_update(struct backlight_device
*bd
,
274 enum backlight_update_reason reason
)
276 mutex_lock(&bd
->ops_lock
);
277 if (bd
->ops
&& bd
->ops
->get_brightness
)
278 bd
->props
.brightness
= bd
->ops
->get_brightness(bd
);
279 mutex_unlock(&bd
->ops_lock
);
280 backlight_generate_event(bd
, reason
);
282 EXPORT_SYMBOL(backlight_force_update
);
285 * backlight_device_register - create and register a new object of
286 * backlight_device class.
287 * @name: the name of the new object(must be the same as the name of the
288 * respective framebuffer device).
289 * @parent: a pointer to the parent device
290 * @devdata: an optional pointer to be stored for private driver use. The
291 * methods may retrieve it by using bl_get_data(bd).
292 * @ops: the backlight operations structure.
294 * Creates and registers new backlight device. Returns either an
295 * ERR_PTR() or a pointer to the newly allocated device.
297 struct backlight_device
*backlight_device_register(const char *name
,
298 struct device
*parent
, void *devdata
, const struct backlight_ops
*ops
,
299 const struct backlight_properties
*props
)
301 struct backlight_device
*new_bd
;
304 pr_debug("backlight_device_register: name=%s\n", name
);
306 new_bd
= kzalloc(sizeof(struct backlight_device
), GFP_KERNEL
);
308 return ERR_PTR(-ENOMEM
);
310 mutex_init(&new_bd
->update_lock
);
311 mutex_init(&new_bd
->ops_lock
);
313 new_bd
->dev
.class = backlight_class
;
314 new_bd
->dev
.parent
= parent
;
315 new_bd
->dev
.release
= bl_device_release
;
316 dev_set_name(&new_bd
->dev
, "%s", name
);
317 dev_set_drvdata(&new_bd
->dev
, devdata
);
319 /* Set default properties */
321 memcpy(&new_bd
->props
, props
,
322 sizeof(struct backlight_properties
));
323 if (props
->type
<= 0 || props
->type
>= BACKLIGHT_TYPE_MAX
) {
324 WARN(1, "%s: invalid backlight type", name
);
325 new_bd
->props
.type
= BACKLIGHT_RAW
;
328 new_bd
->props
.type
= BACKLIGHT_RAW
;
331 rc
= device_register(&new_bd
->dev
);
337 rc
= backlight_register_fb(new_bd
);
339 device_unregister(&new_bd
->dev
);
345 #ifdef CONFIG_PMAC_BACKLIGHT
346 mutex_lock(&pmac_backlight_mutex
);
348 pmac_backlight
= new_bd
;
349 mutex_unlock(&pmac_backlight_mutex
);
354 EXPORT_SYMBOL(backlight_device_register
);
357 * backlight_device_unregister - unregisters a backlight device object.
358 * @bd: the backlight device object to be unregistered and freed.
360 * Unregisters a previously registered via backlight_device_register object.
362 void backlight_device_unregister(struct backlight_device
*bd
)
367 #ifdef CONFIG_PMAC_BACKLIGHT
368 mutex_lock(&pmac_backlight_mutex
);
369 if (pmac_backlight
== bd
)
370 pmac_backlight
= NULL
;
371 mutex_unlock(&pmac_backlight_mutex
);
373 mutex_lock(&bd
->ops_lock
);
375 mutex_unlock(&bd
->ops_lock
);
377 backlight_unregister_fb(bd
);
378 device_unregister(&bd
->dev
);
380 EXPORT_SYMBOL(backlight_device_unregister
);
382 static void devm_backlight_device_release(struct device
*dev
, void *res
)
384 struct backlight_device
*backlight
= *(struct backlight_device
**)res
;
386 backlight_device_unregister(backlight
);
389 static int devm_backlight_device_match(struct device
*dev
, void *res
,
392 struct backlight_device
**r
= res
;
398 * devm_backlight_device_register - resource managed backlight_device_register()
399 * @dev: the device to register
400 * @name: the name of the device
401 * @parent: a pointer to the parent device
402 * @devdata: an optional pointer to be stored for private driver use
403 * @ops: the backlight operations structure
404 * @props: the backlight properties
406 * @return a struct backlight on success, or an ERR_PTR on error
408 * Managed backlight_device_register(). The backlight_device returned
409 * from this function are automatically freed on driver detach.
410 * See backlight_device_register() for more information.
412 struct backlight_device
*devm_backlight_device_register(struct device
*dev
,
413 const char *name
, struct device
*parent
, void *devdata
,
414 const struct backlight_ops
*ops
,
415 const struct backlight_properties
*props
)
417 struct backlight_device
**ptr
, *backlight
;
419 ptr
= devres_alloc(devm_backlight_device_release
, sizeof(*ptr
),
422 return ERR_PTR(-ENOMEM
);
424 backlight
= backlight_device_register(name
, parent
, devdata
, ops
,
426 if (!IS_ERR(backlight
)) {
428 devres_add(dev
, ptr
);
435 EXPORT_SYMBOL(devm_backlight_device_register
);
438 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
439 * @dev: the device to unregister
440 * @bd: the backlight device to unregister
442 * Deallocated a backlight allocated with devm_backlight_device_register().
443 * Normally this function will not need to be called and the resource management
444 * code will ensure that the resource is freed.
446 void devm_backlight_device_unregister(struct device
*dev
,
447 struct backlight_device
*bd
)
451 rc
= devres_release(dev
, devm_backlight_device_release
,
452 devm_backlight_device_match
, bd
);
455 EXPORT_SYMBOL(devm_backlight_device_unregister
);
458 static int of_parent_match(struct device
*dev
, const void *data
)
460 return dev
->parent
&& dev
->parent
->of_node
== data
;
464 * of_find_backlight_by_node() - find backlight device by device-tree node
465 * @node: device-tree node of the backlight device
467 * Returns a pointer to the backlight device corresponding to the given DT
468 * node or NULL if no such backlight device exists or if the device hasn't
471 * This function obtains a reference on the backlight device and it is the
472 * caller's responsibility to drop the reference by calling put_device() on
473 * the backlight device's .dev field.
475 struct backlight_device
*of_find_backlight_by_node(struct device_node
*node
)
479 dev
= class_find_device(backlight_class
, NULL
, node
, of_parent_match
);
481 return dev
? to_backlight_device(dev
) : NULL
;
483 EXPORT_SYMBOL(of_find_backlight_by_node
);
486 static void __exit
backlight_class_exit(void)
488 class_destroy(backlight_class
);
491 static int __init
backlight_class_init(void)
493 backlight_class
= class_create(THIS_MODULE
, "backlight");
494 if (IS_ERR(backlight_class
)) {
495 pr_warn("Unable to create backlight class; errno = %ld\n",
496 PTR_ERR(backlight_class
));
497 return PTR_ERR(backlight_class
);
500 backlight_class
->dev_groups
= bl_device_groups
;
501 backlight_class
->pm
= &backlight_class_dev_pm_ops
;
506 * if this is compiled into the kernel, we need to ensure that the
507 * class is registered before users of the class try to register lcd's
509 postcore_initcall(backlight_class_init
);
510 module_exit(backlight_class_exit
);
512 MODULE_LICENSE("GPL");
513 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
514 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");