gma500: Add the HDMI bits
[pandora-kernel.git] / drivers / staging / gma500 / cdv_device.c
1 /**************************************************************************
2  * Copyright (c) 2011, Intel Corporation.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  **************************************************************************/
19
20 #include <linux/backlight.h>
21 #include <drm/drmP.h>
22 #include <drm/drm.h>
23 #include "psb_drm.h"
24 #include "psb_drv.h"
25 #include "psb_reg.h"
26 #include "psb_intel_reg.h"
27 #include "intel_bios.h"
28 #include "cdv_device.h"
29
30 #define VGA_SR_INDEX            0x3c4
31 #define VGA_SR_DATA             0x3c5
32
33 /* FIXME: should check if we are the active VGA device ?? */
34 static void cdv_disable_vga(struct drm_device *dev)
35 {
36         u8 sr1;
37         u32 vga_reg;
38
39         vga_reg = VGACNTRL;
40
41         outb(1, VGA_SR_INDEX);
42         sr1 = inb(VGA_SR_DATA);
43         outb(sr1 | 1<<5, VGA_SR_DATA);
44         udelay(300);
45
46         REG_WRITE(vga_reg, VGA_DISP_DISABLE);
47         REG_READ(vga_reg);
48 }
49
50 static int cdv_output_init(struct drm_device *dev)
51 {
52         struct drm_psb_private *dev_priv = dev->dev_private;
53         cdv_disable_vga(dev);
54
55         cdv_intel_crt_init(dev, &dev_priv->mode_dev);
56         cdv_intel_lvds_init(dev, &dev_priv->mode_dev);
57
58         /* These bits indicate HDMI not SDVO on CDV, but we don't yet support
59            the HDMI interface */
60         if (REG_READ(SDVOB) & SDVO_DETECTED)
61                 cdv_hdmi_init(dev, &dev_priv->mode_dev, SDVOB);
62         if (REG_READ(SDVOC) & SDVO_DETECTED)
63                 cdv_hdmi_init(dev, &dev_priv->mode_dev, SDVOC);
64         return 0;
65 }
66
67 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
68
69 /*
70  *      Poulsbo Backlight Interfaces
71  */
72
73 #define BLC_PWM_PRECISION_FACTOR 100    /* 10000000 */
74 #define BLC_PWM_FREQ_CALC_CONSTANT 32
75 #define MHz 1000000
76
77 #define PSB_BLC_PWM_PRECISION_FACTOR    10
78 #define PSB_BLC_MAX_PWM_REG_FREQ        0xFFFE
79 #define PSB_BLC_MIN_PWM_REG_FREQ        0x2
80
81 #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
82 #define PSB_BACKLIGHT_PWM_CTL_SHIFT     (16)
83
84 static int cdv_brightness;
85 static struct backlight_device *cdv_backlight_device;
86
87 static int cdv_get_brightness(struct backlight_device *bd)
88 {
89         /* return locally cached var instead of HW read (due to DPST etc.) */
90         /* FIXME: ideally return actual value in case firmware fiddled with
91            it */
92         return cdv_brightness;
93 }
94
95
96 static int cdv_backlight_setup(struct drm_device *dev)
97 {
98         struct drm_psb_private *dev_priv = dev->dev_private;
99         unsigned long core_clock;
100         /* u32 bl_max_freq; */
101         /* unsigned long value; */
102         u16 bl_max_freq;
103         uint32_t value;
104         uint32_t blc_pwm_precision_factor;
105
106         /* get bl_max_freq and pol from dev_priv*/
107         if (!dev_priv->lvds_bl) {
108                 dev_err(dev->dev, "Has no valid LVDS backlight info\n");
109                 return -ENOENT;
110         }
111         bl_max_freq = dev_priv->lvds_bl->freq;
112         blc_pwm_precision_factor = PSB_BLC_PWM_PRECISION_FACTOR;
113
114         core_clock = dev_priv->core_freq;
115
116         value = (core_clock * MHz) / BLC_PWM_FREQ_CALC_CONSTANT;
117         value *= blc_pwm_precision_factor;
118         value /= bl_max_freq;
119         value /= blc_pwm_precision_factor;
120
121         if (value > (unsigned long long)PSB_BLC_MAX_PWM_REG_FREQ ||
122                  value < (unsigned long long)PSB_BLC_MIN_PWM_REG_FREQ)
123                                 return -ERANGE;
124         else {
125                 /* FIXME */
126         }
127         return 0;
128 }
129
130 static int cdv_set_brightness(struct backlight_device *bd)
131 {
132         struct drm_device *dev = bl_get_data(cdv_backlight_device);
133         int level = bd->props.brightness;
134
135         /* Percentage 1-100% being valid */
136         if (level < 1)
137                 level = 1;
138
139         /*cdv_intel_lvds_set_brightness(dev, level); FIXME */
140         cdv_brightness = level;
141         return 0;
142 }
143
144 static const struct backlight_ops cdv_ops = {
145         .get_brightness = cdv_get_brightness,
146         .update_status  = cdv_set_brightness,
147 };
148
149 static int cdv_backlight_init(struct drm_device *dev)
150 {
151         struct drm_psb_private *dev_priv = dev->dev_private;
152         int ret;
153         struct backlight_properties props;
154
155         memset(&props, 0, sizeof(struct backlight_properties));
156         props.max_brightness = 100;
157         props.type = BACKLIGHT_PLATFORM;
158
159         cdv_backlight_device = backlight_device_register("psb-bl",
160                                         NULL, (void *)dev, &cdv_ops, &props);
161         if (IS_ERR(cdv_backlight_device))
162                 return PTR_ERR(cdv_backlight_device);
163
164         ret = cdv_backlight_setup(dev);
165         if (ret < 0) {
166                 backlight_device_unregister(cdv_backlight_device);
167                 cdv_backlight_device = NULL;
168                 return ret;
169         }
170         cdv_backlight_device->props.brightness = 100;
171         cdv_backlight_device->props.max_brightness = 100;
172         backlight_update_status(cdv_backlight_device);
173         dev_priv->backlight_device = cdv_backlight_device;
174         return 0;
175 }
176
177 #endif
178
179 /*
180  *      Provide the Cedarview specific chip logic and low level methods
181  *      for power management
182  *
183  *      FIXME: we need to implement the apm/ospm base management bits
184  *      for this and the MID devices.
185  */
186
187 static inline u32 CDV_MSG_READ32(uint port, uint offset)
188 {
189         int mcr = (0x10<<24) | (port << 16) | (offset << 8);
190         uint32_t ret_val = 0;
191         struct pci_dev *pci_root = pci_get_bus_and_slot (0, 0);
192         pci_write_config_dword (pci_root, 0xD0, mcr);
193         pci_read_config_dword (pci_root, 0xD4, &ret_val);
194         pci_dev_put(pci_root);
195         return ret_val;
196 }
197
198 static inline void CDV_MSG_WRITE32(uint port, uint offset, u32 value)
199 {
200         int mcr = (0x11<<24) | (port << 16) | (offset << 8) | 0xF0;
201         struct pci_dev *pci_root = pci_get_bus_and_slot (0, 0);
202         pci_write_config_dword (pci_root, 0xD4, value);
203         pci_write_config_dword (pci_root, 0xD0, mcr);
204         pci_dev_put(pci_root);
205 }
206
207 #define PSB_APM_CMD                     0x0
208 #define PSB_APM_STS                     0x04
209 #define PSB_PM_SSC                      0x20
210 #define PSB_PM_SSS                      0x30
211 #define PSB_PWRGT_GFX_MASK              0x3
212 #define CDV_PWRGT_DISPLAY_CNTR          0x000fc00c
213 #define CDV_PWRGT_DISPLAY_STS           0x000fc00c
214
215 static void cdv_init_pm(struct drm_device *dev)
216 {
217         struct drm_psb_private *dev_priv = dev->dev_private;
218         u32 pwr_cnt;
219         int i;
220
221         dev_priv->apm_base = CDV_MSG_READ32(PSB_PUNIT_PORT, PSB_APMBA) & 0xFFFF;
222         dev_priv->ospm_base = CDV_MSG_READ32(PSB_PUNIT_PORT, PSB_OSPMBA) & 0xFFFF;
223
224         /* Force power on for now */
225         pwr_cnt = inl(dev_priv->apm_base + PSB_APM_CMD);
226         pwr_cnt &= ~PSB_PWRGT_GFX_MASK;
227
228         outl(pwr_cnt, dev_priv->apm_base + PSB_APM_CMD);
229         for (i = 0; i < 5; i++) {
230                 u32 pwr_sts = inl(dev_priv->apm_base + PSB_APM_STS);
231                 if ((pwr_sts & PSB_PWRGT_GFX_MASK) == 0)
232                         break;
233                 udelay(10);
234         }
235         pwr_cnt = inl(dev_priv->ospm_base + PSB_PM_SSC);
236         pwr_cnt &= ~CDV_PWRGT_DISPLAY_CNTR;
237         outl(pwr_cnt, dev_priv->ospm_base + PSB_PM_SSC);
238         for (i = 0; i < 5; i++) {
239                 u32 pwr_sts = inl(dev_priv->ospm_base + PSB_PM_SSS);
240                 if ((pwr_sts & CDV_PWRGT_DISPLAY_STS) == 0)
241                         break;
242                 udelay(10);
243         }
244 }
245
246 /**
247  *      cdv_save_display_registers      -       save registers lost on suspend
248  *      @dev: our DRM device
249  *
250  *      Save the state we need in order to be able to restore the interface
251  *      upon resume from suspend
252  *
253  *      FIXME: review
254  */
255 static int cdv_save_display_registers(struct drm_device *dev)
256 {
257         return 0;
258 }
259
260 /**
261  *      cdv_restore_display_registers   -       restore lost register state
262  *      @dev: our DRM device
263  *
264  *      Restore register state that was lost during suspend and resume.
265  *
266  *      FIXME: review
267  */
268 static int cdv_restore_display_registers(struct drm_device *dev)
269 {
270         return 0;
271 }
272
273 static int cdv_power_down(struct drm_device *dev)
274 {
275         return 0;
276 }
277
278 static int cdv_power_up(struct drm_device *dev)
279 {
280         return 0;
281 }
282
283 /* FIXME ? - shared with Poulsbo */
284 static void cdv_get_core_freq(struct drm_device *dev)
285 {
286         uint32_t clock;
287         struct pci_dev *pci_root = pci_get_bus_and_slot(0, 0);
288         struct drm_psb_private *dev_priv = dev->dev_private;
289
290         pci_write_config_dword(pci_root, 0xD0, 0xD0050300);
291         pci_read_config_dword(pci_root, 0xD4, &clock);
292         pci_dev_put(pci_root);
293
294         switch (clock & 0x07) {
295         case 0:
296                 dev_priv->core_freq = 100;
297                 break;
298         case 1:
299                 dev_priv->core_freq = 133;
300                 break;
301         case 2:
302                 dev_priv->core_freq = 150;
303                 break;
304         case 3:
305                 dev_priv->core_freq = 178;
306                 break;
307         case 4:
308                 dev_priv->core_freq = 200;
309                 break;
310         case 5:
311         case 6:
312         case 7:
313                 dev_priv->core_freq = 266;
314         default:
315                 dev_priv->core_freq = 0;
316         }
317 }
318
319 static int cdv_chip_setup(struct drm_device *dev)
320 {
321         cdv_get_core_freq(dev);
322         intel_opregion_init(dev);
323         psb_intel_init_bios(dev);
324         return 0;
325 }
326
327 /* CDV is much like Poulsbo but has MID like SGX offsets and PM */
328
329 const struct psb_ops cdv_chip_ops = {
330         .name = "Cedartrail",
331         .accel_2d = 0,
332         .pipes = 2,
333         .sgx_offset = MRST_SGX_OFFSET,
334         .chip_setup = cdv_chip_setup,
335
336         .crtc_helper = &cdv_intel_helper_funcs,
337         .crtc_funcs = &cdv_intel_crtc_funcs,
338
339         .output_init = cdv_output_init,
340
341 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
342         .backlight_init = cdv_backlight_init,
343 #endif
344
345         .init_pm = cdv_init_pm,
346         .save_regs = cdv_save_display_registers,
347         .restore_regs = cdv_restore_display_registers,
348         .power_down = cdv_power_down,
349         .power_up = cdv_power_up,       
350 };