blob: a849e912f30ad89df4019b55bded0cea1a5caefd [file] [log] [blame]
Alan Cox6a227d52011-11-03 18:22:37 +00001/*
2 * Copyright © 2006-2011 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 * Authors:
24 * jim liu <jim.liu@intel.com>
25 *
26 * FIXME:
27 * We should probably make this generic and share it with Medfield
28 */
29
30#include <drm/drmP.h>
31#include <drm/drm.h>
32#include <drm/drm_crtc.h>
33#include <drm/drm_edid.h>
34#include "psb_intel_drv.h"
35#include "psb_drv.h"
36#include "psb_intel_reg.h"
Kirill A. Shutemov455b9e92012-03-08 16:10:38 +000037#include "cdv_device.h"
Alan Cox6a227d52011-11-03 18:22:37 +000038#include <linux/pm_runtime.h>
39
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)
48
49
50struct mid_intel_hdmi_priv {
51 u32 hdmi_reg;
52 u32 save_HDMIB;
53 bool has_hdmi_sink;
54 bool has_hdmi_audio;
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;
60};
61
62static void cdv_hdmi_mode_set(struct drm_encoder *encoder,
63 struct drm_display_mode *mode,
64 struct drm_display_mode *adjusted_mode)
65{
66 struct drm_device *dev = encoder->dev;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +000067 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;
Alan Cox6a227d52011-11-03 18:22:37 +000069 u32 hdmib;
70 struct drm_crtc *crtc = encoder->crtc;
71 struct psb_intel_crtc *intel_crtc = to_psb_intel_crtc(crtc);
72
73 hdmib = (2 << 10);
74
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;
79
80 if (intel_crtc->pipe == 1)
81 hdmib |= HDMIB_PIPE_B_SELECT;
82
83 if (hdmi_priv->has_hdmi_audio) {
84 hdmib |= HDMI_AUDIO_ENABLE;
85 hdmib |= HDMI_NULL_PACKETS_DURING_VSYNC;
86 }
87
88 REG_WRITE(hdmi_priv->hdmi_reg, hdmib);
89 REG_READ(hdmi_priv->hdmi_reg);
90}
91
92static bool cdv_hdmi_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020093 const struct drm_display_mode *mode,
Alan Cox6a227d52011-11-03 18:22:37 +000094 struct drm_display_mode *adjusted_mode)
95{
96 return true;
97}
98
99static void cdv_hdmi_dpms(struct drm_encoder *encoder, int mode)
100{
101 struct drm_device *dev = encoder->dev;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000102 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;
Alan Cox6a227d52011-11-03 18:22:37 +0000105 u32 hdmib;
106
107 hdmib = REG_READ(hdmi_priv->hdmi_reg);
108
109 if (mode != DRM_MODE_DPMS_ON)
110 REG_WRITE(hdmi_priv->hdmi_reg, hdmib & ~HDMIB_PORT_EN);
111 else
112 REG_WRITE(hdmi_priv->hdmi_reg, hdmib | HDMIB_PORT_EN);
113 REG_READ(hdmi_priv->hdmi_reg);
114}
115
116static void cdv_hdmi_save(struct drm_connector *connector)
117{
118 struct drm_device *dev = connector->dev;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000119 struct psb_intel_encoder *psb_intel_encoder =
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200120 gma_attached_encoder(connector);
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000121 struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
Alan Cox6a227d52011-11-03 18:22:37 +0000122
123 hdmi_priv->save_HDMIB = REG_READ(hdmi_priv->hdmi_reg);
124}
125
126static void cdv_hdmi_restore(struct drm_connector *connector)
127{
128 struct drm_device *dev = connector->dev;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000129 struct psb_intel_encoder *psb_intel_encoder =
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200130 gma_attached_encoder(connector);
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000131 struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
Alan Cox6a227d52011-11-03 18:22:37 +0000132
133 REG_WRITE(hdmi_priv->hdmi_reg, hdmi_priv->save_HDMIB);
134 REG_READ(hdmi_priv->hdmi_reg);
135}
136
137static enum drm_connector_status cdv_hdmi_detect(
138 struct drm_connector *connector, bool force)
139{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000140 struct psb_intel_encoder *psb_intel_encoder =
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200141 gma_attached_encoder(connector);
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000142 struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
Alan Cox6a227d52011-11-03 18:22:37 +0000143 struct edid *edid = NULL;
144 enum drm_connector_status status = connector_status_disconnected;
145
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000146 edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
Alan Cox6a227d52011-11-03 18:22:37 +0000147
148 hdmi_priv->has_hdmi_sink = false;
149 hdmi_priv->has_hdmi_audio = false;
150 if (edid) {
151 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
152 status = connector_status_connected;
153 hdmi_priv->has_hdmi_sink =
154 drm_detect_hdmi_monitor(edid);
155 hdmi_priv->has_hdmi_audio =
156 drm_detect_monitor_audio(edid);
157 }
Alan Cox6a227d52011-11-03 18:22:37 +0000158 kfree(edid);
159 }
160 return status;
161}
162
163static int cdv_hdmi_set_property(struct drm_connector *connector,
164 struct drm_property *property,
165 uint64_t value)
166{
167 struct drm_encoder *encoder = connector->encoder;
168
169 if (!strcmp(property->name, "scaling mode") && encoder) {
170 struct psb_intel_crtc *crtc = to_psb_intel_crtc(encoder->crtc);
171 bool centre;
172 uint64_t curValue;
173
174 if (!crtc)
175 return -1;
176
177 switch (value) {
178 case DRM_MODE_SCALE_FULLSCREEN:
179 break;
180 case DRM_MODE_SCALE_NO_SCALE:
181 break;
182 case DRM_MODE_SCALE_ASPECT:
183 break;
184 default:
185 return -1;
186 }
187
Rob Clarka69ac9e2012-10-11 20:38:23 -0500188 if (drm_object_property_get_value(&connector->base,
Alan Cox6a227d52011-11-03 18:22:37 +0000189 property, &curValue))
190 return -1;
191
192 if (curValue == value)
193 return 0;
194
Rob Clarka69ac9e2012-10-11 20:38:23 -0500195 if (drm_object_property_set_value(&connector->base,
Alan Cox6a227d52011-11-03 18:22:37 +0000196 property, value))
197 return -1;
198
199 centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
200 (value == DRM_MODE_SCALE_NO_SCALE);
201
202 if (crtc->saved_mode.hdisplay != 0 &&
203 crtc->saved_mode.vdisplay != 0) {
204 if (centre) {
205 if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
206 encoder->crtc->x, encoder->crtc->y, encoder->crtc->fb))
207 return -1;
208 } else {
209 struct drm_encoder_helper_funcs *helpers
210 = encoder->helper_private;
211 helpers->mode_set(encoder, &crtc->saved_mode,
212 &crtc->saved_adjusted_mode);
213 }
214 }
215 }
216 return 0;
217}
218
219/*
220 * Return the list of HDMI DDC modes if available.
221 */
222static int cdv_hdmi_get_modes(struct drm_connector *connector)
223{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000224 struct psb_intel_encoder *psb_intel_encoder =
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200225 gma_attached_encoder(connector);
Alan Cox6a227d52011-11-03 18:22:37 +0000226 struct edid *edid = NULL;
227 int ret = 0;
228
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000229 edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
Alan Cox6a227d52011-11-03 18:22:37 +0000230 if (edid) {
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000231 drm_mode_connector_update_edid_property(connector, edid);
232 ret = drm_add_edid_modes(connector, edid);
Alan Cox6a227d52011-11-03 18:22:37 +0000233 kfree(edid);
234 }
235 return ret;
236}
237
238static int cdv_hdmi_mode_valid(struct drm_connector *connector,
239 struct drm_display_mode *mode)
240{
Alan Cox6a227d52011-11-03 18:22:37 +0000241 if (mode->clock > 165000)
242 return MODE_CLOCK_HIGH;
243 if (mode->clock < 20000)
244 return MODE_CLOCK_HIGH;
245
246 /* just in case */
247 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
248 return MODE_NO_DBLESCAN;
249
250 /* just in case */
251 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
252 return MODE_NO_INTERLACE;
253
Alan Cox6a227d52011-11-03 18:22:37 +0000254 return MODE_OK;
255}
256
257static void cdv_hdmi_destroy(struct drm_connector *connector)
258{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000259 struct psb_intel_encoder *psb_intel_encoder =
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200260 gma_attached_encoder(connector);
Alan Cox6a227d52011-11-03 18:22:37 +0000261
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000262 if (psb_intel_encoder->i2c_bus)
263 psb_intel_i2c_destroy(psb_intel_encoder->i2c_bus);
Alan Cox6a227d52011-11-03 18:22:37 +0000264 drm_sysfs_connector_remove(connector);
265 drm_connector_cleanup(connector);
266 kfree(connector);
267}
268
269static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs = {
270 .dpms = cdv_hdmi_dpms,
271 .mode_fixup = cdv_hdmi_mode_fixup,
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200272 .prepare = gma_encoder_prepare,
Alan Cox6a227d52011-11-03 18:22:37 +0000273 .mode_set = cdv_hdmi_mode_set,
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200274 .commit = gma_encoder_commit,
Alan Cox6a227d52011-11-03 18:22:37 +0000275};
276
277static const struct drm_connector_helper_funcs
278 cdv_hdmi_connector_helper_funcs = {
279 .get_modes = cdv_hdmi_get_modes,
280 .mode_valid = cdv_hdmi_mode_valid,
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200281 .best_encoder = gma_best_encoder,
Alan Cox6a227d52011-11-03 18:22:37 +0000282};
283
284static const struct drm_connector_funcs cdv_hdmi_connector_funcs = {
285 .dpms = drm_helper_connector_dpms,
286 .save = cdv_hdmi_save,
287 .restore = cdv_hdmi_restore,
288 .detect = cdv_hdmi_detect,
289 .fill_modes = drm_helper_probe_single_connector_modes,
290 .set_property = cdv_hdmi_set_property,
291 .destroy = cdv_hdmi_destroy,
292};
293
294void cdv_hdmi_init(struct drm_device *dev,
295 struct psb_intel_mode_device *mode_dev, int reg)
296{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000297 struct psb_intel_encoder *psb_intel_encoder;
298 struct psb_intel_connector *psb_intel_connector;
Alan Cox6a227d52011-11-03 18:22:37 +0000299 struct drm_connector *connector;
300 struct drm_encoder *encoder;
301 struct mid_intel_hdmi_priv *hdmi_priv;
302 int ddc_bus;
303
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000304 psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder),
305 GFP_KERNEL);
306
307 if (!psb_intel_encoder)
Alan Cox6a227d52011-11-03 18:22:37 +0000308 return;
309
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000310 psb_intel_connector = kzalloc(sizeof(struct psb_intel_connector),
311 GFP_KERNEL);
312
313 if (!psb_intel_connector)
314 goto err_connector;
315
316 hdmi_priv = kzalloc(sizeof(struct mid_intel_hdmi_priv), GFP_KERNEL);
317
318 if (!hdmi_priv)
319 goto err_priv;
320
321 connector = &psb_intel_connector->base;
Kero van Gelderbda50032013-03-31 13:38:44 +0200322 connector->polled = DRM_CONNECTOR_POLL_HPD;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000323 encoder = &psb_intel_encoder->base;
324 drm_connector_init(dev, connector,
Alan Cox6a227d52011-11-03 18:22:37 +0000325 &cdv_hdmi_connector_funcs,
326 DRM_MODE_CONNECTOR_DVID);
327
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000328 drm_encoder_init(dev, encoder, &psb_intel_lvds_enc_funcs,
Alan Cox6a227d52011-11-03 18:22:37 +0000329 DRM_MODE_ENCODER_TMDS);
330
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200331 gma_connector_attach_encoder(psb_intel_connector, psb_intel_encoder);
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000332 psb_intel_encoder->type = INTEL_OUTPUT_HDMI;
Alan Cox6a227d52011-11-03 18:22:37 +0000333 hdmi_priv->hdmi_reg = reg;
334 hdmi_priv->has_hdmi_sink = false;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000335 psb_intel_encoder->dev_priv = hdmi_priv;
Alan Cox6a227d52011-11-03 18:22:37 +0000336
337 drm_encoder_helper_add(encoder, &cdv_hdmi_helper_funcs);
338 drm_connector_helper_add(connector,
339 &cdv_hdmi_connector_helper_funcs);
340 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
341 connector->interlace_allowed = false;
342 connector->doublescan_allowed = false;
343
Rob Clarka69ac9e2012-10-11 20:38:23 -0500344 drm_object_attach_property(&connector->base,
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000345 dev->mode_config.scaling_mode_property,
346 DRM_MODE_SCALE_FULLSCREEN);
Alan Cox6a227d52011-11-03 18:22:37 +0000347
348 switch (reg) {
349 case SDVOB:
350 ddc_bus = GPIOE;
Zhao Yakuid6676092012-08-08 13:53:48 +0000351 psb_intel_encoder->ddi_select = DDI0_SELECT;
Alan Cox6a227d52011-11-03 18:22:37 +0000352 break;
353 case SDVOC:
354 ddc_bus = GPIOD;
Zhao Yakuid6676092012-08-08 13:53:48 +0000355 psb_intel_encoder->ddi_select = DDI1_SELECT;
Alan Cox6a227d52011-11-03 18:22:37 +0000356 break;
357 default:
358 DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
359 goto failed_ddc;
360 break;
361 }
362
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000363 psb_intel_encoder->i2c_bus = psb_intel_i2c_create(dev,
Alan Cox6a227d52011-11-03 18:22:37 +0000364 ddc_bus, (reg == SDVOB) ? "HDMIB" : "HDMIC");
365
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000366 if (!psb_intel_encoder->i2c_bus) {
Alan Cox6a227d52011-11-03 18:22:37 +0000367 dev_err(dev->dev, "No ddc adapter available!\n");
368 goto failed_ddc;
369 }
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000370
371 hdmi_priv->hdmi_i2c_adapter =
372 &(psb_intel_encoder->i2c_bus->adapter);
Alan Cox6a227d52011-11-03 18:22:37 +0000373 hdmi_priv->dev = dev;
374 drm_sysfs_connector_add(connector);
375 return;
376
377failed_ddc:
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000378 drm_encoder_cleanup(encoder);
379 drm_connector_cleanup(connector);
380err_priv:
381 kfree(psb_intel_connector);
382err_connector:
383 kfree(psb_intel_encoder);
Alan Cox6a227d52011-11-03 18:22:37 +0000384}