apply.c 30.2 KB
Newer Older
T
Tomi Valkeinen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Copyright (C) 2011 Texas Instruments
 * Author: Tomi Valkeinen <tomi.valkeinen@ti.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/>.
 */

#define DSS_SUBSYS_NAME "APPLY"

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/jiffies.h>

#include <video/omapdss.h>

#include "dss.h"
#include "dss_features.h"

/*
 * We have 4 levels of cache for the dispc settings. First two are in SW and
 * the latter two in HW.
 *
34 35
 *       set_info()
 *          v
T
Tomi Valkeinen 已提交
36
 * +--------------------+
37
 * |     user_info      |
T
Tomi Valkeinen 已提交
38 39 40 41 42
 * +--------------------+
 *          v
 *        apply()
 *          v
 * +--------------------+
43
 * |       info         |
T
Tomi Valkeinen 已提交
44 45
 * +--------------------+
 *          v
46
 *      write_regs()
T
Tomi Valkeinen 已提交
47 48 49 50 51 52 53 54 55 56 57 58
 *          v
 * +--------------------+
 * |  shadow registers  |
 * +--------------------+
 *          v
 * VFP or lcd/digit_enable
 *          v
 * +--------------------+
 * |      registers     |
 * +--------------------+
 */

59
struct ovl_priv_data {
60 61 62 63

	bool user_info_dirty;
	struct omap_overlay_info user_info;

64
	bool info_dirty;
T
Tomi Valkeinen 已提交
65 66
	struct omap_overlay_info info;

67 68
	bool shadow_info_dirty;

69 70 71 72
	bool extra_info_dirty;
	bool shadow_extra_info_dirty;

	bool enabled;
73
	enum omap_channel channel;
74
	u32 fifo_low, fifo_high;
T
Tomi Valkeinen 已提交
75 76 77 78 79 80

	/*
	 * True if overlay is to be enabled. Used to check and calculate configs
	 * for the overlay before it is enabled in the HW.
	 */
	bool enabling;
T
Tomi Valkeinen 已提交
81 82
};

83
struct mgr_priv_data {
84 85 86 87

	bool user_info_dirty;
	struct omap_overlay_manager_info user_info;

88
	bool info_dirty;
T
Tomi Valkeinen 已提交
89 90
	struct omap_overlay_manager_info info;

91 92
	bool shadow_info_dirty;

93 94 95 96
	/* If true, GO bit is up and shadow registers cannot be written.
	 * Never true for manual update displays */
	bool busy;

T
Tomi Valkeinen 已提交
97 98 99
	/* If true, dispc output is enabled */
	bool updating;

100 101
	/* If true, a display is enabled using this manager */
	bool enabled;
T
Tomi Valkeinen 已提交
102 103 104
};

static struct {
105
	struct ovl_priv_data ovl_priv_data_array[MAX_DSS_OVERLAYS];
106
	struct mgr_priv_data mgr_priv_data_array[MAX_DSS_MANAGERS];
T
Tomi Valkeinen 已提交
107

108 109 110
	bool fifo_merge_dirty;
	bool fifo_merge;

T
Tomi Valkeinen 已提交
111
	bool irq_enabled;
112
} dss_data;
T
Tomi Valkeinen 已提交
113

114
/* protects dss_data */
115
static spinlock_t data_lock;
T
Tomi Valkeinen 已提交
116 117
/* lock for blocking functions */
static DEFINE_MUTEX(apply_lock);
118
static DECLARE_COMPLETION(extra_updated_completion);
119

120 121
static void dss_register_vsync_isr(void);

122 123
static struct ovl_priv_data *get_ovl_priv(struct omap_overlay *ovl)
{
124
	return &dss_data.ovl_priv_data_array[ovl->id];
125 126
}

127 128
static struct mgr_priv_data *get_mgr_priv(struct omap_overlay_manager *mgr)
{
129
	return &dss_data.mgr_priv_data_array[mgr->id];
130 131
}

T
Tomi Valkeinen 已提交
132 133
void dss_apply_init(void)
{
134 135 136
	const int num_ovls = dss_feat_get_num_ovls();
	int i;

137
	spin_lock_init(&data_lock);
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 165

	for (i = 0; i < num_ovls; ++i) {
		struct ovl_priv_data *op;

		op = &dss_data.ovl_priv_data_array[i];

		op->info.global_alpha = 255;

		switch (i) {
		case 0:
			op->info.zorder = 0;
			break;
		case 1:
			op->info.zorder =
				dss_has_feature(FEAT_ALPHA_FREE_ZORDER) ? 3 : 0;
			break;
		case 2:
			op->info.zorder =
				dss_has_feature(FEAT_ALPHA_FREE_ZORDER) ? 2 : 0;
			break;
		case 3:
			op->info.zorder =
				dss_has_feature(FEAT_ALPHA_FREE_ZORDER) ? 1 : 0;
			break;
		}

		op->user_info = op->info;
	}
T
Tomi Valkeinen 已提交
166 167 168 169 170 171 172 173 174 175 176 177
}

static bool ovl_manual_update(struct omap_overlay *ovl)
{
	return ovl->manager->device->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE;
}

static bool mgr_manual_update(struct omap_overlay_manager *mgr)
{
	return mgr->device->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE;
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
static int dss_check_settings_low(struct omap_overlay_manager *mgr,
		struct omap_dss_device *dssdev, bool applying)
{
	struct omap_overlay_info *oi;
	struct omap_overlay_manager_info *mi;
	struct omap_overlay *ovl;
	struct omap_overlay_info *ois[MAX_DSS_OVERLAYS];
	struct ovl_priv_data *op;
	struct mgr_priv_data *mp;

	mp = get_mgr_priv(mgr);

	if (applying && mp->user_info_dirty)
		mi = &mp->user_info;
	else
		mi = &mp->info;

	/* collect the infos to be tested into the array */
	list_for_each_entry(ovl, &mgr->overlays, list) {
		op = get_ovl_priv(ovl);

T
Tomi Valkeinen 已提交
199
		if (!op->enabled && !op->enabling)
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
			oi = NULL;
		else if (applying && op->user_info_dirty)
			oi = &op->user_info;
		else
			oi = &op->info;

		ois[ovl->id] = oi;
	}

	return dss_mgr_check(mgr, dssdev, mi, ois);
}

/*
 * check manager and overlay settings using overlay_info from data->info
 */
static int dss_check_settings(struct omap_overlay_manager *mgr,
		struct omap_dss_device *dssdev)
{
	return dss_check_settings_low(mgr, dssdev, false);
}

/*
 * check manager and overlay settings using overlay_info from ovl->info if
 * dirty and from data->info otherwise
 */
static int dss_check_settings_apply(struct omap_overlay_manager *mgr,
		struct omap_dss_device *dssdev)
{
	return dss_check_settings_low(mgr, dssdev, true);
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
static bool need_isr(void)
{
	const int num_mgrs = dss_feat_get_num_mgrs();
	int i;

	for (i = 0; i < num_mgrs; ++i) {
		struct omap_overlay_manager *mgr;
		struct mgr_priv_data *mp;
		struct omap_overlay *ovl;

		mgr = omap_dss_get_overlay_manager(i);
		mp = get_mgr_priv(mgr);

		if (!mp->enabled)
			continue;

T
Tomi Valkeinen 已提交
247 248 249 250 251 252 253 254
		if (mgr_manual_update(mgr)) {
			/* to catch FRAMEDONE */
			if (mp->updating)
				return true;
		} else {
			/* to catch GO bit going down */
			if (mp->busy)
				return true;
255

T
Tomi Valkeinen 已提交
256
			/* to write new values to registers */
257
			if (mp->info_dirty)
T
Tomi Valkeinen 已提交
258
				return true;
259

T
Tomi Valkeinen 已提交
260 261 262 263
			/* to set GO bit */
			if (mp->shadow_info_dirty)
				return true;

T
Tomi Valkeinen 已提交
264 265
			list_for_each_entry(ovl, &mgr->overlays, list) {
				struct ovl_priv_data *op;
266

T
Tomi Valkeinen 已提交
267
				op = get_ovl_priv(ovl);
268

T
Tomi Valkeinen 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282
				/*
				 * NOTE: we check extra_info flags even for
				 * disabled overlays, as extra_infos need to be
				 * always written.
				 */

				/* to write new values to registers */
				if (op->extra_info_dirty)
					return true;

				/* to set GO bit */
				if (op->shadow_extra_info_dirty)
					return true;

T
Tomi Valkeinen 已提交
283 284
				if (!op->enabled)
					continue;
285

T
Tomi Valkeinen 已提交
286
				/* to write new values to registers */
T
Tomi Valkeinen 已提交
287 288 289 290 291
				if (op->info_dirty)
					return true;

				/* to set GO bit */
				if (op->shadow_info_dirty)
T
Tomi Valkeinen 已提交
292 293
					return true;
			}
294 295 296 297 298 299 300 301 302 303 304 305 306 307
		}
	}

	return false;
}

static bool need_go(struct omap_overlay_manager *mgr)
{
	struct omap_overlay *ovl;
	struct mgr_priv_data *mp;
	struct ovl_priv_data *op;

	mp = get_mgr_priv(mgr);

308
	if (mp->shadow_info_dirty)
309 310 311 312
		return true;

	list_for_each_entry(ovl, &mgr->overlays, list) {
		op = get_ovl_priv(ovl);
313
		if (op->shadow_info_dirty || op->shadow_extra_info_dirty)
314 315 316 317 318 319
			return true;
	}

	return false;
}

320 321 322 323 324 325 326 327 328 329 330 331 332
/* returns true if an extra_info field is currently being updated */
static bool extra_info_update_ongoing(void)
{
	const int num_ovls = omap_dss_get_num_overlays();
	struct ovl_priv_data *op;
	struct omap_overlay *ovl;
	struct mgr_priv_data *mp;
	int i;

	for (i = 0; i < num_ovls; ++i) {
		ovl = omap_dss_get_overlay(i);
		op = get_ovl_priv(ovl);

333 334 335
		if (!ovl->manager)
			continue;

336 337 338 339 340
		mp = get_mgr_priv(ovl->manager);

		if (!mp->enabled)
			continue;

341
		if (!mp->updating)
342 343
			continue;

344 345
		if (op->extra_info_dirty || op->shadow_extra_info_dirty)
			return true;
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	}

	return false;
}

/* wait until no extra_info updates are pending */
static void wait_pending_extra_info_updates(void)
{
	bool updating;
	unsigned long flags;
	unsigned long t;

	spin_lock_irqsave(&data_lock, flags);

	updating = extra_info_update_ongoing();

	if (!updating) {
		spin_unlock_irqrestore(&data_lock, flags);
		return;
	}

	init_completion(&extra_updated_completion);

	spin_unlock_irqrestore(&data_lock, flags);

	t = msecs_to_jiffies(500);
	wait_for_completion_timeout(&extra_updated_completion, t);

	updating = extra_info_update_ongoing();

	WARN_ON(updating);
}

T
Tomi Valkeinen 已提交
379 380 381
int dss_mgr_wait_for_go(struct omap_overlay_manager *mgr)
{
	unsigned long timeout = msecs_to_jiffies(500);
382
	struct mgr_priv_data *mp;
T
Tomi Valkeinen 已提交
383 384 385 386 387 388 389 390 391 392 393
	u32 irq;
	int r;
	int i;
	struct omap_dss_device *dssdev = mgr->device;

	if (!dssdev || dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
		return 0;

	if (mgr_manual_update(mgr))
		return 0;

394
	irq = dispc_mgr_get_vsync_irq(mgr->id);
T
Tomi Valkeinen 已提交
395

396
	mp = get_mgr_priv(mgr);
T
Tomi Valkeinen 已提交
397 398 399 400 401
	i = 0;
	while (1) {
		unsigned long flags;
		bool shadow_dirty, dirty;

402
		spin_lock_irqsave(&data_lock, flags);
403 404
		dirty = mp->info_dirty;
		shadow_dirty = mp->shadow_info_dirty;
405
		spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

		if (!dirty && !shadow_dirty) {
			r = 0;
			break;
		}

		/* 4 iterations is the worst case:
		 * 1 - initial iteration, dirty = true (between VFP and VSYNC)
		 * 2 - first VSYNC, dirty = true
		 * 3 - dirty = false, shadow_dirty = true
		 * 4 - shadow_dirty = false */
		if (i++ == 3) {
			DSSERR("mgr(%d)->wait_for_go() not finishing\n",
					mgr->id);
			r = 0;
			break;
		}

		r = omap_dispc_wait_for_irq_interruptible_timeout(irq, timeout);
		if (r == -ERESTARTSYS)
			break;

		if (r) {
			DSSERR("mgr(%d)->wait_for_go() timeout\n", mgr->id);
			break;
		}
	}

	return r;
}

int dss_mgr_wait_for_go_ovl(struct omap_overlay *ovl)
{
	unsigned long timeout = msecs_to_jiffies(500);
440
	struct ovl_priv_data *op;
T
Tomi Valkeinen 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
	struct omap_dss_device *dssdev;
	u32 irq;
	int r;
	int i;

	if (!ovl->manager)
		return 0;

	dssdev = ovl->manager->device;

	if (!dssdev || dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
		return 0;

	if (ovl_manual_update(ovl))
		return 0;

457
	irq = dispc_mgr_get_vsync_irq(ovl->manager->id);
T
Tomi Valkeinen 已提交
458

459
	op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
460 461 462 463 464
	i = 0;
	while (1) {
		unsigned long flags;
		bool shadow_dirty, dirty;

465
		spin_lock_irqsave(&data_lock, flags);
466 467
		dirty = op->info_dirty;
		shadow_dirty = op->shadow_info_dirty;
468
		spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
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

		if (!dirty && !shadow_dirty) {
			r = 0;
			break;
		}

		/* 4 iterations is the worst case:
		 * 1 - initial iteration, dirty = true (between VFP and VSYNC)
		 * 2 - first VSYNC, dirty = true
		 * 3 - dirty = false, shadow_dirty = true
		 * 4 - shadow_dirty = false */
		if (i++ == 3) {
			DSSERR("ovl(%d)->wait_for_go() not finishing\n",
					ovl->id);
			r = 0;
			break;
		}

		r = omap_dispc_wait_for_irq_interruptible_timeout(irq, timeout);
		if (r == -ERESTARTSYS)
			break;

		if (r) {
			DSSERR("ovl(%d)->wait_for_go() timeout\n", ovl->id);
			break;
		}
	}

	return r;
}

500
static void dss_ovl_write_regs(struct omap_overlay *ovl)
T
Tomi Valkeinen 已提交
501
{
502
	struct ovl_priv_data *op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
503 504
	struct omap_overlay_info *oi;
	bool ilace, replication;
T
Tomi Valkeinen 已提交
505
	struct mgr_priv_data *mp;
T
Tomi Valkeinen 已提交
506 507
	int r;

508
	DSSDBGF("%d", ovl->id);
T
Tomi Valkeinen 已提交
509

510
	if (!op->enabled || !op->info_dirty)
511
		return;
T
Tomi Valkeinen 已提交
512

513
	oi = &op->info;
T
Tomi Valkeinen 已提交
514 515 516 517 518

	replication = dss_use_replication(ovl->manager->device, oi->color_mode);

	ilace = ovl->manager->device->type == OMAP_DISPLAY_TYPE_VENC;

519
	r = dispc_ovl_setup(ovl->id, oi, ilace, replication);
T
Tomi Valkeinen 已提交
520
	if (r) {
521 522 523 524
		/*
		 * We can't do much here, as this function can be called from
		 * vsync interrupt.
		 */
525
		DSSERR("dispc_ovl_setup failed for ovl %d\n", ovl->id);
526 527 528 529 530

		/* This will leave fifo configurations in a nonoptimal state */
		op->enabled = false;
		dispc_ovl_enable(ovl->id, false);
		return;
T
Tomi Valkeinen 已提交
531 532
	}

T
Tomi Valkeinen 已提交
533 534
	mp = get_mgr_priv(ovl->manager);

535
	op->info_dirty = false;
T
Tomi Valkeinen 已提交
536
	if (mp->updating)
537
		op->shadow_info_dirty = true;
T
Tomi Valkeinen 已提交
538 539
}

540 541 542
static void dss_ovl_write_regs_extra(struct omap_overlay *ovl)
{
	struct ovl_priv_data *op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
543
	struct mgr_priv_data *mp;
544 545 546

	DSSDBGF("%d", ovl->id);

547 548 549
	if (!op->extra_info_dirty)
		return;

550 551 552 553
	/* note: write also when op->enabled == false, so that the ovl gets
	 * disabled */

	dispc_ovl_enable(ovl->id, op->enabled);
554
	dispc_ovl_set_channel_out(ovl->id, op->channel);
555
	dispc_ovl_set_fifo_threshold(ovl->id, op->fifo_low, op->fifo_high);
556

T
Tomi Valkeinen 已提交
557 558
	mp = get_mgr_priv(ovl->manager);

559
	op->extra_info_dirty = false;
T
Tomi Valkeinen 已提交
560 561
	if (mp->updating)
		op->shadow_extra_info_dirty = true;
562 563
}

564
static void dss_mgr_write_regs(struct omap_overlay_manager *mgr)
T
Tomi Valkeinen 已提交
565
{
566 567
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
	struct omap_overlay *ovl;
T
Tomi Valkeinen 已提交
568

569
	DSSDBGF("%d", mgr->id);
T
Tomi Valkeinen 已提交
570

571 572
	if (!mp->enabled)
		return;
T
Tomi Valkeinen 已提交
573

574
	WARN_ON(mp->busy);
T
Tomi Valkeinen 已提交
575 576

	/* Commit overlay settings */
577 578
	list_for_each_entry(ovl, &mgr->overlays, list) {
		dss_ovl_write_regs(ovl);
579 580 581
		dss_ovl_write_regs_extra(ovl);
	}

582
	if (mp->info_dirty) {
583
		dispc_mgr_setup(mgr->id, &mp->info);
T
Tomi Valkeinen 已提交
584

585
		mp->info_dirty = false;
T
Tomi Valkeinen 已提交
586
		if (mp->updating)
587
			mp->shadow_info_dirty = true;
T
Tomi Valkeinen 已提交
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
static void dss_write_regs_common(void)
{
	const int num_mgrs = omap_dss_get_num_overlay_managers();
	int i;

	if (!dss_data.fifo_merge_dirty)
		return;

	for (i = 0; i < num_mgrs; ++i) {
		struct omap_overlay_manager *mgr;
		struct mgr_priv_data *mp;

		mgr = omap_dss_get_overlay_manager(i);
		mp = get_mgr_priv(mgr);

		if (mp->enabled) {
			if (dss_data.fifo_merge_dirty) {
				dispc_enable_fifomerge(dss_data.fifo_merge);
				dss_data.fifo_merge_dirty = false;
			}

			if (mp->updating)
				mp->shadow_info_dirty = true;
		}
	}
}

618 619 620 621
static void dss_write_regs(void)
{
	const int num_mgrs = omap_dss_get_num_overlay_managers();
	int i;
T
Tomi Valkeinen 已提交
622

623 624
	dss_write_regs_common();

T
Tomi Valkeinen 已提交
625
	for (i = 0; i < num_mgrs; ++i) {
626 627
		struct omap_overlay_manager *mgr;
		struct mgr_priv_data *mp;
628
		int r;
629

630 631
		mgr = omap_dss_get_overlay_manager(i);
		mp = get_mgr_priv(mgr);
T
Tomi Valkeinen 已提交
632

633
		if (!mp->enabled || mgr_manual_update(mgr) || mp->busy)
T
Tomi Valkeinen 已提交
634 635
			continue;

636 637 638 639 640 641 642
		r = dss_check_settings(mgr, mgr->device);
		if (r) {
			DSSERR("cannot write registers for manager %s: "
					"illegal configuration\n", mgr->name);
			continue;
		}

643
		dss_mgr_write_regs(mgr);
644 645
	}
}
646

647 648 649 650
static void dss_set_go_bits(void)
{
	const int num_mgrs = omap_dss_get_num_overlay_managers();
	int i;
T
Tomi Valkeinen 已提交
651

652 653 654
	for (i = 0; i < num_mgrs; ++i) {
		struct omap_overlay_manager *mgr;
		struct mgr_priv_data *mp;
T
Tomi Valkeinen 已提交
655

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
		mgr = omap_dss_get_overlay_manager(i);
		mp = get_mgr_priv(mgr);

		if (!mp->enabled || mgr_manual_update(mgr) || mp->busy)
			continue;

		if (!need_go(mgr))
			continue;

		mp->busy = true;

		if (!dss_data.irq_enabled && need_isr())
			dss_register_vsync_isr();

		dispc_mgr_go(mgr->id);
671
	}
672

T
Tomi Valkeinen 已提交
673 674 675 676
}

void dss_mgr_start_update(struct omap_overlay_manager *mgr)
{
677
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
678
	unsigned long flags;
679
	int r;
680 681

	spin_lock_irqsave(&data_lock, flags);
T
Tomi Valkeinen 已提交
682

T
Tomi Valkeinen 已提交
683 684
	WARN_ON(mp->updating);

685 686 687 688 689 690 691
	r = dss_check_settings(mgr, mgr->device);
	if (r) {
		DSSERR("cannot start manual update: illegal configuration\n");
		spin_unlock_irqrestore(&data_lock, flags);
		return;
	}

692
	dss_mgr_write_regs(mgr);
T
Tomi Valkeinen 已提交
693

694 695
	dss_write_regs_common();

T
Tomi Valkeinen 已提交
696
	mp->updating = true;
T
Tomi Valkeinen 已提交
697

T
Tomi Valkeinen 已提交
698 699
	if (!dss_data.irq_enabled && need_isr())
		dss_register_vsync_isr();
T
Tomi Valkeinen 已提交
700

701
	dispc_mgr_enable(mgr->id, true);
702 703

	spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
704 705
}

706 707 708 709
static void dss_apply_irq_handler(void *data, u32 mask);

static void dss_register_vsync_isr(void)
{
710
	const int num_mgrs = dss_feat_get_num_mgrs();
711
	u32 mask;
712
	int r, i;
713

714 715 716
	mask = 0;
	for (i = 0; i < num_mgrs; ++i)
		mask |= dispc_mgr_get_vsync_irq(i);
717

T
Tomi Valkeinen 已提交
718 719 720
	for (i = 0; i < num_mgrs; ++i)
		mask |= dispc_mgr_get_framedone_irq(i);

721 722 723
	r = omap_dispc_register_isr(dss_apply_irq_handler, NULL, mask);
	WARN_ON(r);

724
	dss_data.irq_enabled = true;
725 726 727 728
}

static void dss_unregister_vsync_isr(void)
{
729
	const int num_mgrs = dss_feat_get_num_mgrs();
730
	u32 mask;
731
	int r, i;
732

733 734 735
	mask = 0;
	for (i = 0; i < num_mgrs; ++i)
		mask |= dispc_mgr_get_vsync_irq(i);
736

T
Tomi Valkeinen 已提交
737 738 739
	for (i = 0; i < num_mgrs; ++i)
		mask |= dispc_mgr_get_framedone_irq(i);

740 741 742
	r = omap_dispc_unregister_isr(dss_apply_irq_handler, NULL, mask);
	WARN_ON(r);

743
	dss_data.irq_enabled = false;
744 745
}

746
static void mgr_clear_shadow_dirty(struct omap_overlay_manager *mgr)
T
Tomi Valkeinen 已提交
747
{
748
	struct omap_overlay *ovl;
749
	struct mgr_priv_data *mp;
750
	struct ovl_priv_data *op;
751 752

	mp = get_mgr_priv(mgr);
753
	mp->shadow_info_dirty = false;
754 755 756

	list_for_each_entry(ovl, &mgr->overlays, list) {
		op = get_ovl_priv(ovl);
757
		op->shadow_info_dirty = false;
758 759 760 761 762 763
		op->shadow_extra_info_dirty = false;
	}
}

static void dss_apply_irq_handler(void *data, u32 mask)
{
T
Tomi Valkeinen 已提交
764
	const int num_mgrs = dss_feat_get_num_mgrs();
765
	int i;
766
	bool extra_updating;
T
Tomi Valkeinen 已提交
767

768
	spin_lock(&data_lock);
T
Tomi Valkeinen 已提交
769

770
	/* clear busy, updating flags, shadow_dirty flags */
771
	for (i = 0; i < num_mgrs; i++) {
772 773
		struct omap_overlay_manager *mgr;
		struct mgr_priv_data *mp;
774
		bool was_updating;
775

776 777 778
		mgr = omap_dss_get_overlay_manager(i);
		mp = get_mgr_priv(mgr);

779
		if (!mp->enabled)
780 781
			continue;

782
		was_updating = mp->updating;
783
		mp->updating = dispc_mgr_is_enabled(i);
T
Tomi Valkeinen 已提交
784

785
		if (!mgr_manual_update(mgr)) {
786
			bool was_busy = mp->busy;
787
			mp->busy = dispc_mgr_go_busy(i);
788

789
			if (was_busy && !mp->busy)
790 791
				mgr_clear_shadow_dirty(mgr);
		} else {
792
			if (was_updating && !mp->updating)
793 794
				mgr_clear_shadow_dirty(mgr);
		}
T
Tomi Valkeinen 已提交
795 796
	}

797
	dss_write_regs();
798
	dss_set_go_bits();
T
Tomi Valkeinen 已提交
799

800 801 802 803
	extra_updating = extra_info_update_ongoing();
	if (!extra_updating)
		complete_all(&extra_updated_completion);

804 805
	if (!need_isr())
		dss_unregister_vsync_isr();
T
Tomi Valkeinen 已提交
806

807
	spin_unlock(&data_lock);
T
Tomi Valkeinen 已提交
808 809
}

810
static void omap_dss_mgr_apply_ovl(struct omap_overlay *ovl)
T
Tomi Valkeinen 已提交
811
{
812
	struct ovl_priv_data *op;
T
Tomi Valkeinen 已提交
813

814
	op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
815

816
	if (!op->user_info_dirty)
817
		return;
T
Tomi Valkeinen 已提交
818

819
	op->user_info_dirty = false;
820
	op->info_dirty = true;
821
	op->info = op->user_info;
T
Tomi Valkeinen 已提交
822 823 824 825
}

static void omap_dss_mgr_apply_mgr(struct omap_overlay_manager *mgr)
{
826
	struct mgr_priv_data *mp;
T
Tomi Valkeinen 已提交
827

828
	mp = get_mgr_priv(mgr);
T
Tomi Valkeinen 已提交
829

830
	if (!mp->user_info_dirty)
T
Tomi Valkeinen 已提交
831 832
		return;

833
	mp->user_info_dirty = false;
834
	mp->info_dirty = true;
835
	mp->info = mp->user_info;
T
Tomi Valkeinen 已提交
836 837
}

838
int omap_dss_mgr_apply(struct omap_overlay_manager *mgr)
T
Tomi Valkeinen 已提交
839
{
840 841
	unsigned long flags;
	struct omap_overlay *ovl;
842
	int r;
843 844 845 846 847

	DSSDBG("omap_dss_mgr_apply(%s)\n", mgr->name);

	spin_lock_irqsave(&data_lock, flags);

848 849 850 851 852 853 854
	r = dss_check_settings_apply(mgr, mgr->device);
	if (r) {
		spin_unlock_irqrestore(&data_lock, flags);
		DSSERR("failed to apply settings: illegal configuration.\n");
		return r;
	}

855 856 857 858 859 860 861 862
	/* Configure overlays */
	list_for_each_entry(ovl, &mgr->overlays, list)
		omap_dss_mgr_apply_ovl(ovl);

	/* Configure manager */
	omap_dss_mgr_apply_mgr(mgr);

	dss_write_regs();
863
	dss_set_go_bits();
864 865 866

	spin_unlock_irqrestore(&data_lock, flags);

867
	return 0;
868 869
}

870 871 872 873 874 875 876 877 878 879 880 881 882
static void dss_apply_ovl_enable(struct omap_overlay *ovl, bool enable)
{
	struct ovl_priv_data *op;

	op = get_ovl_priv(ovl);

	if (op->enabled == enable)
		return;

	op->enabled = enable;
	op->extra_info_dirty = true;
}

883 884 885 886 887 888 889 890 891 892 893 894 895
static void dss_apply_ovl_fifo_thresholds(struct omap_overlay *ovl,
		u32 fifo_low, u32 fifo_high)
{
	struct ovl_priv_data *op = get_ovl_priv(ovl);

	if (op->fifo_low == fifo_low && op->fifo_high == fifo_high)
		return;

	op->fifo_low = fifo_low;
	op->fifo_high = fifo_high;
	op->extra_info_dirty = true;
}

896 897 898 899 900 901 902 903 904
static void dss_apply_fifo_merge(bool use_fifo_merge)
{
	if (dss_data.fifo_merge == use_fifo_merge)
		return;

	dss_data.fifo_merge = use_fifo_merge;
	dss_data.fifo_merge_dirty = true;
}

905 906
static void dss_ovl_setup_fifo(struct omap_overlay *ovl,
		bool use_fifo_merge)
907 908
{
	struct ovl_priv_data *op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
909 910
	struct omap_dss_device *dssdev;
	u32 size, burst_size;
911
	u32 fifo_low, fifo_high;
T
Tomi Valkeinen 已提交
912

913 914 915
	if (!op->enabled && !op->enabling)
		return;

T
Tomi Valkeinen 已提交
916 917
	dssdev = ovl->manager->device;

918 919 920 921 922 923 924 925 926 927
	if (use_fifo_merge) {
		int i;

		size = 0;

		for (i = 0; i < omap_dss_get_num_overlays(); ++i)
			size += dispc_ovl_get_fifo_size(i);
	} else {
		size = dispc_ovl_get_fifo_size(ovl->id);
	}
T
Tomi Valkeinen 已提交
928 929 930 931 932 933 934 935 936 937

	burst_size = dispc_ovl_get_burst_size(ovl->id);

	switch (dssdev->type) {
	case OMAP_DISPLAY_TYPE_DPI:
	case OMAP_DISPLAY_TYPE_DBI:
	case OMAP_DISPLAY_TYPE_SDI:
	case OMAP_DISPLAY_TYPE_VENC:
	case OMAP_DISPLAY_TYPE_HDMI:
		default_get_overlay_fifo_thresholds(ovl->id, size,
938
				burst_size, &fifo_low, &fifo_high);
T
Tomi Valkeinen 已提交
939 940 941 942
		break;
#ifdef CONFIG_OMAP2_DSS_DSI
	case OMAP_DISPLAY_TYPE_DSI:
		dsi_get_overlay_fifo_thresholds(ovl->id, size,
943
				burst_size, &fifo_low, &fifo_high);
T
Tomi Valkeinen 已提交
944 945 946 947 948
		break;
#endif
	default:
		BUG();
	}
949

950
	dss_apply_ovl_fifo_thresholds(ovl, fifo_low, fifo_high);
T
Tomi Valkeinen 已提交
951 952
}

953 954
static void dss_mgr_setup_fifos(struct omap_overlay_manager *mgr,
		bool use_fifo_merge)
T
Tomi Valkeinen 已提交
955
{
956
	struct omap_overlay *ovl;
957
	struct mgr_priv_data *mp;
T
Tomi Valkeinen 已提交
958

959
	mp = get_mgr_priv(mgr);
T
Tomi Valkeinen 已提交
960

961 962
	if (!mp->enabled)
		return;
T
Tomi Valkeinen 已提交
963

964
	list_for_each_entry(ovl, &mgr->overlays, list)
965
		dss_ovl_setup_fifo(ovl, use_fifo_merge);
966
}
T
Tomi Valkeinen 已提交
967

968
static void dss_setup_fifos(bool use_fifo_merge)
969 970 971 972
{
	const int num_mgrs = omap_dss_get_num_overlay_managers();
	struct omap_overlay_manager *mgr;
	int i;
T
Tomi Valkeinen 已提交
973

974 975
	for (i = 0; i < num_mgrs; ++i) {
		mgr = omap_dss_get_overlay_manager(i);
976
		dss_mgr_setup_fifos(mgr, use_fifo_merge);
977
	}
T
Tomi Valkeinen 已提交
978 979
}

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
static int get_num_used_managers(void)
{
	const int num_mgrs = omap_dss_get_num_overlay_managers();
	struct omap_overlay_manager *mgr;
	struct mgr_priv_data *mp;
	int i;
	int enabled_mgrs;

	enabled_mgrs = 0;

	for (i = 0; i < num_mgrs; ++i) {
		mgr = omap_dss_get_overlay_manager(i);
		mp = get_mgr_priv(mgr);

		if (!mp->enabled)
			continue;

		enabled_mgrs++;
	}

	return enabled_mgrs;
}

static int get_num_used_overlays(void)
{
	const int num_ovls = omap_dss_get_num_overlays();
	struct omap_overlay *ovl;
	struct ovl_priv_data *op;
	struct mgr_priv_data *mp;
	int i;
	int enabled_ovls;

	enabled_ovls = 0;

	for (i = 0; i < num_ovls; ++i) {
		ovl = omap_dss_get_overlay(i);
		op = get_ovl_priv(ovl);

		if (!op->enabled && !op->enabling)
			continue;

		mp = get_mgr_priv(ovl->manager);

		if (!mp->enabled)
			continue;

		enabled_ovls++;
	}

	return enabled_ovls;
}

static bool get_use_fifo_merge(void)
{
	int enabled_mgrs = get_num_used_managers();
	int enabled_ovls = get_num_used_overlays();

	if (!dss_has_feature(FEAT_FIFO_MERGE))
		return false;

	/*
	 * In theory the only requirement for fifomerge is enabled_ovls <= 1.
	 * However, if we have two managers enabled and set/unset the fifomerge,
	 * we need to set the GO bits in particular sequence for the managers,
	 * and wait in between.
	 *
	 * This is rather difficult as new apply calls can happen at any time,
	 * so we simplify the problem by requiring also that enabled_mgrs <= 1.
	 * In practice this shouldn't matter, because when only one overlay is
	 * enabled, most likely only one output is enabled.
	 */

	return enabled_mgrs <= 1 && enabled_ovls <= 1;
}

1055
int dss_mgr_enable(struct omap_overlay_manager *mgr)
1056
{
1057 1058
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
	unsigned long flags;
1059
	int r;
1060
	bool fifo_merge;
1061

T
Tomi Valkeinen 已提交
1062 1063
	mutex_lock(&apply_lock);

1064 1065 1066
	if (mp->enabled)
		goto out;

1067 1068
	spin_lock_irqsave(&data_lock, flags);

1069
	mp->enabled = true;
1070

1071 1072 1073 1074
	r = dss_check_settings(mgr, mgr->device);
	if (r) {
		DSSERR("failed to enable manager %d: check_settings failed\n",
				mgr->id);
1075
		goto err;
1076 1077
	}

1078 1079 1080 1081 1082
	/* step 1: setup fifos/fifomerge before enabling the manager */

	fifo_merge = get_use_fifo_merge();
	dss_setup_fifos(fifo_merge);
	dss_apply_fifo_merge(fifo_merge);
1083

1084
	dss_write_regs();
1085
	dss_set_go_bits();
1086

1087 1088 1089 1090 1091 1092 1093 1094
	spin_unlock_irqrestore(&data_lock, flags);

	/* wait until fifo config is in */
	wait_pending_extra_info_updates();

	/* step 2: enable the manager */
	spin_lock_irqsave(&data_lock, flags);

T
Tomi Valkeinen 已提交
1095 1096 1097
	if (!mgr_manual_update(mgr))
		mp->updating = true;

1098
	spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
1099

1100 1101 1102
	if (!mgr_manual_update(mgr))
		dispc_mgr_enable(mgr->id, true);

1103
out:
T
Tomi Valkeinen 已提交
1104
	mutex_unlock(&apply_lock);
1105 1106 1107 1108

	return 0;

err:
1109
	mp->enabled = false;
1110 1111 1112
	spin_unlock_irqrestore(&data_lock, flags);
	mutex_unlock(&apply_lock);
	return r;
1113 1114 1115 1116
}

void dss_mgr_disable(struct omap_overlay_manager *mgr)
{
1117 1118
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
	unsigned long flags;
1119
	bool fifo_merge;
1120

T
Tomi Valkeinen 已提交
1121 1122
	mutex_lock(&apply_lock);

1123 1124 1125
	if (!mp->enabled)
		goto out;

1126 1127
	if (!mgr_manual_update(mgr))
		dispc_mgr_enable(mgr->id, false);
1128 1129 1130

	spin_lock_irqsave(&data_lock, flags);

T
Tomi Valkeinen 已提交
1131
	mp->updating = false;
1132 1133
	mp->enabled = false;

1134 1135 1136 1137 1138 1139 1140
	fifo_merge = get_use_fifo_merge();
	dss_setup_fifos(fifo_merge);
	dss_apply_fifo_merge(fifo_merge);

	dss_write_regs();
	dss_set_go_bits();

1141
	spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
1142

1143
	wait_pending_extra_info_updates();
1144
out:
T
Tomi Valkeinen 已提交
1145
	mutex_unlock(&apply_lock);
1146 1147
}

1148 1149 1150
int dss_mgr_set_info(struct omap_overlay_manager *mgr,
		struct omap_overlay_manager_info *info)
{
1151
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
1152
	unsigned long flags;
1153 1154 1155 1156 1157
	int r;

	r = dss_mgr_simple_check(mgr, info);
	if (r)
		return r;
1158 1159 1160

	spin_lock_irqsave(&data_lock, flags);

1161 1162
	mp->user_info = *info;
	mp->user_info_dirty = true;
1163

1164 1165
	spin_unlock_irqrestore(&data_lock, flags);

1166 1167 1168 1169 1170 1171
	return 0;
}

void dss_mgr_get_info(struct omap_overlay_manager *mgr,
		struct omap_overlay_manager_info *info)
{
1172
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
1173 1174 1175 1176
	unsigned long flags;

	spin_lock_irqsave(&data_lock, flags);

1177
	*info = mp->user_info;
1178 1179

	spin_unlock_irqrestore(&data_lock, flags);
1180 1181 1182 1183 1184 1185 1186
}

int dss_mgr_set_device(struct omap_overlay_manager *mgr,
		struct omap_dss_device *dssdev)
{
	int r;

T
Tomi Valkeinen 已提交
1187 1188
	mutex_lock(&apply_lock);

1189 1190 1191
	if (dssdev->manager) {
		DSSERR("display '%s' already has a manager '%s'\n",
			       dssdev->name, dssdev->manager->name);
T
Tomi Valkeinen 已提交
1192 1193
		r = -EINVAL;
		goto err;
1194 1195 1196 1197 1198
	}

	if ((mgr->supported_displays & dssdev->type) == 0) {
		DSSERR("display '%s' does not support manager '%s'\n",
			       dssdev->name, mgr->name);
T
Tomi Valkeinen 已提交
1199 1200
		r = -EINVAL;
		goto err;
1201 1202 1203 1204 1205
	}

	dssdev->manager = mgr;
	mgr->device = dssdev;

T
Tomi Valkeinen 已提交
1206 1207
	mutex_unlock(&apply_lock);

1208
	return 0;
T
Tomi Valkeinen 已提交
1209 1210 1211
err:
	mutex_unlock(&apply_lock);
	return r;
1212 1213 1214 1215
}

int dss_mgr_unset_device(struct omap_overlay_manager *mgr)
{
T
Tomi Valkeinen 已提交
1216 1217 1218 1219
	int r;

	mutex_lock(&apply_lock);

1220 1221
	if (!mgr->device) {
		DSSERR("failed to unset display, display not set.\n");
T
Tomi Valkeinen 已提交
1222 1223
		r = -EINVAL;
		goto err;
1224 1225 1226 1227 1228 1229
	}

	/*
	 * Don't allow currently enabled displays to have the overlay manager
	 * pulled out from underneath them
	 */
T
Tomi Valkeinen 已提交
1230 1231 1232 1233
	if (mgr->device->state != OMAP_DSS_DISPLAY_DISABLED) {
		r = -EINVAL;
		goto err;
	}
1234 1235 1236 1237

	mgr->device->manager = NULL;
	mgr->device = NULL;

T
Tomi Valkeinen 已提交
1238 1239
	mutex_unlock(&apply_lock);

1240
	return 0;
T
Tomi Valkeinen 已提交
1241 1242 1243
err:
	mutex_unlock(&apply_lock);
	return r;
1244 1245 1246
}


1247 1248 1249
int dss_ovl_set_info(struct omap_overlay *ovl,
		struct omap_overlay_info *info)
{
1250
	struct ovl_priv_data *op = get_ovl_priv(ovl);
1251
	unsigned long flags;
1252 1253 1254 1255 1256
	int r;

	r = dss_ovl_simple_check(ovl, info);
	if (r)
		return r;
1257 1258 1259

	spin_lock_irqsave(&data_lock, flags);

1260 1261
	op->user_info = *info;
	op->user_info_dirty = true;
1262

1263 1264
	spin_unlock_irqrestore(&data_lock, flags);

1265 1266 1267 1268 1269 1270
	return 0;
}

void dss_ovl_get_info(struct omap_overlay *ovl,
		struct omap_overlay_info *info)
{
1271
	struct ovl_priv_data *op = get_ovl_priv(ovl);
1272 1273 1274 1275
	unsigned long flags;

	spin_lock_irqsave(&data_lock, flags);

1276
	*info = op->user_info;
1277 1278

	spin_unlock_irqrestore(&data_lock, flags);
1279 1280 1281 1282 1283
}

int dss_ovl_set_manager(struct omap_overlay *ovl,
		struct omap_overlay_manager *mgr)
{
1284 1285
	struct ovl_priv_data *op = get_ovl_priv(ovl);
	unsigned long flags;
T
Tomi Valkeinen 已提交
1286 1287
	int r;

1288 1289 1290
	if (!mgr)
		return -EINVAL;

T
Tomi Valkeinen 已提交
1291 1292
	mutex_lock(&apply_lock);

1293 1294 1295
	if (ovl->manager) {
		DSSERR("overlay '%s' already has a manager '%s'\n",
				ovl->name, ovl->manager->name);
T
Tomi Valkeinen 已提交
1296 1297
		r = -EINVAL;
		goto err;
1298 1299
	}

1300 1301 1302 1303
	spin_lock_irqsave(&data_lock, flags);

	if (op->enabled) {
		spin_unlock_irqrestore(&data_lock, flags);
1304
		DSSERR("overlay has to be disabled to change the manager\n");
T
Tomi Valkeinen 已提交
1305 1306
		r = -EINVAL;
		goto err;
1307 1308
	}

1309 1310 1311
	op->channel = mgr->id;
	op->extra_info_dirty = true;

1312 1313 1314
	ovl->manager = mgr;
	list_add_tail(&ovl->list, &mgr->overlays);

1315 1316
	spin_unlock_irqrestore(&data_lock, flags);

1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	/* XXX: When there is an overlay on a DSI manual update display, and
	 * the overlay is first disabled, then moved to tv, and enabled, we
	 * seem to get SYNC_LOST_DIGIT error.
	 *
	 * Waiting doesn't seem to help, but updating the manual update display
	 * after disabling the overlay seems to fix this. This hints that the
	 * overlay is perhaps somehow tied to the LCD output until the output
	 * is updated.
	 *
	 * Userspace workaround for this is to update the LCD after disabling
	 * the overlay, but before moving the overlay to TV.
	 */

T
Tomi Valkeinen 已提交
1330 1331
	mutex_unlock(&apply_lock);

1332
	return 0;
T
Tomi Valkeinen 已提交
1333 1334 1335
err:
	mutex_unlock(&apply_lock);
	return r;
1336 1337 1338 1339
}

int dss_ovl_unset_manager(struct omap_overlay *ovl)
{
1340 1341
	struct ovl_priv_data *op = get_ovl_priv(ovl);
	unsigned long flags;
T
Tomi Valkeinen 已提交
1342 1343 1344 1345
	int r;

	mutex_lock(&apply_lock);

1346 1347
	if (!ovl->manager) {
		DSSERR("failed to detach overlay: manager not set\n");
T
Tomi Valkeinen 已提交
1348 1349
		r = -EINVAL;
		goto err;
1350 1351
	}

1352 1353 1354 1355
	spin_lock_irqsave(&data_lock, flags);

	if (op->enabled) {
		spin_unlock_irqrestore(&data_lock, flags);
1356
		DSSERR("overlay has to be disabled to unset the manager\n");
T
Tomi Valkeinen 已提交
1357 1358
		r = -EINVAL;
		goto err;
1359 1360
	}

1361 1362
	op->channel = -1;

1363 1364 1365
	ovl->manager = NULL;
	list_del(&ovl->list);

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
	spin_unlock_irqrestore(&data_lock, flags);

	mutex_unlock(&apply_lock);

	return 0;
err:
	mutex_unlock(&apply_lock);
	return r;
}

bool dss_ovl_is_enabled(struct omap_overlay *ovl)
{
	struct ovl_priv_data *op = get_ovl_priv(ovl);
	unsigned long flags;
	bool e;

	spin_lock_irqsave(&data_lock, flags);

	e = op->enabled;

	spin_unlock_irqrestore(&data_lock, flags);

	return e;
}

int dss_ovl_enable(struct omap_overlay *ovl)
{
	struct ovl_priv_data *op = get_ovl_priv(ovl);
	unsigned long flags;
1395
	bool fifo_merge;
1396 1397 1398 1399
	int r;

	mutex_lock(&apply_lock);

1400 1401
	if (op->enabled) {
		r = 0;
1402
		goto err1;
1403 1404
	}

1405 1406
	if (ovl->manager == NULL || ovl->manager->device == NULL) {
		r = -EINVAL;
1407
		goto err1;
1408 1409 1410 1411
	}

	spin_lock_irqsave(&data_lock, flags);

T
Tomi Valkeinen 已提交
1412 1413
	op->enabling = true;

1414 1415 1416 1417 1418 1419 1420
	r = dss_check_settings(ovl->manager, ovl->manager->device);
	if (r) {
		DSSERR("failed to enable overlay %d: check_settings failed\n",
				ovl->id);
		goto err2;
	}

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
	/* step 1: configure fifos/fifomerge for currently enabled ovls */

	fifo_merge = get_use_fifo_merge();
	dss_setup_fifos(fifo_merge);
	dss_apply_fifo_merge(fifo_merge);

	dss_write_regs();
	dss_set_go_bits();

	spin_unlock_irqrestore(&data_lock, flags);

	/* wait for fifo configs to go in */
	wait_pending_extra_info_updates();

	/* step 2: enable the overlay */
	spin_lock_irqsave(&data_lock, flags);
1437

T
Tomi Valkeinen 已提交
1438 1439 1440
	op->enabling = false;
	dss_apply_ovl_enable(ovl, true);

1441
	dss_write_regs();
1442
	dss_set_go_bits();
1443

1444 1445
	spin_unlock_irqrestore(&data_lock, flags);

1446 1447 1448
	/* wait for overlay to be enabled */
	wait_pending_extra_info_updates();

1449 1450 1451
	mutex_unlock(&apply_lock);

	return 0;
1452
err2:
T
Tomi Valkeinen 已提交
1453
	op->enabling = false;
1454 1455
	spin_unlock_irqrestore(&data_lock, flags);
err1:
1456 1457 1458 1459 1460 1461 1462 1463
	mutex_unlock(&apply_lock);
	return r;
}

int dss_ovl_disable(struct omap_overlay *ovl)
{
	struct ovl_priv_data *op = get_ovl_priv(ovl);
	unsigned long flags;
1464
	bool fifo_merge;
1465 1466 1467 1468
	int r;

	mutex_lock(&apply_lock);

1469 1470 1471 1472 1473
	if (!op->enabled) {
		r = 0;
		goto err;
	}

1474 1475 1476 1477 1478
	if (ovl->manager == NULL || ovl->manager->device == NULL) {
		r = -EINVAL;
		goto err;
	}

1479
	/* step 1: disable the overlay */
1480 1481
	spin_lock_irqsave(&data_lock, flags);

1482
	dss_apply_ovl_enable(ovl, false);
1483

1484
	dss_write_regs();
1485
	dss_set_go_bits();
1486

1487 1488
	spin_unlock_irqrestore(&data_lock, flags);

1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
	/* wait for the overlay to be disabled */
	wait_pending_extra_info_updates();

	/* step 2: configure fifos/fifomerge */
	spin_lock_irqsave(&data_lock, flags);

	fifo_merge = get_use_fifo_merge();
	dss_setup_fifos(fifo_merge);
	dss_apply_fifo_merge(fifo_merge);

	dss_write_regs();
	dss_set_go_bits();

	spin_unlock_irqrestore(&data_lock, flags);

	/* wait for fifo config to go in */
	wait_pending_extra_info_updates();

T
Tomi Valkeinen 已提交
1507 1508
	mutex_unlock(&apply_lock);

1509
	return 0;
1510

T
Tomi Valkeinen 已提交
1511 1512 1513
err:
	mutex_unlock(&apply_lock);
	return r;
1514 1515
}