rcar_du_kms.c 10.7 KB
Newer Older
1 2 3
/*
 * rcar_du_kms.c  --  R-Car Display Unit Mode Setting
 *
4
 * Copyright (C) 2013-2014 Renesas Electronics Corporation
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
 *
 * 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.
 */

#include <drm/drmP.h>
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_fb_cma_helper.h>
#include <drm/drm_gem_cma_helper.h>

L
Laurent Pinchart 已提交
20 21
#include <linux/of_graph.h>

22 23
#include "rcar_du_crtc.h"
#include "rcar_du_drv.h"
24
#include "rcar_du_encoder.h"
25
#include "rcar_du_kms.h"
26
#include "rcar_du_lvdsenc.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#include "rcar_du_regs.h"

/* -----------------------------------------------------------------------------
 * Format helpers
 */

static const struct rcar_du_format_info rcar_du_format_infos[] = {
	{
		.fourcc = DRM_FORMAT_RGB565,
		.bpp = 16,
		.planes = 1,
		.pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
		.edf = PnDDCR4_EDF_NONE,
	}, {
		.fourcc = DRM_FORMAT_ARGB1555,
		.bpp = 16,
		.planes = 1,
		.pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
		.edf = PnDDCR4_EDF_NONE,
	}, {
		.fourcc = DRM_FORMAT_XRGB1555,
		.bpp = 16,
		.planes = 1,
		.pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
		.edf = PnDDCR4_EDF_NONE,
	}, {
		.fourcc = DRM_FORMAT_XRGB8888,
		.bpp = 32,
		.planes = 1,
		.pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
		.edf = PnDDCR4_EDF_RGB888,
	}, {
		.fourcc = DRM_FORMAT_ARGB8888,
		.bpp = 32,
		.planes = 1,
		.pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
		.edf = PnDDCR4_EDF_ARGB8888,
	}, {
		.fourcc = DRM_FORMAT_UYVY,
		.bpp = 16,
		.planes = 1,
		.pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
		.edf = PnDDCR4_EDF_NONE,
	}, {
		.fourcc = DRM_FORMAT_YUYV,
		.bpp = 16,
		.planes = 1,
		.pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
		.edf = PnDDCR4_EDF_NONE,
	}, {
		.fourcc = DRM_FORMAT_NV12,
		.bpp = 12,
		.planes = 2,
		.pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
		.edf = PnDDCR4_EDF_NONE,
	}, {
		.fourcc = DRM_FORMAT_NV21,
		.bpp = 12,
		.planes = 2,
		.pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
		.edf = PnDDCR4_EDF_NONE,
	}, {
		/* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */
		.fourcc = DRM_FORMAT_NV16,
		.bpp = 16,
		.planes = 2,
		.pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
		.edf = PnDDCR4_EDF_NONE,
	},
};

const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
		if (rcar_du_format_infos[i].fourcc == fourcc)
			return &rcar_du_format_infos[i];
	}

	return NULL;
}

/* -----------------------------------------------------------------------------
 * Frame buffer
 */

114 115 116
int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
			struct drm_mode_create_dumb *args)
{
117
	struct rcar_du_device *rcdu = dev->dev_private;
118 119 120
	unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
	unsigned int align;

121 122 123
	/* The R8A7779 DU requires a 16 pixels pitch alignment as documented,
	 * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
	 */
124
	if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
125 126 127 128
		align = 128;
	else
		align = 16 * args->bpp / 8;

129
	args->pitch = roundup(min_pitch, align);
130

131
	return drm_gem_cma_dumb_create_internal(file, dev, args);
132 133
}

134 135 136 137
static struct drm_framebuffer *
rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
		  struct drm_mode_fb_cmd2 *mode_cmd)
{
138
	struct rcar_du_device *rcdu = dev->dev_private;
139
	const struct rcar_du_format_info *format;
140
	unsigned int max_pitch;
141
	unsigned int align;
142
	unsigned int bpp;
143 144 145 146 147 148 149 150

	format = rcar_du_format_info(mode_cmd->pixel_format);
	if (format == NULL) {
		dev_dbg(dev->dev, "unsupported pixel format %08x\n",
			mode_cmd->pixel_format);
		return ERR_PTR(-EINVAL);
	}

151 152 153 154 155 156 157
	/*
	 * The pitch and alignment constraints are expressed in pixels on the
	 * hardware side and in bytes in the DRM API.
	 */
	bpp = format->planes == 2 ? 1 : format->bpp / 8;
	max_pitch =  4096 * bpp;

158
	if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
159 160
		align = 128;
	else
161
		align = 16 * bpp;
162 163

	if (mode_cmd->pitches[0] & (align - 1) ||
164
	    mode_cmd->pitches[0] >= max_pitch) {
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		dev_dbg(dev->dev, "invalid pitch value %u\n",
			mode_cmd->pitches[0]);
		return ERR_PTR(-EINVAL);
	}

	if (format->planes == 2) {
		if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
			dev_dbg(dev->dev,
				"luma and chroma pitches do not match\n");
			return ERR_PTR(-EINVAL);
		}
	}

	return drm_fb_cma_create(dev, file_priv, mode_cmd);
}

181 182 183 184 185 186 187
static void rcar_du_output_poll_changed(struct drm_device *dev)
{
	struct rcar_du_device *rcdu = dev->dev_private;

	drm_fbdev_cma_hotplug_event(rcdu->fbdev);
}

188 189
static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
	.fb_create = rcar_du_fb_create,
190
	.output_poll_changed = rcar_du_output_poll_changed,
191 192
};

193 194 195
static int rcar_du_encoders_init_one(struct rcar_du_device *rcdu,
				     enum rcar_du_output output,
				     struct of_endpoint *ep)
L
Laurent Pinchart 已提交
196 197 198 199 200 201
{
	static const struct {
		const char *compatible;
		enum rcar_du_encoder_type type;
	} encoders[] = {
		{ "adi,adv7123", RCAR_DU_ENCODER_VGA },
202
		{ "adi,adv7511w", RCAR_DU_ENCODER_HDMI },
L
Laurent Pinchart 已提交
203 204 205 206 207 208
		{ "thine,thc63lvdm83d", RCAR_DU_ENCODER_LVDS },
	};

	enum rcar_du_encoder_type enc_type = RCAR_DU_ENCODER_NONE;
	struct device_node *connector = NULL;
	struct device_node *encoder = NULL;
209
	struct device_node *ep_node = NULL;
L
Laurent Pinchart 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	struct device_node *entity_ep_node;
	struct device_node *entity;
	int ret;

	/*
	 * Locate the connected entity and infer its type from the number of
	 * endpoints.
	 */
	entity = of_graph_get_remote_port_parent(ep->local_node);
	if (!entity) {
		dev_dbg(rcdu->dev, "unconnected endpoint %s, skipping\n",
			ep->local_node->full_name);
		return 0;
	}

	entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0);

	while (1) {
228
		ep_node = of_graph_get_next_endpoint(entity, ep_node);
L
Laurent Pinchart 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

		if (!ep_node)
			break;

		if (ep_node == entity_ep_node)
			continue;

		/*
		 * We've found one endpoint other than the input, this must
		 * be an encoder. Locate the connector.
		 */
		encoder = entity;
		connector = of_graph_get_remote_port_parent(ep_node);
		of_node_put(ep_node);

		if (!connector) {
			dev_warn(rcdu->dev,
				 "no connector for encoder %s, skipping\n",
				 encoder->full_name);
			of_node_put(entity_ep_node);
			of_node_put(encoder);
			return 0;
		}

		break;
	}

	of_node_put(entity_ep_node);

	if (encoder) {
		/*
		 * If an encoder has been found, get its type based on its
		 * compatible string.
		 */
		unsigned int i;

		for (i = 0; i < ARRAY_SIZE(encoders); ++i) {
			if (of_device_is_compatible(encoder,
						    encoders[i].compatible)) {
				enc_type = encoders[i].type;
				break;
			}
		}

		if (i == ARRAY_SIZE(encoders)) {
			dev_warn(rcdu->dev,
				 "unknown encoder type for %s, skipping\n",
				 encoder->full_name);
			of_node_put(encoder);
			of_node_put(connector);
			return 0;
		}
	} else {
		/*
		 * If no encoder has been found the entity must be the
		 * connector.
		 */
		connector = entity;
	}

289
	ret = rcar_du_encoder_init(rcdu, enc_type, output, encoder, connector);
L
Laurent Pinchart 已提交
290 291 292 293 294 295
	of_node_put(encoder);
	of_node_put(connector);

	return ret < 0 ? ret : 1;
}

296
static int rcar_du_encoders_init(struct rcar_du_device *rcdu)
L
Laurent Pinchart 已提交
297 298
{
	struct device_node *np = rcdu->dev->of_node;
299
	struct device_node *ep_node = NULL;
L
Laurent Pinchart 已提交
300 301 302 303 304 305 306 307 308 309 310 311
	unsigned int num_encoders = 0;

	/*
	 * Iterate over the endpoints and create one encoder for each output
	 * pipeline.
	 */
	while (1) {
		enum rcar_du_output output;
		struct of_endpoint ep;
		unsigned int i;
		int ret;

312
		ep_node = of_graph_get_next_endpoint(np, ep_node);
L
Laurent Pinchart 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

		if (ep_node == NULL)
			break;

		ret = of_graph_parse_endpoint(ep_node, &ep);
		if (ret < 0) {
			of_node_put(ep_node);
			return ret;
		}

		/* Find the output route corresponding to the port number. */
		for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
			if (rcdu->info->routes[i].possible_crtcs &&
			    rcdu->info->routes[i].port == ep.port) {
				output = i;
				break;
			}
		}

		if (i == RCAR_DU_OUTPUT_MAX) {
			dev_warn(rcdu->dev,
				 "port %u references unexisting output, skipping\n",
				 ep.port);
			continue;
		}

		/* Process the output pipeline. */
340
		ret = rcar_du_encoders_init_one(rcdu, output, &ep);
L
Laurent Pinchart 已提交
341
		if (ret < 0) {
342 343 344 345 346 347 348 349
			if (ret == -EPROBE_DEFER) {
				of_node_put(ep_node);
				return ret;
			}

			dev_info(rcdu->dev,
				 "encoder initialization failed, skipping\n");
			continue;
L
Laurent Pinchart 已提交
350 351 352 353 354 355 356 357
		}

		num_encoders += ret;
	}

	return num_encoders;
}

358 359
int rcar_du_modeset_init(struct rcar_du_device *rcdu)
{
360 361 362 363
	static const unsigned int mmio_offsets[] = {
		DU0_REG_OFFSET, DU2_REG_OFFSET
	};

364 365
	struct drm_device *dev = rcdu->ddev;
	struct drm_encoder *encoder;
366
	struct drm_fbdev_cma *fbdev;
L
Laurent Pinchart 已提交
367
	unsigned int num_encoders;
368
	unsigned int num_groups;
369 370 371
	unsigned int i;
	int ret;

372
	drm_mode_config_init(dev);
373

374 375 376 377 378
	dev->mode_config.min_width = 0;
	dev->mode_config.min_height = 0;
	dev->mode_config.max_width = 4095;
	dev->mode_config.max_height = 2047;
	dev->mode_config.funcs = &rcar_du_mode_config_funcs;
379

380 381 382 383 384 385 386
	rcdu->num_crtcs = rcdu->info->num_crtcs;

	/* Initialize the groups. */
	num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);

	for (i = 0; i < num_groups; ++i) {
		struct rcar_du_group *rgrp = &rcdu->groups[i];
387

388 389 390
		rgrp->dev = rcdu;
		rgrp->mmio_offset = mmio_offsets[i];
		rgrp->index = i;
391

392
		ret = rcar_du_planes_init(rgrp);
393 394 395
		if (ret < 0)
			return ret;
	}
396

397 398 399 400 401 402 403 404
	/* Create the CRTCs. */
	for (i = 0; i < rcdu->num_crtcs; ++i) {
		struct rcar_du_group *rgrp = &rcdu->groups[i / 2];

		ret = rcar_du_crtc_create(rgrp, i);
		if (ret < 0)
			return ret;
	}
405

406
	/* Initialize the encoders. */
407 408 409 410
	ret = rcar_du_lvdsenc_init(rcdu);
	if (ret < 0)
		return ret;

411
	ret = rcar_du_encoders_init(rcdu);
L
Laurent Pinchart 已提交
412 413
	if (ret < 0)
		return ret;
414

415 416 417 418 419
	if (ret == 0) {
		dev_err(rcdu->dev, "error: no encoder could be initialized\n");
		return -EINVAL;
	}

L
Laurent Pinchart 已提交
420
	num_encoders = ret;
421

422 423 424
	/* Set the possible CRTCs and possible clones. There's always at least
	 * one way for all encoders to clone each other, set all bits in the
	 * possible clones field.
425 426 427
	 */
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
		struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
428 429
		const struct rcar_du_output_routing *route =
			&rcdu->info->routes[renc->output];
430

431
		encoder->possible_crtcs = route->possible_crtcs;
L
Laurent Pinchart 已提交
432
		encoder->possible_clones = (1 << num_encoders) - 1;
433 434
	}

435 436 437 438 439 440
	/* Now that the CRTCs have been initialized register the planes. */
	for (i = 0; i < num_groups; ++i) {
		ret = rcar_du_planes_register(&rcdu->groups[i]);
		if (ret < 0)
			return ret;
	}
441

442 443 444 445 446 447 448 449 450 451 452 453
	drm_kms_helper_poll_init(dev);

	drm_helper_disable_unused_functions(dev);

	fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc,
				   dev->mode_config.num_connector);
	if (IS_ERR(fbdev))
		return PTR_ERR(fbdev);

#ifndef CONFIG_FRAMEBUFFER_CONSOLE
	drm_fbdev_cma_restore_mode(fbdev);
#endif
454

455
	rcdu->fbdev = fbdev;
456 457 458

	return 0;
}