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