Path: blob/master/drivers/gpu/drm/i915/intel_crt.c
15112 views
/*1* Copyright © 2006-2007 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*22* Authors:23* Eric Anholt <[email protected]>24*/2526#include <linux/i2c.h>27#include <linux/slab.h>28#include "drmP.h"29#include "drm.h"30#include "drm_crtc.h"31#include "drm_crtc_helper.h"32#include "drm_edid.h"33#include "intel_drv.h"34#include "i915_drm.h"35#include "i915_drv.h"3637/* Here's the desired hotplug mode */38#define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 | \39ADPA_CRT_HOTPLUG_WARMUP_10MS | \40ADPA_CRT_HOTPLUG_SAMPLE_4S | \41ADPA_CRT_HOTPLUG_VOLTAGE_50 | \42ADPA_CRT_HOTPLUG_VOLREF_325MV | \43ADPA_CRT_HOTPLUG_ENABLE)4445struct intel_crt {46struct intel_encoder base;47bool force_hotplug_required;48};4950static struct intel_crt *intel_attached_crt(struct drm_connector *connector)51{52return container_of(intel_attached_encoder(connector),53struct intel_crt, base);54}5556static void intel_crt_dpms(struct drm_encoder *encoder, int mode)57{58struct drm_device *dev = encoder->dev;59struct drm_i915_private *dev_priv = dev->dev_private;60u32 temp, reg;6162if (HAS_PCH_SPLIT(dev))63reg = PCH_ADPA;64else65reg = ADPA;6667temp = I915_READ(reg);68temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);69temp &= ~ADPA_DAC_ENABLE;7071switch(mode) {72case DRM_MODE_DPMS_ON:73temp |= ADPA_DAC_ENABLE;74break;75case DRM_MODE_DPMS_STANDBY:76temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;77break;78case DRM_MODE_DPMS_SUSPEND:79temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;80break;81case DRM_MODE_DPMS_OFF:82temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;83break;84}8586I915_WRITE(reg, temp);87}8889static int intel_crt_mode_valid(struct drm_connector *connector,90struct drm_display_mode *mode)91{92struct drm_device *dev = connector->dev;9394int max_clock = 0;95if (mode->flags & DRM_MODE_FLAG_DBLSCAN)96return MODE_NO_DBLESCAN;9798if (mode->clock < 25000)99return MODE_CLOCK_LOW;100101if (IS_GEN2(dev))102max_clock = 350000;103else104max_clock = 400000;105if (mode->clock > max_clock)106return MODE_CLOCK_HIGH;107108return MODE_OK;109}110111static bool intel_crt_mode_fixup(struct drm_encoder *encoder,112struct drm_display_mode *mode,113struct drm_display_mode *adjusted_mode)114{115return true;116}117118static void intel_crt_mode_set(struct drm_encoder *encoder,119struct drm_display_mode *mode,120struct drm_display_mode *adjusted_mode)121{122123struct drm_device *dev = encoder->dev;124struct drm_crtc *crtc = encoder->crtc;125struct intel_crtc *intel_crtc = to_intel_crtc(crtc);126struct drm_i915_private *dev_priv = dev->dev_private;127int dpll_md_reg;128u32 adpa, dpll_md;129u32 adpa_reg;130131dpll_md_reg = DPLL_MD(intel_crtc->pipe);132133if (HAS_PCH_SPLIT(dev))134adpa_reg = PCH_ADPA;135else136adpa_reg = ADPA;137138/*139* Disable separate mode multiplier used when cloning SDVO to CRT140* XXX this needs to be adjusted when we really are cloning141*/142if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {143dpll_md = I915_READ(dpll_md_reg);144I915_WRITE(dpll_md_reg,145dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);146}147148adpa = ADPA_HOTPLUG_BITS;149if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)150adpa |= ADPA_HSYNC_ACTIVE_HIGH;151if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)152adpa |= ADPA_VSYNC_ACTIVE_HIGH;153154if (intel_crtc->pipe == 0) {155if (HAS_PCH_CPT(dev))156adpa |= PORT_TRANS_A_SEL_CPT;157else158adpa |= ADPA_PIPE_A_SELECT;159} else {160if (HAS_PCH_CPT(dev))161adpa |= PORT_TRANS_B_SEL_CPT;162else163adpa |= ADPA_PIPE_B_SELECT;164}165166if (!HAS_PCH_SPLIT(dev))167I915_WRITE(BCLRPAT(intel_crtc->pipe), 0);168169I915_WRITE(adpa_reg, adpa);170}171172static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)173{174struct drm_device *dev = connector->dev;175struct intel_crt *crt = intel_attached_crt(connector);176struct drm_i915_private *dev_priv = dev->dev_private;177u32 adpa;178bool ret;179180/* The first time through, trigger an explicit detection cycle */181if (crt->force_hotplug_required) {182bool turn_off_dac = HAS_PCH_SPLIT(dev);183u32 save_adpa;184185crt->force_hotplug_required = 0;186187save_adpa = adpa = I915_READ(PCH_ADPA);188DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);189190adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;191if (turn_off_dac)192adpa &= ~ADPA_DAC_ENABLE;193194I915_WRITE(PCH_ADPA, adpa);195196if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,1971000))198DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");199200if (turn_off_dac) {201I915_WRITE(PCH_ADPA, save_adpa);202POSTING_READ(PCH_ADPA);203}204}205206/* Check the status to see if both blue and green are on now */207adpa = I915_READ(PCH_ADPA);208if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)209ret = true;210else211ret = false;212DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);213214return ret;215}216217/**218* Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.219*220* Not for i915G/i915GM221*222* \return true if CRT is connected.223* \return false if CRT is disconnected.224*/225static bool intel_crt_detect_hotplug(struct drm_connector *connector)226{227struct drm_device *dev = connector->dev;228struct drm_i915_private *dev_priv = dev->dev_private;229u32 hotplug_en, orig, stat;230bool ret = false;231int i, tries = 0;232233if (HAS_PCH_SPLIT(dev))234return intel_ironlake_crt_detect_hotplug(connector);235236/*237* On 4 series desktop, CRT detect sequence need to be done twice238* to get a reliable result.239*/240241if (IS_G4X(dev) && !IS_GM45(dev))242tries = 2;243else244tries = 1;245hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);246hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;247248for (i = 0; i < tries ; i++) {249/* turn on the FORCE_DETECT */250I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);251/* wait for FORCE_DETECT to go off */252if (wait_for((I915_READ(PORT_HOTPLUG_EN) &253CRT_HOTPLUG_FORCE_DETECT) == 0,2541000))255DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");256}257258stat = I915_READ(PORT_HOTPLUG_STAT);259if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)260ret = true;261262/* clear the interrupt we just generated, if any */263I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);264265/* and put the bits back */266I915_WRITE(PORT_HOTPLUG_EN, orig);267268return ret;269}270271static bool intel_crt_detect_ddc(struct drm_connector *connector)272{273struct intel_crt *crt = intel_attached_crt(connector);274struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;275276/* CRT should always be at 0, but check anyway */277if (crt->base.type != INTEL_OUTPUT_ANALOG)278return false;279280if (intel_ddc_probe(&crt->base, dev_priv->crt_ddc_pin)) {281struct edid *edid;282bool is_digital = false;283284edid = drm_get_edid(connector,285&dev_priv->gmbus[dev_priv->crt_ddc_pin].adapter);286/*287* This may be a DVI-I connector with a shared DDC288* link between analog and digital outputs, so we289* have to check the EDID input spec of the attached device.290*291* On the other hand, what should we do if it is a broken EDID?292*/293if (edid != NULL) {294is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;295connector->display_info.raw_edid = NULL;296kfree(edid);297}298299if (!is_digital) {300DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");301return true;302} else {303DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");304}305}306307return false;308}309310static enum drm_connector_status311intel_crt_load_detect(struct intel_crt *crt)312{313struct drm_device *dev = crt->base.base.dev;314struct drm_i915_private *dev_priv = dev->dev_private;315uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;316uint32_t save_bclrpat;317uint32_t save_vtotal;318uint32_t vtotal, vactive;319uint32_t vsample;320uint32_t vblank, vblank_start, vblank_end;321uint32_t dsl;322uint32_t bclrpat_reg;323uint32_t vtotal_reg;324uint32_t vblank_reg;325uint32_t vsync_reg;326uint32_t pipeconf_reg;327uint32_t pipe_dsl_reg;328uint8_t st00;329enum drm_connector_status status;330331DRM_DEBUG_KMS("starting load-detect on CRT\n");332333bclrpat_reg = BCLRPAT(pipe);334vtotal_reg = VTOTAL(pipe);335vblank_reg = VBLANK(pipe);336vsync_reg = VSYNC(pipe);337pipeconf_reg = PIPECONF(pipe);338pipe_dsl_reg = PIPEDSL(pipe);339340save_bclrpat = I915_READ(bclrpat_reg);341save_vtotal = I915_READ(vtotal_reg);342vblank = I915_READ(vblank_reg);343344vtotal = ((save_vtotal >> 16) & 0xfff) + 1;345vactive = (save_vtotal & 0x7ff) + 1;346347vblank_start = (vblank & 0xfff) + 1;348vblank_end = ((vblank >> 16) & 0xfff) + 1;349350/* Set the border color to purple. */351I915_WRITE(bclrpat_reg, 0x500050);352353if (!IS_GEN2(dev)) {354uint32_t pipeconf = I915_READ(pipeconf_reg);355I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);356POSTING_READ(pipeconf_reg);357/* Wait for next Vblank to substitue358* border color for Color info */359intel_wait_for_vblank(dev, pipe);360st00 = I915_READ8(VGA_MSR_WRITE);361status = ((st00 & (1 << 4)) != 0) ?362connector_status_connected :363connector_status_disconnected;364365I915_WRITE(pipeconf_reg, pipeconf);366} else {367bool restore_vblank = false;368int count, detect;369370/*371* If there isn't any border, add some.372* Yes, this will flicker373*/374if (vblank_start <= vactive && vblank_end >= vtotal) {375uint32_t vsync = I915_READ(vsync_reg);376uint32_t vsync_start = (vsync & 0xffff) + 1;377378vblank_start = vsync_start;379I915_WRITE(vblank_reg,380(vblank_start - 1) |381((vblank_end - 1) << 16));382restore_vblank = true;383}384/* sample in the vertical border, selecting the larger one */385if (vblank_start - vactive >= vtotal - vblank_end)386vsample = (vblank_start + vactive) >> 1;387else388vsample = (vtotal + vblank_end) >> 1;389390/*391* Wait for the border to be displayed392*/393while (I915_READ(pipe_dsl_reg) >= vactive)394;395while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)396;397/*398* Watch ST00 for an entire scanline399*/400detect = 0;401count = 0;402do {403count++;404/* Read the ST00 VGA status register */405st00 = I915_READ8(VGA_MSR_WRITE);406if (st00 & (1 << 4))407detect++;408} while ((I915_READ(pipe_dsl_reg) == dsl));409410/* restore vblank if necessary */411if (restore_vblank)412I915_WRITE(vblank_reg, vblank);413/*414* If more than 3/4 of the scanline detected a monitor,415* then it is assumed to be present. This works even on i830,416* where there isn't any way to force the border color across417* the screen418*/419status = detect * 4 > count * 3 ?420connector_status_connected :421connector_status_disconnected;422}423424/* Restore previous settings */425I915_WRITE(bclrpat_reg, save_bclrpat);426427return status;428}429430static enum drm_connector_status431intel_crt_detect(struct drm_connector *connector, bool force)432{433struct drm_device *dev = connector->dev;434struct intel_crt *crt = intel_attached_crt(connector);435struct drm_crtc *crtc;436enum drm_connector_status status;437438if (I915_HAS_HOTPLUG(dev)) {439if (intel_crt_detect_hotplug(connector)) {440DRM_DEBUG_KMS("CRT detected via hotplug\n");441return connector_status_connected;442} else {443DRM_DEBUG_KMS("CRT not detected via hotplug\n");444return connector_status_disconnected;445}446}447448if (intel_crt_detect_ddc(connector))449return connector_status_connected;450451if (!force)452return connector->status;453454/* for pre-945g platforms use load detect */455crtc = crt->base.base.crtc;456if (crtc && crtc->enabled) {457status = intel_crt_load_detect(crt);458} else {459struct intel_load_detect_pipe tmp;460461if (intel_get_load_detect_pipe(&crt->base, connector, NULL,462&tmp)) {463if (intel_crt_detect_ddc(connector))464status = connector_status_connected;465else466status = intel_crt_load_detect(crt);467intel_release_load_detect_pipe(&crt->base, connector,468&tmp);469} else470status = connector_status_unknown;471}472473return status;474}475476static void intel_crt_destroy(struct drm_connector *connector)477{478drm_sysfs_connector_remove(connector);479drm_connector_cleanup(connector);480kfree(connector);481}482483static int intel_crt_get_modes(struct drm_connector *connector)484{485struct drm_device *dev = connector->dev;486struct drm_i915_private *dev_priv = dev->dev_private;487int ret;488489ret = intel_ddc_get_modes(connector,490&dev_priv->gmbus[dev_priv->crt_ddc_pin].adapter);491if (ret || !IS_G4X(dev))492return ret;493494/* Try to probe digital port for output in DVI-I -> VGA mode. */495return intel_ddc_get_modes(connector,496&dev_priv->gmbus[GMBUS_PORT_DPB].adapter);497}498499static int intel_crt_set_property(struct drm_connector *connector,500struct drm_property *property,501uint64_t value)502{503return 0;504}505506static void intel_crt_reset(struct drm_connector *connector)507{508struct drm_device *dev = connector->dev;509struct intel_crt *crt = intel_attached_crt(connector);510511if (HAS_PCH_SPLIT(dev))512crt->force_hotplug_required = 1;513}514515/*516* Routines for controlling stuff on the analog port517*/518519static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {520.dpms = intel_crt_dpms,521.mode_fixup = intel_crt_mode_fixup,522.prepare = intel_encoder_prepare,523.commit = intel_encoder_commit,524.mode_set = intel_crt_mode_set,525};526527static const struct drm_connector_funcs intel_crt_connector_funcs = {528.reset = intel_crt_reset,529.dpms = drm_helper_connector_dpms,530.detect = intel_crt_detect,531.fill_modes = drm_helper_probe_single_connector_modes,532.destroy = intel_crt_destroy,533.set_property = intel_crt_set_property,534};535536static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {537.mode_valid = intel_crt_mode_valid,538.get_modes = intel_crt_get_modes,539.best_encoder = intel_best_encoder,540};541542static const struct drm_encoder_funcs intel_crt_enc_funcs = {543.destroy = intel_encoder_destroy,544};545546void intel_crt_init(struct drm_device *dev)547{548struct drm_connector *connector;549struct intel_crt *crt;550struct intel_connector *intel_connector;551struct drm_i915_private *dev_priv = dev->dev_private;552553crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);554if (!crt)555return;556557intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);558if (!intel_connector) {559kfree(crt);560return;561}562563connector = &intel_connector->base;564drm_connector_init(dev, &intel_connector->base,565&intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);566567drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,568DRM_MODE_ENCODER_DAC);569570intel_connector_attach_encoder(intel_connector, &crt->base);571572crt->base.type = INTEL_OUTPUT_ANALOG;573crt->base.clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT |5741 << INTEL_ANALOG_CLONE_BIT |5751 << INTEL_SDVO_LVDS_CLONE_BIT);576crt->base.crtc_mask = (1 << 0) | (1 << 1);577connector->interlace_allowed = 1;578connector->doublescan_allowed = 0;579580drm_encoder_helper_add(&crt->base.base, &intel_crt_helper_funcs);581drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);582583drm_sysfs_connector_add(connector);584585if (I915_HAS_HOTPLUG(dev))586connector->polled = DRM_CONNECTOR_POLL_HPD;587else588connector->polled = DRM_CONNECTOR_POLL_CONNECT;589590/*591* Configure the automatic hotplug detection stuff592*/593crt->force_hotplug_required = 0;594if (HAS_PCH_SPLIT(dev)) {595u32 adpa;596597adpa = I915_READ(PCH_ADPA);598adpa &= ~ADPA_CRT_HOTPLUG_MASK;599adpa |= ADPA_HOTPLUG_BITS;600I915_WRITE(PCH_ADPA, adpa);601POSTING_READ(PCH_ADPA);602603DRM_DEBUG_KMS("pch crt adpa set to 0x%x\n", adpa);604crt->force_hotplug_required = 1;605}606607dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;608}609610611