Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
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
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*
 * Copyright © 2010-2011 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 * Jim Liu <jim.liu@intel.com>
 * Jackie Li<yaodong.li@intel.com>
 */

#include "mdfld_dsi_dbi_dpu.h"
#include "mdfld_dsi_dbi.h"

/*
 * NOTE: all mdlfd_x_damage funcs should be called by holding dpu_update_lock
 */

static int mdfld_cursor_damage(struct mdfld_dbi_dpu_info *dpu_info,
			   mdfld_plane_t plane,
			   struct psb_drm_dpu_rect *damaged_rect)
{
	int x, y;
	int new_x, new_y;
	struct psb_drm_dpu_rect *rect;
	struct psb_drm_dpu_rect *pipe_rect;
	int cursor_size;
	struct mdfld_cursor_info *cursor;
	mdfld_plane_t fb_plane;

	if (plane == MDFLD_CURSORA) {
		cursor = &dpu_info->cursors[0];
		x = dpu_info->cursors[0].x;
		y = dpu_info->cursors[0].y;
		cursor_size = dpu_info->cursors[0].size;
		pipe_rect = &dpu_info->damage_pipea;
		fb_plane = MDFLD_PLANEA;
	} else {
		cursor = &dpu_info->cursors[1];
		x = dpu_info->cursors[1].x;
		y = dpu_info->cursors[1].y;
		cursor_size = dpu_info->cursors[1].size;
		pipe_rect = &dpu_info->damage_pipec;
		fb_plane = MDFLD_PLANEC;
	}
	new_x = damaged_rect->x;
	new_y = damaged_rect->y;

	if (x == new_x && y == new_y)
		return 0;

	rect = &dpu_info->damaged_rects[plane];
	/* Move to right */
	if (new_x >= x) {
		if (new_y > y) {
			rect->x = x;
			rect->y = y;
			rect->width = (new_x + cursor_size) - x;
			rect->height = (new_y + cursor_size) - y;
			goto cursor_out;
		} else {
			rect->x = x;
			rect->y = new_y;
			rect->width = (new_x + cursor_size) - x;
			rect->height = (y - new_y);
			goto cursor_out;
		}
	} else {
		if (new_y > y) {
			rect->x = new_x;
			rect->y = y;
			rect->width = (x + cursor_size) - new_x;
			rect->height = new_y - y;
			goto cursor_out;
		} else {
			rect->x = new_x;
			rect->y = new_y;
			rect->width = (x + cursor_size) - new_x;
			rect->height = (y + cursor_size) - new_y;
		}
	}
cursor_out:
	if (new_x < 0)
		cursor->x = 0;
	else if (new_x > 864)
		cursor->x = 864;
	else
		cursor->x = new_x;

	if (new_y < 0)
		cursor->y = 0;
	else if (new_y > 480)
		cursor->y = 480;
	else
		cursor->y = new_y;

	/*
	 * FIXME: this is a workaround for cursor plane update,
	 * remove it later!
	 */
	rect->x = 0;
	rect->y = 0;
	rect->width = 864;
	rect->height = 480;

	mdfld_check_boundary(dpu_info, rect);
	mdfld_dpu_region_extent(pipe_rect, rect);

	/* Update pending status of dpu_info */
	dpu_info->pending |= (1 << plane);
	/* Update fb panel as well */
	dpu_info->pending |= (1 << fb_plane);
	return 0;
}

static int mdfld_fb_damage(struct mdfld_dbi_dpu_info *dpu_info,
				   mdfld_plane_t plane,
				   struct psb_drm_dpu_rect *damaged_rect)
{
	struct psb_drm_dpu_rect *rect;

	if (plane == MDFLD_PLANEA)
		rect = &dpu_info->damage_pipea;
	else
		rect = &dpu_info->damage_pipec;

	mdfld_check_boundary(dpu_info, damaged_rect);

	/* Add fb damage area to this pipe */
	mdfld_dpu_region_extent(rect, damaged_rect);

	/* Update pending status of dpu_info */
	dpu_info->pending |= (1 << plane);
	return 0;
}

/* Do nothing here, right now */
static int mdfld_overlay_damage(struct mdfld_dbi_dpu_info *dpu_info,
				mdfld_plane_t plane,
				struct psb_drm_dpu_rect *damaged_rect)
{
	return 0;
}

int mdfld_dbi_dpu_report_damage(struct drm_device *dev,
				mdfld_plane_t plane,
				struct psb_drm_dpu_rect *rect)
{
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct mdfld_dbi_dpu_info *dpu_info = dev_priv->dbi_dpu_info;
	int ret = 0;

	/* DPU not in use, no damage reporting needed */
	if (dpu_info == NULL)
		return 0;

	spin_lock(&dpu_info->dpu_update_lock);

	switch (plane) {
	case MDFLD_PLANEA:
	case MDFLD_PLANEC:
		mdfld_fb_damage(dpu_info, plane, rect);
		break;
	case MDFLD_CURSORA:
	case MDFLD_CURSORC:
		mdfld_cursor_damage(dpu_info, plane, rect);
		break;
	case MDFLD_OVERLAYA:
	case MDFLD_OVERLAYC:
		mdfld_overlay_damage(dpu_info, plane, rect);
		break;
	default:
		DRM_ERROR("Invalid plane type %d\n", plane);
		ret = -EINVAL;
	}
	spin_unlock(&dpu_info->dpu_update_lock);
	return ret;
}

int mdfld_dbi_dpu_report_fullscreen_damage(struct drm_device *dev)
{
	struct drm_psb_private *dev_priv;
	struct mdfld_dbi_dpu_info *dpu_info;
	struct mdfld_dsi_config  *dsi_config;
	struct psb_drm_dpu_rect rect;
	int i;

	if (!dev) {
		DRM_ERROR("Invalid parameter\n");
		return -EINVAL;
	}

	dev_priv = dev->dev_private;
	dpu_info = dev_priv->dbi_dpu_info;

	/* This is fine - we may be in non DPU mode */
	if (!dpu_info)
		return -EINVAL;

	for (i = 0; i < dpu_info->dbi_output_num; i++) {
		dsi_config = dev_priv->dsi_configs[i];
		if (dsi_config) {
			rect.x = rect.y = 0;
			rect.width = dsi_config->fixed_mode->hdisplay;
			rect.height = dsi_config->fixed_mode->vdisplay;
			mdfld_dbi_dpu_report_damage(dev,
				    i ? (MDFLD_PLANEC) : (MDFLD_PLANEA),
				    &rect);
		}
	}
	/* Exit DSR state */
	mdfld_dpu_exit_dsr(dev);
	return 0;
}

int mdfld_dsi_dbi_dsr_off(struct drm_device *dev,
					struct psb_drm_dpu_rect *rect)
{
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct mdfld_dbi_dpu_info *dpu_info = dev_priv->dbi_dpu_info;

	mdfld_dbi_dpu_report_damage(dev, MDFLD_PLANEA, rect);

	/* If dual display mode */
	if (dpu_info->dbi_output_num == 2)
		mdfld_dbi_dpu_report_damage(dev, MDFLD_PLANEC, rect);

	/* Force dsi to exit DSR mode */
	mdfld_dpu_exit_dsr(dev);
	return 0;
}

static void mdfld_dpu_cursor_plane_flush(struct mdfld_dbi_dpu_info *dpu_info,
						 mdfld_plane_t plane)
{
	struct drm_device *dev = dpu_info->dev;
	u32 curpos_reg = CURAPOS;
	u32 curbase_reg = CURABASE;
	u32 curcntr_reg = CURACNTR;
	struct mdfld_cursor_info *cursor = &dpu_info->cursors[0];

	if (plane == MDFLD_CURSORC) {
		curpos_reg = CURCPOS;
		curbase_reg = CURCBASE;
		curcntr_reg = CURCCNTR;
		cursor = &dpu_info->cursors[1];
	}

	REG_WRITE(curcntr_reg, REG_READ(curcntr_reg));
	REG_WRITE(curpos_reg,
		(((cursor->x & CURSOR_POS_MASK) << CURSOR_X_SHIFT) |
		((cursor->y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT)));
	REG_WRITE(curbase_reg, REG_READ(curbase_reg));
}

static void mdfld_dpu_fb_plane_flush(struct mdfld_dbi_dpu_info *dpu_info,
						 mdfld_plane_t plane)
{
	u32 pipesrc_reg = PIPEASRC;
	u32 dspsize_reg = DSPASIZE;
	u32 dspoff_reg = DSPALINOFF;
	u32 dspsurf_reg = DSPASURF;
	u32 dspstride_reg = DSPASTRIDE;
	u32 stride;
	struct psb_drm_dpu_rect *rect = &dpu_info->damage_pipea;
	struct drm_device *dev = dpu_info->dev;

	if (plane == MDFLD_PLANEC) {
		pipesrc_reg = PIPECSRC;
		dspsize_reg = DSPCSIZE;
		dspoff_reg = DSPCLINOFF;
		dspsurf_reg = DSPCSURF;
		dspstride_reg = DSPCSTRIDE;
		rect = &dpu_info->damage_pipec;
	}

	stride = REG_READ(dspstride_reg);
	/* FIXME: should I do the pipe src update here? */
	REG_WRITE(pipesrc_reg, ((rect->width - 1) << 16) | (rect->height - 1));
	/* Flush plane */
	REG_WRITE(dspsize_reg, ((rect->height - 1) << 16) | (rect->width - 1));
	REG_WRITE(dspoff_reg, ((rect->x * 4) + (rect->y * stride)));
	REG_WRITE(dspsurf_reg, REG_READ(dspsurf_reg));

	/*
	 * TODO: wait for flip finished and restore the pipesrc reg,
	 * or cursor will be show at a wrong position
	 */
}

static void mdfld_dpu_overlay_plane_flush(struct mdfld_dbi_dpu_info *dpu_info,
						  mdfld_plane_t plane)
{
}

/*
 * TODO: we are still in dbi normal mode now, we will try to use partial
 * mode later.
 */
static int mdfld_dbi_prepare_cb(struct mdfld_dsi_dbi_output *dbi_output,
				struct mdfld_dbi_dpu_info *dpu_info, int pipe)
{
	u8 *cb_addr = (u8 *)dbi_output->dbi_cb_addr;
	u32 *index;
	struct psb_drm_dpu_rect *rect = pipe ?
		(&dpu_info->damage_pipec) : (&dpu_info->damage_pipea);

	/* FIXME: lock command buffer, this may lead to a deadlock,
	   as we already hold the dpu_update_lock */
	if (!spin_trylock(&dbi_output->cb_lock)) {
		DRM_ERROR("lock command buffer failed, try again\n");
		return -EAGAIN;
	}

	index = &dbi_output->cb_write;

	if (*index) {
		DRM_ERROR("DBI command buffer unclean\n");
		return -EAGAIN;
	}

	/* Column address */
	*(cb_addr + ((*index)++)) = set_column_address;
	*(cb_addr + ((*index)++)) = rect->x >> 8;
	*(cb_addr + ((*index)++)) = rect->x;
	*(cb_addr + ((*index)++)) = (rect->x + rect->width - 1) >> 8;
	*(cb_addr + ((*index)++)) = (rect->x + rect->width - 1);

	*index = 8;

	/* Page address */
	*(cb_addr + ((*index)++)) = set_page_addr;
	*(cb_addr + ((*index)++)) = rect->y >> 8;
	*(cb_addr + ((*index)++)) = rect->y;
	*(cb_addr + ((*index)++)) = (rect->y + rect->height - 1) >> 8;
	*(cb_addr + ((*index)++)) = (rect->y + rect->height - 1);

	*index = 16;

	/*write memory*/
	*(cb_addr + ((*index)++)) = write_mem_start;

	return 0;
}

static int mdfld_dbi_flush_cb(struct mdfld_dsi_dbi_output *dbi_output, int pipe)
{
	u32 cmd_phy = dbi_output->dbi_cb_phy;
	u32 *index = &dbi_output->cb_write;
	int reg_offset = pipe ? MIPIC_REG_OFFSET : 0;
	struct drm_device *dev = dbi_output->dev;

	if (*index == 0 || !dbi_output)
		return 0;

	REG_WRITE((MIPIA_CMD_LEN_REG + reg_offset), 0x010505);
	REG_WRITE((MIPIA_CMD_ADD_REG + reg_offset), cmd_phy | 3);

	*index = 0;

	/* FIXME: unlock command buffer */
	spin_unlock(&dbi_output->cb_lock);
	return 0;
}

static int mdfld_dpu_update_pipe(struct mdfld_dsi_dbi_output *dbi_output,
				 struct mdfld_dbi_dpu_info *dpu_info, int pipe)
{
	struct drm_device *dev =  dbi_output->dev;
	struct drm_psb_private *dev_priv = dev->dev_private;
	mdfld_plane_t cursor_plane = MDFLD_CURSORA;
	mdfld_plane_t fb_plane = MDFLD_PLANEA;
	mdfld_plane_t overlay_plane = MDFLD_OVERLAYA;
	int ret = 0;
	u32 plane_mask = MDFLD_PIPEA_PLANE_MASK;

	/* Damaged rects on this pipe */
	if (pipe) {
		cursor_plane = MDFLD_CURSORC;
		fb_plane = MDFLD_PLANEC;
		overlay_plane = MDFLD_OVERLAYC;
		plane_mask = MDFLD_PIPEC_PLANE_MASK;
	}

	/*update cursor which assigned to @pipe*/
	if (dpu_info->pending & (1 << cursor_plane))
		mdfld_dpu_cursor_plane_flush(dpu_info, cursor_plane);

	/*update fb which assigned to @pipe*/
	if (dpu_info->pending & (1 << fb_plane))
		mdfld_dpu_fb_plane_flush(dpu_info, fb_plane);

	/* TODO: update overlay */
	if (dpu_info->pending & (1 << overlay_plane))
		mdfld_dpu_overlay_plane_flush(dpu_info, overlay_plane);

	/* Flush damage area to panel fb */
	if (dpu_info->pending & plane_mask) {
		ret = mdfld_dbi_prepare_cb(dbi_output, dpu_info, pipe);
		/*
		 * TODO: remove b_dsr_enable later,
		 * added it so that text console could boot smoothly
		 */
		/* Clean pending flags on this pipe */
		if (!ret && dev_priv->dsr_enable) {
			dpu_info->pending &= ~plane_mask;
			/* Reset overlay pipe damage rect */
			mdfld_dpu_init_damage(dpu_info, pipe);
		}
	}
	return ret;
}

static int mdfld_dpu_update_fb(struct drm_device *dev)
{
	struct drm_crtc *crtc;
	struct psb_intel_crtc *psb_crtc;
	struct mdfld_dsi_dbi_output **dbi_output;
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct mdfld_dbi_dpu_info *dpu_info = dev_priv->dbi_dpu_info;
	bool pipe_updated[2];
	unsigned long irq_flags;
	u32 dpll_reg = MRST_DPLL_A;
	u32 dspcntr_reg = DSPACNTR;
	u32 pipeconf_reg = PIPEACONF;
	u32 dsplinoff_reg = DSPALINOFF;
	u32 dspsurf_reg = DSPASURF;
	u32 mipi_state_reg = MIPIA_INTR_STAT_REG;
	u32 reg_offset = 0;
	int pipe;
	int i;
	int ret;

	dbi_output = dpu_info->dbi_outputs;
	pipe_updated[0] = pipe_updated[1] = false;

	if (!gma_power_begin(dev, true))
		return -EAGAIN;

	/* Try to prevent any new damage reports */
	if (!spin_trylock_irqsave(&dpu_info->dpu_update_lock, irq_flags))
		return -EAGAIN;

	for (i = 0; i < dpu_info->dbi_output_num; i++) {
		crtc = dbi_output[i]->base.base.crtc;
		psb_crtc = (crtc) ? to_psb_intel_crtc(crtc) : NULL;

		pipe = dbi_output[i]->channel_num ? 2 : 0;

		if (pipe == 2) {
			dspcntr_reg = DSPCCNTR;
			pipeconf_reg = PIPECCONF;
			dsplinoff_reg = DSPCLINOFF;
			dspsurf_reg = DSPCSURF;
			reg_offset = MIPIC_REG_OFFSET;
		}

		if (!(REG_READ((MIPIA_GEN_FIFO_STAT_REG + reg_offset))
							& (1 << 27)) ||
			!(REG_READ(dpll_reg) & DPLL_VCO_ENABLE) ||
			!(REG_READ(dspcntr_reg) & DISPLAY_PLANE_ENABLE) ||
			!(REG_READ(pipeconf_reg) & DISPLAY_PLANE_ENABLE)) {
			dev_err(dev->dev,
				"DBI FIFO is busy, DSI %d state %x\n",
				pipe,
				REG_READ(mipi_state_reg + reg_offset));
			continue;
		}

		/*
		 *	If DBI output is in a exclusive state then the pipe
		 *	change won't be updated
		 */
		if (dbi_output[i]->dbi_panel_on &&
		   !(dbi_output[i]->mode_flags & MODE_SETTING_ON_GOING) &&
		   !(psb_crtc &&
			psb_crtc->mode_flags & MODE_SETTING_ON_GOING) &&
		   !(dbi_output[i]->mode_flags & MODE_SETTING_IN_DSR)) {
			ret = mdfld_dpu_update_pipe(dbi_output[i],
				dpu_info, dbi_output[i]->channel_num ? 2 : 0);
			if (!ret)
				pipe_updated[i] = true;
		}
	}

	for (i = 0; i < dpu_info->dbi_output_num; i++)
		if (pipe_updated[i])
			mdfld_dbi_flush_cb(dbi_output[i],
				dbi_output[i]->channel_num ? 2 : 0);

	spin_unlock_irqrestore(&dpu_info->dpu_update_lock, irq_flags);
	gma_power_end(dev);
	return 0;
}

static int __mdfld_dbi_exit_dsr(struct mdfld_dsi_dbi_output *dbi_output,
								int pipe)
{
	struct drm_device *dev = dbi_output->dev;
	struct drm_crtc *crtc = dbi_output->base.base.crtc;
	struct psb_intel_crtc *psb_crtc = (crtc) ? to_psb_intel_crtc(crtc)
								: NULL;
	u32 reg_val;
	u32 dpll_reg = MRST_DPLL_A;
	u32 pipeconf_reg = PIPEACONF;
	u32 dspcntr_reg = DSPACNTR;
	u32 dspbase_reg = DSPABASE;
	u32 dspsurf_reg = DSPASURF;
	u32 reg_offset = 0;

	if (!dbi_output)
		return 0;

	/* If mode setting on-going, back off */
	if ((dbi_output->mode_flags & MODE_SETTING_ON_GOING) ||
		(psb_crtc && psb_crtc->mode_flags & MODE_SETTING_ON_GOING))
		return -EAGAIN;

	if (pipe == 2) {
		dpll_reg = MRST_DPLL_A;
		pipeconf_reg = PIPECCONF;
		dspcntr_reg = DSPCCNTR;
		dspbase_reg = MDFLD_DSPCBASE;
		dspsurf_reg = DSPCSURF;

		reg_offset = MIPIC_REG_OFFSET;
	}

	if (!gma_power_begin(dev, true))
		return -EAGAIN;

	/* Enable DPLL */
	reg_val = REG_READ(dpll_reg);
	if (!(reg_val & DPLL_VCO_ENABLE)) {

		if (reg_val & MDFLD_PWR_GATE_EN) {
			reg_val &= ~MDFLD_PWR_GATE_EN;
			REG_WRITE(dpll_reg, reg_val);
			REG_READ(dpll_reg);
			udelay(500);
		}

		reg_val |= DPLL_VCO_ENABLE;
		REG_WRITE(dpll_reg, reg_val);
		REG_READ(dpll_reg);
		udelay(500);

		/* FIXME: add timeout */
		while (!(REG_READ(pipeconf_reg) & PIPECONF_DSIPLL_LOCK))
			cpu_relax();
	}

	/* Enable pipe */
	reg_val = REG_READ(pipeconf_reg);
	if (!(reg_val & PIPEACONF_ENABLE)) {
		reg_val |= PIPEACONF_ENABLE;
		REG_WRITE(pipeconf_reg, reg_val);
		REG_READ(pipeconf_reg);
		udelay(500);
		mdfldWaitForPipeEnable(dev, pipe);
	}

	/* Enable plane */
	reg_val = REG_READ(dspcntr_reg);
	if (!(reg_val & DISPLAY_PLANE_ENABLE)) {
		reg_val |= DISPLAY_PLANE_ENABLE;
		REG_WRITE(dspcntr_reg, reg_val);
		REG_READ(dspcntr_reg);
		udelay(500);
	}

	gma_power_end(dev);

	/* Clean IN_DSR flag */
	dbi_output->mode_flags &= ~MODE_SETTING_IN_DSR;

	return 0;
}

int mdfld_dpu_exit_dsr(struct drm_device *dev)
{
	struct mdfld_dsi_dbi_output **dbi_output;
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct mdfld_dbi_dpu_info *dpu_info = dev_priv->dbi_dpu_info;
	int i;
	int pipe;

	dbi_output = dpu_info->dbi_outputs;

	for (i = 0; i < dpu_info->dbi_output_num; i++) {
		/* If this output is not in DSR mode, don't call exit dsr */
		if (dbi_output[i]->mode_flags & MODE_SETTING_IN_DSR)
			__mdfld_dbi_exit_dsr(dbi_output[i],
					dbi_output[i]->channel_num ? 2 : 0);
	}

	/* Enable TE interrupt */
	for (i = 0; i < dpu_info->dbi_output_num; i++) {
		/* If this output is not in DSR mode, don't call exit dsr */
		pipe = dbi_output[i]->channel_num ? 2 : 0;
		if (dbi_output[i]->dbi_panel_on && pipe) {
			mdfld_disable_te(dev, 0);
			mdfld_enable_te(dev, 2);
		} else if (dbi_output[i]->dbi_panel_on && !pipe) {
			mdfld_disable_te(dev, 2);
			mdfld_enable_te(dev, 0);
		}
	}
	return 0;
}

static int mdfld_dpu_enter_dsr(struct drm_device *dev)
{
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct mdfld_dbi_dpu_info *dpu_info = dev_priv->dbi_dpu_info;
	struct mdfld_dsi_dbi_output **dbi_output;
	int i;

	dbi_output = dpu_info->dbi_outputs;

	for (i = 0; i < dpu_info->dbi_output_num; i++) {
		/* If output is off or already in DSR state, don't re-enter */
		if (dbi_output[i]->dbi_panel_on &&
		   !(dbi_output[i]->mode_flags & MODE_SETTING_IN_DSR)) {
			mdfld_dsi_dbi_enter_dsr(dbi_output[i],
				dbi_output[i]->channel_num ? 2 : 0);
		}
	}

	return 0;
}

static void mdfld_dbi_dpu_timer_func(unsigned long data)
{
	struct drm_device *dev = (struct drm_device *)data;
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct mdfld_dbi_dpu_info *dpu_info = dev_priv->dbi_dpu_info;
	struct timer_list *dpu_timer = &dpu_info->dpu_timer;
	unsigned long flags;

	if (dpu_info->pending) {
		dpu_info->idle_count = 0;
		/* Update panel fb with damaged area */
		mdfld_dpu_update_fb(dev);
	} else {
		dpu_info->idle_count++;
	}

	if (dpu_info->idle_count >= MDFLD_MAX_IDLE_COUNT) {
		mdfld_dpu_enter_dsr(dev);
		/* Stop timer by return */
		return;
	}

	spin_lock_irqsave(&dpu_info->dpu_timer_lock, flags);
	if (!timer_pending(dpu_timer)) {
		dpu_timer->expires = jiffies + MDFLD_DSR_DELAY;
		add_timer(dpu_timer);
	}
	spin_unlock_irqrestore(&dpu_info->dpu_timer_lock, flags);
}

void mdfld_dpu_update_panel(struct drm_device *dev)
{
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct mdfld_dbi_dpu_info *dpu_info = dev_priv->dbi_dpu_info;

	if (dpu_info->pending) {
		dpu_info->idle_count = 0;

		/*update panel fb with damaged area*/
		mdfld_dpu_update_fb(dev);
	} else {
		dpu_info->idle_count++;
	}

	if (dpu_info->idle_count >= MDFLD_MAX_IDLE_COUNT) {
		/*enter dsr*/
		mdfld_dpu_enter_dsr(dev);
	}
}

static int mdfld_dbi_dpu_timer_init(struct drm_device *dev,
				struct mdfld_dbi_dpu_info *dpu_info)
{
	struct timer_list *dpu_timer = &dpu_info->dpu_timer;
	unsigned long flags;

	spin_lock_init(&dpu_info->dpu_timer_lock);
	spin_lock_irqsave(&dpu_info->dpu_timer_lock, flags);

	init_timer(dpu_timer);

	dpu_timer->data = (unsigned long)dev;
	dpu_timer->function = mdfld_dbi_dpu_timer_func;
	dpu_timer->expires = jiffies + MDFLD_DSR_DELAY;

	spin_unlock_irqrestore(&dpu_info->dpu_timer_lock, flags);

	return 0;
}

void mdfld_dbi_dpu_timer_start(struct mdfld_dbi_dpu_info *dpu_info)
{
	struct timer_list *dpu_timer = &dpu_info->dpu_timer;
	unsigned long flags;

	spin_lock_irqsave(&dpu_info->dpu_timer_lock, flags);
	if (!timer_pending(dpu_timer)) {
		dpu_timer->expires = jiffies + MDFLD_DSR_DELAY;
		add_timer(dpu_timer);
	}
	spin_unlock_irqrestore(&dpu_info->dpu_timer_lock, flags);
}

int mdfld_dbi_dpu_init(struct drm_device *dev)
{
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct mdfld_dbi_dpu_info *dpu_info = dev_priv->dbi_dpu_info;

	if (!dpu_info || IS_ERR(dpu_info)) {
		dpu_info = kzalloc(sizeof(struct mdfld_dbi_dpu_info),
								GFP_KERNEL);
		if (!dpu_info) {
			DRM_ERROR("No memory\n");
			return -ENOMEM;
		}
		dev_priv->dbi_dpu_info = dpu_info;
	}

	dpu_info->dev = dev;

	dpu_info->cursors[0].size = MDFLD_CURSOR_SIZE;
	dpu_info->cursors[1].size = MDFLD_CURSOR_SIZE;

	/*init dpu_update_lock*/
	spin_lock_init(&dpu_info->dpu_update_lock);

	/*init dpu refresh timer*/
	mdfld_dbi_dpu_timer_init(dev, dpu_info);

	/*init pipe damage area*/
	mdfld_dpu_init_damage(dpu_info, 0);
	mdfld_dpu_init_damage(dpu_info, 2);

	return 0;
}

void mdfld_dbi_dpu_exit(struct drm_device *dev)
{
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct mdfld_dbi_dpu_info *dpu_info = dev_priv->dbi_dpu_info;

	if (!dpu_info)
		return;

	del_timer_sync(&dpu_info->dpu_timer);
	kfree(dpu_info);
	dev_priv->dbi_dpu_info = NULL;
}