Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[firefly-linux-kernel-4.4.55.git] / drivers / video / backlight / generic_bl.c
1 /*
2  *  Generic Backlight Driver
3  *
4  *  Copyright (c) 2004-2008 Richard Purdie
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/platform_device.h>
18 #include <linux/mutex.h>
19 #include <linux/fb.h>
20 #include <linux/backlight.h>
21
22 static int genericbl_intensity;
23 static struct backlight_device *generic_backlight_device;
24 static struct generic_bl_info *bl_machinfo;
25
26 /* Flag to signal when the battery is low */
27 #define GENERICBL_BATTLOW       BL_CORE_DRIVER1
28
29 static int genericbl_send_intensity(struct backlight_device *bd)
30 {
31         int intensity = bd->props.brightness;
32
33         if (bd->props.power != FB_BLANK_UNBLANK)
34                 intensity = 0;
35         if (bd->props.state & BL_CORE_FBBLANK)
36                 intensity = 0;
37         if (bd->props.state & BL_CORE_SUSPENDED)
38                 intensity = 0;
39         if (bd->props.state & GENERICBL_BATTLOW)
40                 intensity &= bl_machinfo->limit_mask;
41
42         bl_machinfo->set_bl_intensity(intensity);
43
44         genericbl_intensity = intensity;
45
46         if (bl_machinfo->kick_battery)
47                 bl_machinfo->kick_battery();
48
49         return 0;
50 }
51
52 static int genericbl_get_intensity(struct backlight_device *bd)
53 {
54         return genericbl_intensity;
55 }
56
57 /*
58  * Called when the battery is low to limit the backlight intensity.
59  * If limit==0 clear any limit, otherwise limit the intensity
60  */
61 void genericbl_limit_intensity(int limit)
62 {
63         struct backlight_device *bd = generic_backlight_device;
64
65         mutex_lock(&bd->ops_lock);
66         if (limit)
67                 bd->props.state |= GENERICBL_BATTLOW;
68         else
69                 bd->props.state &= ~GENERICBL_BATTLOW;
70         backlight_update_status(generic_backlight_device);
71         mutex_unlock(&bd->ops_lock);
72 }
73 EXPORT_SYMBOL(genericbl_limit_intensity);
74
75 static const struct backlight_ops genericbl_ops = {
76         .options = BL_CORE_SUSPENDRESUME,
77         .get_brightness = genericbl_get_intensity,
78         .update_status  = genericbl_send_intensity,
79 };
80
81 static int genericbl_probe(struct platform_device *pdev)
82 {
83         struct backlight_properties props;
84         struct generic_bl_info *machinfo = pdev->dev.platform_data;
85         const char *name = "generic-bl";
86         struct backlight_device *bd;
87
88         bl_machinfo = machinfo;
89         if (!machinfo->limit_mask)
90                 machinfo->limit_mask = -1;
91
92         if (machinfo->name)
93                 name = machinfo->name;
94
95         memset(&props, 0, sizeof(struct backlight_properties));
96         props.type = BACKLIGHT_RAW;
97         props.max_brightness = machinfo->max_intensity;
98         bd = backlight_device_register(name, &pdev->dev, NULL, &genericbl_ops,
99                                        &props);
100         if (IS_ERR (bd))
101                 return PTR_ERR (bd);
102
103         platform_set_drvdata(pdev, bd);
104
105         bd->props.power = FB_BLANK_UNBLANK;
106         bd->props.brightness = machinfo->default_intensity;
107         backlight_update_status(bd);
108
109         generic_backlight_device = bd;
110
111         pr_info("Generic Backlight Driver Initialized.\n");
112         return 0;
113 }
114
115 static int genericbl_remove(struct platform_device *pdev)
116 {
117         struct backlight_device *bd = platform_get_drvdata(pdev);
118
119         bd->props.power = 0;
120         bd->props.brightness = 0;
121         backlight_update_status(bd);
122
123         backlight_device_unregister(bd);
124
125         pr_info("Generic Backlight Driver Unloaded\n");
126         return 0;
127 }
128
129 static struct platform_driver genericbl_driver = {
130         .probe          = genericbl_probe,
131         .remove         = genericbl_remove,
132         .driver         = {
133                 .name   = "generic-bl",
134         },
135 };
136
137 module_platform_driver(genericbl_driver);
138
139 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
140 MODULE_DESCRIPTION("Generic Backlight Driver");
141 MODULE_LICENSE("GPL");