parallel-display.c 8.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * i.MX drm driver - parallel display implementation
 *
 * Copyright (C) 2012 Sascha Hauer, Pengutronix
 *
 * 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.
 * 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.
 */

16
#include <linux/component.h>
17 18 19 20
#include <linux/module.h>
#include <drm/drmP.h>
#include <drm/drm_fb_helper.h>
#include <drm/drm_crtc_helper.h>
21
#include <drm/drm_panel.h>
22
#include <linux/videodev2.h>
23
#include <video/of_display_timing.h>
24
#include <linux/of_graph.h>
25 26 27 28 29 30 31 32 33 34 35 36

#include "imx-drm.h"

#define con_to_imxpd(x) container_of(x, struct imx_parallel_display, connector)
#define enc_to_imxpd(x) container_of(x, struct imx_parallel_display, encoder)

struct imx_parallel_display {
	struct drm_connector connector;
	struct drm_encoder encoder;
	struct device *dev;
	void *edid;
	int edid_len;
37
	u32 bus_format;
38 39
	int mode_valid;
	struct drm_display_mode mode;
40
	struct drm_panel *panel;
41 42 43 44 45 46 47 48 49 50 51
};

static enum drm_connector_status imx_pd_connector_detect(
		struct drm_connector *connector, bool force)
{
	return connector_status_connected;
}

static int imx_pd_connector_get_modes(struct drm_connector *connector)
{
	struct imx_parallel_display *imxpd = con_to_imxpd(connector);
52
	struct device_node *np = imxpd->dev->of_node;
53 54
	int num_modes = 0;

55 56
	if (imxpd->panel && imxpd->panel->funcs &&
	    imxpd->panel->funcs->get_modes) {
57 58
		struct drm_display_info *di = &connector->display_info;

59
		num_modes = imxpd->panel->funcs->get_modes(imxpd->panel);
60 61
		if (!imxpd->bus_format && di->num_bus_formats)
			imxpd->bus_format = di->bus_formats[0];
62 63 64 65
		if (num_modes > 0)
			return num_modes;
	}

66 67 68 69 70 71 72
	if (imxpd->edid) {
		drm_mode_connector_update_edid_property(connector, imxpd->edid);
		num_modes = drm_add_edid_modes(connector, imxpd->edid);
	}

	if (imxpd->mode_valid) {
		struct drm_display_mode *mode = drm_mode_create(connector->dev);
73

74 75
		if (!mode)
			return -EINVAL;
76 77
		drm_mode_copy(mode, &imxpd->mode);
		mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
78 79 80 81 82 83
		drm_mode_probed_add(connector, mode);
		num_modes++;
	}

	if (np) {
		struct drm_display_mode *mode = drm_mode_create(connector->dev);
84

85 86
		if (!mode)
			return -EINVAL;
87
		of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
88 89
		drm_mode_copy(mode, &imxpd->mode);
		mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		drm_mode_probed_add(connector, mode);
		num_modes++;
	}

	return num_modes;
}

static struct drm_encoder *imx_pd_connector_best_encoder(
		struct drm_connector *connector)
{
	struct imx_parallel_display *imxpd = con_to_imxpd(connector);

	return &imxpd->encoder;
}

static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode)
{
107 108 109 110 111 112
	struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);

	if (mode != DRM_MODE_DPMS_ON)
		drm_panel_disable(imxpd->panel);
	else
		drm_panel_enable(imxpd->panel);
113 114 115 116 117 118 119 120 121 122 123 124 125
}

static bool imx_pd_encoder_mode_fixup(struct drm_encoder *encoder,
			   const struct drm_display_mode *mode,
			   struct drm_display_mode *adjusted_mode)
{
	return true;
}

static void imx_pd_encoder_prepare(struct drm_encoder *encoder)
{
	struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);

126
	imx_drm_set_bus_format(encoder, imxpd->bus_format);
127 128 129 130
}

static void imx_pd_encoder_commit(struct drm_encoder *encoder)
{
131 132 133 134
	struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);

	drm_panel_prepare(imxpd->panel);
	drm_panel_enable(imxpd->panel);
135 136 137
}

static void imx_pd_encoder_mode_set(struct drm_encoder *encoder,
138 139
			 struct drm_display_mode *orig_mode,
			 struct drm_display_mode *mode)
140 141 142 143 144
{
}

static void imx_pd_encoder_disable(struct drm_encoder *encoder)
{
145 146 147 148
	struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);

	drm_panel_disable(imxpd->panel);
	drm_panel_unprepare(imxpd->panel);
149 150 151 152 153 154
}

static struct drm_connector_funcs imx_pd_connector_funcs = {
	.dpms = drm_helper_connector_dpms,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.detect = imx_pd_connector_detect,
155
	.destroy = imx_drm_connector_destroy,
156 157 158 159 160 161 162 163
};

static struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
	.get_modes = imx_pd_connector_get_modes,
	.best_encoder = imx_pd_connector_best_encoder,
};

static struct drm_encoder_funcs imx_pd_encoder_funcs = {
164
	.destroy = imx_drm_encoder_destroy,
165 166 167 168 169 170 171 172 173 174 175
};

static struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
	.dpms = imx_pd_encoder_dpms,
	.mode_fixup = imx_pd_encoder_mode_fixup,
	.prepare = imx_pd_encoder_prepare,
	.commit = imx_pd_encoder_commit,
	.mode_set = imx_pd_encoder_mode_set,
	.disable = imx_pd_encoder_disable,
};

176 177
static int imx_pd_register(struct drm_device *drm,
	struct imx_parallel_display *imxpd)
178 179 180
{
	int ret;

181 182 183 184
	ret = imx_drm_encoder_parse_of(drm, &imxpd->encoder,
				       imxpd->dev->of_node);
	if (ret)
		return ret;
185

186 187 188 189 190 191 192
	/* set the connector's dpms to OFF so that
	 * drm_helper_connector_dpms() won't return
	 * immediately since the current state is ON
	 * at this point.
	 */
	imxpd->connector.dpms = DRM_MODE_DPMS_OFF;

193
	drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs);
194
	drm_encoder_init(drm, &imxpd->encoder, &imx_pd_encoder_funcs,
195
			 DRM_MODE_ENCODER_NONE, NULL);
196 197 198

	drm_connector_helper_add(&imxpd->connector,
			&imx_pd_connector_helper_funcs);
199 200
	drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
			   DRM_MODE_CONNECTOR_VGA);
201

202 203 204
	if (imxpd->panel)
		drm_panel_attach(imxpd->panel, &imxpd->connector);

205
	drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder);
206 207 208 209 210 211

	imxpd->connector.encoder = &imxpd->encoder;

	return 0;
}

212
static int imx_pd_bind(struct device *dev, struct device *master, void *data)
213
{
214
	struct drm_device *drm = data;
215
	struct device_node *np = dev->of_node;
216
	struct device_node *port;
217 218 219 220 221
	const u8 *edidp;
	struct imx_parallel_display *imxpd;
	int ret;
	const char *fmt;

222
	imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
223 224 225 226 227 228 229 230 231 232
	if (!imxpd)
		return -ENOMEM;

	edidp = of_get_property(np, "edid", &imxpd->edid_len);
	if (edidp)
		imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);

	ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
	if (!ret) {
		if (!strcmp(fmt, "rgb24"))
233
			imxpd->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
234
		else if (!strcmp(fmt, "rgb565"))
235
			imxpd->bus_format = MEDIA_BUS_FMT_RGB565_1X16;
236
		else if (!strcmp(fmt, "bgr666"))
237
			imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
238
		else if (!strcmp(fmt, "lvds666"))
239
			imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
240 241
	}

242 243 244 245 246 247 248 249 250 251 252 253 254
	/* port@1 is the output port */
	port = of_graph_get_port_by_id(np, 1);
	if (port) {
		struct device_node *endpoint, *remote;

		endpoint = of_get_child_by_name(port, "endpoint");
		if (endpoint) {
			remote = of_graph_get_remote_port_parent(endpoint);
			if (remote)
				imxpd->panel = of_drm_find_panel(remote);
			if (!imxpd->panel)
				return -EPROBE_DEFER;
		}
255
	}
256

257
	imxpd->dev = dev;
258

259
	ret = imx_pd_register(drm, imxpd);
260 261 262
	if (ret)
		return ret;

263
	dev_set_drvdata(dev, imxpd);
264 265 266 267

	return 0;
}

268 269
static void imx_pd_unbind(struct device *dev, struct device *master,
	void *data)
270
{
271
	struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
272

273 274
	imxpd->encoder.funcs->destroy(&imxpd->encoder);
	imxpd->connector.funcs->destroy(&imxpd->connector);
275 276

	kfree(imxpd->edid);
277
}
278

279 280 281 282 283 284 285 286 287 288 289 290 291
static const struct component_ops imx_pd_ops = {
	.bind	= imx_pd_bind,
	.unbind	= imx_pd_unbind,
};

static int imx_pd_probe(struct platform_device *pdev)
{
	return component_add(&pdev->dev, &imx_pd_ops);
}

static int imx_pd_remove(struct platform_device *pdev)
{
	component_del(&pdev->dev, &imx_pd_ops);
292

293 294 295 296 297 298 299
	return 0;
}

static const struct of_device_id imx_pd_dt_ids[] = {
	{ .compatible = "fsl,imx-parallel-display", },
	{ /* sentinel */ }
};
300
MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
301 302 303

static struct platform_driver imx_pd_driver = {
	.probe		= imx_pd_probe,
304
	.remove		= imx_pd_remove,
305 306 307 308 309 310 311 312 313 314 315
	.driver		= {
		.of_match_table = imx_pd_dt_ids,
		.name	= "imx-parallel-display",
	},
};

module_platform_driver(imx_pd_driver);

MODULE_DESCRIPTION("i.MX parallel display driver");
MODULE_AUTHOR("Sascha Hauer, Pengutronix");
MODULE_LICENSE("GPL");
316
MODULE_ALIAS("platform:imx-parallel-display");