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