Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / gma500 / 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 "gma_drm.h"
24 #include "psb_drv.h"
25 #include "psb_intel_drv.h"
26 #include "psb_intel_reg.h"
27 #include "intel_bios.h"
28
29 #define SLAVE_ADDR1     0x70
30 #define SLAVE_ADDR2     0x72
31
32 static void *find_section(struct bdb_header *bdb, int section_id)
33 {
34         u8 *base = (u8 *)bdb;
35         int index = 0;
36         u16 total, current_size;
37         u8 current_id;
38
39         /* skip to first section */
40         index += bdb->header_size;
41         total = bdb->bdb_size;
42
43         /* walk the sections looking for section_id */
44         while (index < total) {
45                 current_id = *(base + index);
46                 index++;
47                 current_size = *((u16 *)(base + index));
48                 index += 2;
49                 if (current_id == section_id)
50                         return base + index;
51                 index += current_size;
52         }
53
54         return NULL;
55 }
56
57 static u16
58 get_blocksize(void *p)
59 {
60         u16 *block_ptr, block_size;
61
62         block_ptr = (u16 *)((char *)p - 2);
63         block_size = *block_ptr;
64         return block_size;
65 }
66
67 static void fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
68                         struct lvds_dvo_timing *dvo_timing)
69 {
70         panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
71                 dvo_timing->hactive_lo;
72         panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
73                 ((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
74         panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
75                 dvo_timing->hsync_pulse_width;
76         panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
77                 ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
78
79         panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
80                 dvo_timing->vactive_lo;
81         panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
82                 dvo_timing->vsync_off;
83         panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
84                 dvo_timing->vsync_pulse_width;
85         panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
86                 ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
87         panel_fixed_mode->clock = dvo_timing->clock * 10;
88         panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
89
90         if (dvo_timing->hsync_positive)
91                 panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
92         else
93                 panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
94
95         if (dvo_timing->vsync_positive)
96                 panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
97         else
98                 panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
99
100         /* Some VBTs have bogus h/vtotal values */
101         if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
102                 panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
103         if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
104                 panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
105
106         drm_mode_set_name(panel_fixed_mode);
107 }
108
109 static void parse_backlight_data(struct drm_psb_private *dev_priv,
110                                 struct bdb_header *bdb)
111 {
112         struct bdb_lvds_backlight *vbt_lvds_bl = NULL;
113         struct bdb_lvds_backlight *lvds_bl;
114         u8 p_type = 0;
115         void *bl_start = NULL;
116         struct bdb_lvds_options *lvds_opts
117                                 = find_section(bdb, BDB_LVDS_OPTIONS);
118
119         dev_priv->lvds_bl = NULL;
120
121         if (lvds_opts)
122                 p_type = lvds_opts->panel_type;
123         else
124                 return;
125
126         bl_start = find_section(bdb, BDB_LVDS_BACKLIGHT);
127         vbt_lvds_bl = (struct bdb_lvds_backlight *)(bl_start + 1) + p_type;
128
129         lvds_bl = kzalloc(sizeof(*vbt_lvds_bl), GFP_KERNEL);
130         if (!lvds_bl) {
131                 dev_err(dev_priv->dev->dev, "out of memory for backlight data\n");
132                 return;
133         }
134         memcpy(lvds_bl, vbt_lvds_bl, sizeof(*vbt_lvds_bl));
135         dev_priv->lvds_bl = lvds_bl;
136 }
137
138 /* Try to find integrated panel data */
139 static void parse_lfp_panel_data(struct drm_psb_private *dev_priv,
140                             struct bdb_header *bdb)
141 {
142         struct bdb_lvds_options *lvds_options;
143         struct bdb_lvds_lfp_data *lvds_lfp_data;
144         struct bdb_lvds_lfp_data_entry *entry;
145         struct lvds_dvo_timing *dvo_timing;
146         struct drm_display_mode *panel_fixed_mode;
147
148         /* Defaults if we can't find VBT info */
149         dev_priv->lvds_dither = 0;
150         dev_priv->lvds_vbt = 0;
151
152         lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
153         if (!lvds_options)
154                 return;
155
156         dev_priv->lvds_dither = lvds_options->pixel_dither;
157         if (lvds_options->panel_type == 0xff)
158                 return;
159
160         lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
161         if (!lvds_lfp_data)
162                 return;
163
164
165         entry = &lvds_lfp_data->data[lvds_options->panel_type];
166         dvo_timing = &entry->dvo_timing;
167
168         panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode),
169                                       GFP_KERNEL);
170         if (panel_fixed_mode == NULL) {
171                 dev_err(dev_priv->dev->dev, "out of memory for fixed panel mode\n");
172                 return;
173         }
174
175         dev_priv->lvds_vbt = 1;
176         fill_detail_timing_data(panel_fixed_mode, dvo_timing);
177
178         if (panel_fixed_mode->htotal > 0 && panel_fixed_mode->vtotal > 0) {
179                 dev_priv->lfp_lvds_vbt_mode = panel_fixed_mode;
180                 drm_mode_debug_printmodeline(panel_fixed_mode);
181         } else {
182                 dev_dbg(dev_priv->dev->dev, "ignoring invalid LVDS VBT\n");
183                 dev_priv->lvds_vbt = 0;
184                 kfree(panel_fixed_mode);
185         }
186         return;
187 }
188
189 /* Try to find sdvo panel data */
190 static void parse_sdvo_panel_data(struct drm_psb_private *dev_priv,
191                       struct bdb_header *bdb)
192 {
193         struct bdb_sdvo_lvds_options *sdvo_lvds_options;
194         struct lvds_dvo_timing *dvo_timing;
195         struct drm_display_mode *panel_fixed_mode;
196
197         dev_priv->sdvo_lvds_vbt_mode = NULL;
198
199         sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
200         if (!sdvo_lvds_options)
201                 return;
202
203         dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
204         if (!dvo_timing)
205                 return;
206
207         panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
208
209         if (!panel_fixed_mode)
210                 return;
211
212         fill_detail_timing_data(panel_fixed_mode,
213                         dvo_timing + sdvo_lvds_options->panel_type);
214
215         dev_priv->sdvo_lvds_vbt_mode = panel_fixed_mode;
216
217         return;
218 }
219
220 static void parse_general_features(struct drm_psb_private *dev_priv,
221                        struct bdb_header *bdb)
222 {
223         struct bdb_general_features *general;
224
225         /* Set sensible defaults in case we can't find the general block */
226         dev_priv->int_tv_support = 1;
227         dev_priv->int_crt_support = 1;
228
229         general = find_section(bdb, BDB_GENERAL_FEATURES);
230         if (general) {
231                 dev_priv->int_tv_support = general->int_tv_support;
232                 dev_priv->int_crt_support = general->int_crt_support;
233                 dev_priv->lvds_use_ssc = general->enable_ssc;
234
235                 if (dev_priv->lvds_use_ssc) {
236                         dev_priv->lvds_ssc_freq
237                                 = general->ssc_freq ? 100 : 96;
238                 }
239         }
240 }
241
242 static void
243 parse_sdvo_device_mapping(struct drm_psb_private *dev_priv,
244                           struct bdb_header *bdb)
245 {
246         struct sdvo_device_mapping *p_mapping;
247         struct bdb_general_definitions *p_defs;
248         struct child_device_config *p_child;
249         int i, child_device_num, count;
250         u16     block_size;
251
252         p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
253         if (!p_defs) {
254                 DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
255                 return;
256         }
257         /* judge whether the size of child device meets the requirements.
258          * If the child device size obtained from general definition block
259          * is different with sizeof(struct child_device_config), skip the
260          * parsing of sdvo device info
261          */
262         if (p_defs->child_dev_size != sizeof(*p_child)) {
263                 /* different child dev size . Ignore it */
264                 DRM_DEBUG_KMS("different child size is found. Invalid.\n");
265                 return;
266         }
267         /* get the block size of general definitions */
268         block_size = get_blocksize(p_defs);
269         /* get the number of child device */
270         child_device_num = (block_size - sizeof(*p_defs)) /
271                                 sizeof(*p_child);
272         count = 0;
273         for (i = 0; i < child_device_num; i++) {
274                 p_child = &(p_defs->devices[i]);
275                 if (!p_child->device_type) {
276                         /* skip the device block if device type is invalid */
277                         continue;
278                 }
279                 if (p_child->slave_addr != SLAVE_ADDR1 &&
280                         p_child->slave_addr != SLAVE_ADDR2) {
281                         /*
282                          * If the slave address is neither 0x70 nor 0x72,
283                          * it is not a SDVO device. Skip it.
284                          */
285                         continue;
286                 }
287                 if (p_child->dvo_port != DEVICE_PORT_DVOB &&
288                         p_child->dvo_port != DEVICE_PORT_DVOC) {
289                         /* skip the incorrect SDVO port */
290                         DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
291                         continue;
292                 }
293                 DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
294                                 " %s port\n",
295                                 p_child->slave_addr,
296                                 (p_child->dvo_port == DEVICE_PORT_DVOB) ?
297                                         "SDVOB" : "SDVOC");
298                 p_mapping = &(dev_priv->sdvo_mappings[p_child->dvo_port - 1]);
299                 if (!p_mapping->initialized) {
300                         p_mapping->dvo_port = p_child->dvo_port;
301                         p_mapping->slave_addr = p_child->slave_addr;
302                         p_mapping->dvo_wiring = p_child->dvo_wiring;
303                         p_mapping->ddc_pin = p_child->ddc_pin;
304                         p_mapping->i2c_pin = p_child->i2c_pin;
305                         p_mapping->initialized = 1;
306                         DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
307                                       p_mapping->dvo_port,
308                                       p_mapping->slave_addr,
309                                       p_mapping->dvo_wiring,
310                                       p_mapping->ddc_pin,
311                                       p_mapping->i2c_pin);
312                 } else {
313                         DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
314                                          "two SDVO device.\n");
315                 }
316                 if (p_child->slave2_addr) {
317                         /* Maybe this is a SDVO device with multiple inputs */
318                         /* And the mapping info is not added */
319                         DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
320                                 " is a SDVO device with multiple inputs.\n");
321                 }
322                 count++;
323         }
324
325         if (!count) {
326                 /* No SDVO device info is found */
327                 DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
328         }
329         return;
330 }
331
332
333 static void
334 parse_driver_features(struct drm_psb_private *dev_priv,
335                       struct bdb_header *bdb)
336 {
337         struct bdb_driver_features *driver;
338
339         driver = find_section(bdb, BDB_DRIVER_FEATURES);
340         if (!driver)
341                 return;
342
343         /* This bit means to use 96Mhz for DPLL_A or not */
344         if (driver->primary_lfp_id)
345                 dev_priv->dplla_96mhz = true;
346         else
347                 dev_priv->dplla_96mhz = false;
348 }
349
350 static void
351 parse_device_mapping(struct drm_psb_private *dev_priv,
352                        struct bdb_header *bdb)
353 {
354         struct bdb_general_definitions *p_defs;
355         struct child_device_config *p_child, *child_dev_ptr;
356         int i, child_device_num, count;
357         u16     block_size;
358
359         p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
360         if (!p_defs) {
361                 DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
362                 return;
363         }
364         /* judge whether the size of child device meets the requirements.
365          * If the child device size obtained from general definition block
366          * is different with sizeof(struct child_device_config), skip the
367          * parsing of sdvo device info
368          */
369         if (p_defs->child_dev_size != sizeof(*p_child)) {
370                 /* different child dev size . Ignore it */
371                 DRM_DEBUG_KMS("different child size is found. Invalid.\n");
372                 return;
373         }
374         /* get the block size of general definitions */
375         block_size = get_blocksize(p_defs);
376         /* get the number of child device */
377         child_device_num = (block_size - sizeof(*p_defs)) /
378                                 sizeof(*p_child);
379         count = 0;
380         /* get the number of child devices that are present */
381         for (i = 0; i < child_device_num; i++) {
382                 p_child = &(p_defs->devices[i]);
383                 if (!p_child->device_type) {
384                         /* skip the device block if device type is invalid */
385                         continue;
386                 }
387                 count++;
388         }
389         if (!count) {
390                 DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
391                 return;
392         }
393         dev_priv->child_dev = kcalloc(count, sizeof(*p_child), GFP_KERNEL);
394         if (!dev_priv->child_dev) {
395                 DRM_DEBUG_KMS("No memory space for child devices\n");
396                 return;
397         }
398
399         dev_priv->child_dev_num = count;
400         count = 0;
401         for (i = 0; i < child_device_num; i++) {
402                 p_child = &(p_defs->devices[i]);
403                 if (!p_child->device_type) {
404                         /* skip the device block if device type is invalid */
405                         continue;
406                 }
407                 child_dev_ptr = dev_priv->child_dev + count;
408                 count++;
409                 memcpy((void *)child_dev_ptr, (void *)p_child,
410                                         sizeof(*p_child));
411         }
412         return;
413 }
414
415
416 /**
417  * psb_intel_init_bios - initialize VBIOS settings & find VBT
418  * @dev: DRM device
419  *
420  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
421  * to appropriate values.
422  *
423  * VBT existence is a sanity check that is relied on by other i830_bios.c code.
424  * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
425  * feed an updated VBT back through that, compared to what we'll fetch using
426  * this method of groping around in the BIOS data.
427  *
428  * Returns 0 on success, nonzero on failure.
429  */
430 bool psb_intel_init_bios(struct drm_device *dev)
431 {
432         struct drm_psb_private *dev_priv = dev->dev_private;
433         struct pci_dev *pdev = dev->pdev;
434         struct vbt_header *vbt = NULL;
435         struct bdb_header *bdb = NULL;
436         u8 __iomem *bios = NULL;
437         size_t size;
438         int i;
439
440         /* XXX Should this validation be moved to intel_opregion.c? */
441         if (dev_priv->opregion.vbt) {
442                 struct vbt_header *vbt = dev_priv->opregion.vbt;
443                 if (memcmp(vbt->signature, "$VBT", 4) == 0) {
444                         DRM_DEBUG_KMS("Using VBT from OpRegion: %20s\n",
445                                          vbt->signature);
446                         bdb = (struct bdb_header *)((char *)vbt + vbt->bdb_offset);
447                 } else
448                         dev_priv->opregion.vbt = NULL;
449         }
450
451         if (bdb == NULL) {
452                 bios = pci_map_rom(pdev, &size);
453                 if (!bios)
454                         return -1;
455
456                 /* Scour memory looking for the VBT signature */
457                 for (i = 0; i + 4 < size; i++) {
458                         if (!memcmp(bios + i, "$VBT", 4)) {
459                                 vbt = (struct vbt_header *)(bios + i);
460                                 break;
461                         }
462                 }
463
464                 if (!vbt) {
465                         dev_err(dev->dev, "VBT signature missing\n");
466                         pci_unmap_rom(pdev, bios);
467                         return -1;
468                 }
469                 bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
470         }
471
472         /* Grab useful general dxefinitions */
473         parse_general_features(dev_priv, bdb);
474         parse_driver_features(dev_priv, bdb);
475         parse_lfp_panel_data(dev_priv, bdb);
476         parse_sdvo_panel_data(dev_priv, bdb);
477         parse_sdvo_device_mapping(dev_priv, bdb);
478         parse_device_mapping(dev_priv, bdb);
479         parse_backlight_data(dev_priv, bdb);
480
481         if (bios)
482                 pci_unmap_rom(pdev, bios);
483
484         return 0;
485 }
486
487 /**
488  * Destroy and free VBT data
489  */
490 void psb_intel_destroy_bios(struct drm_device *dev)
491 {
492         struct drm_psb_private *dev_priv = dev->dev_private;
493
494         kfree(dev_priv->sdvo_lvds_vbt_mode);
495         kfree(dev_priv->lfp_lvds_vbt_mode);
496         kfree(dev_priv->lvds_bl);
497 }