blob: e240d99dbf92b532264a66b831b6f67328973fea [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
Daniel Vetterbc2481f2012-05-08 15:18:32 +020078static u32 g4x_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
Daniel Vetterbc2481f2012-05-08 15:18:32 +020097static u32 g4x_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
Daniel Vettera3da1df2012-05-08 15:19:06 +0200116static void g4x_write_infoframe(struct drm_encoder *encoder,
117 struct dip_infoframe *frame)
Jesse Barnes45187ac2011-08-03 09:22:55 -0700118{
119 uint32_t *data = (uint32_t *)frame;
David Härdeman3c17fe42010-09-24 21:44:32 +0200120 struct drm_device *dev = encoder->dev;
121 struct drm_i915_private *dev_priv = dev->dev_private;
122 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300123 u32 val = I915_READ(VIDEO_DIP_CTL);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700124 unsigned i, len = DIP_HEADER_SIZE + frame->len;
David Härdeman3c17fe42010-09-24 21:44:32 +0200125
David Härdeman3c17fe42010-09-24 21:44:32 +0200126
127 /* XXX first guess at handling video port, is this corrent? */
Paulo Zanoni3e6e6392012-05-04 17:18:19 -0300128 val &= ~VIDEO_DIP_PORT_MASK;
David Härdeman3c17fe42010-09-24 21:44:32 +0200129 if (intel_hdmi->sdvox_reg == SDVOB)
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300130 val |= VIDEO_DIP_PORT_B;
David Härdeman3c17fe42010-09-24 21:44:32 +0200131 else if (intel_hdmi->sdvox_reg == SDVOC)
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300132 val |= VIDEO_DIP_PORT_C;
David Härdeman3c17fe42010-09-24 21:44:32 +0200133 else
134 return;
135
Paulo Zanoni1d4f85a2012-05-04 17:18:18 -0300136 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200137 val |= g4x_infoframe_index(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700138
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200139 val &= ~g4x_infoframe_enable(frame);
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300140 val |= VIDEO_DIP_ENABLE;
141
142 I915_WRITE(VIDEO_DIP_CTL, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700143
144 for (i = 0; i < len; i += 4) {
David Härdeman3c17fe42010-09-24 21:44:32 +0200145 I915_WRITE(VIDEO_DIP_DATA, *data);
146 data++;
147 }
148
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200149 val |= g4x_infoframe_enable(frame);
Paulo Zanoni60c5ea22012-05-04 17:18:22 -0300150 val &= ~VIDEO_DIP_FREQ_MASK;
Daniel Vetter4b24c932012-05-08 14:41:00 +0200151 val |= VIDEO_DIP_FREQ_VSYNC;
Jesse Barnes45187ac2011-08-03 09:22:55 -0700152
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300153 I915_WRITE(VIDEO_DIP_CTL, val);
David Härdeman3c17fe42010-09-24 21:44:32 +0200154}
155
Paulo Zanonifdf12502012-05-04 17:18:24 -0300156static void ibx_write_infoframe(struct drm_encoder *encoder,
157 struct dip_infoframe *frame)
158{
159 uint32_t *data = (uint32_t *)frame;
160 struct drm_device *dev = encoder->dev;
161 struct drm_i915_private *dev_priv = dev->dev_private;
162 struct drm_crtc *crtc = encoder->crtc;
163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni4e89ee12012-05-04 17:18:26 -0300164 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300165 int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
166 unsigned i, len = DIP_HEADER_SIZE + frame->len;
167 u32 val = I915_READ(reg);
168
Paulo Zanoni4e89ee12012-05-04 17:18:26 -0300169 val &= ~VIDEO_DIP_PORT_MASK;
170 switch (intel_hdmi->sdvox_reg) {
171 case HDMIB:
172 val |= VIDEO_DIP_PORT_B;
173 break;
174 case HDMIC:
175 val |= VIDEO_DIP_PORT_C;
176 break;
177 case HDMID:
178 val |= VIDEO_DIP_PORT_D;
179 break;
180 default:
181 return;
182 }
183
Paulo Zanonifdf12502012-05-04 17:18:24 -0300184 intel_wait_for_vblank(dev, intel_crtc->pipe);
185
186 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200187 val |= g4x_infoframe_index(frame);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300188
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200189 val &= ~g4x_infoframe_enable(frame);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300190 val |= VIDEO_DIP_ENABLE;
191
192 I915_WRITE(reg, val);
193
194 for (i = 0; i < len; i += 4) {
195 I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
196 data++;
197 }
198
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200199 val |= g4x_infoframe_enable(frame);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300200 val &= ~VIDEO_DIP_FREQ_MASK;
Daniel Vetter4b24c932012-05-08 14:41:00 +0200201 val |= VIDEO_DIP_FREQ_VSYNC;
Paulo Zanonifdf12502012-05-04 17:18:24 -0300202
203 I915_WRITE(reg, val);
204}
205
206static void cpt_write_infoframe(struct drm_encoder *encoder,
207 struct dip_infoframe *frame)
Jesse Barnes45187ac2011-08-03 09:22:55 -0700208{
209 uint32_t *data = (uint32_t *)frame;
210 struct drm_device *dev = encoder->dev;
211 struct drm_i915_private *dev_priv = dev->dev_private;
212 struct drm_crtc *crtc = encoder->crtc;
213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
214 int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
215 unsigned i, len = DIP_HEADER_SIZE + frame->len;
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300216 u32 val = I915_READ(reg);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700217
218 intel_wait_for_vblank(dev, intel_crtc->pipe);
219
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530220 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200221 val |= g4x_infoframe_index(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700222
Paulo Zanoniecb97852012-05-04 17:18:21 -0300223 /* The DIP control register spec says that we need to update the AVI
224 * infoframe without clearing its enable bit */
225 if (frame->type == DIP_TYPE_AVI)
226 val |= VIDEO_DIP_ENABLE_AVI;
227 else
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200228 val &= ~g4x_infoframe_enable(frame);
Paulo Zanoniecb97852012-05-04 17:18:21 -0300229
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300230 val |= VIDEO_DIP_ENABLE;
231
232 I915_WRITE(reg, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700233
234 for (i = 0; i < len; i += 4) {
235 I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
236 data++;
237 }
238
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200239 val |= g4x_infoframe_enable(frame);
Paulo Zanoni60c5ea22012-05-04 17:18:22 -0300240 val &= ~VIDEO_DIP_FREQ_MASK;
Daniel Vetter4b24c932012-05-08 14:41:00 +0200241 val |= VIDEO_DIP_FREQ_VSYNC;
Jesse Barnes45187ac2011-08-03 09:22:55 -0700242
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300243 I915_WRITE(reg, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700244}
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700245
246static void vlv_write_infoframe(struct drm_encoder *encoder,
247 struct dip_infoframe *frame)
248{
249 uint32_t *data = (uint32_t *)frame;
250 struct drm_device *dev = encoder->dev;
251 struct drm_i915_private *dev_priv = dev->dev_private;
252 struct drm_crtc *crtc = encoder->crtc;
253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
254 int reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
255 unsigned i, len = DIP_HEADER_SIZE + frame->len;
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300256 u32 val = I915_READ(reg);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700257
258 intel_wait_for_vblank(dev, intel_crtc->pipe);
259
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700260 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200261 val |= g4x_infoframe_index(frame);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700262
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200263 val &= ~g4x_infoframe_enable(frame);
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300264 val |= VIDEO_DIP_ENABLE;
265
266 I915_WRITE(reg, val);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700267
268 for (i = 0; i < len; i += 4) {
269 I915_WRITE(VLV_TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
270 data++;
271 }
272
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200273 val |= g4x_infoframe_enable(frame);
Paulo Zanoni60c5ea22012-05-04 17:18:22 -0300274 val &= ~VIDEO_DIP_FREQ_MASK;
Daniel Vetter4b24c932012-05-08 14:41:00 +0200275 val |= VIDEO_DIP_FREQ_VSYNC;
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700276
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300277 I915_WRITE(reg, val);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700278}
279
Jesse Barnes45187ac2011-08-03 09:22:55 -0700280static void intel_set_infoframe(struct drm_encoder *encoder,
281 struct dip_infoframe *frame)
282{
283 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
284
285 if (!intel_hdmi->has_hdmi_sink)
286 return;
287
288 intel_dip_infoframe_csum(frame);
289 intel_hdmi->write_infoframe(encoder, frame);
290}
291
Paulo Zanonic846b612012-04-13 16:31:41 -0300292static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder,
293 struct drm_display_mode *adjusted_mode)
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700294{
295 struct dip_infoframe avi_if = {
296 .type = DIP_TYPE_AVI,
297 .ver = DIP_VERSION_AVI,
298 .len = DIP_LEN_AVI,
299 };
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700300
Paulo Zanonic846b612012-04-13 16:31:41 -0300301 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
302 avi_if.body.avi.YQ_CN_PR |= DIP_AVI_PR_2;
303
Jesse Barnes45187ac2011-08-03 09:22:55 -0700304 intel_set_infoframe(encoder, &avi_if);
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700305}
306
Jesse Barnesc0864cb2011-08-03 09:22:56 -0700307static void intel_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
308{
309 struct dip_infoframe spd_if;
310
311 memset(&spd_if, 0, sizeof(spd_if));
312 spd_if.type = DIP_TYPE_SPD;
313 spd_if.ver = DIP_VERSION_SPD;
314 spd_if.len = DIP_LEN_SPD;
315 strcpy(spd_if.body.spd.vn, "Intel");
316 strcpy(spd_if.body.spd.pd, "Integrated gfx");
317 spd_if.body.spd.sdi = DIP_SPD_PC;
318
319 intel_set_infoframe(encoder, &spd_if);
320}
321
Eric Anholt7d573822009-01-02 13:33:00 -0800322static void intel_hdmi_mode_set(struct drm_encoder *encoder,
323 struct drm_display_mode *mode,
324 struct drm_display_mode *adjusted_mode)
325{
326 struct drm_device *dev = encoder->dev;
327 struct drm_i915_private *dev_priv = dev->dev_private;
328 struct drm_crtc *crtc = encoder->crtc;
329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100330 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800331 u32 sdvox;
332
Adam Jacksonb599c0b2010-07-16 14:46:31 -0400333 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
Jesse Barnes5d4fac92011-06-24 12:19:19 -0700334 if (!HAS_PCH_SPLIT(dev))
335 sdvox |= intel_hdmi->color_range;
Adam Jacksonb599c0b2010-07-16 14:46:31 -0400336 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
337 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
338 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
339 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -0800340
Jesse Barnes020f6702011-06-24 12:19:25 -0700341 if (intel_crtc->bpp > 24)
342 sdvox |= COLOR_FORMAT_12bpc;
343 else
344 sdvox |= COLOR_FORMAT_8bpc;
345
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800346 /* Required on CPT */
347 if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
348 sdvox |= HDMI_MODE_SELECT;
349
David Härdeman3c17fe42010-09-24 21:44:32 +0200350 if (intel_hdmi->has_audio) {
Wu Fengguange0dac652011-09-05 14:25:34 +0800351 DRM_DEBUG_DRIVER("Enabling HDMI audio on pipe %c\n",
352 pipe_name(intel_crtc->pipe));
Eric Anholt7d573822009-01-02 13:33:00 -0800353 sdvox |= SDVO_AUDIO_ENABLE;
David Härdeman3c17fe42010-09-24 21:44:32 +0200354 sdvox |= SDVO_NULL_PACKETS_DURING_VSYNC;
Wu Fengguange0dac652011-09-05 14:25:34 +0800355 intel_write_eld(encoder, adjusted_mode);
David Härdeman3c17fe42010-09-24 21:44:32 +0200356 }
Eric Anholt7d573822009-01-02 13:33:00 -0800357
Jesse Barnes75770562011-10-12 09:01:58 -0700358 if (HAS_PCH_CPT(dev))
359 sdvox |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
360 else if (intel_crtc->pipe == 1)
361 sdvox |= SDVO_PIPE_B_SELECT;
Eric Anholt7d573822009-01-02 13:33:00 -0800362
Chris Wilsonea5b2132010-08-04 13:50:23 +0100363 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
364 POSTING_READ(intel_hdmi->sdvox_reg);
David Härdeman3c17fe42010-09-24 21:44:32 +0200365
Paulo Zanonic846b612012-04-13 16:31:41 -0300366 intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
Jesse Barnesc0864cb2011-08-03 09:22:56 -0700367 intel_hdmi_set_spd_infoframe(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800368}
369
370static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
371{
372 struct drm_device *dev = encoder->dev;
373 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100374 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800375 u32 temp;
Wu Fengguang2deed762011-12-09 20:42:20 +0800376 u32 enable_bits = SDVO_ENABLE;
377
378 if (intel_hdmi->has_audio)
379 enable_bits |= SDVO_AUDIO_ENABLE;
Eric Anholt7d573822009-01-02 13:33:00 -0800380
Chris Wilsonea5b2132010-08-04 13:50:23 +0100381 temp = I915_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000382
383 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
384 * we do this anyway which shows more stable in testing.
385 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800386 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100387 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
388 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -0800389 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000390
391 if (mode != DRM_MODE_DPMS_ON) {
Wu Fengguang2deed762011-12-09 20:42:20 +0800392 temp &= ~enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000393 } else {
Wu Fengguang2deed762011-12-09 20:42:20 +0800394 temp |= enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000395 }
396
Chris Wilsonea5b2132010-08-04 13:50:23 +0100397 I915_WRITE(intel_hdmi->sdvox_reg, temp);
398 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000399
400 /* HW workaround, need to write this twice for issue that may result
401 * in first write getting masked.
402 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800403 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100404 I915_WRITE(intel_hdmi->sdvox_reg, temp);
405 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000406 }
Eric Anholt7d573822009-01-02 13:33:00 -0800407}
408
Eric Anholt7d573822009-01-02 13:33:00 -0800409static int intel_hdmi_mode_valid(struct drm_connector *connector,
410 struct drm_display_mode *mode)
411{
412 if (mode->clock > 165000)
413 return MODE_CLOCK_HIGH;
414 if (mode->clock < 20000)
Nicolas Kaiser5cbba412011-05-30 12:48:26 +0200415 return MODE_CLOCK_LOW;
Eric Anholt7d573822009-01-02 13:33:00 -0800416
417 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
418 return MODE_NO_DBLESCAN;
419
420 return MODE_OK;
421}
422
423static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
424 struct drm_display_mode *mode,
425 struct drm_display_mode *adjusted_mode)
426{
427 return true;
428}
429
Keith Packardaa93d632009-05-05 09:52:46 -0700430static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100431intel_hdmi_detect(struct drm_connector *connector, bool force)
Ma Ling9dff6af2009-04-02 13:13:26 +0800432{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100433 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700434 struct drm_i915_private *dev_priv = connector->dev->dev_private;
435 struct edid *edid;
Keith Packardaa93d632009-05-05 09:52:46 -0700436 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800437
Chris Wilsonea5b2132010-08-04 13:50:23 +0100438 intel_hdmi->has_hdmi_sink = false;
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800439 intel_hdmi->has_audio = false;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700440 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800441 intel_gmbus_get_adapter(dev_priv,
442 intel_hdmi->ddc_bus));
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800443
Keith Packardaa93d632009-05-05 09:52:46 -0700444 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700445 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700446 status = connector_status_connected;
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800447 if (intel_hdmi->force_audio != HDMI_AUDIO_OFF_DVI)
448 intel_hdmi->has_hdmi_sink =
449 drm_detect_hdmi_monitor(edid);
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800450 intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
Keith Packardaa93d632009-05-05 09:52:46 -0700451 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800452 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700453 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800454 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800455
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100456 if (status == connector_status_connected) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800457 if (intel_hdmi->force_audio != HDMI_AUDIO_AUTO)
458 intel_hdmi->has_audio =
459 (intel_hdmi->force_audio == HDMI_AUDIO_ON);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100460 }
461
Keith Packardaa93d632009-05-05 09:52:46 -0700462 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800463}
464
Eric Anholt7d573822009-01-02 13:33:00 -0800465static int intel_hdmi_get_modes(struct drm_connector *connector)
466{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100467 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700468 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Eric Anholt7d573822009-01-02 13:33:00 -0800469
470 /* We should parse the EDID data and find out if it's an HDMI sink so
471 * we can send audio to it.
472 */
473
Chris Wilsonf899fc62010-07-20 15:44:45 -0700474 return intel_ddc_get_modes(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800475 intel_gmbus_get_adapter(dev_priv,
476 intel_hdmi->ddc_bus));
Eric Anholt7d573822009-01-02 13:33:00 -0800477}
478
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000479static bool
480intel_hdmi_detect_audio(struct drm_connector *connector)
481{
482 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
483 struct drm_i915_private *dev_priv = connector->dev->dev_private;
484 struct edid *edid;
485 bool has_audio = false;
486
487 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800488 intel_gmbus_get_adapter(dev_priv,
489 intel_hdmi->ddc_bus));
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000490 if (edid) {
491 if (edid->input & DRM_EDID_INPUT_DIGITAL)
492 has_audio = drm_detect_monitor_audio(edid);
493
494 connector->display_info.raw_edid = NULL;
495 kfree(edid);
496 }
497
498 return has_audio;
499}
500
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100501static int
502intel_hdmi_set_property(struct drm_connector *connector,
503 struct drm_property *property,
504 uint64_t val)
505{
506 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000507 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100508 int ret;
509
510 ret = drm_connector_property_set_value(connector, property, val);
511 if (ret)
512 return ret;
513
Chris Wilson3f43c482011-05-12 22:17:24 +0100514 if (property == dev_priv->force_audio_property) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800515 enum hdmi_force_audio i = val;
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000516 bool has_audio;
517
518 if (i == intel_hdmi->force_audio)
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100519 return 0;
520
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000521 intel_hdmi->force_audio = i;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100522
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800523 if (i == HDMI_AUDIO_AUTO)
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000524 has_audio = intel_hdmi_detect_audio(connector);
525 else
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800526 has_audio = (i == HDMI_AUDIO_ON);
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000527
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800528 if (i == HDMI_AUDIO_OFF_DVI)
529 intel_hdmi->has_hdmi_sink = 0;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100530
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000531 intel_hdmi->has_audio = has_audio;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100532 goto done;
533 }
534
Chris Wilsone953fd72011-02-21 22:23:52 +0000535 if (property == dev_priv->broadcast_rgb_property) {
536 if (val == !!intel_hdmi->color_range)
537 return 0;
538
539 intel_hdmi->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
540 goto done;
541 }
542
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100543 return -EINVAL;
544
545done:
546 if (intel_hdmi->base.base.crtc) {
547 struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
548 drm_crtc_helper_set_mode(crtc, &crtc->mode,
549 crtc->x, crtc->y,
550 crtc->fb);
551 }
552
553 return 0;
554}
555
Eric Anholt7d573822009-01-02 13:33:00 -0800556static void intel_hdmi_destroy(struct drm_connector *connector)
557{
Eric Anholt7d573822009-01-02 13:33:00 -0800558 drm_sysfs_connector_remove(connector);
559 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800560 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800561}
562
563static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
564 .dpms = intel_hdmi_dpms,
565 .mode_fixup = intel_hdmi_mode_fixup,
566 .prepare = intel_encoder_prepare,
567 .mode_set = intel_hdmi_mode_set,
568 .commit = intel_encoder_commit,
569};
570
571static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700572 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800573 .detect = intel_hdmi_detect,
574 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100575 .set_property = intel_hdmi_set_property,
Eric Anholt7d573822009-01-02 13:33:00 -0800576 .destroy = intel_hdmi_destroy,
577};
578
579static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
580 .get_modes = intel_hdmi_get_modes,
581 .mode_valid = intel_hdmi_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100582 .best_encoder = intel_best_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800583};
584
Eric Anholt7d573822009-01-02 13:33:00 -0800585static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100586 .destroy = intel_encoder_destroy,
Eric Anholt7d573822009-01-02 13:33:00 -0800587};
588
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100589static void
590intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
591{
Chris Wilson3f43c482011-05-12 22:17:24 +0100592 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000593 intel_attach_broadcast_rgb_property(connector);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100594}
595
Eric Anholt7d573822009-01-02 13:33:00 -0800596void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
597{
598 struct drm_i915_private *dev_priv = dev->dev_private;
599 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700600 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800601 struct intel_connector *intel_connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100602 struct intel_hdmi *intel_hdmi;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530603 int i;
Eric Anholt7d573822009-01-02 13:33:00 -0800604
Chris Wilsonea5b2132010-08-04 13:50:23 +0100605 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
606 if (!intel_hdmi)
Eric Anholt7d573822009-01-02 13:33:00 -0800607 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800608
609 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
610 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100611 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800612 return;
613 }
614
Chris Wilsonea5b2132010-08-04 13:50:23 +0100615 intel_encoder = &intel_hdmi->base;
Chris Wilson373a3cf2010-09-15 12:03:59 +0100616 drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
617 DRM_MODE_ENCODER_TMDS);
618
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800619 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800620 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400621 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800622 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
623
Eric Anholt21d40d32010-03-25 11:11:14 -0700624 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800625
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000626 connector->polled = DRM_CONNECTOR_POLL_HPD;
Peter Rossc3febcc2012-01-28 14:49:26 +0100627 connector->interlace_allowed = 1;
Eric Anholt7d573822009-01-02 13:33:00 -0800628 connector->doublescan_allowed = 0;
Jesse Barnes27f82272011-09-02 12:54:37 -0700629 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
Eric Anholt7d573822009-01-02 13:33:00 -0800630
631 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800632 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700633 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700634 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800635 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800636 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700637 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700638 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800639 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800640 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700641 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700642 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800643 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800644 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700645 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700646 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800647 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800648 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700649 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700650 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800651 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800652 }
Eric Anholt7d573822009-01-02 13:33:00 -0800653
Chris Wilsonea5b2132010-08-04 13:50:23 +0100654 intel_hdmi->sdvox_reg = sdvox_reg;
Eric Anholt7d573822009-01-02 13:33:00 -0800655
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530656 if (!HAS_PCH_SPLIT(dev)) {
Daniel Vettera3da1df2012-05-08 15:19:06 +0200657 intel_hdmi->write_infoframe = g4x_write_infoframe;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530658 I915_WRITE(VIDEO_DIP_CTL, 0);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700659 } else if (IS_VALLEYVIEW(dev)) {
660 intel_hdmi->write_infoframe = vlv_write_infoframe;
661 for_each_pipe(i)
662 I915_WRITE(VLV_TVIDEO_DIP_CTL(i), 0);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300663 } else if (HAS_PCH_IBX(dev)) {
664 intel_hdmi->write_infoframe = ibx_write_infoframe;
665 for_each_pipe(i)
666 I915_WRITE(TVIDEO_DIP_CTL(i), 0);
667 } else {
668 intel_hdmi->write_infoframe = cpt_write_infoframe;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530669 for_each_pipe(i)
670 I915_WRITE(TVIDEO_DIP_CTL(i), 0);
671 }
Jesse Barnes45187ac2011-08-03 09:22:55 -0700672
Chris Wilson4ef69c72010-09-09 15:14:28 +0100673 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800674
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100675 intel_hdmi_add_properties(intel_hdmi, connector);
676
Chris Wilsondf0e9242010-09-09 16:20:55 +0100677 intel_connector_attach_encoder(intel_connector, intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800678 drm_sysfs_connector_add(connector);
679
680 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
681 * 0xd. Failure to do so will result in spurious interrupts being
682 * generated on the port when a cable is not attached.
683 */
684 if (IS_G4X(dev) && !IS_GM45(dev)) {
685 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
686 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
687 }
Eric Anholt7d573822009-01-02 13:33:00 -0800688}