Merge branch 'fixes' of git://git.linaro.org/people/rmk/linux-arm
[cascardo/linux.git] / drivers / video / backlight / backlight.c
1 /*
2  * Backlight Lowlevel Control Abstraction
3  *
4  * Copyright (C) 2003,2004 Hewlett-Packard Company
5  *
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
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>
19
20 #ifdef CONFIG_PMAC_BACKLIGHT
21 #include <asm/backlight.h>
22 #endif
23
24 static const char *const backlight_types[] = {
25         [BACKLIGHT_RAW] = "raw",
26         [BACKLIGHT_PLATFORM] = "platform",
27         [BACKLIGHT_FIRMWARE] = "firmware",
28 };
29
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 ...
35  */
36 static int fb_notifier_callback(struct notifier_block *self,
37                                 unsigned long event, void *data)
38 {
39         struct backlight_device *bd;
40         struct fb_event *evdata = data;
41
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;
45
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);
57                 }
58         mutex_unlock(&bd->ops_lock);
59         return 0;
60 }
61
62 static int backlight_register_fb(struct backlight_device *bd)
63 {
64         memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
65         bd->fb_notif.notifier_call = fb_notifier_callback;
66
67         return fb_register_client(&bd->fb_notif);
68 }
69
70 static void backlight_unregister_fb(struct backlight_device *bd)
71 {
72         fb_unregister_client(&bd->fb_notif);
73 }
74 #else
75 static inline int backlight_register_fb(struct backlight_device *bd)
76 {
77         return 0;
78 }
79
80 static inline void backlight_unregister_fb(struct backlight_device *bd)
81 {
82 }
83 #endif /* CONFIG_FB */
84
85 static void backlight_generate_event(struct backlight_device *bd,
86                                      enum backlight_update_reason reason)
87 {
88         char *envp[2];
89
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;
100         }
101         envp[1] = NULL;
102         kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
103         sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
104 }
105
106 static ssize_t backlight_show_power(struct device *dev,
107                 struct device_attribute *attr, char *buf)
108 {
109         struct backlight_device *bd = to_backlight_device(dev);
110
111         return sprintf(buf, "%d\n", bd->props.power);
112 }
113
114 static ssize_t backlight_store_power(struct device *dev,
115                 struct device_attribute *attr, const char *buf, size_t count)
116 {
117         int rc;
118         struct backlight_device *bd = to_backlight_device(dev);
119         unsigned long power;
120
121         rc = kstrtoul(buf, 0, &power);
122         if (rc)
123                 return rc;
124
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);
132                 }
133                 rc = count;
134         }
135         mutex_unlock(&bd->ops_lock);
136
137         return rc;
138 }
139
140 static ssize_t backlight_show_brightness(struct device *dev,
141                 struct device_attribute *attr, char *buf)
142 {
143         struct backlight_device *bd = to_backlight_device(dev);
144
145         return sprintf(buf, "%d\n", bd->props.brightness);
146 }
147
148 static ssize_t backlight_store_brightness(struct device *dev,
149                 struct device_attribute *attr, const char *buf, size_t count)
150 {
151         int rc;
152         struct backlight_device *bd = to_backlight_device(dev);
153         unsigned long brightness;
154
155         rc = kstrtoul(buf, 0, &brightness);
156         if (rc)
157                 return rc;
158
159         rc = -ENXIO;
160
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;
170                 }
171         }
172         mutex_unlock(&bd->ops_lock);
173
174         backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
175
176         return rc;
177 }
178
179 static ssize_t backlight_show_type(struct device *dev,
180                 struct device_attribute *attr, char *buf)
181 {
182         struct backlight_device *bd = to_backlight_device(dev);
183
184         return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
185 }
186
187 static ssize_t backlight_show_max_brightness(struct device *dev,
188                 struct device_attribute *attr, char *buf)
189 {
190         struct backlight_device *bd = to_backlight_device(dev);
191
192         return sprintf(buf, "%d\n", bd->props.max_brightness);
193 }
194
195 static ssize_t backlight_show_actual_brightness(struct device *dev,
196                 struct device_attribute *attr, char *buf)
197 {
198         int rc = -ENXIO;
199         struct backlight_device *bd = to_backlight_device(dev);
200
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);
205
206         return rc;
207 }
208
209 static struct class *backlight_class;
210
211 static int backlight_suspend(struct device *dev, pm_message_t state)
212 {
213         struct backlight_device *bd = to_backlight_device(dev);
214
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);
219         }
220         mutex_unlock(&bd->ops_lock);
221
222         return 0;
223 }
224
225 static int backlight_resume(struct device *dev)
226 {
227         struct backlight_device *bd = to_backlight_device(dev);
228
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);
233         }
234         mutex_unlock(&bd->ops_lock);
235
236         return 0;
237 }
238
239 static void bl_device_release(struct device *dev)
240 {
241         struct backlight_device *bd = to_backlight_device(dev);
242         kfree(bd);
243 }
244
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,
254 };
255
256 /**
257  * backlight_force_update - tell the backlight subsystem that hardware state
258  *   has changed
259  * @bd: the backlight device to update
260  *
261  * Updates the internal state of the backlight in response to a hardware event,
262  * and generate a uevent to notify userspace
263  */
264 void backlight_force_update(struct backlight_device *bd,
265                             enum backlight_update_reason reason)
266 {
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);
272 }
273 EXPORT_SYMBOL(backlight_force_update);
274
275 /**
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.
284  *
285  * Creates and registers new backlight device. Returns either an
286  * ERR_PTR() or a pointer to the newly allocated device.
287  */
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)
291 {
292         struct backlight_device *new_bd;
293         int rc;
294
295         pr_debug("backlight_device_register: name=%s\n", name);
296
297         new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
298         if (!new_bd)
299                 return ERR_PTR(-ENOMEM);
300
301         mutex_init(&new_bd->update_lock);
302         mutex_init(&new_bd->ops_lock);
303
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);
309
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;
317                 }
318         } else {
319                 new_bd->props.type = BACKLIGHT_RAW;
320         }
321
322         rc = device_register(&new_bd->dev);
323         if (rc) {
324                 kfree(new_bd);
325                 return ERR_PTR(rc);
326         }
327
328         rc = backlight_register_fb(new_bd);
329         if (rc) {
330                 device_unregister(&new_bd->dev);
331                 return ERR_PTR(rc);
332         }
333
334         new_bd->ops = ops;
335
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
342
343         return new_bd;
344 }
345 EXPORT_SYMBOL(backlight_device_register);
346
347 /**
348  * backlight_device_unregister - unregisters a backlight device object.
349  * @bd: the backlight device object to be unregistered and freed.
350  *
351  * Unregisters a previously registered via backlight_device_register object.
352  */
353 void backlight_device_unregister(struct backlight_device *bd)
354 {
355         if (!bd)
356                 return;
357
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);
367
368         backlight_unregister_fb(bd);
369         device_unregister(&bd->dev);
370 }
371 EXPORT_SYMBOL(backlight_device_unregister);
372
373 #ifdef CONFIG_OF
374 static int of_parent_match(struct device *dev, void *data)
375 {
376         return dev->parent && dev->parent->of_node == data;
377 }
378
379 /**
380  * of_find_backlight_by_node() - find backlight device by device-tree node
381  * @node: device-tree node of the backlight device
382  *
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.
386  *
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.
390  */
391 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
392 {
393         struct device *dev;
394
395         dev = class_find_device(backlight_class, NULL, node, of_parent_match);
396
397         return dev ? to_backlight_device(dev) : NULL;
398 }
399 EXPORT_SYMBOL(of_find_backlight_by_node);
400 #endif
401
402 static void __exit backlight_class_exit(void)
403 {
404         class_destroy(backlight_class);
405 }
406
407 static int __init backlight_class_init(void)
408 {
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);
414         }
415
416         backlight_class->dev_attrs = bl_device_attributes;
417         backlight_class->suspend = backlight_suspend;
418         backlight_class->resume = backlight_resume;
419         return 0;
420 }
421
422 /*
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
425  */
426 postcore_initcall(backlight_class_init);
427 module_exit(backlight_class_exit);
428
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");