exynos_drm_dpi.c 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Exynos DRM Parallel output support.
 *
 * Copyright (c) 2014 Samsung Electronics Co., Ltd
 *
 * Contacts: Andrzej Hajda <a.hajda@samsung.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.
*/

#include <drm/drmP.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_panel.h>
16
#include <drm/drm_atomic_helper.h>
17

18
#include <linux/of_graph.h>
19 20 21 22 23
#include <linux/regulator/consumer.h>

#include <video/of_videomode.h>
#include <video/videomode.h>

24
#include "exynos_drm_crtc.h"
25 26

struct exynos_dpi {
27
	struct drm_encoder encoder;
28 29 30 31 32 33 34 35 36 37 38
	struct device *dev;
	struct device_node *panel_node;

	struct drm_panel *panel;
	struct drm_connector connector;

	struct videomode *vm;
};

#define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)

39
static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e)
40
{
41
	return container_of(e, struct exynos_dpi, encoder);
42 43
}

44 45 46 47 48
static enum drm_connector_status
exynos_dpi_detect(struct drm_connector *connector, bool force)
{
	struct exynos_dpi *ctx = connector_to_dpi(connector);

49
	if (ctx->panel && !ctx->panel->connector)
50
		drm_panel_attach(ctx->panel, &ctx->connector);
51

52
	return connector_status_connected;
53 54 55 56
}

static void exynos_dpi_connector_destroy(struct drm_connector *connector)
{
57
	drm_connector_unregister(connector);
58 59 60
	drm_connector_cleanup(connector);
}

61
static const struct drm_connector_funcs exynos_dpi_connector_funcs = {
62 63 64
	.detect = exynos_dpi_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = exynos_dpi_connector_destroy,
65 66 67
	.reset = drm_atomic_helper_connector_reset,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
};

static int exynos_dpi_get_modes(struct drm_connector *connector)
{
	struct exynos_dpi *ctx = connector_to_dpi(connector);

	/* fimd timings gets precedence over panel modes */
	if (ctx->vm) {
		struct drm_display_mode *mode;

		mode = drm_mode_create(connector->dev);
		if (!mode) {
			DRM_ERROR("failed to create a new display mode\n");
			return 0;
		}
		drm_display_mode_from_videomode(ctx->vm, mode);
		mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
		drm_mode_probed_add(connector, mode);
		return 1;
	}

	if (ctx->panel)
		return ctx->panel->funcs->get_modes(ctx->panel);

	return 0;
}

95
static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
96 97 98
	.get_modes = exynos_dpi_get_modes,
};

99
static int exynos_dpi_create_connector(struct drm_encoder *encoder)
100
{
101
	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
102 103 104
	struct drm_connector *connector = &ctx->connector;
	int ret;

105
	connector->polled = DRM_CONNECTOR_POLL_HPD;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

	ret = drm_connector_init(encoder->dev, connector,
				 &exynos_dpi_connector_funcs,
				 DRM_MODE_CONNECTOR_VGA);
	if (ret) {
		DRM_ERROR("failed to initialize connector with drm\n");
		return ret;
	}

	drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
	drm_mode_connector_attach_encoder(connector, encoder);

	return 0;
}

121 122 123 124 125 126 127
static void exynos_dpi_mode_set(struct drm_encoder *encoder,
				struct drm_display_mode *mode,
				struct drm_display_mode *adjusted_mode)
{
}

static void exynos_dpi_enable(struct drm_encoder *encoder)
128
{
129
	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
130

131 132
	if (ctx->panel) {
		drm_panel_prepare(ctx->panel);
133
		drm_panel_enable(ctx->panel);
134
	}
135 136
}

137
static void exynos_dpi_disable(struct drm_encoder *encoder)
138
{
139
	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
140

141
	if (ctx->panel) {
142
		drm_panel_disable(ctx->panel);
143 144
		drm_panel_unprepare(ctx->panel);
	}
145 146
}

147
static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = {
148
	.mode_set = exynos_dpi_mode_set,
149 150
	.enable = exynos_dpi_enable,
	.disable = exynos_dpi_disable,
151 152
};

153
static const struct drm_encoder_funcs exynos_dpi_encoder_funcs = {
154 155 156
	.destroy = drm_encoder_cleanup,
};

157 158 159 160 161 162 163 164 165 166 167 168 169 170
enum {
	FIMD_PORT_IN0,
	FIMD_PORT_IN1,
	FIMD_PORT_IN2,
	FIMD_PORT_RGB,
	FIMD_PORT_WRB,
};

static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
{
	struct device *dev = ctx->dev;
	struct device_node *dn = dev->of_node;
	struct device_node *np;

171
	ctx->panel_node = of_graph_get_remote_node(dn, FIMD_PORT_RGB, 0);
172 173 174 175 176 177 178 179 180 181 182 183 184

	np = of_get_child_by_name(dn, "display-timings");
	if (np) {
		struct videomode *vm;
		int ret;

		of_node_put(np);

		vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
		if (!vm)
			return -ENOMEM;

		ret = of_get_videomode(dn, vm, 0);
185 186
		if (ret < 0) {
			devm_kfree(dev, vm);
187
			return ret;
188
		}
189 190 191 192 193 194 195 196 197 198 199 200

		ctx->vm = vm;

		return 0;
	}

	if (!ctx->panel_node)
		return -EINVAL;

	return 0;
}

201
int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
202 203 204
{
	int ret;

205
	drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs,
206
			 DRM_MODE_ENCODER_TMDS, NULL);
207 208 209

	drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);

210 211 212 213
	ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_LCD);
	if (ret < 0)
		return ret;

214
	ret = exynos_dpi_create_connector(encoder);
215 216
	if (ret) {
		DRM_ERROR("failed to create connector ret = %d\n", ret);
217
		drm_encoder_cleanup(encoder);
218 219 220 221 222 223
		return ret;
	}

	return 0;
}

224
struct drm_encoder *exynos_dpi_probe(struct device *dev)
225 226 227 228 229 230
{
	struct exynos_dpi *ctx;
	int ret;

	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
231
		return ERR_PTR(-ENOMEM);
232 233 234 235

	ctx->dev = dev;

	ret = exynos_dpi_parse_dt(ctx);
236 237
	if (ret < 0) {
		devm_kfree(dev, ctx);
238
		return NULL;
239 240 241 242
	}

	if (ctx->panel_node) {
		ctx->panel = of_drm_find_panel(ctx->panel_node);
243 244
		if (IS_ERR(ctx->panel))
			return ERR_CAST(ctx->panel);
245
	}
246

247
	return &ctx->encoder;
248 249
}

250
int exynos_dpi_remove(struct drm_encoder *encoder)
251
{
252
	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
253

254
	exynos_dpi_disable(&ctx->encoder);
255 256 257

	if (ctx->panel)
		drm_panel_detach(ctx->panel);
258 259 260

	return 0;
}