mdp5_kms.c 18.7 KB
Newer Older
R
Rob Clark 已提交
1
/*
2
 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
R
Rob Clark 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Copyright (C) 2013 Red Hat
 * Author: Rob Clark <robdclark@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "msm_drv.h"
#include "msm_mmu.h"
#include "mdp5_kms.h"

24 25 26 27
static const char *iommu_ports[] = {
		"mdp_0",
};

28 29 30 31
static int mdp5_hw_init(struct msm_kms *kms)
{
	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
	struct drm_device *dev = mdp5_kms->dev;
32
	unsigned long flags;
33 34 35

	pm_runtime_get_sync(dev->dev);

R
Rob Clark 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	/* Magic unknown register writes:
	 *
	 *    W VBIF:0x004 00000001      (mdss_mdp.c:839)
	 *    W MDP5:0x2e0 0xe9          (mdss_mdp.c:839)
	 *    W MDP5:0x2e4 0x55          (mdss_mdp.c:839)
	 *    W MDP5:0x3ac 0xc0000ccc    (mdss_mdp.c:839)
	 *    W MDP5:0x3b4 0xc0000ccc    (mdss_mdp.c:839)
	 *    W MDP5:0x3bc 0xcccccc      (mdss_mdp.c:839)
	 *    W MDP5:0x4a8 0xcccc0c0     (mdss_mdp.c:839)
	 *    W MDP5:0x4b0 0xccccc0c0    (mdss_mdp.c:839)
	 *    W MDP5:0x4b8 0xccccc000    (mdss_mdp.c:839)
	 *
	 * Downstream fbdev driver gets these register offsets/values
	 * from DT.. not really sure what these registers are or if
	 * different values for different boards/SoC's, etc.  I guess
	 * they are the golden registers.
	 *
	 * Not setting these does not seem to cause any problem.  But
	 * we may be getting lucky with the bootloader initializing
	 * them for us.  OTOH, if we can always count on the bootloader
	 * setting the golden registers, then perhaps we don't need to
	 * care.
	 */

60
	spin_lock_irqsave(&mdp5_kms->resource_lock, flags);
61
	mdp5_write(mdp5_kms, REG_MDP5_MDP_DISP_INTF_SEL(0), 0);
62
	spin_unlock_irqrestore(&mdp5_kms->resource_lock, flags);
R
Rob Clark 已提交
63

64
	mdp5_ctlm_hw_reset(mdp5_kms->ctlm);
65

R
Rob Clark 已提交
66 67
	pm_runtime_put_sync(dev->dev);

68
	return 0;
R
Rob Clark 已提交
69 70
}

71 72 73 74 75 76 77 78
static void mdp5_prepare_commit(struct msm_kms *kms, struct drm_atomic_state *state)
{
	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
	mdp5_enable(mdp5_kms);
}

static void mdp5_complete_commit(struct msm_kms *kms, struct drm_atomic_state *state)
{
79
	int i;
80
	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
81 82 83 84 85 86 87 88 89 90 91 92
	int nplanes = mdp5_kms->dev->mode_config.num_total_plane;

	for (i = 0; i < nplanes; i++) {
		struct drm_plane *plane = state->planes[i];
		struct drm_plane_state *plane_state = state->plane_states[i];

		if (!plane)
			continue;

		mdp5_plane_complete_commit(plane, plane_state);
	}

93 94 95
	mdp5_disable(mdp5_kms);
}

96 97 98 99 100 101
static void mdp5_wait_for_crtc_commit_done(struct msm_kms *kms,
						struct drm_crtc *crtc)
{
	mdp5_crtc_wait_for_commit_done(crtc);
}

R
Rob Clark 已提交
102 103 104 105 106 107
static long mdp5_round_pixclk(struct msm_kms *kms, unsigned long rate,
		struct drm_encoder *encoder)
{
	return rate;
}

108 109 110 111 112 113 114 115 116 117 118 119
static int mdp5_set_split_display(struct msm_kms *kms,
		struct drm_encoder *encoder,
		struct drm_encoder *slave_encoder,
		bool is_cmd_mode)
{
	if (is_cmd_mode)
		return mdp5_cmd_encoder_set_split_display(encoder,
							slave_encoder);
	else
		return mdp5_encoder_set_split_display(encoder, slave_encoder);
}

R
Rob Clark 已提交
120 121 122
static void mdp5_destroy(struct msm_kms *kms)
{
	struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
123 124
	struct msm_mmu *mmu = mdp5_kms->mmu;

R
Rob Clark 已提交
125 126
	mdp5_irq_domain_fini(mdp5_kms);

127 128 129 130
	if (mmu) {
		mmu->funcs->detach(mmu, iommu_ports, ARRAY_SIZE(iommu_ports));
		mmu->funcs->destroy(mmu);
	}
131 132 133 134 135 136 137

	if (mdp5_kms->ctlm)
		mdp5_ctlm_destroy(mdp5_kms->ctlm);
	if (mdp5_kms->smp)
		mdp5_smp_destroy(mdp5_kms->smp);
	if (mdp5_kms->cfg)
		mdp5_cfg_destroy(mdp5_kms->cfg);
138

R
Rob Clark 已提交
139 140 141 142 143 144 145 146 147 148 149 150
	kfree(mdp5_kms);
}

static const struct mdp_kms_funcs kms_funcs = {
	.base = {
		.hw_init         = mdp5_hw_init,
		.irq_preinstall  = mdp5_irq_preinstall,
		.irq_postinstall = mdp5_irq_postinstall,
		.irq_uninstall   = mdp5_irq_uninstall,
		.irq             = mdp5_irq,
		.enable_vblank   = mdp5_enable_vblank,
		.disable_vblank  = mdp5_disable_vblank,
151 152
		.prepare_commit  = mdp5_prepare_commit,
		.complete_commit = mdp5_complete_commit,
153
		.wait_for_crtc_commit_done = mdp5_wait_for_crtc_commit_done,
R
Rob Clark 已提交
154 155
		.get_format      = mdp_get_format,
		.round_pixclk    = mdp5_round_pixclk,
156
		.set_split_display = mdp5_set_split_display,
R
Rob Clark 已提交
157 158 159 160 161 162 163 164 165 166 167 168
		.destroy         = mdp5_destroy,
	},
	.set_irqmask         = mdp5_set_irqmask,
};

int mdp5_disable(struct mdp5_kms *mdp5_kms)
{
	DBG("");

	clk_disable_unprepare(mdp5_kms->ahb_clk);
	clk_disable_unprepare(mdp5_kms->axi_clk);
	clk_disable_unprepare(mdp5_kms->core_clk);
S
Stephane Viau 已提交
169 170
	if (mdp5_kms->lut_clk)
		clk_disable_unprepare(mdp5_kms->lut_clk);
R
Rob Clark 已提交
171 172 173 174 175 176 177 178 179 180 181

	return 0;
}

int mdp5_enable(struct mdp5_kms *mdp5_kms)
{
	DBG("");

	clk_prepare_enable(mdp5_kms->ahb_clk);
	clk_prepare_enable(mdp5_kms->axi_clk);
	clk_prepare_enable(mdp5_kms->core_clk);
S
Stephane Viau 已提交
182 183
	if (mdp5_kms->lut_clk)
		clk_prepare_enable(mdp5_kms->lut_clk);
R
Rob Clark 已提交
184 185 186 187

	return 0;
}

188 189
static struct drm_encoder *construct_encoder(struct mdp5_kms *mdp5_kms,
		enum mdp5_intf_type intf_type, int intf_num,
190
		enum mdp5_intf_mode intf_mode, struct mdp5_ctl *ctl)
191 192 193 194 195 196 197
{
	struct drm_device *dev = mdp5_kms->dev;
	struct msm_drm_private *priv = dev->dev_private;
	struct drm_encoder *encoder;
	struct mdp5_interface intf = {
			.num	= intf_num,
			.type	= intf_type,
198
			.mode	= intf_mode,
199 200
	};

201 202
	if ((intf_type == INTF_DSI) &&
		(intf_mode == MDP5_INTF_DSI_MODE_COMMAND))
203
		encoder = mdp5_cmd_encoder_init(dev, &intf, ctl);
204
	else
205
		encoder = mdp5_encoder_init(dev, &intf, ctl);
206

207
	if (IS_ERR(encoder)) {
208 209
		dev_err(dev->dev, "failed to construct encoder\n");
		return encoder;
210 211 212 213 214
	}

	encoder->possible_crtcs = (1 << priv->num_crtcs) - 1;
	priv->encoders[priv->num_encoders++] = encoder;

215 216 217
	return encoder;
}

218 219
static int get_dsi_id_from_intf(const struct mdp5_cfg_hw *hw_cfg, int intf_num)
{
220 221
	const enum mdp5_intf_type *intfs = hw_cfg->intf.connect;
	const int intf_cnt = ARRAY_SIZE(hw_cfg->intf.connect);
222 223 224 225 226 227 228 229 230 231 232 233 234 235
	int id = 0, i;

	for (i = 0; i < intf_cnt; i++) {
		if (intfs[i] == INTF_DSI) {
			if (intf_num == i)
				return id;

			id++;
		}
	}

	return -EINVAL;
}

236 237 238 239 240 241
static int modeset_init_intf(struct mdp5_kms *mdp5_kms, int intf_num)
{
	struct drm_device *dev = mdp5_kms->dev;
	struct msm_drm_private *priv = dev->dev_private;
	const struct mdp5_cfg_hw *hw_cfg =
					mdp5_cfg_get_hw_config(mdp5_kms->cfg);
242
	enum mdp5_intf_type intf_type = hw_cfg->intf.connect[intf_num];
243 244
	struct mdp5_ctl_manager *ctlm = mdp5_kms->ctlm;
	struct mdp5_ctl *ctl;
245 246 247 248 249 250 251 252 253 254
	struct drm_encoder *encoder;
	int ret = 0;

	switch (intf_type) {
	case INTF_DISABLED:
		break;
	case INTF_eDP:
		if (!priv->edp)
			break;

255 256 257 258 259 260
		ctl = mdp5_ctlm_request(ctlm, intf_num);
		if (!ctl) {
			ret = -EINVAL;
			break;
		}

261
		encoder = construct_encoder(mdp5_kms, INTF_eDP, intf_num,
262
					MDP5_INTF_MODE_NONE, ctl);
263 264 265 266
		if (IS_ERR(encoder)) {
			ret = PTR_ERR(encoder);
			break;
		}
267 268

		ret = msm_edp_modeset_init(priv->edp, dev, encoder);
269 270 271 272 273
		break;
	case INTF_HDMI:
		if (!priv->hdmi)
			break;

274 275 276 277 278 279
		ctl = mdp5_ctlm_request(ctlm, intf_num);
		if (!ctl) {
			ret = -EINVAL;
			break;
		}

280
		encoder = construct_encoder(mdp5_kms, INTF_HDMI, intf_num,
281
					MDP5_INTF_MODE_NONE, ctl);
282 283 284 285 286
		if (IS_ERR(encoder)) {
			ret = PTR_ERR(encoder);
			break;
		}

A
Arnd Bergmann 已提交
287
		ret = msm_hdmi_modeset_init(priv->hdmi, dev, encoder);
288
		break;
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	case INTF_DSI:
	{
		int dsi_id = get_dsi_id_from_intf(hw_cfg, intf_num);
		struct drm_encoder *dsi_encs[MSM_DSI_ENCODER_NUM];
		enum mdp5_intf_mode mode;
		int i;

		if ((dsi_id >= ARRAY_SIZE(priv->dsi)) || (dsi_id < 0)) {
			dev_err(dev->dev, "failed to find dsi from intf %d\n",
				intf_num);
			ret = -EINVAL;
			break;
		}

		if (!priv->dsi[dsi_id])
			break;

306 307 308 309 310 311
		ctl = mdp5_ctlm_request(ctlm, intf_num);
		if (!ctl) {
			ret = -EINVAL;
			break;
		}

312 313 314 315 316
		for (i = 0; i < MSM_DSI_ENCODER_NUM; i++) {
			mode = (i == MSM_DSI_CMD_ENCODER_ID) ?
				MDP5_INTF_DSI_MODE_COMMAND :
				MDP5_INTF_DSI_MODE_VIDEO;
			dsi_encs[i] = construct_encoder(mdp5_kms, INTF_DSI,
317 318 319
							intf_num, mode, ctl);
			if (IS_ERR(dsi_encs[i])) {
				ret = PTR_ERR(dsi_encs[i]);
320 321 322 323 324 325 326
				break;
			}
		}

		ret = msm_dsi_modeset_init(priv->dsi[dsi_id], dev, dsi_encs);
		break;
	}
327 328 329 330
	default:
		dev_err(dev->dev, "unknown intf: %d\n", intf_type);
		ret = -EINVAL;
		break;
331 332 333 334 335
	}

	return ret;
}

R
Rob Clark 已提交
336 337 338
static int modeset_init(struct mdp5_kms *mdp5_kms)
{
	static const enum mdp5_pipe crtcs[] = {
339
			SSPP_RGB0, SSPP_RGB1, SSPP_RGB2, SSPP_RGB3,
R
Rob Clark 已提交
340
	};
341
	static const enum mdp5_pipe vig_planes[] = {
342 343
			SSPP_VIG0, SSPP_VIG1, SSPP_VIG2, SSPP_VIG3,
	};
344 345 346
	static const enum mdp5_pipe dma_planes[] = {
			SSPP_DMA0, SSPP_DMA1,
	};
R
Rob Clark 已提交
347 348
	struct drm_device *dev = mdp5_kms->dev;
	struct msm_drm_private *priv = dev->dev_private;
349
	const struct mdp5_cfg_hw *hw_cfg;
R
Rob Clark 已提交
350 351
	int i, ret;

352
	hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
353

R
Rob Clark 已提交
354 355 356 357 358 359 360
	/* register our interrupt-controller for hdmi/eDP/dsi/etc
	 * to use for irqs routed through mdp:
	 */
	ret = mdp5_irq_domain_init(mdp5_kms);
	if (ret)
		goto fail;

361
	/* construct CRTCs and their private planes: */
362
	for (i = 0; i < hw_cfg->pipe_rgb.count; i++) {
R
Rob Clark 已提交
363 364 365
		struct drm_plane *plane;
		struct drm_crtc *crtc;

366
		plane = mdp5_plane_init(dev, crtcs[i], true,
367
			hw_cfg->pipe_rgb.base[i], hw_cfg->pipe_rgb.caps);
R
Rob Clark 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
		if (IS_ERR(plane)) {
			ret = PTR_ERR(plane);
			dev_err(dev->dev, "failed to construct plane for %s (%d)\n",
					pipe2name(crtcs[i]), ret);
			goto fail;
		}

		crtc  = mdp5_crtc_init(dev, plane, i);
		if (IS_ERR(crtc)) {
			ret = PTR_ERR(crtc);
			dev_err(dev->dev, "failed to construct crtc for %s (%d)\n",
					pipe2name(crtcs[i]), ret);
			goto fail;
		}
		priv->crtcs[priv->num_crtcs++] = crtc;
	}

385
	/* Construct video planes: */
386 387 388
	for (i = 0; i < hw_cfg->pipe_vig.count; i++) {
		struct drm_plane *plane;

389
		plane = mdp5_plane_init(dev, vig_planes[i], false,
390
			hw_cfg->pipe_vig.base[i], hw_cfg->pipe_vig.caps);
391 392 393
		if (IS_ERR(plane)) {
			ret = PTR_ERR(plane);
			dev_err(dev->dev, "failed to construct %s plane: %d\n",
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
					pipe2name(vig_planes[i]), ret);
			goto fail;
		}
	}

	/* DMA planes */
	for (i = 0; i < hw_cfg->pipe_dma.count; i++) {
		struct drm_plane *plane;

		plane = mdp5_plane_init(dev, dma_planes[i], false,
				hw_cfg->pipe_dma.base[i], hw_cfg->pipe_dma.caps);
		if (IS_ERR(plane)) {
			ret = PTR_ERR(plane);
			dev_err(dev->dev, "failed to construct %s plane: %d\n",
					pipe2name(dma_planes[i]), ret);
409 410 411 412
			goto fail;
		}
	}

413 414 415
	/* Construct encoders and modeset initialize connector devices
	 * for each external display interface.
	 */
416
	for (i = 0; i < ARRAY_SIZE(hw_cfg->intf.connect); i++) {
417
		ret = modeset_init_intf(mdp5_kms, i);
418
		if (ret)
419 420 421
			goto fail;
	}

R
Rob Clark 已提交
422 423 424 425 426 427
	return 0;

fail:
	return ret;
}

428 429 430 431 432 433
static void read_hw_revision(struct mdp5_kms *mdp5_kms,
		uint32_t *major, uint32_t *minor)
{
	uint32_t version;

	mdp5_enable(mdp5_kms);
434
	version = mdp5_read(mdp5_kms, REG_MDSS_HW_VERSION);
435 436
	mdp5_disable(mdp5_kms);

437 438
	*major = FIELD(version, MDSS_HW_VERSION_MAJOR);
	*minor = FIELD(version, MDSS_HW_VERSION_MINOR);
439 440 441 442

	DBG("MDP5 version v%d.%d", *major, *minor);
}

R
Rob Clark 已提交
443
static int get_clk(struct platform_device *pdev, struct clk **clkp,
444
		const char *name, bool mandatory)
R
Rob Clark 已提交
445 446 447
{
	struct device *dev = &pdev->dev;
	struct clk *clk = devm_clk_get(dev, name);
448
	if (IS_ERR(clk) && mandatory) {
R
Rob Clark 已提交
449 450 451
		dev_err(dev, "failed to get %s (%ld)\n", name, PTR_ERR(clk));
		return PTR_ERR(clk);
	}
452 453 454 455 456
	if (IS_ERR(clk))
		DBG("skipping %s", name);
	else
		*clkp = clk;

R
Rob Clark 已提交
457 458 459
	return 0;
}

460 461 462 463 464 465 466 467 468 469 470 471 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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 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 578 579 580
static struct drm_encoder *get_encoder_from_crtc(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	struct drm_encoder *encoder;

	drm_for_each_encoder(encoder, dev)
		if (encoder->crtc == crtc)
			return encoder;

	return NULL;
}

static int mdp5_get_scanoutpos(struct drm_device *dev, unsigned int pipe,
			       unsigned int flags, int *vpos, int *hpos,
			       ktime_t *stime, ktime_t *etime,
			       const struct drm_display_mode *mode)
{
	struct msm_drm_private *priv = dev->dev_private;
	struct drm_crtc *crtc;
	struct drm_encoder *encoder;
	int line, vsw, vbp, vactive_start, vactive_end, vfp_end;
	int ret = 0;

	crtc = priv->crtcs[pipe];
	if (!crtc) {
		DRM_ERROR("Invalid crtc %d\n", pipe);
		return 0;
	}

	encoder = get_encoder_from_crtc(crtc);
	if (!encoder) {
		DRM_ERROR("no encoder found for crtc %d\n", pipe);
		return 0;
	}

	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;

	vsw = mode->crtc_vsync_end - mode->crtc_vsync_start;
	vbp = mode->crtc_vtotal - mode->crtc_vsync_end;

	/*
	 * the line counter is 1 at the start of the VSYNC pulse and VTOTAL at
	 * the end of VFP. Translate the porch values relative to the line
	 * counter positions.
	 */

	vactive_start = vsw + vbp + 1;

	vactive_end = vactive_start + mode->crtc_vdisplay;

	/* last scan line before VSYNC */
	vfp_end = mode->crtc_vtotal;

	if (stime)
		*stime = ktime_get();

	line = mdp5_encoder_get_linecount(encoder);

	if (line < vactive_start) {
		line -= vactive_start;
		ret |= DRM_SCANOUTPOS_IN_VBLANK;
	} else if (line > vactive_end) {
		line = line - vfp_end - vactive_start;
		ret |= DRM_SCANOUTPOS_IN_VBLANK;
	} else {
		line -= vactive_start;
	}

	*vpos = line;
	*hpos = 0;

	if (etime)
		*etime = ktime_get();

	return ret;
}

static int mdp5_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
				     int *max_error,
				     struct timeval *vblank_time,
				     unsigned flags)
{
	struct msm_drm_private *priv = dev->dev_private;
	struct drm_crtc *crtc;

	if (pipe < 0 || pipe >= priv->num_crtcs) {
		DRM_ERROR("Invalid crtc %d\n", pipe);
		return -EINVAL;
	}

	crtc = priv->crtcs[pipe];
	if (!crtc) {
		DRM_ERROR("Invalid crtc %d\n", pipe);
		return -EINVAL;
	}

	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
						     vblank_time, flags,
						     &crtc->mode);
}

static u32 mdp5_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
{
	struct msm_drm_private *priv = dev->dev_private;
	struct drm_crtc *crtc;
	struct drm_encoder *encoder;

	if (pipe < 0 || pipe >= priv->num_crtcs)
		return 0;

	crtc = priv->crtcs[pipe];
	if (!crtc)
		return 0;

	encoder = get_encoder_from_crtc(crtc);
	if (!encoder)
		return 0;

	return mdp5_encoder_get_framecount(encoder);
}

R
Rob Clark 已提交
581 582 583
struct msm_kms *mdp5_kms_init(struct drm_device *dev)
{
	struct platform_device *pdev = dev->platformdev;
584
	struct mdp5_cfg *config;
R
Rob Clark 已提交
585 586 587
	struct mdp5_kms *mdp5_kms;
	struct msm_kms *kms = NULL;
	struct msm_mmu *mmu;
588
	uint32_t major, minor;
589
	int i, ret;
R
Rob Clark 已提交
590 591 592 593 594 595 596 597

	mdp5_kms = kzalloc(sizeof(*mdp5_kms), GFP_KERNEL);
	if (!mdp5_kms) {
		dev_err(dev->dev, "failed to allocate kms\n");
		ret = -ENOMEM;
		goto fail;
	}

598 599
	spin_lock_init(&mdp5_kms->resource_lock);

R
Rob Clark 已提交
600 601 602 603 604 605
	mdp_kms_init(&mdp5_kms->base, &kms_funcs);

	kms = &mdp5_kms->base.base;

	mdp5_kms->dev = dev;

606
	/* mdp5_kms->mmio actually represents the MDSS base address */
R
Rob Clark 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	mdp5_kms->mmio = msm_ioremap(pdev, "mdp_phys", "MDP5");
	if (IS_ERR(mdp5_kms->mmio)) {
		ret = PTR_ERR(mdp5_kms->mmio);
		goto fail;
	}

	mdp5_kms->vbif = msm_ioremap(pdev, "vbif_phys", "VBIF");
	if (IS_ERR(mdp5_kms->vbif)) {
		ret = PTR_ERR(mdp5_kms->vbif);
		goto fail;
	}

	mdp5_kms->vdd = devm_regulator_get(&pdev->dev, "vdd");
	if (IS_ERR(mdp5_kms->vdd)) {
		ret = PTR_ERR(mdp5_kms->vdd);
		goto fail;
	}

	ret = regulator_enable(mdp5_kms->vdd);
	if (ret) {
		dev_err(dev->dev, "failed to enable regulator vdd: %d\n", ret);
		goto fail;
	}

631 632
	/* mandatory clocks: */
	ret = get_clk(pdev, &mdp5_kms->axi_clk, "bus_clk", true);
R
Rob Clark 已提交
633 634
	if (ret)
		goto fail;
635
	ret = get_clk(pdev, &mdp5_kms->ahb_clk, "iface_clk", true);
R
Rob Clark 已提交
636 637
	if (ret)
		goto fail;
638
	ret = get_clk(pdev, &mdp5_kms->src_clk, "core_clk_src", true);
R
Rob Clark 已提交
639 640
	if (ret)
		goto fail;
641
	ret = get_clk(pdev, &mdp5_kms->core_clk, "core_clk", true);
R
Rob Clark 已提交
642 643
	if (ret)
		goto fail;
644
	ret = get_clk(pdev, &mdp5_kms->vsync_clk, "vsync_clk", true);
R
Rob Clark 已提交
645 646 647
	if (ret)
		goto fail;

648 649 650
	/* optional clocks: */
	get_clk(pdev, &mdp5_kms->lut_clk, "lut_clk", false);

651 652 653 654 655 656
	/* we need to set a default rate before enabling.  Set a safe
	 * rate first, then figure out hw revision, and then set a
	 * more optimal rate:
	 */
	clk_set_rate(mdp5_kms->src_clk, 200000000);

657
	read_hw_revision(mdp5_kms, &major, &minor);
658 659 660 661 662

	mdp5_kms->cfg = mdp5_cfg_init(mdp5_kms, major, minor);
	if (IS_ERR(mdp5_kms->cfg)) {
		ret = PTR_ERR(mdp5_kms->cfg);
		mdp5_kms->cfg = NULL;
663
		goto fail;
664
	}
665 666

	config = mdp5_cfg_get_config(mdp5_kms->cfg);
667
	mdp5_kms->caps = config->hw->mdp.caps;
668

669
	/* TODO: compute core clock rate at runtime */
670
	clk_set_rate(mdp5_kms->src_clk, config->hw->max_clk);
671

672 673 674 675 676 677 678 679 680 681 682 683
	/*
	 * Some chipsets have a Shared Memory Pool (SMP), while others
	 * have dedicated latency buffering per source pipe instead;
	 * this section initializes the SMP:
	 */
	if (mdp5_kms->caps & MDP_CAP_SMP) {
		mdp5_kms->smp = mdp5_smp_init(mdp5_kms->dev, &config->hw->smp);
		if (IS_ERR(mdp5_kms->smp)) {
			ret = PTR_ERR(mdp5_kms->smp);
			mdp5_kms->smp = NULL;
			goto fail;
		}
684 685
	}

686
	mdp5_kms->ctlm = mdp5_ctlm_init(dev, mdp5_kms->mmio, mdp5_kms->cfg);
687 688 689
	if (IS_ERR(mdp5_kms->ctlm)) {
		ret = PTR_ERR(mdp5_kms->ctlm);
		mdp5_kms->ctlm = NULL;
690 691 692
		goto fail;
	}

R
Rob Clark 已提交
693 694 695 696 697
	/* make sure things are off before attaching iommu (bootloader could
	 * have left things on, in which case we'll start getting faults if
	 * we don't disable):
	 */
	mdp5_enable(mdp5_kms);
698
	for (i = 0; i < MDP5_INTF_NUM_MAX; i++) {
699 700
		if (mdp5_cfg_intf_is_virtual(config->hw->intf.connect[i]) ||
				!config->hw->intf.base[i])
701
			continue;
702
		mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(i), 0);
703 704

		mdp5_write(mdp5_kms, REG_MDP5_INTF_FRAME_LINE_COUNT_EN(i), 0x3);
705
	}
R
Rob Clark 已提交
706 707 708
	mdp5_disable(mdp5_kms);
	mdelay(16);

709 710
	if (config->platform.iommu) {
		mmu = msm_iommu_new(&pdev->dev, config->platform.iommu);
R
Rob Clark 已提交
711 712
		if (IS_ERR(mmu)) {
			ret = PTR_ERR(mmu);
713
			dev_err(dev->dev, "failed to init iommu: %d\n", ret);
714
			iommu_domain_free(config->platform.iommu);
R
Rob Clark 已提交
715 716
			goto fail;
		}
717

R
Rob Clark 已提交
718 719
		ret = mmu->funcs->attach(mmu, iommu_ports,
				ARRAY_SIZE(iommu_ports));
720 721 722
		if (ret) {
			dev_err(dev->dev, "failed to attach iommu: %d\n", ret);
			mmu->funcs->destroy(mmu);
R
Rob Clark 已提交
723
			goto fail;
724
		}
R
Rob Clark 已提交
725 726 727 728 729
	} else {
		dev_info(dev->dev, "no iommu, fallback to phys "
				"contig buffers for scanout\n");
		mmu = NULL;
	}
730
	mdp5_kms->mmu = mmu;
R
Rob Clark 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744

	mdp5_kms->id = msm_register_mmu(dev, mmu);
	if (mdp5_kms->id < 0) {
		ret = mdp5_kms->id;
		dev_err(dev->dev, "failed to register mdp5 iommu: %d\n", ret);
		goto fail;
	}

	ret = modeset_init(mdp5_kms);
	if (ret) {
		dev_err(dev->dev, "modeset_init failed: %d\n", ret);
		goto fail;
	}

745 746 747 748 749
	dev->mode_config.min_width = 0;
	dev->mode_config.min_height = 0;
	dev->mode_config.max_width = config->hw->lm.max_width;
	dev->mode_config.max_height = config->hw->lm.max_height;

750 751 752 753 754 755
	dev->driver->get_vblank_timestamp = mdp5_get_vblank_timestamp;
	dev->driver->get_scanout_position = mdp5_get_scanoutpos;
	dev->driver->get_vblank_counter = mdp5_get_vblank_counter;
	dev->max_vblank_count = 0xffffffff;
	dev->vblank_disable_immediate = true;

R
Rob Clark 已提交
756 757 758 759 760 761 762
	return kms;

fail:
	if (kms)
		mdp5_destroy(kms);
	return ERR_PTR(ret);
}