blob: af88313d72d039c8b83b16a07ac5cc031db3d476 [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
97static u32 intel_infoframe_flags(struct dip_infoframe *frame)
98{
99 u32 flags = 0;
100
101 switch (frame->type) {
102 case DIP_TYPE_AVI:
103 flags |= VIDEO_DIP_ENABLE_AVI | VIDEO_DIP_FREQ_VSYNC;
104 break;
105 case DIP_TYPE_SPD:
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530106 flags |= VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_FREQ_VSYNC;
Jesse Barnes45187ac2011-08-03 09:22:55 -0700107 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 void i9xx_write_infoframe(struct drm_encoder *encoder,
117 struct dip_infoframe *frame)
118{
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? */
128 if (intel_hdmi->sdvox_reg == SDVOB)
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300129 val |= VIDEO_DIP_PORT_B;
David Härdeman3c17fe42010-09-24 21:44:32 +0200130 else if (intel_hdmi->sdvox_reg == SDVOC)
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300131 val |= VIDEO_DIP_PORT_C;
David Härdeman3c17fe42010-09-24 21:44:32 +0200132 else
133 return;
134
Paulo Zanoni1d4f85a2012-05-04 17:18:18 -0300135 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300136 val |= intel_infoframe_index(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700137
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300138 val |= VIDEO_DIP_ENABLE;
139
140 I915_WRITE(VIDEO_DIP_CTL, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700141
142 for (i = 0; i < len; i += 4) {
David Härdeman3c17fe42010-09-24 21:44:32 +0200143 I915_WRITE(VIDEO_DIP_DATA, *data);
144 data++;
145 }
146
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300147 val |= intel_infoframe_flags(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700148
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300149 I915_WRITE(VIDEO_DIP_CTL, val);
David Härdeman3c17fe42010-09-24 21:44:32 +0200150}
151
Jesse Barnes45187ac2011-08-03 09:22:55 -0700152static void ironlake_write_infoframe(struct drm_encoder *encoder,
153 struct dip_infoframe *frame)
154{
155 uint32_t *data = (uint32_t *)frame;
156 struct drm_device *dev = encoder->dev;
157 struct drm_i915_private *dev_priv = dev->dev_private;
158 struct drm_crtc *crtc = encoder->crtc;
159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
160 int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
161 unsigned i, len = DIP_HEADER_SIZE + frame->len;
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300162 u32 val = I915_READ(reg);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700163
164 intel_wait_for_vblank(dev, intel_crtc->pipe);
165
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530166 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300167 val |= intel_infoframe_index(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700168
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300169 val |= VIDEO_DIP_ENABLE;
170
171 I915_WRITE(reg, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700172
173 for (i = 0; i < len; i += 4) {
174 I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
175 data++;
176 }
177
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300178 val |= intel_infoframe_flags(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700179
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300180 I915_WRITE(reg, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700181}
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700182
183static void vlv_write_infoframe(struct drm_encoder *encoder,
184 struct dip_infoframe *frame)
185{
186 uint32_t *data = (uint32_t *)frame;
187 struct drm_device *dev = encoder->dev;
188 struct drm_i915_private *dev_priv = dev->dev_private;
189 struct drm_crtc *crtc = encoder->crtc;
190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
191 int reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
192 unsigned i, len = DIP_HEADER_SIZE + frame->len;
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300193 u32 val = I915_READ(reg);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700194
195 intel_wait_for_vblank(dev, intel_crtc->pipe);
196
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700197 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300198 val |= intel_infoframe_index(frame);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700199
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300200 val |= VIDEO_DIP_ENABLE;
201
202 I915_WRITE(reg, val);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700203
204 for (i = 0; i < len; i += 4) {
205 I915_WRITE(VLV_TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
206 data++;
207 }
208
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300209 val |= intel_infoframe_flags(frame);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700210
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300211 I915_WRITE(reg, val);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700212}
213
Jesse Barnes45187ac2011-08-03 09:22:55 -0700214static void intel_set_infoframe(struct drm_encoder *encoder,
215 struct dip_infoframe *frame)
216{
217 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
218
219 if (!intel_hdmi->has_hdmi_sink)
220 return;
221
222 intel_dip_infoframe_csum(frame);
223 intel_hdmi->write_infoframe(encoder, frame);
224}
225
Paulo Zanonic846b612012-04-13 16:31:41 -0300226static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder,
227 struct drm_display_mode *adjusted_mode)
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700228{
229 struct dip_infoframe avi_if = {
230 .type = DIP_TYPE_AVI,
231 .ver = DIP_VERSION_AVI,
232 .len = DIP_LEN_AVI,
233 };
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700234
Paulo Zanonic846b612012-04-13 16:31:41 -0300235 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
236 avi_if.body.avi.YQ_CN_PR |= DIP_AVI_PR_2;
237
Jesse Barnes45187ac2011-08-03 09:22:55 -0700238 intel_set_infoframe(encoder, &avi_if);
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700239}
240
Jesse Barnesc0864cb2011-08-03 09:22:56 -0700241static void intel_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
242{
243 struct dip_infoframe spd_if;
244
245 memset(&spd_if, 0, sizeof(spd_if));
246 spd_if.type = DIP_TYPE_SPD;
247 spd_if.ver = DIP_VERSION_SPD;
248 spd_if.len = DIP_LEN_SPD;
249 strcpy(spd_if.body.spd.vn, "Intel");
250 strcpy(spd_if.body.spd.pd, "Integrated gfx");
251 spd_if.body.spd.sdi = DIP_SPD_PC;
252
253 intel_set_infoframe(encoder, &spd_if);
254}
255
Eric Anholt7d573822009-01-02 13:33:00 -0800256static void intel_hdmi_mode_set(struct drm_encoder *encoder,
257 struct drm_display_mode *mode,
258 struct drm_display_mode *adjusted_mode)
259{
260 struct drm_device *dev = encoder->dev;
261 struct drm_i915_private *dev_priv = dev->dev_private;
262 struct drm_crtc *crtc = encoder->crtc;
263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100264 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800265 u32 sdvox;
266
Adam Jacksonb599c0b2010-07-16 14:46:31 -0400267 sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
Jesse Barnes5d4fac92011-06-24 12:19:19 -0700268 if (!HAS_PCH_SPLIT(dev))
269 sdvox |= intel_hdmi->color_range;
Adam Jacksonb599c0b2010-07-16 14:46:31 -0400270 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
271 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
272 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
273 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -0800274
Jesse Barnes020f6702011-06-24 12:19:25 -0700275 if (intel_crtc->bpp > 24)
276 sdvox |= COLOR_FORMAT_12bpc;
277 else
278 sdvox |= COLOR_FORMAT_8bpc;
279
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800280 /* Required on CPT */
281 if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
282 sdvox |= HDMI_MODE_SELECT;
283
David Härdeman3c17fe42010-09-24 21:44:32 +0200284 if (intel_hdmi->has_audio) {
Wu Fengguange0dac652011-09-05 14:25:34 +0800285 DRM_DEBUG_DRIVER("Enabling HDMI audio on pipe %c\n",
286 pipe_name(intel_crtc->pipe));
Eric Anholt7d573822009-01-02 13:33:00 -0800287 sdvox |= SDVO_AUDIO_ENABLE;
David Härdeman3c17fe42010-09-24 21:44:32 +0200288 sdvox |= SDVO_NULL_PACKETS_DURING_VSYNC;
Wu Fengguange0dac652011-09-05 14:25:34 +0800289 intel_write_eld(encoder, adjusted_mode);
David Härdeman3c17fe42010-09-24 21:44:32 +0200290 }
Eric Anholt7d573822009-01-02 13:33:00 -0800291
Jesse Barnes75770562011-10-12 09:01:58 -0700292 if (HAS_PCH_CPT(dev))
293 sdvox |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
294 else if (intel_crtc->pipe == 1)
295 sdvox |= SDVO_PIPE_B_SELECT;
Eric Anholt7d573822009-01-02 13:33:00 -0800296
Chris Wilsonea5b2132010-08-04 13:50:23 +0100297 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
298 POSTING_READ(intel_hdmi->sdvox_reg);
David Härdeman3c17fe42010-09-24 21:44:32 +0200299
Paulo Zanonic846b612012-04-13 16:31:41 -0300300 intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
Jesse Barnesc0864cb2011-08-03 09:22:56 -0700301 intel_hdmi_set_spd_infoframe(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800302}
303
304static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
305{
306 struct drm_device *dev = encoder->dev;
307 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100308 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800309 u32 temp;
Wu Fengguang2deed762011-12-09 20:42:20 +0800310 u32 enable_bits = SDVO_ENABLE;
311
312 if (intel_hdmi->has_audio)
313 enable_bits |= SDVO_AUDIO_ENABLE;
Eric Anholt7d573822009-01-02 13:33:00 -0800314
Chris Wilsonea5b2132010-08-04 13:50:23 +0100315 temp = I915_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000316
317 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
318 * we do this anyway which shows more stable in testing.
319 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800320 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100321 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
322 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -0800323 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000324
325 if (mode != DRM_MODE_DPMS_ON) {
Wu Fengguang2deed762011-12-09 20:42:20 +0800326 temp &= ~enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000327 } else {
Wu Fengguang2deed762011-12-09 20:42:20 +0800328 temp |= enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000329 }
330
Chris Wilsonea5b2132010-08-04 13:50:23 +0100331 I915_WRITE(intel_hdmi->sdvox_reg, temp);
332 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000333
334 /* HW workaround, need to write this twice for issue that may result
335 * in first write getting masked.
336 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800337 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100338 I915_WRITE(intel_hdmi->sdvox_reg, temp);
339 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000340 }
Eric Anholt7d573822009-01-02 13:33:00 -0800341}
342
Eric Anholt7d573822009-01-02 13:33:00 -0800343static int intel_hdmi_mode_valid(struct drm_connector *connector,
344 struct drm_display_mode *mode)
345{
346 if (mode->clock > 165000)
347 return MODE_CLOCK_HIGH;
348 if (mode->clock < 20000)
Nicolas Kaiser5cbba412011-05-30 12:48:26 +0200349 return MODE_CLOCK_LOW;
Eric Anholt7d573822009-01-02 13:33:00 -0800350
351 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
352 return MODE_NO_DBLESCAN;
353
354 return MODE_OK;
355}
356
357static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
358 struct drm_display_mode *mode,
359 struct drm_display_mode *adjusted_mode)
360{
361 return true;
362}
363
Keith Packardaa93d632009-05-05 09:52:46 -0700364static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100365intel_hdmi_detect(struct drm_connector *connector, bool force)
Ma Ling9dff6af2009-04-02 13:13:26 +0800366{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100367 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700368 struct drm_i915_private *dev_priv = connector->dev->dev_private;
369 struct edid *edid;
Keith Packardaa93d632009-05-05 09:52:46 -0700370 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800371
Chris Wilsonea5b2132010-08-04 13:50:23 +0100372 intel_hdmi->has_hdmi_sink = false;
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800373 intel_hdmi->has_audio = false;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700374 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800375 intel_gmbus_get_adapter(dev_priv,
376 intel_hdmi->ddc_bus));
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800377
Keith Packardaa93d632009-05-05 09:52:46 -0700378 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700379 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700380 status = connector_status_connected;
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800381 if (intel_hdmi->force_audio != HDMI_AUDIO_OFF_DVI)
382 intel_hdmi->has_hdmi_sink =
383 drm_detect_hdmi_monitor(edid);
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800384 intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
Keith Packardaa93d632009-05-05 09:52:46 -0700385 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800386 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700387 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800388 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800389
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100390 if (status == connector_status_connected) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800391 if (intel_hdmi->force_audio != HDMI_AUDIO_AUTO)
392 intel_hdmi->has_audio =
393 (intel_hdmi->force_audio == HDMI_AUDIO_ON);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100394 }
395
Keith Packardaa93d632009-05-05 09:52:46 -0700396 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800397}
398
Eric Anholt7d573822009-01-02 13:33:00 -0800399static int intel_hdmi_get_modes(struct drm_connector *connector)
400{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100401 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700402 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Eric Anholt7d573822009-01-02 13:33:00 -0800403
404 /* We should parse the EDID data and find out if it's an HDMI sink so
405 * we can send audio to it.
406 */
407
Chris Wilsonf899fc62010-07-20 15:44:45 -0700408 return intel_ddc_get_modes(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800409 intel_gmbus_get_adapter(dev_priv,
410 intel_hdmi->ddc_bus));
Eric Anholt7d573822009-01-02 13:33:00 -0800411}
412
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000413static bool
414intel_hdmi_detect_audio(struct drm_connector *connector)
415{
416 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
417 struct drm_i915_private *dev_priv = connector->dev->dev_private;
418 struct edid *edid;
419 bool has_audio = false;
420
421 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800422 intel_gmbus_get_adapter(dev_priv,
423 intel_hdmi->ddc_bus));
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000424 if (edid) {
425 if (edid->input & DRM_EDID_INPUT_DIGITAL)
426 has_audio = drm_detect_monitor_audio(edid);
427
428 connector->display_info.raw_edid = NULL;
429 kfree(edid);
430 }
431
432 return has_audio;
433}
434
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100435static int
436intel_hdmi_set_property(struct drm_connector *connector,
437 struct drm_property *property,
438 uint64_t val)
439{
440 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000441 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100442 int ret;
443
444 ret = drm_connector_property_set_value(connector, property, val);
445 if (ret)
446 return ret;
447
Chris Wilson3f43c482011-05-12 22:17:24 +0100448 if (property == dev_priv->force_audio_property) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800449 enum hdmi_force_audio i = val;
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000450 bool has_audio;
451
452 if (i == intel_hdmi->force_audio)
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100453 return 0;
454
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000455 intel_hdmi->force_audio = i;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100456
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800457 if (i == HDMI_AUDIO_AUTO)
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000458 has_audio = intel_hdmi_detect_audio(connector);
459 else
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800460 has_audio = (i == HDMI_AUDIO_ON);
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000461
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800462 if (i == HDMI_AUDIO_OFF_DVI)
463 intel_hdmi->has_hdmi_sink = 0;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100464
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000465 intel_hdmi->has_audio = has_audio;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100466 goto done;
467 }
468
Chris Wilsone953fd72011-02-21 22:23:52 +0000469 if (property == dev_priv->broadcast_rgb_property) {
470 if (val == !!intel_hdmi->color_range)
471 return 0;
472
473 intel_hdmi->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
474 goto done;
475 }
476
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100477 return -EINVAL;
478
479done:
480 if (intel_hdmi->base.base.crtc) {
481 struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
482 drm_crtc_helper_set_mode(crtc, &crtc->mode,
483 crtc->x, crtc->y,
484 crtc->fb);
485 }
486
487 return 0;
488}
489
Eric Anholt7d573822009-01-02 13:33:00 -0800490static void intel_hdmi_destroy(struct drm_connector *connector)
491{
Eric Anholt7d573822009-01-02 13:33:00 -0800492 drm_sysfs_connector_remove(connector);
493 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800494 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800495}
496
497static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
498 .dpms = intel_hdmi_dpms,
499 .mode_fixup = intel_hdmi_mode_fixup,
500 .prepare = intel_encoder_prepare,
501 .mode_set = intel_hdmi_mode_set,
502 .commit = intel_encoder_commit,
503};
504
505static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700506 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800507 .detect = intel_hdmi_detect,
508 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100509 .set_property = intel_hdmi_set_property,
Eric Anholt7d573822009-01-02 13:33:00 -0800510 .destroy = intel_hdmi_destroy,
511};
512
513static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
514 .get_modes = intel_hdmi_get_modes,
515 .mode_valid = intel_hdmi_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100516 .best_encoder = intel_best_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800517};
518
Eric Anholt7d573822009-01-02 13:33:00 -0800519static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100520 .destroy = intel_encoder_destroy,
Eric Anholt7d573822009-01-02 13:33:00 -0800521};
522
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100523static void
524intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
525{
Chris Wilson3f43c482011-05-12 22:17:24 +0100526 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000527 intel_attach_broadcast_rgb_property(connector);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100528}
529
Eric Anholt7d573822009-01-02 13:33:00 -0800530void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
531{
532 struct drm_i915_private *dev_priv = dev->dev_private;
533 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700534 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800535 struct intel_connector *intel_connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100536 struct intel_hdmi *intel_hdmi;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530537 int i;
Eric Anholt7d573822009-01-02 13:33:00 -0800538
Chris Wilsonea5b2132010-08-04 13:50:23 +0100539 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
540 if (!intel_hdmi)
Eric Anholt7d573822009-01-02 13:33:00 -0800541 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800542
543 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
544 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100545 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800546 return;
547 }
548
Chris Wilsonea5b2132010-08-04 13:50:23 +0100549 intel_encoder = &intel_hdmi->base;
Chris Wilson373a3cf2010-09-15 12:03:59 +0100550 drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
551 DRM_MODE_ENCODER_TMDS);
552
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800553 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800554 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400555 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800556 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
557
Eric Anholt21d40d32010-03-25 11:11:14 -0700558 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800559
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000560 connector->polled = DRM_CONNECTOR_POLL_HPD;
Peter Rossc3febcc2012-01-28 14:49:26 +0100561 connector->interlace_allowed = 1;
Eric Anholt7d573822009-01-02 13:33:00 -0800562 connector->doublescan_allowed = 0;
Jesse Barnes27f82272011-09-02 12:54:37 -0700563 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
Eric Anholt7d573822009-01-02 13:33:00 -0800564
565 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800566 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700567 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700568 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800569 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800570 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700571 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700572 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800573 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800574 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700575 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700576 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800577 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800578 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700579 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700580 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800581 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800582 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700583 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700584 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800585 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800586 }
Eric Anholt7d573822009-01-02 13:33:00 -0800587
Chris Wilsonea5b2132010-08-04 13:50:23 +0100588 intel_hdmi->sdvox_reg = sdvox_reg;
Eric Anholt7d573822009-01-02 13:33:00 -0800589
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530590 if (!HAS_PCH_SPLIT(dev)) {
Jesse Barnes45187ac2011-08-03 09:22:55 -0700591 intel_hdmi->write_infoframe = i9xx_write_infoframe;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530592 I915_WRITE(VIDEO_DIP_CTL, 0);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700593 } else if (IS_VALLEYVIEW(dev)) {
594 intel_hdmi->write_infoframe = vlv_write_infoframe;
595 for_each_pipe(i)
596 I915_WRITE(VLV_TVIDEO_DIP_CTL(i), 0);
597 } else {
Jesse Barnes45187ac2011-08-03 09:22:55 -0700598 intel_hdmi->write_infoframe = ironlake_write_infoframe;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530599 for_each_pipe(i)
600 I915_WRITE(TVIDEO_DIP_CTL(i), 0);
601 }
Jesse Barnes45187ac2011-08-03 09:22:55 -0700602
Chris Wilson4ef69c72010-09-09 15:14:28 +0100603 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800604
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100605 intel_hdmi_add_properties(intel_hdmi, connector);
606
Chris Wilsondf0e9242010-09-09 16:20:55 +0100607 intel_connector_attach_encoder(intel_connector, intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800608 drm_sysfs_connector_add(connector);
609
610 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
611 * 0xd. Failure to do so will result in spurious interrupts being
612 * generated on the port when a cable is not attached.
613 */
614 if (IS_G4X(dev) && !IS_GM45(dev)) {
615 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
616 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
617 }
Eric Anholt7d573822009-01-02 13:33:00 -0800618}