imx-drm-core.c 14.8 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 19
#include <linux/fb.h>
#include <linux/module.h>
20
#include <linux/of_graph.h>
21 22 23 24 25 26
#include <linux/platform_device.h>
#include <drm/drmP.h>
#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>
27
#include <drm/drm_plane_helper.h>
28
#include <drm/drm_of.h>
29 30 31 32 33

#include "imx-drm.h"

#define MAX_CRTC	4

34 35 36 37 38
struct imx_drm_component {
	struct device_node *of_node;
	struct list_head list;
};

39 40
struct imx_drm_device {
	struct drm_device			*drm;
41
	struct imx_drm_crtc			*crtc[MAX_CRTC];
42 43 44 45 46 47 48 49 50 51
	int					pipes;
	struct drm_fbdev_cma			*fbhelper;
};

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

52 53 54
static int legacyfb_depth = 16;
module_param(legacyfb_depth, int, 0444);

55 56 57 58
int imx_drm_crtc_id(struct imx_drm_crtc *crtc)
{
	return crtc->pipe;
}
59
EXPORT_SYMBOL_GPL(imx_drm_crtc_id);
60

61 62
static void imx_drm_driver_lastclose(struct drm_device *drm)
{
63
#if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
64 65 66 67
	struct imx_drm_device *imxdrm = drm->dev_private;

	if (imxdrm->fbhelper)
		drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
68
#endif
69 70 71 72
}

static int imx_drm_driver_unload(struct drm_device *drm)
{
73
#if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
74
	struct imx_drm_device *imxdrm = drm->dev_private;
75 76 77
#endif

	drm_kms_helper_poll_fini(drm);
78

79
#if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
80 81 82 83
	if (imxdrm->fbhelper)
		drm_fbdev_cma_fini(imxdrm->fbhelper);
#endif

84 85
	component_unbind_all(drm->dev, drm);

86 87
	drm_vblank_cleanup(drm);
	drm_mode_config_cleanup(drm);
88

89 90
	platform_set_drvdata(drm->platformdev, NULL);

91 92 93
	return 0;
}

94
static struct imx_drm_crtc *imx_drm_find_crtc(struct drm_crtc *crtc)
95
{
96
	struct imx_drm_device *imxdrm = crtc->dev->dev_private;
97 98 99 100 101
	unsigned i;

	for (i = 0; i < MAX_CRTC; i++)
		if (imxdrm->crtc[i] && imxdrm->crtc[i]->crtc == crtc)
			return imxdrm->crtc[i];
102 103 104 105

	return NULL;
}

106 107
int imx_drm_set_bus_format_pins(struct drm_encoder *encoder, u32 bus_format,
		int hsync_pin, int vsync_pin)
108 109
{
	struct imx_drm_crtc_helper_funcs *helper;
110
	struct imx_drm_crtc *imx_crtc;
111

112
	imx_crtc = imx_drm_find_crtc(encoder->crtc);
113 114
	if (!imx_crtc)
		return -EINVAL;
115 116 117

	helper = &imx_crtc->imx_drm_helper_funcs;
	if (helper->set_interface_pix_fmt)
118
		return helper->set_interface_pix_fmt(encoder->crtc,
119
					bus_format, hsync_pin, vsync_pin);
120 121
	return 0;
}
122
EXPORT_SYMBOL_GPL(imx_drm_set_bus_format_pins);
123

124
int imx_drm_set_bus_format(struct drm_encoder *encoder, u32 bus_format)
125
{
126
	return imx_drm_set_bus_format_pins(encoder, bus_format, 2, 3);
127
}
128
EXPORT_SYMBOL_GPL(imx_drm_set_bus_format);
129 130 131

int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
{
132
	return drm_vblank_get(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe);
133 134 135 136 137
}
EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);

void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
{
138
	drm_vblank_put(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe);
139 140 141 142 143
}
EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);

void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
{
144
	drm_handle_vblank(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe);
145 146 147
}
EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);

148
static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
149 150
{
	struct imx_drm_device *imxdrm = drm->dev_private;
151
	struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
152 153 154 155 156 157 158 159 160 161 162 163 164 165
	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;
}

166
static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
167 168
{
	struct imx_drm_device *imxdrm = drm->dev_private;
169
	struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
170 171 172 173 174 175 176 177 178 179

	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);
}

180 181 182 183 184 185 186 187
static void imx_drm_driver_preclose(struct drm_device *drm,
		struct drm_file *file)
{
	int i;

	if (!file->is_master)
		return;

188 189
	for (i = 0; i < MAX_CRTC; i++)
		imx_drm_disable_vblank(drm, i);
190 191
}

192 193 194 195 196 197 198 199 200 201 202
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,
};

203 204
void imx_drm_connector_destroy(struct drm_connector *connector)
{
205
	drm_connector_unregister(connector);
206 207 208 209 210 211 212 213 214 215
	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);

216 217 218 219 220 221 222 223 224
static void imx_drm_output_poll_changed(struct drm_device *drm)
{
#if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
	struct imx_drm_device *imxdrm = drm->dev_private;

	drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
#endif
}

R
Russell King 已提交
225 226
static struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
	.fb_create = drm_fb_cma_create,
227
	.output_poll_changed = imx_drm_output_poll_changed,
R
Russell King 已提交
228 229
};

230
/*
231 232
 * Main DRM initialisation. This binds, initialises and registers
 * with DRM the subcomponents of the driver.
233 234 235
 */
static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
{
236
	struct imx_drm_device *imxdrm;
237
	struct drm_connector *connector;
238 239
	int ret;

240 241 242 243
	imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL);
	if (!imxdrm)
		return -ENOMEM;

244 245 246 247 248 249
	imxdrm->drm = drm;

	drm->dev_private = imxdrm;

	/*
	 * enable drm irq mode.
V
Ville Syrjälä 已提交
250
	 * - with irq_enabled = true, we can use the vblank feature.
251 252 253 254 255 256
	 *
	 * 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ä 已提交
257
	drm->irq_enabled = true;
258

R
Russell King 已提交
259 260 261 262 263 264 265 266 267 268 269
	/*
	 * 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;

270 271
	drm_mode_config_init(drm);

272
	ret = drm_vblank_init(drm, MAX_CRTC);
273
	if (ret)
274
		goto err_kms;
275 276

	/*
277 278 279
	 * with vblank_disable_allowed = true, vblank interrupt will be
	 * disabled by drm timer once a current process gives up ownership
	 * of vblank event. (after drm_vblank_put function is called)
280
	 */
281
	drm->vblank_disable_allowed = true;
282

283
	platform_set_drvdata(drm->platformdev, drm);
284

285 286 287
	/* Now try and bind all our sub-components */
	ret = component_bind_all(drm->dev, drm);
	if (ret)
288
		goto err_vblank;
289 290 291 292 293 294 295

	/*
	 * 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) {
296
		ret = drm_connector_register(connector);
297 298
		if (ret) {
			dev_err(drm->dev,
299
				"[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
300
				connector->base.id,
301
				connector->name, ret);
302 303 304 305
			goto err_unbind;
		}
	}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
	/*
	 * 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.
	 */
#if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER)
	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
324 325 326

	drm_kms_helper_poll_init(drm);

327
	return 0;
328

329 330
err_unbind:
	component_unbind_all(drm->dev, drm);
331
err_vblank:
332 333 334
	drm_vblank_cleanup(drm);
err_kms:
	drm_mode_config_cleanup(drm);
335 336 337 338 339 340 341

	return ret;
}

/*
 * imx_drm_add_crtc - add a new crtc
 */
342
int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
343
		struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane,
344
		const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
345
		struct device_node *port)
346
{
347
	struct imx_drm_device *imxdrm = drm->dev_private;
348 349 350
	struct imx_drm_crtc *imx_drm_crtc;
	int ret;

351 352 353 354
	/*
	 * The vblank arrays are dimensioned by MAX_CRTC - we can't
	 * pass IDs greater than this to those functions.
	 */
355 356
	if (imxdrm->pipes >= MAX_CRTC)
		return -EINVAL;
357

358 359
	if (imxdrm->drm->open_count)
		return -EBUSY;
360 361

	imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
362 363
	if (!imx_drm_crtc)
		return -ENOMEM;
364 365 366 367 368

	imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
	imx_drm_crtc->pipe = imxdrm->pipes++;
	imx_drm_crtc->crtc = crtc;

369 370
	crtc->port = port;

371
	imxdrm->crtc[imx_drm_crtc->pipe] = imx_drm_crtc;
372 373 374

	*new_crtc = imx_drm_crtc;

375
	ret = drm_mode_crtc_set_gamma_size(imx_drm_crtc->crtc, 256);
376 377 378
	if (ret)
		goto err_register;

379 380 381
	drm_crtc_helper_add(crtc,
			imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);

382
	drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
383 384
			imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs);

385 386 387
	return 0;

err_register:
388
	imxdrm->crtc[imx_drm_crtc->pipe] = NULL;
389 390 391 392 393 394 395 396 397 398
	kfree(imx_drm_crtc);
	return ret;
}
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)
{
399
	struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
400 401 402

	drm_crtc_cleanup(imx_drm_crtc->crtc);

403
	imxdrm->crtc[imx_drm_crtc->pipe] = NULL;
404 405 406 407 408 409 410

	kfree(imx_drm_crtc);

	return 0;
}
EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);

411 412
int imx_drm_encoder_parse_of(struct drm_device *drm,
	struct drm_encoder *encoder, struct device_node *np)
413
{
414
	uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
415

416 417 418 419 420 421 422 423
	/*
	 * 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;
424

425
	encoder->possible_crtcs = crtc_mask;
426

427 428
	/* FIXME: this is the mask of outputs which can clone this output. */
	encoder->possible_clones = ~0;
429 430 431

	return 0;
}
432
EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
433

434 435 436 437 438 439
/*
 * @node: device tree node containing encoder input ports
 * @encoder: drm_encoder
 */
int imx_drm_encoder_get_mux_id(struct device_node *node,
			       struct drm_encoder *encoder)
440
{
441
	struct imx_drm_crtc *imx_crtc = imx_drm_find_crtc(encoder->crtc);
442
	struct device_node *ep;
443
	struct of_endpoint endpoint;
444
	struct device_node *port;
445
	int ret;
446

447 448 449
	if (!node || !imx_crtc)
		return -EINVAL;

450
	for_each_endpoint_of_node(node, ep) {
451 452
		port = of_graph_get_remote_port(ep);
		of_node_put(port);
453
		if (port == imx_crtc->crtc->port) {
454
			ret = of_graph_parse_endpoint(ep, &endpoint);
455
			of_node_put(ep);
456
			return ret ? ret : endpoint.port;
457
		}
458
	}
459

460
	return -EINVAL;
461
}
462
EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id);
463

R
Rob Clark 已提交
464
static const struct drm_ioctl_desc imx_drm_ioctls[] = {
465 466 467 468
	/* none so far */
};

static struct drm_driver imx_drm_driver = {
469
	.driver_features	= DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
470 471 472
	.load			= imx_drm_driver_load,
	.unload			= imx_drm_driver_unload,
	.lastclose		= imx_drm_driver_lastclose,
473
	.preclose		= imx_drm_driver_preclose,
474
	.set_busid		= drm_platform_set_busid,
475 476 477 478
	.gem_free_object	= drm_gem_cma_free_object,
	.gem_vm_ops		= &drm_gem_cma_vm_ops,
	.dumb_create		= drm_gem_cma_dumb_create,
	.dumb_map_offset	= drm_gem_cma_dumb_map_offset,
479
	.dumb_destroy		= drm_gem_dumb_destroy,
480

481 482 483 484 485 486 487 488 489
	.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,
490
	.get_vblank_counter	= drm_vblank_no_hw_counter,
491 492 493 494 495 496 497 498 499 500 501 502 503
	.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,
};

504 505
static int compare_of(struct device *dev, void *data)
{
506
	struct device_node *np = data;
507

508 509 510 511
	/* 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);
512 513
	}

514 515
	return dev->of_node == np;
}
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

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,
};

532 533
static int imx_drm_platform_probe(struct platform_device *pdev)
{
534
	int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
535

536 537
	if (!ret)
		ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
538

539
	return ret;
540 541 542 543
}

static int imx_drm_platform_remove(struct platform_device *pdev)
{
544
	component_master_del(&pdev->dev, &imx_drm_ops);
545 546 547
	return 0;
}

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
#ifdef CONFIG_PM_SLEEP
static int imx_drm_suspend(struct device *dev)
{
	struct drm_device *drm_dev = dev_get_drvdata(dev);

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

	drm_kms_helper_poll_disable(drm_dev);

	return 0;
}

static int imx_drm_resume(struct device *dev)
{
	struct drm_device *drm_dev = dev_get_drvdata(dev);

	if (drm_dev == NULL)
		return 0;

	drm_helper_resume_force_mode(drm_dev);
	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);

578
static const struct of_device_id imx_drm_dt_ids[] = {
579
	{ .compatible = "fsl,imx-display-subsystem", },
580 581 582 583
	{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);

584 585
static struct platform_driver imx_drm_pdrv = {
	.probe		= imx_drm_platform_probe,
586
	.remove		= imx_drm_platform_remove,
587 588
	.driver		= {
		.name	= "imx-drm",
589
		.pm	= &imx_drm_pm_ops,
590
		.of_match_table = imx_drm_dt_ids,
591 592
	},
};
593
module_platform_driver(imx_drm_pdrv);
594 595 596 597

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