2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2009 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Eric Anholt <eric@anholt.net>
26 * Jesse Barnes <jesse.barnes@intel.com>
29 #include <linux/i2c.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
36 #include "intel_drv.h"
40 struct intel_hdmi_priv
{
45 static void intel_hdmi_mode_set(struct drm_encoder
*encoder
,
46 struct drm_display_mode
*mode
,
47 struct drm_display_mode
*adjusted_mode
)
49 struct drm_device
*dev
= encoder
->dev
;
50 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
51 struct drm_crtc
*crtc
= encoder
->crtc
;
52 struct intel_crtc
*intel_crtc
= to_intel_crtc(crtc
);
53 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
54 struct intel_hdmi_priv
*hdmi_priv
= intel_encoder
->dev_priv
;
57 sdvox
= SDVO_ENCODING_HDMI
| SDVO_BORDER_ENABLE
;
58 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
59 sdvox
|= SDVO_VSYNC_ACTIVE_HIGH
;
60 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
61 sdvox
|= SDVO_HSYNC_ACTIVE_HIGH
;
63 if (hdmi_priv
->has_hdmi_sink
) {
64 sdvox
|= SDVO_AUDIO_ENABLE
;
66 sdvox
|= HDMI_MODE_SELECT
;
69 if (intel_crtc
->pipe
== 1) {
71 sdvox
|= PORT_TRANS_B_SEL_CPT
;
73 sdvox
|= SDVO_PIPE_B_SELECT
;
76 I915_WRITE(hdmi_priv
->sdvox_reg
, sdvox
);
77 POSTING_READ(hdmi_priv
->sdvox_reg
);
80 static void intel_hdmi_dpms(struct drm_encoder
*encoder
, int mode
)
82 struct drm_device
*dev
= encoder
->dev
;
83 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
84 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
85 struct intel_hdmi_priv
*hdmi_priv
= intel_encoder
->dev_priv
;
88 temp
= I915_READ(hdmi_priv
->sdvox_reg
);
90 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
91 * we do this anyway which shows more stable in testing.
93 if (HAS_PCH_SPLIT(dev
)) {
94 I915_WRITE(hdmi_priv
->sdvox_reg
, temp
& ~SDVO_ENABLE
);
95 POSTING_READ(hdmi_priv
->sdvox_reg
);
98 if (mode
!= DRM_MODE_DPMS_ON
) {
104 I915_WRITE(hdmi_priv
->sdvox_reg
, temp
);
105 POSTING_READ(hdmi_priv
->sdvox_reg
);
107 /* HW workaround, need to write this twice for issue that may result
108 * in first write getting masked.
110 if (HAS_PCH_SPLIT(dev
)) {
111 I915_WRITE(hdmi_priv
->sdvox_reg
, temp
);
112 POSTING_READ(hdmi_priv
->sdvox_reg
);
116 static int intel_hdmi_mode_valid(struct drm_connector
*connector
,
117 struct drm_display_mode
*mode
)
119 if (mode
->clock
> 165000)
120 return MODE_CLOCK_HIGH
;
121 if (mode
->clock
< 20000)
122 return MODE_CLOCK_HIGH
;
124 if (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
125 return MODE_NO_DBLESCAN
;
130 static bool intel_hdmi_mode_fixup(struct drm_encoder
*encoder
,
131 struct drm_display_mode
*mode
,
132 struct drm_display_mode
*adjusted_mode
)
137 static enum drm_connector_status
138 intel_hdmi_detect(struct drm_connector
*connector
)
140 struct drm_encoder
*encoder
= intel_attached_encoder(connector
);
141 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
142 struct intel_hdmi_priv
*hdmi_priv
= intel_encoder
->dev_priv
;
143 struct edid
*edid
= NULL
;
144 enum drm_connector_status status
= connector_status_disconnected
;
146 hdmi_priv
->has_hdmi_sink
= false;
147 edid
= drm_get_edid(connector
,
148 intel_encoder
->ddc_bus
);
151 if (edid
->input
& DRM_EDID_INPUT_DIGITAL
) {
152 status
= connector_status_connected
;
153 hdmi_priv
->has_hdmi_sink
= drm_detect_hdmi_monitor(edid
);
155 connector
->display_info
.raw_edid
= NULL
;
162 static int intel_hdmi_get_modes(struct drm_connector
*connector
)
164 struct drm_encoder
*encoder
= intel_attached_encoder(connector
);
165 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
167 /* We should parse the EDID data and find out if it's an HDMI sink so
168 * we can send audio to it.
171 return intel_ddc_get_modes(connector
, intel_encoder
->ddc_bus
);
174 static void intel_hdmi_destroy(struct drm_connector
*connector
)
176 drm_sysfs_connector_remove(connector
);
177 drm_connector_cleanup(connector
);
181 static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs
= {
182 .dpms
= intel_hdmi_dpms
,
183 .mode_fixup
= intel_hdmi_mode_fixup
,
184 .prepare
= intel_encoder_prepare
,
185 .mode_set
= intel_hdmi_mode_set
,
186 .commit
= intel_encoder_commit
,
189 static const struct drm_connector_funcs intel_hdmi_connector_funcs
= {
190 .dpms
= drm_helper_connector_dpms
,
191 .detect
= intel_hdmi_detect
,
192 .fill_modes
= drm_helper_probe_single_connector_modes
,
193 .destroy
= intel_hdmi_destroy
,
196 static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs
= {
197 .get_modes
= intel_hdmi_get_modes
,
198 .mode_valid
= intel_hdmi_mode_valid
,
199 .best_encoder
= intel_attached_encoder
,
202 static void intel_hdmi_enc_destroy(struct drm_encoder
*encoder
)
204 struct intel_encoder
*intel_encoder
= enc_to_intel_encoder(encoder
);
206 if (intel_encoder
->i2c_bus
)
207 intel_i2c_destroy(intel_encoder
->i2c_bus
);
208 drm_encoder_cleanup(encoder
);
209 kfree(intel_encoder
);
212 static const struct drm_encoder_funcs intel_hdmi_enc_funcs
= {
213 .destroy
= intel_hdmi_enc_destroy
,
216 void intel_hdmi_init(struct drm_device
*dev
, int sdvox_reg
)
218 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
219 struct drm_connector
*connector
;
220 struct intel_encoder
*intel_encoder
;
221 struct intel_connector
*intel_connector
;
222 struct intel_hdmi_priv
*hdmi_priv
;
224 intel_encoder
= kcalloc(sizeof(struct intel_encoder
) +
225 sizeof(struct intel_hdmi_priv
), 1, GFP_KERNEL
);
229 intel_connector
= kzalloc(sizeof(struct intel_connector
), GFP_KERNEL
);
230 if (!intel_connector
) {
231 kfree(intel_encoder
);
235 hdmi_priv
= (struct intel_hdmi_priv
*)(intel_encoder
+ 1);
237 connector
= &intel_connector
->base
;
238 drm_connector_init(dev
, connector
, &intel_hdmi_connector_funcs
,
239 DRM_MODE_CONNECTOR_HDMIA
);
240 drm_connector_helper_add(connector
, &intel_hdmi_connector_helper_funcs
);
242 intel_encoder
->type
= INTEL_OUTPUT_HDMI
;
244 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
245 connector
->interlace_allowed
= 0;
246 connector
->doublescan_allowed
= 0;
247 intel_encoder
->crtc_mask
= (1 << 0) | (1 << 1);
249 /* Set up the DDC bus. */
250 if (sdvox_reg
== SDVOB
) {
251 intel_encoder
->clone_mask
= (1 << INTEL_HDMIB_CLONE_BIT
);
252 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, GPIOE
, "HDMIB");
253 dev_priv
->hotplug_supported_mask
|= HDMIB_HOTPLUG_INT_STATUS
;
254 } else if (sdvox_reg
== SDVOC
) {
255 intel_encoder
->clone_mask
= (1 << INTEL_HDMIC_CLONE_BIT
);
256 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, GPIOD
, "HDMIC");
257 dev_priv
->hotplug_supported_mask
|= HDMIC_HOTPLUG_INT_STATUS
;
258 } else if (sdvox_reg
== HDMIB
) {
259 intel_encoder
->clone_mask
= (1 << INTEL_HDMID_CLONE_BIT
);
260 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, PCH_GPIOE
,
262 dev_priv
->hotplug_supported_mask
|= HDMIB_HOTPLUG_INT_STATUS
;
263 } else if (sdvox_reg
== HDMIC
) {
264 intel_encoder
->clone_mask
= (1 << INTEL_HDMIE_CLONE_BIT
);
265 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, PCH_GPIOD
,
267 dev_priv
->hotplug_supported_mask
|= HDMIC_HOTPLUG_INT_STATUS
;
268 } else if (sdvox_reg
== HDMID
) {
269 intel_encoder
->clone_mask
= (1 << INTEL_HDMIF_CLONE_BIT
);
270 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, PCH_GPIOF
,
272 dev_priv
->hotplug_supported_mask
|= HDMID_HOTPLUG_INT_STATUS
;
274 if (!intel_encoder
->ddc_bus
)
277 hdmi_priv
->sdvox_reg
= sdvox_reg
;
278 intel_encoder
->dev_priv
= hdmi_priv
;
280 drm_encoder_init(dev
, &intel_encoder
->enc
, &intel_hdmi_enc_funcs
,
281 DRM_MODE_ENCODER_TMDS
);
282 drm_encoder_helper_add(&intel_encoder
->enc
, &intel_hdmi_helper_funcs
);
284 drm_mode_connector_attach_encoder(&intel_connector
->base
,
285 &intel_encoder
->enc
);
286 drm_sysfs_connector_add(connector
);
288 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
289 * 0xd. Failure to do so will result in spurious interrupts being
290 * generated on the port when a cable is not attached.
292 if (IS_G4X(dev
) && !IS_GM45(dev
)) {
293 u32 temp
= I915_READ(PEG_BAND_GAP_DATA
);
294 I915_WRITE(PEG_BAND_GAP_DATA
, (temp
& ~0xf) | 0xd);
300 drm_connector_cleanup(connector
);
301 kfree(intel_encoder
);
302 kfree(intel_connector
);