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