blob: 539073ec56bd63ddb609eebdf850387383e93ce4 [file] [log] [blame]
Eric Anholt7d573822009-01-02 13:33:00 -08001/*
2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2009 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 *
24 * Authors:
25 * Eric Anholt <eric@anholt.net>
26 * Jesse Barnes <jesse.barnes@intel.com>
27 */
28
29#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Eric Anholt7d573822009-01-02 13:33:00 -080031#include <linux/delay.h>
32#include "drmP.h"
33#include "drm.h"
34#include "drm_crtc.h"
Keith Packardaa93d632009-05-05 09:52:46 -070035#include "drm_edid.h"
Eric Anholt7d573822009-01-02 13:33:00 -080036#include "intel_drv.h"
37#include "i915_drm.h"
38#include "i915_drv.h"
39
Chris Wilsonea5b2132010-08-04 13:50:23 +010040struct intel_hdmi {
41 struct intel_encoder base;
Eric Anholt7d573822009-01-02 13:33:00 -080042 u32 sdvox_reg;
Chris Wilsonf899fc62010-07-20 15:44:45 -070043 int ddc_bus;
Chris Wilsone953fd72011-02-21 22:23:52 +000044 uint32_t color_range;
Ma Ling9dff6af2009-04-02 13:13:26 +080045 bool has_hdmi_sink;
Zhenyu Wang2e3d6002010-09-10 10:39:40 +080046 bool has_audio;
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +080047 enum hdmi_force_audio force_audio;
Jesse Barnes45187ac2011-08-03 09:22:55 -070048 void (*write_infoframe)(struct drm_encoder *encoder,
49 struct dip_infoframe *frame);
Eric Anholt7d573822009-01-02 13:33:00 -080050};
51
Chris Wilsonea5b2132010-08-04 13:50:23 +010052static struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
53{
Chris Wilson4ef69c72010-09-09 15:14:28 +010054 return container_of(encoder, struct intel_hdmi, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010055}
56
Chris Wilsondf0e9242010-09-09 16:20:55 +010057static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
58{
59 return container_of(intel_attached_encoder(connector),
60 struct intel_hdmi, base);
61}
62
Jesse Barnes45187ac2011-08-03 09:22:55 -070063void intel_dip_infoframe_csum(struct dip_infoframe *frame)
David Härdeman3c17fe42010-09-24 21:44:32 +020064{
Jesse Barnes45187ac2011-08-03 09:22:55 -070065 uint8_t *data = (uint8_t *)frame;
David Härdeman3c17fe42010-09-24 21:44:32 +020066 uint8_t sum = 0;
67 unsigned i;
68
Jesse Barnes45187ac2011-08-03 09:22:55 -070069 frame->checksum = 0;
70 frame->ecc = 0;
David Härdeman3c17fe42010-09-24 21:44:32 +020071
Jesse Barnes64a8fc02011-09-22 11:16:00 +053072 for (i = 0; i < frame->len + DIP_HEADER_SIZE; i++)
David Härdeman3c17fe42010-09-24 21:44:32 +020073 sum += data[i];
74
Jesse Barnes45187ac2011-08-03 09:22:55 -070075 frame->checksum = 0x100 - sum;
David Härdeman3c17fe42010-09-24 21:44:32 +020076}
77
Jesse Barnes45187ac2011-08-03 09:22:55 -070078static u32 intel_infoframe_index(struct dip_infoframe *frame)
David Härdeman3c17fe42010-09-24 21:44:32 +020079{
Jesse Barnes45187ac2011-08-03 09:22:55 -070080 u32 flags = 0;
81
82 switch (frame->type) {
83 case DIP_TYPE_AVI:
84 flags |= VIDEO_DIP_SELECT_AVI;
85 break;
86 case DIP_TYPE_SPD:
87 flags |= VIDEO_DIP_SELECT_SPD;
88 break;
89 default:
90 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
91 break;
92 }
93
94 return flags;
95}
96
Paulo Zanonifa193ff2012-05-04 17:18:20 -030097static u32 intel_infoframe_enable(struct dip_infoframe *frame)
Jesse Barnes45187ac2011-08-03 09:22:55 -070098{
99 u32 flags = 0;
100
101 switch (frame->type) {
102 case DIP_TYPE_AVI:
Paulo Zanonifa193ff2012-05-04 17:18:20 -0300103 flags |= VIDEO_DIP_ENABLE_AVI;
Jesse Barnes45187ac2011-08-03 09:22:55 -0700104 break;
105 case DIP_TYPE_SPD:
Paulo Zanonifa193ff2012-05-04 17:18:20 -0300106 flags |= VIDEO_DIP_ENABLE_SPD;
107 break;
108 default:
109 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
110 break;
111 }
112
113 return flags;
114}
115
116static u32 intel_infoframe_frequency(struct dip_infoframe *frame)
117{
118 u32 flags = 0;
119
120 switch (frame->type) {
121 case DIP_TYPE_AVI:
122 case DIP_TYPE_SPD:
123 flags |= VIDEO_DIP_FREQ_VSYNC;
Jesse Barnes45187ac2011-08-03 09:22:55 -0700124 break;
125 default:
126 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
127 break;
128 }
129
130 return flags;
131}
132
133static void i9xx_write_infoframe(struct drm_encoder *encoder,
134 struct dip_infoframe *frame)
135{
136 uint32_t *data = (uint32_t *)frame;
David Härdeman3c17fe42010-09-24 21:44:32 +0200137 struct drm_device *dev = encoder->dev;
138 struct drm_i915_private *dev_priv = dev->dev_private;
139 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300140 u32 val = I915_READ(VIDEO_DIP_CTL);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700141 unsigned i, len = DIP_HEADER_SIZE + frame->len;
David Härdeman3c17fe42010-09-24 21:44:32 +0200142
David Härdeman3c17fe42010-09-24 21:44:32 +0200143
144 /* XXX first guess at handling video port, is this corrent? */
Paulo Zanoni3e6e6392012-05-04 17:18:19 -0300145 val &= ~VIDEO_DIP_PORT_MASK;
David Härdeman3c17fe42010-09-24 21:44:32 +0200146 if (intel_hdmi->sdvox_reg == SDVOB)
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300147 val |= VIDEO_DIP_PORT_B;
David Härdeman3c17fe42010-09-24 21:44:32 +0200148 else if (intel_hdmi->sdvox_reg == SDVOC)
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300149 val |= VIDEO_DIP_PORT_C;
David Härdeman3c17fe42010-09-24 21:44:32 +0200150 else
151 return;
152
Paulo Zanoni1d4f85a2012-05-04 17:18:18 -0300153 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300154 val |= intel_infoframe_index(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700155
Paulo Zanoniecb97852012-05-04 17:18:21 -0300156 val &= ~intel_infoframe_enable(frame);
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300157 val |= VIDEO_DIP_ENABLE;
158
159 I915_WRITE(VIDEO_DIP_CTL, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700160
161 for (i = 0; i < len; i += 4) {
David Härdeman3c17fe42010-09-24 21:44:32 +0200162 I915_WRITE(VIDEO_DIP_DATA, *data);
163 data++;
164 }
165
Paulo Zanonifa193ff2012-05-04 17:18:20 -0300166 val |= intel_infoframe_enable(frame);
Paulo Zanoni60c5ea22012-05-04 17:18:22 -0300167 val &= ~VIDEO_DIP_FREQ_MASK;
Paulo Zanonifa193ff2012-05-04 17:18:20 -0300168 val |= intel_infoframe_frequency(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700169
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300170 I915_WRITE(VIDEO_DIP_CTL, val);
David Härdeman3c17fe42010-09-24 21:44:32 +0200171}
172
Paulo Zanonifdf12502012-05-04 17:18:24 -0300173static void ibx_write_infoframe(struct drm_encoder *encoder,
174 struct dip_infoframe *frame)
175{
176 uint32_t *data = (uint32_t *)frame;
177 struct drm_device *dev = encoder->dev;
178 struct drm_i915_private *dev_priv = dev->dev_private;
179 struct drm_crtc *crtc = encoder->crtc;
180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
181 int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
182 unsigned i, len = DIP_HEADER_SIZE + frame->len;
183 u32 val = I915_READ(reg);
184
185 intel_wait_for_vblank(dev, intel_crtc->pipe);
186
187 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
188 val |= intel_infoframe_index(frame);
189
Paulo Zanoni4dc20c02012-05-04 17:18:25 -0300190 val &= ~intel_infoframe_enable(frame);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300191 val |= VIDEO_DIP_ENABLE;
192
193 I915_WRITE(reg, val);
194
195 for (i = 0; i < len; i += 4) {
196 I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
197 data++;
198 }
199
200 val |= intel_infoframe_enable(frame);
201 val &= ~VIDEO_DIP_FREQ_MASK;
202 val |= intel_infoframe_frequency(frame);
203
204 I915_WRITE(reg, val);
205}
206
207static void cpt_write_infoframe(struct drm_encoder *encoder,
208 struct dip_infoframe *frame)
Jesse Barnes45187ac2011-08-03 09:22:55 -0700209{
210 uint32_t *data = (uint32_t *)frame;
211 struct drm_device *dev = encoder->dev;
212 struct drm_i915_private *dev_priv = dev->dev_private;
213 struct drm_crtc *crtc = encoder->crtc;
214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
215 int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
216 unsigned i, len = DIP_HEADER_SIZE + frame->len;
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300217 u32 val = I915_READ(reg);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700218
219 intel_wait_for_vblank(dev, intel_crtc->pipe);
220
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530221 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300222 val |= intel_infoframe_index(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700223
Paulo Zanoniecb97852012-05-04 17:18:21 -0300224 /* The DIP control register spec says that we need to update the AVI
225 * infoframe without clearing its enable bit */
226 if (frame->type == DIP_TYPE_AVI)
227 val |= VIDEO_DIP_ENABLE_AVI;
228 else
229 val &= ~intel_infoframe_enable(frame);
230
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300231 val |= VIDEO_DIP_ENABLE;
232
233 I915_WRITE(reg, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700234
235 for (i = 0; i < len; i += 4) {
236 I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
237 data++;
238 }
239
Paulo Zanonifa193ff2012-05-04 17:18:20 -0300240 val |= intel_infoframe_enable(frame);
Paulo Zanoni60c5ea22012-05-04 17:18:22 -0300241 val &= ~VIDEO_DIP_FREQ_MASK;
Paulo Zanonifa193ff2012-05-04 17:18:20 -0300242 val |= intel_infoframe_frequency(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700243
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300244 I915_WRITE(reg, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700245}
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700246
247static void vlv_write_infoframe(struct drm_encoder *encoder,
248 struct dip_infoframe *frame)
249{
250 uint32_t *data = (uint32_t *)frame;
251 struct drm_device *dev = encoder->dev;
252 struct drm_i915_private *dev_priv = dev->dev_private;
253 struct drm_crtc *crtc = encoder->crtc;
254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
255 int reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
256 unsigned i, len = DIP_HEADER_SIZE + frame->len;
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300257 u32 val = I915_READ(reg);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700258
259 intel_wait_for_vblank(dev, intel_crtc->pipe);
260
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700261 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300262 val |= intel_infoframe_index(frame);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700263
Paulo Zanoniecb97852012-05-04 17:18:21 -0300264 val &= ~intel_infoframe_enable(frame);
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300265 val |= VIDEO_DIP_ENABLE;
266
267 I915_WRITE(reg, val);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700268
269 for (i = 0; i < len; i += 4) {
270 I915_WRITE(VLV_TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
271 data++;
272 }
273
Paulo Zanonifa193ff2012-05-04 17:18:20 -0300274 val |= intel_infoframe_enable(frame);
Paulo Zanoni60c5ea22012-05-04 17:18:22 -0300275 val &= ~VIDEO_DIP_FREQ_MASK;
Paulo Zanonifa193ff2012-05-04 17:18:20 -0300276 val |= intel_infoframe_frequency(frame);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700277
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300278 I915_WRITE(reg, val);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700279}
280
Jesse Barnes45187ac2011-08-03 09:22:55 -0700281static void intel_set_infoframe(struct drm_encoder *encoder,
282 struct dip_infoframe *frame)
283{
284 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
285
286 if (!intel_hdmi->has_hdmi_sink)
287 return;
288
289 intel_dip_infoframe_csum(frame);
290 intel_hdmi->write_infoframe(encoder, frame);
291}
292
Paulo Zanonic846b612012-04-13 16:31:41 -0300293static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder,
294 struct drm_display_mode *adjusted_mode)
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700295{
296 struct dip_infoframe avi_if = {
297 .type = DIP_TYPE_AVI,
298 .ver = DIP_VERSION_AVI,
299 .len = DIP_LEN_AVI,
300 };
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700301
Paulo Zanonic846b612012-04-13 16:31:41 -0300302 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
303 avi_if.body.avi.YQ_CN_PR |= DIP_AVI_PR_2;
304
Jesse Barnes45187ac2011-08-03 09:22:55 -0700305 intel_set_infoframe(encoder, &avi_if);
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700306}
307
Jesse Barnesc0864cb2011-08-03 09:22:56 -0700308static void intel_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
309{
310 struct dip_infoframe spd_if;
311
312 memset(&spd_if, 0, sizeof(spd_if));
313 spd_if.type = DIP_TYPE_SPD;
314 spd_if.ver = DIP_VERSION_SPD;
315 spd_if.len = DIP_LEN_SPD;
316 strcpy(spd_if.body.spd.vn, "Intel");
317 strcpy(spd_if.body.spd.pd, "Integrated gfx");
318 spd_if.body.spd.sdi = DIP_SPD_PC;
319
320 intel_set_infoframe(encoder, &spd_if);
321}
322
Eric Anholt7d573822009-01-02 13:33:00 -0800323static void intel_hdmi_mode_set(struct drm_encoder *encoder,
324 struct drm_display_mode *mode,
325 struct drm_display_mode *adjusted_mode)
326{
327 struct drm_device *dev = encoder->dev;
328 struct drm_i915_private *dev_priv = dev->dev_private;
329 struct drm_crtc *crtc = encoder->crtc;
330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100331 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800332 u32 sdvox;
333
Adam Jacksonb599c0b2010-07-16 14:46:31 -0400334 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
Jesse Barnes5d4fac92011-06-24 12:19:19 -0700335 if (!HAS_PCH_SPLIT(dev))
336 sdvox |= intel_hdmi->color_range;
Adam Jacksonb599c0b2010-07-16 14:46:31 -0400337 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
338 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
339 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
340 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -0800341
Jesse Barnes020f6702011-06-24 12:19:25 -0700342 if (intel_crtc->bpp > 24)
343 sdvox |= COLOR_FORMAT_12bpc;
344 else
345 sdvox |= COLOR_FORMAT_8bpc;
346
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800347 /* Required on CPT */
348 if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
349 sdvox |= HDMI_MODE_SELECT;
350
David Härdeman3c17fe42010-09-24 21:44:32 +0200351 if (intel_hdmi->has_audio) {
Wu Fengguange0dac652011-09-05 14:25:34 +0800352 DRM_DEBUG_DRIVER("Enabling HDMI audio on pipe %c\n",
353 pipe_name(intel_crtc->pipe));
Eric Anholt7d573822009-01-02 13:33:00 -0800354 sdvox |= SDVO_AUDIO_ENABLE;
David Härdeman3c17fe42010-09-24 21:44:32 +0200355 sdvox |= SDVO_NULL_PACKETS_DURING_VSYNC;
Wu Fengguange0dac652011-09-05 14:25:34 +0800356 intel_write_eld(encoder, adjusted_mode);
David Härdeman3c17fe42010-09-24 21:44:32 +0200357 }
Eric Anholt7d573822009-01-02 13:33:00 -0800358
Jesse Barnes75770562011-10-12 09:01:58 -0700359 if (HAS_PCH_CPT(dev))
360 sdvox |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
361 else if (intel_crtc->pipe == 1)
362 sdvox |= SDVO_PIPE_B_SELECT;
Eric Anholt7d573822009-01-02 13:33:00 -0800363
Chris Wilsonea5b2132010-08-04 13:50:23 +0100364 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
365 POSTING_READ(intel_hdmi->sdvox_reg);
David Härdeman3c17fe42010-09-24 21:44:32 +0200366
Paulo Zanonic846b612012-04-13 16:31:41 -0300367 intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
Jesse Barnesc0864cb2011-08-03 09:22:56 -0700368 intel_hdmi_set_spd_infoframe(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800369}
370
371static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
372{
373 struct drm_device *dev = encoder->dev;
374 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100375 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800376 u32 temp;
Wu Fengguang2deed762011-12-09 20:42:20 +0800377 u32 enable_bits = SDVO_ENABLE;
378
379 if (intel_hdmi->has_audio)
380 enable_bits |= SDVO_AUDIO_ENABLE;
Eric Anholt7d573822009-01-02 13:33:00 -0800381
Chris Wilsonea5b2132010-08-04 13:50:23 +0100382 temp = I915_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000383
384 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
385 * we do this anyway which shows more stable in testing.
386 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800387 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100388 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
389 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -0800390 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000391
392 if (mode != DRM_MODE_DPMS_ON) {
Wu Fengguang2deed762011-12-09 20:42:20 +0800393 temp &= ~enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000394 } else {
Wu Fengguang2deed762011-12-09 20:42:20 +0800395 temp |= enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000396 }
397
Chris Wilsonea5b2132010-08-04 13:50:23 +0100398 I915_WRITE(intel_hdmi->sdvox_reg, temp);
399 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000400
401 /* HW workaround, need to write this twice for issue that may result
402 * in first write getting masked.
403 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800404 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100405 I915_WRITE(intel_hdmi->sdvox_reg, temp);
406 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000407 }
Eric Anholt7d573822009-01-02 13:33:00 -0800408}
409
Eric Anholt7d573822009-01-02 13:33:00 -0800410static int intel_hdmi_mode_valid(struct drm_connector *connector,
411 struct drm_display_mode *mode)
412{
413 if (mode->clock > 165000)
414 return MODE_CLOCK_HIGH;
415 if (mode->clock < 20000)
Nicolas Kaiser5cbba412011-05-30 12:48:26 +0200416 return MODE_CLOCK_LOW;
Eric Anholt7d573822009-01-02 13:33:00 -0800417
418 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
419 return MODE_NO_DBLESCAN;
420
421 return MODE_OK;
422}
423
424static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
425 struct drm_display_mode *mode,
426 struct drm_display_mode *adjusted_mode)
427{
428 return true;
429}
430
Keith Packardaa93d632009-05-05 09:52:46 -0700431static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100432intel_hdmi_detect(struct drm_connector *connector, bool force)
Ma Ling9dff6af2009-04-02 13:13:26 +0800433{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100434 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700435 struct drm_i915_private *dev_priv = connector->dev->dev_private;
436 struct edid *edid;
Keith Packardaa93d632009-05-05 09:52:46 -0700437 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800438
Chris Wilsonea5b2132010-08-04 13:50:23 +0100439 intel_hdmi->has_hdmi_sink = false;
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800440 intel_hdmi->has_audio = false;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700441 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800442 intel_gmbus_get_adapter(dev_priv,
443 intel_hdmi->ddc_bus));
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800444
Keith Packardaa93d632009-05-05 09:52:46 -0700445 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700446 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700447 status = connector_status_connected;
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800448 if (intel_hdmi->force_audio != HDMI_AUDIO_OFF_DVI)
449 intel_hdmi->has_hdmi_sink =
450 drm_detect_hdmi_monitor(edid);
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800451 intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
Keith Packardaa93d632009-05-05 09:52:46 -0700452 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800453 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700454 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800455 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800456
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100457 if (status == connector_status_connected) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800458 if (intel_hdmi->force_audio != HDMI_AUDIO_AUTO)
459 intel_hdmi->has_audio =
460 (intel_hdmi->force_audio == HDMI_AUDIO_ON);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100461 }
462
Keith Packardaa93d632009-05-05 09:52:46 -0700463 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800464}
465
Eric Anholt7d573822009-01-02 13:33:00 -0800466static int intel_hdmi_get_modes(struct drm_connector *connector)
467{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100468 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700469 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Eric Anholt7d573822009-01-02 13:33:00 -0800470
471 /* We should parse the EDID data and find out if it's an HDMI sink so
472 * we can send audio to it.
473 */
474
Chris Wilsonf899fc62010-07-20 15:44:45 -0700475 return intel_ddc_get_modes(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800476 intel_gmbus_get_adapter(dev_priv,
477 intel_hdmi->ddc_bus));
Eric Anholt7d573822009-01-02 13:33:00 -0800478}
479
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000480static bool
481intel_hdmi_detect_audio(struct drm_connector *connector)
482{
483 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
484 struct drm_i915_private *dev_priv = connector->dev->dev_private;
485 struct edid *edid;
486 bool has_audio = false;
487
488 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800489 intel_gmbus_get_adapter(dev_priv,
490 intel_hdmi->ddc_bus));
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000491 if (edid) {
492 if (edid->input & DRM_EDID_INPUT_DIGITAL)
493 has_audio = drm_detect_monitor_audio(edid);
494
495 connector->display_info.raw_edid = NULL;
496 kfree(edid);
497 }
498
499 return has_audio;
500}
501
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100502static int
503intel_hdmi_set_property(struct drm_connector *connector,
504 struct drm_property *property,
505 uint64_t val)
506{
507 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000508 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100509 int ret;
510
511 ret = drm_connector_property_set_value(connector, property, val);
512 if (ret)
513 return ret;
514
Chris Wilson3f43c482011-05-12 22:17:24 +0100515 if (property == dev_priv->force_audio_property) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800516 enum hdmi_force_audio i = val;
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000517 bool has_audio;
518
519 if (i == intel_hdmi->force_audio)
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100520 return 0;
521
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000522 intel_hdmi->force_audio = i;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100523
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800524 if (i == HDMI_AUDIO_AUTO)
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000525 has_audio = intel_hdmi_detect_audio(connector);
526 else
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800527 has_audio = (i == HDMI_AUDIO_ON);
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000528
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800529 if (i == HDMI_AUDIO_OFF_DVI)
530 intel_hdmi->has_hdmi_sink = 0;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100531
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000532 intel_hdmi->has_audio = has_audio;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100533 goto done;
534 }
535
Chris Wilsone953fd72011-02-21 22:23:52 +0000536 if (property == dev_priv->broadcast_rgb_property) {
537 if (val == !!intel_hdmi->color_range)
538 return 0;
539
540 intel_hdmi->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
541 goto done;
542 }
543
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100544 return -EINVAL;
545
546done:
547 if (intel_hdmi->base.base.crtc) {
548 struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
549 drm_crtc_helper_set_mode(crtc, &crtc->mode,
550 crtc->x, crtc->y,
551 crtc->fb);
552 }
553
554 return 0;
555}
556
Eric Anholt7d573822009-01-02 13:33:00 -0800557static void intel_hdmi_destroy(struct drm_connector *connector)
558{
Eric Anholt7d573822009-01-02 13:33:00 -0800559 drm_sysfs_connector_remove(connector);
560 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800561 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800562}
563
564static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
565 .dpms = intel_hdmi_dpms,
566 .mode_fixup = intel_hdmi_mode_fixup,
567 .prepare = intel_encoder_prepare,
568 .mode_set = intel_hdmi_mode_set,
569 .commit = intel_encoder_commit,
570};
571
572static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700573 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800574 .detect = intel_hdmi_detect,
575 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100576 .set_property = intel_hdmi_set_property,
Eric Anholt7d573822009-01-02 13:33:00 -0800577 .destroy = intel_hdmi_destroy,
578};
579
580static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
581 .get_modes = intel_hdmi_get_modes,
582 .mode_valid = intel_hdmi_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100583 .best_encoder = intel_best_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800584};
585
Eric Anholt7d573822009-01-02 13:33:00 -0800586static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100587 .destroy = intel_encoder_destroy,
Eric Anholt7d573822009-01-02 13:33:00 -0800588};
589
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100590static void
591intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
592{
Chris Wilson3f43c482011-05-12 22:17:24 +0100593 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000594 intel_attach_broadcast_rgb_property(connector);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100595}
596
Eric Anholt7d573822009-01-02 13:33:00 -0800597void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
598{
599 struct drm_i915_private *dev_priv = dev->dev_private;
600 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700601 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800602 struct intel_connector *intel_connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100603 struct intel_hdmi *intel_hdmi;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530604 int i;
Eric Anholt7d573822009-01-02 13:33:00 -0800605
Chris Wilsonea5b2132010-08-04 13:50:23 +0100606 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
607 if (!intel_hdmi)
Eric Anholt7d573822009-01-02 13:33:00 -0800608 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800609
610 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
611 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100612 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800613 return;
614 }
615
Chris Wilsonea5b2132010-08-04 13:50:23 +0100616 intel_encoder = &intel_hdmi->base;
Chris Wilson373a3cf2010-09-15 12:03:59 +0100617 drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
618 DRM_MODE_ENCODER_TMDS);
619
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800620 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800621 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400622 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800623 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
624
Eric Anholt21d40d32010-03-25 11:11:14 -0700625 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800626
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000627 connector->polled = DRM_CONNECTOR_POLL_HPD;
Peter Rossc3febcc2012-01-28 14:49:26 +0100628 connector->interlace_allowed = 1;
Eric Anholt7d573822009-01-02 13:33:00 -0800629 connector->doublescan_allowed = 0;
Jesse Barnes27f82272011-09-02 12:54:37 -0700630 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
Eric Anholt7d573822009-01-02 13:33:00 -0800631
632 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800633 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700634 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700635 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800636 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800637 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700638 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700639 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800640 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800641 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700642 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700643 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800644 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800645 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700646 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700647 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800648 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800649 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700650 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700651 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800652 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800653 }
Eric Anholt7d573822009-01-02 13:33:00 -0800654
Chris Wilsonea5b2132010-08-04 13:50:23 +0100655 intel_hdmi->sdvox_reg = sdvox_reg;
Eric Anholt7d573822009-01-02 13:33:00 -0800656
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530657 if (!HAS_PCH_SPLIT(dev)) {
Jesse Barnes45187ac2011-08-03 09:22:55 -0700658 intel_hdmi->write_infoframe = i9xx_write_infoframe;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530659 I915_WRITE(VIDEO_DIP_CTL, 0);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700660 } else if (IS_VALLEYVIEW(dev)) {
661 intel_hdmi->write_infoframe = vlv_write_infoframe;
662 for_each_pipe(i)
663 I915_WRITE(VLV_TVIDEO_DIP_CTL(i), 0);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300664 } else if (HAS_PCH_IBX(dev)) {
665 intel_hdmi->write_infoframe = ibx_write_infoframe;
666 for_each_pipe(i)
667 I915_WRITE(TVIDEO_DIP_CTL(i), 0);
668 } else {
669 intel_hdmi->write_infoframe = cpt_write_infoframe;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530670 for_each_pipe(i)
671 I915_WRITE(TVIDEO_DIP_CTL(i), 0);
672 }
Jesse Barnes45187ac2011-08-03 09:22:55 -0700673
Chris Wilson4ef69c72010-09-09 15:14:28 +0100674 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800675
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100676 intel_hdmi_add_properties(intel_hdmi, connector);
677
Chris Wilsondf0e9242010-09-09 16:20:55 +0100678 intel_connector_attach_encoder(intel_connector, intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800679 drm_sysfs_connector_add(connector);
680
681 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
682 * 0xd. Failure to do so will result in spurious interrupts being
683 * generated on the port when a cable is not attached.
684 */
685 if (IS_G4X(dev) && !IS_GM45(dev)) {
686 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
687 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
688 }
Eric Anholt7d573822009-01-02 13:33:00 -0800689}