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