]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/video/backlight/backlight.c
Merge remote-tracking branch 'net-next/master'
[karo-tx-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 struct list_head backlight_dev_list;
25 static struct mutex backlight_dev_list_mutex;
26
27 static const char *const backlight_types[] = {
28         [BACKLIGHT_RAW] = "raw",
29         [BACKLIGHT_PLATFORM] = "platform",
30         [BACKLIGHT_FIRMWARE] = "firmware",
31 };
32
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, we're switching backlight power as well ...
38  */
39 static int fb_notifier_callback(struct notifier_block *self,
40                                 unsigned long event, void *data)
41 {
42         struct backlight_device *bd;
43         struct fb_event *evdata = data;
44
45         /* If we aren't interested in this event, skip it immediately ... */
46         if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
47                 return 0;
48
49         bd = container_of(self, struct backlight_device, fb_notif);
50         mutex_lock(&bd->ops_lock);
51         if (bd->ops)
52                 if (!bd->ops->check_fb ||
53                     bd->ops->check_fb(bd, evdata->info)) {
54                         bd->props.fb_blank = *(int *)evdata->data;
55                         if (bd->props.fb_blank == FB_BLANK_UNBLANK)
56                                 bd->props.state &= ~BL_CORE_FBBLANK;
57                         else
58                                 bd->props.state |= BL_CORE_FBBLANK;
59                         backlight_update_status(bd);
60                 }
61         mutex_unlock(&bd->ops_lock);
62         return 0;
63 }
64
65 static int backlight_register_fb(struct backlight_device *bd)
66 {
67         memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
68         bd->fb_notif.notifier_call = fb_notifier_callback;
69
70         return fb_register_client(&bd->fb_notif);
71 }
72
73 static void backlight_unregister_fb(struct backlight_device *bd)
74 {
75         fb_unregister_client(&bd->fb_notif);
76 }
77 #else
78 static inline int backlight_register_fb(struct backlight_device *bd)
79 {
80         return 0;
81 }
82
83 static inline void backlight_unregister_fb(struct backlight_device *bd)
84 {
85 }
86 #endif /* CONFIG_FB */
87
88 static void backlight_generate_event(struct backlight_device *bd,
89                                      enum backlight_update_reason reason)
90 {
91         char *envp[2];
92
93         switch (reason) {
94         case BACKLIGHT_UPDATE_SYSFS:
95                 envp[0] = "SOURCE=sysfs";
96                 break;
97         case BACKLIGHT_UPDATE_HOTKEY:
98                 envp[0] = "SOURCE=hotkey";
99                 break;
100         default:
101                 envp[0] = "SOURCE=unknown";
102                 break;
103         }
104         envp[1] = NULL;
105         kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
106         sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
107 }
108
109 static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
110                 char *buf)
111 {
112         struct backlight_device *bd = to_backlight_device(dev);
113
114         return sprintf(buf, "%d\n", bd->props.power);
115 }
116
117 static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
118                 const char *buf, size_t count)
119 {
120         int rc;
121         struct backlight_device *bd = to_backlight_device(dev);
122         unsigned long power;
123
124         rc = kstrtoul(buf, 0, &power);
125         if (rc)
126                 return rc;
127
128         rc = -ENXIO;
129         mutex_lock(&bd->ops_lock);
130         if (bd->ops) {
131                 pr_debug("set power to %lu\n", power);
132                 if (bd->props.power != power) {
133                         bd->props.power = power;
134                         backlight_update_status(bd);
135                 }
136                 rc = count;
137         }
138         mutex_unlock(&bd->ops_lock);
139
140         return rc;
141 }
142 static DEVICE_ATTR_RW(bl_power);
143
144 static ssize_t brightness_show(struct device *dev,
145                 struct device_attribute *attr, char *buf)
146 {
147         struct backlight_device *bd = to_backlight_device(dev);
148
149         return sprintf(buf, "%d\n", bd->props.brightness);
150 }
151
152 static ssize_t brightness_store(struct device *dev,
153                 struct device_attribute *attr, const char *buf, size_t count)
154 {
155         int rc;
156         struct backlight_device *bd = to_backlight_device(dev);
157         unsigned long brightness;
158
159         rc = kstrtoul(buf, 0, &brightness);
160         if (rc)
161                 return rc;
162
163         rc = -ENXIO;
164
165         mutex_lock(&bd->ops_lock);
166         if (bd->ops) {
167                 if (brightness > bd->props.max_brightness)
168                         rc = -EINVAL;
169                 else {
170                         pr_debug("set brightness to %lu\n", brightness);
171                         bd->props.brightness = brightness;
172                         backlight_update_status(bd);
173                         rc = count;
174                 }
175         }
176         mutex_unlock(&bd->ops_lock);
177
178         backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
179
180         return rc;
181 }
182 static DEVICE_ATTR_RW(brightness);
183
184 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
185                 char *buf)
186 {
187         struct backlight_device *bd = to_backlight_device(dev);
188
189         return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
190 }
191 static DEVICE_ATTR_RO(type);
192
193 static ssize_t max_brightness_show(struct device *dev,
194                 struct device_attribute *attr, char *buf)
195 {
196         struct backlight_device *bd = to_backlight_device(dev);
197
198         return sprintf(buf, "%d\n", bd->props.max_brightness);
199 }
200 static DEVICE_ATTR_RO(max_brightness);
201
202 static ssize_t actual_brightness_show(struct device *dev,
203                 struct device_attribute *attr, char *buf)
204 {
205         int rc = -ENXIO;
206         struct backlight_device *bd = to_backlight_device(dev);
207
208         mutex_lock(&bd->ops_lock);
209         if (bd->ops && bd->ops->get_brightness)
210                 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
211         mutex_unlock(&bd->ops_lock);
212
213         return rc;
214 }
215 static DEVICE_ATTR_RO(actual_brightness);
216
217 static struct class *backlight_class;
218
219 #ifdef CONFIG_PM_SLEEP
220 static int backlight_suspend(struct device *dev)
221 {
222         struct backlight_device *bd = to_backlight_device(dev);
223
224         mutex_lock(&bd->ops_lock);
225         if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
226                 bd->props.state |= BL_CORE_SUSPENDED;
227                 backlight_update_status(bd);
228         }
229         mutex_unlock(&bd->ops_lock);
230
231         return 0;
232 }
233
234 static int backlight_resume(struct device *dev)
235 {
236         struct backlight_device *bd = to_backlight_device(dev);
237
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);
242         }
243         mutex_unlock(&bd->ops_lock);
244
245         return 0;
246 }
247 #endif
248
249 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
250                          backlight_resume);
251
252 static void bl_device_release(struct device *dev)
253 {
254         struct backlight_device *bd = to_backlight_device(dev);
255         kfree(bd);
256 }
257
258 static struct attribute *bl_device_attrs[] = {
259         &dev_attr_bl_power.attr,
260         &dev_attr_brightness.attr,
261         &dev_attr_actual_brightness.attr,
262         &dev_attr_max_brightness.attr,
263         &dev_attr_type.attr,
264         NULL,
265 };
266 ATTRIBUTE_GROUPS(bl_device);
267
268 /**
269  * backlight_force_update - tell the backlight subsystem that hardware state
270  *   has changed
271  * @bd: the backlight device to update
272  *
273  * Updates the internal state of the backlight in response to a hardware event,
274  * and generate a uevent to notify userspace
275  */
276 void backlight_force_update(struct backlight_device *bd,
277                             enum backlight_update_reason reason)
278 {
279         mutex_lock(&bd->ops_lock);
280         if (bd->ops && bd->ops->get_brightness)
281                 bd->props.brightness = bd->ops->get_brightness(bd);
282         mutex_unlock(&bd->ops_lock);
283         backlight_generate_event(bd, reason);
284 }
285 EXPORT_SYMBOL(backlight_force_update);
286
287 /**
288  * backlight_device_register - create and register a new object of
289  *   backlight_device class.
290  * @name: the name of the new object(must be the same as the name of the
291  *   respective framebuffer device).
292  * @parent: a pointer to the parent device
293  * @devdata: an optional pointer to be stored for private driver use. The
294  *   methods may retrieve it by using bl_get_data(bd).
295  * @ops: the backlight operations structure.
296  *
297  * Creates and registers new backlight device. Returns either an
298  * ERR_PTR() or a pointer to the newly allocated device.
299  */
300 struct backlight_device *backlight_device_register(const char *name,
301         struct device *parent, void *devdata, const struct backlight_ops *ops,
302         const struct backlight_properties *props)
303 {
304         struct backlight_device *new_bd;
305         int rc;
306
307         pr_debug("backlight_device_register: name=%s\n", name);
308
309         new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
310         if (!new_bd)
311                 return ERR_PTR(-ENOMEM);
312
313         mutex_init(&new_bd->update_lock);
314         mutex_init(&new_bd->ops_lock);
315
316         new_bd->dev.class = backlight_class;
317         new_bd->dev.parent = parent;
318         new_bd->dev.release = bl_device_release;
319         dev_set_name(&new_bd->dev, "%s", name);
320         dev_set_drvdata(&new_bd->dev, devdata);
321
322         /* Set default properties */
323         if (props) {
324                 memcpy(&new_bd->props, props,
325                        sizeof(struct backlight_properties));
326                 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
327                         WARN(1, "%s: invalid backlight type", name);
328                         new_bd->props.type = BACKLIGHT_RAW;
329                 }
330         } else {
331                 new_bd->props.type = BACKLIGHT_RAW;
332         }
333
334         rc = device_register(&new_bd->dev);
335         if (rc) {
336                 kfree(new_bd);
337                 return ERR_PTR(rc);
338         }
339
340         rc = backlight_register_fb(new_bd);
341         if (rc) {
342                 device_unregister(&new_bd->dev);
343                 return ERR_PTR(rc);
344         }
345
346         new_bd->ops = ops;
347
348 #ifdef CONFIG_PMAC_BACKLIGHT
349         mutex_lock(&pmac_backlight_mutex);
350         if (!pmac_backlight)
351                 pmac_backlight = new_bd;
352         mutex_unlock(&pmac_backlight_mutex);
353 #endif
354
355         mutex_lock(&backlight_dev_list_mutex);
356         list_add(&new_bd->entry, &backlight_dev_list);
357         mutex_unlock(&backlight_dev_list_mutex);
358
359         return new_bd;
360 }
361 EXPORT_SYMBOL(backlight_device_register);
362
363 bool backlight_device_registered(enum backlight_type type)
364 {
365         bool found = false;
366         struct backlight_device *bd;
367
368         mutex_lock(&backlight_dev_list_mutex);
369         list_for_each_entry(bd, &backlight_dev_list, entry) {
370                 if (bd->props.type == type) {
371                         found = true;
372                         break;
373                 }
374         }
375         mutex_unlock(&backlight_dev_list_mutex);
376
377         return found;
378 }
379 EXPORT_SYMBOL(backlight_device_registered);
380
381 /**
382  * backlight_device_unregister - unregisters a backlight device object.
383  * @bd: the backlight device object to be unregistered and freed.
384  *
385  * Unregisters a previously registered via backlight_device_register object.
386  */
387 void backlight_device_unregister(struct backlight_device *bd)
388 {
389         if (!bd)
390                 return;
391
392         mutex_lock(&backlight_dev_list_mutex);
393         list_del(&bd->entry);
394         mutex_unlock(&backlight_dev_list_mutex);
395
396 #ifdef CONFIG_PMAC_BACKLIGHT
397         mutex_lock(&pmac_backlight_mutex);
398         if (pmac_backlight == bd)
399                 pmac_backlight = NULL;
400         mutex_unlock(&pmac_backlight_mutex);
401 #endif
402         mutex_lock(&bd->ops_lock);
403         bd->ops = NULL;
404         mutex_unlock(&bd->ops_lock);
405
406         backlight_unregister_fb(bd);
407         device_unregister(&bd->dev);
408 }
409 EXPORT_SYMBOL(backlight_device_unregister);
410
411 static void devm_backlight_device_release(struct device *dev, void *res)
412 {
413         struct backlight_device *backlight = *(struct backlight_device **)res;
414
415         backlight_device_unregister(backlight);
416 }
417
418 static int devm_backlight_device_match(struct device *dev, void *res,
419                                         void *data)
420 {
421         struct backlight_device **r = res;
422
423         return *r == data;
424 }
425
426 /**
427  * devm_backlight_device_register - resource managed backlight_device_register()
428  * @dev: the device to register
429  * @name: the name of the device
430  * @parent: a pointer to the parent device
431  * @devdata: an optional pointer to be stored for private driver use
432  * @ops: the backlight operations structure
433  * @props: the backlight properties
434  *
435  * @return a struct backlight on success, or an ERR_PTR on error
436  *
437  * Managed backlight_device_register(). The backlight_device returned
438  * from this function are automatically freed on driver detach.
439  * See backlight_device_register() for more information.
440  */
441 struct backlight_device *devm_backlight_device_register(struct device *dev,
442         const char *name, struct device *parent, void *devdata,
443         const struct backlight_ops *ops,
444         const struct backlight_properties *props)
445 {
446         struct backlight_device **ptr, *backlight;
447
448         ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
449                         GFP_KERNEL);
450         if (!ptr)
451                 return ERR_PTR(-ENOMEM);
452
453         backlight = backlight_device_register(name, parent, devdata, ops,
454                                                 props);
455         if (!IS_ERR(backlight)) {
456                 *ptr = backlight;
457                 devres_add(dev, ptr);
458         } else {
459                 devres_free(ptr);
460         }
461
462         return backlight;
463 }
464 EXPORT_SYMBOL(devm_backlight_device_register);
465
466 /**
467  * devm_backlight_device_unregister - resource managed backlight_device_unregister()
468  * @dev: the device to unregister
469  * @bd: the backlight device to unregister
470  *
471  * Deallocated a backlight allocated with devm_backlight_device_register().
472  * Normally this function will not need to be called and the resource management
473  * code will ensure that the resource is freed.
474  */
475 void devm_backlight_device_unregister(struct device *dev,
476                                 struct backlight_device *bd)
477 {
478         int rc;
479
480         rc = devres_release(dev, devm_backlight_device_release,
481                                 devm_backlight_device_match, bd);
482         WARN_ON(rc);
483 }
484 EXPORT_SYMBOL(devm_backlight_device_unregister);
485
486 #ifdef CONFIG_OF
487 static int of_parent_match(struct device *dev, const void *data)
488 {
489         return dev->parent && dev->parent->of_node == data;
490 }
491
492 /**
493  * of_find_backlight_by_node() - find backlight device by device-tree node
494  * @node: device-tree node of the backlight device
495  *
496  * Returns a pointer to the backlight device corresponding to the given DT
497  * node or NULL if no such backlight device exists or if the device hasn't
498  * been probed yet.
499  *
500  * This function obtains a reference on the backlight device and it is the
501  * caller's responsibility to drop the reference by calling put_device() on
502  * the backlight device's .dev field.
503  */
504 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
505 {
506         struct device *dev;
507
508         dev = class_find_device(backlight_class, NULL, node, of_parent_match);
509
510         return dev ? to_backlight_device(dev) : NULL;
511 }
512 EXPORT_SYMBOL(of_find_backlight_by_node);
513 #endif
514
515 static void __exit backlight_class_exit(void)
516 {
517         class_destroy(backlight_class);
518 }
519
520 static int __init backlight_class_init(void)
521 {
522         backlight_class = class_create(THIS_MODULE, "backlight");
523         if (IS_ERR(backlight_class)) {
524                 pr_warn("Unable to create backlight class; errno = %ld\n",
525                         PTR_ERR(backlight_class));
526                 return PTR_ERR(backlight_class);
527         }
528
529         backlight_class->dev_groups = bl_device_groups;
530         backlight_class->pm = &backlight_class_dev_pm_ops;
531         INIT_LIST_HEAD(&backlight_dev_list);
532         mutex_init(&backlight_dev_list_mutex);
533         return 0;
534 }
535
536 /*
537  * if this is compiled into the kernel, we need to ensure that the
538  * class is registered before users of the class try to register lcd's
539  */
540 postcore_initcall(backlight_class_init);
541 module_exit(backlight_class_exit);
542
543 MODULE_LICENSE("GPL");
544 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
545 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");