backlight: move the backlight off operation to the front of the LCD
[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 static ssize_t brightness_store(struct device *dev,
171                 struct device_attribute *attr, const char *buf, size_t count)
172 {
173         int rc;
174         struct backlight_device *bd = to_backlight_device(dev);
175         unsigned long brightness;
176
177         rc = kstrtoul(buf, 0, &brightness);
178         if (rc)
179                 return rc;
180
181         rc = -ENXIO;
182
183         mutex_lock(&bd->ops_lock);
184         if (bd->ops) {
185                 if (brightness > bd->props.max_brightness)
186                         rc = -EINVAL;
187                 else {
188                         pr_debug("set brightness to %lu\n", brightness);
189                         bd->props.brightness = brightness;
190                         backlight_update_status(bd);
191                         rc = count;
192                 }
193         }
194         mutex_unlock(&bd->ops_lock);
195
196         backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
197
198         return rc;
199 }
200 static DEVICE_ATTR_RW(brightness);
201
202 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
203                 char *buf)
204 {
205         struct backlight_device *bd = to_backlight_device(dev);
206
207         return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
208 }
209 static DEVICE_ATTR_RO(type);
210
211 static ssize_t max_brightness_show(struct device *dev,
212                 struct device_attribute *attr, char *buf)
213 {
214         struct backlight_device *bd = to_backlight_device(dev);
215
216         return sprintf(buf, "%d\n", bd->props.max_brightness);
217 }
218 static DEVICE_ATTR_RO(max_brightness);
219
220 static ssize_t actual_brightness_show(struct device *dev,
221                 struct device_attribute *attr, char *buf)
222 {
223         int rc = -ENXIO;
224         struct backlight_device *bd = to_backlight_device(dev);
225
226         mutex_lock(&bd->ops_lock);
227         if (bd->ops && bd->ops->get_brightness)
228                 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
229         else
230                 rc = sprintf(buf, "%d\n", bd->props.brightness);
231         mutex_unlock(&bd->ops_lock);
232
233         return rc;
234 }
235 static DEVICE_ATTR_RO(actual_brightness);
236
237 static struct class *backlight_class;
238
239 #ifdef CONFIG_PM_SLEEP
240 static int backlight_suspend(struct device *dev)
241 {
242         struct backlight_device *bd = to_backlight_device(dev);
243
244         mutex_lock(&bd->ops_lock);
245         if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
246                 bd->props.state |= BL_CORE_SUSPENDED;
247                 backlight_update_status(bd);
248         }
249         mutex_unlock(&bd->ops_lock);
250
251         return 0;
252 }
253
254 static int backlight_resume(struct device *dev)
255 {
256         struct backlight_device *bd = to_backlight_device(dev);
257
258         mutex_lock(&bd->ops_lock);
259         if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
260                 bd->props.state &= ~BL_CORE_SUSPENDED;
261                 backlight_update_status(bd);
262         }
263         mutex_unlock(&bd->ops_lock);
264
265         return 0;
266 }
267 #endif
268
269 static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
270                          backlight_resume);
271
272 static void bl_device_release(struct device *dev)
273 {
274         struct backlight_device *bd = to_backlight_device(dev);
275         kfree(bd);
276 }
277
278 static struct attribute *bl_device_attrs[] = {
279         &dev_attr_bl_power.attr,
280         &dev_attr_brightness.attr,
281         &dev_attr_actual_brightness.attr,
282         &dev_attr_max_brightness.attr,
283         &dev_attr_type.attr,
284         NULL,
285 };
286 ATTRIBUTE_GROUPS(bl_device);
287
288 /**
289  * backlight_force_update - tell the backlight subsystem that hardware state
290  *   has changed
291  * @bd: the backlight device to update
292  *
293  * Updates the internal state of the backlight in response to a hardware event,
294  * and generate a uevent to notify userspace
295  */
296 void backlight_force_update(struct backlight_device *bd,
297                             enum backlight_update_reason reason)
298 {
299         mutex_lock(&bd->ops_lock);
300         if (bd->ops && bd->ops->get_brightness)
301                 bd->props.brightness = bd->ops->get_brightness(bd);
302         mutex_unlock(&bd->ops_lock);
303         backlight_generate_event(bd, reason);
304 }
305 EXPORT_SYMBOL(backlight_force_update);
306
307 /**
308  * backlight_device_register - create and register a new object of
309  *   backlight_device class.
310  * @name: the name of the new object(must be the same as the name of the
311  *   respective framebuffer device).
312  * @parent: a pointer to the parent device
313  * @devdata: an optional pointer to be stored for private driver use. The
314  *   methods may retrieve it by using bl_get_data(bd).
315  * @ops: the backlight operations structure.
316  *
317  * Creates and registers new backlight device. Returns either an
318  * ERR_PTR() or a pointer to the newly allocated device.
319  */
320 struct backlight_device *backlight_device_register(const char *name,
321         struct device *parent, void *devdata, const struct backlight_ops *ops,
322         const struct backlight_properties *props)
323 {
324         struct backlight_device *new_bd;
325         int rc;
326
327         pr_debug("backlight_device_register: name=%s\n", name);
328
329         new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
330         if (!new_bd)
331                 return ERR_PTR(-ENOMEM);
332
333         mutex_init(&new_bd->update_lock);
334         mutex_init(&new_bd->ops_lock);
335
336         new_bd->dev.class = backlight_class;
337         new_bd->dev.parent = parent;
338         new_bd->dev.release = bl_device_release;
339         dev_set_name(&new_bd->dev, "%s", name);
340         dev_set_drvdata(&new_bd->dev, devdata);
341
342         /* Set default properties */
343         if (props) {
344                 memcpy(&new_bd->props, props,
345                        sizeof(struct backlight_properties));
346                 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
347                         WARN(1, "%s: invalid backlight type", name);
348                         new_bd->props.type = BACKLIGHT_RAW;
349                 }
350         } else {
351                 new_bd->props.type = BACKLIGHT_RAW;
352         }
353
354         rc = device_register(&new_bd->dev);
355         if (rc) {
356                 put_device(&new_bd->dev);
357                 return ERR_PTR(rc);
358         }
359
360         rc = backlight_register_fb(new_bd);
361         if (rc) {
362                 device_unregister(&new_bd->dev);
363                 return ERR_PTR(rc);
364         }
365
366         new_bd->ops = ops;
367
368 #ifdef CONFIG_PMAC_BACKLIGHT
369         mutex_lock(&pmac_backlight_mutex);
370         if (!pmac_backlight)
371                 pmac_backlight = new_bd;
372         mutex_unlock(&pmac_backlight_mutex);
373 #endif
374
375         mutex_lock(&backlight_dev_list_mutex);
376         list_add(&new_bd->entry, &backlight_dev_list);
377         mutex_unlock(&backlight_dev_list_mutex);
378
379         blocking_notifier_call_chain(&backlight_notifier,
380                                      BACKLIGHT_REGISTERED, new_bd);
381
382         return new_bd;
383 }
384 EXPORT_SYMBOL(backlight_device_register);
385
386 bool backlight_device_registered(enum backlight_type type)
387 {
388         bool found = false;
389         struct backlight_device *bd;
390
391         mutex_lock(&backlight_dev_list_mutex);
392         list_for_each_entry(bd, &backlight_dev_list, entry) {
393                 if (bd->props.type == type) {
394                         found = true;
395                         break;
396                 }
397         }
398         mutex_unlock(&backlight_dev_list_mutex);
399
400         return found;
401 }
402 EXPORT_SYMBOL(backlight_device_registered);
403
404 /**
405  * backlight_device_unregister - unregisters a backlight device object.
406  * @bd: the backlight device object to be unregistered and freed.
407  *
408  * Unregisters a previously registered via backlight_device_register object.
409  */
410 void backlight_device_unregister(struct backlight_device *bd)
411 {
412         if (!bd)
413                 return;
414
415         mutex_lock(&backlight_dev_list_mutex);
416         list_del(&bd->entry);
417         mutex_unlock(&backlight_dev_list_mutex);
418
419 #ifdef CONFIG_PMAC_BACKLIGHT
420         mutex_lock(&pmac_backlight_mutex);
421         if (pmac_backlight == bd)
422                 pmac_backlight = NULL;
423         mutex_unlock(&pmac_backlight_mutex);
424 #endif
425
426         blocking_notifier_call_chain(&backlight_notifier,
427                                      BACKLIGHT_UNREGISTERED, bd);
428
429         mutex_lock(&bd->ops_lock);
430         bd->ops = NULL;
431         mutex_unlock(&bd->ops_lock);
432
433         backlight_unregister_fb(bd);
434         device_unregister(&bd->dev);
435 }
436 EXPORT_SYMBOL(backlight_device_unregister);
437
438 static void devm_backlight_device_release(struct device *dev, void *res)
439 {
440         struct backlight_device *backlight = *(struct backlight_device **)res;
441
442         backlight_device_unregister(backlight);
443 }
444
445 static int devm_backlight_device_match(struct device *dev, void *res,
446                                         void *data)
447 {
448         struct backlight_device **r = res;
449
450         return *r == data;
451 }
452
453 /**
454  * backlight_register_notifier - get notified of backlight (un)registration
455  * @nb: notifier block with the notifier to call on backlight (un)registration
456  *
457  * @return 0 on success, otherwise a negative error code
458  *
459  * Register a notifier to get notified when backlight devices get registered
460  * or unregistered.
461  */
462 int backlight_register_notifier(struct notifier_block *nb)
463 {
464         return blocking_notifier_chain_register(&backlight_notifier, nb);
465 }
466 EXPORT_SYMBOL(backlight_register_notifier);
467
468 /**
469  * backlight_unregister_notifier - unregister a backlight notifier
470  * @nb: notifier block to unregister
471  *
472  * @return 0 on success, otherwise a negative error code
473  *
474  * Register a notifier to get notified when backlight devices get registered
475  * or unregistered.
476  */
477 int backlight_unregister_notifier(struct notifier_block *nb)
478 {
479         return blocking_notifier_chain_unregister(&backlight_notifier, nb);
480 }
481 EXPORT_SYMBOL(backlight_unregister_notifier);
482
483 /**
484  * devm_backlight_device_register - resource managed backlight_device_register()
485  * @dev: the device to register
486  * @name: the name of the device
487  * @parent: a pointer to the parent device
488  * @devdata: an optional pointer to be stored for private driver use
489  * @ops: the backlight operations structure
490  * @props: the backlight properties
491  *
492  * @return a struct backlight on success, or an ERR_PTR on error
493  *
494  * Managed backlight_device_register(). The backlight_device returned
495  * from this function are automatically freed on driver detach.
496  * See backlight_device_register() for more information.
497  */
498 struct backlight_device *devm_backlight_device_register(struct device *dev,
499         const char *name, struct device *parent, void *devdata,
500         const struct backlight_ops *ops,
501         const struct backlight_properties *props)
502 {
503         struct backlight_device **ptr, *backlight;
504
505         ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
506                         GFP_KERNEL);
507         if (!ptr)
508                 return ERR_PTR(-ENOMEM);
509
510         backlight = backlight_device_register(name, parent, devdata, ops,
511                                                 props);
512         if (!IS_ERR(backlight)) {
513                 *ptr = backlight;
514                 devres_add(dev, ptr);
515         } else {
516                 devres_free(ptr);
517         }
518
519         return backlight;
520 }
521 EXPORT_SYMBOL(devm_backlight_device_register);
522
523 /**
524  * devm_backlight_device_unregister - resource managed backlight_device_unregister()
525  * @dev: the device to unregister
526  * @bd: the backlight device to unregister
527  *
528  * Deallocated a backlight allocated with devm_backlight_device_register().
529  * Normally this function will not need to be called and the resource management
530  * code will ensure that the resource is freed.
531  */
532 void devm_backlight_device_unregister(struct device *dev,
533                                 struct backlight_device *bd)
534 {
535         int rc;
536
537         rc = devres_release(dev, devm_backlight_device_release,
538                                 devm_backlight_device_match, bd);
539         WARN_ON(rc);
540 }
541 EXPORT_SYMBOL(devm_backlight_device_unregister);
542
543 #ifdef CONFIG_OF
544 static int of_parent_match(struct device *dev, const void *data)
545 {
546         return dev->parent && dev->parent->of_node == data;
547 }
548
549 /**
550  * of_find_backlight_by_node() - find backlight device by device-tree node
551  * @node: device-tree node of the backlight device
552  *
553  * Returns a pointer to the backlight device corresponding to the given DT
554  * node or NULL if no such backlight device exists or if the device hasn't
555  * been probed yet.
556  *
557  * This function obtains a reference on the backlight device and it is the
558  * caller's responsibility to drop the reference by calling put_device() on
559  * the backlight device's .dev field.
560  */
561 struct backlight_device *of_find_backlight_by_node(struct device_node *node)
562 {
563         struct device *dev;
564
565         dev = class_find_device(backlight_class, NULL, node, of_parent_match);
566
567         return dev ? to_backlight_device(dev) : NULL;
568 }
569 EXPORT_SYMBOL(of_find_backlight_by_node);
570 #endif
571
572 static void __exit backlight_class_exit(void)
573 {
574         class_destroy(backlight_class);
575 }
576
577 static int __init backlight_class_init(void)
578 {
579         backlight_class = class_create(THIS_MODULE, "backlight");
580         if (IS_ERR(backlight_class)) {
581                 pr_warn("Unable to create backlight class; errno = %ld\n",
582                         PTR_ERR(backlight_class));
583                 return PTR_ERR(backlight_class);
584         }
585
586         backlight_class->dev_groups = bl_device_groups;
587         backlight_class->pm = &backlight_class_dev_pm_ops;
588         INIT_LIST_HEAD(&backlight_dev_list);
589         mutex_init(&backlight_dev_list_mutex);
590         BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier);
591
592         return 0;
593 }
594
595 /*
596  * if this is compiled into the kernel, we need to ensure that the
597  * class is registered before users of the class try to register lcd's
598  */
599 postcore_initcall(backlight_class_init);
600 module_exit(backlight_class_exit);
601
602 MODULE_LICENSE("GPL");
603 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
604 MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");