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 / omap1_bl.c
1 /*
2  * Backlight driver for OMAP based boards.
3  *
4  * Copyright (c) 2006 Andrzej Zaborowski  <balrog@zabor.org>
5  *
6  * This package is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This package is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/platform_device.h>
27 #include <linux/fb.h>
28 #include <linux/backlight.h>
29 #include <linux/slab.h>
30
31 #include <mach/hardware.h>
32 #include <plat/board.h>
33 #include <plat/mux.h>
34
35 #define OMAPBL_MAX_INTENSITY            0xff
36
37 struct omap_backlight {
38         int powermode;
39         int current_intensity;
40
41         struct device *dev;
42         struct omap_backlight_config *pdata;
43 };
44
45 static void inline omapbl_send_intensity(int intensity)
46 {
47         omap_writeb(intensity, OMAP_PWL_ENABLE);
48 }
49
50 static void inline omapbl_send_enable(int enable)
51 {
52         omap_writeb(enable, OMAP_PWL_CLK_ENABLE);
53 }
54
55 static void omapbl_blank(struct omap_backlight *bl, int mode)
56 {
57         if (bl->pdata->set_power)
58                 bl->pdata->set_power(bl->dev, mode);
59
60         switch (mode) {
61         case FB_BLANK_NORMAL:
62         case FB_BLANK_VSYNC_SUSPEND:
63         case FB_BLANK_HSYNC_SUSPEND:
64         case FB_BLANK_POWERDOWN:
65                 omapbl_send_intensity(0);
66                 omapbl_send_enable(0);
67                 break;
68
69         case FB_BLANK_UNBLANK:
70                 omapbl_send_intensity(bl->current_intensity);
71                 omapbl_send_enable(1);
72                 break;
73         }
74 }
75
76 #ifdef CONFIG_PM
77 static int omapbl_suspend(struct platform_device *pdev, pm_message_t state)
78 {
79         struct backlight_device *dev = platform_get_drvdata(pdev);
80         struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
81
82         omapbl_blank(bl, FB_BLANK_POWERDOWN);
83         return 0;
84 }
85
86 static int omapbl_resume(struct platform_device *pdev)
87 {
88         struct backlight_device *dev = platform_get_drvdata(pdev);
89         struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
90
91         omapbl_blank(bl, bl->powermode);
92         return 0;
93 }
94 #else
95 #define omapbl_suspend  NULL
96 #define omapbl_resume   NULL
97 #endif
98
99 static int omapbl_set_power(struct backlight_device *dev, int state)
100 {
101         struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
102
103         omapbl_blank(bl, state);
104         bl->powermode = state;
105
106         return 0;
107 }
108
109 static int omapbl_update_status(struct backlight_device *dev)
110 {
111         struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
112
113         if (bl->current_intensity != dev->props.brightness) {
114                 if (bl->powermode == FB_BLANK_UNBLANK)
115                         omapbl_send_intensity(dev->props.brightness);
116                 bl->current_intensity = dev->props.brightness;
117         }
118
119         if (dev->props.fb_blank != bl->powermode)
120                 omapbl_set_power(dev, dev->props.fb_blank);
121
122         return 0;
123 }
124
125 static int omapbl_get_intensity(struct backlight_device *dev)
126 {
127         struct omap_backlight *bl = dev_get_drvdata(&dev->dev);
128         return bl->current_intensity;
129 }
130
131 static const struct backlight_ops omapbl_ops = {
132         .get_brightness = omapbl_get_intensity,
133         .update_status  = omapbl_update_status,
134 };
135
136 static int omapbl_probe(struct platform_device *pdev)
137 {
138         struct backlight_properties props;
139         struct backlight_device *dev;
140         struct omap_backlight *bl;
141         struct omap_backlight_config *pdata = pdev->dev.platform_data;
142
143         if (!pdata)
144                 return -ENXIO;
145
146         bl = devm_kzalloc(&pdev->dev, sizeof(struct omap_backlight),
147                           GFP_KERNEL);
148         if (unlikely(!bl))
149                 return -ENOMEM;
150
151         memset(&props, 0, sizeof(struct backlight_properties));
152         props.type = BACKLIGHT_RAW;
153         props.max_brightness = OMAPBL_MAX_INTENSITY;
154         dev = backlight_device_register("omap-bl", &pdev->dev, bl, &omapbl_ops,
155                                         &props);
156         if (IS_ERR(dev))
157                 return PTR_ERR(dev);
158
159         bl->powermode = FB_BLANK_POWERDOWN;
160         bl->current_intensity = 0;
161
162         bl->pdata = pdata;
163         bl->dev = &pdev->dev;
164
165         platform_set_drvdata(pdev, dev);
166
167         omap_cfg_reg(PWL);      /* Conflicts with UART3 */
168
169         dev->props.fb_blank = FB_BLANK_UNBLANK;
170         dev->props.brightness = pdata->default_intensity;
171         omapbl_update_status(dev);
172
173         pr_info("OMAP LCD backlight initialised\n");
174
175         return 0;
176 }
177
178 static int omapbl_remove(struct platform_device *pdev)
179 {
180         struct backlight_device *dev = platform_get_drvdata(pdev);
181
182         backlight_device_unregister(dev);
183
184         return 0;
185 }
186
187 static struct platform_driver omapbl_driver = {
188         .probe          = omapbl_probe,
189         .remove         = omapbl_remove,
190         .suspend        = omapbl_suspend,
191         .resume         = omapbl_resume,
192         .driver         = {
193                 .name   = "omap-bl",
194         },
195 };
196
197 module_platform_driver(omapbl_driver);
198
199 MODULE_AUTHOR("Andrzej Zaborowski <balrog@zabor.org>");
200 MODULE_DESCRIPTION("OMAP LCD Backlight driver");
201 MODULE_LICENSE("GPL");