parallel-display.c 7.6 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
#include <linux/module.h>
#include <drm/drmP.h>
19
#include <drm/drm_atomic_helper.h>
20 21
#include <drm/drm_fb_helper.h>
#include <drm/drm_crtc_helper.h>
22
#include <drm/drm_panel.h>
23
#include <linux/videodev2.h>
24
#include <video/of_display_timing.h>
25
#include <linux/of_graph.h>
26 27 28 29

#include "imx-drm.h"

#define con_to_imxpd(x) container_of(x, struct imx_parallel_display, connector)
30 31
#define imx_enc_to_imxpd(x)	\
		container_of(x, struct imx_parallel_display, imx_encoder)
32 33 34

struct imx_parallel_display {
	struct drm_connector connector;
35
	struct imx_drm_encoder imx_encoder;
36 37 38 39
	struct device *dev;
	void *edid;
	int edid_len;
	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 62
		if (!imxpd->imx_encoder.bus_format && di->num_bus_formats)
			imxpd->imx_encoder.bus_format = di->bus_formats[0];
		imxpd->imx_encoder.bus_flags = di->bus_flags;
63 64 65 66
		if (num_modes > 0)
			return num_modes;
	}

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

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

75 76
		if (!mode)
			return -EINVAL;
77
		of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
78 79
		drm_mode_copy(mode, &imxpd->mode);
		mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
80 81 82 83 84 85 86 87 88 89 90 91
		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);

92
	return &imxpd->imx_encoder.encoder;
93 94
}

95
static void imx_pd_encoder_enable(struct drm_encoder *encoder)
96
{
97 98
	struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
	struct imx_parallel_display *imxpd = imx_enc_to_imxpd(imx_encoder);
99 100 101

	drm_panel_prepare(imxpd->panel);
	drm_panel_enable(imxpd->panel);
102 103 104 105
}

static void imx_pd_encoder_disable(struct drm_encoder *encoder)
{
106 107
	struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
	struct imx_parallel_display *imxpd = imx_enc_to_imxpd(imx_encoder);
108 109 110

	drm_panel_disable(imxpd->panel);
	drm_panel_unprepare(imxpd->panel);
111 112
}

113
static const struct drm_connector_funcs imx_pd_connector_funcs = {
114
	.dpms = drm_atomic_helper_connector_dpms,
115 116
	.fill_modes = drm_helper_probe_single_connector_modes,
	.detect = imx_pd_connector_detect,
117
	.destroy = imx_drm_connector_destroy,
118 119 120
	.reset = drm_atomic_helper_connector_reset,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
121 122
};

123
static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
124 125 126 127
	.get_modes = imx_pd_connector_get_modes,
	.best_encoder = imx_pd_connector_best_encoder,
};

128
static const struct drm_encoder_funcs imx_pd_encoder_funcs = {
129
	.destroy = imx_drm_encoder_destroy,
130 131
};

132
static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
133
	.enable = imx_pd_encoder_enable,
134 135 136
	.disable = imx_pd_encoder_disable,
};

137 138
static int imx_pd_register(struct drm_device *drm,
	struct imx_parallel_display *imxpd)
139 140 141
{
	int ret;

142
	ret = imx_drm_encoder_parse_of(drm, &imxpd->imx_encoder.encoder,
143 144 145
				       imxpd->dev->of_node);
	if (ret)
		return ret;
146

147 148 149 150 151 152 153
	/* 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;

154 155 156 157
	drm_encoder_helper_add(&imxpd->imx_encoder.encoder,
			       &imx_pd_encoder_helper_funcs);
	drm_encoder_init(drm, &imxpd->imx_encoder.encoder,
			 &imx_pd_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL);
158 159 160

	drm_connector_helper_add(&imxpd->connector,
			&imx_pd_connector_helper_funcs);
161 162
	drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
			   DRM_MODE_CONNECTOR_VGA);
163

164 165 166
	if (imxpd->panel)
		drm_panel_attach(imxpd->panel, &imxpd->connector);

167 168
	drm_mode_connector_attach_encoder(&imxpd->connector,
					  &imxpd->imx_encoder.encoder);
169 170 171 172

	return 0;
}

173
static int imx_pd_bind(struct device *dev, struct device *master, void *data)
174
{
175
	struct drm_device *drm = data;
176
	struct device_node *np = dev->of_node;
177
	struct device_node *ep;
178 179 180
	const u8 *edidp;
	struct imx_parallel_display *imxpd;
	int ret;
181
	u32 bus_format = 0;
182 183
	const char *fmt;

184
	imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
185 186 187 188 189 190 191 192 193 194
	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"))
195
			bus_format = MEDIA_BUS_FMT_RGB888_1X24;
196
		else if (!strcmp(fmt, "rgb565"))
197
			bus_format = MEDIA_BUS_FMT_RGB565_1X16;
198
		else if (!strcmp(fmt, "bgr666"))
199
			bus_format = MEDIA_BUS_FMT_RGB666_1X18;
200
		else if (!strcmp(fmt, "lvds666"))
201
			bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
202
	}
203 204 205
	imxpd->imx_encoder.bus_format = bus_format;
	imxpd->imx_encoder.di_hsync_pin = 2;
	imxpd->imx_encoder.di_vsync_pin = 3;
206

207
	/* port@1 is the output port */
208 209 210 211 212 213 214 215 216
	ep = of_graph_get_endpoint_by_regs(np, 1, -1);
	if (ep) {
		struct device_node *remote;

		remote = of_graph_get_remote_port_parent(ep);
		of_node_put(ep);
		if (remote) {
			imxpd->panel = of_drm_find_panel(remote);
			of_node_put(remote);
217
		}
218 219
		if (!imxpd->panel)
			return -EPROBE_DEFER;
220
	}
221

222
	imxpd->dev = dev;
223

224
	ret = imx_pd_register(drm, imxpd);
225 226 227
	if (ret)
		return ret;

228
	dev_set_drvdata(dev, imxpd);
229 230 231 232

	return 0;
}

233 234
static void imx_pd_unbind(struct device *dev, struct device *master,
	void *data)
235
{
236
	struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
237

238
	imxpd->imx_encoder.encoder.funcs->destroy(&imxpd->imx_encoder.encoder);
239
	imxpd->connector.funcs->destroy(&imxpd->connector);
240 241

	kfree(imxpd->edid);
242
}
243

244 245 246 247 248 249 250 251 252 253 254 255 256
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);
257

258 259 260 261 262 263 264
	return 0;
}

static const struct of_device_id imx_pd_dt_ids[] = {
	{ .compatible = "fsl,imx-parallel-display", },
	{ /* sentinel */ }
};
265
MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
266 267 268

static struct platform_driver imx_pd_driver = {
	.probe		= imx_pd_probe,
269
	.remove		= imx_pd_remove,
270 271 272 273 274 275 276 277 278 279 280
	.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");
281
MODULE_ALIAS("platform:imx-parallel-display");