add panel dpi
[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
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
48 #include <mach/iomux.h>
49 #include <mach/gpio.h>
50 #include <mach/board.h>
51 #include <mach/rk29_iomap.h>
52 //#include <asm/uaccess.h>
53
54 #include "./display/screen/screen.h"
55
56 #define ANDROID_USE_THREE_BUFS  0       //android use three buffers to accelerate UI display in rgb plane
57
58 #if 0
59         #define fbprintk(msg...)        printk(msg);
60 #else
61         #define fbprintk(msg...)
62 #endif
63
64
65 #if 0
66         #define fbprintk2(msg...)       printk(msg);
67 #else
68         #define fbprintk2(msg...)
69 #endif
70
71 #define LcdReadBit(inf, addr, msk)      ((inf->regbak.addr=inf->preg->addr)&(msk))
72 #define LcdWrReg(inf, addr, val)        inf->preg->addr=inf->regbak.addr=(val)
73 #define LcdRdReg(inf, addr)             (inf->preg->addr)
74 #define LcdSetBit(inf, addr, msk)       inf->preg->addr=((inf->regbak.addr) |= (msk))
75 #define LcdClrBit(inf, addr, msk)       inf->preg->addr=((inf->regbak.addr) &= ~(msk))
76 #define LcdSetRegBit(inf, addr, msk)    inf->preg->addr=((inf->preg->addr) |= (msk))
77 #define LcdMskReg(inf, addr, msk, val)  (inf->regbak.addr)&=~(msk);   inf->preg->addr=(inf->regbak.addr|=(val))
78
79
80 #define IsMcuLandscape()                ((SCREEN_MCU==inf->cur_screen->type) && (0==inf->mcu_scandir))
81 #define IsMcuUseFmk()                   ( (2==inf->cur_screen->mcu_usefmk) || (1==inf->cur_screen->mcu_usefmk))
82
83 #define CalScaleW0(x, y)                     (((u32)x*0x1000)/y)
84
85 struct rk29fb_rgb {
86         struct fb_bitfield      red;
87         struct fb_bitfield      green;
88         struct fb_bitfield      blue;
89         struct fb_bitfield      transp;
90 };
91
92 static struct rk29fb_rgb def_rgb_16 = {
93      red:    { offset: 11, length: 5, },
94      green:  { offset: 5,  length: 6, },
95      blue:   { offset: 0,  length: 5, },
96      transp: { offset: 0,  length: 0, },
97 };
98
99 struct win0_par {
100         u32 refcount;
101         u32     pseudo_pal[16];
102         u32 y_offset;
103         u32 uv_offset;
104
105     u8 par_seted;
106     u8 addr_seted;
107 };
108
109 struct win1_par {
110         u32 refcount;
111         u32     pseudo_pal[16];
112         int lstblank;
113 };
114
115 struct rk29fb_inf {
116     struct fb_info *win0fb;
117     struct fb_info *win1fb;
118
119     void __iomem *reg_vir_base;  // virtual basic address of lcdc register
120         u32 reg_phy_base;       // physical basic address of lcdc register
121         u32 len;               // physical map length of lcdc register
122
123     struct clk      *clk;
124     struct clk      *dclk;            //lcdc dclk
125     struct clk      *dclk_parent;     //lcdc dclk divider frequency source
126     struct clk      *dclk_divider;    //lcdc demodulator divider frequency
127     struct clk      *aclk;   //lcdc share memory frequency
128     struct clk      *aclk_parent;     //lcdc aclk divider frequency source
129     unsigned long       dclk_rate;
130
131     /* lcdc reg base address and backup reg */
132     LCDC_REG *preg;
133     LCDC_REG regbak;
134
135         int in_suspend;
136
137     /* variable used in mcu panel */
138         int mcu_needflush;
139         int mcu_isrcnt;
140         u16 mcu_scandir;
141         struct timer_list mcutimer;
142         int mcu_status;
143         u8 mcu_fmksync;
144         int mcu_usetimer;
145         int mcu_stopflush;
146
147     /* external memery */
148         char __iomem *screen_base2;
149     __u32 smem_len2;
150     unsigned long  smem_start2;
151
152     char __iomem *cursor_base;   /* cursor Virtual address*/
153     __u32 cursor_size;           /* Amount of ioremapped VRAM or 0 */
154     unsigned long  cursor_start;
155
156     struct rk29fb_screen lcd_info;
157     struct rk29fb_screen tv_info[5];
158     struct rk29fb_screen hdmi_info[2];
159     struct rk29fb_screen *cur_screen;
160 #ifdef CONFIG_CPU_FREQ
161     struct notifier_block freq_transition;
162 #endif
163
164 };
165
166 typedef enum _TRSP_MODE
167 {
168     TRSP_CLOSE = 0,
169     TRSP_FMREG,
170     TRSP_FMREGEX,
171     TRSP_FMRAM,
172     TRSP_FMRAMEX,
173     TRSP_MASK,
174     TRSP_INVAL
175 } TRSP_MODE;
176
177
178 struct platform_device *g_pdev = NULL;
179 static int win1fb_set_par(struct fb_info *info);
180
181 #if 0
182 #define CHK_SUSPEND(inf)        \
183         if(inf->in_suspend)     {       \
184                 fbprintk(">>>>>> fb is in suspend! return! \n");        \
185                 return -EPERM;  \
186         }
187 #else
188 #define CHK_SUSPEND(inf)
189 #endif
190
191 static DECLARE_WAIT_QUEUE_HEAD(wq);
192 static int wq_condition = 0;
193
194 #if ANDROID_USE_THREE_BUFS
195 static int new_frame_seted = 1;
196 #endif
197
198 void set_lcd_pin(struct platform_device *pdev, int enable)
199 {
200         struct rk29fb_info *mach_info = pdev->dev.platform_data;
201
202         unsigned display_on = mach_info->disp_on_pin;
203         unsigned lcd_standby = mach_info->standby_pin;
204
205         int display_on_pol = mach_info->disp_on_value;
206         int lcd_standby_pol = mach_info->standby_value;
207
208         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
209         fbprintk(">>>>>> display_on(%d) = %d \n", display_on, enable ? display_on_pol : !display_on_pol);
210         fbprintk(">>>>>> lcd_standby(%d) = %d \n", lcd_standby, enable ? lcd_standby_pol : !lcd_standby_pol);
211
212     // set display_on
213
214     if(display_on != INVALID_GPIO)
215     {
216         gpio_direction_output(display_on, 0);
217                 gpio_set_value(display_on, enable ? display_on_pol : !display_on_pol);
218     }
219     if(lcd_standby != INVALID_GPIO)
220     {
221         gpio_direction_output(lcd_standby, 0);
222                 gpio_set_value(lcd_standby, enable ? lcd_standby_pol : !lcd_standby_pol);
223     }
224 }
225
226 int mcu_do_refresh(struct rk29fb_inf *inf)
227 {
228     if(inf->mcu_stopflush)  return 0;
229
230     if(SCREEN_MCU!=inf->cur_screen->type)   return 0;
231
232     // use frame mark
233     if(IsMcuUseFmk())
234     {
235         inf->mcu_needflush = 1;
236         return 0;
237     }
238
239     // not use frame mark
240     if(LcdReadBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_SELECT))
241     {
242         if(!LcdReadBit(inf, MCU_TIMING_CTRL, m_MCU_HOLD_STATUS))
243         {
244             inf->mcu_needflush = 1;
245         }
246         else
247         {
248             if(inf->cur_screen->refresh)    inf->cur_screen->refresh(REFRESH_PRE);
249             inf->mcu_needflush = 0;
250             inf->mcu_isrcnt = 0;
251             LcdSetRegBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST);
252         }
253     }
254     return 0;
255 }
256
257
258 void mcutimer_callback(unsigned long arg)
259 {
260     struct rk29fb_inf *inf = platform_get_drvdata(g_pdev);
261     static int waitcnt = 0;
262
263     mod_timer(&inf->mcutimer, jiffies + HZ/10);
264
265     switch(inf->mcu_status)
266     {
267     case MS_IDLE:
268         inf->mcu_status = MS_MCU;
269         break;
270     case MS_MCU:
271         if(inf->mcu_usetimer)   mcu_do_refresh(inf);
272         break;
273     case MS_EWAITSTART:
274         inf->mcu_status = MS_EWAITEND;
275         waitcnt = 0;
276         break;
277     case MS_EWAITEND:
278         if(0==waitcnt) {
279             mcu_do_refresh(inf);
280         }
281         if(waitcnt++>14) {
282             inf->mcu_status = MS_EEND;
283         }
284         break;
285     case MS_EEND:
286         inf->mcu_status = MS_MCU;
287         break;
288     default:
289         inf->mcu_status = MS_MCU;
290         break;
291     }
292 }
293
294 int mcu_refresh(struct rk29fb_inf *inf)
295 {
296     static int mcutimer_inited = 0;
297
298     if(SCREEN_MCU!=inf->cur_screen->type)   return 0;
299
300     if(!mcutimer_inited)
301     {
302         mcutimer_inited = 1;
303         init_timer(&inf->mcutimer);
304         inf->mcutimer.function = mcutimer_callback;
305         inf->mcutimer.expires = jiffies + HZ/5;
306         inf->mcu_status = MS_IDLE;
307         add_timer(&inf->mcutimer);
308     }
309
310     if(MS_MCU==inf->mcu_status)     mcu_do_refresh(inf);
311
312     return 0;
313 }
314
315 int mcu_ioctl(unsigned int cmd, unsigned long arg)
316 {
317     struct rk29fb_inf *inf = NULL;
318     if(!g_pdev)     return -1;
319
320     inf = dev_get_drvdata(&g_pdev->dev);
321
322     switch(cmd)
323     {
324     case MCU_WRCMD:
325         LcdClrBit(inf, MCU_TIMING_CTRL, m_MCU_RS_SELECT);
326         LcdWrReg(inf, MCU_BYPASS_WPORT, arg);
327         LcdSetBit(inf, MCU_TIMING_CTRL, m_MCU_RS_SELECT);
328         break;
329
330     case MCU_WRDATA:
331         LcdSetBit(inf, MCU_TIMING_CTRL, m_MCU_RS_SELECT);
332         LcdWrReg(inf, MCU_BYPASS_WPORT, arg);
333         break;
334
335     case MCU_SETBYPASS:
336         LcdMskReg(inf, MCU_TIMING_CTRL, m_MCU_BYPASSMODE_SELECT, v_MCU_BYPASSMODE_SELECT(arg));
337         LcdWrReg(inf, REG_CFG_DONE, 0x01);
338         break;
339
340     default:
341         break;
342     }
343
344     return 0;
345 }
346
347 static irqreturn_t mcu_irqfmk(int irq, void *dev_id)
348 {
349         struct platform_device *pdev = (struct platform_device*)dev_id;
350     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
351     struct rk29fb_screen *screen;
352
353     if(!inf)    return IRQ_HANDLED;
354
355     screen = inf->cur_screen;
356
357     if(0==screen->mcu_usefmk) {
358         return IRQ_HANDLED;
359     }
360
361     if(inf->mcu_fmksync == 1)
362         return IRQ_HANDLED;
363
364     inf->mcu_fmksync = 1;
365     if(inf->mcu_needflush)
366     {
367         inf->mcu_needflush = 0;
368         inf->mcu_isrcnt = 0;
369         if(inf->cur_screen->refresh)
370            inf->cur_screen->refresh(REFRESH_PRE);
371         LcdSetBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST);
372     }
373     inf->mcu_fmksync = 0;
374
375         return IRQ_HANDLED;
376 }
377
378 int init_lcdc(struct fb_info *info)
379 {
380     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
381     u32 msk=0, clr=0;
382
383         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
384
385         // set AHB access rule and disable all windows
386     LcdWrReg(inf, SYS_CONFIG, 0x20000000);
387     LcdWrReg(inf, SWAP_CTRL, 0);
388     LcdWrReg(inf, FIFO_WATER_MARK, 0x00000860);
389
390         // and mcu holdmode; and set win1 top.
391     LcdMskReg(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_SELECT | m_MCU_HOLDMODE_FRAME_ST | m_MCU_BYPASSMODE_SELECT ,
392             v_MCU_HOLDMODE_SELECT(0)| v_MCU_HOLDMODE_FRAME_ST(0) |v_MCU_BYPASSMODE_SELECT(0));
393
394     // disable blank out, black out, tristate out, yuv2rgb bypass
395     LcdMskReg(inf, BLEND_CTRL,m_W2_BLEND_EN | m_W1_BLEND_EN | m_W0_BLEND_EN | m_HWC_BLEND_EN |
396              m_HWC_BLEND_FACTOR | m_W1_BLEND_FACTOR | m_W0_BLEND_FACTOR,
397              v_W2_BLEND_EN(0) |v_W1_BLEND_EN(0) | v_W0_BLEND_EN(0) | v_HWC_BLEND_EN(0) |
398              v_HWC_BLEND_FACTOR(0) | v_W2_BLEND_FACTOR(0) | v_W1_BLEND_FACTOR(0) | v_W0_BLEND_FACTOR(0)
399              );
400
401     LcdMskReg(inf, WIN0_COLOR_KEY_CTRL, m_COLORKEY_EN, v_COLORKEY_EN(0));
402     LcdMskReg(inf, WIN1_COLOR_KEY_CTRL, m_COLORKEY_EN, v_COLORKEY_EN(0));
403
404     LcdWrReg(inf, DSP_CTRL0, 0);
405     LcdWrReg(inf, DSP_CTRL1, 0);
406
407     // initialize all interrupt
408     clr = v_HOR_STARTCLEAR(1) | v_FRM_STARTCLEAR(1) | v_SCANNING_CLEAR(1);
409
410     msk = v_HOR_STARTMASK(1) | v_FRM_STARTMASK(0) | v_SCANNING_MASK(1);
411
412     LcdWrReg(inf, INT_STATUS, clr | msk);
413
414         // let above to take effect
415     LcdWrReg(inf, REG_CFG_DONE, 0x01);
416
417     return 0;
418 }
419
420 void load_screen(struct fb_info *info, bool initscreen)
421 {
422     int ret = -EINVAL;
423     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
424     struct rk29fb_screen *screen = inf->cur_screen;
425     u16 face = screen->face;
426     u16 mcu_total, mcu_rwstart, mcu_csstart, mcu_rwend, mcu_csend;
427     u16 right_margin = screen->right_margin, lower_margin = screen->lower_margin;
428     u16 x_res = screen->x_res, y_res = screen->y_res;
429     u32 clk_rate = 0;
430     u32 dclk_rate = 0;
431     u32 aclk_rate = 150000000;
432
433     if(!g_pdev)     return -1;
434
435         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
436
437     // set the rgb or mcu
438     LcdMskReg(inf, MCU_TIMING_CTRL, m_MCU_OUTPUT_SELECT, v_MCU_OUTPUT_SELECT((SCREEN_MCU==screen->type)?(1):(0)));
439
440         // set out format and mcu timing
441     mcu_total  = (screen->mcu_wrperiod*150*1000)/1000000;
442     if(mcu_total>31)    mcu_total = 31;
443     if(mcu_total<3)     mcu_total = 3;
444     mcu_rwstart = (mcu_total+1)/4 - 1;
445     mcu_rwend = ((mcu_total+1)*3)/4 - 1;
446     mcu_csstart = (mcu_rwstart>2) ? (mcu_rwstart-3) : (0);
447     mcu_csend = (mcu_rwend>15) ? (mcu_rwend-1) : (mcu_rwend);
448
449     fbprintk(">> mcu_total=%d, mcu_rwstart=%d, mcu_csstart=%d, mcu_rwend=%d, mcu_csend=%d \n",
450         mcu_total, mcu_rwstart, mcu_csstart, mcu_rwend, mcu_csend);
451
452     LcdMskReg(inf, MCU_TIMING_CTRL,
453              m_MCU_CS_ST | m_MCU_CS_END| m_MCU_RW_ST | m_MCU_RW_END |
454              m_MCU_WRITE_PERIOD | m_MCU_HOLDMODE_SELECT | m_MCU_HOLDMODE_FRAME_ST,
455             v_MCU_CS_ST(mcu_csstart) | v_MCU_CS_END(mcu_csend) | v_MCU_RW_ST(mcu_rwstart) |
456             v_MCU_RW_END(mcu_rwend) |  v_MCU_WRITE_PERIOD(mcu_total) |
457             v_MCU_HOLDMODE_SELECT((SCREEN_MCU==screen->type)?(1):(0)) | v_MCU_HOLDMODE_FRAME_ST(0)
458            );
459
460         // set synchronous pin polarity and data pin swap rule
461      LcdMskReg(inf, DSP_CTRL0,
462         m_DISPLAY_FORMAT | m_HSYNC_POLARITY | m_VSYNC_POLARITY | m_DEN_POLARITY |
463         m_DCLK_POLARITY | m_COLOR_SPACE_CONVERSION,
464         v_DISPLAY_FORMAT(face) | v_HSYNC_POLARITY(screen->pin_hsync) | v_VSYNC_POLARITY(screen->pin_vsync) |
465         v_DEN_POLARITY(screen->pin_den) | v_DCLK_POLARITY(screen->pin_dclk) | v_COLOR_SPACE_CONVERSION(0)
466         );
467
468      LcdMskReg(inf, DSP_CTRL1, m_BG_COLOR,  v_BG_COLOR(0x000000) );
469
470      LcdMskReg(inf, SWAP_CTRL, m_OUTPUT_RB_SWAP | m_OUTPUT_RG_SWAP | m_DELTA_SWAP | m_DUMMY_SWAP,
471             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));
472
473         // set horizontal & vertical out timing
474         if(SCREEN_MCU==inf->cur_screen->type)
475     {
476             right_margin = x_res/6;
477         }
478
479     printk("screen->hsync_len =%d,  screen->left_margin =%d, x_res =%d,  right_margin = %d \n",
480         screen->hsync_len , screen->left_margin , x_res , right_margin );
481     LcdMskReg(inf, DSP_HTOTAL_HS_END, m_BIT11LO | m_BIT11HI, v_BIT11LO(screen->hsync_len) |
482              v_BIT11HI(screen->hsync_len + screen->left_margin + x_res + right_margin));
483     LcdMskReg(inf, DSP_HACT_ST_END, m_BIT11LO | m_BIT11HI, v_BIT11LO(screen->hsync_len + screen->left_margin + x_res) |
484              v_BIT11HI(screen->hsync_len + screen->left_margin));
485
486     LcdMskReg(inf, DSP_VTOTAL_VS_END, m_BIT11LO | m_BIT11HI, v_BIT11LO(screen->vsync_len) |
487               v_BIT11HI(screen->vsync_len + screen->upper_margin + y_res + lower_margin));
488     LcdMskReg(inf, DSP_VACT_ST_END, m_BIT11LO | m_BIT11HI,  v_BIT11LO(screen->vsync_len + screen->upper_margin+y_res)|
489               v_BIT11HI(screen->vsync_len + screen->upper_margin));
490
491     LcdMskReg(inf, DSP_VS_ST_END_F1, m_BIT11LO | m_BIT11HI, v_BIT11LO(0) | v_BIT11HI(0));
492     LcdMskReg(inf, DSP_VACT_ST_END_F1, m_BIT11LO | m_BIT11HI, v_BIT11LO(0) | v_BIT11HI(0));
493
494         // let above to take effect
495     LcdWrReg(inf, REG_CFG_DONE, 0x01);
496
497     inf->clk = clk_get(&g_pdev->dev, "hclk_lcdc");
498     if (!inf->clk || IS_ERR(inf->clk))
499     {
500         printk(KERN_ERR "failed to get lcdc_hclk source\n");
501         return ;
502     }
503
504     inf->dclk = clk_get(&g_pdev->dev, "dclk_lcdc");
505         if (!inf->dclk || IS_ERR(inf->dclk))
506     {
507                 printk(KERN_ERR "failed to get lcd dclock source\n");
508                 return ;
509         }
510     inf->dclk_divider= clk_get(&g_pdev->dev, "dclk_lcdc_div");
511     if (!inf->dclk_divider || IS_ERR(inf->dclk_divider))
512     {
513                 printk(KERN_ERR "failed to get lcd clock lcdc_divider source \n");
514                 return ;
515         }
516
517     if(inf->cur_screen == &inf->lcd_info)    {
518         inf->dclk_parent = clk_get(&g_pdev->dev, "periph_pll");
519     }    else    {
520         inf->dclk_parent = clk_get(&g_pdev->dev, "codec_pll");
521     }
522
523     if (!inf->dclk_parent || IS_ERR(inf->dclk_parent))
524     {
525                 printk(KERN_ERR "failed to get lcd dclock parent source\n");
526                 return ;
527         }
528
529     inf->aclk = clk_get(&g_pdev->dev, "aclk_lcdc");
530     if (!inf->aclk || IS_ERR(inf->aclk))
531     {
532                 printk(KERN_ERR "failed to get lcd clock clk_share_mem source \n");
533                 return ;
534         }
535     inf->aclk_parent = clk_get(&g_pdev->dev, "periph_pll");
536     if (!inf->dclk_parent || IS_ERR(inf->dclk_parent))
537     {
538                 printk(KERN_ERR "failed to get lcd dclock parent source\n");
539                 return ;
540         }
541
542     // set lcdc clk
543     if(SCREEN_MCU==screen->type)    screen->pixclock = 150; //mcu fix to 150 MHz
544
545     clk_set_parent(inf->dclk_divider, inf->dclk_parent);
546     clk_set_parent(inf->dclk, inf->dclk_divider);
547     clk_set_parent(inf->aclk, inf->aclk_parent);
548
549     dclk_rate = screen->pixclock * 1000000;
550
551     fbprintk(">>>>>> set lcdc dclk need %d HZ, clk_parent = %d hz \n ", screen->pixclock, clk_rate);
552
553     ret = clk_set_rate(inf->dclk_divider, dclk_rate);
554     if(ret)
555     {
556         printk(KERN_ERR ">>>>>> set lcdc dclk_divider faild \n ");
557     }
558     if(screen->lcdc_aclk){
559         aclk_rate = screen->lcdc_aclk * 1000000;
560     }
561     ret = clk_set_rate(inf->aclk, aclk_rate);
562     if(ret){
563         printk(KERN_ERR ">>>>>> set lcdc dclk_divider faild \n ");
564     }
565
566     clk_enable(inf->dclk);
567     clk_enable(inf->clk);
568     clk_enable(inf->aclk);
569
570     // init screen panel
571     if(screen->init && initscreen)
572     {
573         screen->init();
574     }
575
576 }
577 #ifdef CONFIG_CPU_FREQ
578 /*
579 * CPU clock speed change handler. We need to adjust the LCD timing
580 * parameters when the CPU clock is adjusted by the power management
581 * subsystem.
582 */
583 #define TO_INF(ptr,member) container_of(ptr,struct rk29fb_inf,member)
584
585 static int
586 rk29fb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
587 {
588     struct rk29fb_inf *inf = TO_INF(nb, freq_transition);
589     struct rk29fb_screen *screen = inf->cur_screen;
590     u32 dclk_rate = 0;
591
592     switch (val)
593     {
594     case CPUFREQ_PRECHANGE:
595           break;
596     case CPUFREQ_POSTCHANGE:
597         {
598          dclk_rate = screen->pixclock * 1000000;
599
600          fbprintk(">>>>>> set lcdc dclk need %d HZ, clk_parent = %d hz \n ", screen->pixclock, dclk_rate);
601
602          clk_set_rate(inf->dclk_divider, dclk_rate);
603          break;
604         }
605     }
606     return 0;
607 }
608 #endif
609
610 static inline unsigned int chan_to_field(unsigned int chan,
611                                          struct fb_bitfield *bf)
612 {
613         chan &= 0xffff;
614         chan >>= 16 - bf->length;
615         return chan << bf->offset;
616 }
617
618 static int fb_setcolreg(unsigned regno,
619                                unsigned red, unsigned green, unsigned blue,
620                                unsigned transp, struct fb_info *info)
621 {
622         unsigned int val;
623 //      fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
624
625         switch (info->fix.visual) {
626         case FB_VISUAL_TRUECOLOR:
627                 /* true-colour, use pseudo-palette */
628                 if (regno < 16) {
629                         u32 *pal = info->pseudo_palette;
630                         val  = chan_to_field(red,   &info->var.red);
631                         val |= chan_to_field(green, &info->var.green);
632                         val |= chan_to_field(blue,  &info->var.blue);
633                         pal[regno] = val;
634                 }
635                 break;
636         default:
637                 return -1;      /* unknown type */
638         }
639
640         return 0;
641 }
642 #if 0
643
644 int rk29_set_cursor(struct fb_info *info, struct fb_cursor *cursor)
645 {
646     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
647
648     //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
649
650     /* check not being asked to exceed capabilities */
651
652     if (cursor->image.width > 32)
653         return -EINVAL;
654
655     if (cursor->image.height > 32)
656         return -EINVAL;
657
658     if (cursor->image.depth > 1)
659         return -EINVAL;
660
661     if (cursor->enable)
662         LcdSetBit(inf, SYS_CONFIG, m_HWC_ENABLE);
663     else
664         LcdClrBit(inf, SYS_CONFIG, m_HWC_ENABLE);
665
666     /* set data */
667     if (cursor->set & FB_CUR_SETPOS)
668     {
669         unsigned int x = cursor->image.dx;
670         unsigned int y = cursor->image.dy;
671
672         if (x >= 0x800 || y >= 0x800 )
673             return -EINVAL;
674         LcdWrReg(inf, HWC_DSP_ST, v_BIT11LO(x)|v_BIT11HI(y));
675     }
676
677
678     if (cursor->set & FB_CUR_SETCMAP)
679     {
680         unsigned int bg_col = cursor->image.bg_color;
681         unsigned int fg_col = cursor->image.fg_color;
682
683         fbprintk("%s: update cmap (%08x,%08x)\n",
684             __func__, bg_col, fg_col);
685
686         LcdMskReg(inf, HWC_COLOR_LUT0, m_HWC_R|m_HWC_G|m_HWC_B,
687                   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));
688
689         LcdMskReg(inf, HWC_COLOR_LUT2, m_HWC_R|m_HWC_G|m_HWC_B,
690                          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));
691     }
692
693     if ((cursor->set & FB_CUR_SETSIZE ||
694         cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE))
695         && info->screen_base && info->fix.smem_start && info->fix.smem_len)
696     {
697         /* rk29 cursor is a 2 bpp 32x32 bitmap this routine
698          * clears it to transparent then combines the cursor
699          * shape plane with the colour plane to set the
700          * cursor */
701         int x, y;
702         const unsigned char *pcol = cursor->image.data;
703         const unsigned char *pmsk = cursor->mask;
704         void __iomem   *dst;
705         unsigned long cursor_mem_start;
706         unsigned char  dcol = 0;
707         unsigned char  dmsk = 0;
708         unsigned int   op;
709
710         dst = info->screen_base + info->fix.smem_len - CURSOR_BUF_SIZE;
711             cursor_mem_start = info->fix.smem_start + info->fix.smem_len - CURSOR_BUF_SIZE;
712
713         fbprintk("%s: setting shape (%d,%d)\n",
714             __func__, cursor->image.width, cursor->image.height);
715
716         memset(dst, 0, CURSOR_BUF_SIZE);
717
718         for (y = 0; y < cursor->image.height; y++)
719         {
720             for (x = 0; x < cursor->image.width; x++)
721             {
722                 if ((x % 8) == 0) {
723                     dcol = *pcol++;
724                     dmsk = *pmsk++;
725                 } else {
726                     dcol >>= 1;
727                     dmsk >>= 1;
728                 }
729
730                 if (dmsk & 1) {
731                     op = (dcol & 1) ? 1 : 3;
732                     op <<= ((x % 4) * 2);
733                     *(u8*)(dst+(x/4)) |= op;
734                 }
735             }
736             dst += (32*2)/8;
737         }
738         LcdSetBit(inf, SYS_CONFIG,m_HWC_RELOAD_EN);
739         LcdWrReg(inf, HWC_MST, cursor_mem_start);
740         // flush end when wq_condition=1 in mcu panel, but not in rgb panel
741         if(SCREEN_MCU == inf->cur_screen->type) {
742             wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
743             wq_condition = 0;
744         } else {
745             wq_condition = 0;
746             wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
747         }
748         LcdClrBit(inf, SYS_CONFIG, m_HWC_RELOAD_EN);
749     }
750
751     return 0;
752 }
753 #endif
754
755 static int win0fb_blank(int blank_mode, struct fb_info *info)
756 {
757     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
758
759     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
760
761         CHK_SUSPEND(inf);
762
763     switch(blank_mode)
764     {
765     case FB_BLANK_UNBLANK:
766         LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(1));
767         break;
768     default:
769         LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(0));
770         break;
771     }
772     LcdWrReg(inf, REG_CFG_DONE, 0x01);
773
774         mcu_refresh(inf);
775     return 0;
776 }
777
778 static int win0fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
779 {
780     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
781     struct rk29fb_screen *screen = inf->cur_screen;
782
783     u32 ScaleYRGBY=0x1000;
784     u16 xpos = (var->nonstd>>8) & 0xfff;   //offset in panel
785     u16 ypos = (var->nonstd>>20) & 0xfff;
786     u16 xsize = (var->grayscale>>8) & 0xfff;   //visiable size in panel
787     u16 ysize = (var->grayscale>>20) & 0xfff;
788     u16 xlcd = screen->x_res;        //size of panel
789     u16 ylcd = screen->y_res;
790     u16 yres = 0;
791
792     if(inf->win0fb->var.rotate == 270) {
793         xlcd = screen->y_res;
794         ylcd = screen->x_res;
795     }
796
797     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
798
799         CHK_SUSPEND(inf);
800
801     if( 0==var->xres_virtual || 0==var->yres_virtual ||
802         0==var->xres || 0==var->yres || var->xres<16 ||
803         0==xsize || 0==ysize || xsize<16 ||
804         ((16!=var->bits_per_pixel)&&(32!=var->bits_per_pixel)) )
805     {
806         printk(">>>>>> win0fb_check_var fail 1!!! \n");
807                 printk("0==%d || 0==%d || 0==%d || 0==%d || %d<16 \n ||0==%d || 0==%d || %d<16 ||((16!=%d)&&(32!=%d)) \n",
808                                 var->xres_virtual, var->yres_virtual, var->xres, var->yres, var->xres, xsize, ysize, xsize,
809                         var->bits_per_pixel, var->bits_per_pixel);
810         return -EINVAL;
811     }
812
813     if( (var->xoffset+var->xres)>var->xres_virtual ||
814         (var->yoffset+var->yres)>var->yres_virtual ||
815         (xpos+xsize)>xlcd || (ypos+ysize)>ylcd )
816     {
817         printk(">>>>>> win0fb_check_var fail 2!!! \n");
818                 printk("(%d+%d)>%d || (%d+%d)>%d || (%d+%d)>%d || (%d+%d)>%d \n ",
819                                 var->xoffset, var->xres, var->xres_virtual, var->yoffset, var->yres,
820                                 var->yres_virtual, xpos, xsize, xlcd, ypos, ysize, ylcd);
821         return -EINVAL;
822     }
823
824     switch(var->nonstd&0x0f)
825     {
826     case 0: // rgb
827         switch(var->bits_per_pixel)
828         {
829         case 16:    // rgb565
830             var->xres_virtual = (var->xres_virtual + 0x1) & (~0x1);
831             var->xres = (var->xres + 0x1) & (~0x1);
832             var->xoffset = (var->xoffset) & (~0x1);
833             break;
834         default:    // rgb888
835             var->bits_per_pixel = 32;
836             break;
837         }
838         var->nonstd &= ~0xc0;  //not support I2P in this format
839         break;
840     case 1: // yuv422
841         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
842         var->xres = (var->xres + 0x3) & (~0x3);
843         var->xoffset = (var->xoffset) & (~0x3);
844         break;
845     case 2: // yuv4200
846         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
847         var->yres_virtual = (var->yres_virtual + 0x1) & (~0x1);
848         var->xres = (var->xres + 0x3) & (~0x3);
849         var->yres = (var->yres + 0x1) & (~0x1);
850         var->xoffset = (var->xoffset) & (~0x3);
851         var->yoffset = (var->yoffset) & (~0x1);
852         break;
853     case 3: // yuv4201
854         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
855         var->yres_virtual = (var->yres_virtual + 0x1) & (~0x1);
856         var->xres = (var->xres + 0x3) & (~0x3);
857         var->yres = (var->yres + 0x1) & (~0x1);
858         var->xoffset = (var->xoffset) & (~0x3);
859         var->yoffset = (var->yoffset) & (~0x1);
860         var->nonstd &= ~0xc0;   //not support I2P in this format
861         break;
862     case 4: // none
863     case 5: // yuv444
864         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
865         var->xres = (var->xres + 0x3) & (~0x3);
866         var->xoffset = (var->xoffset) & (~0x3);
867         var->nonstd &= ~0xc0;   //not support I2P in this format
868         break;
869     default:
870         printk(">>>>>> win0fb var->nonstd=%d is invalid! \n", var->nonstd);
871         return -EINVAL;
872     }
873
874     if(var->rotate == 270)
875     {
876         yres = var->xres;
877     }
878     else
879     {
880         yres = var->yres;
881     }
882     ScaleYRGBY = CalScaleW0(yres, ysize);
883
884     if((ScaleYRGBY>0x8000) || (ScaleYRGBY<0x200))
885     {
886         return -EINVAL;        // multiple of scale down or scale up can't exceed 8
887     }
888
889     return 0;
890 }
891
892 static int win0fb_set_par(struct fb_info *info)
893 {
894     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
895     struct rk29fb_screen *screen = inf->cur_screen;
896     struct fb_var_screeninfo *var = &info->var;
897     struct fb_fix_screeninfo *fix = &info->fix;
898     struct win0_par *par = info->par;
899
900     u8 format = 0;
901     u32 cblen=0, crlen=0, map_size=0, smem_len=0;
902
903         u32 xact = var->xres;                       /* visible resolution               */
904         u32 yact = var->yres;
905         u32 xvir = var->xres_virtual;           /* virtual resolution           */
906         u32 yvir = var->yres_virtual;
907         u32 xact_st = var->xoffset;                     /* offset from virtual to visible */
908         u32 yact_st = var->yoffset;                     /* resolution                   */
909
910     u16 xpos = (var->nonstd>>8) & 0xfff;      //visiable pos in panel
911     u16 ypos = (var->nonstd>>20) & 0xfff;
912     u16 xsize = (var->grayscale>>8) & 0xfff;  //visiable size in panel
913     u16 ysize = (var->grayscale>>20) & 0xfff;
914
915     u32 ScaleYrgbX=0x1000,ScaleYrgbY=0x1000;
916     u32 ScaleCbrX=0x1000, ScaleCbrY=0x1000;
917
918     u8 data_format = var->nonstd&0x0f;
919     u32 win0_en = var->reserved[2];
920     u32 y_addr = var->reserved[3];       //user alloc buf addr y
921     u32 uv_addr = var->reserved[4];
922
923     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
924
925         CHK_SUSPEND(inf);
926
927         /* calculate y_offset,uv_offset,line_length,cblen and crlen  */
928     switch (data_format)
929     {
930     case 0: // rgb
931         switch(var->bits_per_pixel)
932         {
933         case 16:    // rgb565
934             format = 1;
935             fix->line_length = 2 * xvir;
936             par->y_offset = (yact_st*xvir + xact_st)*2;
937             break;
938         case 32:    // rgb888
939             format = 0;
940             fix->line_length = 4 * xvir;
941             par->y_offset = (yact_st*xvir + xact_st)*4;
942             break;
943         default:
944             return -EINVAL;
945         }
946         break;
947     case 1: // yuv422
948         format = 2;
949         fix->line_length = xvir;
950         cblen = crlen = (xvir*yvir)/2;
951         par->y_offset = yact_st*xvir + xact_st;
952         par->uv_offset = yact_st*xvir + xact_st;
953         break;
954     case 2: // yuv4200
955         format = 3;
956         fix->line_length = xvir;
957         cblen = crlen = (xvir*yvir)/4;
958
959         par->y_offset = yact_st*xvir + xact_st;
960         par->uv_offset = (yact_st/2)*xvir + xact_st;
961
962         break;
963     case 3: // yuv4201
964         format = 4;
965         fix->line_length = xvir;
966         par->y_offset = (yact_st/2)*2*xvir + (xact_st)*2;
967         par->uv_offset = (yact_st/2)*xvir + xact_st;
968         cblen = crlen = (xvir*yvir)/4;
969         break;
970     case 4: // none
971     case 5: // yuv444
972         format = 5;
973         fix->line_length = xvir;
974         par->y_offset = yact_st*xvir + xact_st;
975         par->uv_offset = yact_st*2*xvir + xact_st*2;
976         cblen = crlen = (xvir*yvir);
977         break;
978     default:
979         return -EINVAL;
980     }
981
982     smem_len = fix->line_length * yvir + cblen + crlen;
983     map_size = PAGE_ALIGN(smem_len);
984
985     if (info->screen_base) {
986         printk(">>>>>> win0fb unmap memory(%d)! \n", info->fix.smem_len);
987         dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
988         info->screen_base = 0;
989     }
990     fix->smem_start = y_addr;
991     fix->smem_len = smem_len;
992     fix->mmio_start = uv_addr;
993
994     par->addr_seted = ((-1==(int)y_addr)&&(-1==(int)uv_addr)) ? 0 : 1;
995     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);
996
997         // calculate the display phy address
998     y_addr = fix->smem_start + par->y_offset;
999     uv_addr = fix->mmio_start + par->uv_offset;
1000
1001     fbprintk("y_addr 0x%08x = 0x%08x + %d\n", y_addr, (u32)fix->smem_start, par->y_offset);
1002     fbprintk("uv_addr 0x%08x = 0x%08x + %d\n", uv_addr, (u32)fix->mmio_start , par->uv_offset);
1003
1004     ScaleYrgbX = CalScaleW0(xact, xsize);
1005     ScaleYrgbY = CalScaleW0(yact, ysize);
1006
1007     switch (data_format)
1008     {
1009        case 1:// yuv422
1010            ScaleCbrX= CalScaleW0((xact/2), xsize);
1011            ScaleCbrY =  CalScaleW0(yact, ysize);
1012            break;
1013        case 2: // yuv4200
1014        case 3: // yuv4201
1015            ScaleCbrX= CalScaleW0(xact/2, xsize);
1016            ScaleCbrY =  CalScaleW0(yact/2, ysize);
1017            break;
1018        case 4: // none
1019        case 5:// yuv444
1020            ScaleCbrX= CalScaleW0(xact, xsize);
1021            ScaleCbrY =  CalScaleW0(yact, ysize);
1022            break;
1023     }
1024
1025     xpos += (screen->left_margin + screen->hsync_len);
1026     ypos += (screen->upper_margin + screen->vsync_len);
1027
1028     LcdWrReg(inf, WIN0_YRGB_MST, y_addr);
1029     LcdWrReg(inf, WIN0_CBR_MST, uv_addr);
1030
1031     LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE | m_W0_FORMAT, v_W0_ENABLE(win0_en) | v_W0_FORMAT(format));
1032
1033     LcdMskReg(inf, WIN0_VIR, m_WORDLO | m_WORDHI, v_VIRWIDTH(xvir) | v_VIRHEIGHT((yvir)) );
1034     LcdMskReg(inf, WIN0_ACT_INFO, m_WORDLO | m_WORDHI, v_WORDLO(xact) | v_WORDHI(yact));
1035     LcdMskReg(inf, WIN0_DSP_ST, m_BIT11LO | m_BIT11HI, v_BIT11LO(xpos) | v_BIT11HI(ypos));
1036     LcdMskReg(inf, WIN0_DSP_INFO, m_BIT11LO | m_BIT11HI,  v_BIT11LO(xsize) | v_BIT11HI(ysize));
1037     LcdMskReg(inf, WIN0_SCL_FACTOR_YRGB, m_WORDLO | m_WORDHI, v_WORDLO(ScaleYrgbX) | v_WORDHI(ScaleYrgbY));
1038     LcdMskReg(inf, WIN0_SCL_FACTOR_CBR, m_WORDLO | m_WORDHI, v_WORDLO(ScaleCbrX) | v_WORDHI(ScaleCbrY));
1039
1040     switch(format)
1041     {
1042     case 1:  //rgb565
1043         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,
1044             v_W0_YRGB_8_SWAP(0) | v_W0_YRGB_16_SWAP(0) | v_W0_YRGB_R_SHIFT_SWAP(0) | v_W0_565_RB_SWAP(1) | v_W0_YRGB_M8_SWAP(0) | v_W0_CBR_8_SWAP(0));
1045         break;
1046     case 4:   //yuv4201
1047         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,
1048             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) |
1049             v_W0_YRGB_M8_SWAP((var->rotate==0)) | v_W0_CBR_8_SWAP(0));
1050         break;
1051     default:
1052         LcdMskReg(inf, SWAP_CTRL, m_W0_YRGB_8_SWAP | m_W0_YRGB_16_SWAP | m_W0_YRGB_R_SHIFT_SWAP | m_W0_565_RB_SWAP | m_W0_YRGB_M8_SWAP | m_W0_CBR_8_SWAP,
1053             v_W0_YRGB_8_SWAP(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) );
1054     }
1055
1056     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1057
1058     return 0;
1059 }
1060
1061 static int win0fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1062 {
1063     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1064    // struct fb_var_screeninfo *var0 = &info->var;
1065     struct fb_fix_screeninfo *fix0 = &info->fix;
1066     struct win0_par *par = info->par;
1067     u32 y_addr=0, uv_addr=0;
1068
1069     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1070
1071         CHK_SUSPEND(inf);
1072
1073     y_addr = fix0->smem_start +  par->y_offset;//y_offset;
1074     uv_addr = fix0->mmio_start + par->uv_offset ;//uv_offset;
1075
1076     LcdWrReg(inf, WIN0_YRGB_MST, y_addr);
1077     LcdWrReg(inf, WIN0_CBR_MST, uv_addr);
1078     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1079
1080      // enable win0 after the win0 addr is seted
1081     par->par_seted = 1;
1082         LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE((1==par->addr_seted)?(1):(0)));
1083         mcu_refresh(inf);
1084
1085     return 0;
1086 }
1087
1088 int win0fb_open(struct fb_info *info, int user)
1089 {
1090     struct win0_par *par = info->par;
1091
1092     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1093
1094     par->par_seted = 0;
1095     par->addr_seted = 0;
1096
1097     if(par->refcount) {
1098         printk(">>>>>> win0fb has opened! \n");
1099         return -EACCES;
1100     } else {
1101         par->refcount++;
1102         return 0;
1103     }
1104 }
1105
1106 int win0fb_release(struct fb_info *info, int user)
1107 {
1108     struct win0_par *par = info->par;
1109         struct fb_var_screeninfo *var0 = &info->var;
1110
1111     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1112
1113     if(par->refcount) {
1114         par->refcount--;
1115
1116         win0fb_blank(FB_BLANK_POWERDOWN, info);
1117         // wait for lcdc stop access memory
1118         msleep(50);
1119
1120         // unmap memory
1121         if (info->screen_base) {
1122             printk(">>>>>> win0fb unmap memory(%d)! \n", info->fix.smem_len);
1123             dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
1124             info->screen_base = 0;
1125             info->fix.smem_start = 0;
1126             info->fix.smem_len = 0;
1127         }
1128
1129                 // clean the var param
1130                 memset(var0, 0, sizeof(struct fb_var_screeninfo));
1131     }
1132
1133     return 0;
1134 }
1135
1136 static int win0fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
1137 {
1138     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1139     struct win0_par *par = info->par;
1140     void __user *argp = (void __user *)arg;
1141
1142         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1143     fbprintk("win0fb_ioctl cmd = %8x, arg = %8x \n", (u32)cmd, (u32)arg);
1144
1145         CHK_SUSPEND(inf);
1146
1147     switch(cmd)
1148     {
1149     case FB1_IOCTL_GET_PANEL_SIZE:    //get panel size
1150         {
1151             u32 panel_size[2];
1152              if(inf->win0fb->var.rotate == 270) {
1153                 panel_size[0] = inf->cur_screen->y_res;
1154                 panel_size[1] = inf->cur_screen->x_res;
1155             } else {
1156                 panel_size[0] = inf->cur_screen->x_res;
1157                 panel_size[1] = inf->cur_screen->y_res;
1158             }
1159
1160             if(copy_to_user(argp, panel_size, 8))  return -EFAULT;
1161         }
1162         break;
1163
1164     case FB1_IOCTL_SET_YUV_ADDR:    //set y&uv address to register direct
1165         {
1166             u32 yuv_phy[2];
1167             if (copy_from_user(yuv_phy, argp, 8))
1168                             return -EFAULT;
1169
1170             yuv_phy[0] += par->y_offset;
1171             yuv_phy[1] += par->uv_offset;
1172
1173             LcdWrReg(inf, WIN0_YRGB_MST, yuv_phy[0]);
1174             LcdWrReg(inf, WIN0_CBR_MST, yuv_phy[1]);
1175             LcdWrReg(inf, REG_CFG_DONE, 0x01);
1176             // enable win0 after the win0 par is seted
1177             par->addr_seted = 1;
1178             if(par->par_seted) {
1179                 LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(1));
1180                 mcu_refresh(inf);
1181             }
1182         }
1183         break;
1184
1185     case FB1_IOCTL_SET_ROTATE:    //change MCU panel scan direction
1186         fbprintk(">>>>>> change lcdc direction(%d) \n", (int)arg);
1187         return -1;
1188         break;
1189     default:
1190         break;
1191     }
1192     return 0;
1193 }
1194
1195 static struct fb_ops win0fb_ops = {
1196         .owner          = THIS_MODULE,
1197         .fb_open    = win0fb_open,
1198         .fb_release = win0fb_release,
1199         .fb_check_var   = win0fb_check_var,
1200         .fb_set_par     = win0fb_set_par,
1201         .fb_blank       = win0fb_blank,
1202     .fb_pan_display = win0fb_pan_display,
1203     .fb_ioctl = win0fb_ioctl,
1204         .fb_setcolreg   = fb_setcolreg,
1205         .fb_fillrect    = cfb_fillrect,
1206         .fb_copyarea    = cfb_copyarea,
1207         .fb_imageblit   = cfb_imageblit,
1208 };
1209
1210 static int win1fb_blank(int blank_mode, struct fb_info *info)
1211 {
1212     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1213
1214     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1215
1216         CHK_SUSPEND(inf);
1217
1218         switch(blank_mode)
1219     {
1220     case FB_BLANK_UNBLANK:
1221         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(1));
1222         break;
1223     default:
1224         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
1225         break;
1226     }
1227     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1228
1229         mcu_refresh(inf);
1230     return 0;
1231 }
1232
1233 static int win1fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1234 {
1235     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1236     struct rk29fb_screen *screen = inf->cur_screen;
1237     u16 xpos = (var->nonstd>>8) & 0xfff;
1238     u16 ypos = (var->nonstd>>20) & 0xfff;
1239     u16 xlcd = screen->x_res;
1240     u16 ylcd = screen->y_res;
1241     u8 trspmode = (var->grayscale>>8) & 0xff;
1242     u8 trspval = (var->grayscale) & 0xff;
1243
1244     //fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1245
1246         CHK_SUSPEND(inf);
1247
1248     if( 0==var->xres_virtual || 0==var->yres_virtual ||
1249         0==var->xres || 0==var->yres || var->xres<16 ||
1250         trspmode>5 || trspval>16 ||
1251         ((16!=var->bits_per_pixel)&&(32!=var->bits_per_pixel)) )
1252     {
1253         printk(">>>>>> win1fb_check_var fail 1!!! \n");
1254         printk(">>>>>> 0==%d || 0==%d ", var->xres_virtual,var->yres_virtual);
1255         printk("0==%d || 0==%d || %d<16 || ", var->xres,var->yres,var->xres<16);
1256         printk("%d>5 || %d>16 \n", trspmode,trspval);
1257         printk("bits_per_pixel=%d \n", var->bits_per_pixel);
1258         return -EINVAL;
1259     }
1260
1261     if( (var->xoffset+var->xres)>var->xres_virtual ||
1262         (var->yoffset+var->yres)>var->yres_virtual ||
1263         (xpos+var->xres)>xlcd || (ypos+var->yres)>ylcd )
1264     {
1265         printk(">>>>>> win1fb_check_var fail 2!!! \n");
1266         printk(">>>>>> (%d+%d)>%d || ", var->xoffset,var->xres,var->xres_virtual);
1267         printk("(%d+%d)>%d || ", var->yoffset,var->yres,var->yres_virtual);
1268         printk("(%d+%d)>%d || (%d+%d)>%d \n", xpos,var->xres,xlcd,ypos,var->yres,ylcd);
1269         return -EINVAL;
1270     }
1271
1272     switch(var->bits_per_pixel)
1273     {
1274     case 16:    // rgb565
1275         var->xres_virtual = (var->xres_virtual + 0x1) & (~0x1);
1276         var->xres = (var->xres + 0x1) & (~0x1);
1277         var->xoffset = (var->xoffset) & (~0x1);
1278         break;
1279     default:    // rgb888
1280         var->bits_per_pixel = 32;
1281         break;
1282     }
1283
1284     return 0;
1285 }
1286
1287 static int win1fb_set_par(struct fb_info *info)
1288 {
1289     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1290     struct fb_var_screeninfo *var = &info->var;
1291     struct fb_fix_screeninfo *fix = &info->fix;
1292     struct rk29fb_screen *screen = inf->cur_screen;
1293
1294     u8 format = 0;
1295     dma_addr_t map_dma;
1296     u32 offset=0, addr=0, map_size=0, smem_len=0;
1297
1298     u16 xres_virtual = var->xres_virtual;      //virtual screen size
1299     //u16 yres_virtual = var->yres_virtual;
1300
1301     u16 xpos_virtual = var->xoffset;           //visiable offset in virtual screen
1302     u16 ypos_virtual = var->yoffset;
1303
1304     u16 xpos = 0;                 //visiable offset in panel
1305     u16 ypos = 0;
1306     u16 xsize = screen->x_res;    //visiable size in panel
1307     u16 ysize = screen->y_res;
1308     u8 trspmode = TRSP_CLOSE;
1309     u8 trspval = 0;
1310
1311     //fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1312
1313         CHK_SUSPEND(inf);
1314
1315     switch(var->bits_per_pixel)
1316     {
1317     case 16:    // rgb565
1318         format = 1;
1319         fix->line_length = 2 * xres_virtual;
1320         offset = (ypos_virtual*xres_virtual + xpos_virtual)*2;
1321         break;
1322     case 32:    // rgb888
1323     default:
1324         format = 0;
1325         fix->line_length = 4 * xres_virtual;
1326         offset = (ypos_virtual*xres_virtual + xpos_virtual)*4;
1327         break;
1328     }
1329
1330     smem_len = fix->line_length * var->yres_virtual;   //cursor buf also alloc here
1331     map_size = PAGE_ALIGN(smem_len);
1332
1333     if (smem_len != fix->smem_len)     // buffer need realloc
1334     {
1335         fbprintk(">>>>>> win1 buffer size is change(%d->%d)! remap memory!\n",fix->smem_len, smem_len);
1336         fbprintk(">>>>>> smem_len %d = %d * %d \n", smem_len, fix->line_length, var->yres_virtual);
1337         fbprintk(">>>>>> map_size = %d\n", map_size);
1338         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
1339         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1340         msleep(50);
1341         if (info->screen_base) {
1342             printk(">>>>>> win1fb unmap memory(%d)! \n", info->fix.smem_len);
1343                 dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len), info->screen_base, info->fix.smem_start);
1344                 info->screen_base = 0;
1345                 fix->smem_start = 0;
1346                 fix->smem_len = 0;
1347         }
1348
1349         info->screen_base = dma_alloc_writecombine(NULL, map_size, &map_dma, GFP_KERNEL);
1350         if(!info->screen_base) {
1351             printk(">>>>>> win1fb dma_alloc_writecombine fail!\n");
1352             return -ENOMEM;
1353         }
1354         memset(info->screen_base, 0, map_size);
1355         fix->smem_start = map_dma;
1356         fix->smem_len = smem_len;
1357         fbprintk(">>>>>> alloc succ, mem=%08x, len=%d!\n", (u32)fix->smem_start, fix->smem_len);
1358     }
1359
1360     addr = fix->smem_start + offset;
1361
1362 #if ANDROID_USE_THREE_BUFS
1363     if(0==new_frame_seted) {
1364         wq_condition = 0;
1365         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1366     }
1367     new_frame_seted = 0;
1368 #endif
1369
1370     LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE|m_W1_FORMAT, v_W1_ENABLE(1)|v_W1_FORMAT(format));
1371
1372     xpos += (screen->left_margin + screen->hsync_len);
1373     ypos += (screen->upper_margin + screen->vsync_len);
1374
1375     LcdWrReg(inf, WIN1_YRGB_MST, addr);
1376
1377     LcdMskReg(inf, WIN1_DSP_ST, m_BIT11LO|m_BIT11HI, v_BIT11LO(xpos) | v_BIT11HI(ypos));
1378     LcdMskReg(inf, WIN1_DSP_INFO, m_BIT11LO|m_BIT11HI, v_BIT11LO(xsize) | v_BIT11HI(ysize));
1379
1380     LcdMskReg(inf, WIN1_VIR, m_WORDLO | m_WORDHI , v_WORDLO(xres_virtual) | v_WORDHI(var->yres_virtual));
1381
1382     LcdMskReg(inf, BLEND_CTRL, m_W1_BLEND_EN |  m_W1_BLEND_FACTOR,
1383         v_W1_BLEND_EN((TRSP_FMREG==trspmode) || (TRSP_MASK==trspmode)) | v_W1_BLEND_FACTOR(trspval));
1384
1385      // enable win1 color key and set the color to black(rgb=0)
1386     LcdMskReg(inf, WIN1_COLOR_KEY_CTRL, m_COLORKEY_EN | m_KEYCOLOR, v_COLORKEY_EN(1) | v_KEYCOLOR(0));
1387
1388     if(1==format) //rgb565
1389     {
1390         LcdMskReg(inf, SWAP_CTRL, m_W1_8_SWAP | m_W1_16_SWAP | m_W1_R_SHIFT_SWAP | m_W1_565_RB_SWAP,
1391             v_W1_8_SWAP(0) | v_W1_16_SWAP(0) | v_W1_R_SHIFT_SWAP(0) | v_W1_565_RB_SWAP(0) );
1392     }
1393     else
1394     {
1395      LcdMskReg(inf, SWAP_CTRL, m_W1_8_SWAP | m_W1_16_SWAP | m_W1_R_SHIFT_SWAP | m_W1_565_RB_SWAP,
1396             v_W1_8_SWAP(0) | v_W1_16_SWAP(0) | v_W1_R_SHIFT_SWAP(0) | v_W1_565_RB_SWAP(0) );
1397
1398      LcdMskReg(inf, DSP_CTRL0, m_W1_TRANSP_FROM, v_W1_TRANSP_FROM(TRSP_FMRAM==trspmode) );
1399     }
1400
1401         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1402
1403     return 0;
1404 }
1405
1406 static int win1fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1407 {
1408     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1409     struct fb_var_screeninfo *var1 = &info->var;
1410     struct fb_fix_screeninfo *fix1 = &info->fix;
1411     int i;
1412     u32 offset = 0, addr = 0;
1413
1414         //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1415
1416         CHK_SUSPEND(inf);
1417
1418     switch(var1->bits_per_pixel)
1419     {
1420     case 16:    // rgb565
1421         var->xoffset = (var->xoffset) & (~0x1);
1422         offset = (var->yoffset*var1->xres_virtual + var->xoffset)*2;
1423         break;
1424     case 32:    // rgb888
1425         offset = (var->yoffset*var1->xres_virtual + var->xoffset)*4;
1426         break;
1427     default:
1428         return -EINVAL;
1429     }
1430
1431     addr = fix1->smem_start + offset;
1432
1433     //fbprintk("info->screen_base = %8x ; fix1->smem_len = %d , addr = %8x\n",(u32)info->screen_base, fix1->smem_len, addr);
1434
1435     LcdWrReg(inf, WIN1_YRGB_MST, addr);
1436     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1437
1438         mcu_refresh(inf);
1439
1440 #if !ANDROID_USE_THREE_BUFS
1441     // flush end when wq_condition=1 in mcu panel, but not in rgb panel
1442     if(SCREEN_MCU == inf->cur_screen->type) {
1443         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1444         wq_condition = 0;
1445     } else {
1446         wq_condition = 0;
1447         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1448     }
1449 #endif
1450
1451 #if 0
1452     for(i=0;i<=(0xc0/4);i+=4)
1453     {
1454         fbprintk("0x%02X: 0x%08X 0x%08X 0x%08X 0x%08X \n", i*4,
1455             *((u32*)inf->reg_vir_base+i),
1456             *((u32*)inf->reg_vir_base+i+1),
1457             *((u32*)inf->reg_vir_base+i+2),
1458             *((u32*)inf->reg_vir_base+i+3));
1459     }
1460 #endif
1461
1462     return 0;
1463 }
1464
1465
1466 static int win1fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
1467 {
1468     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1469     struct rk29fb_info *mach_info = info->device->platform_data;
1470     unsigned display_on;
1471     int display_on_pol;
1472
1473         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1474
1475         CHK_SUSPEND(inf);
1476
1477     switch(cmd)
1478     {
1479     case FB0_IOCTL_STOP_TIMER_FLUSH:    //stop timer flush mcu panel after android is runing
1480         if(1==arg)
1481         {
1482             inf->mcu_usetimer = 0;
1483         }
1484         break;
1485
1486     case FB0_IOCTL_SET_PANEL:
1487         if(arg>7)   return -1;
1488
1489         /* Black out, because some display device need clock to standby */
1490         //LcdMskReg(inf, DSP_CTRL_REG1, m_BLACK_OUT, v_BLACK_OUT(1));
1491         LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(0));
1492         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
1493         LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(1));
1494         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1495         if(inf->cur_screen)
1496         {
1497             if(inf->cur_screen->standby)    inf->cur_screen->standby(1);
1498             // operate the display_on pin to power down the lcd
1499             if(SCREEN_RGB==inf->cur_screen->type || SCREEN_MCU==inf->cur_screen->type)
1500             {
1501                 if(mach_info && mach_info->disp_on_pin)
1502                 {
1503                     display_on = mach_info->disp_on_pin;
1504                     display_on_pol = mach_info->disp_on_value;
1505                     gpio_direction_output(display_on, 0);
1506                         gpio_set_value(display_on, !display_on_pol);
1507                 }
1508             }
1509         }
1510
1511         /* Load the new device's param */
1512         switch(arg)
1513         {
1514         case 0: inf->cur_screen = &inf->lcd_info;   break;  //lcd
1515         case 1: inf->cur_screen = &inf->tv_info[0]; break;  //tv ntsc cvbs
1516         case 2: inf->cur_screen = &inf->tv_info[1]; break;  //tv pal cvbs
1517         case 3: inf->cur_screen = &inf->tv_info[2]; break;  //tv 480 ypbpr
1518         case 4: inf->cur_screen = &inf->tv_info[3]; break;  //tv 576 ypbpr
1519         case 5: inf->cur_screen = &inf->tv_info[4]; break;  //tv 720 ypbpr
1520         case 6: inf->cur_screen = &inf->hdmi_info[0];  break;  //hdmi 576
1521         case 7: inf->cur_screen = &inf->hdmi_info[1];  break;  //hdmi 720
1522         default: break;
1523         }
1524         load_screen(info, 1);
1525                 mcu_refresh(inf);
1526         break;
1527     default:
1528         break;
1529     }
1530     return 0;
1531 }
1532
1533
1534 static struct fb_ops win1fb_ops = {
1535         .owner          = THIS_MODULE,
1536         .fb_check_var   = win1fb_check_var,
1537         .fb_set_par = win1fb_set_par,
1538         .fb_blank   = win1fb_blank,
1539         .fb_pan_display = win1fb_pan_display,
1540     .fb_ioctl = win1fb_ioctl,
1541         .fb_setcolreg   = fb_setcolreg,
1542         .fb_fillrect    = cfb_fillrect,
1543         .fb_copyarea    = cfb_copyarea,
1544         .fb_imageblit   = cfb_imageblit,
1545         //.fb_cursor      = rk29_set_cursor,
1546 };
1547
1548
1549 static irqreturn_t rk29fb_irq(int irq, void *dev_id)
1550 {
1551         struct platform_device *pdev = (struct platform_device*)dev_id;
1552     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
1553     if(!inf)
1554         return IRQ_HANDLED;
1555
1556         //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1557
1558     LcdMskReg(inf, INT_STATUS, m_FRM_STARTCLEAR, v_FRM_STARTCLEAR(1));
1559
1560         if(SCREEN_MCU == inf->cur_screen->type)
1561         {
1562         inf->mcu_isrcnt = !inf->mcu_isrcnt;
1563         if(inf->mcu_isrcnt)
1564             return IRQ_HANDLED;
1565
1566         if(IsMcuUseFmk())
1567         {
1568             if(LcdReadBit(inf, MCU_TIMING_CTRL, m_MCU_HOLD_STATUS) && (inf->mcu_fmksync == 0))
1569             {
1570                 inf->mcu_fmksync = 1;
1571                  if(inf->cur_screen->refresh)
1572                    inf->cur_screen->refresh(REFRESH_END);
1573                 inf->mcu_fmksync = 0;
1574             }
1575             else
1576             {
1577                 return IRQ_HANDLED;
1578             }
1579         }
1580         else
1581         {
1582             if(inf->mcu_needflush) {
1583                 if(inf->cur_screen->refresh)
1584                     inf->cur_screen->refresh(REFRESH_PRE);
1585                 inf->mcu_needflush = 0;
1586                 inf->mcu_isrcnt = 0;
1587                 LcdSetRegBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST);
1588             } else {
1589                 if(inf->cur_screen->refresh)
1590                     inf->cur_screen->refresh(REFRESH_END);
1591             }
1592         }
1593         }
1594
1595 #if ANDROID_USE_THREE_BUFS
1596     new_frame_seted = 1;
1597 #endif
1598
1599         wq_condition = 1;
1600         wake_up_interruptible(&wq);
1601
1602         return IRQ_HANDLED;
1603 }
1604
1605 #ifdef CONFIG_HAS_EARLYSUSPEND
1606
1607 struct suspend_info {
1608         struct early_suspend early_suspend;
1609         struct rk29fb_inf *inf;
1610 };
1611
1612 void suspend(struct early_suspend *h)
1613 {
1614         struct suspend_info *info = container_of(h, struct suspend_info,
1615                                                 early_suspend);
1616
1617     struct rk29fb_inf *inf = info->inf;
1618
1619     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1620
1621     if(!inf) {
1622         printk("inf==0, rk29fb_suspend fail! \n");
1623         return;
1624     }
1625
1626     set_lcd_pin(g_pdev, 0);
1627
1628         if(inf->cur_screen->standby)
1629         {
1630                 fbprintk(">>>>>> power down the screen! \n");
1631                 inf->cur_screen->standby(1);
1632         }
1633
1634     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(1));
1635     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(1));
1636         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1637
1638         if(!inf->in_suspend)
1639         {
1640                 fbprintk(">>>>>> diable the lcdc clk! \n");
1641                 msleep(100);
1642         if (inf->dclk){
1643             clk_disable(inf->dclk);
1644         }
1645         if(inf->clk){
1646             clk_disable(inf->clk);
1647         }
1648
1649                 inf->in_suspend = 1;
1650         }
1651
1652 }
1653
1654 void resume(struct early_suspend *h)
1655 {
1656         struct suspend_info *info = container_of(h, struct suspend_info,
1657                                         early_suspend);
1658
1659     struct rk29fb_inf *inf = info->inf;
1660
1661     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1662     if(!inf) {
1663         printk("inf==0, rk29fb_resume fail! \n");
1664         return ;
1665     }
1666
1667         if(inf->in_suspend)
1668         {
1669             inf->in_suspend = 0;
1670         fbprintk(">>>>>> enable the lcdc clk! \n");
1671         if (inf->dclk){
1672             clk_enable(inf->dclk);
1673         }
1674         if(inf->clk){
1675             clk_enable(inf->clk);
1676         }
1677         msleep(100);
1678         }
1679     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(0));
1680     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(0));
1681     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1682
1683         if(inf->cur_screen->standby)
1684         {
1685                 fbprintk(">>>>>> power on the screen! \n");
1686                 inf->cur_screen->standby(0);
1687         }
1688     msleep(100);
1689     set_lcd_pin(g_pdev, 1);
1690         memcpy(inf->preg, &inf->regbak, 0xa4);  //resume reg
1691 }
1692
1693 struct suspend_info suspend_info = {
1694         .early_suspend.suspend = suspend,
1695         .early_suspend.resume = resume,
1696         .early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB,
1697 };
1698 #endif
1699
1700 static int __init rk29fb_probe (struct platform_device *pdev)
1701 {
1702     struct rk29fb_inf *inf = NULL;
1703     struct resource *res = NULL;
1704     struct resource *mem = NULL;
1705     struct rk29fb_info *mach_info = NULL;
1706     struct rk29fb_screen *screen = NULL;
1707         int irq = 0;
1708     int ret = 0;
1709
1710     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1711
1712     /* Malloc rk29fb_inf and set it to pdev for drvdata */
1713     fbprintk(">> Malloc rk29fb_inf and set it to pdev for drvdata \n");
1714     inf = kmalloc(sizeof(struct rk29fb_inf), GFP_KERNEL);
1715     if(!inf)
1716     {
1717         dev_err(&pdev->dev, ">> inf kmalloc fail!");
1718         ret = -ENOMEM;
1719                 goto release_drvdata;
1720     }
1721     memset(inf, 0, sizeof(struct rk29fb_inf));
1722         platform_set_drvdata(pdev, inf);
1723
1724     mach_info = pdev->dev.platform_data;
1725     /* Fill screen info and set current screen */
1726     fbprintk(">> Fill screen info and set current screen \n");
1727     set_lcd_info(&inf->lcd_info, mach_info->lcd_info);
1728     set_tv_info(&inf->tv_info[0]);
1729     set_hdmi_info(&inf->hdmi_info[0]);
1730     inf->cur_screen = &inf->lcd_info;
1731     screen = inf->cur_screen;
1732     if(SCREEN_NULL==screen->type)
1733     {
1734         dev_err(&pdev->dev, ">> Please select a display device! \n");
1735         ret = -EINVAL;
1736                 goto release_drvdata;
1737     }
1738
1739     /* get virtual basic address of lcdc register */
1740     fbprintk(">> get virtual basic address of lcdc register \n");
1741     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1742     if (res == NULL)
1743     {
1744         dev_err(&pdev->dev, "failed to get memory registers\n");
1745         ret = -ENOENT;
1746                 goto release_drvdata;
1747     }
1748     inf->reg_phy_base = res->start;
1749     inf->len = (res->end - res->start) + 1;
1750     mem = request_mem_region(inf->reg_phy_base, inf->len, pdev->name);
1751     if (mem == NULL)
1752     {
1753         dev_err(&pdev->dev, "failed to get memory region\n");
1754         ret = -ENOENT;
1755                 goto release_drvdata;
1756     }
1757     fbprintk("inf->reg_phy_base = 0x%08x, inf->len = %d \n", inf->reg_phy_base, inf->len);
1758     inf->reg_vir_base = ioremap(inf->reg_phy_base, inf->len);
1759     if (inf->reg_vir_base == NULL)
1760     {
1761         dev_err(&pdev->dev, "ioremap() of registers failed\n");
1762         ret = -ENXIO;
1763                 goto release_drvdata;
1764     }
1765     inf->preg = (LCDC_REG*)inf->reg_vir_base;
1766
1767     /* Prepare win1 info */
1768     fbprintk(">> Prepare win1 info \n");
1769         inf->win1fb = framebuffer_alloc(sizeof(struct win1_par), &pdev->dev);
1770     if(!inf->win1fb)
1771     {
1772         dev_err(&pdev->dev, ">> win1fb framebuffer_alloc fail!");
1773                 inf->win1fb = NULL;
1774         ret = -ENOMEM;
1775                 goto release_win1fb;
1776     }
1777
1778     strcpy(inf->win1fb->fix.id, "win1fb");
1779     inf->win1fb->fix.type        = FB_TYPE_PACKED_PIXELS;
1780     inf->win1fb->fix.type_aux    = 0;
1781     inf->win1fb->fix.xpanstep    = 1;
1782     inf->win1fb->fix.ypanstep    = 1;
1783     inf->win1fb->fix.ywrapstep   = 0;
1784     inf->win1fb->fix.accel       = FB_ACCEL_NONE;
1785     inf->win1fb->fix.visual      = FB_VISUAL_TRUECOLOR;
1786     inf->win1fb->fix.smem_len    = 0;
1787     inf->win1fb->fix.line_length = 0;
1788     inf->win1fb->fix.smem_start  = 0;
1789
1790     inf->win1fb->var.xres = screen->x_res;
1791     inf->win1fb->var.yres = screen->y_res;
1792     inf->win1fb->var.bits_per_pixel = 16;
1793     inf->win1fb->var.xres_virtual = screen->x_res;
1794     inf->win1fb->var.yres_virtual = screen->y_res;
1795     inf->win1fb->var.width = screen->width;
1796     inf->win1fb->var.height = screen->height;
1797     inf->win1fb->var.pixclock = screen->pixclock;
1798     inf->win1fb->var.left_margin = screen->left_margin;
1799     inf->win1fb->var.right_margin = screen->right_margin;
1800     inf->win1fb->var.upper_margin = screen->upper_margin;
1801     inf->win1fb->var.lower_margin = screen->lower_margin;
1802     inf->win1fb->var.vsync_len = screen->vsync_len;
1803     inf->win1fb->var.hsync_len = screen->hsync_len;
1804     inf->win1fb->var.red    = def_rgb_16.red;
1805     inf->win1fb->var.green  = def_rgb_16.green;
1806     inf->win1fb->var.blue   = def_rgb_16.blue;
1807     inf->win1fb->var.transp = def_rgb_16.transp;
1808
1809     inf->win1fb->var.nonstd      = 0;  //win1 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
1810     inf->win1fb->var.grayscale   = 0;  //win1 transprent mode & value(mode<<8 + value)
1811     inf->win1fb->var.activate    = FB_ACTIVATE_NOW;
1812     inf->win1fb->var.accel_flags = 0;
1813     inf->win1fb->var.vmode       = FB_VMODE_NONINTERLACED;
1814
1815     inf->win1fb->fbops           = &win1fb_ops;
1816     inf->win1fb->flags           = FBINFO_FLAG_DEFAULT;
1817     inf->win1fb->pseudo_palette  = ((struct win1_par*)inf->win1fb->par)->pseudo_pal;
1818     inf->win1fb->screen_base     = 0;
1819
1820     memset(inf->win1fb->par, 0, sizeof(struct win1_par));
1821         ret = fb_alloc_cmap(&inf->win1fb->cmap, 256, 0);
1822         if (ret < 0)
1823                 goto release_cmap;
1824
1825     /* Prepare win0 info */
1826     fbprintk(">> Prepare win0 info \n");
1827     inf->win0fb = framebuffer_alloc(sizeof(struct win0_par), &pdev->dev);
1828     if(!inf->win0fb)
1829     {
1830         dev_err(&pdev->dev, ">> win0fb framebuffer_alloc fail!");
1831                 inf->win0fb = NULL;
1832                 ret = -ENOMEM;
1833                 goto release_win0fb;
1834     }
1835
1836     strcpy(inf->win0fb->fix.id, "win0fb");
1837         inf->win0fb->fix.type         = FB_TYPE_PACKED_PIXELS;
1838         inf->win0fb->fix.type_aux    = 0;
1839         inf->win0fb->fix.xpanstep    = 1;
1840         inf->win0fb->fix.ypanstep    = 1;
1841         inf->win0fb->fix.ywrapstep   = 0;
1842         inf->win0fb->fix.accel       = FB_ACCEL_NONE;
1843     inf->win0fb->fix.visual      = FB_VISUAL_TRUECOLOR;
1844     inf->win0fb->fix.smem_len    = 0;
1845     inf->win0fb->fix.line_length = 0;
1846     inf->win0fb->fix.smem_start  = 0;
1847
1848     inf->win0fb->var.xres = screen->x_res;
1849     inf->win0fb->var.yres = screen->y_res;
1850     inf->win0fb->var.bits_per_pixel = 16;
1851     inf->win0fb->var.xres_virtual = screen->x_res;
1852     inf->win0fb->var.yres_virtual = screen->y_res;
1853     inf->win0fb->var.width = screen->width;
1854     inf->win0fb->var.height = screen->height;
1855     inf->win0fb->var.pixclock = screen->pixclock;
1856     inf->win0fb->var.left_margin = screen->left_margin;
1857     inf->win0fb->var.right_margin = screen->right_margin;
1858     inf->win0fb->var.upper_margin = screen->upper_margin;
1859     inf->win0fb->var.lower_margin = screen->lower_margin;
1860     inf->win0fb->var.vsync_len = screen->vsync_len;
1861     inf->win0fb->var.hsync_len = screen->hsync_len;
1862     inf->win0fb->var.red    = def_rgb_16.red;
1863     inf->win0fb->var.green  = def_rgb_16.green;
1864     inf->win0fb->var.blue   = def_rgb_16.blue;
1865     inf->win0fb->var.transp = def_rgb_16.transp;
1866
1867     inf->win0fb->var.nonstd      = 0;  //win0 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
1868     inf->win0fb->var.grayscale   = ((inf->win0fb->var.yres<<20)&0xfff00000) + ((inf->win0fb->var.xres<<8)&0xfff00);//win0 xsize & ysize
1869     inf->win0fb->var.activate    = FB_ACTIVATE_NOW;
1870     inf->win0fb->var.accel_flags = 0;
1871     inf->win0fb->var.vmode       = FB_VMODE_NONINTERLACED;
1872
1873     inf->win0fb->fbops           = &win0fb_ops;
1874         inf->win0fb->flags                    = FBINFO_FLAG_DEFAULT;
1875         inf->win0fb->pseudo_palette  = ((struct win0_par*)inf->win0fb->par)->pseudo_pal;
1876         inf->win0fb->screen_base     = 0;
1877
1878     memset(inf->win0fb->par, 0, sizeof(struct win0_par));
1879
1880         /* Init all lcdc and lcd before register_framebuffer. */
1881         /* because after register_framebuffer, the win1fb_check_par and winfb_set_par execute immediately */
1882         fbprintk(">> Init all lcdc and lcd before register_framebuffer \n");
1883     init_lcdc(inf->win1fb);
1884
1885   #ifdef CONFIG_CPU_FREQ
1886    // inf->freq_transition.notifier_call = rk29fb_freq_transition;
1887    // cpufreq_register_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
1888   #endif
1889         fbprintk("got clock\n");
1890
1891         if(mach_info)
1892     {
1893         struct rk29_fb_setting_info fb_setting;
1894         if( OUT_P888==inf->lcd_info.face ||
1895             OUT_P888==inf->tv_info[0].face ||
1896             OUT_P888==inf->hdmi_info[0].face )     // set lcdc iomux
1897         {
1898             fb_setting.data_num = 24;
1899         }
1900         else if(OUT_P666 == inf->lcd_info.face )
1901         {
1902             fb_setting.data_num = 18;
1903         }
1904         else
1905         {
1906             fb_setting.data_num = 16;
1907         }
1908         fb_setting.den_en = 1;
1909         fb_setting.vsync_en = 1;
1910         fb_setting.disp_on_en = 1;
1911         fb_setting.standby_en = 1;
1912         if( inf->lcd_info.mcu_usefmk )
1913             fb_setting.mcu_fmk_en =1;
1914         mach_info->io_init(&fb_setting);
1915     }
1916
1917         set_lcd_pin(pdev, 1);
1918         mdelay(10);
1919         g_pdev = pdev;
1920         inf->mcu_usetimer = 1;
1921     inf->mcu_fmksync = 0;
1922         load_screen(inf->win1fb, 1);
1923
1924     /* Register framebuffer(win1fb & win0fb) */
1925     fbprintk(">> Register framebuffer(win1fb) \n");
1926     ret = register_framebuffer(inf->win1fb);
1927     if(ret<0)
1928     {
1929         printk(">> win1fb register_framebuffer fail!\n");
1930         ret = -EINVAL;
1931                 goto release_win0fb;
1932     }
1933     fbprintk(">> Register framebuffer(win0fb) \n");
1934
1935     ret = register_framebuffer(inf->win0fb);
1936     if(ret<0)
1937     {
1938         printk(">> win0fb register_framebuffer fail!\n");
1939         ret = -EINVAL;
1940                 goto unregister_win1fb;
1941     }
1942
1943 #ifdef CONFIG_HAS_EARLYSUSPEND
1944         suspend_info.inf = inf;
1945         register_early_suspend(&suspend_info.early_suspend);
1946 #endif
1947
1948     /* get and request irq */
1949     fbprintk(">> get and request irq \n");
1950     irq = platform_get_irq(pdev, 0);
1951     if (irq < 0) {
1952         dev_err(&pdev->dev, "no irq for device\n");
1953         ret = -ENOENT;
1954         goto unregister_win1fb;
1955     }
1956     ret = request_irq(irq, rk29fb_irq, IRQF_DISABLED, pdev->name, pdev);
1957     if (ret) {
1958         dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
1959         ret = -EBUSY;
1960         goto release_irq;
1961     }
1962
1963     if( inf->lcd_info.mcu_usefmk && (mach_info->mcu_fmk_pin != -1) )
1964     {
1965         ret = request_irq(gpio_to_irq(mach_info->mcu_fmk_pin), mcu_irqfmk, GPIOEdgelFalling, pdev->name, pdev);
1966         if (ret)
1967         {
1968             dev_err(&pdev->dev, "cannot get fmk irq %d - err %d\n", irq, ret);
1969             ret = -EBUSY;
1970             goto release_irq;
1971         }
1972     }
1973
1974     printk(" %s ok\n", __FUNCTION__);
1975     return ret;
1976
1977 release_irq:
1978         if(irq>=0)
1979         free_irq(irq, pdev);
1980 unregister_win1fb:
1981     unregister_framebuffer(inf->win1fb);
1982 release_win0fb:
1983         if(inf->win0fb)
1984                 framebuffer_release(inf->win0fb);
1985         inf->win0fb = NULL;
1986 release_cmap:
1987     if(&inf->win1fb->cmap)
1988         fb_dealloc_cmap(&inf->win1fb->cmap);
1989 release_win1fb:
1990         if(inf->win1fb)
1991                 framebuffer_release(inf->win1fb);
1992         inf->win1fb = NULL;
1993 release_drvdata:
1994         if(inf && inf->reg_vir_base)
1995         iounmap(inf->reg_vir_base);
1996         if(inf && mem)
1997         release_mem_region(inf->reg_phy_base, inf->len);
1998         if(inf)
1999         kfree(inf);
2000         platform_set_drvdata(pdev, NULL);
2001         return ret;
2002 }
2003
2004 static int rk29fb_remove(struct platform_device *pdev)
2005 {
2006     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
2007     struct fb_info *info = NULL;
2008         //pm_message_t msg;
2009     struct rk29fb_info *mach_info = NULL;
2010     int irq = 0;
2011
2012         fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2013
2014     if(!inf) {
2015         printk("inf==0, rk29_fb_remove fail! \n");
2016         return -EINVAL;
2017     }
2018
2019     irq = platform_get_irq(pdev, 0);
2020     if (irq >0)
2021     {
2022     free_irq(irq, pdev);
2023     }
2024
2025     mach_info = pdev->dev.platform_data;
2026     if(mach_info->mcu_fmk_pin)
2027     {
2028         free_irq(gpio_to_irq(mach_info->mcu_fmk_pin), pdev);
2029     }
2030
2031         set_lcd_pin(pdev, 0);
2032
2033     // blank the lcdc
2034     if(inf->win0fb)
2035         win0fb_blank(FB_BLANK_POWERDOWN, inf->win0fb);
2036     if(inf->win1fb)
2037         win1fb_blank(FB_BLANK_POWERDOWN, inf->win1fb);
2038
2039         // suspend the lcdc
2040         //rk29fb_suspend(pdev, msg);
2041     // unmap memory and release framebuffer
2042     if(inf->win0fb) {
2043         info = inf->win0fb;
2044         if (info->screen_base) {
2045                 //dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
2046                 info->screen_base = 0;
2047                 info->fix.smem_start = 0;
2048                 info->fix.smem_len = 0;
2049         }
2050         unregister_framebuffer(inf->win0fb);
2051         framebuffer_release(inf->win0fb);
2052         inf->win0fb = NULL;
2053     }
2054     if(inf->win1fb) {
2055         info = inf->win1fb;
2056         if (info->screen_base) {
2057                 dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
2058                 info->screen_base = 0;
2059                 info->fix.smem_start = 0;
2060                 info->fix.smem_len = 0;
2061         }
2062         unregister_framebuffer(inf->win1fb);
2063         framebuffer_release(inf->win1fb);
2064         inf->win1fb = NULL;
2065     }
2066
2067   #ifdef CONFIG_CPU_FREQ
2068     cpufreq_unregister_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
2069   #endif
2070
2071         if (inf->clk)
2072     {
2073                 clk_disable(inf->clk);
2074                 clk_put(inf->clk);
2075                 inf->clk = NULL;
2076         }
2077     if (inf->dclk)
2078     {
2079                 clk_disable(inf->dclk);
2080                 clk_put(inf->dclk);
2081                 inf->dclk = NULL;
2082         }
2083
2084     kfree(inf);
2085     platform_set_drvdata(pdev, NULL);
2086
2087     return 0;
2088 }
2089
2090 static void rk29fb_shutdown(struct platform_device *pdev)
2091 {
2092     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
2093     mdelay(300);
2094         //printk("----------------------------rk29fb_shutdown----------------------------\n");
2095         set_lcd_pin(pdev, 0);
2096     if (inf->dclk)
2097     {
2098         clk_disable(inf->dclk);
2099     }
2100     if (inf->clk)
2101     {
2102         clk_disable(inf->clk);
2103     }
2104 }
2105
2106 static struct platform_driver rk29fb_driver = {
2107         .probe          = rk29fb_probe,
2108         .remove         = rk29fb_remove,
2109         .driver         = {
2110                 .name   = "rk29-fb",
2111                 .owner  = THIS_MODULE,
2112         },
2113         .shutdown   = rk29fb_shutdown,
2114 };
2115
2116 static int __init rk29fb_init(void)
2117 {
2118     return platform_driver_register(&rk29fb_driver);
2119 }
2120
2121 static void __exit rk29fb_exit(void)
2122 {
2123     platform_driver_unregister(&rk29fb_driver);
2124 }
2125
2126 //subsys_initcall(rk29fb_init);
2127
2128 module_init(rk29fb_init);
2129 module_exit(rk29fb_exit);
2130
2131
2132 MODULE_AUTHOR("  zyw@rock-chips.com");
2133 MODULE_DESCRIPTION("Driver for rk29 fb device");
2134 MODULE_LICENSE("GPL");
2135
2136