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