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