2 * Copyright © 2006-2007 Intel Corporation
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:
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
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.
24 * Eric Anholt <eric@anholt.net>
27 #include <linux/i2c.h>
31 #include "drm_crtc_helper.h"
32 #include "intel_drv.h"
36 static void intel_crt_dpms(struct drm_encoder *encoder, int mode)
38 struct drm_device *dev = encoder->dev;
39 struct drm_i915_private *dev_priv = dev->dev_private;
42 temp = I915_READ(ADPA);
43 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
44 temp &= ~ADPA_DAC_ENABLE;
47 case DRM_MODE_DPMS_ON:
48 temp |= ADPA_DAC_ENABLE;
50 case DRM_MODE_DPMS_STANDBY:
51 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
53 case DRM_MODE_DPMS_SUSPEND:
54 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
56 case DRM_MODE_DPMS_OFF:
57 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
61 I915_WRITE(ADPA, temp);
64 static void intel_crt_save(struct drm_connector *connector)
69 static void intel_crt_restore(struct drm_connector *connector)
74 static int intel_crt_mode_valid(struct drm_connector *connector,
75 struct drm_display_mode *mode)
77 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
78 return MODE_NO_DBLESCAN;
80 if (mode->clock > 400000 || mode->clock < 25000)
81 return MODE_CLOCK_RANGE;
86 static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
87 struct drm_display_mode *mode,
88 struct drm_display_mode *adjusted_mode)
93 static void intel_crt_mode_set(struct drm_encoder *encoder,
94 struct drm_display_mode *mode,
95 struct drm_display_mode *adjusted_mode)
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;
105 if (intel_crtc->pipe == 0)
106 dpll_md_reg = DPLL_A_MD;
108 dpll_md_reg = DPLL_B_MD;
111 * Disable separate mode multiplier used when cloning SDVO to CRT
112 * XXX this needs to be adjusted when we really are cloning
115 dpll_md = I915_READ(dpll_md_reg);
116 I915_WRITE(dpll_md_reg,
117 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
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;
126 if (intel_crtc->pipe == 0)
127 adpa |= ADPA_PIPE_A_SELECT;
129 adpa |= ADPA_PIPE_B_SELECT;
131 I915_WRITE(ADPA, adpa);
135 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
137 * Not for i915G/i915GM
139 * \return true if CRT is connected.
140 * \return false if CRT is disconnected.
142 static bool intel_crt_detect_hotplug(struct drm_connector *connector)
144 struct drm_device *dev = connector->dev;
145 struct drm_i915_private *dev_priv = dev->dev_private;
148 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
150 temp = I915_READ(PORT_HOTPLUG_EN);
152 I915_WRITE(PORT_HOTPLUG_EN,
153 temp | CRT_HOTPLUG_FORCE_DETECT | (1 << 5));
156 if (!(I915_READ(PORT_HOTPLUG_EN) & CRT_HOTPLUG_FORCE_DETECT))
159 } while (time_after(timeout, jiffies));
161 if ((I915_READ(PORT_HOTPLUG_STAT) & CRT_HOTPLUG_MONITOR_MASK) ==
162 CRT_HOTPLUG_MONITOR_COLOR)
168 static bool intel_crt_detect_ddc(struct drm_connector *connector)
170 struct intel_output *intel_output = to_intel_output(connector);
172 /* CRT should always be at 0, but check anyway */
173 if (intel_output->type != INTEL_OUTPUT_ANALOG)
176 return intel_ddc_probe(intel_output);
179 static enum drm_connector_status intel_crt_detect(struct drm_connector *connector)
181 struct drm_device *dev = connector->dev;
183 if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
184 if (intel_crt_detect_hotplug(connector))
185 return connector_status_connected;
187 return connector_status_disconnected;
190 if (intel_crt_detect_ddc(connector))
191 return connector_status_connected;
193 /* TODO use load detect */
194 return connector_status_unknown;
197 static void intel_crt_destroy(struct drm_connector *connector)
199 struct intel_output *intel_output = to_intel_output(connector);
201 intel_i2c_destroy(intel_output->ddc_bus);
202 drm_sysfs_connector_remove(connector);
203 drm_connector_cleanup(connector);
207 static int intel_crt_get_modes(struct drm_connector *connector)
209 struct intel_output *intel_output = to_intel_output(connector);
210 return intel_ddc_get_modes(intel_output);
213 static int intel_crt_set_property(struct drm_connector *connector,
214 struct drm_property *property,
217 struct drm_device *dev = connector->dev;
219 if (property == dev->mode_config.dpms_property && connector->encoder)
220 intel_crt_dpms(connector->encoder, (uint32_t)(value & 0xf));
226 * Routines for controlling stuff on the analog port
229 static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
230 .dpms = intel_crt_dpms,
231 .mode_fixup = intel_crt_mode_fixup,
232 .prepare = intel_encoder_prepare,
233 .commit = intel_encoder_commit,
234 .mode_set = intel_crt_mode_set,
237 static const struct drm_connector_funcs intel_crt_connector_funcs = {
238 .save = intel_crt_save,
239 .restore = intel_crt_restore,
240 .detect = intel_crt_detect,
241 .fill_modes = drm_helper_probe_single_connector_modes,
242 .destroy = intel_crt_destroy,
243 .set_property = intel_crt_set_property,
246 static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
247 .mode_valid = intel_crt_mode_valid,
248 .get_modes = intel_crt_get_modes,
249 .best_encoder = intel_best_encoder,
252 void intel_crt_enc_destroy(struct drm_encoder *encoder)
254 drm_encoder_cleanup(encoder);
257 static const struct drm_encoder_funcs intel_crt_enc_funcs = {
258 .destroy = intel_crt_enc_destroy,
261 void intel_crt_init(struct drm_device *dev)
263 struct drm_connector *connector;
264 struct intel_output *intel_output;
266 intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
270 connector = &intel_output->base;
271 drm_connector_init(dev, &intel_output->base,
272 &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
274 drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
275 DRM_MODE_ENCODER_DAC);
277 drm_mode_connector_attach_encoder(&intel_output->base,
280 /* Set up the DDC bus. */
281 intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
282 if (!intel_output->ddc_bus) {
283 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
288 intel_output->type = INTEL_OUTPUT_ANALOG;
289 connector->interlace_allowed = 0;
290 connector->doublescan_allowed = 0;
292 drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
293 drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
295 drm_sysfs_connector_add(connector);