]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/video/ipu_disp.c
merged tx6dl-devel into denx master branch
[karo-tx-uboot.git] / drivers / video / ipu_disp.c
1 /*
2  * Porting to u-boot:
3  *
4  * (C) Copyright 2010
5  * Stefano Babic, DENX Software Engineering, sbabic@denx.de
6  *
7  * Linux IPU driver
8  *
9  * (C) Copyright 2005-2011 Freescale Semiconductor, Inc.
10  *
11  * SPDX-License-Identifier:     GPL-2.0+
12  */
13
14 /* #define DEBUG */
15
16 #include <common.h>
17 #include <ipu.h>
18 #include <linux/types.h>
19 #include <asm/errno.h>
20 #include <asm/io.h>
21 #include <asm/arch/imx-regs.h>
22 #include <asm/arch/sys_proto.h>
23 #include "ipu_regs.h"
24
25 enum csc_type_t {
26         RGB2YUV = 0,
27         YUV2RGB,
28         RGB2RGB,
29         YUV2YUV,
30         CSC_NONE,
31         CSC_NUM
32 };
33
34 struct dp_csc_param_t {
35         int mode;
36         void *coeff;
37 };
38
39 #define SYNC_WAVE 0
40
41 /* DC display ID assignments */
42 #define DC_DISP_ID_SYNC(di)     (di)
43 #define DC_DISP_ID_SERIAL       2
44 #define DC_DISP_ID_ASYNC        3
45
46 static int dmfc_type_setup;
47 static int dmfc_size_28, dmfc_size_29, dmfc_size_24, dmfc_size_27, dmfc_size_23;
48 static int g_di1_tvout;
49
50 void ipu_dmfc_init(int dmfc_type, int first)
51 {
52         u32 dmfc_wr_chan, dmfc_dp_chan;
53
54         if (first) {
55                 if (dmfc_type_setup > dmfc_type)
56                         dmfc_type = dmfc_type_setup;
57                 else
58                         dmfc_type_setup = dmfc_type;
59
60                 /* disable DMFC-IC channel*/
61                 __raw_writel(0x2, DMFC_IC_CTRL);
62         } else if (dmfc_type_setup >= DMFC_HIGH_RESOLUTION_DC) {
63                 printf("DMFC high resolution has set, will not change\n");
64                 return;
65         } else
66                 dmfc_type_setup = dmfc_type;
67
68         if (dmfc_type == DMFC_HIGH_RESOLUTION_DC) {
69                 /* 1 - segment 0~3;
70                  * 5B - segement 4, 5;
71                  * 5F - segement 6, 7;
72                  * 1C, 2C and 6B, 6F unused;
73                  */
74                 debug("IPU DMFC DC HIGH RES: 1(0~3), 5B(4,5), 5F(6,7)\n");
75                 dmfc_wr_chan = 0x00000088;
76                 dmfc_dp_chan = 0x00009694;
77                 dmfc_size_28 = 256 * 4;
78                 dmfc_size_29 = 0;
79                 dmfc_size_24 = 0;
80                 dmfc_size_27 = 128 * 4;
81                 dmfc_size_23 = 128 * 4;
82         } else if (dmfc_type == DMFC_HIGH_RESOLUTION_DP) {
83                 /* 1 - segment 0, 1;
84                  * 5B - segement 2~5;
85                  * 5F - segement 6,7;
86                  * 1C, 2C and 6B, 6F unused;
87                  */
88                 debug("IPU DMFC DP HIGH RES: 1(0,1), 5B(2~5), 5F(6,7)\n");
89                 dmfc_wr_chan = 0x00000090;
90                 dmfc_dp_chan = 0x0000968a;
91                 dmfc_size_28 = 128 * 4;
92                 dmfc_size_29 = 0;
93                 dmfc_size_24 = 0;
94                 dmfc_size_27 = 128 * 4;
95                 dmfc_size_23 = 256 * 4;
96         } else if (dmfc_type == DMFC_HIGH_RESOLUTION_ONLY_DP) {
97                 /* 5B - segement 0~3;
98                  * 5F - segement 4~7;
99                  * 1, 1C, 2C and 6B, 6F unused;
100                  */
101                 debug("IPU DMFC ONLY-DP HIGH RES: 5B(0~3), 5F(4~7)\n");
102                 dmfc_wr_chan = 0x00000000;
103                 dmfc_dp_chan = 0x00008c88;
104                 dmfc_size_28 = 0;
105                 dmfc_size_29 = 0;
106                 dmfc_size_24 = 0;
107                 dmfc_size_27 = 256 * 4;
108                 dmfc_size_23 = 256 * 4;
109         } else {
110                 /* 1 - segment 0, 1;
111                  * 5B - segement 4, 5;
112                  * 5F - segement 6, 7;
113                  * 1C, 2C and 6B, 6F unused;
114                  */
115                 debug("IPU DMFC NORMAL mode: 1(0~1), 5B(4,5), 5F(6,7)\n");
116                 dmfc_wr_chan = 0x00000090;
117                 dmfc_dp_chan = 0x00009694;
118                 dmfc_size_28 = 128 * 4;
119                 dmfc_size_29 = 0;
120                 dmfc_size_24 = 0;
121                 dmfc_size_27 = 128 * 4;
122                 dmfc_size_23 = 128 * 4;
123         }
124         __raw_writel(dmfc_wr_chan, DMFC_WR_CHAN);
125         __raw_writel(0x202020F6, DMFC_WR_CHAN_DEF);
126         __raw_writel(dmfc_dp_chan, DMFC_DP_CHAN);
127         /* Enable chan 5 watermark set at 5 bursts and clear at 7 bursts */
128         __raw_writel(0x2020F6F6, DMFC_DP_CHAN_DEF);
129 }
130
131 void ipu_dmfc_set_wait4eot(int dma_chan, int width)
132 {
133         u32 dmfc_gen1 = __raw_readl(DMFC_GENERAL1);
134
135         if (width >= HIGH_RESOLUTION_WIDTH) {
136                 if (dma_chan == 23)
137                         ipu_dmfc_init(DMFC_HIGH_RESOLUTION_DP, 0);
138                 else if (dma_chan == 28)
139                         ipu_dmfc_init(DMFC_HIGH_RESOLUTION_DC, 0);
140         }
141
142         if (dma_chan == 23) { /*5B*/
143                 if (dmfc_size_23 / width > 3)
144                         dmfc_gen1 |= 1UL << 20;
145                 else
146                         dmfc_gen1 &= ~(1UL << 20);
147         } else if (dma_chan == 24) { /*6B*/
148                 if (dmfc_size_24 / width > 1)
149                         dmfc_gen1 |= 1UL << 22;
150                 else
151                         dmfc_gen1 &= ~(1UL << 22);
152         } else if (dma_chan == 27) { /*5F*/
153                 if (dmfc_size_27 / width > 2)
154                         dmfc_gen1 |= 1UL << 21;
155                 else
156                         dmfc_gen1 &= ~(1UL << 21);
157         } else if (dma_chan == 28) { /*1*/
158                 if (dmfc_size_28 / width > 2)
159                         dmfc_gen1 |= 1UL << 16;
160                 else
161                         dmfc_gen1 &= ~(1UL << 16);
162         } else if (dma_chan == 29) { /*6F*/
163                 if (dmfc_size_29 / width > 1)
164                         dmfc_gen1 |= 1UL << 23;
165                 else
166                         dmfc_gen1 &= ~(1UL << 23);
167         }
168
169         __raw_writel(dmfc_gen1, DMFC_GENERAL1);
170 }
171
172 static void ipu_di_data_wave_config(int di,
173                                      int wave_gen,
174                                      int access_size, int component_size)
175 {
176         u32 reg;
177         reg = (access_size << DI_DW_GEN_ACCESS_SIZE_OFFSET) |
178             (component_size << DI_DW_GEN_COMPONENT_SIZE_OFFSET);
179         __raw_writel(reg, DI_DW_GEN(di, wave_gen));
180 }
181
182 static void ipu_di_data_pin_config(int di, int wave_gen, int di_pin, int set,
183                                     int up, int down)
184 {
185         u32 reg;
186
187         reg = __raw_readl(DI_DW_GEN(di, wave_gen));
188         reg &= ~(0x3 << (di_pin * 2));
189         reg |= set << (di_pin * 2);
190         __raw_writel(reg, DI_DW_GEN(di, wave_gen));
191
192         __raw_writel((down << 16) | up, DI_DW_SET(di, wave_gen, set));
193 }
194
195 static void ipu_di_sync_config(int di, int wave_gen,
196                                 int run_count, int run_src,
197                                 int offset_count, int offset_src,
198                                 int repeat_count, int cnt_clr_src,
199                                 int cnt_polarity_gen_en,
200                                 int cnt_polarity_clr_src,
201                                 int cnt_polarity_trigger_src,
202                                 int cnt_up, int cnt_down)
203 {
204         u32 reg;
205
206         if ((run_count >= 0x1000) || (offset_count >= 0x1000) ||
207                 (repeat_count >= 0x1000) ||
208                 (cnt_up >= 0x400) || (cnt_down >= 0x400)) {
209                 printf("DI%d counters out of range.\n", di);
210                 return;
211         }
212
213         reg = (run_count << 19) | (++run_src << 16) |
214             (offset_count << 3) | ++offset_src;
215         __raw_writel(reg, DI_SW_GEN0(di, wave_gen));
216         reg = (cnt_polarity_gen_en << 29) | (++cnt_clr_src << 25) |
217             (++cnt_polarity_trigger_src << 12) | (++cnt_polarity_clr_src << 9);
218         reg |= (cnt_down << 16) | cnt_up;
219         if (repeat_count == 0) {
220                 /* Enable auto reload */
221                 reg |= 0x10000000;
222         }
223         __raw_writel(reg, DI_SW_GEN1(di, wave_gen));
224         reg = __raw_readl(DI_STP_REP(di, wave_gen));
225         reg &= ~(0xFFFF << (16 * ((wave_gen - 1) & 0x1)));
226         reg |= repeat_count << (16 * ((wave_gen - 1) & 0x1));
227         __raw_writel(reg, DI_STP_REP(di, wave_gen));
228 }
229
230 static void ipu_dc_map_config(int map, int byte_num, int offset, int mask)
231 {
232         int ptr = map * 3 + byte_num;
233         u32 reg;
234
235         reg = __raw_readl(DC_MAP_CONF_VAL(ptr));
236         reg &= ~(0xFFFF << (16 * (ptr & 0x1)));
237         reg |= ((offset << 8) | mask) << (16 * (ptr & 0x1));
238         __raw_writel(reg, DC_MAP_CONF_VAL(ptr));
239
240         reg = __raw_readl(DC_MAP_CONF_PTR(map));
241         reg &= ~(0x1F << ((16 * (map & 0x1)) + (5 * byte_num)));
242         reg |= ptr << ((16 * (map & 0x1)) + (5 * byte_num));
243         __raw_writel(reg, DC_MAP_CONF_PTR(map));
244 }
245
246 static void ipu_dc_map_clear(int map)
247 {
248         u32 reg = __raw_readl(DC_MAP_CONF_PTR(map));
249         __raw_writel(reg & ~(0xFFFF << (16 * (map & 0x1))),
250                      DC_MAP_CONF_PTR(map));
251 }
252
253 static void ipu_dc_write_tmpl(int word, u32 opcode, u32 operand, int map,
254                                int wave, int glue, int sync)
255 {
256         u32 reg;
257         int stop = 1;
258
259         reg = sync;
260         reg |= (glue << 4);
261         reg |= (++wave << 11);
262         reg |= (++map << 15);
263         reg |= (operand << 20) & 0xFFF00000;
264         __raw_writel(reg, ipu_dc_tmpl_reg + word * 2);
265
266         reg = (operand >> 12);
267         reg |= opcode << 4;
268         reg |= (stop << 9);
269         __raw_writel(reg, ipu_dc_tmpl_reg + word * 2 + 1);
270 }
271
272 static void ipu_dc_link_event(int chan, int event, int addr, int priority)
273 {
274         u32 reg;
275
276         reg = __raw_readl(DC_RL_CH(chan, event));
277         reg &= ~(0xFFFF << (16 * (event & 0x1)));
278         reg |= ((addr << 8) | priority) << (16 * (event & 0x1));
279         __raw_writel(reg, DC_RL_CH(chan, event));
280 }
281
282 /* Y = R *  1.200 + G *  2.343 + B *  .453 + 0.250;
283  * U = R * -.672 + G * -1.328 + B *  2.000 + 512.250.;
284  * V = R *  2.000 + G * -1.672 + B * -.328 + 512.250.;
285  */
286 static const int rgb2ycbcr_coeff[5][3] = {
287         {0x4D, 0x96, 0x1D},
288         {0x3D5, 0x3AB, 0x80},
289         {0x80, 0x395, 0x3EB},
290         {0x0000, 0x0200, 0x0200},       /* B0, B1, B2 */
291         {0x2, 0x2, 0x2},        /* S0, S1, S2 */
292 };
293
294 /* R = (1.164 * (Y - 16)) + (1.596 * (Cr - 128));
295  * G = (1.164 * (Y - 16)) - (0.392 * (Cb - 128)) - (0.813 * (Cr - 128));
296  * B = (1.164 * (Y - 16)) + (2.017 * (Cb - 128);
297  */
298 static const int ycbcr2rgb_coeff[5][3] = {
299         {0x095, 0x000, 0x0CC},
300         {0x095, 0x3CE, 0x398},
301         {0x095, 0x0FF, 0x000},
302         {0x3E42, 0x010A, 0x3DD6},       /*B0,B1,B2 */
303         {0x1, 0x1, 0x1},        /*S0,S1,S2 */
304 };
305
306 #define mask_a(a) ((u32)(a) & 0x3FF)
307 #define mask_b(b) ((u32)(b) & 0x3FFF)
308
309 /* Pls keep S0, S1 and S2 as 0x2 by using this convertion */
310 static int rgb_to_yuv(int n, int red, int green, int blue)
311 {
312         int c;
313         c = red * rgb2ycbcr_coeff[n][0];
314         c += green * rgb2ycbcr_coeff[n][1];
315         c += blue * rgb2ycbcr_coeff[n][2];
316         c /= 16;
317         c += rgb2ycbcr_coeff[3][n] * 4;
318         c += 8;
319         c /= 16;
320         if (c < 0)
321                 c = 0;
322         if (c > 255)
323                 c = 255;
324         return c;
325 }
326
327 /*
328  * Row is for BG:       RGB2YUV YUV2RGB RGB2RGB YUV2YUV CSC_NONE
329  * Column is for FG:    RGB2YUV YUV2RGB RGB2RGB YUV2YUV CSC_NONE
330  */
331 static struct dp_csc_param_t dp_csc_array[CSC_NUM][CSC_NUM] = {
332         {
333                 {DP_COM_CONF_CSC_DEF_BOTH, &rgb2ycbcr_coeff},
334                 {0, 0},
335                 {0, 0},
336                 {DP_COM_CONF_CSC_DEF_BG, &rgb2ycbcr_coeff},
337                 {DP_COM_CONF_CSC_DEF_BG, &rgb2ycbcr_coeff}
338         },
339         {
340                 {0, 0},
341                 {DP_COM_CONF_CSC_DEF_BOTH, &ycbcr2rgb_coeff},
342                 {DP_COM_CONF_CSC_DEF_BG, &ycbcr2rgb_coeff},
343                 {0, 0},
344                 {DP_COM_CONF_CSC_DEF_BG, &ycbcr2rgb_coeff}
345         },
346         {
347                 {0, 0},
348                 {DP_COM_CONF_CSC_DEF_FG, &ycbcr2rgb_coeff},
349                 {0, 0},
350                 {0, 0},
351                 {0, 0}
352         },
353         {
354                 {DP_COM_CONF_CSC_DEF_FG, &rgb2ycbcr_coeff},
355                 {0, 0},
356                 {0, 0},
357                 {0, 0},
358                 {0, 0}
359         },
360         {
361                 {DP_COM_CONF_CSC_DEF_FG, &rgb2ycbcr_coeff},
362                 {DP_COM_CONF_CSC_DEF_FG, &ycbcr2rgb_coeff},
363                 {0, 0},
364                 {0, 0},
365                 {0, 0}
366         }
367 };
368
369 static enum csc_type_t fg_csc_type = CSC_NONE, bg_csc_type = CSC_NONE;
370 static int color_key_4rgb = 1;
371
372 void ipu_dp_csc_setup(int dp, struct dp_csc_param_t dp_csc_param,
373                         unsigned char srm_mode_update)
374 {
375         u32 reg;
376         const int (*coeff)[5][3];
377
378         if (dp_csc_param.mode >= 0) {
379                 reg = __raw_readl(DP_COM_CONF());
380                 reg &= ~DP_COM_CONF_CSC_DEF_MASK;
381                 reg |= dp_csc_param.mode;
382                 __raw_writel(reg, DP_COM_CONF());
383         }
384
385         coeff = dp_csc_param.coeff;
386
387         if (coeff) {
388                 __raw_writel(mask_a((*coeff)[0][0]) |
389                                 (mask_a((*coeff)[0][1]) << 16), DP_CSC_A_0());
390                 __raw_writel(mask_a((*coeff)[0][2]) |
391                                 (mask_a((*coeff)[1][0]) << 16), DP_CSC_A_1());
392                 __raw_writel(mask_a((*coeff)[1][1]) |
393                                 (mask_a((*coeff)[1][2]) << 16), DP_CSC_A_2());
394                 __raw_writel(mask_a((*coeff)[2][0]) |
395                                 (mask_a((*coeff)[2][1]) << 16), DP_CSC_A_3());
396                 __raw_writel(mask_a((*coeff)[2][2]) |
397                                 (mask_b((*coeff)[3][0]) << 16) |
398                                 ((*coeff)[4][0] << 30), DP_CSC_0());
399                 __raw_writel(mask_b((*coeff)[3][1]) | ((*coeff)[4][1] << 14) |
400                                 (mask_b((*coeff)[3][2]) << 16) |
401                                 ((*coeff)[4][2] << 30), DP_CSC_1());
402         }
403
404         if (srm_mode_update) {
405                 reg = __raw_readl(IPU_SRM_PRI2) | 0x8;
406                 __raw_writel(reg, IPU_SRM_PRI2);
407         }
408 }
409
410 int ipu_dp_init(ipu_channel_t channel, uint32_t in_pixel_fmt,
411                  uint32_t out_pixel_fmt)
412 {
413         int in_fmt, out_fmt;
414         int dp;
415         int partial = 0;
416         uint32_t reg;
417
418         if (channel == MEM_FG_SYNC) {
419                 dp = DP_SYNC;
420                 partial = 1;
421         } else if (channel == MEM_BG_SYNC) {
422                 dp = DP_SYNC;
423                 partial = 0;
424         } else if (channel == MEM_BG_ASYNC0) {
425                 dp = DP_ASYNC0;
426                 partial = 0;
427         } else {
428                 return -EINVAL;
429         }
430
431         in_fmt = format_to_colorspace(in_pixel_fmt);
432         out_fmt = format_to_colorspace(out_pixel_fmt);
433
434         if (partial) {
435                 if (in_fmt == RGB) {
436                         if (out_fmt == RGB)
437                                 fg_csc_type = RGB2RGB;
438                         else
439                                 fg_csc_type = RGB2YUV;
440                 } else {
441                         if (out_fmt == RGB)
442                                 fg_csc_type = YUV2RGB;
443                         else
444                                 fg_csc_type = YUV2YUV;
445                 }
446         } else {
447                 if (in_fmt == RGB) {
448                         if (out_fmt == RGB)
449                                 bg_csc_type = RGB2RGB;
450                         else
451                                 bg_csc_type = RGB2YUV;
452                 } else {
453                         if (out_fmt == RGB)
454                                 bg_csc_type = YUV2RGB;
455                         else
456                                 bg_csc_type = YUV2YUV;
457                 }
458         }
459
460         /* Transform color key from rgb to yuv if CSC is enabled */
461         reg = __raw_readl(DP_COM_CONF());
462         if (color_key_4rgb && (reg & DP_COM_CONF_GWCKE) &&
463                 (((fg_csc_type == RGB2YUV) && (bg_csc_type == YUV2YUV)) ||
464                 ((fg_csc_type == YUV2YUV) && (bg_csc_type == RGB2YUV)) ||
465                 ((fg_csc_type == YUV2YUV) && (bg_csc_type == YUV2YUV)) ||
466                 ((fg_csc_type == YUV2RGB) && (bg_csc_type == YUV2RGB)))) {
467                 int red, green, blue;
468                 int y, u, v;
469                 uint32_t color_key = __raw_readl(DP_GRAPH_WIND_CTRL()) &
470                         0xFFFFFFL;
471
472                 debug("_ipu_dp_init color key 0x%x need change to yuv fmt!\n",
473                         color_key);
474
475                 red = (color_key >> 16) & 0xFF;
476                 green = (color_key >> 8) & 0xFF;
477                 blue = color_key & 0xFF;
478
479                 y = rgb_to_yuv(0, red, green, blue);
480                 u = rgb_to_yuv(1, red, green, blue);
481                 v = rgb_to_yuv(2, red, green, blue);
482                 color_key = (y << 16) | (u << 8) | v;
483
484                 reg = __raw_readl(DP_GRAPH_WIND_CTRL()) & 0xFF000000L;
485                 __raw_writel(reg | color_key, DP_GRAPH_WIND_CTRL());
486                 color_key_4rgb = 0;
487
488                 debug("_ipu_dp_init color key change to yuv fmt 0x%x!\n",
489                         color_key);
490         }
491
492         ipu_dp_csc_setup(dp, dp_csc_array[bg_csc_type][fg_csc_type], 1);
493
494         return 0;
495 }
496
497 void ipu_dp_uninit(ipu_channel_t channel)
498 {
499         int dp;
500         int partial = 0;
501
502         if (channel == MEM_FG_SYNC) {
503                 dp = DP_SYNC;
504                 partial = 1;
505         } else if (channel == MEM_BG_SYNC) {
506                 dp = DP_SYNC;
507                 partial = 0;
508         } else if (channel == MEM_BG_ASYNC0) {
509                 dp = DP_ASYNC0;
510                 partial = 0;
511         } else {
512                 return;
513         }
514
515         if (partial)
516                 fg_csc_type = CSC_NONE;
517         else
518                 bg_csc_type = CSC_NONE;
519
520         ipu_dp_csc_setup(dp, dp_csc_array[bg_csc_type][fg_csc_type], 0);
521 }
522
523 void ipu_dc_init(int dc_chan, int di, unsigned char interlaced)
524 {
525         u32 reg = 0;
526
527         if ((dc_chan == 1) || (dc_chan == 5)) {
528                 if (interlaced) {
529                         ipu_dc_link_event(dc_chan, DC_EVT_NL, 0, 3);
530                         ipu_dc_link_event(dc_chan, DC_EVT_EOL, 0, 2);
531                         ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA, 0, 1);
532                 } else {
533                         if (di) {
534                                 ipu_dc_link_event(dc_chan, DC_EVT_NL, 2, 3);
535                                 ipu_dc_link_event(dc_chan, DC_EVT_EOL, 3, 2);
536                                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA,
537                                         4, 1);
538                         } else {
539                                 ipu_dc_link_event(dc_chan, DC_EVT_NL, 5, 3);
540                                 ipu_dc_link_event(dc_chan, DC_EVT_EOL, 6, 2);
541                                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA,
542                                         7, 1);
543                         }
544                 }
545                 ipu_dc_link_event(dc_chan, DC_EVT_NF, 0, 0);
546                 ipu_dc_link_event(dc_chan, DC_EVT_NFIELD, 0, 0);
547                 ipu_dc_link_event(dc_chan, DC_EVT_EOF, 0, 0);
548                 ipu_dc_link_event(dc_chan, DC_EVT_EOFIELD, 0, 0);
549                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_CHAN, 0, 0);
550                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_ADDR, 0, 0);
551
552                 reg = 0x2;
553                 reg |= DC_DISP_ID_SYNC(di) << DC_WR_CH_CONF_PROG_DISP_ID_OFFSET;
554                 reg |= di << 2;
555                 if (interlaced)
556                         reg |= DC_WR_CH_CONF_FIELD_MODE;
557         } else if ((dc_chan == 8) || (dc_chan == 9)) {
558                 /* async channels */
559                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA_W_0, 0x64, 1);
560                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA_W_1, 0x64, 1);
561
562                 reg = 0x3;
563                 reg |= DC_DISP_ID_SERIAL << DC_WR_CH_CONF_PROG_DISP_ID_OFFSET;
564         }
565         __raw_writel(reg, DC_WR_CH_CONF(dc_chan));
566
567         __raw_writel(0x00000000, DC_WR_CH_ADDR(dc_chan));
568
569         __raw_writel(0x00000084, DC_GEN);
570 }
571
572 void ipu_dc_uninit(int dc_chan)
573 {
574         if ((dc_chan == 1) || (dc_chan == 5)) {
575                 ipu_dc_link_event(dc_chan, DC_EVT_NL, 0, 0);
576                 ipu_dc_link_event(dc_chan, DC_EVT_EOL, 0, 0);
577                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA, 0, 0);
578                 ipu_dc_link_event(dc_chan, DC_EVT_NF, 0, 0);
579                 ipu_dc_link_event(dc_chan, DC_EVT_NFIELD, 0, 0);
580                 ipu_dc_link_event(dc_chan, DC_EVT_EOF, 0, 0);
581                 ipu_dc_link_event(dc_chan, DC_EVT_EOFIELD, 0, 0);
582                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_CHAN, 0, 0);
583                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_ADDR, 0, 0);
584         } else if ((dc_chan == 8) || (dc_chan == 9)) {
585                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_ADDR_W_0, 0, 0);
586                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_ADDR_W_1, 0, 0);
587                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_CHAN_W_0, 0, 0);
588                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_CHAN_W_1, 0, 0);
589                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA_W_0, 0, 0);
590                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA_W_1, 0, 0);
591                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_ADDR_R_0, 0, 0);
592                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_ADDR_R_1, 0, 0);
593                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_CHAN_R_0, 0, 0);
594                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_CHAN_R_1, 0, 0);
595                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA_R_0, 0, 0);
596                 ipu_dc_link_event(dc_chan, DC_EVT_NEW_DATA_R_1, 0, 0);
597         }
598 }
599
600 int ipu_chan_is_interlaced(ipu_channel_t channel)
601 {
602         if (channel == MEM_DC_SYNC)
603                 return !!(__raw_readl(DC_WR_CH_CONF_1) &
604                           DC_WR_CH_CONF_FIELD_MODE);
605         else if ((channel == MEM_BG_SYNC) || (channel == MEM_FG_SYNC))
606                 return !!(__raw_readl(DC_WR_CH_CONF_5) &
607                           DC_WR_CH_CONF_FIELD_MODE);
608         return 0;
609 }
610
611 void ipu_dp_dc_enable(ipu_channel_t channel)
612 {
613         int di;
614         uint32_t reg;
615         uint32_t dc_chan;
616
617         if (channel == MEM_FG_SYNC)
618                 dc_chan = 5;
619         if (channel == MEM_DC_SYNC)
620                 dc_chan = 1;
621         else if (channel == MEM_BG_SYNC)
622                 dc_chan = 5;
623         else
624                 return;
625
626         if (channel == MEM_FG_SYNC) {
627                 /* Enable FG channel */
628                 reg = __raw_readl(DP_COM_CONF());
629                 __raw_writel(reg | DP_COM_CONF_FG_EN, DP_COM_CONF());
630
631                 reg = __raw_readl(IPU_SRM_PRI2) | 0x8;
632                 __raw_writel(reg, IPU_SRM_PRI2);
633                 return;
634         }
635
636         di = g_dc_di_assignment[dc_chan];
637
638         /* Make sure other DC sync channel is not assigned same DI */
639         reg = __raw_readl(DC_WR_CH_CONF(6 - dc_chan));
640         if ((di << 2) == (reg & DC_WR_CH_CONF_PROG_DI_ID)) {
641                 reg &= ~DC_WR_CH_CONF_PROG_DI_ID;
642                 reg |= di ? 0 : DC_WR_CH_CONF_PROG_DI_ID;
643                 __raw_writel(reg, DC_WR_CH_CONF(6 - dc_chan));
644         }
645
646         reg = __raw_readl(DC_WR_CH_CONF(dc_chan));
647         reg |= 4 << DC_WR_CH_CONF_PROG_TYPE_OFFSET;
648         __raw_writel(reg, DC_WR_CH_CONF(dc_chan));
649
650         clk_enable(g_pixel_clk[di]);
651 }
652
653 static unsigned char dc_swap;
654
655 void ipu_dp_dc_disable(ipu_channel_t channel, unsigned char swap)
656 {
657         uint32_t reg;
658         uint32_t csc;
659         uint32_t dc_chan = 0;
660         int timeout = 50;
661
662         dc_swap = swap;
663
664         if (channel == MEM_DC_SYNC) {
665                 dc_chan = 1;
666         } else if (channel == MEM_BG_SYNC) {
667                 dc_chan = 5;
668         } else if (channel == MEM_FG_SYNC) {
669                 /* Disable FG channel */
670                 dc_chan = 5;
671
672                 reg = __raw_readl(DP_COM_CONF());
673                 csc = reg & DP_COM_CONF_CSC_DEF_MASK;
674                 if (csc == DP_COM_CONF_CSC_DEF_FG)
675                         reg &= ~DP_COM_CONF_CSC_DEF_MASK;
676
677                 reg &= ~DP_COM_CONF_FG_EN;
678                 __raw_writel(reg, DP_COM_CONF());
679
680                 reg = __raw_readl(IPU_SRM_PRI2) | 0x8;
681                 __raw_writel(reg, IPU_SRM_PRI2);
682
683                 timeout = 50;
684
685                 /*
686                  * Wait for DC triple buffer to empty,
687                  * this check is useful for tv overlay.
688                  */
689                 if (g_dc_di_assignment[dc_chan] == 0)
690                         while ((__raw_readl(DC_STAT) & 0x00000002)
691                                != 0x00000002) {
692                                 udelay(2000);
693                                 timeout -= 2;
694                                 if (timeout <= 0)
695                                         break;
696                         }
697                 else if (g_dc_di_assignment[dc_chan] == 1)
698                         while ((__raw_readl(DC_STAT) & 0x00000020)
699                                != 0x00000020) {
700                                 udelay(2000);
701                                 timeout -= 2;
702                                 if (timeout <= 0)
703                                         break;
704                         }
705                 return;
706         } else {
707                 return;
708         }
709
710         if (dc_swap) {
711                 /* Swap DC channel 1 and 5 settings, and disable old dc chan */
712                 reg = __raw_readl(DC_WR_CH_CONF(dc_chan));
713                 __raw_writel(reg, DC_WR_CH_CONF(6 - dc_chan));
714                 reg &= ~DC_WR_CH_CONF_PROG_TYPE_MASK;
715                 reg ^= DC_WR_CH_CONF_PROG_DI_ID;
716                 __raw_writel(reg, DC_WR_CH_CONF(dc_chan));
717         } else {
718                 timeout = 50;
719
720                 /* Wait for DC triple buffer to empty */
721                 if (g_dc_di_assignment[dc_chan] == 0)
722                         while ((__raw_readl(DC_STAT) & 0x00000002)
723                                 != 0x00000002) {
724                                 udelay(2000);
725                                 timeout -= 2;
726                                 if (timeout <= 0)
727                                         break;
728                         }
729                 else if (g_dc_di_assignment[dc_chan] == 1)
730                         while ((__raw_readl(DC_STAT) & 0x00000020)
731                                 != 0x00000020) {
732                                 udelay(2000);
733                                 timeout -= 2;
734                                 if (timeout <= 0)
735                                         break;
736                         }
737
738                 reg = __raw_readl(DC_WR_CH_CONF(dc_chan));
739                 reg &= ~DC_WR_CH_CONF_PROG_TYPE_MASK;
740                 __raw_writel(reg, DC_WR_CH_CONF(dc_chan));
741
742                 reg = __raw_readl(IPU_DISP_GEN);
743                 if (g_dc_di_assignment[dc_chan])
744                         reg &= ~DI1_COUNTER_RELEASE;
745                 else
746                         reg &= ~DI0_COUNTER_RELEASE;
747                 __raw_writel(reg, IPU_DISP_GEN);
748
749                 /* Clock is already off because it must be done quickly, but
750                    we need to fix the ref count */
751                 clk_disable(g_pixel_clk[g_dc_di_assignment[dc_chan]]);
752         }
753 }
754
755 void ipu_init_dc_mappings(void)
756 {
757         /* IPU_PIX_FMT_RGB24 */
758         ipu_dc_map_clear(0);
759         ipu_dc_map_config(0, 0, 7, 0xFF);
760         ipu_dc_map_config(0, 1, 15, 0xFF);
761         ipu_dc_map_config(0, 2, 23, 0xFF);
762
763         /* IPU_PIX_FMT_RGB666 */
764         ipu_dc_map_clear(1);
765         ipu_dc_map_config(1, 0, 5, 0xFC);
766         ipu_dc_map_config(1, 1, 11, 0xFC);
767         ipu_dc_map_config(1, 2, 17, 0xFC);
768
769         /* IPU_PIX_FMT_YUV444 */
770         ipu_dc_map_clear(2);
771         ipu_dc_map_config(2, 0, 15, 0xFF);
772         ipu_dc_map_config(2, 1, 23, 0xFF);
773         ipu_dc_map_config(2, 2, 7, 0xFF);
774
775         /* IPU_PIX_FMT_RGB565 */
776         ipu_dc_map_clear(3);
777         ipu_dc_map_config(3, 0, 4, 0xF8);
778         ipu_dc_map_config(3, 1, 10, 0xFC);
779         ipu_dc_map_config(3, 2, 15, 0xF8);
780
781         /* IPU_PIX_FMT_LVDS666 */
782         ipu_dc_map_clear(4);
783         ipu_dc_map_config(4, 0, 5, 0xFC);
784         ipu_dc_map_config(4, 1, 11, 0xFC);
785         ipu_dc_map_config(4, 2, 17, 0xFC);
786 }
787
788 int ipu_pixfmt_to_map(uint32_t fmt)
789 {
790         switch (fmt) {
791         case IPU_PIX_FMT_GENERIC:
792         case IPU_PIX_FMT_RGB24:
793         case IPU_PIX_FMT_LVDS888:
794                 return 0;
795         case IPU_PIX_FMT_RGB666:
796                 return 1;
797         case IPU_PIX_FMT_YUV444:
798                 return 2;
799         case IPU_PIX_FMT_RGB565:
800                 return 3;
801         case IPU_PIX_FMT_LVDS666:
802                 return 4;
803         }
804
805         return -1;
806 }
807
808 /*
809  * This function is called to adapt synchronous LCD panel to IPU restriction.
810  */
811 void adapt_panel_to_ipu_restricitions(uint32_t *pixel_clk,
812                                       uint16_t width, uint16_t height,
813                                       uint16_t h_start_width,
814                                       uint16_t h_end_width,
815                                       uint16_t v_start_width,
816                                       uint16_t *v_end_width)
817 {
818         if (*v_end_width < 2) {
819                 uint16_t total_width = width + h_start_width + h_end_width;
820                 uint16_t total_height_old = height + v_start_width +
821                         (*v_end_width);
822                 uint16_t total_height_new = height + v_start_width + 2;
823                 *v_end_width = 2;
824                 *pixel_clk = (*pixel_clk) * total_width * total_height_new /
825                         (total_width * total_height_old);
826                 printf("WARNING: adapt panel end blank lines\n");
827         }
828 }
829
830 /*
831  * This function is called to initialize a synchronous LCD panel.
832  *
833  * @param       disp            The DI the panel is attached to.
834  *
835  * @param       pixel_clk       Desired pixel clock frequency in Hz.
836  *
837  * @param       pixel_fmt       Input parameter for pixel format of buffer.
838  *                              Pixel format is a FOURCC ASCII code.
839  *
840  * @param       width           The width of panel in pixels.
841  *
842  * @param       height          The height of panel in pixels.
843  *
844  * @param       hStartWidth     The number of pixel clocks between the HSYNC
845  *                              signal pulse and the start of valid data.
846  *
847  * @param       hSyncWidth      The width of the HSYNC signal in units of pixel
848  *                              clocks.
849  *
850  * @param       hEndWidth       The number of pixel clocks between the end of
851  *                              valid data and the HSYNC signal for next line.
852  *
853  * @param       vStartWidth     The number of lines between the VSYNC
854  *                              signal pulse and the start of valid data.
855  *
856  * @param       vSyncWidth      The width of the VSYNC signal in units of lines
857  *
858  * @param       vEndWidth       The number of lines between the end of valid
859  *                              data and the VSYNC signal for next frame.
860  *
861  * @param       sig             Bitfield of signal polarities for LCD interface.
862  *
863  * @return      This function returns 0 on success or negative error code on
864  *              fail.
865  */
866
867 int ipu_init_sync_panel(int disp, uint32_t pixel_clk,
868                         uint16_t width, uint16_t height,
869                         uint32_t pixel_fmt,
870                         uint16_t h_start_width, uint16_t h_sync_width,
871                         uint16_t h_end_width, uint16_t v_start_width,
872                         uint16_t v_sync_width, uint16_t v_end_width,
873                         uint32_t v_to_h_sync, ipu_di_signal_cfg_t sig)
874 {
875         uint32_t reg;
876         uint32_t di_gen, vsync_cnt;
877         uint32_t div, rounded_pixel_clk;
878         uint32_t h_total, v_total;
879         int map;
880         struct clk *di_parent;
881
882         debug("panel size = %d x %d\n", width, height);
883
884         if ((v_sync_width == 0) || (h_sync_width == 0))
885                 return EINVAL;
886
887         adapt_panel_to_ipu_restricitions(&pixel_clk, width, height,
888                                          h_start_width, h_end_width,
889                                          v_start_width, &v_end_width);
890         h_total = width + h_sync_width + h_start_width + h_end_width;
891         v_total = height + v_sync_width + v_start_width + v_end_width;
892
893         /* Init clocking */
894         debug("pixel clk = %d\n", pixel_clk);
895
896         if (sig.ext_clk) {
897                 if (!(g_di1_tvout && (disp == 1))) { /* don't round div for tvout */
898                         /*
899                          * Set the  PLL to be an even multiple
900                          * of the pixel clock.
901                          */
902                         if ((clk_get_usecount(g_pixel_clk[0]) == 0) &&
903                                 (clk_get_usecount(g_pixel_clk[1]) == 0)) {
904                                 di_parent = clk_get_parent(g_di_clk[disp]);
905                                 rounded_pixel_clk =
906                                         clk_round_rate(g_pixel_clk[disp],
907                                                 pixel_clk);
908                                 if (di_parent != NULL) {
909                                         div  = clk_get_rate(di_parent) /
910                                                 rounded_pixel_clk;
911                                         if (div % 2)
912                                                 div++;
913                                         if (clk_get_rate(di_parent) != div *
914                                                 rounded_pixel_clk)
915                                                 clk_set_rate(di_parent,
916                                                         div * rounded_pixel_clk);
917                                         udelay(10000);
918                                         clk_set_rate(g_di_clk[disp],
919                                                 2 * rounded_pixel_clk);
920                                         udelay(10000);
921                                 }
922                         }
923                 }
924                 clk_set_parent(g_pixel_clk[disp], g_ldb_clk);
925         } else {
926                 if (clk_get_usecount(g_pixel_clk[disp]) != 0)
927                         clk_set_parent(g_pixel_clk[disp], g_ipu_clk);
928         }
929         rounded_pixel_clk = clk_round_rate(g_pixel_clk[disp], pixel_clk);
930         clk_set_rate(g_pixel_clk[disp], rounded_pixel_clk);
931         udelay(5000);
932         /* Get integer portion of divider */
933         div = clk_get_rate(clk_get_parent(g_pixel_clk[disp])) /
934                 rounded_pixel_clk;
935
936         /* Enable for a divide by 2 clock change. */
937         reg = __raw_readl(IPU_PM);
938         reg &= ~(0x7f << 7);
939         reg |= 0x20 << 7;
940         reg &= ~(0x7f << 23);
941         reg |= 0x20 << 23;
942         __raw_writel(reg, IPU_PM);
943
944         di_gen = 0;
945
946         if (pixel_fmt != IPU_PIX_FMT_LVDS666 &&
947                         pixel_fmt != IPU_PIX_FMT_LVDS888) {
948                 clk_set_rate(g_pixel_clk[disp], rounded_pixel_clk);
949                 udelay(5000);
950                 /* Get integer portion of divider */
951                 div = clk_get_rate(clk_get_parent(g_pixel_clk[disp])) /
952                         rounded_pixel_clk;
953                 ipu_di_data_wave_config(disp, SYNC_WAVE, div - 1, div - 1);
954         } else {
955                 clk_set_rate(g_pixel_clk[disp], clk_get_rate(g_ipu_clk));
956                 div = 1;
957                 ipu_di_data_wave_config(disp, SYNC_WAVE, 0, 0);
958                 di_gen |= (6 << 24);
959                 di_gen |= DI_GEN_DI_CLK_EXT;
960         }
961         ipu_di_data_pin_config(disp, SYNC_WAVE, DI_PIN15, 3, 0, div * 2);
962
963         map = ipu_pixfmt_to_map(pixel_fmt);
964         if (map < 0) {
965                 debug("IPU_DISP: No MAP\n");
966                 return -EINVAL;
967         }
968
969         if (sig.interlaced) {
970                 /* Setup internal HSYNC waveform */
971                 ipu_di_sync_config(
972                                 disp,           /* display */
973                                 1,              /* counter */
974                                 h_total / 2 - 1,/* run count */
975                                 DI_SYNC_CLK,    /* run_resolution */
976                                 0,              /* offset */
977                                 DI_SYNC_NONE,   /* offset resolution */
978                                 0,              /* repeat count */
979                                 DI_SYNC_NONE,   /* CNT_CLR_SEL */
980                                 0,              /* CNT_POLARITY_GEN_EN */
981                                 DI_SYNC_NONE,   /* CNT_POLARITY_CLR_SEL */
982                                 DI_SYNC_NONE,   /* CNT_POLARITY_TRIGGER_SEL */
983                                 0,              /* COUNT UP */
984                                 0               /* COUNT DOWN */
985                                 );
986
987                 /* Field 1 VSYNC waveform */
988                 ipu_di_sync_config(
989                                 disp,           /* display */
990                                 2,              /* counter */
991                                 h_total - 1,    /* run count */
992                                 DI_SYNC_CLK,    /* run_resolution */
993                                 0,              /* offset */
994                                 DI_SYNC_NONE,   /* offset resolution */
995                                 0,              /* repeat count */
996                                 DI_SYNC_NONE,   /* CNT_CLR_SEL */
997                                 0,              /* CNT_POLARITY_GEN_EN */
998                                 DI_SYNC_NONE,   /* CNT_POLARITY_CLR_SEL */
999                                 DI_SYNC_NONE,   /* CNT_POLARITY_TRIGGER_SEL */
1000                                 0,              /* COUNT UP */
1001                                 4               /* COUNT DOWN */
1002                                 );
1003
1004                 /* Setup internal HSYNC waveform */
1005                 ipu_di_sync_config(
1006                                 disp,           /* display */
1007                                 3,              /* counter */
1008                                 v_total * 2 - 1,/* run count */
1009                                 DI_SYNC_INT_HSYNC,      /* run_resolution */
1010                                 1,              /* offset */
1011                                 DI_SYNC_INT_HSYNC,      /* offset resolution */
1012                                 0,              /* repeat count */
1013                                 DI_SYNC_NONE,   /* CNT_CLR_SEL */
1014                                 0,              /* CNT_POLARITY_GEN_EN */
1015                                 DI_SYNC_NONE,   /* CNT_POLARITY_CLR_SEL */
1016                                 DI_SYNC_NONE,   /* CNT_POLARITY_TRIGGER_SEL */
1017                                 0,              /* COUNT UP */
1018                                 4               /* COUNT DOWN */
1019                                 );
1020
1021                 /* Active Field ? */
1022                 ipu_di_sync_config(
1023                                 disp,           /* display */
1024                                 4,              /* counter */
1025                                 v_total / 2 - 1,/* run count */
1026                                 DI_SYNC_HSYNC,  /* run_resolution */
1027                                 v_start_width,  /*  offset */
1028                                 DI_SYNC_HSYNC,  /* offset resolution */
1029                                 2,              /* repeat count */
1030                                 DI_SYNC_VSYNC,  /* CNT_CLR_SEL */
1031                                 0,              /* CNT_POLARITY_GEN_EN */
1032                                 DI_SYNC_NONE,   /* CNT_POLARITY_CLR_SEL */
1033                                 DI_SYNC_NONE,   /* CNT_POLARITY_TRIGGER_SEL */
1034                                 0,              /* COUNT UP */
1035                                 0               /* COUNT DOWN */
1036                                 );
1037
1038                 /* Active Line */
1039                 ipu_di_sync_config(
1040                                 disp,           /* display */
1041                                 5,              /* counter */
1042                                 0,              /* run count */
1043                                 DI_SYNC_HSYNC,  /* run_resolution */
1044                                 0,              /*  offset */
1045                                 DI_SYNC_NONE,   /* offset resolution */
1046                                 height / 2,     /* repeat count */
1047                                 4,              /* CNT_CLR_SEL */
1048                                 0,              /* CNT_POLARITY_GEN_EN */
1049                                 DI_SYNC_NONE,   /* CNT_POLARITY_CLR_SEL */
1050                                 DI_SYNC_NONE,   /* CNT_POLARITY_TRIGGER_SEL */
1051                                 0,              /* COUNT UP */
1052                                 0               /* COUNT DOWN */
1053                                 );
1054
1055                 /* Field 0 VSYNC waveform */
1056                 ipu_di_sync_config(
1057                                 disp,           /* display */
1058                                 6,              /* counter */
1059                                 v_total - 1,    /* run count */
1060                                 DI_SYNC_HSYNC,  /* run_resolution */
1061                                 0,              /* offset */
1062                                 DI_SYNC_NONE,   /* offset resolution */
1063                                 0,              /* repeat count */
1064                                 DI_SYNC_NONE,   /* CNT_CLR_SEL  */
1065                                 0,              /* CNT_POLARITY_GEN_EN */
1066                                 DI_SYNC_NONE,   /* CNT_POLARITY_CLR_SEL */
1067                                 DI_SYNC_NONE,   /* CNT_POLARITY_TRIGGER_SEL */
1068                                 0,              /* COUNT UP */
1069                                 0               /* COUNT DOWN */
1070                                 );
1071
1072                 /* DC VSYNC waveform */
1073                 vsync_cnt = 7;
1074                 ipu_di_sync_config(
1075                                 disp,           /* display */
1076                                 7,              /* counter */
1077                                 v_total / 2 - 1,/* run count */
1078                                 DI_SYNC_HSYNC,  /* run_resolution  */
1079                                 9,              /* offset  */
1080                                 DI_SYNC_HSYNC,  /* offset resolution */
1081                                 2,              /* repeat count */
1082                                 DI_SYNC_VSYNC,  /* CNT_CLR_SEL */
1083                                 0,              /* CNT_POLARITY_GEN_EN */
1084                                 DI_SYNC_NONE,   /* CNT_POLARITY_CLR_SEL */
1085                                 DI_SYNC_NONE,   /* CNT_POLARITY_TRIGGER_SEL */
1086                                 0,              /* COUNT UP */
1087                                 0               /* COUNT DOWN */
1088                                 );
1089
1090                 /* active pixel waveform */
1091                 ipu_di_sync_config(
1092                                 disp,           /* display */
1093                                 8,              /* counter */
1094                                 0,              /* run count  */
1095                                 DI_SYNC_CLK,    /* run_resolution */
1096                                 h_start_width,  /* offset  */
1097                                 DI_SYNC_CLK,    /* offset resolution */
1098                                 width,          /* repeat count  */
1099                                 5,              /* CNT_CLR_SEL  */
1100                                 0,              /* CNT_POLARITY_GEN_EN  */
1101                                 DI_SYNC_NONE,   /* CNT_POLARITY_CLR_SEL */
1102                                 DI_SYNC_NONE,   /* CNT_POLARITY_TRIGGER_SEL  */
1103                                 0,              /* COUNT UP  */
1104                                 0               /* COUNT DOWN */
1105                                 );
1106
1107                 ipu_di_sync_config(
1108                                 disp,           /* display */
1109                                 9,              /* counter */
1110                                 v_total - 1,    /* run count */
1111                                 DI_SYNC_INT_HSYNC,/* run_resolution */
1112                                 v_total / 2,    /* offset  */
1113                                 DI_SYNC_INT_HSYNC,/* offset resolution  */
1114                                 0,              /* repeat count */
1115                                 DI_SYNC_HSYNC,  /* CNT_CLR_SEL */
1116                                 0,              /* CNT_POLARITY_GEN_EN  */
1117                                 DI_SYNC_NONE,   /* CNT_POLARITY_CLR_SEL  */
1118                                 DI_SYNC_NONE,   /* CNT_POLARITY_TRIGGER_SEL */
1119                                 0,              /* COUNT UP */
1120                                 4               /* COUNT DOWN */
1121                                 );
1122
1123                 /* set gentime select and tag sel */
1124                 reg = __raw_readl(DI_SW_GEN1(disp, 9));
1125                 reg &= 0x1FFFFFFF;
1126                 reg |= ((3 - 1) << 29) | 0x00008000;
1127                 __raw_writel(reg, DI_SW_GEN1(disp, 9));
1128
1129                 __raw_writel(v_total / 2 - 1, DI_SCR_CONF(disp));
1130
1131                 /* set y_sel = 1 */
1132                 di_gen |= 0x10000000;
1133                 di_gen |= DI_GEN_POLARITY_5;
1134                 di_gen |= DI_GEN_POLARITY_8;
1135         } else {
1136                 /* Setup internal HSYNC waveform */
1137                 ipu_di_sync_config(disp, 1, h_total - 1, DI_SYNC_CLK,
1138                                 0, DI_SYNC_NONE, 0, DI_SYNC_NONE,
1139                                 0, DI_SYNC_NONE,
1140                                 DI_SYNC_NONE, 0, 0);
1141
1142                 /* Setup external (delayed) HSYNC waveform */
1143                 ipu_di_sync_config(disp, DI_SYNC_HSYNC, h_total - 1,
1144                                 DI_SYNC_CLK, div * v_to_h_sync, DI_SYNC_CLK,
1145                                 0, DI_SYNC_NONE, 1, DI_SYNC_NONE,
1146                                 DI_SYNC_CLK, 0, h_sync_width * 2);
1147                 /* Setup VSYNC waveform */
1148                 vsync_cnt = DI_SYNC_VSYNC;
1149                 ipu_di_sync_config(disp, DI_SYNC_VSYNC, v_total - 1,
1150                                 DI_SYNC_INT_HSYNC, 0, DI_SYNC_NONE, 0,
1151                                 DI_SYNC_NONE, 1, DI_SYNC_NONE,
1152                                 DI_SYNC_INT_HSYNC, 0, v_sync_width * 2);
1153                 __raw_writel(v_total - 1, DI_SCR_CONF(disp));
1154
1155                 /* Setup active data waveform to sync with DC */
1156                 ipu_di_sync_config(disp, 4, 0, DI_SYNC_HSYNC,
1157                                 v_sync_width + v_start_width, DI_SYNC_HSYNC,
1158                                 height,
1159                                 DI_SYNC_VSYNC, 0, DI_SYNC_NONE,
1160                                 DI_SYNC_NONE, 0, 0);
1161                 ipu_di_sync_config(disp, 5, 0, DI_SYNC_CLK,
1162                                 h_sync_width + h_start_width, DI_SYNC_CLK,
1163                                 width, 4, 0, DI_SYNC_NONE, DI_SYNC_NONE, 0,
1164                                 0);
1165
1166                 /* reset all unused counters */
1167                 __raw_writel(0, DI_SW_GEN0(disp, 6));
1168                 __raw_writel(0, DI_SW_GEN1(disp, 6));
1169                 __raw_writel(0, DI_SW_GEN0(disp, 7));
1170                 __raw_writel(0, DI_SW_GEN1(disp, 7));
1171                 __raw_writel(0, DI_SW_GEN0(disp, 8));
1172                 __raw_writel(0, DI_SW_GEN1(disp, 8));
1173                 __raw_writel(0, DI_SW_GEN0(disp, 9));
1174                 __raw_writel(0, DI_SW_GEN1(disp, 9));
1175
1176                 reg = __raw_readl(DI_STP_REP(disp, 6));
1177                 reg &= 0x0000FFFF;
1178                 __raw_writel(reg, DI_STP_REP(disp, 6));
1179                 __raw_writel(0, DI_STP_REP(disp, 7));
1180                 __raw_writel(0, DI_STP_REP(disp, 9));
1181
1182                 h_total = ((width + h_start_width + h_sync_width) / 2) - 2;
1183                 ipu_di_sync_config(disp, 6, 1, 0, 2, DI_SYNC_CLK, h_total,
1184                                 DI_SYNC_INT_HSYNC, 0, DI_SYNC_NONE,
1185                                 DI_SYNC_NONE, 0, 0);
1186
1187                 /* Init template microcode */
1188                 if (disp) {
1189                         ipu_dc_write_tmpl(2, WROD(0), 0, map, SYNC_WAVE, 8, 5);
1190                         ipu_dc_write_tmpl(3, WROD(0), 0, map, SYNC_WAVE, 4, 5);
1191                         ipu_dc_write_tmpl(4, WROD(0), 0, map, SYNC_WAVE, 0, 5);
1192                 } else {
1193                         ipu_dc_write_tmpl(5, WROD(0), 0, map, SYNC_WAVE, 8, 5);
1194                         ipu_dc_write_tmpl(6, WROD(0), 0, map, SYNC_WAVE, 4, 5);
1195                         ipu_dc_write_tmpl(7, WROD(0), 0, map, SYNC_WAVE, 0, 5);
1196                 }
1197
1198                 if (sig.Hsync_pol)
1199                         di_gen |= DI_GEN_POLARITY_2;
1200                 if (sig.Vsync_pol)
1201                         di_gen |= DI_GEN_POLARITY_3;
1202
1203                 if (sig.clk_pol)
1204                         di_gen |= DI_GEN_POL_CLK;
1205
1206                 /* Set the clock to stop at counter 6. */
1207                 di_gen |= 0x6000000;
1208         }
1209
1210         __raw_writel(di_gen, DI_GENERAL(disp));
1211
1212         if (sig.interlaced)
1213                 __raw_writel((--vsync_cnt << DI_VSYNC_SEL_OFFSET) |
1214                                 0x00000002, DI_SYNC_AS_GEN(disp));
1215         else
1216                 __raw_writel((--vsync_cnt << DI_VSYNC_SEL_OFFSET),
1217                                 DI_SYNC_AS_GEN(disp));
1218
1219         reg = __raw_readl(DI_POL(disp));
1220         reg &= ~(DI_POL_DRDY_DATA_POLARITY | DI_POL_DRDY_POLARITY_15);
1221         if (sig.enable_pol)
1222                 reg |= DI_POL_DRDY_POLARITY_15;
1223         if (sig.data_pol)
1224                 reg |= DI_POL_DRDY_DATA_POLARITY;
1225         __raw_writel(reg, DI_POL(disp));
1226
1227         __raw_writel(width, DC_DISP_CONF2(DC_DISP_ID_SYNC(disp)));
1228
1229         return 0;
1230 }
1231
1232 /*
1233  * This function sets the foreground and background plane global alpha blending
1234  * modes. This function also sets the DP graphic plane according to the
1235  * parameter of IPUv3 DP channel.
1236  *
1237  * @param       channel         IPUv3 DP channel
1238  *
1239  * @param       enable          Boolean to enable or disable global alpha
1240  *                              blending. If disabled, local blending is used.
1241  *
1242  * @param       alpha           Global alpha value.
1243  *
1244  * @return      Returns 0 on success or negative error code on fail
1245  */
1246 int ipu_disp_set_global_alpha(ipu_channel_t channel, unsigned char enable,
1247                                   uint8_t alpha)
1248 {
1249         int ret;
1250         uint32_t reg;
1251
1252         unsigned char bg_chan;
1253
1254         if (!((channel == MEM_BG_SYNC || channel == MEM_FG_SYNC) ||
1255                 (channel == MEM_BG_ASYNC0 || channel == MEM_FG_ASYNC0) ||
1256                 (channel == MEM_BG_ASYNC1 || channel == MEM_FG_ASYNC1)))
1257                 return -EINVAL;
1258
1259         if (channel == MEM_BG_SYNC || channel == MEM_BG_ASYNC0 ||
1260             channel == MEM_BG_ASYNC1)
1261                 bg_chan = 1;
1262         else
1263                 bg_chan = 0;
1264
1265         ret = clk_enable(g_ipu_clk);
1266         if (ret)
1267                 return ret;
1268
1269         if (bg_chan) {
1270                 reg = __raw_readl(DP_COM_CONF());
1271                 __raw_writel(reg & ~DP_COM_CONF_GWSEL, DP_COM_CONF());
1272         } else {
1273                 reg = __raw_readl(DP_COM_CONF());
1274                 __raw_writel(reg | DP_COM_CONF_GWSEL, DP_COM_CONF());
1275         }
1276
1277         if (enable) {
1278                 reg = __raw_readl(DP_GRAPH_WIND_CTRL()) & 0x00FFFFFFL;
1279                 __raw_writel(reg | ((uint32_t) alpha << 24),
1280                              DP_GRAPH_WIND_CTRL());
1281
1282                 reg = __raw_readl(DP_COM_CONF());
1283                 __raw_writel(reg | DP_COM_CONF_GWAM, DP_COM_CONF());
1284         } else {
1285                 reg = __raw_readl(DP_COM_CONF());
1286                 __raw_writel(reg & ~DP_COM_CONF_GWAM, DP_COM_CONF());
1287         }
1288
1289         reg = __raw_readl(IPU_SRM_PRI2) | 0x8;
1290         __raw_writel(reg, IPU_SRM_PRI2);
1291
1292         clk_disable(g_ipu_clk);
1293
1294         return 0;
1295 }
1296
1297 /*
1298  * This function sets the transparent color key for SDC graphic plane.
1299  *
1300  * @param       channel         Input parameter for the logical channel ID.
1301  *
1302  * @param       enable          Boolean to enable or disable color key
1303  *
1304  * @param       colorKey        24-bit RGB color for transparent color key.
1305  *
1306  * @return      Returns 0 on success or negative error code on fail
1307  */
1308 int ipu_disp_set_color_key(ipu_channel_t channel, unsigned char enable,
1309                                uint32_t color_key)
1310 {
1311         int ret;
1312         uint32_t reg;
1313         int y, u, v;
1314         int red, green, blue;
1315
1316         if (!((channel == MEM_BG_SYNC || channel == MEM_FG_SYNC) ||
1317                 (channel == MEM_BG_ASYNC0 || channel == MEM_FG_ASYNC0) ||
1318                 (channel == MEM_BG_ASYNC1 || channel == MEM_FG_ASYNC1)))
1319                 return -EINVAL;
1320
1321         ret = clk_enable(g_ipu_clk);
1322         if (ret)
1323                 return ret;
1324
1325         color_key_4rgb = 1;
1326         /* Transform color key from rgb to yuv if CSC is enabled */
1327         if (((fg_csc_type == RGB2YUV) && (bg_csc_type == YUV2YUV)) ||
1328                 ((fg_csc_type == YUV2YUV) && (bg_csc_type == RGB2YUV)) ||
1329                 ((fg_csc_type == YUV2YUV) && (bg_csc_type == YUV2YUV)) ||
1330                 ((fg_csc_type == YUV2RGB) && (bg_csc_type == YUV2RGB))) {
1331
1332                 debug("color key 0x%x need change to yuv fmt\n", color_key);
1333
1334                 red = (color_key >> 16) & 0xFF;
1335                 green = (color_key >> 8) & 0xFF;
1336                 blue = color_key & 0xFF;
1337
1338                 y = rgb_to_yuv(0, red, green, blue);
1339                 u = rgb_to_yuv(1, red, green, blue);
1340                 v = rgb_to_yuv(2, red, green, blue);
1341                 color_key = (y << 16) | (u << 8) | v;
1342
1343                 color_key_4rgb = 0;
1344
1345                 debug("color key change to yuv fmt 0x%x\n", color_key);
1346         }
1347
1348         if (enable) {
1349                 reg = __raw_readl(DP_GRAPH_WIND_CTRL()) & 0xFF000000L;
1350                 __raw_writel(reg | color_key, DP_GRAPH_WIND_CTRL());
1351
1352                 reg = __raw_readl(DP_COM_CONF());
1353                 __raw_writel(reg | DP_COM_CONF_GWCKE, DP_COM_CONF());
1354         } else {
1355                 reg = __raw_readl(DP_COM_CONF());
1356                 __raw_writel(reg & ~DP_COM_CONF_GWCKE, DP_COM_CONF());
1357         }
1358
1359         reg = __raw_readl(IPU_SRM_PRI2) | 0x8;
1360         __raw_writel(reg, IPU_SRM_PRI2);
1361
1362         clk_disable(g_ipu_clk);
1363
1364         return 0;
1365 }