rk29fb:add config FB_SCALING_OSD_1080P for scale UI in 1080p 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 == 270)||(var->rotate == 90) || (var->rotate == 180)) && (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 == 270)||(var->rotate == 90)) {
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 == 270)||(var->rotate == 90))
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
2016         CHK_SUSPEND(inf);
2017     if((var->rotate == 270)||(var->rotate == 90))
2018     {
2019         #ifdef CONFIG_FB_ROTATE_VIDEO
2020         xpos = (var->nonstd>>20) & 0xfff;      //visiable pos in panel
2021         ypos = (var->nonstd>>8) & 0xfff;
2022         xsize = (var->grayscale>>20) & 0xfff;  //visiable size in panel
2023         ysize = (var->grayscale>>8) & 0xfff;
2024         #else //CONFIG_FB_ROTATE_VIDEO
2025         printk("LCDC not support rotate!\n");
2026         return -EINVAL;
2027       #endif
2028     } else{
2029         xpos = (xpos * screen->x_res) / inf->panel1_info.x_res;
2030         ypos = (ypos * screen->y_res) / inf->panel1_info.y_res;
2031         xsize = (xsize * screen->x_res) / inf->panel1_info.x_res;
2032         ysize = (ysize * screen->y_res) / inf->panel1_info.y_res;
2033     }
2034         /* calculate y_offset,c_offset,line_length,cblen and crlen  */
2035     switch (data_format)
2036     {
2037     case 0: // rgb
2038         switch(var->bits_per_pixel)
2039         {
2040         case 16:    // rgb565
2041             format = 1;
2042             fix->line_length = 2 * xvir;
2043             par->y_offset = (yact_st*xvir + xact_st)*2;
2044             break;
2045         case 32:    // rgb888
2046             format = 0;
2047             fix->line_length = 4 * xvir;
2048             par->y_offset = (yact_st*xvir + xact_st)*4;
2049             break;
2050         default:
2051             return -EINVAL;
2052         }
2053         break;
2054     case 1: // yuv422
2055         format = 2;
2056         fix->line_length = xvir;
2057         cblen = crlen = (xvir*yvir)/2;
2058         par->y_offset = yact_st*xvir + xact_st;
2059         par->c_offset = yact_st*xvir + xact_st;
2060         break;
2061     case 2: // yuv4200
2062         format = 3;
2063         fix->line_length = xvir;
2064         cblen = crlen = (xvir*yvir)/4;
2065
2066         par->y_offset = yact_st*xvir + xact_st;
2067         par->c_offset = (yact_st/2)*xvir + xact_st;
2068
2069         break;
2070     case 3: // yuv4201
2071         format = 4;
2072         fix->line_length = xvir;
2073         par->y_offset = (yact_st/2)*2*xvir + (xact_st)*2;
2074         par->c_offset = (yact_st/2)*xvir + xact_st;
2075         cblen = crlen = (xvir*yvir)/4;
2076         break;
2077     case 4: // none
2078     case 5: // yuv444
2079         format = 5;
2080         fix->line_length = xvir;
2081         par->y_offset = yact_st*xvir + xact_st;
2082         par->c_offset = yact_st*2*xvir + xact_st*2;
2083         cblen = crlen = (xvir*yvir);
2084         break;
2085     default:
2086         return -EINVAL;
2087     }
2088
2089     smem_len = fix->line_length * yvir + cblen + crlen;
2090     map_size = PAGE_ALIGN(smem_len);
2091
2092    // fix->smem_start = y_addr;
2093     fix->smem_len = smem_len;
2094   //  fix->mmio_start = uv_addr;
2095
2096  //   par->addr_seted = ((-1==(int)y_addr) || (0==(int)y_addr) || (win0_en==0)) ? 0 : 1;
2097     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);
2098
2099     par->format = format;
2100     par->xpos = xpos;
2101     par->ypos = ypos;
2102     par->xsize = xsize;
2103     par->ysize = ysize;
2104     win0_set_par(info);
2105
2106     if ( wq_condition2 == 0 ) {
2107         wait_event_interruptible_timeout(wq,  wq_condition2, HZ/20);
2108     }
2109     wq_condition2 = 0;
2110
2111 #ifdef CONFIG_FB_ROTATE_VIDEO
2112 //need refresh  ,zyc add                
2113         if((has_set_rotate == true) && (last_yuv_phy[0] != 0) && (last_yuv_phy[1] != 0))
2114         {
2115                 u32 yuv_phy[2];
2116                 struct rk29_ipp_req ipp_req;
2117                 static u32 dstoffset = 0;
2118                 static int fb_index = 0;
2119                 memset(&ipp_req, 0, sizeof(struct rk29_ipp_req));
2120                 yuv_phy[0] = last_yuv_phy[0];
2121                 yuv_phy[1] = last_yuv_phy[1];
2122                 yuv_phy[0] += par->y_offset;
2123                 yuv_phy[1] += par->c_offset;
2124         #if 0
2125                 if((var->rotate == 90) ||(var->rotate == 270))
2126         #else
2127         if(var->rotate%360 != 0)
2128         #endif
2129                         {
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                                 ipp_req.dst0.YrgbMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*2*dstoffset;
2142                                 ipp_req.dst0.CbrMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*(2*dstoffset+1);
2143                                    if(var->xres > screen->x_res)
2144                                    {
2145                                         ipp_req.dst0.w = screen->x_res;
2146                                         ipp_req.dst0.h = screen->y_res;
2147                                   }   else      {
2148                                           ipp_req.dst0.w = var->xres;
2149                                           ipp_req.dst0.h = var->yres;
2150                                    }
2151                                  ipp_req.dst_vir_w = (ipp_req.dst0.w + 15) & (~15);
2152
2153                                 ipp_req.timeout = 100;
2154                                 if(var->rotate == 90)
2155                                         ipp_req.flag = IPP_ROT_90;
2156                 else if (var->rotate == 180)
2157                                         ipp_req.flag = IPP_ROT_180;
2158                                 else if(var->rotate == 270)
2159                                         ipp_req.flag = IPP_ROT_270;
2160                                 //ipp_do_blit(&ipp_req);
2161                                 ipp_blit_sync(&ipp_req);
2162                                 fbprintk("yaddr=0x%x,uvaddr=0x%x\n",ipp_req.dst0.YrgbMst,ipp_req.dst0.CbrMst);
2163                                 yuv_phy[0] = ipp_req.dst0.YrgbMst;
2164                                 yuv_phy[1] = ipp_req.dst0.CbrMst;        
2165                                 fix->smem_start = yuv_phy[0];
2166                                 fix->mmio_start = yuv_phy[1];
2167                         }
2168                 else
2169                         {
2170                         
2171                                 fix->smem_start = yuv_phy[0];
2172                                 fix->mmio_start = yuv_phy[1];
2173                         }
2174                         
2175                 LcdWrReg(inf, WIN0_YRGB_MST, yuv_phy[0]);
2176                 LcdWrReg(inf, WIN0_CBR_MST, yuv_phy[1]);
2177                 // enable win0 after the win0 par is seted
2178                 LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(par->par_seted && par->addr_seted));
2179                 par->addr_seted = 1;
2180                 LcdWrReg(inf, REG_CFG_DONE, 0x01);
2181                 if(par->addr_seted ) {
2182                 unsigned long flags;
2183
2184                 local_irq_save(flags);
2185                 par->mirror.y_offset = yuv_phy[0];
2186                 par->mirror.c_offset = yuv_phy[1];
2187                 local_irq_restore(flags);
2188
2189                 mcu_refresh(inf);
2190                 //printk("0x%.8x 0x%.8x mirror\n", par->mirror.y_offset, par->mirror.c_offset);
2191                 }
2192
2193         }
2194
2195     has_set_rotate = false;
2196 #endif
2197     return 0;
2198 }
2199
2200 static int fb1_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
2201 {
2202     struct win0_par *par = info->par;
2203      // enable win0 after the win0 addr is seted
2204
2205     win0_pan(info);
2206     par->par_seted = 1;
2207     return 0;
2208 }
2209
2210 int fb1_open(struct fb_info *info, int user)
2211 {
2212     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
2213     struct win0_par *par = info->par;
2214
2215     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2216
2217     par->par_seted = 0;
2218     par->addr_seted = 0;
2219     inf->video_mode = 1;
2220     wq_condition2 = 1;
2221 #ifdef CONFIG_FB_ROTATE_VIDEO
2222    //reinitialize  the var when open,zyc
2223     last_yuv_phy[0] = 0;
2224     last_yuv_phy[1] = 0;
2225     has_set_rotate = 0;
2226 #endif
2227     if(par->refcount) {
2228         printk(">>>>>> fb1 has opened! \n");
2229         return -EACCES;
2230     } else {
2231         par->refcount++;
2232         win0_blank(FB_BLANK_NORMAL, info);
2233         fb0_set_par(inf->fb0);
2234         
2235             rk29fb_notify(inf, RK29FB_EVENT_FB1_ON);
2236         return 0;
2237     }
2238 }
2239
2240 int fb1_release(struct fb_info *info, int user)
2241 {
2242     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
2243     struct win0_par *par = info->par;
2244         struct fb_var_screeninfo *var0 = &info->var;
2245
2246     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2247     if(inf->setFlag == 0)
2248     {
2249         wait_event_interruptible_timeout(wq, inf->setFlag, HZ*10);
2250     } 
2251     if(par->refcount) {
2252         par->refcount--;
2253         inf->video_mode = 0;
2254         par->par_seted = 0;
2255         par->addr_seted = 0;
2256         win1_blank(FB_BLANK_NORMAL, info);
2257
2258         //if(inf->cur_screen->type != SCREEN_HDMI)
2259             fb0_set_par(inf->fb0);
2260
2261         // unmap memory
2262         info->screen_base = 0;
2263         info->fix.smem_start = 0;
2264         info->fix.smem_len = 0;
2265                 // clean the var param
2266                 memset(var0, 0, sizeof(struct fb_var_screeninfo));
2267             rk29fb_notify(inf, RK29FB_EVENT_FB1_OFF);
2268         #ifdef CONFIG_CLOSE_WIN1_DYNAMIC   
2269          cancel_delayed_work_sync(&rk29_win1_check_work);
2270         #endif  
2271     }
2272
2273     return 0;
2274 }
2275
2276 static int fb1_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
2277 {
2278     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
2279     struct win0_par *par = info->par;
2280     struct fb_fix_screeninfo *fix0 = &info->fix;
2281     struct fb_var_screeninfo *var = &info->var;
2282     void __user *argp = (void __user *)arg;
2283     
2284 #ifdef CONFIG_FB_ROTATE_VIDEO   
2285      struct rk29fb_screen *screen = inf->cur_screen;
2286      struct rk29_ipp_req ipp_req;
2287      static u32 dstoffset = 0;
2288     memset(&ipp_req, 0, sizeof(struct rk29_ipp_req)); 
2289 #endif
2290
2291         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2292     fbprintk("win0fb_ioctl cmd = %8x, arg = %8x \n", (u32)cmd, (u32)arg);
2293
2294         CHK_SUSPEND(inf);
2295
2296     switch(cmd)
2297     {
2298     case FB1_IOCTL_GET_PANEL_SIZE:    //get panel size
2299         {
2300             u32 panel_size[2];
2301              if((var->rotate == 270)||(var->rotate == 90)) {
2302                 panel_size[0] = inf->panel1_info.y_res; //inf->cur_screen->y_res; change for hdmi video size
2303                 panel_size[1] = inf->panel1_info.x_res;
2304             } else {
2305                 panel_size[0] = inf->panel1_info.x_res;
2306                 panel_size[1] = inf->panel1_info.y_res;
2307             }
2308
2309             if(copy_to_user(argp, panel_size, 8))  return -EFAULT;
2310         }
2311         break;
2312
2313     case FB1_IOCTL_SET_YUV_ADDR:    //set y&uv address to register direct
2314         {
2315             u32 yuv_phy[2];
2316             if (copy_from_user(yuv_phy, argp, 8))
2317                             return -EFAULT;
2318 #ifdef CONFIG_FB_ROTATE_VIDEO 
2319                 //add by zyc
2320                 if(has_set_rotate == true)
2321                 break;
2322                 last_yuv_phy[0] = yuv_phy[0];
2323                 last_yuv_phy[1] = yuv_phy[1];
2324 #endif
2325
2326             fix0->smem_start = yuv_phy[0];
2327             fix0->mmio_start = yuv_phy[1];
2328             yuv_phy[0] += par->y_offset;
2329             yuv_phy[1] += par->c_offset;
2330          
2331             #if 0
2332                 if((var->rotate == 90) ||(var->rotate == 270))
2333             #else
2334             if(var->rotate%360 != 0)
2335             #endif
2336             {
2337                 #ifdef CONFIG_FB_ROTATE_VIDEO 
2338                 dstoffset = (dstoffset+1)%2;
2339                 ipp_req.src0.fmt = 3;
2340                 ipp_req.src0.YrgbMst = yuv_phy[0];
2341                 ipp_req.src0.CbrMst = yuv_phy[1];
2342                     ipp_req.src0.w = var->xres ;
2343                 ipp_req.src0.h = var->yres ;
2344                         ipp_req.src_vir_w= (var->xres + 15) & (~15);
2345                         ipp_req.dst_vir_w=screen->x_res;
2346
2347                 ipp_req.dst0.fmt = 3;
2348                 ipp_req.dst0.YrgbMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*2*dstoffset;
2349                 ipp_req.dst0.CbrMst = inf->fb0->fix.mmio_start + screen->x_res*screen->y_res*(2*dstoffset+1);
2350                 if(var->xres > screen->x_res)
2351                 {
2352                     ipp_req.dst0.w = screen->x_res;
2353                     ipp_req.dst0.h = screen->y_res;
2354                 }   else  {
2355                                         ipp_req.dst0.w = var->xres;
2356                                         ipp_req.dst0.h = var->yres;
2357
2358                 }
2359                 ipp_req.dst_vir_w = (ipp_req.dst0.w + 15) & (~15);
2360                 ipp_req.timeout = 100;
2361                 if(var->rotate == 90)
2362                     ipp_req.flag = IPP_ROT_90;
2363                 else if(var->rotate == 180)
2364                     ipp_req.flag = IPP_ROT_180;
2365                 else if(var->rotate == 270)
2366                     ipp_req.flag = IPP_ROT_270;
2367                 //ipp_do_blit(&ipp_req);
2368                 ipp_blit_sync(&ipp_req);
2369                                 
2370                 fbprintk("yaddr=0x%x,uvaddr=0x%x\n",ipp_req.dst0.YrgbMst,ipp_req.dst0.CbrMst);
2371                 yuv_phy[0] = ipp_req.dst0.YrgbMst;
2372                 yuv_phy[1] = ipp_req.dst0.CbrMst;    
2373                 fix0->smem_start = yuv_phy[0];
2374                 fix0->mmio_start = yuv_phy[1];
2375                 #else //CONFIG_FB_ROTATE_VIDEO
2376                 printk("LCDC not support rotate!\n");
2377                 #endif
2378             }
2379       
2380             LcdWrReg(inf, WIN0_YRGB_MST, yuv_phy[0]);
2381             LcdWrReg(inf, WIN0_CBR_MST, yuv_phy[1]);
2382             // enable win0 after the win0 par is seted
2383             LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(par->par_seted && par->addr_seted));
2384             par->addr_seted = 1;
2385             LcdWrReg(inf, REG_CFG_DONE, 0x01);
2386             if(par->par_seted) {
2387                 unsigned long flags;
2388
2389                 local_irq_save(flags);
2390                 par->mirror.y_offset = yuv_phy[0];
2391                 par->mirror.c_offset = yuv_phy[1];
2392                 local_irq_restore(flags);
2393
2394                 mcu_refresh(inf);
2395                 //printk("0x%.8x 0x%.8x mirror\n", par->mirror.y_offset, par->mirror.c_offset);
2396             }
2397         }
2398         break;
2399
2400     case FB1_IOCTL_SET_ROTATE:    //change MCU panel scan direction
2401         fbprintk(">>>>>> change lcdc direction(%d) \n", (int)arg);
2402       #ifdef CONFIG_FB_ROTATE_VIDEO 
2403                 //zyc add
2404         has_set_rotate = true;
2405         if(arg == 0 || arg==180)
2406             var->rotate = arg;    
2407         else if (arg == 90 || arg==270)
2408             var->rotate = arg;  
2409       #else //CONFIG_FB_ROTATE_VIDEO
2410         printk("LCDC not support rotate!\n");
2411       #endif
2412         break;
2413     case FB1_IOCTL_SET_WIN0_TOP:
2414         fbprintk(">>>>>> FB1_IOCTL_SET_WIN0_TOP %d\n",arg);
2415         LcdMskReg(inf, DSP_CTRL0, m_W0_ON_TOP, v_W0_ON_TOP(arg));
2416         LcdWrReg(inf, REG_CFG_DONE, 0x01);
2417         break;
2418     default:
2419         break;
2420     }
2421     return 0;
2422 }
2423
2424 static struct fb_ops fb1_ops = {
2425         .owner          = THIS_MODULE,
2426         .fb_open    = fb1_open,
2427         .fb_release = fb1_release,
2428         .fb_check_var   = fb1_check_var,
2429         .fb_set_par     = fb1_set_par,
2430         .fb_blank       = fb1_blank,
2431     .fb_pan_display = fb1_pan_display,
2432     .fb_ioctl = fb1_ioctl,
2433         .fb_setcolreg   = fb_setcolreg,
2434         .fb_fillrect    = cfb_fillrect,
2435         .fb_copyarea    = cfb_copyarea,
2436         .fb_imageblit   = cfb_imageblit,
2437 };
2438
2439 static struct fb_ops fb0_ops = {
2440         .owner          = THIS_MODULE,
2441         .fb_check_var   = fb0_check_var,
2442         .fb_set_par = fb0_set_par,
2443         .fb_blank   = fb0_blank,
2444         .fb_pan_display = fb0_pan_display,
2445     .fb_ioctl = fb0_ioctl,
2446         .fb_setcolreg   = fb_setcolreg,
2447         .fb_fillrect    = cfb_fillrect,
2448         .fb_copyarea    = cfb_copyarea,
2449         .fb_imageblit   = cfb_imageblit,
2450         //.fb_cursor      = rk29_set_cursor,
2451 };
2452
2453 int fb_get_video_mode(void)
2454 {
2455         struct rk29fb_inf *inf;
2456         if(!g_pdev)
2457                 return 0;
2458         inf = platform_get_drvdata(g_pdev);
2459         return inf->video_mode;
2460 }
2461 /*
2462 enable: 1, switch to tv or hdmi; 0, switch to lcd
2463 */
2464 int FB_Switch_Screen( struct rk29fb_screen *screen, u32 enable )
2465 {
2466     struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
2467    // struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2468     struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2469
2470     memcpy(&inf->panel2_info, screen, sizeof( struct rk29fb_screen ));
2471
2472     if(enable)inf->cur_screen = &inf->panel2_info;
2473     else inf->cur_screen = &inf->panel1_info;
2474
2475     /* Black out, because some display device need clock to standby */
2476     //LcdMskReg(inf, DSP_CTRL_REG1, m_BLACK_OUT, v_BLACK_OUT(1));
2477    // LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(0));
2478    // LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
2479     LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(1));
2480     LcdWrReg(inf, REG_CFG_DONE, 0x01);
2481     wake_lock(&idlelock);
2482     msleep(20);
2483     wake_unlock(&idlelock);
2484
2485     if(inf->cur_screen->standby)    inf->cur_screen->standby(1);
2486     // operate the display_on pin to power down the lcd
2487    
2488     if(enable && mach_info->io_disable)mach_info->io_disable();  //close lcd out
2489     else if (mach_info->io_enable)mach_info->io_enable();       //open lcd out
2490     
2491     load_screen(inf->fb0, 0);
2492         mcu_refresh(inf);
2493
2494     fb1_set_par(inf->fb1);
2495     fb0_set_par(inf->fb0);
2496     LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(0));
2497     LcdWrReg(inf, REG_CFG_DONE, 0x01);
2498     rk29fb_notify(inf, enable ? RK29FB_EVENT_HDMI_ON : RK29FB_EVENT_HDMI_OFF);
2499     return 0;
2500 }
2501
2502 static ssize_t dsp_win0_info_read(struct device *device,
2503                             struct device_attribute *attr, char *buf)
2504 {
2505     //char * s = _buf;
2506     struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
2507     struct rk29fb_screen *screen = inf->cur_screen;
2508
2509     u16 xpos=0,ypos=0,xsize=0,ysize=0;
2510
2511     fbprintk("%s\n",__FUNCTION__);
2512     xpos = LcdRdReg(inf, WIN0_DSP_ST) & 0xffff;
2513     ypos = (LcdRdReg(inf, WIN0_DSP_ST)>>16) & 0xffff;
2514
2515     xpos -= (screen->left_margin + screen->hsync_len);
2516     ypos -= (screen->upper_margin + screen->vsync_len);
2517
2518     xsize=LcdRdReg(inf, WIN0_DSP_INFO)& 0xffff;
2519     ysize=(LcdRdReg(inf, WIN0_DSP_INFO)>>16) & 0xffff;
2520     fbprintk("%s %d , %d, %d ,%d\n",__FUNCTION__,xpos,ypos,xsize,ysize);
2521         rk29_set_cursor_test(inf->fb0);
2522
2523     return snprintf(buf, PAGE_SIZE, "%d,%d,%d,%d\n", xpos,ypos,xsize,ysize);
2524 }
2525
2526 static ssize_t dsp_win0_info_write(struct device *device,
2527                            struct device_attribute *attr, const char *buf, size_t count)
2528 {
2529      printk("%s\n",__FUNCTION__);
2530      printk("%s %x \n",__FUNCTION__,*buf);
2531      return count;
2532 }
2533
2534 static DEVICE_ATTR(dsp_win0_info,  S_IRUGO|S_IWUSR, dsp_win0_info_read, dsp_win0_info_write);
2535
2536 static irqreturn_t rk29fb_irq(int irq, void *dev_id)
2537 {
2538         struct platform_device *pdev = (struct platform_device*)dev_id;
2539     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
2540     struct win0_par *par = (struct win0_par *)inf->fb1->par;
2541     if(!inf)
2542         return IRQ_HANDLED;
2543
2544         //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
2545
2546     LcdMskReg(inf, INT_STATUS, m_FRM_STARTCLEAR, v_FRM_STARTCLEAR(1));
2547
2548         if(SCREEN_MCU == inf->cur_screen->type)
2549         {
2550         inf->mcu_isrcnt = !inf->mcu_isrcnt;
2551         if(inf->mcu_isrcnt)
2552             return IRQ_HANDLED;
2553
2554         if(IsMcuUseFmk())
2555         {
2556             if(LcdReadBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST) && (inf->mcu_fmksync == 0))
2557             {
2558                 inf->mcu_fmksync = 1;
2559                  if(inf->cur_screen->refresh)
2560                    inf->cur_screen->refresh(REFRESH_END);
2561                 inf->mcu_fmksync = 0;
2562             }
2563             else
2564             {
2565                 return IRQ_HANDLED;
2566             }
2567         }
2568         else
2569         {
2570             if(inf->mcu_needflush) {
2571                 if(inf->cur_screen->refresh)
2572                     inf->cur_screen->refresh(REFRESH_PRE);
2573                 inf->mcu_needflush = 0;
2574                 inf->mcu_isrcnt = 0;
2575                 LcdSetRegisterBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST);
2576             } else {
2577                 if(inf->cur_screen->refresh)
2578                     inf->cur_screen->refresh(REFRESH_END);
2579             }
2580         }
2581         }
2582 #if ANDROID_USE_THREE_BUFS
2583     new_frame_seted = 1;
2584 #endif
2585
2586     if(waitqueue_active(&par->wait)) {
2587         if (par->mirror.c_offset == 0)
2588             printk("error: no new buffer to display\n");
2589
2590         par->done.y_offset = par->displ.y_offset;
2591         par->done.c_offset = par->displ.c_offset;
2592         par->displ.y_offset = par->mirror.y_offset;
2593         par->displ.c_offset = par->mirror.c_offset;
2594         par->mirror.y_offset = 0;
2595         par->mirror.c_offset = 0;
2596
2597         //printk("0x%.8x 0x%.8x displaying\n", par->displ.y_offset, par->displ.c_offset);
2598         //printk("0x%.8x 0x%.8x done\n", par->done.y_offset, par->done.c_offset);
2599         wake_up_interruptible(&par->wait);
2600     }
2601
2602     wq_condition2 = 1;
2603         wq_condition = 1;
2604         wake_up_interruptible(&wq);
2605
2606         rk29fb_irq_notify_ddr();
2607         return IRQ_HANDLED;
2608 }
2609
2610 #ifdef CONFIG_HAS_EARLYSUSPEND
2611
2612 struct suspend_info {
2613         struct early_suspend early_suspend;
2614         struct rk29fb_inf *inf;
2615 };
2616
2617 static void rk29fb_early_suspend(struct early_suspend *h)
2618 {
2619         struct suspend_info *info = container_of(h, struct suspend_info,
2620                                                 early_suspend);
2621
2622     struct rk29fb_inf *inf = info->inf;
2623     struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2624
2625     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2626
2627     if(!inf) {
2628         printk("inf==0, rk29fb_suspend fail! \n");
2629         return;
2630     }
2631
2632 #ifdef CONFIG_CLOSE_WIN1_DYNAMIC   
2633      cancel_delayed_work_sync(&rk29_win1_check_work);
2634 #endif  
2635
2636     if((inf->cur_screen != &inf->panel2_info) && mach_info->io_disable)  // close lcd pwr when output screen is lcd
2637        mach_info->io_disable();  //close lcd out 
2638
2639         if(inf->cur_screen->standby)
2640         {
2641                 fbprintk(">>>>>> power down the screen! \n");
2642                 inf->cur_screen->standby(1);
2643         }
2644
2645     LcdMskReg(inf, DSP_CTRL0, m_HSYNC_POLARITY | m_VSYNC_POLARITY | m_DEN_POLARITY ,
2646        v_HSYNC_POLARITY(1) | v_VSYNC_POLARITY(1) | v_DEN_POLARITY(1) );
2647
2648     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(1));
2649     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(1));
2650         LcdWrReg(inf, REG_CFG_DONE, 0x01);
2651
2652         if(!inf->in_suspend)
2653         {
2654                 fbprintk(">>>>>> diable the lcdc clk! \n");
2655                 wake_lock(&idlelock);
2656                 msleep(100);
2657                 wake_unlock(&idlelock);
2658         clk_disable(inf->aclk_ddr_lcdc);
2659         clk_disable(inf->aclk_disp_matrix);
2660         clk_disable(inf->hclk_cpu_display);
2661         clk_disable(inf->clk);
2662         if (inf->dclk){
2663             clk_disable(inf->dclk);
2664         }
2665         if(inf->clk){
2666             clk_disable(inf->aclk);
2667         }
2668         //clk_disable(inf->pd_display);
2669
2670                 inf->in_suspend = 1;
2671         }
2672 }
2673
2674 static void rk29fb_early_resume(struct early_suspend *h)
2675 {
2676         struct suspend_info *info = container_of(h, struct suspend_info,
2677                                         early_suspend);
2678
2679     struct rk29fb_inf *inf = info->inf;
2680     struct rk29fb_screen *screen = inf->cur_screen;
2681     struct rk29fb_info *mach_info = g_pdev->dev.platform_data;
2682
2683     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2684     if(!inf) {
2685         printk("inf==0, rk29fb_resume fail! \n");
2686         return ;
2687     }
2688
2689     if(inf->in_suspend)
2690         {
2691             inf->in_suspend = 0;
2692         fbprintk(">>>>>> enable the lcdc clk! \n");
2693        // clk_enable(inf->pd_display);
2694         clk_enable(inf->aclk_disp_matrix);
2695         clk_enable(inf->hclk_cpu_display);
2696         clk_enable(inf->clk);
2697         clk_enable(inf->aclk_ddr_lcdc);
2698
2699         if (inf->dclk){
2700             clk_enable(inf->dclk);
2701         }
2702         if(inf->clk){
2703             clk_enable(inf->aclk);
2704         }
2705         usleep_range(100*1000, 100*1000);
2706         }
2707     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(0));
2708     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(0));
2709     LcdWrReg(inf, REG_CFG_DONE, 0x01);
2710
2711     LcdMskReg(inf, DSP_CTRL0, m_HSYNC_POLARITY | m_VSYNC_POLARITY | m_DEN_POLARITY ,
2712        v_HSYNC_POLARITY(screen->pin_hsync) | v_VSYNC_POLARITY(screen->pin_vsync) | v_DEN_POLARITY(screen->pin_den) );
2713
2714         if(inf->cur_screen->standby)
2715         {
2716                 fbprintk(">>>>>> power on the screen! \n");
2717                 inf->cur_screen->standby(0);
2718         }
2719     usleep_range(10*1000, 10*1000);
2720     memcpy((u8*)inf->preg, (u8*)&inf->regbak, 0xa4);  //resume reg
2721     usleep_range(40*1000, 40*1000);
2722     
2723     if((inf->cur_screen != &inf->panel2_info) && mach_info->io_enable)  // open lcd pwr when output screen is lcd
2724        mach_info->io_enable();  //close lcd out 
2725         
2726 }
2727
2728 static struct suspend_info suspend_info = {
2729         .early_suspend.suspend = rk29fb_early_suspend,
2730         .early_suspend.resume = rk29fb_early_resume,
2731         .early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB,
2732 };
2733 #endif
2734 struct fb_info *g_fb0_inf = NULL;  //add cym@rk 20101027 for charger logo
2735 static int __init rk29fb_probe (struct platform_device *pdev)
2736 {
2737     struct rk29fb_inf *inf = NULL;
2738     struct resource *res = NULL;
2739     struct resource *mem = NULL;
2740     struct rk29fb_info *mach_info = NULL;
2741     struct rk29fb_screen *screen = NULL;
2742     struct win0_par* par = NULL;
2743         int irq = 0;
2744     int ret = 0;
2745
2746     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2747
2748     /* Malloc rk29fb_inf and set it to pdev for drvdata */
2749     fbprintk(">> Malloc rk29fb_inf and set it to pdev for drvdata \n");
2750     inf = kmalloc(sizeof(struct rk29fb_inf), GFP_KERNEL);
2751     if(!inf)
2752     {
2753         dev_err(&pdev->dev, ">> inf kmalloc fail!");
2754         ret = -ENOMEM;
2755                 goto release_drvdata;
2756     }
2757     memset(inf, 0, sizeof(struct rk29fb_inf));
2758         platform_set_drvdata(pdev, inf);
2759
2760     mach_info = pdev->dev.platform_data;
2761     /* Fill screen info and set current screen */
2762     fbprintk(">> Fill screen info and set current screen \n");
2763    #ifdef CONFIG_DEFAULT_OUT_HDMI  // set hdmi for default output 
2764     hdmi_get_default_resolution(&inf->panel1_info);
2765    #else
2766     set_lcd_info(&inf->panel1_info, mach_info->lcd_info);
2767    #endif
2768     inf->cur_screen = &inf->panel1_info;
2769     screen = inf->cur_screen;
2770     if(SCREEN_NULL==screen->type)
2771     {
2772         dev_err(&pdev->dev, ">> Please select a display device! \n");
2773         ret = -EINVAL;
2774                 goto release_drvdata;
2775     }
2776
2777     /* get virtual basic address of lcdc register */
2778     fbprintk(">> get virtual basic address of lcdc register \n");
2779     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcdc reg");
2780     if (res == NULL)
2781     {
2782         dev_err(&pdev->dev, "failed to get memory registers\n");
2783         ret = -ENOENT;
2784                 goto release_drvdata;
2785     }
2786     inf->reg_phy_base = res->start;
2787     inf->len = (res->end - res->start) + 1;
2788     mem = request_mem_region(inf->reg_phy_base, inf->len, pdev->name);
2789     if (mem == NULL)
2790     {
2791         dev_err(&pdev->dev, "failed to get memory region\n");
2792         ret = -ENOENT;
2793                 goto release_drvdata;
2794     }
2795     fbprintk("inf->reg_phy_base = 0x%08x, inf->len = %d \n", inf->reg_phy_base, inf->len);
2796     inf->reg_vir_base = ioremap(inf->reg_phy_base, inf->len);
2797     if (inf->reg_vir_base == NULL)
2798     {
2799         dev_err(&pdev->dev, "ioremap() of registers failed\n");
2800         ret = -ENXIO;
2801                 goto release_drvdata;
2802     }
2803     inf->preg = (LCDC_REG*)inf->reg_vir_base;
2804
2805     /* Prepare win1 info */
2806     fbprintk(">> Prepare win1 info \n");
2807         inf->fb0 = framebuffer_alloc(sizeof(struct win0_par), &pdev->dev);
2808     if(!inf->fb0)
2809     {
2810         dev_err(&pdev->dev, ">> fb0 framebuffer_alloc fail!");
2811                 inf->fb0 = NULL;
2812         ret = -ENOMEM;
2813                 goto release_win1fb;
2814     }
2815
2816     par = (struct win0_par*)inf->fb0->par;
2817     strcpy(inf->fb0->fix.id, "fb0");
2818     inf->fb0->fix.type        = FB_TYPE_PACKED_PIXELS;
2819     inf->fb0->fix.type_aux    = 0;
2820     inf->fb0->fix.xpanstep    = 1;
2821     inf->fb0->fix.ypanstep    = 1;
2822     inf->fb0->fix.ywrapstep   = 0;
2823     inf->fb0->fix.accel       = FB_ACCEL_NONE;
2824     inf->fb0->fix.visual      = FB_VISUAL_TRUECOLOR;
2825     inf->fb0->fix.smem_len    = 0;
2826     inf->fb0->fix.line_length = 0;
2827     inf->fb0->fix.smem_start  = 0;
2828
2829     inf->fb0->var.xres = screen->x_res;
2830     inf->fb0->var.yres = screen->y_res;
2831     inf->fb0->var.bits_per_pixel = 16;
2832     inf->fb0_color_deepth = 0;
2833     inf->fb0->var.xres_virtual = screen->x_res;
2834     inf->fb0->var.yres_virtual = screen->y_res;
2835     inf->fb0->var.width = screen->width;
2836     inf->fb0->var.height = screen->height;
2837     //inf->fb0->var.pixclock = div_u64(1000000000000llu, screen->pixclock);
2838     inf->fb0->var.left_margin = screen->left_margin;
2839     inf->fb0->var.right_margin = screen->right_margin;
2840     inf->fb0->var.upper_margin = screen->upper_margin;
2841     inf->fb0->var.lower_margin = screen->lower_margin;
2842     inf->fb0->var.vsync_len = screen->vsync_len;
2843     inf->fb0->var.hsync_len = screen->hsync_len;
2844     inf->fb0->var.red    = def_rgb_16.red;
2845     inf->fb0->var.green  = def_rgb_16.green;
2846     inf->fb0->var.blue   = def_rgb_16.blue;
2847     inf->fb0->var.transp = def_rgb_16.transp;
2848
2849     inf->fb0->var.nonstd      = 0;  //win1 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
2850     inf->fb0->var.grayscale   = 0;  //win1 transprent mode & value(mode<<8 + value)
2851     inf->fb0->var.activate    = FB_ACTIVATE_NOW;
2852     inf->fb0->var.accel_flags = 0;
2853     inf->fb0->var.vmode       = FB_VMODE_NONINTERLACED;
2854
2855     inf->fb0->fbops           = &fb0_ops;
2856     inf->fb0->flags           = FBINFO_FLAG_DEFAULT;
2857     inf->fb0->pseudo_palette  = par->pseudo_pal;
2858     inf->fb0->screen_base     = 0;
2859
2860     memset(par, 0, sizeof(struct win0_par));
2861
2862         ret = fb_alloc_cmap(&inf->fb0->cmap, 256, 0);
2863         if (ret < 0)
2864                 goto release_cmap;
2865
2866         g_fb0_inf = inf->fb0; //add cym@rk 20101027
2867
2868     /* alloc win1 buf */
2869     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "win1 buf");
2870     if (res == NULL)
2871     {
2872         dev_err(&pdev->dev, "failed to get win1 memory \n");
2873         ret = -ENOENT;
2874         goto release_win1fb;
2875     }
2876     inf->fb0->fix.smem_start = res->start;
2877     inf->fb0->fix.smem_len = res->end - res->start + 1;
2878     inf->fb0->screen_base = ioremap(res->start, inf->fb0->fix.smem_len);
2879     memset(inf->fb0->screen_base, 0, inf->fb0->fix.smem_len);
2880
2881     #ifdef CONFIG_FB_WORK_IPP
2882        /* alloc win1 ipp buf */
2883     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "win1 ipp buf");
2884     if (res == NULL)
2885     {
2886         dev_err(&pdev->dev, "failed to get win1 ipp memory \n");
2887         ret = -ENOENT;
2888         goto release_win1fb;
2889     }
2890     inf->fb0->fix.mmio_start = res->start;
2891     inf->fb0->fix.mmio_len = res->end - res->start + 1;
2892     #endif
2893
2894     /* Prepare win0 info */
2895     fbprintk(">> Prepare win0 info \n");
2896     inf->fb1 = framebuffer_alloc(sizeof(struct win0_par), &pdev->dev);
2897     if(!inf->fb1)
2898     {
2899         dev_err(&pdev->dev, ">> fb1 framebuffer_alloc fail!");
2900                 inf->fb1 = NULL;
2901                 ret = -ENOMEM;
2902                 goto release_win0fb;
2903     }
2904
2905     par = (struct win0_par*)inf->fb1->par;
2906
2907     strcpy(inf->fb1->fix.id, "fb1");
2908         inf->fb1->fix.type            = FB_TYPE_PACKED_PIXELS;
2909         inf->fb1->fix.type_aux    = 0;
2910         inf->fb1->fix.xpanstep    = 1;
2911         inf->fb1->fix.ypanstep    = 1;
2912         inf->fb1->fix.ywrapstep   = 0;
2913         inf->fb1->fix.accel       = FB_ACCEL_NONE;
2914     inf->fb1->fix.visual      = FB_VISUAL_TRUECOLOR;
2915     inf->fb1->fix.smem_len    = 0;
2916     inf->fb1->fix.line_length = 0;
2917     inf->fb1->fix.smem_start  = 0;
2918
2919     inf->fb1->var.xres = screen->x_res;
2920     inf->fb1->var.yres = screen->y_res;
2921     inf->fb1->var.bits_per_pixel = 16;
2922     inf->fb1->var.xres_virtual = screen->x_res;
2923     inf->fb1->var.yres_virtual = screen->y_res;
2924     inf->fb1->var.width = screen->width;
2925     inf->fb1->var.height = screen->height;
2926     //inf->fb1->var.pixclock = div_u64(1000000000000llu, screen->pixclock);
2927     inf->fb1->var.left_margin = screen->left_margin;
2928     inf->fb1->var.right_margin = screen->right_margin;
2929     inf->fb1->var.upper_margin = screen->upper_margin;
2930     inf->fb1->var.lower_margin = screen->lower_margin;
2931     inf->fb1->var.vsync_len = screen->vsync_len;
2932     inf->fb1->var.hsync_len = screen->hsync_len;
2933     inf->fb1->var.red    = def_rgb_16.red;
2934     inf->fb1->var.green  = def_rgb_16.green;
2935     inf->fb1->var.blue   = def_rgb_16.blue;
2936     inf->fb1->var.transp = def_rgb_16.transp;
2937
2938     inf->fb1->var.nonstd      = 0;  //win0 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
2939     inf->fb1->var.grayscale   = ((inf->fb1->var.yres<<20)&0xfff00000) + ((inf->fb1->var.xres<<8)&0xfff00);//win0 xsize & ysize
2940     inf->fb1->var.activate    = FB_ACTIVATE_NOW;
2941     inf->fb1->var.accel_flags = 0;
2942     inf->fb1->var.vmode       = FB_VMODE_NONINTERLACED;
2943
2944     inf->fb1->fbops           = &fb1_ops;
2945         inf->fb1->flags               = FBINFO_FLAG_DEFAULT;
2946         inf->fb1->pseudo_palette  = par->pseudo_pal;
2947         inf->fb1->screen_base     = 0;
2948
2949     memset(par, 0, sizeof(struct win0_par));
2950
2951         init_waitqueue_head(&par->wait);
2952
2953         /* Init all lcdc and lcd before register_framebuffer. */
2954         /* because after register_framebuffer, the win1fb_check_par and winfb_set_par execute immediately */
2955         fbprintk(">> Init all lcdc and lcd before register_framebuffer \n");
2956     init_lcdc(inf->fb0);
2957
2958   #if 0 //def CONFIG_CPU_FREQ
2959    // inf->freq_transition.notifier_call = rk29fb_freq_transition;
2960    // cpufreq_register_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
2961   #endif
2962         fbprintk("got clock\n");
2963
2964         if(mach_info)
2965     {
2966         struct rk29_fb_setting_info fb_setting;
2967         if( OUT_P888==inf->cur_screen->face )     // set lcdc iomux
2968         {
2969             fb_setting.data_num = 24;
2970         }
2971         else if(OUT_P666 == inf->cur_screen->face )
2972         {
2973             fb_setting.data_num = 18;
2974         }
2975         else
2976         {
2977             fb_setting.data_num = 16;
2978         }
2979         fb_setting.den_en = 1;
2980         fb_setting.vsync_en = 1;
2981         fb_setting.disp_on_en = 1;
2982         fb_setting.standby_en = 1;
2983         if( inf->cur_screen->mcu_usefmk )
2984             fb_setting.mcu_fmk_en =1;
2985         mach_info->io_init(&fb_setting);
2986     }
2987
2988         //set_lcd_pin(pdev, 1);
2989         mdelay(10);
2990         g_pdev = pdev;
2991         inf->mcu_usetimer = 1;
2992     inf->mcu_fmksync = 0;
2993         load_screen(inf->fb0, 1);
2994
2995     /* Register framebuffer(fb0 & fb1) */
2996     fbprintk(">> Register framebuffer(fb0) \n");
2997     ret = register_framebuffer(inf->fb0);
2998     if(ret<0)
2999     {
3000         printk(">> fb0 register_framebuffer fail!\n");
3001         ret = -EINVAL;
3002                 goto release_win0fb;
3003     }
3004     fbprintk(">> Register framebuffer(fb1) \n");
3005
3006     ret = register_framebuffer(inf->fb1);
3007     if(ret<0)
3008     {
3009         printk(">> fb1 register_framebuffer fail!\n");
3010         ret = -EINVAL;
3011                 goto unregister_win1fb;
3012     }
3013
3014     ret = device_create_file(inf->fb1->dev, &dev_attr_dsp_win0_info);
3015     if(ret)
3016     {
3017         printk(">> fb1 dsp win0 info device_create_file err\n");
3018         ret = -EINVAL;
3019     }
3020 #ifdef CONFIG_HAS_EARLYSUSPEND
3021         suspend_info.inf = inf;
3022         register_early_suspend(&suspend_info.early_suspend);
3023 #endif
3024
3025     /* get and request irq */
3026     fbprintk(">> get and request irq \n");
3027     irq = platform_get_irq(pdev, 0);
3028     if (irq < 0) {
3029         dev_err(&pdev->dev, "no irq for device\n");
3030         ret = -ENOENT;
3031         goto unregister_win1fb;
3032     }
3033     ret = request_irq(irq, rk29fb_irq, IRQF_DISABLED, pdev->name, pdev);
3034     if (ret) {
3035         dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
3036         ret = -EBUSY;
3037         goto release_irq;
3038     }
3039
3040     if( inf->cur_screen->mcu_usefmk && (mach_info->mcu_fmk_pin != -1) )
3041     {
3042         ret = request_irq(gpio_to_irq(mach_info->mcu_fmk_pin), mcu_irqfmk, GPIOEdgelFalling, pdev->name, pdev);
3043         if (ret)
3044         {
3045             dev_err(&pdev->dev, "cannot get fmk irq %d - err %d\n", irq, ret);
3046             ret = -EBUSY;
3047             goto release_irq;
3048         }
3049     }
3050
3051 #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
3052     fb0_set_par(inf->fb0);
3053     if (fb_prepare_logo(inf->fb0, FB_ROTATE_UR)) {
3054         /* Start display and show logo on boot */
3055         fb_set_cmap(&inf->fb0->cmap, inf->fb0);
3056         fb_show_logo(inf->fb0, FB_ROTATE_UR);
3057         fb0_blank(FB_BLANK_UNBLANK, inf->fb0);
3058     }
3059 #endif
3060
3061     printk(" %s ok\n", __FUNCTION__);
3062     return ret;
3063
3064 release_irq:
3065         if(irq>=0)
3066         free_irq(irq, pdev);
3067 unregister_win1fb:
3068     unregister_framebuffer(inf->fb0);
3069 release_win0fb:
3070         if(inf->fb1)
3071                 framebuffer_release(inf->fb1);
3072         inf->fb1 = NULL;
3073 release_cmap:
3074     if(&inf->fb0->cmap)
3075         fb_dealloc_cmap(&inf->fb0->cmap);
3076 release_win1fb:
3077         if(inf->fb0)
3078                 framebuffer_release(inf->fb0);
3079         inf->fb0 = NULL;
3080 release_drvdata:
3081         if(inf && inf->reg_vir_base)
3082         iounmap(inf->reg_vir_base);
3083         if(inf && mem)
3084         release_mem_region(inf->reg_phy_base, inf->len);
3085         if(inf)
3086         kfree(inf);
3087         platform_set_drvdata(pdev, NULL);
3088         return ret;
3089 }
3090
3091 static int rk29fb_remove(struct platform_device *pdev)
3092 {
3093     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
3094     struct fb_info *info = NULL;
3095         //pm_message_t msg;
3096     struct rk29fb_info *mach_info = NULL;
3097     int irq = 0;
3098
3099         fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
3100
3101     if(!inf) {
3102         printk("inf==0, rk29_fb_remove fail! \n");
3103         return -EINVAL;
3104     }
3105     device_remove_file(inf->fb1->dev, &dev_attr_dsp_win0_info);
3106
3107     irq = platform_get_irq(pdev, 0);
3108     if (irq >0)
3109     {
3110     free_irq(irq, pdev);
3111     }
3112
3113     mach_info = pdev->dev.platform_data;
3114     if(mach_info->mcu_fmk_pin)
3115     {
3116         free_irq(gpio_to_irq(mach_info->mcu_fmk_pin), pdev);
3117     }
3118
3119     if(mach_info->io_disable)  
3120        mach_info->io_disable();  //close lcd out 
3121
3122     // blank the lcdc
3123     if(inf->fb1)
3124         fb1_blank(FB_BLANK_POWERDOWN, inf->fb1);
3125     if(inf->fb0)
3126         fb0_blank(FB_BLANK_POWERDOWN, inf->fb0);
3127
3128         // suspend the lcdc
3129         //rk29fb_suspend(pdev, msg);
3130     // unmap memory and release framebuffer
3131     if(inf->fb1) {
3132         info = inf->fb1;
3133         if (info->screen_base) {
3134                 //dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
3135                 info->screen_base = 0;
3136                 info->fix.smem_start = 0;
3137                 info->fix.smem_len = 0;
3138         }
3139         unregister_framebuffer(inf->fb1);
3140         framebuffer_release(inf->fb1);
3141         inf->fb1 = NULL;
3142     }
3143     if(inf->fb0) {
3144         info = inf->fb0;
3145         if (info->screen_base) {
3146             //    dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
3147                 info->screen_base = 0;
3148                 info->fix.smem_start = 0;
3149                 info->fix.smem_len = 0;
3150         }
3151         unregister_framebuffer(inf->fb0);
3152         framebuffer_release(inf->fb0);
3153         inf->fb0 = NULL;
3154     }
3155
3156   #if 0 //def CONFIG_CPU_FREQ
3157    // cpufreq_unregister_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
3158   #endif
3159
3160     msleep(100);
3161
3162         if (inf->clk)
3163     {
3164                 clk_disable(inf->clk);
3165                 clk_put(inf->clk);
3166                 inf->clk = NULL;
3167         }
3168     if (inf->dclk)
3169     {
3170                 clk_disable(inf->dclk);
3171                 clk_put(inf->dclk);
3172                 inf->dclk = NULL;
3173         }
3174
3175     kfree(inf);
3176     platform_set_drvdata(pdev, NULL);
3177
3178     return 0;
3179 }
3180
3181 static void rk29fb_shutdown(struct platform_device *pdev)
3182 {
3183     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
3184     struct rk29fb_info *mach_info = pdev->dev.platform_data;;
3185
3186         fbprintk("----------------------------rk29fb_shutdown----------------------------\n");
3187
3188     if(mach_info->io_disable)  
3189        mach_info->io_disable();  //close lcd out 
3190        
3191     if(!inf->in_suspend)
3192     {
3193         LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(1));
3194         LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(1));
3195         LcdWrReg(inf, REG_CFG_DONE, 0x01);
3196         mdelay(100);
3197         clk_disable(inf->aclk_ddr_lcdc);
3198         clk_disable(inf->aclk_disp_matrix);
3199         clk_disable(inf->hclk_cpu_display);
3200         clk_disable(inf->clk);
3201         if(inf->dclk){
3202             clk_disable(inf->dclk);
3203         }
3204         if(inf->clk){
3205             clk_disable(inf->aclk);
3206         }
3207         clk_disable(inf->pd_display);
3208         //pmu_set_power_domain(PD_DISPLAY, 0);
3209                 inf->in_suspend = 1;
3210         }
3211
3212 }
3213
3214 static struct platform_driver rk29fb_driver = {
3215         .probe          = rk29fb_probe,
3216         .remove         = rk29fb_remove,
3217         .driver         = {
3218                 .name   = "rk29-fb",
3219                 .owner  = THIS_MODULE,
3220         },
3221         .shutdown   = rk29fb_shutdown,
3222 };
3223
3224 static int __init rk29fb_init(void)
3225 {
3226         wake_lock_init(&idlelock, WAKE_LOCK_IDLE, "fb");
3227     return platform_driver_register(&rk29fb_driver);
3228 }
3229
3230 static void __exit rk29fb_exit(void)
3231 {
3232     platform_driver_unregister(&rk29fb_driver);
3233 }
3234
3235 fs_initcall(rk29fb_init);
3236 module_exit(rk29fb_exit);
3237
3238
3239 MODULE_AUTHOR("  zyw@rock-chips.com");
3240 MODULE_DESCRIPTION("Driver for rk29 fb device");
3241 MODULE_LICENSE("GPL");
3242
3243