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