do_add_mount() should sanitize mnt_flags
[sfrench/cifs-2.6.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/delay.h>
31 #include "drmP.h"
32 #include "drm.h"
33 #include "drm_crtc.h"
34 #include "drm_edid.h"
35 #include "intel_drv.h"
36 #include "i915_drm.h"
37 #include "i915_drv.h"
38
39 struct intel_hdmi_priv {
40         u32 sdvox_reg;
41         u32 save_SDVOX;
42         bool has_hdmi_sink;
43 };
44
45 static void intel_hdmi_mode_set(struct drm_encoder *encoder,
46                                 struct drm_display_mode *mode,
47                                 struct drm_display_mode *adjusted_mode)
48 {
49         struct drm_device *dev = encoder->dev;
50         struct drm_i915_private *dev_priv = dev->dev_private;
51         struct drm_crtc *crtc = encoder->crtc;
52         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
53         struct intel_output *intel_output = enc_to_intel_output(encoder);
54         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
55         u32 sdvox;
56
57         sdvox = SDVO_ENCODING_HDMI |
58                 SDVO_BORDER_ENABLE |
59                 SDVO_VSYNC_ACTIVE_HIGH |
60                 SDVO_HSYNC_ACTIVE_HIGH;
61
62         if (hdmi_priv->has_hdmi_sink)
63                 sdvox |= SDVO_AUDIO_ENABLE;
64
65         if (intel_crtc->pipe == 1)
66                 sdvox |= SDVO_PIPE_B_SELECT;
67
68         I915_WRITE(hdmi_priv->sdvox_reg, sdvox);
69         POSTING_READ(hdmi_priv->sdvox_reg);
70 }
71
72 static void intel_hdmi_dpms(struct drm_encoder *encoder, int mode)
73 {
74         struct drm_device *dev = encoder->dev;
75         struct drm_i915_private *dev_priv = dev->dev_private;
76         struct intel_output *intel_output = enc_to_intel_output(encoder);
77         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
78         u32 temp;
79
80         temp = I915_READ(hdmi_priv->sdvox_reg);
81
82         /* HW workaround, need to toggle enable bit off and on for 12bpc, but
83          * we do this anyway which shows more stable in testing.
84          */
85         if (IS_IRONLAKE(dev)) {
86                 I915_WRITE(hdmi_priv->sdvox_reg, temp & ~SDVO_ENABLE);
87                 POSTING_READ(hdmi_priv->sdvox_reg);
88         }
89
90         if (mode != DRM_MODE_DPMS_ON) {
91                 temp &= ~SDVO_ENABLE;
92         } else {
93                 temp |= SDVO_ENABLE;
94         }
95
96         I915_WRITE(hdmi_priv->sdvox_reg, temp);
97         POSTING_READ(hdmi_priv->sdvox_reg);
98
99         /* HW workaround, need to write this twice for issue that may result
100          * in first write getting masked.
101          */
102         if (IS_IRONLAKE(dev)) {
103                 I915_WRITE(hdmi_priv->sdvox_reg, temp);
104                 POSTING_READ(hdmi_priv->sdvox_reg);
105         }
106 }
107
108 static void intel_hdmi_save(struct drm_connector *connector)
109 {
110         struct drm_device *dev = connector->dev;
111         struct drm_i915_private *dev_priv = dev->dev_private;
112         struct intel_output *intel_output = to_intel_output(connector);
113         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
114
115         hdmi_priv->save_SDVOX = I915_READ(hdmi_priv->sdvox_reg);
116 }
117
118 static void intel_hdmi_restore(struct drm_connector *connector)
119 {
120         struct drm_device *dev = connector->dev;
121         struct drm_i915_private *dev_priv = dev->dev_private;
122         struct intel_output *intel_output = to_intel_output(connector);
123         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
124
125         I915_WRITE(hdmi_priv->sdvox_reg, hdmi_priv->save_SDVOX);
126         POSTING_READ(hdmi_priv->sdvox_reg);
127 }
128
129 static int intel_hdmi_mode_valid(struct drm_connector *connector,
130                                  struct drm_display_mode *mode)
131 {
132         if (mode->clock > 165000)
133                 return MODE_CLOCK_HIGH;
134         if (mode->clock < 20000)
135                 return MODE_CLOCK_HIGH;
136
137         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
138                 return MODE_NO_DBLESCAN;
139
140         return MODE_OK;
141 }
142
143 static bool intel_hdmi_mode_fixup(struct drm_encoder *encoder,
144                                   struct drm_display_mode *mode,
145                                   struct drm_display_mode *adjusted_mode)
146 {
147         return true;
148 }
149
150 static enum drm_connector_status
151 intel_hdmi_detect(struct drm_connector *connector)
152 {
153         struct intel_output *intel_output = to_intel_output(connector);
154         struct intel_hdmi_priv *hdmi_priv = intel_output->dev_priv;
155         struct edid *edid = NULL;
156         enum drm_connector_status status = connector_status_disconnected;
157
158         hdmi_priv->has_hdmi_sink = false;
159         edid = drm_get_edid(&intel_output->base,
160                             intel_output->ddc_bus);
161
162         if (edid) {
163                 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
164                         status = connector_status_connected;
165                         hdmi_priv->has_hdmi_sink = drm_detect_hdmi_monitor(edid);
166                 }
167                 intel_output->base.display_info.raw_edid = NULL;
168                 kfree(edid);
169         }
170
171         return status;
172 }
173
174 static int intel_hdmi_get_modes(struct drm_connector *connector)
175 {
176         struct intel_output *intel_output = to_intel_output(connector);
177
178         /* We should parse the EDID data and find out if it's an HDMI sink so
179          * we can send audio to it.
180          */
181
182         return intel_ddc_get_modes(intel_output);
183 }
184
185 static void intel_hdmi_destroy(struct drm_connector *connector)
186 {
187         struct intel_output *intel_output = to_intel_output(connector);
188
189         if (intel_output->i2c_bus)
190                 intel_i2c_destroy(intel_output->i2c_bus);
191         drm_sysfs_connector_remove(connector);
192         drm_connector_cleanup(connector);
193         kfree(intel_output);
194 }
195
196 static const struct drm_encoder_helper_funcs intel_hdmi_helper_funcs = {
197         .dpms = intel_hdmi_dpms,
198         .mode_fixup = intel_hdmi_mode_fixup,
199         .prepare = intel_encoder_prepare,
200         .mode_set = intel_hdmi_mode_set,
201         .commit = intel_encoder_commit,
202 };
203
204 static const struct drm_connector_funcs intel_hdmi_connector_funcs = {
205         .dpms = drm_helper_connector_dpms,
206         .save = intel_hdmi_save,
207         .restore = intel_hdmi_restore,
208         .detect = intel_hdmi_detect,
209         .fill_modes = drm_helper_probe_single_connector_modes,
210         .destroy = intel_hdmi_destroy,
211 };
212
213 static const struct drm_connector_helper_funcs intel_hdmi_connector_helper_funcs = {
214         .get_modes = intel_hdmi_get_modes,
215         .mode_valid = intel_hdmi_mode_valid,
216         .best_encoder = intel_best_encoder,
217 };
218
219 static void intel_hdmi_enc_destroy(struct drm_encoder *encoder)
220 {
221         drm_encoder_cleanup(encoder);
222 }
223
224 static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
225         .destroy = intel_hdmi_enc_destroy,
226 };
227
228 /*
229  * Enumerate the child dev array parsed from VBT to check whether
230  * the given HDMI is present.
231  * If it is present, return 1.
232  * If it is not present, return false.
233  * If no child dev is parsed from VBT, it assumes that the given
234  * HDMI is present.
235  */
236 static int hdmi_is_present_in_vbt(struct drm_device *dev, int hdmi_reg)
237 {
238         struct drm_i915_private *dev_priv = dev->dev_private;
239         struct child_device_config *p_child;
240         int i, hdmi_port, ret;
241
242         if (!dev_priv->child_dev_num)
243                 return 1;
244
245         if (hdmi_reg == SDVOB)
246                 hdmi_port = DVO_B;
247         else if (hdmi_reg == SDVOC)
248                 hdmi_port = DVO_C;
249         else if (hdmi_reg == HDMIB)
250                 hdmi_port = DVO_B;
251         else if (hdmi_reg == HDMIC)
252                 hdmi_port = DVO_C;
253         else if (hdmi_reg == HDMID)
254                 hdmi_port = DVO_D;
255         else
256                 return 0;
257
258         ret = 0;
259         for (i = 0; i < dev_priv->child_dev_num; i++) {
260                 p_child = dev_priv->child_dev + i;
261                 /*
262                  * If the device type is not HDMI, continue.
263                  */
264                 if (p_child->device_type != DEVICE_TYPE_HDMI)
265                         continue;
266                 /* Find the HDMI port */
267                 if (p_child->dvo_port == hdmi_port) {
268                         ret = 1;
269                         break;
270                 }
271         }
272         return ret;
273 }
274 void intel_hdmi_init(struct drm_device *dev, int sdvox_reg)
275 {
276         struct drm_i915_private *dev_priv = dev->dev_private;
277         struct drm_connector *connector;
278         struct intel_output *intel_output;
279         struct intel_hdmi_priv *hdmi_priv;
280
281         if (!hdmi_is_present_in_vbt(dev, sdvox_reg)) {
282                 DRM_DEBUG_KMS("HDMI is not present. Ignored it \n");
283                 return;
284         }
285         intel_output = kcalloc(sizeof(struct intel_output) +
286                                sizeof(struct intel_hdmi_priv), 1, GFP_KERNEL);
287         if (!intel_output)
288                 return;
289         hdmi_priv = (struct intel_hdmi_priv *)(intel_output + 1);
290
291         connector = &intel_output->base;
292         drm_connector_init(dev, connector, &intel_hdmi_connector_funcs,
293                            DRM_MODE_CONNECTOR_HDMIA);
294         drm_connector_helper_add(connector, &intel_hdmi_connector_helper_funcs);
295
296         intel_output->type = INTEL_OUTPUT_HDMI;
297
298         connector->interlace_allowed = 0;
299         connector->doublescan_allowed = 0;
300         intel_output->crtc_mask = (1 << 0) | (1 << 1);
301
302         /* Set up the DDC bus. */
303         if (sdvox_reg == SDVOB) {
304                 intel_output->clone_mask = (1 << INTEL_HDMIB_CLONE_BIT);
305                 intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "HDMIB");
306                 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
307         } else if (sdvox_reg == SDVOC) {
308                 intel_output->clone_mask = (1 << INTEL_HDMIC_CLONE_BIT);
309                 intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "HDMIC");
310                 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
311         } else if (sdvox_reg == HDMIB) {
312                 intel_output->clone_mask = (1 << INTEL_HDMID_CLONE_BIT);
313                 intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOE,
314                                                                 "HDMIB");
315                 dev_priv->hotplug_supported_mask |= HDMIB_HOTPLUG_INT_STATUS;
316         } else if (sdvox_reg == HDMIC) {
317                 intel_output->clone_mask = (1 << INTEL_HDMIE_CLONE_BIT);
318                 intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOD,
319                                                                 "HDMIC");
320                 dev_priv->hotplug_supported_mask |= HDMIC_HOTPLUG_INT_STATUS;
321         } else if (sdvox_reg == HDMID) {
322                 intel_output->clone_mask = (1 << INTEL_HDMIF_CLONE_BIT);
323                 intel_output->ddc_bus = intel_i2c_create(dev, PCH_GPIOF,
324                                                                 "HDMID");
325                 dev_priv->hotplug_supported_mask |= HDMID_HOTPLUG_INT_STATUS;
326         }
327         if (!intel_output->ddc_bus)
328                 goto err_connector;
329
330         hdmi_priv->sdvox_reg = sdvox_reg;
331         intel_output->dev_priv = hdmi_priv;
332
333         drm_encoder_init(dev, &intel_output->enc, &intel_hdmi_enc_funcs,
334                          DRM_MODE_ENCODER_TMDS);
335         drm_encoder_helper_add(&intel_output->enc, &intel_hdmi_helper_funcs);
336
337         drm_mode_connector_attach_encoder(&intel_output->base,
338                                           &intel_output->enc);
339         drm_sysfs_connector_add(connector);
340
341         /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
342          * 0xd.  Failure to do so will result in spurious interrupts being
343          * generated on the port when a cable is not attached.
344          */
345         if (IS_G4X(dev) && !IS_GM45(dev)) {
346                 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
347                 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
348         }
349
350         return;
351
352 err_connector:
353         drm_connector_cleanup(connector);
354         kfree(intel_output);
355
356         return;
357 }