Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2006 Intel Corporation
0004  *
0005  * Authors:
0006  *    Eric Anholt <eric@anholt.net>
0007  */
0008 
0009 #include <drm/display/drm_dp_helper.h>
0010 #include <drm/drm.h>
0011 
0012 #include "intel_bios.h"
0013 #include "psb_drv.h"
0014 #include "psb_intel_drv.h"
0015 #include "psb_intel_reg.h"
0016 
0017 #define SLAVE_ADDR1 0x70
0018 #define SLAVE_ADDR2 0x72
0019 
0020 static void *find_section(struct bdb_header *bdb, int section_id)
0021 {
0022     u8 *base = (u8 *)bdb;
0023     int index = 0;
0024     u16 total, current_size;
0025     u8 current_id;
0026 
0027     /* skip to first section */
0028     index += bdb->header_size;
0029     total = bdb->bdb_size;
0030 
0031     /* walk the sections looking for section_id */
0032     while (index < total) {
0033         current_id = *(base + index);
0034         index++;
0035         current_size = *((u16 *)(base + index));
0036         index += 2;
0037         if (current_id == section_id)
0038             return base + index;
0039         index += current_size;
0040     }
0041 
0042     return NULL;
0043 }
0044 
0045 static void
0046 parse_edp(struct drm_psb_private *dev_priv, struct bdb_header *bdb)
0047 {
0048     struct bdb_edp *edp;
0049     struct edp_power_seq *edp_pps;
0050     struct edp_link_params *edp_link_params;
0051     uint8_t panel_type;
0052 
0053     edp = find_section(bdb, BDB_EDP);
0054 
0055     dev_priv->edp.bpp = 18;
0056     if (!edp) {
0057         if (dev_priv->edp.support) {
0058             DRM_DEBUG_KMS("No eDP BDB found but eDP panel supported, assume %dbpp panel color depth.\n",
0059                       dev_priv->edp.bpp);
0060         }
0061         return;
0062     }
0063 
0064     panel_type = dev_priv->panel_type;
0065     switch ((edp->color_depth >> (panel_type * 2)) & 3) {
0066     case EDP_18BPP:
0067         dev_priv->edp.bpp = 18;
0068         break;
0069     case EDP_24BPP:
0070         dev_priv->edp.bpp = 24;
0071         break;
0072     case EDP_30BPP:
0073         dev_priv->edp.bpp = 30;
0074         break;
0075     }
0076 
0077     /* Get the eDP sequencing and link info */
0078     edp_pps = &edp->power_seqs[panel_type];
0079     edp_link_params = &edp->link_params[panel_type];
0080 
0081     dev_priv->edp.pps = *edp_pps;
0082 
0083     DRM_DEBUG_KMS("EDP timing in vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
0084                 dev_priv->edp.pps.t1_t3, dev_priv->edp.pps.t8,
0085                 dev_priv->edp.pps.t9, dev_priv->edp.pps.t10,
0086                 dev_priv->edp.pps.t11_t12);
0087 
0088     dev_priv->edp.rate = edp_link_params->rate ? DP_LINK_BW_2_7 :
0089         DP_LINK_BW_1_62;
0090     switch (edp_link_params->lanes) {
0091     case 0:
0092         dev_priv->edp.lanes = 1;
0093         break;
0094     case 1:
0095         dev_priv->edp.lanes = 2;
0096         break;
0097     case 3:
0098     default:
0099         dev_priv->edp.lanes = 4;
0100         break;
0101     }
0102     DRM_DEBUG_KMS("VBT reports EDP: Lane_count %d, Lane_rate %d, Bpp %d\n",
0103             dev_priv->edp.lanes, dev_priv->edp.rate, dev_priv->edp.bpp);
0104 
0105     switch (edp_link_params->preemphasis) {
0106     case 0:
0107         dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
0108         break;
0109     case 1:
0110         dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
0111         break;
0112     case 2:
0113         dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
0114         break;
0115     case 3:
0116         dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
0117         break;
0118     }
0119     switch (edp_link_params->vswing) {
0120     case 0:
0121         dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
0122         break;
0123     case 1:
0124         dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
0125         break;
0126     case 2:
0127         dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
0128         break;
0129     case 3:
0130         dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
0131         break;
0132     }
0133     DRM_DEBUG_KMS("VBT reports EDP: VSwing  %d, Preemph %d\n",
0134             dev_priv->edp.vswing, dev_priv->edp.preemphasis);
0135 }
0136 
0137 static u16
0138 get_blocksize(void *p)
0139 {
0140     u16 *block_ptr, block_size;
0141 
0142     block_ptr = (u16 *)((char *)p - 2);
0143     block_size = *block_ptr;
0144     return block_size;
0145 }
0146 
0147 static void fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
0148             struct lvds_dvo_timing *dvo_timing)
0149 {
0150     panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
0151         dvo_timing->hactive_lo;
0152     panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
0153         ((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
0154     panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
0155         dvo_timing->hsync_pulse_width;
0156     panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
0157         ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
0158 
0159     panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
0160         dvo_timing->vactive_lo;
0161     panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
0162         dvo_timing->vsync_off;
0163     panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
0164         dvo_timing->vsync_pulse_width;
0165     panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
0166         ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
0167     panel_fixed_mode->clock = dvo_timing->clock * 10;
0168     panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
0169 
0170     if (dvo_timing->hsync_positive)
0171         panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
0172     else
0173         panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
0174 
0175     if (dvo_timing->vsync_positive)
0176         panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
0177     else
0178         panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
0179 
0180     /* Some VBTs have bogus h/vtotal values */
0181     if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
0182         panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
0183     if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
0184         panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
0185 
0186     drm_mode_set_name(panel_fixed_mode);
0187 }
0188 
0189 static void parse_backlight_data(struct drm_psb_private *dev_priv,
0190                 struct bdb_header *bdb)
0191 {
0192     struct bdb_lvds_backlight *vbt_lvds_bl = NULL;
0193     struct bdb_lvds_backlight *lvds_bl;
0194     u8 p_type = 0;
0195     void *bl_start = NULL;
0196     struct bdb_lvds_options *lvds_opts
0197                 = find_section(bdb, BDB_LVDS_OPTIONS);
0198 
0199     dev_priv->lvds_bl = NULL;
0200 
0201     if (lvds_opts)
0202         p_type = lvds_opts->panel_type;
0203     else
0204         return;
0205 
0206     bl_start = find_section(bdb, BDB_LVDS_BACKLIGHT);
0207     vbt_lvds_bl = (struct bdb_lvds_backlight *)(bl_start + 1) + p_type;
0208 
0209     lvds_bl = kmemdup(vbt_lvds_bl, sizeof(*vbt_lvds_bl), GFP_KERNEL);
0210     if (!lvds_bl) {
0211         dev_err(dev_priv->dev.dev, "out of memory for backlight data\n");
0212         return;
0213     }
0214     dev_priv->lvds_bl = lvds_bl;
0215 }
0216 
0217 /* Try to find integrated panel data */
0218 static void parse_lfp_panel_data(struct drm_psb_private *dev_priv,
0219                 struct bdb_header *bdb)
0220 {
0221     struct bdb_lvds_options *lvds_options;
0222     struct bdb_lvds_lfp_data *lvds_lfp_data;
0223     struct bdb_lvds_lfp_data_entry *entry;
0224     struct lvds_dvo_timing *dvo_timing;
0225     struct drm_display_mode *panel_fixed_mode;
0226 
0227     /* Defaults if we can't find VBT info */
0228     dev_priv->lvds_dither = 0;
0229     dev_priv->lvds_vbt = 0;
0230 
0231     lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
0232     if (!lvds_options)
0233         return;
0234 
0235     dev_priv->lvds_dither = lvds_options->pixel_dither;
0236     dev_priv->panel_type = lvds_options->panel_type;
0237 
0238     if (lvds_options->panel_type == 0xff)
0239         return;
0240 
0241     lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
0242     if (!lvds_lfp_data)
0243         return;
0244 
0245 
0246     entry = &lvds_lfp_data->data[lvds_options->panel_type];
0247     dvo_timing = &entry->dvo_timing;
0248 
0249     panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode),
0250                       GFP_KERNEL);
0251     if (panel_fixed_mode == NULL) {
0252         dev_err(dev_priv->dev.dev, "out of memory for fixed panel mode\n");
0253         return;
0254     }
0255 
0256     dev_priv->lvds_vbt = 1;
0257     fill_detail_timing_data(panel_fixed_mode, dvo_timing);
0258 
0259     if (panel_fixed_mode->htotal > 0 && panel_fixed_mode->vtotal > 0) {
0260         dev_priv->lfp_lvds_vbt_mode = panel_fixed_mode;
0261         drm_mode_debug_printmodeline(panel_fixed_mode);
0262     } else {
0263         dev_dbg(dev_priv->dev.dev, "ignoring invalid LVDS VBT\n");
0264         dev_priv->lvds_vbt = 0;
0265         kfree(panel_fixed_mode);
0266     }
0267     return;
0268 }
0269 
0270 /* Try to find sdvo panel data */
0271 static void parse_sdvo_panel_data(struct drm_psb_private *dev_priv,
0272               struct bdb_header *bdb)
0273 {
0274     struct bdb_sdvo_lvds_options *sdvo_lvds_options;
0275     struct lvds_dvo_timing *dvo_timing;
0276     struct drm_display_mode *panel_fixed_mode;
0277 
0278     dev_priv->sdvo_lvds_vbt_mode = NULL;
0279 
0280     sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
0281     if (!sdvo_lvds_options)
0282         return;
0283 
0284     dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
0285     if (!dvo_timing)
0286         return;
0287 
0288     panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
0289 
0290     if (!panel_fixed_mode)
0291         return;
0292 
0293     fill_detail_timing_data(panel_fixed_mode,
0294             dvo_timing + sdvo_lvds_options->panel_type);
0295 
0296     dev_priv->sdvo_lvds_vbt_mode = panel_fixed_mode;
0297 
0298     return;
0299 }
0300 
0301 static void parse_general_features(struct drm_psb_private *dev_priv,
0302                struct bdb_header *bdb)
0303 {
0304     struct bdb_general_features *general;
0305 
0306     /* Set sensible defaults in case we can't find the general block */
0307     dev_priv->int_tv_support = 1;
0308     dev_priv->int_crt_support = 1;
0309 
0310     general = find_section(bdb, BDB_GENERAL_FEATURES);
0311     if (general) {
0312         dev_priv->int_tv_support = general->int_tv_support;
0313         dev_priv->int_crt_support = general->int_crt_support;
0314         dev_priv->lvds_use_ssc = general->enable_ssc;
0315 
0316         if (dev_priv->lvds_use_ssc) {
0317             dev_priv->lvds_ssc_freq
0318                 = general->ssc_freq ? 100 : 96;
0319         }
0320     }
0321 }
0322 
0323 static void
0324 parse_sdvo_device_mapping(struct drm_psb_private *dev_priv,
0325               struct bdb_header *bdb)
0326 {
0327     struct sdvo_device_mapping *p_mapping;
0328     struct bdb_general_definitions *p_defs;
0329     struct child_device_config *p_child;
0330     int i, child_device_num, count;
0331     u16 block_size;
0332 
0333     p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
0334     if (!p_defs) {
0335         DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
0336         return;
0337     }
0338     /* judge whether the size of child device meets the requirements.
0339      * If the child device size obtained from general definition block
0340      * is different with sizeof(struct child_device_config), skip the
0341      * parsing of sdvo device info
0342      */
0343     if (p_defs->child_dev_size != sizeof(*p_child)) {
0344         /* different child dev size . Ignore it */
0345         DRM_DEBUG_KMS("different child size is found. Invalid.\n");
0346         return;
0347     }
0348     /* get the block size of general definitions */
0349     block_size = get_blocksize(p_defs);
0350     /* get the number of child device */
0351     child_device_num = (block_size - sizeof(*p_defs)) /
0352                 sizeof(*p_child);
0353     count = 0;
0354     for (i = 0; i < child_device_num; i++) {
0355         p_child = &(p_defs->devices[i]);
0356         if (!p_child->device_type) {
0357             /* skip the device block if device type is invalid */
0358             continue;
0359         }
0360         if (p_child->slave_addr != SLAVE_ADDR1 &&
0361             p_child->slave_addr != SLAVE_ADDR2) {
0362             /*
0363              * If the slave address is neither 0x70 nor 0x72,
0364              * it is not a SDVO device. Skip it.
0365              */
0366             continue;
0367         }
0368         if (p_child->dvo_port != DEVICE_PORT_DVOB &&
0369             p_child->dvo_port != DEVICE_PORT_DVOC) {
0370             /* skip the incorrect SDVO port */
0371             DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
0372             continue;
0373         }
0374         DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
0375                 " %s port\n",
0376                 p_child->slave_addr,
0377                 (p_child->dvo_port == DEVICE_PORT_DVOB) ?
0378                     "SDVOB" : "SDVOC");
0379         p_mapping = &(dev_priv->sdvo_mappings[p_child->dvo_port - 1]);
0380         if (!p_mapping->initialized) {
0381             p_mapping->dvo_port = p_child->dvo_port;
0382             p_mapping->slave_addr = p_child->slave_addr;
0383             p_mapping->dvo_wiring = p_child->dvo_wiring;
0384             p_mapping->ddc_pin = p_child->ddc_pin;
0385             p_mapping->i2c_pin = p_child->i2c_pin;
0386             p_mapping->initialized = 1;
0387             DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
0388                       p_mapping->dvo_port,
0389                       p_mapping->slave_addr,
0390                       p_mapping->dvo_wiring,
0391                       p_mapping->ddc_pin,
0392                       p_mapping->i2c_pin);
0393         } else {
0394             DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
0395                      "two SDVO device.\n");
0396         }
0397         if (p_child->slave2_addr) {
0398             /* Maybe this is a SDVO device with multiple inputs */
0399             /* And the mapping info is not added */
0400             DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
0401                 " is a SDVO device with multiple inputs.\n");
0402         }
0403         count++;
0404     }
0405 
0406     if (!count) {
0407         /* No SDVO device info is found */
0408         DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
0409     }
0410     return;
0411 }
0412 
0413 
0414 static void
0415 parse_driver_features(struct drm_psb_private *dev_priv,
0416               struct bdb_header *bdb)
0417 {
0418     struct bdb_driver_features *driver;
0419 
0420     driver = find_section(bdb, BDB_DRIVER_FEATURES);
0421     if (!driver)
0422         return;
0423 
0424     if (driver->lvds_config == BDB_DRIVER_FEATURE_EDP)
0425         dev_priv->edp.support = 1;
0426 
0427     dev_priv->lvds_enabled_in_vbt = driver->lvds_config != 0;
0428     DRM_DEBUG_KMS("LVDS VBT config bits: 0x%x\n", driver->lvds_config);
0429 
0430     /* This bit means to use 96Mhz for DPLL_A or not */
0431     if (driver->primary_lfp_id)
0432         dev_priv->dplla_96mhz = true;
0433     else
0434         dev_priv->dplla_96mhz = false;
0435 }
0436 
0437 static void
0438 parse_device_mapping(struct drm_psb_private *dev_priv,
0439                struct bdb_header *bdb)
0440 {
0441     struct bdb_general_definitions *p_defs;
0442     struct child_device_config *p_child, *child_dev_ptr;
0443     int i, child_device_num, count;
0444     u16 block_size;
0445 
0446     p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
0447     if (!p_defs) {
0448         DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
0449         return;
0450     }
0451     /* judge whether the size of child device meets the requirements.
0452      * If the child device size obtained from general definition block
0453      * is different with sizeof(struct child_device_config), skip the
0454      * parsing of sdvo device info
0455      */
0456     if (p_defs->child_dev_size != sizeof(*p_child)) {
0457         /* different child dev size . Ignore it */
0458         DRM_DEBUG_KMS("different child size is found. Invalid.\n");
0459         return;
0460     }
0461     /* get the block size of general definitions */
0462     block_size = get_blocksize(p_defs);
0463     /* get the number of child device */
0464     child_device_num = (block_size - sizeof(*p_defs)) /
0465                 sizeof(*p_child);
0466     count = 0;
0467     /* get the number of child devices that are present */
0468     for (i = 0; i < child_device_num; i++) {
0469         p_child = &(p_defs->devices[i]);
0470         if (!p_child->device_type) {
0471             /* skip the device block if device type is invalid */
0472             continue;
0473         }
0474         count++;
0475     }
0476     if (!count) {
0477         DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
0478         return;
0479     }
0480     dev_priv->child_dev = kcalloc(count, sizeof(*p_child), GFP_KERNEL);
0481     if (!dev_priv->child_dev) {
0482         DRM_DEBUG_KMS("No memory space for child devices\n");
0483         return;
0484     }
0485 
0486     dev_priv->child_dev_num = count;
0487     count = 0;
0488     for (i = 0; i < child_device_num; i++) {
0489         p_child = &(p_defs->devices[i]);
0490         if (!p_child->device_type) {
0491             /* skip the device block if device type is invalid */
0492             continue;
0493         }
0494         child_dev_ptr = dev_priv->child_dev + count;
0495         count++;
0496         memcpy((void *)child_dev_ptr, (void *)p_child,
0497                     sizeof(*p_child));
0498     }
0499     return;
0500 }
0501 
0502 
0503 /**
0504  * psb_intel_init_bios - initialize VBIOS settings & find VBT
0505  * @dev: DRM device
0506  *
0507  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
0508  * to appropriate values.
0509  *
0510  * VBT existence is a sanity check that is relied on by other i830_bios.c code.
0511  * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
0512  * feed an updated VBT back through that, compared to what we'll fetch using
0513  * this method of groping around in the BIOS data.
0514  *
0515  * Returns 0 on success, nonzero on failure.
0516  */
0517 int psb_intel_init_bios(struct drm_device *dev)
0518 {
0519     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0520     struct pci_dev *pdev = to_pci_dev(dev->dev);
0521     struct vbt_header *vbt = NULL;
0522     struct bdb_header *bdb = NULL;
0523     u8 __iomem *bios = NULL;
0524     size_t size;
0525     int i;
0526 
0527 
0528     dev_priv->panel_type = 0xff;
0529 
0530     /* XXX Should this validation be moved to intel_opregion.c? */
0531     if (dev_priv->opregion.vbt) {
0532         struct vbt_header *vbt = dev_priv->opregion.vbt;
0533         if (memcmp(vbt->signature, "$VBT", 4) == 0) {
0534             DRM_DEBUG_KMS("Using VBT from OpRegion: %20s\n",
0535                      vbt->signature);
0536             bdb = (struct bdb_header *)((char *)vbt + vbt->bdb_offset);
0537         } else
0538             dev_priv->opregion.vbt = NULL;
0539     }
0540 
0541     if (bdb == NULL) {
0542         bios = pci_map_rom(pdev, &size);
0543         if (!bios)
0544             return -1;
0545 
0546         /* Scour memory looking for the VBT signature */
0547         for (i = 0; i + 4 < size; i++) {
0548             if (!memcmp(bios + i, "$VBT", 4)) {
0549                 vbt = (struct vbt_header *)(bios + i);
0550                 break;
0551             }
0552         }
0553 
0554         if (!vbt) {
0555             dev_err(dev->dev, "VBT signature missing\n");
0556             pci_unmap_rom(pdev, bios);
0557             return -1;
0558         }
0559         bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
0560     }
0561 
0562     /* Grab useful general dxefinitions */
0563     parse_general_features(dev_priv, bdb);
0564     parse_driver_features(dev_priv, bdb);
0565     parse_lfp_panel_data(dev_priv, bdb);
0566     parse_sdvo_panel_data(dev_priv, bdb);
0567     parse_sdvo_device_mapping(dev_priv, bdb);
0568     parse_device_mapping(dev_priv, bdb);
0569     parse_backlight_data(dev_priv, bdb);
0570     parse_edp(dev_priv, bdb);
0571 
0572     if (bios)
0573         pci_unmap_rom(pdev, bios);
0574 
0575     return 0;
0576 }
0577 
0578 /*
0579  * Destroy and free VBT data
0580  */
0581 void psb_intel_destroy_bios(struct drm_device *dev)
0582 {
0583     struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
0584 
0585     kfree(dev_priv->sdvo_lvds_vbt_mode);
0586     kfree(dev_priv->lfp_lvds_vbt_mode);
0587     kfree(dev_priv->lvds_bl);
0588 }