modify fb pixclock for android refresh rate
[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
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, screen->pixclock);
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     int i=0;
1209     int *p = NULL;
1210     int blank_data,total_data;
1211
1212     u16 xres_virtual = fb0_inf->var.xres_virtual;      //virtual screen size
1213     u16 xpos_virtual = fb0_inf->var.xoffset;           //visiable offset in virtual screen
1214     u16 ypos_virtual = fb0_inf->var.yoffset;
1215
1216     int offset = (ypos_virtual*xres_virtual + xpos_virtual)*((inf->fb0_color_deepth || fb0_inf->var.bits_per_pixel==32)? 4:2)/4;  
1217     p = (int*)fb0_inf->screen_base + offset; 
1218     blank_data = (inf->fb0_color_deepth==32) ? 0xff000000 : 0;
1219     total_data = fb0_inf->var.xres*fb0_inf->var.yres*fb0_inf->var.bits_per_pixel/32;
1220     
1221     for(i=0; i < total_data; i++)
1222     {
1223         if(*p++ != blank_data) 
1224         {
1225             //printk("win1 have no 0 data in %d, total %d\n",i,total_data);
1226             return;
1227         }            
1228     }
1229
1230     win1_blank(FB_BLANK_POWERDOWN, fb0_inf);
1231    // printk("%s close win1!\n",__func__);
1232 }
1233 static DECLARE_DELAYED_WORK(rk29_win1_check_work, win1_check_work_func);
1234 #endif
1235 static int win1_set_par(struct fb_info *info)
1236 {
1237     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1238     struct fb_fix_screeninfo *fix = &info->fix;
1239     struct rk29fb_screen *screen = inf->cur_screen;
1240     struct win0_par *par = info->par;
1241     struct fb_var_screeninfo *var = &info->var;
1242         u32 addr;
1243         u16 xres_virtual,xpos,ypos;
1244         u8 trspval,trspmode;
1245  #ifdef CONFIG_CLOSE_WIN1_DYNAMIC   
1246     cancel_delayed_work_sync(&rk29_win1_check_work);
1247  #endif   
1248     if(((screen->x_res != var->xres) || (screen->y_res != var->yres))
1249         && !((screen->x_res>1280) && (var->bits_per_pixel == 32)))
1250     {
1251         hdmi_set_fbscale(info);
1252     }else  if(((screen->x_res==1920) ))
1253     {
1254         if(hdmi_get_fbscale() < 100)
1255                         par->ypos -=screen->y_res * (100-hdmi_get_fbscale()) / 200;
1256         }
1257     //u32 offset=0, addr=0, map_size=0, smem_len=0;
1258     addr=0;
1259     xres_virtual = 0;      //virtual screen size
1260
1261     //u16 xpos_virtual = var->xoffset;           //visiable offset in virtual screen
1262     //u16 ypos_virtual = var->yoffset;
1263
1264     xpos = par->xpos;                 //visiable offset in panel
1265     ypos = par->ypos;
1266
1267     trspmode = TRSP_CLOSE;
1268     trspval = 0;
1269
1270     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1271
1272    #ifdef CONFIG_FB_SCALING_OSD
1273     if(((screen->x_res != var->xres) || (screen->y_res != var->yres)) 
1274             && (screen->x_res<=1280))
1275     {
1276         addr = fix->mmio_start + par->y_offset* hdmi_get_fbscale()/100;
1277         xres_virtual = screen->x_res* hdmi_get_fbscale()/100;      //virtual screen size
1278     }
1279     else
1280    #endif
1281     {
1282         addr = fix->smem_start + par->y_offset;
1283         xres_virtual = var->xres_virtual;      //virtual screen size
1284     }
1285     LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE|m_W1_FORMAT, v_W1_ENABLE(1)|v_W1_FORMAT(par->format));
1286
1287     xpos += (screen->left_margin + screen->hsync_len);
1288     ypos += (screen->upper_margin + screen->vsync_len);
1289
1290     LcdWrReg(inf, WIN1_YRGB_MST, addr);
1291
1292     LcdMskReg(inf, WIN1_DSP_ST, m_BIT11LO|m_BIT11HI, v_BIT11LO(xpos) | v_BIT11HI(ypos));
1293     LcdMskReg(inf, WIN1_DSP_INFO, m_BIT12LO|m_BIT12HI, v_BIT12LO(par->xsize) | v_BIT12HI(par->ysize));
1294
1295     LcdMskReg(inf, WIN1_VIR, m_WORDLO , v_WORDLO(xres_virtual));
1296
1297     LcdMskReg(inf, BLEND_CTRL, m_W1_BLEND_EN |  m_W1_BLEND_FACTOR,
1298         v_W1_BLEND_EN((TRSP_FMREG==trspmode) || (TRSP_MASK==trspmode)) | v_W1_BLEND_FACTOR(trspval));
1299
1300      // enable win1 color key and set the color to black(rgb=0)
1301     LcdMskReg(inf, WIN1_COLOR_KEY_CTRL, m_COLORKEY_EN | m_KEYCOLOR, v_COLORKEY_EN(1) | v_KEYCOLOR(0));
1302
1303     if(1==par->format) //rgb565
1304     {
1305         LcdMskReg(inf, SWAP_CTRL, m_W1_8_SWAP | m_W1_16_SWAP | m_W1_R_SHIFT_SWAP | m_W1_565_RB_SWAP,
1306             v_W1_8_SWAP(0) | v_W1_16_SWAP(0) | v_W1_R_SHIFT_SWAP(0) | v_W1_565_RB_SWAP(0) );
1307     }
1308     else
1309     {
1310          LcdMskReg(inf, SWAP_CTRL, m_W1_8_SWAP | m_W1_16_SWAP | m_W1_R_SHIFT_SWAP | m_W1_565_RB_SWAP,
1311                 v_W1_8_SWAP(1) | v_W1_16_SWAP(1) | v_W1_R_SHIFT_SWAP(1) | v_W1_565_RB_SWAP(0) );
1312
1313          LcdMskReg(inf, DSP_CTRL0, m_W1_TRANSP_FROM, v_W1_TRANSP_FROM(TRSP_FMRAM==trspmode) );
1314     }
1315
1316         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1317     
1318 #ifdef CONFIG_CLOSE_WIN1_DYNAMIC 
1319     schedule_delayed_work(&rk29_win1_check_work, msecs_to_jiffies(5000));
1320 #endif
1321
1322     return 0;
1323 }
1324
1325 static int win1_pan( struct fb_info *info )
1326 {
1327     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1328     struct fb_fix_screeninfo *fix1 = &info->fix;
1329     struct win0_par *par = info->par;
1330     u32 addr = 0;
1331
1332     #ifdef CONFIG_FB_SCALING_OSD
1333     struct rk29fb_screen *screen = inf->cur_screen;
1334     struct fb_var_screeninfo *var = &info->var;
1335     if(((screen->x_res != var->xres) || (screen->y_res != var->yres)) 
1336             && (screen->x_res<=1280))
1337     {
1338         addr = fix1->mmio_start + par->y_offset* hdmi_get_fbscale()/100;
1339     }
1340     else
1341     #endif
1342     {
1343         addr = fix1->smem_start + par->y_offset;
1344     }
1345
1346     //fbprintk("info->screen_base = %8x ; fix1->smem_len = %d , addr = %8x\n",(u32)info->screen_base, fix1->smem_len, addr);
1347
1348     LcdWrReg(inf, WIN1_YRGB_MST, addr);
1349     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1350         mcu_refresh(inf);
1351
1352     return 0;
1353 }
1354
1355 static int fb0_blank(int blank_mode, struct fb_info *info)
1356 {
1357         struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1358
1359     if(inf->video_mode == 1)
1360     {
1361         win1_blank(blank_mode, info);
1362     }
1363     else
1364     {
1365         win0_blank(blank_mode, info);
1366     }
1367     return 0;
1368 }
1369
1370 static int fb0_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1371 {
1372     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1373     struct rk29fb_screen *screen = inf->cur_screen;
1374     u16 xpos = (var->nonstd>>8) & 0xfff;
1375     u16 ypos = (var->nonstd>>20) & 0xfff;
1376     u16 xlcd = screen->x_res;
1377     u16 ylcd = screen->y_res;
1378
1379     //fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1380
1381         CHK_SUSPEND(inf);
1382
1383     if( 0==var->xres_virtual || 0==var->yres_virtual ||
1384         0==var->xres || 0==var->yres || var->xres<16 ||
1385         ((16!=var->bits_per_pixel)&&(32!=var->bits_per_pixel)) )
1386     {
1387         printk(">>>>>> fb0_check_var fail 1!!! \n");
1388         printk(">>>>>> 0==%d || 0==%d ", var->xres_virtual,var->yres_virtual);
1389         printk("0==%d || 0==%d || %d<16 || ", var->xres,var->yres,var->xres<16);
1390         printk("bits_per_pixel=%d \n", var->bits_per_pixel);
1391         return -EINVAL;
1392     }
1393
1394     if( (var->xoffset+var->xres)>var->xres_virtual ||
1395         (var->yoffset+var->yres)>var->yres_virtual*2 )
1396     {
1397         printk(">>>>>> fb0_check_var fail 2!!! \n");
1398         printk(">>>>>> (%d+%d)>%d || ", var->xoffset,var->xres,var->xres_virtual);
1399         printk("(%d+%d)>%d || ", var->yoffset,var->yres,var->yres_virtual);
1400         printk("(%d+%d)>%d || (%d+%d)>%d \n", xpos,var->xres,xlcd,ypos,var->yres,ylcd);
1401         return -EINVAL;
1402     }
1403
1404     if(inf->fb0_color_deepth)var->bits_per_pixel=inf->fb0_color_deepth;
1405     switch(var->bits_per_pixel)
1406     {
1407     case 16:    // rgb565
1408         var->xres_virtual = (var->xres_virtual + 0x1) & (~0x1);
1409         var->xres = (var->xres + 0x1) & (~0x1);
1410         var->xoffset = (var->xoffset) & (~0x1);
1411         break;
1412     default:    // rgb888
1413         var->bits_per_pixel = 32;
1414         break;
1415     }
1416
1417     return 0;
1418 }
1419
1420
1421 static int fb0_set_par(struct fb_info *info)
1422 {
1423     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1424     struct fb_var_screeninfo *var = &info->var;
1425     struct fb_fix_screeninfo *fix = &info->fix;
1426     struct rk29fb_screen *screen = inf->cur_screen;
1427     struct win0_par *par = info->par;
1428
1429     u32 offset=0,  smem_len=0;
1430     u16 xres_virtual = var->xres_virtual;      //virtual screen size
1431     u16 xpos_virtual = var->xoffset;           //visiable offset in virtual screen
1432     u16 ypos_virtual = var->yoffset;
1433
1434 #ifdef CONFIG_FB_SCALING_OSD
1435     struct rk29_ipp_req ipp_req;
1436     u32 dstoffset=0;
1437 #endif
1438
1439     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1440
1441     inf->setFlag = 0;
1442         CHK_SUSPEND(inf);
1443
1444     if(inf->fb0_color_deepth)var->bits_per_pixel=inf->fb0_color_deepth;
1445     #if !defined(CONFIG_FB_SCALING_OSD)
1446     if((inf->video_mode == 1)&&(screen->y_res < var->yres))ypos_virtual += (var->yres-screen->y_res);
1447     #endif
1448
1449     switch(var->bits_per_pixel)
1450     {
1451     case 16:    // rgb565
1452         par->format = 1;
1453          if( ypos_virtual == 0)
1454             fb0_first_buff_bits = 16;
1455         else
1456             fb0_second_buff_bits = 16;
1457
1458         //fix->line_length = 2 * xres_virtual;
1459         fix->line_length = (inf->fb0_color_deepth ? 4:2) * xres_virtual;   //32bit and 16bit change
1460
1461         #ifdef CONFIG_FB_SCALING_OSD
1462         dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*2;
1463         ipp_req.src0.fmt = IPP_RGB_565;
1464         ipp_req.dst0.fmt = IPP_RGB_565;
1465         #endif
1466         offset = (ypos_virtual*xres_virtual + xpos_virtual)*(inf->fb0_color_deepth ? 4:2);
1467         if(ypos_virtual == 3*var->yres && inf->fb0_color_deepth)
1468             offset -= var->yres * var->xres *2;
1469         break;
1470     case 32:    // rgb888
1471     default:
1472         par->format = 0;
1473          if( ypos_virtual == 0)
1474             fb0_first_buff_bits = 32;
1475         else
1476             fb0_second_buff_bits = 32;
1477         fix->line_length = 4 * xres_virtual;
1478         #ifdef CONFIG_FB_SCALING_OSD
1479         dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*4;       
1480
1481         ipp_req.src0.fmt = IPP_XRGB_8888;
1482         ipp_req.dst0.fmt = IPP_XRGB_8888;
1483         #endif
1484         offset = (ypos_virtual*xres_virtual + xpos_virtual)*4;
1485         
1486         if(ypos_virtual >= 2*var->yres)
1487         {
1488             par->format = 1;
1489             #ifdef CONFIG_FB_SCALING_OSD
1490             dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*2;
1491             ipp_req.src0.fmt = IPP_RGB_565;
1492             ipp_req.dst0.fmt = IPP_RGB_565;
1493             #endif
1494             if(ypos_virtual == 3*var->yres)
1495             {            
1496                 offset -= var->yres * var->xres *2;
1497             }
1498         }
1499         break;
1500     }
1501
1502     smem_len = fix->line_length * var->yres_virtual;
1503     //map_size = PAGE_ALIGN(smem_len);
1504
1505     if (smem_len > fix->smem_len)     // buffer need realloc
1506     {
1507         printk("%s sorry!!! win1 buf is not enough\n",__FUNCTION__);
1508         printk("line_length = %d, yres_virtual = %d, win1_buf only = %dB\n",fix->line_length,var->yres_virtual,fix->smem_len);
1509         printk("you can change buf size MEM_FB_SIZE in board-xxx.c \n");
1510     }
1511
1512
1513     par->addr_seted = 1;
1514 #if ANDROID_USE_THREE_BUFS
1515     if(0==new_frame_seted) {
1516         wq_condition = 0;
1517         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1518     }
1519     new_frame_seted = 0;
1520 #endif
1521
1522     if(inf->video_mode == 1)
1523     {
1524         #ifdef CONFIG_FB_SCALING_OSD
1525         if(((screen->x_res != var->xres) || (screen->y_res != var->yres)) 
1526             && (screen->x_res<=1280))
1527         {
1528             par->xpos = 0;
1529             par->ypos = 0;
1530             par->xsize = screen->x_res;
1531             par->ysize = screen->y_res;
1532             par->y_offset = dstoffset;
1533
1534             ipp_req.src0.YrgbMst = fix->smem_start + offset;
1535             ipp_req.src0.w = var->xres;
1536             ipp_req.src0.h = var->yres;
1537
1538             ipp_req.dst0.YrgbMst = fix->mmio_start + dstoffset* hdmi_get_fbscale()/100;
1539             ipp_req.dst0.w = screen->x_res* hdmi_get_fbscale()/100;
1540             ipp_req.dst0.h = screen->y_res* hdmi_get_fbscale()/100;
1541
1542             ipp_req.src_vir_w = ipp_req.src0.w;
1543             ipp_req.dst_vir_w = ipp_req.dst0.w;
1544             ipp_req.timeout = 100;
1545             ipp_req.flag = IPP_ROT_0;
1546             //ipp_do_blit(&ipp_req);
1547             ipp_blit_sync(&ipp_req);
1548         }else
1549         #endif
1550         {
1551             par->y_offset = offset;
1552             par->xpos = (screen->x_res >= var->xres)?((screen->x_res - var->xres)/2):0;              //visiable offset in panel
1553             par->ypos = (screen->y_res >= var->yres)?(screen->y_res - var->yres):0;
1554             par->xsize = var->xres;                                //visiable size in panel
1555             par->ysize = (screen->y_res >= var->yres) ? var->yres : screen->y_res;
1556         }
1557         win1_set_par(info);
1558     }
1559     else
1560     {
1561         par->y_offset = offset;
1562         par->xpos = 0;
1563         par->ypos = 0;
1564         par->xsize = screen->x_res;
1565         par->ysize = screen->y_res;
1566         win0_set_par(info);
1567     }
1568     inf->setFlag = 1;
1569     return 0;
1570 }
1571
1572 static int fb0_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1573 {
1574
1575     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1576     struct fb_var_screeninfo *var1 = &info->var;
1577     struct rk29fb_screen *screen = inf->cur_screen;
1578     struct win0_par *par = info->par;
1579
1580     u32 offset = 0;
1581     u16 ypos_virtual = var->yoffset;
1582     u16 xpos_virtual = var->xoffset;
1583    #ifdef CONFIG_FB_SCALING_OSD
1584     struct fb_fix_screeninfo *fix = &info->fix;
1585     struct rk29_ipp_req ipp_req;
1586     u32 dstoffset = 0;
1587    #endif
1588         //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1589
1590         CHK_SUSPEND(inf);
1591
1592     if(inf->fb0_color_deepth)var->bits_per_pixel=inf->fb0_color_deepth;
1593     #if !defined(CONFIG_FB_SCALING_OSD)
1594         if((inf->video_mode == 1)&&(screen->y_res < var->yres))ypos_virtual += (var->yres-screen->y_res);
1595     #endif
1596
1597     switch(var1->bits_per_pixel)
1598     {
1599     case 16:    // rgb565
1600         var->xoffset = (var->xoffset) & (~0x1);
1601         #ifdef CONFIG_FB_SCALING_OSD
1602         dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres) * 2;
1603         ipp_req.src0.fmt = IPP_RGB_565;
1604         ipp_req.dst0.fmt = IPP_RGB_565;
1605         #endif
1606         offset = (ypos_virtual*var1->xres_virtual + xpos_virtual)*(inf->fb0_color_deepth ? 4:2);
1607         if(ypos_virtual == 3*var->yres && inf->fb0_color_deepth)
1608             offset -= var->yres * var->xres *2;
1609         break;
1610     case 32:    // rgb888
1611         #ifdef CONFIG_FB_SCALING_OSD
1612         dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*4;
1613         ipp_req.src0.fmt = IPP_XRGB_8888;
1614         ipp_req.dst0.fmt = IPP_XRGB_8888;
1615         #endif
1616         offset = (ypos_virtual*var1->xres_virtual + xpos_virtual)*4;
1617         if(ypos_virtual >= 2*var->yres)
1618         {
1619             par->format = 1;
1620             #ifdef CONFIG_FB_SCALING_OSD
1621             dstoffset = ((ypos_virtual*screen->y_res/var->yres) *screen->x_res + (xpos_virtual*screen->x_res)/var->xres )*2;
1622             ipp_req.src0.fmt = IPP_RGB_565;
1623             ipp_req.dst0.fmt = IPP_RGB_565;
1624             #endif
1625             if(ypos_virtual == 3*var->yres)
1626             {            
1627                 offset -= var->yres * var->xres *2;
1628             }
1629         }
1630         break;
1631     default:
1632         return -EINVAL;
1633     }
1634
1635     if(inf->video_mode == 1)
1636     {
1637         #ifdef CONFIG_FB_SCALING_OSD
1638         if(((screen->x_res != var->xres) || (screen->y_res != var->yres)) 
1639             && (screen->x_res<=1280))
1640         {
1641             par->y_offset = dstoffset;
1642
1643             ipp_req.src0.YrgbMst = fix->smem_start + offset;
1644             ipp_req.src0.w = var->xres;
1645             ipp_req.src0.h = var->yres;
1646
1647             ipp_req.dst0.YrgbMst = fix->mmio_start + dstoffset* hdmi_get_fbscale()/100;
1648             ipp_req.dst0.w = screen->x_res* hdmi_get_fbscale()/100;
1649             ipp_req.dst0.h = screen->y_res* hdmi_get_fbscale()/100;
1650
1651             ipp_req.src_vir_w = ipp_req.src0.w;
1652             ipp_req.dst_vir_w = ipp_req.dst0.w;
1653             ipp_req.timeout = 100;
1654             ipp_req.flag = IPP_ROT_0;
1655             //ipp_do_blit(&ipp_req);
1656             ipp_blit_sync(&ipp_req);
1657             win1_pan(info);
1658             return 0;
1659         }else
1660         #endif
1661             par->y_offset = offset;
1662         win1_pan(info);
1663     }
1664     else
1665     {
1666         par->y_offset = offset;
1667         win0_pan(info);
1668     }
1669         // flush end when wq_condition=1 in mcu panel, but not in rgb panel
1670 #if !ANDROID_USE_THREE_BUFS
1671     // flush end when wq_condition=1 in mcu panel, but not in rgb panel
1672     if(SCREEN_MCU == inf->cur_screen->type) {
1673         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1674         wq_condition = 0;
1675     } else {
1676         wq_condition = 0;
1677         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1678     }
1679 #endif
1680     return 0;
1681 }
1682
1683 static int fb0_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
1684 {
1685     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1686         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1687
1688         CHK_SUSPEND(inf);
1689
1690     switch(cmd)
1691     {
1692     case FB0_IOCTL_STOP_TIMER_FLUSH:    //stop timer flush mcu panel after android is runing
1693         if(1==arg)
1694         {
1695             inf->mcu_usetimer = 0;
1696         }
1697         break;
1698    case FBIOPUT_16OR32:
1699
1700         inf->fb0_color_deepth = arg;
1701
1702             break;
1703         case FBIOGET_16OR32:
1704             return  inf->fb0_color_deepth;
1705         case FBIOGET_IDLEFBUff_16OR32:
1706         if(info->var.yoffset == 0)
1707         {
1708             return fb0_second_buff_bits;
1709         }
1710         else
1711         {
1712             return fb0_first_buff_bits;
1713         }
1714         case FBIOPUT_FBPHYADD:
1715         return info->fix.smem_start;
1716     case FBIOGET_OVERLAY_STATE:
1717         return inf->video_mode;
1718     case FBIOGET_SCREEN_STATE:
1719         return inf->cur_screen->type;
1720         
1721         case FBIOPUT_SET_CURSOR_EN:
1722                 {
1723                         int en;
1724                         if(copy_from_user(&en, (void*)arg, sizeof(int)))
1725                             return -EFAULT;
1726                         rk29_set_cursor_en(inf, en);
1727                 }
1728                 break;
1729         case FBIOPUT_SET_CURSOR_POS:
1730                 {
1731                         struct fbcurpos pos;
1732                         if(copy_from_user(&pos, (void*)arg, sizeof(struct fbcurpos)))
1733                             return -EFAULT;
1734                         rk29_set_cursor_pos(inf, pos.x , pos.y);
1735                 }
1736                 break;
1737         case FBIOPUT_SET_CURSOR_IMG:
1738                 {
1739                         char cursor_buf[CURSOR_BUF_SIZE];
1740                         if(copy_from_user(cursor_buf, (void*)arg, CURSOR_BUF_SIZE))
1741                                 return -EFAULT;
1742                         rk29_set_cursor_img(inf, cursor_buf);
1743                 }
1744                 break;
1745         case FBIOPUT_SET_CURSOR_CMAP:
1746                 {
1747                         struct fb_image img;
1748                         if(copy_from_user(&img, (void*)arg, sizeof(struct fb_image)))
1749                             return -EFAULT;
1750                         rk29_set_cursor_colour_map(inf, img.bg_color, img.fg_color);
1751                 }
1752                 break;
1753         case FBIOPUT_GET_CURSOR_RESOLUTION:
1754                 {
1755             u32 panel_size[2];
1756                         //struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1757              if(inf->fb1->var.rotate == 270) {
1758                 panel_size[0] = inf->cur_screen->y_res; //inf->cur_screen->y_res; change for hdmi video size
1759                 panel_size[1] = inf->cur_screen->x_res;
1760             } else {
1761                 panel_size[0] = inf->cur_screen->x_res;
1762                 panel_size[1] = inf->cur_screen->y_res;
1763             }
1764             if(copy_to_user((void*)arg, panel_size, 8))  return -EFAULT;
1765             break;
1766                 }
1767         case FBIOPUT_GET_CURSOR_EN:
1768                 {
1769             u32 en = (inf->regbak.SYS_CONFIG & m_HWC_ENABLE);
1770             if(copy_to_user((void*)arg, &en, 4))  return -EFAULT;
1771             break;
1772                 }
1773    default:
1774         break;
1775     }
1776     return 0;
1777 }
1778
1779 static int fb1_blank(int blank_mode, struct fb_info *info)
1780 {
1781     win0_blank(blank_mode, info);
1782     return 0;
1783 }
1784
1785 static int fb1_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1786 {
1787     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1788     struct rk29fb_screen *screen = inf->cur_screen;
1789
1790     u32 ScaleYRGBY=0x1000;
1791     u16 xpos = (var->nonstd>>8) & 0xfff;   //offset in panel
1792     u16 ypos = (var->nonstd>>20) & 0xfff;
1793     u16 xsize = (var->grayscale>>8) & 0xfff;   //visiable size in panel
1794     u16 ysize = (var->grayscale>>20) & 0xfff;
1795     u16 xlcd = screen->x_res;        //size of panel
1796     u16 ylcd = screen->y_res;
1797     u16 yres = 0;
1798 #if 0
1799         struct hdmi *hdmi = get_hdmi_struct(0);
1800 #endif
1801
1802     if((var->rotate == 270)||(var->rotate == 90)) {
1803       #ifdef CONFIG_FB_ROTATE_VIDEO
1804         xlcd = screen->y_res;
1805         ylcd = screen->x_res;
1806       #else //CONFIG_FB_ROTATE_VIDEO
1807         printk("LCDC not support rotate!\n");
1808         return -EINVAL;
1809       #endif
1810     }
1811
1812     xpos = (xpos * screen->x_res) / inf->panel1_info.x_res;
1813     ypos = (ypos * screen->y_res) / inf->panel1_info.y_res;
1814     xsize = (xsize * screen->x_res) / inf->panel1_info.x_res;
1815     ysize = (ysize * screen->y_res) / inf->panel1_info.y_res;
1816
1817     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1818
1819         CHK_SUSPEND(inf);
1820
1821     if( 0==var->xres_virtual || 0==var->yres_virtual ||
1822         0==var->xres || 0==var->yres || var->xres<16 ||
1823         0==xsize || 0==ysize || xsize<16 ||
1824         ((16!=var->bits_per_pixel)&&(32!=var->bits_per_pixel)) )
1825     {
1826         printk(">>>>>> win0fb_check_var fail 1!!! \n");
1827                 printk("0==%d || 0==%d || 0==%d || 0==%d || %d<16 \n ||0==%d || 0==%d || %d<16 ||((16!=%d)&&(32!=%d)) \n",
1828                                 var->xres_virtual, var->yres_virtual, var->xres, var->yres, var->xres, xsize, ysize, xsize,
1829                         var->bits_per_pixel, var->bits_per_pixel);
1830         return -EINVAL;
1831     }
1832
1833     if( (var->xoffset+var->xres)>var->xres_virtual ||
1834         (var->yoffset+var->yres)>var->yres_virtual ||
1835         (xpos+xsize)>xlcd || (ypos+ysize)>ylcd  )
1836     {
1837         printk(">>>>>> win0fb_check_var fail 2!!! \n");
1838                 printk("(%d+%d)>%d || (%d+%d)>%d || (%d+%d)>%d || (%d+%d)>%d \n ",
1839                                 var->xoffset, var->xres, var->xres_virtual, var->yoffset, var->yres,
1840                                 var->yres_virtual, xpos, xsize, xlcd, ypos, ysize, ylcd);       
1841         return -EINVAL;
1842     }
1843
1844     switch(var->nonstd&0x0f)
1845     {
1846     case 0: // rgb
1847         switch(var->bits_per_pixel)
1848         {
1849         case 16:    // rgb565
1850             var->xres_virtual = (var->xres_virtual + 0x1) & (~0x1);
1851             var->xres = (var->xres + 0x1) & (~0x1);
1852             var->xoffset = (var->xoffset) & (~0x1);
1853             break;
1854         default:    // rgb888
1855             var->bits_per_pixel = 32;
1856             break;
1857         }
1858         var->nonstd &= ~0xc0;  //not support I2P in this format
1859         break;
1860     case 1: // yuv422
1861         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
1862         var->xres = (var->xres + 0x3) & (~0x3);
1863         var->xoffset = (var->xoffset) & (~0x3);
1864         break;
1865     case 2: // yuv4200
1866         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
1867         var->yres_virtual = (var->yres_virtual + 0x1) & (~0x1);
1868         var->xres = (var->xres + 0x3) & (~0x3);
1869         var->yres = (var->yres + 0x1) & (~0x1);
1870         var->xoffset = (var->xoffset) & (~0x3);
1871         var->yoffset = (var->yoffset) & (~0x1);
1872         break;
1873     case 3: // yuv4201
1874         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
1875         var->yres_virtual = (var->yres_virtual + 0x1) & (~0x1);
1876         var->xres = (var->xres + 0x3) & (~0x3);
1877         var->yres = (var->yres + 0x1) & (~0x1);
1878         var->xoffset = (var->xoffset) & (~0x3);
1879         var->yoffset = (var->yoffset) & (~0x1);
1880         var->nonstd &= ~0xc0;   //not support I2P in this format
1881         break;
1882     case 4: // none
1883     case 5: // yuv444
1884         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
1885         var->xres = (var->xres + 0x3) & (~0x3);
1886         var->xoffset = (var->xoffset) & (~0x3);
1887         var->nonstd &= ~0xc0;   //not support I2P in this format
1888         break;
1889     default:
1890         printk(">>>>>> fb1 var->nonstd=%d is invalid! \n", var->nonstd);
1891         return -EINVAL;
1892     }
1893
1894     if((var->rotate == 270)||(var->rotate == 90))
1895      {
1896          yres = var->xres;
1897      }
1898      else
1899      {
1900          yres = var->yres;
1901      }
1902
1903     ScaleYRGBY = CalScaleW0(yres, ysize);
1904
1905     if((ScaleYRGBY>0x8000) || (ScaleYRGBY<0x200))
1906     {
1907         return -EINVAL;        // multiple of scale down or scale up can't exceed 8
1908     }
1909 #if 0
1910         if(inf->video_mode == 1) {
1911                 if(hdmi_resolution_changed(hdmi,var->xres,var->yres, 1) == 1)
1912                 {
1913                         LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(1));
1914                 LcdWrReg(inf, REG_CFG_DONE, 0x01);
1915                         init_completion(&hdmi->complete);
1916                         hdmi->wait = 1;
1917                         wait_for_completion_interruptible_timeout(&hdmi->complete,
1918                                                                 msecs_to_jiffies(10000));
1919                 }
1920         }
1921 #endif
1922     return 0;
1923 }
1924
1925 static int fb1_set_par(struct fb_info *info)
1926 {
1927     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1928     struct rk29fb_screen *screen = inf->cur_screen;
1929     struct fb_var_screeninfo *var = &info->var;
1930     struct fb_fix_screeninfo *fix = &info->fix;
1931     struct win0_par *par = info->par;
1932
1933     u8 format = 0;
1934     u32 cblen=0, crlen=0, map_size=0, smem_len=0;
1935
1936         //u32 xact = var->xres;                     /* visible resolution               */
1937         //u32 yact = var->yres;
1938         u32 xvir = var->xres_virtual;           /* virtual resolution           */
1939         u32 yvir = var->yres_virtual;
1940         u32 xact_st = var->xoffset;                     /* offset from virtual to visible */
1941         u32 yact_st = var->yoffset;                     /* resolution                   */
1942
1943     u16 xpos = (var->nonstd>>8) & 0xfff;      //visiable pos in panel
1944     u16 ypos = (var->nonstd>>20) & 0xfff;
1945     u16 xsize = (var->grayscale>>8) & 0xfff;  //visiable size in panel
1946     u16 ysize = (var->grayscale>>20) & 0xfff;
1947
1948     //u32 ScaleYrgbX=0x1000,ScaleYrgbY=0x1000;
1949     //u32 ScaleCbrX=0x1000, ScaleCbrY=0x1000;
1950
1951     u8 data_format = var->nonstd&0x0f;
1952    // u32 win0_en = var->reserved[2];
1953    // u32 y_addr = var->reserved[3];       //user alloc buf addr y
1954    // u32 uv_addr = var->reserved[4];
1955
1956     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1957
1958         CHK_SUSPEND(inf);
1959     if((var->rotate == 270)||(var->rotate == 90))
1960     {
1961         #ifdef CONFIG_FB_ROTATE_VIDEO
1962         xpos = (var->nonstd>>20) & 0xfff;      //visiable pos in panel
1963         ypos = (var->nonstd>>8) & 0xfff;
1964         xsize = (var->grayscale>>20) & 0xfff;  //visiable size in panel
1965         ysize = (var->grayscale>>8) & 0xfff;
1966         #else //CONFIG_FB_ROTATE_VIDEO
1967         printk("LCDC not support rotate!\n");
1968         return -EINVAL;
1969       #endif
1970     } else{
1971         xpos = (xpos * screen->x_res) / inf->panel1_info.x_res;
1972         ypos = (ypos * screen->y_res) / inf->panel1_info.y_res;
1973         xsize = (xsize * screen->x_res) / inf->panel1_info.x_res;
1974         ysize = (ysize * screen->y_res) / inf->panel1_info.y_res;
1975     }
1976         /* calculate y_offset,c_offset,line_length,cblen and crlen  */
1977     switch (data_format)
1978     {
1979     case 0: // rgb
1980         switch(var->bits_per_pixel)
1981         {
1982         case 16:    // rgb565
1983             format = 1;
1984             fix->line_length = 2 * xvir;
1985             par->y_offset = (yact_st*xvir + xact_st)*2;
1986             break;
1987         case 32:    // rgb888
1988             format = 0;
1989             fix->line_length = 4 * xvir;
1990             par->y_offset = (yact_st*xvir + xact_st)*4;
1991             break;
1992         default:
1993             return -EINVAL;
1994         }
1995         break;
1996     case 1: // yuv422
1997         format = 2;
1998         fix->line_length = xvir;
1999         cblen = crlen = (xvir*yvir)/2;
2000         par->y_offset = yact_st*xvir + xact_st;
2001         par->c_offset = yact_st*xvir + xact_st;
2002         break;
2003     case 2: // yuv4200
2004         format = 3;
2005         fix->line_length = xvir;
2006         cblen = crlen = (xvir*yvir)/4;
2007
2008         par->y_offset = yact_st*xvir + xact_st;
2009         par->c_offset = (yact_st/2)*xvir + xact_st;
2010
2011         break;
2012     case 3: // yuv4201
2013         format = 4;
2014         fix->line_length = xvir;
2015         par->y_offset = (yact_st/2)*2*xvir + (xact_st)*2;
2016         par->c_offset = (yact_st/2)*xvir + xact_st;
2017         cblen = crlen = (xvir*yvir)/4;
2018         break;
2019     case 4: // none
2020     case 5: // yuv444
2021         format = 5;
2022         fix->line_length = xvir;
2023         par->y_offset = yact_st*xvir + xact_st;
2024         par->c_offset = yact_st*2*xvir + xact_st*2;
2025         cblen = crlen = (xvir*yvir);
2026         break;
2027     default:
2028         return -EINVAL;
2029     }
2030
2031     smem_len = fix->line_length * yvir + cblen + crlen;
2032     map_size = PAGE_ALIGN(smem_len);
2033
2034    // fix->smem_start = y_addr;
2035     fix->smem_len = smem_len;
2036   //  fix->mmio_start = uv_addr;
2037
2038  //   par->addr_seted = ((-1==(int)y_addr) || (0==(int)y_addr) || (win0_en==0)) ? 0 : 1;
2039     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);
2040
2041     par->format = format;
2042     par->xpos = xpos;
2043     par->ypos = ypos;
2044     par->xsize = xsize;
2045     par->ysize = ysize;
2046     win0_set_par(info);
2047
2048     if ( wq_condition2 == 0 ) {
2049         wait_event_interruptible_timeout(wq,  wq_condition2, HZ/20);
2050     }
2051     wq_condition2 = 0;
2052
2053 #ifdef CONFIG_FB_ROTATE_VIDEO
2054 //need refresh  ,zyc add                
2055         if((has_set_rotate == true) && (last_yuv_phy[0] != 0) && (last_yuv_phy[1] != 0))
2056         {
2057                 u32 yuv_phy[2];
2058                 struct rk29_ipp_req ipp_req;
2059                 static u32 dstoffset = 0;
2060                 static int fb_index = 0;
2061                 
2062                 yuv_phy[0] = last_yuv_phy[0];
2063                 yuv_phy[1] = last_yuv_phy[1];
2064                 yuv_phy[0] += par->y_offset;
2065                 yuv_phy[1] += par->c_offset;
2066         #if 0
2067                 if((var->rotate == 90) ||(var->rotate == 270))
2068         #else
2069         if(var->rotate%360 != 0)
2070         #endif
2071                         {
2072                                 dstoffset = (dstoffset+1)%2;
2073                                 ipp_req.src0.fmt = 3;
2074                                 ipp_req.src0.YrgbMst = yuv_phy[0];
2075                                 ipp_req.src0.CbrMst = yuv_phy[1];
2076                                 ipp_req.src0.w = var->xres;
2077                                 ipp_req.src0.h = var->yres;
2078                                 
2079                                 ipp_req.src_vir_w= (var->xres + 15) & (~15);
2080                                 ipp_req.dst_vir_w=screen->x_res;
2081
2082                                 ipp_req.dst0.fmt = 3;
2083                                 ipp_req.dst0.YrgbMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*2*dstoffset;
2084                                 ipp_req.dst0.CbrMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*(2*dstoffset+1);
2085                                    if(var->xres > screen->x_res)
2086                                    {
2087                                         ipp_req.dst0.w = screen->x_res;
2088                                         ipp_req.dst0.h = screen->y_res;
2089                                   }   else      {
2090                                           ipp_req.dst0.w = var->xres;
2091                                           ipp_req.dst0.h = var->yres;
2092                                    }
2093                                  ipp_req.dst_vir_w = (ipp_req.dst0.w + 15) & (~15);
2094
2095                                 ipp_req.timeout = 100;
2096                                 if(var->rotate == 90)
2097                                         ipp_req.flag = IPP_ROT_90;
2098                 else if (var->rotate == 180)
2099                                         ipp_req.flag = IPP_ROT_180;
2100                                 else if(var->rotate == 270)
2101                                         ipp_req.flag = IPP_ROT_270;
2102                                 //ipp_do_blit(&ipp_req);
2103                                 ipp_blit_sync(&ipp_req);
2104                                 fbprintk("yaddr=0x%x,uvaddr=0x%x\n",ipp_req.dst0.YrgbMst,ipp_req.dst0.CbrMst);
2105                                 yuv_phy[0] = ipp_req.dst0.YrgbMst;
2106                                 yuv_phy[1] = ipp_req.dst0.CbrMst;        
2107                                 fix->smem_start = yuv_phy[0];
2108                                 fix->mmio_start = yuv_phy[1];
2109                         }
2110                 else
2111                         {
2112                         
2113                                 fix->smem_start = yuv_phy[0];
2114                                 fix->mmio_start = yuv_phy[1];
2115                         }
2116                         
2117                 LcdWrReg(inf, WIN0_YRGB_MST, yuv_phy[0]);
2118                 LcdWrReg(inf, WIN0_CBR_MST, yuv_phy[1]);
2119                 // enable win0 after the win0 par is seted
2120                 LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(par->par_seted && par->addr_seted));
2121                 par->addr_seted = 1;
2122                 LcdWrReg(inf, REG_CFG_DONE, 0x01);
2123                 if(par->addr_seted ) {
2124                 unsigned long flags;
2125
2126                 local_irq_save(flags);
2127                 par->mirror.y_offset = yuv_phy[0];
2128                 par->mirror.c_offset = yuv_phy[1];
2129                 local_irq_restore(flags);
2130
2131                 mcu_refresh(inf);
2132                 //printk("0x%.8x 0x%.8x mirror\n", par->mirror.y_offset, par->mirror.c_offset);
2133                 }
2134
2135         }
2136
2137     has_set_rotate = false;
2138 #endif
2139     return 0;
2140 }
2141
2142 static int fb1_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
2143 {
2144     struct win0_par *par = info->par;
2145      // enable win0 after the win0 addr is seted
2146
2147     win0_pan(info);
2148     par->par_seted = 1;
2149     return 0;
2150 }
2151
2152 int fb1_open(struct fb_info *info, int user)
2153 {
2154     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
2155     struct win0_par *par = info->par;
2156
2157     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2158
2159     par->par_seted = 0;
2160     par->addr_seted = 0;
2161     inf->video_mode = 1;
2162     wq_condition2 = 1;
2163 #ifdef CONFIG_FB_ROTATE_VIDEO
2164    //reinitialize  the var when open,zyc
2165     last_yuv_phy[0] = 0;
2166     last_yuv_phy[1] = 0;
2167     has_set_rotate = 0;
2168 #endif
2169     if(par->refcount) {
2170         printk(">>>>>> fb1 has opened! \n");
2171         return -EACCES;
2172     } else {
2173         par->refcount++;
2174         win0_blank(FB_BLANK_NORMAL, info);
2175         fb0_set_par(inf->fb0);
2176         
2177             rk29fb_notify(inf, RK29FB_EVENT_FB1_ON);
2178         return 0;
2179     }
2180 }
2181
2182 int fb1_release(struct fb_info *info, int user)
2183 {
2184     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
2185     struct win0_par *par = info->par;
2186         struct fb_var_screeninfo *var0 = &info->var;
2187
2188     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2189     if(inf->setFlag == 0)
2190     {
2191         wait_event_interruptible_timeout(wq, inf->setFlag, HZ*10);
2192     } 
2193     if(par->refcount) {
2194         par->refcount--;
2195         inf->video_mode = 0;
2196         par->par_seted = 0;
2197         par->addr_seted = 0;
2198         win1_blank(FB_BLANK_NORMAL, info);
2199         fb0_set_par(inf->fb0);
2200
2201         // unmap memory
2202         info->screen_base = 0;
2203         info->fix.smem_start = 0;
2204         info->fix.smem_len = 0;
2205                 // clean the var param
2206                 memset(var0, 0, sizeof(struct fb_var_screeninfo));
2207             rk29fb_notify(inf, RK29FB_EVENT_FB1_OFF);
2208         #ifdef CONFIG_CLOSE_WIN1_DYNAMIC   
2209          cancel_delayed_work_sync(&rk29_win1_check_work);
2210         #endif  
2211     }
2212
2213     return 0;
2214 }
2215
2216 static int fb1_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
2217 {
2218     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
2219     struct win0_par *par = info->par;
2220     struct fb_fix_screeninfo *fix0 = &info->fix;
2221     struct fb_var_screeninfo *var = &info->var;
2222     void __user *argp = (void __user *)arg;
2223     
2224 #ifdef CONFIG_FB_ROTATE_VIDEO   
2225      struct rk29fb_screen *screen = inf->cur_screen;
2226      struct rk29_ipp_req ipp_req;
2227      static u32 dstoffset = 0;
2228      
2229 #endif
2230
2231         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2232     fbprintk("win0fb_ioctl cmd = %8x, arg = %8x \n", (u32)cmd, (u32)arg);
2233
2234         CHK_SUSPEND(inf);
2235
2236     switch(cmd)
2237     {
2238     case FB1_IOCTL_GET_PANEL_SIZE:    //get panel size
2239         {
2240             u32 panel_size[2];
2241              if((var->rotate == 270)||(var->rotate == 90)) {
2242                 panel_size[0] = inf->panel1_info.y_res; //inf->cur_screen->y_res; change for hdmi video size
2243                 panel_size[1] = inf->panel1_info.x_res;
2244             } else {
2245                 panel_size[0] = inf->panel1_info.x_res;
2246                 panel_size[1] = inf->panel1_info.y_res;
2247             }
2248
2249             if(copy_to_user(argp, panel_size, 8))  return -EFAULT;
2250         }
2251         break;
2252
2253     case FB1_IOCTL_SET_YUV_ADDR:    //set y&uv address to register direct
2254         {
2255             u32 yuv_phy[2];
2256             if (copy_from_user(yuv_phy, argp, 8))
2257                             return -EFAULT;
2258 #ifdef CONFIG_FB_ROTATE_VIDEO 
2259                 //add by zyc
2260                 if(has_set_rotate == true)
2261                 break;
2262                 last_yuv_phy[0] = yuv_phy[0];
2263                 last_yuv_phy[1] = yuv_phy[1];
2264 #endif
2265
2266             fix0->smem_start = yuv_phy[0];
2267             fix0->mmio_start = yuv_phy[1];
2268             yuv_phy[0] += par->y_offset;
2269             yuv_phy[1] += par->c_offset;
2270          
2271             #if 0
2272                 if((var->rotate == 90) ||(var->rotate == 270))
2273             #else
2274             if(var->rotate%360 != 0)
2275             #endif
2276             {
2277                 #ifdef CONFIG_FB_ROTATE_VIDEO 
2278                 dstoffset = (dstoffset+1)%2;
2279                 ipp_req.src0.fmt = 3;
2280                 ipp_req.src0.YrgbMst = yuv_phy[0];
2281                 ipp_req.src0.CbrMst = yuv_phy[1];
2282                   ipp_req.src0.w = var->xres ;
2283                 ipp_req.src0.h = var->yres ;
2284                  ipp_req.src_vir_w= (var->xres + 15) & (~15);
2285                  ipp_req.dst_vir_w=screen->x_res;
2286
2287                 ipp_req.dst0.fmt = 3;
2288                 ipp_req.dst0.YrgbMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*2*dstoffset;
2289                 ipp_req.dst0.CbrMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*(2*dstoffset+1);
2290                 if(var->xres > screen->x_res)
2291                 {
2292                     ipp_req.dst0.w = screen->x_res;
2293                     ipp_req.dst0.h = screen->y_res;
2294                 }   else  {
2295                                         ipp_req.dst0.w = var->xres;
2296                                         ipp_req.dst0.h = var->yres;
2297
2298                 }
2299                 ipp_req.dst_vir_w = (ipp_req.dst0.w + 15) & (~15);
2300                 ipp_req.timeout = 100;
2301                 if(var->rotate == 90)
2302                     ipp_req.flag = IPP_ROT_90;
2303                 else if(var->rotate == 180)
2304                     ipp_req.flag = IPP_ROT_180;
2305                 else if(var->rotate == 270)
2306                     ipp_req.flag = IPP_ROT_270;
2307                 //ipp_do_blit(&ipp_req);
2308                 ipp_blit_sync(&ipp_req);
2309                                 
2310                 fbprintk("yaddr=0x%x,uvaddr=0x%x\n",ipp_req.dst0.YrgbMst,ipp_req.dst0.CbrMst);
2311                 yuv_phy[0] = ipp_req.dst0.YrgbMst;
2312                 yuv_phy[1] = ipp_req.dst0.CbrMst;    
2313                 fix0->smem_start = yuv_phy[0];
2314                 fix0->mmio_start = yuv_phy[1];
2315                 #else //CONFIG_FB_ROTATE_VIDEO
2316                 printk("LCDC not support rotate!\n");
2317                 #endif
2318             }
2319       
2320             LcdWrReg(inf, WIN0_YRGB_MST, yuv_phy[0]);
2321             LcdWrReg(inf, WIN0_CBR_MST, yuv_phy[1]);
2322             // enable win0 after the win0 par is seted
2323             LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(par->par_seted && par->addr_seted));
2324             par->addr_seted = 1;
2325             LcdWrReg(inf, REG_CFG_DONE, 0x01);
2326             if(par->par_seted) {
2327                 unsigned long flags;
2328
2329                 local_irq_save(flags);
2330                 par->mirror.y_offset = yuv_phy[0];
2331                 par->mirror.c_offset = yuv_phy[1];
2332                 local_irq_restore(flags);
2333
2334                 mcu_refresh(inf);
2335                 //printk("0x%.8x 0x%.8x mirror\n", par->mirror.y_offset, par->mirror.c_offset);
2336             }
2337         }
2338         break;
2339
2340     case FB1_IOCTL_SET_ROTATE:    //change MCU panel scan direction
2341         fbprintk(">>>>>> change lcdc direction(%d) \n", (int)arg);
2342       #ifdef CONFIG_FB_ROTATE_VIDEO 
2343                 //zyc add
2344         has_set_rotate = true;
2345         if(arg == 0 || arg==180)
2346             var->rotate = arg;    
2347         else if (arg == 90 || arg==270)
2348             var->rotate = arg;  
2349       #else //CONFIG_FB_ROTATE_VIDEO
2350         printk("LCDC not support rotate!\n");
2351       #endif
2352         break;
2353     case FB1_IOCTL_SET_WIN0_TOP:
2354         fbprintk(">>>>>> FB1_IOCTL_SET_WIN0_TOP %d\n",arg);
2355         LcdMskReg(inf, DSP_CTRL0, m_W0_ON_TOP, v_W0_ON_TOP(arg));
2356         LcdWrReg(inf, REG_CFG_DONE, 0x01);
2357         break;
2358     default:
2359         break;
2360     }
2361     return 0;
2362 }
2363
2364 static struct fb_ops fb1_ops = {
2365         .owner          = THIS_MODULE,
2366         .fb_open    = fb1_open,
2367         .fb_release = fb1_release,
2368         .fb_check_var   = fb1_check_var,
2369         .fb_set_par     = fb1_set_par,
2370         .fb_blank       = fb1_blank,
2371     .fb_pan_display = fb1_pan_display,
2372     .fb_ioctl = fb1_ioctl,
2373         .fb_setcolreg   = fb_setcolreg,
2374         .fb_fillrect    = cfb_fillrect,
2375         .fb_copyarea    = cfb_copyarea,
2376         .fb_imageblit   = cfb_imageblit,
2377 };
2378
2379 static struct fb_ops fb0_ops = {
2380         .owner          = THIS_MODULE,
2381         .fb_check_var   = fb0_check_var,
2382         .fb_set_par = fb0_set_par,
2383         .fb_blank   = fb0_blank,
2384         .fb_pan_display = fb0_pan_display,
2385     .fb_ioctl = fb0_ioctl,
2386         .fb_setcolreg   = fb_setcolreg,
2387         .fb_fillrect    = cfb_fillrect,
2388         .fb_copyarea    = cfb_copyarea,
2389         .fb_imageblit   = cfb_imageblit,
2390         //.fb_cursor      = rk29_set_cursor,
2391 };
2392
2393 int fb_get_video_mode(void)
2394 {
2395         struct rk29fb_inf *inf;
2396         if(!g_pdev)
2397                 return 0;
2398         inf = platform_get_drvdata(g_pdev);
2399         return inf->video_mode;
2400 }
2401 /*
2402 enable: 1, switch to tv or hdmi; 0, switch to lcd
2403 */
2404 int FB_Switch_Screen( struct rk29fb_screen *screen, u32 enable )
2405 {
2406     struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
2407    // struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2408     struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2409
2410     memcpy(&inf->panel2_info, screen, sizeof( struct rk29fb_screen ));
2411
2412     if(enable)inf->cur_screen = &inf->panel2_info;
2413     else inf->cur_screen = &inf->panel1_info;
2414
2415     /* Black out, because some display device need clock to standby */
2416     //LcdMskReg(inf, DSP_CTRL_REG1, m_BLACK_OUT, v_BLACK_OUT(1));
2417    // LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(0));
2418    // LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
2419     LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(1));
2420     LcdWrReg(inf, REG_CFG_DONE, 0x01);
2421     wake_lock(&idlelock);
2422     msleep(20);
2423     wake_unlock(&idlelock);
2424
2425     if(inf->cur_screen->standby)    inf->cur_screen->standby(1);
2426     // operate the display_on pin to power down the lcd
2427    
2428     if(enable && mach_info->io_disable)mach_info->io_disable();  //close lcd out
2429     else if (mach_info->io_enable)mach_info->io_enable();       //open lcd out
2430     
2431     load_screen(inf->fb0, 0);
2432         mcu_refresh(inf);
2433
2434     fb1_set_par(inf->fb1);
2435     fb0_set_par(inf->fb0);
2436     LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(0));
2437     LcdWrReg(inf, REG_CFG_DONE, 0x01);
2438     rk29fb_notify(inf, enable ? RK29FB_EVENT_HDMI_ON : RK29FB_EVENT_HDMI_OFF);
2439     return 0;
2440 }
2441
2442 static ssize_t dsp_win0_info_read(struct device *device,
2443                             struct device_attribute *attr, char *buf)
2444 {
2445     //char * s = _buf;
2446     struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
2447     struct rk29fb_screen *screen = inf->cur_screen;
2448
2449     u16 xpos=0,ypos=0,xsize=0,ysize=0;
2450
2451     fbprintk("%s\n",__FUNCTION__);
2452     xpos = LcdRdReg(inf, WIN0_DSP_ST) & 0xffff;
2453     ypos = (LcdRdReg(inf, WIN0_DSP_ST)>>16) & 0xffff;
2454
2455     xpos -= (screen->left_margin + screen->hsync_len);
2456     ypos -= (screen->upper_margin + screen->vsync_len);
2457
2458     xsize=LcdRdReg(inf, WIN0_DSP_INFO)& 0xffff;
2459     ysize=(LcdRdReg(inf, WIN0_DSP_INFO)>>16) & 0xffff;
2460     fbprintk("%s %d , %d, %d ,%d\n",__FUNCTION__,xpos,ypos,xsize,ysize);
2461         rk29_set_cursor_test(inf->fb0);
2462
2463     return snprintf(buf, PAGE_SIZE, "%d,%d,%d,%d\n", xpos,ypos,xsize,ysize);
2464 }
2465
2466 static ssize_t dsp_win0_info_write(struct device *device,
2467                            struct device_attribute *attr, const char *buf, size_t count)
2468 {
2469      printk("%s\n",__FUNCTION__);
2470      printk("%s %x \n",__FUNCTION__,*buf);
2471      return count;
2472 }
2473
2474 static DEVICE_ATTR(dsp_win0_info,  S_IRUGO|S_IWUSR, dsp_win0_info_read, dsp_win0_info_write);
2475
2476 static irqreturn_t rk29fb_irq(int irq, void *dev_id)
2477 {
2478         struct platform_device *pdev = (struct platform_device*)dev_id;
2479     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
2480     struct win0_par *par = (struct win0_par *)inf->fb1->par;
2481     if(!inf)
2482         return IRQ_HANDLED;
2483
2484         //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2485
2486     LcdMskReg(inf, INT_STATUS, m_FRM_STARTCLEAR, v_FRM_STARTCLEAR(1));
2487
2488         if(SCREEN_MCU == inf->cur_screen->type)
2489         {
2490         inf->mcu_isrcnt = !inf->mcu_isrcnt;
2491         if(inf->mcu_isrcnt)
2492             return IRQ_HANDLED;
2493
2494         if(IsMcuUseFmk())
2495         {
2496             if(LcdReadBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST) && (inf->mcu_fmksync == 0))
2497             {
2498                 inf->mcu_fmksync = 1;
2499                  if(inf->cur_screen->refresh)
2500                    inf->cur_screen->refresh(REFRESH_END);
2501                 inf->mcu_fmksync = 0;
2502             }
2503             else
2504             {
2505                 return IRQ_HANDLED;
2506             }
2507         }
2508         else
2509         {
2510             if(inf->mcu_needflush) {
2511                 if(inf->cur_screen->refresh)
2512                     inf->cur_screen->refresh(REFRESH_PRE);
2513                 inf->mcu_needflush = 0;
2514                 inf->mcu_isrcnt = 0;
2515                 LcdSetRegisterBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST);
2516             } else {
2517                 if(inf->cur_screen->refresh)
2518                     inf->cur_screen->refresh(REFRESH_END);
2519             }
2520         }
2521         }
2522 #if ANDROID_USE_THREE_BUFS
2523     new_frame_seted = 1;
2524 #endif
2525
2526     if(waitqueue_active(&par->wait)) {
2527         if (par->mirror.c_offset == 0)
2528             printk("error: no new buffer to display\n");
2529
2530         par->done.y_offset = par->displ.y_offset;
2531         par->done.c_offset = par->displ.c_offset;
2532         par->displ.y_offset = par->mirror.y_offset;
2533         par->displ.c_offset = par->mirror.c_offset;
2534         par->mirror.y_offset = 0;
2535         par->mirror.c_offset = 0;
2536
2537         //printk("0x%.8x 0x%.8x displaying\n", par->displ.y_offset, par->displ.c_offset);
2538         //printk("0x%.8x 0x%.8x done\n", par->done.y_offset, par->done.c_offset);
2539         wake_up_interruptible(&par->wait);
2540     }
2541
2542     wq_condition2 = 1;
2543         wq_condition = 1;
2544         wake_up_interruptible(&wq);
2545
2546         rk29fb_irq_notify_ddr();
2547         return IRQ_HANDLED;
2548 }
2549
2550 #ifdef CONFIG_HAS_EARLYSUSPEND
2551
2552 struct suspend_info {
2553         struct early_suspend early_suspend;
2554         struct rk29fb_inf *inf;
2555 };
2556
2557 static void rk29fb_early_suspend(struct early_suspend *h)
2558 {
2559         struct suspend_info *info = container_of(h, struct suspend_info,
2560                                                 early_suspend);
2561
2562     struct rk29fb_inf *inf = info->inf;
2563     struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2564
2565     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2566
2567     if(!inf) {
2568         printk("inf==0, rk29fb_suspend fail! \n");
2569         return;
2570     }
2571
2572 #ifdef CONFIG_CLOSE_WIN1_DYNAMIC   
2573      cancel_delayed_work_sync(&rk29_win1_check_work);
2574 #endif  
2575
2576     if((inf->cur_screen != &inf->panel2_info) && mach_info->io_disable)  // close lcd pwr when output screen is lcd
2577        mach_info->io_disable();  //close lcd out 
2578
2579         if(inf->cur_screen->standby)
2580         {
2581                 fbprintk(">>>>>> power down the screen! \n");
2582                 inf->cur_screen->standby(1);
2583         }
2584
2585     LcdMskReg(inf, DSP_CTRL0, m_HSYNC_POLARITY | m_VSYNC_POLARITY | m_DEN_POLARITY ,
2586        v_HSYNC_POLARITY(1) | v_VSYNC_POLARITY(1) | v_DEN_POLARITY(1) );
2587
2588     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(1));
2589     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(1));
2590         LcdWrReg(inf, REG_CFG_DONE, 0x01);
2591
2592         if(!inf->in_suspend)
2593         {
2594                 fbprintk(">>>>>> diable the lcdc clk! \n");
2595                 wake_lock(&idlelock);
2596                 msleep(100);
2597                 wake_unlock(&idlelock);
2598         clk_disable(inf->aclk_ddr_lcdc);
2599         clk_disable(inf->aclk_disp_matrix);
2600         clk_disable(inf->hclk_cpu_display);
2601         clk_disable(inf->clk);
2602         if (inf->dclk){
2603             clk_disable(inf->dclk);
2604         }
2605         if(inf->clk){
2606             clk_disable(inf->aclk);
2607         }
2608         //clk_disable(inf->pd_display);
2609
2610                 inf->in_suspend = 1;
2611         }
2612 }
2613
2614 static void rk29fb_early_resume(struct early_suspend *h)
2615 {
2616         struct suspend_info *info = container_of(h, struct suspend_info,
2617                                         early_suspend);
2618
2619     struct rk29fb_inf *inf = info->inf;
2620     struct rk29fb_screen *screen = inf->cur_screen;
2621     struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2622
2623     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2624     if(!inf) {
2625         printk("inf==0, rk29fb_resume fail! \n");
2626         return ;
2627     }
2628
2629     if(inf->in_suspend)
2630         {
2631             inf->in_suspend = 0;
2632         fbprintk(">>>>>> enable the lcdc clk! \n");
2633        // clk_enable(inf->pd_display);
2634         clk_enable(inf->aclk_disp_matrix);
2635         clk_enable(inf->hclk_cpu_display);
2636         clk_enable(inf->clk);
2637         clk_enable(inf->aclk_ddr_lcdc);
2638
2639         if (inf->dclk){
2640             clk_enable(inf->dclk);
2641         }
2642         if(inf->clk){
2643             clk_enable(inf->aclk);
2644         }
2645         usleep_range(100*1000, 100*1000);
2646         }
2647     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(0));
2648     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(0));
2649     LcdWrReg(inf, REG_CFG_DONE, 0x01);
2650
2651     LcdMskReg(inf, DSP_CTRL0, m_HSYNC_POLARITY | m_VSYNC_POLARITY | m_DEN_POLARITY ,
2652        v_HSYNC_POLARITY(screen->pin_hsync) | v_VSYNC_POLARITY(screen->pin_vsync) | v_DEN_POLARITY(screen->pin_den) );
2653
2654         if(inf->cur_screen->standby)
2655         {
2656                 fbprintk(">>>>>> power on the screen! \n");
2657                 inf->cur_screen->standby(0);
2658         }
2659     usleep_range(10*1000, 10*1000);
2660     memcpy((u8*)inf->preg, (u8*)&inf->regbak, 0xa4);  //resume reg
2661     usleep_range(40*1000, 40*1000);
2662     
2663     if((inf->cur_screen != &inf->panel2_info) && mach_info->io_enable)  // open lcd pwr when output screen is lcd
2664        mach_info->io_enable();  //close lcd out 
2665         
2666 }
2667
2668 static struct suspend_info suspend_info = {
2669         .early_suspend.suspend = rk29fb_early_suspend,
2670         .early_suspend.resume = rk29fb_early_resume,
2671         .early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB,
2672 };
2673 #endif
2674 struct fb_info *g_fb0_inf = NULL;  //add cym@rk 20101027 for charger logo
2675 static int __init rk29fb_probe (struct platform_device *pdev)
2676 {
2677     struct rk29fb_inf *inf = NULL;
2678     struct resource *res = NULL;
2679     struct resource *mem = NULL;
2680     struct rk29fb_info *mach_info = NULL;
2681     struct rk29fb_screen *screen = NULL;
2682     struct win0_par* par = NULL;
2683         int irq = 0;
2684     int ret = 0;
2685
2686     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2687
2688     /* Malloc rk29fb_inf and set it to pdev for drvdata */
2689     fbprintk(">> Malloc rk29fb_inf and set it to pdev for drvdata \n");
2690     inf = kmalloc(sizeof(struct rk29fb_inf), GFP_KERNEL);
2691     if(!inf)
2692     {
2693         dev_err(&pdev->dev, ">> inf kmalloc fail!");
2694         ret = -ENOMEM;
2695                 goto release_drvdata;
2696     }
2697     memset(inf, 0, sizeof(struct rk29fb_inf));
2698         platform_set_drvdata(pdev, inf);
2699
2700     mach_info = pdev->dev.platform_data;
2701     /* Fill screen info and set current screen */
2702     fbprintk(">> Fill screen info and set current screen \n");
2703    #ifdef CONFIG_DEFAULT_OUT_HDMI  // set hdmi for default output 
2704     hdmi_get_default_resolution(&inf->panel1_info);
2705    #else
2706     set_lcd_info(&inf->panel1_info, mach_info->lcd_info);
2707    #endif
2708     inf->cur_screen = &inf->panel1_info;
2709     screen = inf->cur_screen;
2710     if(SCREEN_NULL==screen->type)
2711     {
2712         dev_err(&pdev->dev, ">> Please select a display device! \n");
2713         ret = -EINVAL;
2714                 goto release_drvdata;
2715     }
2716
2717     /* get virtual basic address of lcdc register */
2718     fbprintk(">> get virtual basic address of lcdc register \n");
2719     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcdc reg");
2720     if (res == NULL)
2721     {
2722         dev_err(&pdev->dev, "failed to get memory registers\n");
2723         ret = -ENOENT;
2724                 goto release_drvdata;
2725     }
2726     inf->reg_phy_base = res->start;
2727     inf->len = (res->end - res->start) + 1;
2728     mem = request_mem_region(inf->reg_phy_base, inf->len, pdev->name);
2729     if (mem == NULL)
2730     {
2731         dev_err(&pdev->dev, "failed to get memory region\n");
2732         ret = -ENOENT;
2733                 goto release_drvdata;
2734     }
2735     fbprintk("inf->reg_phy_base = 0x%08x, inf->len = %d \n", inf->reg_phy_base, inf->len);
2736     inf->reg_vir_base = ioremap(inf->reg_phy_base, inf->len);
2737     if (inf->reg_vir_base == NULL)
2738     {
2739         dev_err(&pdev->dev, "ioremap() of registers failed\n");
2740         ret = -ENXIO;
2741                 goto release_drvdata;
2742     }
2743     inf->preg = (LCDC_REG*)inf->reg_vir_base;
2744
2745     /* Prepare win1 info */
2746     fbprintk(">> Prepare win1 info \n");
2747         inf->fb0 = framebuffer_alloc(sizeof(struct win0_par), &pdev->dev);
2748     if(!inf->fb0)
2749     {
2750         dev_err(&pdev->dev, ">> fb0 framebuffer_alloc fail!");
2751                 inf->fb0 = NULL;
2752         ret = -ENOMEM;
2753                 goto release_win1fb;
2754     }
2755
2756     par = (struct win0_par*)inf->fb0->par;
2757     strcpy(inf->fb0->fix.id, "fb0");
2758     inf->fb0->fix.type        = FB_TYPE_PACKED_PIXELS;
2759     inf->fb0->fix.type_aux    = 0;
2760     inf->fb0->fix.xpanstep    = 1;
2761     inf->fb0->fix.ypanstep    = 1;
2762     inf->fb0->fix.ywrapstep   = 0;
2763     inf->fb0->fix.accel       = FB_ACCEL_NONE;
2764     inf->fb0->fix.visual      = FB_VISUAL_TRUECOLOR;
2765     inf->fb0->fix.smem_len    = 0;
2766     inf->fb0->fix.line_length = 0;
2767     inf->fb0->fix.smem_start  = 0;
2768
2769     inf->fb0->var.xres = screen->x_res;
2770     inf->fb0->var.yres = screen->y_res;
2771     inf->fb0->var.bits_per_pixel = 16;
2772     inf->fb0_color_deepth = 0;
2773     inf->fb0->var.xres_virtual = screen->x_res;
2774     inf->fb0->var.yres_virtual = screen->y_res;
2775     inf->fb0->var.width = screen->width;
2776     inf->fb0->var.height = screen->height;
2777     inf->fb0->var.pixclock = div_u64(1000000000000llu, screen->pixclock);
2778     inf->fb0->var.left_margin = screen->left_margin;
2779     inf->fb0->var.right_margin = screen->right_margin;
2780     inf->fb0->var.upper_margin = screen->upper_margin;
2781     inf->fb0->var.lower_margin = screen->lower_margin;
2782     inf->fb0->var.vsync_len = screen->vsync_len;
2783     inf->fb0->var.hsync_len = screen->hsync_len;
2784     inf->fb0->var.red    = def_rgb_16.red;
2785     inf->fb0->var.green  = def_rgb_16.green;
2786     inf->fb0->var.blue   = def_rgb_16.blue;
2787     inf->fb0->var.transp = def_rgb_16.transp;
2788
2789     inf->fb0->var.nonstd      = 0;  //win1 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
2790     inf->fb0->var.grayscale   = 0;  //win1 transprent mode & value(mode<<8 + value)
2791     inf->fb0->var.activate    = FB_ACTIVATE_NOW;
2792     inf->fb0->var.accel_flags = 0;
2793     inf->fb0->var.vmode       = FB_VMODE_NONINTERLACED;
2794
2795     inf->fb0->fbops           = &fb0_ops;
2796     inf->fb0->flags           = FBINFO_FLAG_DEFAULT;
2797     inf->fb0->pseudo_palette  = par->pseudo_pal;
2798     inf->fb0->screen_base     = 0;
2799
2800     memset(par, 0, sizeof(struct win0_par));
2801
2802         ret = fb_alloc_cmap(&inf->fb0->cmap, 256, 0);
2803         if (ret < 0)
2804                 goto release_cmap;
2805
2806         g_fb0_inf = inf->fb0; //add cym@rk 20101027
2807
2808     /* alloc win1 buf */
2809     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "win1 buf");
2810     if (res == NULL)
2811     {
2812         dev_err(&pdev->dev, "failed to get win1 memory \n");
2813         ret = -ENOENT;
2814         goto release_win1fb;
2815     }
2816     inf->fb0->fix.smem_start = res->start;
2817     inf->fb0->fix.smem_len = res->end - res->start + 1;
2818     inf->fb0->screen_base = ioremap(res->start, inf->fb0->fix.smem_len);
2819     memset(inf->fb0->screen_base, 0, inf->fb0->fix.smem_len);
2820
2821     #ifdef CONFIG_FB_WORK_IPP
2822        /* alloc win1 ipp buf */
2823     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "win1 ipp buf");
2824     if (res == NULL)
2825     {
2826         dev_err(&pdev->dev, "failed to get win1 ipp memory \n");
2827         ret = -ENOENT;
2828         goto release_win1fb;
2829     }
2830     inf->fb0->fix.mmio_start = res->start;
2831     inf->fb0->fix.mmio_len = res->end - res->start + 1;
2832     #endif
2833
2834     /* Prepare win0 info */
2835     fbprintk(">> Prepare win0 info \n");
2836     inf->fb1 = framebuffer_alloc(sizeof(struct win0_par), &pdev->dev);
2837     if(!inf->fb1)
2838     {
2839         dev_err(&pdev->dev, ">> fb1 framebuffer_alloc fail!");
2840                 inf->fb1 = NULL;
2841                 ret = -ENOMEM;
2842                 goto release_win0fb;
2843     }
2844
2845     par = (struct win0_par*)inf->fb1->par;
2846
2847     strcpy(inf->fb1->fix.id, "fb1");
2848         inf->fb1->fix.type            = FB_TYPE_PACKED_PIXELS;
2849         inf->fb1->fix.type_aux    = 0;
2850         inf->fb1->fix.xpanstep    = 1;
2851         inf->fb1->fix.ypanstep    = 1;
2852         inf->fb1->fix.ywrapstep   = 0;
2853         inf->fb1->fix.accel       = FB_ACCEL_NONE;
2854     inf->fb1->fix.visual      = FB_VISUAL_TRUECOLOR;
2855     inf->fb1->fix.smem_len    = 0;
2856     inf->fb1->fix.line_length = 0;
2857     inf->fb1->fix.smem_start  = 0;
2858
2859     inf->fb1->var.xres = screen->x_res;
2860     inf->fb1->var.yres = screen->y_res;
2861     inf->fb1->var.bits_per_pixel = 16;
2862     inf->fb1->var.xres_virtual = screen->x_res;
2863     inf->fb1->var.yres_virtual = screen->y_res;
2864     inf->fb1->var.width = screen->width;
2865     inf->fb1->var.height = screen->height;
2866     inf->fb1->var.pixclock = div_u64(1000000000000llu, screen->pixclock);
2867     inf->fb1->var.left_margin = screen->left_margin;
2868     inf->fb1->var.right_margin = screen->right_margin;
2869     inf->fb1->var.upper_margin = screen->upper_margin;
2870     inf->fb1->var.lower_margin = screen->lower_margin;
2871     inf->fb1->var.vsync_len = screen->vsync_len;
2872     inf->fb1->var.hsync_len = screen->hsync_len;
2873     inf->fb1->var.red    = def_rgb_16.red;
2874     inf->fb1->var.green  = def_rgb_16.green;
2875     inf->fb1->var.blue   = def_rgb_16.blue;
2876     inf->fb1->var.transp = def_rgb_16.transp;
2877
2878     inf->fb1->var.nonstd      = 0;  //win0 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
2879     inf->fb1->var.grayscale   = ((inf->fb1->var.yres<<20)&0xfff00000) + ((inf->fb1->var.xres<<8)&0xfff00);//win0 xsize & ysize
2880     inf->fb1->var.activate    = FB_ACTIVATE_NOW;
2881     inf->fb1->var.accel_flags = 0;
2882     inf->fb1->var.vmode       = FB_VMODE_NONINTERLACED;
2883
2884     inf->fb1->fbops           = &fb1_ops;
2885         inf->fb1->flags               = FBINFO_FLAG_DEFAULT;
2886         inf->fb1->pseudo_palette  = par->pseudo_pal;
2887         inf->fb1->screen_base     = 0;
2888
2889     memset(par, 0, sizeof(struct win0_par));
2890
2891         init_waitqueue_head(&par->wait);
2892
2893         /* Init all lcdc and lcd before register_framebuffer. */
2894         /* because after register_framebuffer, the win1fb_check_par and winfb_set_par execute immediately */
2895         fbprintk(">> Init all lcdc and lcd before register_framebuffer \n");
2896     init_lcdc(inf->fb0);
2897
2898   #if 0 //def CONFIG_CPU_FREQ
2899    // inf->freq_transition.notifier_call = rk29fb_freq_transition;
2900    // cpufreq_register_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
2901   #endif
2902         fbprintk("got clock\n");
2903
2904         if(mach_info)
2905     {
2906         struct rk29_fb_setting_info fb_setting;
2907         if( OUT_P888==inf->cur_screen->face )     // set lcdc iomux
2908         {
2909             fb_setting.data_num = 24;
2910         }
2911         else if(OUT_P666 == inf->cur_screen->face )
2912         {
2913             fb_setting.data_num = 18;
2914         }
2915         else
2916         {
2917             fb_setting.data_num = 16;
2918         }
2919         fb_setting.den_en = 1;
2920         fb_setting.vsync_en = 1;
2921         fb_setting.disp_on_en = 1;
2922         fb_setting.standby_en = 1;
2923         if( inf->cur_screen->mcu_usefmk )
2924             fb_setting.mcu_fmk_en =1;
2925         mach_info->io_init(&fb_setting);
2926     }
2927
2928         //set_lcd_pin(pdev, 1);
2929         mdelay(10);
2930         g_pdev = pdev;
2931         inf->mcu_usetimer = 1;
2932     inf->mcu_fmksync = 0;
2933         load_screen(inf->fb0, 1);
2934
2935     /* Register framebuffer(fb0 & fb1) */
2936     fbprintk(">> Register framebuffer(fb0) \n");
2937     ret = register_framebuffer(inf->fb0);
2938     if(ret<0)
2939     {
2940         printk(">> fb0 register_framebuffer fail!\n");
2941         ret = -EINVAL;
2942                 goto release_win0fb;
2943     }
2944     fbprintk(">> Register framebuffer(fb1) \n");
2945
2946     ret = register_framebuffer(inf->fb1);
2947     if(ret<0)
2948     {
2949         printk(">> fb1 register_framebuffer fail!\n");
2950         ret = -EINVAL;
2951                 goto unregister_win1fb;
2952     }
2953
2954     ret = device_create_file(inf->fb1->dev, &dev_attr_dsp_win0_info);
2955     if(ret)
2956     {
2957         printk(">> fb1 dsp win0 info device_create_file err\n");
2958         ret = -EINVAL;
2959     }
2960 #ifdef CONFIG_HAS_EARLYSUSPEND
2961         suspend_info.inf = inf;
2962         register_early_suspend(&suspend_info.early_suspend);
2963 #endif
2964
2965     /* get and request irq */
2966     fbprintk(">> get and request irq \n");
2967     irq = platform_get_irq(pdev, 0);
2968     if (irq < 0) {
2969         dev_err(&pdev->dev, "no irq for device\n");
2970         ret = -ENOENT;
2971         goto unregister_win1fb;
2972     }
2973     ret = request_irq(irq, rk29fb_irq, IRQF_DISABLED, pdev->name, pdev);
2974     if (ret) {
2975         dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
2976         ret = -EBUSY;
2977         goto release_irq;
2978     }
2979
2980     if( inf->cur_screen->mcu_usefmk && (mach_info->mcu_fmk_pin != -1) )
2981     {
2982         ret = request_irq(gpio_to_irq(mach_info->mcu_fmk_pin), mcu_irqfmk, GPIOEdgelFalling, pdev->name, pdev);
2983         if (ret)
2984         {
2985             dev_err(&pdev->dev, "cannot get fmk irq %d - err %d\n", irq, ret);
2986             ret = -EBUSY;
2987             goto release_irq;
2988         }
2989     }
2990
2991 #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
2992     fb0_set_par(inf->fb0);
2993     if (fb_prepare_logo(inf->fb0, FB_ROTATE_UR)) {
2994         /* Start display and show logo on boot */
2995         fb_set_cmap(&inf->fb0->cmap, inf->fb0);
2996         fb_show_logo(inf->fb0, FB_ROTATE_UR);
2997         fb0_blank(FB_BLANK_UNBLANK, inf->fb0);
2998     }
2999 #endif
3000
3001     printk(" %s ok\n", __FUNCTION__);
3002     return ret;
3003
3004 release_irq:
3005         if(irq>=0)
3006         free_irq(irq, pdev);
3007 unregister_win1fb:
3008     unregister_framebuffer(inf->fb0);
3009 release_win0fb:
3010         if(inf->fb1)
3011                 framebuffer_release(inf->fb1);
3012         inf->fb1 = NULL;
3013 release_cmap:
3014     if(&inf->fb0->cmap)
3015         fb_dealloc_cmap(&inf->fb0->cmap);
3016 release_win1fb:
3017         if(inf->fb0)
3018                 framebuffer_release(inf->fb0);
3019         inf->fb0 = NULL;
3020 release_drvdata:
3021         if(inf && inf->reg_vir_base)
3022         iounmap(inf->reg_vir_base);
3023         if(inf && mem)
3024         release_mem_region(inf->reg_phy_base, inf->len);
3025         if(inf)
3026         kfree(inf);
3027         platform_set_drvdata(pdev, NULL);
3028         return ret;
3029 }
3030
3031 static int rk29fb_remove(struct platform_device *pdev)
3032 {
3033     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
3034     struct fb_info *info = NULL;
3035         //pm_message_t msg;
3036     struct rk29fb_info *mach_info = NULL;
3037     int irq = 0;
3038
3039         fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
3040
3041     if(!inf) {
3042         printk("inf==0, rk29_fb_remove fail! \n");
3043         return -EINVAL;
3044     }
3045     device_remove_file(inf->fb1->dev, &dev_attr_dsp_win0_info);
3046
3047     irq = platform_get_irq(pdev, 0);
3048     if (irq >0)
3049     {
3050     free_irq(irq, pdev);
3051     }
3052
3053     mach_info = pdev->dev.platform_data;
3054     if(mach_info->mcu_fmk_pin)
3055     {
3056         free_irq(gpio_to_irq(mach_info->mcu_fmk_pin), pdev);
3057     }
3058
3059     if(mach_info->io_disable)  
3060        mach_info->io_disable();  //close lcd out 
3061
3062     // blank the lcdc
3063     if(inf->fb1)
3064         fb1_blank(FB_BLANK_POWERDOWN, inf->fb1);
3065     if(inf->fb0)
3066         fb0_blank(FB_BLANK_POWERDOWN, inf->fb0);
3067
3068         // suspend the lcdc
3069         //rk29fb_suspend(pdev, msg);
3070     // unmap memory and release framebuffer
3071     if(inf->fb1) {
3072         info = inf->fb1;
3073         if (info->screen_base) {
3074                 //dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
3075                 info->screen_base = 0;
3076                 info->fix.smem_start = 0;
3077                 info->fix.smem_len = 0;
3078         }
3079         unregister_framebuffer(inf->fb1);
3080         framebuffer_release(inf->fb1);
3081         inf->fb1 = NULL;
3082     }
3083     if(inf->fb0) {
3084         info = inf->fb0;
3085         if (info->screen_base) {
3086             //    dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
3087                 info->screen_base = 0;
3088                 info->fix.smem_start = 0;
3089                 info->fix.smem_len = 0;
3090         }
3091         unregister_framebuffer(inf->fb0);
3092         framebuffer_release(inf->fb0);
3093         inf->fb0 = NULL;
3094     }
3095
3096   #if 0 //def CONFIG_CPU_FREQ
3097    // cpufreq_unregister_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
3098   #endif
3099
3100     msleep(100);
3101
3102         if (inf->clk)
3103     {
3104                 clk_disable(inf->clk);
3105                 clk_put(inf->clk);
3106                 inf->clk = NULL;
3107         }
3108     if (inf->dclk)
3109     {
3110                 clk_disable(inf->dclk);
3111                 clk_put(inf->dclk);
3112                 inf->dclk = NULL;
3113         }
3114
3115     kfree(inf);
3116     platform_set_drvdata(pdev, NULL);
3117
3118     return 0;
3119 }
3120
3121 static void rk29fb_shutdown(struct platform_device *pdev)
3122 {
3123     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
3124     struct rk29fb_info *mach_info = pdev->dev.platform_data;;
3125
3126         fbprintk("----------------------------rk29fb_shutdown----------------------------\n");
3127
3128     if(mach_info->io_disable)  
3129        mach_info->io_disable();  //close lcd out 
3130        
3131     if(!inf->in_suspend)
3132     {
3133         LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(1));
3134         LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(1));
3135         LcdWrReg(inf, REG_CFG_DONE, 0x01);
3136         mdelay(100);
3137         clk_disable(inf->aclk_ddr_lcdc);
3138         clk_disable(inf->aclk_disp_matrix);
3139         clk_disable(inf->hclk_cpu_display);
3140         clk_disable(inf->clk);
3141         if(inf->dclk){
3142             clk_disable(inf->dclk);
3143         }
3144         if(inf->clk){
3145             clk_disable(inf->aclk);
3146         }
3147         clk_disable(inf->pd_display);
3148         //pmu_set_power_domain(PD_DISPLAY, 0);
3149                 inf->in_suspend = 1;
3150         }
3151
3152 }
3153
3154 static struct platform_driver rk29fb_driver = {
3155         .probe          = rk29fb_probe,
3156         .remove         = rk29fb_remove,
3157         .driver         = {
3158                 .name   = "rk29-fb",
3159                 .owner  = THIS_MODULE,
3160         },
3161         .shutdown   = rk29fb_shutdown,
3162 };
3163
3164 static int __init rk29fb_init(void)
3165 {
3166         wake_lock_init(&idlelock, WAKE_LOCK_IDLE, "fb");
3167     return platform_driver_register(&rk29fb_driver);
3168 }
3169
3170 static void __exit rk29fb_exit(void)
3171 {
3172     platform_driver_unregister(&rk29fb_driver);
3173 }
3174
3175 fs_initcall(rk29fb_init);
3176 module_exit(rk29fb_exit);
3177
3178
3179 MODULE_AUTHOR("  zyw@rock-chips.com");
3180 MODULE_DESCRIPTION("Driver for rk29 fb device");
3181 MODULE_LICENSE("GPL");
3182
3183