gma500: Medfield support
[pandora-kernel.git] / drivers / staging / gma500 / psb_bl.c
1 /*
2  * GMA500 Backlight Interface
3  *
4  * Copyright (c) 2009-2011, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Authors: Eric Knopp
20  *
21  */
22
23 #include <linux/backlight.h>
24 #include <linux/version.h>
25 #include "psb_drv.h"
26 #include "psb_intel_reg.h"
27 #include "psb_intel_drv.h"
28 #include "psb_intel_bios.h"
29 #include "psb_powermgmt.h"
30
31 #define MRST_BLC_MAX_PWM_REG_FREQ           0xFFFF
32 #define BLC_PWM_PRECISION_FACTOR 100    /* 10000000 */
33 #define BLC_PWM_FREQ_CALC_CONSTANT 32
34 #define MHz 1000000
35 #define BRIGHTNESS_MIN_LEVEL 1
36 #define BRIGHTNESS_MASK 0xFF
37 #define BLC_POLARITY_NORMAL 0
38 #define BLC_POLARITY_INVERSE 1
39 #define BLC_ADJUSTMENT_MAX 100
40
41 #define PSB_BLC_PWM_PRECISION_FACTOR    10
42 #define PSB_BLC_MAX_PWM_REG_FREQ        0xFFFE
43 #define PSB_BLC_MIN_PWM_REG_FREQ        0x2
44
45 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
46 #define PSB_BACKLIGHT_PWM_CTL_SHIFT     (16)
47
48 static int psb_brightness;
49 static struct backlight_device *psb_backlight_device;
50 static u8 blc_brightnesscmd;
51 static u8 blc_pol;
52 static u8 blc_type;
53
54 int psb_set_brightness(struct backlight_device *bd)
55 {
56         struct drm_device *dev = bl_get_data(psb_backlight_device);
57         int level = bd->props.brightness;
58
59         /* Percentage 1-100% being valid */
60         if (level < 1)
61                 level = 1;
62
63         psb_intel_lvds_set_brightness(dev, level);
64         psb_brightness = level;
65         return 0;
66 }
67
68 int mrst_set_brightness(struct backlight_device *bd)
69 {
70         struct drm_device *dev = bl_get_data(psb_backlight_device);
71         struct drm_psb_private *dev_priv = dev->dev_private;
72         int level = bd->props.brightness;
73         u32 blc_pwm_ctl;
74         u32 max_pwm_blc;
75
76         /* Percentage 1-100% being valid */
77         if (level < 1)
78                 level = 1;
79
80         if (gma_power_begin(dev, 0)) {
81                 /* Calculate and set the brightness value */
82                 max_pwm_blc = REG_READ(BLC_PWM_CTL) >> 16;
83                 blc_pwm_ctl = level * max_pwm_blc / 100;
84
85                 /* Adjust the backlight level with the percent in
86                  * dev_priv->blc_adj1;
87                  */
88                 blc_pwm_ctl = blc_pwm_ctl * dev_priv->blc_adj1;
89                 blc_pwm_ctl = blc_pwm_ctl / 100;
90
91                 /* Adjust the backlight level with the percent in
92                  * dev_priv->blc_adj2;
93                  */
94                 blc_pwm_ctl = blc_pwm_ctl * dev_priv->blc_adj2;
95                 blc_pwm_ctl = blc_pwm_ctl / 100;
96
97                 if (blc_pol == BLC_POLARITY_INVERSE)
98                         blc_pwm_ctl = max_pwm_blc - blc_pwm_ctl;
99                 /* force PWM bit on */
100                 REG_WRITE(BLC_PWM_CTL2, (0x80000000 | REG_READ(BLC_PWM_CTL2)));
101                 REG_WRITE(BLC_PWM_CTL, (max_pwm_blc << 16) | blc_pwm_ctl);
102                 gma_power_end(dev);
103         }
104         psb_brightness = level;
105         return 0;
106 }
107
108 int mfld_set_brightness(struct backlight_device *bd)
109 {
110         struct drm_device *dev = bl_get_data(psb_backlight_device);
111         struct drm_psb_private *dev_priv = dev->dev_private;
112         int level = bd->props.brightness;
113
114         DRM_DEBUG_DRIVER("backlight level set to %d\n", level);
115
116         /* Percentage 1-100% being valid */
117         if (level < 1)
118                 level = 1;
119
120         if (gma_power_begin(dev, 0)) {
121                 /* Calculate and set the brightness value */
122                 u32 adjusted_level;
123
124                 /* Adjust the backlight level with the percent in
125                  * dev_priv->blc_adj2;
126                  */
127                 adjusted_level = level * dev_priv->blc_adj2;
128                 adjusted_level = adjusted_level / BLC_ADJUSTMENT_MAX;
129 #if 0
130 #ifndef CONFIG_MDFLD_DSI_DPU
131                 if(!(dev_priv->dsr_fb_update & MDFLD_DSR_MIPI_CONTROL) && 
132                         (dev_priv->dbi_panel_on || dev_priv->dbi_panel_on2)){
133                         mdfld_dsi_dbi_exit_dsr(dev,MDFLD_DSR_MIPI_CONTROL, 0, 0);
134                         dev_dbg(dev->dev, "Out of DSR before set brightness to %d.\n",adjusted_level);
135                 }
136 #endif
137                 mdfld_dsi_brightness_control(dev, 0, adjusted_level);
138
139                 if ((dev_priv->dbi_panel_on2) || (dev_priv->dpi_panel_on2))
140                         mdfld_dsi_brightness_control(dev, 2, adjusted_level);
141 #endif
142                 gma_power_end(dev);
143         }
144         psb_brightness = level;
145         return 0;
146 }
147
148 int psb_get_brightness(struct backlight_device *bd)
149 {
150         /* return locally cached var instead of HW read (due to DPST etc.) */
151         /* FIXME: ideally return actual value in case firmware fiddled with
152            it */
153         return psb_brightness;
154 }
155
156 static const struct backlight_ops psb_ops = {
157         .get_brightness = psb_get_brightness,
158         .update_status  = psb_set_brightness,
159 };
160
161 static const struct backlight_ops mrst_ops = {
162         .get_brightness = psb_get_brightness,
163         .update_status  = mrst_set_brightness,
164 };
165
166 static const struct backlight_ops mfld_ops = {
167         .get_brightness = psb_get_brightness,
168         .update_status  = mfld_set_brightness,
169 };
170
171 static int device_backlight_init(struct drm_device *dev)
172 {
173         struct drm_psb_private *dev_priv = dev->dev_private;
174         unsigned long core_clock;
175         /* u32 bl_max_freq; */
176         /* unsigned long value; */
177         u16 bl_max_freq;
178         uint32_t value;
179         uint32_t blc_pwm_precision_factor;
180
181         if (IS_MFLD(dev)) {
182                 dev_priv->blc_adj1 = BLC_ADJUSTMENT_MAX;
183                 dev_priv->blc_adj2 = BLC_ADJUSTMENT_MAX;
184                 return 0;
185         } else if (IS_MRST(dev)) {
186                 dev_priv->blc_adj1 = BLC_ADJUSTMENT_MAX;
187                 dev_priv->blc_adj2 = BLC_ADJUSTMENT_MAX;
188                 bl_max_freq = 256;
189                 /* this needs to be set elsewhere */
190                 blc_pol = BLC_POLARITY_NORMAL;
191                 blc_pwm_precision_factor = BLC_PWM_PRECISION_FACTOR;
192         } else {
193                 /* get bl_max_freq and pol from dev_priv*/
194                 if (!dev_priv->lvds_bl) {
195                         dev_err(dev->dev, "Has no valid LVDS backlight info\n");
196                         return 1;
197                 }
198                 bl_max_freq = dev_priv->lvds_bl->freq;
199                 blc_pol = dev_priv->lvds_bl->pol;
200                 blc_pwm_precision_factor = PSB_BLC_PWM_PRECISION_FACTOR;
201                 blc_brightnesscmd = dev_priv->lvds_bl->brightnesscmd;
202                 blc_type = dev_priv->lvds_bl->type;
203         }
204
205         core_clock = dev_priv->core_freq;
206
207         value = (core_clock * MHz) / BLC_PWM_FREQ_CALC_CONSTANT;
208         value *= blc_pwm_precision_factor;
209         value /= bl_max_freq;
210         value /= blc_pwm_precision_factor;
211
212         if (gma_power_begin(dev, false)) {
213                 if (IS_MRST(dev)) {
214                         if (value > (unsigned long long)MRST_BLC_MAX_PWM_REG_FREQ)
215                                 return 2;
216                         else {
217                                 REG_WRITE(BLC_PWM_CTL2,
218                                         (0x80000000 | REG_READ(BLC_PWM_CTL2)));
219                                 REG_WRITE(BLC_PWM_CTL, value | (value << 16));
220                         }
221                 } else {
222                         if (value > (unsigned long long)PSB_BLC_MAX_PWM_REG_FREQ ||
223                          value < (unsigned long long)PSB_BLC_MIN_PWM_REG_FREQ)
224                                 return 2;
225                         else {
226                                 value &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
227                                 REG_WRITE(BLC_PWM_CTL,
228                                         (value << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
229                                         (value));
230                         }
231                 }
232                 gma_power_end(dev);
233         }
234         return 0;
235 }
236
237 int psb_backlight_init(struct drm_device *dev)
238 {
239 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
240         int ret = 0;
241
242         struct backlight_properties props;
243         memset(&props, 0, sizeof(struct backlight_properties));
244         props.max_brightness = 100;
245         props.type = BACKLIGHT_PLATFORM;
246
247         if (IS_MFLD(dev))
248                 psb_backlight_device = backlight_device_register("mfld-bl",
249                                         NULL, (void *)dev, &mfld_ops, &props);
250         else if (IS_MRST(dev))
251                 psb_backlight_device = backlight_device_register("mrst-bl",
252                                         NULL, (void *)dev, &psb_ops, &props);
253         else
254                 psb_backlight_device = backlight_device_register("psb-bl",
255                                         NULL, (void *)dev, &psb_ops, &props);
256                                         
257         if (IS_ERR(psb_backlight_device))
258                 return PTR_ERR(psb_backlight_device);
259
260         ret = device_backlight_init(dev);
261         if (ret < 0)
262                 return ret;
263
264         psb_backlight_device->props.brightness = 100;
265         psb_backlight_device->props.max_brightness = 100;
266         backlight_update_status(psb_backlight_device);
267 #endif
268         return 0;
269 }
270
271 void psb_backlight_exit(void)
272 {
273 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
274         psb_backlight_device->props.brightness = 0;
275         backlight_update_status(psb_backlight_device);
276         backlight_device_unregister(psb_backlight_device);
277 #endif
278 }
279
280 struct backlight_device *psb_get_backlight_device(void)
281 {
282         return psb_backlight_device;
283 }