Merge branch 'tty-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[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         if (panel_fixed_mode->htotal > 0 && panel_fixed_mode->vtotal > 0) {
158                 dev_priv->lfp_lvds_vbt_mode = panel_fixed_mode;
159                 DRM_DEBUG("Found panel mode in BIOS VBT tables:\n");
160                 drm_mode_debug_printmodeline(panel_fixed_mode);
161         } else {
162                 DRM_DEBUG("Ignoring bogus LVDS VBT mode.\n");
163                 dev_priv->lvds_vbt = 0;
164                 kfree(panel_fixed_mode);
165         }
166
167         return;
168 }
169
170 /* Try to find sdvo panel data */
171 static void parse_sdvo_panel_data(struct drm_psb_private *dev_priv,
172                       struct bdb_header *bdb)
173 {
174         struct bdb_sdvo_lvds_options *sdvo_lvds_options;
175         struct lvds_dvo_timing *dvo_timing;
176         struct drm_display_mode *panel_fixed_mode;
177
178         dev_priv->sdvo_lvds_vbt_mode = NULL;
179
180         sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
181         if (!sdvo_lvds_options)
182                 return;
183
184         dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
185         if (!dvo_timing)
186                 return;
187
188         panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
189
190         if (!panel_fixed_mode)
191                 return;
192
193         fill_detail_timing_data(panel_fixed_mode,
194                         dvo_timing + sdvo_lvds_options->panel_type);
195
196         dev_priv->sdvo_lvds_vbt_mode = panel_fixed_mode;
197
198         return;
199 }
200
201 static void parse_general_features(struct drm_psb_private *dev_priv,
202                        struct bdb_header *bdb)
203 {
204         struct bdb_general_features *general;
205
206         /* Set sensible defaults in case we can't find the general block */
207         dev_priv->int_tv_support = 1;
208         dev_priv->int_crt_support = 1;
209
210         general = find_section(bdb, BDB_GENERAL_FEATURES);
211         if (general) {
212                 dev_priv->int_tv_support = general->int_tv_support;
213                 dev_priv->int_crt_support = general->int_crt_support;
214                 dev_priv->lvds_use_ssc = general->enable_ssc;
215
216                 if (dev_priv->lvds_use_ssc) {
217                         dev_priv->lvds_ssc_freq
218                                 = general->ssc_freq ? 100 : 96;
219                 }
220         }
221 }
222
223 /**
224  * psb_intel_init_bios - initialize VBIOS settings & find VBT
225  * @dev: DRM device
226  *
227  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
228  * to appropriate values.
229  *
230  * VBT existence is a sanity check that is relied on by other i830_bios.c code.
231  * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
232  * feed an updated VBT back through that, compared to what we'll fetch using
233  * this method of groping around in the BIOS data.
234  *
235  * Returns 0 on success, nonzero on failure.
236  */
237 bool psb_intel_init_bios(struct drm_device *dev)
238 {
239         struct drm_psb_private *dev_priv = dev->dev_private;
240         struct pci_dev *pdev = dev->pdev;
241         struct vbt_header *vbt = NULL;
242         struct bdb_header *bdb;
243         u8 __iomem *bios;
244         size_t size;
245         int i;
246
247         bios = pci_map_rom(pdev, &size);
248         if (!bios)
249                 return -1;
250
251         /* Scour memory looking for the VBT signature */
252         for (i = 0; i + 4 < size; i++) {
253                 if (!memcmp(bios + i, "$VBT", 4)) {
254                         vbt = (struct vbt_header *)(bios + i);
255                         break;
256                 }
257         }
258
259         if (!vbt) {
260                 DRM_ERROR("VBT signature missing\n");
261                 pci_unmap_rom(pdev, bios);
262                 return -1;
263         }
264
265         bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
266
267         /* Grab useful general definitions */
268         parse_general_features(dev_priv, bdb);
269         parse_lfp_panel_data(dev_priv, bdb);
270         parse_sdvo_panel_data(dev_priv, bdb);
271         parse_backlight_data(dev_priv, bdb);
272
273         pci_unmap_rom(pdev, bios);
274
275         return 0;
276 }
277
278 /**
279  * Destroy and free VBT data
280  */
281 void psb_intel_destroy_bios(struct drm_device *dev)
282 {
283         struct drm_psb_private *dev_priv = dev->dev_private;
284         struct drm_display_mode *sdvo_lvds_vbt_mode =
285                                 dev_priv->sdvo_lvds_vbt_mode;
286         struct drm_display_mode *lfp_lvds_vbt_mode =
287                                 dev_priv->lfp_lvds_vbt_mode;
288         struct bdb_lvds_backlight *lvds_bl =
289                                 dev_priv->lvds_bl;
290
291         /*free sdvo panel mode*/
292         if (sdvo_lvds_vbt_mode) {
293                 dev_priv->sdvo_lvds_vbt_mode = NULL;
294                 kfree(sdvo_lvds_vbt_mode);
295         }
296
297         if (lfp_lvds_vbt_mode) {
298                 dev_priv->lfp_lvds_vbt_mode = NULL;
299                 kfree(lfp_lvds_vbt_mode);
300         }
301
302         if (lvds_bl) {
303                 dev_priv->lvds_bl = NULL;
304                 kfree(lvds_bl);
305         }
306 }