tilcdc_crtc.c 27.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2012 Texas Instruments
 * 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/>.
 */

18
#include <drm/drm_atomic.h>
19
#include <drm/drm_atomic_helper.h>
20 21 22
#include <drm/drm_crtc.h>
#include <drm/drm_flip_work.h>
#include <drm/drm_plane_helper.h>
23
#include <linux/workqueue.h>
24 25
#include <linux/completion.h>
#include <linux/dma-mapping.h>
26 27 28 29

#include "tilcdc_drv.h"
#include "tilcdc_regs.h"

30 31 32
#define TILCDC_VBLANK_SAFETY_THRESHOLD_US	1000
#define TILCDC_REV1_PALETTE_SIZE		32
#define TILCDC_REV1_PALETTE_FIRST_ENTRY		0x4000
33

34 35 36
struct tilcdc_crtc {
	struct drm_crtc base;

37
	struct drm_plane primary;
38 39
	const struct tilcdc_panel_info *info;
	struct drm_pending_vblank_event *event;
40
	struct mutex enable_lock;
41
	bool enabled;
42
	bool shutdown;
43 44
	wait_queue_head_t frame_done_wq;
	bool frame_done;
45 46
	spinlock_t irq_lock;

47 48
	unsigned int lcd_fck_rate;

49
	ktime_t last_vblank;
50

51
	struct drm_framebuffer *curr_fb;
52
	struct drm_framebuffer *next_fb;
53 54

	/* for deferred fb unref's: */
R
Rob Clark 已提交
55
	struct drm_flip_work unref_work;
56 57 58

	/* Only set if an external encoder is connected */
	bool simulate_vesa_sync;
59 60 61

	int sync_lost_count;
	bool frame_intact;
62
	struct work_struct recover_work;
63 64 65 66

	dma_addr_t palette_dma_handle;
	void *palette_base;
	struct completion palette_loaded;
67 68 69
};
#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)

R
Rob Clark 已提交
70
static void unref_worker(struct drm_flip_work *work, void *val)
71
{
72
	struct tilcdc_crtc *tilcdc_crtc =
R
Rob Clark 已提交
73
		container_of(work, struct tilcdc_crtc, unref_work);
74 75 76
	struct drm_device *dev = tilcdc_crtc->base.dev;

	mutex_lock(&dev->mode_config.mutex);
R
Rob Clark 已提交
77
	drm_framebuffer_unreference(val);
78 79 80
	mutex_unlock(&dev->mode_config.mutex);
}

81
static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
82 83 84
{
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
	struct drm_device *dev = crtc->dev;
85
	struct tilcdc_drm_private *priv = dev->dev_private;
86
	struct drm_gem_cma_object *gem;
87
	dma_addr_t start, end;
88
	u64 dma_base_and_ceiling;
89 90 91

	gem = drm_fb_cma_get_gem_obj(fb, 0);

92 93
	start = gem->paddr + fb->offsets[0] +
		crtc->y * fb->pitches[0] +
94
		crtc->x * drm_format_plane_cpp(fb->pixel_format, 0);
95

96
	end = start + (crtc->mode.vdisplay * fb->pitches[0]);
97

98 99 100 101 102
	/* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
	 * with a single insruction, if available. This should make it more
	 * unlikely that LCDC would fetch the DMA addresses in the middle of
	 * an update.
	 */
103 104 105 106
	if (priv->rev == 1)
		end -= 1;

	dma_base_and_ceiling = (u64)end << 32 | start;
107
	tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
108 109 110 111 112 113

	if (tilcdc_crtc->curr_fb)
		drm_flip_work_queue(&tilcdc_crtc->unref_work,
			tilcdc_crtc->curr_fb);

	tilcdc_crtc->curr_fb = fb;
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/*
 * The driver currently only supports the RGB565 format for revision 1. For
 * 16 bits-per-pixel the palette block is bypassed, but the first 32 bytes of
 * the framebuffer are still considered palette. The first 16-bit entry must
 * be 0x4000 while all other entries must be zeroed.
 */
static void tilcdc_crtc_load_palette(struct drm_crtc *crtc)
{
	u32 dma_fb_base, dma_fb_ceiling, raster_ctl;
	struct tilcdc_crtc *tilcdc_crtc;
	struct drm_device *dev;
	u16 *first_entry;

	dev = crtc->dev;
	tilcdc_crtc = to_tilcdc_crtc(crtc);
	first_entry = tilcdc_crtc->palette_base;

	*first_entry = TILCDC_REV1_PALETTE_FIRST_ENTRY;

	dma_fb_base = tilcdc_read(dev, LCDC_DMA_FB_BASE_ADDR_0_REG);
	dma_fb_ceiling = tilcdc_read(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG);
	raster_ctl = tilcdc_read(dev, LCDC_RASTER_CTRL_REG);

	/* Tell the LCDC where the palette is located. */
	tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG,
		     tilcdc_crtc->palette_dma_handle);
	tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG,
		     (u32)tilcdc_crtc->palette_dma_handle
				+ TILCDC_REV1_PALETTE_SIZE - 1);

	/* Load it. */
	tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
		     LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
	tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
		   LCDC_PALETTE_LOAD_MODE(PALETTE_ONLY));

	/* Enable the LCDC and wait for palette to be loaded. */
	tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_PL_INT_ENA);
	tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);

	wait_for_completion(&tilcdc_crtc->palette_loaded);

	/* Restore the registers. */
	tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
	tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_fb_base);
	tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, dma_fb_ceiling);
	tilcdc_write(dev, LCDC_RASTER_CTRL_REG, raster_ctl);
}

165 166 167 168 169 170 171 172
static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
{
	struct tilcdc_drm_private *priv = dev->dev_private;

	tilcdc_clear_irqstatus(dev, 0xffffffff);

	if (priv->rev == 1) {
		tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
173
			LCDC_V1_SYNC_LOST_INT_ENA |
174
			LCDC_V1_UNDERFLOW_INT_ENA);
175 176
		tilcdc_set(dev, LCDC_DMA_CTRL_REG,
			LCDC_V1_END_OF_FRAME_INT_ENA);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	} else {
		tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
			LCDC_V2_UNDERFLOW_INT_ENA |
			LCDC_V2_END_OF_FRAME0_INT_ENA |
			LCDC_FRAME_DONE | LCDC_SYNC_LOST);
	}
}

static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
{
	struct tilcdc_drm_private *priv = dev->dev_private;

	/* disable irqs that we might have enabled: */
	if (priv->rev == 1) {
		tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
192
			LCDC_V1_SYNC_LOST_INT_ENA |
193 194 195 196 197 198 199 200 201 202 203
			LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
		tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
			LCDC_V1_END_OF_FRAME_INT_ENA);
	} else {
		tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
			LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
			LCDC_V2_END_OF_FRAME0_INT_ENA |
			LCDC_FRAME_DONE | LCDC_SYNC_LOST);
	}
}

204
static void reset(struct drm_crtc *crtc)
205 206 207 208
{
	struct drm_device *dev = crtc->dev;
	struct tilcdc_drm_private *priv = dev->dev_private;

209 210 211 212 213 214 215 216
	if (priv->rev != 2)
		return;

	tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
	usleep_range(250, 1000);
	tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
}

217
static void tilcdc_crtc_enable(struct drm_crtc *crtc)
218 219
{
	struct drm_device *dev = crtc->dev;
220
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
221
	struct tilcdc_drm_private *priv = dev->dev_private;
222

223
	WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
224 225 226
	mutex_lock(&tilcdc_crtc->enable_lock);
	if (tilcdc_crtc->enabled || tilcdc_crtc->shutdown) {
		mutex_unlock(&tilcdc_crtc->enable_lock);
227
		return;
228
	}
229 230

	pm_runtime_get_sync(dev->dev);
231 232

	reset(crtc);
233

234 235 236
	if (priv->rev == 1 && !completion_done(&tilcdc_crtc->palette_loaded))
		tilcdc_crtc_load_palette(crtc);

237 238
	tilcdc_crtc_enable_irqs(dev);

239
	tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
240 241 242
	tilcdc_write_mask(dev, LCDC_RASTER_CTRL_REG,
			  LCDC_PALETTE_LOAD_MODE(DATA_ONLY),
			  LCDC_PALETTE_LOAD_MODE_MASK);
243
	tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
244 245

	drm_crtc_vblank_on(crtc);
246 247

	tilcdc_crtc->enabled = true;
248
	mutex_unlock(&tilcdc_crtc->enable_lock);
249 250
}

251
static void tilcdc_crtc_off(struct drm_crtc *crtc, bool shutdown)
252
{
253
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
254
	struct drm_device *dev = crtc->dev;
255
	struct tilcdc_drm_private *priv = dev->dev_private;
256

257 258 259 260 261
	mutex_lock(&tilcdc_crtc->enable_lock);
	if (shutdown)
		tilcdc_crtc->shutdown = true;
	if (!tilcdc_crtc->enabled) {
		mutex_unlock(&tilcdc_crtc->enable_lock);
262
		return;
263
	}
264
	tilcdc_crtc->frame_done = false;
265
	tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
266 267 268 269 270 271 272 273

	/*
	 * if necessary wait for framedone irq which will still come
	 * before putting things to sleep..
	 */
	if (priv->rev == 2) {
		int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
					     tilcdc_crtc->frame_done,
274
					     msecs_to_jiffies(500));
275 276 277 278
		if (ret == 0)
			dev_err(dev->dev, "%s: timeout waiting for framedone\n",
				__func__);
	}
279

280 281 282 283 284 285 286
	/*
	 * LCDC will not retain the palette when reset. Make sure it gets
	 * reloaded on tilcdc_crtc_enable().
	 */
	if (priv->rev == 1)
		reinit_completion(&tilcdc_crtc->palette_loaded);

287
	drm_crtc_vblank_off(crtc);
288 289

	tilcdc_crtc_disable_irqs(dev);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

	pm_runtime_put_sync(dev->dev);

	if (tilcdc_crtc->next_fb) {
		drm_flip_work_queue(&tilcdc_crtc->unref_work,
				    tilcdc_crtc->next_fb);
		tilcdc_crtc->next_fb = NULL;
	}

	if (tilcdc_crtc->curr_fb) {
		drm_flip_work_queue(&tilcdc_crtc->unref_work,
				    tilcdc_crtc->curr_fb);
		tilcdc_crtc->curr_fb = NULL;
	}

	drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
	tilcdc_crtc->last_vblank = ktime_set(0, 0);

	tilcdc_crtc->enabled = false;
309
	mutex_unlock(&tilcdc_crtc->enable_lock);
310 311
}

312 313 314
static void tilcdc_crtc_disable(struct drm_crtc *crtc)
{
	WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
315 316 317 318 319 320
	tilcdc_crtc_off(crtc, false);
}

void tilcdc_crtc_shutdown(struct drm_crtc *crtc)
{
	tilcdc_crtc_off(crtc, true);
321 322
}

323 324 325
static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
{
	return crtc->state && crtc->state->enable && crtc->state->active;
326 327
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
static void tilcdc_crtc_recover_work(struct work_struct *work)
{
	struct tilcdc_crtc *tilcdc_crtc =
		container_of(work, struct tilcdc_crtc, recover_work);
	struct drm_crtc *crtc = &tilcdc_crtc->base;

	dev_info(crtc->dev->dev, "%s: Reset CRTC", __func__);

	drm_modeset_lock_crtc(crtc, NULL);

	if (!tilcdc_crtc_is_on(crtc))
		goto out;

	tilcdc_crtc_disable(crtc);
	tilcdc_crtc_enable(crtc);
out:
	drm_modeset_unlock_crtc(crtc);
}

347 348 349
static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
{
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
350
	struct tilcdc_drm_private *priv = crtc->dev->dev_private;
351

352
	drm_modeset_lock_crtc(crtc, NULL);
353
	tilcdc_crtc_disable(crtc);
354
	drm_modeset_unlock_crtc(crtc);
355

356
	flush_workqueue(priv->wq);
357

J
Jyri Sarha 已提交
358
	of_node_put(crtc->port);
359
	drm_crtc_cleanup(crtc);
R
Rob Clark 已提交
360
	drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
361 362
}

363
int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
364
		struct drm_framebuffer *fb,
365
		struct drm_pending_vblank_event *event)
366 367 368
{
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
	struct drm_device *dev = crtc->dev;
369
	unsigned long flags;
T
Tomi Valkeinen 已提交
370

371 372
	WARN_ON(!drm_modeset_is_locked(&crtc->mutex));

373 374 375 376 377
	if (tilcdc_crtc->event) {
		dev_err(dev->dev, "already pending page flip!\n");
		return -EBUSY;
	}

378 379
	drm_framebuffer_reference(fb);

380
	crtc->primary->fb = fb;
381

382 383
	spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);

384 385 386
	if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
		ktime_t next_vblank;
		s64 tdiff;
387

388 389
		next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
			1000000 / crtc->hwmode.vrefresh);
390

391 392 393 394 395 396 397
		tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));

		if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
			tilcdc_crtc->next_fb = fb;
	}

	if (tilcdc_crtc->next_fb != fb)
398
		set_scanout(crtc, fb);
399 400

	tilcdc_crtc->event = event;
401 402

	spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
403 404 405 406 407 408 409 410

	return 0;
}

static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
		const struct drm_display_mode *mode,
		struct drm_display_mode *adjusted_mode)
{
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);

	if (!tilcdc_crtc->simulate_vesa_sync)
		return true;

	/*
	 * tilcdc does not generate VESA-compliant sync but aligns
	 * VS on the second edge of HS instead of first edge.
	 * We use adjusted_mode, to fixup sync by aligning both rising
	 * edges and add HSKEW offset to fix the sync.
	 */
	adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
	adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;

	if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
		adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
		adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
	} else {
		adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
		adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
	}

433 434 435
	return true;
}

436 437 438 439 440 441 442 443 444 445 446 447
/*
 * Calculate the percentage difference between the requested pixel clock rate
 * and the effective rate resulting from calculating the clock divider value.
 */
static unsigned int tilcdc_pclk_diff(unsigned long rate,
				     unsigned long real_rate)
{
	int r = rate / 100, rr = real_rate / 100;

	return (unsigned int)(abs(((rr - r) * 100) / r));
}

448 449 450 451 452
static void tilcdc_crtc_set_clk(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	struct tilcdc_drm_private *priv = dev->dev_private;
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
453 454
	unsigned long clk_rate, real_rate, req_rate;
	unsigned int clkdiv;
455 456
	int ret;

457 458
	clkdiv = 2; /* first try using a standard divider of 2 */

459
	/* mode.clock is in KHz, set_rate wants parameter in Hz */
460 461 462 463
	req_rate = crtc->mode.clock * 1000;

	ret = clk_set_rate(priv->clk, req_rate * clkdiv);
	clk_rate = clk_get_rate(priv->clk);
464
	if (ret < 0) {
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
		/*
		 * If we fail to set the clock rate (some architectures don't
		 * use the common clock framework yet and may not implement
		 * all the clk API calls for every clock), try the next best
		 * thing: adjusting the clock divider, unless clk_get_rate()
		 * failed as well.
		 */
		if (!clk_rate) {
			/* Nothing more we can do. Just bail out. */
			dev_err(dev->dev,
				"failed to set the pixel clock - unable to read current lcdc clock rate\n");
			return;
		}

		clkdiv = DIV_ROUND_CLOSEST(clk_rate, req_rate);

		/*
		 * Emit a warning if the real clock rate resulting from the
		 * calculated divider differs much from the requested rate.
		 *
		 * 5% is an arbitrary value - LCDs are usually quite tolerant
		 * about pixel clock rates.
		 */
		real_rate = clkdiv * req_rate;

		if (tilcdc_pclk_diff(clk_rate, real_rate) > 5) {
			dev_warn(dev->dev,
				 "effective pixel clock rate (%luHz) differs from the calculated rate (%luHz)\n",
				 clk_rate, real_rate);
		}
495 496
	}

497
	tilcdc_crtc->lcd_fck_rate = clk_rate;
498 499 500 501 502 503 504 505 506 507 508 509 510 511

	DBG("lcd_clk=%u, mode clock=%d, div=%u",
	    tilcdc_crtc->lcd_fck_rate, crtc->mode.clock, clkdiv);

	/* Configure the LCD clock divisor. */
	tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
		     LCDC_RASTER_MODE);

	if (priv->rev == 2)
		tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
				LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
				LCDC_V2_CORE_CLK_EN);
}

512 513 514 515 516 517 518 519 520 521
static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
{
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
	struct drm_device *dev = crtc->dev;
	struct tilcdc_drm_private *priv = dev->dev_private;
	const struct tilcdc_panel_info *info = tilcdc_crtc->info;
	uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
	struct drm_framebuffer *fb = crtc->primary->state->fb;

522 523
	WARN_ON(!drm_modeset_is_locked(&crtc->mutex));

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 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	if (WARN_ON(!info))
		return;

	if (WARN_ON(!fb))
		return;

	/* Configure the Burst Size and fifo threshold of DMA: */
	reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
	switch (info->dma_burst_sz) {
	case 1:
		reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
		break;
	case 2:
		reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
		break;
	case 4:
		reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
		break;
	case 8:
		reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
		break;
	case 16:
		reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
		break;
	default:
		dev_err(dev->dev, "invalid burst size\n");
		return;
	}
	reg |= (info->fifo_th << 8);
	tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);

	/* Configure timings: */
	hbp = mode->htotal - mode->hsync_end;
	hfp = mode->hsync_start - mode->hdisplay;
	hsw = mode->hsync_end - mode->hsync_start;
	vbp = mode->vtotal - mode->vsync_end;
	vfp = mode->vsync_start - mode->vdisplay;
	vsw = mode->vsync_end - mode->vsync_start;

	DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
	    mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);

	/* Set AC Bias Period and Number of Transitions per Interrupt: */
	reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
	reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
		LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);

	/*
	 * subtract one from hfp, hbp, hsw because the hardware uses
	 * a value of 0 as 1
	 */
	if (priv->rev == 2) {
		/* clear bits we're going to set */
		reg &= ~0x78000033;
		reg |= ((hfp-1) & 0x300) >> 8;
		reg |= ((hbp-1) & 0x300) >> 4;
		reg |= ((hsw-1) & 0x3c0) << 21;
	}
	tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);

	reg = (((mode->hdisplay >> 4) - 1) << 4) |
		(((hbp-1) & 0xff) << 24) |
		(((hfp-1) & 0xff) << 16) |
		(((hsw-1) & 0x3f) << 10);
	if (priv->rev == 2)
		reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
	tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);

	reg = ((mode->vdisplay - 1) & 0x3ff) |
		((vbp & 0xff) << 24) |
		((vfp & 0xff) << 16) |
		(((vsw-1) & 0x3f) << 10);
	tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);

	/*
	 * be sure to set Bit 10 for the V2 LCDC controller,
	 * otherwise limited to 1024 pixels width, stopping
	 * 1920x1080 being supported.
	 */
	if (priv->rev == 2) {
		if ((mode->vdisplay - 1) & 0x400) {
			tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
				LCDC_LPP_B10);
		} else {
			tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
				LCDC_LPP_B10);
		}
	}

	/* Configure display type: */
	reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
		~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
		  LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
		  0x000ff000 /* Palette Loading Delay bits */);
	reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
	if (info->tft_alt_mode)
		reg |= LCDC_TFT_ALT_ENABLE;
	if (priv->rev == 2) {
622 623 624
		switch (fb->pixel_format) {
		case DRM_FORMAT_BGR565:
		case DRM_FORMAT_RGB565:
625
			break;
626 627
		case DRM_FORMAT_XBGR8888:
		case DRM_FORMAT_XRGB8888:
628 629
			reg |= LCDC_V2_TFT_24BPP_UNPACK;
			/* fallthrough */
630 631
		case DRM_FORMAT_BGR888:
		case DRM_FORMAT_RGB888:
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
			reg |= LCDC_V2_TFT_24BPP_MODE;
			break;
		default:
			dev_err(dev->dev, "invalid pixel format\n");
			return;
		}
	}
	reg |= info->fdd < 12;
	tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);

	if (info->invert_pxl_clk)
		tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
	else
		tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);

	if (info->sync_ctrl)
		tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
	else
		tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);

	if (info->sync_edge)
		tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
	else
		tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);

	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
		tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
	else
		tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);

	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
		tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
	else
		tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);

	if (info->raster_order)
		tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
	else
		tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);

	drm_framebuffer_reference(fb);

	set_scanout(crtc, fb);

676
	tilcdc_crtc_set_clk(crtc);
677 678 679 680

	crtc->hwmode = crtc->state->adjusted_mode;
}

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
				    struct drm_crtc_state *state)
{
	struct drm_display_mode *mode = &state->mode;
	int ret;

	/* If we are not active we don't care */
	if (!state->active)
		return 0;

	if (state->state->planes[0].ptr != crtc->primary ||
	    state->state->planes[0].state == NULL ||
	    state->state->planes[0].state->crtc != crtc) {
		dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
		return -EINVAL;
	}

	ret = tilcdc_crtc_mode_valid(crtc, mode);
	if (ret) {
		dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
		return -EINVAL;
	}

	return 0;
}

707
static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
708 709 710 711 712 713
	.destroy        = tilcdc_crtc_destroy,
	.set_config     = drm_atomic_helper_set_config,
	.page_flip      = drm_atomic_helper_page_flip,
	.reset		= drm_atomic_helper_crtc_reset,
	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
714 715 716 717
};

static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
		.mode_fixup     = tilcdc_crtc_mode_fixup,
718 719
		.enable		= tilcdc_crtc_enable,
		.disable	= tilcdc_crtc_disable,
720
		.atomic_check	= tilcdc_crtc_atomic_check,
721
		.mode_set_nofb	= tilcdc_crtc_mode_set_nofb,
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
};

int tilcdc_crtc_max_width(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	struct tilcdc_drm_private *priv = dev->dev_private;
	int max_width = 0;

	if (priv->rev == 1)
		max_width = 1024;
	else if (priv->rev == 2)
		max_width = 2048;

	return max_width;
}

int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
{
	struct tilcdc_drm_private *priv = crtc->dev->dev_private;
	unsigned int bandwidth;
742
	uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
743

744 745 746 747
	/*
	 * check to see if the width is within the range that
	 * the LCD Controller physically supports
	 */
748 749 750 751 752 753 754 755 756 757
	if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
		return MODE_VIRTUAL_X;

	/* width must be multiple of 16 */
	if (mode->hdisplay & 0xf)
		return MODE_VIRTUAL_X;

	if (mode->vdisplay > 2048)
		return MODE_VIRTUAL_Y;

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	DBG("Processing mode %dx%d@%d with pixel clock %d",
		mode->hdisplay, mode->vdisplay,
		drm_mode_vrefresh(mode), mode->clock);

	hbp = mode->htotal - mode->hsync_end;
	hfp = mode->hsync_start - mode->hdisplay;
	hsw = mode->hsync_end - mode->hsync_start;
	vbp = mode->vtotal - mode->vsync_end;
	vfp = mode->vsync_start - mode->vdisplay;
	vsw = mode->vsync_end - mode->vsync_start;

	if ((hbp-1) & ~0x3ff) {
		DBG("Pruning mode: Horizontal Back Porch out of range");
		return MODE_HBLANK_WIDE;
	}

	if ((hfp-1) & ~0x3ff) {
		DBG("Pruning mode: Horizontal Front Porch out of range");
		return MODE_HBLANK_WIDE;
	}

	if ((hsw-1) & ~0x3ff) {
		DBG("Pruning mode: Horizontal Sync Width out of range");
		return MODE_HSYNC_WIDE;
	}

	if (vbp & ~0xff) {
		DBG("Pruning mode: Vertical Back Porch out of range");
		return MODE_VBLANK_WIDE;
	}

	if (vfp & ~0xff) {
		DBG("Pruning mode: Vertical Front Porch out of range");
		return MODE_VBLANK_WIDE;
	}

	if ((vsw-1) & ~0x3f) {
		DBG("Pruning mode: Vertical Sync Width out of range");
		return MODE_VSYNC_WIDE;
	}

799 800 801 802 803
	/*
	 * some devices have a maximum allowed pixel clock
	 * configured from the DT
	 */
	if (mode->clock > priv->max_pixelclock) {
804
		DBG("Pruning mode: pixel clock too high");
805 806 807 808 809 810 811 812 813 814
		return MODE_CLOCK_HIGH;
	}

	/*
	 * some devices further limit the max horizontal resolution
	 * configured from the DT
	 */
	if (mode->hdisplay > priv->max_width)
		return MODE_BAD_WIDTH;

815
	/* filter out modes that would require too much memory bandwidth: */
816 817 818
	bandwidth = mode->hdisplay * mode->vdisplay *
		drm_mode_vrefresh(mode);
	if (bandwidth > priv->max_bandwidth) {
819
		DBG("Pruning mode: exceeds defined bandwidth limit");
820
		return MODE_BAD;
821
	}
822 823 824 825 826 827 828 829 830 831 832

	return MODE_OK;
}

void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
		const struct tilcdc_panel_info *info)
{
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
	tilcdc_crtc->info = info;
}

833 834 835 836 837 838 839 840
void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
					bool simulate_vesa_sync)
{
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);

	tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
}

841 842 843 844
void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
{
	struct drm_device *dev = crtc->dev;
	struct tilcdc_drm_private *priv = dev->dev_private;
845
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
846

847 848 849 850 851
	drm_modeset_lock_crtc(crtc, NULL);
	if (tilcdc_crtc->lcd_fck_rate != clk_get_rate(priv->clk)) {
		if (tilcdc_crtc_is_on(crtc)) {
			pm_runtime_get_sync(dev->dev);
			tilcdc_crtc_disable(crtc);
852

853
			tilcdc_crtc_set_clk(crtc);
854

855 856 857
			tilcdc_crtc_enable(crtc);
			pm_runtime_put_sync(dev->dev);
		}
858
	}
859
	drm_modeset_unlock_crtc(crtc);
860 861
}

862 863
#define SYNC_LOST_COUNT_LIMIT 50

864 865 866 867 868
irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
{
	struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
	struct drm_device *dev = crtc->dev;
	struct tilcdc_drm_private *priv = dev->dev_private;
869
	uint32_t stat;
870

871 872 873
	stat = tilcdc_read_irqstatus(dev);
	tilcdc_clear_irqstatus(dev, stat);

874
	if (stat & LCDC_END_OF_FRAME0) {
875
		unsigned long flags;
876 877 878 879
		bool skip_event = false;
		ktime_t now;

		now = ktime_get();
880

881
		drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
882

883 884 885 886 887 888 889 890 891 892 893 894
		spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);

		tilcdc_crtc->last_vblank = now;

		if (tilcdc_crtc->next_fb) {
			set_scanout(crtc, tilcdc_crtc->next_fb);
			tilcdc_crtc->next_fb = NULL;
			skip_event = true;
		}

		spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);

895
		drm_crtc_handle_vblank(crtc);
896

897 898
		if (!skip_event) {
			struct drm_pending_vblank_event *event;
899

900 901 902
			spin_lock_irqsave(&dev->event_lock, flags);

			event = tilcdc_crtc->event;
903
			tilcdc_crtc->event = NULL;
904
			if (event)
905
				drm_crtc_send_vblank_event(crtc, event);
906

907 908
			spin_unlock_irqrestore(&dev->event_lock, flags);
		}
909 910 911 912 913

		if (tilcdc_crtc->frame_intact)
			tilcdc_crtc->sync_lost_count = 0;
		else
			tilcdc_crtc->frame_intact = true;
914 915
	}

916
	if (stat & LCDC_FIFO_UNDERFLOW)
917
		dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underflow",
918 919
				    __func__, stat);

920 921 922 923 924 925 926 927
	if (priv->rev == 1) {
		if (stat & LCDC_PL_LOAD_DONE) {
			complete(&tilcdc_crtc->palette_loaded);
			tilcdc_clear(dev,
				     LCDC_RASTER_CTRL_REG, LCDC_V1_PL_INT_ENA);
		}
	}

928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
	if (stat & LCDC_SYNC_LOST) {
		dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
				    __func__, stat);
		tilcdc_crtc->frame_intact = false;
		if (tilcdc_crtc->sync_lost_count++ >
		    SYNC_LOST_COUNT_LIMIT) {
			dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, recovering", __func__, stat);
			queue_work(system_wq, &tilcdc_crtc->recover_work);
			if (priv->rev == 1)
				tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
					     LCDC_V1_SYNC_LOST_INT_ENA);
			else
				tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
					     LCDC_SYNC_LOST);
			tilcdc_crtc->sync_lost_count = 0;
		}
	}

946
	/* For revision 2 only */
947 948 949 950 951 952
	if (priv->rev == 2) {
		if (stat & LCDC_FRAME_DONE) {
			tilcdc_crtc->frame_done = true;
			wake_up(&tilcdc_crtc->frame_done_wq);
		}

953 954 955 956 957
		/* Indicate to LCDC that the interrupt service routine has
		 * completed, see 13.3.6.1.6 in AM335x TRM.
		 */
		tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
	}
958

959 960 961
	return IRQ_HANDLED;
}

962
int tilcdc_crtc_create(struct drm_device *dev)
963
{
J
Jyri Sarha 已提交
964
	struct tilcdc_drm_private *priv = dev->dev_private;
965 966 967 968
	struct tilcdc_crtc *tilcdc_crtc;
	struct drm_crtc *crtc;
	int ret;

969
	tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
970 971
	if (!tilcdc_crtc) {
		dev_err(dev->dev, "allocation failed\n");
972
		return -ENOMEM;
973 974
	}

975 976 977 978 979 980 981
	if (priv->rev == 1) {
		init_completion(&tilcdc_crtc->palette_loaded);
		tilcdc_crtc->palette_base = dmam_alloc_coherent(dev->dev,
					TILCDC_REV1_PALETTE_SIZE,
					&tilcdc_crtc->palette_dma_handle,
					GFP_KERNEL | __GFP_ZERO);
		if (!tilcdc_crtc->palette_base)
982
			return -ENOMEM;
983 984
	}

985 986
	crtc = &tilcdc_crtc->base;

987 988 989 990
	ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
	if (ret < 0)
		goto fail;

991 992
	mutex_init(&tilcdc_crtc->enable_lock);

993 994
	init_waitqueue_head(&tilcdc_crtc->frame_done_wq);

995
	drm_flip_work_init(&tilcdc_crtc->unref_work,
R
Rob Clark 已提交
996
			"unref", unref_worker);
997

998
	spin_lock_init(&tilcdc_crtc->irq_lock);
999
	INIT_WORK(&tilcdc_crtc->recover_work, tilcdc_crtc_recover_work);
1000

1001 1002 1003 1004 1005
	ret = drm_crtc_init_with_planes(dev, crtc,
					&tilcdc_crtc->primary,
					NULL,
					&tilcdc_crtc_funcs,
					"tilcdc crtc");
1006 1007 1008 1009 1010
	if (ret < 0)
		goto fail;

	drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);

J
Jyri Sarha 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	if (priv->is_componentized) {
		struct device_node *ports =
			of_get_child_by_name(dev->dev->of_node, "ports");

		if (ports) {
			crtc->port = of_get_child_by_name(ports, "port");
			of_node_put(ports);
		} else {
			crtc->port =
				of_get_child_by_name(dev->dev->of_node, "port");
		}
		if (!crtc->port) { /* This should never happen */
			dev_err(dev->dev, "Port node not found in %s\n",
				dev->dev->of_node->full_name);
1025
			ret = -EINVAL;
J
Jyri Sarha 已提交
1026 1027 1028 1029
			goto fail;
		}
	}

1030 1031
	priv->crtc = crtc;
	return 0;
1032 1033 1034

fail:
	tilcdc_crtc_destroy(crtc);
1035
	return -ENOMEM;
1036
}