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