imx-drm-core.c 13.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Freescale i.MX drm driver
 *
 * Copyright (C) 2011 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
#include <linux/device.h>
18
#include <linux/dma-buf.h>
19 20
#include <linux/fb.h>
#include <linux/module.h>
21
#include <linux/platform_device.h>
22
#include <linux/reservation.h>
23
#include <drm/drmP.h>
24 25
#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
26 27 28 29
#include <drm/drm_fb_helper.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_gem_cma_helper.h>
#include <drm/drm_fb_cma_helper.h>
30
#include <drm/drm_plane_helper.h>
31
#include <drm/drm_of.h>
32
#include <video/imx-ipu-v3.h>
33 34 35 36 37

#include "imx-drm.h"

#define MAX_CRTC	4

38 39 40 41 42
struct imx_drm_component {
	struct device_node *of_node;
	struct list_head list;
};

43 44
struct imx_drm_device {
	struct drm_device			*drm;
45
	struct imx_drm_crtc			*crtc[MAX_CRTC];
46
	unsigned int				pipes;
47
	struct drm_fbdev_cma			*fbhelper;
48
	struct drm_atomic_state			*state;
49 50 51 52 53 54 55
};

struct imx_drm_crtc {
	struct drm_crtc				*crtc;
	struct imx_drm_crtc_helper_funcs	imx_drm_helper_funcs;
};

56
#if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
57 58
static int legacyfb_depth = 16;
module_param(legacyfb_depth, int, 0444);
59
#endif
60

61 62 63 64
static void imx_drm_driver_lastclose(struct drm_device *drm)
{
	struct imx_drm_device *imxdrm = drm->dev_private;

65
	drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
66 67 68 69 70
}

static int imx_drm_driver_unload(struct drm_device *drm)
{
	struct imx_drm_device *imxdrm = drm->dev_private;
71 72

	drm_kms_helper_poll_fini(drm);
73

74 75 76
	if (imxdrm->fbhelper)
		drm_fbdev_cma_fini(imxdrm->fbhelper);

77 78
	component_unbind_all(drm->dev, drm);

79 80
	drm_vblank_cleanup(drm);
	drm_mode_config_cleanup(drm);
81

82 83
	platform_set_drvdata(drm->platformdev, NULL);

84 85 86
	return 0;
}

87
static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
88 89
{
	struct imx_drm_device *imxdrm = drm->dev_private;
90
	struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
91 92 93 94 95 96 97 98 99 100 101 102 103 104
	int ret;

	if (!imx_drm_crtc)
		return -EINVAL;

	if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
		return -ENOSYS;

	ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
			imx_drm_crtc->crtc);

	return ret;
}

105
static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
106 107
{
	struct imx_drm_device *imxdrm = drm->dev_private;
108
	struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

	if (!imx_drm_crtc)
		return;

	if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
		return;

	imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
}

static const struct file_operations imx_drm_driver_fops = {
	.owner = THIS_MODULE,
	.open = drm_open,
	.release = drm_release,
	.unlocked_ioctl = drm_ioctl,
	.mmap = drm_gem_cma_mmap,
	.poll = drm_poll,
	.read = drm_read,
	.llseek = noop_llseek,
};

130 131
void imx_drm_connector_destroy(struct drm_connector *connector)
{
132
	drm_connector_unregister(connector);
133 134 135 136 137 138 139 140 141 142
	drm_connector_cleanup(connector);
}
EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);

void imx_drm_encoder_destroy(struct drm_encoder *encoder)
{
	drm_encoder_cleanup(encoder);
}
EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);

143 144 145 146 147 148 149
static void imx_drm_output_poll_changed(struct drm_device *drm)
{
	struct imx_drm_device *imxdrm = drm->dev_private;

	drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
}

150
static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
R
Russell King 已提交
151
	.fb_create = drm_fb_cma_create,
152
	.output_poll_changed = imx_drm_output_poll_changed,
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	.atomic_check = drm_atomic_helper_check,
	.atomic_commit = drm_atomic_helper_commit,
};

static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
{
	struct drm_device *dev = state->dev;
	struct drm_crtc *crtc;
	struct drm_crtc_state *crtc_state;
	struct drm_plane_state *plane_state;
	struct drm_gem_cma_object *cma_obj;
	struct fence *excl;
	unsigned shared_count;
	struct fence **shared;
	unsigned int i, j;
	int ret;

	/* Wait for fences. */
	for_each_crtc_in_state(state, crtc, crtc_state, i) {
		plane_state = crtc->primary->state;
		if (plane_state->fb) {
			cma_obj = drm_fb_cma_get_gem_obj(plane_state->fb, 0);
			if (cma_obj->base.dma_buf) {
				ret = reservation_object_get_fences_rcu(
					cma_obj->base.dma_buf->resv, &excl,
					&shared_count, &shared);
				if (unlikely(ret))
					DRM_ERROR("failed to get fences "
						  "for buffer\n");

				if (excl) {
					fence_wait(excl, false);
					fence_put(excl);
				}
				for (j = 0; j < shared_count; i++) {
					fence_wait(shared[j], false);
					fence_put(shared[j]);
				}
			}
		}
	}

	drm_atomic_helper_commit_modeset_disables(dev, state);

	drm_atomic_helper_commit_planes(dev, state, true);

	drm_atomic_helper_commit_modeset_enables(dev, state);

	drm_atomic_helper_commit_hw_done(state);

	drm_atomic_helper_wait_for_vblanks(dev, state);

	drm_atomic_helper_cleanup_planes(dev, state);
}

static struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
	.atomic_commit_tail = imx_drm_atomic_commit_tail,
R
Russell King 已提交
210 211
};

212
/*
213 214
 * Main DRM initialisation. This binds, initialises and registers
 * with DRM the subcomponents of the driver.
215 216 217
 */
static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
{
218
	struct imx_drm_device *imxdrm;
219
	struct drm_connector *connector;
220 221
	int ret;

222 223 224 225
	imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL);
	if (!imxdrm)
		return -ENOMEM;

226 227 228 229 230 231
	imxdrm->drm = drm;

	drm->dev_private = imxdrm;

	/*
	 * enable drm irq mode.
V
Ville Syrjälä 已提交
232
	 * - with irq_enabled = true, we can use the vblank feature.
233 234 235 236 237 238
	 *
	 * P.S. note that we wouldn't use drm irq handler but
	 *      just specific driver own one instead because
	 *      drm framework supports only one irq handler and
	 *      drivers can well take care of their interrupts
	 */
V
Ville Syrjälä 已提交
239
	drm->irq_enabled = true;
240

R
Russell King 已提交
241 242 243 244 245 246 247 248 249 250
	/*
	 * set max width and height as default value(4096x4096).
	 * this value would be used to check framebuffer size limitation
	 * at drm_mode_addfb().
	 */
	drm->mode_config.min_width = 64;
	drm->mode_config.min_height = 64;
	drm->mode_config.max_width = 4096;
	drm->mode_config.max_height = 4096;
	drm->mode_config.funcs = &imx_drm_mode_config_funcs;
251
	drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
R
Russell King 已提交
252

253 254
	drm_mode_config_init(drm);

255
	ret = drm_vblank_init(drm, MAX_CRTC);
256
	if (ret)
257
		goto err_kms;
258

259
	platform_set_drvdata(drm->platformdev, drm);
260

261 262 263
	/* Now try and bind all our sub-components */
	ret = component_bind_all(drm->dev, drm);
	if (ret)
264
		goto err_vblank;
265 266 267 268 269 270 271

	/*
	 * All components are now added, we can publish the connector sysfs
	 * entries to userspace.  This will generate hotplug events and so
	 * userspace will expect to be able to access DRM at this point.
	 */
	list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
272
		ret = drm_connector_register(connector);
273 274
		if (ret) {
			dev_err(drm->dev,
275
				"[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
276
				connector->base.id,
277
				connector->name, ret);
278 279 280 281
			goto err_unbind;
		}
	}

282 283
	drm_mode_config_reset(drm);

284 285 286 287 288
	/*
	 * All components are now initialised, so setup the fb helper.
	 * The fb helper takes copies of key hardware information, so the
	 * crtcs/connectors/encoders must not change after this point.
	 */
289
#if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
290 291 292 293 294 295 296 297 298 299 300 301
	if (legacyfb_depth != 16 && legacyfb_depth != 32) {
		dev_warn(drm->dev, "Invalid legacyfb_depth.  Defaulting to 16bpp\n");
		legacyfb_depth = 16;
	}
	imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth,
				drm->mode_config.num_crtc, MAX_CRTC);
	if (IS_ERR(imxdrm->fbhelper)) {
		ret = PTR_ERR(imxdrm->fbhelper);
		imxdrm->fbhelper = NULL;
		goto err_unbind;
	}
#endif
302 303 304

	drm_kms_helper_poll_init(drm);

305
	return 0;
306

307 308
err_unbind:
	component_unbind_all(drm->dev, drm);
309
err_vblank:
310 311 312
	drm_vblank_cleanup(drm);
err_kms:
	drm_mode_config_cleanup(drm);
313 314 315 316 317 318 319

	return ret;
}

/*
 * imx_drm_add_crtc - add a new crtc
 */
320
int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
321
		struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane,
322
		const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
323
		struct device_node *port)
324
{
325
	struct imx_drm_device *imxdrm = drm->dev_private;
326 327
	struct imx_drm_crtc *imx_drm_crtc;

328 329 330 331
	/*
	 * The vblank arrays are dimensioned by MAX_CRTC - we can't
	 * pass IDs greater than this to those functions.
	 */
332 333
	if (imxdrm->pipes >= MAX_CRTC)
		return -EINVAL;
334

335 336
	if (imxdrm->drm->open_count)
		return -EBUSY;
337 338

	imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
339 340
	if (!imx_drm_crtc)
		return -ENOMEM;
341 342 343 344

	imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
	imx_drm_crtc->crtc = crtc;

345 346
	crtc->port = port;

347
	imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc;
348 349 350

	*new_crtc = imx_drm_crtc;

351 352 353
	drm_crtc_helper_add(crtc,
			imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);

354
	drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
355
			imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL);
356

357 358 359 360 361 362 363 364 365
	return 0;
}
EXPORT_SYMBOL_GPL(imx_drm_add_crtc);

/*
 * imx_drm_remove_crtc - remove a crtc
 */
int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
{
366
	struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
367
	unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc);
368 369 370

	drm_crtc_cleanup(imx_drm_crtc->crtc);

371
	imxdrm->crtc[pipe] = NULL;
372 373 374 375 376 377 378

	kfree(imx_drm_crtc);

	return 0;
}
EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);

379 380
int imx_drm_encoder_parse_of(struct drm_device *drm,
	struct drm_encoder *encoder, struct device_node *np)
381
{
382
	uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
383

384 385 386 387 388 389 390 391
	/*
	 * If we failed to find the CRTC(s) which this encoder is
	 * supposed to be connected to, it's because the CRTC has
	 * not been registered yet.  Defer probing, and hope that
	 * the required CRTC is added later.
	 */
	if (crtc_mask == 0)
		return -EPROBE_DEFER;
392

393
	encoder->possible_crtcs = crtc_mask;
394

395 396
	/* FIXME: this is the mask of outputs which can clone this output. */
	encoder->possible_clones = ~0;
397 398 399

	return 0;
}
400
EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
401

R
Rob Clark 已提交
402
static const struct drm_ioctl_desc imx_drm_ioctls[] = {
403 404 405 406
	/* none so far */
};

static struct drm_driver imx_drm_driver = {
407 408
	.driver_features	= DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
				  DRIVER_ATOMIC,
409 410 411
	.load			= imx_drm_driver_load,
	.unload			= imx_drm_driver_unload,
	.lastclose		= imx_drm_driver_lastclose,
412
	.gem_free_object_unlocked = drm_gem_cma_free_object,
413 414 415
	.gem_vm_ops		= &drm_gem_cma_vm_ops,
	.dumb_create		= drm_gem_cma_dumb_create,
	.dumb_map_offset	= drm_gem_cma_dumb_map_offset,
416
	.dumb_destroy		= drm_gem_dumb_destroy,
417

418 419 420 421 422 423 424 425 426
	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
	.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,
427
	.get_vblank_counter	= drm_vblank_no_hw_counter,
428 429 430 431 432 433 434 435 436 437 438 439 440
	.enable_vblank		= imx_drm_enable_vblank,
	.disable_vblank		= imx_drm_disable_vblank,
	.ioctls			= imx_drm_ioctls,
	.num_ioctls		= ARRAY_SIZE(imx_drm_ioctls),
	.fops			= &imx_drm_driver_fops,
	.name			= "imx-drm",
	.desc			= "i.MX DRM graphics",
	.date			= "20120507",
	.major			= 1,
	.minor			= 0,
	.patchlevel		= 0,
};

441 442
static int compare_of(struct device *dev, void *data)
{
443
	struct device_node *np = data;
444

445 446 447 448 449 450 451
	/* Special case for DI, dev->of_node may not be set yet */
	if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
		struct ipu_client_platformdata *pdata = dev->platform_data;

		return pdata->of_node == np;
	}

452 453 454 455
	/* Special case for LDB, one device for two channels */
	if (of_node_cmp(np->name, "lvds-channel") == 0) {
		np = of_get_parent(np);
		of_node_put(np);
456 457
	}

458 459
	return dev->of_node == np;
}
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

static int imx_drm_bind(struct device *dev)
{
	return drm_platform_init(&imx_drm_driver, to_platform_device(dev));
}

static void imx_drm_unbind(struct device *dev)
{
	drm_put_dev(dev_get_drvdata(dev));
}

static const struct component_master_ops imx_drm_ops = {
	.bind = imx_drm_bind,
	.unbind = imx_drm_unbind,
};

476 477
static int imx_drm_platform_probe(struct platform_device *pdev)
{
478
	int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
479

480 481
	if (!ret)
		ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
482

483
	return ret;
484 485 486 487
}

static int imx_drm_platform_remove(struct platform_device *pdev)
{
488
	component_master_del(&pdev->dev, &imx_drm_ops);
489 490 491
	return 0;
}

492 493 494 495
#ifdef CONFIG_PM_SLEEP
static int imx_drm_suspend(struct device *dev)
{
	struct drm_device *drm_dev = dev_get_drvdata(dev);
496
	struct imx_drm_device *imxdrm;
497 498 499 500 501 502 503

	/* The drm_dev is NULL before .load hook is called */
	if (drm_dev == NULL)
		return 0;

	drm_kms_helper_poll_disable(drm_dev);

504 505 506 507 508 509 510
	imxdrm = drm_dev->dev_private;
	imxdrm->state = drm_atomic_helper_suspend(drm_dev);
	if (IS_ERR(imxdrm->state)) {
		drm_kms_helper_poll_enable(drm_dev);
		return PTR_ERR(imxdrm->state);
	}

511 512 513 514 515 516
	return 0;
}

static int imx_drm_resume(struct device *dev)
{
	struct drm_device *drm_dev = dev_get_drvdata(dev);
517
	struct imx_drm_device *imx_drm;
518 519 520 521

	if (drm_dev == NULL)
		return 0;

522 523
	imx_drm = drm_dev->dev_private;
	drm_atomic_helper_resume(drm_dev, imx_drm->state);
524 525 526 527 528 529 530 531
	drm_kms_helper_poll_enable(drm_dev);

	return 0;
}
#endif

static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);

532
static const struct of_device_id imx_drm_dt_ids[] = {
533
	{ .compatible = "fsl,imx-display-subsystem", },
534 535 536 537
	{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);

538 539
static struct platform_driver imx_drm_pdrv = {
	.probe		= imx_drm_platform_probe,
540
	.remove		= imx_drm_platform_remove,
541 542
	.driver		= {
		.name	= "imx-drm",
543
		.pm	= &imx_drm_pm_ops,
544
		.of_match_table = imx_drm_dt_ids,
545 546
	},
};
547
module_platform_driver(imx_drm_pdrv);
548 549 550 551

MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
MODULE_DESCRIPTION("i.MX drm driver core");
MODULE_LICENSE("GPL");