aboutsummaryrefslogtreecommitdiff
path: root/drivers/mxc/ipu3/ipu_ic.c
blob: 7387e518deb5e495473839c9feb352652c5e3d66 (plain)
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*
 * Copyright 2005-2011 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*
 * @file ipu_ic.c
 *
 * @brief IPU IC functions
 *
 * @ingroup IPU
 */
#include <linux/types.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/spinlock.h>
#include <linux/videodev2.h>
#include <linux/io.h>
#include <mach/ipu-v3.h>

#include "ipu_prv.h"
#include "ipu_regs.h"
#include "ipu_param_mem.h"

enum {
	IC_TASK_VIEWFINDER,
	IC_TASK_ENCODER,
	IC_TASK_POST_PROCESSOR
};

static void _init_csc(struct ipu_soc *ipu, uint8_t ic_task, ipu_color_space_t in_format,
		      ipu_color_space_t out_format, int csc_index);
static bool _calc_resize_coeffs(struct ipu_soc *ipu,
				uint32_t inSize, uint32_t outSize,
				uint32_t *resizeCoeff,
				uint32_t *downsizeCoeff);

void _ipu_vdi_set_top_field_man(struct ipu_soc *ipu, bool top_field_0)
{
	uint32_t reg;

	reg = ipu_vdi_read(ipu, VDI_C);
	if (top_field_0)
		reg &= ~VDI_C_TOP_FIELD_MAN_1;
	else
		reg |= VDI_C_TOP_FIELD_MAN_1;
	ipu_vdi_write(ipu, reg, VDI_C);
}

void _ipu_vdi_set_motion(struct ipu_soc *ipu, ipu_motion_sel motion_sel)
{
	uint32_t reg;

	reg = ipu_vdi_read(ipu, VDI_C);
	reg &= ~(VDI_C_MOT_SEL_FULL | VDI_C_MOT_SEL_MED | VDI_C_MOT_SEL_LOW);
	if (motion_sel == HIGH_MOTION)
		reg |= VDI_C_MOT_SEL_FULL;
	else if (motion_sel == MED_MOTION)
		reg |= VDI_C_MOT_SEL_MED;
	else
		reg |= VDI_C_MOT_SEL_LOW;

	ipu_vdi_write(ipu, reg, VDI_C);
}

void ic_dump_register(struct ipu_soc *ipu)
{
	printk(KERN_DEBUG "IC_CONF = \t0x%08X\n", ipu_ic_read(ipu, IC_CONF));
	printk(KERN_DEBUG "IC_PRP_ENC_RSC = \t0x%08X\n",
	       ipu_ic_read(ipu, IC_PRP_ENC_RSC));
	printk(KERN_DEBUG "IC_PRP_VF_RSC = \t0x%08X\n",
	       ipu_ic_read(ipu, IC_PRP_VF_RSC));
	printk(KERN_DEBUG "IC_PP_RSC = \t0x%08X\n", ipu_ic_read(ipu, IC_PP_RSC));
	printk(KERN_DEBUG "IC_IDMAC_1 = \t0x%08X\n", ipu_ic_read(ipu, IC_IDMAC_1));
	printk(KERN_DEBUG "IC_IDMAC_2 = \t0x%08X\n", ipu_ic_read(ipu, IC_IDMAC_2));
	printk(KERN_DEBUG "IC_IDMAC_3 = \t0x%08X\n", ipu_ic_read(ipu, IC_IDMAC_3));
}

void _ipu_ic_enable_task(struct ipu_soc *ipu, ipu_channel_t channel)
{
	uint32_t ic_conf;

	ic_conf = ipu_ic_read(ipu, IC_CONF);
	switch (channel) {
	case CSI_PRP_VF_MEM:
	case MEM_PRP_VF_MEM:
		ic_conf |= IC_CONF_PRPVF_EN;
		break;
	case MEM_VDI_PRP_VF_MEM:
		ic_conf |= IC_CONF_PRPVF_EN;
		break;
	case MEM_ROT_VF_MEM:
		ic_conf |= IC_CONF_PRPVF_ROT_EN;
		break;
	case CSI_PRP_ENC_MEM:
	case MEM_PRP_ENC_MEM:
		ic_conf |= IC_CONF_PRPENC_EN;
		break;
	case MEM_ROT_ENC_MEM:
		ic_conf |= IC_CONF_PRPENC_ROT_EN;
		break;
	case MEM_PP_MEM:
		ic_conf |= IC_CONF_PP_EN;
		break;
	case MEM_ROT_PP_MEM:
		ic_conf |= IC_CONF_PP_ROT_EN;
		break;
	default:
		break;
	}
	ipu_ic_write(ipu, ic_conf, IC_CONF);
}

void _ipu_ic_disable_task(struct ipu_soc *ipu, ipu_channel_t channel)
{
	uint32_t ic_conf;

	ic_conf = ipu_ic_read(ipu, IC_CONF);
	switch (channel) {
	case CSI_PRP_VF_MEM:
	case MEM_PRP_VF_MEM:
		ic_conf &= ~IC_CONF_PRPVF_EN;
		break;
	case MEM_VDI_PRP_VF_MEM:
		ic_conf &= ~IC_CONF_PRPVF_EN;
		break;
	case MEM_ROT_VF_MEM:
		ic_conf &= ~IC_CONF_PRPVF_ROT_EN;
		break;
	case CSI_PRP_ENC_MEM:
	case MEM_PRP_ENC_MEM:
		ic_conf &= ~IC_CONF_PRPENC_EN;
		break;
	case MEM_ROT_ENC_MEM:
		ic_conf &= ~IC_CONF_PRPENC_ROT_EN;
		break;
	case MEM_PP_MEM:
		ic_conf &= ~IC_CONF_PP_EN;
		break;
	case MEM_ROT_PP_MEM:
		ic_conf &= ~IC_CONF_PP_ROT_EN;
		break;
	default:
		break;
	}
	ipu_ic_write(ipu, ic_conf, IC_CONF);
}

void _ipu_vdi_init(struct ipu_soc *ipu, ipu_channel_t channel, ipu_channel_params_t *params)
{
	uint32_t reg;
	uint32_t pixel_fmt;

	reg = ((params->mem_prp_vf_mem.in_height-1) << 16) |
	  (params->mem_prp_vf_mem.in_width-1);
	ipu_vdi_write(ipu, reg, VDI_FSIZE);

	/* Full motion, only vertical filter is used
	   Burst size is 4 accesses */
	if (params->mem_prp_vf_mem.in_pixel_fmt ==
	     IPU_PIX_FMT_UYVY ||
	     params->mem_prp_vf_mem.in_pixel_fmt ==
	     IPU_PIX_FMT_YUYV)
		pixel_fmt = VDI_C_CH_422;
	else
		pixel_fmt = VDI_C_CH_420;

	reg = ipu_vdi_read(ipu, VDI_C);
	reg |= pixel_fmt;
	switch (channel) {
	case MEM_VDI_PRP_VF_MEM:
		reg |= VDI_C_BURST_SIZE2_4;
		break;
	case MEM_VDI_PRP_VF_MEM_P:
		reg |= VDI_C_BURST_SIZE1_4 | VDI_C_VWM1_SET_1 | VDI_C_VWM1_CLR_2;
		break;
	case MEM_VDI_PRP_VF_MEM_N:
		reg |= VDI_C_BURST_SIZE3_4 | VDI_C_VWM3_SET_1 | VDI_C_VWM3_CLR_2;
		break;
	default:
		break;
	}
	ipu_vdi_write(ipu, reg, VDI_C);

	if (params->mem_prp_vf_mem.field_fmt == V4L2_FIELD_INTERLACED_TB)
		_ipu_vdi_set_top_field_man(ipu, false);
	else if (params->mem_prp_vf_mem.field_fmt == V4L2_FIELD_INTERLACED_BT)
		_ipu_vdi_set_top_field_man(ipu, true);

	_ipu_vdi_set_motion(ipu, params->mem_prp_vf_mem.motion_sel);

	reg = ipu_ic_read(ipu, IC_CONF);
	reg &= ~IC_CONF_RWS_EN;
	ipu_ic_write(ipu, reg, IC_CONF);
}

void _ipu_vdi_uninit(struct ipu_soc *ipu)
{
	ipu_vdi_write(ipu, 0, VDI_FSIZE);
	ipu_vdi_write(ipu, 0, VDI_C);
}

void _ipu_ic_init_prpvf(struct ipu_soc *ipu, ipu_channel_params_t *params, bool src_is_csi)
{
	uint32_t reg, ic_conf;
	uint32_t downsizeCoeff, resizeCoeff;
	ipu_color_space_t in_fmt, out_fmt;

	/* Setup vertical resizing */
	if (!(params->mem_prp_vf_mem.outv_resize_ratio)) {
		_calc_resize_coeffs(ipu, params->mem_prp_vf_mem.in_height,
				params->mem_prp_vf_mem.out_height,
				&resizeCoeff, &downsizeCoeff);
		reg = (downsizeCoeff << 30) | (resizeCoeff << 16);
	} else
		reg = (params->mem_prp_vf_mem.outv_resize_ratio) << 16;

	/* Setup horizontal resizing */
	/* Upadeted for IC split case */
	if (!(params->mem_prp_vf_mem.outh_resize_ratio)) {
		_calc_resize_coeffs(ipu, params->mem_prp_vf_mem.in_width,
				params->mem_prp_vf_mem.out_width,
				&resizeCoeff, &downsizeCoeff);
		reg |= (downsizeCoeff << 14) | resizeCoeff;
	} else
		reg |= params->mem_prp_vf_mem.outh_resize_ratio;

	ipu_ic_write(ipu, reg, IC_PRP_VF_RSC);

	ic_conf = ipu_ic_read(ipu, IC_CONF);

	/* Setup color space conversion */
	in_fmt = format_to_colorspace(params->mem_prp_vf_mem.in_pixel_fmt);
	out_fmt = format_to_colorspace(params->mem_prp_vf_mem.out_pixel_fmt);
	if (in_fmt == RGB) {
		if ((out_fmt == YCbCr) || (out_fmt == YUV)) {
			/* Enable RGB->YCBCR CSC1 */
			_init_csc(ipu, IC_TASK_VIEWFINDER, RGB, out_fmt, 1);
			ic_conf |= IC_CONF_PRPVF_CSC1;
		}
	}
	if ((in_fmt == YCbCr) || (in_fmt == YUV)) {
		if (out_fmt == RGB) {
			/* Enable YCBCR->RGB CSC1 */
			_init_csc(ipu, IC_TASK_VIEWFINDER, YCbCr, RGB, 1);
			ic_conf |= IC_CONF_PRPVF_CSC1;
		} else {
			/* TODO: Support YUV<->YCbCr conversion? */
		}
	}

	if (params->mem_prp_vf_mem.graphics_combine_en) {
		ic_conf |= IC_CONF_PRPVF_CMB;

		if (!(ic_conf & IC_CONF_PRPVF_CSC1)) {
			/* need transparent CSC1 conversion */
			_init_csc(ipu, IC_TASK_VIEWFINDER, RGB, RGB, 1);
			ic_conf |= IC_CONF_PRPVF_CSC1;  /* Enable RGB->RGB CSC */
		}
		in_fmt = format_to_colorspace(params->mem_prp_vf_mem.in_g_pixel_fmt);
		out_fmt = format_to_colorspace(params->mem_prp_vf_mem.out_pixel_fmt);
		if (in_fmt == RGB) {
			if ((out_fmt == YCbCr) || (out_fmt == YUV)) {
				/* Enable RGB->YCBCR CSC2 */
				_init_csc(ipu, IC_TASK_VIEWFINDER, RGB, out_fmt, 2);
				ic_conf |= IC_CONF_PRPVF_CSC2;
			}
		}
		if ((in_fmt == YCbCr) || (in_fmt == YUV)) {
			if (out_fmt == RGB) {
				/* Enable YCBCR->RGB CSC2 */
				_init_csc(ipu, IC_TASK_VIEWFINDER, YCbCr, RGB, 2);
				ic_conf |= IC_CONF_PRPVF_CSC2;
			} else {
				/* TODO: Support YUV<->YCbCr conversion? */
			}
		}

		if (params->mem_prp_vf_mem.global_alpha_en) {
			ic_conf |= IC_CONF_IC_GLB_LOC_A;
			reg = ipu_ic_read(ipu, IC_CMBP_1);
			reg &= ~(0xff);
			reg |= params->mem_prp_vf_mem.alpha;
			ipu_ic_write(ipu, reg, IC_CMBP_1);
		} else
			ic_conf &= ~IC_CONF_IC_GLB_LOC_A;

		if (params->mem_prp_vf_mem.key_color_en) {
			ic_conf |= IC_CONF_KEY_COLOR_EN;
			ipu_ic_write(ipu, params->mem_prp_vf_mem.key_color,
					IC_CMBP_2);
		} else
			ic_conf &= ~IC_CONF_KEY_COLOR_EN;
	} else {
		ic_conf &= ~IC_CONF_PRPVF_CMB;
	}

	if (src_is_csi)
		ic_conf &= ~IC_CONF_RWS_EN;
	else
		ic_conf |= IC_CONF_RWS_EN;

	ipu_ic_write(ipu, ic_conf, IC_CONF);
}

void _ipu_ic_uninit_prpvf(struct ipu_soc *ipu)
{
	uint32_t reg;

	reg = ipu_ic_read(ipu, IC_CONF);
	reg &= ~(IC_CONF_PRPVF_EN | IC_CONF_PRPVF_CMB |
		 IC_CONF_PRPVF_CSC2 | IC_CONF_PRPVF_CSC1);
	ipu_ic_write(ipu, reg, IC_CONF);
}

void _ipu_ic_init_rotate_vf(struct ipu_soc *ipu, ipu_channel_params_t *params)
{
}

void _ipu_ic_uninit_rotate_vf(struct ipu_soc *ipu)
{
	uint32_t reg;
	reg = ipu_ic_read(ipu, IC_CONF);
	reg &= ~IC_CONF_PRPVF_ROT_EN;
	ipu_ic_write(ipu, reg, IC_CONF);
}

void _ipu_ic_init_prpenc(struct ipu_soc *ipu, ipu_channel_params_t *params, bool src_is_csi)
{
	uint32_t reg, ic_conf;
	uint32_t downsizeCoeff, resizeCoeff;
	ipu_color_space_t in_fmt, out_fmt;

	/* Setup vertical resizing */
	if (!(params->mem_prp_enc_mem.outv_resize_ratio)) {
		_calc_resize_coeffs(ipu, params->mem_prp_enc_mem.in_height,
				params->mem_prp_enc_mem.out_height,
				&resizeCoeff, &downsizeCoeff);
		reg = (downsizeCoeff << 30) | (resizeCoeff << 16);
	} else
		reg = (params->mem_prp_enc_mem.outv_resize_ratio) << 16;

	/* Setup horizontal resizing */
	/* Upadeted for IC split case */
	if (!(params->mem_prp_enc_mem.outh_resize_ratio)) {
		_calc_resize_coeffs(ipu, params->mem_prp_enc_mem.in_width,
				params->mem_prp_enc_mem.out_width,
				&resizeCoeff, &downsizeCoeff);
		reg |= (downsizeCoeff << 14) | resizeCoeff;
	} else
		reg |= params->mem_prp_enc_mem.outh_resize_ratio;

	ipu_ic_write(ipu, reg, IC_PRP_ENC_RSC);

	ic_conf = ipu_ic_read(ipu, IC_CONF);

	/* Setup color space conversion */
	in_fmt = format_to_colorspace(params->mem_prp_enc_mem.in_pixel_fmt);
	out_fmt = format_to_colorspace(params->mem_prp_enc_mem.out_pixel_fmt);
	if (in_fmt == RGB) {
		if ((out_fmt == YCbCr) || (out_fmt == YUV)) {
			/* Enable RGB->YCBCR CSC1 */
			_init_csc(ipu, IC_TASK_ENCODER, RGB, out_fmt, 1);
			ic_conf |= IC_CONF_PRPENC_CSC1;
		}
	}
	if ((in_fmt == YCbCr) || (in_fmt == YUV)) {
		if (out_fmt == RGB) {
			/* Enable YCBCR->RGB CSC1 */
			_init_csc(ipu, IC_TASK_ENCODER, YCbCr, RGB, 1);
			ic_conf |= IC_CONF_PRPENC_CSC1;
		} else {
			/* TODO: Support YUV<->YCbCr conversion? */
		}
	}

	if (src_is_csi)
		ic_conf &= ~IC_CONF_RWS_EN;
	else
		ic_conf |= IC_CONF_RWS_EN;

	ipu_ic_write(ipu, ic_conf, IC_CONF);
}

void _ipu_ic_uninit_prpenc(struct ipu_soc *ipu)
{
	uint32_t reg;

	reg = ipu_ic_read(ipu, IC_CONF);
	reg &= ~(IC_CONF_PRPENC_EN | IC_CONF_PRPENC_CSC1);
	ipu_ic_write(ipu, reg, IC_CONF);
}

void _ipu_ic_init_rotate_enc(struct ipu_soc *ipu, ipu_channel_params_t *params)
{
}

void _ipu_ic_uninit_rotate_enc(struct ipu_soc *ipu)
{
	uint32_t reg;

	reg = ipu_ic_read(ipu, IC_CONF);
	reg &= ~(IC_CONF_PRPENC_ROT_EN);
	ipu_ic_write(ipu, reg, IC_CONF);
}

void _ipu_ic_init_pp(struct ipu_soc *ipu, ipu_channel_params_t *params)
{
	uint32_t reg, ic_conf;
	uint32_t downsizeCoeff, resizeCoeff;
	ipu_color_space_t in_fmt, out_fmt;

	/* Setup vertical resizing */
	if (!(params->mem_pp_mem.outv_resize_ratio)) {
		_calc_resize_coeffs(ipu, params->mem_pp_mem.in_height,
			    params->mem_pp_mem.out_height,
			    &resizeCoeff, &downsizeCoeff);
		reg = (downsizeCoeff << 30) | (resizeCoeff << 16);
	} else {
		reg = (params->mem_pp_mem.outv_resize_ratio) << 16;
	}

	/* Setup horizontal resizing */
	/* Upadeted for IC split case */
	if (!(params->mem_pp_mem.outh_resize_ratio)) {
		_calc_resize_coeffs(ipu, params->mem_pp_mem.in_width,
							params->mem_pp_mem.out_width,
							&resizeCoeff, &downsizeCoeff);
		reg |= (downsizeCoeff << 14) | resizeCoeff;
	} else {
		reg |= params->mem_pp_mem.outh_resize_ratio;
	}

	ipu_ic_write(ipu, reg, IC_PP_RSC);

	ic_conf = ipu_ic_read(ipu, IC_CONF);

	/* Setup color space conversion */
	in_fmt = format_to_colorspace(params->mem_pp_mem.in_pixel_fmt);
	out_fmt = format_to_colorspace(params->mem_pp_mem.out_pixel_fmt);
	if (in_fmt == RGB) {
		if ((out_fmt == YCbCr) || (out_fmt == YUV)) {
			/* Enable RGB->YCBCR CSC1 */
			_init_csc(ipu, IC_TASK_POST_PROCESSOR, RGB, out_fmt, 1);
			ic_conf |= IC_CONF_PP_CSC1;
		}
	}
	if ((in_fmt == YCbCr) || (in_fmt == YUV)) {
		if (out_fmt == RGB) {
			/* Enable YCBCR->RGB CSC1 */
			_init_csc(ipu, IC_TASK_POST_PROCESSOR, YCbCr, RGB, 1);
			ic_conf |= IC_CONF_PP_CSC1;
		} else {
			/* TODO: Support YUV<->YCbCr conversion? */
		}
	}

	if (params->mem_pp_mem.graphics_combine_en) {
		ic_conf |= IC_CONF_PP_CMB;

		if (!(ic_conf & IC_CONF_PP_CSC1)) {
			/* need transparent CSC1 conversion */
			_init_csc(ipu, IC_TASK_POST_PROCESSOR, RGB, RGB, 1);
			ic_conf |= IC_CONF_PP_CSC1;  /* Enable RGB->RGB CSC */
		}

		in_fmt = format_to_colorspace(params->mem_pp_mem.in_g_pixel_fmt);
		out_fmt = format_to_colorspace(params->mem_pp_mem.out_pixel_fmt);
		if (in_fmt == RGB) {
			if ((out_fmt == YCbCr) || (out_fmt == YUV)) {
				/* Enable RGB->YCBCR CSC2 */
				_init_csc(ipu, IC_TASK_POST_PROCESSOR, RGB, out_fmt, 2);
				ic_conf |= IC_CONF_PP_CSC2;
			}
		}
		if ((in_fmt == YCbCr) || (in_fmt == YUV)) {
			if (out_fmt == RGB) {
				/* Enable YCBCR->RGB CSC2 */
				_init_csc(ipu, IC_TASK_POST_PROCESSOR, YCbCr, RGB, 2);
				ic_conf |= IC_CONF_PP_CSC2;
			} else {
				/* TODO: Support YUV<->YCbCr conversion? */
			}
		}

		if (params->mem_pp_mem.global_alpha_en) {
			ic_conf |= IC_CONF_IC_GLB_LOC_A;
			reg = ipu_ic_read(ipu, IC_CMBP_1);
			reg &= ~(0xff00);
			reg |= (params->mem_pp_mem.alpha << 8);
			ipu_ic_write(ipu, reg, IC_CMBP_1);
		} else
			ic_conf &= ~IC_CONF_IC_GLB_LOC_A;

		if (params->mem_pp_mem.key_color_en) {
			ic_conf |= IC_CONF_KEY_COLOR_EN;
			ipu_ic_write(ipu, params->mem_pp_mem.key_color,
					IC_CMBP_2);
		} else
			ic_conf &= ~IC_CONF_KEY_COLOR_EN;
	} else {
		ic_conf &= ~IC_CONF_PP_CMB;
	}

	ipu_ic_write(ipu, ic_conf, IC_CONF);
}

void _ipu_ic_uninit_pp(struct ipu_soc *ipu)
{
	uint32_t reg;

	reg = ipu_ic_read(ipu, IC_CONF);
	reg &= ~(IC_CONF_PP_EN | IC_CONF_PP_CSC1 | IC_CONF_PP_CSC2 |
		 IC_CONF_PP_CMB);
	ipu_ic_write(ipu, reg, IC_CONF);
}

void _ipu_ic_init_rotate_pp(struct ipu_soc *ipu, ipu_channel_params_t *params)
{
}

void _ipu_ic_uninit_rotate_pp(struct ipu_soc *ipu)
{
	uint32_t reg;
	reg = ipu_ic_read(ipu, IC_CONF);
	reg &= ~IC_CONF_PP_ROT_EN;
	ipu_ic_write(ipu, reg, IC_CONF);
}

int _ipu_ic_idma_init(struct ipu_soc *ipu, int dma_chan,
		uint16_t width, uint16_t height,
		int burst_size, ipu_rotate_mode_t rot)
{
	u32 ic_idmac_1, ic_idmac_2, ic_idmac_3;
	u32 temp_rot = bitrev8(rot) >> 5;
	bool need_hor_flip = false;

	if ((burst_size != 8) && (burst_size != 16)) {
		dev_dbg(ipu->dev, "Illegal burst length for IC\n");
		return -EINVAL;
	}

	width--;
	height--;

	if (temp_rot & 0x2)	/* Need horizontal flip */
		need_hor_flip = true;

	ic_idmac_1 = ipu_ic_read(ipu, IC_IDMAC_1);
	ic_idmac_2 = ipu_ic_read(ipu, IC_IDMAC_2);
	ic_idmac_3 = ipu_ic_read(ipu, IC_IDMAC_3);
	if (dma_chan == 22) {	/* PP output - CB2 */
		if (burst_size == 16)
			ic_idmac_1 |= IC_IDMAC_1_CB2_BURST_16;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_CB2_BURST_16;

		if (need_hor_flip)
			ic_idmac_1 |= IC_IDMAC_1_PP_FLIP_RS;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_PP_FLIP_RS;

		ic_idmac_2 &= ~IC_IDMAC_2_PP_HEIGHT_MASK;
		ic_idmac_2 |= height << IC_IDMAC_2_PP_HEIGHT_OFFSET;

		ic_idmac_3 &= ~IC_IDMAC_3_PP_WIDTH_MASK;
		ic_idmac_3 |= width << IC_IDMAC_3_PP_WIDTH_OFFSET;
	} else if (dma_chan == 11) {	/* PP Input - CB5 */
		if (burst_size == 16)
			ic_idmac_1 |= IC_IDMAC_1_CB5_BURST_16;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_CB5_BURST_16;
	} else if (dma_chan == 47) {	/* PP Rot input */
		ic_idmac_1 &= ~IC_IDMAC_1_PP_ROT_MASK;
		ic_idmac_1 |= temp_rot << IC_IDMAC_1_PP_ROT_OFFSET;
	}

	if (dma_chan == 12) {	/* PRP Input - CB6 */
		if (burst_size == 16)
			ic_idmac_1 |= IC_IDMAC_1_CB6_BURST_16;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_CB6_BURST_16;
	}

	if (dma_chan == 20) {	/* PRP ENC output - CB0 */
		if (burst_size == 16)
			ic_idmac_1 |= IC_IDMAC_1_CB0_BURST_16;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_CB0_BURST_16;

		if (need_hor_flip)
			ic_idmac_1 |= IC_IDMAC_1_PRPENC_FLIP_RS;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_PRPENC_FLIP_RS;

		ic_idmac_2 &= ~IC_IDMAC_2_PRPENC_HEIGHT_MASK;
		ic_idmac_2 |= height << IC_IDMAC_2_PRPENC_HEIGHT_OFFSET;

		ic_idmac_3 &= ~IC_IDMAC_3_PRPENC_WIDTH_MASK;
		ic_idmac_3 |= width << IC_IDMAC_3_PRPENC_WIDTH_OFFSET;

	} else if (dma_chan == 45) {	/* PRP ENC Rot input */
		ic_idmac_1 &= ~IC_IDMAC_1_PRPENC_ROT_MASK;
		ic_idmac_1 |= temp_rot << IC_IDMAC_1_PRPENC_ROT_OFFSET;
	}

	if (dma_chan == 21) {	/* PRP VF output - CB1 */
		if (burst_size == 16)
			ic_idmac_1 |= IC_IDMAC_1_CB1_BURST_16;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_CB1_BURST_16;

		if (need_hor_flip)
			ic_idmac_1 |= IC_IDMAC_1_PRPVF_FLIP_RS;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_PRPVF_FLIP_RS;

		ic_idmac_2 &= ~IC_IDMAC_2_PRPVF_HEIGHT_MASK;
		ic_idmac_2 |= height << IC_IDMAC_2_PRPVF_HEIGHT_OFFSET;

		ic_idmac_3 &= ~IC_IDMAC_3_PRPVF_WIDTH_MASK;
		ic_idmac_3 |= width << IC_IDMAC_3_PRPVF_WIDTH_OFFSET;

	} else if (dma_chan == 46) {	/* PRP VF Rot input */
		ic_idmac_1 &= ~IC_IDMAC_1_PRPVF_ROT_MASK;
		ic_idmac_1 |= temp_rot << IC_IDMAC_1_PRPVF_ROT_OFFSET;
	}

	if (dma_chan == 14) {	/* PRP VF graphics combining input - CB3 */
		if (burst_size == 16)
			ic_idmac_1 |= IC_IDMAC_1_CB3_BURST_16;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_CB3_BURST_16;
	} else if (dma_chan == 15) {	/* PP graphics combining input - CB4 */
		if (burst_size == 16)
			ic_idmac_1 |= IC_IDMAC_1_CB4_BURST_16;
		else
			ic_idmac_1 &= ~IC_IDMAC_1_CB4_BURST_16;
	}

	ipu_ic_write(ipu, ic_idmac_1, IC_IDMAC_1);
	ipu_ic_write(ipu, ic_idmac_2, IC_IDMAC_2);
	ipu_ic_write(ipu, ic_idmac_3, IC_IDMAC_3);

	return 0;
}

static void _init_csc(struct ipu_soc *ipu, uint8_t ic_task, ipu_color_space_t in_format,
		      ipu_color_space_t out_format, int csc_index)
{

/*     Y = R *  .299 + G *  .587 + B *  .114;
       U = R * -.169 + G * -.332 + B *  .500 + 128.;
       V = R *  .500 + G * -.419 + B * -.0813 + 128.;*/
	static const uint32_t rgb2ycbcr_coeff[4][3] = {
		{0x004D, 0x0096, 0x001D},
		{0x01D5, 0x01AB, 0x0080},
		{0x0080, 0x0195, 0x01EB},
		{0x0000, 0x0200, 0x0200},	/* A0, A1, A2 */
	};

	/* transparent RGB->RGB matrix for combining
	 */
	static const uint32_t rgb2rgb_coeff[4][3] = {
		{0x0080, 0x0000, 0x0000},
		{0x0000, 0x0080, 0x0000},
		{0x0000, 0x0000, 0x0080},
		{0x0000, 0x0000, 0x0000},	/* A0, A1, A2 */
	};

/*     R = (1.164 * (Y - 16)) + (1.596 * (Cr - 128));
       G = (1.164 * (Y - 16)) - (0.392 * (Cb - 128)) - (0.813 * (Cr - 128));
       B = (1.164 * (Y - 16)) + (2.017 * (Cb - 128); */
	static const uint32_t ycbcr2rgb_coeff[4][3] = {
		{149, 0, 204},
		{149, 462, 408},
		{149, 255, 0},
		{8192 - 446, 266, 8192 - 554},	/* A0, A1, A2 */
	};

	uint32_t param;
	uint32_t *base = NULL;

	if (ic_task == IC_TASK_ENCODER) {
		base = ipu->tpmem_base + 0x2008 / 4;
	} else if (ic_task == IC_TASK_VIEWFINDER) {
		if (csc_index == 1)
			base = ipu->tpmem_base + 0x4028 / 4;
		else
			base = ipu->tpmem_base + 0x4040 / 4;
	} else if (ic_task == IC_TASK_POST_PROCESSOR) {
		if (csc_index == 1)
			base = ipu->tpmem_base + 0x6060 / 4;
		else
			base = ipu->tpmem_base + 0x6078 / 4;
	} else {
		BUG();
	}

	if ((in_format == YCbCr) && (out_format == RGB)) {
		/* Init CSC (YCbCr->RGB) */
		param = (ycbcr2rgb_coeff[3][0] << 27) |
			(ycbcr2rgb_coeff[0][0] << 18) |
			(ycbcr2rgb_coeff[1][1] << 9) | ycbcr2rgb_coeff[2][2];
		writel(param, base++);
		/* scale = 2, sat = 0 */
		param = (ycbcr2rgb_coeff[3][0] >> 5) | (2L << (40 - 32));
		writel(param, base++);

		param = (ycbcr2rgb_coeff[3][1] << 27) |
			(ycbcr2rgb_coeff[0][1] << 18) |
			(ycbcr2rgb_coeff[1][0] << 9) | ycbcr2rgb_coeff[2][0];
		writel(param, base++);
		param = (ycbcr2rgb_coeff[3][1] >> 5);
		writel(param, base++);

		param = (ycbcr2rgb_coeff[3][2] << 27) |
			(ycbcr2rgb_coeff[0][2] << 18) |
			(ycbcr2rgb_coeff[1][2] << 9) | ycbcr2rgb_coeff[2][1];
		writel(param, base++);
		param = (ycbcr2rgb_coeff[3][2] >> 5);
		writel(param, base++);
	} else if ((in_format == RGB) && (out_format == YCbCr)) {
		/* Init CSC (RGB->YCbCr) */
		param = (rgb2ycbcr_coeff[3][0] << 27) |
			(rgb2ycbcr_coeff[0][0] << 18) |
			(rgb2ycbcr_coeff[1][1] << 9) | rgb2ycbcr_coeff[2][2];
		writel(param, base++);
		/* scale = 1, sat = 0 */
		param = (rgb2ycbcr_coeff[3][0] >> 5) | (1UL << 8);
		writel(param, base++);

		param = (rgb2ycbcr_coeff[3][1] << 27) |
			(rgb2ycbcr_coeff[0][1] << 18) |
			(rgb2ycbcr_coeff[1][0] << 9) | rgb2ycbcr_coeff[2][0];
		writel(param, base++);
		param = (rgb2ycbcr_coeff[3][1] >> 5);
		writel(param, base++);

		param = (rgb2ycbcr_coeff[3][2] << 27) |
			(rgb2ycbcr_coeff[0][2] << 18) |
			(rgb2ycbcr_coeff[1][2] << 9) | rgb2ycbcr_coeff[2][1];
		writel(param, base++);
		param = (rgb2ycbcr_coeff[3][2] >> 5);
		writel(param, base++);
	} else if ((in_format == RGB) && (out_format == RGB)) {
		/* Init CSC */
		param =
		    (rgb2rgb_coeff[3][0] << 27) | (rgb2rgb_coeff[0][0] << 18) |
		    (rgb2rgb_coeff[1][1] << 9) | rgb2rgb_coeff[2][2];
		writel(param, base++);
		/* scale = 2, sat = 0 */
		param = (rgb2rgb_coeff[3][0] >> 5) | (2UL << 8);
		writel(param, base++);

		param =
		    (rgb2rgb_coeff[3][1] << 27) | (rgb2rgb_coeff[0][1] << 18) |
		    (rgb2rgb_coeff[1][0] << 9) | rgb2rgb_coeff[2][0];
		writel(param, base++);
		param = (rgb2rgb_coeff[3][1] >> 5);
		writel(param, base++);

		param =
		    (rgb2rgb_coeff[3][2] << 27) | (rgb2rgb_coeff[0][2] << 18) |
		    (rgb2rgb_coeff[1][2] << 9) | rgb2rgb_coeff[2][1];
		writel(param, base++);
		param = (rgb2rgb_coeff[3][2] >> 5);
		writel(param, base++);
	} else {
		dev_err(ipu->dev, "Unsupported color space conversion\n");
	}
}

static bool _calc_resize_coeffs(struct ipu_soc *ipu,
				uint32_t inSize, uint32_t outSize,
				uint32_t *resizeCoeff,
				uint32_t *downsizeCoeff)
{
	uint32_t tempSize;
	uint32_t tempDownsize;

	/* Input size cannot be more than 4096 */
	/* Output size cannot be more than 1024 */
	if ((inSize > 4096) || (outSize > 1024))
		return false;

	/* Cannot downsize more than 8:1 */
	if ((outSize << 3) < inSize)
		return false;

	/* Compute downsizing coefficient */
	/* Output of downsizing unit cannot be more than 1024 */
	tempDownsize = 0;
	tempSize = inSize;
	while (((tempSize > 1024) || (tempSize >= outSize * 2)) &&
	       (tempDownsize < 2)) {
		tempSize >>= 1;
		tempDownsize++;
	}
	*downsizeCoeff = tempDownsize;

	/* compute resizing coefficient using the following equation:
	   resizeCoeff = M*(SI -1)/(SO - 1)
	   where M = 2^13, SI - input size, SO - output size    */
	*resizeCoeff = (8192L * (tempSize - 1)) / (outSize - 1);
	if (*resizeCoeff >= 16384L) {
		dev_err(ipu->dev, "Warning! Overflow on resize coeff.\n");
		*resizeCoeff = 0x3FFF;
	}

	dev_dbg(ipu->dev, "resizing from %u -> %u pixels, "
		"downsize=%u, resize=%u.%lu (reg=%u)\n", inSize, outSize,
		*downsizeCoeff, (*resizeCoeff >= 8192L) ? 1 : 0,
		((*resizeCoeff & 0x1FFF) * 10000L) / 8192L, *resizeCoeff);

	return true;
}

void _ipu_vdi_toggle_top_field_man(struct ipu_soc *ipu)
{
	uint32_t reg;
	uint32_t mask_reg;

	reg = ipu_vdi_read(ipu, VDI_C);
	mask_reg = reg & VDI_C_TOP_FIELD_MAN_1;
	if (mask_reg == VDI_C_TOP_FIELD_MAN_1)
		reg &= ~VDI_C_TOP_FIELD_MAN_1;
	else
		reg |= VDI_C_TOP_FIELD_MAN_1;

	ipu_vdi_write(ipu, reg, VDI_C);
}