omap_connector.c 9.1 KB
Newer Older
1
/*
R
Rob Clark 已提交
2
 * drivers/gpu/drm/omapdrm/omap_connector.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Copyright (C) 2011 Texas Instruments
 * Author: Rob Clark <rob@ti.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
#include <drm/drm_atomic_helper.h>
21 22
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
23

24
#include "omap_drv.h"
25 26 27 28 29 30 31 32 33 34

/*
 * connector funcs
 */

#define to_omap_connector(x) container_of(x, struct omap_connector, base)

struct omap_connector {
	struct drm_connector base;
	struct omap_dss_device *dssdev;
35
	bool hdmi_mode;
36 37
};

38 39 40 41 42 43 44
bool omap_connector_get_hdmi_mode(struct drm_connector *connector)
{
	struct omap_connector *omap_connector = to_omap_connector(connector);

	return omap_connector->hdmi_mode;
}

45
void copy_timings_omap_to_drm(struct drm_display_mode *mode,
46 47
		struct omap_video_timings *timings)
{
48
	mode->clock = timings->pixelclock / 1000;
49

50
	mode->hdisplay = timings->hactive;
51
	mode->hsync_start = mode->hdisplay + timings->hfront_porch;
52
	mode->hsync_end = mode->hsync_start + timings->hsync_len;
53
	mode->htotal = mode->hsync_end + timings->hback_porch;
54

55
	mode->vdisplay = timings->vactive;
56
	mode->vsync_start = mode->vdisplay + timings->vfront_porch;
57
	mode->vsync_end = mode->vsync_start + timings->vsync_len;
58
	mode->vtotal = mode->vsync_end + timings->vback_porch;
59

R
Rob Clark 已提交
60 61
	mode->flags = 0;

62
	if (timings->flags & DISPLAY_FLAGS_INTERLACED)
R
Rob Clark 已提交
63
		mode->flags |= DRM_MODE_FLAG_INTERLACE;
64

T
Tomi Valkeinen 已提交
65 66 67
	if (timings->double_pixel)
		mode->flags |= DRM_MODE_FLAG_DBLCLK;

68 69 70 71 72 73 74 75 76
	if (timings->hsync_level == OMAPDSS_SIG_ACTIVE_HIGH)
		mode->flags |= DRM_MODE_FLAG_PHSYNC;
	else
		mode->flags |= DRM_MODE_FLAG_NHSYNC;

	if (timings->vsync_level == OMAPDSS_SIG_ACTIVE_HIGH)
		mode->flags |= DRM_MODE_FLAG_PVSYNC;
	else
		mode->flags |= DRM_MODE_FLAG_NVSYNC;
77 78
}

79
void copy_timings_drm_to_omap(struct omap_video_timings *timings,
80 81
		struct drm_display_mode *mode)
{
82
	timings->pixelclock = mode->clock * 1000;
83

84
	timings->hactive = mode->hdisplay;
85
	timings->hfront_porch = mode->hsync_start - mode->hdisplay;
86
	timings->hsync_len = mode->hsync_end - mode->hsync_start;
87
	timings->hback_porch = mode->htotal - mode->hsync_end;
88

89
	timings->vactive = mode->vdisplay;
90
	timings->vfront_porch = mode->vsync_start - mode->vdisplay;
91
	timings->vsync_len = mode->vsync_end - mode->vsync_start;
92
	timings->vback_porch = mode->vtotal - mode->vsync_end;
R
Rob Clark 已提交
93

94 95 96
	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
		timings->flags |= DISPLAY_FLAGS_INTERLACED;

T
Tomi Valkeinen 已提交
97
	timings->double_pixel = !!(mode->flags & DRM_MODE_FLAG_DBLCLK);
98 99 100 101 102 103 104 105 106 107 108 109 110

	if (mode->flags & DRM_MODE_FLAG_PHSYNC)
		timings->hsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
	else
		timings->hsync_level = OMAPDSS_SIG_ACTIVE_LOW;

	if (mode->flags & DRM_MODE_FLAG_PVSYNC)
		timings->vsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
	else
		timings->vsync_level = OMAPDSS_SIG_ACTIVE_LOW;

	timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
	timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
111
	timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE;
112 113
}

114
static enum drm_connector_status omap_connector_detect(
115 116 117 118 119 120 121 122
		struct drm_connector *connector, bool force)
{
	struct omap_connector *omap_connector = to_omap_connector(connector);
	struct omap_dss_device *dssdev = omap_connector->dssdev;
	struct omap_dss_driver *dssdrv = dssdev->driver;
	enum drm_connector_status ret;

	if (dssdrv->detect) {
123
		if (dssdrv->detect(dssdev))
124
			ret = connector_status_connected;
125
		else
126
			ret = connector_status_disconnected;
127 128 129 130 131
	} else if (dssdev->type == OMAP_DISPLAY_TYPE_DPI ||
			dssdev->type == OMAP_DISPLAY_TYPE_DBI ||
			dssdev->type == OMAP_DISPLAY_TYPE_SDI ||
			dssdev->type == OMAP_DISPLAY_TYPE_DSI) {
		ret = connector_status_connected;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	} else {
		ret = connector_status_unknown;
	}

	VERB("%s: %d (force=%d)", omap_connector->dssdev->name, ret, force);

	return ret;
}

static void omap_connector_destroy(struct drm_connector *connector)
{
	struct omap_connector *omap_connector = to_omap_connector(connector);
	struct omap_dss_device *dssdev = omap_connector->dssdev;

	DBG("%s", omap_connector->dssdev->name);
147
	drm_connector_unregister(connector);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	drm_connector_cleanup(connector);
	kfree(omap_connector);

	omap_dss_put_device(dssdev);
}

#define MAX_EDID  512

static int omap_connector_get_modes(struct drm_connector *connector)
{
	struct omap_connector *omap_connector = to_omap_connector(connector);
	struct omap_dss_device *dssdev = omap_connector->dssdev;
	struct omap_dss_driver *dssdrv = dssdev->driver;
	struct drm_device *dev = connector->dev;
	int n = 0;

	DBG("%s", omap_connector->dssdev->name);

	/* if display exposes EDID, then we parse that in the normal way to
	 * build table of supported modes.. otherwise (ie. fixed resolution
	 * LCD panels) we just return a single mode corresponding to the
	 * currently configured timings:
	 */
	if (dssdrv->read_edid) {
		void *edid = kzalloc(MAX_EDID, GFP_KERNEL);

		if ((dssdrv->read_edid(dssdev, edid, MAX_EDID) > 0) &&
				drm_edid_is_valid(edid)) {
			drm_mode_connector_update_edid_property(
					connector, edid);
			n = drm_add_edid_modes(connector, edid);
179 180 181

			omap_connector->hdmi_mode =
				drm_detect_hdmi_monitor(edid);
182 183 184 185
		} else {
			drm_mode_connector_update_edid_property(
					connector, NULL);
		}
186

187
		kfree(edid);
188 189
	} else {
		struct drm_display_mode *mode = drm_mode_create(dev);
R
Rob Clark 已提交
190
		struct omap_video_timings timings = {0};
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

		dssdrv->get_timings(dssdev, &timings);

		copy_timings_omap_to_drm(mode, &timings);

		mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
		drm_mode_set_name(mode);
		drm_mode_probed_add(connector, mode);

		n = 1;
	}

	return n;
}

static int omap_connector_mode_valid(struct drm_connector *connector,
				 struct drm_display_mode *mode)
{
	struct omap_connector *omap_connector = to_omap_connector(connector);
	struct omap_dss_device *dssdev = omap_connector->dssdev;
	struct omap_dss_driver *dssdrv = dssdev->driver;
	struct omap_video_timings timings = {0};
	struct drm_device *dev = connector->dev;
	struct drm_display_mode *new_mode;
215
	int r, ret = MODE_BAD;
216 217 218 219

	copy_timings_drm_to_omap(&timings, mode);
	mode->vrefresh = drm_mode_vrefresh(mode);

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	/*
	 * if the panel driver doesn't have a check_timings, it's most likely
	 * a fixed resolution panel, check if the timings match with the
	 * panel's timings
	 */
	if (dssdrv->check_timings) {
		r = dssdrv->check_timings(dssdev, &timings);
	} else {
		struct omap_video_timings t = {0};

		dssdrv->get_timings(dssdev, &t);

		if (memcmp(&timings, &t, sizeof(struct omap_video_timings)))
			r = -EINVAL;
		else
			r = 0;
	}

	if (!r) {
239 240
		/* check if vrefresh is still valid */
		new_mode = drm_mode_duplicate(dev, mode);
241
		new_mode->clock = timings.pixelclock / 1000;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
		new_mode->vrefresh = 0;
		if (mode->vrefresh == drm_mode_vrefresh(new_mode))
			ret = MODE_OK;
		drm_mode_destroy(dev, new_mode);
	}

	DBG("connector: mode %s: "
			"%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
			(ret == MODE_OK) ? "valid" : "invalid",
			mode->base.id, mode->name, mode->vrefresh, mode->clock,
			mode->hdisplay, mode->hsync_start,
			mode->hsync_end, mode->htotal,
			mode->vdisplay, mode->vsync_start,
			mode->vsync_end, mode->vtotal, mode->type, mode->flags);

	return ret;
}

static const struct drm_connector_funcs omap_connector_funcs = {
261
	.dpms = drm_atomic_helper_connector_dpms,
262
	.reset = drm_atomic_helper_connector_reset,
263 264 265
	.detect = omap_connector_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = omap_connector_destroy,
266 267
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
268 269 270 271 272 273 274 275 276
};

static const struct drm_connector_helper_funcs omap_connector_helper_funcs = {
	.get_modes = omap_connector_get_modes,
	.mode_valid = omap_connector_mode_valid,
};

/* initialize connector */
struct drm_connector *omap_connector_init(struct drm_device *dev,
277 278
		int connector_type, struct omap_dss_device *dssdev,
		struct drm_encoder *encoder)
279 280 281 282 283 284 285 286 287
{
	struct drm_connector *connector = NULL;
	struct omap_connector *omap_connector;

	DBG("%s", dssdev->name);

	omap_dss_get_device(dssdev);

	omap_connector = kzalloc(sizeof(struct omap_connector), GFP_KERNEL);
288
	if (!omap_connector)
289 290 291
		goto fail;

	omap_connector->dssdev = dssdev;
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	connector = &omap_connector->base;

	drm_connector_init(dev, connector, &omap_connector_funcs,
				connector_type);
	drm_connector_helper_add(connector, &omap_connector_helper_funcs);

#if 0 /* enable when dss2 supports hotplug */
	if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_HPD)
		connector->polled = 0;
	else
#endif
		connector->polled = DRM_CONNECTOR_POLL_CONNECT |
				DRM_CONNECTOR_POLL_DISCONNECT;

	connector->interlace_allowed = 1;
	connector->doublescan_allowed = 0;

310
	drm_connector_register(connector);
311 312 313 314

	return connector;

fail:
315
	if (connector)
316 317 318 319
		omap_connector_destroy(connector);

	return NULL;
}