rockchip_drm_drv.c 12.2 KB
Newer Older
M
Mark Yao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
 * Author:Mark Yao <mark.yao@rock-chips.com>
 *
 * based on exynos_drm_drv.c
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 */

#include <asm/dma-iommu.h>

#include <drm/drmP.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_fb_helper.h>
D
Daniel Vetter 已提交
22
#include <drm/drm_gem_cma_helper.h>
M
Mark Yao 已提交
23 24
#include <linux/dma-mapping.h>
#include <linux/pm_runtime.h>
25
#include <linux/module.h>
M
Mark Yao 已提交
26 27
#include <linux/of_graph.h>
#include <linux/component.h>
28
#include <linux/console.h>
M
Mark Yao 已提交
29 30 31 32 33 34 35 36 37 38 39 40

#include "rockchip_drm_drv.h"
#include "rockchip_drm_fb.h"
#include "rockchip_drm_fbdev.h"
#include "rockchip_drm_gem.h"

#define DRIVER_NAME	"rockchip"
#define DRIVER_DESC	"RockChip Soc DRM"
#define DRIVER_DATE	"20140818"
#define DRIVER_MAJOR	1
#define DRIVER_MINOR	0

41
static bool is_support_iommu = true;
42
static struct drm_driver rockchip_drm_driver;
43

M
Mark Yao 已提交
44 45 46 47 48 49 50 51 52 53 54
/*
 * Attach a (component) device to the shared drm dma mapping from master drm
 * device.  This is used by the VOPs to map GEM buffers to a common DMA
 * mapping.
 */
int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
				   struct device *dev)
{
	struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
	int ret;

55 56 57
	if (!is_support_iommu)
		return 0;

M
Mark Yao 已提交
58 59 60 61 62 63 64 65 66 67 68 69
	ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
	if (ret)
		return ret;

	dma_set_max_seg_size(dev, DMA_BIT_MASK(32));

	return arm_iommu_attach_device(dev, mapping);
}

void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
				    struct device *dev)
{
70 71 72
	if (!is_support_iommu)
		return;

M
Mark Yao 已提交
73 74 75
	arm_iommu_detach_device(dev);
}

76 77
int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
				 const struct rockchip_crtc_funcs *crtc_funcs)
M
Mark Yao 已提交
78
{
79 80
	int pipe = drm_crtc_index(crtc);
	struct rockchip_drm_private *priv = crtc->dev->dev_private;
M
Mark Yao 已提交
81

82
	if (pipe >= ROCKCHIP_MAX_CRTC)
M
Mark Yao 已提交
83 84 85 86 87 88 89
		return -EINVAL;

	priv->crtc_funcs[pipe] = crtc_funcs;

	return 0;
}

90
void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
M
Mark Yao 已提交
91
{
92 93
	int pipe = drm_crtc_index(crtc);
	struct rockchip_drm_private *priv = crtc->dev->dev_private;
M
Mark Yao 已提交
94

95
	if (pipe >= ROCKCHIP_MAX_CRTC)
M
Mark Yao 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
		return;

	priv->crtc_funcs[pipe] = NULL;
}

static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
						int pipe)
{
	struct drm_crtc *crtc;
	int i = 0;

	list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
		if (i++ == pipe)
			return crtc;

	return NULL;
}

114 115
static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
					   unsigned int pipe)
M
Mark Yao 已提交
116 117 118 119 120 121 122 123 124 125 126
{
	struct rockchip_drm_private *priv = dev->dev_private;
	struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);

	if (crtc && priv->crtc_funcs[pipe] &&
	    priv->crtc_funcs[pipe]->enable_vblank)
		return priv->crtc_funcs[pipe]->enable_vblank(crtc);

	return 0;
}

127 128
static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
					     unsigned int pipe)
M
Mark Yao 已提交
129 130 131 132 133 134 135 136 137
{
	struct rockchip_drm_private *priv = dev->dev_private;
	struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);

	if (crtc && priv->crtc_funcs[pipe] &&
	    priv->crtc_funcs[pipe]->enable_vblank)
		priv->crtc_funcs[pipe]->disable_vblank(crtc);
}

138
static int rockchip_drm_bind(struct device *dev)
M
Mark Yao 已提交
139
{
140
	struct drm_device *drm_dev;
M
Mark Yao 已提交
141
	struct rockchip_drm_private *private;
142
	struct dma_iommu_mapping *mapping = NULL;
M
Mark Yao 已提交
143 144
	int ret;

145 146
	drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
	if (!drm_dev)
M
Mark Yao 已提交
147 148
		return -ENOMEM;

149 150 151 152 153
	dev_set_drvdata(dev, drm_dev);

	private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
	if (!private) {
		ret = -ENOMEM;
154
		goto err_free;
155 156
	}

M
Mark Yao 已提交
157 158
	drm_dev->dev_private = private;

159 160 161
	INIT_LIST_HEAD(&private->psr_list);
	mutex_init(&private->psr_list_mutex);

M
Mark Yao 已提交
162 163 164 165 166 167 168 169 170 171 172
	drm_mode_config_init(drm_dev);

	rockchip_drm_mode_config_init(drm_dev);

	dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
				      GFP_KERNEL);
	if (!dev->dma_parms) {
		ret = -ENOMEM;
		goto err_config_cleanup;
	}

173 174 175 176 177 178 179 180 181
	if (is_support_iommu) {
		/* TODO(djkurtz): fetch the mapping start/size from somewhere */
		mapping = arm_iommu_create_mapping(&platform_bus_type,
						   0x00000000,
						   SZ_2G);
		if (IS_ERR(mapping)) {
			ret = PTR_ERR(mapping);
			goto err_config_cleanup;
		}
M
Mark Yao 已提交
182

183 184 185
		ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
		if (ret)
			goto err_release_mapping;
M
Mark Yao 已提交
186

187
		dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
M
Mark Yao 已提交
188

189 190 191 192
		ret = arm_iommu_attach_device(dev, mapping);
		if (ret)
			goto err_release_mapping;
	}
M
Mark Yao 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

	/* Try to bind all sub drivers. */
	ret = component_bind_all(dev, drm_dev);
	if (ret)
		goto err_detach_device;

	/* init kms poll for handling hpd */
	drm_kms_helper_poll_init(drm_dev);

	/*
	 * enable drm irq mode.
	 * - with irq_enabled = true, we can use the vblank feature.
	 */
	drm_dev->irq_enabled = true;

	ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
	if (ret)
		goto err_kms_helper_poll_fini;

212 213
	drm_mode_config_reset(drm_dev);

M
Mark Yao 已提交
214 215 216 217
	ret = rockchip_drm_fbdev_init(drm_dev);
	if (ret)
		goto err_vblank_cleanup;

218 219 220 221
	ret = drm_dev_register(drm_dev, 0);
	if (ret)
		goto err_fbdev_fini;

222 223
	if (is_support_iommu)
		arm_iommu_release_mapping(mapping);
M
Mark Yao 已提交
224
	return 0;
225 226
err_fbdev_fini:
	rockchip_drm_fbdev_fini(drm_dev);
M
Mark Yao 已提交
227 228 229 230 231 232
err_vblank_cleanup:
	drm_vblank_cleanup(drm_dev);
err_kms_helper_poll_fini:
	drm_kms_helper_poll_fini(drm_dev);
	component_unbind_all(dev, drm_dev);
err_detach_device:
233 234
	if (is_support_iommu)
		arm_iommu_detach_device(dev);
M
Mark Yao 已提交
235
err_release_mapping:
236 237
	if (is_support_iommu)
		arm_iommu_release_mapping(mapping);
M
Mark Yao 已提交
238 239 240
err_config_cleanup:
	drm_mode_config_cleanup(drm_dev);
	drm_dev->dev_private = NULL;
241 242
err_free:
	drm_dev_unref(drm_dev);
M
Mark Yao 已提交
243 244 245
	return ret;
}

246
static void rockchip_drm_unbind(struct device *dev)
M
Mark Yao 已提交
247
{
248
	struct drm_device *drm_dev = dev_get_drvdata(dev);
M
Mark Yao 已提交
249 250 251 252 253

	rockchip_drm_fbdev_fini(drm_dev);
	drm_vblank_cleanup(drm_dev);
	drm_kms_helper_poll_fini(drm_dev);
	component_unbind_all(dev, drm_dev);
254 255
	if (is_support_iommu)
		arm_iommu_detach_device(dev);
M
Mark Yao 已提交
256 257
	drm_mode_config_cleanup(drm_dev);
	drm_dev->dev_private = NULL;
258 259 260
	drm_dev_unregister(drm_dev);
	drm_dev_unref(drm_dev);
	dev_set_drvdata(dev, NULL);
M
Mark Yao 已提交
261 262
}

263
static void rockchip_drm_lastclose(struct drm_device *dev)
M
Mark Yao 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
{
	struct rockchip_drm_private *priv = dev->dev_private;

	drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
}

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

static struct drm_driver rockchip_drm_driver = {
284 285
	.driver_features	= DRIVER_MODESET | DRIVER_GEM |
				  DRIVER_PRIME | DRIVER_ATOMIC,
M
Mark Yao 已提交
286
	.lastclose		= rockchip_drm_lastclose,
287
	.get_vblank_counter	= drm_vblank_no_hw_counter,
M
Mark Yao 已提交
288 289
	.enable_vblank		= rockchip_drm_crtc_enable_vblank,
	.disable_vblank		= rockchip_drm_crtc_disable_vblank,
D
Daniel Vetter 已提交
290
	.gem_vm_ops		= &drm_gem_cma_vm_ops,
291
	.gem_free_object_unlocked = rockchip_gem_free_object,
M
Mark Yao 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	.dumb_create		= rockchip_gem_dumb_create,
	.dumb_map_offset	= rockchip_gem_dumb_map_offset,
	.dumb_destroy		= drm_gem_dumb_destroy,
	.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	= rockchip_gem_prime_get_sg_table,
	.gem_prime_vmap		= rockchip_gem_prime_vmap,
	.gem_prime_vunmap	= rockchip_gem_prime_vunmap,
	.gem_prime_mmap		= rockchip_gem_mmap_buf,
	.fops			= &rockchip_drm_driver_fops,
	.name	= DRIVER_NAME,
	.desc	= DRIVER_DESC,
	.date	= DRIVER_DATE,
	.major	= DRIVER_MAJOR,
	.minor	= DRIVER_MINOR,
};

#ifdef CONFIG_PM_SLEEP
312
void rockchip_drm_fb_suspend(struct drm_device *drm)
M
Mark Yao 已提交
313
{
314
	struct rockchip_drm_private *priv = drm->dev_private;
M
Mark Yao 已提交
315

316 317 318 319
	console_lock();
	drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
	console_unlock();
}
M
Mark Yao 已提交
320

321 322 323
void rockchip_drm_fb_resume(struct drm_device *drm)
{
	struct rockchip_drm_private *priv = drm->dev_private;
M
Mark Yao 已提交
324

325 326 327 328
	console_lock();
	drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
	console_unlock();
}
M
Mark Yao 已提交
329

330 331 332 333 334 335 336 337 338 339 340 341 342
static int rockchip_drm_sys_suspend(struct device *dev)
{
	struct drm_device *drm = dev_get_drvdata(dev);
	struct rockchip_drm_private *priv = drm->dev_private;

	drm_kms_helper_poll_disable(drm);
	rockchip_drm_fb_suspend(drm);

	priv->state = drm_atomic_helper_suspend(drm);
	if (IS_ERR(priv->state)) {
		rockchip_drm_fb_resume(drm);
		drm_kms_helper_poll_enable(drm);
		return PTR_ERR(priv->state);
M
Mark Yao 已提交
343 344 345 346 347 348 349 350
	}

	return 0;
}

static int rockchip_drm_sys_resume(struct device *dev)
{
	struct drm_device *drm = dev_get_drvdata(dev);
351
	struct rockchip_drm_private *priv = drm->dev_private;
M
Mark Yao 已提交
352

353 354 355
	drm_atomic_helper_resume(drm, priv->state);
	rockchip_drm_fb_resume(drm);
	drm_kms_helper_poll_enable(drm);
M
Mark Yao 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

	return 0;
}
#endif

static const struct dev_pm_ops rockchip_drm_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
				rockchip_drm_sys_resume)
};

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

	return dev->of_node == np;
}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
static void rockchip_add_endpoints(struct device *dev,
				   struct component_match **match,
				   struct device_node *port)
{
	struct device_node *ep, *remote;

	for_each_child_of_node(port, ep) {
		remote = of_graph_get_remote_port_parent(ep);
		if (!remote || !of_device_is_available(remote)) {
			of_node_put(remote);
			continue;
		} else if (!of_device_is_available(remote->parent)) {
			dev_warn(dev, "parent device of %s is not available\n",
				 remote->full_name);
			of_node_put(remote);
			continue;
		}

		component_match_add(dev, match, compare_of, remote);
		of_node_put(remote);
	}
}

M
Mark Yao 已提交
396 397 398 399 400 401 402
static const struct component_master_ops rockchip_drm_ops = {
	.bind = rockchip_drm_bind,
	.unbind = rockchip_drm_unbind,
};

static int rockchip_drm_platform_probe(struct platform_device *pdev)
{
403 404 405 406 407
	struct device *dev = &pdev->dev;
	struct component_match *match = NULL;
	struct device_node *np = dev->of_node;
	struct device_node *port;
	int i;
M
Mark Yao 已提交
408

409
	if (!np)
M
Mark Yao 已提交
410
		return -ENODEV;
411 412 413 414 415 416
	/*
	 * Bind the crtc ports first, so that
	 * drm_of_find_possible_crtcs called from encoder .bind callbacks
	 * works as expected.
	 */
	for (i = 0;; i++) {
417 418
		struct device_node *iommu;

419 420 421 422 423 424 425 426
		port = of_parse_phandle(np, "ports", i);
		if (!port)
			break;

		if (!of_device_is_available(port->parent)) {
			of_node_put(port);
			continue;
		}
M
Mark Yao 已提交
427

428 429 430 431 432 433 434 435 436 437 438
		iommu = of_parse_phandle(port->parent, "iommus", 0);
		if (!iommu || !of_device_is_available(iommu->parent)) {
			dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
				port->parent->full_name);
			/*
			 * if there is a crtc not support iommu, force set all
			 * crtc use non-iommu buffer.
			 */
			is_support_iommu = false;
		}

439
		of_node_put(iommu);
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
		component_match_add(dev, &match, compare_of, port->parent);
		of_node_put(port);
	}

	if (i == 0) {
		dev_err(dev, "missing 'ports' property\n");
		return -ENODEV;
	}

	if (!match) {
		dev_err(dev, "No available vop found for display-subsystem.\n");
		return -ENODEV;
	}
	/*
	 * For each bound crtc, bind the encoders attached to its
	 * remote endpoint.
	 */
	for (i = 0;; i++) {
		port = of_parse_phandle(np, "ports", i);
		if (!port)
			break;

		if (!of_device_is_available(port->parent)) {
			of_node_put(port);
			continue;
		}

		rockchip_add_endpoints(dev, &match, port);
		of_node_put(port);
	}

	return component_master_add_with_match(dev, &rockchip_drm_ops, match);
M
Mark Yao 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
}

static int rockchip_drm_platform_remove(struct platform_device *pdev)
{
	component_master_del(&pdev->dev, &rockchip_drm_ops);

	return 0;
}

static const struct of_device_id rockchip_drm_dt_ids[] = {
	{ .compatible = "rockchip,display-subsystem", },
	{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);

static struct platform_driver rockchip_drm_platform_driver = {
	.probe = rockchip_drm_platform_probe,
	.remove = rockchip_drm_platform_remove,
	.driver = {
		.name = "rockchip-drm",
		.of_match_table = rockchip_drm_dt_ids,
		.pm = &rockchip_drm_pm_ops,
	},
};

module_platform_driver(rockchip_drm_platform_driver);

MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
MODULE_LICENSE("GPL v2");