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
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;
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;
69	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,
93				  struct drm_display_mode *mode,
94				  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;
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;
105	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;
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;
122
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;
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;
132
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{
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;
147
148	edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
149
150	hdmi_priv->has_hdmi_sink = false;
151	hdmi_priv->has_hdmi_audio = false;
152	if (edid) {
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);
159		}
160
161		psb_intel_connector->base.display_info.raw_edid = NULL;
162		kfree(edid);
163	}
164	return status;
165}
166
167static int cdv_hdmi_set_property(struct drm_connector *connector,
168				       struct drm_property *property,
169				       uint64_t value)
170{
171	struct drm_encoder *encoder = connector->encoder;
172
173	if (!strcmp(property->name, "scaling mode") && encoder) {
174		struct psb_intel_crtc *crtc = to_psb_intel_crtc(encoder->crtc);
175		bool centre;
176		uint64_t curValue;
177
178		if (!crtc)
179			return -1;
180
181		switch (value) {
182		case DRM_MODE_SCALE_FULLSCREEN:
183			break;
184		case DRM_MODE_SCALE_NO_SCALE:
185			break;
186		case DRM_MODE_SCALE_ASPECT:
187			break;
188		default:
189			return -1;
190		}
191
192		if (drm_connector_property_get_value(connector,
193							property, &curValue))
194			return -1;
195
196		if (curValue == value)
197			return 0;
198
199		if (drm_connector_property_set_value(connector,
200							property, value))
201			return -1;
202
203		centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
204			(value == DRM_MODE_SCALE_NO_SCALE);
205
206		if (crtc->saved_mode.hdisplay != 0 &&
207		    crtc->saved_mode.vdisplay != 0) {
208			if (centre) {
209				if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
210					    encoder->crtc->x, encoder->crtc->y, encoder->crtc->fb))
211					return -1;
212			} else {
213				struct drm_encoder_helper_funcs *helpers
214						    = encoder->helper_private;
215				helpers->mode_set(encoder, &crtc->saved_mode,
216					     &crtc->saved_adjusted_mode);
217			}
218		}
219	}
220	return 0;
221}
222
223/*
224 * Return the list of HDMI DDC modes if available.
225 */
226static int cdv_hdmi_get_modes(struct drm_connector *connector)
227{
228	struct psb_intel_encoder *psb_intel_encoder =
229					psb_intel_attached_encoder(connector);
230	struct edid *edid = NULL;
231	int ret = 0;
232
233	edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
234	if (edid) {
235		drm_mode_connector_update_edid_property(connector, edid);
236		ret = drm_add_edid_modes(connector, edid);
237		kfree(edid);
238	}
239	return ret;
240}
241
242static int cdv_hdmi_mode_valid(struct drm_connector *connector,
243				 struct drm_display_mode *mode)
244{
245	struct drm_psb_private *dev_priv = connector->dev->dev_private;
246
247	if (mode->clock > 165000)
248		return MODE_CLOCK_HIGH;
249	if (mode->clock < 20000)
250		return MODE_CLOCK_HIGH;
251
252	/* just in case */
253	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
254		return MODE_NO_DBLESCAN;
255
256	/* just in case */
257	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
258		return MODE_NO_INTERLACE;
259
260	/* We assume worst case scenario of 32 bpp here, since we don't know */
261	if ((ALIGN(mode->hdisplay * 4, 64) * mode->vdisplay) >
262	    dev_priv->vram_stolen_size)
263		return MODE_MEM;
264
265	return MODE_OK;
266}
267
268static void cdv_hdmi_destroy(struct drm_connector *connector)
269{
270	struct psb_intel_encoder *psb_intel_encoder =
271					psb_intel_attached_encoder(connector);
272
273	if (psb_intel_encoder->i2c_bus)
274		psb_intel_i2c_destroy(psb_intel_encoder->i2c_bus);
275	drm_sysfs_connector_remove(connector);
276	drm_connector_cleanup(connector);
277	kfree(connector);
278}
279
280static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs = {
281	.dpms = cdv_hdmi_dpms,
282	.mode_fixup = cdv_hdmi_mode_fixup,
283	.prepare = psb_intel_encoder_prepare,
284	.mode_set = cdv_hdmi_mode_set,
285	.commit = psb_intel_encoder_commit,
286};
287
288static const struct drm_connector_helper_funcs
289					cdv_hdmi_connector_helper_funcs = {
290	.get_modes = cdv_hdmi_get_modes,
291	.mode_valid = cdv_hdmi_mode_valid,
292	.best_encoder = psb_intel_best_encoder,
293};
294
295static const struct drm_connector_funcs cdv_hdmi_connector_funcs = {
296	.dpms = drm_helper_connector_dpms,
297	.save = cdv_hdmi_save,
298	.restore = cdv_hdmi_restore,
299	.detect = cdv_hdmi_detect,
300	.fill_modes = drm_helper_probe_single_connector_modes,
301	.set_property = cdv_hdmi_set_property,
302	.destroy = cdv_hdmi_destroy,
303};
304
305void cdv_hdmi_init(struct drm_device *dev,
306			struct psb_intel_mode_device *mode_dev, int reg)
307{
308	struct psb_intel_encoder *psb_intel_encoder;
309	struct psb_intel_connector *psb_intel_connector;
310	struct drm_connector *connector;
311	struct drm_encoder *encoder;
312	struct mid_intel_hdmi_priv *hdmi_priv;
313	int ddc_bus;
314
315	psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder),
316				    GFP_KERNEL);
317
318	if (!psb_intel_encoder)
319		return;
320
321	psb_intel_connector = kzalloc(sizeof(struct psb_intel_connector),
322				      GFP_KERNEL);
323
324	if (!psb_intel_connector)
325		goto err_connector;
326
327	hdmi_priv = kzalloc(sizeof(struct mid_intel_hdmi_priv), GFP_KERNEL);
328
329	if (!hdmi_priv)
330		goto err_priv;
331
332	connector = &psb_intel_connector->base;
333	encoder = &psb_intel_encoder->base;
334	drm_connector_init(dev, connector,
335			   &cdv_hdmi_connector_funcs,
336			   DRM_MODE_CONNECTOR_DVID);
337
338	drm_encoder_init(dev, encoder, &psb_intel_lvds_enc_funcs,
339			 DRM_MODE_ENCODER_TMDS);
340
341	psb_intel_connector_attach_encoder(psb_intel_connector,
342					   psb_intel_encoder);
343	psb_intel_encoder->type = INTEL_OUTPUT_HDMI;
344	hdmi_priv->hdmi_reg = reg;
345	hdmi_priv->has_hdmi_sink = false;
346	psb_intel_encoder->dev_priv = hdmi_priv;
347
348	drm_encoder_helper_add(encoder, &cdv_hdmi_helper_funcs);
349	drm_connector_helper_add(connector,
350				 &cdv_hdmi_connector_helper_funcs);
351	connector->display_info.subpixel_order = SubPixelHorizontalRGB;
352	connector->interlace_allowed = false;
353	connector->doublescan_allowed = false;
354
355	drm_connector_attach_property(connector,
356				      dev->mode_config.scaling_mode_property,
357				      DRM_MODE_SCALE_FULLSCREEN);
358
359	switch (reg) {
360	case SDVOB:
361		ddc_bus = GPIOE;
362		break;
363	case SDVOC:
364		ddc_bus = GPIOD;
365		break;
366	default:
367		DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
368		goto failed_ddc;
369		break;
370	}
371
372	psb_intel_encoder->i2c_bus = psb_intel_i2c_create(dev,
373				ddc_bus, (reg == SDVOB) ? "HDMIB" : "HDMIC");
374
375	if (!psb_intel_encoder->i2c_bus) {
376		dev_err(dev->dev, "No ddc adapter available!\n");
377		goto failed_ddc;
378	}
379
380	hdmi_priv->hdmi_i2c_adapter =
381				&(psb_intel_encoder->i2c_bus->adapter);
382	hdmi_priv->dev = dev;
383	drm_sysfs_connector_add(connector);
384	return;
385
386failed_ddc:
387	drm_encoder_cleanup(encoder);
388	drm_connector_cleanup(connector);
389err_priv:
390	kfree(psb_intel_connector);
391err_connector:
392	kfree(psb_intel_encoder);
393}
394