Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/bp/bp
[firefly-linux-kernel-4.4.55.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_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
41
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
45
46 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
47 #define PSB_BACKLIGHT_PWM_CTL_SHIFT     (16)
48
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;
54
55 int psb_set_brightness(struct backlight_device *bd)
56 {
57         struct drm_device *dev = bl_get_data(psb_backlight_device);
58         int level = bd->props.brightness;
59
60         DRM_DEBUG_DRIVER("backlight level set to %d\n", level);
61
62         /* Perform value bounds checking */
63         if (level < BRIGHTNESS_MIN_LEVEL)
64                 level = BRIGHTNESS_MIN_LEVEL;
65
66         psb_intel_lvds_set_brightness(dev, level);
67         psb_brightness = level;
68         return 0;
69 }
70
71 int psb_get_brightness(struct backlight_device *bd)
72 {
73         DRM_DEBUG_DRIVER("brightness = 0x%x\n", psb_brightness);
74
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;
79 }
80
81 static const struct backlight_ops psb_ops = {
82         .get_brightness = psb_get_brightness,
83         .update_status  = psb_set_brightness,
84 };
85
86 static int device_backlight_init(struct drm_device *dev)
87 {
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;
95
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;
100         }
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;
106
107         core_clock = dev_priv->core_freq;
108
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;
113
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));
126                 }
127                 ospm_power_using_hw_end(OSPM_DISPLAY_ISLAND);
128         }
129         return 0;
130 }
131
132 int psb_backlight_init(struct drm_device *dev)
133 {
134 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
135         int ret = 0;
136
137         struct backlight_properties props;
138         memset(&props, 0, sizeof(struct backlight_properties));
139         props.max_brightness = BRIGHTNESS_MAX_LEVEL;
140
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);
145
146         ret = device_backlight_init(dev);
147         if (ret < 0)
148                 return ret;
149
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;
155 }
156
157 void psb_backlight_exit(void)
158 {
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
164 }
165
166 struct backlight_device *psb_get_backlight_device(void)
167 {
168         return psb_backlight_device;
169 }