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>
30 #include "intel_bios.h"
32 #include "psb_intel_drv.h"
33 #include "psb_intel_reg.h"
35 #include "cdv_device.h"
36 #include <linux/pm_runtime.h>
39 static void cdv_intel_crt_dpms(struct drm_encoder
*encoder
, int mode
)
41 struct drm_device
*dev
= encoder
->dev
;
46 temp
&= ~(ADPA_HSYNC_CNTL_DISABLE
| ADPA_VSYNC_CNTL_DISABLE
);
47 temp
&= ~ADPA_DAC_ENABLE
;
50 case DRM_MODE_DPMS_ON
:
51 temp
|= ADPA_DAC_ENABLE
;
53 case DRM_MODE_DPMS_STANDBY
:
54 temp
|= ADPA_DAC_ENABLE
| ADPA_HSYNC_CNTL_DISABLE
;
56 case DRM_MODE_DPMS_SUSPEND
:
57 temp
|= ADPA_DAC_ENABLE
| ADPA_VSYNC_CNTL_DISABLE
;
59 case DRM_MODE_DPMS_OFF
:
60 temp
|= ADPA_HSYNC_CNTL_DISABLE
| ADPA_VSYNC_CNTL_DISABLE
;
67 static int cdv_intel_crt_mode_valid(struct drm_connector
*connector
,
68 struct drm_display_mode
*mode
)
70 if (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
71 return MODE_NO_DBLESCAN
;
73 /* The lowest clock for CDV is 20000KHz */
74 if (mode
->clock
< 20000)
75 return MODE_CLOCK_LOW
;
77 /* The max clock for CDV is 355 instead of 400 */
78 if (mode
->clock
> 355000)
79 return MODE_CLOCK_HIGH
;
84 static bool cdv_intel_crt_mode_fixup(struct drm_encoder
*encoder
,
85 struct drm_display_mode
*mode
,
86 struct drm_display_mode
*adjusted_mode
)
91 static void cdv_intel_crt_mode_set(struct drm_encoder
*encoder
,
92 struct drm_display_mode
*mode
,
93 struct drm_display_mode
*adjusted_mode
)
96 struct drm_device
*dev
= encoder
->dev
;
97 struct drm_crtc
*crtc
= encoder
->crtc
;
98 struct psb_intel_crtc
*psb_intel_crtc
=
99 to_psb_intel_crtc(crtc
);
104 if (psb_intel_crtc
->pipe
== 0)
105 dpll_md_reg
= DPLL_A_MD
;
107 dpll_md_reg
= DPLL_B_MD
;
112 * Disable separate mode multiplier used when cloning SDVO to CRT
113 * XXX this needs to be adjusted when we really are cloning
116 dpll_md
= REG_READ(dpll_md_reg
);
117 REG_WRITE(dpll_md_reg
,
118 dpll_md
& ~DPLL_MD_UDI_MULTIPLIER_MASK
);
122 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
123 adpa
|= ADPA_HSYNC_ACTIVE_HIGH
;
124 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
125 adpa
|= ADPA_VSYNC_ACTIVE_HIGH
;
127 if (psb_intel_crtc
->pipe
== 0)
128 adpa
|= ADPA_PIPE_A_SELECT
;
130 adpa
|= ADPA_PIPE_B_SELECT
;
132 REG_WRITE(adpa_reg
, adpa
);
137 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
139 * \return true if CRT is connected.
140 * \return false if CRT is disconnected.
142 static bool cdv_intel_crt_detect_hotplug(struct drm_connector
*connector
,
145 struct drm_device
*dev
= connector
->dev
;
147 int i
, tries
= 0, ret
= false;
151 * On a CDV thep, CRT detect sequence need to be done twice
152 * to get a reliable result.
156 orig
= hotplug_en
= REG_READ(PORT_HOTPLUG_EN
);
157 hotplug_en
&= ~(CRT_HOTPLUG_DETECT_MASK
);
158 hotplug_en
|= CRT_HOTPLUG_FORCE_DETECT
;
160 hotplug_en
|= CRT_HOTPLUG_ACTIVATION_PERIOD_64
;
161 hotplug_en
|= CRT_HOTPLUG_VOLTAGE_COMPARE_50
;
163 for (i
= 0; i
< tries
; i
++) {
164 unsigned long timeout
;
165 /* turn on the FORCE_DETECT */
166 REG_WRITE(PORT_HOTPLUG_EN
, hotplug_en
);
167 timeout
= jiffies
+ msecs_to_jiffies(1000);
168 /* wait for FORCE_DETECT to go off */
170 if (!(REG_READ(PORT_HOTPLUG_EN
) &
171 CRT_HOTPLUG_FORCE_DETECT
))
174 } while (time_after(timeout
, jiffies
));
177 if ((REG_READ(PORT_HOTPLUG_STAT
) & CRT_HOTPLUG_MONITOR_MASK
) !=
178 CRT_HOTPLUG_MONITOR_NONE
)
181 /* clear the interrupt we just generated, if any */
182 REG_WRITE(PORT_HOTPLUG_STAT
, CRT_HOTPLUG_INT_STATUS
);
184 /* and put the bits back */
185 REG_WRITE(PORT_HOTPLUG_EN
, orig
);
189 static enum drm_connector_status
cdv_intel_crt_detect(
190 struct drm_connector
*connector
, bool force
)
192 if (cdv_intel_crt_detect_hotplug(connector
, force
))
193 return connector_status_connected
;
195 return connector_status_disconnected
;
198 static void cdv_intel_crt_destroy(struct drm_connector
*connector
)
200 struct psb_intel_encoder
*psb_intel_encoder
=
201 psb_intel_attached_encoder(connector
);
203 psb_intel_i2c_destroy(psb_intel_encoder
->ddc_bus
);
204 drm_sysfs_connector_remove(connector
);
205 drm_connector_cleanup(connector
);
209 static int cdv_intel_crt_get_modes(struct drm_connector
*connector
)
211 struct psb_intel_encoder
*psb_intel_encoder
=
212 psb_intel_attached_encoder(connector
);
213 return psb_intel_ddc_get_modes(connector
, &psb_intel_encoder
->ddc_bus
->adapter
);
216 static int cdv_intel_crt_set_property(struct drm_connector
*connector
,
217 struct drm_property
*property
,
224 * Routines for controlling stuff on the analog port
227 static const struct drm_encoder_helper_funcs cdv_intel_crt_helper_funcs
= {
228 .dpms
= cdv_intel_crt_dpms
,
229 .mode_fixup
= cdv_intel_crt_mode_fixup
,
230 .prepare
= psb_intel_encoder_prepare
,
231 .commit
= psb_intel_encoder_commit
,
232 .mode_set
= cdv_intel_crt_mode_set
,
235 static const struct drm_connector_funcs cdv_intel_crt_connector_funcs
= {
236 .dpms
= drm_helper_connector_dpms
,
237 .detect
= cdv_intel_crt_detect
,
238 .fill_modes
= drm_helper_probe_single_connector_modes
,
239 .destroy
= cdv_intel_crt_destroy
,
240 .set_property
= cdv_intel_crt_set_property
,
243 static const struct drm_connector_helper_funcs
244 cdv_intel_crt_connector_helper_funcs
= {
245 .mode_valid
= cdv_intel_crt_mode_valid
,
246 .get_modes
= cdv_intel_crt_get_modes
,
247 .best_encoder
= psb_intel_best_encoder
,
250 static void cdv_intel_crt_enc_destroy(struct drm_encoder
*encoder
)
252 drm_encoder_cleanup(encoder
);
255 static const struct drm_encoder_funcs cdv_intel_crt_enc_funcs
= {
256 .destroy
= cdv_intel_crt_enc_destroy
,
259 void cdv_intel_crt_init(struct drm_device
*dev
,
260 struct psb_intel_mode_device
*mode_dev
)
263 struct psb_intel_connector
*psb_intel_connector
;
264 struct psb_intel_encoder
*psb_intel_encoder
;
265 struct drm_connector
*connector
;
266 struct drm_encoder
*encoder
;
270 psb_intel_encoder
= kzalloc(sizeof(struct psb_intel_encoder
), GFP_KERNEL
);
271 if (!psb_intel_encoder
)
274 psb_intel_connector
= kzalloc(sizeof(struct psb_intel_connector
), GFP_KERNEL
);
275 if (!psb_intel_connector
)
276 goto failed_connector
;
278 connector
= &psb_intel_connector
->base
;
279 drm_connector_init(dev
, connector
,
280 &cdv_intel_crt_connector_funcs
, DRM_MODE_CONNECTOR_VGA
);
282 encoder
= &psb_intel_encoder
->base
;
283 drm_encoder_init(dev
, encoder
,
284 &cdv_intel_crt_enc_funcs
, DRM_MODE_ENCODER_DAC
);
286 psb_intel_connector_attach_encoder(psb_intel_connector
,
289 /* Set up the DDC bus. */
291 /* Remove the following code for CDV */
293 if (dev_priv->crt_ddc_bus != 0)
294 i2c_reg = dev_priv->crt_ddc_bus;
296 psb_intel_encoder
->ddc_bus
= psb_intel_i2c_create(dev
,
297 i2c_reg
, "CRTDDC_A");
298 if (!psb_intel_encoder
->ddc_bus
) {
299 dev_printk(KERN_ERR
, &dev
->pdev
->dev
, "DDC bus registration "
304 psb_intel_encoder
->type
= INTEL_OUTPUT_ANALOG
;
306 psb_intel_output->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT);
307 psb_intel_output->crtc_mask = (1 << 0) | (1 << 1);
309 connector
->interlace_allowed
= 0;
310 connector
->doublescan_allowed
= 0;
312 drm_encoder_helper_add(encoder
, &cdv_intel_crt_helper_funcs
);
313 drm_connector_helper_add(connector
,
314 &cdv_intel_crt_connector_helper_funcs
);
316 drm_sysfs_connector_add(connector
);
320 drm_encoder_cleanup(&psb_intel_encoder
->base
);
321 drm_connector_cleanup(&psb_intel_connector
->base
);
322 kfree(psb_intel_connector
);
324 kfree(psb_intel_encoder
);