exynos_drm_connector.c 8.4 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 * Authors:
 *	Inki Dae <inki.dae@samsung.com>
 *	Joonyoung Shim <jy0922.shim@samsung.com>
 *	Seung-Woo Kim <sw0312.kim@samsung.com>
 *
8 9 10 11
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
12 13
 */

14 15
#include <drm/drmP.h>
#include <drm/drm_crtc_helper.h>
16

17
#include <drm/exynos_drm.h>
18 19
#include "exynos_drm_drv.h"
#include "exynos_drm_encoder.h"
M
Mark Brown 已提交
20
#include "exynos_drm_connector.h"
21 22 23 24 25 26

#define to_exynos_connector(x)	container_of(x, struct exynos_drm_connector,\
				drm_connector)

struct exynos_drm_connector {
	struct drm_connector	drm_connector;
27 28
	uint32_t		encoder_id;
	struct exynos_drm_manager *manager;
29
	uint32_t		dpms;
30 31 32 33
};

static int exynos_drm_connector_get_modes(struct drm_connector *connector)
{
34 35 36
	struct exynos_drm_connector *exynos_connector =
					to_exynos_connector(connector);
	struct exynos_drm_manager *manager = exynos_connector->manager;
37
	struct exynos_drm_display_ops *display_ops = manager->display_ops;
38 39 40
	struct edid *edid = NULL;
	unsigned int count = 0;
	int ret;
41

42 43
	if (!display_ops) {
		DRM_DEBUG_KMS("display_ops is null.\n");
44 45 46 47 48 49 50 51 52 53 54
		return 0;
	}

	/*
	 * if get_edid() exists then get_edid() callback of hdmi side
	 * is called to get edid data through i2c interface else
	 * get timing from the FIMD driver(display controller).
	 *
	 * P.S. in case of lcd panel, count is always 1 if success
	 * because lcd panel has only one mode.
	 */
55
	if (display_ops->get_edid) {
56 57 58 59 60 61
		edid = display_ops->get_edid(manager->dev, connector);
		if (IS_ERR_OR_NULL(edid)) {
			ret = PTR_ERR(edid);
			edid = NULL;
			DRM_ERROR("Panel operation get_edid failed %d\n", ret);
			goto out;
62 63
		}

64
		count = drm_add_edid_modes(connector, edid);
65
		if (!count) {
66 67
			DRM_ERROR("Add edid modes failed %d\n", count);
			goto out;
68 69 70 71
		}

		drm_mode_connector_update_edid_property(connector, edid);
	} else {
72
		struct exynos_drm_panel_info *panel;
73 74 75 76 77
		struct drm_display_mode *mode = drm_mode_create(connector->dev);
		if (!mode) {
			DRM_ERROR("failed to create a new display mode.\n");
			return 0;
		}
78

79 80
		if (display_ops->get_panel)
			panel = display_ops->get_panel(manager->dev);
81 82 83 84 85
		else {
			drm_mode_destroy(connector->dev, mode);
			return 0;
		}

86 87 88
		drm_display_mode_from_videomode(&panel->vm, mode);
		mode->width_mm = panel->width_mm;
		mode->height_mm = panel->height_mm;
89 90
		connector->display_info.width_mm = mode->width_mm;
		connector->display_info.height_mm = mode->height_mm;
91 92 93 94 95 96 97 98

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

		count = 1;
	}

99 100
out:
	kfree(edid);
101 102 103 104 105 106
	return count;
}

static int exynos_drm_connector_mode_valid(struct drm_connector *connector,
					    struct drm_display_mode *mode)
{
107 108 109
	struct exynos_drm_connector *exynos_connector =
					to_exynos_connector(connector);
	struct exynos_drm_manager *manager = exynos_connector->manager;
110
	struct exynos_drm_display_ops *display_ops = manager->display_ops;
111 112 113 114
	int ret = MODE_BAD;

	DRM_DEBUG_KMS("%s\n", __FILE__);

115 116
	if (display_ops && display_ops->check_mode)
		if (!display_ops->check_mode(manager->dev, mode))
117 118 119 120 121
			ret = MODE_OK;

	return ret;
}

122
struct drm_encoder *exynos_drm_best_encoder(struct drm_connector *connector)
123
{
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	struct drm_device *dev = connector->dev;
	struct exynos_drm_connector *exynos_connector =
					to_exynos_connector(connector);
	struct drm_mode_object *obj;
	struct drm_encoder *encoder;

	obj = drm_mode_object_find(dev, exynos_connector->encoder_id,
				   DRM_MODE_OBJECT_ENCODER);
	if (!obj) {
		DRM_DEBUG_KMS("Unknown ENCODER ID %d\n",
				exynos_connector->encoder_id);
		return NULL;
	}

	encoder = obj_to_encoder(obj);

	return encoder;
141 142 143 144 145 146 147 148
}

static struct drm_connector_helper_funcs exynos_connector_helper_funcs = {
	.get_modes	= exynos_drm_connector_get_modes,
	.mode_valid	= exynos_drm_connector_mode_valid,
	.best_encoder	= exynos_drm_best_encoder,
};

149 150
void exynos_drm_display_power(struct drm_connector *connector, int mode)
{
151
	struct drm_encoder *encoder = exynos_drm_best_encoder(connector);
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 179 180 181 182 183
	struct exynos_drm_connector *exynos_connector;
	struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
	struct exynos_drm_display_ops *display_ops = manager->display_ops;

	exynos_connector = to_exynos_connector(connector);

	if (exynos_connector->dpms == mode) {
		DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
		return;
	}

	if (display_ops && display_ops->power_on)
		display_ops->power_on(manager->dev, mode);

	exynos_connector->dpms = mode;
}

static void exynos_drm_connector_dpms(struct drm_connector *connector,
					int mode)
{
	/*
	 * in case that drm_crtc_helper_set_mode() is called,
	 * encoder/crtc->funcs->dpms() will be just returned
	 * because they already were DRM_MODE_DPMS_ON so only
	 * exynos_drm_display_power() will be called.
	 */
	drm_helper_connector_dpms(connector, mode);

	exynos_drm_display_power(connector, mode);

}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
static int exynos_drm_connector_fill_modes(struct drm_connector *connector,
				unsigned int max_width, unsigned int max_height)
{
	struct exynos_drm_connector *exynos_connector =
					to_exynos_connector(connector);
	struct exynos_drm_manager *manager = exynos_connector->manager;
	struct exynos_drm_manager_ops *ops = manager->ops;
	unsigned int width, height;

	width = max_width;
	height = max_height;

	/*
	 * if specific driver want to find desired_mode using maxmum
	 * resolution then get max width and height from that driver.
	 */
	if (ops && ops->get_max_resol)
		ops->get_max_resol(manager->dev, &width, &height);

	return drm_helper_probe_single_connector_modes(connector, width,
							height);
}

207 208 209 210
/* get detection status of display device. */
static enum drm_connector_status
exynos_drm_connector_detect(struct drm_connector *connector, bool force)
{
211 212 213
	struct exynos_drm_connector *exynos_connector =
					to_exynos_connector(connector);
	struct exynos_drm_manager *manager = exynos_connector->manager;
214 215
	struct exynos_drm_display_ops *display_ops =
					manager->display_ops;
216 217
	enum drm_connector_status status = connector_status_disconnected;

218 219
	if (display_ops && display_ops->is_connected) {
		if (display_ops->is_connected(manager->dev))
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
			status = connector_status_connected;
		else
			status = connector_status_disconnected;
	}

	return status;
}

static void exynos_drm_connector_destroy(struct drm_connector *connector)
{
	struct exynos_drm_connector *exynos_connector =
		to_exynos_connector(connector);

	drm_sysfs_connector_remove(connector);
	drm_connector_cleanup(connector);
	kfree(exynos_connector);
}

static struct drm_connector_funcs exynos_connector_funcs = {
239
	.dpms		= exynos_drm_connector_dpms,
240
	.fill_modes	= exynos_drm_connector_fill_modes,
241 242 243 244 245 246 247 248 249 250 251 252 253 254
	.detect		= exynos_drm_connector_detect,
	.destroy	= exynos_drm_connector_destroy,
};

struct drm_connector *exynos_drm_connector_create(struct drm_device *dev,
						   struct drm_encoder *encoder)
{
	struct exynos_drm_connector *exynos_connector;
	struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
	struct drm_connector *connector;
	int type;
	int err;

	exynos_connector = kzalloc(sizeof(*exynos_connector), GFP_KERNEL);
255
	if (!exynos_connector)
256 257 258 259
		return NULL;

	connector = &exynos_connector->drm_connector;

260
	switch (manager->display_ops->type) {
261 262
	case EXYNOS_DISPLAY_TYPE_HDMI:
		type = DRM_MODE_CONNECTOR_HDMIA;
263 264
		connector->interlace_allowed = true;
		connector->polled = DRM_CONNECTOR_POLL_HPD;
265
		break;
266 267 268 269
	case EXYNOS_DISPLAY_TYPE_VIDI:
		type = DRM_MODE_CONNECTOR_VIRTUAL;
		connector->polled = DRM_CONNECTOR_POLL_HPD;
		break;
270 271 272 273 274 275 276 277 278 279 280 281
	default:
		type = DRM_MODE_CONNECTOR_Unknown;
		break;
	}

	drm_connector_init(dev, connector, &exynos_connector_funcs, type);
	drm_connector_helper_add(connector, &exynos_connector_helper_funcs);

	err = drm_sysfs_connector_add(connector);
	if (err)
		goto err_connector;

282 283
	exynos_connector->encoder_id = encoder->base.id;
	exynos_connector->manager = manager;
284
	exynos_connector->dpms = DRM_MODE_DPMS_OFF;
I
Inki Dae 已提交
285
	connector->dpms = DRM_MODE_DPMS_OFF;
286
	connector->encoder = encoder;
287

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	err = drm_mode_connector_attach_encoder(connector, encoder);
	if (err) {
		DRM_ERROR("failed to attach a connector to a encoder\n");
		goto err_sysfs;
	}

	DRM_DEBUG_KMS("connector has been created\n");

	return connector;

err_sysfs:
	drm_sysfs_connector_remove(connector);
err_connector:
	drm_connector_cleanup(connector);
	kfree(exynos_connector);
	return NULL;
}