2 * GMA500 Backlight Interface
4 * Copyright (c) 2009-2011, 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
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.
23 #include <linux/backlight.h>
24 #include <linux/version.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
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
;
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 /* Percentage 1-100% being valid */
63 psb_intel_lvds_set_brightness(dev
, level
);
64 psb_brightness
= level
;
68 int mrst_set_brightness(struct backlight_device
*bd
)
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
;
76 /* Percentage 1-100% being valid */
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;
85 /* Adjust the backlight level with the percent in
88 blc_pwm_ctl
= blc_pwm_ctl
* dev_priv
->blc_adj1
;
89 blc_pwm_ctl
= blc_pwm_ctl
/ 100;
91 /* Adjust the backlight level with the percent in
94 blc_pwm_ctl
= blc_pwm_ctl
* dev_priv
->blc_adj2
;
95 blc_pwm_ctl
= blc_pwm_ctl
/ 100;
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
);
104 psb_brightness
= level
;
108 int mfld_set_brightness(struct backlight_device
*bd
)
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
;
114 DRM_DEBUG_DRIVER("backlight level set to %d\n", level
);
116 /* Percentage 1-100% being valid */
120 if (gma_power_begin(dev
, 0)) {
121 /* Calculate and set the brightness value */
124 /* Adjust the backlight level with the percent in
125 * dev_priv->blc_adj2;
127 adjusted_level
= level
* dev_priv
->blc_adj2
;
128 adjusted_level
= adjusted_level
/ BLC_ADJUSTMENT_MAX
;
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
);
137 mdfld_dsi_brightness_control(dev
, 0, adjusted_level
);
139 if ((dev_priv
->dbi_panel_on2
) || (dev_priv
->dpi_panel_on2
))
140 mdfld_dsi_brightness_control(dev
, 2, adjusted_level
);
144 psb_brightness
= level
;
148 int psb_get_brightness(struct backlight_device
*bd
)
150 /* return locally cached var instead of HW read (due to DPST etc.) */
151 /* FIXME: ideally return actual value in case firmware fiddled with
153 return psb_brightness
;
156 static const struct backlight_ops psb_ops
= {
157 .get_brightness
= psb_get_brightness
,
158 .update_status
= psb_set_brightness
,
161 static const struct backlight_ops mrst_ops
= {
162 .get_brightness
= psb_get_brightness
,
163 .update_status
= mrst_set_brightness
,
166 static const struct backlight_ops mfld_ops
= {
167 .get_brightness
= psb_get_brightness
,
168 .update_status
= mfld_set_brightness
,
171 static int device_backlight_init(struct drm_device
*dev
)
173 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
174 unsigned long core_clock
;
175 /* u32 bl_max_freq; */
176 /* unsigned long value; */
179 uint32_t blc_pwm_precision_factor
;
182 dev_priv
->blc_adj1
= BLC_ADJUSTMENT_MAX
;
183 dev_priv
->blc_adj2
= BLC_ADJUSTMENT_MAX
;
185 } else if (IS_MRST(dev
)) {
186 dev_priv
->blc_adj1
= BLC_ADJUSTMENT_MAX
;
187 dev_priv
->blc_adj2
= BLC_ADJUSTMENT_MAX
;
189 /* this needs to be set elsewhere */
190 blc_pol
= BLC_POLARITY_NORMAL
;
191 blc_pwm_precision_factor
= BLC_PWM_PRECISION_FACTOR
;
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");
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
;
205 core_clock
= dev_priv
->core_freq
;
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
;
212 if (gma_power_begin(dev
, false)) {
214 if (value
> (unsigned long long)MRST_BLC_MAX_PWM_REG_FREQ
)
217 REG_WRITE(BLC_PWM_CTL2
,
218 (0x80000000 | REG_READ(BLC_PWM_CTL2
)));
219 REG_WRITE(BLC_PWM_CTL
, value
| (value
<< 16));
222 if (value
> (unsigned long long)PSB_BLC_MAX_PWM_REG_FREQ
||
223 value
< (unsigned long long)PSB_BLC_MIN_PWM_REG_FREQ
)
226 value
&= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR
;
227 REG_WRITE(BLC_PWM_CTL
,
228 (value
<< PSB_BACKLIGHT_PWM_CTL_SHIFT
) |
237 int psb_backlight_init(struct drm_device
*dev
)
239 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
242 struct backlight_properties props
;
243 memset(&props
, 0, sizeof(struct backlight_properties
));
244 props
.max_brightness
= 100;
245 props
.type
= BACKLIGHT_PLATFORM
;
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
);
254 psb_backlight_device
= backlight_device_register("psb-bl",
255 NULL
, (void *)dev
, &psb_ops
, &props
);
257 if (IS_ERR(psb_backlight_device
))
258 return PTR_ERR(psb_backlight_device
);
260 ret
= device_backlight_init(dev
);
264 psb_backlight_device
->props
.brightness
= 100;
265 psb_backlight_device
->props
.max_brightness
= 100;
266 backlight_update_status(psb_backlight_device
);
271 void psb_backlight_exit(void)
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
);
280 struct backlight_device
*psb_get_backlight_device(void)
282 return psb_backlight_device
;