]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/gpu/drm/i915/intel_hdmi.c
e7e716cb1c998a0cd1f07ee607c3416464c7f00c
[mv-sheeva.git] / drivers / gpu / drm / i915 / intel_hdmi.c
1 /*
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>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include "drmP.h"
33 #include "drm.h"
34 #include "drm_crtc.h"
35 #include "drm_edid.h"
36 #include "intel_drv.h"
37 #include "i915_drm.h"
38 #include "i915_drv.h"
39
40 struct intel_hdmi {
41         struct intel_encoder base;
42         u32 sdvox_reg;
43         int ddc_bus;
44         uint32_t color_range;
45         bool has_hdmi_sink;
46         bool has_audio;
47         int force_audio;
48         void (*write_infoframe)(struct drm_encoder *encoder,
49                                 struct dip_infoframe *frame);
50 };
51
52 static struct intel_hdmi *enc_to_intel_hdmi(struct drm_encoder *encoder)
53 {
54         return container_of(encoder, struct intel_hdmi, base.base);
55 }
56
57 static 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
63 void intel_dip_infoframe_csum(struct dip_infoframe *frame)
64 {
65         uint8_t *data = (uint8_t *)frame;
66         uint8_t sum = 0;
67         unsigned i;
68
69         frame->checksum = 0;
70         frame->ecc = 0;
71
72         /* Header isn't part of the checksum */
73         for (i = 5; i < frame->len; i++)
74                 sum += data[i];
75
76         frame->checksum = 0x100 - sum;
77 }
78
79 static u32 intel_infoframe_index(struct dip_infoframe *frame)
80 {
81         u32 flags = 0;
82
83         switch (frame->type) {
84         case DIP_TYPE_AVI:
85                 flags |= VIDEO_DIP_SELECT_AVI;
86                 break;
87         case DIP_TYPE_SPD:
88                 flags |= VIDEO_DIP_SELECT_SPD;
89                 break;
90         default:
91                 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
92                 break;
93         }
94
95         return flags;
96 }
97
98 static u32 intel_infoframe_flags(struct dip_infoframe *frame)
99 {
100         u32 flags = 0;
101
102         switch (frame->type) {
103         case DIP_TYPE_AVI:
104                 flags |= VIDEO_DIP_ENABLE_AVI | VIDEO_DIP_FREQ_VSYNC;
105                 break;
106         case DIP_TYPE_SPD:
107                 flags |= VIDEO_DIP_ENABLE_SPD | VIDEO_DIP_FREQ_2VSYNC;
108                 break;
109         default:
110                 DRM_DEBUG_DRIVER("unknown info frame type %d\n", frame->type);
111                 break;
112         }
113
114         return flags;
115 }
116
117 static void i9xx_write_infoframe(struct drm_encoder *encoder,
118                                  struct dip_infoframe *frame)
119 {
120         uint32_t *data = (uint32_t *)frame;
121         struct drm_device *dev = encoder->dev;
122         struct drm_i915_private *dev_priv = dev->dev_private;
123         struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
124         u32 port, flags, val = I915_READ(VIDEO_DIP_CTL);
125         unsigned i, len = DIP_HEADER_SIZE + frame->len;
126
127
128         /* XXX first guess at handling video port, is this corrent? */
129         if (intel_hdmi->sdvox_reg == SDVOB)
130                 port = VIDEO_DIP_PORT_B;
131         else if (intel_hdmi->sdvox_reg == SDVOC)
132                 port = VIDEO_DIP_PORT_C;
133         else
134                 return;
135
136         flags = intel_infoframe_index(frame);
137
138         val &= ~VIDEO_DIP_SELECT_MASK;
139
140         I915_WRITE(VIDEO_DIP_CTL, val | port | flags);
141
142         for (i = 0; i < len; i += 4) {
143                 I915_WRITE(VIDEO_DIP_DATA, *data);
144                 data++;
145         }
146
147         flags |= intel_infoframe_flags(frame);
148
149         I915_WRITE(VIDEO_DIP_CTL, VIDEO_DIP_ENABLE | val | port | flags);
150 }
151
152 static 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;
162         u32 flags, val = I915_READ(reg);
163
164         intel_wait_for_vblank(dev, intel_crtc->pipe);
165
166         flags = intel_infoframe_index(frame);
167
168         val &= ~VIDEO_DIP_SELECT_MASK;
169
170         I915_WRITE(reg, val | flags);
171
172         for (i = 0; i < len; i += 4) {
173                 I915_WRITE(TVIDEO_DIP_DATA(intel_crtc->pipe), *data);
174                 data++;
175         }
176
177         flags |= intel_infoframe_flags(frame);
178
179         I915_WRITE(reg, VIDEO_DIP_ENABLE | val | flags);
180 }
181 static void intel_set_infoframe(struct drm_encoder *encoder,
182                                 struct dip_infoframe *frame)
183 {
184         struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
185
186         if (!intel_hdmi->has_hdmi_sink)
187                 return;
188
189         intel_dip_infoframe_csum(frame);
190         intel_hdmi->write_infoframe(encoder, frame);
191 }
192
193 static void intel_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
194 {
195         struct dip_infoframe avi_if = {
196                 .type = DIP_TYPE_AVI,
197                 .ver = DIP_VERSION_AVI,
198                 .len = DIP_LEN_AVI,
199         };
200
201         intel_set_infoframe(encoder, &avi_if);
202 }
203
204 static void intel_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
205 {
206         struct dip_infoframe spd_if;
207
208         memset(&spd_if, 0, sizeof(spd_if));
209         spd_if.type = DIP_TYPE_SPD;
210         spd_if.ver = DIP_VERSION_SPD;
211         spd_if.len = DIP_LEN_SPD;
212         strcpy(spd_if.body.spd.vn, "Intel");
213         strcpy(spd_if.body.spd.pd, "Integrated gfx");
214         spd_if.body.spd.sdi = DIP_SPD_PC;
215
216         intel_set_infoframe(encoder, &spd_if);
217 }
218
219 static void intel_hdmi_mode_set(struct drm_encoder *encoder,
220                                 struct drm_display_mode *mode,
221                                 struct drm_display_mode *adjusted_mode)
222 {
223         struct drm_device *dev = encoder->dev;
224         struct drm_i915_private *dev_priv = dev->dev_private;
225         struct drm_crtc *crtc = encoder->crtc;
226         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
227         struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
228         u32 sdvox;
229
230         sdvox = SDVO_ENCODING_HDMI | SDVO_BORDER_ENABLE;
231         if (!HAS_PCH_SPLIT(dev))
232                 sdvox |= intel_hdmi->color_range;
233         if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
234                 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
235         if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
236                 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
237
238         if (intel_crtc->bpp > 24)
239                 sdvox |= COLOR_FORMAT_12bpc;
240         else
241                 sdvox |= COLOR_FORMAT_8bpc;
242
243         /* Required on CPT */
244         if (intel_hdmi->has_hdmi_sink && HAS_PCH_CPT(dev))
245                 sdvox |= HDMI_MODE_SELECT;
246
247         if (intel_hdmi->has_audio) {
248                 DRM_DEBUG_DRIVER("Enabling HDMI audio on pipe %c\n",
249                                  pipe_name(intel_crtc->pipe));
250                 sdvox |= SDVO_AUDIO_ENABLE;
251                 sdvox |= SDVO_NULL_PACKETS_DURING_VSYNC;
252                 intel_write_eld(encoder, adjusted_mode);
253         }
254
255         if (HAS_PCH_CPT(dev))
256                 sdvox |= PORT_TRANS_SEL_CPT(intel_crtc->pipe);
257         else if (intel_crtc->pipe == 1)
258                 sdvox |= SDVO_PIPE_B_SELECT;
259
260         I915_WRITE(intel_hdmi->sdvox_reg, sdvox);
261         POSTING_READ(intel_hdmi->sdvox_reg);
262
263         intel_hdmi_set_avi_infoframe(encoder);
264         intel_hdmi_set_spd_infoframe(encoder);
265 }
266
267 static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
268 {
269         struct drm_device *dev = encoder->dev;
270         struct drm_i915_private *dev_priv = dev->dev_private;
271         struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
272         u32 temp;
273
274         temp = I915_READ(intel_hdmi->sdvox_reg);
275
276         /* HW workaround, need to toggle enable bit off and on for 12bpc, but
277          * we do this anyway which shows more stable in testing.
278          */
279         if (HAS_PCH_SPLIT(dev)) {
280                 I915_WRITE(intel_hdmi->sdvox_reg, temp & ~SDVO_ENABLE);
281                 POSTING_READ(intel_hdmi->sdvox_reg);
282         }
283
284         if (mode != DRM_MODE_DPMS_ON) {
285                 temp &= ~SDVO_ENABLE;
286         } else {
287                 temp |= SDVO_ENABLE;
288         }
289
290         I915_WRITE(intel_hdmi->sdvox_reg, temp);
291         POSTING_READ(intel_hdmi->sdvox_reg);
292
293         /* HW workaround, need to write this twice for issue that may result
294          * in first write getting masked.
295          */
296         if (HAS_PCH_SPLIT(dev)) {
297                 I915_WRITE(intel_hdmi->sdvox_reg, temp);
298                 POSTING_READ(intel_hdmi->sdvox_reg);
299         }
300 }
301
302 static int intel_hdmi_mode_valid(struct drm_connector *connector,
303                                  struct drm_display_mode *mode)
304 {
305         if (mode->clock > 165000)
306                 return MODE_CLOCK_HIGH;
307         if (mode->clock < 20000)
308                 return MODE_CLOCK_LOW;
309
310         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
311                 return MODE_NO_DBLESCAN;
312
313         return MODE_OK;
314 }
315
316 static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
317                                   struct drm_display_mode *mode,
318                                   struct drm_display_mode *adjusted_mode)
319 {
320         return true;
321 }
322
323 static enum drm_connector_status
324 intel_hdmi_detect(struct drm_connector *connector, bool force)
325 {
326         struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
327         struct drm_i915_private *dev_priv = connector->dev->dev_private;
328         struct edid *edid;
329         enum drm_connector_status status = connector_status_disconnected;
330
331         intel_hdmi->has_hdmi_sink = false;
332         intel_hdmi->has_audio = false;
333         edid = drm_get_edid(connector,
334                             &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
335
336         if (edid) {
337                 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
338                         status = connector_status_connected;
339                         intel_hdmi->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
340                         intel_hdmi->has_audio = drm_detect_monitor_audio(edid);
341                 }
342                 connector->display_info.raw_edid = NULL;
343                 kfree(edid);
344         }
345
346         if (status == connector_status_connected) {
347                 if (intel_hdmi->force_audio)
348                         intel_hdmi->has_audio = intel_hdmi->force_audio > 0;
349         }
350
351         return status;
352 }
353
354 static int intel_hdmi_get_modes(struct drm_connector *connector)
355 {
356         struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
357         struct drm_i915_private *dev_priv = connector->dev->dev_private;
358
359         /* We should parse the EDID data and find out if it's an HDMI sink so
360          * we can send audio to it.
361          */
362
363         return intel_ddc_get_modes(connector,
364                                    &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
365 }
366
367 static bool
368 intel_hdmi_detect_audio(struct drm_connector *connector)
369 {
370         struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
371         struct drm_i915_private *dev_priv = connector->dev->dev_private;
372         struct edid *edid;
373         bool has_audio = false;
374
375         edid = drm_get_edid(connector,
376                             &dev_priv->gmbus[intel_hdmi->ddc_bus].adapter);
377         if (edid) {
378                 if (edid->input & DRM_EDID_INPUT_DIGITAL)
379                         has_audio = drm_detect_monitor_audio(edid);
380
381                 connector->display_info.raw_edid = NULL;
382                 kfree(edid);
383         }
384
385         return has_audio;
386 }
387
388 static int
389 intel_hdmi_set_property(struct drm_connector *connector,
390                       struct drm_property *property,
391                       uint64_t val)
392 {
393         struct intel_hdmi *intel_hdmi = intel_attached_hdmi(connector);
394         struct drm_i915_private *dev_priv = connector->dev->dev_private;
395         int ret;
396
397         ret = drm_connector_property_set_value(connector, property, val);
398         if (ret)
399                 return ret;
400
401         if (property == dev_priv->force_audio_property) {
402                 int i = val;
403                 bool has_audio;
404
405                 if (i == intel_hdmi->force_audio)
406                         return 0;
407
408                 intel_hdmi->force_audio = i;
409
410                 if (i == 0)
411                         has_audio = intel_hdmi_detect_audio(connector);
412                 else
413                         has_audio = i > 0;
414
415                 if (has_audio == intel_hdmi->has_audio)
416                         return 0;
417
418                 intel_hdmi->has_audio = has_audio;
419                 goto done;
420         }
421
422         if (property == dev_priv->broadcast_rgb_property) {
423                 if (val == !!intel_hdmi->color_range)
424                         return 0;
425
426                 intel_hdmi->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
427                 goto done;
428         }
429
430         return -EINVAL;
431
432 done:
433         if (intel_hdmi->base.base.crtc) {
434                 struct drm_crtc *crtc = intel_hdmi->base.base.crtc;
435                 drm_crtc_helper_set_mode(crtc, &crtc->mode,
436                                          crtc->x, crtc->y,
437                                          crtc->fb);
438         }
439
440         return 0;
441 }
442
443 static void intel_hdmi_destroy(struct drm_connector *connector)
444 {
445         drm_sysfs_connector_remove(connector);
446         drm_connector_cleanup(connector);
447         kfree(connector);
448 }
449
450 static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
451         .dpms = intel_hdmi_dpms,
452         .mode_fixup = intel_hdmi_mode_fixup,
453         .prepare = intel_encoder_prepare,
454         .mode_set = intel_hdmi_mode_set,
455         .commit = intel_encoder_commit,
456 };
457
458 static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
459         .dpms = drm_helper_connector_dpms,
460         .detect = intel_hdmi_detect,
461         .fill_modes = drm_helper_probe_single_connector_modes,
462         .set_property = intel_hdmi_set_property,
463         .destroy = intel_hdmi_destroy,
464 };
465
466 static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
467         .get_modes = intel_hdmi_get_modes,
468         .mode_valid = intel_hdmi_mode_valid,
469         .best_encoder = intel_best_encoder,
470 };
471
472 static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
473         .destroy = intel_encoder_destroy,
474 };
475
476 static void
477 intel_hdmi_add_properties(struct intel_hdmi *intel_hdmi, struct drm_connector *connector)
478 {
479         intel_attach_force_audio_property(connector);
480         intel_attach_broadcast_rgb_property(connector);
481 }
482
483 void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
484 {
485         struct drm_i915_private *dev_priv = dev->dev_private;
486         struct drm_connector *connector;
487         struct intel_encoder *intel_encoder;
488         struct intel_connector *intel_connector;
489         struct intel_hdmi *intel_hdmi;
490
491         intel_hdmi = kzalloc(sizeof(struct intel_hdmi), GFP_KERNEL);
492         if (!intel_hdmi)
493                 return;
494
495         intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
496         if (!intel_connector) {
497                 kfree(intel_hdmi);
498                 return;
499         }
500
501         intel_encoder = &intel_hdmi->base;
502         drm_encoder_init(dev, &intel_encoder->base, &intel_hdmi_enc_funcs,
503                          DRM_MODE_ENCODER_TMDS);
504
505         connector = &intel_connector->base;
506         drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
507                            DRM_MODE_CONNECTOR_HDMIA);
508         drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
509
510         intel_encoder->type = INTEL_OUTPUT_HDMI;
511
512         connector->polled = DRM_CONNECTOR_POLL_HPD;
513         connector->interlace_allowed = 0;
514         connector->doublescan_allowed = 0;
515         intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
516
517         /* Set up the DDC bus. */
518         if (sdvox_reg == SDVOB) {
519                 intel_encoder->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
520                 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
521                 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
522         } else if (sdvox_reg == SDVOC) {
523                 intel_encoder->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
524                 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
525                 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
526         } else if (sdvox_reg == HDMIB) {
527                 intel_encoder->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
528                 intel_hdmi->ddc_bus = GMBUS_PORT_DPB;
529                 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
530         } else if (sdvox_reg == HDMIC) {
531                 intel_encoder->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
532                 intel_hdmi->ddc_bus = GMBUS_PORT_DPC;
533                 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
534         } else if (sdvox_reg == HDMID) {
535                 intel_encoder->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
536                 intel_hdmi->ddc_bus = GMBUS_PORT_DPD;
537                 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
538         }
539
540         intel_hdmi->sdvox_reg = sdvox_reg;
541
542         if (!HAS_PCH_SPLIT(dev))
543                 intel_hdmi->write_infoframe = i9xx_write_infoframe;
544         else
545                 intel_hdmi->write_infoframe = ironlake_write_infoframe;
546
547         drm_encoder_helper_add(&intel_encoder->base, &intel_hdmi_helper_funcs);
548
549         intel_hdmi_add_properties(intel_hdmi, connector);
550
551         intel_connector_attach_encoder(intel_connector, intel_encoder);
552         drm_sysfs_connector_add(connector);
553
554         /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
555          * 0xd.  Failure to do so will result in spurious interrupts being
556          * generated on the port when a cable is not attached.
557          */
558         if (IS_G4X(dev) && !IS_GM45(dev)) {
559                 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
560                 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
561         }
562 }