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 "cdv_device.h"
38 #include <linux/pm_runtime.h>
40 /* hdmi control bits */
41 #define HDMI_NULL_PACKETS_DURING_VSYNC (1 << 9)
42 #define HDMI_BORDER_ENABLE (1 << 7)
43 #define HDMI_AUDIO_ENABLE (1 << 6)
44 #define HDMI_VSYNC_ACTIVE_HIGH (1 << 4)
45 #define HDMI_HSYNC_ACTIVE_HIGH (1 << 3)
46 /* hdmi-b control bits */
47 #define HDMIB_PIPE_B_SELECT (1 << 30)
50 struct mid_intel_hdmi_priv
{
55 /* Should set this when detect hotplug */
56 bool hdmi_device_connected
;
57 struct mdfld_hdmi_i2c
*i2c_bus
;
58 struct i2c_adapter
*hdmi_i2c_adapter
; /* for control functions */
59 struct drm_device
*dev
;
62 static void cdv_hdmi_mode_set(struct drm_encoder
*encoder
,
63 struct drm_display_mode
*mode
,
64 struct drm_display_mode
*adjusted_mode
)
66 struct drm_device
*dev
= encoder
->dev
;
67 struct gma_encoder
*gma_encoder
= to_gma_encoder(encoder
);
68 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_encoder
->dev_priv
;
70 struct drm_crtc
*crtc
= encoder
->crtc
;
71 struct gma_crtc
*gma_crtc
= to_gma_crtc(crtc
);
75 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
76 hdmib
|= HDMI_VSYNC_ACTIVE_HIGH
;
77 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
78 hdmib
|= HDMI_HSYNC_ACTIVE_HIGH
;
80 if (gma_crtc
->pipe
== 1)
81 hdmib
|= HDMIB_PIPE_B_SELECT
;
83 if (hdmi_priv
->has_hdmi_audio
) {
84 hdmib
|= HDMI_AUDIO_ENABLE
;
85 hdmib
|= HDMI_NULL_PACKETS_DURING_VSYNC
;
88 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmib
);
89 REG_READ(hdmi_priv
->hdmi_reg
);
92 static bool cdv_hdmi_mode_fixup(struct drm_encoder
*encoder
,
93 const struct drm_display_mode
*mode
,
94 struct drm_display_mode
*adjusted_mode
)
99 static void cdv_hdmi_dpms(struct drm_encoder
*encoder
, int mode
)
101 struct drm_device
*dev
= encoder
->dev
;
102 struct gma_encoder
*gma_encoder
= to_gma_encoder(encoder
);
103 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_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 gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
119 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_encoder
->dev_priv
;
121 hdmi_priv
->save_HDMIB
= REG_READ(hdmi_priv
->hdmi_reg
);
124 static void cdv_hdmi_restore(struct drm_connector
*connector
)
126 struct drm_device
*dev
= connector
->dev
;
127 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
128 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_encoder
->dev_priv
;
130 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmi_priv
->save_HDMIB
);
131 REG_READ(hdmi_priv
->hdmi_reg
);
134 static enum drm_connector_status
cdv_hdmi_detect(
135 struct drm_connector
*connector
, bool force
)
137 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
138 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_encoder
->dev_priv
;
139 struct edid
*edid
= NULL
;
140 enum drm_connector_status status
= connector_status_disconnected
;
142 edid
= drm_get_edid(connector
, &gma_encoder
->i2c_bus
->adapter
);
144 hdmi_priv
->has_hdmi_sink
= false;
145 hdmi_priv
->has_hdmi_audio
= false;
147 if (edid
->input
& DRM_EDID_INPUT_DIGITAL
) {
148 status
= connector_status_connected
;
149 hdmi_priv
->has_hdmi_sink
=
150 drm_detect_hdmi_monitor(edid
);
151 hdmi_priv
->has_hdmi_audio
=
152 drm_detect_monitor_audio(edid
);
159 static int cdv_hdmi_set_property(struct drm_connector
*connector
,
160 struct drm_property
*property
,
163 struct drm_encoder
*encoder
= connector
->encoder
;
165 if (!strcmp(property
->name
, "scaling mode") && encoder
) {
166 struct gma_crtc
*crtc
= to_gma_crtc(encoder
->crtc
);
174 case DRM_MODE_SCALE_FULLSCREEN
:
176 case DRM_MODE_SCALE_NO_SCALE
:
178 case DRM_MODE_SCALE_ASPECT
:
184 if (drm_object_property_get_value(&connector
->base
,
185 property
, &curValue
))
188 if (curValue
== value
)
191 if (drm_object_property_set_value(&connector
->base
,
195 centre
= (curValue
== DRM_MODE_SCALE_NO_SCALE
) ||
196 (value
== DRM_MODE_SCALE_NO_SCALE
);
198 if (crtc
->saved_mode
.hdisplay
!= 0 &&
199 crtc
->saved_mode
.vdisplay
!= 0) {
201 if (!drm_crtc_helper_set_mode(encoder
->crtc
, &crtc
->saved_mode
,
202 encoder
->crtc
->x
, encoder
->crtc
->y
, encoder
->crtc
->fb
))
205 struct drm_encoder_helper_funcs
*helpers
206 = encoder
->helper_private
;
207 helpers
->mode_set(encoder
, &crtc
->saved_mode
,
208 &crtc
->saved_adjusted_mode
);
216 * Return the list of HDMI DDC modes if available.
218 static int cdv_hdmi_get_modes(struct drm_connector
*connector
)
220 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
221 struct edid
*edid
= NULL
;
224 edid
= drm_get_edid(connector
, &gma_encoder
->i2c_bus
->adapter
);
226 drm_mode_connector_update_edid_property(connector
, edid
);
227 ret
= drm_add_edid_modes(connector
, edid
);
233 static int cdv_hdmi_mode_valid(struct drm_connector
*connector
,
234 struct drm_display_mode
*mode
)
236 if (mode
->clock
> 165000)
237 return MODE_CLOCK_HIGH
;
238 if (mode
->clock
< 20000)
239 return MODE_CLOCK_HIGH
;
242 if (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
243 return MODE_NO_DBLESCAN
;
246 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
247 return MODE_NO_INTERLACE
;
252 static void cdv_hdmi_destroy(struct drm_connector
*connector
)
254 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
256 if (gma_encoder
->i2c_bus
)
257 psb_intel_i2c_destroy(gma_encoder
->i2c_bus
);
258 drm_sysfs_connector_remove(connector
);
259 drm_connector_cleanup(connector
);
263 static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs
= {
264 .dpms
= cdv_hdmi_dpms
,
265 .mode_fixup
= cdv_hdmi_mode_fixup
,
266 .prepare
= gma_encoder_prepare
,
267 .mode_set
= cdv_hdmi_mode_set
,
268 .commit
= gma_encoder_commit
,
271 static const struct drm_connector_helper_funcs
272 cdv_hdmi_connector_helper_funcs
= {
273 .get_modes
= cdv_hdmi_get_modes
,
274 .mode_valid
= cdv_hdmi_mode_valid
,
275 .best_encoder
= gma_best_encoder
,
278 static const struct drm_connector_funcs cdv_hdmi_connector_funcs
= {
279 .dpms
= drm_helper_connector_dpms
,
280 .save
= cdv_hdmi_save
,
281 .restore
= cdv_hdmi_restore
,
282 .detect
= cdv_hdmi_detect
,
283 .fill_modes
= drm_helper_probe_single_connector_modes
,
284 .set_property
= cdv_hdmi_set_property
,
285 .destroy
= cdv_hdmi_destroy
,
288 void cdv_hdmi_init(struct drm_device
*dev
,
289 struct psb_intel_mode_device
*mode_dev
, int reg
)
291 struct gma_encoder
*gma_encoder
;
292 struct gma_connector
*gma_connector
;
293 struct drm_connector
*connector
;
294 struct drm_encoder
*encoder
;
295 struct mid_intel_hdmi_priv
*hdmi_priv
;
298 gma_encoder
= kzalloc(sizeof(struct gma_encoder
), GFP_KERNEL
);
303 gma_connector
= kzalloc(sizeof(struct gma_connector
),
309 hdmi_priv
= kzalloc(sizeof(struct mid_intel_hdmi_priv
), GFP_KERNEL
);
314 connector
= &gma_connector
->base
;
315 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
316 encoder
= &gma_encoder
->base
;
317 drm_connector_init(dev
, connector
,
318 &cdv_hdmi_connector_funcs
,
319 DRM_MODE_CONNECTOR_DVID
);
321 drm_encoder_init(dev
, encoder
, &psb_intel_lvds_enc_funcs
,
322 DRM_MODE_ENCODER_TMDS
);
324 gma_connector_attach_encoder(gma_connector
, gma_encoder
);
325 gma_encoder
->type
= INTEL_OUTPUT_HDMI
;
326 hdmi_priv
->hdmi_reg
= reg
;
327 hdmi_priv
->has_hdmi_sink
= false;
328 gma_encoder
->dev_priv
= hdmi_priv
;
330 drm_encoder_helper_add(encoder
, &cdv_hdmi_helper_funcs
);
331 drm_connector_helper_add(connector
,
332 &cdv_hdmi_connector_helper_funcs
);
333 connector
->display_info
.subpixel_order
= SubPixelHorizontalRGB
;
334 connector
->interlace_allowed
= false;
335 connector
->doublescan_allowed
= false;
337 drm_object_attach_property(&connector
->base
,
338 dev
->mode_config
.scaling_mode_property
,
339 DRM_MODE_SCALE_FULLSCREEN
);
344 gma_encoder
->ddi_select
= DDI0_SELECT
;
348 gma_encoder
->ddi_select
= DDI1_SELECT
;
351 DRM_ERROR("unknown reg 0x%x for HDMI\n", reg
);
356 gma_encoder
->i2c_bus
= psb_intel_i2c_create(dev
,
357 ddc_bus
, (reg
== SDVOB
) ? "HDMIB" : "HDMIC");
359 if (!gma_encoder
->i2c_bus
) {
360 dev_err(dev
->dev
, "No ddc adapter available!\n");
364 hdmi_priv
->hdmi_i2c_adapter
= &(gma_encoder
->i2c_bus
->adapter
);
365 hdmi_priv
->dev
= dev
;
366 drm_sysfs_connector_add(connector
);
370 drm_encoder_cleanup(encoder
);
371 drm_connector_cleanup(connector
);
373 kfree(gma_connector
);