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