parallel-display.c 8.8 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 30

#include "imx-drm.h"

struct imx_parallel_display {
	struct drm_connector connector;
31
	struct drm_encoder encoder;
32 33 34
	struct device *dev;
	void *edid;
	int edid_len;
35
	u32 bus_format;
36
	u32 bus_flags;
37
	struct drm_display_mode mode;
38
	struct drm_panel *panel;
39
	struct drm_bridge *bridge;
40 41
};

42 43 44 45 46
static inline struct imx_parallel_display *con_to_imxpd(struct drm_connector *c)
{
	return container_of(c, struct imx_parallel_display, connector);
}

47 48 49 50 51
static inline struct imx_parallel_display *enc_to_imxpd(struct drm_encoder *e)
{
	return container_of(e, struct imx_parallel_display, encoder);
}

52 53 54 55 56 57 58 59 60
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);
61
	struct device_node *np = imxpd->dev->of_node;
62 63
	int num_modes = 0;

64 65 66 67 68 69 70
	if (imxpd->panel && imxpd->panel->funcs &&
	    imxpd->panel->funcs->get_modes) {
		num_modes = imxpd->panel->funcs->get_modes(imxpd->panel);
		if (num_modes > 0)
			return num_modes;
	}

71 72 73 74 75
	if (imxpd->edid) {
		drm_mode_connector_update_edid_property(connector, imxpd->edid);
		num_modes = drm_add_edid_modes(connector, imxpd->edid);
	}

76 77
	if (np) {
		struct drm_display_mode *mode = drm_mode_create(connector->dev);
78
		int ret;
79

80 81
		if (!mode)
			return -EINVAL;
82 83

		ret = of_get_drm_display_mode(np, &imxpd->mode,
84
					      &imxpd->bus_flags,
85 86 87 88
					      OF_USE_NATIVE_MODE);
		if (ret)
			return ret;

89 90
		drm_mode_copy(mode, &imxpd->mode);
		mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
91 92 93 94 95 96 97 98 99 100 101 102
		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);

103
	return &imxpd->encoder;
104 105
}

106
static void imx_pd_encoder_enable(struct drm_encoder *encoder)
107
{
108
	struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
109 110 111

	drm_panel_prepare(imxpd->panel);
	drm_panel_enable(imxpd->panel);
112 113 114 115
}

static void imx_pd_encoder_disable(struct drm_encoder *encoder)
{
116
	struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
117 118 119

	drm_panel_disable(imxpd->panel);
	drm_panel_unprepare(imxpd->panel);
120 121
}

122 123 124 125 126 127 128 129
static int imx_pd_encoder_atomic_check(struct drm_encoder *encoder,
				       struct drm_crtc_state *crtc_state,
				       struct drm_connector_state *conn_state)
{
	struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
	struct drm_display_info *di = &conn_state->connector->display_info;
	struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);

130 131
	if (!imxpd->bus_format && di->num_bus_formats) {
		imx_crtc_state->bus_flags = di->bus_flags;
132
		imx_crtc_state->bus_format = di->bus_formats[0];
133 134
	} else {
		imx_crtc_state->bus_flags = imxpd->bus_flags;
135
		imx_crtc_state->bus_format = imxpd->bus_format;
136
	}
137 138 139 140 141 142
	imx_crtc_state->di_hsync_pin = 2;
	imx_crtc_state->di_vsync_pin = 3;

	return 0;
}

143
static const struct drm_connector_funcs imx_pd_connector_funcs = {
144
	.dpms = drm_atomic_helper_connector_dpms,
145 146
	.fill_modes = drm_helper_probe_single_connector_modes,
	.detect = imx_pd_connector_detect,
147
	.destroy = imx_drm_connector_destroy,
148 149 150
	.reset = drm_atomic_helper_connector_reset,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
151 152
};

153
static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
154 155 156 157
	.get_modes = imx_pd_connector_get_modes,
	.best_encoder = imx_pd_connector_best_encoder,
};

158
static const struct drm_encoder_funcs imx_pd_encoder_funcs = {
159
	.destroy = imx_drm_encoder_destroy,
160 161
};

162
static const struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
163
	.enable = imx_pd_encoder_enable,
164
	.disable = imx_pd_encoder_disable,
165
	.atomic_check = imx_pd_encoder_atomic_check,
166 167
};

168 169
static int imx_pd_register(struct drm_device *drm,
	struct imx_parallel_display *imxpd)
170
{
171
	struct drm_encoder *encoder = &imxpd->encoder;
172 173
	int ret;

174
	ret = imx_drm_encoder_parse_of(drm, encoder, imxpd->dev->of_node);
175 176
	if (ret)
		return ret;
177

178 179 180 181 182 183 184
	/* 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;

185 186 187
	drm_encoder_helper_add(encoder, &imx_pd_encoder_helper_funcs);
	drm_encoder_init(drm, encoder, &imx_pd_encoder_funcs,
			 DRM_MODE_ENCODER_NONE, NULL);
188

189 190 191 192 193 194 195
	if (!imxpd->bridge) {
		drm_connector_helper_add(&imxpd->connector,
				&imx_pd_connector_helper_funcs);
		drm_connector_init(drm, &imxpd->connector,
				   &imx_pd_connector_funcs,
				   DRM_MODE_CONNECTOR_VGA);
	}
196

197 198 199
	if (imxpd->panel)
		drm_panel_attach(imxpd->panel, &imxpd->connector);

200 201 202 203 204 205 206 207 208 209 210 211
	if (imxpd->bridge) {
		imxpd->bridge->encoder = encoder;
		encoder->bridge = imxpd->bridge;
		ret = drm_bridge_attach(drm, imxpd->bridge);
		if (ret < 0) {
			dev_err(imxpd->dev, "failed to attach bridge: %d\n",
				ret);
			return ret;
		}
	} else {
		drm_mode_connector_attach_encoder(&imxpd->connector, encoder);
	}
212 213 214 215

	return 0;
}

216
static int imx_pd_bind(struct device *dev, struct device *master, void *data)
217
{
218
	struct drm_device *drm = data;
219
	struct device_node *np = dev->of_node;
220
	struct device_node *ep;
221 222 223
	const u8 *edidp;
	struct imx_parallel_display *imxpd;
	int ret;
224
	u32 bus_format = 0;
225 226
	const char *fmt;

227
	imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
228 229 230 231 232 233 234 235 236 237
	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"))
238
			bus_format = MEDIA_BUS_FMT_RGB888_1X24;
239
		else if (!strcmp(fmt, "rgb565"))
240
			bus_format = MEDIA_BUS_FMT_RGB565_1X16;
241
		else if (!strcmp(fmt, "bgr666"))
242
			bus_format = MEDIA_BUS_FMT_RGB666_1X18;
243
		else if (!strcmp(fmt, "lvds666"))
244
			bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
245
	}
246
	imxpd->bus_format = bus_format;
247

248
	/* port@1 is the output port */
249 250 251 252 253
	ep = of_graph_get_endpoint_by_regs(np, 1, -1);
	if (ep) {
		struct device_node *remote;

		remote = of_graph_get_remote_port_parent(ep);
254 255 256 257 258 259
		if (!remote) {
			dev_warn(dev, "endpoint %s not connected\n",
				 ep->full_name);
			of_node_put(ep);
			return -ENODEV;
		}
260
		of_node_put(ep);
261 262 263 264 265 266 267 268 269

		imxpd->panel = of_drm_find_panel(remote);
		if (imxpd->panel) {
			dev_dbg(dev, "found panel %s\n", remote->full_name);
		} else {
			imxpd->bridge = of_drm_find_bridge(remote);
			if (imxpd->bridge)
				dev_dbg(dev, "found bridge %s\n",
					remote->full_name);
270
		}
271 272 273 274
		if (!imxpd->panel && !imxpd->bridge) {
			dev_dbg(dev, "waiting for panel or bridge %s\n",
				remote->full_name);
			of_node_put(remote);
275
			return -EPROBE_DEFER;
276 277
		}
		of_node_put(remote);
278
	}
279

280
	imxpd->dev = dev;
281

282
	ret = imx_pd_register(drm, imxpd);
283 284 285
	if (ret)
		return ret;

286
	dev_set_drvdata(dev, imxpd);
287 288 289 290

	return 0;
}

291 292
static void imx_pd_unbind(struct device *dev, struct device *master,
	void *data)
293
{
294
	struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
295

296 297 298 299 300
	if (imxpd->bridge)
		drm_bridge_detach(imxpd->bridge);
	if (imxpd->panel)
		drm_panel_detach(imxpd->panel);

301
	kfree(imxpd->edid);
302
}
303

304 305 306 307 308 309 310 311 312 313 314 315 316
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);
317

318 319 320 321 322 323 324
	return 0;
}

static const struct of_device_id imx_pd_dt_ids[] = {
	{ .compatible = "fsl,imx-parallel-display", },
	{ /* sentinel */ }
};
325
MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
326 327 328

static struct platform_driver imx_pd_driver = {
	.probe		= imx_pd_probe,
329
	.remove		= imx_pd_remove,
330 331 332 333 334 335 336 337 338 339 340
	.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");
341
MODULE_ALIAS("platform:imx-parallel-display");