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"
41 struct intel_encoder base
;
46 static struct intel_hdmi
*enc_to_intel_hdmi(struct drm_encoder
*encoder
)
48 return container_of(enc_to_intel_encoder(encoder
), struct intel_hdmi
, base
);
51 static void intel_hdmi_mode_set(struct drm_encoder
*encoder
,
52 struct drm_display_mode
*mode
,
53 struct drm_display_mode
*adjusted_mode
)
55 struct drm_device
*dev
= encoder
->dev
;
56 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
57 struct drm_crtc
*crtc
= encoder
->crtc
;
58 struct intel_crtc
*intel_crtc
= to_intel_crtc(crtc
);
59 struct intel_hdmi
*intel_hdmi
= enc_to_intel_hdmi(encoder
);
62 sdvox
= SDVO_ENCODING_HDMI
| SDVO_BORDER_ENABLE
;
63 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
64 sdvox
|= SDVO_VSYNC_ACTIVE_HIGH
;
65 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
66 sdvox
|= SDVO_HSYNC_ACTIVE_HIGH
;
68 if (intel_hdmi
->has_hdmi_sink
) {
69 sdvox
|= SDVO_AUDIO_ENABLE
;
71 sdvox
|= HDMI_MODE_SELECT
;
74 if (intel_crtc
->pipe
== 1) {
76 sdvox
|= PORT_TRANS_B_SEL_CPT
;
78 sdvox
|= SDVO_PIPE_B_SELECT
;
81 I915_WRITE(intel_hdmi
->sdvox_reg
, sdvox
);
82 POSTING_READ(intel_hdmi
->sdvox_reg
);
85 static void intel_hdmi_dpms(struct drm_encoder
*encoder
, int mode
)
87 struct drm_device
*dev
= encoder
->dev
;
88 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
89 struct intel_hdmi
*intel_hdmi
= enc_to_intel_hdmi(encoder
);
92 temp
= I915_READ(intel_hdmi
->sdvox_reg
);
94 if (HAS_PCH_SPLIT(dev
)) {
95 I915_WRITE(intel_hdmi
->sdvox_reg
, temp
& ~SDVO_ENABLE
);
96 POSTING_READ(intel_hdmi
->sdvox_reg
);
99 if (mode
!= DRM_MODE_DPMS_ON
) {
100 temp
&= ~SDVO_ENABLE
;
105 I915_WRITE(intel_hdmi
->sdvox_reg
, temp
);
106 POSTING_READ(intel_hdmi
->sdvox_reg
);
108 if (HAS_PCH_SPLIT(dev
)) {
109 I915_WRITE(intel_hdmi
->sdvox_reg
, temp
);
110 POSTING_READ(intel_hdmi
->sdvox_reg
);
114 static int intel_hdmi_mode_valid(struct drm_connector
*connector
,
115 struct drm_display_mode
*mode
)
117 if (mode
->clock
> 165000)
118 return MODE_CLOCK_HIGH
;
119 if (mode
->clock
< 20000)
120 return MODE_CLOCK_HIGH
;
122 if (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
123 return MODE_NO_DBLESCAN
;
128 static bool intel_hdmi_mode_fixup(struct drm_encoder
*encoder
,
129 struct drm_display_mode
*mode
,
130 struct drm_display_mode
*adjusted_mode
)
135 static enum drm_connector_status
136 intel_hdmi_detect(struct drm_connector
*connector
, bool force
)
138 struct drm_encoder
*encoder
= intel_attached_encoder(connector
);
139 struct intel_hdmi
*intel_hdmi
= enc_to_intel_hdmi(encoder
);
140 struct edid
*edid
= NULL
;
141 enum drm_connector_status status
= connector_status_disconnected
;
143 intel_hdmi
->has_hdmi_sink
= false;
144 edid
= drm_get_edid(connector
, intel_hdmi
->base
.ddc_bus
);
147 if (edid
->input
& DRM_EDID_INPUT_DIGITAL
) {
148 status
= connector_status_connected
;
149 intel_hdmi
->has_hdmi_sink
= drm_detect_hdmi_monitor(edid
);
151 connector
->display_info
.raw_edid
= NULL
;
158 static int intel_hdmi_get_modes(struct drm_connector
*connector
)
160 struct drm_encoder
*encoder
= intel_attached_encoder(connector
);
161 struct intel_hdmi
*intel_hdmi
= enc_to_intel_hdmi(encoder
);
163 /* We should parse the EDID data and find out if it's an HDMI sink so
164 * we can send audio to it.
167 return intel_ddc_get_modes(connector
, intel_hdmi
->base
.ddc_bus
);
170 static void intel_hdmi_destroy(struct drm_connector
*connector
)
172 drm_sysfs_connector_remove(connector
);
173 drm_connector_cleanup(connector
);
177 static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs
= {
178 .dpms
= intel_hdmi_dpms
,
179 .mode_fixup
= intel_hdmi_mode_fixup
,
180 .prepare
= intel_encoder_prepare
,
181 .mode_set
= intel_hdmi_mode_set
,
182 .commit
= intel_encoder_commit
,
185 static const struct drm_connector_funcs intel_hdmi_connector_funcs
= {
186 .dpms
= drm_helper_connector_dpms
,
187 .detect
= intel_hdmi_detect
,
188 .fill_modes
= drm_helper_probe_single_connector_modes
,
189 .destroy
= intel_hdmi_destroy
,
192 static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs
= {
193 .get_modes
= intel_hdmi_get_modes
,
194 .mode_valid
= intel_hdmi_mode_valid
,
195 .best_encoder
= intel_attached_encoder
,
198 static const struct drm_encoder_funcs intel_hdmi_enc_funcs
= {
199 .destroy
= intel_encoder_destroy
,
202 void intel_hdmi_init(struct drm_device
*dev
, int sdvox_reg
)
204 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
205 struct drm_connector
*connector
;
206 struct intel_encoder
*intel_encoder
;
207 struct intel_connector
*intel_connector
;
208 struct intel_hdmi
*intel_hdmi
;
210 intel_hdmi
= kzalloc(sizeof(struct intel_hdmi
), GFP_KERNEL
);
214 intel_connector
= kzalloc(sizeof(struct intel_connector
), GFP_KERNEL
);
215 if (!intel_connector
) {
220 intel_encoder
= &intel_hdmi
->base
;
221 connector
= &intel_connector
->base
;
222 drm_connector_init(dev
, connector
, &intel_hdmi_connector_funcs
,
223 DRM_MODE_CONNECTOR_HDMIA
);
224 drm_connector_helper_add(connector
, &intel_hdmi_connector_helper_funcs
);
226 intel_encoder
->type
= INTEL_OUTPUT_HDMI
;
228 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
229 connector
->interlace_allowed
= 0;
230 connector
->doublescan_allowed
= 0;
231 intel_encoder
->crtc_mask
= (1 << 0) | (1 << 1);
233 /* Set up the DDC bus. */
234 if (sdvox_reg
== SDVOB
) {
235 intel_encoder
->clone_mask
= (1 << INTEL_HDMIB_CLONE_BIT
);
236 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, GPIOE
, "HDMIB");
237 dev_priv
->hotplug_supported_mask
|= HDMIB_HOTPLUG_INT_STATUS
;
238 } else if (sdvox_reg
== SDVOC
) {
239 intel_encoder
->clone_mask
= (1 << INTEL_HDMIC_CLONE_BIT
);
240 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, GPIOD
, "HDMIC");
241 dev_priv
->hotplug_supported_mask
|= HDMIC_HOTPLUG_INT_STATUS
;
242 } else if (sdvox_reg
== HDMIB
) {
243 intel_encoder
->clone_mask
= (1 << INTEL_HDMID_CLONE_BIT
);
244 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, PCH_GPIOE
,
246 dev_priv
->hotplug_supported_mask
|= HDMIB_HOTPLUG_INT_STATUS
;
247 } else if (sdvox_reg
== HDMIC
) {
248 intel_encoder
->clone_mask
= (1 << INTEL_HDMIE_CLONE_BIT
);
249 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, PCH_GPIOD
,
251 dev_priv
->hotplug_supported_mask
|= HDMIC_HOTPLUG_INT_STATUS
;
252 } else if (sdvox_reg
== HDMID
) {
253 intel_encoder
->clone_mask
= (1 << INTEL_HDMIF_CLONE_BIT
);
254 intel_encoder
->ddc_bus
= intel_i2c_create(dev
, PCH_GPIOF
,
256 dev_priv
->hotplug_supported_mask
|= HDMID_HOTPLUG_INT_STATUS
;
258 if (!intel_encoder
->ddc_bus
)
261 intel_hdmi
->sdvox_reg
= sdvox_reg
;
263 drm_encoder_init(dev
, &intel_encoder
->enc
, &intel_hdmi_enc_funcs
,
264 DRM_MODE_ENCODER_TMDS
);
265 drm_encoder_helper_add(&intel_encoder
->enc
, &intel_hdmi_helper_funcs
);
267 drm_mode_connector_attach_encoder(&intel_connector
->base
,
268 &intel_encoder
->enc
);
269 drm_sysfs_connector_add(connector
);
271 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
272 * 0xd. Failure to do so will result in spurious interrupts being
273 * generated on the port when a cable is not attached.
275 if (IS_G4X(dev
) && !IS_GM45(dev
)) {
276 u32 temp
= I915_READ(PEG_BAND_GAP_DATA
);
277 I915_WRITE(PEG_BAND_GAP_DATA
, (temp
& ~0xf) | 0xd);
283 drm_connector_cleanup(connector
);
285 kfree(intel_connector
);