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