Merge branch 'drm-nouveau-fixes-3.9' of git://anongit.freedesktop.org/git/nouveau...
[linux-2.6/libata-dev.git] / drivers / video / backlight / backlight.c
blobc74e7aa467312097de2944452ad428ab8fe41b46
1 /*
2 * Backlight Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
6 */
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>
17 #include <linux/fb.h>
18 #include <linux/slab.h>
20 #ifdef CONFIG_PMAC_BACKLIGHT
21 #include <asm/backlight.h>
22 #endif
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)
44 return 0;
46 bd = container_of(self, struct backlight_device, fb_notif);
47 mutex_lock(&bd->ops_lock);
48 if (bd->ops)
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;
54 else
55 bd->props.state |= BL_CORE_FBBLANK;
56 backlight_update_status(bd);
58 mutex_unlock(&bd->ops_lock);
59 return 0;
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);
74 #else
75 static inline int backlight_register_fb(struct backlight_device *bd)
77 return 0;
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)
88 char *envp[2];
90 switch (reason) {
91 case BACKLIGHT_UPDATE_SYSFS:
92 envp[0] = "SOURCE=sysfs";
93 break;
94 case BACKLIGHT_UPDATE_HOTKEY:
95 envp[0] = "SOURCE=hotkey";
96 break;
97 default:
98 envp[0] = "SOURCE=unknown";
99 break;
101 envp[1] = NULL;
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)
117 int rc;
118 struct backlight_device *bd = to_backlight_device(dev);
119 unsigned long power;
121 rc = kstrtoul(buf, 0, &power);
122 if (rc)
123 return rc;
125 rc = -ENXIO;
126 mutex_lock(&bd->ops_lock);
127 if (bd->ops) {
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);
133 rc = count;
135 mutex_unlock(&bd->ops_lock);
137 return rc;
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)
151 int rc;
152 struct backlight_device *bd = to_backlight_device(dev);
153 unsigned long brightness;
155 rc = kstrtoul(buf, 0, &brightness);
156 if (rc)
157 return rc;
159 rc = -ENXIO;
161 mutex_lock(&bd->ops_lock);
162 if (bd->ops) {
163 if (brightness > bd->props.max_brightness)
164 rc = -EINVAL;
165 else {
166 pr_debug("set brightness to %lu\n", brightness);
167 bd->props.brightness = brightness;
168 backlight_update_status(bd);
169 rc = count;
172 mutex_unlock(&bd->ops_lock);
174 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
176 return rc;
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)
198 int rc = -ENXIO;
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);
206 return rc;
209 static struct class *backlight_class;
211 static int backlight_suspend(struct device *dev, pm_message_t state)
213 struct backlight_device *bd = to_backlight_device(dev);
215 mutex_lock(&bd->ops_lock);
216 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
217 bd->props.state |= BL_CORE_SUSPENDED;
218 backlight_update_status(bd);
220 mutex_unlock(&bd->ops_lock);
222 return 0;
225 static int backlight_resume(struct device *dev)
227 struct backlight_device *bd = to_backlight_device(dev);
229 mutex_lock(&bd->ops_lock);
230 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
231 bd->props.state &= ~BL_CORE_SUSPENDED;
232 backlight_update_status(bd);
234 mutex_unlock(&bd->ops_lock);
236 return 0;
239 static void bl_device_release(struct device *dev)
241 struct backlight_device *bd = to_backlight_device(dev);
242 kfree(bd);
245 static struct device_attribute bl_device_attributes[] = {
246 __ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
247 __ATTR(brightness, 0644, backlight_show_brightness,
248 backlight_store_brightness),
249 __ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
250 NULL),
251 __ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
252 __ATTR(type, 0444, backlight_show_type, NULL),
253 __ATTR_NULL,
257 * backlight_force_update - tell the backlight subsystem that hardware state
258 * has changed
259 * @bd: the backlight device to update
261 * Updates the internal state of the backlight in response to a hardware event,
262 * and generate a uevent to notify userspace
264 void backlight_force_update(struct backlight_device *bd,
265 enum backlight_update_reason reason)
267 mutex_lock(&bd->ops_lock);
268 if (bd->ops && bd->ops->get_brightness)
269 bd->props.brightness = bd->ops->get_brightness(bd);
270 mutex_unlock(&bd->ops_lock);
271 backlight_generate_event(bd, reason);
273 EXPORT_SYMBOL(backlight_force_update);
276 * backlight_device_register - create and register a new object of
277 * backlight_device class.
278 * @name: the name of the new object(must be the same as the name of the
279 * respective framebuffer device).
280 * @parent: a pointer to the parent device
281 * @devdata: an optional pointer to be stored for private driver use. The
282 * methods may retrieve it by using bl_get_data(bd).
283 * @ops: the backlight operations structure.
285 * Creates and registers new backlight device. Returns either an
286 * ERR_PTR() or a pointer to the newly allocated device.
288 struct backlight_device *backlight_device_register(const char *name,
289 struct device *parent, void *devdata, const struct backlight_ops *ops,
290 const struct backlight_properties *props)
292 struct backlight_device *new_bd;
293 int rc;
295 pr_debug("backlight_device_register: name=%s\n", name);
297 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
298 if (!new_bd)
299 return ERR_PTR(-ENOMEM);
301 mutex_init(&new_bd->update_lock);
302 mutex_init(&new_bd->ops_lock);
304 new_bd->dev.class = backlight_class;
305 new_bd->dev.parent = parent;
306 new_bd->dev.release = bl_device_release;
307 dev_set_name(&new_bd->dev, name);
308 dev_set_drvdata(&new_bd->dev, devdata);
310 /* Set default properties */
311 if (props) {
312 memcpy(&new_bd->props, props,
313 sizeof(struct backlight_properties));
314 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
315 WARN(1, "%s: invalid backlight type", name);
316 new_bd->props.type = BACKLIGHT_RAW;
318 } else {
319 new_bd->props.type = BACKLIGHT_RAW;
322 rc = device_register(&new_bd->dev);
323 if (rc) {
324 kfree(new_bd);
325 return ERR_PTR(rc);
328 rc = backlight_register_fb(new_bd);
329 if (rc) {
330 device_unregister(&new_bd->dev);
331 return ERR_PTR(rc);
334 new_bd->ops = ops;
336 #ifdef CONFIG_PMAC_BACKLIGHT
337 mutex_lock(&pmac_backlight_mutex);
338 if (!pmac_backlight)
339 pmac_backlight = new_bd;
340 mutex_unlock(&pmac_backlight_mutex);
341 #endif
343 return new_bd;
345 EXPORT_SYMBOL(backlight_device_register);
348 * backlight_device_unregister - unregisters a backlight device object.
349 * @bd: the backlight device object to be unregistered and freed.
351 * Unregisters a previously registered via backlight_device_register object.
353 void backlight_device_unregister(struct backlight_device *bd)
355 if (!bd)
356 return;
358 #ifdef CONFIG_PMAC_BACKLIGHT
359 mutex_lock(&pmac_backlight_mutex);
360 if (pmac_backlight == bd)
361 pmac_backlight = NULL;
362 mutex_unlock(&pmac_backlight_mutex);
363 #endif
364 mutex_lock(&bd->ops_lock);
365 bd->ops = NULL;
366 mutex_unlock(&bd->ops_lock);
368 backlight_unregister_fb(bd);
369 device_unregister(&bd->dev);
371 EXPORT_SYMBOL(backlight_device_unregister);
373 #ifdef CONFIG_OF
374 static int of_parent_match(struct device *dev, const void *data)
376 return dev->parent && dev->parent->of_node == data;
380 * of_find_backlight_by_node() - find backlight device by device-tree node
381 * @node: device-tree node of the backlight device
383 * Returns a pointer to the backlight device corresponding to the given DT
384 * node or NULL if no such backlight device exists or if the device hasn't
385 * been probed yet.
387 * This function obtains a reference on the backlight device and it is the
388 * caller's responsibility to drop the reference by calling put_device() on
389 * the backlight device's .dev field.
391 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
393 struct device *dev;
395 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
397 return dev ? to_backlight_device(dev) : NULL;
399 EXPORT_SYMBOL(of_find_backlight_by_node);
400 #endif
402 static void __exit backlight_class_exit(void)
404 class_destroy(backlight_class);
407 static int __init backlight_class_init(void)
409 backlight_class = class_create(THIS_MODULE, "backlight");
410 if (IS_ERR(backlight_class)) {
411 pr_warn("Unable to create backlight class; errno = %ld\n",
412 PTR_ERR(backlight_class));
413 return PTR_ERR(backlight_class);
416 backlight_class->dev_attrs = bl_device_attributes;
417 backlight_class->suspend = backlight_suspend;
418 backlight_class->resume = backlight_resume;
419 return 0;
423 * if this is compiled into the kernel, we need to ensure that the
424 * class is registered before users of the class try to register lcd's
426 postcore_initcall(backlight_class_init);
427 module_exit(backlight_class_exit);
429 MODULE_LICENSE("GPL");
430 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
431 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");