blob: b507d38faa10d9cc035932b1f4bf537ca263fbca [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
Eugeni Dodonovf5bbfca2012-05-09 15:37:30 -030040struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
Chris Wilsonea5b2132010-08-04 13:50:23 +010041{
Chris Wilson4ef69c72010-09-09 15:14:28 +010042 return container_of(encoder, struct intel_hdmi, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +010043}
44
Chris Wilsondf0e9242010-09-09 16:20:55 +010045static struct intel_hdmi *intel_attached_hdmi(struct drm_connector *connector)
46{
47 return container_of(intel_attached_encoder(connector),
48 struct intel_hdmi, base);
49}
50
Jesse Barnes45187ac2011-08-03 09:22:55 -070051void intel_dip_infoframe_csum(struct dip_infoframe *frame)
David Härdeman3c17fe42010-09-24 21:44:32 +020052{
Jesse Barnes45187ac2011-08-03 09:22:55 -070053 uint8_t *data = (uint8_t *)frame;
David Härdeman3c17fe42010-09-24 21:44:32 +020054 uint8_t sum = 0;
55 unsigned i;
56
Jesse Barnes45187ac2011-08-03 09:22:55 -070057 frame->checksum = 0;
58 frame->ecc = 0;
David Härdeman3c17fe42010-09-24 21:44:32 +020059
Jesse Barnes64a8fc02011-09-22 11:16:00 +053060 for (i = 0; i < frame->len + DIP_HEADER_SIZE; i++)
David Härdeman3c17fe42010-09-24 21:44:32 +020061 sum += data[i];
62
Jesse Barnes45187ac2011-08-03 09:22:55 -070063 frame->checksum = 0x100 - sum;
David Härdeman3c17fe42010-09-24 21:44:32 +020064}
65
Daniel Vetterbc2481f2012-05-08 15:18:32 +020066static u32 g4x_infoframe_index(struct dip_infoframe *frame)
David Härdeman3c17fe42010-09-24 21:44:32 +020067{
Jesse Barnes45187ac2011-08-03 09:22:55 -070068 switch (frame->type) {
69 case DIP_TYPE_AVI:
Paulo Zanonied517fb2012-05-14 17:12:50 -030070 return VIDEO_DIP_SELECT_AVI;
Jesse Barnes45187ac2011-08-03 09:22:55 -070071 case DIP_TYPE_SPD:
Paulo Zanonied517fb2012-05-14 17:12:50 -030072 return VIDEO_DIP_SELECT_SPD;
Jesse Barnes45187ac2011-08-03 09:22:55 -070073 default:
74 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
Paulo Zanonied517fb2012-05-14 17:12:50 -030075 return 0;
Jesse Barnes45187ac2011-08-03 09:22:55 -070076 }
Jesse Barnes45187ac2011-08-03 09:22:55 -070077}
78
Daniel Vetterbc2481f2012-05-08 15:18:32 +020079static u32 g4x_infoframe_enable(struct dip_infoframe *frame)
Jesse Barnes45187ac2011-08-03 09:22:55 -070080{
Jesse Barnes45187ac2011-08-03 09:22:55 -070081 switch (frame->type) {
82 case DIP_TYPE_AVI:
Paulo Zanonied517fb2012-05-14 17:12:50 -030083 return VIDEO_DIP_ENABLE_AVI;
Jesse Barnes45187ac2011-08-03 09:22:55 -070084 case DIP_TYPE_SPD:
Paulo Zanonied517fb2012-05-14 17:12:50 -030085 return VIDEO_DIP_ENABLE_SPD;
Paulo Zanonifa193ff2012-05-04 17:18:20 -030086 default:
87 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
Paulo Zanonied517fb2012-05-14 17:12:50 -030088 return 0;
Paulo Zanonifa193ff2012-05-04 17:18:20 -030089 }
Paulo Zanonifa193ff2012-05-04 17:18:20 -030090}
91
Paulo Zanoni2da8af52012-05-14 17:12:51 -030092static u32 hsw_infoframe_enable(struct dip_infoframe *frame)
93{
94 switch (frame->type) {
95 case DIP_TYPE_AVI:
96 return VIDEO_DIP_ENABLE_AVI_HSW;
97 case DIP_TYPE_SPD:
98 return VIDEO_DIP_ENABLE_SPD_HSW;
99 default:
100 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
101 return 0;
102 }
103}
104
105static u32 hsw_infoframe_data_reg(struct dip_infoframe *frame, enum pipe pipe)
106{
107 switch (frame->type) {
108 case DIP_TYPE_AVI:
109 return HSW_TVIDEO_DIP_AVI_DATA(pipe);
110 case DIP_TYPE_SPD:
111 return HSW_TVIDEO_DIP_SPD_DATA(pipe);
112 default:
113 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
114 return 0;
115 }
116}
117
Daniel Vettera3da1df2012-05-08 15:19:06 +0200118static void g4x_write_infoframe(struct drm_encoder *encoder,
119 struct dip_infoframe *frame)
Jesse Barnes45187ac2011-08-03 09:22:55 -0700120{
121 uint32_t *data = (uint32_t *)frame;
David Härdeman3c17fe42010-09-24 21:44:32 +0200122 struct drm_device *dev = encoder->dev;
123 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300124 u32 val = I915_READ(VIDEO_DIP_CTL);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700125 unsigned i, len = DIP_HEADER_SIZE + frame->len;
David Härdeman3c17fe42010-09-24 21:44:32 +0200126
Paulo Zanoni822974a2012-05-28 16:42:51 -0300127 WARN(!(val & VIDEO_DIP_ENABLE), "Writing DIP with CTL reg disabled\n");
128
Paulo Zanoni1d4f85a2012-05-04 17:18:18 -0300129 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200130 val |= g4x_infoframe_index(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700131
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200132 val &= ~g4x_infoframe_enable(frame);
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300133
134 I915_WRITE(VIDEO_DIP_CTL, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700135
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300136 mmiowb();
Jesse Barnes45187ac2011-08-03 09:22:55 -0700137 for (i = 0; i < len; i += 4) {
David Härdeman3c17fe42010-09-24 21:44:32 +0200138 I915_WRITE(VIDEO_DIP_DATA, *data);
139 data++;
140 }
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300141 mmiowb();
David Härdeman3c17fe42010-09-24 21:44:32 +0200142
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200143 val |= g4x_infoframe_enable(frame);
Paulo Zanoni60c5ea22012-05-04 17:18:22 -0300144 val &= ~VIDEO_DIP_FREQ_MASK;
Daniel Vetter4b24c932012-05-08 14:41:00 +0200145 val |= VIDEO_DIP_FREQ_VSYNC;
Jesse Barnes45187ac2011-08-03 09:22:55 -0700146
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300147 I915_WRITE(VIDEO_DIP_CTL, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300148 POSTING_READ(VIDEO_DIP_CTL);
David Härdeman3c17fe42010-09-24 21:44:32 +0200149}
150
Paulo Zanonifdf12502012-05-04 17:18:24 -0300151static void ibx_write_infoframe(struct drm_encoder *encoder,
152 struct dip_infoframe *frame)
153{
154 uint32_t *data = (uint32_t *)frame;
155 struct drm_device *dev = encoder->dev;
156 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonied517fb2012-05-14 17:12:50 -0300157 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300158 int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
159 unsigned i, len = DIP_HEADER_SIZE + frame->len;
160 u32 val = I915_READ(reg);
161
Paulo Zanoni822974a2012-05-28 16:42:51 -0300162 WARN(!(val & VIDEO_DIP_ENABLE), "Writing DIP with CTL reg disabled\n");
163
Paulo Zanonifdf12502012-05-04 17:18:24 -0300164 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200165 val |= g4x_infoframe_index(frame);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300166
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200167 val &= ~g4x_infoframe_enable(frame);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300168
169 I915_WRITE(reg, val);
170
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300171 mmiowb();
Paulo Zanonifdf12502012-05-04 17:18:24 -0300172 for (i = 0; i < len; i += 4) {
173 I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
174 data++;
175 }
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300176 mmiowb();
Paulo Zanonifdf12502012-05-04 17:18:24 -0300177
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200178 val |= g4x_infoframe_enable(frame);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300179 val &= ~VIDEO_DIP_FREQ_MASK;
Daniel Vetter4b24c932012-05-08 14:41:00 +0200180 val |= VIDEO_DIP_FREQ_VSYNC;
Paulo Zanonifdf12502012-05-04 17:18:24 -0300181
182 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300183 POSTING_READ(reg);
Paulo Zanonifdf12502012-05-04 17:18:24 -0300184}
185
186static void cpt_write_infoframe(struct drm_encoder *encoder,
187 struct dip_infoframe *frame)
Jesse Barnes45187ac2011-08-03 09:22:55 -0700188{
189 uint32_t *data = (uint32_t *)frame;
190 struct drm_device *dev = encoder->dev;
191 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonied517fb2012-05-14 17:12:50 -0300192 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700193 int reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
194 unsigned i, len = DIP_HEADER_SIZE + frame->len;
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300195 u32 val = I915_READ(reg);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700196
Paulo Zanoni822974a2012-05-28 16:42:51 -0300197 WARN(!(val & VIDEO_DIP_ENABLE), "Writing DIP with CTL reg disabled\n");
198
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530199 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200200 val |= g4x_infoframe_index(frame);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700201
Paulo Zanoniecb97852012-05-04 17:18:21 -0300202 /* The DIP control register spec says that we need to update the AVI
203 * infoframe without clearing its enable bit */
Paulo Zanoni822974a2012-05-28 16:42:51 -0300204 if (frame->type != DIP_TYPE_AVI)
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200205 val &= ~g4x_infoframe_enable(frame);
Paulo Zanoniecb97852012-05-04 17:18:21 -0300206
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300207 I915_WRITE(reg, val);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700208
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300209 mmiowb();
Jesse Barnes45187ac2011-08-03 09:22:55 -0700210 for (i = 0; i < len; i += 4) {
211 I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
212 data++;
213 }
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300214 mmiowb();
Jesse Barnes45187ac2011-08-03 09:22:55 -0700215
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200216 val |= g4x_infoframe_enable(frame);
Paulo Zanoni60c5ea22012-05-04 17:18:22 -0300217 val &= ~VIDEO_DIP_FREQ_MASK;
Daniel Vetter4b24c932012-05-08 14:41:00 +0200218 val |= VIDEO_DIP_FREQ_VSYNC;
Jesse Barnes45187ac2011-08-03 09:22:55 -0700219
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300220 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300221 POSTING_READ(reg);
Jesse Barnes45187ac2011-08-03 09:22:55 -0700222}
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700223
224static void vlv_write_infoframe(struct drm_encoder *encoder,
225 struct dip_infoframe *frame)
226{
227 uint32_t *data = (uint32_t *)frame;
228 struct drm_device *dev = encoder->dev;
229 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonied517fb2012-05-14 17:12:50 -0300230 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700231 int reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
232 unsigned i, len = DIP_HEADER_SIZE + frame->len;
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300233 u32 val = I915_READ(reg);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700234
Paulo Zanoni822974a2012-05-28 16:42:51 -0300235 WARN(!(val & VIDEO_DIP_ENABLE), "Writing DIP with CTL reg disabled\n");
236
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700237 val &= ~(VIDEO_DIP_SELECT_MASK | 0xf); /* clear DIP data offset */
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200238 val |= g4x_infoframe_index(frame);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700239
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200240 val &= ~g4x_infoframe_enable(frame);
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300241
242 I915_WRITE(reg, val);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700243
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300244 mmiowb();
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700245 for (i = 0; i < len; i += 4) {
246 I915_WRITE(VLV_TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
247 data++;
248 }
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300249 mmiowb();
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700250
Daniel Vetterbc2481f2012-05-08 15:18:32 +0200251 val |= g4x_infoframe_enable(frame);
Paulo Zanoni60c5ea22012-05-04 17:18:22 -0300252 val &= ~VIDEO_DIP_FREQ_MASK;
Daniel Vetter4b24c932012-05-08 14:41:00 +0200253 val |= VIDEO_DIP_FREQ_VSYNC;
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700254
Paulo Zanoni22509ec2012-05-04 17:18:17 -0300255 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300256 POSTING_READ(reg);
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700257}
258
Eugeni Dodonov8c5f5f72012-05-10 10:18:02 -0300259static void hsw_write_infoframe(struct drm_encoder *encoder,
Paulo Zanonied517fb2012-05-14 17:12:50 -0300260 struct dip_infoframe *frame)
Eugeni Dodonov8c5f5f72012-05-10 10:18:02 -0300261{
Paulo Zanoni2da8af52012-05-14 17:12:51 -0300262 uint32_t *data = (uint32_t *)frame;
263 struct drm_device *dev = encoder->dev;
264 struct drm_i915_private *dev_priv = dev->dev_private;
265 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
266 u32 ctl_reg = HSW_TVIDEO_DIP_CTL(intel_crtc->pipe);
267 u32 data_reg = hsw_infoframe_data_reg(frame, intel_crtc->pipe);
268 unsigned int i, len = DIP_HEADER_SIZE + frame->len;
269 u32 val = I915_READ(ctl_reg);
Eugeni Dodonov8c5f5f72012-05-10 10:18:02 -0300270
Paulo Zanoni2da8af52012-05-14 17:12:51 -0300271 if (data_reg == 0)
272 return;
Eugeni Dodonov8c5f5f72012-05-10 10:18:02 -0300273
Paulo Zanoni2da8af52012-05-14 17:12:51 -0300274 val &= ~hsw_infoframe_enable(frame);
275 I915_WRITE(ctl_reg, val);
276
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300277 mmiowb();
Paulo Zanoni2da8af52012-05-14 17:12:51 -0300278 for (i = 0; i < len; i += 4) {
279 I915_WRITE(data_reg + i, *data);
280 data++;
281 }
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300282 mmiowb();
Paulo Zanoni2da8af52012-05-14 17:12:51 -0300283
284 val |= hsw_infoframe_enable(frame);
285 I915_WRITE(ctl_reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300286 POSTING_READ(ctl_reg);
Eugeni Dodonov8c5f5f72012-05-10 10:18:02 -0300287}
288
Jesse Barnes45187ac2011-08-03 09:22:55 -0700289static void intel_set_infoframe(struct drm_encoder *encoder,
290 struct dip_infoframe *frame)
291{
292 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
293
Jesse Barnes45187ac2011-08-03 09:22:55 -0700294 intel_dip_infoframe_csum(frame);
295 intel_hdmi->write_infoframe(encoder, frame);
296}
297
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300298static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder,
Paulo Zanonic846b612012-04-13 16:31:41 -0300299 struct drm_display_mode *adjusted_mode)
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700300{
301 struct dip_infoframe avi_if = {
302 .type = DIP_TYPE_AVI,
303 .ver = DIP_VERSION_AVI,
304 .len = DIP_LEN_AVI,
305 };
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700306
Paulo Zanonic846b612012-04-13 16:31:41 -0300307 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
308 avi_if.body.avi.YQ_CN_PR |= DIP_AVI_PR_2;
309
Jesse Barnes45187ac2011-08-03 09:22:55 -0700310 intel_set_infoframe(encoder, &avi_if);
Jesse Barnesb055c8f2011-07-08 11:31:57 -0700311}
312
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300313static void intel_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
Jesse Barnesc0864cb2011-08-03 09:22:56 -0700314{
315 struct dip_infoframe spd_if;
316
317 memset(&spd_if, 0, sizeof(spd_if));
318 spd_if.type = DIP_TYPE_SPD;
319 spd_if.ver = DIP_VERSION_SPD;
320 spd_if.len = DIP_LEN_SPD;
321 strcpy(spd_if.body.spd.vn, "Intel");
322 strcpy(spd_if.body.spd.pd, "Integrated gfx");
323 spd_if.body.spd.sdi = DIP_SPD_PC;
324
325 intel_set_infoframe(encoder, &spd_if);
326}
327
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300328static void g4x_set_infoframes(struct drm_encoder *encoder,
329 struct drm_display_mode *adjusted_mode)
330{
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300331 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
332 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
333 u32 reg = VIDEO_DIP_CTL;
334 u32 val = I915_READ(reg);
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300335 u32 port;
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300336
337 /* If the registers were not initialized yet, they might be zeroes,
338 * which means we're selecting the AVI DIP and we're setting its
339 * frequency to once. This seems to really confuse the HW and make
340 * things stop working (the register spec says the AVI always needs to
341 * be sent every VSync). So here we avoid writing to the register more
342 * than we need and also explicitly select the AVI DIP and explicitly
343 * set its frequency to every VSync. Avoiding to write it twice seems to
344 * be enough to solve the problem, but being defensive shouldn't hurt us
345 * either. */
346 val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;
347
348 if (!intel_hdmi->has_hdmi_sink) {
349 if (!(val & VIDEO_DIP_ENABLE))
350 return;
351 val &= ~VIDEO_DIP_ENABLE;
352 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300353 POSTING_READ(reg);
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300354 return;
355 }
356
Paulo Zanonif278d972012-05-28 16:42:50 -0300357 switch (intel_hdmi->sdvox_reg) {
358 case SDVOB:
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300359 port = VIDEO_DIP_PORT_B;
Paulo Zanonif278d972012-05-28 16:42:50 -0300360 break;
361 case SDVOC:
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300362 port = VIDEO_DIP_PORT_C;
Paulo Zanonif278d972012-05-28 16:42:50 -0300363 break;
364 default:
365 return;
366 }
367
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300368 if (port != (val & VIDEO_DIP_PORT_MASK)) {
369 if (val & VIDEO_DIP_ENABLE) {
370 val &= ~VIDEO_DIP_ENABLE;
371 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300372 POSTING_READ(reg);
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300373 }
374 val &= ~VIDEO_DIP_PORT_MASK;
375 val |= port;
376 }
377
Paulo Zanoni822974a2012-05-28 16:42:51 -0300378 val |= VIDEO_DIP_ENABLE;
Paulo Zanoni0dd87d22012-05-28 16:42:53 -0300379 val &= ~VIDEO_DIP_ENABLE_VENDOR;
Paulo Zanoni822974a2012-05-28 16:42:51 -0300380
Paulo Zanonif278d972012-05-28 16:42:50 -0300381 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300382 POSTING_READ(reg);
Paulo Zanonif278d972012-05-28 16:42:50 -0300383
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300384 intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
385 intel_hdmi_set_spd_infoframe(encoder);
386}
387
388static void ibx_set_infoframes(struct drm_encoder *encoder,
389 struct drm_display_mode *adjusted_mode)
390{
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300391 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
392 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
393 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
394 u32 reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
395 u32 val = I915_READ(reg);
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300396 u32 port;
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300397
398 /* See the big comment in g4x_set_infoframes() */
399 val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;
400
401 if (!intel_hdmi->has_hdmi_sink) {
402 if (!(val & VIDEO_DIP_ENABLE))
403 return;
404 val &= ~VIDEO_DIP_ENABLE;
405 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300406 POSTING_READ(reg);
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300407 return;
408 }
409
Paulo Zanonif278d972012-05-28 16:42:50 -0300410 switch (intel_hdmi->sdvox_reg) {
411 case HDMIB:
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300412 port = VIDEO_DIP_PORT_B;
Paulo Zanonif278d972012-05-28 16:42:50 -0300413 break;
414 case HDMIC:
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300415 port = VIDEO_DIP_PORT_C;
Paulo Zanonif278d972012-05-28 16:42:50 -0300416 break;
417 case HDMID:
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300418 port = VIDEO_DIP_PORT_D;
Paulo Zanonif278d972012-05-28 16:42:50 -0300419 break;
420 default:
421 return;
422 }
423
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300424 if (port != (val & VIDEO_DIP_PORT_MASK)) {
425 if (val & VIDEO_DIP_ENABLE) {
426 val &= ~VIDEO_DIP_ENABLE;
427 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300428 POSTING_READ(reg);
Paulo Zanoni72b78c92012-05-28 16:42:54 -0300429 }
430 val &= ~VIDEO_DIP_PORT_MASK;
431 val |= port;
432 }
433
Paulo Zanoni822974a2012-05-28 16:42:51 -0300434 val |= VIDEO_DIP_ENABLE;
Paulo Zanoni0dd87d22012-05-28 16:42:53 -0300435 val &= ~(VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
436 VIDEO_DIP_ENABLE_GCP);
Paulo Zanoni822974a2012-05-28 16:42:51 -0300437
Paulo Zanonif278d972012-05-28 16:42:50 -0300438 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300439 POSTING_READ(reg);
Paulo Zanonif278d972012-05-28 16:42:50 -0300440
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300441 intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
442 intel_hdmi_set_spd_infoframe(encoder);
443}
444
445static void cpt_set_infoframes(struct drm_encoder *encoder,
446 struct drm_display_mode *adjusted_mode)
447{
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300448 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
449 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
450 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
451 u32 reg = TVIDEO_DIP_CTL(intel_crtc->pipe);
452 u32 val = I915_READ(reg);
453
454 /* See the big comment in g4x_set_infoframes() */
455 val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;
456
457 if (!intel_hdmi->has_hdmi_sink) {
458 if (!(val & VIDEO_DIP_ENABLE))
459 return;
460 val &= ~(VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI);
461 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300462 POSTING_READ(reg);
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300463 return;
464 }
465
Paulo Zanoni822974a2012-05-28 16:42:51 -0300466 /* Set both together, unset both together: see the spec. */
467 val |= VIDEO_DIP_ENABLE | VIDEO_DIP_ENABLE_AVI;
Paulo Zanoni0dd87d22012-05-28 16:42:53 -0300468 val &= ~(VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
469 VIDEO_DIP_ENABLE_GCP);
Paulo Zanoni822974a2012-05-28 16:42:51 -0300470
471 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300472 POSTING_READ(reg);
Paulo Zanoni822974a2012-05-28 16:42:51 -0300473
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300474 intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
475 intel_hdmi_set_spd_infoframe(encoder);
476}
477
478static void vlv_set_infoframes(struct drm_encoder *encoder,
479 struct drm_display_mode *adjusted_mode)
480{
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300481 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
482 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
483 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
484 u32 reg = VLV_TVIDEO_DIP_CTL(intel_crtc->pipe);
485 u32 val = I915_READ(reg);
486
487 /* See the big comment in g4x_set_infoframes() */
488 val |= VIDEO_DIP_SELECT_AVI | VIDEO_DIP_FREQ_VSYNC;
489
490 if (!intel_hdmi->has_hdmi_sink) {
491 if (!(val & VIDEO_DIP_ENABLE))
492 return;
493 val &= ~VIDEO_DIP_ENABLE;
494 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300495 POSTING_READ(reg);
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300496 return;
497 }
498
Paulo Zanoni822974a2012-05-28 16:42:51 -0300499 val |= VIDEO_DIP_ENABLE;
Paulo Zanoni0dd87d22012-05-28 16:42:53 -0300500 val &= ~(VIDEO_DIP_ENABLE_VENDOR | VIDEO_DIP_ENABLE_GAMUT |
501 VIDEO_DIP_ENABLE_GCP);
Paulo Zanoni822974a2012-05-28 16:42:51 -0300502
503 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300504 POSTING_READ(reg);
Paulo Zanoni822974a2012-05-28 16:42:51 -0300505
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300506 intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
507 intel_hdmi_set_spd_infoframe(encoder);
508}
509
510static void hsw_set_infoframes(struct drm_encoder *encoder,
511 struct drm_display_mode *adjusted_mode)
512{
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300513 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
514 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
515 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
516 u32 reg = HSW_TVIDEO_DIP_CTL(intel_crtc->pipe);
Paulo Zanoni0dd87d22012-05-28 16:42:53 -0300517 u32 val = I915_READ(reg);
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300518
519 if (!intel_hdmi->has_hdmi_sink) {
520 I915_WRITE(reg, 0);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300521 POSTING_READ(reg);
Paulo Zanoni0c14c7f2012-05-28 16:42:49 -0300522 return;
523 }
524
Paulo Zanoni0dd87d22012-05-28 16:42:53 -0300525 val &= ~(VIDEO_DIP_ENABLE_VSC_HSW | VIDEO_DIP_ENABLE_GCP_HSW |
526 VIDEO_DIP_ENABLE_VS_HSW | VIDEO_DIP_ENABLE_GMP_HSW);
527
528 I915_WRITE(reg, val);
Paulo Zanoni9d9740f2012-05-28 16:43:00 -0300529 POSTING_READ(reg);
Paulo Zanoni0dd87d22012-05-28 16:42:53 -0300530
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300531 intel_hdmi_set_avi_infoframe(encoder, adjusted_mode);
532 intel_hdmi_set_spd_infoframe(encoder);
533}
534
Eric Anholt7d573822009-01-02 13:33:00 -0800535static void intel_hdmi_mode_set(struct drm_encoder *encoder,
536 struct drm_display_mode *mode,
537 struct drm_display_mode *adjusted_mode)
538{
539 struct drm_device *dev = encoder->dev;
540 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonied517fb2012-05-14 17:12:50 -0300541 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100542 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800543 u32 sdvox;
544
Paulo Zanonib659c3d2012-05-28 16:42:56 -0300545 sdvox = SDVO_ENCODING_HDMI;
Jesse Barnes5d4fac92011-06-24 12:19:19 -0700546 if (!HAS_PCH_SPLIT(dev))
547 sdvox |= intel_hdmi->color_range;
Adam Jacksonb599c0b2010-07-16 14:46:31 -0400548 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
549 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
550 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
551 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Eric Anholt7d573822009-01-02 13:33:00 -0800552
Jesse Barnes020f6702011-06-24 12:19:25 -0700553 if (intel_crtc->bpp > 24)
554 sdvox |= COLOR_FORMAT_12bpc;
555 else
556 sdvox |= COLOR_FORMAT_8bpc;
557
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800558 /* Required on CPT */
559 if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
560 sdvox |= HDMI_MODE_SELECT;
561
David Härdeman3c17fe42010-09-24 21:44:32 +0200562 if (intel_hdmi->has_audio) {
Wu Fengguange0dac652011-09-05 14:25:34 +0800563 DRM_DEBUG_DRIVER("Enabling HDMI audio on pipe %c\n",
564 pipe_name(intel_crtc->pipe));
Eric Anholt7d573822009-01-02 13:33:00 -0800565 sdvox |= SDVO_AUDIO_ENABLE;
David Härdeman3c17fe42010-09-24 21:44:32 +0200566 sdvox |= SDVO_NULL_PACKETS_DURING_VSYNC;
Wu Fengguange0dac652011-09-05 14:25:34 +0800567 intel_write_eld(encoder, adjusted_mode);
David Härdeman3c17fe42010-09-24 21:44:32 +0200568 }
Eric Anholt7d573822009-01-02 13:33:00 -0800569
Jesse Barnes75770562011-10-12 09:01:58 -0700570 if (HAS_PCH_CPT(dev))
571 sdvox |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
572 else if (intel_crtc->pipe == 1)
573 sdvox |= SDVO_PIPE_B_SELECT;
Eric Anholt7d573822009-01-02 13:33:00 -0800574
Chris Wilsonea5b2132010-08-04 13:50:23 +0100575 I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
576 POSTING_READ(intel_hdmi->sdvox_reg);
David Härdeman3c17fe42010-09-24 21:44:32 +0200577
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300578 intel_hdmi->set_infoframes(encoder, adjusted_mode);
Eric Anholt7d573822009-01-02 13:33:00 -0800579}
580
581static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
582{
583 struct drm_device *dev = encoder->dev;
584 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100585 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800586 u32 temp;
Wu Fengguang2deed762011-12-09 20:42:20 +0800587 u32 enable_bits = SDVO_ENABLE;
588
589 if (intel_hdmi->has_audio)
590 enable_bits |= SDVO_AUDIO_ENABLE;
Eric Anholt7d573822009-01-02 13:33:00 -0800591
Chris Wilsonea5b2132010-08-04 13:50:23 +0100592 temp = I915_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000593
594 /* HW workaround, need to toggle enable bit off and on for 12bpc, but
595 * we do this anyway which shows more stable in testing.
596 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800597 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100598 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
599 POSTING_READ(intel_hdmi->sdvox_reg);
Eric Anholt7d573822009-01-02 13:33:00 -0800600 }
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000601
602 if (mode != DRM_MODE_DPMS_ON) {
Wu Fengguang2deed762011-12-09 20:42:20 +0800603 temp &= ~enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000604 } else {
Wu Fengguang2deed762011-12-09 20:42:20 +0800605 temp |= enable_bits;
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000606 }
607
Chris Wilsonea5b2132010-08-04 13:50:23 +0100608 I915_WRITE(intel_hdmi->sdvox_reg, temp);
609 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000610
611 /* HW workaround, need to write this twice for issue that may result
612 * in first write getting masked.
613 */
Eric Anholtc619eed2010-01-28 16:45:52 -0800614 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100615 I915_WRITE(intel_hdmi->sdvox_reg, temp);
616 POSTING_READ(intel_hdmi->sdvox_reg);
Zhenyu Wangd8a2d0e2009-11-02 07:52:30 +0000617 }
Eric Anholt7d573822009-01-02 13:33:00 -0800618}
619
Eric Anholt7d573822009-01-02 13:33:00 -0800620static int intel_hdmi_mode_valid(struct drm_connector *connector,
621 struct drm_display_mode *mode)
622{
623 if (mode->clock > 165000)
624 return MODE_CLOCK_HIGH;
625 if (mode->clock < 20000)
Nicolas Kaiser5cbba412011-05-30 12:48:26 +0200626 return MODE_CLOCK_LOW;
Eric Anholt7d573822009-01-02 13:33:00 -0800627
628 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
629 return MODE_NO_DBLESCAN;
630
631 return MODE_OK;
632}
633
634static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
635 struct drm_display_mode *mode,
636 struct drm_display_mode *adjusted_mode)
637{
638 return true;
639}
640
Chris Wilson8ec22b22012-05-11 18:01:34 +0100641static bool g4x_hdmi_connected(struct intel_hdmi *intel_hdmi)
642{
643 struct drm_device *dev = intel_hdmi->base.base.dev;
644 struct drm_i915_private *dev_priv = dev->dev_private;
645 uint32_t bit;
646
647 switch (intel_hdmi->sdvox_reg) {
Chris Wilsoneeafaac2012-05-25 10:23:37 +0100648 case SDVOB:
Chris Wilson8ec22b22012-05-11 18:01:34 +0100649 bit = HDMIB_HOTPLUG_LIVE_STATUS;
650 break;
Chris Wilsoneeafaac2012-05-25 10:23:37 +0100651 case SDVOC:
Chris Wilson8ec22b22012-05-11 18:01:34 +0100652 bit = HDMIC_HOTPLUG_LIVE_STATUS;
653 break;
Chris Wilson8ec22b22012-05-11 18:01:34 +0100654 default:
655 bit = 0;
656 break;
657 }
658
659 return I915_READ(PORT_HOTPLUG_STAT) & bit;
660}
661
Keith Packardaa93d632009-05-05 09:52:46 -0700662static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +0100663intel_hdmi_detect(struct drm_connector *connector, bool force)
Ma Ling9dff6af2009-04-02 13:13:26 +0800664{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100665 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700666 struct drm_i915_private *dev_priv = connector->dev->dev_private;
667 struct edid *edid;
Keith Packardaa93d632009-05-05 09:52:46 -0700668 enum drm_connector_status status = connector_status_disconnected;
Ma Ling9dff6af2009-04-02 13:13:26 +0800669
Chris Wilson8ec22b22012-05-11 18:01:34 +0100670 if (IS_G4X(connector->dev) && !g4x_hdmi_connected(intel_hdmi))
671 return status;
672
Chris Wilsonea5b2132010-08-04 13:50:23 +0100673 intel_hdmi->has_hdmi_sink = false;
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800674 intel_hdmi->has_audio = false;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700675 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800676 intel_gmbus_get_adapter(dev_priv,
677 intel_hdmi->ddc_bus));
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800678
Keith Packardaa93d632009-05-05 09:52:46 -0700679 if (edid) {
Eric Anholtbe9f1c42009-06-21 22:14:55 -0700680 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
Keith Packardaa93d632009-05-05 09:52:46 -0700681 status = connector_status_connected;
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800682 if (intel_hdmi->force_audio != HDMI_AUDIO_OFF_DVI)
683 intel_hdmi->has_hdmi_sink =
684 drm_detect_hdmi_monitor(edid);
Zhenyu Wang2e3d6002010-09-10 10:39:40 +0800685 intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
Keith Packardaa93d632009-05-05 09:52:46 -0700686 }
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800687 connector->display_info.raw_edid = NULL;
Keith Packardaa93d632009-05-05 09:52:46 -0700688 kfree(edid);
Ma Ling9dff6af2009-04-02 13:13:26 +0800689 }
ling.ma@intel.com2ded9e22009-07-16 17:23:09 +0800690
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100691 if (status == connector_status_connected) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800692 if (intel_hdmi->force_audio != HDMI_AUDIO_AUTO)
693 intel_hdmi->has_audio =
694 (intel_hdmi->force_audio == HDMI_AUDIO_ON);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100695 }
696
Keith Packardaa93d632009-05-05 09:52:46 -0700697 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +0800698}
699
Eric Anholt7d573822009-01-02 13:33:00 -0800700static int intel_hdmi_get_modes(struct drm_connector *connector)
701{
Chris Wilsondf0e9242010-09-09 16:20:55 +0100702 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700703 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Eric Anholt7d573822009-01-02 13:33:00 -0800704
705 /* We should parse the EDID data and find out if it's an HDMI sink so
706 * we can send audio to it.
707 */
708
Chris Wilsonf899fc62010-07-20 15:44:45 -0700709 return intel_ddc_get_modes(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800710 intel_gmbus_get_adapter(dev_priv,
711 intel_hdmi->ddc_bus));
Eric Anholt7d573822009-01-02 13:33:00 -0800712}
713
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000714static bool
715intel_hdmi_detect_audio(struct drm_connector *connector)
716{
717 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
718 struct drm_i915_private *dev_priv = connector->dev->dev_private;
719 struct edid *edid;
720 bool has_audio = false;
721
722 edid = drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800723 intel_gmbus_get_adapter(dev_priv,
724 intel_hdmi->ddc_bus));
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000725 if (edid) {
726 if (edid->input & DRM_EDID_INPUT_DIGITAL)
727 has_audio = drm_detect_monitor_audio(edid);
728
729 connector->display_info.raw_edid = NULL;
730 kfree(edid);
731 }
732
733 return has_audio;
734}
735
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100736static int
737intel_hdmi_set_property(struct drm_connector *connector,
Paulo Zanonied517fb2012-05-14 17:12:50 -0300738 struct drm_property *property,
739 uint64_t val)
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100740{
741 struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000742 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100743 int ret;
744
745 ret = drm_connector_property_set_value(connector, property, val);
746 if (ret)
747 return ret;
748
Chris Wilson3f43c482011-05-12 22:17:24 +0100749 if (property == dev_priv->force_audio_property) {
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800750 enum hdmi_force_audio i = val;
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000751 bool has_audio;
752
753 if (i == intel_hdmi->force_audio)
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100754 return 0;
755
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000756 intel_hdmi->force_audio = i;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100757
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800758 if (i == HDMI_AUDIO_AUTO)
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000759 has_audio = intel_hdmi_detect_audio(connector);
760 else
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800761 has_audio = (i == HDMI_AUDIO_ON);
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000762
Wu Fengguangb1d7e4b2012-02-14 11:45:36 +0800763 if (i == HDMI_AUDIO_OFF_DVI)
764 intel_hdmi->has_hdmi_sink = 0;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100765
Chris Wilson1aad7ac2011-02-09 18:46:58 +0000766 intel_hdmi->has_audio = has_audio;
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100767 goto done;
768 }
769
Chris Wilsone953fd72011-02-21 22:23:52 +0000770 if (property == dev_priv->broadcast_rgb_property) {
771 if (val == !!intel_hdmi->color_range)
772 return 0;
773
774 intel_hdmi->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
775 goto done;
776 }
777
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100778 return -EINVAL;
779
780done:
781 if (intel_hdmi->base.base.crtc) {
782 struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
783 drm_crtc_helper_set_mode(crtc, &crtc->mode,
784 crtc->x, crtc->y,
785 crtc->fb);
786 }
787
788 return 0;
789}
790
Eric Anholt7d573822009-01-02 13:33:00 -0800791static void intel_hdmi_destroy(struct drm_connector *connector)
792{
Eric Anholt7d573822009-01-02 13:33:00 -0800793 drm_sysfs_connector_remove(connector);
794 drm_connector_cleanup(connector);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800795 kfree(connector);
Eric Anholt7d573822009-01-02 13:33:00 -0800796}
797
Eugeni Dodonov72662e12012-05-09 15:37:31 -0300798static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs_hsw = {
799 .dpms = intel_ddi_dpms,
800 .mode_fixup = intel_hdmi_mode_fixup,
801 .prepare = intel_encoder_prepare,
802 .mode_set = intel_ddi_mode_set,
803 .commit = intel_encoder_commit,
804};
805
Eric Anholt7d573822009-01-02 13:33:00 -0800806static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
807 .dpms = intel_hdmi_dpms,
808 .mode_fixup = intel_hdmi_mode_fixup,
809 .prepare = intel_encoder_prepare,
810 .mode_set = intel_hdmi_mode_set,
811 .commit = intel_encoder_commit,
812};
813
814static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -0700815 .dpms = drm_helper_connector_dpms,
Eric Anholt7d573822009-01-02 13:33:00 -0800816 .detect = intel_hdmi_detect,
817 .fill_modes = drm_helper_probe_single_connector_modes,
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100818 .set_property = intel_hdmi_set_property,
Eric Anholt7d573822009-01-02 13:33:00 -0800819 .destroy = intel_hdmi_destroy,
820};
821
822static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
823 .get_modes = intel_hdmi_get_modes,
824 .mode_valid = intel_hdmi_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +0100825 .best_encoder = intel_best_encoder,
Eric Anholt7d573822009-01-02 13:33:00 -0800826};
827
Eric Anholt7d573822009-01-02 13:33:00 -0800828static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100829 .destroy = intel_encoder_destroy,
Eric Anholt7d573822009-01-02 13:33:00 -0800830};
831
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100832static void
833intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
834{
Chris Wilson3f43c482011-05-12 22:17:24 +0100835 intel_attach_force_audio_property(connector);
Chris Wilsone953fd72011-02-21 22:23:52 +0000836 intel_attach_broadcast_rgb_property(connector);
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100837}
838
Eric Anholt7d573822009-01-02 13:33:00 -0800839void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
840{
841 struct drm_i915_private *dev_priv = dev->dev_private;
842 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -0700843 struct intel_encoder *intel_encoder;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800844 struct intel_connector *intel_connector;
Chris Wilsonea5b2132010-08-04 13:50:23 +0100845 struct intel_hdmi *intel_hdmi;
Eric Anholt7d573822009-01-02 13:33:00 -0800846
Chris Wilsonea5b2132010-08-04 13:50:23 +0100847 intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
848 if (!intel_hdmi)
Eric Anholt7d573822009-01-02 13:33:00 -0800849 return;
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800850
851 intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
852 if (!intel_connector) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100853 kfree(intel_hdmi);
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800854 return;
855 }
856
Chris Wilsonea5b2132010-08-04 13:50:23 +0100857 intel_encoder = &intel_hdmi->base;
Chris Wilson373a3cf2010-09-15 12:03:59 +0100858 drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
859 DRM_MODE_ENCODER_TMDS);
860
Zhenyu Wang674e2d02010-03-29 15:57:42 +0800861 connector = &intel_connector->base;
Eric Anholt7d573822009-01-02 13:33:00 -0800862 drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
Adam Jackson8d911042009-09-23 15:08:29 -0400863 DRM_MODE_CONNECTOR_HDMIA);
Eric Anholt7d573822009-01-02 13:33:00 -0800864 drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
865
Eric Anholt21d40d32010-03-25 11:11:14 -0700866 intel_encoder->type = INTEL_OUTPUT_HDMI;
Eric Anholt7d573822009-01-02 13:33:00 -0800867
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000868 connector->polled = DRM_CONNECTOR_POLL_HPD;
Peter Rossc3febcc2012-01-28 14:49:26 +0100869 connector->interlace_allowed = 1;
Eric Anholt7d573822009-01-02 13:33:00 -0800870 connector->doublescan_allowed = 0;
Jesse Barnes27f82272011-09-02 12:54:37 -0700871 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
Eric Anholt7d573822009-01-02 13:33:00 -0800872
873 /* Set up the DDC bus. */
Ma Lingf8aed702009-08-24 13:50:24 +0800874 if (sdvox_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700875 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700876 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800877 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800878 } else if (sdvox_reg == SDVOC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700879 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700880 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800881 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800882 } else if (sdvox_reg == HDMIB) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700883 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700884 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800885 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800886 } else if (sdvox_reg == HDMIC) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700887 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700888 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800889 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
Ma Lingf8aed702009-08-24 13:50:24 +0800890 } else if (sdvox_reg == HDMID) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700891 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700892 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
Jesse Barnesb01f2c32009-12-11 11:07:17 -0800893 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Eugeni Dodonov7ceae0a2012-05-09 15:37:28 -0300894 } else if (sdvox_reg == DDI_BUF_CTL(PORT_B)) {
895 DRM_DEBUG_DRIVER("LPT: detected output on DDI B\n");
896 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
897 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
898 intel_hdmi->ddi_port = PORT_B;
899 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
900 } else if (sdvox_reg == DDI_BUF_CTL(PORT_C)) {
901 DRM_DEBUG_DRIVER("LPT: detected output on DDI C\n");
902 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
903 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
904 intel_hdmi->ddi_port = PORT_C;
905 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
906 } else if (sdvox_reg == DDI_BUF_CTL(PORT_D)) {
907 DRM_DEBUG_DRIVER("LPT: detected output on DDI D\n");
908 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
909 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
910 intel_hdmi->ddi_port = PORT_D;
911 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
Eugeni Dodonov6e4c1672012-05-09 15:37:13 -0300912 } else {
913 /* If we got an unknown sdvox_reg, things are pretty much broken
914 * in a way that we should let the kernel know about it */
915 BUG();
Ma Lingf8aed702009-08-24 13:50:24 +0800916 }
Eric Anholt7d573822009-01-02 13:33:00 -0800917
Chris Wilsonea5b2132010-08-04 13:50:23 +0100918 intel_hdmi->sdvox_reg = sdvox_reg;
Eric Anholt7d573822009-01-02 13:33:00 -0800919
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530920 if (!HAS_PCH_SPLIT(dev)) {
Daniel Vettera3da1df2012-05-08 15:19:06 +0200921 intel_hdmi->write_infoframe = g4x_write_infoframe;
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300922 intel_hdmi->set_infoframes = g4x_set_infoframes;
Shobhit Kumar90b107c2012-03-28 13:39:32 -0700923 } else if (IS_VALLEYVIEW(dev)) {
924 intel_hdmi->write_infoframe = vlv_write_infoframe;
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300925 intel_hdmi->set_infoframes = vlv_set_infoframes;
Eugeni Dodonov8c5f5f72012-05-10 10:18:02 -0300926 } else if (IS_HASWELL(dev)) {
Eugeni Dodonov8c5f5f72012-05-10 10:18:02 -0300927 intel_hdmi->write_infoframe = hsw_write_infoframe;
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300928 intel_hdmi->set_infoframes = hsw_set_infoframes;
Paulo Zanonifdf12502012-05-04 17:18:24 -0300929 } else if (HAS_PCH_IBX(dev)) {
930 intel_hdmi->write_infoframe = ibx_write_infoframe;
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300931 intel_hdmi->set_infoframes = ibx_set_infoframes;
Paulo Zanonifdf12502012-05-04 17:18:24 -0300932 } else {
933 intel_hdmi->write_infoframe = cpt_write_infoframe;
Paulo Zanoni687f4d02012-05-28 16:42:48 -0300934 intel_hdmi->set_infoframes = cpt_set_infoframes;
Jesse Barnes64a8fc02011-09-22 11:16:00 +0530935 }
Jesse Barnes45187ac2011-08-03 09:22:55 -0700936
Eugeni Dodonov72662e12012-05-09 15:37:31 -0300937 if (IS_HASWELL(dev))
938 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs_hsw);
939 else
940 drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
Eric Anholt7d573822009-01-02 13:33:00 -0800941
Chris Wilson55b7d6e82010-09-19 09:29:33 +0100942 intel_hdmi_add_properties(intel_hdmi, connector);
943
Chris Wilsondf0e9242010-09-09 16:20:55 +0100944 intel_connector_attach_encoder(intel_connector, intel_encoder);
Eric Anholt7d573822009-01-02 13:33:00 -0800945 drm_sysfs_connector_add(connector);
946
947 /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
948 * 0xd. Failure to do so will result in spurious interrupts being
949 * generated on the port when a cable is not attached.
950 */
951 if (IS_G4X(dev) && !IS_GM45(dev)) {
952 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
953 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
954 }
Eric Anholt7d573822009-01-02 13:33:00 -0800955}