Merge branch 'fix/asoc' into for-linus
[linux-2.6] / drivers / gpu / drm / i915 / intel_crt.c
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Eric Anholt <eric@anholt.net>
25  */
26
27 #include <linux/i2c.h>
28 #include "drmP.h"
29 #include "drm.h"
30 #include "drm_crtc.h"
31 #include "drm_crtc_helper.h"
32 #include "intel_drv.h"
33 #include "i915_drm.h"
34 #include "i915_drv.h"
35
36 static void intel_crt_dpms(struct drm_encoder *encoder, int mode)
37 {
38         struct drm_device *dev = encoder->dev;
39         struct drm_i915_private *dev_priv = dev->dev_private;
40         u32 temp;
41
42         temp = I915_READ(ADPA);
43         temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
44         temp |= ADPA_DAC_ENABLE;
45
46         switch(mode) {
47         case DRM_MODE_DPMS_ON:
48                 temp |= ADPA_DAC_ENABLE;
49                 break;
50         case DRM_MODE_DPMS_STANDBY:
51                 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
52                 break;
53         case DRM_MODE_DPMS_SUSPEND:
54                 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
55                 break;
56         case DRM_MODE_DPMS_OFF:
57                 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
58                 break;
59         }
60
61         I915_WRITE(ADPA, temp);
62 }
63
64 static int intel_crt_mode_valid(struct drm_connector *connector,
65                                 struct drm_display_mode *mode)
66 {
67         struct drm_device *dev = connector->dev;
68
69         int max_clock = 0;
70         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
71                 return MODE_NO_DBLESCAN;
72
73         if (mode->clock < 25000)
74                 return MODE_CLOCK_LOW;
75
76         if (!IS_I9XX(dev))
77                 max_clock = 350000;
78         else
79                 max_clock = 400000;
80         if (mode->clock > max_clock)
81                 return MODE_CLOCK_HIGH;
82
83         return MODE_OK;
84 }
85
86 static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
87                                  struct drm_display_mode *mode,
88                                  struct drm_display_mode *adjusted_mode)
89 {
90         return true;
91 }
92
93 static void intel_crt_mode_set(struct drm_encoder *encoder,
94                                struct drm_display_mode *mode,
95                                struct drm_display_mode *adjusted_mode)
96 {
97
98         struct drm_device *dev = encoder->dev;
99         struct drm_crtc *crtc = encoder->crtc;
100         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
101         struct drm_i915_private *dev_priv = dev->dev_private;
102         int dpll_md_reg;
103         u32 adpa, dpll_md;
104
105         if (intel_crtc->pipe == 0)
106                 dpll_md_reg = DPLL_A_MD;
107         else
108                 dpll_md_reg = DPLL_B_MD;
109
110         /*
111          * Disable separate mode multiplier used when cloning SDVO to CRT
112          * XXX this needs to be adjusted when we really are cloning
113          */
114         if (IS_I965G(dev)) {
115                 dpll_md = I915_READ(dpll_md_reg);
116                 I915_WRITE(dpll_md_reg,
117                            dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
118         }
119
120         adpa = 0;
121         if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
122                 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
123         if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
124                 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
125
126         if (intel_crtc->pipe == 0) {
127                 adpa |= ADPA_PIPE_A_SELECT;
128                 I915_WRITE(BCLRPAT_A, 0);
129         } else {
130                 adpa |= ADPA_PIPE_B_SELECT;
131                 I915_WRITE(BCLRPAT_B, 0);
132         }
133
134         I915_WRITE(ADPA, adpa);
135 }
136
137 /**
138  * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
139  *
140  * Not for i915G/i915GM
141  *
142  * \return true if CRT is connected.
143  * \return false if CRT is disconnected.
144  */
145 static bool intel_crt_detect_hotplug(struct drm_connector *connector)
146 {
147         struct drm_device *dev = connector->dev;
148         struct drm_i915_private *dev_priv = dev->dev_private;
149         u32 hotplug_en;
150         int i, tries = 0;
151         /*
152          * On 4 series desktop, CRT detect sequence need to be done twice
153          * to get a reliable result.
154          */
155
156         if (IS_G4X(dev) && !IS_GM45(dev))
157                 tries = 2;
158         else
159                 tries = 1;
160         hotplug_en = I915_READ(PORT_HOTPLUG_EN);
161         hotplug_en &= CRT_FORCE_HOTPLUG_MASK;
162         hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
163
164         if (IS_GM45(dev))
165                 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
166
167         hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
168
169         for (i = 0; i < tries ; i++) {
170                 unsigned long timeout;
171                 /* turn on the FORCE_DETECT */
172                 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
173                 timeout = jiffies + msecs_to_jiffies(1000);
174                 /* wait for FORCE_DETECT to go off */
175                 do {
176                         if (!(I915_READ(PORT_HOTPLUG_EN) &
177                                         CRT_HOTPLUG_FORCE_DETECT))
178                                 break;
179                         msleep(1);
180                 } while (time_after(timeout, jiffies));
181         }
182
183         if ((I915_READ(PORT_HOTPLUG_STAT) & CRT_HOTPLUG_MONITOR_MASK) ==
184             CRT_HOTPLUG_MONITOR_COLOR)
185                 return true;
186
187         return false;
188 }
189
190 static bool intel_crt_detect_ddc(struct drm_connector *connector)
191 {
192         struct intel_output *intel_output = to_intel_output(connector);
193
194         /* CRT should always be at 0, but check anyway */
195         if (intel_output->type != INTEL_OUTPUT_ANALOG)
196                 return false;
197
198         return intel_ddc_probe(intel_output);
199 }
200
201 static enum drm_connector_status intel_crt_detect(struct drm_connector *connector)
202 {
203         struct drm_device *dev = connector->dev;
204
205         if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
206                 if (intel_crt_detect_hotplug(connector))
207                         return connector_status_connected;
208                 else
209                         return connector_status_disconnected;
210         }
211
212         if (intel_crt_detect_ddc(connector))
213                 return connector_status_connected;
214
215         /* TODO use load detect */
216         return connector_status_unknown;
217 }
218
219 static void intel_crt_destroy(struct drm_connector *connector)
220 {
221         struct intel_output *intel_output = to_intel_output(connector);
222
223         intel_i2c_destroy(intel_output->ddc_bus);
224         drm_sysfs_connector_remove(connector);
225         drm_connector_cleanup(connector);
226         kfree(connector);
227 }
228
229 static int intel_crt_get_modes(struct drm_connector *connector)
230 {
231         struct intel_output *intel_output = to_intel_output(connector);
232         return intel_ddc_get_modes(intel_output);
233 }
234
235 static int intel_crt_set_property(struct drm_connector *connector,
236                                   struct drm_property *property,
237                                   uint64_t value)
238 {
239         struct drm_device *dev = connector->dev;
240
241         if (property == dev->mode_config.dpms_property && connector->encoder)
242                 intel_crt_dpms(connector->encoder, (uint32_t)(value & 0xf));
243
244         return 0;
245 }
246
247 /*
248  * Routines for controlling stuff on the analog port
249  */
250
251 static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
252         .dpms = intel_crt_dpms,
253         .mode_fixup = intel_crt_mode_fixup,
254         .prepare = intel_encoder_prepare,
255         .commit = intel_encoder_commit,
256         .mode_set = intel_crt_mode_set,
257 };
258
259 static const struct drm_connector_funcs intel_crt_connector_funcs = {
260         .detect = intel_crt_detect,
261         .fill_modes = drm_helper_probe_single_connector_modes,
262         .destroy = intel_crt_destroy,
263         .set_property = intel_crt_set_property,
264 };
265
266 static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
267         .mode_valid = intel_crt_mode_valid,
268         .get_modes = intel_crt_get_modes,
269         .best_encoder = intel_best_encoder,
270 };
271
272 static void intel_crt_enc_destroy(struct drm_encoder *encoder)
273 {
274         drm_encoder_cleanup(encoder);
275 }
276
277 static const struct drm_encoder_funcs intel_crt_enc_funcs = {
278         .destroy = intel_crt_enc_destroy,
279 };
280
281 void intel_crt_init(struct drm_device *dev)
282 {
283         struct drm_connector *connector;
284         struct intel_output *intel_output;
285
286         intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
287         if (!intel_output)
288                 return;
289
290         connector = &intel_output->base;
291         drm_connector_init(dev, &intel_output->base,
292                            &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
293
294         drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
295                          DRM_MODE_ENCODER_DAC);
296
297         drm_mode_connector_attach_encoder(&intel_output->base,
298                                           &intel_output->enc);
299
300         /* Set up the DDC bus. */
301         intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
302         if (!intel_output->ddc_bus) {
303                 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
304                            "failed.\n");
305                 return;
306         }
307
308         intel_output->type = INTEL_OUTPUT_ANALOG;
309         connector->interlace_allowed = 0;
310         connector->doublescan_allowed = 0;
311
312         drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
313         drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
314
315         drm_sysfs_connector_add(connector);
316 }