2 * Copyright © 2006-2011 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 * jim liu <jim.liu@intel.com>
27 * We should probably make this generic and share it with Medfield
32 #include <drm/drm_crtc.h>
33 #include <drm/drm_edid.h>
34 #include "psb_intel_drv.h"
36 #include "psb_intel_reg.h"
37 #include <linux/pm_runtime.h>
39 /* hdmi control bits */
40 #define HDMI_NULL_PACKETS_DURING_VSYNC (1 << 9)
41 #define HDMI_BORDER_ENABLE (1 << 7)
42 #define HDMI_AUDIO_ENABLE (1 << 6)
43 #define HDMI_VSYNC_ACTIVE_HIGH (1 << 4)
44 #define HDMI_HSYNC_ACTIVE_HIGH (1 << 3)
45 /* hdmi-b control bits */
46 #define HDMIB_PIPE_B_SELECT (1 << 30)
49 struct mid_intel_hdmi_priv
{
54 /* Should set this when detect hotplug */
55 bool hdmi_device_connected
;
56 struct mdfld_hdmi_i2c
*i2c_bus
;
57 struct i2c_adapter
*hdmi_i2c_adapter
; /* for control functions */
58 struct drm_device
*dev
;
61 static void cdv_hdmi_mode_set(struct drm_encoder
*encoder
,
62 struct drm_display_mode
*mode
,
63 struct drm_display_mode
*adjusted_mode
)
65 struct drm_device
*dev
= encoder
->dev
;
66 struct psb_intel_encoder
*psb_intel_encoder
= to_psb_intel_encoder(encoder
);
67 struct mid_intel_hdmi_priv
*hdmi_priv
= psb_intel_encoder
->dev_priv
;
69 struct drm_crtc
*crtc
= encoder
->crtc
;
70 struct psb_intel_crtc
*intel_crtc
= to_psb_intel_crtc(crtc
);
74 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
75 hdmib
|= HDMI_VSYNC_ACTIVE_HIGH
;
76 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
77 hdmib
|= HDMI_HSYNC_ACTIVE_HIGH
;
79 if (intel_crtc
->pipe
== 1)
80 hdmib
|= HDMIB_PIPE_B_SELECT
;
82 if (hdmi_priv
->has_hdmi_audio
) {
83 hdmib
|= HDMI_AUDIO_ENABLE
;
84 hdmib
|= HDMI_NULL_PACKETS_DURING_VSYNC
;
87 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmib
);
88 REG_READ(hdmi_priv
->hdmi_reg
);
91 static bool cdv_hdmi_mode_fixup(struct drm_encoder
*encoder
,
92 struct drm_display_mode
*mode
,
93 struct drm_display_mode
*adjusted_mode
)
98 static void cdv_hdmi_dpms(struct drm_encoder
*encoder
, int mode
)
100 struct drm_device
*dev
= encoder
->dev
;
101 struct psb_intel_encoder
*psb_intel_encoder
=
102 to_psb_intel_encoder(encoder
);
103 struct mid_intel_hdmi_priv
*hdmi_priv
= psb_intel_encoder
->dev_priv
;
106 hdmib
= REG_READ(hdmi_priv
->hdmi_reg
);
108 if (mode
!= DRM_MODE_DPMS_ON
)
109 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmib
& ~HDMIB_PORT_EN
);
111 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmib
| HDMIB_PORT_EN
);
112 REG_READ(hdmi_priv
->hdmi_reg
);
115 static void cdv_hdmi_save(struct drm_connector
*connector
)
117 struct drm_device
*dev
= connector
->dev
;
118 struct psb_intel_encoder
*psb_intel_encoder
=
119 psb_intel_attached_encoder(connector
);
120 struct mid_intel_hdmi_priv
*hdmi_priv
= psb_intel_encoder
->dev_priv
;
122 hdmi_priv
->save_HDMIB
= REG_READ(hdmi_priv
->hdmi_reg
);
125 static void cdv_hdmi_restore(struct drm_connector
*connector
)
127 struct drm_device
*dev
= connector
->dev
;
128 struct psb_intel_encoder
*psb_intel_encoder
=
129 psb_intel_attached_encoder(connector
);
130 struct mid_intel_hdmi_priv
*hdmi_priv
= psb_intel_encoder
->dev_priv
;
132 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmi_priv
->save_HDMIB
);
133 REG_READ(hdmi_priv
->hdmi_reg
);
136 static enum drm_connector_status
cdv_hdmi_detect(
137 struct drm_connector
*connector
, bool force
)
139 struct psb_intel_encoder
*psb_intel_encoder
=
140 psb_intel_attached_encoder(connector
);
141 struct psb_intel_connector
*psb_intel_connector
=
142 to_psb_intel_connector(connector
);
143 struct mid_intel_hdmi_priv
*hdmi_priv
= psb_intel_encoder
->dev_priv
;
144 struct edid
*edid
= NULL
;
145 enum drm_connector_status status
= connector_status_disconnected
;
147 edid
= drm_get_edid(connector
, &psb_intel_encoder
->i2c_bus
->adapter
);
149 hdmi_priv
->has_hdmi_sink
= false;
150 hdmi_priv
->has_hdmi_audio
= false;
152 if (edid
->input
& DRM_EDID_INPUT_DIGITAL
) {
153 status
= connector_status_connected
;
154 hdmi_priv
->has_hdmi_sink
=
155 drm_detect_hdmi_monitor(edid
);
156 hdmi_priv
->has_hdmi_audio
=
157 drm_detect_monitor_audio(edid
);
160 psb_intel_connector
->base
.display_info
.raw_edid
= NULL
;
166 static int cdv_hdmi_set_property(struct drm_connector
*connector
,
167 struct drm_property
*property
,
170 struct drm_encoder
*encoder
= connector
->encoder
;
172 if (!strcmp(property
->name
, "scaling mode") && encoder
) {
173 struct psb_intel_crtc
*crtc
= to_psb_intel_crtc(encoder
->crtc
);
181 case DRM_MODE_SCALE_FULLSCREEN
:
183 case DRM_MODE_SCALE_NO_SCALE
:
185 case DRM_MODE_SCALE_ASPECT
:
191 if (drm_connector_property_get_value(connector
,
192 property
, &curValue
))
195 if (curValue
== value
)
198 if (drm_connector_property_set_value(connector
,
202 centre
= (curValue
== DRM_MODE_SCALE_NO_SCALE
) ||
203 (value
== DRM_MODE_SCALE_NO_SCALE
);
205 if (crtc
->saved_mode
.hdisplay
!= 0 &&
206 crtc
->saved_mode
.vdisplay
!= 0) {
208 if (!drm_crtc_helper_set_mode(encoder
->crtc
, &crtc
->saved_mode
,
209 encoder
->crtc
->x
, encoder
->crtc
->y
, encoder
->crtc
->fb
))
212 struct drm_encoder_helper_funcs
*helpers
213 = encoder
->helper_private
;
214 helpers
->mode_set(encoder
, &crtc
->saved_mode
,
215 &crtc
->saved_adjusted_mode
);
223 * Return the list of HDMI DDC modes if available.
225 static int cdv_hdmi_get_modes(struct drm_connector
*connector
)
227 struct psb_intel_encoder
*psb_intel_encoder
=
228 psb_intel_attached_encoder(connector
);
229 struct edid
*edid
= NULL
;
232 edid
= drm_get_edid(connector
, &psb_intel_encoder
->i2c_bus
->adapter
);
234 drm_mode_connector_update_edid_property(connector
, edid
);
235 ret
= drm_add_edid_modes(connector
, edid
);
241 static int cdv_hdmi_mode_valid(struct drm_connector
*connector
,
242 struct drm_display_mode
*mode
)
244 struct drm_psb_private
*dev_priv
= connector
->dev
->dev_private
;
246 if (mode
->clock
> 165000)
247 return MODE_CLOCK_HIGH
;
248 if (mode
->clock
< 20000)
249 return MODE_CLOCK_HIGH
;
252 if (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
253 return MODE_NO_DBLESCAN
;
256 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
257 return MODE_NO_INTERLACE
;
259 /* We assume worst case scenario of 32 bpp here, since we don't know */
260 if ((ALIGN(mode
->hdisplay
* 4, 64) * mode
->vdisplay
) >
261 dev_priv
->vram_stolen_size
)
267 static void cdv_hdmi_destroy(struct drm_connector
*connector
)
269 struct psb_intel_encoder
*psb_intel_encoder
=
270 psb_intel_attached_encoder(connector
);
272 if (psb_intel_encoder
->i2c_bus
)
273 psb_intel_i2c_destroy(psb_intel_encoder
->i2c_bus
);
274 drm_sysfs_connector_remove(connector
);
275 drm_connector_cleanup(connector
);
279 static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs
= {
280 .dpms
= cdv_hdmi_dpms
,
281 .mode_fixup
= cdv_hdmi_mode_fixup
,
282 .prepare
= psb_intel_encoder_prepare
,
283 .mode_set
= cdv_hdmi_mode_set
,
284 .commit
= psb_intel_encoder_commit
,
287 static const struct drm_connector_helper_funcs
288 cdv_hdmi_connector_helper_funcs
= {
289 .get_modes
= cdv_hdmi_get_modes
,
290 .mode_valid
= cdv_hdmi_mode_valid
,
291 .best_encoder
= psb_intel_best_encoder
,
294 static const struct drm_connector_funcs cdv_hdmi_connector_funcs
= {
295 .dpms
= drm_helper_connector_dpms
,
296 .save
= cdv_hdmi_save
,
297 .restore
= cdv_hdmi_restore
,
298 .detect
= cdv_hdmi_detect
,
299 .fill_modes
= drm_helper_probe_single_connector_modes
,
300 .set_property
= cdv_hdmi_set_property
,
301 .destroy
= cdv_hdmi_destroy
,
304 void cdv_hdmi_init(struct drm_device
*dev
,
305 struct psb_intel_mode_device
*mode_dev
, int reg
)
307 struct psb_intel_encoder
*psb_intel_encoder
;
308 struct psb_intel_connector
*psb_intel_connector
;
309 struct drm_connector
*connector
;
310 struct drm_encoder
*encoder
;
311 struct mid_intel_hdmi_priv
*hdmi_priv
;
314 psb_intel_encoder
= kzalloc(sizeof(struct psb_intel_encoder
),
317 if (!psb_intel_encoder
)
320 psb_intel_connector
= kzalloc(sizeof(struct psb_intel_connector
),
323 if (!psb_intel_connector
)
326 hdmi_priv
= kzalloc(sizeof(struct mid_intel_hdmi_priv
), GFP_KERNEL
);
331 connector
= &psb_intel_connector
->base
;
332 encoder
= &psb_intel_encoder
->base
;
333 drm_connector_init(dev
, connector
,
334 &cdv_hdmi_connector_funcs
,
335 DRM_MODE_CONNECTOR_DVID
);
337 drm_encoder_init(dev
, encoder
, &psb_intel_lvds_enc_funcs
,
338 DRM_MODE_ENCODER_TMDS
);
340 psb_intel_connector_attach_encoder(psb_intel_connector
,
342 psb_intel_encoder
->type
= INTEL_OUTPUT_HDMI
;
343 hdmi_priv
->hdmi_reg
= reg
;
344 hdmi_priv
->has_hdmi_sink
= false;
345 psb_intel_encoder
->dev_priv
= hdmi_priv
;
347 drm_encoder_helper_add(encoder
, &cdv_hdmi_helper_funcs
);
348 drm_connector_helper_add(connector
,
349 &cdv_hdmi_connector_helper_funcs
);
350 connector
->display_info
.subpixel_order
= SubPixelHorizontalRGB
;
351 connector
->interlace_allowed
= false;
352 connector
->doublescan_allowed
= false;
354 drm_connector_attach_property(connector
,
355 dev
->mode_config
.scaling_mode_property
,
356 DRM_MODE_SCALE_FULLSCREEN
);
366 DRM_ERROR("unknown reg 0x%x for HDMI\n", reg
);
371 psb_intel_encoder
->i2c_bus
= psb_intel_i2c_create(dev
,
372 ddc_bus
, (reg
== SDVOB
) ? "HDMIB" : "HDMIC");
374 if (!psb_intel_encoder
->i2c_bus
) {
375 dev_err(dev
->dev
, "No ddc adapter available!\n");
379 hdmi_priv
->hdmi_i2c_adapter
=
380 &(psb_intel_encoder
->i2c_bus
->adapter
);
381 hdmi_priv
->dev
= dev
;
382 drm_sysfs_connector_add(connector
);
386 drm_encoder_cleanup(encoder
);
387 drm_connector_cleanup(connector
);
389 kfree(psb_intel_connector
);
391 kfree(psb_intel_encoder
);