gma500: backlight warning
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / gma500 / psb_bl.c
blob5dffc71c5125616b45170d3d14c0d08ceda44489
1 /*
2 * psb backlight interface
4 * Copyright (c) 2009, Intel Corporation.
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.
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.
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.
19 * Authors: Eric Knopp
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"
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
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
45 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
46 #define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
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;
54 int psb_set_brightness(struct backlight_device *bd)
56 struct drm_device *dev = bl_get_data(psb_backlight_device);
57 int level = bd->props.brightness;
59 DRM_DEBUG_DRIVER("backlight level set to %d\n", level);
61 /* Percentage 1-100% being valid */
62 if (level < 1)
63 level = 1;
65 psb_intel_lvds_set_brightness(dev, level);
66 psb_brightness = level;
67 return 0;
70 int mrst_set_brightness(struct backlight_device *bd)
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;
78 DRM_DEBUG_DRIVER("backlight level set to %d\n", level);
80 /* Percentage 1-100% being valid */
81 if (level < 1)
82 level = 1;
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;
89 /* Adjust the backlight level with the percent in
90 * dev_priv->blc_adj1;
92 blc_pwm_ctl = blc_pwm_ctl * dev_priv->blc_adj1;
93 blc_pwm_ctl = blc_pwm_ctl / 100;
95 /* Adjust the backlight level with the percent in
96 * dev_priv->blc_adj2;
98 blc_pwm_ctl = blc_pwm_ctl * dev_priv->blc_adj2;
99 blc_pwm_ctl = blc_pwm_ctl / 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);
108 psb_brightness = level;
109 return 0;
112 int psb_get_brightness(struct backlight_device *bd)
114 DRM_DEBUG_DRIVER("brightness = 0x%x\n", psb_brightness);
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;
122 static const struct backlight_ops psb_ops = {
123 .get_brightness = psb_get_brightness,
124 .update_status = psb_set_brightness,
127 static int device_backlight_init(struct drm_device *dev)
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;
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;
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;
157 core_clock = dev_priv->core_freq;
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;
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));
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));
184 gma_power_end(dev);
186 return 0;
189 int psb_backlight_init(struct drm_device *dev)
191 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
192 int ret = 0;
194 struct backlight_properties props;
195 memset(&props, 0, sizeof(struct backlight_properties));
196 props.max_brightness = 100;
197 props.type = BACKLIGHT_PLATFORM;
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);
204 ret = device_backlight_init(dev);
205 if (ret < 0)
206 return ret;
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;
215 void psb_backlight_exit(void)
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
224 struct backlight_device *psb_get_backlight_device(void)
226 return psb_backlight_device;