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 #include <linux/pm_runtime.h>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_edid.h>
32 #include <drm/drm_simple_kms_helper.h>
34 #include "cdv_device.h"
36 #include "psb_intel_drv.h"
37 #include "psb_intel_reg.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 i2c_adapter *hdmi_i2c_adapter; /* for control functions */
57 struct drm_device *dev;
60 static void cdv_hdmi_mode_set(struct drm_encoder *encoder,
61 struct drm_display_mode *mode,
62 struct drm_display_mode *adjusted_mode)
64 struct drm_device *dev = encoder->dev;
65 struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
66 struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
68 struct drm_crtc *crtc = encoder->crtc;
69 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
73 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
74 hdmib |= HDMI_VSYNC_ACTIVE_HIGH;
75 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
76 hdmib |= HDMI_HSYNC_ACTIVE_HIGH;
78 if (gma_crtc->pipe == 1)
79 hdmib |= HDMIB_PIPE_B_SELECT;
81 if (hdmi_priv->has_hdmi_audio) {
82 hdmib |= HDMI_AUDIO_ENABLE;
83 hdmib |= HDMI_NULL_PACKETS_DURING_VSYNC;
86 REG_WRITE(hdmi_priv->hdmi_reg, hdmib);
87 REG_READ(hdmi_priv->hdmi_reg);
90 static void cdv_hdmi_dpms(struct drm_encoder *encoder, int mode)
92 struct drm_device *dev = encoder->dev;
93 struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
94 struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
97 hdmib = REG_READ(hdmi_priv->hdmi_reg);
99 if (mode != DRM_MODE_DPMS_ON)
100 REG_WRITE(hdmi_priv->hdmi_reg, hdmib & ~HDMIB_PORT_EN);
102 REG_WRITE(hdmi_priv->hdmi_reg, hdmib | HDMIB_PORT_EN);
103 REG_READ(hdmi_priv->hdmi_reg);
106 static void cdv_hdmi_save(struct drm_connector *connector)
108 struct drm_device *dev = connector->dev;
109 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
110 struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
112 hdmi_priv->save_HDMIB = REG_READ(hdmi_priv->hdmi_reg);
115 static void cdv_hdmi_restore(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 REG_WRITE(hdmi_priv->hdmi_reg, hdmi_priv->save_HDMIB);
122 REG_READ(hdmi_priv->hdmi_reg);
125 static enum drm_connector_status cdv_hdmi_detect(
126 struct drm_connector *connector, bool force)
128 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
129 struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
130 struct edid *edid = NULL;
131 enum drm_connector_status status = connector_status_disconnected;
133 edid = drm_get_edid(connector, &gma_encoder->i2c_bus->adapter);
135 hdmi_priv->has_hdmi_sink = false;
136 hdmi_priv->has_hdmi_audio = false;
138 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
139 status = connector_status_connected;
140 hdmi_priv->has_hdmi_sink =
141 drm_detect_hdmi_monitor(edid);
142 hdmi_priv->has_hdmi_audio =
143 drm_detect_monitor_audio(edid);
150 static int cdv_hdmi_set_property(struct drm_connector *connector,
151 struct drm_property *property,
154 struct drm_encoder *encoder = connector->encoder;
156 if (!strcmp(property->name, "scaling mode") && encoder) {
157 struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
165 case DRM_MODE_SCALE_FULLSCREEN:
167 case DRM_MODE_SCALE_NO_SCALE:
169 case DRM_MODE_SCALE_ASPECT:
175 if (drm_object_property_get_value(&connector->base,
176 property, &curValue))
179 if (curValue == value)
182 if (drm_object_property_set_value(&connector->base,
186 centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
187 (value == DRM_MODE_SCALE_NO_SCALE);
189 if (crtc->saved_mode.hdisplay != 0 &&
190 crtc->saved_mode.vdisplay != 0) {
192 if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
193 encoder->crtc->x, encoder->crtc->y, encoder->crtc->primary->fb))
196 const struct drm_encoder_helper_funcs *helpers
197 = encoder->helper_private;
198 helpers->mode_set(encoder, &crtc->saved_mode,
199 &crtc->saved_adjusted_mode);
207 * Return the list of HDMI DDC modes if available.
209 static int cdv_hdmi_get_modes(struct drm_connector *connector)
211 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
212 struct edid *edid = NULL;
215 edid = drm_get_edid(connector, &gma_encoder->i2c_bus->adapter);
217 drm_connector_update_edid_property(connector, edid);
218 ret = drm_add_edid_modes(connector, edid);
224 static enum drm_mode_status cdv_hdmi_mode_valid(struct drm_connector *connector,
225 struct drm_display_mode *mode)
227 if (mode->clock > 165000)
228 return MODE_CLOCK_HIGH;
229 if (mode->clock < 20000)
230 return MODE_CLOCK_HIGH;
233 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
234 return MODE_NO_DBLESCAN;
237 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
238 return MODE_NO_INTERLACE;
243 static void cdv_hdmi_destroy(struct drm_connector *connector)
245 struct gma_connector *gma_connector = to_gma_connector(connector);
246 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
248 psb_intel_i2c_destroy(gma_encoder->i2c_bus);
249 drm_connector_cleanup(connector);
250 kfree(gma_connector);
253 static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs = {
254 .dpms = cdv_hdmi_dpms,
255 .prepare = gma_encoder_prepare,
256 .mode_set = cdv_hdmi_mode_set,
257 .commit = gma_encoder_commit,
260 static const struct drm_connector_helper_funcs
261 cdv_hdmi_connector_helper_funcs = {
262 .get_modes = cdv_hdmi_get_modes,
263 .mode_valid = cdv_hdmi_mode_valid,
264 .best_encoder = gma_best_encoder,
267 static const struct drm_connector_funcs cdv_hdmi_connector_funcs = {
268 .dpms = drm_helper_connector_dpms,
269 .detect = cdv_hdmi_detect,
270 .fill_modes = drm_helper_probe_single_connector_modes,
271 .set_property = cdv_hdmi_set_property,
272 .destroy = cdv_hdmi_destroy,
275 void cdv_hdmi_init(struct drm_device *dev,
276 struct psb_intel_mode_device *mode_dev, int reg)
278 struct gma_encoder *gma_encoder;
279 struct gma_connector *gma_connector;
280 struct drm_connector *connector;
281 struct drm_encoder *encoder;
282 struct mid_intel_hdmi_priv *hdmi_priv;
285 gma_encoder = kzalloc(sizeof(struct gma_encoder), GFP_KERNEL);
290 gma_connector = kzalloc(sizeof(struct gma_connector),
296 hdmi_priv = kzalloc(sizeof(struct mid_intel_hdmi_priv), GFP_KERNEL);
301 connector = &gma_connector->base;
302 connector->polled = DRM_CONNECTOR_POLL_HPD;
303 gma_connector->save = cdv_hdmi_save;
304 gma_connector->restore = cdv_hdmi_restore;
306 encoder = &gma_encoder->base;
307 drm_connector_init(dev, connector,
308 &cdv_hdmi_connector_funcs,
309 DRM_MODE_CONNECTOR_DVID);
311 drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
313 gma_connector_attach_encoder(gma_connector, gma_encoder);
314 gma_encoder->type = INTEL_OUTPUT_HDMI;
315 hdmi_priv->hdmi_reg = reg;
316 hdmi_priv->has_hdmi_sink = false;
317 gma_encoder->dev_priv = hdmi_priv;
319 drm_encoder_helper_add(encoder, &cdv_hdmi_helper_funcs);
320 drm_connector_helper_add(connector,
321 &cdv_hdmi_connector_helper_funcs);
322 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
323 connector->interlace_allowed = false;
324 connector->doublescan_allowed = false;
326 drm_object_attach_property(&connector->base,
327 dev->mode_config.scaling_mode_property,
328 DRM_MODE_SCALE_FULLSCREEN);
333 gma_encoder->ddi_select = DDI0_SELECT;
337 gma_encoder->ddi_select = DDI1_SELECT;
340 DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
345 gma_encoder->i2c_bus = psb_intel_i2c_create(dev,
346 ddc_bus, (reg == SDVOB) ? "HDMIB" : "HDMIC");
348 if (!gma_encoder->i2c_bus) {
349 dev_err(dev->dev, "No ddc adapter available!\n");
353 hdmi_priv->hdmi_i2c_adapter = &(gma_encoder->i2c_bus->adapter);
354 hdmi_priv->dev = dev;
358 drm_encoder_cleanup(encoder);
359 drm_connector_cleanup(connector);
361 kfree(gma_connector);