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