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