2 * Backlight Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/device.h>
11 #include <linux/backlight.h>
12 #include <linux/notifier.h>
13 #include <linux/ctype.h>
14 #include <linux/err.h>
16 #include <linux/slab.h>
18 #ifdef CONFIG_PMAC_BACKLIGHT
19 #include <asm/backlight.h>
22 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
23 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
24 /* This callback gets called when something important happens inside a
25 * framebuffer driver. We're looking if that important event is blanking,
26 * and if it is, we're switching backlight power as well ...
28 static int fb_notifier_callback(struct notifier_block
*self
,
29 unsigned long event
, void *data
)
31 struct backlight_device
*bd
;
32 struct fb_event
*evdata
= data
;
34 /* If we aren't interested in this event, skip it immediately ... */
35 if (event
!= FB_EVENT_BLANK
&& event
!= FB_EVENT_CONBLANK
)
38 bd
= container_of(self
, struct backlight_device
, fb_notif
);
39 mutex_lock(&bd
->ops_lock
);
41 if (!bd
->ops
->check_fb
||
42 bd
->ops
->check_fb(bd
, evdata
->info
)) {
43 bd
->props
.fb_blank
= *(int *)evdata
->data
;
44 if (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
)
45 bd
->props
.state
&= ~BL_CORE_FBBLANK
;
47 bd
->props
.state
|= BL_CORE_FBBLANK
;
48 backlight_update_status(bd
);
50 mutex_unlock(&bd
->ops_lock
);
54 static int backlight_register_fb(struct backlight_device
*bd
)
56 memset(&bd
->fb_notif
, 0, sizeof(bd
->fb_notif
));
57 bd
->fb_notif
.notifier_call
= fb_notifier_callback
;
59 return fb_register_client(&bd
->fb_notif
);
62 static void backlight_unregister_fb(struct backlight_device
*bd
)
64 fb_unregister_client(&bd
->fb_notif
);
67 static inline int backlight_register_fb(struct backlight_device
*bd
)
72 static inline void backlight_unregister_fb(struct backlight_device
*bd
)
75 #endif /* CONFIG_FB */
77 static void backlight_generate_event(struct backlight_device
*bd
,
78 enum backlight_update_reason reason
)
83 case BACKLIGHT_UPDATE_SYSFS
:
84 envp
[0] = "SOURCE=sysfs";
86 case BACKLIGHT_UPDATE_HOTKEY
:
87 envp
[0] = "SOURCE=hotkey";
90 envp
[0] = "SOURCE=unknown";
94 kobject_uevent_env(&bd
->dev
.kobj
, KOBJ_CHANGE
, envp
);
95 sysfs_notify(&bd
->dev
.kobj
, NULL
, "actual_brightness");
98 static ssize_t
backlight_show_power(struct device
*dev
,
99 struct device_attribute
*attr
,char *buf
)
101 struct backlight_device
*bd
= to_backlight_device(dev
);
103 return sprintf(buf
, "%d\n", bd
->props
.power
);
106 static ssize_t
backlight_store_power(struct device
*dev
,
107 struct device_attribute
*attr
, const char *buf
, size_t count
)
110 struct backlight_device
*bd
= to_backlight_device(dev
);
113 rc
= strict_strtoul(buf
, 0, &power
);
118 mutex_lock(&bd
->ops_lock
);
120 pr_debug("backlight: set power to %lu\n", power
);
121 if (bd
->props
.power
!= power
) {
122 bd
->props
.power
= power
;
123 backlight_update_status(bd
);
127 mutex_unlock(&bd
->ops_lock
);
132 static ssize_t
backlight_show_brightness(struct device
*dev
,
133 struct device_attribute
*attr
, char *buf
)
135 struct backlight_device
*bd
= to_backlight_device(dev
);
137 return sprintf(buf
, "%d\n", bd
->props
.brightness
);
140 static ssize_t
backlight_store_brightness(struct device
*dev
,
141 struct device_attribute
*attr
, const char *buf
, size_t count
)
144 struct backlight_device
*bd
= to_backlight_device(dev
);
145 unsigned long brightness
;
147 rc
= strict_strtoul(buf
, 0, &brightness
);
153 mutex_lock(&bd
->ops_lock
);
155 if (brightness
> bd
->props
.max_brightness
)
158 pr_debug("backlight: set brightness to %lu\n",
160 bd
->props
.brightness
= brightness
;
161 backlight_update_status(bd
);
165 mutex_unlock(&bd
->ops_lock
);
167 backlight_generate_event(bd
, BACKLIGHT_UPDATE_SYSFS
);
172 static ssize_t
backlight_show_max_brightness(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
.max_brightness
);
180 static ssize_t
backlight_show_actual_brightness(struct device
*dev
,
181 struct device_attribute
*attr
, char *buf
)
184 struct backlight_device
*bd
= to_backlight_device(dev
);
186 mutex_lock(&bd
->ops_lock
);
187 if (bd
->ops
&& bd
->ops
->get_brightness
)
188 rc
= sprintf(buf
, "%d\n", bd
->ops
->get_brightness(bd
));
189 mutex_unlock(&bd
->ops_lock
);
194 static struct class *backlight_class
;
196 static int backlight_suspend(struct device
*dev
, pm_message_t state
)
198 struct backlight_device
*bd
= to_backlight_device(dev
);
200 mutex_lock(&bd
->ops_lock
);
201 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
202 bd
->props
.state
|= BL_CORE_SUSPENDED
;
203 backlight_update_status(bd
);
205 mutex_unlock(&bd
->ops_lock
);
210 static int backlight_resume(struct device
*dev
)
212 struct backlight_device
*bd
= to_backlight_device(dev
);
214 mutex_lock(&bd
->ops_lock
);
215 if (bd
->ops
&& bd
->ops
->options
& BL_CORE_SUSPENDRESUME
) {
216 bd
->props
.state
&= ~BL_CORE_SUSPENDED
;
217 backlight_update_status(bd
);
219 mutex_unlock(&bd
->ops_lock
);
224 static void bl_device_release(struct device
*dev
)
226 struct backlight_device
*bd
= to_backlight_device(dev
);
230 static struct device_attribute bl_device_attributes
[] = {
231 __ATTR(bl_power
, 0644, backlight_show_power
, backlight_store_power
),
232 __ATTR(brightness
, 0644, backlight_show_brightness
,
233 backlight_store_brightness
),
234 __ATTR(actual_brightness
, 0444, backlight_show_actual_brightness
,
236 __ATTR(max_brightness
, 0444, backlight_show_max_brightness
, NULL
),
241 * backlight_force_update - tell the backlight subsystem that hardware state
243 * @bd: the backlight device to update
245 * Updates the internal state of the backlight in response to a hardware event,
246 * and generate a uevent to notify userspace
248 void backlight_force_update(struct backlight_device
*bd
,
249 enum backlight_update_reason reason
)
251 mutex_lock(&bd
->ops_lock
);
252 if (bd
->ops
&& bd
->ops
->get_brightness
)
253 bd
->props
.brightness
= bd
->ops
->get_brightness(bd
);
254 mutex_unlock(&bd
->ops_lock
);
255 backlight_generate_event(bd
, reason
);
257 EXPORT_SYMBOL(backlight_force_update
);
260 * backlight_device_register - create and register a new object of
261 * backlight_device class.
262 * @name: the name of the new object(must be the same as the name of the
263 * respective framebuffer device).
264 * @parent: a pointer to the parent device
265 * @devdata: an optional pointer to be stored for private driver use. The
266 * methods may retrieve it by using bl_get_data(bd).
267 * @ops: the backlight operations structure.
269 * Creates and registers new backlight device. Returns either an
270 * ERR_PTR() or a pointer to the newly allocated device.
272 struct backlight_device
*backlight_device_register(const char *name
,
273 struct device
*parent
, void *devdata
, const struct backlight_ops
*ops
,
274 const struct backlight_properties
*props
)
276 struct backlight_device
*new_bd
;
279 pr_debug("backlight_device_register: name=%s\n", name
);
281 new_bd
= kzalloc(sizeof(struct backlight_device
), GFP_KERNEL
);
283 return ERR_PTR(-ENOMEM
);
285 mutex_init(&new_bd
->update_lock
);
286 mutex_init(&new_bd
->ops_lock
);
288 new_bd
->dev
.class = backlight_class
;
289 new_bd
->dev
.parent
= parent
;
290 new_bd
->dev
.release
= bl_device_release
;
291 dev_set_name(&new_bd
->dev
, name
);
292 dev_set_drvdata(&new_bd
->dev
, devdata
);
294 /* Set default properties */
296 memcpy(&new_bd
->props
, props
,
297 sizeof(struct backlight_properties
));
299 rc
= device_register(&new_bd
->dev
);
305 rc
= backlight_register_fb(new_bd
);
307 device_unregister(&new_bd
->dev
);
313 #ifdef CONFIG_PMAC_BACKLIGHT
314 mutex_lock(&pmac_backlight_mutex
);
316 pmac_backlight
= new_bd
;
317 mutex_unlock(&pmac_backlight_mutex
);
322 EXPORT_SYMBOL(backlight_device_register
);
325 * backlight_device_unregister - unregisters a backlight device object.
326 * @bd: the backlight device object to be unregistered and freed.
328 * Unregisters a previously registered via backlight_device_register object.
330 void backlight_device_unregister(struct backlight_device
*bd
)
335 #ifdef CONFIG_PMAC_BACKLIGHT
336 mutex_lock(&pmac_backlight_mutex
);
337 if (pmac_backlight
== bd
)
338 pmac_backlight
= NULL
;
339 mutex_unlock(&pmac_backlight_mutex
);
341 mutex_lock(&bd
->ops_lock
);
343 mutex_unlock(&bd
->ops_lock
);
345 backlight_unregister_fb(bd
);
346 device_unregister(&bd
->dev
);
348 EXPORT_SYMBOL(backlight_device_unregister
);
350 static void __exit
backlight_class_exit(void)
352 class_destroy(backlight_class
);
355 static int __init
backlight_class_init(void)
357 backlight_class
= class_create(THIS_MODULE
, "backlight");
358 if (IS_ERR(backlight_class
)) {
359 printk(KERN_WARNING
"Unable to create backlight class; errno = %ld\n",
360 PTR_ERR(backlight_class
));
361 return PTR_ERR(backlight_class
);
364 backlight_class
->dev_attrs
= bl_device_attributes
;
365 backlight_class
->suspend
= backlight_suspend
;
366 backlight_class
->resume
= backlight_resume
;
371 * if this is compiled into the kernel, we need to ensure that the
372 * class is registered before users of the class try to register lcd's
374 postcore_initcall(backlight_class_init
);
375 module_exit(backlight_class_exit
);
377 MODULE_LICENSE("GPL");
378 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
379 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");