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