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 psb_intel_encoder *psb_intel_encoder = to_psb_intel_encoder(encoder);
68 struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
70 struct drm_crtc *crtc = encoder->crtc;
71 struct psb_intel_crtc *intel_crtc = to_psb_intel_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 (intel_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 psb_intel_encoder *psb_intel_encoder =
103 to_psb_intel_encoder(encoder);
104 struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
107 hdmib = REG_READ(hdmi_priv->hdmi_reg);
109 if (mode != DRM_MODE_DPMS_ON)
110 REG_WRITE(hdmi_priv->hdmi_reg, hdmib & ~HDMIB_PORT_EN);
112 REG_WRITE(hdmi_priv->hdmi_reg, hdmib | HDMIB_PORT_EN);
113 REG_READ(hdmi_priv->hdmi_reg);
116 static void cdv_hdmi_save(struct drm_connector *connector)
118 struct drm_device *dev = connector->dev;
119 struct psb_intel_encoder *psb_intel_encoder =
120 psb_intel_attached_encoder(connector);
121 struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
123 hdmi_priv->save_HDMIB = REG_READ(hdmi_priv->hdmi_reg);
126 static void cdv_hdmi_restore(struct drm_connector *connector)
128 struct drm_device *dev = connector->dev;
129 struct psb_intel_encoder *psb_intel_encoder =
130 psb_intel_attached_encoder(connector);
131 struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
133 REG_WRITE(hdmi_priv->hdmi_reg, hdmi_priv->save_HDMIB);
134 REG_READ(hdmi_priv->hdmi_reg);
137 static enum drm_connector_status cdv_hdmi_detect(
138 struct drm_connector *connector, bool force)
140 struct psb_intel_encoder *psb_intel_encoder =
141 psb_intel_attached_encoder(connector);
142 struct psb_intel_connector *psb_intel_connector =
143 to_psb_intel_connector(connector);
144 struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
145 struct edid *edid = NULL;
146 enum drm_connector_status status = connector_status_disconnected;
148 edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
150 hdmi_priv->has_hdmi_sink = false;
151 hdmi_priv->has_hdmi_audio = false;
153 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
154 status = connector_status_connected;
155 hdmi_priv->has_hdmi_sink =
156 drm_detect_hdmi_monitor(edid);
157 hdmi_priv->has_hdmi_audio =
158 drm_detect_monitor_audio(edid);
161 psb_intel_connector->base.display_info.raw_edid = NULL;
167 static int cdv_hdmi_set_property(struct drm_connector *connector,
168 struct drm_property *property,
171 struct drm_encoder *encoder = connector->encoder;
173 if (!strcmp(property->name, "scaling mode") && encoder) {
174 struct psb_intel_crtc *crtc = to_psb_intel_crtc(encoder->crtc);
182 case DRM_MODE_SCALE_FULLSCREEN:
184 case DRM_MODE_SCALE_NO_SCALE:
186 case DRM_MODE_SCALE_ASPECT:
192 if (drm_connector_property_get_value(connector,
193 property, &curValue))
196 if (curValue == value)
199 if (drm_connector_property_set_value(connector,
203 centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
204 (value == DRM_MODE_SCALE_NO_SCALE);
206 if (crtc->saved_mode.hdisplay != 0 &&
207 crtc->saved_mode.vdisplay != 0) {
209 if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
210 encoder->crtc->x, encoder->crtc->y, encoder->crtc->fb))
213 struct drm_encoder_helper_funcs *helpers
214 = encoder->helper_private;
215 helpers->mode_set(encoder, &crtc->saved_mode,
216 &crtc->saved_adjusted_mode);
224 * Return the list of HDMI DDC modes if available.
226 static int cdv_hdmi_get_modes(struct drm_connector *connector)
228 struct psb_intel_encoder *psb_intel_encoder =
229 psb_intel_attached_encoder(connector);
230 struct edid *edid = NULL;
233 edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
235 drm_mode_connector_update_edid_property(connector, edid);
236 ret = drm_add_edid_modes(connector, edid);
242 static int cdv_hdmi_mode_valid(struct drm_connector *connector,
243 struct drm_display_mode *mode)
245 if (mode->clock > 165000)
246 return MODE_CLOCK_HIGH;
247 if (mode->clock < 20000)
248 return MODE_CLOCK_HIGH;
251 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
252 return MODE_NO_DBLESCAN;
255 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
256 return MODE_NO_INTERLACE;
261 static void cdv_hdmi_destroy(struct drm_connector *connector)
263 struct psb_intel_encoder *psb_intel_encoder =
264 psb_intel_attached_encoder(connector);
266 if (psb_intel_encoder->i2c_bus)
267 psb_intel_i2c_destroy(psb_intel_encoder->i2c_bus);
268 drm_sysfs_connector_remove(connector);
269 drm_connector_cleanup(connector);
273 static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs = {
274 .dpms = cdv_hdmi_dpms,
275 .mode_fixup = cdv_hdmi_mode_fixup,
276 .prepare = psb_intel_encoder_prepare,
277 .mode_set = cdv_hdmi_mode_set,
278 .commit = psb_intel_encoder_commit,
281 static const struct drm_connector_helper_funcs
282 cdv_hdmi_connector_helper_funcs = {
283 .get_modes = cdv_hdmi_get_modes,
284 .mode_valid = cdv_hdmi_mode_valid,
285 .best_encoder = psb_intel_best_encoder,
288 static const struct drm_connector_funcs cdv_hdmi_connector_funcs = {
289 .dpms = drm_helper_connector_dpms,
290 .save = cdv_hdmi_save,
291 .restore = cdv_hdmi_restore,
292 .detect = cdv_hdmi_detect,
293 .fill_modes = drm_helper_probe_single_connector_modes,
294 .set_property = cdv_hdmi_set_property,
295 .destroy = cdv_hdmi_destroy,
298 void cdv_hdmi_init(struct drm_device *dev,
299 struct psb_intel_mode_device *mode_dev, int reg)
301 struct psb_intel_encoder *psb_intel_encoder;
302 struct psb_intel_connector *psb_intel_connector;
303 struct drm_connector *connector;
304 struct drm_encoder *encoder;
305 struct mid_intel_hdmi_priv *hdmi_priv;
308 psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder),
311 if (!psb_intel_encoder)
314 psb_intel_connector = kzalloc(sizeof(struct psb_intel_connector),
317 if (!psb_intel_connector)
320 hdmi_priv = kzalloc(sizeof(struct mid_intel_hdmi_priv), GFP_KERNEL);
325 connector = &psb_intel_connector->base;
326 encoder = &psb_intel_encoder->base;
327 drm_connector_init(dev, connector,
328 &cdv_hdmi_connector_funcs,
329 DRM_MODE_CONNECTOR_DVID);
331 drm_encoder_init(dev, encoder, &psb_intel_lvds_enc_funcs,
332 DRM_MODE_ENCODER_TMDS);
334 psb_intel_connector_attach_encoder(psb_intel_connector,
336 psb_intel_encoder->type = INTEL_OUTPUT_HDMI;
337 hdmi_priv->hdmi_reg = reg;
338 hdmi_priv->has_hdmi_sink = false;
339 psb_intel_encoder->dev_priv = hdmi_priv;
341 drm_encoder_helper_add(encoder, &cdv_hdmi_helper_funcs);
342 drm_connector_helper_add(connector,
343 &cdv_hdmi_connector_helper_funcs);
344 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
345 connector->interlace_allowed = false;
346 connector->doublescan_allowed = false;
348 drm_connector_attach_property(connector,
349 dev->mode_config.scaling_mode_property,
350 DRM_MODE_SCALE_FULLSCREEN);
360 DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
365 psb_intel_encoder->i2c_bus = psb_intel_i2c_create(dev,
366 ddc_bus, (reg == SDVOB) ? "HDMIB" : "HDMIC");
368 if (!psb_intel_encoder->i2c_bus) {
369 dev_err(dev->dev, "No ddc adapter available!\n");
373 hdmi_priv->hdmi_i2c_adapter =
374 &(psb_intel_encoder->i2c_bus->adapter);
375 hdmi_priv->dev = dev;
376 drm_sysfs_connector_add(connector);
380 drm_encoder_cleanup(encoder);
381 drm_connector_cleanup(connector);
383 kfree(psb_intel_connector);
385 kfree(psb_intel_encoder);