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