apply.c 20.4 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
struct mgr_priv_data {
78 79 80 81

	bool user_info_dirty;
	struct omap_overlay_manager_info user_info;

82
	bool info_dirty;
T
Tomi Valkeinen 已提交
83 84
	struct omap_overlay_manager_info info;

85 86
	bool shadow_info_dirty;

87 88 89 90
	/* If true, GO bit is up and shadow registers cannot be written.
	 * Never true for manual update displays */
	bool busy;

T
Tomi Valkeinen 已提交
91 92 93
	/* If true, dispc output is enabled */
	bool updating;

94 95
	/* If true, a display is enabled using this manager */
	bool enabled;
T
Tomi Valkeinen 已提交
96 97 98
};

static struct {
99
	struct ovl_priv_data ovl_priv_data_array[MAX_DSS_OVERLAYS];
100
	struct mgr_priv_data mgr_priv_data_array[MAX_DSS_MANAGERS];
T
Tomi Valkeinen 已提交
101 102

	bool irq_enabled;
103
} dss_data;
T
Tomi Valkeinen 已提交
104

105
/* protects dss_data */
106
static spinlock_t data_lock;
T
Tomi Valkeinen 已提交
107 108
/* lock for blocking functions */
static DEFINE_MUTEX(apply_lock);
109

110 111
static void dss_register_vsync_isr(void);

112 113
static struct ovl_priv_data *get_ovl_priv(struct omap_overlay *ovl)
{
114
	return &dss_data.ovl_priv_data_array[ovl->id];
115 116
}

117 118
static struct mgr_priv_data *get_mgr_priv(struct omap_overlay_manager *mgr)
{
119
	return &dss_data.mgr_priv_data_array[mgr->id];
120 121
}

T
Tomi Valkeinen 已提交
122 123
void dss_apply_init(void)
{
124 125 126
	const int num_ovls = dss_feat_get_num_ovls();
	int i;

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

	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 已提交
156 157 158 159 160 161 162 163 164 165 166 167
}

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

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
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 已提交
184 185 186 187 188 189 190 191
		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;
192

T
Tomi Valkeinen 已提交
193
			/* to write new values to registers */
194
			if (mp->info_dirty)
T
Tomi Valkeinen 已提交
195
				return true;
196

T
Tomi Valkeinen 已提交
197 198
			list_for_each_entry(ovl, &mgr->overlays, list) {
				struct ovl_priv_data *op;
199

T
Tomi Valkeinen 已提交
200
				op = get_ovl_priv(ovl);
201

T
Tomi Valkeinen 已提交
202 203
				if (!op->enabled)
					continue;
204

T
Tomi Valkeinen 已提交
205
				/* to write new values to registers */
206
				if (op->info_dirty || op->extra_info_dirty)
T
Tomi Valkeinen 已提交
207 208
					return true;
			}
209 210 211 212 213 214 215 216 217 218 219 220 221 222
		}
	}

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

223
	if (mp->shadow_info_dirty)
224 225 226 227
		return true;

	list_for_each_entry(ovl, &mgr->overlays, list) {
		op = get_ovl_priv(ovl);
228
		if (op->shadow_info_dirty || op->shadow_extra_info_dirty)
229 230 231 232 233 234
			return true;
	}

	return false;
}

T
Tomi Valkeinen 已提交
235 236 237
int dss_mgr_wait_for_go(struct omap_overlay_manager *mgr)
{
	unsigned long timeout = msecs_to_jiffies(500);
238
	struct mgr_priv_data *mp;
T
Tomi Valkeinen 已提交
239 240 241 242 243 244 245 246 247 248 249
	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;

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

252
	mp = get_mgr_priv(mgr);
T
Tomi Valkeinen 已提交
253 254 255 256 257
	i = 0;
	while (1) {
		unsigned long flags;
		bool shadow_dirty, dirty;

258
		spin_lock_irqsave(&data_lock, flags);
259 260
		dirty = mp->info_dirty;
		shadow_dirty = mp->shadow_info_dirty;
261
		spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

		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);
296
	struct ovl_priv_data *op;
T
Tomi Valkeinen 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	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;

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

315
	op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
316 317 318 319 320
	i = 0;
	while (1) {
		unsigned long flags;
		bool shadow_dirty, dirty;

321
		spin_lock_irqsave(&data_lock, flags);
322 323
		dirty = op->info_dirty;
		shadow_dirty = op->shadow_info_dirty;
324
		spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

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

356
static void dss_ovl_write_regs(struct omap_overlay *ovl)
T
Tomi Valkeinen 已提交
357
{
358
	struct ovl_priv_data *op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
359 360
	struct omap_overlay_info *oi;
	bool ilace, replication;
T
Tomi Valkeinen 已提交
361
	struct mgr_priv_data *mp;
T
Tomi Valkeinen 已提交
362 363
	int r;

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

366
	if (!op->enabled || !op->info_dirty)
367
		return;
T
Tomi Valkeinen 已提交
368

369
	oi = &op->info;
T
Tomi Valkeinen 已提交
370 371 372 373 374

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

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

375
	r = dispc_ovl_setup(ovl->id, oi, ilace, replication);
T
Tomi Valkeinen 已提交
376
	if (r) {
377 378 379 380
		/*
		 * We can't do much here, as this function can be called from
		 * vsync interrupt.
		 */
381
		DSSERR("dispc_ovl_setup failed for ovl %d\n", ovl->id);
382 383 384 385 386

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

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

391
	op->info_dirty = false;
T
Tomi Valkeinen 已提交
392
	if (mp->updating)
393
		op->shadow_info_dirty = true;
T
Tomi Valkeinen 已提交
394 395
}

396 397 398
static void dss_ovl_write_regs_extra(struct omap_overlay *ovl)
{
	struct ovl_priv_data *op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
399
	struct mgr_priv_data *mp;
400 401 402

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

403 404 405
	if (!op->extra_info_dirty)
		return;

406 407 408 409
	/* note: write also when op->enabled == false, so that the ovl gets
	 * disabled */

	dispc_ovl_enable(ovl->id, op->enabled);
410
	dispc_ovl_set_channel_out(ovl->id, op->channel);
411
	dispc_ovl_set_fifo_threshold(ovl->id, op->fifo_low, op->fifo_high);
412

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

415
	op->extra_info_dirty = false;
T
Tomi Valkeinen 已提交
416 417
	if (mp->updating)
		op->shadow_extra_info_dirty = true;
418 419
}

420
static void dss_mgr_write_regs(struct omap_overlay_manager *mgr)
T
Tomi Valkeinen 已提交
421
{
422 423
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
	struct omap_overlay *ovl;
T
Tomi Valkeinen 已提交
424

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

427 428
	if (!mp->enabled)
		return;
T
Tomi Valkeinen 已提交
429

430
	WARN_ON(mp->busy);
T
Tomi Valkeinen 已提交
431 432

	/* Commit overlay settings */
433 434
	list_for_each_entry(ovl, &mgr->overlays, list) {
		dss_ovl_write_regs(ovl);
435 436 437
		dss_ovl_write_regs_extra(ovl);
	}

438
	if (mp->info_dirty) {
439
		dispc_mgr_setup(mgr->id, &mp->info);
T
Tomi Valkeinen 已提交
440

441
		mp->info_dirty = false;
T
Tomi Valkeinen 已提交
442
		if (mp->updating)
443
			mp->shadow_info_dirty = true;
T
Tomi Valkeinen 已提交
444
	}
445 446 447 448 449 450
}

static void dss_write_regs(void)
{
	const int num_mgrs = omap_dss_get_num_overlay_managers();
	int i;
T
Tomi Valkeinen 已提交
451 452

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

456 457
		mgr = omap_dss_get_overlay_manager(i);
		mp = get_mgr_priv(mgr);
T
Tomi Valkeinen 已提交
458

459
		if (!mp->enabled || mgr_manual_update(mgr) || mp->busy)
T
Tomi Valkeinen 已提交
460 461
			continue;

462 463 464
		dss_mgr_write_regs(mgr);

		if (need_go(mgr)) {
465
			mp->busy = true;
T
Tomi Valkeinen 已提交
466

467 468
			if (!dss_data.irq_enabled && need_isr())
				dss_register_vsync_isr();
T
Tomi Valkeinen 已提交
469

470 471 472
			dispc_mgr_go(mgr->id);
		}
	}
T
Tomi Valkeinen 已提交
473 474 475 476
}

void dss_mgr_start_update(struct omap_overlay_manager *mgr)
{
477
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
478 479 480
	unsigned long flags;

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

T
Tomi Valkeinen 已提交
482 483
	WARN_ON(mp->updating);

484
	dss_mgr_write_regs(mgr);
T
Tomi Valkeinen 已提交
485

T
Tomi Valkeinen 已提交
486
	mp->updating = true;
T
Tomi Valkeinen 已提交
487

T
Tomi Valkeinen 已提交
488 489
	if (!dss_data.irq_enabled && need_isr())
		dss_register_vsync_isr();
T
Tomi Valkeinen 已提交
490

491
	dispc_mgr_enable(mgr->id, true);
492 493

	spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
494 495
}

496 497 498 499
static void dss_apply_irq_handler(void *data, u32 mask);

static void dss_register_vsync_isr(void)
{
500
	const int num_mgrs = dss_feat_get_num_mgrs();
501
	u32 mask;
502
	int r, i;
503

504 505 506
	mask = 0;
	for (i = 0; i < num_mgrs; ++i)
		mask |= dispc_mgr_get_vsync_irq(i);
507

T
Tomi Valkeinen 已提交
508 509 510
	for (i = 0; i < num_mgrs; ++i)
		mask |= dispc_mgr_get_framedone_irq(i);

511 512 513
	r = omap_dispc_register_isr(dss_apply_irq_handler, NULL, mask);
	WARN_ON(r);

514
	dss_data.irq_enabled = true;
515 516 517 518
}

static void dss_unregister_vsync_isr(void)
{
519
	const int num_mgrs = dss_feat_get_num_mgrs();
520
	u32 mask;
521
	int r, i;
522

523 524 525
	mask = 0;
	for (i = 0; i < num_mgrs; ++i)
		mask |= dispc_mgr_get_vsync_irq(i);
526

T
Tomi Valkeinen 已提交
527 528 529
	for (i = 0; i < num_mgrs; ++i)
		mask |= dispc_mgr_get_framedone_irq(i);

530 531 532
	r = omap_dispc_unregister_isr(dss_apply_irq_handler, NULL, mask);
	WARN_ON(r);

533
	dss_data.irq_enabled = false;
534 535
}

536
static void mgr_clear_shadow_dirty(struct omap_overlay_manager *mgr)
T
Tomi Valkeinen 已提交
537
{
538
	struct omap_overlay *ovl;
539
	struct mgr_priv_data *mp;
540
	struct ovl_priv_data *op;
541 542

	mp = get_mgr_priv(mgr);
543
	mp->shadow_info_dirty = false;
544 545 546

	list_for_each_entry(ovl, &mgr->overlays, list) {
		op = get_ovl_priv(ovl);
547
		op->shadow_info_dirty = false;
548 549 550 551 552 553
		op->shadow_extra_info_dirty = false;
	}
}

static void dss_apply_irq_handler(void *data, u32 mask)
{
T
Tomi Valkeinen 已提交
554
	const int num_mgrs = dss_feat_get_num_mgrs();
555
	int i;
T
Tomi Valkeinen 已提交
556

557
	spin_lock(&data_lock);
T
Tomi Valkeinen 已提交
558

559
	/* clear busy, updating flags, shadow_dirty flags */
560
	for (i = 0; i < num_mgrs; i++) {
561 562 563
		struct omap_overlay_manager *mgr;
		struct mgr_priv_data *mp;

564 565 566
		mgr = omap_dss_get_overlay_manager(i);
		mp = get_mgr_priv(mgr);

567
		if (!mp->enabled)
568 569
			continue;

570
		mp->updating = dispc_mgr_is_enabled(i);
T
Tomi Valkeinen 已提交
571

572 573
		if (!mgr_manual_update(mgr)) {
			mp->busy = dispc_mgr_go_busy(i);
574

575 576 577 578 579 580
			if (!mp->busy)
				mgr_clear_shadow_dirty(mgr);
		} else {
			if (!mp->updating)
				mgr_clear_shadow_dirty(mgr);
		}
T
Tomi Valkeinen 已提交
581 582
	}

583
	dss_write_regs();
T
Tomi Valkeinen 已提交
584

585 586
	if (!need_isr())
		dss_unregister_vsync_isr();
T
Tomi Valkeinen 已提交
587

588
	spin_unlock(&data_lock);
T
Tomi Valkeinen 已提交
589 590
}

591
static void omap_dss_mgr_apply_ovl(struct omap_overlay *ovl)
T
Tomi Valkeinen 已提交
592
{
593
	struct ovl_priv_data *op;
T
Tomi Valkeinen 已提交
594

595
	op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
596

597
	if (!op->user_info_dirty)
598
		return;
T
Tomi Valkeinen 已提交
599

600
	op->user_info_dirty = false;
601
	op->info_dirty = true;
602
	op->info = op->user_info;
T
Tomi Valkeinen 已提交
603 604 605 606
}

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

609
	mp = get_mgr_priv(mgr);
T
Tomi Valkeinen 已提交
610

611
	if (!mp->user_info_dirty)
T
Tomi Valkeinen 已提交
612 613
		return;

614
	mp->user_info_dirty = false;
615
	mp->info_dirty = true;
616
	mp->info = mp->user_info;
T
Tomi Valkeinen 已提交
617 618
}

619
int omap_dss_mgr_apply(struct omap_overlay_manager *mgr)
T
Tomi Valkeinen 已提交
620
{
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
	int r;
	unsigned long flags;
	struct omap_overlay *ovl;

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

	r = dispc_runtime_get();
	if (r)
		return r;

	spin_lock_irqsave(&data_lock, flags);

	/* 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();

	spin_unlock_irqrestore(&data_lock, flags);

	dispc_runtime_put();

	return r;
}

static void dss_ovl_setup_fifo(struct omap_overlay *ovl)
{
	struct ovl_priv_data *op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
652 653
	struct omap_dss_device *dssdev;
	u32 size, burst_size;
654
	u32 fifo_low, fifo_high;
T
Tomi Valkeinen 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668

	dssdev = ovl->manager->device;

	size = dispc_ovl_get_fifo_size(ovl->id);

	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,
669
				burst_size, &fifo_low, &fifo_high);
T
Tomi Valkeinen 已提交
670 671 672 673
		break;
#ifdef CONFIG_OMAP2_DSS_DSI
	case OMAP_DISPLAY_TYPE_DSI:
		dsi_get_overlay_fifo_thresholds(ovl->id, size,
674
				burst_size, &fifo_low, &fifo_high);
T
Tomi Valkeinen 已提交
675 676 677 678 679
		break;
#endif
	default:
		BUG();
	}
680 681 682 683

	op->fifo_low = fifo_low;
	op->fifo_high = fifo_high;
	op->extra_info_dirty = true;
T
Tomi Valkeinen 已提交
684 685
}

686
static void dss_mgr_setup_fifos(struct omap_overlay_manager *mgr)
T
Tomi Valkeinen 已提交
687
{
688
	struct omap_overlay *ovl;
689 690
	struct ovl_priv_data *op;
	struct mgr_priv_data *mp;
T
Tomi Valkeinen 已提交
691

692
	mp = get_mgr_priv(mgr);
T
Tomi Valkeinen 已提交
693

694 695
	if (!mp->enabled)
		return;
T
Tomi Valkeinen 已提交
696

697 698
	list_for_each_entry(ovl, &mgr->overlays, list) {
		op = get_ovl_priv(ovl);
T
Tomi Valkeinen 已提交
699

700 701
		if (!op->enabled)
			continue;
T
Tomi Valkeinen 已提交
702

703 704
		dss_ovl_setup_fifo(ovl);
	}
T
Tomi Valkeinen 已提交
705 706
}

707 708
void dss_mgr_enable(struct omap_overlay_manager *mgr)
{
709 710 711
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
	unsigned long flags;

T
Tomi Valkeinen 已提交
712 713
	mutex_lock(&apply_lock);

714 715 716 717
	spin_lock_irqsave(&data_lock, flags);

	mp->enabled = true;

718 719
	dss_mgr_setup_fifos(mgr);

720 721
	dss_write_regs();

T
Tomi Valkeinen 已提交
722 723 724
	if (!mgr_manual_update(mgr))
		mp->updating = true;

725
	spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
726

727 728 729
	if (!mgr_manual_update(mgr))
		dispc_mgr_enable(mgr->id, true);

T
Tomi Valkeinen 已提交
730
	mutex_unlock(&apply_lock);
731 732 733 734
}

void dss_mgr_disable(struct omap_overlay_manager *mgr)
{
735 736 737
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
	unsigned long flags;

T
Tomi Valkeinen 已提交
738 739
	mutex_lock(&apply_lock);

740 741
	if (!mgr_manual_update(mgr))
		dispc_mgr_enable(mgr->id, false);
742 743 744

	spin_lock_irqsave(&data_lock, flags);

T
Tomi Valkeinen 已提交
745
	mp->updating = false;
746 747 748
	mp->enabled = false;

	spin_unlock_irqrestore(&data_lock, flags);
T
Tomi Valkeinen 已提交
749 750

	mutex_unlock(&apply_lock);
751 752
}

753 754 755
int dss_mgr_set_info(struct omap_overlay_manager *mgr,
		struct omap_overlay_manager_info *info)
{
756
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
757 758 759 760
	unsigned long flags;

	spin_lock_irqsave(&data_lock, flags);

761 762
	mp->user_info = *info;
	mp->user_info_dirty = true;
763

764 765
	spin_unlock_irqrestore(&data_lock, flags);

766 767 768 769 770 771
	return 0;
}

void dss_mgr_get_info(struct omap_overlay_manager *mgr,
		struct omap_overlay_manager_info *info)
{
772
	struct mgr_priv_data *mp = get_mgr_priv(mgr);
773 774 775 776
	unsigned long flags;

	spin_lock_irqsave(&data_lock, flags);

777
	*info = mp->user_info;
778 779

	spin_unlock_irqrestore(&data_lock, flags);
780 781 782 783 784 785 786
}

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

T
Tomi Valkeinen 已提交
787 788
	mutex_lock(&apply_lock);

789 790 791
	if (dssdev->manager) {
		DSSERR("display '%s' already has a manager '%s'\n",
			       dssdev->name, dssdev->manager->name);
T
Tomi Valkeinen 已提交
792 793
		r = -EINVAL;
		goto err;
794 795 796 797 798
	}

	if ((mgr->supported_displays & dssdev->type) == 0) {
		DSSERR("display '%s' does not support manager '%s'\n",
			       dssdev->name, mgr->name);
T
Tomi Valkeinen 已提交
799 800
		r = -EINVAL;
		goto err;
801 802 803 804 805
	}

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

T
Tomi Valkeinen 已提交
806 807
	mutex_unlock(&apply_lock);

808
	return 0;
T
Tomi Valkeinen 已提交
809 810 811
err:
	mutex_unlock(&apply_lock);
	return r;
812 813 814 815
}

int dss_mgr_unset_device(struct omap_overlay_manager *mgr)
{
T
Tomi Valkeinen 已提交
816 817 818 819
	int r;

	mutex_lock(&apply_lock);

820 821
	if (!mgr->device) {
		DSSERR("failed to unset display, display not set.\n");
T
Tomi Valkeinen 已提交
822 823
		r = -EINVAL;
		goto err;
824 825 826 827 828 829
	}

	/*
	 * Don't allow currently enabled displays to have the overlay manager
	 * pulled out from underneath them
	 */
T
Tomi Valkeinen 已提交
830 831 832 833
	if (mgr->device->state != OMAP_DSS_DISPLAY_DISABLED) {
		r = -EINVAL;
		goto err;
	}
834 835 836 837

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

T
Tomi Valkeinen 已提交
838 839
	mutex_unlock(&apply_lock);

840
	return 0;
T
Tomi Valkeinen 已提交
841 842 843
err:
	mutex_unlock(&apply_lock);
	return r;
844 845 846 847
}



848 849 850
int dss_ovl_set_info(struct omap_overlay *ovl,
		struct omap_overlay_info *info)
{
851
	struct ovl_priv_data *op = get_ovl_priv(ovl);
852 853 854 855
	unsigned long flags;

	spin_lock_irqsave(&data_lock, flags);

856 857
	op->user_info = *info;
	op->user_info_dirty = true;
858

859 860
	spin_unlock_irqrestore(&data_lock, flags);

861 862 863 864 865 866
	return 0;
}

void dss_ovl_get_info(struct omap_overlay *ovl,
		struct omap_overlay_info *info)
{
867
	struct ovl_priv_data *op = get_ovl_priv(ovl);
868 869 870 871
	unsigned long flags;

	spin_lock_irqsave(&data_lock, flags);

872
	*info = op->user_info;
873 874

	spin_unlock_irqrestore(&data_lock, flags);
875 876 877 878 879
}

int dss_ovl_set_manager(struct omap_overlay *ovl,
		struct omap_overlay_manager *mgr)
{
880 881
	struct ovl_priv_data *op = get_ovl_priv(ovl);
	unsigned long flags;
T
Tomi Valkeinen 已提交
882 883
	int r;

884 885 886
	if (!mgr)
		return -EINVAL;

T
Tomi Valkeinen 已提交
887 888
	mutex_lock(&apply_lock);

889 890 891
	if (ovl->manager) {
		DSSERR("overlay '%s' already has a manager '%s'\n",
				ovl->name, ovl->manager->name);
T
Tomi Valkeinen 已提交
892 893
		r = -EINVAL;
		goto err;
894 895
	}

896 897 898 899
	spin_lock_irqsave(&data_lock, flags);

	if (op->enabled) {
		spin_unlock_irqrestore(&data_lock, flags);
900
		DSSERR("overlay has to be disabled to change the manager\n");
T
Tomi Valkeinen 已提交
901 902
		r = -EINVAL;
		goto err;
903 904
	}

905 906 907
	op->channel = mgr->id;
	op->extra_info_dirty = true;

908 909 910
	ovl->manager = mgr;
	list_add_tail(&ovl->list, &mgr->overlays);

911 912
	spin_unlock_irqrestore(&data_lock, flags);

913 914 915 916 917 918 919 920 921 922 923 924 925
	/* 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 已提交
926 927
	mutex_unlock(&apply_lock);

928
	return 0;
T
Tomi Valkeinen 已提交
929 930 931
err:
	mutex_unlock(&apply_lock);
	return r;
932 933 934 935
}

int dss_ovl_unset_manager(struct omap_overlay *ovl)
{
936 937
	struct ovl_priv_data *op = get_ovl_priv(ovl);
	unsigned long flags;
T
Tomi Valkeinen 已提交
938 939 940 941
	int r;

	mutex_lock(&apply_lock);

942 943
	if (!ovl->manager) {
		DSSERR("failed to detach overlay: manager not set\n");
T
Tomi Valkeinen 已提交
944 945
		r = -EINVAL;
		goto err;
946 947
	}

948 949 950 951
	spin_lock_irqsave(&data_lock, flags);

	if (op->enabled) {
		spin_unlock_irqrestore(&data_lock, flags);
952
		DSSERR("overlay has to be disabled to unset the manager\n");
T
Tomi Valkeinen 已提交
953 954
		r = -EINVAL;
		goto err;
955 956
	}

957 958
	op->channel = -1;

959 960 961
	ovl->manager = NULL;
	list_del(&ovl->list);

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 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
	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;
	int r;

	mutex_lock(&apply_lock);

	if (ovl->manager == NULL || ovl->manager->device == NULL) {
		r = -EINVAL;
		goto err;
	}

	spin_lock_irqsave(&data_lock, flags);

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

1005 1006
	dss_ovl_setup_fifo(ovl);

1007 1008
	dss_write_regs();

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
	spin_unlock_irqrestore(&data_lock, flags);

	mutex_unlock(&apply_lock);

	return 0;
err:
	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;
	int r;

	mutex_lock(&apply_lock);

	if (ovl->manager == NULL || ovl->manager->device == NULL) {
		r = -EINVAL;
		goto err;
	}

	spin_lock_irqsave(&data_lock, flags);

	op->enabled = false;
	op->extra_info_dirty = true;

1037 1038
	dss_write_regs();

1039 1040
	spin_unlock_irqrestore(&data_lock, flags);

T
Tomi Valkeinen 已提交
1041 1042
	mutex_unlock(&apply_lock);

1043
	return 0;
1044

T
Tomi Valkeinen 已提交
1045 1046 1047
err:
	mutex_unlock(&apply_lock);
	return r;
1048 1049
}