a05b3632e8f88621192b6249b92f694b957ca5a0
[firefly-linux-kernel-4.4.55.git] / drivers / video / rk29_fb.c
1 /*
2  * drivers/video/rk29_fb.c
3  *
4  * Copyright (C) 2010 ROCKCHIP, Inc.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/mm.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/fb.h>
25 #include <linux/init.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/interrupt.h>
28 #include <linux/platform_device.h>
29 #include <linux/clk.h>
30 #include <linux/backlight.h>
31 #include <linux/timer.h>
32 #include <linux/time.h>
33 #include <linux/wait.h>
34 #include <linux/earlysuspend.h>
35 #include <linux/cpufreq.h>
36 #include <linux/wakelock.h>
37
38 #include <asm/io.h>
39 #include <asm/div64.h>
40 #include <asm/uaccess.h>
41 #include <asm/cacheflush.h>
42
43 #include "rk29_fb.h"
44
45 #ifdef CONFIG_PM
46 #include <linux/pm.h>
47 #endif
48 #ifdef CONFIG_HDMI
49 #include <linux/completion.h>
50
51 #include <linux/hdmi.h>
52 #endif
53
54 #include <mach/iomux.h>
55 #include <mach/gpio.h>
56 #include <mach/board.h>
57 #include <mach/rk29_iomap.h>
58 #include <mach/pmu.h>
59 #include <mach/rk29-ipp.h>
60 #include <mach/ddr.h>
61
62 #include "./display/screen/screen.h"
63
64 #define ANDROID_USE_THREE_BUFS  0       //android use three buffers to accelerate UI display in rgb plane
65 #define CURSOR_BUF_SIZE         256     //RK2818 cursor need 256B buf
66 int rk29_cursor_buf[CURSOR_BUF_SIZE];
67 char cursor_img[] = {
68 0x00,0x00,0x00,0x00,
69 0x00,0x00,0x00,0x00,
70 0x00,0x00,0x00,0x00,
71 0x00,0x00,0x00,0x00,
72 0x00,0x00,0x00,0x00,
73 0x00,0x20,0x00,0x00,
74 0x00,0x30,0x00,0x00,
75 0x00,0x28,0x00,0x00,
76 0x00,0x24,0x00,0x00,
77 0x00,0x22,0x00,0x00,
78 0x00,0x21,0x00,0x00,
79 0x00,0x20,0x80,0x00,
80 0x00,0x20,0x40,0x00,
81 0x00,0x20,0x20,0x00,
82 0x00,0x20,0x10,0x00,
83 0x00,0x20,0x08,0x00,
84 0x00,0x20,0x7C,0x00,
85 0x00,0x22,0x40,0x00,
86 0x00,0x26,0x40,0x00,
87 0x00,0x29,0x20,0x00,
88 0x00,0x31,0x20,0x00,
89 0x00,0x20,0x90,0x00,
90 0x00,0x00,0x90,0x00,
91 0x00,0x00,0x48,0x00,
92 0x00,0x00,0x48,0x00,
93 0x00,0x00,0x30,0x00,
94 0x00,0x00,0x00,0x00,
95 0x00,0x00,0x00,0x00,
96 0x00,0x00,0x00,0x00,
97 0x00,0x00,0x00,0x00,
98 0x00,0x00,0x00,0x00,
99 0x00,0x00,0x00,0x00
100 };
101
102 #if 0
103         #define fbprintk(msg...)        printk(msg);
104 #else
105         #define fbprintk(msg...)
106 #endif
107
108
109 #if 0
110         #define fbprintk2(msg...)       printk(msg);
111 #else
112         #define fbprintk2(msg...)
113 #endif
114
115 #define LcdReadBit(inf, addr, msk)      ((inf->regbak.addr=inf->preg->addr)&(msk))
116 #define LcdWrReg(inf, addr, val)        inf->preg->addr=inf->regbak.addr=(val)
117 #define LcdRdReg(inf, addr)             (inf->preg->addr)
118 #define LcdSetBit(inf, addr, msk)       inf->preg->addr=((inf->regbak.addr) |= (msk))
119 #define LcdClrBit(inf, addr, msk)       inf->preg->addr=((inf->regbak.addr) &= ~(msk))
120 #define LcdSetRegisterBit(inf, addr, msk)    inf->preg->addr=((inf->preg->addr) |= (msk))
121 #define LcdMskReg(inf, addr, msk, val)  (inf->regbak.addr)&=~(msk);   inf->preg->addr=(inf->regbak.addr|=(val))
122
123
124 #define IsMcuLandscape()                ((SCREEN_MCU==inf->cur_screen->type) && (0==inf->mcu_scandir))
125 #define IsMcuUseFmk()                   ( (2==inf->cur_screen->mcu_usefmk) || (1==inf->cur_screen->mcu_usefmk))
126
127 #define CalScaleW0(x, y)                     (((u32)x*0x1000)/y)
128
129 struct rk29fb_rgb {
130         struct fb_bitfield      red;
131         struct fb_bitfield      green;
132         struct fb_bitfield      blue;
133         struct fb_bitfield      transp;
134 };
135
136 static struct rk29fb_rgb def_rgb_16 = {
137      red:    { offset: 11, length: 5, },
138      green:  { offset: 5,  length: 6, },
139      blue:   { offset: 0,  length: 5, },
140      transp: { offset: 0,  length: 0, },
141 };
142
143 struct win_set {
144         volatile u32 y_offset;
145         volatile u32 c_offset;
146 };
147
148 struct win0_par {
149     u32 refcount;
150     u32 pseudo_pal[16];
151     u32 y_offset;
152     u32 c_offset;
153     u32 xpos;         //size in panel
154     u32 ypos;
155     u32 xsize;        //start point in panel
156     u32 ysize;
157     u32 format;
158
159     wait_queue_head_t wait;
160     struct win_set mirror;
161     struct win_set displ;
162     struct win_set done;
163
164     u8 par_seted;
165     u8 addr_seted;
166 };
167
168 /*
169 struct win1_par {
170         u32 refcount;
171         u32     pseudo_pal[16];
172         int lstblank;
173     u32 xpos;
174     u32 ypos;
175     u32 xsize;
176     u32 ysize;
177     u32 format;
178     u32 addr_offset;
179 };
180 */
181
182 struct rk29fb_inf {
183     struct fb_info *fb1;
184     struct fb_info *fb0;
185
186     void __iomem *reg_vir_base;  // virtual basic address of lcdc register
187         u32 reg_phy_base;       // physical basic address of lcdc register
188         u32 len;               // physical map length of lcdc register
189     u32 video_mode;
190
191     struct clk      *clk;
192     struct clk      *dclk;            //lcdc dclk
193     struct clk      *aclk;   //lcdc share memory frequency
194     struct clk      *aclk_parent;     //lcdc aclk divider frequency source
195     struct clk      *aclk_ddr_lcdc;   //DDR LCDC AXI clock disable.
196     struct clk      *aclk_disp_matrix;  //DISPLAY matrix AXI clock disable.
197     struct clk      *hclk_cpu_display;  //CPU DISPLAY AHB bus clock disable.
198     struct clk      *pd_display;        // display power domain
199     unsigned long       dclk_rate;
200
201     /* lcdc reg base address and backup reg */
202     LCDC_REG *preg;
203     LCDC_REG regbak;
204
205         int in_suspend;
206     int fb0_color_deepth;
207     /* variable used in mcu panel */
208         int mcu_needflush;
209         int mcu_isrcnt;
210         u16 mcu_scandir;
211         struct timer_list mcutimer;
212         int mcu_status;
213         u8 mcu_fmksync;
214         int mcu_usetimer;
215         int mcu_stopflush;
216
217     int setFlag;
218     /* external memery */
219         char __iomem *screen_base2;
220     __u32 smem_len2;
221     unsigned long  smem_start2;
222
223     char __iomem *cursor_base;   /* cursor Virtual address*/
224     __u32 cursor_size;           /* Amount of ioremapped VRAM or 0 */
225     unsigned long  cursor_start;
226
227     struct rk29fb_screen panel1_info;         // 1st panel, it's lcd normally
228     struct rk29fb_screen panel2_info;         // 2nd panel
229     struct rk29fb_screen *cur_screen;
230 #if 0 //def CONFIG_CPU_FREQ
231     struct notifier_block freq_transition;
232 #endif
233
234 };
235
236 typedef enum _TRSP_MODE
237 {
238     TRSP_CLOSE = 0,
239     TRSP_FMREG,
240     TRSP_FMREGEX,
241     TRSP_FMRAM,
242     TRSP_FMRAMEX,
243     TRSP_MASK,
244     TRSP_INVAL
245 } TRSP_MODE;
246
247
248 struct platform_device *g_pdev = NULL;
249 //static int win1fb_set_par(struct fb_info *info);
250
251 #if 0
252 #define CHK_SUSPEND(inf)        \
253         if(inf->in_suspend)     {       \
254                 fbprintk(">>>>>> fb is in suspend! return! \n");        \
255                 return -EPERM;  \
256         }
257 #else
258 #define CHK_SUSPEND(inf)
259 #endif
260
261 static DECLARE_WAIT_QUEUE_HEAD(wq);
262 static int wq_condition = 0;
263 static int wq_condition2 = 0;
264 #if ANDROID_USE_THREE_BUFS
265 static int new_frame_seted = 1;
266 #endif
267 static struct wake_lock idlelock; /* only for fb */
268 #ifdef CONFIG_FB_ROTATE_VIDEO   
269 //add by zyc
270 static bool has_set_rotate; 
271 static u32 last_yuv_phy[2] = {0,0};
272 #endif
273 int fb0_first_buff_bits = 32;
274 int fb0_second_buff_bits = 32;
275 int fb_compose_layer_count = 0;
276 static BLOCKING_NOTIFIER_HEAD(rk29fb_notifier_list);
277 int rk29fb_register_notifier(struct notifier_block *nb)
278 {
279         int ret = 0;
280         if (g_pdev) {
281                 struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
282                 if (inf) {
283                         if (inf->cur_screen && inf->cur_screen->type == SCREEN_HDMI)
284                                 nb->notifier_call(nb, RK29FB_EVENT_HDMI_ON, inf->cur_screen);
285                         if (inf->fb1 && inf->fb1->par) {
286                                 struct win0_par *par = inf->fb1->par;
287                                 if (par->refcount)
288                                         nb->notifier_call(nb, RK29FB_EVENT_FB1_ON, inf->cur_screen);
289                         }
290                 }
291         }
292         ret = blocking_notifier_chain_register(&rk29fb_notifier_list, nb);
293
294         return ret;
295 }
296
297 int rk29fb_unregister_notifier(struct notifier_block *nb)
298 {
299         return blocking_notifier_chain_unregister(&rk29fb_notifier_list, nb);
300 }
301
302 static int rk29fb_notify(struct rk29fb_inf *inf, unsigned long event)
303 {
304         return blocking_notifier_call_chain(&rk29fb_notifier_list, event, inf->cur_screen);
305 }
306
307 int mcu_do_refresh(struct rk29fb_inf *inf)
308 {
309     if(inf->mcu_stopflush)  return 0;
310
311     if(SCREEN_MCU!=inf->cur_screen->type)   return 0;
312
313     // use frame mark
314     if(IsMcuUseFmk())
315     {
316         inf->mcu_needflush = 1;
317         return 0;
318     }
319
320     // not use frame mark
321     if(LcdReadBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_SELECT))
322     {
323         if(!LcdReadBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST))
324         {
325             inf->mcu_needflush = 1;
326         }
327         else
328         {
329             if(inf->cur_screen->refresh)    inf->cur_screen->refresh(REFRESH_PRE);
330             inf->mcu_needflush = 0;
331             inf->mcu_isrcnt = 0;
332             LcdSetRegisterBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST);
333         }
334     }
335     return 0;
336 }
337
338
339 void mcutimer_callback(unsigned long arg)
340 {
341     struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
342     static int waitcnt = 0;
343
344     mod_timer(&inf->mcutimer, jiffies + HZ/10);
345
346     switch(inf->mcu_status)
347     {
348     case MS_IDLE:
349         inf->mcu_status = MS_MCU;
350         break;
351     case MS_MCU:
352         if(inf->mcu_usetimer)   mcu_do_refresh(inf);
353         break;
354     case MS_EWAITSTART:
355         inf->mcu_status = MS_EWAITEND;
356         waitcnt = 0;
357         break;
358     case MS_EWAITEND:
359         if(0==waitcnt) {
360             mcu_do_refresh(inf);
361         }
362         if(waitcnt++>14) {
363             inf->mcu_status = MS_EEND;
364         }
365         break;
366     case MS_EEND:
367         inf->mcu_status = MS_MCU;
368         break;
369     default:
370         inf->mcu_status = MS_MCU;
371         break;
372     }
373 }
374
375 int mcu_refresh(struct rk29fb_inf *inf)
376 {
377     static int mcutimer_inited = 0;
378
379     if(SCREEN_MCU!=inf->cur_screen->type)   return 0;
380
381     if(!mcutimer_inited)
382     {
383         mcutimer_inited = 1;
384         init_timer(&inf->mcutimer);
385         inf->mcutimer.function = mcutimer_callback;
386         inf->mcutimer.expires = jiffies + HZ/5;
387         inf->mcu_status = MS_IDLE;
388         add_timer(&inf->mcutimer);
389     }
390
391     if(MS_MCU==inf->mcu_status)     mcu_do_refresh(inf);
392
393     return 0;
394 }
395
396 int mcu_ioctl(unsigned int cmd, unsigned long arg)
397 {
398     struct rk29fb_inf *inf = NULL;
399     if(!g_pdev)     return -1;
400
401     inf = dev_get_drvdata(&g_pdev->dev);
402
403     switch(cmd)
404     {
405     case MCU_WRCMD:
406         LcdClrBit(inf, MCU_TIMING_CTRL, m_MCU_RS_SELECT);
407         LcdWrReg(inf, MCU_BYPASS_WPORT, arg);
408         LcdSetBit(inf, MCU_TIMING_CTRL, m_MCU_RS_SELECT);
409         break;
410
411     case MCU_WRDATA:
412         LcdSetBit(inf, MCU_TIMING_CTRL, m_MCU_RS_SELECT);
413         LcdWrReg(inf, MCU_BYPASS_WPORT, arg);
414         break;
415
416     case MCU_SETBYPASS:
417         LcdMskReg(inf, MCU_TIMING_CTRL, m_MCU_BYPASSMODE_SELECT, v_MCU_BYPASSMODE_SELECT(arg));
418         LcdWrReg(inf, REG_CFG_DONE, 0x01);
419         break;
420
421     default:
422         break;
423     }
424
425     return 0;
426 }
427
428 static irqreturn_t mcu_irqfmk(int irq, void *dev_id)
429 {
430         struct platform_device *pdev = (struct platform_device*)dev_id;
431     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
432     struct rk29fb_screen *screen;
433
434     if(!inf)    return IRQ_HANDLED;
435
436     screen = inf->cur_screen;
437
438     if(0==screen->mcu_usefmk) {
439         return IRQ_HANDLED;
440     }
441
442     if(inf->mcu_fmksync == 1)
443         return IRQ_HANDLED;
444
445     inf->mcu_fmksync = 1;
446     if(inf->mcu_needflush)
447     {
448         inf->mcu_needflush = 0;
449         inf->mcu_isrcnt = 0;
450         if(inf->cur_screen->refresh)
451            inf->cur_screen->refresh(REFRESH_PRE);
452         LcdSetBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST);
453     }
454     inf->mcu_fmksync = 0;
455
456         return IRQ_HANDLED;
457 }
458
459 int init_lcdc(struct fb_info *info)
460 {
461     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
462     u32 msk=0, clr=0;
463
464         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
465
466     inf->clk = clk_get(NULL, "hclk_lcdc");
467     inf->aclk_ddr_lcdc = clk_get(NULL, "aclk_ddr_lcdc");
468     inf->aclk_disp_matrix = clk_get(NULL, "aclk_disp_matrix");
469     inf->hclk_cpu_display = clk_get(NULL, "hclk_cpu_display");
470     inf->pd_display = clk_get(NULL, "pd_display");
471     if ((IS_ERR(inf->clk)) ||
472         (IS_ERR(inf->aclk_ddr_lcdc)) ||
473         (IS_ERR(inf->aclk_disp_matrix)) ||
474         (IS_ERR(inf->hclk_cpu_display)) ||
475         (IS_ERR(inf->pd_display)))
476     {
477         printk(KERN_ERR "failed to get lcdc_hclk source\n");
478         return PTR_ERR(inf->clk);
479     }
480     clk_enable(inf->aclk_disp_matrix);
481     clk_enable(inf->hclk_cpu_display);
482     clk_enable(inf->clk);
483     clk_enable(inf->pd_display);
484     //pmu_set_power_domain(PD_DISPLAY, 1);
485     clk_enable(inf->aclk_ddr_lcdc);
486
487         // set AHB access rule and disable all windows
488     LcdWrReg(inf, SYS_CONFIG, 0x60000000);
489     LcdWrReg(inf, SWAP_CTRL, 0);
490     LcdWrReg(inf, FIFO_WATER_MARK, 0x00000862);//68
491     LcdWrReg(inf, AXI_MS_ID, 0x54321);
492
493         // and mcu holdmode; and set win1 top.
494     LcdMskReg(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_SELECT | m_MCU_HOLDMODE_FRAME_ST | m_MCU_BYPASSMODE_SELECT ,
495             v_MCU_HOLDMODE_SELECT(0)| v_MCU_HOLDMODE_FRAME_ST(0) |v_MCU_BYPASSMODE_SELECT(0));
496
497     // disable blank out, black out, tristate out, yuv2rgb bypass
498     LcdMskReg(inf, BLEND_CTRL,m_W2_BLEND_EN | m_W1_BLEND_EN | m_W0_BLEND_EN | m_HWC_BLEND_EN |
499              m_HWC_BLEND_FACTOR | m_W1_BLEND_FACTOR | m_W0_BLEND_FACTOR,
500              v_W2_BLEND_EN(0) |v_W1_BLEND_EN(0) | v_W0_BLEND_EN(0) | v_HWC_BLEND_EN(0) |
501              v_HWC_BLEND_FACTOR(0) | v_W2_BLEND_FACTOR(0) | v_W1_BLEND_FACTOR(0) | v_W0_BLEND_FACTOR(0)
502              );
503
504     LcdMskReg(inf, WIN0_COLOR_KEY_CTRL, m_COLORKEY_EN, v_COLORKEY_EN(0));
505     LcdMskReg(inf, WIN1_COLOR_KEY_CTRL, m_COLORKEY_EN, v_COLORKEY_EN(0));
506
507     LcdWrReg(inf, DSP_CTRL1, 0);
508
509     // initialize all interrupt
510     clr = v_HOR_STARTCLEAR(1) | v_FRM_STARTCLEAR(1) | v_SCANNING_CLEAR(1);
511
512     msk = v_HOR_STARTMASK(1) | v_FRM_STARTMASK(0) | v_SCANNING_MASK(1);
513
514     LcdWrReg(inf, INT_STATUS, clr | msk);
515
516         // let above to take effect
517     LcdWrReg(inf, REG_CFG_DONE, 0x01);
518
519     return 0;
520 }
521
522 void load_screen(struct fb_info *info, bool initscreen)
523 {
524     int ret = -EINVAL;
525     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
526     struct rk29fb_screen *screen = inf->cur_screen;
527     u16 face;
528     u16 mcu_total, mcu_rwstart, mcu_csstart, mcu_rwend, mcu_csend;
529     u16 right_margin = screen->right_margin, lower_margin = screen->lower_margin;
530     u16 x_res = screen->x_res, y_res = screen->y_res;
531     u32 aclk_rate = 150000000;
532
533     if(!g_pdev){
534         printk(">>>>>> %s : %s no g_pdev\n", __FILE__, __FUNCTION__);
535         return;
536     }
537
538         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
539
540     // set the rgb or mcu
541     LcdMskReg(inf, MCU_TIMING_CTRL, m_MCU_OUTPUT_SELECT, v_MCU_OUTPUT_SELECT((SCREEN_MCU==screen->type)?(1):(0)));
542
543         // set out format and mcu timing
544     mcu_total  = (screen->mcu_wrperiod*150*1000)/1000000;
545     if(mcu_total>31)    mcu_total = 31;
546     if(mcu_total<3)     mcu_total = 3;
547     mcu_rwstart = (mcu_total+1)/4 - 1;
548     mcu_rwend = ((mcu_total+1)*3)/4 - 1;
549     mcu_csstart = (mcu_rwstart>2) ? (mcu_rwstart-3) : (0);
550     mcu_csend = (mcu_rwend>15) ? (mcu_rwend-1) : (mcu_rwend);
551
552     fbprintk(">> mcu_total=%d, mcu_rwstart=%d, mcu_csstart=%d, mcu_rwend=%d, mcu_csend=%d \n",
553         mcu_total, mcu_rwstart, mcu_csstart, mcu_rwend, mcu_csend);
554
555     LcdMskReg(inf, MCU_TIMING_CTRL,
556              m_MCU_CS_ST | m_MCU_CS_END| m_MCU_RW_ST | m_MCU_RW_END |
557              m_MCU_WRITE_PERIOD | m_MCU_HOLDMODE_SELECT | m_MCU_HOLDMODE_FRAME_ST,
558             v_MCU_CS_ST(mcu_csstart) | v_MCU_CS_END(mcu_csend) | v_MCU_RW_ST(mcu_rwstart) |
559             v_MCU_RW_END(mcu_rwend) |  v_MCU_WRITE_PERIOD(mcu_total) |
560             v_MCU_HOLDMODE_SELECT((SCREEN_MCU==screen->type)?(1):(0)) | v_MCU_HOLDMODE_FRAME_ST(0)
561            );
562
563         // set synchronous pin polarity and data pin swap rule
564         switch (screen->face)
565         {
566         case OUT_P565:
567             face = OUT_P565;
568             LcdMskReg(inf, DSP_CTRL0, m_DITHER_DOWN_EN | m_DITHER_DOWN_MODE, v_DITHER_DOWN_EN(1) | v_DITHER_DOWN_MODE(0));
569             break;
570         case OUT_P666:
571             face = OUT_P666;
572             LcdMskReg(inf, DSP_CTRL0, m_DITHER_DOWN_EN | m_DITHER_DOWN_MODE, v_DITHER_DOWN_EN(1) | v_DITHER_DOWN_MODE(1));
573             break;
574         case OUT_D888_P565:
575             face = OUT_P888;
576             LcdMskReg(inf, DSP_CTRL0, m_DITHER_DOWN_EN | m_DITHER_DOWN_MODE, v_DITHER_DOWN_EN(1) | v_DITHER_DOWN_MODE(0));
577             break;
578         case OUT_D888_P666:
579             face = OUT_P888;
580             LcdMskReg(inf, DSP_CTRL0, m_DITHER_DOWN_EN | m_DITHER_DOWN_MODE, v_DITHER_DOWN_EN(1) | v_DITHER_DOWN_MODE(1));
581             break;
582         case OUT_P888:
583             face = OUT_P888;
584             LcdMskReg(inf, DSP_CTRL0, m_DITHER_UP_EN, v_DITHER_UP_EN(1));
585             LcdMskReg(inf, DSP_CTRL0, m_DITHER_DOWN_EN | m_DITHER_DOWN_MODE, v_DITHER_DOWN_EN(0) | v_DITHER_DOWN_MODE(0));
586             break;
587         default:
588             LcdMskReg(inf, DSP_CTRL0, m_DITHER_UP_EN, v_DITHER_UP_EN(0));
589             LcdMskReg(inf, DSP_CTRL0, m_DITHER_DOWN_EN | m_DITHER_DOWN_MODE, v_DITHER_DOWN_EN(0) | v_DITHER_DOWN_MODE(0));
590             face = screen->face;
591             break;
592     }
593
594      LcdMskReg(inf, DSP_CTRL0,
595         m_DISPLAY_FORMAT | m_HSYNC_POLARITY | m_VSYNC_POLARITY | m_DEN_POLARITY |
596         m_DCLK_POLARITY | m_COLOR_SPACE_CONVERSION,
597         v_DISPLAY_FORMAT(face) | v_HSYNC_POLARITY(screen->pin_hsync) | v_VSYNC_POLARITY(screen->pin_vsync) |
598         v_DEN_POLARITY(screen->pin_den) | v_DCLK_POLARITY(screen->pin_dclk) | v_COLOR_SPACE_CONVERSION(0)
599         );
600
601      LcdMskReg(inf, DSP_CTRL1, m_BG_COLOR,  v_BG_COLOR(0x000000) );
602
603      LcdMskReg(inf, SWAP_CTRL, m_OUTPUT_RB_SWAP | m_OUTPUT_RG_SWAP | m_DELTA_SWAP | m_DUMMY_SWAP,
604             v_OUTPUT_RB_SWAP(screen->swap_rb) | v_OUTPUT_RG_SWAP(screen->swap_rg) | v_DELTA_SWAP(screen->swap_delta) | v_DUMMY_SWAP(screen->swap_dumy));
605
606         // set horizontal & vertical out timing
607         if(SCREEN_MCU==inf->cur_screen->type)
608     {
609             right_margin = x_res/6;
610         }
611
612     fbprintk("screen->hsync_len =%d,  screen->left_margin =%d, x_res =%d,  right_margin = %d \n",
613         screen->hsync_len , screen->left_margin , x_res , right_margin );
614     LcdMskReg(inf, DSP_HTOTAL_HS_END, m_BIT12LO | m_BIT12HI, v_BIT12LO(screen->hsync_len) |
615              v_BIT12HI(screen->hsync_len + screen->left_margin + x_res + right_margin));
616     LcdMskReg(inf, DSP_HACT_ST_END, m_BIT12LO | m_BIT12HI, v_BIT12LO(screen->hsync_len + screen->left_margin + x_res) |
617              v_BIT12HI(screen->hsync_len + screen->left_margin));
618
619     LcdMskReg(inf, DSP_VTOTAL_VS_END, m_BIT11LO | m_BIT11HI, v_BIT11LO(screen->vsync_len) |
620               v_BIT11HI(screen->vsync_len + screen->upper_margin + y_res + lower_margin));
621     LcdMskReg(inf, DSP_VACT_ST_END, m_BIT11LO | m_BIT11HI,  v_BIT11LO(screen->vsync_len + screen->upper_margin+y_res)|
622               v_BIT11HI(screen->vsync_len + screen->upper_margin));
623
624     LcdMskReg(inf, DSP_VS_ST_END_F1, m_BIT11LO | m_BIT11HI, v_BIT11LO(0) | v_BIT11HI(0));
625     LcdMskReg(inf, DSP_VACT_ST_END_F1, m_BIT11LO | m_BIT11HI, v_BIT11LO(0) | v_BIT11HI(0));
626
627         // let above to take effect
628     LcdWrReg(inf, REG_CFG_DONE, 0x01);
629
630     inf->dclk = clk_get(NULL, "dclk_lcdc");
631     if (IS_ERR(inf->dclk))
632     {
633         printk(KERN_ERR "failed to get lcd dclock source\n");
634         return ;
635     }
636
637     inf->aclk = clk_get(NULL, "aclk_lcdc");
638     if (IS_ERR(inf->aclk))
639     {
640         printk(KERN_ERR "failed to get lcd clock clk_share_mem source \n");
641         return;
642     }
643     inf->aclk_parent = clk_get(NULL, "ddr_pll");//general_pll //ddr_pll
644     if (IS_ERR(inf->aclk_parent))
645     {
646         printk(KERN_ERR "failed to get lcd clock parent source\n");
647         return ;
648     }
649
650     // set lcdc clk
651     if(SCREEN_MCU==screen->type)    screen->pixclock = 150000000; //mcu fix to 150 MHz
652
653     if(initscreen == 0)    //not init
654     {
655         clk_disable(inf->dclk);
656       //  clk_disable(inf->aclk);
657     }
658
659    // clk_disable(inf->aclk_ddr_lcdc);
660    // clk_disable(inf->aclk_disp_matrix);
661    // clk_disable(inf->hclk_cpu_display);
662
663    // clk_disable(inf->clk);
664     
665
666     fbprintk(">>>>>> set lcdc dclk need %d HZ, clk_parent = %d hz ret =%d\n ", screen->pixclock, screen->lcdc_aclk, ret);
667
668     ret = clk_set_rate(inf->dclk, screen->pixclock);
669     if(ret)
670     {
671         printk(KERN_ERR ">>>>>> set lcdc dclk failed\n");
672     }
673     inf->fb0->var.pixclock = inf->fb1->var.pixclock = div_u64(1000000000000llu, clk_get_rate(inf->dclk));
674     if(initscreen)
675     {
676         ret = clk_set_parent(inf->aclk, inf->aclk_parent);
677         if(screen->lcdc_aclk){
678            aclk_rate = screen->lcdc_aclk;
679         }
680         ret = clk_set_rate(inf->aclk, aclk_rate);
681         if(ret){
682             printk(KERN_ERR ">>>>>> set lcdc aclk failed\n");
683         }
684         clk_enable(inf->aclk);
685     }
686   //  clk_enable(inf->aclk_ddr_lcdc);
687   //  clk_enable(inf->aclk_disp_matrix);
688   //  clk_enable(inf->hclk_cpu_display);       
689   //  clk_enable(inf->clk);
690     clk_enable(inf->dclk);
691
692     // init screen panel
693     if(screen->init)
694     {
695         screen->init();
696     }
697 }
698 #if 0 //def  CONFIG_CPU_FREQ
699 /*
700 * CPU clock speed change handler. We need to adjust the LCD timing
701 * parameters when the CPU clock is adjusted by the power management
702 * subsystem.
703 */
704 #define TO_INF(ptr,member) container_of(ptr,struct rk29fb_inf,member)
705
706 static int
707 rk29fb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
708 {
709     struct rk29fb_inf *inf = TO_INF(nb, freq_transition);
710     struct rk29fb_screen *screen = inf->cur_screen;
711     u32 dclk_rate = 0;
712
713     switch (val)
714     {
715     case CPUFREQ_PRECHANGE:
716           break;
717     case CPUFREQ_POSTCHANGE:
718         {
719          dclk_rate = screen->pixclock * 1000000;
720
721          fbprintk(">>>>>> set lcdc dclk need %d HZ, clk_parent = %d hz \n ", screen->pixclock, dclk_rate);
722
723          clk_set_rate(inf->dclk_divider, dclk_rate);
724          break;
725         }
726     }
727     return 0;
728 }
729 #endif
730
731 static inline unsigned int chan_to_field(unsigned int chan,
732                                          struct fb_bitfield *bf)
733 {
734         chan &= 0xffff;
735         chan >>= 16 - bf->length;
736         return chan << bf->offset;
737 }
738
739 static int fb_setcolreg(unsigned regno,
740                                unsigned red, unsigned green, unsigned blue,
741                                unsigned transp, struct fb_info *info)
742 {
743         unsigned int val;
744 //      fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
745
746         switch (info->fix.visual) {
747         case FB_VISUAL_TRUECOLOR:
748                 /* true-colour, use pseudo-palette */
749                 if (regno < 16) {
750                         u32 *pal = info->pseudo_palette;
751                         val  = chan_to_field(red,   &info->var.red);
752                         val |= chan_to_field(green, &info->var.green);
753                         val |= chan_to_field(blue,  &info->var.blue);
754                         pal[regno] = val;
755                 }
756                 break;
757         default:
758                 return -1;      /* unknown type */
759         }
760
761         return 0;
762 }
763
764 int rk29_set_cursor_en(struct rk29fb_inf *inf, int enable)
765 {
766     if (enable){
767         LcdSetBit(inf, SYS_CONFIG, m_HWC_ENABLE);
768     }else{
769         LcdClrBit(inf, SYS_CONFIG, m_HWC_ENABLE);
770         }
771         LcdWrReg(inf, REG_CFG_DONE, 0x01);
772         return 0;
773 }
774
775 int rk29_set_cursor_pos(struct rk29fb_inf *inf, int x, int y)
776 {
777         struct rk29fb_screen *screen = inf->cur_screen;
778
779     /* set data */
780     if (x >= 0x800 || y >= 0x800 )
781         return -EINVAL;
782         //printk("%s: %08x,%08x \n",__func__, x, y);
783     x += (screen->left_margin + screen->hsync_len);
784     y += (screen->upper_margin + screen->vsync_len);
785     LcdWrReg(inf, HWC_DSP_ST, v_BIT11LO(x)|v_BIT11HI(y));
786         LcdWrReg(inf, REG_CFG_DONE, 0x01);
787     return 0;
788 }
789
790
791 int rk29_set_cursor_colour_map(struct rk29fb_inf *inf, int bg_col, int fg_col)
792 {
793         LcdMskReg(inf, HWC_COLOR_LUT0, m_HWC_R|m_HWC_G|m_HWC_B,bg_col);
794         LcdMskReg(inf, HWC_COLOR_LUT2, m_HWC_R|m_HWC_G|m_HWC_B,fg_col);
795         LcdWrReg(inf, REG_CFG_DONE, 0x01);
796     return 0;
797 }
798
799 #define RK29_CURSOR_WRITE_BIT(addr,mask,value)          (*((char*)addr)) = (((*((char*)addr))&(~((char)mask)))|((char)value))
800 int rk29_set_cursor_img_transform(char *data)
801 {       int x, y;
802         char *pmsk = data;
803         char  *dst = (char*)rk29_cursor_buf;
804         unsigned char  dmsk = 0;
805         unsigned int   op,shift,offset;
806
807         /* rk29 cursor is a 2 bpp 32x32 bitmap this routine
808          * clears it to transparent then combines the cursor
809          * shape plane with the colour plane to set the
810          * cursor */
811         for (y = 0; y < 32; y++)
812         {
813                 for (x = 0; x < 32; x++)
814                 {
815                         if ((x % 8) == 0) {
816                                 dmsk = *pmsk++;
817                         } else {
818                                 dmsk <<= 1;
819                         }
820                         op = (dmsk & 0x80) ? 2 : 3;
821                         shift = (x*2)%8;
822                         offset = x/4;
823                         RK29_CURSOR_WRITE_BIT((dst+offset),(3<<shift),(op<<shift));
824                 }
825                 dst += 8;
826         }
827
828         return 0;
829 }
830
831
832
833 int rk29_set_cursor_img(struct rk29fb_inf *inf, char *data)
834 {
835     if(data)
836     {
837         rk29_set_cursor_img_transform(data);
838     }
839     else
840     {
841         rk29_set_cursor_img_transform(cursor_img);
842     }
843     LcdWrReg(inf, HWC_MST, __pa(rk29_cursor_buf));
844     //LcdSetBit(inf, SYS_CONFIG,m_HWC_RELOAD_EN);
845     LcdSetBit(inf, SYS_CONFIG, m_HWC_RELOAD_EN);
846     flush_cache_all();
847     LcdWrReg(inf, REG_CFG_DONE, 0x01);
848     return 0;
849 }
850
851
852 int rk29_set_cursor_test(struct fb_info *info)
853 {
854         struct rk29fb_inf *inf = dev_get_drvdata(info->device);
855         rk29_set_cursor_colour_map(inf, 0x000000ff, 0x00ff0000);
856         rk29_set_cursor_pos(inf, 0, 0);
857         rk29_set_cursor_img(inf, 0);
858         rk29_set_cursor_en(inf, 1);
859         return 0;
860 }
861 #if 0
862
863 int rk29_set_cursor(struct fb_info *info, struct fb_cursor *cursor)
864 {
865     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
866
867     //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
868
869     /* check not being asked to exceed capabilities */
870
871     if (cursor->image.width > 32)
872         return -EINVAL;
873
874     if (cursor->image.height > 32)
875         return -EINVAL;
876
877     if (cursor->image.depth > 1)
878         return -EINVAL;
879
880     if (cursor->enable)
881         LcdSetBit(inf, SYS_CONFIG, m_HWC_ENABLE);
882     else
883         LcdClrBit(inf, SYS_CONFIG, m_HWC_ENABLE);
884
885     /* set data */
886     if (cursor->set & FB_CUR_SETPOS)
887     {
888         unsigned int x = cursor->image.dx;
889         unsigned int y = cursor->image.dy;
890
891         if (x >= 0x800 || y >= 0x800 )
892             return -EINVAL;
893         LcdWrReg(inf, HWC_DSP_ST, v_BIT11LO(x)|v_BIT11HI(y));
894     }
895
896
897     if (cursor->set & FB_CUR_SETCMAP)
898     {
899         unsigned int bg_col = cursor->image.bg_color;
900         unsigned int fg_col = cursor->image.fg_color;
901
902         fbprintk("%s: update cmap (%08x,%08x)\n",
903             __func__, bg_col, fg_col);
904
905         LcdMskReg(inf, HWC_COLOR_LUT0, m_HWC_R|m_HWC_G|m_HWC_B,
906                   v_HWC_R(info->cmap.red[bg_col]>>8) | v_HWC_G(info->cmap.green[bg_col]>>8) | v_HWC_B(info->cmap.blue[bg_col]>>8));
907
908         LcdMskReg(inf, HWC_COLOR_LUT2, m_HWC_R|m_HWC_G|m_HWC_B,
909                          v_HWC_R(info->cmap.red[fg_col]>>8) | v_HWC_G(info->cmap.green[fg_col]>>8) | v_HWC_B(info->cmap.blue[fg_col]>>8));
910     }
911
912     if ((cursor->set & FB_CUR_SETSIZE ||
913         cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE))
914         && info->screen_base && info->fix.smem_start && info->fix.smem_len)
915     {
916         /* rk29 cursor is a 2 bpp 32x32 bitmap this routine
917          * clears it to transparent then combines the cursor
918          * shape plane with the colour plane to set the
919          * cursor */
920         int x, y;
921         const unsigned char *pcol = cursor->image.data;
922         const unsigned char *pmsk = cursor->mask;
923         void __iomem   *dst;
924         unsigned long cursor_mem_start;
925         unsigned char  dcol = 0;
926         unsigned char  dmsk = 0;
927         unsigned int   op;
928
929         dst = info->screen_base + info->fix.smem_len - CURSOR_BUF_SIZE;
930             cursor_mem_start = info->fix.smem_start + info->fix.smem_len - CURSOR_BUF_SIZE;
931
932         fbprintk("%s: setting shape (%d,%d)\n",
933             __func__, cursor->image.width, cursor->image.height);
934
935         memset(dst, 0, CURSOR_BUF_SIZE);
936
937         for (y = 0; y < cursor->image.height; y++)
938         {
939             for (x = 0; x < cursor->image.width; x++)
940             {
941                 if ((x % 8) == 0) {
942                     dcol = *pcol++;
943                     dmsk = *pmsk++;
944                 } else {
945                     dcol >>= 1;
946                     dmsk >>= 1;
947                 }
948
949                 if (dmsk & 1) {
950                     op = (dcol & 1) ? 1 : 3;
951                     op <<= ((x % 4) * 2);
952                     *(u8*)(dst+(x/4)) |= op;
953                 }
954             }
955             dst += (32*2)/8;
956         }
957         LcdSetBit(inf, SYS_CONFIG,m_HWC_RELOAD_EN);
958         LcdWrReg(inf, HWC_MST, cursor_mem_start);
959         // flush end when wq_condition=1 in mcu panel, but not in rgb panel
960         if(SCREEN_MCU == inf->cur_screen->type) {
961             wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
962             wq_condition = 0;
963         } else {
964             wq_condition = 0;
965             wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
966         }
967         LcdClrBit(inf, SYS_CONFIG, m_HWC_RELOAD_EN);
968     }
969
970     return 0;
971 }
972 #endif
973 static int hdmi_get_fbscale(void)
974 {
975 #ifdef CONFIG_HDMI
976         return hdmi_get_scale();
977 #else
978         return 100;
979 #endif
980 }
981 static void hdmi_set_fbscale(struct fb_info *info)
982 {
983 #ifdef CONFIG_HDMI
984     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
985     struct rk29fb_screen *screen = inf->cur_screen;
986     struct win0_par *par = info->par;
987         int scale;
988         
989         scale = hdmi_get_scale();
990         if(scale == 100)
991                 return;
992         par->xpos += screen->x_res * (100-scale) / 200;
993         par->ypos += screen->y_res * (100-scale) / 200;
994         par->xsize = par->xsize *scale /100;
995         par->ysize = par->ysize *scale /100;
996 #endif
997 }
998 static int win0_blank(int blank_mode, struct fb_info *info)
999 {
1000     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1001
1002     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1003
1004         CHK_SUSPEND(inf);
1005
1006     switch(blank_mode)
1007     {
1008     case FB_BLANK_UNBLANK:
1009         LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(1));
1010             LcdWrReg(inf, REG_CFG_DONE, 0x01);
1011         break;
1012     case FB_BLANK_NORMAL:
1013          LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(0));
1014              break;
1015     default:
1016         LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(0));
1017             LcdWrReg(inf, REG_CFG_DONE, 0x01);
1018 #ifdef CONFIG_DDR_RECONFIG
1019         msleep(40);
1020 #endif
1021         break;
1022     }
1023
1024         mcu_refresh(inf);
1025     return 0;
1026 }
1027
1028 static int win0_set_par(struct fb_info *info)
1029 {
1030     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1031     struct rk29fb_screen *screen = inf->cur_screen;
1032     struct fb_var_screeninfo *var = &info->var;
1033     struct fb_fix_screeninfo *fix = &info->fix;
1034     struct win0_par *par = info->par;
1035         u32 xact, yact, xvir, yvir, xpos, ypos, ScaleYrgbX,ScaleYrgbY, ScaleCbrX, ScaleCbrY, y_addr,uv_addr;
1036         hdmi_set_fbscale(info);
1037         xact = var->xres;                           /* visible resolution               */
1038         yact = var->yres;
1039         xvir = var->xres_virtual;               /* virtual resolution           */
1040         yvir = var->yres_virtual;
1041         //u32 xact_st = var->xoffset;         /* offset from virtual to visible */
1042         //u32 yact_st = var->yoffset;         /* resolution                     */
1043     xpos = par->xpos;
1044     ypos = par->ypos;
1045
1046     ScaleYrgbX=0x1000;
1047         ScaleYrgbY=0x1000;
1048     ScaleCbrX=0x1000;
1049         ScaleCbrY=0x1000;
1050
1051     y_addr = 0;       //user alloc buf addr y
1052     uv_addr = 0;
1053
1054     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1055
1056         CHK_SUSPEND(inf);
1057
1058     if(((var->rotate == 270)||(var->rotate == 90) || (var->rotate == 180)) && (inf->video_mode))
1059     {
1060       #ifdef CONFIG_FB_ROTATE_VIDEO  
1061         if(xact > screen->x_res)
1062         {
1063             xact = screen->x_res;       /* visible resolution       */
1064             yact = screen->y_res;
1065             xvir = screen->x_res;       /* virtual resolution       */
1066             yvir = screen->y_res;
1067         }   else   {            
1068             xact = var->xres;               /* visible resolution       */
1069             yact = var->yres;
1070             xvir = var->xres_virtual;       /* virtual resolution       */
1071             yvir = var->yres_virtual;
1072                         printk("xact=%d yact =%d \n",xact,yact);
1073         }
1074       #else //CONFIG_FB_ROTATE_VIDEO
1075         printk("LCDC not support rotate!\n");
1076         return -EINVAL;
1077       #endif
1078     }
1079     
1080         // calculate the display phy address
1081     y_addr = fix->smem_start + par->y_offset;
1082     uv_addr = fix->mmio_start + par->c_offset;
1083
1084     ScaleYrgbX = CalScaleW0(xact, par->xsize);
1085     ScaleYrgbY = CalScaleW0(yact, par->ysize);
1086
1087     switch (par->format)
1088     {
1089        case 2:// yuv422
1090            ScaleCbrX= CalScaleW0((xact/2), par->xsize);
1091            ScaleCbrY =  CalScaleW0(yact, par->ysize);
1092            break;
1093        case 3: // yuv4200
1094        case 4: // yuv4201
1095            ScaleCbrX= CalScaleW0(xact/2, par->xsize);
1096            ScaleCbrY =  CalScaleW0(yact/2, par->ysize);
1097            break;
1098        case 5:// yuv444
1099            ScaleCbrX= CalScaleW0(xact, par->xsize);
1100            ScaleCbrY =  CalScaleW0(yact, par->ysize);
1101            break;
1102        default:
1103            break;
1104     }
1105
1106     xpos += (screen->left_margin + screen->hsync_len);
1107     ypos += (screen->upper_margin + screen->vsync_len);
1108
1109     LcdWrReg(inf, WIN0_YRGB_MST, y_addr);
1110     LcdWrReg(inf, WIN0_CBR_MST, uv_addr);
1111
1112     LcdMskReg(inf, SYS_CONFIG,  m_W0_FORMAT , v_W0_FORMAT(par->format));//(inf->video_mode==0)
1113
1114     LcdMskReg(inf, WIN0_VIR, m_WORDLO | m_WORDHI, v_VIRWIDTH(xvir) | v_VIRHEIGHT((yvir)) );
1115     LcdMskReg(inf, WIN0_ACT_INFO, m_WORDLO | m_WORDHI, v_WORDLO(xact) | v_WORDHI(yact));
1116     LcdMskReg(inf, WIN0_DSP_ST, m_BIT11LO | m_BIT11HI, v_BIT11LO(xpos) | v_BIT11HI(ypos));
1117     LcdMskReg(inf, WIN0_DSP_INFO, m_BIT12LO | m_BIT12HI,  v_BIT12LO(par->xsize) | v_BIT12HI(par->ysize));
1118     LcdMskReg(inf, WIN0_SCL_FACTOR_YRGB, m_WORDLO | m_WORDHI, v_WORDLO(ScaleYrgbX) | v_WORDHI(ScaleYrgbY));
1119     LcdMskReg(inf, WIN0_SCL_FACTOR_CBR, m_WORDLO | m_WORDHI, v_WORDLO(ScaleCbrX) | v_WORDHI(ScaleCbrY));
1120
1121     switch(par->format)
1122     {
1123     case 0:  //rgb888
1124         LcdMskReg(inf, SWAP_CTRL, m_W0_YRGB_8_SWAP | m_W0_YRGB_16_SWAP | m_W0_YRGB_R_SHIFT_SWAP | m_W0_565_RB_SWAP | m_W0_YRGB_M8_SWAP | m_W0_CBR_8_SWAP,
1125             v_W0_YRGB_8_SWAP(1) | v_W0_YRGB_16_SWAP(1) | v_W0_YRGB_R_SHIFT_SWAP(1) | v_W0_565_RB_SWAP(0) | v_W0_YRGB_M8_SWAP(0) | v_W0_CBR_8_SWAP(0));
1126                 break;
1127     case 1:  //rgb565
1128         LcdMskReg(inf, SWAP_CTRL, m_W0_YRGB_8_SWAP | m_W0_YRGB_16_SWAP | m_W0_YRGB_R_SHIFT_SWAP | m_W0_565_RB_SWAP | m_W0_YRGB_M8_SWAP | m_W0_CBR_8_SWAP,
1129             v_W0_YRGB_8_SWAP(0) | v_W0_YRGB_16_SWAP(0) | v_W0_YRGB_R_SHIFT_SWAP(0) | v_W0_565_RB_SWAP(0) | v_W0_YRGB_M8_SWAP(0) | v_W0_CBR_8_SWAP(0));
1130         break;
1131     case 4:   //yuv4201
1132         LcdMskReg(inf, SWAP_CTRL, m_W0_YRGB_8_SWAP | m_W0_YRGB_16_SWAP | m_W0_YRGB_R_SHIFT_SWAP | m_W0_565_RB_SWAP | m_W0_YRGB_M8_SWAP | m_W0_CBR_8_SWAP,
1133             v_W0_YRGB_8_SWAP(0) | v_W0_YRGB_16_SWAP(0) | v_W0_YRGB_R_SHIFT_SWAP(0) | v_W0_565_RB_SWAP(0) |
1134             v_W0_YRGB_M8_SWAP(1) | v_W0_CBR_8_SWAP(0));
1135         break;
1136     default:
1137         LcdMskReg(inf, SWAP_CTRL, m_W0_YRGB_8_SWAP | m_W0_YRGB_16_SWAP | m_W0_YRGB_R_SHIFT_SWAP | m_W0_565_RB_SWAP | m_W0_YRGB_M8_SWAP | m_W0_CBR_8_SWAP,
1138             v_W0_YRGB_8_SWAP(0) | v_W0_YRGB_16_SWAP(0) | v_W0_YRGB_R_SHIFT_SWAP(0) | v_W0_565_RB_SWAP(0) | v_W0_YRGB_M8_SWAP(0) | v_W0_CBR_8_SWAP(0) );
1139                 break;
1140     }
1141
1142     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1143
1144     return 0;
1145
1146 }
1147
1148 static int win0_pan( struct fb_info *info )
1149 {
1150     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1151    // struct fb_var_screeninfo *var0 = &info->var;
1152     struct fb_fix_screeninfo *fix0 = &info->fix;
1153     struct win0_par *par = info->par;
1154     u32 y_addr=0, uv_addr=0;
1155
1156     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1157
1158         CHK_SUSPEND(inf);
1159
1160     y_addr = fix0->smem_start +  par->y_offset;//y_offset;
1161     uv_addr = fix0->mmio_start + par->c_offset ;//c_offset;
1162
1163     LcdWrReg(inf, WIN0_YRGB_MST, y_addr);
1164     LcdWrReg(inf, WIN0_CBR_MST, uv_addr);
1165     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1166
1167      // enable win0 after the win0 addr is seted
1168         LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE((1==par->addr_seted)?(1):(0)));
1169         mcu_refresh(inf);
1170
1171     return 0;
1172 }
1173
1174 static int win1_blank(int blank_mode, struct fb_info *info)
1175 {
1176     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1177
1178     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1179
1180         CHK_SUSPEND(inf);
1181
1182         switch(blank_mode)
1183     {
1184     case FB_BLANK_UNBLANK:
1185         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(1));
1186         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1187         break;
1188     case FB_BLANK_NORMAL:
1189          LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
1190              break;
1191     default:
1192         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
1193         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1194         break;
1195     }
1196     
1197
1198         mcu_refresh(inf);
1199     return 0;
1200 }
1201
1202
1203 #ifdef CONFIG_CLOSE_WIN1_DYNAMIC 
1204 static void win1_check_work_func(struct work_struct *work)
1205 {
1206     struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
1207     struct fb_info *fb0_inf = inf->fb0;
1208      struct fb_var_screeninfo *var = &fb0_inf->var;
1209     int i=0;
1210     int *p = NULL;
1211     int blank_data,total_data;
1212     int format = 0;
1213     u16 xres_virtual = fb0_inf->var.xres_virtual;      //virtual screen size
1214     u16 xpos_virtual = fb0_inf->var.xoffset;           //visiable offset in virtual screen
1215     u16 ypos_virtual = fb0_inf->var.yoffset;
1216
1217     int offset = 0;//(ypos_virtual*xres_virtual + xpos_virtual)*((inf->fb0_color_deepth || fb0_inf->var.bits_per_pixel==32)? 4:2)/4;  
1218     switch(var->bits_per_pixel)
1219     {
1220         case 16: 
1221             format = 1;
1222             offset = (ypos_virtual*xres_virtual + xpos_virtual)*(inf->fb0_color_deepth ? 4:2);
1223             if(ypos_virtual == 3*var->yres && inf->fb0_color_deepth)
1224                 offset -= var->yres * var->xres *2;
1225             break;
1226         default:
1227             format = 0;
1228             offset = (ypos_virtual*xres_virtual + xpos_virtual)*4;            
1229             if(ypos_virtual >= 2*var->yres)
1230             {
1231                 format = 1;
1232                 if(ypos_virtual == 3*var->yres)
1233                 {            
1234                     offset -= var->yres * var->xres *2;
1235                 }
1236             }
1237             break;
1238     }
1239     p = (int*)fb0_inf->screen_base + offset; 
1240     blank_data = (inf->fb0_color_deepth==32) ? 0xff000000 : 0;
1241     total_data = fb0_inf->var.xres * fb0_inf->var.yres / (format+1);
1242     
1243    // printk("var->bits_per_pixel=%d,ypos_virtual=%d, var->yres=%d,offset=%d,total_data=%d\n",var->bits_per_pixel,ypos_virtual,var->yres,offset,total_data);
1244     
1245     for(i=0; i < total_data; i++)
1246     {
1247         if(*p++ != blank_data) 
1248         {
1249             //printk("win1 have no 0 data in %d, total %d\n",i,total_data);
1250             return;
1251         }            
1252     }
1253
1254     win1_blank(FB_BLANK_POWERDOWN, fb0_inf);
1255    // printk("%s close win1!\n",__func__);
1256 }
1257 static DECLARE_DELAYED_WORK(rk29_win1_check_work, win1_check_work_func);
1258 #endif
1259 static int win1_set_par(struct fb_info *info)
1260 {
1261     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1262     struct fb_fix_screeninfo *fix = &info->fix;
1263     struct rk29fb_screen *screen = inf->cur_screen;
1264     struct win0_par *par = info->par;
1265     struct fb_var_screeninfo *var = &info->var;
1266         u32 addr;
1267         u16 xres_virtual,xpos,ypos;
1268         u8 trspval,trspmode;
1269  #ifdef CONFIG_CLOSE_WIN1_DYNAMIC   
1270     cancel_delayed_work_sync(&rk29_win1_check_work);
1271  #endif   
1272     if(((screen->x_res != var->xres) || (screen->y_res != var->yres))
1273         && !((screen->x_res>1280) && (var->bits_per_pixel == 32)))
1274     {
1275         hdmi_set_fbscale(info);
1276     }else  if(((screen->x_res==1920) ))
1277     {
1278         if(hdmi_get_fbscale() < 100)
1279                         par->ypos -=screen->y_res * (100-hdmi_get_fbscale()) / 200;
1280         }
1281     //u32 offset=0, addr=0, map_size=0, smem_len=0;
1282     addr=0;
1283     xres_virtual = 0;      //virtual screen size
1284
1285     //u16 xpos_virtual = var->xoffset;           //visiable offset in virtual screen
1286     //u16 ypos_virtual = var->yoffset;
1287
1288     xpos = par->xpos;                 //visiable offset in panel
1289     ypos = par->ypos;
1290
1291     trspmode = TRSP_CLOSE;
1292     trspval = 0;
1293
1294     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1295
1296    #ifdef CONFIG_FB_SCALING_OSD
1297     if(((screen->x_res != var->xres) || (screen->y_res != var->yres)) 
1298             && (screen->x_res<=1280))
1299     {
1300         addr = fix->mmio_start + par->y_offset* hdmi_get_fbscale()/100;
1301         xres_virtual = screen->x_res* hdmi_get_fbscale()/100;      //virtual screen size
1302     }
1303     else
1304    #endif
1305     {
1306         addr = fix->smem_start + par->y_offset;
1307         xres_virtual = var->xres_virtual;      //virtual screen size
1308     }
1309     LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE|m_W1_FORMAT, v_W1_ENABLE(1)|v_W1_FORMAT(par->format));
1310
1311     xpos += (screen->left_margin + screen->hsync_len);
1312     ypos += (screen->upper_margin + screen->vsync_len);
1313
1314     LcdWrReg(inf, WIN1_YRGB_MST, addr);
1315
1316     LcdMskReg(inf, WIN1_DSP_ST, m_BIT11LO|m_BIT11HI, v_BIT11LO(xpos) | v_BIT11HI(ypos));
1317     LcdMskReg(inf, WIN1_DSP_INFO, m_BIT12LO|m_BIT12HI, v_BIT12LO(par->xsize) | v_BIT12HI(par->ysize));
1318
1319     LcdMskReg(inf, WIN1_VIR, m_WORDLO , v_WORDLO(xres_virtual));
1320
1321     LcdMskReg(inf, BLEND_CTRL, m_W1_BLEND_EN |  m_W1_BLEND_FACTOR,
1322         v_W1_BLEND_EN((TRSP_FMREG==trspmode) || (TRSP_MASK==trspmode)) | v_W1_BLEND_FACTOR(trspval));
1323
1324      // enable win1 color key and set the color to black(rgb=0)
1325     LcdMskReg(inf, WIN1_COLOR_KEY_CTRL, m_COLORKEY_EN | m_KEYCOLOR, v_COLORKEY_EN(1) | v_KEYCOLOR(0));
1326
1327     if(1==par->format) //rgb565
1328     {
1329         LcdMskReg(inf, SWAP_CTRL, m_W1_8_SWAP | m_W1_16_SWAP | m_W1_R_SHIFT_SWAP | m_W1_565_RB_SWAP,
1330             v_W1_8_SWAP(0) | v_W1_16_SWAP(0) | v_W1_R_SHIFT_SWAP(0) | v_W1_565_RB_SWAP(0) );
1331     }
1332     else
1333     {
1334          LcdMskReg(inf, SWAP_CTRL, m_W1_8_SWAP | m_W1_16_SWAP | m_W1_R_SHIFT_SWAP | m_W1_565_RB_SWAP,
1335                 v_W1_8_SWAP(1) | v_W1_16_SWAP(1) | v_W1_R_SHIFT_SWAP(1) | v_W1_565_RB_SWAP(0) );
1336
1337          LcdMskReg(inf, DSP_CTRL0, m_W1_TRANSP_FROM, v_W1_TRANSP_FROM(TRSP_FMRAM==trspmode) );
1338     }
1339
1340         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1341     
1342 #ifdef CONFIG_CLOSE_WIN1_DYNAMIC 
1343     schedule_delayed_work(&rk29_win1_check_work, msecs_to_jiffies(5000));
1344 #endif
1345
1346     return 0;
1347 }
1348
1349 static int win1_pan( struct fb_info *info )
1350 {
1351     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1352     struct fb_fix_screeninfo *fix1 = &info->fix;
1353     struct win0_par *par = info->par;
1354     u32 addr = 0;
1355
1356     #ifdef CONFIG_FB_SCALING_OSD
1357     struct rk29fb_screen *screen = inf->cur_screen;
1358     struct fb_var_screeninfo *var = &info->var;
1359     if(((screen->x_res != var->xres) || (screen->y_res != var->yres)) 
1360             && (screen->x_res<=1280))
1361     {
1362         addr = fix1->mmio_start + par->y_offset* hdmi_get_fbscale()/100;
1363     }
1364     else
1365     #endif
1366     {
1367         addr = fix1->smem_start + par->y_offset;
1368     }
1369
1370     //fbprintk("info->screen_base = %8x ; fix1->smem_len = %d , addr = %8x\n",(u32)info->screen_base, fix1->smem_len, addr);
1371
1372     LcdWrReg(inf, WIN1_YRGB_MST, addr);
1373     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1374         mcu_refresh(inf);
1375
1376     return 0;
1377 }
1378
1379 static int fb0_blank(int blank_mode, struct fb_info *info)
1380 {
1381         struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1382
1383     if(inf->video_mode == 1)
1384     {
1385         win1_blank(blank_mode, info);
1386     }
1387     else
1388     {
1389         win0_blank(blank_mode, info);
1390     }
1391     return 0;
1392 }
1393
1394 static int fb0_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1395 {
1396     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1397     struct rk29fb_screen *screen = inf->cur_screen;
1398     u16 xpos = (var->nonstd>>8) & 0xfff;
1399     u16 ypos = (var->nonstd>>20) & 0xfff;
1400     u16 xlcd = screen->x_res;
1401     u16 ylcd = screen->y_res;
1402
1403     //fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1404
1405         CHK_SUSPEND(inf);
1406
1407     if( 0==var->xres_virtual || 0==var->yres_virtual ||
1408         0==var->xres || 0==var->yres || var->xres<16 ||
1409         ((16!=var->bits_per_pixel)&&(32!=var->bits_per_pixel)) )
1410     {
1411         printk(">>>>>> fb0_check_var fail 1!!! \n");
1412         printk(">>>>>> 0==%d || 0==%d ", var->xres_virtual,var->yres_virtual);
1413         printk("0==%d || 0==%d || %d<16 || ", var->xres,var->yres,var->xres<16);
1414         printk("bits_per_pixel=%d \n", var->bits_per_pixel);
1415         return -EINVAL;
1416     }
1417
1418     if( (var->xoffset+var->xres)>var->xres_virtual ||
1419         (var->yoffset+var->yres)>var->yres_virtual*2 )
1420     {
1421         printk(">>>>>> fb0_check_var fail 2!!! \n");
1422         printk(">>>>>> (%d+%d)>%d || ", var->xoffset,var->xres,var->xres_virtual);
1423         printk("(%d+%d)>%d || ", var->yoffset,var->yres,var->yres_virtual);
1424         printk("(%d+%d)>%d || (%d+%d)>%d \n", xpos,var->xres,xlcd,ypos,var->yres,ylcd);
1425         return -EINVAL;
1426     }
1427
1428     if(inf->fb0_color_deepth)var->bits_per_pixel=inf->fb0_color_deepth;
1429     switch(var->bits_per_pixel)
1430     {
1431     case 16:    // rgb565
1432         var->xres_virtual = (var->xres_virtual + 0x1) & (~0x1);
1433         var->xres = (var->xres + 0x1) & (~0x1);
1434         var->xoffset = (var->xoffset) & (~0x1);
1435         break;
1436     default:    // rgb888
1437         var->bits_per_pixel = 32;
1438         break;
1439     }
1440
1441     return 0;
1442 }
1443
1444
1445 static int fb0_set_par(struct fb_info *info)
1446 {
1447     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1448     struct fb_var_screeninfo *var = &info->var;
1449     struct fb_fix_screeninfo *fix = &info->fix;
1450     struct rk29fb_screen *screen = inf->cur_screen;
1451     struct win0_par *par = info->par;
1452
1453     u32 offset=0,  smem_len=0;
1454     u16 xres_virtual = var->xres_virtual;      //virtual screen size
1455     u16 xpos_virtual = var->xoffset;           //visiable offset in virtual screen
1456     u16 ypos_virtual = var->yoffset;
1457
1458 #ifdef CONFIG_FB_SCALING_OSD
1459     struct rk29_ipp_req ipp_req;
1460     u32 dstoffset=0;
1461         memset(&ipp_req, 0, sizeof(struct rk29_ipp_req));
1462 #endif
1463
1464     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1465
1466     inf->setFlag = 0;
1467         CHK_SUSPEND(inf);
1468
1469     if(inf->fb0_color_deepth)var->bits_per_pixel=inf->fb0_color_deepth;
1470     #if !defined(CONFIG_FB_SCALING_OSD)
1471     if((inf->video_mode == 1)&&(screen->y_res < var->yres))ypos_virtual += (var->yres-screen->y_res);
1472     #endif
1473
1474     switch(var->bits_per_pixel)
1475     {
1476     case 16:    // rgb565
1477         par->format = 1;
1478          if( ypos_virtual == 0)
1479             fb0_first_buff_bits = 16;
1480         else
1481             fb0_second_buff_bits = 16;
1482
1483         //fix->line_length = 2 * xres_virtual;
1484         fix->line_length = (inf->fb0_color_deepth ? 4:2) * xres_virtual;   //32bit and 16bit change
1485
1486         #ifdef CONFIG_FB_SCALING_OSD
1487         dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*2;
1488         ipp_req.src0.fmt = IPP_RGB_565;
1489         ipp_req.dst0.fmt = IPP_RGB_565;
1490         #endif
1491         offset = (ypos_virtual*xres_virtual + xpos_virtual)*(inf->fb0_color_deepth ? 4:2);
1492         if(ypos_virtual == 3*var->yres && inf->fb0_color_deepth)
1493             offset -= var->yres * var->xres *2;
1494         break;
1495     case 32:    // rgb888
1496     default:
1497         par->format = 0;
1498          if( ypos_virtual == 0)
1499             fb0_first_buff_bits = 32;
1500         else
1501             fb0_second_buff_bits = 32;
1502         fix->line_length = 4 * xres_virtual;
1503         #ifdef CONFIG_FB_SCALING_OSD
1504         dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*4;       
1505
1506         ipp_req.src0.fmt = IPP_XRGB_8888;
1507         ipp_req.dst0.fmt = IPP_XRGB_8888;
1508         #endif
1509         offset = (ypos_virtual*xres_virtual + xpos_virtual)*4;
1510         
1511         if(ypos_virtual >= 2*var->yres)
1512         {
1513             par->format = 1;
1514             #ifdef CONFIG_FB_SCALING_OSD
1515             dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*2;
1516             ipp_req.src0.fmt = IPP_RGB_565;
1517             ipp_req.dst0.fmt = IPP_RGB_565;
1518             #endif
1519             if(ypos_virtual == 3*var->yres)
1520             {            
1521                 offset -= var->yres * var->xres *2;
1522             }
1523         }
1524         break;
1525     }
1526
1527     smem_len = fix->line_length * var->yres_virtual;
1528     //map_size = PAGE_ALIGN(smem_len);
1529
1530     if (smem_len > fix->smem_len)     // buffer need realloc
1531     {
1532         printk("%s sorry!!! win1 buf is not enough\n",__FUNCTION__);
1533         printk("line_length = %d, yres_virtual = %d, win1_buf only = %dB\n",fix->line_length,var->yres_virtual,fix->smem_len);
1534         printk("you can change buf size MEM_FB_SIZE in board-xxx.c \n");
1535     }
1536
1537
1538     par->addr_seted = 1;
1539 #if ANDROID_USE_THREE_BUFS
1540     if(0==new_frame_seted) {
1541         wq_condition = 0;
1542         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1543     }
1544     new_frame_seted = 0;
1545 #endif
1546
1547     if(inf->video_mode == 1)
1548     {
1549         #ifdef CONFIG_FB_SCALING_OSD
1550         if(((screen->x_res != var->xres) || (screen->y_res != var->yres)) 
1551             && (screen->x_res<=1280))
1552         {
1553             par->xpos = 0;
1554             par->ypos = 0;
1555             par->xsize = screen->x_res;
1556             par->ysize = screen->y_res;
1557             par->y_offset = dstoffset;
1558
1559             ipp_req.src0.YrgbMst = fix->smem_start + offset;
1560             ipp_req.src0.w = var->xres;
1561             ipp_req.src0.h = var->yres;
1562
1563             ipp_req.dst0.YrgbMst = fix->mmio_start + dstoffset* hdmi_get_fbscale()/100;
1564             ipp_req.dst0.w = screen->x_res* hdmi_get_fbscale()/100;
1565             ipp_req.dst0.h = screen->y_res* hdmi_get_fbscale()/100;
1566
1567             ipp_req.src_vir_w = ipp_req.src0.w;
1568             ipp_req.dst_vir_w = ipp_req.dst0.w;
1569             ipp_req.timeout = 100;
1570             ipp_req.flag = IPP_ROT_0;
1571             //ipp_do_blit(&ipp_req);
1572             ipp_blit_sync(&ipp_req);
1573         }else
1574         #endif
1575         {
1576             par->y_offset = offset;
1577             par->xpos = (screen->x_res >= var->xres)?((screen->x_res - var->xres)/2):0;              //visiable offset in panel
1578             par->ypos = (screen->y_res >= var->yres)?(screen->y_res - var->yres):0;
1579             par->xsize = var->xres;                                //visiable size in panel
1580             par->ysize = (screen->y_res >= var->yres) ? var->yres : screen->y_res;
1581         }
1582         win1_set_par(info);
1583     }
1584     else
1585     {
1586         par->y_offset = offset;
1587         par->xpos = 0;
1588         par->ypos = 0;
1589         par->xsize = screen->x_res;
1590         par->ysize = screen->y_res;
1591         win0_set_par(info);
1592     }
1593     inf->setFlag = 1;
1594     return 0;
1595 }
1596
1597 static int fb0_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1598 {
1599
1600     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1601     struct fb_var_screeninfo *var1 = &info->var;
1602     struct rk29fb_screen *screen = inf->cur_screen;
1603     struct win0_par *par = info->par;
1604
1605     u32 offset = 0;
1606     u16 ypos_virtual = var->yoffset;
1607     u16 xpos_virtual = var->xoffset;
1608    #ifdef CONFIG_FB_SCALING_OSD
1609     struct fb_fix_screeninfo *fix = &info->fix;
1610     struct rk29_ipp_req ipp_req;
1611     u32 dstoffset = 0;
1612         memset(&ipp_req, 0, sizeof(struct rk29_ipp_req));
1613    #endif
1614         //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1615
1616         CHK_SUSPEND(inf);
1617
1618     if(inf->fb0_color_deepth)var->bits_per_pixel=inf->fb0_color_deepth;
1619     #if !defined(CONFIG_FB_SCALING_OSD)
1620         if((inf->video_mode == 1)&&(screen->y_res < var->yres))ypos_virtual += (var->yres-screen->y_res);
1621     #endif
1622
1623     switch(var1->bits_per_pixel)
1624     {
1625     case 16:    // rgb565
1626         var->xoffset = (var->xoffset) & (~0x1);
1627         #ifdef CONFIG_FB_SCALING_OSD
1628         dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres) * 2;
1629         ipp_req.src0.fmt = IPP_RGB_565;
1630         ipp_req.dst0.fmt = IPP_RGB_565;
1631         #endif
1632         offset = (ypos_virtual*var1->xres_virtual + xpos_virtual)*(inf->fb0_color_deepth ? 4:2);
1633         if(ypos_virtual == 3*var->yres && inf->fb0_color_deepth)
1634             offset -= var->yres * var->xres *2;
1635         break;
1636     case 32:    // rgb888
1637         #ifdef CONFIG_FB_SCALING_OSD
1638         dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*4;
1639         ipp_req.src0.fmt = IPP_XRGB_8888;
1640         ipp_req.dst0.fmt = IPP_XRGB_8888;
1641         #endif
1642         offset = (ypos_virtual*var1->xres_virtual + xpos_virtual)*4;
1643         if(ypos_virtual >= 2*var->yres)
1644         {
1645             par->format = 1;
1646             #ifdef CONFIG_FB_SCALING_OSD
1647             dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*2;
1648             ipp_req.src0.fmt = IPP_RGB_565;
1649             ipp_req.dst0.fmt = IPP_RGB_565;
1650             #endif
1651             if(ypos_virtual == 3*var->yres)
1652             {            
1653                 offset -= var->yres * var->xres *2;
1654             }
1655         }
1656         break;
1657     default:
1658         return -EINVAL;
1659     }
1660
1661     if(inf->video_mode == 1)
1662     {
1663         #ifdef CONFIG_FB_SCALING_OSD
1664         if(((screen->x_res != var->xres) || (screen->y_res != var->yres)) 
1665             && (screen->x_res<=1280))
1666         {
1667             par->y_offset = dstoffset;
1668
1669             ipp_req.src0.YrgbMst = fix->smem_start + offset;
1670             ipp_req.src0.w = var->xres;
1671             ipp_req.src0.h = var->yres;
1672
1673             ipp_req.dst0.YrgbMst = fix->mmio_start + dstoffset* hdmi_get_fbscale()/100;
1674             ipp_req.dst0.w = screen->x_res* hdmi_get_fbscale()/100;
1675             ipp_req.dst0.h = screen->y_res* hdmi_get_fbscale()/100;
1676
1677             ipp_req.src_vir_w = ipp_req.src0.w;
1678             ipp_req.dst_vir_w = ipp_req.dst0.w;
1679             ipp_req.timeout = 100;
1680             ipp_req.flag = IPP_ROT_0;
1681             //ipp_do_blit(&ipp_req);
1682             ipp_blit_sync(&ipp_req);
1683             win1_pan(info);
1684             return 0;
1685         }else
1686         #endif
1687             par->y_offset = offset;
1688         win1_pan(info);
1689     }
1690     else
1691     {
1692         par->y_offset = offset;
1693         win0_pan(info);
1694     }
1695         // flush end when wq_condition=1 in mcu panel, but not in rgb panel
1696 #if !ANDROID_USE_THREE_BUFS
1697     // flush end when wq_condition=1 in mcu panel, but not in rgb panel
1698     if(SCREEN_MCU == inf->cur_screen->type) {
1699         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1700         wq_condition = 0;
1701     } else {
1702         wq_condition = 0;
1703         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1704     }
1705 #endif
1706     return 0;
1707 }
1708
1709 static int fb0_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
1710 {
1711     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1712         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1713
1714         CHK_SUSPEND(inf);
1715
1716     switch(cmd)
1717     {
1718     case FB0_IOCTL_STOP_TIMER_FLUSH:    //stop timer flush mcu panel after android is runing
1719         if(1==arg)
1720         {
1721             inf->mcu_usetimer = 0;
1722         }
1723         break;
1724    case FBIOPUT_16OR32:
1725
1726         inf->fb0_color_deepth = arg;
1727
1728             break;
1729         case FBIOGET_16OR32:
1730             return  inf->fb0_color_deepth;
1731         case FBIOGET_IDLEFBUff_16OR32:
1732         if(info->var.yoffset == 0)
1733         {
1734             return fb0_second_buff_bits;
1735         }
1736         else
1737         {
1738             return fb0_first_buff_bits;
1739         }
1740     case FBIOSET_COMPOSE_LAYER_COUNTS:
1741         fb_compose_layer_count = arg;
1742         break;
1743
1744     case FBIOGET_COMPOSE_LAYER_COUNTS:
1745         
1746         return fb_compose_layer_count;
1747         
1748         case FBIOPUT_FBPHYADD:
1749         return info->fix.smem_start;
1750     case FBIOGET_OVERLAY_STATE:
1751         return inf->video_mode;
1752     case FBIOGET_SCREEN_STATE:
1753         return inf->cur_screen->type;
1754         
1755         case FBIOPUT_SET_CURSOR_EN:
1756                 {
1757                         int en;
1758                         if(copy_from_user(&en, (void*)arg, sizeof(int)))
1759                             return -EFAULT;
1760                         rk29_set_cursor_en(inf, en);
1761                 }
1762                 break;
1763         case FBIOPUT_SET_CURSOR_POS:
1764                 {
1765                         struct fbcurpos pos;
1766                         if(copy_from_user(&pos, (void*)arg, sizeof(struct fbcurpos)))
1767                             return -EFAULT;
1768                         rk29_set_cursor_pos(inf, pos.x , pos.y);
1769                 }
1770                 break;
1771         case FBIOPUT_SET_CURSOR_IMG:
1772                 {
1773                         char cursor_buf[CURSOR_BUF_SIZE];
1774                         if(copy_from_user(cursor_buf, (void*)arg, CURSOR_BUF_SIZE))
1775                                 return -EFAULT;
1776                         rk29_set_cursor_img(inf, cursor_buf);
1777                 }
1778                 break;
1779         case FBIOPUT_SET_CURSOR_CMAP:
1780                 {
1781                         struct fb_image img;
1782                         if(copy_from_user(&img, (void*)arg, sizeof(struct fb_image)))
1783                             return -EFAULT;
1784                         rk29_set_cursor_colour_map(inf, img.bg_color, img.fg_color);
1785                 }
1786                 break;
1787         case FBIOPUT_GET_CURSOR_RESOLUTION:
1788                 {
1789             u32 panel_size[2];
1790                         //struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1791              if(inf->fb1->var.rotate == 270) {
1792                 panel_size[0] = inf->cur_screen->y_res; //inf->cur_screen->y_res; change for hdmi video size
1793                 panel_size[1] = inf->cur_screen->x_res;
1794             } else {
1795                 panel_size[0] = inf->cur_screen->x_res;
1796                 panel_size[1] = inf->cur_screen->y_res;
1797             }
1798             if(copy_to_user((void*)arg, panel_size, 8))  return -EFAULT;
1799             break;
1800                 }
1801         case FBIOPUT_GET_CURSOR_EN:
1802                 {
1803             u32 en = (inf->regbak.SYS_CONFIG & m_HWC_ENABLE);
1804             if(copy_to_user((void*)arg, &en, 4))  return -EFAULT;
1805             break;
1806                 }
1807    default:
1808         break;
1809     }
1810     return 0;
1811 }
1812
1813 static int fb1_blank(int blank_mode, struct fb_info *info)
1814 {
1815     win0_blank(blank_mode, info);
1816     return 0;
1817 }
1818
1819 static int fb1_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1820 {
1821     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1822     struct rk29fb_screen *screen = inf->cur_screen;
1823
1824     u32 ScaleYRGBY=0x1000;
1825     u16 xpos = (var->nonstd>>8) & 0xfff;   //offset in panel
1826     u16 ypos = (var->nonstd>>20) & 0xfff;
1827     u16 xsize = (var->grayscale>>8) & 0xfff;   //visiable size in panel
1828     u16 ysize = (var->grayscale>>20) & 0xfff;
1829     u16 xlcd = screen->x_res;        //size of panel
1830     u16 ylcd = screen->y_res;
1831     u16 yres = 0;
1832 #if 0
1833         struct hdmi *hdmi = get_hdmi_struct(0);
1834 #endif
1835
1836     if((var->rotate == 270)||(var->rotate == 90)) {
1837       #ifdef CONFIG_FB_ROTATE_VIDEO
1838         xlcd = screen->y_res;
1839         ylcd = screen->x_res;
1840       #else //CONFIG_FB_ROTATE_VIDEO
1841         printk("LCDC not support rotate!\n");
1842         return -EINVAL;
1843       #endif
1844     }
1845
1846     xpos = (xpos * screen->x_res) / inf->panel1_info.x_res;
1847     ypos = (ypos * screen->y_res) / inf->panel1_info.y_res;
1848     xsize = (xsize * screen->x_res) / inf->panel1_info.x_res;
1849     ysize = (ysize * screen->y_res) / inf->panel1_info.y_res;
1850
1851     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1852
1853         CHK_SUSPEND(inf);
1854
1855     if( 0==var->xres_virtual || 0==var->yres_virtual ||
1856         0==var->xres || 0==var->yres || var->xres<16 ||
1857         0==xsize || 0==ysize || xsize<16 ||
1858         ((16!=var->bits_per_pixel)&&(32!=var->bits_per_pixel)) )
1859     {
1860         printk(">>>>>> win0fb_check_var fail 1!!! \n");
1861                 printk("0==%d || 0==%d || 0==%d || 0==%d || %d<16 \n ||0==%d || 0==%d || %d<16 ||((16!=%d)&&(32!=%d)) \n",
1862                                 var->xres_virtual, var->yres_virtual, var->xres, var->yres, var->xres, xsize, ysize, xsize,
1863                         var->bits_per_pixel, var->bits_per_pixel);
1864         return -EINVAL;
1865     }
1866
1867     if( (var->xoffset+var->xres)>var->xres_virtual ||
1868         (var->yoffset+var->yres)>var->yres_virtual ||
1869         (xpos+xsize)>xlcd || (ypos+ysize)>ylcd  )
1870     {
1871         printk(">>>>>> win0fb_check_var fail 2!!! \n");
1872                 printk("(%d+%d)>%d || (%d+%d)>%d || (%d+%d)>%d || (%d+%d)>%d \n ",
1873                                 var->xoffset, var->xres, var->xres_virtual, var->yoffset, var->yres,
1874                                 var->yres_virtual, xpos, xsize, xlcd, ypos, ysize, ylcd);       
1875         return -EINVAL;
1876     }
1877
1878     switch(var->nonstd&0x0f)
1879     {
1880     case 0: // rgb
1881         switch(var->bits_per_pixel)
1882         {
1883         case 16:    // rgb565
1884             var->xres_virtual = (var->xres_virtual + 0x1) & (~0x1);
1885             var->xres = (var->xres + 0x1) & (~0x1);
1886             var->xoffset = (var->xoffset) & (~0x1);
1887             break;
1888         default:    // rgb888
1889             var->bits_per_pixel = 32;
1890             break;
1891         }
1892         var->nonstd &= ~0xc0;  //not support I2P in this format
1893         break;
1894     case 1: // yuv422
1895         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
1896         var->xres = (var->xres + 0x3) & (~0x3);
1897         var->xoffset = (var->xoffset) & (~0x3);
1898         break;
1899     case 2: // yuv4200
1900         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
1901         var->yres_virtual = (var->yres_virtual + 0x1) & (~0x1);
1902         var->xres = (var->xres + 0x3) & (~0x3);
1903         var->yres = (var->yres + 0x1) & (~0x1);
1904         var->xoffset = (var->xoffset) & (~0x3);
1905         var->yoffset = (var->yoffset) & (~0x1);
1906         break;
1907     case 3: // yuv4201
1908         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
1909         var->yres_virtual = (var->yres_virtual + 0x1) & (~0x1);
1910         var->xres = (var->xres + 0x3) & (~0x3);
1911         var->yres = (var->yres + 0x1) & (~0x1);
1912         var->xoffset = (var->xoffset) & (~0x3);
1913         var->yoffset = (var->yoffset) & (~0x1);
1914         var->nonstd &= ~0xc0;   //not support I2P in this format
1915         break;
1916     case 4: // none
1917     case 5: // yuv444
1918         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
1919         var->xres = (var->xres + 0x3) & (~0x3);
1920         var->xoffset = (var->xoffset) & (~0x3);
1921         var->nonstd &= ~0xc0;   //not support I2P in this format
1922         break;
1923     default:
1924         printk(">>>>>> fb1 var->nonstd=%d is invalid! \n", var->nonstd);
1925         return -EINVAL;
1926     }
1927
1928     if((var->rotate == 270)||(var->rotate == 90))
1929      {
1930          yres = var->xres;
1931      }
1932      else
1933      {
1934          yres = var->yres;
1935      }
1936
1937     ScaleYRGBY = CalScaleW0(yres, ysize);
1938
1939     if((ScaleYRGBY>0x8000) || (ScaleYRGBY<0x200))
1940     {
1941         return -EINVAL;        // multiple of scale down or scale up can't exceed 8
1942     }
1943 #if 0
1944         if(inf->video_mode == 1) {
1945                 if(hdmi_resolution_changed(hdmi,var->xres,var->yres, 1) == 1)
1946                 {
1947                         LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(1));
1948                 LcdWrReg(inf, REG_CFG_DONE, 0x01);
1949                         init_completion(&hdmi->complete);
1950                         hdmi->wait = 1;
1951                         wait_for_completion_interruptible_timeout(&hdmi->complete,
1952                                                                 msecs_to_jiffies(10000));
1953                 }
1954         }
1955 #endif
1956     return 0;
1957 }
1958
1959 static int fb1_set_par(struct fb_info *info)
1960 {
1961     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1962     struct rk29fb_screen *screen = inf->cur_screen;
1963     struct fb_var_screeninfo *var = &info->var;
1964     struct fb_fix_screeninfo *fix = &info->fix;
1965     struct win0_par *par = info->par;
1966
1967     u8 format = 0;
1968     u32 cblen=0, crlen=0, map_size=0, smem_len=0;
1969
1970         //u32 xact = var->xres;                     /* visible resolution               */
1971         //u32 yact = var->yres;
1972         u32 xvir = var->xres_virtual;           /* virtual resolution           */
1973         u32 yvir = var->yres_virtual;
1974         u32 xact_st = var->xoffset;                     /* offset from virtual to visible */
1975         u32 yact_st = var->yoffset;                     /* resolution                   */
1976
1977     u16 xpos = (var->nonstd>>8) & 0xfff;      //visiable pos in panel
1978     u16 ypos = (var->nonstd>>20) & 0xfff;
1979     u16 xsize = (var->grayscale>>8) & 0xfff;  //visiable size in panel
1980     u16 ysize = (var->grayscale>>20) & 0xfff;
1981
1982     //u32 ScaleYrgbX=0x1000,ScaleYrgbY=0x1000;
1983     //u32 ScaleCbrX=0x1000, ScaleCbrY=0x1000;
1984
1985     u8 data_format = var->nonstd&0x0f;
1986    // u32 win0_en = var->reserved[2];
1987    // u32 y_addr = var->reserved[3];       //user alloc buf addr y
1988    // u32 uv_addr = var->reserved[4];
1989
1990     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1991
1992         CHK_SUSPEND(inf);
1993     if((var->rotate == 270)||(var->rotate == 90))
1994     {
1995         #ifdef CONFIG_FB_ROTATE_VIDEO
1996         xpos = (var->nonstd>>20) & 0xfff;      //visiable pos in panel
1997         ypos = (var->nonstd>>8) & 0xfff;
1998         xsize = (var->grayscale>>20) & 0xfff;  //visiable size in panel
1999         ysize = (var->grayscale>>8) & 0xfff;
2000         #else //CONFIG_FB_ROTATE_VIDEO
2001         printk("LCDC not support rotate!\n");
2002         return -EINVAL;
2003       #endif
2004     } else{
2005         xpos = (xpos * screen->x_res) / inf->panel1_info.x_res;
2006         ypos = (ypos * screen->y_res) / inf->panel1_info.y_res;
2007         xsize = (xsize * screen->x_res) / inf->panel1_info.x_res;
2008         ysize = (ysize * screen->y_res) / inf->panel1_info.y_res;
2009     }
2010         /* calculate y_offset,c_offset,line_length,cblen and crlen  */
2011     switch (data_format)
2012     {
2013     case 0: // rgb
2014         switch(var->bits_per_pixel)
2015         {
2016         case 16:    // rgb565
2017             format = 1;
2018             fix->line_length = 2 * xvir;
2019             par->y_offset = (yact_st*xvir + xact_st)*2;
2020             break;
2021         case 32:    // rgb888
2022             format = 0;
2023             fix->line_length = 4 * xvir;
2024             par->y_offset = (yact_st*xvir + xact_st)*4;
2025             break;
2026         default:
2027             return -EINVAL;
2028         }
2029         break;
2030     case 1: // yuv422
2031         format = 2;
2032         fix->line_length = xvir;
2033         cblen = crlen = (xvir*yvir)/2;
2034         par->y_offset = yact_st*xvir + xact_st;
2035         par->c_offset = yact_st*xvir + xact_st;
2036         break;
2037     case 2: // yuv4200
2038         format = 3;
2039         fix->line_length = xvir;
2040         cblen = crlen = (xvir*yvir)/4;
2041
2042         par->y_offset = yact_st*xvir + xact_st;
2043         par->c_offset = (yact_st/2)*xvir + xact_st;
2044
2045         break;
2046     case 3: // yuv4201
2047         format = 4;
2048         fix->line_length = xvir;
2049         par->y_offset = (yact_st/2)*2*xvir + (xact_st)*2;
2050         par->c_offset = (yact_st/2)*xvir + xact_st;
2051         cblen = crlen = (xvir*yvir)/4;
2052         break;
2053     case 4: // none
2054     case 5: // yuv444
2055         format = 5;
2056         fix->line_length = xvir;
2057         par->y_offset = yact_st*xvir + xact_st;
2058         par->c_offset = yact_st*2*xvir + xact_st*2;
2059         cblen = crlen = (xvir*yvir);
2060         break;
2061     default:
2062         return -EINVAL;
2063     }
2064
2065     smem_len = fix->line_length * yvir + cblen + crlen;
2066     map_size = PAGE_ALIGN(smem_len);
2067
2068    // fix->smem_start = y_addr;
2069     fix->smem_len = smem_len;
2070   //  fix->mmio_start = uv_addr;
2071
2072  //   par->addr_seted = ((-1==(int)y_addr) || (0==(int)y_addr) || (win0_en==0)) ? 0 : 1;
2073     fbprintk("buffer alloced by user fix->smem_start = %8x, fix->smem_len = %8x, fix->mmio_start = %8x \n", (u32)fix->smem_start, (u32)fix->smem_len, (u32)fix->mmio_start);
2074
2075     par->format = format;
2076     par->xpos = xpos;
2077     par->ypos = ypos;
2078     par->xsize = xsize;
2079     par->ysize = ysize;
2080     win0_set_par(info);
2081
2082     if ( wq_condition2 == 0 ) {
2083         wait_event_interruptible_timeout(wq,  wq_condition2, HZ/20);
2084     }
2085     wq_condition2 = 0;
2086
2087 #ifdef CONFIG_FB_ROTATE_VIDEO
2088 //need refresh  ,zyc add                
2089         if((has_set_rotate == true) && (last_yuv_phy[0] != 0) && (last_yuv_phy[1] != 0))
2090         {
2091                 u32 yuv_phy[2];
2092                 struct rk29_ipp_req ipp_req;
2093                 static u32 dstoffset = 0;
2094                 static int fb_index = 0;
2095                 memset(&ipp_req, 0, sizeof(struct rk29_ipp_req));
2096                 yuv_phy[0] = last_yuv_phy[0];
2097                 yuv_phy[1] = last_yuv_phy[1];
2098                 yuv_phy[0] += par->y_offset;
2099                 yuv_phy[1] += par->c_offset;
2100         #if 0
2101                 if((var->rotate == 90) ||(var->rotate == 270))
2102         #else
2103         if(var->rotate%360 != 0)
2104         #endif
2105                         {
2106                                 dstoffset = (dstoffset+1)%2;
2107                                 ipp_req.src0.fmt = 3;
2108                                 ipp_req.src0.YrgbMst = yuv_phy[0];
2109                                 ipp_req.src0.CbrMst = yuv_phy[1];
2110                                 ipp_req.src0.w = var->xres;
2111                                 ipp_req.src0.h = var->yres;
2112                                 
2113                                 ipp_req.src_vir_w= (var->xres + 15) & (~15);
2114                                 ipp_req.dst_vir_w=screen->x_res;
2115
2116                                 ipp_req.dst0.fmt = 3;
2117                                 ipp_req.dst0.YrgbMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*2*dstoffset;
2118                                 ipp_req.dst0.CbrMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*(2*dstoffset+1);
2119                                    if(var->xres > screen->x_res)
2120                                    {
2121                                         ipp_req.dst0.w = screen->x_res;
2122                                         ipp_req.dst0.h = screen->y_res;
2123                                   }   else      {
2124                                           ipp_req.dst0.w = var->xres;
2125                                           ipp_req.dst0.h = var->yres;
2126                                    }
2127                                  ipp_req.dst_vir_w = (ipp_req.dst0.w + 15) & (~15);
2128
2129                                 ipp_req.timeout = 100;
2130                                 if(var->rotate == 90)
2131                                         ipp_req.flag = IPP_ROT_90;
2132                 else if (var->rotate == 180)
2133                                         ipp_req.flag = IPP_ROT_180;
2134                                 else if(var->rotate == 270)
2135                                         ipp_req.flag = IPP_ROT_270;
2136                                 //ipp_do_blit(&ipp_req);
2137                                 ipp_blit_sync(&ipp_req);
2138                                 fbprintk("yaddr=0x%x,uvaddr=0x%x\n",ipp_req.dst0.YrgbMst,ipp_req.dst0.CbrMst);
2139                                 yuv_phy[0] = ipp_req.dst0.YrgbMst;
2140                                 yuv_phy[1] = ipp_req.dst0.CbrMst;        
2141                                 fix->smem_start = yuv_phy[0];
2142                                 fix->mmio_start = yuv_phy[1];
2143                         }
2144                 else
2145                         {
2146                         
2147                                 fix->smem_start = yuv_phy[0];
2148                                 fix->mmio_start = yuv_phy[1];
2149                         }
2150                         
2151                 LcdWrReg(inf, WIN0_YRGB_MST, yuv_phy[0]);
2152                 LcdWrReg(inf, WIN0_CBR_MST, yuv_phy[1]);
2153                 // enable win0 after the win0 par is seted
2154                 LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(par->par_seted && par->addr_seted));
2155                 par->addr_seted = 1;
2156                 LcdWrReg(inf, REG_CFG_DONE, 0x01);
2157                 if(par->addr_seted ) {
2158                 unsigned long flags;
2159
2160                 local_irq_save(flags);
2161                 par->mirror.y_offset = yuv_phy[0];
2162                 par->mirror.c_offset = yuv_phy[1];
2163                 local_irq_restore(flags);
2164
2165                 mcu_refresh(inf);
2166                 //printk("0x%.8x 0x%.8x mirror\n", par->mirror.y_offset, par->mirror.c_offset);
2167                 }
2168
2169         }
2170
2171     has_set_rotate = false;
2172 #endif
2173     return 0;
2174 }
2175
2176 static int fb1_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
2177 {
2178     struct win0_par *par = info->par;
2179      // enable win0 after the win0 addr is seted
2180
2181     win0_pan(info);
2182     par->par_seted = 1;
2183     return 0;
2184 }
2185
2186 int fb1_open(struct fb_info *info, int user)
2187 {
2188     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
2189     struct win0_par *par = info->par;
2190
2191     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2192
2193     par->par_seted = 0;
2194     par->addr_seted = 0;
2195     inf->video_mode = 1;
2196     wq_condition2 = 1;
2197 #ifdef CONFIG_FB_ROTATE_VIDEO
2198    //reinitialize  the var when open,zyc
2199     last_yuv_phy[0] = 0;
2200     last_yuv_phy[1] = 0;
2201     has_set_rotate = 0;
2202 #endif
2203     if(par->refcount) {
2204         printk(">>>>>> fb1 has opened! \n");
2205         return -EACCES;
2206     } else {
2207         par->refcount++;
2208         win0_blank(FB_BLANK_NORMAL, info);
2209         fb0_set_par(inf->fb0);
2210         
2211             rk29fb_notify(inf, RK29FB_EVENT_FB1_ON);
2212         return 0;
2213     }
2214 }
2215
2216 int fb1_release(struct fb_info *info, int user)
2217 {
2218     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
2219     struct win0_par *par = info->par;
2220         struct fb_var_screeninfo *var0 = &info->var;
2221
2222     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2223     if(inf->setFlag == 0)
2224     {
2225         wait_event_interruptible_timeout(wq, inf->setFlag, HZ*10);
2226     } 
2227     if(par->refcount) {
2228         par->refcount--;
2229         inf->video_mode = 0;
2230         par->par_seted = 0;
2231         par->addr_seted = 0;
2232         win1_blank(FB_BLANK_NORMAL, info);
2233         if(inf->cur_screen->type != SCREEN_HDMI)
2234             fb0_set_par(inf->fb0);
2235
2236         // unmap memory
2237         info->screen_base = 0;
2238         info->fix.smem_start = 0;
2239         info->fix.smem_len = 0;
2240                 // clean the var param
2241                 memset(var0, 0, sizeof(struct fb_var_screeninfo));
2242             rk29fb_notify(inf, RK29FB_EVENT_FB1_OFF);
2243         #ifdef CONFIG_CLOSE_WIN1_DYNAMIC   
2244          cancel_delayed_work_sync(&rk29_win1_check_work);
2245         #endif  
2246     }
2247
2248     return 0;
2249 }
2250
2251 static int fb1_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
2252 {
2253     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
2254     struct win0_par *par = info->par;
2255     struct fb_fix_screeninfo *fix0 = &info->fix;
2256     struct fb_var_screeninfo *var = &info->var;
2257     void __user *argp = (void __user *)arg;
2258     
2259 #ifdef CONFIG_FB_ROTATE_VIDEO   
2260      struct rk29fb_screen *screen = inf->cur_screen;
2261      struct rk29_ipp_req ipp_req;
2262      static u32 dstoffset = 0;
2263     memset(&ipp_req, 0, sizeof(struct rk29_ipp_req)); 
2264 #endif
2265
2266         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2267     fbprintk("win0fb_ioctl cmd = %8x, arg = %8x \n", (u32)cmd, (u32)arg);
2268
2269         CHK_SUSPEND(inf);
2270
2271     switch(cmd)
2272     {
2273     case FB1_IOCTL_GET_PANEL_SIZE:    //get panel size
2274         {
2275             u32 panel_size[2];
2276              if((var->rotate == 270)||(var->rotate == 90)) {
2277                 panel_size[0] = inf->panel1_info.y_res; //inf->cur_screen->y_res; change for hdmi video size
2278                 panel_size[1] = inf->panel1_info.x_res;
2279             } else {
2280                 panel_size[0] = inf->panel1_info.x_res;
2281                 panel_size[1] = inf->panel1_info.y_res;
2282             }
2283
2284             if(copy_to_user(argp, panel_size, 8))  return -EFAULT;
2285         }
2286         break;
2287
2288     case FB1_IOCTL_SET_YUV_ADDR:    //set y&uv address to register direct
2289         {
2290             u32 yuv_phy[2];
2291             if (copy_from_user(yuv_phy, argp, 8))
2292                             return -EFAULT;
2293 #ifdef CONFIG_FB_ROTATE_VIDEO 
2294                 //add by zyc
2295                 if(has_set_rotate == true)
2296                 break;
2297                 last_yuv_phy[0] = yuv_phy[0];
2298                 last_yuv_phy[1] = yuv_phy[1];
2299 #endif
2300
2301             fix0->smem_start = yuv_phy[0];
2302             fix0->mmio_start = yuv_phy[1];
2303             yuv_phy[0] += par->y_offset;
2304             yuv_phy[1] += par->c_offset;
2305          
2306             #if 0
2307                 if((var->rotate == 90) ||(var->rotate == 270))
2308             #else
2309             if(var->rotate%360 != 0)
2310             #endif
2311             {
2312                 #ifdef CONFIG_FB_ROTATE_VIDEO 
2313                 dstoffset = (dstoffset+1)%2;
2314                 ipp_req.src0.fmt = 3;
2315                 ipp_req.src0.YrgbMst = yuv_phy[0];
2316                 ipp_req.src0.CbrMst = yuv_phy[1];
2317                     ipp_req.src0.w = var->xres ;
2318                 ipp_req.src0.h = var->yres ;
2319                         ipp_req.src_vir_w= (var->xres + 15) & (~15);
2320                         ipp_req.dst_vir_w=screen->x_res;
2321
2322                 ipp_req.dst0.fmt = 3;
2323                 ipp_req.dst0.YrgbMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*2*dstoffset;
2324                 ipp_req.dst0.CbrMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*(2*dstoffset+1);
2325                 if(var->xres > screen->x_res)
2326                 {
2327                     ipp_req.dst0.w = screen->x_res;
2328                     ipp_req.dst0.h = screen->y_res;
2329                 }   else  {
2330                                         ipp_req.dst0.w = var->xres;
2331                                         ipp_req.dst0.h = var->yres;
2332
2333                 }
2334                 ipp_req.dst_vir_w = (ipp_req.dst0.w + 15) & (~15);
2335                 ipp_req.timeout = 100;
2336                 if(var->rotate == 90)
2337                     ipp_req.flag = IPP_ROT_90;
2338                 else if(var->rotate == 180)
2339                     ipp_req.flag = IPP_ROT_180;
2340                 else if(var->rotate == 270)
2341                     ipp_req.flag = IPP_ROT_270;
2342                 //ipp_do_blit(&ipp_req);
2343                 ipp_blit_sync(&ipp_req);
2344                                 
2345                 fbprintk("yaddr=0x%x,uvaddr=0x%x\n",ipp_req.dst0.YrgbMst,ipp_req.dst0.CbrMst);
2346                 yuv_phy[0] = ipp_req.dst0.YrgbMst;
2347                 yuv_phy[1] = ipp_req.dst0.CbrMst;    
2348                 fix0->smem_start = yuv_phy[0];
2349                 fix0->mmio_start = yuv_phy[1];
2350                 #else //CONFIG_FB_ROTATE_VIDEO
2351                 printk("LCDC not support rotate!\n");
2352                 #endif
2353             }
2354       
2355             LcdWrReg(inf, WIN0_YRGB_MST, yuv_phy[0]);
2356             LcdWrReg(inf, WIN0_CBR_MST, yuv_phy[1]);
2357             // enable win0 after the win0 par is seted
2358             LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(par->par_seted && par->addr_seted));
2359             par->addr_seted = 1;
2360             LcdWrReg(inf, REG_CFG_DONE, 0x01);
2361             if(par->par_seted) {
2362                 unsigned long flags;
2363
2364                 local_irq_save(flags);
2365                 par->mirror.y_offset = yuv_phy[0];
2366                 par->mirror.c_offset = yuv_phy[1];
2367                 local_irq_restore(flags);
2368
2369                 mcu_refresh(inf);
2370                 //printk("0x%.8x 0x%.8x mirror\n", par->mirror.y_offset, par->mirror.c_offset);
2371             }
2372         }
2373         break;
2374
2375     case FB1_IOCTL_SET_ROTATE:    //change MCU panel scan direction
2376         fbprintk(">>>>>> change lcdc direction(%d) \n", (int)arg);
2377       #ifdef CONFIG_FB_ROTATE_VIDEO 
2378                 //zyc add
2379         has_set_rotate = true;
2380         if(arg == 0 || arg==180)
2381             var->rotate = arg;    
2382         else if (arg == 90 || arg==270)
2383             var->rotate = arg;  
2384       #else //CONFIG_FB_ROTATE_VIDEO
2385         printk("LCDC not support rotate!\n");
2386       #endif
2387         break;
2388     case FB1_IOCTL_SET_WIN0_TOP:
2389         fbprintk(">>>>>> FB1_IOCTL_SET_WIN0_TOP %d\n",arg);
2390         LcdMskReg(inf, DSP_CTRL0, m_W0_ON_TOP, v_W0_ON_TOP(arg));
2391         LcdWrReg(inf, REG_CFG_DONE, 0x01);
2392         break;
2393     default:
2394         break;
2395     }
2396     return 0;
2397 }
2398
2399 static struct fb_ops fb1_ops = {
2400         .owner          = THIS_MODULE,
2401         .fb_open    = fb1_open,
2402         .fb_release = fb1_release,
2403         .fb_check_var   = fb1_check_var,
2404         .fb_set_par     = fb1_set_par,
2405         .fb_blank       = fb1_blank,
2406     .fb_pan_display = fb1_pan_display,
2407     .fb_ioctl = fb1_ioctl,
2408         .fb_setcolreg   = fb_setcolreg,
2409         .fb_fillrect    = cfb_fillrect,
2410         .fb_copyarea    = cfb_copyarea,
2411         .fb_imageblit   = cfb_imageblit,
2412 };
2413
2414 static struct fb_ops fb0_ops = {
2415         .owner          = THIS_MODULE,
2416         .fb_check_var   = fb0_check_var,
2417         .fb_set_par = fb0_set_par,
2418         .fb_blank   = fb0_blank,
2419         .fb_pan_display = fb0_pan_display,
2420     .fb_ioctl = fb0_ioctl,
2421         .fb_setcolreg   = fb_setcolreg,
2422         .fb_fillrect    = cfb_fillrect,
2423         .fb_copyarea    = cfb_copyarea,
2424         .fb_imageblit   = cfb_imageblit,
2425         //.fb_cursor      = rk29_set_cursor,
2426 };
2427
2428 int fb_get_video_mode(void)
2429 {
2430         struct rk29fb_inf *inf;
2431         if(!g_pdev)
2432                 return 0;
2433         inf = platform_get_drvdata(g_pdev);
2434         return inf->video_mode;
2435 }
2436 /*
2437 enable: 1, switch to tv or hdmi; 0, switch to lcd
2438 */
2439 int FB_Switch_Screen( struct rk29fb_screen *screen, u32 enable )
2440 {
2441     struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
2442    // struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2443     struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2444
2445     memcpy(&inf->panel2_info, screen, sizeof( struct rk29fb_screen ));
2446
2447     if(enable)inf->cur_screen = &inf->panel2_info;
2448     else inf->cur_screen = &inf->panel1_info;
2449
2450     /* Black out, because some display device need clock to standby */
2451     //LcdMskReg(inf, DSP_CTRL_REG1, m_BLACK_OUT, v_BLACK_OUT(1));
2452    // LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(0));
2453    // LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
2454     LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(1));
2455     LcdWrReg(inf, REG_CFG_DONE, 0x01);
2456     wake_lock(&idlelock);
2457     msleep(20);
2458     wake_unlock(&idlelock);
2459
2460     if(inf->cur_screen->standby)    inf->cur_screen->standby(1);
2461     // operate the display_on pin to power down the lcd
2462    
2463     if(enable && mach_info->io_disable)mach_info->io_disable();  //close lcd out
2464     else if (mach_info->io_enable)mach_info->io_enable();       //open lcd out
2465     
2466     load_screen(inf->fb0, 0);
2467         mcu_refresh(inf);
2468
2469     fb1_set_par(inf->fb1);
2470     fb0_set_par(inf->fb0);
2471     LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(0));
2472     LcdWrReg(inf, REG_CFG_DONE, 0x01);
2473     rk29fb_notify(inf, enable ? RK29FB_EVENT_HDMI_ON : RK29FB_EVENT_HDMI_OFF);
2474     return 0;
2475 }
2476
2477 static ssize_t dsp_win0_info_read(struct device *device,
2478                             struct device_attribute *attr, char *buf)
2479 {
2480     //char * s = _buf;
2481     struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
2482     struct rk29fb_screen *screen = inf->cur_screen;
2483
2484     u16 xpos=0,ypos=0,xsize=0,ysize=0;
2485
2486     fbprintk("%s\n",__FUNCTION__);
2487     xpos = LcdRdReg(inf, WIN0_DSP_ST) & 0xffff;
2488     ypos = (LcdRdReg(inf, WIN0_DSP_ST)>>16) & 0xffff;
2489
2490     xpos -= (screen->left_margin + screen->hsync_len);
2491     ypos -= (screen->upper_margin + screen->vsync_len);
2492
2493     xsize=LcdRdReg(inf, WIN0_DSP_INFO)& 0xffff;
2494     ysize=(LcdRdReg(inf, WIN0_DSP_INFO)>>16) & 0xffff;
2495     fbprintk("%s %d , %d, %d ,%d\n",__FUNCTION__,xpos,ypos,xsize,ysize);
2496         rk29_set_cursor_test(inf->fb0);
2497
2498     return snprintf(buf, PAGE_SIZE, "%d,%d,%d,%d\n", xpos,ypos,xsize,ysize);
2499 }
2500
2501 static ssize_t dsp_win0_info_write(struct device *device,
2502                            struct device_attribute *attr, const char *buf, size_t count)
2503 {
2504      printk("%s\n",__FUNCTION__);
2505      printk("%s %x \n",__FUNCTION__,*buf);
2506      return count;
2507 }
2508
2509 static DEVICE_ATTR(dsp_win0_info,  S_IRUGO|S_IWUSR, dsp_win0_info_read, dsp_win0_info_write);
2510
2511 static irqreturn_t rk29fb_irq(int irq, void *dev_id)
2512 {
2513         struct platform_device *pdev = (struct platform_device*)dev_id;
2514     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
2515     struct win0_par *par = (struct win0_par *)inf->fb1->par;
2516     if(!inf)
2517         return IRQ_HANDLED;
2518
2519         //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2520
2521     LcdMskReg(inf, INT_STATUS, m_FRM_STARTCLEAR, v_FRM_STARTCLEAR(1));
2522
2523         if(SCREEN_MCU == inf->cur_screen->type)
2524         {
2525         inf->mcu_isrcnt = !inf->mcu_isrcnt;
2526         if(inf->mcu_isrcnt)
2527             return IRQ_HANDLED;
2528
2529         if(IsMcuUseFmk())
2530         {
2531             if(LcdReadBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST) && (inf->mcu_fmksync == 0))
2532             {
2533                 inf->mcu_fmksync = 1;
2534                  if(inf->cur_screen->refresh)
2535                    inf->cur_screen->refresh(REFRESH_END);
2536                 inf->mcu_fmksync = 0;
2537             }
2538             else
2539             {
2540                 return IRQ_HANDLED;
2541             }
2542         }
2543         else
2544         {
2545             if(inf->mcu_needflush) {
2546                 if(inf->cur_screen->refresh)
2547                     inf->cur_screen->refresh(REFRESH_PRE);
2548                 inf->mcu_needflush = 0;
2549                 inf->mcu_isrcnt = 0;
2550                 LcdSetRegisterBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST);
2551             } else {
2552                 if(inf->cur_screen->refresh)
2553                     inf->cur_screen->refresh(REFRESH_END);
2554             }
2555         }
2556         }
2557 #if ANDROID_USE_THREE_BUFS
2558     new_frame_seted = 1;
2559 #endif
2560
2561     if(waitqueue_active(&par->wait)) {
2562         if (par->mirror.c_offset == 0)
2563             printk("error: no new buffer to display\n");
2564
2565         par->done.y_offset = par->displ.y_offset;
2566         par->done.c_offset = par->displ.c_offset;
2567         par->displ.y_offset = par->mirror.y_offset;
2568         par->displ.c_offset = par->mirror.c_offset;
2569         par->mirror.y_offset = 0;
2570         par->mirror.c_offset = 0;
2571
2572         //printk("0x%.8x 0x%.8x displaying\n", par->displ.y_offset, par->displ.c_offset);
2573         //printk("0x%.8x 0x%.8x done\n", par->done.y_offset, par->done.c_offset);
2574         wake_up_interruptible(&par->wait);
2575     }
2576
2577     wq_condition2 = 1;
2578         wq_condition = 1;
2579         wake_up_interruptible(&wq);
2580
2581         rk29fb_irq_notify_ddr();
2582         return IRQ_HANDLED;
2583 }
2584
2585 #ifdef CONFIG_HAS_EARLYSUSPEND
2586
2587 struct suspend_info {
2588         struct early_suspend early_suspend;
2589         struct rk29fb_inf *inf;
2590 };
2591
2592 static void rk29fb_early_suspend(struct early_suspend *h)
2593 {
2594         struct suspend_info *info = container_of(h, struct suspend_info,
2595                                                 early_suspend);
2596
2597     struct rk29fb_inf *inf = info->inf;
2598     struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2599
2600     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2601
2602     if(!inf) {
2603         printk("inf==0, rk29fb_suspend fail! \n");
2604         return;
2605     }
2606
2607 #ifdef CONFIG_CLOSE_WIN1_DYNAMIC   
2608      cancel_delayed_work_sync(&rk29_win1_check_work);
2609 #endif  
2610
2611     if((inf->cur_screen != &inf->panel2_info) && mach_info->io_disable)  // close lcd pwr when output screen is lcd
2612        mach_info->io_disable();  //close lcd out 
2613
2614         if(inf->cur_screen->standby)
2615         {
2616                 fbprintk(">>>>>> power down the screen! \n");
2617                 inf->cur_screen->standby(1);
2618         }
2619
2620     LcdMskReg(inf, DSP_CTRL0, m_HSYNC_POLARITY | m_VSYNC_POLARITY | m_DEN_POLARITY ,
2621        v_HSYNC_POLARITY(1) | v_VSYNC_POLARITY(1) | v_DEN_POLARITY(1) );
2622
2623     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(1));
2624     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(1));
2625         LcdWrReg(inf, REG_CFG_DONE, 0x01);
2626
2627         if(!inf->in_suspend)
2628         {
2629                 fbprintk(">>>>>> diable the lcdc clk! \n");
2630                 wake_lock(&idlelock);
2631                 msleep(100);
2632                 wake_unlock(&idlelock);
2633         clk_disable(inf->aclk_ddr_lcdc);
2634         clk_disable(inf->aclk_disp_matrix);
2635         clk_disable(inf->hclk_cpu_display);
2636         clk_disable(inf->clk);
2637         if (inf->dclk){
2638             clk_disable(inf->dclk);
2639         }
2640         if(inf->clk){
2641             clk_disable(inf->aclk);
2642         }
2643         //clk_disable(inf->pd_display);
2644
2645                 inf->in_suspend = 1;
2646         }
2647 }
2648
2649 static void rk29fb_early_resume(struct early_suspend *h)
2650 {
2651         struct suspend_info *info = container_of(h, struct suspend_info,
2652                                         early_suspend);
2653
2654     struct rk29fb_inf *inf = info->inf;
2655     struct rk29fb_screen *screen = inf->cur_screen;
2656     struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2657
2658     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2659     if(!inf) {
2660         printk("inf==0, rk29fb_resume fail! \n");
2661         return ;
2662     }
2663
2664     if(inf->in_suspend)
2665         {
2666             inf->in_suspend = 0;
2667         fbprintk(">>>>>> enable the lcdc clk! \n");
2668        // clk_enable(inf->pd_display);
2669         clk_enable(inf->aclk_disp_matrix);
2670         clk_enable(inf->hclk_cpu_display);
2671         clk_enable(inf->clk);
2672         clk_enable(inf->aclk_ddr_lcdc);
2673
2674         if (inf->dclk){
2675             clk_enable(inf->dclk);
2676         }
2677         if(inf->clk){
2678             clk_enable(inf->aclk);
2679         }
2680         usleep_range(100*1000, 100*1000);
2681         }
2682     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(0));
2683     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(0));
2684     LcdWrReg(inf, REG_CFG_DONE, 0x01);
2685
2686     LcdMskReg(inf, DSP_CTRL0, m_HSYNC_POLARITY | m_VSYNC_POLARITY | m_DEN_POLARITY ,
2687        v_HSYNC_POLARITY(screen->pin_hsync) | v_VSYNC_POLARITY(screen->pin_vsync) | v_DEN_POLARITY(screen->pin_den) );
2688
2689         if(inf->cur_screen->standby)
2690         {
2691                 fbprintk(">>>>>> power on the screen! \n");
2692                 inf->cur_screen->standby(0);
2693         }
2694     usleep_range(10*1000, 10*1000);
2695     memcpy((u8*)inf->preg, (u8*)&inf->regbak, 0xa4);  //resume reg
2696     usleep_range(40*1000, 40*1000);
2697     
2698     if((inf->cur_screen != &inf->panel2_info) && mach_info->io_enable)  // open lcd pwr when output screen is lcd
2699        mach_info->io_enable();  //close lcd out 
2700         
2701 }
2702
2703 static struct suspend_info suspend_info = {
2704         .early_suspend.suspend = rk29fb_early_suspend,
2705         .early_suspend.resume = rk29fb_early_resume,
2706         .early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB,
2707 };
2708 #endif
2709 struct fb_info *g_fb0_inf = NULL;  //add cym@rk 20101027 for charger logo
2710 static int __init rk29fb_probe (struct platform_device *pdev)
2711 {
2712     struct rk29fb_inf *inf = NULL;
2713     struct resource *res = NULL;
2714     struct resource *mem = NULL;
2715     struct rk29fb_info *mach_info = NULL;
2716     struct rk29fb_screen *screen = NULL;
2717     struct win0_par* par = NULL;
2718         int irq = 0;
2719     int ret = 0;
2720
2721     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2722
2723     /* Malloc rk29fb_inf and set it to pdev for drvdata */
2724     fbprintk(">> Malloc rk29fb_inf and set it to pdev for drvdata \n");
2725     inf = kmalloc(sizeof(struct rk29fb_inf), GFP_KERNEL);
2726     if(!inf)
2727     {
2728         dev_err(&pdev->dev, ">> inf kmalloc fail!");
2729         ret = -ENOMEM;
2730                 goto release_drvdata;
2731     }
2732     memset(inf, 0, sizeof(struct rk29fb_inf));
2733         platform_set_drvdata(pdev, inf);
2734
2735     mach_info = pdev->dev.platform_data;
2736     /* Fill screen info and set current screen */
2737     fbprintk(">> Fill screen info and set current screen \n");
2738    #ifdef CONFIG_DEFAULT_OUT_HDMI  // set hdmi for default output 
2739     hdmi_get_default_resolution(&inf->panel1_info);
2740    #else
2741     set_lcd_info(&inf->panel1_info, mach_info->lcd_info);
2742    #endif
2743     inf->cur_screen = &inf->panel1_info;
2744     screen = inf->cur_screen;
2745     if(SCREEN_NULL==screen->type)
2746     {
2747         dev_err(&pdev->dev, ">> Please select a display device! \n");
2748         ret = -EINVAL;
2749                 goto release_drvdata;
2750     }
2751
2752     /* get virtual basic address of lcdc register */
2753     fbprintk(">> get virtual basic address of lcdc register \n");
2754     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcdc reg");
2755     if (res == NULL)
2756     {
2757         dev_err(&pdev->dev, "failed to get memory registers\n");
2758         ret = -ENOENT;
2759                 goto release_drvdata;
2760     }
2761     inf->reg_phy_base = res->start;
2762     inf->len = (res->end - res->start) + 1;
2763     mem = request_mem_region(inf->reg_phy_base, inf->len, pdev->name);
2764     if (mem == NULL)
2765     {
2766         dev_err(&pdev->dev, "failed to get memory region\n");
2767         ret = -ENOENT;
2768                 goto release_drvdata;
2769     }
2770     fbprintk("inf->reg_phy_base = 0x%08x, inf->len = %d \n", inf->reg_phy_base, inf->len);
2771     inf->reg_vir_base = ioremap(inf->reg_phy_base, inf->len);
2772     if (inf->reg_vir_base == NULL)
2773     {
2774         dev_err(&pdev->dev, "ioremap() of registers failed\n");
2775         ret = -ENXIO;
2776                 goto release_drvdata;
2777     }
2778     inf->preg = (LCDC_REG*)inf->reg_vir_base;
2779
2780     /* Prepare win1 info */
2781     fbprintk(">> Prepare win1 info \n");
2782         inf->fb0 = framebuffer_alloc(sizeof(struct win0_par), &pdev->dev);
2783     if(!inf->fb0)
2784     {
2785         dev_err(&pdev->dev, ">> fb0 framebuffer_alloc fail!");
2786                 inf->fb0 = NULL;
2787         ret = -ENOMEM;
2788                 goto release_win1fb;
2789     }
2790
2791     par = (struct win0_par*)inf->fb0->par;
2792     strcpy(inf->fb0->fix.id, "fb0");
2793     inf->fb0->fix.type        = FB_TYPE_PACKED_PIXELS;
2794     inf->fb0->fix.type_aux    = 0;
2795     inf->fb0->fix.xpanstep    = 1;
2796     inf->fb0->fix.ypanstep    = 1;
2797     inf->fb0->fix.ywrapstep   = 0;
2798     inf->fb0->fix.accel       = FB_ACCEL_NONE;
2799     inf->fb0->fix.visual      = FB_VISUAL_TRUECOLOR;
2800     inf->fb0->fix.smem_len    = 0;
2801     inf->fb0->fix.line_length = 0;
2802     inf->fb0->fix.smem_start  = 0;
2803
2804     inf->fb0->var.xres = screen->x_res;
2805     inf->fb0->var.yres = screen->y_res;
2806     inf->fb0->var.bits_per_pixel = 16;
2807     inf->fb0_color_deepth = 0;
2808     inf->fb0->var.xres_virtual = screen->x_res;
2809     inf->fb0->var.yres_virtual = screen->y_res;
2810     inf->fb0->var.width = screen->width;
2811     inf->fb0->var.height = screen->height;
2812     //inf->fb0->var.pixclock = div_u64(1000000000000llu, screen->pixclock);
2813     inf->fb0->var.left_margin = screen->left_margin;
2814     inf->fb0->var.right_margin = screen->right_margin;
2815     inf->fb0->var.upper_margin = screen->upper_margin;
2816     inf->fb0->var.lower_margin = screen->lower_margin;
2817     inf->fb0->var.vsync_len = screen->vsync_len;
2818     inf->fb0->var.hsync_len = screen->hsync_len;
2819     inf->fb0->var.red    = def_rgb_16.red;
2820     inf->fb0->var.green  = def_rgb_16.green;
2821     inf->fb0->var.blue   = def_rgb_16.blue;
2822     inf->fb0->var.transp = def_rgb_16.transp;
2823
2824     inf->fb0->var.nonstd      = 0;  //win1 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
2825     inf->fb0->var.grayscale   = 0;  //win1 transprent mode & value(mode<<8 + value)
2826     inf->fb0->var.activate    = FB_ACTIVATE_NOW;
2827     inf->fb0->var.accel_flags = 0;
2828     inf->fb0->var.vmode       = FB_VMODE_NONINTERLACED;
2829
2830     inf->fb0->fbops           = &fb0_ops;
2831     inf->fb0->flags           = FBINFO_FLAG_DEFAULT;
2832     inf->fb0->pseudo_palette  = par->pseudo_pal;
2833     inf->fb0->screen_base     = 0;
2834
2835     memset(par, 0, sizeof(struct win0_par));
2836
2837         ret = fb_alloc_cmap(&inf->fb0->cmap, 256, 0);
2838         if (ret < 0)
2839                 goto release_cmap;
2840
2841         g_fb0_inf = inf->fb0; //add cym@rk 20101027
2842
2843     /* alloc win1 buf */
2844     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "win1 buf");
2845     if (res == NULL)
2846     {
2847         dev_err(&pdev->dev, "failed to get win1 memory \n");
2848         ret = -ENOENT;
2849         goto release_win1fb;
2850     }
2851     inf->fb0->fix.smem_start = res->start;
2852     inf->fb0->fix.smem_len = res->end - res->start + 1;
2853     inf->fb0->screen_base = ioremap(res->start, inf->fb0->fix.smem_len);
2854     memset(inf->fb0->screen_base, 0, inf->fb0->fix.smem_len);
2855
2856     #ifdef CONFIG_FB_WORK_IPP
2857        /* alloc win1 ipp buf */
2858     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "win1 ipp buf");
2859     if (res == NULL)
2860     {
2861         dev_err(&pdev->dev, "failed to get win1 ipp memory \n");
2862         ret = -ENOENT;
2863         goto release_win1fb;
2864     }
2865     inf->fb0->fix.mmio_start = res->start;
2866     inf->fb0->fix.mmio_len = res->end - res->start + 1;
2867     #endif
2868
2869     /* Prepare win0 info */
2870     fbprintk(">> Prepare win0 info \n");
2871     inf->fb1 = framebuffer_alloc(sizeof(struct win0_par), &pdev->dev);
2872     if(!inf->fb1)
2873     {
2874         dev_err(&pdev->dev, ">> fb1 framebuffer_alloc fail!");
2875                 inf->fb1 = NULL;
2876                 ret = -ENOMEM;
2877                 goto release_win0fb;
2878     }
2879
2880     par = (struct win0_par*)inf->fb1->par;
2881
2882     strcpy(inf->fb1->fix.id, "fb1");
2883         inf->fb1->fix.type            = FB_TYPE_PACKED_PIXELS;
2884         inf->fb1->fix.type_aux    = 0;
2885         inf->fb1->fix.xpanstep    = 1;
2886         inf->fb1->fix.ypanstep    = 1;
2887         inf->fb1->fix.ywrapstep   = 0;
2888         inf->fb1->fix.accel       = FB_ACCEL_NONE;
2889     inf->fb1->fix.visual      = FB_VISUAL_TRUECOLOR;
2890     inf->fb1->fix.smem_len    = 0;
2891     inf->fb1->fix.line_length = 0;
2892     inf->fb1->fix.smem_start  = 0;
2893
2894     inf->fb1->var.xres = screen->x_res;
2895     inf->fb1->var.yres = screen->y_res;
2896     inf->fb1->var.bits_per_pixel = 16;
2897     inf->fb1->var.xres_virtual = screen->x_res;
2898     inf->fb1->var.yres_virtual = screen->y_res;
2899     inf->fb1->var.width = screen->width;
2900     inf->fb1->var.height = screen->height;
2901     //inf->fb1->var.pixclock = div_u64(1000000000000llu, screen->pixclock);
2902     inf->fb1->var.left_margin = screen->left_margin;
2903     inf->fb1->var.right_margin = screen->right_margin;
2904     inf->fb1->var.upper_margin = screen->upper_margin;
2905     inf->fb1->var.lower_margin = screen->lower_margin;
2906     inf->fb1->var.vsync_len = screen->vsync_len;
2907     inf->fb1->var.hsync_len = screen->hsync_len;
2908     inf->fb1->var.red    = def_rgb_16.red;
2909     inf->fb1->var.green  = def_rgb_16.green;
2910     inf->fb1->var.blue   = def_rgb_16.blue;
2911     inf->fb1->var.transp = def_rgb_16.transp;
2912
2913     inf->fb1->var.nonstd      = 0;  //win0 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
2914     inf->fb1->var.grayscale   = ((inf->fb1->var.yres<<20)&0xfff00000) + ((inf->fb1->var.xres<<8)&0xfff00);//win0 xsize & ysize
2915     inf->fb1->var.activate    = FB_ACTIVATE_NOW;
2916     inf->fb1->var.accel_flags = 0;
2917     inf->fb1->var.vmode       = FB_VMODE_NONINTERLACED;
2918
2919     inf->fb1->fbops           = &fb1_ops;
2920         inf->fb1->flags               = FBINFO_FLAG_DEFAULT;
2921         inf->fb1->pseudo_palette  = par->pseudo_pal;
2922         inf->fb1->screen_base     = 0;
2923
2924     memset(par, 0, sizeof(struct win0_par));
2925
2926         init_waitqueue_head(&par->wait);
2927
2928         /* Init all lcdc and lcd before register_framebuffer. */
2929         /* because after register_framebuffer, the win1fb_check_par and winfb_set_par execute immediately */
2930         fbprintk(">> Init all lcdc and lcd before register_framebuffer \n");
2931     init_lcdc(inf->fb0);
2932
2933   #if 0 //def CONFIG_CPU_FREQ
2934    // inf->freq_transition.notifier_call = rk29fb_freq_transition;
2935    // cpufreq_register_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
2936   #endif
2937         fbprintk("got clock\n");
2938
2939         if(mach_info)
2940     {
2941         struct rk29_fb_setting_info fb_setting;
2942         if( OUT_P888==inf->cur_screen->face )     // set lcdc iomux
2943         {
2944             fb_setting.data_num = 24;
2945         }
2946         else if(OUT_P666 == inf->cur_screen->face )
2947         {
2948             fb_setting.data_num = 18;
2949         }
2950         else
2951         {
2952             fb_setting.data_num = 16;
2953         }
2954         fb_setting.den_en = 1;
2955         fb_setting.vsync_en = 1;
2956         fb_setting.disp_on_en = 1;
2957         fb_setting.standby_en = 1;
2958         if( inf->cur_screen->mcu_usefmk )
2959             fb_setting.mcu_fmk_en =1;
2960         mach_info->io_init(&fb_setting);
2961     }
2962
2963         //set_lcd_pin(pdev, 1);
2964         mdelay(10);
2965         g_pdev = pdev;
2966         inf->mcu_usetimer = 1;
2967     inf->mcu_fmksync = 0;
2968         load_screen(inf->fb0, 1);
2969
2970     /* Register framebuffer(fb0 & fb1) */
2971     fbprintk(">> Register framebuffer(fb0) \n");
2972     ret = register_framebuffer(inf->fb0);
2973     if(ret<0)
2974     {
2975         printk(">> fb0 register_framebuffer fail!\n");
2976         ret = -EINVAL;
2977                 goto release_win0fb;
2978     }
2979     fbprintk(">> Register framebuffer(fb1) \n");
2980
2981     ret = register_framebuffer(inf->fb1);
2982     if(ret<0)
2983     {
2984         printk(">> fb1 register_framebuffer fail!\n");
2985         ret = -EINVAL;
2986                 goto unregister_win1fb;
2987     }
2988
2989     ret = device_create_file(inf->fb1->dev, &dev_attr_dsp_win0_info);
2990     if(ret)
2991     {
2992         printk(">> fb1 dsp win0 info device_create_file err\n");
2993         ret = -EINVAL;
2994     }
2995 #ifdef CONFIG_HAS_EARLYSUSPEND
2996         suspend_info.inf = inf;
2997         register_early_suspend(&suspend_info.early_suspend);
2998 #endif
2999
3000     /* get and request irq */
3001     fbprintk(">> get and request irq \n");
3002     irq = platform_get_irq(pdev, 0);
3003     if (irq < 0) {
3004         dev_err(&pdev->dev, "no irq for device\n");
3005         ret = -ENOENT;
3006         goto unregister_win1fb;
3007     }
3008     ret = request_irq(irq, rk29fb_irq, IRQF_DISABLED, pdev->name, pdev);
3009     if (ret) {
3010         dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
3011         ret = -EBUSY;
3012         goto release_irq;
3013     }
3014
3015     if( inf->cur_screen->mcu_usefmk && (mach_info->mcu_fmk_pin != -1) )
3016     {
3017         ret = request_irq(gpio_to_irq(mach_info->mcu_fmk_pin), mcu_irqfmk, GPIOEdgelFalling, pdev->name, pdev);
3018         if (ret)
3019         {
3020             dev_err(&pdev->dev, "cannot get fmk irq %d - err %d\n", irq, ret);
3021             ret = -EBUSY;
3022             goto release_irq;
3023         }
3024     }
3025
3026 #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
3027     fb0_set_par(inf->fb0);
3028     if (fb_prepare_logo(inf->fb0, FB_ROTATE_UR)) {
3029         /* Start display and show logo on boot */
3030         fb_set_cmap(&inf->fb0->cmap, inf->fb0);
3031         fb_show_logo(inf->fb0, FB_ROTATE_UR);
3032         fb0_blank(FB_BLANK_UNBLANK, inf->fb0);
3033     }
3034 #endif
3035
3036     printk(" %s ok\n", __FUNCTION__);
3037     return ret;
3038
3039 release_irq:
3040         if(irq>=0)
3041         free_irq(irq, pdev);
3042 unregister_win1fb:
3043     unregister_framebuffer(inf->fb0);
3044 release_win0fb:
3045         if(inf->fb1)
3046                 framebuffer_release(inf->fb1);
3047         inf->fb1 = NULL;
3048 release_cmap:
3049     if(&inf->fb0->cmap)
3050         fb_dealloc_cmap(&inf->fb0->cmap);
3051 release_win1fb:
3052         if(inf->fb0)
3053                 framebuffer_release(inf->fb0);
3054         inf->fb0 = NULL;
3055 release_drvdata:
3056         if(inf && inf->reg_vir_base)
3057         iounmap(inf->reg_vir_base);
3058         if(inf && mem)
3059         release_mem_region(inf->reg_phy_base, inf->len);
3060         if(inf)
3061         kfree(inf);
3062         platform_set_drvdata(pdev, NULL);
3063         return ret;
3064 }
3065
3066 static int rk29fb_remove(struct platform_device *pdev)
3067 {
3068     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
3069     struct fb_info *info = NULL;
3070         //pm_message_t msg;
3071     struct rk29fb_info *mach_info = NULL;
3072     int irq = 0;
3073
3074         fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
3075
3076     if(!inf) {
3077         printk("inf==0, rk29_fb_remove fail! \n");
3078         return -EINVAL;
3079     }
3080     device_remove_file(inf->fb1->dev, &dev_attr_dsp_win0_info);
3081
3082     irq = platform_get_irq(pdev, 0);
3083     if (irq >0)
3084     {
3085     free_irq(irq, pdev);
3086     }
3087
3088     mach_info = pdev->dev.platform_data;
3089     if(mach_info->mcu_fmk_pin)
3090     {
3091         free_irq(gpio_to_irq(mach_info->mcu_fmk_pin), pdev);
3092     }
3093
3094     if(mach_info->io_disable)  
3095        mach_info->io_disable();  //close lcd out 
3096
3097     // blank the lcdc
3098     if(inf->fb1)
3099         fb1_blank(FB_BLANK_POWERDOWN, inf->fb1);
3100     if(inf->fb0)
3101         fb0_blank(FB_BLANK_POWERDOWN, inf->fb0);
3102
3103         // suspend the lcdc
3104         //rk29fb_suspend(pdev, msg);
3105     // unmap memory and release framebuffer
3106     if(inf->fb1) {
3107         info = inf->fb1;
3108         if (info->screen_base) {
3109                 //dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
3110                 info->screen_base = 0;
3111                 info->fix.smem_start = 0;
3112                 info->fix.smem_len = 0;
3113         }
3114         unregister_framebuffer(inf->fb1);
3115         framebuffer_release(inf->fb1);
3116         inf->fb1 = NULL;
3117     }
3118     if(inf->fb0) {
3119         info = inf->fb0;
3120         if (info->screen_base) {
3121             //    dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
3122                 info->screen_base = 0;
3123                 info->fix.smem_start = 0;
3124                 info->fix.smem_len = 0;
3125         }
3126         unregister_framebuffer(inf->fb0);
3127         framebuffer_release(inf->fb0);
3128         inf->fb0 = NULL;
3129     }
3130
3131   #if 0 //def CONFIG_CPU_FREQ
3132    // cpufreq_unregister_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
3133   #endif
3134
3135     msleep(100);
3136
3137         if (inf->clk)
3138     {
3139                 clk_disable(inf->clk);
3140                 clk_put(inf->clk);
3141                 inf->clk = NULL;
3142         }
3143     if (inf->dclk)
3144     {
3145                 clk_disable(inf->dclk);
3146                 clk_put(inf->dclk);
3147                 inf->dclk = NULL;
3148         }
3149
3150     kfree(inf);
3151     platform_set_drvdata(pdev, NULL);
3152
3153     return 0;
3154 }
3155
3156 static void rk29fb_shutdown(struct platform_device *pdev)
3157 {
3158     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
3159     struct rk29fb_info *mach_info = pdev->dev.platform_data;;
3160
3161         fbprintk("----------------------------rk29fb_shutdown----------------------------\n");
3162
3163     if(mach_info->io_disable)  
3164        mach_info->io_disable();  //close lcd out 
3165        
3166     if(!inf->in_suspend)
3167     {
3168         LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(1));
3169         LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(1));
3170         LcdWrReg(inf, REG_CFG_DONE, 0x01);
3171         mdelay(100);
3172         clk_disable(inf->aclk_ddr_lcdc);
3173         clk_disable(inf->aclk_disp_matrix);
3174         clk_disable(inf->hclk_cpu_display);
3175         clk_disable(inf->clk);
3176         if(inf->dclk){
3177             clk_disable(inf->dclk);
3178         }
3179         if(inf->clk){
3180             clk_disable(inf->aclk);
3181         }
3182         clk_disable(inf->pd_display);
3183         //pmu_set_power_domain(PD_DISPLAY, 0);
3184                 inf->in_suspend = 1;
3185         }
3186
3187 }
3188
3189 static struct platform_driver rk29fb_driver = {
3190         .probe          = rk29fb_probe,
3191         .remove         = rk29fb_remove,
3192         .driver         = {
3193                 .name   = "rk29-fb",
3194                 .owner  = THIS_MODULE,
3195         },
3196         .shutdown   = rk29fb_shutdown,
3197 };
3198
3199 static int __init rk29fb_init(void)
3200 {
3201         wake_lock_init(&idlelock, WAKE_LOCK_IDLE, "fb");
3202     return platform_driver_register(&rk29fb_driver);
3203 }
3204
3205 static void __exit rk29fb_exit(void)
3206 {
3207     platform_driver_unregister(&rk29fb_driver);
3208 }
3209
3210 fs_initcall(rk29fb_init);
3211 module_exit(rk29fb_exit);
3212
3213
3214 MODULE_AUTHOR("  zyw@rock-chips.com");
3215 MODULE_DESCRIPTION("Driver for rk29 fb device");
3216 MODULE_LICENSE("GPL");
3217
3218