rcar_du_drv.c 9.8 KB
Newer Older
1 2 3
/*
 * rcar_du_drv.c  --  R-Car Display Unit DRM driver
 *
4
 * Copyright (C) 2013-2015 Renesas Electronics Corporation
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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 <linux/clk.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <linux/module.h>
L
Laurent Pinchart 已提交
18
#include <linux/of_device.h>
19 20 21
#include <linux/platform_device.h>
#include <linux/pm.h>
#include <linux/slab.h>
22
#include <linux/wait.h>
23 24 25

#include <drm/drmP.h>
#include <drm/drm_crtc_helper.h>
26
#include <drm/drm_fb_cma_helper.h>
27 28 29 30 31 32 33
#include <drm/drm_gem_cma_helper.h>

#include "rcar_du_crtc.h"
#include "rcar_du_drv.h"
#include "rcar_du_kms.h"
#include "rcar_du_regs.h"

L
Laurent Pinchart 已提交
34 35 36 37 38
/* -----------------------------------------------------------------------------
 * Device Information
 */

static const struct rcar_du_device_info rcar_du_r8a7779_info = {
39
	.gen = 2,
L
Laurent Pinchart 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	.features = 0,
	.num_crtcs = 2,
	.routes = {
		/* R8A7779 has two RGB outputs and one (currently unsupported)
		 * TCON output.
		 */
		[RCAR_DU_OUTPUT_DPAD0] = {
			.possible_crtcs = BIT(0),
			.encoder_type = DRM_MODE_ENCODER_NONE,
			.port = 0,
		},
		[RCAR_DU_OUTPUT_DPAD1] = {
			.possible_crtcs = BIT(1) | BIT(0),
			.encoder_type = DRM_MODE_ENCODER_NONE,
			.port = 1,
		},
	},
	.num_lvds = 0,
};

static const struct rcar_du_device_info rcar_du_r8a7790_info = {
61
	.gen = 2,
62 63
	.features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
		  | RCAR_DU_FEATURE_EXT_CTRL_REGS,
L
Laurent Pinchart 已提交
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
	.quirks = RCAR_DU_QUIRK_ALIGN_128B | RCAR_DU_QUIRK_LVDS_LANES,
	.num_crtcs = 3,
	.routes = {
		/* R8A7790 has one RGB output, two LVDS outputs and one
		 * (currently unsupported) TCON output.
		 */
		[RCAR_DU_OUTPUT_DPAD0] = {
			.possible_crtcs = BIT(2) | BIT(1) | BIT(0),
			.encoder_type = DRM_MODE_ENCODER_NONE,
			.port = 0,
		},
		[RCAR_DU_OUTPUT_LVDS0] = {
			.possible_crtcs = BIT(0),
			.encoder_type = DRM_MODE_ENCODER_LVDS,
			.port = 1,
		},
		[RCAR_DU_OUTPUT_LVDS1] = {
			.possible_crtcs = BIT(2) | BIT(1),
			.encoder_type = DRM_MODE_ENCODER_LVDS,
			.port = 2,
		},
	},
	.num_lvds = 2,
};

89
/* M2-W (r8a7791) and M2-N (r8a7793) are identical */
L
Laurent Pinchart 已提交
90
static const struct rcar_du_device_info rcar_du_r8a7791_info = {
91
	.gen = 2,
92 93
	.features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
		  | RCAR_DU_FEATURE_EXT_CTRL_REGS,
L
Laurent Pinchart 已提交
94 95
	.num_crtcs = 2,
	.routes = {
96
		/* R8A779[13] has one RGB output, one LVDS output and one
L
Laurent Pinchart 已提交
97 98 99
		 * (currently unsupported) TCON output.
		 */
		[RCAR_DU_OUTPUT_DPAD0] = {
100
			.possible_crtcs = BIT(1) | BIT(0),
L
Laurent Pinchart 已提交
101 102 103 104 105 106 107 108 109 110 111 112
			.encoder_type = DRM_MODE_ENCODER_NONE,
			.port = 0,
		},
		[RCAR_DU_OUTPUT_LVDS0] = {
			.possible_crtcs = BIT(0),
			.encoder_type = DRM_MODE_ENCODER_LVDS,
			.port = 1,
		},
	},
	.num_lvds = 1,
};

113
static const struct rcar_du_device_info rcar_du_r8a7794_info = {
114
	.gen = 2,
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	.features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
		  | RCAR_DU_FEATURE_EXT_CTRL_REGS,
	.num_crtcs = 2,
	.routes = {
		/* R8A7794 has two RGB outputs and one (currently unsupported)
		 * TCON output.
		 */
		[RCAR_DU_OUTPUT_DPAD0] = {
			.possible_crtcs = BIT(0),
			.encoder_type = DRM_MODE_ENCODER_NONE,
			.port = 0,
		},
		[RCAR_DU_OUTPUT_DPAD1] = {
			.possible_crtcs = BIT(1),
			.encoder_type = DRM_MODE_ENCODER_NONE,
			.port = 1,
		},
	},
	.num_lvds = 0,
};

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static const struct rcar_du_device_info rcar_du_r8a7795_info = {
	.gen = 3,
	.features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK
		  | RCAR_DU_FEATURE_EXT_CTRL_REGS
		  | RCAR_DU_FEATURE_VSP1_SOURCE,
	.num_crtcs = 4,
	.routes = {
		/* R8A7795 has one RGB output, and two HDMI and one LVDS
		 * (currently unsupported) outputs
		 */
		[RCAR_DU_OUTPUT_DPAD0] = {
			.possible_crtcs = BIT(3),
			.encoder_type = DRM_MODE_ENCODER_NONE,
			.port = 0,
		},
	},
};

L
Laurent Pinchart 已提交
154 155 156 157
static const struct of_device_id rcar_du_of_table[] = {
	{ .compatible = "renesas,du-r8a7779", .data = &rcar_du_r8a7779_info },
	{ .compatible = "renesas,du-r8a7790", .data = &rcar_du_r8a7790_info },
	{ .compatible = "renesas,du-r8a7791", .data = &rcar_du_r8a7791_info },
158
	{ .compatible = "renesas,du-r8a7793", .data = &rcar_du_r8a7791_info },
159
	{ .compatible = "renesas,du-r8a7794", .data = &rcar_du_r8a7794_info },
160
	{ .compatible = "renesas,du-r8a7795", .data = &rcar_du_r8a7795_info },
L
Laurent Pinchart 已提交
161 162 163 164 165
	{ }
};

MODULE_DEVICE_TABLE(of, rcar_du_of_table);

166 167 168 169
/* -----------------------------------------------------------------------------
 * DRM operations
 */

170
static void rcar_du_lastclose(struct drm_device *dev)
171 172 173
{
	struct rcar_du_device *rcdu = dev->dev_private;

174
	drm_fbdev_cma_restore_mode(rcdu->fbdev);
175 176
}

177
static int rcar_du_enable_vblank(struct drm_device *dev, unsigned int pipe)
178 179 180
{
	struct rcar_du_device *rcdu = dev->dev_private;

181
	rcar_du_crtc_enable_vblank(&rcdu->crtcs[pipe], true);
182 183 184 185

	return 0;
}

186
static void rcar_du_disable_vblank(struct drm_device *dev, unsigned int pipe)
187 188 189
{
	struct rcar_du_device *rcdu = dev->dev_private;

190
	rcar_du_crtc_enable_vblank(&rcdu->crtcs[pipe], false);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
}

static const struct file_operations rcar_du_fops = {
	.owner		= THIS_MODULE,
	.open		= drm_open,
	.release	= drm_release,
	.unlocked_ioctl	= drm_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl	= drm_compat_ioctl,
#endif
	.poll		= drm_poll,
	.read		= drm_read,
	.llseek		= no_llseek,
	.mmap		= drm_gem_cma_mmap,
};

static struct drm_driver rcar_du_driver = {
208 209
	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME
				| DRIVER_ATOMIC,
210
	.lastclose		= rcar_du_lastclose,
211
	.get_vblank_counter	= drm_vblank_no_hw_counter,
212 213 214 215 216 217
	.enable_vblank		= rcar_du_enable_vblank,
	.disable_vblank		= rcar_du_disable_vblank,
	.gem_free_object	= drm_gem_cma_free_object,
	.gem_vm_ops		= &drm_gem_cma_vm_ops,
	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
218 219 220 221 222 223 224
	.gem_prime_import	= drm_gem_prime_import,
	.gem_prime_export	= drm_gem_prime_export,
	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
	.gem_prime_vmap		= drm_gem_cma_prime_vmap,
	.gem_prime_vunmap	= drm_gem_cma_prime_vunmap,
	.gem_prime_mmap		= drm_gem_cma_prime_mmap,
225
	.dumb_create		= rcar_du_dumb_create,
226
	.dumb_map_offset	= drm_gem_cma_dumb_map_offset,
227
	.dumb_destroy		= drm_gem_dumb_destroy,
228 229 230 231 232 233 234 235 236 237 238 239
	.fops			= &rcar_du_fops,
	.name			= "rcar-du",
	.desc			= "Renesas R-Car Display Unit",
	.date			= "20130110",
	.major			= 1,
	.minor			= 0,
};

/* -----------------------------------------------------------------------------
 * Power management
 */

R
Russell King 已提交
240
#ifdef CONFIG_PM_SLEEP
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
static int rcar_du_pm_suspend(struct device *dev)
{
	struct rcar_du_device *rcdu = dev_get_drvdata(dev);

	drm_kms_helper_poll_disable(rcdu->ddev);
	/* TODO Suspend the CRTC */

	return 0;
}

static int rcar_du_pm_resume(struct device *dev)
{
	struct rcar_du_device *rcdu = dev_get_drvdata(dev);

	/* TODO Resume the CRTC */

	drm_kms_helper_poll_enable(rcdu->ddev);
	return 0;
}
#endif

static const struct dev_pm_ops rcar_du_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume)
};

/* -----------------------------------------------------------------------------
 * Platform driver
 */

270
static int rcar_du_remove(struct platform_device *pdev)
271
{
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	struct rcar_du_device *rcdu = platform_get_drvdata(pdev);
	struct drm_device *ddev = rcdu->ddev;

	mutex_lock(&ddev->mode_config.mutex);
	drm_connector_unplug_all(ddev);
	mutex_unlock(&ddev->mode_config.mutex);

	drm_dev_unregister(ddev);

	if (rcdu->fbdev)
		drm_fbdev_cma_fini(rcdu->fbdev);

	drm_kms_helper_poll_fini(ddev);
	drm_mode_config_cleanup(ddev);
	drm_vblank_cleanup(ddev);

	drm_dev_unref(ddev);

	return 0;
291 292
}

293
static int rcar_du_probe(struct platform_device *pdev)
294
{
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	struct device_node *np = pdev->dev.of_node;
	struct rcar_du_device *rcdu;
	struct drm_connector *connector;
	struct drm_device *ddev;
	struct resource *mem;
	int ret;

	if (np == NULL) {
		dev_err(&pdev->dev, "no device tree node\n");
		return -ENODEV;
	}

	/* Allocate and initialize the DRM and R-Car device structures. */
	rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL);
	if (rcdu == NULL)
		return -ENOMEM;

	init_waitqueue_head(&rcdu->commit.wait);

	rcdu->dev = &pdev->dev;
	rcdu->info = of_match_device(rcar_du_of_table, rcdu->dev)->data;

	ddev = drm_dev_alloc(&rcar_du_driver, &pdev->dev);
	if (!ddev)
		return -ENOMEM;

	drm_dev_set_unique(ddev, dev_name(&pdev->dev));

	rcdu->ddev = ddev;
	ddev->dev_private = rcdu;
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	platform_set_drvdata(pdev, rcdu);

	/* I/O resources */
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	rcdu->mmio = devm_ioremap_resource(&pdev->dev, mem);
	if (IS_ERR(rcdu->mmio)) {
		ret = PTR_ERR(rcdu->mmio);
		goto error;
	}

	/* Initialize vertical blanking interrupts handling. Start with vblank
	 * disabled for all CRTCs.
	 */
	ret = drm_vblank_init(ddev, (1 << rcdu->info->num_crtcs) - 1);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to initialize vblank\n");
		goto error;
	}

	/* DRM/KMS objects */
	ret = rcar_du_modeset_init(rcdu);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to initialize DRM/KMS (%d)\n", ret);
		goto error;
	}

	ddev->irq_enabled = 1;

	/* Register the DRM device with the core and the connectors with
	 * sysfs.
	 */
	ret = drm_dev_register(ddev, 0);
	if (ret)
		goto error;

	mutex_lock(&ddev->mode_config.mutex);
	drm_for_each_connector(connector, ddev) {
		ret = drm_connector_register(connector);
		if (ret < 0)
			break;
	}
	mutex_unlock(&ddev->mode_config.mutex);

	if (ret < 0)
		goto error;

	DRM_INFO("Device %s probed\n", dev_name(&pdev->dev));
373 374

	return 0;
375 376 377 378 379

error:
	rcar_du_remove(pdev);

	return ret;
380 381 382 383 384 385 386 387
}

static struct platform_driver rcar_du_platform_driver = {
	.probe		= rcar_du_probe,
	.remove		= rcar_du_remove,
	.driver		= {
		.name	= "rcar-du",
		.pm	= &rcar_du_pm_ops,
L
Laurent Pinchart 已提交
388
		.of_match_table = rcar_du_of_table,
389 390 391 392 393 394 395 396
	},
};

module_platform_driver(rcar_du_platform_driver);

MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver");
MODULE_LICENSE("GPL");