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;
26 static struct blocking_notifier_head backlight_notifier;
28 static const char *const backlight_types[] = {
29 [BACKLIGHT_RAW] = "raw",
30 [BACKLIGHT_PLATFORM] = "platform",
31 [BACKLIGHT_FIRMWARE] = "firmware",
34 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
35 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
36 /* This callback gets called when something important happens inside a
37 * framebuffer driver. We're looking if that important event is blanking,
38 * and if it is and necessary, we're switching backlight power as well ...
40 static int fb_notifier_callback(struct notifier_block *self,
41 unsigned long event, void *data)
43 struct backlight_device *bd;
44 struct fb_event *evdata = data;
45 int node = evdata->info->node;
48 /* If we aren't interested in this event, skip it immediately ... */
49 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK &&
50 event != FB_EARLY_EVENT_BLANK)
53 bd = container_of(self, struct backlight_device, fb_notif);
54 mutex_lock(&bd->ops_lock);
56 if (!bd->ops->check_fb ||
57 bd->ops->check_fb(bd, evdata->info)) {
58 fb_blank = *(int *)evdata->data;
59 if (fb_blank == FB_BLANK_UNBLANK &&
60 !bd->fb_bl_on[node] &&
61 event != FB_EARLY_EVENT_BLANK) {
62 bd->fb_bl_on[node] = true;
63 if (!bd->use_count++) {
64 bd->props.state &= ~BL_CORE_FBBLANK;
65 bd->props.fb_blank = FB_BLANK_UNBLANK;
66 backlight_update_status(bd);
68 } else if (fb_blank != FB_BLANK_UNBLANK &&
70 event == FB_EARLY_EVENT_BLANK) {
71 bd->fb_bl_on[node] = false;
72 if (!(--bd->use_count)) {
73 bd->props.state |= BL_CORE_FBBLANK;
74 bd->props.fb_blank = fb_blank;
75 backlight_update_status(bd);
79 mutex_unlock(&bd->ops_lock);
83 static int backlight_register_fb(struct backlight_device *bd)
85 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
86 bd->fb_notif.notifier_call = fb_notifier_callback;
88 return fb_register_client(&bd->fb_notif);
91 static void backlight_unregister_fb(struct backlight_device *bd)
93 fb_unregister_client(&bd->fb_notif);
96 static inline int backlight_register_fb(struct backlight_device *bd)
101 static inline void backlight_unregister_fb(struct backlight_device *bd)
104 #endif /* CONFIG_FB */
106 static void backlight_generate_event(struct backlight_device *bd,
107 enum backlight_update_reason reason)
112 case BACKLIGHT_UPDATE_SYSFS:
113 envp[0] = "SOURCE=sysfs";
115 case BACKLIGHT_UPDATE_HOTKEY:
116 envp[0] = "SOURCE=hotkey";
119 envp[0] = "SOURCE=unknown";
123 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
124 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
127 static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
130 struct backlight_device *bd = to_backlight_device(dev);
132 return sprintf(buf, "%d\n", bd->props.power);
135 static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
136 const char *buf, size_t count)
139 struct backlight_device *bd = to_backlight_device(dev);
142 rc = kstrtoul(buf, 0, &power);
147 mutex_lock(&bd->ops_lock);
149 pr_debug("set power to %lu\n", power);
150 if (bd->props.power != power) {
151 bd->props.power = power;
152 backlight_update_status(bd);
156 mutex_unlock(&bd->ops_lock);
160 static DEVICE_ATTR_RW(bl_power);
162 static ssize_t brightness_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
165 struct backlight_device *bd = to_backlight_device(dev);
167 return sprintf(buf, "%d\n", bd->props.brightness);
170 static ssize_t brightness_store(struct device *dev,
171 struct device_attribute *attr, const char *buf, size_t count)
174 struct backlight_device *bd = to_backlight_device(dev);
175 unsigned long brightness;
177 rc = kstrtoul(buf, 0, &brightness);
183 mutex_lock(&bd->ops_lock);
185 if (brightness > bd->props.max_brightness)
188 pr_debug("set brightness to %lu\n", brightness);
189 bd->props.brightness = brightness;
190 backlight_update_status(bd);
194 mutex_unlock(&bd->ops_lock);
196 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
200 static DEVICE_ATTR_RW(brightness);
202 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
205 struct backlight_device *bd = to_backlight_device(dev);
207 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
209 static DEVICE_ATTR_RO(type);
211 static ssize_t max_brightness_show(struct device *dev,
212 struct device_attribute *attr, char *buf)
214 struct backlight_device *bd = to_backlight_device(dev);
216 return sprintf(buf, "%d\n", bd->props.max_brightness);
218 static DEVICE_ATTR_RO(max_brightness);
220 static ssize_t actual_brightness_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
224 struct backlight_device *bd = to_backlight_device(dev);
226 mutex_lock(&bd->ops_lock);
227 if (bd->ops && bd->ops->get_brightness)
228 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
230 rc = sprintf(buf, "%d\n", bd->props.brightness);
231 mutex_unlock(&bd->ops_lock);
235 static DEVICE_ATTR_RO(actual_brightness);
237 static struct class *backlight_class;
239 #ifdef CONFIG_PM_SLEEP
240 static int backlight_suspend(struct device *dev)
242 struct backlight_device *bd = to_backlight_device(dev);
244 mutex_lock(&bd->ops_lock);
245 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
246 bd->props.state |= BL_CORE_SUSPENDED;
247 backlight_update_status(bd);
249 mutex_unlock(&bd->ops_lock);
254 static int backlight_resume(struct device *dev)
256 struct backlight_device *bd = to_backlight_device(dev);
258 mutex_lock(&bd->ops_lock);
259 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
260 bd->props.state &= ~BL_CORE_SUSPENDED;
261 backlight_update_status(bd);
263 mutex_unlock(&bd->ops_lock);
269 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
272 static void bl_device_release(struct device *dev)
274 struct backlight_device *bd = to_backlight_device(dev);
278 static struct attribute *bl_device_attrs[] = {
279 &dev_attr_bl_power.attr,
280 &dev_attr_brightness.attr,
281 &dev_attr_actual_brightness.attr,
282 &dev_attr_max_brightness.attr,
286 ATTRIBUTE_GROUPS(bl_device);
289 * backlight_force_update - tell the backlight subsystem that hardware state
291 * @bd: the backlight device to update
293 * Updates the internal state of the backlight in response to a hardware event,
294 * and generate a uevent to notify userspace
296 void backlight_force_update(struct backlight_device *bd,
297 enum backlight_update_reason reason)
299 mutex_lock(&bd->ops_lock);
300 if (bd->ops && bd->ops->get_brightness)
301 bd->props.brightness = bd->ops->get_brightness(bd);
302 mutex_unlock(&bd->ops_lock);
303 backlight_generate_event(bd, reason);
305 EXPORT_SYMBOL(backlight_force_update);
308 * backlight_device_register - create and register a new object of
309 * backlight_device class.
310 * @name: the name of the new object(must be the same as the name of the
311 * respective framebuffer device).
312 * @parent: a pointer to the parent device
313 * @devdata: an optional pointer to be stored for private driver use. The
314 * methods may retrieve it by using bl_get_data(bd).
315 * @ops: the backlight operations structure.
317 * Creates and registers new backlight device. Returns either an
318 * ERR_PTR() or a pointer to the newly allocated device.
320 struct backlight_device *backlight_device_register(const char *name,
321 struct device *parent, void *devdata, const struct backlight_ops *ops,
322 const struct backlight_properties *props)
324 struct backlight_device *new_bd;
327 pr_debug("backlight_device_register: name=%s\n", name);
329 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
331 return ERR_PTR(-ENOMEM);
333 mutex_init(&new_bd->update_lock);
334 mutex_init(&new_bd->ops_lock);
336 new_bd->dev.class = backlight_class;
337 new_bd->dev.parent = parent;
338 new_bd->dev.release = bl_device_release;
339 dev_set_name(&new_bd->dev, "%s", name);
340 dev_set_drvdata(&new_bd->dev, devdata);
342 /* Set default properties */
344 memcpy(&new_bd->props, props,
345 sizeof(struct backlight_properties));
346 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
347 WARN(1, "%s: invalid backlight type", name);
348 new_bd->props.type = BACKLIGHT_RAW;
351 new_bd->props.type = BACKLIGHT_RAW;
354 rc = device_register(&new_bd->dev);
356 put_device(&new_bd->dev);
360 rc = backlight_register_fb(new_bd);
362 device_unregister(&new_bd->dev);
368 #ifdef CONFIG_PMAC_BACKLIGHT
369 mutex_lock(&pmac_backlight_mutex);
371 pmac_backlight = new_bd;
372 mutex_unlock(&pmac_backlight_mutex);
375 mutex_lock(&backlight_dev_list_mutex);
376 list_add(&new_bd->entry, &backlight_dev_list);
377 mutex_unlock(&backlight_dev_list_mutex);
379 blocking_notifier_call_chain(&backlight_notifier,
380 BACKLIGHT_REGISTERED, new_bd);
384 EXPORT_SYMBOL(backlight_device_register);
386 bool backlight_device_registered(enum backlight_type type)
389 struct backlight_device *bd;
391 mutex_lock(&backlight_dev_list_mutex);
392 list_for_each_entry(bd, &backlight_dev_list, entry) {
393 if (bd->props.type == type) {
398 mutex_unlock(&backlight_dev_list_mutex);
402 EXPORT_SYMBOL(backlight_device_registered);
405 * backlight_device_unregister - unregisters a backlight device object.
406 * @bd: the backlight device object to be unregistered and freed.
408 * Unregisters a previously registered via backlight_device_register object.
410 void backlight_device_unregister(struct backlight_device *bd)
415 mutex_lock(&backlight_dev_list_mutex);
416 list_del(&bd->entry);
417 mutex_unlock(&backlight_dev_list_mutex);
419 #ifdef CONFIG_PMAC_BACKLIGHT
420 mutex_lock(&pmac_backlight_mutex);
421 if (pmac_backlight == bd)
422 pmac_backlight = NULL;
423 mutex_unlock(&pmac_backlight_mutex);
426 blocking_notifier_call_chain(&backlight_notifier,
427 BACKLIGHT_UNREGISTERED, bd);
429 mutex_lock(&bd->ops_lock);
431 mutex_unlock(&bd->ops_lock);
433 backlight_unregister_fb(bd);
434 device_unregister(&bd->dev);
436 EXPORT_SYMBOL(backlight_device_unregister);
438 static void devm_backlight_device_release(struct device *dev, void *res)
440 struct backlight_device *backlight = *(struct backlight_device **)res;
442 backlight_device_unregister(backlight);
445 static int devm_backlight_device_match(struct device *dev, void *res,
448 struct backlight_device **r = res;
454 * backlight_register_notifier - get notified of backlight (un)registration
455 * @nb: notifier block with the notifier to call on backlight (un)registration
457 * @return 0 on success, otherwise a negative error code
459 * Register a notifier to get notified when backlight devices get registered
462 int backlight_register_notifier(struct notifier_block *nb)
464 return blocking_notifier_chain_register(&backlight_notifier, nb);
466 EXPORT_SYMBOL(backlight_register_notifier);
469 * backlight_unregister_notifier - unregister a backlight notifier
470 * @nb: notifier block to unregister
472 * @return 0 on success, otherwise a negative error code
474 * Register a notifier to get notified when backlight devices get registered
477 int backlight_unregister_notifier(struct notifier_block *nb)
479 return blocking_notifier_chain_unregister(&backlight_notifier, nb);
481 EXPORT_SYMBOL(backlight_unregister_notifier);
484 * devm_backlight_device_register - resource managed backlight_device_register()
485 * @dev: the device to register
486 * @name: the name of the device
487 * @parent: a pointer to the parent device
488 * @devdata: an optional pointer to be stored for private driver use
489 * @ops: the backlight operations structure
490 * @props: the backlight properties
492 * @return a struct backlight on success, or an ERR_PTR on error
494 * Managed backlight_device_register(). The backlight_device returned
495 * from this function are automatically freed on driver detach.
496 * See backlight_device_register() for more information.
498 struct backlight_device *devm_backlight_device_register(struct device *dev,
499 const char *name, struct device *parent, void *devdata,
500 const struct backlight_ops *ops,
501 const struct backlight_properties *props)
503 struct backlight_device **ptr, *backlight;
505 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
508 return ERR_PTR(-ENOMEM);
510 backlight = backlight_device_register(name, parent, devdata, ops,
512 if (!IS_ERR(backlight)) {
514 devres_add(dev, ptr);
521 EXPORT_SYMBOL(devm_backlight_device_register);
524 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
525 * @dev: the device to unregister
526 * @bd: the backlight device to unregister
528 * Deallocated a backlight allocated with devm_backlight_device_register().
529 * Normally this function will not need to be called and the resource management
530 * code will ensure that the resource is freed.
532 void devm_backlight_device_unregister(struct device *dev,
533 struct backlight_device *bd)
537 rc = devres_release(dev, devm_backlight_device_release,
538 devm_backlight_device_match, bd);
541 EXPORT_SYMBOL(devm_backlight_device_unregister);
544 static int of_parent_match(struct device *dev, const void *data)
546 return dev->parent && dev->parent->of_node == data;
550 * of_find_backlight_by_node() - find backlight device by device-tree node
551 * @node: device-tree node of the backlight device
553 * Returns a pointer to the backlight device corresponding to the given DT
554 * node or NULL if no such backlight device exists or if the device hasn't
557 * This function obtains a reference on the backlight device and it is the
558 * caller's responsibility to drop the reference by calling put_device() on
559 * the backlight device's .dev field.
561 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
565 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
567 return dev ? to_backlight_device(dev) : NULL;
569 EXPORT_SYMBOL(of_find_backlight_by_node);
572 static void __exit backlight_class_exit(void)
574 class_destroy(backlight_class);
577 static int __init backlight_class_init(void)
579 backlight_class = class_create(THIS_MODULE, "backlight");
580 if (IS_ERR(backlight_class)) {
581 pr_warn("Unable to create backlight class; errno = %ld\n",
582 PTR_ERR(backlight_class));
583 return PTR_ERR(backlight_class);
586 backlight_class->dev_groups = bl_device_groups;
587 backlight_class->pm = &backlight_class_dev_pm_ops;
588 INIT_LIST_HEAD(&backlight_dev_list);
589 mutex_init(&backlight_dev_list_mutex);
590 BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier);
596 * if this is compiled into the kernel, we need to ensure that the
597 * class is registered before users of the class try to register lcd's
599 postcore_initcall(backlight_class_init);
600 module_exit(backlight_class_exit);
602 MODULE_LICENSE("GPL");
603 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
604 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");