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
30 #include <linux/pm_runtime.h>
33 #include <drm/drm_crtc.h>
34 #include <drm/drm_edid.h>
35 #include <drm/drm_simple_kms_helper.h>
37 #include "cdv_device.h"
39 #include "psb_intel_drv.h"
40 #include "psb_intel_reg.h"
42 /* hdmi control bits */
43 #define HDMI_NULL_PACKETS_DURING_VSYNC (1 << 9)
44 #define HDMI_BORDER_ENABLE (1 << 7)
45 #define HDMI_AUDIO_ENABLE (1 << 6)
46 #define HDMI_VSYNC_ACTIVE_HIGH (1 << 4)
47 #define HDMI_HSYNC_ACTIVE_HIGH (1 << 3)
48 /* hdmi-b control bits */
49 #define HDMIB_PIPE_B_SELECT (1 << 30)
52 struct mid_intel_hdmi_priv
{
57 /* Should set this when detect hotplug */
58 bool hdmi_device_connected
;
59 struct mdfld_hdmi_i2c
*i2c_bus
;
60 struct i2c_adapter
*hdmi_i2c_adapter
; /* for control functions */
61 struct drm_device
*dev
;
64 static void cdv_hdmi_mode_set(struct drm_encoder
*encoder
,
65 struct drm_display_mode
*mode
,
66 struct drm_display_mode
*adjusted_mode
)
68 struct drm_device
*dev
= encoder
->dev
;
69 struct gma_encoder
*gma_encoder
= to_gma_encoder(encoder
);
70 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_encoder
->dev_priv
;
72 struct drm_crtc
*crtc
= encoder
->crtc
;
73 struct gma_crtc
*gma_crtc
= to_gma_crtc(crtc
);
77 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
78 hdmib
|= HDMI_VSYNC_ACTIVE_HIGH
;
79 if (adjusted_mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
80 hdmib
|= HDMI_HSYNC_ACTIVE_HIGH
;
82 if (gma_crtc
->pipe
== 1)
83 hdmib
|= HDMIB_PIPE_B_SELECT
;
85 if (hdmi_priv
->has_hdmi_audio
) {
86 hdmib
|= HDMI_AUDIO_ENABLE
;
87 hdmib
|= HDMI_NULL_PACKETS_DURING_VSYNC
;
90 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmib
);
91 REG_READ(hdmi_priv
->hdmi_reg
);
94 static void cdv_hdmi_dpms(struct drm_encoder
*encoder
, int mode
)
96 struct drm_device
*dev
= encoder
->dev
;
97 struct gma_encoder
*gma_encoder
= to_gma_encoder(encoder
);
98 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_encoder
->dev_priv
;
101 hdmib
= REG_READ(hdmi_priv
->hdmi_reg
);
103 if (mode
!= DRM_MODE_DPMS_ON
)
104 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmib
& ~HDMIB_PORT_EN
);
106 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmib
| HDMIB_PORT_EN
);
107 REG_READ(hdmi_priv
->hdmi_reg
);
110 static void cdv_hdmi_save(struct drm_connector
*connector
)
112 struct drm_device
*dev
= connector
->dev
;
113 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
114 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_encoder
->dev_priv
;
116 hdmi_priv
->save_HDMIB
= REG_READ(hdmi_priv
->hdmi_reg
);
119 static void cdv_hdmi_restore(struct drm_connector
*connector
)
121 struct drm_device
*dev
= connector
->dev
;
122 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
123 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_encoder
->dev_priv
;
125 REG_WRITE(hdmi_priv
->hdmi_reg
, hdmi_priv
->save_HDMIB
);
126 REG_READ(hdmi_priv
->hdmi_reg
);
129 static enum drm_connector_status
cdv_hdmi_detect(
130 struct drm_connector
*connector
, bool force
)
132 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
133 struct mid_intel_hdmi_priv
*hdmi_priv
= gma_encoder
->dev_priv
;
134 struct edid
*edid
= NULL
;
135 enum drm_connector_status status
= connector_status_disconnected
;
137 edid
= drm_get_edid(connector
, &gma_encoder
->i2c_bus
->adapter
);
139 hdmi_priv
->has_hdmi_sink
= false;
140 hdmi_priv
->has_hdmi_audio
= false;
142 if (edid
->input
& DRM_EDID_INPUT_DIGITAL
) {
143 status
= connector_status_connected
;
144 hdmi_priv
->has_hdmi_sink
=
145 drm_detect_hdmi_monitor(edid
);
146 hdmi_priv
->has_hdmi_audio
=
147 drm_detect_monitor_audio(edid
);
154 static int cdv_hdmi_set_property(struct drm_connector
*connector
,
155 struct drm_property
*property
,
158 struct drm_encoder
*encoder
= connector
->encoder
;
160 if (!strcmp(property
->name
, "scaling mode") && encoder
) {
161 struct gma_crtc
*crtc
= to_gma_crtc(encoder
->crtc
);
169 case DRM_MODE_SCALE_FULLSCREEN
:
171 case DRM_MODE_SCALE_NO_SCALE
:
173 case DRM_MODE_SCALE_ASPECT
:
179 if (drm_object_property_get_value(&connector
->base
,
180 property
, &curValue
))
183 if (curValue
== value
)
186 if (drm_object_property_set_value(&connector
->base
,
190 centre
= (curValue
== DRM_MODE_SCALE_NO_SCALE
) ||
191 (value
== DRM_MODE_SCALE_NO_SCALE
);
193 if (crtc
->saved_mode
.hdisplay
!= 0 &&
194 crtc
->saved_mode
.vdisplay
!= 0) {
196 if (!drm_crtc_helper_set_mode(encoder
->crtc
, &crtc
->saved_mode
,
197 encoder
->crtc
->x
, encoder
->crtc
->y
, encoder
->crtc
->primary
->fb
))
200 const struct drm_encoder_helper_funcs
*helpers
201 = encoder
->helper_private
;
202 helpers
->mode_set(encoder
, &crtc
->saved_mode
,
203 &crtc
->saved_adjusted_mode
);
211 * Return the list of HDMI DDC modes if available.
213 static int cdv_hdmi_get_modes(struct drm_connector
*connector
)
215 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
216 struct edid
*edid
= NULL
;
219 edid
= drm_get_edid(connector
, &gma_encoder
->i2c_bus
->adapter
);
221 drm_connector_update_edid_property(connector
, edid
);
222 ret
= drm_add_edid_modes(connector
, edid
);
228 static enum drm_mode_status
cdv_hdmi_mode_valid(struct drm_connector
*connector
,
229 struct drm_display_mode
*mode
)
231 if (mode
->clock
> 165000)
232 return MODE_CLOCK_HIGH
;
233 if (mode
->clock
< 20000)
234 return MODE_CLOCK_HIGH
;
237 if (mode
->flags
& DRM_MODE_FLAG_DBLSCAN
)
238 return MODE_NO_DBLESCAN
;
241 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
242 return MODE_NO_INTERLACE
;
247 static void cdv_hdmi_destroy(struct drm_connector
*connector
)
249 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
251 psb_intel_i2c_destroy(gma_encoder
->i2c_bus
);
252 drm_connector_unregister(connector
);
253 drm_connector_cleanup(connector
);
257 static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs
= {
258 .dpms
= cdv_hdmi_dpms
,
259 .prepare
= gma_encoder_prepare
,
260 .mode_set
= cdv_hdmi_mode_set
,
261 .commit
= gma_encoder_commit
,
264 static const struct drm_connector_helper_funcs
265 cdv_hdmi_connector_helper_funcs
= {
266 .get_modes
= cdv_hdmi_get_modes
,
267 .mode_valid
= cdv_hdmi_mode_valid
,
268 .best_encoder
= gma_best_encoder
,
271 static const struct drm_connector_funcs cdv_hdmi_connector_funcs
= {
272 .dpms
= drm_helper_connector_dpms
,
273 .detect
= cdv_hdmi_detect
,
274 .fill_modes
= drm_helper_probe_single_connector_modes
,
275 .set_property
= cdv_hdmi_set_property
,
276 .destroy
= cdv_hdmi_destroy
,
279 void cdv_hdmi_init(struct drm_device
*dev
,
280 struct psb_intel_mode_device
*mode_dev
, int reg
)
282 struct gma_encoder
*gma_encoder
;
283 struct gma_connector
*gma_connector
;
284 struct drm_connector
*connector
;
285 struct drm_encoder
*encoder
;
286 struct mid_intel_hdmi_priv
*hdmi_priv
;
289 gma_encoder
= kzalloc(sizeof(struct gma_encoder
), GFP_KERNEL
);
294 gma_connector
= kzalloc(sizeof(struct gma_connector
),
300 hdmi_priv
= kzalloc(sizeof(struct mid_intel_hdmi_priv
), GFP_KERNEL
);
305 connector
= &gma_connector
->base
;
306 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
307 gma_connector
->save
= cdv_hdmi_save
;
308 gma_connector
->restore
= cdv_hdmi_restore
;
310 encoder
= &gma_encoder
->base
;
311 drm_connector_init(dev
, connector
,
312 &cdv_hdmi_connector_funcs
,
313 DRM_MODE_CONNECTOR_DVID
);
315 drm_simple_encoder_init(dev
, encoder
, DRM_MODE_ENCODER_TMDS
);
317 gma_connector_attach_encoder(gma_connector
, gma_encoder
);
318 gma_encoder
->type
= INTEL_OUTPUT_HDMI
;
319 hdmi_priv
->hdmi_reg
= reg
;
320 hdmi_priv
->has_hdmi_sink
= false;
321 gma_encoder
->dev_priv
= hdmi_priv
;
323 drm_encoder_helper_add(encoder
, &cdv_hdmi_helper_funcs
);
324 drm_connector_helper_add(connector
,
325 &cdv_hdmi_connector_helper_funcs
);
326 connector
->display_info
.subpixel_order
= SubPixelHorizontalRGB
;
327 connector
->interlace_allowed
= false;
328 connector
->doublescan_allowed
= false;
330 drm_object_attach_property(&connector
->base
,
331 dev
->mode_config
.scaling_mode_property
,
332 DRM_MODE_SCALE_FULLSCREEN
);
337 gma_encoder
->ddi_select
= DDI0_SELECT
;
341 gma_encoder
->ddi_select
= DDI1_SELECT
;
344 DRM_ERROR("unknown reg 0x%x for HDMI\n", reg
);
349 gma_encoder
->i2c_bus
= psb_intel_i2c_create(dev
,
350 ddc_bus
, (reg
== SDVOB
) ? "HDMIB" : "HDMIC");
352 if (!gma_encoder
->i2c_bus
) {
353 dev_err(dev
->dev
, "No ddc adapter available!\n");
357 hdmi_priv
->hdmi_i2c_adapter
= &(gma_encoder
->i2c_bus
->adapter
);
358 hdmi_priv
->dev
= dev
;
359 drm_connector_register(connector
);
363 drm_encoder_cleanup(encoder
);
364 drm_connector_cleanup(connector
);
366 kfree(gma_connector
);