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 and necessary, 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;
44 int node = evdata->info->node;
47 /* If we aren't interested in this event, skip it immediately ... */
48 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
51 bd = container_of(self, struct backlight_device, fb_notif);
52 mutex_lock(&bd->ops_lock);
54 if (!bd->ops->check_fb ||
55 bd->ops->check_fb(bd, evdata->info)) {
56 fb_blank = *(int *)evdata->data;
57 if (fb_blank == FB_BLANK_UNBLANK &&
58 !bd->fb_bl_on[node]) {
59 bd->fb_bl_on[node] = true;
60 if (!bd->use_count++) {
61 bd->props.state &= ~BL_CORE_FBBLANK;
62 bd->props.fb_blank = FB_BLANK_UNBLANK;
63 backlight_update_status(bd);
65 } else if (fb_blank != FB_BLANK_UNBLANK &&
67 bd->fb_bl_on[node] = false;
68 if (!(--bd->use_count)) {
69 bd->props.state |= BL_CORE_FBBLANK;
70 bd->props.fb_blank = fb_blank;
71 backlight_update_status(bd);
75 mutex_unlock(&bd->ops_lock);
79 static int backlight_register_fb(struct backlight_device *bd)
81 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
82 bd->fb_notif.notifier_call = fb_notifier_callback;
84 return fb_register_client(&bd->fb_notif);
87 static void backlight_unregister_fb(struct backlight_device *bd)
89 fb_unregister_client(&bd->fb_notif);
92 static inline int backlight_register_fb(struct backlight_device *bd)
97 static inline void backlight_unregister_fb(struct backlight_device *bd)
100 #endif /* CONFIG_FB */
102 static void backlight_generate_event(struct backlight_device *bd,
103 enum backlight_update_reason reason)
108 case BACKLIGHT_UPDATE_SYSFS:
109 envp[0] = "SOURCE=sysfs";
111 case BACKLIGHT_UPDATE_HOTKEY:
112 envp[0] = "SOURCE=hotkey";
115 envp[0] = "SOURCE=unknown";
119 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
120 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
123 static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
126 struct backlight_device *bd = to_backlight_device(dev);
128 return sprintf(buf, "%d\n", bd->props.power);
131 static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
132 const char *buf, size_t count)
135 struct backlight_device *bd = to_backlight_device(dev);
138 rc = kstrtoul(buf, 0, &power);
143 mutex_lock(&bd->ops_lock);
145 pr_debug("set power to %lu\n", power);
146 if (bd->props.power != power) {
147 bd->props.power = power;
148 backlight_update_status(bd);
152 mutex_unlock(&bd->ops_lock);
156 static DEVICE_ATTR_RW(bl_power);
158 static ssize_t brightness_show(struct device *dev,
159 struct device_attribute *attr, char *buf)
161 struct backlight_device *bd = to_backlight_device(dev);
163 return sprintf(buf, "%d\n", bd->props.brightness);
166 static ssize_t brightness_store(struct device *dev,
167 struct device_attribute *attr, const char *buf, size_t count)
170 struct backlight_device *bd = to_backlight_device(dev);
171 unsigned long brightness;
173 rc = kstrtoul(buf, 0, &brightness);
179 mutex_lock(&bd->ops_lock);
181 if (brightness > bd->props.max_brightness)
184 pr_debug("set brightness to %lu\n", brightness);
185 bd->props.brightness = brightness;
186 backlight_update_status(bd);
190 mutex_unlock(&bd->ops_lock);
192 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
196 static DEVICE_ATTR_RW(brightness);
198 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
201 struct backlight_device *bd = to_backlight_device(dev);
203 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
205 static DEVICE_ATTR_RO(type);
207 static ssize_t max_brightness_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
210 struct backlight_device *bd = to_backlight_device(dev);
212 return sprintf(buf, "%d\n", bd->props.max_brightness);
214 static DEVICE_ATTR_RO(max_brightness);
216 static ssize_t actual_brightness_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
220 struct backlight_device *bd = to_backlight_device(dev);
222 mutex_lock(&bd->ops_lock);
223 if (bd->ops && bd->ops->get_brightness)
224 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
225 mutex_unlock(&bd->ops_lock);
229 static DEVICE_ATTR_RO(actual_brightness);
231 static struct class *backlight_class;
233 #ifdef CONFIG_PM_SLEEP
234 static int backlight_suspend(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);
248 static int backlight_resume(struct device *dev)
250 struct backlight_device *bd = to_backlight_device(dev);
252 mutex_lock(&bd->ops_lock);
253 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
254 bd->props.state &= ~BL_CORE_SUSPENDED;
255 backlight_update_status(bd);
257 mutex_unlock(&bd->ops_lock);
263 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
266 static void bl_device_release(struct device *dev)
268 struct backlight_device *bd = to_backlight_device(dev);
272 static struct attribute *bl_device_attrs[] = {
273 &dev_attr_bl_power.attr,
274 &dev_attr_brightness.attr,
275 &dev_attr_actual_brightness.attr,
276 &dev_attr_max_brightness.attr,
280 ATTRIBUTE_GROUPS(bl_device);
283 * backlight_force_update - tell the backlight subsystem that hardware state
285 * @bd: the backlight device to update
287 * Updates the internal state of the backlight in response to a hardware event,
288 * and generate a uevent to notify userspace
290 void backlight_force_update(struct backlight_device *bd,
291 enum backlight_update_reason reason)
293 mutex_lock(&bd->ops_lock);
294 if (bd->ops && bd->ops->get_brightness)
295 bd->props.brightness = bd->ops->get_brightness(bd);
296 mutex_unlock(&bd->ops_lock);
297 backlight_generate_event(bd, reason);
299 EXPORT_SYMBOL(backlight_force_update);
302 * backlight_device_register - create and register a new object of
303 * backlight_device class.
304 * @name: the name of the new object(must be the same as the name of the
305 * respective framebuffer device).
306 * @parent: a pointer to the parent device
307 * @devdata: an optional pointer to be stored for private driver use. The
308 * methods may retrieve it by using bl_get_data(bd).
309 * @ops: the backlight operations structure.
311 * Creates and registers new backlight device. Returns either an
312 * ERR_PTR() or a pointer to the newly allocated device.
314 struct backlight_device *backlight_device_register(const char *name,
315 struct device *parent, void *devdata, const struct backlight_ops *ops,
316 const struct backlight_properties *props)
318 struct backlight_device *new_bd;
321 pr_debug("backlight_device_register: name=%s\n", name);
323 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
325 return ERR_PTR(-ENOMEM);
327 mutex_init(&new_bd->update_lock);
328 mutex_init(&new_bd->ops_lock);
330 new_bd->dev.class = backlight_class;
331 new_bd->dev.parent = parent;
332 new_bd->dev.release = bl_device_release;
333 dev_set_name(&new_bd->dev, "%s", name);
334 dev_set_drvdata(&new_bd->dev, devdata);
336 /* Set default properties */
338 memcpy(&new_bd->props, props,
339 sizeof(struct backlight_properties));
340 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
341 WARN(1, "%s: invalid backlight type", name);
342 new_bd->props.type = BACKLIGHT_RAW;
345 new_bd->props.type = BACKLIGHT_RAW;
348 rc = device_register(&new_bd->dev);
354 rc = backlight_register_fb(new_bd);
356 device_unregister(&new_bd->dev);
362 #ifdef CONFIG_PMAC_BACKLIGHT
363 mutex_lock(&pmac_backlight_mutex);
365 pmac_backlight = new_bd;
366 mutex_unlock(&pmac_backlight_mutex);
369 mutex_lock(&backlight_dev_list_mutex);
370 list_add(&new_bd->entry, &backlight_dev_list);
371 mutex_unlock(&backlight_dev_list_mutex);
375 EXPORT_SYMBOL(backlight_device_register);
377 bool backlight_device_registered(enum backlight_type type)
380 struct backlight_device *bd;
382 mutex_lock(&backlight_dev_list_mutex);
383 list_for_each_entry(bd, &backlight_dev_list, entry) {
384 if (bd->props.type == type) {
389 mutex_unlock(&backlight_dev_list_mutex);
393 EXPORT_SYMBOL(backlight_device_registered);
396 * backlight_device_unregister - unregisters a backlight device object.
397 * @bd: the backlight device object to be unregistered and freed.
399 * Unregisters a previously registered via backlight_device_register object.
401 void backlight_device_unregister(struct backlight_device *bd)
406 mutex_lock(&backlight_dev_list_mutex);
407 list_del(&bd->entry);
408 mutex_unlock(&backlight_dev_list_mutex);
410 #ifdef CONFIG_PMAC_BACKLIGHT
411 mutex_lock(&pmac_backlight_mutex);
412 if (pmac_backlight == bd)
413 pmac_backlight = NULL;
414 mutex_unlock(&pmac_backlight_mutex);
416 mutex_lock(&bd->ops_lock);
418 mutex_unlock(&bd->ops_lock);
420 backlight_unregister_fb(bd);
421 device_unregister(&bd->dev);
423 EXPORT_SYMBOL(backlight_device_unregister);
425 static void devm_backlight_device_release(struct device *dev, void *res)
427 struct backlight_device *backlight = *(struct backlight_device **)res;
429 backlight_device_unregister(backlight);
432 static int devm_backlight_device_match(struct device *dev, void *res,
435 struct backlight_device **r = res;
441 * devm_backlight_device_register - resource managed backlight_device_register()
442 * @dev: the device to register
443 * @name: the name of the device
444 * @parent: a pointer to the parent device
445 * @devdata: an optional pointer to be stored for private driver use
446 * @ops: the backlight operations structure
447 * @props: the backlight properties
449 * @return a struct backlight on success, or an ERR_PTR on error
451 * Managed backlight_device_register(). The backlight_device returned
452 * from this function are automatically freed on driver detach.
453 * See backlight_device_register() for more information.
455 struct backlight_device *devm_backlight_device_register(struct device *dev,
456 const char *name, struct device *parent, void *devdata,
457 const struct backlight_ops *ops,
458 const struct backlight_properties *props)
460 struct backlight_device **ptr, *backlight;
462 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
465 return ERR_PTR(-ENOMEM);
467 backlight = backlight_device_register(name, parent, devdata, ops,
469 if (!IS_ERR(backlight)) {
471 devres_add(dev, ptr);
478 EXPORT_SYMBOL(devm_backlight_device_register);
481 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
482 * @dev: the device to unregister
483 * @bd: the backlight device to unregister
485 * Deallocated a backlight allocated with devm_backlight_device_register().
486 * Normally this function will not need to be called and the resource management
487 * code will ensure that the resource is freed.
489 void devm_backlight_device_unregister(struct device *dev,
490 struct backlight_device *bd)
494 rc = devres_release(dev, devm_backlight_device_release,
495 devm_backlight_device_match, bd);
498 EXPORT_SYMBOL(devm_backlight_device_unregister);
501 static int of_parent_match(struct device *dev, const void *data)
503 return dev->parent && dev->parent->of_node == data;
507 * of_find_backlight_by_node() - find backlight device by device-tree node
508 * @node: device-tree node of the backlight device
510 * Returns a pointer to the backlight device corresponding to the given DT
511 * node or NULL if no such backlight device exists or if the device hasn't
514 * This function obtains a reference on the backlight device and it is the
515 * caller's responsibility to drop the reference by calling put_device() on
516 * the backlight device's .dev field.
518 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
522 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
524 return dev ? to_backlight_device(dev) : NULL;
526 EXPORT_SYMBOL(of_find_backlight_by_node);
529 static void __exit backlight_class_exit(void)
531 class_destroy(backlight_class);
534 static int __init backlight_class_init(void)
536 backlight_class = class_create(THIS_MODULE, "backlight");
537 if (IS_ERR(backlight_class)) {
538 pr_warn("Unable to create backlight class; errno = %ld\n",
539 PTR_ERR(backlight_class));
540 return PTR_ERR(backlight_class);
543 backlight_class->dev_groups = bl_device_groups;
544 backlight_class->pm = &backlight_class_dev_pm_ops;
545 INIT_LIST_HEAD(&backlight_dev_list);
546 mutex_init(&backlight_dev_list_mutex);
551 * if this is compiled into the kernel, we need to ensure that the
552 * class is registered before users of the class try to register lcd's
554 postcore_initcall(backlight_class_init);
555 module_exit(backlight_class_exit);
557 MODULE_LICENSE("GPL");
558 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
559 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");