5dffc71c5125616b45170d3d14c0d08ceda44489
[linux-2.6.git] / drivers / staging / gma500 / psb_bl.c
1 /*
2  *  psb backlight interface
3  *
4  * Copyright (c) 2009, 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         DRM_DEBUG_DRIVER("backlight level set to %d\n", level);
60
61         /* Percentage 1-100% being valid */
62         if (level < 1)
63                 level = 1;
64
65         psb_intel_lvds_set_brightness(dev, level);
66         psb_brightness = level;
67         return 0;
68 }
69
70 int mrst_set_brightness(struct backlight_device *bd)
71 {
72         struct drm_device *dev = bl_get_data(psb_backlight_device);
73         struct drm_psb_private *dev_priv = dev->dev_private;
74         int level = bd->props.brightness;
75         u32 blc_pwm_ctl;
76         u32 max_pwm_blc;
77
78         DRM_DEBUG_DRIVER("backlight level set to %d\n", level);
79
80         /* Percentage 1-100% being valid */
81         if (level < 1)
82                 level = 1;
83
84         if (gma_power_begin(dev, 0)) {
85                 /* Calculate and set the brightness value */
86                 max_pwm_blc = REG_READ(BLC_PWM_CTL) >> 16;
87                 blc_pwm_ctl = level * max_pwm_blc / 100;
88
89                 /* Adjust the backlight level with the percent in
90                  * dev_priv->blc_adj1;
91                  */
92                 blc_pwm_ctl = blc_pwm_ctl * dev_priv->blc_adj1;
93                 blc_pwm_ctl = blc_pwm_ctl / 100;
94
95                 /* Adjust the backlight level with the percent in
96                  * dev_priv->blc_adj2;
97                  */
98                 blc_pwm_ctl = blc_pwm_ctl * dev_priv->blc_adj2;
99                 blc_pwm_ctl = blc_pwm_ctl / 100;
100
101                 if (blc_pol == BLC_POLARITY_INVERSE)
102                         blc_pwm_ctl = max_pwm_blc - blc_pwm_ctl;
103                 /* force PWM bit on */
104                 REG_WRITE(BLC_PWM_CTL2, (0x80000000 | REG_READ(BLC_PWM_CTL2)));
105                 REG_WRITE(BLC_PWM_CTL, (max_pwm_blc << 16) | blc_pwm_ctl);
106                 gma_power_end(dev);
107         }
108         psb_brightness = level;
109         return 0;
110 }
111
112 int psb_get_brightness(struct backlight_device *bd)
113 {
114         DRM_DEBUG_DRIVER("brightness = 0x%x\n", psb_brightness);
115
116         /* return locally cached var instead of HW read (due to DPST etc.) */
117         /* FIXME: ideally return actual value in case firmware fiddled with
118            it */
119         return psb_brightness;
120 }
121
122 static const struct backlight_ops psb_ops = {
123         .get_brightness = psb_get_brightness,
124         .update_status  = psb_set_brightness,
125 };
126
127 static int device_backlight_init(struct drm_device *dev)
128 {
129         struct drm_psb_private *dev_priv = dev->dev_private;
130         unsigned long core_clock;
131         /* u32 bl_max_freq; */
132         /* unsigned long value; */
133         u16 bl_max_freq;
134         uint32_t value;
135         uint32_t blc_pwm_precision_factor;
136
137         if (IS_MRST(dev)) {
138                 dev_priv->blc_adj1 = BLC_ADJUSTMENT_MAX;
139                 dev_priv->blc_adj2 = BLC_ADJUSTMENT_MAX;
140                 bl_max_freq = 256;
141                 /* this needs to be set elsewhere */
142                 blc_pol = BLC_POLARITY_NORMAL;
143                 blc_pwm_precision_factor = BLC_PWM_PRECISION_FACTOR;
144         } else {
145                 /* get bl_max_freq and pol from dev_priv*/
146                 if (!dev_priv->lvds_bl) {
147                         DRM_ERROR("Has no valid LVDS backlight info\n");
148                         return 1;
149                 }
150                 bl_max_freq = dev_priv->lvds_bl->freq;
151                 blc_pol = dev_priv->lvds_bl->pol;
152                 blc_pwm_precision_factor = PSB_BLC_PWM_PRECISION_FACTOR;
153                 blc_brightnesscmd = dev_priv->lvds_bl->brightnesscmd;
154                 blc_type = dev_priv->lvds_bl->type;
155         }
156
157         core_clock = dev_priv->core_freq;
158
159         value = (core_clock * MHz) / BLC_PWM_FREQ_CALC_CONSTANT;
160         value *= blc_pwm_precision_factor;
161         value /= bl_max_freq;
162         value /= blc_pwm_precision_factor;
163
164         if (gma_power_begin(dev, false)) {
165                 if (IS_MRST(dev)) {
166                         if (value > (unsigned long long)MRST_BLC_MAX_PWM_REG_FREQ)
167                                 return 2;
168                         else {
169                                 REG_WRITE(BLC_PWM_CTL2,
170                                         (0x80000000 | REG_READ(BLC_PWM_CTL2)));
171                                 REG_WRITE(BLC_PWM_CTL, value | (value << 16));
172                         }
173                 } else {
174                         if (value > (unsigned long long)PSB_BLC_MAX_PWM_REG_FREQ ||
175                          value < (unsigned long long)PSB_BLC_MIN_PWM_REG_FREQ)
176                                 return 2;
177                         else {
178                                 value &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
179                                 REG_WRITE(BLC_PWM_CTL,
180                                         (value << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
181                                         (value));
182                         }
183                 }
184                 gma_power_end(dev);
185         }
186         return 0;
187 }
188
189 int psb_backlight_init(struct drm_device *dev)
190 {
191 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
192         int ret = 0;
193
194         struct backlight_properties props;
195         memset(&props, 0, sizeof(struct backlight_properties));
196         props.max_brightness = 100;
197         props.type = BACKLIGHT_PLATFORM;
198
199         psb_backlight_device = backlight_device_register("psb-bl", NULL,
200                                                 (void *)dev, &psb_ops, &props);
201         if (IS_ERR(psb_backlight_device))
202                 return PTR_ERR(psb_backlight_device);
203
204         ret = device_backlight_init(dev);
205         if (ret < 0)
206                 return ret;
207
208         psb_backlight_device->props.brightness = 100;
209         psb_backlight_device->props.max_brightness = 100;
210         backlight_update_status(psb_backlight_device);
211 #endif
212         return 0;
213 }
214
215 void psb_backlight_exit(void)
216 {
217 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
218         psb_backlight_device->props.brightness = 0;
219         backlight_update_status(psb_backlight_device);
220         backlight_device_unregister(psb_backlight_device);
221 #endif
222 }
223
224 struct backlight_device *psb_get_backlight_device(void)
225 {
226         return psb_backlight_device;
227 }