update gc830 driver
[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: // yuv420m
863         var->xres_virtual = (var->xres_virtual + 0x7) & (~0x7);
864         var->yres_virtual = (var->yres_virtual + 0x1) & (~0x1);
865         var->xres = (var->xres + 0x7) & (~0x7);
866         var->yres = (var->yres + 0x1) & (~0x1);
867         var->xoffset = (var->xoffset) & (~0x7);
868         var->yoffset = (var->yoffset) & (~0x1);
869         var->nonstd &= ~0xc0;   //not support I2P in this format
870         break;
871     case 5: // yuv444
872         var->xres_virtual = (var->xres_virtual + 0x3) & (~0x3);
873         var->xres = (var->xres + 0x3) & (~0x3);
874         var->xoffset = (var->xoffset) & (~0x3);
875         var->nonstd &= ~0xc0;   //not support I2P in this format
876         break;
877     default:
878         printk(">>>>>> win0fb var->nonstd=%d is invalid! \n", var->nonstd);
879         return -EINVAL;
880     }
881
882     if(var->rotate == 270)
883     {
884         yres = var->xres;
885     }
886     else
887     {
888         yres = var->yres;
889     }
890     ScaleYRGBY = CalScaleW0(yres, ysize);
891
892     if((ScaleYRGBY>0x8000) || (ScaleYRGBY<0x200))
893     {
894         return -EINVAL;        // multiple of scale down or scale up can't exceed 8
895     }
896
897     return 0;
898 }
899
900 static int win0fb_set_par(struct fb_info *info)
901 {
902     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
903     struct rk29fb_screen *screen = inf->cur_screen;
904     struct fb_var_screeninfo *var = &info->var;
905     struct fb_fix_screeninfo *fix = &info->fix;
906     struct win0_par *par = info->par;
907
908     u8 format = 0;
909     u32 cblen=0, crlen=0, map_size=0, smem_len=0;
910
911         u32 xact = var->xres;                       /* visible resolution               */
912         u32 yact = var->yres;
913         u32 xvir = var->xres_virtual;           /* virtual resolution           */
914         u32 yvir = var->yres_virtual;
915         u32 xact_st = var->xoffset;                     /* offset from virtual to visible */
916         u32 yact_st = var->yoffset;                     /* resolution                   */
917
918     u16 xpos = (var->nonstd>>8) & 0xfff;      //visiable pos in panel
919     u16 ypos = (var->nonstd>>20) & 0xfff;
920     u16 xsize = (var->grayscale>>8) & 0xfff;  //visiable size in panel
921     u16 ysize = (var->grayscale>>20) & 0xfff;
922
923     u32 ScaleYrgbX=0x1000,ScaleYrgbY=0x1000;
924     u32 ScaleCbrX=0x1000, ScaleCbrY=0x1000;
925
926     u8 data_format = var->nonstd&0x0f;
927     u32 win0_en = var->reserved[2];
928     u32 y_addr = var->reserved[3];       //user alloc buf addr y
929     u32 uv_addr = var->reserved[4];
930
931     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
932
933         CHK_SUSPEND(inf);
934
935         /* calculate y_offset,uv_offset,line_length,cblen and crlen  */
936     switch (data_format)
937     {
938     case 0: // rgb
939         switch(var->bits_per_pixel)
940         {
941         case 16:    // rgb565
942             format = 1;
943             fix->line_length = 2 * xvir;
944             par->y_offset = (yact_st*xvir + xact_st)*2;
945             break;
946         case 32:    // rgb888
947             format = 0;
948             fix->line_length = 4 * xvir;
949             par->y_offset = (yact_st*xvir + xact_st)*4;
950             break;
951         default:
952             return -EINVAL;
953         }
954         break;
955     case 1: // yuv422
956         format = 2;
957         fix->line_length = xvir;
958         cblen = crlen = (xvir*yvir)/2;
959         par->y_offset = yact_st*xvir + xact_st;
960         par->uv_offset = yact_st*xvir + xact_st;
961         break;
962     case 2: // yuv4200
963         format = 3;
964         fix->line_length = xvir;
965         cblen = crlen = (xvir*yvir)/4;
966
967         par->y_offset = yact_st*xvir + xact_st;
968         par->uv_offset = (yact_st/2)*xvir + xact_st;
969
970         break;
971     case 3: // yuv4201
972         format = 4;
973         fix->line_length = xvir;
974         par->y_offset = (yact_st/2)*2*xvir + (xact_st)*2;
975         par->uv_offset = (yact_st/2)*xvir + xact_st;
976         cblen = crlen = (xvir*yvir)/4;
977         break;
978     case 4: // yuv420m
979         format = 5;
980         fix->line_length = xvir;
981         par->y_offset = (yact_st/2)*3*xvir + (xact_st)*3;
982         cblen = crlen = (xvir*yvir)/4;
983         break;
984     case 5: // yuv444
985         format = 6;
986         fix->line_length = xvir;
987         par->y_offset = yact_st*xvir + xact_st;
988         par->uv_offset = yact_st*2*xvir + xact_st*2;
989         cblen = crlen = (xvir*yvir);
990         break;
991     default:
992         return -EINVAL;
993     }
994
995     smem_len = fix->line_length * yvir + cblen + crlen;
996     map_size = PAGE_ALIGN(smem_len);
997
998     if (info->screen_base) {
999         printk(">>>>>> win0fb unmap memory(%d)! \n", info->fix.smem_len);
1000         dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
1001         info->screen_base = 0;
1002     }
1003     fix->smem_start = y_addr;
1004     fix->smem_len = smem_len;
1005     fix->mmio_start = uv_addr;
1006
1007     par->addr_seted = ((-1==(int)y_addr)&&(-1==(int)uv_addr)) ? 0 : 1;
1008     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);
1009
1010         // calculate the display phy address
1011     y_addr = fix->smem_start + par->y_offset;
1012     uv_addr = fix->mmio_start + par->uv_offset;
1013
1014     fbprintk("y_addr 0x%08x = 0x%08x + %d\n", y_addr, (u32)fix->smem_start, par->y_offset);
1015     fbprintk("uv_addr 0x%08x = 0x%08x + %d\n", uv_addr, (u32)fix->mmio_start , par->uv_offset);
1016
1017     ScaleYrgbX = CalScaleW0(xact, xsize);
1018     ScaleYrgbY = CalScaleW0(yact, ysize);
1019
1020     switch (data_format)
1021     {
1022        case 1:// yuv422
1023            ScaleCbrX= CalScaleW0((xact/2), xsize);
1024            ScaleCbrY =  CalScaleW0(yact, ysize);
1025            break;
1026        case 2: // yuv4200
1027        case 3: // yuv4201
1028        case 4: // yuv420m
1029            ScaleCbrX= CalScaleW0(xact/2, xsize);
1030            ScaleCbrY =  CalScaleW0(yact/2, ysize);
1031            break;
1032        case 5:// yuv444
1033            ScaleCbrX= CalScaleW0(xact, xsize);
1034            ScaleCbrY =  CalScaleW0(yact, ysize);
1035            break;
1036     }
1037
1038     xpos += (screen->left_margin + screen->hsync_len);
1039     ypos += (screen->upper_margin + screen->vsync_len);
1040
1041     LcdWrReg(inf, WIN0_YRGB_MST, y_addr);
1042     LcdWrReg(inf, WIN0_CBR_MST, uv_addr);
1043
1044     LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE | m_W0_FORMAT, v_W0_ENABLE(win0_en) | v_W0_FORMAT(format));
1045
1046     LcdMskReg(inf, WIN0_VIR, m_WORDLO | m_WORDHI, v_VIRWIDTH(xvir) | v_VIRHEIGHT((yvir)) );
1047     LcdMskReg(inf, WIN0_ACT_INFO, m_WORDLO | m_WORDHI, v_WORDLO(xact) | v_WORDHI(yact));
1048     LcdMskReg(inf, WIN0_DSP_ST, m_BIT11LO | m_BIT11HI, v_BIT11LO(xpos) | v_BIT11HI(ypos));
1049     LcdMskReg(inf, WIN0_DSP_INFO, m_BIT11LO | m_BIT11HI,  v_BIT11LO(xsize) | v_BIT11HI(ysize));
1050     LcdMskReg(inf, WIN0_SCL_FACTOR_YRGB, m_WORDLO | m_WORDHI, v_WORDLO(ScaleYrgbX) | v_WORDHI(ScaleYrgbY));
1051     LcdMskReg(inf, WIN0_SCL_FACTOR_CBR, m_WORDLO | m_WORDHI, v_WORDLO(ScaleCbrX) | v_WORDHI(ScaleCbrY));
1052
1053     switch(format)
1054     {
1055     case 1:  //rgb565
1056         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,
1057             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));
1058         break;
1059     case 4:   //yuv4201
1060         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,
1061             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) |
1062             v_W0_YRGB_M8_SWAP((var->rotate==0)) | v_W0_CBR_8_SWAP(0));
1063         break;
1064     default:
1065         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,
1066             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) );
1067     }
1068
1069     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1070
1071     return 0;
1072 }
1073
1074 static int win0fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1075 {
1076     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1077    // struct fb_var_screeninfo *var0 = &info->var;
1078     struct fb_fix_screeninfo *fix0 = &info->fix;
1079     struct win0_par *par = info->par;
1080     u32 y_addr=0, uv_addr=0;
1081
1082     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1083
1084         CHK_SUSPEND(inf);
1085
1086     y_addr = fix0->smem_start +  par->y_offset;//y_offset;
1087     uv_addr = fix0->mmio_start + par->uv_offset ;//uv_offset;
1088
1089     LcdWrReg(inf, WIN0_YRGB_MST, y_addr);
1090     LcdWrReg(inf, WIN0_CBR_MST, uv_addr);
1091     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1092
1093      // enable win0 after the win0 addr is seted
1094     par->par_seted = 1;
1095         LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE((1==par->addr_seted)?(1):(0)));
1096         mcu_refresh(inf);
1097
1098     return 0;
1099 }
1100
1101 int win0fb_open(struct fb_info *info, int user)
1102 {
1103     struct win0_par *par = info->par;
1104
1105     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1106
1107     par->par_seted = 0;
1108     par->addr_seted = 0;
1109
1110     if(par->refcount) {
1111         printk(">>>>>> win0fb has opened! \n");
1112         return -EACCES;
1113     } else {
1114         par->refcount++;
1115         return 0;
1116     }
1117 }
1118
1119 int win0fb_release(struct fb_info *info, int user)
1120 {
1121     struct win0_par *par = info->par;
1122         struct fb_var_screeninfo *var0 = &info->var;
1123
1124     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1125
1126     if(par->refcount) {
1127         par->refcount--;
1128
1129         win0fb_blank(FB_BLANK_POWERDOWN, info);
1130         // wait for lcdc stop access memory
1131         msleep(50);
1132
1133         // unmap memory
1134         if (info->screen_base) {
1135             printk(">>>>>> win0fb unmap memory(%d)! \n", info->fix.smem_len);
1136             dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
1137             info->screen_base = 0;
1138             info->fix.smem_start = 0;
1139             info->fix.smem_len = 0;
1140         }
1141
1142                 // clean the var param
1143                 memset(var0, 0, sizeof(struct fb_var_screeninfo));
1144     }
1145
1146     return 0;
1147 }
1148
1149 static int win0fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
1150 {
1151     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1152     struct win0_par *par = info->par;
1153     void __user *argp = (void __user *)arg;
1154
1155         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1156     fbprintk("win0fb_ioctl cmd = %8x, arg = %8x \n", (u32)cmd, (u32)arg);
1157
1158         CHK_SUSPEND(inf);
1159
1160     switch(cmd)
1161     {
1162     case FB1_IOCTL_GET_PANEL_SIZE:    //get panel size
1163         {
1164             u32 panel_size[2];
1165              if(inf->win0fb->var.rotate == 270) {
1166                 panel_size[0] = inf->cur_screen->y_res;
1167                 panel_size[1] = inf->cur_screen->x_res;
1168             } else {
1169                 panel_size[0] = inf->cur_screen->x_res;
1170                 panel_size[1] = inf->cur_screen->y_res;
1171             }
1172
1173             if(copy_to_user(argp, panel_size, 8))  return -EFAULT;
1174         }
1175         break;
1176
1177     case FB1_IOCTL_SET_YUV_ADDR:    //set y&uv address to register direct
1178         {
1179             u32 yuv_phy[2];
1180             if (copy_from_user(yuv_phy, argp, 8))
1181                             return -EFAULT;
1182
1183             yuv_phy[0] += par->y_offset;
1184             yuv_phy[1] += par->uv_offset;
1185
1186             LcdWrReg(inf, WIN0_YRGB_MST, yuv_phy[0]);
1187             LcdWrReg(inf, WIN0_CBR_MST, yuv_phy[1]);
1188             LcdWrReg(inf, REG_CFG_DONE, 0x01);
1189             // enable win0 after the win0 par is seted
1190             par->addr_seted = 1;
1191             if(par->par_seted) {
1192                 LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(1));
1193                 mcu_refresh(inf);
1194             }
1195         }
1196         break;
1197
1198     case FB1_IOCTL_SET_ROTATE:    //change MCU panel scan direction
1199         fbprintk(">>>>>> change lcdc direction(%d) \n", (int)arg);
1200         return -1;
1201         break;
1202     default:
1203         break;
1204     }
1205     return 0;
1206 }
1207
1208 static struct fb_ops win0fb_ops = {
1209         .owner          = THIS_MODULE,
1210         .fb_open    = win0fb_open,
1211         .fb_release = win0fb_release,
1212         .fb_check_var   = win0fb_check_var,
1213         .fb_set_par     = win0fb_set_par,
1214         .fb_blank       = win0fb_blank,
1215     .fb_pan_display = win0fb_pan_display,
1216     .fb_ioctl = win0fb_ioctl,
1217         .fb_setcolreg   = fb_setcolreg,
1218         .fb_fillrect    = cfb_fillrect,
1219         .fb_copyarea    = cfb_copyarea,
1220         .fb_imageblit   = cfb_imageblit,
1221 };
1222
1223 static int win1fb_blank(int blank_mode, struct fb_info *info)
1224 {
1225     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1226
1227     fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1228
1229         CHK_SUSPEND(inf);
1230
1231         switch(blank_mode)
1232     {
1233     case FB_BLANK_UNBLANK:
1234         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(1));
1235         break;
1236     default:
1237         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
1238         break;
1239     }
1240     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1241
1242         mcu_refresh(inf);
1243     return 0;
1244 }
1245
1246 static int win1fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1247 {
1248     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1249     struct rk29fb_screen *screen = inf->cur_screen;
1250     u16 xpos = (var->nonstd>>8) & 0xfff;
1251     u16 ypos = (var->nonstd>>20) & 0xfff;
1252     u16 xlcd = screen->x_res;
1253     u16 ylcd = screen->y_res;
1254     u8 trspmode = (var->grayscale>>8) & 0xff;
1255     u8 trspval = (var->grayscale) & 0xff;
1256
1257     //fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1258
1259         CHK_SUSPEND(inf);
1260
1261     if( 0==var->xres_virtual || 0==var->yres_virtual ||
1262         0==var->xres || 0==var->yres || var->xres<16 ||
1263         trspmode>5 || trspval>16 ||
1264         ((16!=var->bits_per_pixel)&&(32!=var->bits_per_pixel)) )
1265     {
1266         printk(">>>>>> win1fb_check_var fail 1!!! \n");
1267         printk(">>>>>> 0==%d || 0==%d ", var->xres_virtual,var->yres_virtual);
1268         printk("0==%d || 0==%d || %d<16 || ", var->xres,var->yres,var->xres<16);
1269         printk("%d>5 || %d>16 \n", trspmode,trspval);
1270         printk("bits_per_pixel=%d \n", var->bits_per_pixel);
1271         return -EINVAL;
1272     }
1273
1274     if( (var->xoffset+var->xres)>var->xres_virtual ||
1275         (var->yoffset+var->yres)>var->yres_virtual ||
1276         (xpos+var->xres)>xlcd || (ypos+var->yres)>ylcd )
1277     {
1278         printk(">>>>>> win1fb_check_var fail 2!!! \n");
1279         printk(">>>>>> (%d+%d)>%d || ", var->xoffset,var->xres,var->xres_virtual);
1280         printk("(%d+%d)>%d || ", var->yoffset,var->yres,var->yres_virtual);
1281         printk("(%d+%d)>%d || (%d+%d)>%d \n", xpos,var->xres,xlcd,ypos,var->yres,ylcd);
1282         return -EINVAL;
1283     }
1284
1285     switch(var->bits_per_pixel)
1286     {
1287     case 16:    // rgb565
1288         var->xres_virtual = (var->xres_virtual + 0x1) & (~0x1);
1289         var->xres = (var->xres + 0x1) & (~0x1);
1290         var->xoffset = (var->xoffset) & (~0x1);
1291         break;
1292     default:    // rgb888
1293         var->bits_per_pixel = 32;
1294         break;
1295     }
1296
1297     return 0;
1298 }
1299
1300 static int win1fb_set_par(struct fb_info *info)
1301 {
1302     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1303     struct fb_var_screeninfo *var = &info->var;
1304     struct fb_fix_screeninfo *fix = &info->fix;
1305     struct rk29fb_screen *screen = inf->cur_screen;
1306
1307     u8 format = 0;
1308     dma_addr_t map_dma;
1309     u32 offset=0, addr=0, map_size=0, smem_len=0;
1310
1311     u16 xres_virtual = var->xres_virtual;      //virtual screen size
1312     //u16 yres_virtual = var->yres_virtual;
1313
1314     u16 xpos_virtual = var->xoffset;           //visiable offset in virtual screen
1315     u16 ypos_virtual = var->yoffset;
1316
1317     u16 xpos = 0;                 //visiable offset in panel
1318     u16 ypos = 0;
1319     u16 xsize = screen->x_res;    //visiable size in panel
1320     u16 ysize = screen->y_res;
1321     u8 trspmode = TRSP_CLOSE;
1322     u8 trspval = 0;
1323
1324     //fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1325
1326         CHK_SUSPEND(inf);
1327
1328     switch(var->bits_per_pixel)
1329     {
1330     case 16:    // rgb565
1331         format = 1;
1332         fix->line_length = 2 * xres_virtual;
1333         offset = (ypos_virtual*xres_virtual + xpos_virtual)*2;
1334         break;
1335     case 32:    // rgb888
1336     default:
1337         format = 0;
1338         fix->line_length = 4 * xres_virtual;
1339         offset = (ypos_virtual*xres_virtual + xpos_virtual)*4;
1340         break;
1341     }
1342
1343     smem_len = fix->line_length * var->yres_virtual;   //cursor buf also alloc here
1344     map_size = PAGE_ALIGN(smem_len);
1345
1346     if (smem_len != fix->smem_len)     // buffer need realloc
1347     {
1348         fbprintk(">>>>>> win1 buffer size is change(%d->%d)! remap memory!\n",fix->smem_len, smem_len);
1349         fbprintk(">>>>>> smem_len %d = %d * %d \n", smem_len, fix->line_length, var->yres_virtual);
1350         fbprintk(">>>>>> map_size = %d\n", map_size);
1351         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
1352         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1353         msleep(50);
1354         if (info->screen_base) {
1355             printk(">>>>>> win1fb unmap memory(%d)! \n", info->fix.smem_len);
1356                 dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len), info->screen_base, info->fix.smem_start);
1357                 info->screen_base = 0;
1358                 fix->smem_start = 0;
1359                 fix->smem_len = 0;
1360         }
1361
1362         info->screen_base = dma_alloc_writecombine(NULL, map_size, &map_dma, GFP_KERNEL);
1363         if(!info->screen_base) {
1364             printk(">>>>>> win1fb dma_alloc_writecombine fail!\n");
1365             return -ENOMEM;
1366         }
1367         memset(info->screen_base, 0, map_size);
1368         fix->smem_start = map_dma;
1369         fix->smem_len = smem_len;
1370         fbprintk(">>>>>> alloc succ, mem=%08x, len=%d!\n", (u32)fix->smem_start, fix->smem_len);
1371     }
1372
1373     addr = fix->smem_start + offset;
1374
1375 #if ANDROID_USE_THREE_BUFS
1376     if(0==new_frame_seted) {
1377         wq_condition = 0;
1378         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1379     }
1380     new_frame_seted = 0;
1381 #endif
1382
1383     LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE|m_W1_FORMAT, v_W1_ENABLE(1)|v_W1_FORMAT(format));
1384
1385     xpos += (screen->left_margin + screen->hsync_len);
1386     ypos += (screen->upper_margin + screen->vsync_len);
1387
1388     LcdWrReg(inf, WIN1_YRGB_MST, addr);
1389
1390     LcdMskReg(inf, WIN1_DSP_ST, m_BIT11LO|m_BIT11HI, v_BIT11LO(xpos) | v_BIT11HI(ypos));
1391     LcdMskReg(inf, WIN1_DSP_INFO, m_BIT11LO|m_BIT11HI, v_BIT11LO(xsize) | v_BIT11HI(ysize));
1392
1393     LcdMskReg(inf, WIN1_VIR, m_WORDLO | m_WORDHI , v_WORDLO(xres_virtual) | v_WORDHI(var->yres_virtual));
1394
1395     LcdMskReg(inf, BLEND_CTRL, m_W1_BLEND_EN |  m_W1_BLEND_FACTOR,
1396         v_W1_BLEND_EN((TRSP_FMREG==trspmode) || (TRSP_MASK==trspmode)) | v_W1_BLEND_FACTOR(trspval));
1397
1398      // enable win1 color key and set the color to black(rgb=0)
1399     LcdMskReg(inf, WIN1_COLOR_KEY_CTRL, m_COLORKEY_EN | m_KEYCOLOR, v_COLORKEY_EN(1) | v_KEYCOLOR(0));
1400
1401     if(1==format) //rgb565
1402     {
1403         LcdMskReg(inf, SWAP_CTRL, m_W1_8_SWAP | m_W1_16_SWAP | m_W1_R_SHIFT_SWAP | m_W1_565_RB_SWAP,
1404             v_W1_8_SWAP(0) | v_W1_16_SWAP(0) | v_W1_R_SHIFT_SWAP(0) | v_W1_565_RB_SWAP(0) );
1405     }
1406     else
1407     {
1408      LcdMskReg(inf, SWAP_CTRL, m_W1_8_SWAP | m_W1_16_SWAP | m_W1_R_SHIFT_SWAP | m_W1_565_RB_SWAP,
1409             v_W1_8_SWAP(0) | v_W1_16_SWAP(0) | v_W1_R_SHIFT_SWAP(0) | v_W1_565_RB_SWAP(0) );
1410
1411      LcdMskReg(inf, DSP_CTRL0, m_W1_TRANSP_FROM, v_W1_TRANSP_FROM(TRSP_FMRAM==trspmode) );
1412     }
1413
1414         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1415
1416     return 0;
1417 }
1418
1419 static int win1fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1420 {
1421     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1422     struct fb_var_screeninfo *var1 = &info->var;
1423     struct fb_fix_screeninfo *fix1 = &info->fix;
1424     int i;
1425     u32 offset = 0, addr = 0;
1426
1427         //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1428
1429         CHK_SUSPEND(inf);
1430
1431     switch(var1->bits_per_pixel)
1432     {
1433     case 16:    // rgb565
1434         var->xoffset = (var->xoffset) & (~0x1);
1435         offset = (var->yoffset*var1->xres_virtual + var->xoffset)*2;
1436         break;
1437     case 32:    // rgb888
1438         offset = (var->yoffset*var1->xres_virtual + var->xoffset)*4;
1439         break;
1440     default:
1441         return -EINVAL;
1442     }
1443
1444     addr = fix1->smem_start + offset;
1445
1446     //fbprintk("info->screen_base = %8x ; fix1->smem_len = %d , addr = %8x\n",(u32)info->screen_base, fix1->smem_len, addr);
1447
1448     LcdWrReg(inf, WIN1_YRGB_MST, addr);
1449     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1450
1451         mcu_refresh(inf);
1452
1453 #if !ANDROID_USE_THREE_BUFS
1454     // flush end when wq_condition=1 in mcu panel, but not in rgb panel
1455     if(SCREEN_MCU == inf->cur_screen->type) {
1456         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1457         wq_condition = 0;
1458     } else {
1459         wq_condition = 0;
1460         wait_event_interruptible_timeout(wq, wq_condition, HZ/20);
1461     }
1462 #endif
1463
1464 #if 0
1465     for(i=0;i<=(0xc0/4);i+=4)
1466     {
1467         fbprintk("0x%02X: 0x%08X 0x%08X 0x%08X 0x%08X \n", i*4,
1468             *((u32*)inf->reg_vir_base+i),
1469             *((u32*)inf->reg_vir_base+i+1),
1470             *((u32*)inf->reg_vir_base+i+2),
1471             *((u32*)inf->reg_vir_base+i+3));
1472     }
1473 #endif
1474
1475     return 0;
1476 }
1477
1478
1479 static int win1fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
1480 {
1481     struct rk29fb_inf *inf = dev_get_drvdata(info->device);
1482     struct rk29fb_info *mach_info = info->device->platform_data;
1483     unsigned display_on;
1484     int display_on_pol;
1485
1486         fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1487
1488         CHK_SUSPEND(inf);
1489
1490     switch(cmd)
1491     {
1492     case FB0_IOCTL_STOP_TIMER_FLUSH:    //stop timer flush mcu panel after android is runing
1493         if(1==arg)
1494         {
1495             inf->mcu_usetimer = 0;
1496         }
1497         break;
1498
1499     case FB0_IOCTL_SET_PANEL:
1500         if(arg>7)   return -1;
1501
1502         /* Black out, because some display device need clock to standby */
1503         //LcdMskReg(inf, DSP_CTRL_REG1, m_BLACK_OUT, v_BLACK_OUT(1));
1504         LcdMskReg(inf, SYS_CONFIG, m_W0_ENABLE, v_W0_ENABLE(0));
1505         LcdMskReg(inf, SYS_CONFIG, m_W1_ENABLE, v_W1_ENABLE(0));
1506         LcdMskReg(inf, DSP_CTRL1, m_BLACK_MODE,  v_BLACK_MODE(1));
1507         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1508         if(inf->cur_screen)
1509         {
1510             if(inf->cur_screen->standby)    inf->cur_screen->standby(1);
1511             // operate the display_on pin to power down the lcd
1512             if(SCREEN_RGB==inf->cur_screen->type || SCREEN_MCU==inf->cur_screen->type)
1513             {
1514                 if(mach_info && mach_info->disp_on_pin)
1515                 {
1516                     display_on = mach_info->disp_on_pin;
1517                     display_on_pol = mach_info->disp_on_value;
1518                     gpio_direction_output(display_on, 0);
1519                         gpio_set_value(display_on, !display_on_pol);
1520                 }
1521             }
1522         }
1523
1524         /* Load the new device's param */
1525         switch(arg)
1526         {
1527         case 0: inf->cur_screen = &inf->lcd_info;   break;  //lcd
1528         case 1: inf->cur_screen = &inf->tv_info[0]; break;  //tv ntsc cvbs
1529         case 2: inf->cur_screen = &inf->tv_info[1]; break;  //tv pal cvbs
1530         case 3: inf->cur_screen = &inf->tv_info[2]; break;  //tv 480 ypbpr
1531         case 4: inf->cur_screen = &inf->tv_info[3]; break;  //tv 576 ypbpr
1532         case 5: inf->cur_screen = &inf->tv_info[4]; break;  //tv 720 ypbpr
1533         case 6: inf->cur_screen = &inf->hdmi_info[0];  break;  //hdmi 576
1534         case 7: inf->cur_screen = &inf->hdmi_info[1];  break;  //hdmi 720
1535         default: break;
1536         }
1537         load_screen(info, 1);
1538                 mcu_refresh(inf);
1539         break;
1540     default:
1541         break;
1542     }
1543     return 0;
1544 }
1545
1546
1547 static struct fb_ops win1fb_ops = {
1548         .owner          = THIS_MODULE,
1549         .fb_check_var   = win1fb_check_var,
1550         .fb_set_par = win1fb_set_par,
1551         .fb_blank   = win1fb_blank,
1552         .fb_pan_display = win1fb_pan_display,
1553     .fb_ioctl = win1fb_ioctl,
1554         .fb_setcolreg   = fb_setcolreg,
1555         .fb_fillrect    = cfb_fillrect,
1556         .fb_copyarea    = cfb_copyarea,
1557         .fb_imageblit   = cfb_imageblit,
1558         //.fb_cursor      = rk29_set_cursor,
1559 };
1560
1561
1562 static irqreturn_t rk29fb_irq(int irq, void *dev_id)
1563 {
1564         struct platform_device *pdev = (struct platform_device*)dev_id;
1565     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
1566     if(!inf)
1567         return IRQ_HANDLED;
1568
1569         //fbprintk(">>>>>> %s : %s \n", __FILE__, __FUNCTION__);
1570
1571     LcdMskReg(inf, INT_STATUS, m_FRM_STARTCLEAR, v_FRM_STARTCLEAR(1));
1572
1573         if(SCREEN_MCU == inf->cur_screen->type)
1574         {
1575         inf->mcu_isrcnt = !inf->mcu_isrcnt;
1576         if(inf->mcu_isrcnt)
1577             return IRQ_HANDLED;
1578
1579         if(IsMcuUseFmk())
1580         {
1581             if(LcdReadBit(inf, MCU_TIMING_CTRL, m_MCU_HOLD_STATUS) && (inf->mcu_fmksync == 0))
1582             {
1583                 inf->mcu_fmksync = 1;
1584                  if(inf->cur_screen->refresh)
1585                    inf->cur_screen->refresh(REFRESH_END);
1586                 inf->mcu_fmksync = 0;
1587             }
1588             else
1589             {
1590                 return IRQ_HANDLED;
1591             }
1592         }
1593         else
1594         {
1595             if(inf->mcu_needflush) {
1596                 if(inf->cur_screen->refresh)
1597                     inf->cur_screen->refresh(REFRESH_PRE);
1598                 inf->mcu_needflush = 0;
1599                 inf->mcu_isrcnt = 0;
1600                 LcdSetRegBit(inf, MCU_TIMING_CTRL, m_MCU_HOLDMODE_FRAME_ST);
1601             } else {
1602                 if(inf->cur_screen->refresh)
1603                     inf->cur_screen->refresh(REFRESH_END);
1604             }
1605         }
1606         }
1607
1608 #if ANDROID_USE_THREE_BUFS
1609     new_frame_seted = 1;
1610 #endif
1611
1612         wq_condition = 1;
1613         wake_up_interruptible(&wq);
1614
1615         return IRQ_HANDLED;
1616 }
1617
1618 #ifdef CONFIG_HAS_EARLYSUSPEND
1619
1620 struct suspend_info {
1621         struct early_suspend early_suspend;
1622         struct rk29fb_inf *inf;
1623 };
1624
1625 void suspend(struct early_suspend *h)
1626 {
1627         struct suspend_info *info = container_of(h, struct suspend_info,
1628                                                 early_suspend);
1629
1630     struct rk29fb_inf *inf = info->inf;
1631
1632     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1633
1634     if(!inf) {
1635         printk("inf==0, rk29fb_suspend fail! \n");
1636         return;
1637     }
1638
1639     set_lcd_pin(g_pdev, 0);
1640
1641         if(inf->cur_screen->standby)
1642         {
1643                 fbprintk(">>>>>> power down the screen! \n");
1644                 inf->cur_screen->standby(1);
1645         }
1646
1647     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(1));
1648     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(1));
1649         LcdWrReg(inf, REG_CFG_DONE, 0x01);
1650
1651         if(!inf->in_suspend)
1652         {
1653                 fbprintk(">>>>>> diable the lcdc clk! \n");
1654                 msleep(100);
1655         if (inf->dclk){
1656             clk_disable(inf->dclk);
1657         }
1658         if(inf->clk){
1659             clk_disable(inf->clk);
1660         }
1661
1662                 inf->in_suspend = 1;
1663         }
1664
1665 }
1666
1667 void resume(struct early_suspend *h)
1668 {
1669         struct suspend_info *info = container_of(h, struct suspend_info,
1670                                         early_suspend);
1671
1672     struct rk29fb_inf *inf = info->inf;
1673
1674     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1675     if(!inf) {
1676         printk("inf==0, rk29fb_resume fail! \n");
1677         return ;
1678     }
1679
1680         if(inf->in_suspend)
1681         {
1682             inf->in_suspend = 0;
1683         fbprintk(">>>>>> enable the lcdc clk! \n");
1684         if (inf->dclk){
1685             clk_enable(inf->dclk);
1686         }
1687         if(inf->clk){
1688             clk_enable(inf->clk);
1689         }
1690         msleep(100);
1691         }
1692     LcdMskReg(inf, DSP_CTRL1, m_BLANK_MODE , v_BLANK_MODE(0));
1693     LcdMskReg(inf, SYS_CONFIG, m_STANDBY, v_STANDBY(0));
1694     LcdWrReg(inf, REG_CFG_DONE, 0x01);
1695
1696         if(inf->cur_screen->standby)
1697         {
1698                 fbprintk(">>>>>> power on the screen! \n");
1699                 inf->cur_screen->standby(0);
1700         }
1701     msleep(100);
1702     set_lcd_pin(g_pdev, 1);
1703         memcpy(inf->preg, &inf->regbak, 0xa4);  //resume reg
1704 }
1705
1706 struct suspend_info suspend_info = {
1707         .early_suspend.suspend = suspend,
1708         .early_suspend.resume = resume,
1709         .early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB,
1710 };
1711 #endif
1712
1713 static int __init rk29fb_probe (struct platform_device *pdev)
1714 {
1715     struct rk29fb_inf *inf = NULL;
1716     struct resource *res = NULL;
1717     struct resource *mem = NULL;
1718     struct rk29fb_info *mach_info = NULL;
1719     struct rk29fb_screen *screen = NULL;
1720         int irq = 0;
1721     int ret = 0;
1722
1723     fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
1724
1725     /* Malloc rk29fb_inf and set it to pdev for drvdata */
1726     fbprintk(">> Malloc rk29fb_inf and set it to pdev for drvdata \n");
1727     inf = kmalloc(sizeof(struct rk29fb_inf), GFP_KERNEL);
1728     if(!inf)
1729     {
1730         dev_err(&pdev->dev, ">> inf kmalloc fail!");
1731         ret = -ENOMEM;
1732                 goto release_drvdata;
1733     }
1734     memset(inf, 0, sizeof(struct rk29fb_inf));
1735         platform_set_drvdata(pdev, inf);
1736
1737     mach_info = pdev->dev.platform_data;
1738     /* Fill screen info and set current screen */
1739     fbprintk(">> Fill screen info and set current screen \n");
1740     set_lcd_info(&inf->lcd_info, mach_info->lcd_info);
1741     set_tv_info(&inf->tv_info[0]);
1742     set_hdmi_info(&inf->hdmi_info[0]);
1743     inf->cur_screen = &inf->lcd_info;
1744     screen = inf->cur_screen;
1745     if(SCREEN_NULL==screen->type)
1746     {
1747         dev_err(&pdev->dev, ">> Please select a display device! \n");
1748         ret = -EINVAL;
1749                 goto release_drvdata;
1750     }
1751
1752     /* get virtual basic address of lcdc register */
1753     fbprintk(">> get virtual basic address of lcdc register \n");
1754     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1755     if (res == NULL)
1756     {
1757         dev_err(&pdev->dev, "failed to get memory registers\n");
1758         ret = -ENOENT;
1759                 goto release_drvdata;
1760     }
1761     inf->reg_phy_base = res->start;
1762     inf->len = (res->end - res->start) + 1;
1763     mem = request_mem_region(inf->reg_phy_base, inf->len, pdev->name);
1764     if (mem == NULL)
1765     {
1766         dev_err(&pdev->dev, "failed to get memory region\n");
1767         ret = -ENOENT;
1768                 goto release_drvdata;
1769     }
1770     fbprintk("inf->reg_phy_base = 0x%08x, inf->len = %d \n", inf->reg_phy_base, inf->len);
1771     inf->reg_vir_base = ioremap(inf->reg_phy_base, inf->len);
1772     if (inf->reg_vir_base == NULL)
1773     {
1774         dev_err(&pdev->dev, "ioremap() of registers failed\n");
1775         ret = -ENXIO;
1776                 goto release_drvdata;
1777     }
1778     inf->preg = (LCDC_REG*)inf->reg_vir_base;
1779
1780     /* Prepare win1 info */
1781     fbprintk(">> Prepare win1 info \n");
1782         inf->win1fb = framebuffer_alloc(sizeof(struct win1_par), &pdev->dev);
1783     if(!inf->win1fb)
1784     {
1785         dev_err(&pdev->dev, ">> win1fb framebuffer_alloc fail!");
1786                 inf->win1fb = NULL;
1787         ret = -ENOMEM;
1788                 goto release_win1fb;
1789     }
1790
1791     strcpy(inf->win1fb->fix.id, "win1fb");
1792     inf->win1fb->fix.type        = FB_TYPE_PACKED_PIXELS;
1793     inf->win1fb->fix.type_aux    = 0;
1794     inf->win1fb->fix.xpanstep    = 1;
1795     inf->win1fb->fix.ypanstep    = 1;
1796     inf->win1fb->fix.ywrapstep   = 0;
1797     inf->win1fb->fix.accel       = FB_ACCEL_NONE;
1798     inf->win1fb->fix.visual      = FB_VISUAL_TRUECOLOR;
1799     inf->win1fb->fix.smem_len    = 0;
1800     inf->win1fb->fix.line_length = 0;
1801     inf->win1fb->fix.smem_start  = 0;
1802
1803     inf->win1fb->var.xres = screen->x_res;
1804     inf->win1fb->var.yres = screen->y_res;
1805     inf->win1fb->var.bits_per_pixel = 16;
1806     inf->win1fb->var.xres_virtual = screen->x_res;
1807     inf->win1fb->var.yres_virtual = screen->y_res;
1808     inf->win1fb->var.width = screen->x_res;
1809     inf->win1fb->var.height = screen->y_res;
1810     inf->win1fb->var.pixclock = screen->pixclock;
1811     inf->win1fb->var.left_margin = screen->left_margin;
1812     inf->win1fb->var.right_margin = screen->right_margin;
1813     inf->win1fb->var.upper_margin = screen->upper_margin;
1814     inf->win1fb->var.lower_margin = screen->lower_margin;
1815     inf->win1fb->var.vsync_len = screen->vsync_len;
1816     inf->win1fb->var.hsync_len = screen->hsync_len;
1817     inf->win1fb->var.red    = def_rgb_16.red;
1818     inf->win1fb->var.green  = def_rgb_16.green;
1819     inf->win1fb->var.blue   = def_rgb_16.blue;
1820     inf->win1fb->var.transp = def_rgb_16.transp;
1821
1822     inf->win1fb->var.nonstd      = 0;  //win1 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
1823     inf->win1fb->var.grayscale   = 0;  //win1 transprent mode & value(mode<<8 + value)
1824     inf->win1fb->var.activate    = FB_ACTIVATE_NOW;
1825     inf->win1fb->var.accel_flags = 0;
1826     inf->win1fb->var.vmode       = FB_VMODE_NONINTERLACED;
1827
1828     inf->win1fb->fbops           = &win1fb_ops;
1829     inf->win1fb->flags           = FBINFO_FLAG_DEFAULT;
1830     inf->win1fb->pseudo_palette  = ((struct win1_par*)inf->win1fb->par)->pseudo_pal;
1831     inf->win1fb->screen_base     = 0;
1832
1833     memset(inf->win1fb->par, 0, sizeof(struct win1_par));
1834         ret = fb_alloc_cmap(&inf->win1fb->cmap, 256, 0);
1835         if (ret < 0)
1836                 goto release_cmap;
1837
1838     /* Prepare win0 info */
1839     fbprintk(">> Prepare win0 info \n");
1840     inf->win0fb = framebuffer_alloc(sizeof(struct win0_par), &pdev->dev);
1841     if(!inf->win0fb)
1842     {
1843         dev_err(&pdev->dev, ">> win0fb framebuffer_alloc fail!");
1844                 inf->win0fb = NULL;
1845                 ret = -ENOMEM;
1846                 goto release_win0fb;
1847     }
1848
1849     strcpy(inf->win0fb->fix.id, "win0fb");
1850         inf->win0fb->fix.type         = FB_TYPE_PACKED_PIXELS;
1851         inf->win0fb->fix.type_aux    = 0;
1852         inf->win0fb->fix.xpanstep    = 1;
1853         inf->win0fb->fix.ypanstep    = 1;
1854         inf->win0fb->fix.ywrapstep   = 0;
1855         inf->win0fb->fix.accel       = FB_ACCEL_NONE;
1856     inf->win0fb->fix.visual      = FB_VISUAL_TRUECOLOR;
1857     inf->win0fb->fix.smem_len    = 0;
1858     inf->win0fb->fix.line_length = 0;
1859     inf->win0fb->fix.smem_start  = 0;
1860
1861     inf->win0fb->var.xres = screen->x_res;
1862     inf->win0fb->var.yres = screen->y_res;
1863     inf->win0fb->var.bits_per_pixel = 16;
1864     inf->win0fb->var.xres_virtual = screen->x_res;
1865     inf->win0fb->var.yres_virtual = screen->y_res;
1866     inf->win0fb->var.width = screen->x_res;
1867     inf->win0fb->var.height = screen->y_res;
1868     inf->win0fb->var.pixclock = screen->pixclock;
1869     inf->win0fb->var.left_margin = screen->left_margin;
1870     inf->win0fb->var.right_margin = screen->right_margin;
1871     inf->win0fb->var.upper_margin = screen->upper_margin;
1872     inf->win0fb->var.lower_margin = screen->lower_margin;
1873     inf->win0fb->var.vsync_len = screen->vsync_len;
1874     inf->win0fb->var.hsync_len = screen->hsync_len;
1875     inf->win0fb->var.red    = def_rgb_16.red;
1876     inf->win0fb->var.green  = def_rgb_16.green;
1877     inf->win0fb->var.blue   = def_rgb_16.blue;
1878     inf->win0fb->var.transp = def_rgb_16.transp;
1879
1880     inf->win0fb->var.nonstd      = 0;  //win0 format & ypos & xpos (ypos<<20 + xpos<<8 + format)
1881     inf->win0fb->var.grayscale   = ((inf->win0fb->var.yres<<20)&0xfff00000) + ((inf->win0fb->var.xres<<8)&0xfff00);//win0 xsize & ysize
1882     inf->win0fb->var.activate    = FB_ACTIVATE_NOW;
1883     inf->win0fb->var.accel_flags = 0;
1884     inf->win0fb->var.vmode       = FB_VMODE_NONINTERLACED;
1885
1886     inf->win0fb->fbops           = &win0fb_ops;
1887         inf->win0fb->flags                    = FBINFO_FLAG_DEFAULT;
1888         inf->win0fb->pseudo_palette  = ((struct win0_par*)inf->win0fb->par)->pseudo_pal;
1889         inf->win0fb->screen_base     = 0;
1890
1891     memset(inf->win0fb->par, 0, sizeof(struct win0_par));
1892
1893         /* Init all lcdc and lcd before register_framebuffer. */
1894         /* because after register_framebuffer, the win1fb_check_par and winfb_set_par execute immediately */
1895         fbprintk(">> Init all lcdc and lcd before register_framebuffer \n");
1896     init_lcdc(inf->win1fb);
1897
1898   #ifdef CONFIG_CPU_FREQ
1899    // inf->freq_transition.notifier_call = rk29fb_freq_transition;
1900    // cpufreq_register_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
1901   #endif
1902         fbprintk("got clock\n");
1903
1904         if(mach_info)
1905     {
1906         struct rk29_fb_setting_info fb_setting;
1907         if( OUT_P888==inf->lcd_info.face ||
1908             OUT_P888==inf->tv_info[0].face ||
1909             OUT_P888==inf->hdmi_info[0].face )     // set lcdc iomux
1910         {
1911             fb_setting.data_num = 24;
1912         }
1913         else if(OUT_P666 == inf->lcd_info.face )
1914         {
1915             fb_setting.data_num = 18;
1916         }
1917         else
1918         {
1919             fb_setting.data_num = 16;
1920         }
1921         fb_setting.den_en = 1;
1922         fb_setting.vsync_en = 1;
1923         fb_setting.disp_on_en = 1;
1924         fb_setting.standby_en = 1;
1925         if( inf->lcd_info.mcu_usefmk )
1926             fb_setting.mcu_fmk_en =1;
1927         mach_info->io_init(&fb_setting);
1928     }
1929
1930         set_lcd_pin(pdev, 1);
1931         mdelay(10);
1932         g_pdev = pdev;
1933         inf->mcu_usetimer = 1;
1934     inf->mcu_fmksync = 0;
1935         load_screen(inf->win1fb, 1);
1936
1937     /* Register framebuffer(win1fb & win0fb) */
1938     fbprintk(">> Register framebuffer(win1fb) \n");
1939     ret = register_framebuffer(inf->win1fb);
1940     if(ret<0)
1941     {
1942         printk(">> win1fb register_framebuffer fail!\n");
1943         ret = -EINVAL;
1944                 goto release_win0fb;
1945     }
1946     fbprintk(">> Register framebuffer(win0fb) \n");
1947
1948     ret = register_framebuffer(inf->win0fb);
1949     if(ret<0)
1950     {
1951         printk(">> win0fb register_framebuffer fail!\n");
1952         ret = -EINVAL;
1953                 goto unregister_win1fb;
1954     }
1955
1956 #ifdef CONFIG_HAS_EARLYSUSPEND
1957         suspend_info.inf = inf;
1958         register_early_suspend(&suspend_info.early_suspend);
1959 #endif
1960
1961     /* get and request irq */
1962     fbprintk(">> get and request irq \n");
1963     irq = platform_get_irq(pdev, 0);
1964     if (irq < 0) {
1965         dev_err(&pdev->dev, "no irq for device\n");
1966         ret = -ENOENT;
1967         goto unregister_win1fb;
1968     }
1969     ret = request_irq(irq, rk29fb_irq, IRQF_DISABLED, pdev->name, pdev);
1970     if (ret) {
1971         dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
1972         ret = -EBUSY;
1973         goto release_irq;
1974     }
1975
1976     if( inf->lcd_info.mcu_usefmk && (mach_info->mcu_fmk_pin != -1) )
1977     {
1978         ret = request_irq(gpio_to_irq(mach_info->mcu_fmk_pin), mcu_irqfmk, GPIOEdgelFalling, pdev->name, pdev);
1979         if (ret)
1980         {
1981             dev_err(&pdev->dev, "cannot get fmk irq %d - err %d\n", irq, ret);
1982             ret = -EBUSY;
1983             goto release_irq;
1984         }
1985     }
1986
1987     printk(" %s ok\n", __FUNCTION__);
1988     return ret;
1989
1990 release_irq:
1991         if(irq>=0)
1992         free_irq(irq, pdev);
1993 unregister_win1fb:
1994     unregister_framebuffer(inf->win1fb);
1995 release_win0fb:
1996         if(inf->win0fb)
1997                 framebuffer_release(inf->win0fb);
1998         inf->win0fb = NULL;
1999 release_cmap:
2000     if(&inf->win1fb->cmap)
2001         fb_dealloc_cmap(&inf->win1fb->cmap);
2002 release_win1fb:
2003         if(inf->win1fb)
2004                 framebuffer_release(inf->win1fb);
2005         inf->win1fb = NULL;
2006 release_drvdata:
2007         if(inf && inf->reg_vir_base)
2008         iounmap(inf->reg_vir_base);
2009         if(inf && mem)
2010         release_mem_region(inf->reg_phy_base, inf->len);
2011         if(inf)
2012         kfree(inf);
2013         platform_set_drvdata(pdev, NULL);
2014         return ret;
2015 }
2016
2017 static int rk29fb_remove(struct platform_device *pdev)
2018 {
2019     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
2020     struct fb_info *info = NULL;
2021         //pm_message_t msg;
2022     struct rk29fb_info *mach_info = NULL;
2023     int irq = 0;
2024
2025         fbprintk(">>>>>> %s : %s\n", __FILE__, __FUNCTION__);
2026
2027     if(!inf) {
2028         printk("inf==0, rk29_fb_remove fail! \n");
2029         return -EINVAL;
2030     }
2031
2032     irq = platform_get_irq(pdev, 0);
2033     if (irq >0)
2034     {
2035     free_irq(irq, pdev);
2036     }
2037
2038     mach_info = pdev->dev.platform_data;
2039     if(mach_info->mcu_fmk_pin)
2040     {
2041         free_irq(gpio_to_irq(mach_info->mcu_fmk_pin), pdev);
2042     }
2043
2044         set_lcd_pin(pdev, 0);
2045
2046     // blank the lcdc
2047     if(inf->win0fb)
2048         win0fb_blank(FB_BLANK_POWERDOWN, inf->win0fb);
2049     if(inf->win1fb)
2050         win1fb_blank(FB_BLANK_POWERDOWN, inf->win1fb);
2051
2052         // suspend the lcdc
2053         //rk29fb_suspend(pdev, msg);
2054     // unmap memory and release framebuffer
2055     if(inf->win0fb) {
2056         info = inf->win0fb;
2057         if (info->screen_base) {
2058                 //dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
2059                 info->screen_base = 0;
2060                 info->fix.smem_start = 0;
2061                 info->fix.smem_len = 0;
2062         }
2063         unregister_framebuffer(inf->win0fb);
2064         framebuffer_release(inf->win0fb);
2065         inf->win0fb = NULL;
2066     }
2067     if(inf->win1fb) {
2068         info = inf->win1fb;
2069         if (info->screen_base) {
2070                 dma_free_writecombine(NULL, PAGE_ALIGN(info->fix.smem_len),info->screen_base, info->fix.smem_start);
2071                 info->screen_base = 0;
2072                 info->fix.smem_start = 0;
2073                 info->fix.smem_len = 0;
2074         }
2075         unregister_framebuffer(inf->win1fb);
2076         framebuffer_release(inf->win1fb);
2077         inf->win1fb = NULL;
2078     }
2079
2080   #ifdef CONFIG_CPU_FREQ
2081     cpufreq_unregister_notifier(&inf->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
2082   #endif
2083
2084         if (inf->clk)
2085     {
2086                 clk_disable(inf->clk);
2087                 clk_put(inf->clk);
2088                 inf->clk = NULL;
2089         }
2090     if (inf->dclk)
2091     {
2092                 clk_disable(inf->dclk);
2093                 clk_put(inf->dclk);
2094                 inf->dclk = NULL;
2095         }
2096
2097     kfree(inf);
2098     platform_set_drvdata(pdev, NULL);
2099
2100     return 0;
2101 }
2102
2103 static void rk29fb_shutdown(struct platform_device *pdev)
2104 {
2105     struct rk29fb_inf *inf = platform_get_drvdata(pdev);
2106     mdelay(300);
2107         //printk("----------------------------rk29fb_shutdown----------------------------\n");
2108         set_lcd_pin(pdev, 0);
2109     if (inf->dclk)
2110     {
2111         clk_disable(inf->dclk);
2112     }
2113     if (inf->clk)
2114     {
2115         clk_disable(inf->clk);
2116     }
2117 }
2118
2119 static struct platform_driver rk29fb_driver = {
2120         .probe          = rk29fb_probe,
2121         .remove         = rk29fb_remove,
2122         .driver         = {
2123                 .name   = "rk29-fb",
2124                 .owner  = THIS_MODULE,
2125         },
2126         .shutdown   = rk29fb_shutdown,
2127 };
2128
2129 static int __init rk29fb_init(void)
2130 {
2131     return platform_driver_register(&rk29fb_driver);
2132 }
2133
2134 static void __exit rk29fb_exit(void)
2135 {
2136     platform_driver_unregister(&rk29fb_driver);
2137 }
2138
2139 //subsys_initcall(rk29fb_init);
2140
2141 module_init(rk29fb_init);
2142 module_exit(rk29fb_exit);
2143
2144
2145 MODULE_AUTHOR("  zyw@rock-chips.com");
2146 MODULE_DESCRIPTION("Driver for rk29 fb device");
2147 MODULE_LICENSE("GPL");
2148
2149