xref: /linux/drivers/gpu/drm/gma500/cdv_intel_hdmi.c (revision 564eb714f5f09ac733c26860d5f0831f213fbdf1)
1 /*
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"
37 #include "cdv_device.h"
38 #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 
50 struct 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 
62 static 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;
67 	struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
68 	struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
69 	u32 hdmib;
70 	struct drm_crtc *crtc = encoder->crtc;
71 	struct gma_crtc *gma_crtc = to_gma_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 (gma_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 
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)
95 {
96 	return true;
97 }
98 
99 static void cdv_hdmi_dpms(struct drm_encoder *encoder, int mode)
100 {
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;
104 	u32 hdmib;
105 
106 	hdmib = REG_READ(hdmi_priv->hdmi_reg);
107 
108 	if (mode != DRM_MODE_DPMS_ON)
109 		REG_WRITE(hdmi_priv->hdmi_reg, hdmib & ~HDMIB_PORT_EN);
110 	else
111 		REG_WRITE(hdmi_priv->hdmi_reg, hdmib | HDMIB_PORT_EN);
112 	REG_READ(hdmi_priv->hdmi_reg);
113 }
114 
115 static void cdv_hdmi_save(struct drm_connector *connector)
116 {
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;
120 
121 	hdmi_priv->save_HDMIB = REG_READ(hdmi_priv->hdmi_reg);
122 }
123 
124 static void cdv_hdmi_restore(struct drm_connector *connector)
125 {
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;
129 
130 	REG_WRITE(hdmi_priv->hdmi_reg, hdmi_priv->save_HDMIB);
131 	REG_READ(hdmi_priv->hdmi_reg);
132 }
133 
134 static enum drm_connector_status cdv_hdmi_detect(
135 				struct drm_connector *connector, bool force)
136 {
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;
141 
142 	edid = drm_get_edid(connector, &gma_encoder->i2c_bus->adapter);
143 
144 	hdmi_priv->has_hdmi_sink = false;
145 	hdmi_priv->has_hdmi_audio = false;
146 	if (edid) {
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);
153 		}
154 		kfree(edid);
155 	}
156 	return status;
157 }
158 
159 static int cdv_hdmi_set_property(struct drm_connector *connector,
160 				       struct drm_property *property,
161 				       uint64_t value)
162 {
163 	struct drm_encoder *encoder = connector->encoder;
164 
165 	if (!strcmp(property->name, "scaling mode") && encoder) {
166 		struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
167 		bool centre;
168 		uint64_t curValue;
169 
170 		if (!crtc)
171 			return -1;
172 
173 		switch (value) {
174 		case DRM_MODE_SCALE_FULLSCREEN:
175 			break;
176 		case DRM_MODE_SCALE_NO_SCALE:
177 			break;
178 		case DRM_MODE_SCALE_ASPECT:
179 			break;
180 		default:
181 			return -1;
182 		}
183 
184 		if (drm_object_property_get_value(&connector->base,
185 							property, &curValue))
186 			return -1;
187 
188 		if (curValue == value)
189 			return 0;
190 
191 		if (drm_object_property_set_value(&connector->base,
192 							property, value))
193 			return -1;
194 
195 		centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
196 			(value == DRM_MODE_SCALE_NO_SCALE);
197 
198 		if (crtc->saved_mode.hdisplay != 0 &&
199 		    crtc->saved_mode.vdisplay != 0) {
200 			if (centre) {
201 				if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
202 					    encoder->crtc->x, encoder->crtc->y, encoder->crtc->fb))
203 					return -1;
204 			} else {
205 				struct drm_encoder_helper_funcs *helpers
206 						    = encoder->helper_private;
207 				helpers->mode_set(encoder, &crtc->saved_mode,
208 					     &crtc->saved_adjusted_mode);
209 			}
210 		}
211 	}
212 	return 0;
213 }
214 
215 /*
216  * Return the list of HDMI DDC modes if available.
217  */
218 static int cdv_hdmi_get_modes(struct drm_connector *connector)
219 {
220 	struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
221 	struct edid *edid = NULL;
222 	int ret = 0;
223 
224 	edid = drm_get_edid(connector, &gma_encoder->i2c_bus->adapter);
225 	if (edid) {
226 		drm_mode_connector_update_edid_property(connector, edid);
227 		ret = drm_add_edid_modes(connector, edid);
228 		kfree(edid);
229 	}
230 	return ret;
231 }
232 
233 static int cdv_hdmi_mode_valid(struct drm_connector *connector,
234 				 struct drm_display_mode *mode)
235 {
236 	if (mode->clock > 165000)
237 		return MODE_CLOCK_HIGH;
238 	if (mode->clock < 20000)
239 		return MODE_CLOCK_HIGH;
240 
241 	/* just in case */
242 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
243 		return MODE_NO_DBLESCAN;
244 
245 	/* just in case */
246 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
247 		return MODE_NO_INTERLACE;
248 
249 	return MODE_OK;
250 }
251 
252 static void cdv_hdmi_destroy(struct drm_connector *connector)
253 {
254 	struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
255 
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);
260 	kfree(connector);
261 }
262 
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,
269 };
270 
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,
276 };
277 
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,
286 };
287 
288 void cdv_hdmi_init(struct drm_device *dev,
289 			struct psb_intel_mode_device *mode_dev, int reg)
290 {
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;
296 	int ddc_bus;
297 
298 	gma_encoder = kzalloc(sizeof(struct gma_encoder), GFP_KERNEL);
299 
300 	if (!gma_encoder)
301 		return;
302 
303 	gma_connector = kzalloc(sizeof(struct gma_connector),
304 				      GFP_KERNEL);
305 
306 	if (!gma_connector)
307 		goto err_connector;
308 
309 	hdmi_priv = kzalloc(sizeof(struct mid_intel_hdmi_priv), GFP_KERNEL);
310 
311 	if (!hdmi_priv)
312 		goto err_priv;
313 
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);
320 
321 	drm_encoder_init(dev, encoder, &psb_intel_lvds_enc_funcs,
322 			 DRM_MODE_ENCODER_TMDS);
323 
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;
329 
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;
336 
337 	drm_object_attach_property(&connector->base,
338 				      dev->mode_config.scaling_mode_property,
339 				      DRM_MODE_SCALE_FULLSCREEN);
340 
341 	switch (reg) {
342 	case SDVOB:
343 		ddc_bus = GPIOE;
344 		gma_encoder->ddi_select = DDI0_SELECT;
345 		break;
346 	case SDVOC:
347 		ddc_bus = GPIOD;
348 		gma_encoder->ddi_select = DDI1_SELECT;
349 		break;
350 	default:
351 		DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
352 		goto failed_ddc;
353 		break;
354 	}
355 
356 	gma_encoder->i2c_bus = psb_intel_i2c_create(dev,
357 				ddc_bus, (reg == SDVOB) ? "HDMIB" : "HDMIC");
358 
359 	if (!gma_encoder->i2c_bus) {
360 		dev_err(dev->dev, "No ddc adapter available!\n");
361 		goto failed_ddc;
362 	}
363 
364 	hdmi_priv->hdmi_i2c_adapter = &(gma_encoder->i2c_bus->adapter);
365 	hdmi_priv->dev = dev;
366 	drm_sysfs_connector_add(connector);
367 	return;
368 
369 failed_ddc:
370 	drm_encoder_cleanup(encoder);
371 	drm_connector_cleanup(connector);
372 err_priv:
373 	kfree(gma_connector);
374 err_connector:
375 	kfree(gma_encoder);
376 }
377