ASoC: Fix Blackfin I2S _pointer() implementation return in bounds values
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / gma500 / psb_bl.c
blob70c17b352f9f3d9ff2946f621986b9c5107096a4
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_MAX_LEVEL 100
37 #define BRIGHTNESS_MASK 0xFF
38 #define BLC_POLARITY_NORMAL 0
39 #define BLC_POLARITY_INVERSE 1
40 #define BLC_ADJUSTMENT_MAX 100
42 #define PSB_BLC_PWM_PRECISION_FACTOR 10
43 #define PSB_BLC_MAX_PWM_REG_FREQ 0xFFFE
44 #define PSB_BLC_MIN_PWM_REG_FREQ 0x2
46 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
47 #define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
49 static int psb_brightness;
50 static struct backlight_device *psb_backlight_device;
51 static u8 blc_brightnesscmd;
52 static u8 blc_pol;
53 static u8 blc_type;
55 int psb_set_brightness(struct backlight_device *bd)
57 struct drm_device *dev = bl_get_data(psb_backlight_device);
58 int level = bd->props.brightness;
60 DRM_DEBUG_DRIVER("backlight level set to %d\n", level);
62 /* Perform value bounds checking */
63 if (level < BRIGHTNESS_MIN_LEVEL)
64 level = BRIGHTNESS_MIN_LEVEL;
66 psb_intel_lvds_set_brightness(dev, level);
67 psb_brightness = level;
68 return 0;
71 int psb_get_brightness(struct backlight_device *bd)
73 DRM_DEBUG_DRIVER("brightness = 0x%x\n", psb_brightness);
75 /* return locally cached var instead of HW read (due to DPST etc.) */
76 /* FIXME: ideally return actual value in case firmware fiddled with
77 it */
78 return psb_brightness;
81 static const struct backlight_ops psb_ops = {
82 .get_brightness = psb_get_brightness,
83 .update_status = psb_set_brightness,
86 static int device_backlight_init(struct drm_device *dev)
88 unsigned long core_clock;
89 /* u32 bl_max_freq; */
90 /* unsigned long value; */
91 u16 bl_max_freq;
92 uint32_t value;
93 uint32_t blc_pwm_precision_factor;
94 struct drm_psb_private *dev_priv = dev->dev_private;
96 /* get bl_max_freq and pol from dev_priv*/
97 if (!dev_priv->lvds_bl) {
98 DRM_ERROR("Has no valid LVDS backlight info\n");
99 return 1;
101 bl_max_freq = dev_priv->lvds_bl->freq;
102 blc_pol = dev_priv->lvds_bl->pol;
103 blc_pwm_precision_factor = PSB_BLC_PWM_PRECISION_FACTOR;
104 blc_brightnesscmd = dev_priv->lvds_bl->brightnesscmd;
105 blc_type = dev_priv->lvds_bl->type;
107 core_clock = dev_priv->core_freq;
109 value = (core_clock * MHz) / BLC_PWM_FREQ_CALC_CONSTANT;
110 value *= blc_pwm_precision_factor;
111 value /= bl_max_freq;
112 value /= blc_pwm_precision_factor;
114 if (ospm_power_using_hw_begin(OSPM_DISPLAY_ISLAND,
115 OSPM_UHB_ONLY_IF_ON)) {
116 /* Check: may be MFLD only */
117 if (
118 value > (unsigned long long)PSB_BLC_MAX_PWM_REG_FREQ ||
119 value < (unsigned long long)PSB_BLC_MIN_PWM_REG_FREQ)
120 return 2;
121 else {
122 value &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
123 REG_WRITE(BLC_PWM_CTL,
124 (value << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
125 (value));
127 ospm_power_using_hw_end(OSPM_DISPLAY_ISLAND);
129 return 0;
132 int psb_backlight_init(struct drm_device *dev)
134 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
135 int ret = 0;
137 struct backlight_properties props;
138 memset(&props, 0, sizeof(struct backlight_properties));
139 props.max_brightness = BRIGHTNESS_MAX_LEVEL;
141 psb_backlight_device = backlight_device_register("psb-bl", NULL,
142 (void *)dev, &psb_ops, &props);
143 if (IS_ERR(psb_backlight_device))
144 return PTR_ERR(psb_backlight_device);
146 ret = device_backlight_init(dev);
147 if (ret < 0)
148 return ret;
150 psb_backlight_device->props.brightness = BRIGHTNESS_MAX_LEVEL;
151 psb_backlight_device->props.max_brightness = BRIGHTNESS_MAX_LEVEL;
152 backlight_update_status(psb_backlight_device);
153 #endif
154 return 0;
157 void psb_backlight_exit(void)
159 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
160 psb_backlight_device->props.brightness = 0;
161 backlight_update_status(psb_backlight_device);
162 backlight_device_unregister(psb_backlight_device);
163 #endif
166 struct backlight_device *psb_get_backlight_device(void)
168 return psb_backlight_device;