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