Merge branches 'doc', 'multitouch', 'upstream' and 'upstream-fixes' into for-linus
[pandora-kernel.git] / drivers / staging / gma500 / psb_intel_bios.c
1 /*
2  * Copyright (c) 2006 Intel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Authors:
18  *    Eric Anholt <eric@anholt.net>
19  *
20  */
21 #include <drm/drmP.h>
22 #include <drm/drm.h>
23 #include "psb_drm.h"
24 #include "psb_drv.h"
25 #include "psb_intel_drv.h"
26 #include "psb_intel_reg.h"
27 #include "psb_intel_bios.h"
28
29
30 static void *find_section(struct bdb_header *bdb, int section_id)
31 {
32         u8 *base = (u8 *)bdb;
33         int index = 0;
34         u16 total, current_size;
35         u8 current_id;
36
37         /* skip to first section */
38         index += bdb->header_size;
39         total = bdb->bdb_size;
40
41         /* walk the sections looking for section_id */
42         while (index < total) {
43                 current_id = *(base + index);
44                 index++;
45                 current_size = *((u16 *)(base + index));
46                 index += 2;
47                 if (current_id == section_id)
48                         return base + index;
49                 index += current_size;
50         }
51
52         return NULL;
53 }
54
55 static void fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
56                         struct lvds_dvo_timing *dvo_timing)
57 {
58         panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
59                 dvo_timing->hactive_lo;
60         panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
61                 ((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
62         panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
63                 dvo_timing->hsync_pulse_width;
64         panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
65                 ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
66
67         panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
68                 dvo_timing->vactive_lo;
69         panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
70                 dvo_timing->vsync_off;
71         panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
72                 dvo_timing->vsync_pulse_width;
73         panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
74                 ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
75         panel_fixed_mode->clock = dvo_timing->clock * 10;
76         panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
77
78         /* Some VBTs have bogus h/vtotal values */
79         if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
80                 panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
81         if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
82                 panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
83
84         drm_mode_set_name(panel_fixed_mode);
85 }
86
87 static void parse_backlight_data(struct drm_psb_private *dev_priv,
88                                 struct bdb_header *bdb)
89 {
90         struct bdb_lvds_backlight *vbt_lvds_bl = NULL;
91         struct bdb_lvds_backlight *lvds_bl;
92         u8 p_type = 0;
93         void *bl_start = NULL;
94         struct bdb_lvds_options *lvds_opts
95                                 = find_section(bdb, BDB_LVDS_OPTIONS);
96
97         dev_priv->lvds_bl = NULL;
98
99         if (lvds_opts) {
100                 DRM_DEBUG("lvds_options found at %p\n", lvds_opts);
101                 p_type = lvds_opts->panel_type;
102         } else {
103                 DRM_DEBUG("no lvds_options\n");
104                 return;
105         }
106
107         bl_start = find_section(bdb, BDB_LVDS_BACKLIGHT);
108         vbt_lvds_bl = (struct bdb_lvds_backlight *)(bl_start + 1) + p_type;
109
110         lvds_bl = kzalloc(sizeof(*vbt_lvds_bl), GFP_KERNEL);
111         if (!lvds_bl) {
112                 DRM_DEBUG("No memory\n");
113                 return;
114         }
115
116         memcpy(lvds_bl, vbt_lvds_bl, sizeof(*vbt_lvds_bl));
117
118         dev_priv->lvds_bl = lvds_bl;
119 }
120
121 /* Try to find integrated panel data */
122 static void parse_lfp_panel_data(struct drm_psb_private *dev_priv,
123                             struct bdb_header *bdb)
124 {
125         struct bdb_lvds_options *lvds_options;
126         struct bdb_lvds_lfp_data *lvds_lfp_data;
127         struct bdb_lvds_lfp_data_entry *entry;
128         struct lvds_dvo_timing *dvo_timing;
129         struct drm_display_mode *panel_fixed_mode;
130
131         /* Defaults if we can't find VBT info */
132         dev_priv->lvds_dither = 0;
133         dev_priv->lvds_vbt = 0;
134
135         lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
136         if (!lvds_options)
137                 return;
138
139         dev_priv->lvds_dither = lvds_options->pixel_dither;
140         if (lvds_options->panel_type == 0xff)
141                 return;
142
143         lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
144         if (!lvds_lfp_data)
145                 return;
146
147         dev_priv->lvds_vbt = 1;
148
149         entry = &lvds_lfp_data->data[lvds_options->panel_type];
150         dvo_timing = &entry->dvo_timing;
151
152         panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode),
153                                       GFP_KERNEL);
154
155         fill_detail_timing_data(panel_fixed_mode, dvo_timing);
156
157         dev_priv->lfp_lvds_vbt_mode = panel_fixed_mode;
158
159         DRM_DEBUG("Found panel mode in BIOS VBT tables:\n");
160         drm_mode_debug_printmodeline(panel_fixed_mode);
161
162         return;
163 }
164
165 /* Try to find sdvo panel data */
166 static void parse_sdvo_panel_data(struct drm_psb_private *dev_priv,
167                       struct bdb_header *bdb)
168 {
169         struct bdb_sdvo_lvds_options *sdvo_lvds_options;
170         struct lvds_dvo_timing *dvo_timing;
171         struct drm_display_mode *panel_fixed_mode;
172
173         dev_priv->sdvo_lvds_vbt_mode = NULL;
174
175         sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
176         if (!sdvo_lvds_options)
177                 return;
178
179         dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
180         if (!dvo_timing)
181                 return;
182
183         panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
184
185         if (!panel_fixed_mode)
186                 return;
187
188         fill_detail_timing_data(panel_fixed_mode,
189                         dvo_timing + sdvo_lvds_options->panel_type);
190
191         dev_priv->sdvo_lvds_vbt_mode = panel_fixed_mode;
192
193         return;
194 }
195
196 static void parse_general_features(struct drm_psb_private *dev_priv,
197                        struct bdb_header *bdb)
198 {
199         struct bdb_general_features *general;
200
201         /* Set sensible defaults in case we can't find the general block */
202         dev_priv->int_tv_support = 1;
203         dev_priv->int_crt_support = 1;
204
205         general = find_section(bdb, BDB_GENERAL_FEATURES);
206         if (general) {
207                 dev_priv->int_tv_support = general->int_tv_support;
208                 dev_priv->int_crt_support = general->int_crt_support;
209                 dev_priv->lvds_use_ssc = general->enable_ssc;
210
211                 if (dev_priv->lvds_use_ssc) {
212                         dev_priv->lvds_ssc_freq
213                                 = general->ssc_freq ? 100 : 96;
214                 }
215         }
216 }
217
218 /**
219  * psb_intel_init_bios - initialize VBIOS settings & find VBT
220  * @dev: DRM device
221  *
222  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
223  * to appropriate values.
224  *
225  * VBT existence is a sanity check that is relied on by other i830_bios.c code.
226  * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
227  * feed an updated VBT back through that, compared to what we'll fetch using
228  * this method of groping around in the BIOS data.
229  *
230  * Returns 0 on success, nonzero on failure.
231  */
232 bool psb_intel_init_bios(struct drm_device *dev)
233 {
234         struct drm_psb_private *dev_priv = dev->dev_private;
235         struct pci_dev *pdev = dev->pdev;
236         struct vbt_header *vbt = NULL;
237         struct bdb_header *bdb;
238         u8 __iomem *bios;
239         size_t size;
240         int i;
241
242         bios = pci_map_rom(pdev, &size);
243         if (!bios)
244                 return -1;
245
246         /* Scour memory looking for the VBT signature */
247         for (i = 0; i + 4 < size; i++) {
248                 if (!memcmp(bios + i, "$VBT", 4)) {
249                         vbt = (struct vbt_header *)(bios + i);
250                         break;
251                 }
252         }
253
254         if (!vbt) {
255                 DRM_ERROR("VBT signature missing\n");
256                 pci_unmap_rom(pdev, bios);
257                 return -1;
258         }
259
260         bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
261
262         /* Grab useful general definitions */
263         parse_general_features(dev_priv, bdb);
264         parse_lfp_panel_data(dev_priv, bdb);
265         parse_sdvo_panel_data(dev_priv, bdb);
266         parse_backlight_data(dev_priv, bdb);
267
268         pci_unmap_rom(pdev, bios);
269
270         return 0;
271 }
272
273 /**
274  * Destroy and free VBT data
275  */
276 void psb_intel_destroy_bios(struct drm_device *dev)
277 {
278         struct drm_psb_private *dev_priv = dev->dev_private;
279         struct drm_display_mode *sdvo_lvds_vbt_mode =
280                                 dev_priv->sdvo_lvds_vbt_mode;
281         struct drm_display_mode *lfp_lvds_vbt_mode =
282                                 dev_priv->lfp_lvds_vbt_mode;
283         struct bdb_lvds_backlight *lvds_bl =
284                                 dev_priv->lvds_bl;
285
286         /*free sdvo panel mode*/
287         if (sdvo_lvds_vbt_mode) {
288                 dev_priv->sdvo_lvds_vbt_mode = NULL;
289                 kfree(sdvo_lvds_vbt_mode);
290         }
291
292         if (lfp_lvds_vbt_mode) {
293                 dev_priv->lfp_lvds_vbt_mode = NULL;
294                 kfree(lfp_lvds_vbt_mode);
295         }
296
297         if (lvds_bl) {
298                 dev_priv->lvds_bl = NULL;
299                 kfree(lvds_bl);
300         }
301 }