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 backlight_show_power(struct device *dev,
107 struct device_attribute *attr, char *buf)
109 struct backlight_device *bd = to_backlight_device(dev);
111 return sprintf(buf, "%d\n", bd->props.power);
114 static ssize_t backlight_store_power(struct device *dev,
115 struct device_attribute *attr, 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);
140 static ssize_t backlight_show_brightness(struct device *dev,
141 struct device_attribute *attr, char *buf)
143 struct backlight_device *bd = to_backlight_device(dev);
145 return sprintf(buf, "%d\n", bd->props.brightness);
148 static ssize_t backlight_store_brightness(struct device *dev,
149 struct device_attribute *attr, const char *buf, size_t count)
152 struct backlight_device *bd = to_backlight_device(dev);
153 unsigned long brightness;
155 rc = kstrtoul(buf, 0, &brightness);
161 mutex_lock(&bd->ops_lock);
163 if (brightness > bd->props.max_brightness)
166 pr_debug("set brightness to %lu\n", brightness);
167 bd->props.brightness = brightness;
168 backlight_update_status(bd);
172 mutex_unlock(&bd->ops_lock);
174 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
179 static ssize_t backlight_show_type(struct device *dev,
180 struct device_attribute *attr, char *buf)
182 struct backlight_device *bd = to_backlight_device(dev);
184 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
187 static ssize_t backlight_show_max_brightness(struct device *dev,
188 struct device_attribute *attr, char *buf)
190 struct backlight_device *bd = to_backlight_device(dev);
192 return sprintf(buf, "%d\n", bd->props.max_brightness);
195 static ssize_t backlight_show_actual_brightness(struct device *dev,
196 struct device_attribute *attr, char *buf)
199 struct backlight_device *bd = to_backlight_device(dev);
201 mutex_lock(&bd->ops_lock);
202 if (bd->ops && bd->ops->get_brightness)
203 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
204 mutex_unlock(&bd->ops_lock);
209 static struct class *backlight_class;
211 #ifdef CONFIG_PM_SLEEP
212 static int backlight_suspend(struct device *dev)
214 struct backlight_device *bd = to_backlight_device(dev);
216 mutex_lock(&bd->ops_lock);
217 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
218 bd->props.state |= BL_CORE_SUSPENDED;
219 backlight_update_status(bd);
221 mutex_unlock(&bd->ops_lock);
226 static int backlight_resume(struct device *dev)
228 struct backlight_device *bd = to_backlight_device(dev);
230 mutex_lock(&bd->ops_lock);
231 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
232 bd->props.state &= ~BL_CORE_SUSPENDED;
233 backlight_update_status(bd);
235 mutex_unlock(&bd->ops_lock);
241 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
244 static void bl_device_release(struct device *dev)
246 struct backlight_device *bd = to_backlight_device(dev);
250 static struct device_attribute bl_device_attributes[] = {
251 __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
252 __ATTR(brightness, 0644, backlight_show_brightness,
253 backlight_store_brightness),
254 __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
256 __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
257 __ATTR(type, 0444, backlight_show_type, NULL),
262 * backlight_force_update - tell the backlight subsystem that hardware state
264 * @bd: the backlight device to update
266 * Updates the internal state of the backlight in response to a hardware event,
267 * and generate a uevent to notify userspace
269 void backlight_force_update(struct backlight_device *bd,
270 enum backlight_update_reason reason)
272 mutex_lock(&bd->ops_lock);
273 if (bd->ops && bd->ops->get_brightness)
274 bd->props.brightness = bd->ops->get_brightness(bd);
275 mutex_unlock(&bd->ops_lock);
276 backlight_generate_event(bd, reason);
278 EXPORT_SYMBOL(backlight_force_update);
281 * backlight_device_register - create and register a new object of
282 * backlight_device class.
283 * @name: the name of the new object(must be the same as the name of the
284 * respective framebuffer device).
285 * @parent: a pointer to the parent device
286 * @devdata: an optional pointer to be stored for private driver use. The
287 * methods may retrieve it by using bl_get_data(bd).
288 * @ops: the backlight operations structure.
290 * Creates and registers new backlight device. Returns either an
291 * ERR_PTR() or a pointer to the newly allocated device.
293 struct backlight_device *backlight_device_register(const char *name,
294 struct device *parent, void *devdata, const struct backlight_ops *ops,
295 const struct backlight_properties *props)
297 struct backlight_device *new_bd;
300 pr_debug("backlight_device_register: name=%s\n", name);
302 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
304 return ERR_PTR(-ENOMEM);
306 mutex_init(&new_bd->update_lock);
307 mutex_init(&new_bd->ops_lock);
309 new_bd->dev.class = backlight_class;
310 new_bd->dev.parent = parent;
311 new_bd->dev.release = bl_device_release;
312 dev_set_name(&new_bd->dev, "%s", name);
313 dev_set_drvdata(&new_bd->dev, devdata);
315 /* Set default properties */
317 memcpy(&new_bd->props, props,
318 sizeof(struct backlight_properties));
319 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
320 WARN(1, "%s: invalid backlight type", name);
321 new_bd->props.type = BACKLIGHT_RAW;
324 new_bd->props.type = BACKLIGHT_RAW;
327 rc = device_register(&new_bd->dev);
333 rc = backlight_register_fb(new_bd);
335 device_unregister(&new_bd->dev);
341 #ifdef CONFIG_PMAC_BACKLIGHT
342 mutex_lock(&pmac_backlight_mutex);
344 pmac_backlight = new_bd;
345 mutex_unlock(&pmac_backlight_mutex);
350 EXPORT_SYMBOL(backlight_device_register);
353 * backlight_device_unregister - unregisters a backlight device object.
354 * @bd: the backlight device object to be unregistered and freed.
356 * Unregisters a previously registered via backlight_device_register object.
358 void backlight_device_unregister(struct backlight_device *bd)
363 #ifdef CONFIG_PMAC_BACKLIGHT
364 mutex_lock(&pmac_backlight_mutex);
365 if (pmac_backlight == bd)
366 pmac_backlight = NULL;
367 mutex_unlock(&pmac_backlight_mutex);
369 mutex_lock(&bd->ops_lock);
371 mutex_unlock(&bd->ops_lock);
373 backlight_unregister_fb(bd);
374 device_unregister(&bd->dev);
376 EXPORT_SYMBOL(backlight_device_unregister);
378 static void devm_backlight_device_release(struct device *dev, void *res)
380 struct backlight_device *backlight = *(struct backlight_device **)res;
382 backlight_device_unregister(backlight);
385 static int devm_backlight_device_match(struct device *dev, void *res,
388 struct backlight_device **r = res;
394 * devm_backlight_device_register - resource managed backlight_device_register()
395 * @dev: the device to register
396 * @name: the name of the device
397 * @parent: a pointer to the parent device
398 * @devdata: an optional pointer to be stored for private driver use
399 * @ops: the backlight operations structure
400 * @props: the backlight properties
402 * @return a struct backlight on success, or an ERR_PTR on error
404 * Managed backlight_device_register(). The backlight_device returned
405 * from this function are automatically freed on driver detach.
406 * See backlight_device_register() for more information.
408 struct backlight_device *devm_backlight_device_register(struct device *dev,
409 const char *name, struct device *parent, void *devdata,
410 const struct backlight_ops *ops,
411 const struct backlight_properties *props)
413 struct backlight_device **ptr, *backlight;
415 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
418 return ERR_PTR(-ENOMEM);
420 backlight = backlight_device_register(name, parent, devdata, ops,
422 if (!IS_ERR(backlight)) {
424 devres_add(dev, ptr);
431 EXPORT_SYMBOL(devm_backlight_device_register);
434 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
435 * @dev: the device to unregister
436 * @bd: the backlight device to unregister
438 * Deallocated a backlight allocated with devm_backlight_device_register().
439 * Normally this function will not need to be called and the resource management
440 * code will ensure that the resource is freed.
442 void devm_backlight_device_unregister(struct device *dev,
443 struct backlight_device *bd)
447 rc = devres_release(dev, devm_backlight_device_release,
448 devm_backlight_device_match, bd);
451 EXPORT_SYMBOL(devm_backlight_device_unregister);
454 static int of_parent_match(struct device *dev, const void *data)
456 return dev->parent && dev->parent->of_node == data;
460 * of_find_backlight_by_node() - find backlight device by device-tree node
461 * @node: device-tree node of the backlight device
463 * Returns a pointer to the backlight device corresponding to the given DT
464 * node or NULL if no such backlight device exists or if the device hasn't
467 * This function obtains a reference on the backlight device and it is the
468 * caller's responsibility to drop the reference by calling put_device() on
469 * the backlight device's .dev field.
471 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
475 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
477 return dev ? to_backlight_device(dev) : NULL;
479 EXPORT_SYMBOL(of_find_backlight_by_node);
482 static void __exit backlight_class_exit(void)
484 class_destroy(backlight_class);
487 static int __init backlight_class_init(void)
489 backlight_class = class_create(THIS_MODULE, "backlight");
490 if (IS_ERR(backlight_class)) {
491 pr_warn("Unable to create backlight class; errno = %ld\n",
492 PTR_ERR(backlight_class));
493 return PTR_ERR(backlight_class);
496 backlight_class->dev_attrs = bl_device_attributes;
497 backlight_class->pm = &backlight_class_dev_pm_ops;
502 * if this is compiled into the kernel, we need to ensure that the
503 * class is registered before users of the class try to register lcd's
505 postcore_initcall(backlight_class_init);
506 module_exit(backlight_class_exit);
508 MODULE_LICENSE("GPL");
509 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
510 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");