1fe662f43978fc017d29d847b7919abc9b549d3a
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / rk_fb.c
1 /*
2  * drivers/video/rockchip/rk_fb.c
3  *
4  * Copyright (C) ROCKCHIP, Inc.
5  * Author:yxj<yxj@rock-chips.com>
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/kthread.h>
25 #include <linux/fb.h>
26 #include <linux/init.h>
27 #include <asm/div64.h>
28 #include <linux/uaccess.h>
29 #include <linux/rk_fb.h>
30 #include <linux/linux_logo.h>
31 #include <linux/dma-mapping.h>
32
33 #if defined(CONFIG_RK_HDMI)
34 #include "hdmi/rk_hdmi.h"
35 #endif
36
37 #if defined(CONFIG_ROCKCHIP_RGA) || defined(CONFIG_ROCKCHIP_RGA2)
38 #include "rga/rga.h"
39 #endif
40
41 #ifdef CONFIG_OF
42 #include <linux/of.h>
43 #include <linux/of_platform.h>
44 #include <linux/of_gpio.h>
45 #include <video/of_display_timing.h>
46 #include <video/display_timing.h>
47 #include <dt-bindings/rkfb/rk_fb.h>
48 #endif
49
50 #if defined(CONFIG_ION_ROCKCHIP)
51 #include <linux/rockchip_ion.h>
52 #include <linux/rockchip-iovmm.h>
53 #include <linux/dma-buf.h>
54 #include <linux/highmem.h>
55 #endif
56
57 #define H_USE_FENCE 1
58 static int hdmi_switch_complete;
59 static struct platform_device *fb_pdev;
60 struct list_head saved_list;
61
62 #if defined(CONFIG_FB_MIRRORING)
63 int (*video_data_to_mirroring) (struct fb_info *info, u32 yuv_phy[2]);
64 EXPORT_SYMBOL(video_data_to_mirroring);
65 #endif
66
67 struct rk_fb_reg_win_data g_reg_win_data[4];
68 static int g_last_win_num;
69 static int g_first_buf = 1;
70 static struct rk_fb_trsm_ops *trsm_lvds_ops;
71 static struct rk_fb_trsm_ops *trsm_edp_ops;
72 static struct rk_fb_trsm_ops *trsm_mipi_ops;
73 static int uboot_logo_on;
74 int support_uboot_display(void)
75 {
76         return uboot_logo_on;
77 }
78
79 int rk_fb_trsm_ops_register(struct rk_fb_trsm_ops *ops, int type)
80 {
81         switch (type) {
82         case SCREEN_RGB:
83         case SCREEN_LVDS:
84         case SCREEN_DUAL_LVDS:
85                 trsm_lvds_ops = ops;
86                 break;
87         case SCREEN_EDP:
88                 trsm_edp_ops = ops;
89                 break;
90         case SCREEN_MIPI:
91         case SCREEN_DUAL_MIPI:
92                 trsm_mipi_ops = ops;
93                 break;
94         default:
95                 printk(KERN_WARNING "%s:un supported transmitter:%d!\n",
96                        __func__, type);
97                 break;
98         }
99         return 0;
100 }
101
102 struct rk_fb_trsm_ops *rk_fb_trsm_ops_get(int type)
103 {
104         struct rk_fb_trsm_ops *ops;
105         switch (type) {
106         case SCREEN_RGB:
107         case SCREEN_LVDS:
108         case SCREEN_DUAL_LVDS:
109                 ops = trsm_lvds_ops;
110                 break;
111         case SCREEN_EDP:
112                 ops = trsm_edp_ops;
113                 break;
114         case SCREEN_MIPI:
115         case SCREEN_DUAL_MIPI:
116                 ops = trsm_mipi_ops;
117                 break;
118         default:
119                 ops = NULL;
120                 printk(KERN_WARNING "%s:un supported transmitter:%d!\n",
121                        __func__, type);
122                 break;
123         }
124         return ops;
125 }
126
127 int rk_fb_pixel_width(int data_format)
128 {
129         int pixel_width;
130         switch (data_format) {
131         case XBGR888:
132         case ABGR888:
133         case ARGB888:
134                 pixel_width = 4 * 8;
135                 break;
136         case RGB888:
137                 pixel_width = 3 * 8;
138                 break;
139         case RGB565:
140                 pixel_width = 2 * 8;
141                 break;
142         case YUV422:
143         case YUV420:
144         case YUV444:
145                 pixel_width = 1 * 8;
146                 break;
147         case YUV422_A:
148         case YUV420_A:
149         case YUV444_A:
150                 pixel_width = 8;
151                 break;
152         default:
153                 printk(KERN_WARNING "%s:un supported format:0x%x\n",
154                        __func__, data_format);
155                 return -EINVAL;
156         }
157         return pixel_width;
158 }
159
160 static int rk_fb_data_fmt(int data_format, int bits_per_pixel)
161 {
162         int fb_data_fmt;
163         if (data_format) {
164                 switch (data_format) {
165                 case HAL_PIXEL_FORMAT_RGBX_8888:
166                         fb_data_fmt = XBGR888;
167                         break;
168                 case HAL_PIXEL_FORMAT_RGBA_8888:
169                         fb_data_fmt = ABGR888;
170                         break;
171                 case HAL_PIXEL_FORMAT_BGRA_8888:
172                         fb_data_fmt = ARGB888;
173                         break;
174                 case HAL_PIXEL_FORMAT_RGB_888:
175                         fb_data_fmt = RGB888;
176                         break;
177                 case HAL_PIXEL_FORMAT_RGB_565:
178                         fb_data_fmt = RGB565;
179                         break;
180                 case HAL_PIXEL_FORMAT_YCbCr_422_SP:     /* yuv422 */
181                         fb_data_fmt = YUV422;
182                         break;
183                 case HAL_PIXEL_FORMAT_YCrCb_NV12:       /* YUV420---uvuvuv */
184                         fb_data_fmt = YUV420;
185                         break;
186                 case HAL_PIXEL_FORMAT_YCrCb_444:        /* yuv444 */
187                         fb_data_fmt = YUV444;
188                         break;
189                 case HAL_PIXEL_FORMAT_YCrCb_NV12_10:    /* yuv444 */
190                         fb_data_fmt = YUV420_A;
191                         break;
192                 case HAL_PIXEL_FORMAT_YCbCr_422_SP_10:  /* yuv444 */
193                         fb_data_fmt = YUV422_A;
194                         break;
195                 case HAL_PIXEL_FORMAT_YCrCb_420_SP_10:  /* yuv444 */
196                         fb_data_fmt = YUV444_A;
197                         break;
198                 default:
199                         printk(KERN_WARNING "%s:un supported format:0x%x\n",
200                                __func__, data_format);
201                         return -EINVAL;
202                 }
203         } else {
204                 switch (bits_per_pixel) {
205                 case 32:
206                         fb_data_fmt = ARGB888;
207                         break;
208                 case 16:
209                         fb_data_fmt = RGB565;
210                         break;
211                 default:
212                         printk(KERN_WARNING
213                                "%s:un supported bits_per_pixel:%d\n", __func__,
214                                bits_per_pixel);
215                         break;
216                 }
217         }
218         return fb_data_fmt;
219 }
220
221 /*
222  * rk display power control parse from dts
223  */
224 int rk_disp_pwr_ctr_parse_dt(struct rk_lcdc_driver *dev_drv)
225 {
226         struct device_node *root = of_get_child_by_name(dev_drv->dev->of_node,
227                                                         "power_ctr");
228         struct device_node *child;
229         struct rk_disp_pwr_ctr_list *pwr_ctr;
230         struct list_head *pos;
231         enum of_gpio_flags flags;
232         u32 val = 0;
233         u32 debug = 0;
234         int ret;
235
236         INIT_LIST_HEAD(&dev_drv->pwrlist_head);
237         if (!root) {
238                 dev_err(dev_drv->dev, "can't find power_ctr node for lcdc%d\n",
239                         dev_drv->id);
240                 return -ENODEV;
241         }
242
243         for_each_child_of_node(root, child) {
244                 pwr_ctr = kmalloc(sizeof(struct rk_disp_pwr_ctr_list),
245                                   GFP_KERNEL);
246                 strcpy(pwr_ctr->pwr_ctr.name, child->name);
247                 if (!of_property_read_u32(child, "rockchip,power_type", &val)) {
248                         if (val == GPIO) {
249                                 pwr_ctr->pwr_ctr.type = GPIO;
250                                 pwr_ctr->pwr_ctr.gpio = of_get_gpio_flags(child, 0, &flags);
251                                 if (!gpio_is_valid(pwr_ctr->pwr_ctr.gpio)) {
252                                         dev_err(dev_drv->dev, "%s ivalid gpio\n",
253                                                 child->name);
254                                         return -EINVAL;
255                                 }
256                                 pwr_ctr->pwr_ctr.atv_val = !(flags & OF_GPIO_ACTIVE_LOW);
257                                 ret = gpio_request(pwr_ctr->pwr_ctr.gpio,
258                                                    child->name);
259                                 if (ret) {
260                                         dev_err(dev_drv->dev,
261                                                 "request %s gpio fail:%d\n",
262                                                 child->name, ret);
263                                 }
264
265                         } else {
266                                 pwr_ctr->pwr_ctr.type = REGULATOR;
267
268                         }
269                 };
270                 of_property_read_u32(child, "rockchip,delay", &val);
271                 pwr_ctr->pwr_ctr.delay = val;
272                 list_add_tail(&pwr_ctr->list, &dev_drv->pwrlist_head);
273         }
274
275         of_property_read_u32(root, "rockchip,debug", &debug);
276
277         if (debug) {
278                 list_for_each(pos, &dev_drv->pwrlist_head) {
279                         pwr_ctr = list_entry(pos, struct rk_disp_pwr_ctr_list,
280                                              list);
281                         printk(KERN_INFO "pwr_ctr_name:%s\n"
282                                "pwr_type:%s\n"
283                                "gpio:%d\n"
284                                "atv_val:%d\n"
285                                "delay:%d\n\n",
286                                pwr_ctr->pwr_ctr.name,
287                                (pwr_ctr->pwr_ctr.type == GPIO) ? "gpio" : "regulator",
288                                pwr_ctr->pwr_ctr.gpio,
289                                pwr_ctr->pwr_ctr.atv_val,
290                                pwr_ctr->pwr_ctr.delay);
291                 }
292         }
293
294         return 0;
295
296 }
297
298 int rk_disp_pwr_enable(struct rk_lcdc_driver *dev_drv)
299 {
300         struct list_head *pos;
301         struct rk_disp_pwr_ctr_list *pwr_ctr_list;
302         struct pwr_ctr *pwr_ctr;
303         if (list_empty(&dev_drv->pwrlist_head))
304                 return 0;
305         list_for_each(pos, &dev_drv->pwrlist_head) {
306                 pwr_ctr_list = list_entry(pos, struct rk_disp_pwr_ctr_list,
307                                           list);
308                 pwr_ctr = &pwr_ctr_list->pwr_ctr;
309                 if (pwr_ctr->type == GPIO) {
310                         gpio_direction_output(pwr_ctr->gpio, pwr_ctr->atv_val);
311                         mdelay(pwr_ctr->delay);
312                 }
313         }
314
315         return 0;
316 }
317
318 int rk_disp_pwr_disable(struct rk_lcdc_driver *dev_drv)
319 {
320         struct list_head *pos;
321         struct rk_disp_pwr_ctr_list *pwr_ctr_list;
322         struct pwr_ctr *pwr_ctr;
323         if (list_empty(&dev_drv->pwrlist_head))
324                 return 0;
325         list_for_each(pos, &dev_drv->pwrlist_head) {
326                 pwr_ctr_list = list_entry(pos, struct rk_disp_pwr_ctr_list,
327                                           list);
328                 pwr_ctr = &pwr_ctr_list->pwr_ctr;
329                 if (pwr_ctr->type == GPIO)
330                         gpio_set_value(pwr_ctr->gpio, !pwr_ctr->atv_val);
331         }
332         return 0;
333 }
334
335 int rk_fb_video_mode_from_timing(const struct display_timing *dt,
336                                  struct rk_screen *screen)
337 {
338         screen->mode.pixclock = dt->pixelclock.typ;
339         screen->mode.left_margin = dt->hback_porch.typ;
340         screen->mode.right_margin = dt->hfront_porch.typ;
341         screen->mode.xres = dt->hactive.typ;
342         screen->mode.hsync_len = dt->hsync_len.typ;
343         screen->mode.upper_margin = dt->vback_porch.typ;
344         screen->mode.lower_margin = dt->vfront_porch.typ;
345         screen->mode.yres = dt->vactive.typ;
346         screen->mode.vsync_len = dt->vsync_len.typ;
347         screen->type = dt->screen_type;
348         screen->lvds_format = dt->lvds_format;
349         screen->face = dt->face;
350         screen->color_mode = dt->color_mode;
351         screen->dsp_lut = dt->dsp_lut;
352
353         if (dt->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
354                 screen->pin_dclk = 1;
355         else
356                 screen->pin_dclk = 0;
357         if (dt->flags & DISPLAY_FLAGS_HSYNC_HIGH)
358                 screen->pin_hsync = 1;
359         else
360                 screen->pin_hsync = 0;
361         if (dt->flags & DISPLAY_FLAGS_VSYNC_HIGH)
362                 screen->pin_vsync = 1;
363         else
364                 screen->pin_vsync = 0;
365         if (dt->flags & DISPLAY_FLAGS_DE_HIGH)
366                 screen->pin_den = 1;
367         else
368                 screen->pin_den = 0;
369
370         return 0;
371
372 }
373
374 int rk_fb_prase_timing_dt(struct device_node *np, struct rk_screen *screen)
375 {
376         struct display_timings *disp_timing;
377         struct display_timing *dt;
378         disp_timing = of_get_display_timings(np);
379         if (!disp_timing) {
380                 pr_err("parse display timing err\n");
381                 return -EINVAL;
382         }
383         dt = display_timings_get(disp_timing, disp_timing->native_mode);
384         rk_fb_video_mode_from_timing(dt, screen);
385         return 0;
386
387 }
388
389 int rk_fb_calc_fps(struct rk_screen *screen, u32 pixclock)
390 {
391         int x, y;
392         unsigned long long hz;
393         if (!screen) {
394                 printk(KERN_ERR "%s:null screen!\n", __func__);
395                 return 0;
396         }
397         x = screen->mode.xres + screen->mode.left_margin +
398             screen->mode.right_margin + screen->mode.hsync_len;
399         y = screen->mode.yres + screen->mode.upper_margin +
400             screen->mode.lower_margin + screen->mode.vsync_len;
401
402         hz = 1000000000000ULL;  /* 1e12 picoseconds per second */
403
404         hz += (x * y) / 2;
405         do_div(hz, x * y);      /* divide by x * y with rounding */
406
407         hz += pixclock / 2;
408         do_div(hz, pixclock);   /* divide by pixclock with rounding */
409
410         return hz;
411 }
412
413 char *get_format_string(enum data_format format, char *fmt)
414 {
415         if (!fmt)
416                 return NULL;
417         switch (format) {
418         case ARGB888:
419                 strcpy(fmt, "ARGB888");
420                 break;
421         case RGB888:
422                 strcpy(fmt, "RGB888");
423                 break;
424         case RGB565:
425                 strcpy(fmt, "RGB565");
426                 break;
427         case YUV420:
428                 strcpy(fmt, "YUV420");
429                 break;
430         case YUV422:
431                 strcpy(fmt, "YUV422");
432                 break;
433         case YUV444:
434                 strcpy(fmt, "YUV444");
435                 break;
436         case YUV420_A:
437                 strcpy(fmt, "YUV420_A");
438                 break;
439         case YUV422_A:
440                 strcpy(fmt, "YUV422_A");
441                 break;
442         case YUV444_A:
443                 strcpy(fmt, "YUV444_A");
444                 break;
445         case XRGB888:
446                 strcpy(fmt, "XRGB888");
447                 break;
448         case XBGR888:
449                 strcpy(fmt, "XBGR888");
450                 break;
451         case ABGR888:
452                 strcpy(fmt, "XBGR888");
453                 break;
454         default:
455                 strcpy(fmt, "invalid");
456                 break;
457         }
458
459         return fmt;
460
461 }
462
463 /*
464  * this is for hdmi
465  * name: lcdc device name ,lcdc0 , lcdc1
466  */
467 struct rk_lcdc_driver *rk_get_lcdc_drv(char *name)
468 {
469         struct rk_fb *inf = NULL;
470         struct rk_lcdc_driver *dev_drv = NULL;
471         int i = 0;
472
473         if (likely(fb_pdev))
474                 inf = platform_get_drvdata(fb_pdev);
475         else
476                 return NULL;
477
478         for (i = 0; i < inf->num_lcdc; i++) {
479                 if (!strcmp(inf->lcdc_dev_drv[i]->name, name)) {
480                         dev_drv = inf->lcdc_dev_drv[i];
481                         break;
482                 }
483         }
484
485         return dev_drv;
486 }
487
488 static struct rk_lcdc_driver *rk_get_prmry_lcdc_drv(void)
489 {
490         struct rk_fb *inf = NULL;
491         struct rk_lcdc_driver *dev_drv = NULL;
492         int i = 0;
493
494         if (likely(fb_pdev))
495                 inf = platform_get_drvdata(fb_pdev);
496         else
497                 return NULL;
498
499         for (i = 0; i < inf->num_lcdc; i++) {
500                 if (inf->lcdc_dev_drv[i]->prop == PRMRY) {
501                         dev_drv = inf->lcdc_dev_drv[i];
502                         break;
503                 }
504         }
505
506         return dev_drv;
507 }
508
509 /*
510  * get one frame time of the prmry screen, unit: us
511  */
512 u32 rk_fb_get_prmry_screen_ft(void)
513 {
514         struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
515         uint32_t htotal, vtotal, pixclock_ps;
516         u64 pix_total, ft_us;
517
518         if (unlikely(!dev_drv))
519                 return 0;
520
521         pixclock_ps = dev_drv->pixclock;
522
523         vtotal = (dev_drv->cur_screen->mode.upper_margin +
524                  dev_drv->cur_screen->mode.lower_margin +
525                  dev_drv->cur_screen->mode.yres +
526                  dev_drv->cur_screen->mode.vsync_len);
527         htotal = (dev_drv->cur_screen->mode.left_margin +
528                  dev_drv->cur_screen->mode.right_margin +
529                  dev_drv->cur_screen->mode.xres +
530                  dev_drv->cur_screen->mode.hsync_len);
531         pix_total = htotal * vtotal;
532         ft_us = pix_total * pixclock_ps;
533         do_div(ft_us, 1000000);
534         if (dev_drv->frame_time.ft == 0)
535                 dev_drv->frame_time.ft = ft_us;
536
537         ft_us = dev_drv->frame_time.framedone_t - dev_drv->frame_time.last_framedone_t;
538         do_div(ft_us, 1000);
539         ft_us = min(dev_drv->frame_time.ft, (u32)ft_us);
540         if (ft_us != 0)
541                 dev_drv->frame_time.ft = ft_us;
542
543         return dev_drv->frame_time.ft;
544 }
545
546 /*
547  * get the vblanking time of the prmry screen, unit: us
548  */
549 u32 rk_fb_get_prmry_screen_vbt(void)
550 {
551         struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
552         uint32_t htotal, vblank, pixclock_ps;
553         u64 pix_blank, vbt_us;
554
555         if (unlikely(!dev_drv))
556                 return 0;
557
558         pixclock_ps = dev_drv->pixclock;
559
560         htotal = (dev_drv->cur_screen->mode.left_margin +
561                  dev_drv->cur_screen->mode.right_margin +
562                  dev_drv->cur_screen->mode.xres +
563                  dev_drv->cur_screen->mode.hsync_len);
564         vblank = (dev_drv->cur_screen->mode.upper_margin +
565                  dev_drv->cur_screen->mode.lower_margin +
566                  dev_drv->cur_screen->mode.vsync_len);
567         pix_blank = htotal * vblank;
568         vbt_us = pix_blank * pixclock_ps;
569         do_div(vbt_us, 1000000);
570         return (u32)vbt_us;
571 }
572
573 /*
574  * get the frame done time of the prmry screen, unit: us
575  */
576 u64 rk_fb_get_prmry_screen_framedone_t(void)
577 {
578         struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
579
580         if (unlikely(!dev_drv))
581                 return 0;
582         else
583                 return dev_drv->frame_time.framedone_t;
584 }
585
586 /*
587  * set prmry screen status
588  */
589 int rk_fb_set_prmry_screen_status(int status)
590 {
591         struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
592         struct rk_screen *screen;
593
594         if (unlikely(!dev_drv))
595                 return 0;
596
597         screen = dev_drv->cur_screen;
598         switch (status) {
599         case SCREEN_PREPARE_DDR_CHANGE:
600                 if (screen->type == SCREEN_MIPI
601                         || screen->type == SCREEN_DUAL_MIPI) {
602                         if (dev_drv->trsm_ops->dsp_pwr_off)
603                                 dev_drv->trsm_ops->dsp_pwr_off();
604                 }
605                 break;
606         case SCREEN_UNPREPARE_DDR_CHANGE:
607                 if (screen->type == SCREEN_MIPI
608                         || screen->type == SCREEN_DUAL_MIPI) {
609                         if (dev_drv->trsm_ops->dsp_pwr_on)
610                                 dev_drv->trsm_ops->dsp_pwr_on();
611                 }
612                 break;
613         default:
614                 break;
615         }
616
617         return 0;
618 }
619
620 static struct rk_lcdc_driver *rk_get_extend_lcdc_drv(void)
621 {
622         struct rk_fb *inf = NULL;
623         struct rk_lcdc_driver *dev_drv = NULL;
624         int i = 0;
625
626         if (likely(fb_pdev))
627                 inf = platform_get_drvdata(fb_pdev);
628         else
629                 return NULL;
630
631         for (i = 0; i < inf->num_lcdc; i++) {
632                 if (inf->lcdc_dev_drv[i]->prop == EXTEND) {
633                         dev_drv = inf->lcdc_dev_drv[i];
634                         break;
635                 }
636         }
637
638         return dev_drv;
639 }
640
641 u32 rk_fb_get_prmry_screen_pixclock(void)
642 {
643         struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
644
645         if (unlikely(!dev_drv))
646                 return 0;
647         else
648                 return dev_drv->pixclock;
649 }
650
651 int rk_fb_poll_prmry_screen_vblank(void)
652 {
653         struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
654
655         if (likely(dev_drv)) {
656                 if (dev_drv->ops->poll_vblank)
657                         return dev_drv->ops->poll_vblank(dev_drv);
658                 else
659                         return RK_LF_STATUS_NC;
660         } else {
661                 return RK_LF_STATUS_NC;
662         }
663 }
664
665 bool rk_fb_poll_wait_frame_complete(void)
666 {
667         uint32_t timeout = RK_LF_MAX_TIMEOUT;
668         struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
669
670         if (likely(dev_drv)) {
671                 if (dev_drv->ops->set_irq_to_cpu)
672                         dev_drv->ops->set_irq_to_cpu(dev_drv, 0);
673         }
674
675         if (rk_fb_poll_prmry_screen_vblank() == RK_LF_STATUS_NC) {
676                 if (likely(dev_drv)) {
677                         if (dev_drv->ops->set_irq_to_cpu)
678                                 dev_drv->ops->set_irq_to_cpu(dev_drv, 1);
679                 }
680                 return false;
681         }
682         while (!(rk_fb_poll_prmry_screen_vblank() == RK_LF_STATUS_FR) && --timeout)
683                 ;
684         while (!(rk_fb_poll_prmry_screen_vblank() == RK_LF_STATUS_FC) && --timeout)
685                 ;
686         if (likely(dev_drv)) {
687                 if (dev_drv->ops->set_irq_to_cpu)
688                         dev_drv->ops->set_irq_to_cpu(dev_drv, 1);
689         }
690
691         return true;
692 }
693
694
695 /* rk_fb_get_sysmmu_device_by_compatible()
696  * @compt: dts device compatible name
697  * return value: success: pointer to the device inside of platform device
698  *               fail: NULL
699  */
700 struct device *rk_fb_get_sysmmu_device_by_compatible(const char *compt)
701 {
702         struct device_node *dn = NULL;
703         struct platform_device *pd = NULL;
704         struct device *ret = NULL ;
705
706         dn = of_find_compatible_node(NULL, NULL, compt);
707         if (!dn) {
708                 printk("can't find device node %s \r\n", compt);
709                 return NULL;
710         }
711
712         pd = of_find_device_by_node(dn);
713         if (!pd) {
714                 printk("can't find platform device in device node %s \r\n", compt);
715                 return  NULL;
716         }
717         ret = &pd->dev;
718
719         return ret;
720 }
721
722 #ifdef CONFIG_IOMMU_API
723 void rk_fb_platform_set_sysmmu(struct device *sysmmu, struct device *dev)
724 {
725         dev->archdata.iommu = sysmmu;
726 }
727 #else
728 void rk_fb_platform_set_sysmmu(struct device *sysmmu, struct device *dev)
729 {
730
731 }
732 #endif
733
734 static int rk_fb_open(struct fb_info *info, int user)
735 {
736         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
737         int win_id;
738
739         win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
740         dev_drv->win[win_id]->logicalstate++;
741         /* if this win aready opened ,no need to reopen */
742         if (dev_drv->win[win_id]->state)
743                 return 0;
744         else
745                 dev_drv->ops->open(dev_drv, win_id, 1);
746         return 0;
747 }
748
749 static int get_extend_fb_id(struct fb_info *info)
750 {
751         int fb_id = 0;
752         char *id = info->fix.id;
753         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
754
755         if (!strcmp(id, "fb0"))
756                 fb_id = 0;
757         else if (!strcmp(id, "fb1"))
758                 fb_id = 1;
759         else if (!strcmp(id, "fb2") && (dev_drv->lcdc_win_num > 2))
760                 fb_id = 2;
761         return fb_id;
762 }
763
764 static int rk_fb_close(struct fb_info *info, int user)
765 {
766         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
767         struct rk_lcdc_win *win = NULL;
768         int win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
769
770         if (win_id >= 0) {
771                 dev_drv->win[win_id]->logicalstate--;
772                 if (!dev_drv->win[win_id]->logicalstate) {
773                         win = dev_drv->win[win_id];
774                         info->fix.smem_start = win->reserved;
775                         info->var.xres = dev_drv->screen0->mode.xres;
776                         info->var.yres = dev_drv->screen0->mode.yres;
777                         /*
778                         info->var.grayscale |=
779                             (info->var.xres << 8) + (info->var.yres << 20);
780                         */
781                         info->var.xres_virtual = info->var.xres;
782                         info->var.yres_virtual = info->var.yres;
783 #if defined(CONFIG_LOGO_LINUX_BMP)
784                         info->var.bits_per_pixel = 32;
785 #else
786                         info->var.bits_per_pixel = 16;
787 #endif
788                         info->fix.line_length =
789                             (info->var.xres_virtual) * (info->var.bits_per_pixel >> 3);
790                         info->var.width = dev_drv->screen0->width;
791                         info->var.height = dev_drv->screen0->height;
792                         info->var.pixclock = dev_drv->pixclock;
793                         info->var.left_margin = dev_drv->screen0->mode.left_margin;
794                         info->var.right_margin = dev_drv->screen0->mode.right_margin;
795                         info->var.upper_margin = dev_drv->screen0->mode.upper_margin;
796                         info->var.lower_margin = dev_drv->screen0->mode.lower_margin;
797                         info->var.vsync_len = dev_drv->screen0->mode.vsync_len;
798                         info->var.hsync_len = dev_drv->screen0->mode.hsync_len;
799                 }
800         }
801
802         return 0;
803 }
804
805
806 #if defined(CONFIG_RK29_IPP)
807 static int get_ipp_format(int fmt)
808 {
809         int ipp_fmt = IPP_XRGB_8888;
810         switch (fmt) {
811         case HAL_PIXEL_FORMAT_RGBX_8888:
812         case HAL_PIXEL_FORMAT_RGBA_8888:
813         case HAL_PIXEL_FORMAT_BGRA_8888:
814         case HAL_PIXEL_FORMAT_RGB_888:
815                 ipp_fmt = IPP_XRGB_8888;
816                 break;
817         case HAL_PIXEL_FORMAT_RGB_565:
818                 ipp_fmt = IPP_RGB_565;
819                 break;
820         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
821                 ipp_fmt = IPP_Y_CBCR_H2V1;
822                 break;
823         case HAL_PIXEL_FORMAT_YCrCb_NV12:
824                 ipp_fmt = IPP_Y_CBCR_H2V2;
825                 break;
826         case HAL_PIXEL_FORMAT_YCrCb_444:
827                 ipp_fmt = IPP_Y_CBCR_H1V1;
828                 break;
829         default:
830                 ipp_fmt = IPP_IMGTYPE_LIMIT;
831                 break;
832         }
833
834         return ipp_fmt;
835 }
836
837 static void ipp_win_check(int *dst_w, int *dst_h, int *dst_vir_w,
838                           int rotation, int fmt)
839 {
840         int align16 = 2;
841         int align64 = 8;
842
843         if (fmt == IPP_XRGB_8888) {
844                 align16 = 1;
845                 align64 = 2;
846         } else if (fmt == IPP_RGB_565) {
847                 align16 = 1;
848                 align64 = 4;
849         } else {
850                 align16 = 2;
851                 align64 = 8;
852         }
853         align16 -= 1;           /*for YUV, 1 */
854         align64 -= 1;           /*for YUV, 7 */
855
856         if (rotation == IPP_ROT_0) {
857                 if (fmt > IPP_RGB_565) {
858                         if ((*dst_w & 1) != 0)
859                                 *dst_w = *dst_w + 1;
860                         if ((*dst_h & 1) != 0)
861                                 *dst_h = *dst_h + 1;
862                         if (*dst_vir_w < *dst_w)
863                                 *dst_vir_w = *dst_w;
864                 }
865         } else {
866                 if ((*dst_w & align64) != 0)
867                         *dst_w = (*dst_w + align64) & (~align64);
868                 if ((fmt > IPP_RGB_565) && ((*dst_h & 1) == 1))
869                         *dst_h = *dst_h + 1;
870                 if (*dst_vir_w < *dst_w)
871                         *dst_vir_w = *dst_w;
872         }
873 }
874
875 static void fb_copy_by_ipp(struct fb_info *dst_info,
876                            struct fb_info *src_info, int offset)
877 {
878         struct rk29_ipp_req ipp_req;
879         uint32_t rotation = 0;
880         int dst_w, dst_h, dst_vir_w;
881         int ipp_fmt;
882         u8 data_format = (dst_info->var.nonstd) & 0xff;
883         struct rk_lcdc_driver *ext_dev_drv =
884                         (struct rk_lcdc_driver *)dst_info->par;
885         u16 orientation = ext_dev_drv->rotate_mode;
886
887         memset(&ipp_req, 0, sizeof(struct rk29_ipp_req));
888
889         switch (orientation) {
890         case 0:
891                 rotation = IPP_ROT_0;
892                 break;
893         case ROTATE_90:
894                 rotation = IPP_ROT_90;
895                 break;
896         case ROTATE_180:
897                 rotation = IPP_ROT_180;
898                 break;
899         case ROTATE_270:
900                 rotation = IPP_ROT_270;
901                 break;
902         default:
903                 rotation = IPP_ROT_270;
904                 break;
905         }
906
907         dst_w = dst_info->var.xres;
908         dst_h = dst_info->var.yres;
909         dst_vir_w = dst_info->var.xres_virtual;
910         ipp_fmt = get_ipp_format(data_format);
911         ipp_win_check(&dst_w, &dst_h, &dst_vir_w, rotation, ipp_fmt);
912         ipp_req.src0.YrgbMst = src_info->fix.smem_start + offset;
913         ipp_req.src0.w = src_info->var.xres;
914         ipp_req.src0.h = src_info->var.yres;
915         ipp_req.src_vir_w = src_info->var.xres_virtual;
916         ipp_req.src0.fmt = ipp_fmt;
917
918         ipp_req.dst0.YrgbMst = dst_info->fix.smem_start + offset;
919         ipp_req.dst0.w = dst_w;
920         ipp_req.dst0.h = dst_h;
921         ipp_req.dst_vir_w = dst_vir_w;
922         ipp_req.dst0.fmt = ipp_fmt;
923
924         ipp_req.timeout = 100;
925         ipp_req.flag = rotation;
926         ipp_blit_sync(&ipp_req);
927 }
928
929 #endif
930
931 #if defined(CONFIG_ROCKCHIP_RGA) || defined(CONFIG_ROCKCHIP_RGA2)
932 static int get_rga_format(int fmt)
933 {
934         int rga_fmt = 0;
935
936         switch (fmt) {
937         case XBGR888:
938                 rga_fmt = RK_FORMAT_RGBX_8888;
939                 break;
940         case ABGR888:
941                 rga_fmt = RK_FORMAT_RGBA_8888;
942                 break;
943         case ARGB888:
944                 rga_fmt = RK_FORMAT_BGRA_8888;
945                 break;
946         case RGB888:
947                 rga_fmt = RK_FORMAT_RGB_888;
948                 break;
949         case RGB565:
950                 rga_fmt = RK_FORMAT_RGB_565;
951                 break;
952         case YUV422:
953                 rga_fmt = RK_FORMAT_YCbCr_422_SP;
954                 break;
955         case YUV420:
956                 rga_fmt = RK_FORMAT_YCbCr_420_SP;
957                 break;
958         default:
959                 rga_fmt = RK_FORMAT_RGBA_8888;
960                 break;
961         }
962
963         return rga_fmt;
964 }
965
966 static void rga_win_check(struct rk_lcdc_win *dst_win,
967                           struct rk_lcdc_win *src_win)
968 {
969         int align32 = 4;
970
971         align32 -= 1;
972         /*width and height must be aligned by 32 bit */
973         if ((src_win->area[0].xact & align32) != 0)
974                 src_win->area[0].xact =
975                     (src_win->area[0].xact + align32) & (~align32);
976         if ((src_win->area[0].yact & align32) != 0)
977                 src_win->area[0].yact =
978                     (src_win->area[0].yact + align32) & (~align32);
979         if (src_win->area[0].xvir < src_win->area[0].xact)
980                 src_win->area[0].xvir = src_win->area[0].xact;
981         if (src_win->area[0].yvir < src_win->area[0].yact)
982                 src_win->area[0].yvir = src_win->area[0].yact;
983
984         if ((dst_win->area[0].xact & align32) != 0)
985                 dst_win->area[0].xact =
986                     (dst_win->area[0].xact + align32) & (~align32);
987         if ((dst_win->area[0].yact & align32) != 0)
988                 dst_win->area[0].yact =
989                     (dst_win->area[0].yact + align32) & (~align32);
990         if (dst_win->area[0].xvir < dst_win->area[0].xact)
991                 dst_win->area[0].xvir = dst_win->area[0].xact;
992         if (dst_win->area[0].yvir < dst_win->area[0].yact)
993                 dst_win->area[0].yvir = dst_win->area[0].yact;
994 }
995
996 static void win_copy_by_rga(struct rk_lcdc_win *dst_win,
997                             struct rk_lcdc_win *src_win,
998                             u16 orientation, int iommu_en)
999 {
1000         struct rga_req Rga_Request;
1001         long ret = 0;
1002         /* int fd = 0; */
1003
1004         memset(&Rga_Request, 0, sizeof(Rga_Request));
1005         rga_win_check(dst_win, src_win);
1006
1007         switch (orientation) {
1008         case ROTATE_90:
1009                 Rga_Request.rotate_mode = 1;
1010                 Rga_Request.sina = 65536;
1011                 Rga_Request.cosa = 0;
1012                 Rga_Request.dst.act_w = dst_win->area[0].yact;
1013                 Rga_Request.dst.act_h = dst_win->area[0].xact;
1014                 Rga_Request.dst.x_offset = dst_win->area[0].xact - 1;
1015                 Rga_Request.dst.y_offset = 0;
1016                 break;
1017         case ROTATE_180:
1018                 Rga_Request.rotate_mode = 1;
1019                 Rga_Request.sina = 0;
1020                 Rga_Request.cosa = -65536;
1021                 Rga_Request.dst.act_w = dst_win->area[0].xact;
1022                 Rga_Request.dst.act_h = dst_win->area[0].yact;
1023                 Rga_Request.dst.x_offset = dst_win->area[0].xact - 1;
1024                 Rga_Request.dst.y_offset = dst_win->area[0].yact - 1;
1025                 break;
1026         case ROTATE_270:
1027                 Rga_Request.rotate_mode = 1;
1028                 Rga_Request.sina = -65536;
1029                 Rga_Request.cosa = 0;
1030                 Rga_Request.dst.act_w = dst_win->area[0].yact;
1031                 Rga_Request.dst.act_h = dst_win->area[0].xact;
1032                 Rga_Request.dst.x_offset = 0;
1033                 Rga_Request.dst.y_offset = dst_win->area[0].yact - 1;
1034                 break;
1035         default:
1036                 Rga_Request.rotate_mode = 0;
1037                 Rga_Request.dst.act_w = dst_win->area[0].xact;
1038                 Rga_Request.dst.act_h = dst_win->area[0].yact;
1039                 Rga_Request.dst.x_offset = dst_win->area[0].xact - 1;
1040                 Rga_Request.dst.y_offset = dst_win->area[0].yact - 1;
1041                 break;
1042         }
1043
1044 /*
1045         fd = ion_share_dma_buf_fd(rk_fb->ion_client, src_win->area[0].ion_hdl);
1046         Rga_Request.src.yrgb_addr = fd;
1047         fd = ion_share_dma_buf_fd(rk_fb->ion_client, dst_win->area[0].ion_hdl);
1048         Rga_Request.dst.yrgb_addr = fd;
1049 */
1050         Rga_Request.src.yrgb_addr = 0;
1051         Rga_Request.src.uv_addr =
1052             src_win->area[0].smem_start + src_win->area[0].y_offset;
1053         Rga_Request.src.v_addr = 0;
1054
1055         Rga_Request.dst.yrgb_addr = 0;
1056         Rga_Request.dst.uv_addr =
1057             dst_win->area[0].smem_start + dst_win->area[0].y_offset;
1058         Rga_Request.dst.v_addr = 0;
1059
1060         Rga_Request.src.vir_w = src_win->area[0].xvir;
1061         Rga_Request.src.vir_h = src_win->area[0].yvir;
1062         Rga_Request.src.format = get_rga_format(src_win->format);
1063         Rga_Request.src.act_w = src_win->area[0].xact;
1064         Rga_Request.src.act_h = src_win->area[0].yact;
1065         Rga_Request.src.x_offset = 0;
1066         Rga_Request.src.y_offset = 0;
1067
1068         Rga_Request.dst.vir_w = dst_win->area[0].xvir;
1069         Rga_Request.dst.vir_h = dst_win->area[0].yvir;
1070         Rga_Request.dst.format = get_rga_format(dst_win->format);
1071
1072         Rga_Request.clip.xmin = 0;
1073         Rga_Request.clip.xmax = dst_win->area[0].xact - 1;
1074         Rga_Request.clip.ymin = 0;
1075         Rga_Request.clip.ymax = dst_win->area[0].yact - 1;
1076         Rga_Request.scale_mode = 0;
1077 #if defined(CONFIG_ROCKCHIP_IOMMU)
1078         if (iommu_en) {
1079                 Rga_Request.mmu_info.mmu_en = 1;
1080                 Rga_Request.mmu_info.mmu_flag = 1;
1081         } else {
1082                 Rga_Request.mmu_info.mmu_en = 0;
1083                 Rga_Request.mmu_info.mmu_flag = 0;
1084         }
1085 #else
1086         Rga_Request.mmu_info.mmu_en = 0;
1087         Rga_Request.mmu_info.mmu_flag = 0;
1088 #endif
1089
1090         ret = rga_ioctl_kernel(&Rga_Request);
1091 }
1092
1093 /*
1094  * This function is used for copying fb by RGA Module
1095  * RGA only support copy RGB to RGB
1096  * RGA2 support copy RGB to RGB and YUV to YUV
1097  */
1098 static void fb_copy_by_rga(struct fb_info *dst_info, struct fb_info *src_info,
1099                            int yrgb_offset)
1100 {
1101         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)src_info->par;
1102         struct rk_lcdc_driver *ext_dev_drv =
1103             (struct rk_lcdc_driver *)dst_info->par;
1104         int win_id = 0, ext_win_id;
1105         struct rk_lcdc_win *src_win, *dst_win;
1106
1107         win_id = dev_drv->ops->fb_get_win_id(dev_drv, src_info->fix.id);
1108         src_win = dev_drv->win[win_id];
1109
1110         ext_win_id =
1111             ext_dev_drv->ops->fb_get_win_id(ext_dev_drv, dst_info->fix.id);
1112         dst_win = ext_dev_drv->win[ext_win_id];
1113
1114         win_copy_by_rga(dst_win, src_win, ext_dev_drv->rotate_mode,
1115                         ext_dev_drv->iommu_enabled);
1116 }
1117
1118 #endif
1119
1120 static int rk_fb_rotate(struct fb_info *dst_info,
1121                         struct fb_info *src_info, int offset)
1122 {
1123 #if defined(CONFIG_RK29_IPP)
1124         fb_copy_by_ipp(dst_info, src_info, offset);
1125 #elif defined(CONFIG_ROCKCHIP_RGA) || defined(CONFIG_ROCKCHIP_RGA2)
1126         fb_copy_by_rga(dst_info, src_info, offset);
1127 #else
1128         return -1;
1129 #endif
1130         return 0;
1131 }
1132
1133 static int rk_fb_win_rotate(struct rk_lcdc_win *dst_win,
1134                             struct rk_lcdc_win *src_win,
1135                             u16 rotate, int iommu_en)
1136 {
1137 #if defined(CONFIG_ROCKCHIP_RGA) || defined(CONFIG_ROCKCHIP_RGA2)
1138         win_copy_by_rga(dst_win, src_win, rotate, iommu_en);
1139 #else
1140         return -1;
1141 #endif
1142         return 0;
1143 }
1144
1145
1146 static int rk_fb_pan_display(struct fb_var_screeninfo *var,
1147                              struct fb_info *info)
1148 {
1149         struct rk_fb *rk_fb = dev_get_drvdata(info->device);
1150         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
1151         struct fb_fix_screeninfo *fix = &info->fix;
1152         struct fb_info *extend_info = NULL;
1153         struct rk_lcdc_driver *extend_dev_drv = NULL;
1154         int win_id = 0, extend_win_id = 0;
1155         struct rk_lcdc_win *extend_win = NULL;
1156         struct rk_lcdc_win *win = NULL;
1157         struct rk_screen *screen = dev_drv->cur_screen;
1158         int fb_id = 0;
1159
1160         u32 xoffset = var->xoffset;
1161         u32 yoffset = var->yoffset;
1162         u32 xvir = var->xres_virtual;
1163         /* u32 yvir = var->yres_virtual; */
1164         /* u8 data_format = var->nonstd&0xff; */
1165
1166         u8 pixel_width;
1167         u32 vir_width_bit;
1168         u32 stride, uv_stride;
1169         u32 stride_32bit_1;
1170         u32 stride_32bit_2;
1171         u16 uv_x_off, uv_y_off, uv_y_act;
1172         u8 is_pic_yuv = 0;
1173 #ifdef CONFIG_ROCKCHIP_IOMMU
1174         ion_phys_addr_t phy_addr;
1175         size_t len = 0;
1176         int ret = 0;
1177 #endif
1178
1179         win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
1180         if (win_id < 0)
1181                 return -ENODEV;
1182         else
1183                 win = dev_drv->win[win_id];
1184
1185         if (rk_fb->disp_mode == DUAL) {
1186                 fb_id = get_extend_fb_id(info);
1187                 extend_info = rk_fb->fb[(rk_fb->num_fb >> 1) + fb_id];
1188                 extend_dev_drv = (struct rk_lcdc_driver *)extend_info->par;
1189                 extend_win_id = dev_drv->ops->fb_get_win_id(extend_dev_drv,
1190                                                            extend_info->fix.id);
1191                 extend_win = extend_dev_drv->win[extend_win_id];
1192         }
1193
1194         pixel_width = rk_fb_pixel_width(win->format);
1195         vir_width_bit = pixel_width * xvir;
1196         /* pixel_width = byte_num * 8 */
1197         stride_32bit_1 = ALIGN_N_TIMES(vir_width_bit, 32) / 8;
1198         stride_32bit_2 = ALIGN_N_TIMES(vir_width_bit * 2, 32) / 8;
1199
1200         switch (win->format) {
1201         case YUV422:
1202         case YUV422_A:
1203                 is_pic_yuv = 1;
1204                 stride = stride_32bit_1;
1205                 uv_stride = stride_32bit_1 >> 1;
1206                 uv_x_off = xoffset >> 1;
1207                 uv_y_off = yoffset;
1208                 fix->line_length = stride;
1209                 uv_y_act = win->area[0].yact >> 1;
1210                 break;
1211         case YUV420:            /* 420sp */
1212         case YUV420_A:
1213                 is_pic_yuv = 1;
1214                 stride = stride_32bit_1;
1215                 uv_stride = stride_32bit_1;
1216                 uv_x_off = xoffset;
1217                 uv_y_off = yoffset >> 1;
1218                 fix->line_length = stride;
1219                 uv_y_act = win->area[0].yact >> 1;
1220                 break;
1221         case YUV444:
1222         case YUV444_A:
1223                 is_pic_yuv = 1;
1224                 stride = stride_32bit_1;
1225                 uv_stride = stride_32bit_2;
1226                 uv_x_off = xoffset * 2;
1227                 uv_y_off = yoffset;
1228                 fix->line_length = stride << 2;
1229                 uv_y_act = win->area[0].yact;
1230                 break;
1231         default:
1232                 stride = stride_32bit_1;        /* default rgb */
1233                 fix->line_length = stride;
1234                 break;
1235         }
1236
1237         /* x y mirror ,jump line */
1238         if (screen->y_mirror == 1) {
1239                 if (screen->interlace == 1) {
1240                         win->area[0].y_offset = yoffset * stride * 2 +
1241                             ((win->area[0].yact - 1) * 2 + 1) * stride +
1242                             xoffset * pixel_width / 8;
1243                 } else {
1244                         win->area[0].y_offset = yoffset * stride +
1245                             (win->area[0].yact - 1) * stride +
1246                             xoffset * pixel_width / 8;
1247                 }
1248         } else {
1249                 if (screen->interlace == 1) {
1250                         win->area[0].y_offset =
1251                             yoffset * stride * 2 + xoffset * pixel_width / 8;
1252                 } else {
1253                         win->area[0].y_offset =
1254                             yoffset * stride + xoffset * pixel_width / 8;
1255                 }
1256         }
1257         if (is_pic_yuv == 1) {
1258                 if (screen->y_mirror == 1) {
1259                         if (screen->interlace == 1) {
1260                                 win->area[0].c_offset =
1261                                     uv_y_off * uv_stride * 2 +
1262                                     ((uv_y_act - 1) * 2 + 1) * uv_stride +
1263                                     uv_x_off * pixel_width / 8;
1264                         } else {
1265                                 win->area[0].c_offset = uv_y_off * uv_stride +
1266                                     (uv_y_act - 1) * uv_stride +
1267                                     uv_x_off * pixel_width / 8;
1268                         }
1269                 } else {
1270                         if (screen->interlace == 1) {
1271                                 win->area[0].c_offset =
1272                                     uv_y_off * uv_stride * 2 +
1273                                     uv_x_off * pixel_width / 8;
1274                         } else {
1275                                 win->area[0].c_offset =
1276                                     uv_y_off * uv_stride +
1277                                     uv_x_off * pixel_width / 8;
1278                         }
1279                 }
1280         }
1281
1282         win->area[0].smem_start = fix->smem_start;
1283         win->area[0].cbr_start = fix->mmio_start;
1284         win->area[0].state = 1;
1285         win->area_num = 1;
1286         win->state = 1;
1287
1288         dev_drv->ops->pan_display(dev_drv, win_id);
1289         if (rk_fb->disp_mode == DUAL) {
1290                 if (extend_win->state &&
1291                     (hdmi_switch_complete & (1 << extend_win_id))) {
1292                         extend_win->area[0].y_offset = win->area[0].y_offset;
1293                         if (extend_dev_drv->rotate_mode > X_Y_MIRROR) {
1294                                 rk_fb_rotate(extend_info, info,
1295                                                 win->area[0].y_offset);
1296                         } else {
1297 #ifdef CONFIG_ROCKCHIP_IOMMU
1298                                 if (extend_dev_drv->iommu_enabled) {
1299                                         ret = ion_map_iommu(extend_dev_drv->dev,
1300                                                             rk_fb->ion_client,
1301                                                             win->area[0].ion_hdl,
1302                                                             (unsigned long *)&phy_addr,
1303                                                             (unsigned long *)&len);
1304                                         if (ret < 0) {
1305                                                 dev_err(extend_dev_drv->dev, "ion map to get phy addr failed\n");
1306                                                 ion_free(rk_fb->ion_client, win->area[0].ion_hdl);
1307                                                 return -ENOMEM;
1308                                         }
1309                                         extend_win->area[0].smem_start = phy_addr;
1310                                         extend_win->area[0].cbr_start =
1311                                                         win->area[0].cbr_start;
1312                                 } else {
1313                                         extend_win->area[0].smem_start =
1314                                                         win->area[0].smem_start;
1315                                         extend_win->area[0].cbr_start =
1316                                                         win->area[0].cbr_start;
1317                                 }
1318 #else
1319                                 extend_win->area[0].smem_start =
1320                                                 win->area[0].smem_start;
1321                                 extend_win->area[0].cbr_start =
1322                                                 win->area[0].cbr_start;
1323 #endif
1324                         }
1325                         extend_dev_drv->ops->pan_display(extend_dev_drv,
1326                                                          extend_win_id);
1327                 }
1328         }
1329 #ifdef  CONFIG_FB_MIRRORING
1330         if (video_data_to_mirroring)
1331                 video_data_to_mirroring(info, NULL);
1332 #endif
1333         dev_drv->ops->cfg_done(dev_drv);
1334         return 0;
1335 }
1336
1337 static int rk_fb_get_list_stat(struct rk_lcdc_driver *dev_drv)
1338 {
1339         int i, j;
1340
1341         i = list_empty(&dev_drv->update_regs_list);
1342         j = list_empty(&saved_list);
1343         return i == j ? 0 : 1;
1344 }
1345
1346 void rk_fd_fence_wait(struct rk_lcdc_driver *dev_drv, struct sync_fence *fence)
1347 {
1348         int err = sync_fence_wait(fence, 1000);
1349
1350         if (err >= 0)
1351                 return;
1352
1353         if (err == -ETIME)
1354                 err = sync_fence_wait(fence, 10 * MSEC_PER_SEC);
1355
1356         if (err < 0)
1357                 printk("error waiting on fence\n");
1358 }
1359
1360 static int rk_fb_copy_from_loader(struct fb_info *info)
1361 {
1362         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
1363         void *dst = info->screen_base;
1364         u32 dsp_addr[4];
1365         u32 src;
1366         u32 i;
1367         struct rk_lcdc_win *win = dev_drv->win[0];
1368         u32 size = (win->area[0].xact) * (win->area[0].yact) << 2;
1369         dev_drv->ops->get_dsp_addr(dev_drv, dsp_addr);
1370         src = dsp_addr[0];
1371         dev_info(info->dev, "copy fb data %d x %d  from  dst_addr:%08x\n",
1372                  win->area[0].xact, win->area[0].yact, src);
1373         for (i = 0; i < size; i += PAGE_SIZE) {
1374                 void *page = phys_to_page(i + src);
1375                 void *from_virt = kmap(page);
1376                 void *to_virt = dst + i;
1377                 memcpy(to_virt, from_virt, PAGE_SIZE);
1378         }
1379         return 0;
1380 }
1381
1382 #ifdef CONFIG_ROCKCHIP_IOMMU
1383 static int g_last_addr[4];
1384 int g_last_timeout;
1385 u32 freed_addr[10];
1386 u32 freed_index;
1387
1388 #define DUMP_CHUNK 256
1389 char buf[PAGE_SIZE];
1390
1391 int rk_fb_sysmmu_fault_handler(struct device *dev,
1392                                enum rk_iommu_inttype itype,
1393                                unsigned long pgtable_base,
1394                                unsigned long fault_addr, unsigned int status)
1395 {
1396         struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
1397         int i = 0;
1398         static int page_fault_cnt;
1399         if ((page_fault_cnt++) >= 10)
1400                 return 0;
1401         pr_err
1402             ("PAGE FAULT occurred at 0x%lx (Page table base: 0x%lx),status=%d\n",
1403              fault_addr, pgtable_base, status);
1404         printk("last config addr:\n" "win0:0x%08x\n" "win1:0x%08x\n"
1405                "win2:0x%08x\n" "win3:0x%08x\n", g_last_addr[0], g_last_addr[1],
1406                g_last_addr[2], g_last_addr[3]);
1407         printk("last freed buffer:\n");
1408         for (i = 0; (freed_addr[i] != 0xfefefefe) && freed_addr[i]; i++)
1409                 printk("%d:0x%08x\n", i, freed_addr[i]);
1410         printk("last timeout:%d\n", g_last_timeout);
1411         dev_drv->ops->get_disp_info(dev_drv, buf, 0);
1412         for (i = 0; i < PAGE_SIZE; i += DUMP_CHUNK) {
1413                 if ((PAGE_SIZE - i) > DUMP_CHUNK) {
1414                         char c = buf[i + DUMP_CHUNK];
1415                         buf[i + DUMP_CHUNK] = 0;
1416                         pr_cont("%s", buf + i);
1417                         buf[i + DUMP_CHUNK] = c;
1418                 } else {
1419                         buf[PAGE_SIZE - 1] = 0;
1420                         pr_cont("%s", buf + i);
1421                 }
1422         }
1423
1424         return 0;
1425 }
1426 #endif
1427
1428 void rk_fb_free_dma_buf(struct rk_lcdc_driver *dev_drv,
1429                         struct rk_fb_reg_win_data *reg_win_data)
1430 {
1431         int i, index_buf;
1432         struct rk_fb_reg_area_data *area_data;
1433         struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
1434 #if defined(CONFIG_ROCKCHIP_IOMMU)
1435         struct rk_lcdc_driver *ext_dev_drv = rk_get_extend_lcdc_drv();
1436 #endif
1437
1438         for (i = 0; i < reg_win_data->area_num; i++) {
1439                 area_data = &reg_win_data->reg_area_data[i];
1440                 index_buf = area_data->index_buf;
1441 #if defined(CONFIG_ROCKCHIP_IOMMU)
1442                 if (dev_drv->iommu_enabled) {
1443                         ion_unmap_iommu(dev_drv->dev, rk_fb->ion_client,
1444                                         area_data->ion_handle);
1445                         freed_addr[freed_index++] = area_data->smem_start;
1446                 }
1447                 if (rk_fb->disp_mode == DUAL && hdmi_switch_complete == 0xff) {
1448                         if (ext_dev_drv->iommu_enabled)
1449                                 ion_unmap_iommu(ext_dev_drv->dev,
1450                                                 rk_fb->ion_client,
1451                                                 area_data->ion_handle);
1452                 }
1453 #endif
1454                 if (area_data->ion_handle != NULL) {
1455                         ion_unmap_kernel(rk_fb->ion_client,
1456                                          area_data->ion_handle);
1457                         ion_free(rk_fb->ion_client, area_data->ion_handle);
1458                 }
1459                 if (area_data->acq_fence)
1460                         sync_fence_put(area_data->acq_fence);
1461         }
1462         memset(reg_win_data, 0, sizeof(struct rk_fb_reg_win_data));
1463 }
1464
1465 static void rk_fb_update_driver(struct rk_lcdc_driver *dev_drv,
1466                                 struct rk_lcdc_win *win,
1467                                 struct rk_fb_reg_win_data *reg_win_data)
1468 {
1469         int i = 0;
1470         struct rk_fb *inf = platform_get_drvdata(fb_pdev);
1471         struct rk_screen *cur_screen;
1472         struct rk_screen primary_screen;
1473
1474         if (unlikely(!inf) || unlikely(!dev_drv) ||
1475             unlikely(!win) || unlikely(!reg_win_data))
1476                 return;
1477
1478         cur_screen = dev_drv->cur_screen;
1479         rk_fb_get_prmry_screen(&primary_screen);
1480
1481         win->area_num = reg_win_data->area_num;
1482         win->format = reg_win_data->data_format;
1483         win->id = reg_win_data->win_id;
1484         win->z_order = reg_win_data->z_order;
1485
1486         if (reg_win_data->reg_area_data[0].smem_start > 0) {
1487                 win->state = 1;
1488                 win->area_num = reg_win_data->area_num;
1489                 win->format = reg_win_data->data_format;
1490                 win->id = reg_win_data->win_id;
1491                 win->z_order = reg_win_data->z_order;
1492                 win->area[0].uv_vir_stride =
1493                     reg_win_data->reg_area_data[0].uv_vir_stride;
1494                 win->area[0].cbr_start =
1495                     reg_win_data->reg_area_data[0].cbr_start;
1496                 win->area[0].c_offset = reg_win_data->reg_area_data[0].c_offset;
1497                 win->alpha_en = reg_win_data->alpha_en;
1498                 win->alpha_mode = reg_win_data->alpha_mode;
1499                 win->g_alpha_val = reg_win_data->g_alpha_val;
1500                 for (i = 0; i < RK_WIN_MAX_AREA; i++) {
1501                         if (reg_win_data->reg_area_data[i].smem_start > 0) {
1502                                 win->area[i].ion_hdl =
1503                                         reg_win_data->reg_area_data[i].ion_handle;
1504                                 win->area[i].smem_start =
1505                                         reg_win_data->reg_area_data[i].smem_start;
1506                                 if (inf->disp_mode == DUAL) {
1507                                         win->area[i].xpos =
1508                                                 reg_win_data->reg_area_data[i].xpos;
1509                                         win->area[i].ypos =
1510                                                 reg_win_data->reg_area_data[i].ypos;
1511                                         win->area[i].xsize =
1512                                                 reg_win_data->reg_area_data[i].xsize;
1513                                         win->area[i].ysize =
1514                                                 reg_win_data->reg_area_data[i].ysize;
1515                                 } else {
1516                                         win->area[i].xpos =
1517                                                 reg_win_data->reg_area_data[i].xpos *
1518                                                 cur_screen->mode.xres /
1519                                                 primary_screen.mode.xres;
1520                                         win->area[i].ypos =
1521                                                 reg_win_data->reg_area_data[i].ypos *
1522                                                 cur_screen->mode.yres /
1523                                                 primary_screen.mode.yres;
1524                                         win->area[i].xsize =
1525                                                 reg_win_data->reg_area_data[i].xsize *
1526                                                 cur_screen->mode.xres /
1527                                                 primary_screen.mode.xres;
1528                                         win->area[i].ysize =
1529                                                 reg_win_data->reg_area_data[i].ysize *
1530                                                 cur_screen->mode.yres /
1531                                                 primary_screen.mode.yres;
1532                                 }
1533                                 win->area[i].xact =
1534                                     reg_win_data->reg_area_data[i].xact;
1535                                 win->area[i].yact =
1536                                     reg_win_data->reg_area_data[i].yact;
1537                                 win->area[i].xvir =
1538                                     reg_win_data->reg_area_data[i].xvir;
1539                                 win->area[i].yvir =
1540                                     reg_win_data->reg_area_data[i].yvir;
1541                                 win->area[i].y_offset =
1542                                     reg_win_data->reg_area_data[i].y_offset;
1543                                 win->area[i].y_vir_stride =
1544                                     reg_win_data->reg_area_data[i].y_vir_stride;
1545                                 win->area[i].state = 1;
1546                         } else {
1547                                 win->area[i].state = 0;
1548                         }
1549                 }
1550         } else {
1551         /*
1552                 win->state = 0;
1553                 win->z_order = -1;
1554         */
1555         }
1556 }
1557
1558 static int rk_fb_update_hdmi_win(struct rk_lcdc_win *ext_win,
1559                                  struct rk_lcdc_win *win)
1560 {
1561         struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
1562         struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
1563         struct rk_lcdc_driver *ext_dev_drv = rk_get_extend_lcdc_drv();
1564         struct rk_screen *screen;
1565         struct rk_screen *ext_screen;
1566         int hdmi_xsize;
1567         int hdmi_ysize;
1568         int pixel_width, vir_width_bit, y_stride;
1569         struct rk_lcdc_win *last_win;
1570         bool is_yuv = false;
1571         static u8 fb_index = 0;
1572 #ifdef CONFIG_ROCKCHIP_IOMMU
1573         ion_phys_addr_t phy_addr;
1574         size_t len = 0;
1575         int ret = 0;
1576 #endif
1577         if ((rk_fb->disp_mode != DUAL) ||
1578                 (hdmi_get_hotplug() != HDMI_HPD_ACTIVED) ||
1579                 (hdmi_switch_complete != 0xff)) {
1580                 printk(KERN_INFO "%s: hdmi is disconnect!\n", __func__);
1581                 return -1;
1582         }
1583
1584         if (unlikely(!dev_drv) || unlikely(!ext_dev_drv))
1585                 return -1;
1586
1587         screen = dev_drv->cur_screen;
1588         ext_screen = ext_dev_drv->cur_screen;
1589         hdmi_xsize = ext_screen->xsize;
1590         hdmi_ysize = ext_screen->ysize;
1591
1592         ext_win->state = win->state;
1593         ext_win->id = win->id;
1594         if (win->area[0].smem_start == 0)
1595                 ext_win->state = 0;
1596         if (ext_win->state == 0)
1597                 return 0;
1598
1599         ext_win->area_num = win->area_num;
1600         ext_win->format = win->format;
1601         ext_win->z_order = win->z_order;
1602         ext_win->alpha_en = win->alpha_en;
1603         ext_win->alpha_mode = win->alpha_mode;
1604         ext_win->g_alpha_val = win->g_alpha_val;
1605
1606         /* win0 and win1 have only one area and support scale
1607          * but win2 and win3 don't support scale
1608          * so hdmi only use win0 or win1
1609          */
1610         ext_win->area[0].state = win->area[0].state;
1611
1612         switch (ext_win->format) {
1613         case YUV422:
1614         case YUV420:
1615         case YUV444:
1616         case YUV422_A:
1617         case YUV420_A:
1618         case YUV444_A:
1619                 is_yuv = true;
1620                 break;
1621         default:
1622                 is_yuv = false;
1623                 break;
1624         }
1625
1626         if (ext_dev_drv->rotate_mode == ROTATE_90 ||
1627                         ext_dev_drv->rotate_mode == ROTATE_270) {
1628                 if (ext_win->id == 0) {
1629                         ext_win->area[0].smem_start =
1630                                 rk_fb->fb[rk_fb->num_fb >> 1]->fix.smem_start;
1631                         ext_win->area[0].y_offset = (get_rotate_fb_size() >> 1) * fb_index;
1632                         if ((++fb_index) > 1)
1633                                 fb_index = 0;
1634                 } else {
1635                         ext_win->area[0].y_offset = 0;
1636                         last_win = ext_dev_drv->win[ext_win->id - 1];
1637                         if (last_win->area[0].cbr_start)
1638                                 ext_win->area[0].smem_start =
1639                                         last_win->area[0].cbr_start +
1640                                         last_win->area[0].c_offset +
1641                                         last_win->area[0].xvir * last_win->area[0].yvir;
1642                         else
1643                                 ext_win->area[0].smem_start =
1644                                         last_win->area[0].smem_start +
1645                                         last_win->area[0].y_offset +
1646                                         last_win->area[0].xvir * last_win->area[0].yvir;
1647                 }
1648
1649                 ext_win->area[0].xact = win->area[0].yact;
1650                 ext_win->area[0].yact = win->area[0].xact;
1651                 ext_win->area[0].xvir = win->area[0].yact;
1652                 ext_win->area[0].yvir = win->area[0].xact;
1653                 pixel_width = rk_fb_pixel_width(ext_win->format);
1654                 vir_width_bit = pixel_width * ext_win->area[0].xvir;
1655                 y_stride = ALIGN_N_TIMES(vir_width_bit, 32) / 8;
1656                 ext_win->area[0].y_vir_stride = y_stride >> 2;
1657         } else {
1658 #ifdef CONFIG_ROCKCHIP_IOMMU
1659                 if (ext_dev_drv->iommu_enabled) {
1660                         ret = ion_map_iommu(ext_dev_drv->dev,
1661                                             rk_fb->ion_client,
1662                                             win->area[0].ion_hdl,
1663                                             (unsigned long *)&phy_addr,
1664                                             (unsigned long *)&len);
1665                         if (ret < 0) {
1666                                 dev_err(ext_dev_drv->dev, "ion map to get phy addr failed\n");
1667                                 ion_free(rk_fb->ion_client, win->area[0].ion_hdl);
1668                                 return -ENOMEM;
1669                         }
1670                         ext_win->area[0].smem_start = phy_addr;
1671                 } else {
1672                         ext_win->area[0].smem_start = win->area[0].smem_start;
1673                 }
1674 #else
1675                 ext_win->area[0].smem_start = win->area[0].smem_start;
1676 #endif
1677                 ext_win->area[0].y_offset = win->area[0].y_offset;
1678                 ext_win->area[0].xact = win->area[0].xact;
1679                 ext_win->area[0].yact = win->area[0].yact;
1680                 ext_win->area[0].xvir = win->area[0].xvir;
1681                 ext_win->area[0].yvir = win->area[0].yvir;
1682                 ext_win->area[0].y_vir_stride = win->area[0].y_vir_stride;
1683         }
1684
1685         if (win->area[0].xpos != 0 || win->area[0].ypos != 0) {
1686                 if (ext_dev_drv->rotate_mode == ROTATE_270) {
1687                         int xbom_pos = 0, ybom_pos = 0;
1688                         int xtop_pos = 0, ytop_pos = 0;
1689
1690                         ext_win->area[0].xsize = hdmi_xsize *
1691                                                 win->area[0].ysize /
1692                                                 screen->mode.yres;
1693                         ext_win->area[0].ysize = hdmi_ysize *
1694                                                 win->area[0].xsize /
1695                                                 screen->mode.xres;
1696                         xbom_pos = hdmi_xsize * win->area[0].ypos / screen->mode.yres;
1697                         ybom_pos = hdmi_ysize * win->area[0].xpos / screen->mode.xres;
1698                         xtop_pos = hdmi_xsize - ext_win->area[0].xsize - xbom_pos;
1699                         ytop_pos = hdmi_ysize - ext_win->area[0].ysize - ybom_pos;
1700                         ext_win->area[0].xpos =
1701                                 ((ext_screen->mode.xres - hdmi_xsize) >> 1) + xtop_pos;
1702                         ext_win->area[0].ypos =
1703                                 ((ext_screen->mode.yres - hdmi_ysize) >> 1) + ytop_pos;
1704                 } else if (ext_dev_drv->rotate_mode == ROTATE_90) {
1705                         ext_win->area[0].xsize = hdmi_xsize *
1706                                                 win->area[0].ysize /
1707                                                 screen->mode.yres;
1708                         ext_win->area[0].ysize = hdmi_ysize *
1709                                                 win->area[0].xsize /
1710                                                 screen->mode.xres;
1711                         ext_win->area[0].xpos =
1712                                 ((ext_screen->mode.xres - hdmi_xsize) >> 1) +
1713                                 hdmi_xsize * win->area[0].ypos / screen->mode.yres;
1714                         ext_win->area[0].ypos =
1715                                 ((ext_screen->mode.yres - hdmi_ysize) >> 1) +
1716                                 hdmi_ysize * win->area[0].xpos / screen->mode.xres;
1717                 } else {
1718                         ext_win->area[0].xsize = hdmi_xsize *
1719                                                 win->area[0].xsize /
1720                                                 screen->mode.xres;
1721                         ext_win->area[0].ysize = hdmi_ysize *
1722                                                 win->area[0].ysize /
1723                                                 screen->mode.yres;
1724                         ext_win->area[0].xpos =
1725                                 ((ext_screen->mode.xres - hdmi_xsize) >> 1) +
1726                                 hdmi_xsize * win->area[0].xpos / screen->mode.xres;
1727                         ext_win->area[0].ypos =
1728                                 ((ext_screen->mode.yres - hdmi_ysize) >> 1) +
1729                                 hdmi_ysize * win->area[0].ypos / screen->mode.yres;
1730                 }
1731         } else {
1732                 ext_win->area[0].xsize = hdmi_xsize;
1733                 ext_win->area[0].ysize = hdmi_ysize;
1734                 ext_win->area[0].xpos =
1735                                 (ext_screen->mode.xres - hdmi_xsize) >> 1;
1736                 ext_win->area[0].ypos =
1737                                 (ext_screen->mode.yres - hdmi_ysize) >> 1;
1738         }
1739
1740         if (!is_yuv) {
1741                 ext_win->area[0].uv_vir_stride = 0;
1742                 ext_win->area[0].cbr_start = 0;
1743                 ext_win->area[0].c_offset = 0;
1744                 return 0;
1745         }
1746
1747         if (ext_dev_drv->rotate_mode == ROTATE_90 ||
1748                 ext_dev_drv->rotate_mode == ROTATE_270) {
1749                 ext_win->area[0].uv_vir_stride = ext_win->area[0].y_vir_stride;
1750                 ext_win->area[0].cbr_start = ext_win->area[0].smem_start +
1751                                 ext_win->area[0].y_offset +
1752                                 ext_win->area[0].xvir * ext_win->area[0].yvir;
1753                 ext_win->area[0].c_offset = win->area[0].c_offset;
1754         } else {
1755                 ext_win->area[0].uv_vir_stride = win->area[0].uv_vir_stride;
1756 #ifdef CONFIG_ROCKCHIP_IOMMU
1757                 if (ext_dev_drv->iommu_enabled)
1758                         ext_win->area[0].cbr_start =
1759                                 ext_win->area[0].smem_start +
1760                                 ext_win->area[0].y_offset +
1761                                 ext_win->area[0].xvir * ext_win->area[0].yvir;
1762                 else
1763                         ext_win->area[0].cbr_start = win->area[0].cbr_start;
1764 #else
1765                 ext_win->area[0].cbr_start = win->area[0].cbr_start;
1766 #endif
1767                 ext_win->area[0].c_offset = win->area[0].c_offset;
1768         }
1769
1770         return 0;
1771 }
1772
1773 static struct rk_fb_reg_win_data *rk_fb_get_win_data(struct rk_fb_reg_data
1774                                                      *regs, int win_id)
1775 {
1776         int i;
1777         struct rk_fb_reg_win_data *win_data = NULL;
1778         for (i = 0; i < regs->win_num; i++) {
1779                 if (regs->reg_win_data[i].win_id == win_id) {
1780                         win_data = &(regs->reg_win_data[i]);
1781                         break;
1782                 }
1783         }
1784
1785         return win_data;
1786 }
1787
1788 static void rk_fb_update_reg(struct rk_lcdc_driver *dev_drv,
1789                              struct rk_fb_reg_data *regs)
1790 {
1791         int i, j;
1792         struct rk_lcdc_win *win;
1793         ktime_t timestamp = dev_drv->vsync_info.timestamp;
1794         struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
1795         struct rk_lcdc_driver *ext_dev_drv;
1796         struct rk_lcdc_win *ext_win;
1797         struct rk_fb_reg_win_data *win_data;
1798         bool wait_for_vsync;
1799         int count = 100;
1800         unsigned int dsp_addr[4];
1801         long timeout;
1802
1803         /* acq_fence wait */
1804         for (i = 0; i < regs->win_num; i++) {
1805                 win_data = &regs->reg_win_data[i];
1806                 for (j = 0; j < RK_WIN_MAX_AREA; j++) {
1807                         if (win_data->reg_area_data[j].acq_fence) {
1808                                 /* printk("acq_fence wait!!!!!\n"); */
1809                                 rk_fd_fence_wait(dev_drv, win_data->reg_area_data[j].acq_fence);
1810                         }
1811                 }
1812         }
1813
1814         for (i = 0; i < dev_drv->lcdc_win_num; i++) {
1815                 win = dev_drv->win[i];
1816                 win_data = rk_fb_get_win_data(regs, i);
1817                 if (win_data) {
1818                         rk_fb_update_driver(dev_drv, win, win_data);
1819                         win->state = 1;
1820                         dev_drv->ops->set_par(dev_drv, i);
1821                         dev_drv->ops->pan_display(dev_drv, i);
1822 #if defined(CONFIG_ROCKCHIP_IOMMU)
1823                         if (dev_drv->iommu_enabled) {
1824                                 g_last_addr[i] = win_data->reg_area_data[0].smem_start +
1825                                         win_data->reg_area_data[0].y_offset;
1826                         }
1827 #endif
1828                 } else {
1829                         win->z_order = -1;
1830                         win->state = 0;
1831                 }
1832         }
1833         dev_drv->ops->ovl_mgr(dev_drv, 0, 1);
1834
1835         if ((rk_fb->disp_mode == DUAL)
1836             && (hdmi_get_hotplug() == HDMI_HPD_ACTIVED)
1837             && (hdmi_switch_complete == 0xff)) {
1838                 ext_dev_drv = rk_get_extend_lcdc_drv();
1839                 if (!ext_dev_drv) {
1840                         printk(KERN_ERR "hdmi lcdc driver not found!\n");
1841                         goto ext_win_exit;
1842                 }
1843
1844                 /* win0 and win1 have only one area and support scale
1845                  * but win2 and win3 don't support scale
1846                  * so hdmi only use win0 or win1
1847                  */
1848                 for (i = 0; i < 2; i++) {
1849                         win = dev_drv->win[i];
1850                         ext_win = ext_dev_drv->win[i];
1851                         ext_win->state = win->state;
1852                         if (!ext_win->state)
1853                                 continue;
1854
1855                         rk_fb_update_hdmi_win(ext_win, win);
1856
1857                         if (ext_dev_drv->rotate_mode > X_Y_MIRROR)
1858                                 rk_fb_win_rotate(ext_win, win,
1859                                                  ext_dev_drv->rotate_mode,
1860                                                  ext_dev_drv->iommu_enabled);
1861
1862                         ext_dev_drv->ops->set_par(ext_dev_drv, i);
1863                         ext_dev_drv->ops->pan_display(ext_dev_drv, i);
1864                 }
1865
1866                 ext_dev_drv->ops->cfg_done(ext_dev_drv);
1867         }
1868 ext_win_exit:
1869         dev_drv->ops->cfg_done(dev_drv);
1870
1871         do {
1872                 timestamp = dev_drv->vsync_info.timestamp;
1873                 timeout = wait_event_interruptible_timeout(dev_drv->vsync_info.wait,
1874                                 ktime_compare(dev_drv->vsync_info.timestamp, timestamp) > 0,
1875                                 msecs_to_jiffies(25));
1876                 dev_drv->ops->get_dsp_addr(dev_drv, dsp_addr);
1877                 wait_for_vsync = false;
1878                 for (i = 0; i < dev_drv->lcdc_win_num; i++) {
1879                         if (dev_drv->win[i]->state == 1) {
1880                                 u32 new_start =
1881                                     dev_drv->win[i]->area[0].smem_start +
1882                                     dev_drv->win[i]->area[0].y_offset;
1883                                 u32 reg_start = dsp_addr[i];
1884
1885                                 if (unlikely(new_start != reg_start)) {
1886                                         wait_for_vsync = true;
1887                                         dev_info(dev_drv->dev,
1888                                                "win%d:new_addr:0x%08x cur_addr:0x%08x--%d\n",
1889                                                i, new_start, reg_start, 101 - count);
1890                                         break;
1891                                 }
1892                         }
1893                 }
1894         } while (wait_for_vsync && count--);
1895 #ifdef H_USE_FENCE
1896         sw_sync_timeline_inc(dev_drv->timeline, 1);
1897 #endif
1898         if (!g_first_buf) {
1899 #if defined(CONFIG_ROCKCHIP_IOMMU)
1900                 if (dev_drv->iommu_enabled) {
1901                         freed_index = 0;
1902                         g_last_timeout = timeout;
1903                 }
1904 #endif
1905                 for (i = 0; i < g_last_win_num; i++)
1906                         rk_fb_free_dma_buf(dev_drv, &g_reg_win_data[i]);
1907
1908 #if defined(CONFIG_ROCKCHIP_IOMMU)
1909                 if (dev_drv->iommu_enabled)
1910                         freed_addr[freed_index] = 0xfefefefe;
1911 #endif
1912         }
1913         for (i = 0; i < regs->win_num; i++) {
1914                 memcpy(&g_reg_win_data[i], &(regs->reg_win_data[i]),
1915                        sizeof(struct rk_fb_reg_win_data));
1916         }
1917         g_last_win_num = regs->win_num;
1918         g_first_buf = 0;
1919 }
1920
1921 static void rk_fb_update_regs_handler(struct kthread_work *work)
1922 {
1923         struct rk_lcdc_driver *dev_drv =
1924             container_of(work, struct rk_lcdc_driver, update_regs_work);
1925         struct rk_fb_reg_data *data, *next;
1926         /* struct list_head saved_list; */
1927
1928         mutex_lock(&dev_drv->update_regs_list_lock);
1929         saved_list = dev_drv->update_regs_list;
1930         list_replace_init(&dev_drv->update_regs_list, &saved_list);
1931         mutex_unlock(&dev_drv->update_regs_list_lock);
1932
1933         list_for_each_entry_safe(data, next, &saved_list, list) {
1934                 rk_fb_update_reg(dev_drv, data);
1935                 list_del(&data->list);
1936                 kfree(data);
1937         }
1938
1939         if (dev_drv->wait_fs && list_empty(&dev_drv->update_regs_list))
1940                 wake_up(&dev_drv->update_regs_wait);
1941 }
1942
1943 static int rk_fb_check_config_var(struct rk_fb_area_par *area_par,
1944                                   struct rk_screen *screen)
1945 {
1946         if ((area_par->x_offset + area_par->xact > area_par->xvir) ||
1947             (area_par->xact <= 0) || (area_par->yact <= 0) ||
1948             (area_par->xvir <= 0) || (area_par->yvir <= 0)) {
1949                 pr_err("check config var fail 0:\n"
1950                        "x_offset=%d,xact=%d,xvir=%d\n",
1951                        area_par->x_offset, area_par->xact, area_par->xvir);
1952                 return -EINVAL;
1953         }
1954
1955         if ((area_par->xpos + area_par->xsize > screen->mode.xres) ||
1956             (area_par->ypos + area_par->ysize > screen->mode.yres) ||
1957             (area_par->xsize <= 0) || (area_par->ysize <= 0)) {
1958                 pr_err("check config var fail 1:\n"
1959                        "xpos=%d,xsize=%d,xres=%d\n"
1960                        "ypos=%d,ysize=%d,yres=%d\n",
1961                        area_par->xpos, area_par->xsize, screen->mode.xres,
1962                        area_par->ypos, area_par->ysize, screen->mode.yres);
1963                 return -EINVAL;
1964         }
1965         return 0;
1966 }
1967
1968 static int rk_fb_set_win_buffer(struct fb_info *info,
1969                                 struct rk_fb_win_par *win_par,
1970                                 struct rk_fb_reg_win_data *reg_win_data)
1971 {
1972         struct rk_fb *rk_fb = dev_get_drvdata(info->device);
1973         struct fb_fix_screeninfo *fix = &info->fix;
1974         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
1975         struct rk_screen *screen = dev_drv->cur_screen;
1976         struct rk_screen primary_screen;
1977         struct fb_info *fbi = rk_fb->fb[0];
1978         int i, ion_fd, acq_fence_fd;
1979         u32 xvir, yvir;
1980         u32 xoffset, yoffset;
1981
1982         struct ion_handle *hdl;
1983         size_t len;
1984         int index_buf;
1985         u8 fb_data_fmt;
1986         u8 pixel_width;
1987         u32 vir_width_bit;
1988         u32 stride, uv_stride;
1989         u32 stride_32bit_1;
1990         u32 stride_32bit_2;
1991         u16 uv_x_off, uv_y_off, uv_y_act;
1992         u8 is_pic_yuv = 0;
1993         u8 ppixel_a = 0, global_a = 0;
1994         ion_phys_addr_t phy_addr;
1995         int ret = 0;
1996
1997         reg_win_data->reg_area_data[0].smem_start = -1;
1998         reg_win_data->area_num = 0;
1999         fbi = rk_fb->fb[reg_win_data->win_id];
2000         if (win_par->area_par[0].phy_addr == 0) {
2001                 for (i = 0; i < RK_WIN_MAX_AREA; i++) {
2002                         ion_fd = win_par->area_par[i].ion_fd;
2003                         if (ion_fd > 0) {
2004                                 hdl =
2005                                     ion_import_dma_buf(rk_fb->ion_client,
2006                                                        ion_fd);
2007                                 if (IS_ERR(hdl)) {
2008                                         pr_info
2009                                             ("%s: Could not import handle: %d\n",
2010                                              __func__, (int)hdl);
2011                                         /*return -EINVAL; */
2012                                         break;
2013                                 }
2014                                 fbi->screen_base =
2015                                     ion_map_kernel(rk_fb->ion_client, hdl);
2016                                 reg_win_data->area_num++;
2017                                 reg_win_data->reg_area_data[i].ion_handle = hdl;
2018 #ifndef CONFIG_ROCKCHIP_IOMMU
2019                                 ret = ion_phys(rk_fb->ion_client, hdl, &phy_addr,
2020                                                 &len);
2021 #else
2022                                 if (dev_drv->iommu_enabled)
2023                                         ret = ion_map_iommu(dev_drv->dev,
2024                                                                 rk_fb->ion_client,
2025                                                                 hdl,
2026                                                                 (unsigned long *)&phy_addr,
2027                                                                 (unsigned long *)&len);
2028                                 else
2029                                         ret = ion_phys(rk_fb->ion_client, hdl,
2030                                                         &phy_addr, &len);
2031 #endif
2032                                 if (ret < 0) {
2033                                         dev_err(fbi->dev, "ion map to get phy addr failed\n");
2034                                         ion_free(rk_fb->ion_client, hdl);
2035                                         return -ENOMEM;
2036                                 }
2037                                 reg_win_data->reg_area_data[i].smem_start = phy_addr;
2038                                 reg_win_data->area_buf_num++;
2039                                 reg_win_data->reg_area_data[i].index_buf = 1;
2040                         }
2041                 }
2042         } else {
2043                 reg_win_data->reg_area_data[0].smem_start =
2044                     win_par->area_par[0].phy_addr;
2045                 reg_win_data->area_num = 1;
2046                 fbi->screen_base = phys_to_virt(win_par->area_par[0].phy_addr);
2047         }
2048
2049         if (reg_win_data->area_num == 0)
2050                 return 0;
2051
2052         for (i = 0; i < reg_win_data->area_num; i++) {
2053                 acq_fence_fd = win_par->area_par[i].acq_fence_fd;
2054                 index_buf = reg_win_data->reg_area_data[i].index_buf;
2055                 if ((acq_fence_fd > 0) && (index_buf == 1)) {
2056                         reg_win_data->reg_area_data[i].acq_fence =
2057                             sync_fence_fdget(win_par->area_par[i].acq_fence_fd);
2058                 }
2059         }
2060         fb_data_fmt = rk_fb_data_fmt(win_par->data_format, 0);
2061         reg_win_data->data_format = fb_data_fmt;
2062         pixel_width = rk_fb_pixel_width(fb_data_fmt);
2063
2064         ppixel_a = ((fb_data_fmt == ARGB888) ||
2065                     (fb_data_fmt == ABGR888)) ? 1 : 0;
2066         global_a = (win_par->g_alpha_val == 0) ? 0 : 1;
2067         reg_win_data->alpha_en = ppixel_a | global_a;
2068         reg_win_data->g_alpha_val = win_par->g_alpha_val;
2069         reg_win_data->alpha_mode = win_par->alpha_mode;
2070         if (reg_win_data->reg_area_data[0].smem_start > 0) {
2071                 reg_win_data->z_order = win_par->z_order;
2072                 reg_win_data->win_id = win_par->win_id;
2073         } else {
2074                 reg_win_data->z_order = -1;
2075                 reg_win_data->win_id = -1;
2076         }
2077
2078         rk_fb_get_prmry_screen(&primary_screen);
2079         for (i = 0; i < reg_win_data->area_num; i++) {
2080                 rk_fb_check_config_var(&win_par->area_par[i], &primary_screen);
2081                 /* visiable pos in panel */
2082                 reg_win_data->reg_area_data[i].xpos = win_par->area_par[i].xpos;
2083                 reg_win_data->reg_area_data[i].ypos = win_par->area_par[i].ypos;
2084
2085                 /* realy size in panel */
2086                 reg_win_data->reg_area_data[i].xsize = win_par->area_par[i].xsize;
2087                 reg_win_data->reg_area_data[i].ysize = win_par->area_par[i].ysize;
2088
2089                 /* realy size in panel */
2090                 reg_win_data->reg_area_data[i].xact = win_par->area_par[i].xact;
2091                 reg_win_data->reg_area_data[i].yact = win_par->area_par[i].yact;
2092
2093                 xoffset = win_par->area_par[i].x_offset;        /* buf offset */
2094                 yoffset = win_par->area_par[i].y_offset;
2095                 xvir = win_par->area_par[i].xvir;
2096                 reg_win_data->reg_area_data[i].xvir = xvir;
2097                 yvir = win_par->area_par[i].yvir;
2098                 reg_win_data->reg_area_data[i].yvir = yvir;
2099
2100                 vir_width_bit = pixel_width * xvir;
2101                 /* pixel_width = byte_num*8 */
2102                 stride_32bit_1 = ((vir_width_bit + 31) & (~31)) / 8;
2103                 stride_32bit_2 = ((vir_width_bit * 2 + 31) & (~31)) / 8;
2104
2105                 stride = stride_32bit_1;        /* default rgb */
2106                 fix->line_length = stride;
2107                 reg_win_data->reg_area_data[i].y_vir_stride = stride >> 2;
2108
2109                 /* x y mirror ,jump line
2110                  * reg_win_data->reg_area_data[i].y_offset =
2111                  *              yoffset*stride+xoffset*pixel_width/8;
2112                  */
2113                 if (screen->y_mirror == 1) {
2114                         if (screen->interlace == 1) {
2115                                 reg_win_data->reg_area_data[i].y_offset =
2116                                     yoffset * stride * 2 +
2117                                     ((reg_win_data->reg_area_data[i].yact - 1) * 2 + 1) * stride +
2118                                     xoffset * pixel_width / 8;
2119                         } else {
2120                                 reg_win_data->reg_area_data[i].y_offset =
2121                                     yoffset * stride +
2122                                     (reg_win_data->reg_area_data[i].yact - 1) * stride +
2123                                     xoffset * pixel_width / 8;
2124                         }
2125                 } else {
2126                         if (screen->interlace == 1) {
2127                                 reg_win_data->reg_area_data[i].y_offset =
2128                                     yoffset * stride * 2 +
2129                                     xoffset * pixel_width / 8;
2130                         } else {
2131                                 reg_win_data->reg_area_data[i].y_offset =
2132                                     yoffset * stride +
2133                                     xoffset * pixel_width / 8;
2134                         }
2135                 }
2136         }
2137         switch (fb_data_fmt) {
2138         case YUV422:
2139         case YUV422_A:
2140                 is_pic_yuv = 1;
2141                 stride = stride_32bit_1;
2142                 uv_stride = stride_32bit_1 >> 1;
2143                 uv_x_off = xoffset >> 1;
2144                 uv_y_off = yoffset;
2145                 fix->line_length = stride;
2146                 uv_y_act = win_par->area_par[0].yact >> 1;
2147                 break;
2148         case YUV420:            /* 420sp */
2149         case YUV420_A:
2150                 is_pic_yuv = 1;
2151                 stride = stride_32bit_1;
2152                 uv_stride = stride_32bit_1;
2153                 uv_x_off = xoffset;
2154                 uv_y_off = yoffset >> 1;
2155                 fix->line_length = stride;
2156                 uv_y_act = win_par->area_par[0].yact >> 1;
2157                 break;
2158         case YUV444:
2159         case YUV444_A:
2160                 is_pic_yuv = 1;
2161                 stride = stride_32bit_1;
2162                 uv_stride = stride_32bit_2;
2163                 uv_x_off = xoffset * 2;
2164                 uv_y_off = yoffset;
2165                 fix->line_length = stride << 2;
2166                 uv_y_act = win_par->area_par[0].yact;
2167                 break;
2168         default:
2169                 break;
2170         }
2171         if (is_pic_yuv == 1) {
2172                 reg_win_data->reg_area_data[0].cbr_start =
2173                     reg_win_data->reg_area_data[0].smem_start + xvir * yvir;
2174                 reg_win_data->reg_area_data[0].uv_vir_stride = uv_stride >> 2;
2175                 if (screen->y_mirror == 1) {
2176                         if (screen->interlace == 1) {
2177                                 reg_win_data->reg_area_data[0].c_offset =
2178                                     uv_y_off * uv_stride * 2 +
2179                                     ((uv_y_act - 1) * 2 + 1) * uv_stride +
2180                                     uv_x_off * pixel_width / 8;
2181                         } else {
2182                                 reg_win_data->reg_area_data[0].c_offset =
2183                                     uv_y_off * uv_stride +
2184                                     (uv_y_act - 1) * uv_stride +
2185                                     uv_x_off * pixel_width / 8;
2186                         }
2187                 } else {
2188                         if (screen->interlace == 1) {
2189                                 reg_win_data->reg_area_data[0].c_offset =
2190                                     uv_y_off * uv_stride * 2 +
2191                                     uv_x_off * pixel_width / 8;
2192                         } else {
2193                                 reg_win_data->reg_area_data[0].c_offset =
2194                                     uv_y_off * uv_stride +
2195                                     uv_x_off * pixel_width / 8;
2196                         }
2197                 }
2198         }
2199         return 0;
2200 }
2201
2202 static int rk_fb_set_win_config(struct fb_info *info,
2203                                 struct rk_fb_win_cfg_data *win_data)
2204 {
2205         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2206         struct rk_fb_reg_data *regs;
2207 #ifdef H_USE_FENCE
2208         struct sync_fence *release_fence[RK_MAX_BUF_NUM];
2209         struct sync_fence *retire_fence;
2210         struct sync_pt *release_sync_pt[RK_MAX_BUF_NUM];
2211         struct sync_pt *retire_sync_pt;
2212         char fence_name[20];
2213 #endif
2214         int ret = 0, i, j = 0;
2215         int list_is_empty = 0;
2216
2217         regs = kzalloc(sizeof(struct rk_fb_reg_data), GFP_KERNEL);
2218         if (!regs) {
2219                 printk(KERN_INFO "could not allocate rk_fb_reg_data\n");
2220                 ret = -ENOMEM;
2221                 return ret;
2222         }
2223
2224 /*
2225         regs->post_cfg.xpos = win_data->post_cfg.xpos;
2226         regs->post_cfg.ypos = win_data->post_cfg.ypos;
2227         regs->post_cfg.xsize = win_data->post_cfg.xsize;
2228         regs->post_cfg.ysize = win_data->post_cfg.xsize;
2229 */
2230
2231         for (i = 0; i < dev_drv->lcdc_win_num; i++) {
2232                 if (win_data->win_par[i].win_id < dev_drv->lcdc_win_num) {
2233                         if (rk_fb_set_win_buffer(info, &win_data->win_par[i],
2234                                                         &regs->reg_win_data[j]))
2235                                 return -ENOMEM;
2236                         if (regs->reg_win_data[j].area_num > 0) {
2237                                 regs->win_num++;
2238                                 regs->buf_num +=
2239                                     regs->reg_win_data[j].area_buf_num;
2240                         }
2241                         j++;
2242                 } else {
2243                         printk(KERN_INFO "error:win_id bigger than lcdc_win_num\n");
2244                         printk(KERN_INFO "i=%d,win_id=%d\n", i,
2245                                win_data->win_par[i].win_id);
2246                 }
2247         }
2248
2249         mutex_lock(&dev_drv->output_lock);
2250         if (!(dev_drv->suspend_flag == 0)) {
2251                 rk_fb_update_reg(dev_drv, regs);
2252                 kfree(regs);
2253                 printk(KERN_INFO "suspend_flag = 1\n");
2254                 goto err;
2255         }
2256
2257         dev_drv->timeline_max++;
2258 #ifdef H_USE_FENCE
2259         for (i = 0; i < RK_MAX_BUF_NUM; i++) {
2260                 if (i < regs->buf_num) {
2261                         sprintf(fence_name, "fence%d", i);
2262                         win_data->rel_fence_fd[i] = get_unused_fd();
2263                         if (win_data->rel_fence_fd[i] < 0) {
2264                                 printk(KERN_INFO "rel_fence_fd=%d\n",
2265                                        win_data->rel_fence_fd[i]);
2266                                 ret = -EFAULT;
2267                                 goto err;
2268                         }
2269                         release_sync_pt[i] =
2270                             sw_sync_pt_create(dev_drv->timeline,
2271                                               dev_drv->timeline_max);
2272                         release_fence[i] =
2273                             sync_fence_create(fence_name, release_sync_pt[i]);
2274                         sync_fence_install(release_fence[i],
2275                                            win_data->rel_fence_fd[i]);
2276                 } else {
2277                         win_data->rel_fence_fd[i] = -1;
2278                 }
2279         }
2280
2281         win_data->ret_fence_fd = get_unused_fd();
2282         if (win_data->ret_fence_fd < 0) {
2283                 printk("ret_fence_fd=%d\n", win_data->ret_fence_fd);
2284                 ret = -EFAULT;
2285                 goto err;
2286         }
2287         retire_sync_pt =
2288             sw_sync_pt_create(dev_drv->timeline, dev_drv->timeline_max);
2289         retire_fence = sync_fence_create("ret_fence", retire_sync_pt);
2290         sync_fence_install(retire_fence, win_data->ret_fence_fd);
2291 #else
2292         for (i = 0; i < RK_MAX_BUF_NUM; i++)
2293                 win_data->rel_fence_fd[i] = -1;
2294
2295         win_data->ret_fence_fd = -1;
2296 #endif
2297         if (dev_drv->wait_fs == 0) {
2298                 mutex_lock(&dev_drv->update_regs_list_lock);
2299                 list_add_tail(&regs->list, &dev_drv->update_regs_list);
2300                 mutex_unlock(&dev_drv->update_regs_list_lock);
2301                 queue_kthread_work(&dev_drv->update_regs_worker,
2302                                    &dev_drv->update_regs_work);
2303         } else {
2304                 mutex_lock(&dev_drv->update_regs_list_lock);
2305                 list_is_empty = list_empty(&dev_drv->update_regs_list) &&
2306                                         list_empty(&saved_list);
2307                 mutex_unlock(&dev_drv->update_regs_list_lock);
2308                 if (!list_is_empty) {
2309                         ret = wait_event_timeout(dev_drv->update_regs_wait,
2310                                 list_empty(&dev_drv->update_regs_list) && list_empty(&saved_list),
2311                                 msecs_to_jiffies(60));
2312                         if (ret > 0)
2313                                 rk_fb_update_reg(dev_drv, regs);
2314                         else
2315                                 printk("%s: wait update_regs_wait timeout\n", __func__);
2316                 } else if (ret == 0) {
2317                         rk_fb_update_reg(dev_drv, regs);
2318                 }
2319                 kfree(regs);
2320         }
2321
2322 err:
2323         mutex_unlock(&dev_drv->output_lock);
2324         return ret;
2325 }
2326
2327 #if 1
2328 static int cfgdone_distlist[10] = { 0 };
2329
2330 static int cfgdone_index;
2331 static int cfgdone_lasttime;
2332
2333 int rk_get_real_fps(int before)
2334 {
2335         struct timespec now;
2336         int dist_curr;
2337         int dist_total = 0;
2338         int dist_count = 0;
2339         int dist_first = 0;
2340
2341         int index = cfgdone_index;
2342         int i = 0, fps = 0;
2343         int total;
2344
2345         if (before > 100)
2346                 before = 100;
2347         if (before < 0)
2348                 before = 0;
2349
2350         getnstimeofday(&now);
2351         dist_curr = (now.tv_sec * 1000000 + now.tv_nsec / 1000) -
2352                         cfgdone_lasttime;
2353         total = dist_curr;
2354         /*
2355            printk("fps: ");
2356          */
2357         for (i = 0; i < 10; i++) {
2358                 if (--index < 0)
2359                         index = 9;
2360                 total += cfgdone_distlist[index];
2361                 if (i == 0)
2362                         dist_first = cfgdone_distlist[index];
2363                 if (total < (before * 1000)) {
2364                         /*
2365                            printk("[%d:%d] ", dist_count, cfgdone_distlist[index]);
2366                          */
2367                         dist_total += cfgdone_distlist[index];
2368                         dist_count++;
2369                 } else {
2370                         break;
2371                 }
2372         }
2373
2374         /*
2375            printk("total %d, count %d, curr %d, ", dist_total, dist_count, dist_curr);
2376          */
2377         dist_curr = (dist_curr > dist_first) ? dist_curr : dist_first;
2378         dist_total += dist_curr;
2379         dist_count++;
2380
2381         if (dist_total > 0)
2382                 fps = (1000000 * dist_count) / dist_total;
2383         else
2384                 fps = 60;
2385
2386         /*
2387            printk("curr2 %d, fps=%d\n", dist_curr, fps);
2388          */
2389         return fps;
2390 }
2391 EXPORT_SYMBOL(rk_get_real_fps);
2392
2393 #endif
2394 #ifdef CONFIG_ROCKCHIP_IOMMU
2395 #define ION_MAX 10
2396 static struct ion_handle *ion_hanle[ION_MAX];
2397 #endif
2398 static int rk_fb_ioctl(struct fb_info *info, unsigned int cmd,
2399                        unsigned long arg)
2400 {
2401         struct rk_fb *rk_fb = dev_get_drvdata(info->device);
2402         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2403         struct fb_fix_screeninfo *fix = &info->fix;
2404         int fb_id = 0, extend_win_id = 0;
2405         struct fb_info *extend_info = NULL;
2406         struct rk_lcdc_driver *extend_dev_drv = NULL;
2407         struct rk_lcdc_win *extend_win = NULL;
2408         struct rk_lcdc_win *win;
2409         int enable;     /* enable fb:1 enable;0 disable */
2410         int ovl;        /* overlay:0 win1 on the top of win0;1,win0 on the top of win1 */
2411         int num_buf;    /* buffer_number */
2412         int ret;
2413         struct rk_fb_win_cfg_data win_data;
2414         unsigned int dsp_addr[4];
2415         int list_stat;
2416
2417         int win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
2418
2419         void __user *argp = (void __user *)arg;
2420         win = dev_drv->win[win_id];
2421         if (rk_fb->disp_mode == DUAL) {
2422                 fb_id = get_extend_fb_id(info);
2423                 extend_info = rk_fb->fb[(rk_fb->num_fb >> 1) + fb_id];
2424                 extend_dev_drv = (struct rk_lcdc_driver *)extend_info->par;
2425                 extend_win_id = dev_drv->ops->fb_get_win_id(extend_dev_drv,
2426                                                             extend_info->fix.id);
2427                 extend_win = extend_dev_drv->win[extend_win_id];
2428         }
2429
2430         switch (cmd) {
2431         case RK_FBIOSET_YUV_ADDR:
2432                 {
2433                         u32 yuv_phy[2];
2434
2435                         if (copy_from_user(yuv_phy, argp, 8))
2436                                 return -EFAULT;
2437                         #ifdef CONFIG_ROCKCHIP_IOMMU
2438                         if (!dev_drv->iommu_enabled || !strcmp(info->fix.id, "fb0")) {
2439                         #endif
2440                                 fix->smem_start = yuv_phy[0];
2441                                 fix->mmio_start = yuv_phy[1];
2442                         #ifdef CONFIG_ROCKCHIP_IOMMU
2443                         } else {
2444                                 int usr_fd, offset, tmp;
2445                                 struct ion_handle *hdl;
2446                                 ion_phys_addr_t phy_addr;
2447                                 size_t len;
2448
2449                                 usr_fd = yuv_phy[0];
2450                                 offset = yuv_phy[1] - yuv_phy[0];
2451
2452                                 if (!usr_fd) {
2453                                         fix->smem_start = 0;
2454                                         fix->mmio_start = 0;
2455                                         break;
2456                                 }
2457
2458                                 if (ion_hanle[ION_MAX - 1] != 0) {
2459                                         /*ion_unmap_kernel(rk_fb->ion_client, ion_hanle[ION_MAX - 1]);*/
2460                                         /*ion_unmap_iommu(dev_drv->dev, rk_fb->ion_client, ion_hanle[ION_MAX - 1]);*/
2461                                         ion_free(rk_fb->ion_client, ion_hanle[ION_MAX - 1]);
2462                                         ion_hanle[ION_MAX - 1] = 0;
2463                                 }
2464
2465                                 hdl = ion_import_dma_buf(rk_fb->ion_client, usr_fd);
2466                                 if (IS_ERR(hdl)) {
2467                                         dev_err(info->dev, "failed to get ion handle:%ld\n",
2468                                                 PTR_ERR(hdl));
2469                                         return -EFAULT;
2470                                 }
2471
2472                                 ret = ion_map_iommu(dev_drv->dev, rk_fb->ion_client, hdl,
2473                                                         (unsigned long *)&phy_addr,
2474                                                         (unsigned long *)&len);
2475                                 if (ret < 0) {
2476                                         dev_err(info->dev, "ion map to get phy addr failed");
2477                                         ion_free(rk_fb->ion_client, hdl);
2478                                         return -ENOMEM;
2479                                 }
2480                                 fix->smem_start = phy_addr;
2481                                 fix->mmio_start = phy_addr + offset;
2482                                 fix->smem_len = len;
2483                                 /*info->screen_base = ion_map_kernel(rk_fb->ion_client, hdl);*/
2484
2485                                 ion_hanle[0] = hdl;
2486                                 for (tmp = ION_MAX - 1; tmp > 0; tmp--)
2487                                         ion_hanle[tmp] = ion_hanle[tmp - 1];
2488                                 ion_hanle[0] = 0;
2489                         }
2490                         #endif
2491                         break;
2492                 }
2493         case RK_FBIOSET_ENABLE:
2494                 if (copy_from_user(&enable, argp, sizeof(enable)))
2495                         return -EFAULT;
2496                 dev_drv->ops->open(dev_drv, win_id, enable);
2497                 break;
2498         case RK_FBIOGET_ENABLE:
2499                 enable = dev_drv->ops->get_win_state(dev_drv, win_id);
2500                 if (copy_to_user(argp, &enable, sizeof(enable)))
2501                         return -EFAULT;
2502                 break;
2503         case RK_FBIOSET_OVERLAY_STA:
2504                 if (copy_from_user(&ovl, argp, sizeof(ovl)))
2505                         return -EFAULT;
2506                 dev_drv->ops->ovl_mgr(dev_drv, ovl, 1);
2507                 break;
2508         case RK_FBIOGET_OVERLAY_STA:
2509                 ovl = dev_drv->ops->ovl_mgr(dev_drv, 0, 0);
2510                 if (copy_to_user(argp, &ovl, sizeof(ovl)))
2511                         return -EFAULT;
2512                 break;
2513         case RK_FBIOPUT_NUM_BUFFERS:
2514                 if (copy_from_user(&num_buf, argp, sizeof(num_buf)))
2515                         return -EFAULT;
2516                 dev_drv->num_buf = num_buf;
2517                 break;
2518         case RK_FBIOSET_VSYNC_ENABLE:
2519                 if (copy_from_user(&enable, argp, sizeof(enable)))
2520                         return -EFAULT;
2521                 dev_drv->vsync_info.active = enable;
2522                 break;
2523
2524         case RK_FBIOGET_DSP_ADDR:
2525                 dev_drv->ops->get_dsp_addr(dev_drv, dsp_addr);
2526                 if (copy_to_user(argp, &dsp_addr, sizeof(dsp_addr)))
2527                         return -EFAULT;
2528                 break;
2529         case RK_FBIOGET_LIST_STA:
2530                 list_stat = rk_fb_get_list_stat(dev_drv);
2531                 if (copy_to_user(argp, &list_stat, sizeof(list_stat)))
2532                         return -EFAULT;
2533
2534                 break;
2535         case RK_FBIOGET_IOMMU_STA:
2536                 if (copy_to_user(argp, &dev_drv->iommu_enabled,
2537                                  sizeof(dev_drv->iommu_enabled)))
2538                         return -EFAULT;
2539                 break;
2540 #if defined(CONFIG_ION_ROCKCHIP)
2541         case RK_FBIOSET_DMABUF_FD:
2542                 {
2543                         int usr_fd;
2544                         struct ion_handle *hdl;
2545                         ion_phys_addr_t phy_addr;
2546                         size_t len;
2547                         if (copy_from_user(&usr_fd, argp, sizeof(usr_fd)))
2548                                 return -EFAULT;
2549                         hdl = ion_import_dma_buf(rk_fb->ion_client, usr_fd);
2550                         ion_phys(rk_fb->ion_client, hdl, &phy_addr, &len);
2551                         fix->smem_start = phy_addr;
2552                         break;
2553                 }
2554         case RK_FBIOGET_DMABUF_FD:
2555                 {
2556                         int fd =
2557                             ion_share_dma_buf_fd(rk_fb->ion_client,
2558                                                  win->area[0].ion_hdl);
2559                         if (fd < 0) {
2560                                 dev_err(info->dev,
2561                                         "ion_share_dma_buf_fd failed\n");
2562                                 return fd;
2563                         }
2564                         if (copy_to_user(argp, &fd, sizeof(fd)))
2565                                 return -EFAULT;
2566                         break;
2567                 }
2568 #endif
2569         case RK_FBIOSET_CLEAR_FB:
2570                 memset(info->screen_base, 0, get_fb_size());
2571                 break;
2572         case RK_FBIOSET_CONFIG_DONE:
2573                 {
2574                         int curr = 0;
2575                         struct timespec now;
2576
2577                         getnstimeofday(&now);
2578                         curr = now.tv_sec * 1000000 + now.tv_nsec / 1000;
2579                         cfgdone_distlist[cfgdone_index++] =
2580                             curr - cfgdone_lasttime;
2581                         /*
2582                            printk("%d ", curr - cfgdone_lasttime);
2583                          */
2584                         cfgdone_lasttime = curr;
2585                         if (cfgdone_index >= 10)
2586                                 cfgdone_index = 0;
2587                 }
2588                 if (copy_from_user(&win_data,
2589                                    (struct rk_fb_win_cfg_data __user *)argp,
2590                                    sizeof(win_data))) {
2591                         ret = -EFAULT;
2592                         break;
2593                 };
2594
2595                 dev_drv->wait_fs = win_data.wait_fs;
2596                 rk_fb_set_win_config(info, &win_data);
2597
2598                 if (copy_to_user((struct rk_fb_win_cfg_data __user *)arg,
2599                                  &win_data, sizeof(win_data))) {
2600                         ret = -EFAULT;
2601                         break;
2602                 }
2603                 memset(&win_data, 0, sizeof(struct rk_fb_win_cfg_data));
2604                 break;
2605         default:
2606                 dev_drv->ops->ioctl(dev_drv, cmd, arg, win_id);
2607                 break;
2608         }
2609
2610         return 0;
2611 }
2612
2613 static int rk_fb_blank(int blank_mode, struct fb_info *info)
2614 {
2615         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2616         struct fb_fix_screeninfo *fix = &info->fix;
2617         int win_id;
2618 #if defined(CONFIG_RK_HDMI)
2619         struct rk_fb *rk_fb = dev_get_drvdata(info->device);
2620 #endif
2621
2622         win_id = dev_drv->ops->fb_get_win_id(dev_drv, fix->id);
2623         if (win_id < 0)
2624                 return -ENODEV;
2625 #if defined(CONFIG_RK_HDMI)
2626         if ((rk_fb->disp_mode == ONE_DUAL) &&
2627             (hdmi_get_hotplug() == HDMI_HPD_ACTIVED)) {
2628                 printk(KERN_INFO "hdmi is connect , not blank lcdc\n");
2629         } else
2630 #endif
2631         {
2632                 dev_drv->ops->blank(dev_drv, win_id, blank_mode);
2633         }
2634         return 0;
2635 }
2636
2637 static int rk_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
2638 {
2639         if ((0 == var->xres_virtual) || (0 == var->yres_virtual) ||
2640             (0 == var->xres) || (0 == var->yres) || (var->xres < 16) ||
2641             ((16 != var->bits_per_pixel) &&
2642             (32 != var->bits_per_pixel) &&
2643             (24 != var->bits_per_pixel))) {
2644                 dev_err(info->dev, "%s check var fail 1:\n"
2645                         "xres_vir:%d>>yres_vir:%d\n"
2646                         "xres:%d>>yres:%d\n"
2647                         "bits_per_pixel:%d\n",
2648                         info->fix.id,
2649                         var->xres_virtual,
2650                         var->yres_virtual,
2651                         var->xres, var->yres, var->bits_per_pixel);
2652                 return -EINVAL;
2653         }
2654
2655         if (((var->xoffset + var->xres) > var->xres_virtual) ||
2656             ((var->yoffset + var->yres) > (var->yres_virtual))) {
2657                 dev_err(info->dev, "%s check_var fail 2:\n"
2658                         "xoffset:%d>>xres:%d>>xres_vir:%d\n"
2659                         "yoffset:%d>>yres:%d>>yres_vir:%d\n",
2660                         info->fix.id,
2661                         var->xoffset,
2662                         var->xres,
2663                         var->xres_virtual,
2664                         var->yoffset, var->yres, var->yres_virtual);
2665                 return -EINVAL;
2666         }
2667
2668         return 0;
2669 }
2670
2671 static ssize_t rk_fb_read(struct fb_info *info, char __user *buf,
2672                           size_t count, loff_t *ppos)
2673 {
2674         unsigned long p = *ppos;
2675         u8 *buffer, *dst;
2676         u8 __iomem *src;
2677         int c, cnt = 0, err = 0;
2678         unsigned long total_size;
2679         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2680         struct rk_lcdc_win *win = NULL;
2681         int win_id = 0;
2682
2683         win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
2684         if (win_id < 0)
2685                 return -ENODEV;
2686         else
2687                 win = dev_drv->win[win_id];
2688
2689         /* only read the current frame buffer */
2690         if (win->format == RGB565) {
2691                 total_size = win->area[0].y_vir_stride * win->area[0].yact << 1;
2692         } else if (win->format == YUV420) {
2693                 total_size =
2694                     (win->area[0].y_vir_stride * win->area[0].yact * 6);
2695         } else {
2696                 total_size = win->area[0].y_vir_stride * win->area[0].yact << 2;
2697         }
2698         if (p >= total_size)
2699                 return 0;
2700
2701         if (count >= total_size)
2702                 count = total_size;
2703
2704         if (count + p > total_size)
2705                 count = total_size - p;
2706
2707         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, GFP_KERNEL);
2708         if (!buffer)
2709                 return -ENOMEM;
2710
2711         src = (u8 __iomem *)(info->screen_base + p + win->area[0].y_offset);
2712
2713         while (count) {
2714                 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
2715                 dst = buffer;
2716                 fb_memcpy_fromfb(dst, src, c);
2717                 dst += c;
2718                 src += c;
2719
2720                 if (copy_to_user(buf, buffer, c)) {
2721                         err = -EFAULT;
2722                         break;
2723                 }
2724                 *ppos += c;
2725                 buf += c;
2726                 cnt += c;
2727                 count -= c;
2728         }
2729
2730         kfree(buffer);
2731
2732         return (err) ? err : cnt;
2733 }
2734
2735 static ssize_t rk_fb_write(struct fb_info *info, const char __user *buf,
2736                            size_t count, loff_t *ppos)
2737 {
2738         unsigned long p = *ppos;
2739         u8 *buffer, *src;
2740         u8 __iomem *dst;
2741         int c, cnt = 0, err = 0;
2742         unsigned long total_size;
2743         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2744         struct rk_lcdc_win *win = NULL;
2745         int win_id = 0;
2746
2747         win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
2748         if (win_id < 0)
2749                 return -ENODEV;
2750         else
2751                 win = dev_drv->win[win_id];
2752
2753         /* write the current frame buffer */
2754         if (win->format == RGB565)
2755                 total_size = win->area[0].xact * win->area[0].yact << 1;
2756         else
2757                 total_size = win->area[0].xact * win->area[0].yact << 2;
2758
2759         if (p > total_size)
2760                 return -EFBIG;
2761
2762         if (count > total_size) {
2763                 err = -EFBIG;
2764                 count = total_size;
2765         }
2766
2767         if (count + p > total_size) {
2768                 if (!err)
2769                         err = -ENOSPC;
2770
2771                 count = total_size - p;
2772         }
2773
2774         buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, GFP_KERNEL);
2775         if (!buffer)
2776                 return -ENOMEM;
2777
2778         dst = (u8 __iomem *)(info->screen_base + p + win->area[0].y_offset);
2779
2780         while (count) {
2781                 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
2782                 src = buffer;
2783
2784                 if (copy_from_user(src, buf, c)) {
2785                         err = -EFAULT;
2786                         break;
2787                 }
2788
2789                 fb_memcpy_tofb(dst, src, c);
2790                 dst += c;
2791                 src += c;
2792                 *ppos += c;
2793                 buf += c;
2794                 cnt += c;
2795                 count -= c;
2796         }
2797
2798         kfree(buffer);
2799
2800         return (cnt) ? cnt : err;
2801 }
2802
2803 static int rk_fb_set_par(struct fb_info *info)
2804 {
2805         struct fb_var_screeninfo *var = &info->var;
2806         struct fb_fix_screeninfo *fix = &info->fix;
2807         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2808         struct rk_fb *rk_fb = dev_get_drvdata(info->device);
2809         int fb_id, extend_win_id = 0;
2810         struct fb_info *extend_info = NULL;
2811         struct rk_lcdc_driver *extend_dev_drv = NULL;
2812         struct rk_lcdc_win *extend_win = NULL;
2813         struct rk_lcdc_win *win = NULL;
2814         struct rk_screen *screen = dev_drv->cur_screen;
2815         int win_id = 0;
2816         u32 cblen = 0, crlen = 0;
2817         u16 xsize = 0, ysize = 0;       /* winx display window height/width --->LCDC_WINx_DSP_INFO */
2818         u32 xoffset = var->xoffset;     /* offset from virtual to visible */
2819         u32 yoffset = var->yoffset;
2820         u16 xpos = (var->nonstd >> 8) & 0xfff;  /*visiable pos in panel */
2821         u16 ypos = (var->nonstd >> 20) & 0xfff;
2822         u32 xvir = var->xres_virtual;
2823         u32 yvir = var->yres_virtual;
2824         u8 data_format = var->nonstd & 0xff;
2825         u8 fb_data_fmt;
2826         u8 pixel_width;
2827         u32 vir_width_bit;
2828         u32 stride, uv_stride;
2829         u32 stride_32bit_1;
2830         u32 stride_32bit_2;
2831         u16 uv_x_off, uv_y_off, uv_y_act;
2832         u8 is_pic_yuv = 0;
2833
2834         var->pixclock = dev_drv->pixclock;
2835         win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
2836         if (win_id < 0)
2837                 return -ENODEV;
2838         else
2839                 win = dev_drv->win[win_id];
2840         if (rk_fb->disp_mode == DUAL) {
2841                 fb_id = get_extend_fb_id(info);
2842                 extend_info = rk_fb->fb[(rk_fb->num_fb >> 1) + fb_id];
2843                 extend_dev_drv = (struct rk_lcdc_driver *)extend_info->par;
2844                 extend_win_id = dev_drv->ops->fb_get_win_id(extend_dev_drv,
2845                                                             extend_info->fix.
2846                                                             id);
2847                 extend_win = extend_dev_drv->win[extend_win_id];
2848         }
2849         /* if the application has specific the horizontal and vertical display size */
2850         if (var->grayscale >> 8) {
2851                 xsize = (var->grayscale >> 8) & 0xfff;
2852                 ysize = (var->grayscale >> 20) & 0xfff;
2853                 if (xsize > screen->mode.xres)
2854                         xsize = screen->mode.xres;
2855                 if (ysize > screen->mode.yres)
2856                         ysize = screen->mode.yres;
2857         } else {                /*ohterwise  full  screen display */
2858                 xsize = screen->mode.xres;
2859                 ysize = screen->mode.yres;
2860         }
2861
2862         fb_data_fmt = rk_fb_data_fmt(data_format, var->bits_per_pixel);
2863         pixel_width = rk_fb_pixel_width(fb_data_fmt);
2864         vir_width_bit = pixel_width * xvir;
2865         /* pixel_width = byte_num * 8 */
2866         stride_32bit_1 = ALIGN_N_TIMES(vir_width_bit, 32) / 8;
2867         stride_32bit_2 = ALIGN_N_TIMES(vir_width_bit * 2, 32) / 8;
2868
2869         switch (fb_data_fmt) {
2870         case YUV422:
2871         case YUV422_A:
2872                 is_pic_yuv = 1;
2873                 stride = stride_32bit_1;
2874                 uv_stride = stride_32bit_1 >> 1;
2875                 uv_x_off = xoffset >> 1;
2876                 uv_y_off = yoffset;
2877                 fix->line_length = stride;
2878                 cblen = crlen = (xvir * yvir) >> 1;
2879                 uv_y_act = win->area[0].yact >> 1;
2880                 break;
2881         case YUV420:            /* 420sp */
2882         case YUV420_A:
2883                 is_pic_yuv = 1;
2884                 stride = stride_32bit_1;
2885                 uv_stride = stride_32bit_1;
2886                 uv_x_off = xoffset;
2887                 uv_y_off = yoffset >> 1;
2888                 fix->line_length = stride;
2889                 cblen = crlen = (xvir * yvir) >> 2;
2890                 uv_y_act = win->area[0].yact >> 1;
2891                 break;
2892         case YUV444:
2893         case YUV444_A:
2894                 is_pic_yuv = 1;
2895                 stride = stride_32bit_1;
2896                 uv_stride = stride_32bit_2;
2897                 uv_x_off = xoffset * 2;
2898                 uv_y_off = yoffset;
2899                 fix->line_length = stride << 2;
2900                 cblen = crlen = (xvir * yvir);
2901                 uv_y_act = win->area[0].yact;
2902                 break;
2903         default:
2904                 stride = stride_32bit_1;        /* default rgb */
2905                 fix->line_length = stride;
2906                 break;
2907         }
2908
2909         /* x y mirror ,jump line */
2910         if (screen->y_mirror == 1) {
2911                 if (screen->interlace == 1) {
2912                         win->area[0].y_offset = yoffset * stride * 2 +
2913                             ((win->area[0].yact - 1) * 2 + 1) * stride +
2914                             xoffset * pixel_width / 8;
2915                 } else {
2916                         win->area[0].y_offset = yoffset * stride +
2917                             (win->area[0].yact - 1) * stride +
2918                             xoffset * pixel_width / 8;
2919                 }
2920         } else {
2921                 if (screen->interlace == 1) {
2922                         win->area[0].y_offset =
2923                             yoffset * stride * 2 + xoffset * pixel_width / 8;
2924                 } else {
2925                         win->area[0].y_offset =
2926                             yoffset * stride + xoffset * pixel_width / 8;
2927                 }
2928         }
2929         if (is_pic_yuv == 1) {
2930                 if (screen->y_mirror == 1) {
2931                         if (screen->interlace == 1) {
2932                                 win->area[0].c_offset =
2933                                     uv_y_off * uv_stride * 2 +
2934                                     ((uv_y_act - 1) * 2 + 1) * uv_stride +
2935                                     uv_x_off * pixel_width / 8;
2936                         } else {
2937                                 win->area[0].c_offset = uv_y_off * uv_stride +
2938                                     (uv_y_act - 1) * uv_stride +
2939                                     uv_x_off * pixel_width / 8;
2940                         }
2941                 } else {
2942                         if (screen->interlace == 1) {
2943                                 win->area[0].c_offset =
2944                                     uv_y_off * uv_stride * 2 +
2945                                     uv_x_off * pixel_width / 8;
2946                         } else {
2947                                 win->area[0].c_offset =
2948                                     uv_y_off * uv_stride +
2949                                     uv_x_off * pixel_width / 8;
2950                         }
2951                 }
2952         }
2953
2954         win->format = fb_data_fmt;
2955         win->area[0].y_vir_stride = stride >> 2;
2956         win->area[0].uv_vir_stride = uv_stride >> 2;
2957         win->area[0].xpos = xpos;
2958         win->area[0].ypos = ypos;
2959         win->area[0].xsize = xsize;
2960         win->area[0].ysize = ysize;
2961         win->area[0].xact = var->xres;  /* winx active window height,is a wint of vir */
2962         win->area[0].yact = var->yres;
2963         win->area[0].xvir = var->xres_virtual;  /* virtual resolution  stride --->LCDC_WINx_VIR */
2964         win->area[0].yvir = var->yres_virtual;
2965
2966         win->area_num = 1;
2967         win->alpha_mode = 4;    /* AB_SRC_OVER; */
2968         win->alpha_en = ((win->format == ARGB888) ||
2969                          (win->format == ABGR888)) ? 1 : 0;
2970         win->g_alpha_val = 0;
2971
2972         if (rk_fb->disp_mode == DUAL) {
2973                 if (extend_win->state && (hdmi_switch_complete == 0xff)) {
2974                         if (info != extend_info) {
2975                                 if (win->area[0].xact < win->area[0].yact) {
2976                                         extend_win->area[0].xact =
2977                                             win->area[0].yact;
2978                                         extend_win->area[0].yact =
2979                                             win->area[0].xact;
2980                                         extend_win->area[0].xvir =
2981                                             win->area[0].yact;
2982                                         extend_info->var.xres = var->yres;
2983                                         extend_info->var.yres = var->xres;
2984                                         extend_info->var.xres_virtual =
2985                                             var->yres;
2986                                 } else {
2987                                         extend_win->area[0].xact =
2988                                             win->area[0].xact;
2989                                         extend_win->area[0].yact =
2990                                             win->area[0].yact;
2991                                         extend_win->area[0].xvir =
2992                                             win->area[0].xvir;
2993                                         extend_info->var.xres = var->xres;
2994                                         extend_info->var.yres = var->yres;
2995                                         extend_info->var.xres_virtual =
2996                                             var->xres_virtual;
2997                                 }
2998                                 extend_win->area[0].y_vir_stride =
2999                                     win->area[0].y_vir_stride;
3000                                 extend_win->area[0].uv_vir_stride =
3001                                     win->area[0].uv_vir_stride;
3002                                 if (win->area[0].xpos != 0 ||
3003                                     win->area[0].ypos != 0) {
3004                                         extend_win->area[0].xsize =
3005                                             (extend_dev_drv->cur_screen->xsize * win->area[0].xsize) / screen->mode.xres;
3006                                         extend_win->area[0].ysize =
3007                                             (extend_dev_drv->cur_screen->ysize * win->area[0].ysize) / screen->mode.yres;
3008                                         extend_win->area[0].xpos =
3009                                             ((extend_dev_drv->cur_screen->mode.xres - extend_dev_drv->cur_screen->xsize) >> 1) +
3010                                             extend_dev_drv->cur_screen->xsize * win->area[0].xpos / screen->mode.xres;
3011                                         extend_win->area[0].ypos =
3012                                             ((extend_dev_drv->cur_screen->mode.yres - extend_dev_drv->cur_screen->ysize) >> 1) +
3013                                             extend_dev_drv->cur_screen->ysize * win->area[0].ypos / screen->mode.yres;
3014                                 } else {        /* the display image of the primary screen is full screen size */
3015                                         extend_win->area[0].xpos =
3016                                             (extend_dev_drv->cur_screen->mode.xres - extend_dev_drv->cur_screen->xsize) >> 1;
3017                                         extend_win->area[0].ypos =
3018                                             (extend_dev_drv->cur_screen->mode.yres - extend_dev_drv->cur_screen->ysize) >> 1;
3019                                         extend_win->area[0].xsize =
3020                                             extend_dev_drv->cur_screen->xsize;
3021                                         extend_win->area[0].ysize =
3022                                             extend_dev_drv->cur_screen->ysize;
3023                                 }
3024
3025                                 extend_win->area[0].state = 1;
3026                                 extend_win->area_num = 1;
3027                                 extend_win->alpha_en = 0;
3028                                 extend_win->format = win->format;
3029                                 extend_info->var.nonstd &= 0xffffff00;
3030                                 extend_info->var.nonstd |= data_format;
3031                                 extend_dev_drv->ops->set_par(extend_dev_drv,
3032                                                              extend_win_id);
3033                         }
3034                 }
3035         }
3036         dev_drv->ops->set_par(dev_drv, win_id);
3037
3038         return 0;
3039 }
3040
3041 static inline unsigned int chan_to_field(unsigned int chan,
3042                                          struct fb_bitfield *bf)
3043 {
3044         chan &= 0xffff;
3045         chan >>= 16 - bf->length;
3046         return chan << bf->offset;
3047 }
3048
3049 static int fb_setcolreg(unsigned regno,
3050                         unsigned red, unsigned green, unsigned blue,
3051                         unsigned transp, struct fb_info *info)
3052 {
3053         unsigned int val;
3054
3055         switch (info->fix.visual) {
3056         case FB_VISUAL_TRUECOLOR:
3057                 /* true-colour, use pseudo-palette */
3058                 if (regno < 16) {
3059                         u32 *pal = info->pseudo_palette;
3060                         val = chan_to_field(red, &info->var.red);
3061                         val |= chan_to_field(green, &info->var.green);
3062                         val |= chan_to_field(blue, &info->var.blue);
3063                         pal[regno] = val;
3064                 }
3065                 break;
3066         default:
3067                 return -1;      /* unknown type */
3068         }
3069
3070         return 0;
3071 }
3072
3073 static int rk_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
3074 {
3075         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
3076         int win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
3077         struct rk_lcdc_win *win;
3078         win = dev_drv->win[win_id];
3079
3080         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
3081
3082         return dma_buf_mmap(win->area[0].dma_buf, vma, 0);
3083 }
3084
3085 static struct fb_ops fb_ops = {
3086         .owner = THIS_MODULE,
3087         .fb_open = rk_fb_open,
3088         .fb_release = rk_fb_close,
3089         .fb_check_var = rk_fb_check_var,
3090         .fb_set_par = rk_fb_set_par,
3091         .fb_blank = rk_fb_blank,
3092         .fb_ioctl = rk_fb_ioctl,
3093         .fb_pan_display = rk_fb_pan_display,
3094         .fb_read = rk_fb_read,
3095         .fb_write = rk_fb_write,
3096         .fb_setcolreg = fb_setcolreg,
3097         .fb_fillrect = cfb_fillrect,
3098         .fb_copyarea = cfb_copyarea,
3099         .fb_imageblit = cfb_imageblit,
3100 };
3101
3102 static struct fb_var_screeninfo def_var = {
3103 #if defined(CONFIG_LOGO_LINUX_BMP)
3104         .red = {16, 8, 0},
3105         .green = {8, 8, 0},
3106         .blue = {0, 8, 0},
3107         .transp = {0, 0, 0},
3108         .nonstd = HAL_PIXEL_FORMAT_BGRA_8888,
3109 #else
3110         .red = {11, 5, 0},
3111         .green = {5, 6, 0},
3112         .blue = {0, 5, 0},
3113         .transp = {0, 0, 0},
3114         .nonstd = HAL_PIXEL_FORMAT_RGB_565,     /* (ypos<<20+xpos<<8+format) format */
3115 #endif
3116         .grayscale = 0,         /* (ysize<<20+xsize<<8) */
3117         .activate = FB_ACTIVATE_NOW,
3118         .accel_flags = 0,
3119         .vmode = FB_VMODE_NONINTERLACED,
3120 };
3121
3122 static struct fb_fix_screeninfo def_fix = {
3123         .type = FB_TYPE_PACKED_PIXELS,
3124         .type_aux = 0,
3125         .xpanstep = 1,
3126         .ypanstep = 1,
3127         .ywrapstep = 0,
3128         .accel = FB_ACCEL_NONE,
3129         .visual = FB_VISUAL_TRUECOLOR,
3130
3131 };
3132
3133 static int rk_fb_wait_for_vsync_thread(void *data)
3134 {
3135         struct rk_lcdc_driver *dev_drv = data;
3136         struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3137         struct fb_info *fbi = rk_fb->fb[0];
3138
3139         while (!kthread_should_stop()) {
3140                 ktime_t timestamp = dev_drv->vsync_info.timestamp;
3141                 int ret = wait_event_interruptible(dev_drv->vsync_info.wait,
3142                                 !ktime_equal(timestamp, dev_drv->vsync_info.timestamp) &&
3143                                 (dev_drv->vsync_info.active || dev_drv->vsync_info.irq_stop));
3144
3145                 if (!ret)
3146                         sysfs_notify(&fbi->dev->kobj, NULL, "vsync");
3147         }
3148
3149         return 0;
3150 }
3151
3152 static ssize_t rk_fb_vsync_show(struct device *dev,
3153                                 struct device_attribute *attr, char *buf)
3154 {
3155         struct fb_info *fbi = dev_get_drvdata(dev);
3156         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)fbi->par;
3157         return scnprintf(buf, PAGE_SIZE, "%llu\n",
3158                          ktime_to_ns(dev_drv->vsync_info.timestamp));
3159 }
3160
3161 static DEVICE_ATTR(vsync, S_IRUGO, rk_fb_vsync_show, NULL);
3162
3163 /*
3164  * this two function is for other module that in the kernel which
3165  * need show image directly through fb
3166  * fb_id:we have 4 fb here,default we use fb0 for ui display
3167  */
3168 struct fb_info *rk_get_fb(int fb_id)
3169 {
3170         struct rk_fb *inf = platform_get_drvdata(fb_pdev);
3171         struct fb_info *fb = inf->fb[fb_id];
3172         return fb;
3173 }
3174 EXPORT_SYMBOL(rk_get_fb);
3175
3176 void rk_direct_fb_show(struct fb_info *fbi)
3177 {
3178         rk_fb_set_par(fbi);
3179         rk_fb_pan_display(&fbi->var, fbi);
3180 }
3181 EXPORT_SYMBOL(rk_direct_fb_show);
3182
3183 int rk_fb_dpi_open(bool open)
3184 {
3185         struct rk_lcdc_driver *dev_drv = NULL;
3186         dev_drv = rk_get_prmry_lcdc_drv();
3187
3188         if (dev_drv->ops->dpi_open)
3189                 dev_drv->ops->dpi_open(dev_drv, open);
3190         return 0;
3191 }
3192
3193 int rk_fb_dpi_win_sel(int win_id)
3194 {
3195         struct rk_lcdc_driver *dev_drv = NULL;
3196         dev_drv = rk_get_prmry_lcdc_drv();
3197
3198         if (dev_drv->ops->dpi_win_sel)
3199                 dev_drv->ops->dpi_win_sel(dev_drv, win_id);
3200         return 0;
3201 }
3202
3203 int rk_fb_dpi_status(void)
3204 {
3205         int ret = 0;
3206         struct rk_lcdc_driver *dev_drv = NULL;
3207
3208         dev_drv = rk_get_prmry_lcdc_drv();
3209         if (dev_drv->ops->dpi_status)
3210                 ret = dev_drv->ops->dpi_status(dev_drv);
3211
3212         return ret;
3213 }
3214
3215 /*
3216  * function: this function will be called to modify hdmi info according to
3217  *              pmy info for DUAL disp_mode when HDMI is used as EXTEND
3218  * @pmy_info: primary screen fb_info
3219  * @hdmi_info: hdmi screen fb_info
3220  * @screen: the hdmi screen info
3221  */
3222 static int rk_fb_hdmi_info_align(struct fb_info *pmy_info,
3223                                  struct fb_info *hdmi_info,
3224                                  struct rk_screen *screen)
3225 {
3226         struct rk_fb *inf =  platform_get_drvdata(fb_pdev);
3227         struct fb_var_screeninfo *hdmi_var = NULL;
3228         struct fb_var_screeninfo *pmy_var = NULL;
3229
3230         if(unlikely(!pmy_info) || unlikely(!hdmi_info))
3231                 return -1;
3232
3233         if (inf->disp_mode != DUAL || pmy_info == hdmi_info)
3234                 return 0;
3235
3236         pmy_var = &pmy_info->var;
3237         hdmi_var = &hdmi_info->var;
3238         if (pmy_var->xres < pmy_var->yres) {    /* vertical lcd screen */
3239                 hdmi_var->xres = pmy_var->yres;
3240                 hdmi_var->yres = pmy_var->xres;
3241                 hdmi_var->xres_virtual = pmy_var->yres;
3242         } else {
3243                 hdmi_var->xres = pmy_var->xres;
3244                 hdmi_var->yres = pmy_var->yres;
3245                 hdmi_var->xres_virtual = pmy_var->xres_virtual;
3246         }
3247
3248         /* use the same format as primary screen */
3249         hdmi_var->nonstd &= 0xffffff00;
3250         hdmi_var->nonstd |= (pmy_var->nonstd & 0xff);
3251
3252         if (screen) {
3253                 hdmi_var->grayscale &= 0xff;
3254                 hdmi_var->grayscale |= (screen->mode.xres << 8) +
3255                                         (screen->mode.yres << 20);
3256         }
3257
3258         return 0;
3259 }
3260
3261 /*
3262  * function: this function will be called by display device, enable/disable lcdc
3263  * @screen: screen timing to be set to lcdc
3264  * @enable: 0 disable lcdc; 1 enable change lcdc timing; 2 just enable dclk
3265  * @lcdc_id: the lcdc id the display device attached ,0 or 1
3266  */
3267 int rk_fb_switch_screen(struct rk_screen *screen, int enable, int lcdc_id)
3268 {
3269         struct rk_fb *rk_fb =  platform_get_drvdata(fb_pdev);
3270         struct fb_info *info = NULL;
3271         struct fb_info *pmy_info = NULL;
3272         struct rk_lcdc_driver *dev_drv = NULL;
3273         struct rk_lcdc_driver *pmy_dev_drv = rk_get_prmry_lcdc_drv();
3274         char name[6] = {0};
3275         int i, win_id, load_screen = 0;
3276
3277         if (unlikely(!rk_fb) || unlikely(!pmy_dev_drv) || unlikely(!screen))
3278                 return -ENODEV;
3279
3280         /* get lcdc driver */
3281         sprintf(name, "lcdc%d", lcdc_id);
3282         if (rk_fb->disp_mode != DUAL)
3283                 dev_drv = rk_fb->lcdc_dev_drv[0];
3284         else
3285                 dev_drv = rk_get_lcdc_drv(name);
3286
3287         if (dev_drv == NULL) {
3288                 printk(KERN_ERR "%s driver not found!", name);
3289                 return -ENODEV;
3290         }
3291         if (screen->type == SCREEN_HDMI)
3292                 printk("hdmi %s lcdc%d\n", enable ? "connect to" : "remove from",
3293                         dev_drv->id);
3294         else if (screen->type == SCREEN_TVOUT)
3295                 printk("cvbs %s lcdc%d\n", enable ? "connect to" : "remove from",
3296                         dev_drv->id);
3297
3298         if (enable == 2 /*&& dev_drv->enable*/)
3299                 return 0;
3300
3301         if (rk_fb->disp_mode == ONE_DUAL) {
3302                 if (dev_drv->trsm_ops && dev_drv->trsm_ops->disable)
3303                         dev_drv->trsm_ops->disable();
3304                 if (dev_drv->ops->set_screen_scaler)
3305                         dev_drv->ops->set_screen_scaler(dev_drv, dev_drv->screen0, 0);
3306         }
3307
3308         if (!enable) {
3309                 /* if screen type is different, we do not disable lcdc. */
3310                 if (dev_drv->cur_screen->type != screen->type)
3311                         return 0;
3312
3313                 /* if used one lcdc to dual disp, no need to close win */
3314                 if (rk_fb->disp_mode == ONE_DUAL) {
3315                         dev_drv->cur_screen = dev_drv->screen0;
3316                         dev_drv->ops->load_screen(dev_drv, 1);
3317                         if (dev_drv->trsm_ops && dev_drv->trsm_ops->enable)
3318                                 dev_drv->trsm_ops->enable();
3319                 } else if (rk_fb->num_lcdc > 1) {
3320                         /* If there is more than one lcdc device, we disable
3321                            the layer which attached to this device */
3322                         for (i = 0; i < dev_drv->lcdc_win_num; i++) {
3323                                 if (dev_drv->win[i] && dev_drv->win[i]->state)
3324                                         dev_drv->ops->open(dev_drv, i, 0);
3325                         }
3326                 }
3327
3328                 hdmi_switch_complete = 0;
3329                 return 0;
3330         } else {
3331                 if (dev_drv->screen1)
3332                         dev_drv->cur_screen = dev_drv->screen1;
3333                 memcpy(dev_drv->cur_screen, screen, sizeof(struct rk_screen));
3334                 dev_drv->cur_screen->x_mirror = dev_drv->rotate_mode & X_MIRROR;
3335                 dev_drv->cur_screen->y_mirror = dev_drv->rotate_mode & Y_MIRROR;
3336         }
3337
3338         for (i = 0; i < dev_drv->lcdc_win_num; i++) {
3339                 info = rk_fb->fb[dev_drv->fb_index_base + i];
3340                 if (rk_fb->disp_mode == DUAL &&
3341                                 dev_drv != pmy_dev_drv) {
3342                         pmy_info = rk_fb->fb[pmy_dev_drv->fb_index_base + i];
3343                         rk_fb_hdmi_info_align(pmy_info, info,
3344                                                 dev_drv->cur_screen);
3345                 }
3346
3347                 win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
3348                 if (dev_drv->win[win_id]) {
3349                         if (rk_fb->disp_mode == DUAL) {
3350                                 if (dev_drv != pmy_dev_drv &&
3351                                                 pmy_dev_drv->win[win_id]) {
3352                                         dev_drv->win[win_id]->logicalstate =
3353                                                 pmy_dev_drv->win[win_id]->logicalstate;
3354                                 }
3355                         }
3356                         if (dev_drv->win[win_id]->logicalstate) {
3357                                 if (!dev_drv->win[win_id]->state)
3358                                         dev_drv->ops->open(dev_drv, win_id, 1);
3359                                 if (!load_screen) {
3360                                         dev_drv->ops->load_screen(dev_drv, 1);
3361                                         load_screen = 1;
3362                                 }
3363                                 info->var.activate |= FB_ACTIVATE_FORCE;
3364                                 info->fbops->fb_set_par(info);
3365                                 hdmi_switch_complete |= (1 << win_id);
3366                                 if (rk_fb->disp_mode == DUAL)
3367                                         pmy_info->fbops->fb_pan_display(&pmy_info->var,
3368                                                                         pmy_info);
3369                                 else
3370                                         info->fbops->fb_pan_display(&info->var, info);
3371                         }
3372                 }
3373         }
3374
3375         hdmi_switch_complete = 0xff;
3376         if (rk_fb->disp_mode == ONE_DUAL) {
3377                 if (dev_drv->ops->set_screen_scaler)
3378                         dev_drv->ops->set_screen_scaler(dev_drv, dev_drv->screen0, 1);
3379                 if (dev_drv->trsm_ops && dev_drv->trsm_ops->enable)
3380                         dev_drv->trsm_ops->enable();
3381         }
3382         return 0;
3383 }
3384
3385 /*
3386  * function:this function current only called by hdmi for
3387  *      scale the display
3388  * scale_x: scale rate of x resolution
3389  * scale_y: scale rate of y resolution
3390  * lcdc_id: the lcdc id the hdmi attached ,0 or 1
3391  */
3392 int rk_fb_disp_scale(u8 scale_x, u8 scale_y, u8 lcdc_id)
3393 {
3394         struct rk_fb *inf = platform_get_drvdata(fb_pdev);
3395         struct fb_info *info = NULL;
3396         struct fb_var_screeninfo *var = NULL;
3397         struct rk_lcdc_driver *dev_drv = NULL;
3398         u16 screen_x, screen_y;
3399         u16 xpos, ypos;
3400         char name[6];
3401         struct rk_screen primary_screen;
3402         rk_fb_get_prmry_screen(&primary_screen);
3403         if (primary_screen.type == SCREEN_HDMI) {
3404                 return 0;
3405         }
3406
3407         sprintf(name, "lcdc%d", lcdc_id);
3408
3409         if (inf->disp_mode == DUAL) {
3410                 dev_drv = rk_get_lcdc_drv(name);
3411                 if (dev_drv == NULL) {
3412                         printk(KERN_ERR "%s driver not found!", name);
3413                         return -ENODEV;
3414                 }
3415         } else {
3416                 dev_drv = inf->lcdc_dev_drv[0];
3417         }
3418
3419         if (inf->num_lcdc == 1)
3420                 info = inf->fb[0];
3421         else if (inf->num_lcdc == 2)
3422                 info = inf->fb[dev_drv->lcdc_win_num];
3423
3424         var = &info->var;
3425         screen_x = dev_drv->cur_screen->mode.xres;
3426         screen_y = dev_drv->cur_screen->mode.yres;
3427
3428         if (inf->disp_mode != DUAL && dev_drv->screen1) {
3429                 dev_drv->cur_screen->xpos =
3430                     (screen_x - screen_x * scale_x / 100) >> 1;
3431                 dev_drv->cur_screen->ypos =
3432                     (screen_y - screen_y * scale_y / 100) >> 1;
3433                 dev_drv->cur_screen->xsize = screen_x * scale_x / 100;
3434                 dev_drv->cur_screen->ysize = screen_y * scale_y / 100;
3435         } else {
3436                 xpos = (screen_x - screen_x * scale_x / 100) >> 1;
3437                 ypos = (screen_y - screen_y * scale_y / 100) >> 1;
3438                 dev_drv->cur_screen->xsize = screen_x * scale_x / 100;
3439                 dev_drv->cur_screen->ysize = screen_y * scale_y / 100;
3440                 var->nonstd &= 0xff;
3441                 var->nonstd |= (xpos << 8) + (ypos << 20);
3442                 var->grayscale &= 0xff;
3443                 var->grayscale |=
3444                     (dev_drv->cur_screen->xsize << 8) + (dev_drv->cur_screen->ysize << 20);
3445         }
3446
3447         info->fbops->fb_set_par(info);
3448         /* info->fbops->fb_ioctl(info, RK_FBIOSET_CONFIG_DONE, 0); */
3449         dev_drv->ops->cfg_done(dev_drv);
3450         return 0;
3451 }
3452
3453 #if defined(CONFIG_ION_ROCKCHIP)
3454 static int rk_fb_alloc_buffer_by_ion(struct fb_info *fbi,
3455                                      struct rk_lcdc_win *win,
3456                                      unsigned long fb_mem_size)
3457 {
3458         struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3459         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)fbi->par;
3460         struct ion_handle *handle;
3461         ion_phys_addr_t phy_addr;
3462         size_t len;
3463         int ret = 0;
3464
3465         if (dev_drv->iommu_enabled)
3466                 handle = ion_alloc(rk_fb->ion_client, (size_t) fb_mem_size, 0,
3467                                    ION_HEAP(ION_VMALLOC_HEAP_ID), 0);
3468         else
3469                 handle = ion_alloc(rk_fb->ion_client, (size_t) fb_mem_size, 0,
3470                                    ION_HEAP(ION_CMA_HEAP_ID), 0);
3471         if (IS_ERR(handle)) {
3472                 dev_err(fbi->device, "failed to ion_alloc:%ld\n",
3473                         PTR_ERR(handle));
3474                 return -ENOMEM;
3475         }
3476         win->area[0].dma_buf = ion_share_dma_buf(rk_fb->ion_client, handle);
3477         if (IS_ERR_OR_NULL(win->area[0].dma_buf)) {
3478                 printk("ion_share_dma_buf() failed\n");
3479                 goto err_share_dma_buf;
3480         }
3481         win->area[0].ion_hdl = handle;
3482         if (dev_drv->prop == PRMRY)
3483                 fbi->screen_base = ion_map_kernel(rk_fb->ion_client, handle);
3484 #ifdef CONFIG_ROCKCHIP_IOMMU
3485         if (dev_drv->iommu_enabled)
3486                 ret = ion_map_iommu(dev_drv->dev, rk_fb->ion_client, handle,
3487                                         (unsigned long *)&phy_addr,
3488                                         (unsigned long *)&len);
3489         else
3490                 ret = ion_phys(rk_fb->ion_client, handle, &phy_addr, &len);
3491 #else
3492         ret = ion_phys(rk_fb->ion_client, handle, &phy_addr, &len);
3493 #endif
3494         if (ret < 0) {
3495                 dev_err(fbi->dev, "ion map to get phy addr failed\n");
3496                 goto err_share_dma_buf;
3497         }
3498         fbi->fix.smem_start = phy_addr;
3499         fbi->fix.smem_len = len;
3500         printk(KERN_INFO "alloc_buffer:ion_phy_addr=0x%lx\n", phy_addr);
3501         return 0;
3502
3503 err_share_dma_buf:
3504         ion_free(rk_fb->ion_client, handle);
3505         return -ENOMEM;
3506 }
3507 #endif
3508
3509 static int rk_fb_alloc_buffer(struct fb_info *fbi, int fb_id)
3510 {
3511         struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3512         struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)fbi->par;
3513         struct rk_lcdc_win *win = NULL;
3514         int win_id;
3515         int ret = 0;
3516         unsigned long fb_mem_size;
3517 #if !defined(CONFIG_ION_ROCKCHIP)
3518         dma_addr_t fb_mem_phys;
3519         void *fb_mem_virt;
3520 #endif
3521
3522         win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
3523         if (win_id < 0)
3524                 return -ENODEV;
3525         else
3526                 win = dev_drv->win[win_id];
3527
3528         if (!strcmp(fbi->fix.id, "fb0")) {
3529                 fb_mem_size = get_fb_size();
3530 #if defined(CONFIG_ION_ROCKCHIP)
3531                 if (rk_fb_alloc_buffer_by_ion(fbi, win, fb_mem_size) < 0)
3532                         return -ENOMEM;
3533 #else
3534                 fb_mem_virt = dma_alloc_writecombine(fbi->dev, fb_mem_size,
3535                                                      &fb_mem_phys, GFP_KERNEL);
3536                 if (!fb_mem_virt) {
3537                         pr_err("%s: Failed to allocate framebuffer\n",
3538                                __func__);
3539                         return -ENOMEM;
3540                 }
3541                 fbi->fix.smem_len = fb_mem_size;
3542                 fbi->fix.smem_start = fb_mem_phys;
3543                 fbi->screen_base = fb_mem_virt;
3544 #endif
3545                 memset(fbi->screen_base, 0, fbi->fix.smem_len);
3546                 printk(KERN_INFO "fb%d:phy:%lx>>vir:%p>>len:0x%x\n", fb_id,
3547                        fbi->fix.smem_start, fbi->screen_base,
3548                        fbi->fix.smem_len);
3549         } else {
3550                 if (dev_drv->rotate_mode > X_Y_MIRROR) {
3551                         fb_mem_size = get_rotate_fb_size();
3552 #if defined(CONFIG_ION_ROCKCHIP)
3553                         if (rk_fb_alloc_buffer_by_ion(fbi, win, fb_mem_size) < 0)
3554                                 return -ENOMEM;
3555 #else
3556                         fb_mem_virt =
3557                                 dma_alloc_writecombine(fbi->dev,
3558                                                        fb_mem_size,
3559                                                        &fb_mem_phys,
3560                                                        GFP_KERNEL);
3561                         if (!fb_mem_virt) {
3562                                 pr_err("%s: Failed to allocate framebuffer\n",
3563                                         __func__);
3564                                 return -ENOMEM;
3565                         }
3566                         fbi->fix.smem_len = fb_mem_size;
3567                         fbi->fix.smem_start = fb_mem_phys;
3568                         fbi->screen_base = fb_mem_virt;
3569 #endif
3570                 } else {
3571                         fbi->fix.smem_start = rk_fb->fb[0]->fix.smem_start;
3572                         fbi->fix.smem_len = rk_fb->fb[0]->fix.smem_len;
3573                         fbi->screen_base = rk_fb->fb[0]->screen_base;
3574                 }
3575
3576                 printk(KERN_INFO "fb%d:phy:%lx>>vir:%p>>len:0x%x\n", fb_id,
3577                        fbi->fix.smem_start, fbi->screen_base,
3578                        fbi->fix.smem_len);
3579         }
3580
3581         fbi->screen_size = fbi->fix.smem_len;
3582         win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
3583         if (win_id >= 0) {
3584                 win = dev_drv->win[win_id];
3585                 win->reserved = fbi->fix.smem_start;
3586         }
3587
3588         return ret;
3589 }
3590
3591 #if 0
3592 static int rk_release_fb_buffer(struct fb_info *fbi)
3593 {
3594         /* buffer for fb1 and fb3 are alloc by android */
3595         if (!strcmp(fbi->fix.id, "fb1") || !strcmp(fbi->fix.id, "fb3"))
3596                 return 0;
3597         iounmap(fbi->screen_base);
3598         release_mem_region(fbi->fix.smem_start, fbi->fix.smem_len);
3599         return 0;
3600 }
3601 #endif
3602
3603 static int init_lcdc_win(struct rk_lcdc_driver *dev_drv,
3604                          struct rk_lcdc_win *def_win)
3605 {
3606         int i;
3607         int lcdc_win_num = dev_drv->lcdc_win_num;
3608
3609         for (i = 0; i < lcdc_win_num; i++) {
3610                 struct rk_lcdc_win *win = NULL;
3611                 win = kzalloc(sizeof(struct rk_lcdc_win), GFP_KERNEL);
3612                 if (!win) {
3613                         dev_err(dev_drv->dev, "kzmalloc for win fail!");
3614                         return -ENOMEM;
3615                 }
3616
3617                 strcpy(win->name, def_win[i].name);
3618                 win->id = def_win[i].id;
3619                 win->support_3d = def_win[i].support_3d;
3620                 dev_drv->win[i] = win;
3621         }
3622
3623         return 0;
3624 }
3625
3626 static int init_lcdc_device_driver(struct rk_fb *rk_fb,
3627                                    struct rk_lcdc_win *def_win, int index)
3628 {
3629         struct rk_lcdc_driver *dev_drv = rk_fb->lcdc_dev_drv[index];
3630         struct rk_screen *screen = devm_kzalloc(dev_drv->dev,
3631                                                 sizeof(struct rk_screen),
3632                                                 GFP_KERNEL);
3633
3634         if (!screen) {
3635                 dev_err(dev_drv->dev, "malloc screen for lcdc%d fail!",
3636                         dev_drv->id);
3637                 return -ENOMEM;
3638         }
3639
3640         screen->screen_id = 0;
3641         screen->lcdc_id = dev_drv->id;
3642         screen->overscan.left = 100;
3643         screen->overscan.top = 100;
3644         screen->overscan.right = 100;
3645         screen->overscan.bottom = 100;
3646
3647         screen->x_mirror = dev_drv->rotate_mode & X_MIRROR;
3648         screen->y_mirror = dev_drv->rotate_mode & Y_MIRROR;
3649
3650         dev_drv->screen0 = screen;
3651         dev_drv->cur_screen = screen;
3652         /* devie use one lcdc + rk61x scaler for dual display */
3653         if (rk_fb->disp_mode == ONE_DUAL) {
3654                 struct rk_screen *screen1 =
3655                                 devm_kzalloc(dev_drv->dev,
3656                                              sizeof(struct rk_screen),
3657                                              GFP_KERNEL);
3658                 if (!screen1) {
3659                         dev_err(dev_drv->dev, "malloc screen1 for lcdc%d fail!",
3660                                 dev_drv->id);
3661                         return -ENOMEM;
3662                 }
3663                 screen1->screen_id = 1;
3664                 screen1->lcdc_id = 1;
3665                 dev_drv->screen1 = screen1;
3666         }
3667         sprintf(dev_drv->name, "lcdc%d", dev_drv->id);
3668         init_lcdc_win(dev_drv, def_win);
3669         init_completion(&dev_drv->frame_done);
3670         spin_lock_init(&dev_drv->cpl_lock);
3671         mutex_init(&dev_drv->fb_win_id_mutex);
3672         dev_drv->ops->fb_win_remap(dev_drv, dev_drv->fb_win_map);
3673         dev_drv->first_frame = 1;
3674         dev_drv->overscan.left = 100;
3675         dev_drv->overscan.top = 100;
3676         dev_drv->overscan.right = 100;
3677         dev_drv->overscan.bottom = 100;
3678         rk_disp_pwr_ctr_parse_dt(dev_drv);
3679         if (dev_drv->prop == PRMRY) {
3680                 if (dev_drv->ops->set_dsp_cabc)
3681                         dev_drv->ops->set_dsp_cabc(dev_drv, dev_drv->cabc_mode);
3682                 rk_fb_set_prmry_screen(screen);
3683                 rk_fb_get_prmry_screen(screen);
3684         }
3685         dev_drv->trsm_ops = rk_fb_trsm_ops_get(screen->type);
3686         if (dev_drv->prop != PRMRY)
3687                 rk_fb_get_prmry_screen(screen);
3688
3689         return 0;
3690 }
3691
3692 #ifdef CONFIG_LOGO_LINUX_BMP
3693 static struct linux_logo *bmp_logo;
3694 static int fb_prewine_bmp_logo(struct fb_info *info, int rotate)
3695 {
3696         bmp_logo = fb_find_logo(24);
3697         if (bmp_logo == NULL) {
3698                 printk(KERN_INFO "%s error\n", __func__);
3699                 return 0;
3700         }
3701         return 1;
3702 }
3703
3704 static void fb_show_bmp_logo(struct fb_info *info, int rotate)
3705 {
3706         unsigned char *src = bmp_logo->data;
3707         unsigned char *dst = info->screen_base;
3708         int i;
3709         unsigned int Needwidth = (*(src - 24) << 8) | (*(src - 23));
3710         unsigned int Needheight = (*(src - 22) << 8) | (*(src - 21));
3711
3712         for (i = 0; i < Needheight; i++)
3713                 memcpy(dst + info->var.xres * i * 4,
3714                        src + bmp_logo->width * i * 4, Needwidth * 4);
3715 }
3716 #endif
3717
3718 /*
3719  * check if the primary lcdc has registerd,
3720  * the primary lcdc mas register first
3721  */
3722 bool is_prmry_rk_lcdc_registered(void)
3723 {
3724         struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3725
3726         if (rk_fb->lcdc_dev_drv[0])
3727                 return true;
3728         else
3729                 return false;
3730 }
3731
3732 int rk_fb_register(struct rk_lcdc_driver *dev_drv,
3733                    struct rk_lcdc_win *win, int id)
3734 {
3735         struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3736         struct fb_info *fbi;
3737         int i = 0, ret = 0, index = 0;
3738 /*
3739 #if defined(CONFIG_ROCKCHIP_IOMMU)
3740         struct device *mmu_dev = NULL;
3741 #endif
3742 */
3743         if (rk_fb->num_lcdc == RK30_MAX_LCDC_SUPPORT)
3744                 return -ENXIO;
3745
3746         for (i = 0; i < RK30_MAX_LCDC_SUPPORT; i++) {
3747                 if (!rk_fb->lcdc_dev_drv[i]) {
3748                         rk_fb->lcdc_dev_drv[i] = dev_drv;
3749                         rk_fb->lcdc_dev_drv[i]->id = id;
3750                         rk_fb->num_lcdc++;
3751                         break;
3752                 }
3753         }
3754
3755         index = i;
3756         init_lcdc_device_driver(rk_fb, win, index);
3757         dev_drv->fb_index_base = rk_fb->num_fb;
3758         for (i = 0; i < dev_drv->lcdc_win_num; i++) {
3759                 fbi = framebuffer_alloc(0, &fb_pdev->dev);
3760                 if (!fbi) {
3761                         dev_err(&fb_pdev->dev, "fb framebuffer_alloc fail!");
3762                         ret = -ENOMEM;
3763                 }
3764                 fbi->par = dev_drv;
3765                 fbi->var = def_var;
3766                 fbi->fix = def_fix;
3767                 sprintf(fbi->fix.id, "fb%d", rk_fb->num_fb);
3768                 fb_videomode_to_var(&fbi->var, &dev_drv->cur_screen->mode);
3769                 fbi->var.grayscale |=
3770                     (fbi->var.xres << 8) + (fbi->var.yres << 20);
3771 #if defined(CONFIG_LOGO_LINUX_BMP)
3772                 fbi->var.bits_per_pixel = 32;
3773 #else
3774                 fbi->var.bits_per_pixel = 16;
3775 #endif
3776                 fbi->fix.line_length =
3777                     (fbi->var.xres_virtual) * (fbi->var.bits_per_pixel >> 3);
3778                 fbi->var.width = dev_drv->cur_screen->width;
3779                 fbi->var.height = dev_drv->cur_screen->height;
3780                 fbi->var.pixclock = dev_drv->pixclock;
3781                 if (dev_drv->iommu_enabled)
3782                         fb_ops.fb_mmap = rk_fb_mmap;
3783                 fbi->fbops = &fb_ops;
3784                 fbi->flags = FBINFO_FLAG_DEFAULT;
3785                 fbi->pseudo_palette = dev_drv->win[i]->pseudo_pal;
3786                 ret = register_framebuffer(fbi);
3787                 if (ret < 0) {
3788                         dev_err(&fb_pdev->dev,
3789                                 "%s fb%d register_framebuffer fail!\n",
3790                                 __func__, rk_fb->num_fb);
3791                         return ret;
3792                 }
3793                 rkfb_create_sysfs(fbi);
3794                 rk_fb->fb[rk_fb->num_fb] = fbi;
3795                 dev_info(fbi->dev, "rockchip framebuffer registerd:%s\n",
3796                          fbi->fix.id);
3797                 rk_fb->num_fb++;
3798
3799                 if (i == 0) {
3800                         init_waitqueue_head(&dev_drv->vsync_info.wait);
3801                         init_waitqueue_head(&dev_drv->update_regs_wait);
3802                         ret = device_create_file(fbi->dev, &dev_attr_vsync);
3803                         if (ret)
3804                                 dev_err(fbi->dev,
3805                                         "failed to create vsync file\n");
3806                         dev_drv->vsync_info.thread =
3807                             kthread_run(rk_fb_wait_for_vsync_thread, dev_drv,
3808                                         "fb-vsync");
3809                         if (dev_drv->vsync_info.thread == ERR_PTR(-ENOMEM)) {
3810                                 dev_err(fbi->dev,
3811                                         "failed to run vsync thread\n");
3812                                 dev_drv->vsync_info.thread = NULL;
3813                         }
3814                         dev_drv->vsync_info.active = 1;
3815
3816                         mutex_init(&dev_drv->output_lock);
3817
3818                         INIT_LIST_HEAD(&dev_drv->update_regs_list);
3819                         mutex_init(&dev_drv->update_regs_list_lock);
3820                         init_kthread_worker(&dev_drv->update_regs_worker);
3821
3822                         dev_drv->update_regs_thread =
3823                             kthread_run(kthread_worker_fn,
3824                                         &dev_drv->update_regs_worker, "rk-fb");
3825                         if (IS_ERR(dev_drv->update_regs_thread)) {
3826                                 int err = PTR_ERR(dev_drv->update_regs_thread);
3827                                 dev_drv->update_regs_thread = NULL;
3828
3829                                 printk("failed to run update_regs thread\n");
3830                                 return err;
3831                         }
3832                         init_kthread_work(&dev_drv->update_regs_work,
3833                                           rk_fb_update_regs_handler);
3834
3835                         dev_drv->timeline =
3836                             sw_sync_timeline_create("fb-timeline");
3837                         dev_drv->timeline_max = 1;
3838                 }
3839         }
3840
3841         /* show logo for primary display device */
3842 #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
3843         if (dev_drv->prop == PRMRY) {
3844                 struct fb_info *main_fbi = rk_fb->fb[0];
3845                 main_fbi->fbops->fb_open(main_fbi, 1);
3846
3847 #if defined(CONFIG_ROCKCHIP_IOMMU)
3848                 if (dev_drv->iommu_enabled) {
3849                         if (dev_drv->mmu_dev)
3850                                 rockchip_iovmm_set_fault_handler(dev_drv->dev,
3851                                                 rk_fb_sysmmu_fault_handler);
3852                 }
3853 #endif
3854
3855                 rk_fb_alloc_buffer(main_fbi, 0);        /* only alloc memory for main fb */
3856                 if (support_uboot_display()) {
3857                         if (dev_drv->iommu_enabled) {
3858                                 rk_fb_copy_from_loader(main_fbi);
3859                                 dev_drv->ops->direct_set_addr(dev_drv, 0,
3860                                         main_fbi->fix.smem_start);
3861                         }
3862                         return 0;
3863                 }
3864                 main_fbi->fbops->fb_set_par(main_fbi);
3865 #if  defined(CONFIG_LOGO_LINUX_BMP)
3866                 if (fb_prewine_bmp_logo(main_fbi, FB_ROTATE_UR)) {
3867                         fb_set_cmap(&main_fbi->cmap, main_fbi);
3868                         fb_show_bmp_logo(main_fbi, FB_ROTATE_UR);
3869                 }
3870 #else
3871                 if (fb_prepare_logo(main_fbi, FB_ROTATE_UR)) {
3872                         fb_set_cmap(&main_fbi->cmap, main_fbi);
3873                         fb_show_logo(main_fbi, FB_ROTATE_UR);
3874                 }
3875 #endif
3876                 main_fbi->fbops->fb_pan_display(&main_fbi->var, main_fbi);
3877         } else {
3878 #if !defined(CONFIG_ROCKCHIP_IOMMU)
3879                 struct fb_info *extend_fbi = rk_fb->fb[rk_fb->num_fb >> 1];
3880                 int extend_fb_id = get_extend_fb_id(extend_fbi);
3881
3882                 rk_fb_alloc_buffer(extend_fbi, extend_fb_id);
3883 #endif
3884         }
3885 #endif
3886         return 0;
3887 }
3888
3889 int rk_fb_unregister(struct rk_lcdc_driver *dev_drv)
3890 {
3891         struct rk_fb *fb_inf = platform_get_drvdata(fb_pdev);
3892         struct fb_info *fbi;
3893         int fb_index_base = dev_drv->fb_index_base;
3894         int fb_num = dev_drv->lcdc_win_num;
3895         int i = 0;
3896
3897         if (fb_inf->lcdc_dev_drv[i]->vsync_info.thread) {
3898                 fb_inf->lcdc_dev_drv[i]->vsync_info.irq_stop = 1;
3899                 kthread_stop(fb_inf->lcdc_dev_drv[i]->vsync_info.thread);
3900         }
3901
3902         for (i = 0; i < fb_num; i++)
3903                 kfree(dev_drv->win[i]);
3904
3905         for (i = fb_index_base; i < (fb_index_base + fb_num); i++) {
3906                 fbi = fb_inf->fb[i];
3907                 unregister_framebuffer(fbi);
3908                 /* rk_release_fb_buffer(fbi); */
3909                 framebuffer_release(fbi);
3910         }
3911         fb_inf->lcdc_dev_drv[dev_drv->id] = NULL;
3912         fb_inf->num_lcdc--;
3913
3914         return 0;
3915 }
3916
3917 static int rk_fb_probe(struct platform_device *pdev)
3918 {
3919         struct rk_fb *rk_fb = NULL;
3920         struct device_node *np = pdev->dev.of_node;
3921         u32 mode;
3922
3923         if (!np) {
3924                 dev_err(&pdev->dev, "Missing device tree node.\n");
3925                 return -EINVAL;
3926         }
3927
3928         rk_fb = devm_kzalloc(&pdev->dev, sizeof(struct rk_fb), GFP_KERNEL);
3929         if (!rk_fb) {
3930                 dev_err(&pdev->dev, "kmalloc for rk fb fail!");
3931                 return -ENOMEM;
3932         }
3933         platform_set_drvdata(pdev, rk_fb);
3934
3935         if (!of_property_read_u32(np, "rockchip,disp-mode", &mode)) {
3936                 rk_fb->disp_mode = mode;
3937
3938         } else {
3939                 dev_err(&pdev->dev, "no disp-mode node found!");
3940                 return -ENODEV;
3941         }
3942
3943         if (!of_property_read_u32(np, "rockchip,uboot-logo-on", &uboot_logo_on))
3944                 printk(KERN_DEBUG "uboot-logo-on:%d\n", uboot_logo_on);
3945
3946         dev_set_name(&pdev->dev, "rockchip-fb");
3947 #if defined(CONFIG_ION_ROCKCHIP)
3948         rk_fb->ion_client = rockchip_ion_client_create("rk_fb");
3949         if (IS_ERR(rk_fb->ion_client)) {
3950                 dev_err(&pdev->dev, "failed to create ion client for rk fb");
3951                 return PTR_ERR(rk_fb->ion_client);
3952         } else {
3953                 dev_info(&pdev->dev, "rk fb ion client create success!\n");
3954         }
3955 #endif
3956
3957         fb_pdev = pdev;
3958         dev_info(&pdev->dev, "rockchip framebuffer driver probe\n");
3959         return 0;
3960 }
3961
3962 static int rk_fb_remove(struct platform_device *pdev)
3963 {
3964         struct rk_fb *rk_fb = platform_get_drvdata(pdev);
3965
3966         kfree(rk_fb);
3967         platform_set_drvdata(pdev, NULL);
3968         return 0;
3969 }
3970
3971 static void rk_fb_shutdown(struct platform_device *pdev)
3972 {
3973         struct rk_fb *rk_fb = platform_get_drvdata(pdev);
3974         int i;
3975
3976         for (i = 0; i < rk_fb->num_lcdc; i++) {
3977                 if (!rk_fb->lcdc_dev_drv[i])
3978                         continue;
3979         }
3980 }
3981
3982 static const struct of_device_id rkfb_dt_ids[] = {
3983         {.compatible = "rockchip,rk-fb",},
3984         {}
3985 };
3986
3987 static struct platform_driver rk_fb_driver = {
3988         .probe = rk_fb_probe,
3989         .remove = rk_fb_remove,
3990         .driver = {
3991                    .name = "rk-fb",
3992                    .owner = THIS_MODULE,
3993                    .of_match_table = of_match_ptr(rkfb_dt_ids),
3994                    },
3995         .shutdown = rk_fb_shutdown,
3996 };
3997
3998 static int __init rk_fb_init(void)
3999 {
4000         return platform_driver_register(&rk_fb_driver);
4001 }
4002
4003 static void __exit rk_fb_exit(void)
4004 {
4005         platform_driver_unregister(&rk_fb_driver);
4006 }
4007
4008 fs_initcall(rk_fb_init);
4009 module_exit(rk_fb_exit);