2 * drivers/video/rockchip/rk_fb.c
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.
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.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/kthread.h>
26 #include <linux/init.h>
27 #include <asm/div64.h>
28 #include <linux/uaccess.h>
29 #include <linux/rk_fb.h>
30 #include <linux/linux_logo.h>
31 #include <linux/dma-mapping.h>
33 #if defined(CONFIG_RK_HDMI)
34 #include "hdmi/rk_hdmi.h"
37 #if defined(CONFIG_ROCKCHIP_RGA) || defined(CONFIG_ROCKCHIP_RGA2)
43 #include <linux/of_platform.h>
44 #include <linux/of_gpio.h>
45 #include <video/of_display_timing.h>
46 #include <video/display_timing.h>
47 #include <dt-bindings/rkfb/rk_fb.h>
50 #if defined(CONFIG_ION_ROCKCHIP)
51 #include <linux/rockchip_ion.h>
52 #include <linux/rockchip-iovmm.h>
53 #include <linux/dma-buf.h>
54 #include <linux/highmem.h>
58 static int hdmi_switch_complete;
59 static struct platform_device *fb_pdev;
60 struct list_head saved_list;
62 #if defined(CONFIG_FB_MIRRORING)
63 int (*video_data_to_mirroring) (struct fb_info *info, u32 yuv_phy[2]);
64 EXPORT_SYMBOL(video_data_to_mirroring);
67 struct rk_fb_reg_win_data g_reg_win_data[4];
68 static int g_last_win_num;
69 static int g_first_buf = 1;
70 static struct rk_fb_trsm_ops *trsm_lvds_ops;
71 static struct rk_fb_trsm_ops *trsm_edp_ops;
72 static struct rk_fb_trsm_ops *trsm_mipi_ops;
73 static int uboot_logo_on;
74 int support_uboot_display(void)
79 int rk_fb_trsm_ops_register(struct rk_fb_trsm_ops *ops, int type)
84 case SCREEN_DUAL_LVDS:
91 case SCREEN_DUAL_MIPI:
95 printk(KERN_WARNING "%s:un supported transmitter:%d!\n",
102 struct rk_fb_trsm_ops *rk_fb_trsm_ops_get(int type)
104 struct rk_fb_trsm_ops *ops;
108 case SCREEN_DUAL_LVDS:
115 case SCREEN_DUAL_MIPI:
120 printk(KERN_WARNING "%s:un supported transmitter:%d!\n",
127 int rk_fb_pixel_width(int data_format)
130 switch (data_format) {
153 printk(KERN_WARNING "%s:un supported format:0x%x\n",
154 __func__, data_format);
160 static int rk_fb_data_fmt(int data_format, int bits_per_pixel)
164 switch (data_format) {
165 case HAL_PIXEL_FORMAT_RGBX_8888:
166 fb_data_fmt = XBGR888;
168 case HAL_PIXEL_FORMAT_RGBA_8888:
169 fb_data_fmt = ABGR888;
171 case HAL_PIXEL_FORMAT_BGRA_8888:
172 fb_data_fmt = ARGB888;
174 case HAL_PIXEL_FORMAT_RGB_888:
175 fb_data_fmt = RGB888;
177 case HAL_PIXEL_FORMAT_RGB_565:
178 fb_data_fmt = RGB565;
180 case HAL_PIXEL_FORMAT_YCbCr_422_SP: /* yuv422 */
181 fb_data_fmt = YUV422;
183 case HAL_PIXEL_FORMAT_YCrCb_NV12: /* YUV420---uvuvuv */
184 fb_data_fmt = YUV420;
186 case HAL_PIXEL_FORMAT_YCrCb_444: /* yuv444 */
187 fb_data_fmt = YUV444;
189 case HAL_PIXEL_FORMAT_YCrCb_NV12_10: /* yuv444 */
190 fb_data_fmt = YUV420_A;
192 case HAL_PIXEL_FORMAT_YCbCr_422_SP_10: /* yuv444 */
193 fb_data_fmt = YUV422_A;
195 case HAL_PIXEL_FORMAT_YCrCb_420_SP_10: /* yuv444 */
196 fb_data_fmt = YUV444_A;
199 printk(KERN_WARNING "%s:un supported format:0x%x\n",
200 __func__, data_format);
204 switch (bits_per_pixel) {
206 fb_data_fmt = ARGB888;
209 fb_data_fmt = RGB565;
213 "%s:un supported bits_per_pixel:%d\n", __func__,
222 * rk display power control parse from dts
224 int rk_disp_pwr_ctr_parse_dt(struct rk_lcdc_driver *dev_drv)
226 struct device_node *root = of_get_child_by_name(dev_drv->dev->of_node,
228 struct device_node *child;
229 struct rk_disp_pwr_ctr_list *pwr_ctr;
230 struct list_head *pos;
231 enum of_gpio_flags flags;
236 INIT_LIST_HEAD(&dev_drv->pwrlist_head);
238 dev_err(dev_drv->dev, "can't find power_ctr node for lcdc%d\n",
243 for_each_child_of_node(root, child) {
244 pwr_ctr = kmalloc(sizeof(struct rk_disp_pwr_ctr_list),
246 strcpy(pwr_ctr->pwr_ctr.name, child->name);
247 if (!of_property_read_u32(child, "rockchip,power_type", &val)) {
249 pwr_ctr->pwr_ctr.type = GPIO;
250 pwr_ctr->pwr_ctr.gpio = of_get_gpio_flags(child, 0, &flags);
251 if (!gpio_is_valid(pwr_ctr->pwr_ctr.gpio)) {
252 dev_err(dev_drv->dev, "%s ivalid gpio\n",
256 pwr_ctr->pwr_ctr.atv_val = !(flags & OF_GPIO_ACTIVE_LOW);
257 ret = gpio_request(pwr_ctr->pwr_ctr.gpio,
260 dev_err(dev_drv->dev,
261 "request %s gpio fail:%d\n",
266 pwr_ctr->pwr_ctr.type = REGULATOR;
270 of_property_read_u32(child, "rockchip,delay", &val);
271 pwr_ctr->pwr_ctr.delay = val;
272 list_add_tail(&pwr_ctr->list, &dev_drv->pwrlist_head);
275 of_property_read_u32(root, "rockchip,debug", &debug);
278 list_for_each(pos, &dev_drv->pwrlist_head) {
279 pwr_ctr = list_entry(pos, struct rk_disp_pwr_ctr_list,
281 printk(KERN_INFO "pwr_ctr_name:%s\n"
286 pwr_ctr->pwr_ctr.name,
287 (pwr_ctr->pwr_ctr.type == GPIO) ? "gpio" : "regulator",
288 pwr_ctr->pwr_ctr.gpio,
289 pwr_ctr->pwr_ctr.atv_val,
290 pwr_ctr->pwr_ctr.delay);
298 int rk_disp_pwr_enable(struct rk_lcdc_driver *dev_drv)
300 struct list_head *pos;
301 struct rk_disp_pwr_ctr_list *pwr_ctr_list;
302 struct pwr_ctr *pwr_ctr;
303 if (list_empty(&dev_drv->pwrlist_head))
305 list_for_each(pos, &dev_drv->pwrlist_head) {
306 pwr_ctr_list = list_entry(pos, struct rk_disp_pwr_ctr_list,
308 pwr_ctr = &pwr_ctr_list->pwr_ctr;
309 if (pwr_ctr->type == GPIO) {
310 gpio_direction_output(pwr_ctr->gpio, pwr_ctr->atv_val);
311 mdelay(pwr_ctr->delay);
318 int rk_disp_pwr_disable(struct rk_lcdc_driver *dev_drv)
320 struct list_head *pos;
321 struct rk_disp_pwr_ctr_list *pwr_ctr_list;
322 struct pwr_ctr *pwr_ctr;
323 if (list_empty(&dev_drv->pwrlist_head))
325 list_for_each(pos, &dev_drv->pwrlist_head) {
326 pwr_ctr_list = list_entry(pos, struct rk_disp_pwr_ctr_list,
328 pwr_ctr = &pwr_ctr_list->pwr_ctr;
329 if (pwr_ctr->type == GPIO)
330 gpio_set_value(pwr_ctr->gpio, !pwr_ctr->atv_val);
335 int rk_fb_video_mode_from_timing(const struct display_timing *dt,
336 struct rk_screen *screen)
338 screen->mode.pixclock = dt->pixelclock.typ;
339 screen->mode.left_margin = dt->hback_porch.typ;
340 screen->mode.right_margin = dt->hfront_porch.typ;
341 screen->mode.xres = dt->hactive.typ;
342 screen->mode.hsync_len = dt->hsync_len.typ;
343 screen->mode.upper_margin = dt->vback_porch.typ;
344 screen->mode.lower_margin = dt->vfront_porch.typ;
345 screen->mode.yres = dt->vactive.typ;
346 screen->mode.vsync_len = dt->vsync_len.typ;
347 screen->type = dt->screen_type;
348 screen->lvds_format = dt->lvds_format;
349 screen->face = dt->face;
350 screen->color_mode = dt->color_mode;
351 screen->dsp_lut = dt->dsp_lut;
353 if (dt->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
354 screen->pin_dclk = 1;
356 screen->pin_dclk = 0;
357 if (dt->flags & DISPLAY_FLAGS_HSYNC_HIGH)
358 screen->pin_hsync = 1;
360 screen->pin_hsync = 0;
361 if (dt->flags & DISPLAY_FLAGS_VSYNC_HIGH)
362 screen->pin_vsync = 1;
364 screen->pin_vsync = 0;
365 if (dt->flags & DISPLAY_FLAGS_DE_HIGH)
374 int rk_fb_prase_timing_dt(struct device_node *np, struct rk_screen *screen)
376 struct display_timings *disp_timing;
377 struct display_timing *dt;
378 disp_timing = of_get_display_timings(np);
380 pr_err("parse display timing err\n");
383 dt = display_timings_get(disp_timing, disp_timing->native_mode);
384 rk_fb_video_mode_from_timing(dt, screen);
389 int rk_fb_calc_fps(struct rk_screen *screen, u32 pixclock)
392 unsigned long long hz;
394 printk(KERN_ERR "%s:null screen!\n", __func__);
397 x = screen->mode.xres + screen->mode.left_margin +
398 screen->mode.right_margin + screen->mode.hsync_len;
399 y = screen->mode.yres + screen->mode.upper_margin +
400 screen->mode.lower_margin + screen->mode.vsync_len;
402 hz = 1000000000000ULL; /* 1e12 picoseconds per second */
405 do_div(hz, x * y); /* divide by x * y with rounding */
408 do_div(hz, pixclock); /* divide by pixclock with rounding */
413 char *get_format_string(enum data_format format, char *fmt)
419 strcpy(fmt, "ARGB888");
422 strcpy(fmt, "RGB888");
425 strcpy(fmt, "RGB565");
428 strcpy(fmt, "YUV420");
431 strcpy(fmt, "YUV422");
434 strcpy(fmt, "YUV444");
437 strcpy(fmt, "YUV420_A");
440 strcpy(fmt, "YUV422_A");
443 strcpy(fmt, "YUV444_A");
446 strcpy(fmt, "XRGB888");
449 strcpy(fmt, "XBGR888");
452 strcpy(fmt, "XBGR888");
455 strcpy(fmt, "invalid");
465 * name: lcdc device name ,lcdc0 , lcdc1
467 struct rk_lcdc_driver *rk_get_lcdc_drv(char *name)
469 struct rk_fb *inf = NULL;
470 struct rk_lcdc_driver *dev_drv = NULL;
474 inf = platform_get_drvdata(fb_pdev);
478 for (i = 0; i < inf->num_lcdc; i++) {
479 if (!strcmp(inf->lcdc_dev_drv[i]->name, name)) {
480 dev_drv = inf->lcdc_dev_drv[i];
488 static struct rk_lcdc_driver *rk_get_prmry_lcdc_drv(void)
490 struct rk_fb *inf = NULL;
491 struct rk_lcdc_driver *dev_drv = NULL;
495 inf = platform_get_drvdata(fb_pdev);
499 for (i = 0; i < inf->num_lcdc; i++) {
500 if (inf->lcdc_dev_drv[i]->prop == PRMRY) {
501 dev_drv = inf->lcdc_dev_drv[i];
510 * get one frame time of the prmry screen, unit: us
512 u32 rk_fb_get_prmry_screen_ft(void)
514 struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
515 uint32_t htotal, vtotal, pixclock_ps;
516 u64 pix_total, ft_us;
518 if (unlikely(!dev_drv))
521 pixclock_ps = dev_drv->pixclock;
523 vtotal = (dev_drv->cur_screen->mode.upper_margin +
524 dev_drv->cur_screen->mode.lower_margin +
525 dev_drv->cur_screen->mode.yres +
526 dev_drv->cur_screen->mode.vsync_len);
527 htotal = (dev_drv->cur_screen->mode.left_margin +
528 dev_drv->cur_screen->mode.right_margin +
529 dev_drv->cur_screen->mode.xres +
530 dev_drv->cur_screen->mode.hsync_len);
531 pix_total = htotal * vtotal;
532 ft_us = pix_total * pixclock_ps;
533 do_div(ft_us, 1000000);
534 if (dev_drv->frame_time.ft == 0)
535 dev_drv->frame_time.ft = ft_us;
537 ft_us = dev_drv->frame_time.framedone_t - dev_drv->frame_time.last_framedone_t;
539 ft_us = min(dev_drv->frame_time.ft, (u32)ft_us);
541 dev_drv->frame_time.ft = ft_us;
543 return dev_drv->frame_time.ft;
547 * get the vblanking time of the prmry screen, unit: us
549 u32 rk_fb_get_prmry_screen_vbt(void)
551 struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
552 uint32_t htotal, vblank, pixclock_ps;
553 u64 pix_blank, vbt_us;
555 if (unlikely(!dev_drv))
558 pixclock_ps = dev_drv->pixclock;
560 htotal = (dev_drv->cur_screen->mode.left_margin +
561 dev_drv->cur_screen->mode.right_margin +
562 dev_drv->cur_screen->mode.xres +
563 dev_drv->cur_screen->mode.hsync_len);
564 vblank = (dev_drv->cur_screen->mode.upper_margin +
565 dev_drv->cur_screen->mode.lower_margin +
566 dev_drv->cur_screen->mode.vsync_len);
567 pix_blank = htotal * vblank;
568 vbt_us = pix_blank * pixclock_ps;
569 do_div(vbt_us, 1000000);
574 * get the frame done time of the prmry screen, unit: us
576 u64 rk_fb_get_prmry_screen_framedone_t(void)
578 struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
580 if (unlikely(!dev_drv))
583 return dev_drv->frame_time.framedone_t;
587 * set prmry screen status
589 int rk_fb_set_prmry_screen_status(int status)
591 struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
592 struct rk_screen *screen;
594 if (unlikely(!dev_drv))
597 screen = dev_drv->cur_screen;
599 case SCREEN_PREPARE_DDR_CHANGE:
600 if (screen->type == SCREEN_MIPI
601 || screen->type == SCREEN_DUAL_MIPI) {
602 if (dev_drv->trsm_ops->dsp_pwr_off)
603 dev_drv->trsm_ops->dsp_pwr_off();
606 case SCREEN_UNPREPARE_DDR_CHANGE:
607 if (screen->type == SCREEN_MIPI
608 || screen->type == SCREEN_DUAL_MIPI) {
609 if (dev_drv->trsm_ops->dsp_pwr_on)
610 dev_drv->trsm_ops->dsp_pwr_on();
620 static struct rk_lcdc_driver *rk_get_extend_lcdc_drv(void)
622 struct rk_fb *inf = NULL;
623 struct rk_lcdc_driver *dev_drv = NULL;
627 inf = platform_get_drvdata(fb_pdev);
631 for (i = 0; i < inf->num_lcdc; i++) {
632 if (inf->lcdc_dev_drv[i]->prop == EXTEND) {
633 dev_drv = inf->lcdc_dev_drv[i];
641 u32 rk_fb_get_prmry_screen_pixclock(void)
643 struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
645 if (unlikely(!dev_drv))
648 return dev_drv->pixclock;
651 int rk_fb_poll_prmry_screen_vblank(void)
653 struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
655 if (likely(dev_drv)) {
656 if (dev_drv->ops->poll_vblank)
657 return dev_drv->ops->poll_vblank(dev_drv);
659 return RK_LF_STATUS_NC;
661 return RK_LF_STATUS_NC;
665 bool rk_fb_poll_wait_frame_complete(void)
667 uint32_t timeout = RK_LF_MAX_TIMEOUT;
668 struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
670 if (likely(dev_drv)) {
671 if (dev_drv->ops->set_irq_to_cpu)
672 dev_drv->ops->set_irq_to_cpu(dev_drv, 0);
675 if (rk_fb_poll_prmry_screen_vblank() == RK_LF_STATUS_NC) {
676 if (likely(dev_drv)) {
677 if (dev_drv->ops->set_irq_to_cpu)
678 dev_drv->ops->set_irq_to_cpu(dev_drv, 1);
682 while (!(rk_fb_poll_prmry_screen_vblank() == RK_LF_STATUS_FR) && --timeout)
684 while (!(rk_fb_poll_prmry_screen_vblank() == RK_LF_STATUS_FC) && --timeout)
686 if (likely(dev_drv)) {
687 if (dev_drv->ops->set_irq_to_cpu)
688 dev_drv->ops->set_irq_to_cpu(dev_drv, 1);
695 /* rk_fb_get_sysmmu_device_by_compatible()
696 * @compt: dts device compatible name
697 * return value: success: pointer to the device inside of platform device
700 struct device *rk_fb_get_sysmmu_device_by_compatible(const char *compt)
702 struct device_node *dn = NULL;
703 struct platform_device *pd = NULL;
704 struct device *ret = NULL ;
706 dn = of_find_compatible_node(NULL, NULL, compt);
708 printk("can't find device node %s \r\n", compt);
712 pd = of_find_device_by_node(dn);
714 printk("can't find platform device in device node %s \r\n", compt);
722 #ifdef CONFIG_IOMMU_API
723 void rk_fb_platform_set_sysmmu(struct device *sysmmu, struct device *dev)
725 dev->archdata.iommu = sysmmu;
728 void rk_fb_platform_set_sysmmu(struct device *sysmmu, struct device *dev)
734 static int rk_fb_open(struct fb_info *info, int user)
736 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
739 win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
740 dev_drv->win[win_id]->logicalstate++;
741 /* if this win aready opened ,no need to reopen */
742 if (dev_drv->win[win_id]->state)
745 dev_drv->ops->open(dev_drv, win_id, 1);
749 static int get_extend_fb_id(struct fb_info *info)
752 char *id = info->fix.id;
753 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
755 if (!strcmp(id, "fb0"))
757 else if (!strcmp(id, "fb1"))
759 else if (!strcmp(id, "fb2") && (dev_drv->lcdc_win_num > 2))
764 static int rk_fb_close(struct fb_info *info, int user)
766 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
767 struct rk_lcdc_win *win = NULL;
768 int win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
771 dev_drv->win[win_id]->logicalstate--;
772 if (!dev_drv->win[win_id]->logicalstate) {
773 win = dev_drv->win[win_id];
774 info->fix.smem_start = win->reserved;
775 info->var.xres = dev_drv->screen0->mode.xres;
776 info->var.yres = dev_drv->screen0->mode.yres;
778 info->var.grayscale |=
779 (info->var.xres << 8) + (info->var.yres << 20);
781 info->var.xres_virtual = info->var.xres;
782 info->var.yres_virtual = info->var.yres;
783 #if defined(CONFIG_LOGO_LINUX_BMP)
784 info->var.bits_per_pixel = 32;
786 info->var.bits_per_pixel = 16;
788 info->fix.line_length =
789 (info->var.xres_virtual) * (info->var.bits_per_pixel >> 3);
790 info->var.width = dev_drv->screen0->width;
791 info->var.height = dev_drv->screen0->height;
792 info->var.pixclock = dev_drv->pixclock;
793 info->var.left_margin = dev_drv->screen0->mode.left_margin;
794 info->var.right_margin = dev_drv->screen0->mode.right_margin;
795 info->var.upper_margin = dev_drv->screen0->mode.upper_margin;
796 info->var.lower_margin = dev_drv->screen0->mode.lower_margin;
797 info->var.vsync_len = dev_drv->screen0->mode.vsync_len;
798 info->var.hsync_len = dev_drv->screen0->mode.hsync_len;
806 #if defined(CONFIG_RK29_IPP)
807 static int get_ipp_format(int fmt)
809 int ipp_fmt = IPP_XRGB_8888;
811 case HAL_PIXEL_FORMAT_RGBX_8888:
812 case HAL_PIXEL_FORMAT_RGBA_8888:
813 case HAL_PIXEL_FORMAT_BGRA_8888:
814 case HAL_PIXEL_FORMAT_RGB_888:
815 ipp_fmt = IPP_XRGB_8888;
817 case HAL_PIXEL_FORMAT_RGB_565:
818 ipp_fmt = IPP_RGB_565;
820 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
821 ipp_fmt = IPP_Y_CBCR_H2V1;
823 case HAL_PIXEL_FORMAT_YCrCb_NV12:
824 ipp_fmt = IPP_Y_CBCR_H2V2;
826 case HAL_PIXEL_FORMAT_YCrCb_444:
827 ipp_fmt = IPP_Y_CBCR_H1V1;
830 ipp_fmt = IPP_IMGTYPE_LIMIT;
837 static void ipp_win_check(int *dst_w, int *dst_h, int *dst_vir_w,
838 int rotation, int fmt)
843 if (fmt == IPP_XRGB_8888) {
846 } else if (fmt == IPP_RGB_565) {
853 align16 -= 1; /*for YUV, 1 */
854 align64 -= 1; /*for YUV, 7 */
856 if (rotation == IPP_ROT_0) {
857 if (fmt > IPP_RGB_565) {
858 if ((*dst_w & 1) != 0)
860 if ((*dst_h & 1) != 0)
862 if (*dst_vir_w < *dst_w)
866 if ((*dst_w & align64) != 0)
867 *dst_w = (*dst_w + align64) & (~align64);
868 if ((fmt > IPP_RGB_565) && ((*dst_h & 1) == 1))
870 if (*dst_vir_w < *dst_w)
875 static void fb_copy_by_ipp(struct fb_info *dst_info,
876 struct fb_info *src_info, int offset)
878 struct rk29_ipp_req ipp_req;
879 uint32_t rotation = 0;
880 int dst_w, dst_h, dst_vir_w;
882 u8 data_format = (dst_info->var.nonstd) & 0xff;
883 struct rk_lcdc_driver *ext_dev_drv =
884 (struct rk_lcdc_driver *)dst_info->par;
885 u16 orientation = ext_dev_drv->rotate_mode;
887 memset(&ipp_req, 0, sizeof(struct rk29_ipp_req));
889 switch (orientation) {
891 rotation = IPP_ROT_0;
894 rotation = IPP_ROT_90;
897 rotation = IPP_ROT_180;
900 rotation = IPP_ROT_270;
903 rotation = IPP_ROT_270;
907 dst_w = dst_info->var.xres;
908 dst_h = dst_info->var.yres;
909 dst_vir_w = dst_info->var.xres_virtual;
910 ipp_fmt = get_ipp_format(data_format);
911 ipp_win_check(&dst_w, &dst_h, &dst_vir_w, rotation, ipp_fmt);
912 ipp_req.src0.YrgbMst = src_info->fix.smem_start + offset;
913 ipp_req.src0.w = src_info->var.xres;
914 ipp_req.src0.h = src_info->var.yres;
915 ipp_req.src_vir_w = src_info->var.xres_virtual;
916 ipp_req.src0.fmt = ipp_fmt;
918 ipp_req.dst0.YrgbMst = dst_info->fix.smem_start + offset;
919 ipp_req.dst0.w = dst_w;
920 ipp_req.dst0.h = dst_h;
921 ipp_req.dst_vir_w = dst_vir_w;
922 ipp_req.dst0.fmt = ipp_fmt;
924 ipp_req.timeout = 100;
925 ipp_req.flag = rotation;
926 ipp_blit_sync(&ipp_req);
931 #if defined(CONFIG_ROCKCHIP_RGA) || defined(CONFIG_ROCKCHIP_RGA2)
932 static int get_rga_format(int fmt)
938 rga_fmt = RK_FORMAT_RGBX_8888;
941 rga_fmt = RK_FORMAT_RGBA_8888;
944 rga_fmt = RK_FORMAT_BGRA_8888;
947 rga_fmt = RK_FORMAT_RGB_888;
950 rga_fmt = RK_FORMAT_RGB_565;
953 rga_fmt = RK_FORMAT_YCbCr_422_SP;
956 rga_fmt = RK_FORMAT_YCbCr_420_SP;
959 rga_fmt = RK_FORMAT_RGBA_8888;
966 static void rga_win_check(struct rk_lcdc_win *dst_win,
967 struct rk_lcdc_win *src_win)
972 /*width and height must be aligned by 32 bit */
973 if ((src_win->area[0].xact & align32) != 0)
974 src_win->area[0].xact =
975 (src_win->area[0].xact + align32) & (~align32);
976 if ((src_win->area[0].yact & align32) != 0)
977 src_win->area[0].yact =
978 (src_win->area[0].yact + align32) & (~align32);
979 if (src_win->area[0].xvir < src_win->area[0].xact)
980 src_win->area[0].xvir = src_win->area[0].xact;
981 if (src_win->area[0].yvir < src_win->area[0].yact)
982 src_win->area[0].yvir = src_win->area[0].yact;
984 if ((dst_win->area[0].xact & align32) != 0)
985 dst_win->area[0].xact =
986 (dst_win->area[0].xact + align32) & (~align32);
987 if ((dst_win->area[0].yact & align32) != 0)
988 dst_win->area[0].yact =
989 (dst_win->area[0].yact + align32) & (~align32);
990 if (dst_win->area[0].xvir < dst_win->area[0].xact)
991 dst_win->area[0].xvir = dst_win->area[0].xact;
992 if (dst_win->area[0].yvir < dst_win->area[0].yact)
993 dst_win->area[0].yvir = dst_win->area[0].yact;
996 static void win_copy_by_rga(struct rk_lcdc_win *dst_win,
997 struct rk_lcdc_win *src_win,
998 u16 orientation, int iommu_en)
1000 struct rga_req Rga_Request;
1004 memset(&Rga_Request, 0, sizeof(Rga_Request));
1005 rga_win_check(dst_win, src_win);
1007 switch (orientation) {
1009 Rga_Request.rotate_mode = 1;
1010 Rga_Request.sina = 65536;
1011 Rga_Request.cosa = 0;
1012 Rga_Request.dst.act_w = dst_win->area[0].yact;
1013 Rga_Request.dst.act_h = dst_win->area[0].xact;
1014 Rga_Request.dst.x_offset = dst_win->area[0].xact - 1;
1015 Rga_Request.dst.y_offset = 0;
1018 Rga_Request.rotate_mode = 1;
1019 Rga_Request.sina = 0;
1020 Rga_Request.cosa = -65536;
1021 Rga_Request.dst.act_w = dst_win->area[0].xact;
1022 Rga_Request.dst.act_h = dst_win->area[0].yact;
1023 Rga_Request.dst.x_offset = dst_win->area[0].xact - 1;
1024 Rga_Request.dst.y_offset = dst_win->area[0].yact - 1;
1027 Rga_Request.rotate_mode = 1;
1028 Rga_Request.sina = -65536;
1029 Rga_Request.cosa = 0;
1030 Rga_Request.dst.act_w = dst_win->area[0].yact;
1031 Rga_Request.dst.act_h = dst_win->area[0].xact;
1032 Rga_Request.dst.x_offset = 0;
1033 Rga_Request.dst.y_offset = dst_win->area[0].yact - 1;
1036 Rga_Request.rotate_mode = 0;
1037 Rga_Request.dst.act_w = dst_win->area[0].xact;
1038 Rga_Request.dst.act_h = dst_win->area[0].yact;
1039 Rga_Request.dst.x_offset = dst_win->area[0].xact - 1;
1040 Rga_Request.dst.y_offset = dst_win->area[0].yact - 1;
1045 fd = ion_share_dma_buf_fd(rk_fb->ion_client, src_win->area[0].ion_hdl);
1046 Rga_Request.src.yrgb_addr = fd;
1047 fd = ion_share_dma_buf_fd(rk_fb->ion_client, dst_win->area[0].ion_hdl);
1048 Rga_Request.dst.yrgb_addr = fd;
1050 Rga_Request.src.yrgb_addr = 0;
1051 Rga_Request.src.uv_addr =
1052 src_win->area[0].smem_start + src_win->area[0].y_offset;
1053 Rga_Request.src.v_addr = 0;
1055 Rga_Request.dst.yrgb_addr = 0;
1056 Rga_Request.dst.uv_addr =
1057 dst_win->area[0].smem_start + dst_win->area[0].y_offset;
1058 Rga_Request.dst.v_addr = 0;
1060 Rga_Request.src.vir_w = src_win->area[0].xvir;
1061 Rga_Request.src.vir_h = src_win->area[0].yvir;
1062 Rga_Request.src.format = get_rga_format(src_win->format);
1063 Rga_Request.src.act_w = src_win->area[0].xact;
1064 Rga_Request.src.act_h = src_win->area[0].yact;
1065 Rga_Request.src.x_offset = 0;
1066 Rga_Request.src.y_offset = 0;
1068 Rga_Request.dst.vir_w = dst_win->area[0].xvir;
1069 Rga_Request.dst.vir_h = dst_win->area[0].yvir;
1070 Rga_Request.dst.format = get_rga_format(dst_win->format);
1072 Rga_Request.clip.xmin = 0;
1073 Rga_Request.clip.xmax = dst_win->area[0].xact - 1;
1074 Rga_Request.clip.ymin = 0;
1075 Rga_Request.clip.ymax = dst_win->area[0].yact - 1;
1076 Rga_Request.scale_mode = 0;
1077 #if defined(CONFIG_ROCKCHIP_IOMMU)
1079 Rga_Request.mmu_info.mmu_en = 1;
1080 Rga_Request.mmu_info.mmu_flag = 1;
1082 Rga_Request.mmu_info.mmu_en = 0;
1083 Rga_Request.mmu_info.mmu_flag = 0;
1086 Rga_Request.mmu_info.mmu_en = 0;
1087 Rga_Request.mmu_info.mmu_flag = 0;
1090 ret = rga_ioctl_kernel(&Rga_Request);
1094 * This function is used for copying fb by RGA Module
1095 * RGA only support copy RGB to RGB
1096 * RGA2 support copy RGB to RGB and YUV to YUV
1098 static void fb_copy_by_rga(struct fb_info *dst_info, struct fb_info *src_info,
1101 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)src_info->par;
1102 struct rk_lcdc_driver *ext_dev_drv =
1103 (struct rk_lcdc_driver *)dst_info->par;
1104 int win_id = 0, ext_win_id;
1105 struct rk_lcdc_win *src_win, *dst_win;
1107 win_id = dev_drv->ops->fb_get_win_id(dev_drv, src_info->fix.id);
1108 src_win = dev_drv->win[win_id];
1111 ext_dev_drv->ops->fb_get_win_id(ext_dev_drv, dst_info->fix.id);
1112 dst_win = ext_dev_drv->win[ext_win_id];
1114 win_copy_by_rga(dst_win, src_win, ext_dev_drv->rotate_mode,
1115 ext_dev_drv->iommu_enabled);
1120 static int rk_fb_rotate(struct fb_info *dst_info,
1121 struct fb_info *src_info, int offset)
1123 #if defined(CONFIG_RK29_IPP)
1124 fb_copy_by_ipp(dst_info, src_info, offset);
1125 #elif defined(CONFIG_ROCKCHIP_RGA) || defined(CONFIG_ROCKCHIP_RGA2)
1126 fb_copy_by_rga(dst_info, src_info, offset);
1133 static int rk_fb_win_rotate(struct rk_lcdc_win *dst_win,
1134 struct rk_lcdc_win *src_win,
1135 u16 rotate, int iommu_en)
1137 #if defined(CONFIG_ROCKCHIP_RGA) || defined(CONFIG_ROCKCHIP_RGA2)
1138 win_copy_by_rga(dst_win, src_win, rotate, iommu_en);
1146 static int rk_fb_pan_display(struct fb_var_screeninfo *var,
1147 struct fb_info *info)
1149 struct rk_fb *rk_fb = dev_get_drvdata(info->device);
1150 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
1151 struct fb_fix_screeninfo *fix = &info->fix;
1152 struct fb_info *extend_info = NULL;
1153 struct rk_lcdc_driver *extend_dev_drv = NULL;
1154 int win_id = 0, extend_win_id = 0;
1155 struct rk_lcdc_win *extend_win = NULL;
1156 struct rk_lcdc_win *win = NULL;
1157 struct rk_screen *screen = dev_drv->cur_screen;
1160 u32 xoffset = var->xoffset;
1161 u32 yoffset = var->yoffset;
1162 u32 xvir = var->xres_virtual;
1163 /* u32 yvir = var->yres_virtual; */
1164 /* u8 data_format = var->nonstd&0xff; */
1168 u32 stride, uv_stride;
1171 u16 uv_x_off, uv_y_off, uv_y_act;
1173 #ifdef CONFIG_ROCKCHIP_IOMMU
1174 ion_phys_addr_t phy_addr;
1179 win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
1183 win = dev_drv->win[win_id];
1185 if (rk_fb->disp_mode == DUAL) {
1186 fb_id = get_extend_fb_id(info);
1187 extend_info = rk_fb->fb[(rk_fb->num_fb >> 1) + fb_id];
1188 extend_dev_drv = (struct rk_lcdc_driver *)extend_info->par;
1189 extend_win_id = dev_drv->ops->fb_get_win_id(extend_dev_drv,
1190 extend_info->fix.id);
1191 extend_win = extend_dev_drv->win[extend_win_id];
1194 pixel_width = rk_fb_pixel_width(win->format);
1195 vir_width_bit = pixel_width * xvir;
1196 /* pixel_width = byte_num * 8 */
1197 stride_32bit_1 = ALIGN_N_TIMES(vir_width_bit, 32) / 8;
1198 stride_32bit_2 = ALIGN_N_TIMES(vir_width_bit * 2, 32) / 8;
1200 switch (win->format) {
1204 stride = stride_32bit_1;
1205 uv_stride = stride_32bit_1 >> 1;
1206 uv_x_off = xoffset >> 1;
1208 fix->line_length = stride;
1209 uv_y_act = win->area[0].yact >> 1;
1211 case YUV420: /* 420sp */
1214 stride = stride_32bit_1;
1215 uv_stride = stride_32bit_1;
1217 uv_y_off = yoffset >> 1;
1218 fix->line_length = stride;
1219 uv_y_act = win->area[0].yact >> 1;
1224 stride = stride_32bit_1;
1225 uv_stride = stride_32bit_2;
1226 uv_x_off = xoffset * 2;
1228 fix->line_length = stride << 2;
1229 uv_y_act = win->area[0].yact;
1232 stride = stride_32bit_1; /* default rgb */
1233 fix->line_length = stride;
1237 /* x y mirror ,jump line */
1238 if (screen->y_mirror == 1) {
1239 if (screen->interlace == 1) {
1240 win->area[0].y_offset = yoffset * stride * 2 +
1241 ((win->area[0].yact - 1) * 2 + 1) * stride +
1242 xoffset * pixel_width / 8;
1244 win->area[0].y_offset = yoffset * stride +
1245 (win->area[0].yact - 1) * stride +
1246 xoffset * pixel_width / 8;
1249 if (screen->interlace == 1) {
1250 win->area[0].y_offset =
1251 yoffset * stride * 2 + xoffset * pixel_width / 8;
1253 win->area[0].y_offset =
1254 yoffset * stride + xoffset * pixel_width / 8;
1257 if (is_pic_yuv == 1) {
1258 if (screen->y_mirror == 1) {
1259 if (screen->interlace == 1) {
1260 win->area[0].c_offset =
1261 uv_y_off * uv_stride * 2 +
1262 ((uv_y_act - 1) * 2 + 1) * uv_stride +
1263 uv_x_off * pixel_width / 8;
1265 win->area[0].c_offset = uv_y_off * uv_stride +
1266 (uv_y_act - 1) * uv_stride +
1267 uv_x_off * pixel_width / 8;
1270 if (screen->interlace == 1) {
1271 win->area[0].c_offset =
1272 uv_y_off * uv_stride * 2 +
1273 uv_x_off * pixel_width / 8;
1275 win->area[0].c_offset =
1276 uv_y_off * uv_stride +
1277 uv_x_off * pixel_width / 8;
1282 win->area[0].smem_start = fix->smem_start;
1283 win->area[0].cbr_start = fix->mmio_start;
1284 win->area[0].state = 1;
1288 dev_drv->ops->pan_display(dev_drv, win_id);
1289 if (rk_fb->disp_mode == DUAL) {
1290 if (extend_win->state &&
1291 (hdmi_switch_complete & (1 << extend_win_id))) {
1292 extend_win->area[0].y_offset = win->area[0].y_offset;
1293 if (extend_dev_drv->rotate_mode > X_Y_MIRROR) {
1294 rk_fb_rotate(extend_info, info,
1295 win->area[0].y_offset);
1297 #ifdef CONFIG_ROCKCHIP_IOMMU
1298 if (extend_dev_drv->iommu_enabled) {
1299 ret = ion_map_iommu(extend_dev_drv->dev,
1301 win->area[0].ion_hdl,
1302 (unsigned long *)&phy_addr,
1303 (unsigned long *)&len);
1305 dev_err(extend_dev_drv->dev, "ion map to get phy addr failed\n");
1306 ion_free(rk_fb->ion_client, win->area[0].ion_hdl);
1309 extend_win->area[0].smem_start = phy_addr;
1310 extend_win->area[0].cbr_start =
1311 win->area[0].cbr_start;
1313 extend_win->area[0].smem_start =
1314 win->area[0].smem_start;
1315 extend_win->area[0].cbr_start =
1316 win->area[0].cbr_start;
1319 extend_win->area[0].smem_start =
1320 win->area[0].smem_start;
1321 extend_win->area[0].cbr_start =
1322 win->area[0].cbr_start;
1325 extend_dev_drv->ops->pan_display(extend_dev_drv,
1329 #ifdef CONFIG_FB_MIRRORING
1330 if (video_data_to_mirroring)
1331 video_data_to_mirroring(info, NULL);
1333 dev_drv->ops->cfg_done(dev_drv);
1337 static int rk_fb_get_list_stat(struct rk_lcdc_driver *dev_drv)
1341 i = list_empty(&dev_drv->update_regs_list);
1342 j = list_empty(&saved_list);
1343 return i == j ? 0 : 1;
1346 void rk_fd_fence_wait(struct rk_lcdc_driver *dev_drv, struct sync_fence *fence)
1348 int err = sync_fence_wait(fence, 1000);
1354 err = sync_fence_wait(fence, 10 * MSEC_PER_SEC);
1357 printk("error waiting on fence\n");
1360 static int rk_fb_copy_from_loader(struct fb_info *info)
1362 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
1363 void *dst = info->screen_base;
1367 struct rk_lcdc_win *win = dev_drv->win[0];
1368 u32 size = (win->area[0].xact) * (win->area[0].yact) << 2;
1369 dev_drv->ops->get_dsp_addr(dev_drv, dsp_addr);
1371 dev_info(info->dev, "copy fb data %d x %d from dst_addr:%08x\n",
1372 win->area[0].xact, win->area[0].yact, src);
1373 for (i = 0; i < size; i += PAGE_SIZE) {
1374 void *page = phys_to_page(i + src);
1375 void *from_virt = kmap(page);
1376 void *to_virt = dst + i;
1377 memcpy(to_virt, from_virt, PAGE_SIZE);
1382 #ifdef CONFIG_ROCKCHIP_IOMMU
1383 static int g_last_addr[4];
1388 #define DUMP_CHUNK 256
1389 char buf[PAGE_SIZE];
1391 int rk_fb_sysmmu_fault_handler(struct device *dev,
1392 enum rk_iommu_inttype itype,
1393 unsigned long pgtable_base,
1394 unsigned long fault_addr, unsigned int status)
1396 struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
1398 static int page_fault_cnt;
1399 if ((page_fault_cnt++) >= 10)
1402 ("PAGE FAULT occurred at 0x%lx (Page table base: 0x%lx),status=%d\n",
1403 fault_addr, pgtable_base, status);
1404 printk("last config addr:\n" "win0:0x%08x\n" "win1:0x%08x\n"
1405 "win2:0x%08x\n" "win3:0x%08x\n", g_last_addr[0], g_last_addr[1],
1406 g_last_addr[2], g_last_addr[3]);
1407 printk("last freed buffer:\n");
1408 for (i = 0; (freed_addr[i] != 0xfefefefe) && freed_addr[i]; i++)
1409 printk("%d:0x%08x\n", i, freed_addr[i]);
1410 printk("last timeout:%d\n", g_last_timeout);
1411 dev_drv->ops->get_disp_info(dev_drv, buf, 0);
1412 for (i = 0; i < PAGE_SIZE; i += DUMP_CHUNK) {
1413 if ((PAGE_SIZE - i) > DUMP_CHUNK) {
1414 char c = buf[i + DUMP_CHUNK];
1415 buf[i + DUMP_CHUNK] = 0;
1416 pr_cont("%s", buf + i);
1417 buf[i + DUMP_CHUNK] = c;
1419 buf[PAGE_SIZE - 1] = 0;
1420 pr_cont("%s", buf + i);
1428 void rk_fb_free_dma_buf(struct rk_lcdc_driver *dev_drv,
1429 struct rk_fb_reg_win_data *reg_win_data)
1432 struct rk_fb_reg_area_data *area_data;
1433 struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
1434 #if defined(CONFIG_ROCKCHIP_IOMMU)
1435 struct rk_lcdc_driver *ext_dev_drv = rk_get_extend_lcdc_drv();
1438 for (i = 0; i < reg_win_data->area_num; i++) {
1439 area_data = ®_win_data->reg_area_data[i];
1440 index_buf = area_data->index_buf;
1441 #if defined(CONFIG_ROCKCHIP_IOMMU)
1442 if (dev_drv->iommu_enabled) {
1443 ion_unmap_iommu(dev_drv->dev, rk_fb->ion_client,
1444 area_data->ion_handle);
1445 freed_addr[freed_index++] = area_data->smem_start;
1447 if (rk_fb->disp_mode == DUAL && hdmi_switch_complete == 0xff) {
1448 if (ext_dev_drv->iommu_enabled)
1449 ion_unmap_iommu(ext_dev_drv->dev,
1451 area_data->ion_handle);
1454 if (area_data->ion_handle != NULL) {
1455 ion_unmap_kernel(rk_fb->ion_client,
1456 area_data->ion_handle);
1457 ion_free(rk_fb->ion_client, area_data->ion_handle);
1459 if (area_data->acq_fence)
1460 sync_fence_put(area_data->acq_fence);
1462 memset(reg_win_data, 0, sizeof(struct rk_fb_reg_win_data));
1465 static void rk_fb_update_driver(struct rk_lcdc_driver *dev_drv,
1466 struct rk_lcdc_win *win,
1467 struct rk_fb_reg_win_data *reg_win_data)
1470 struct rk_fb *inf = platform_get_drvdata(fb_pdev);
1471 struct rk_screen *cur_screen;
1472 struct rk_screen primary_screen;
1474 if (unlikely(!inf) || unlikely(!dev_drv) ||
1475 unlikely(!win) || unlikely(!reg_win_data))
1478 cur_screen = dev_drv->cur_screen;
1479 rk_fb_get_prmry_screen(&primary_screen);
1481 win->area_num = reg_win_data->area_num;
1482 win->format = reg_win_data->data_format;
1483 win->id = reg_win_data->win_id;
1484 win->z_order = reg_win_data->z_order;
1486 if (reg_win_data->reg_area_data[0].smem_start > 0) {
1488 win->area_num = reg_win_data->area_num;
1489 win->format = reg_win_data->data_format;
1490 win->id = reg_win_data->win_id;
1491 win->z_order = reg_win_data->z_order;
1492 win->area[0].uv_vir_stride =
1493 reg_win_data->reg_area_data[0].uv_vir_stride;
1494 win->area[0].cbr_start =
1495 reg_win_data->reg_area_data[0].cbr_start;
1496 win->area[0].c_offset = reg_win_data->reg_area_data[0].c_offset;
1497 win->alpha_en = reg_win_data->alpha_en;
1498 win->alpha_mode = reg_win_data->alpha_mode;
1499 win->g_alpha_val = reg_win_data->g_alpha_val;
1500 for (i = 0; i < RK_WIN_MAX_AREA; i++) {
1501 if (reg_win_data->reg_area_data[i].smem_start > 0) {
1502 win->area[i].ion_hdl =
1503 reg_win_data->reg_area_data[i].ion_handle;
1504 win->area[i].smem_start =
1505 reg_win_data->reg_area_data[i].smem_start;
1506 if (inf->disp_mode == DUAL) {
1508 reg_win_data->reg_area_data[i].xpos;
1510 reg_win_data->reg_area_data[i].ypos;
1511 win->area[i].xsize =
1512 reg_win_data->reg_area_data[i].xsize;
1513 win->area[i].ysize =
1514 reg_win_data->reg_area_data[i].ysize;
1517 reg_win_data->reg_area_data[i].xpos *
1518 cur_screen->mode.xres /
1519 primary_screen.mode.xres;
1521 reg_win_data->reg_area_data[i].ypos *
1522 cur_screen->mode.yres /
1523 primary_screen.mode.yres;
1524 win->area[i].xsize =
1525 reg_win_data->reg_area_data[i].xsize *
1526 cur_screen->mode.xres /
1527 primary_screen.mode.xres;
1528 win->area[i].ysize =
1529 reg_win_data->reg_area_data[i].ysize *
1530 cur_screen->mode.yres /
1531 primary_screen.mode.yres;
1534 reg_win_data->reg_area_data[i].xact;
1536 reg_win_data->reg_area_data[i].yact;
1538 reg_win_data->reg_area_data[i].xvir;
1540 reg_win_data->reg_area_data[i].yvir;
1541 win->area[i].y_offset =
1542 reg_win_data->reg_area_data[i].y_offset;
1543 win->area[i].y_vir_stride =
1544 reg_win_data->reg_area_data[i].y_vir_stride;
1545 win->area[i].state = 1;
1547 win->area[i].state = 0;
1558 static int rk_fb_update_hdmi_win(struct rk_lcdc_win *ext_win,
1559 struct rk_lcdc_win *win)
1561 struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
1562 struct rk_lcdc_driver *dev_drv = rk_get_prmry_lcdc_drv();
1563 struct rk_lcdc_driver *ext_dev_drv = rk_get_extend_lcdc_drv();
1564 struct rk_screen *screen;
1565 struct rk_screen *ext_screen;
1568 int pixel_width, vir_width_bit, y_stride;
1569 struct rk_lcdc_win *last_win;
1570 bool is_yuv = false;
1571 static u8 fb_index = 0;
1572 #ifdef CONFIG_ROCKCHIP_IOMMU
1573 ion_phys_addr_t phy_addr;
1577 if ((rk_fb->disp_mode != DUAL) ||
1578 (hdmi_get_hotplug() != HDMI_HPD_ACTIVED) ||
1579 (hdmi_switch_complete != 0xff)) {
1580 printk(KERN_INFO "%s: hdmi is disconnect!\n", __func__);
1584 if (unlikely(!dev_drv) || unlikely(!ext_dev_drv))
1587 screen = dev_drv->cur_screen;
1588 ext_screen = ext_dev_drv->cur_screen;
1589 hdmi_xsize = ext_screen->xsize;
1590 hdmi_ysize = ext_screen->ysize;
1592 ext_win->state = win->state;
1593 ext_win->id = win->id;
1594 if (win->area[0].smem_start == 0)
1596 if (ext_win->state == 0)
1599 ext_win->area_num = win->area_num;
1600 ext_win->format = win->format;
1601 ext_win->z_order = win->z_order;
1602 ext_win->alpha_en = win->alpha_en;
1603 ext_win->alpha_mode = win->alpha_mode;
1604 ext_win->g_alpha_val = win->g_alpha_val;
1606 /* win0 and win1 have only one area and support scale
1607 * but win2 and win3 don't support scale
1608 * so hdmi only use win0 or win1
1610 ext_win->area[0].state = win->area[0].state;
1612 switch (ext_win->format) {
1626 if (ext_dev_drv->rotate_mode == ROTATE_90 ||
1627 ext_dev_drv->rotate_mode == ROTATE_270) {
1628 if (ext_win->id == 0) {
1629 ext_win->area[0].smem_start =
1630 rk_fb->fb[rk_fb->num_fb >> 1]->fix.smem_start;
1631 ext_win->area[0].y_offset = (get_rotate_fb_size() >> 1) * fb_index;
1632 if ((++fb_index) > 1)
1635 ext_win->area[0].y_offset = 0;
1636 last_win = ext_dev_drv->win[ext_win->id - 1];
1637 if (last_win->area[0].cbr_start)
1638 ext_win->area[0].smem_start =
1639 last_win->area[0].cbr_start +
1640 last_win->area[0].c_offset +
1641 last_win->area[0].xvir * last_win->area[0].yvir;
1643 ext_win->area[0].smem_start =
1644 last_win->area[0].smem_start +
1645 last_win->area[0].y_offset +
1646 last_win->area[0].xvir * last_win->area[0].yvir;
1649 ext_win->area[0].xact = win->area[0].yact;
1650 ext_win->area[0].yact = win->area[0].xact;
1651 ext_win->area[0].xvir = win->area[0].yact;
1652 ext_win->area[0].yvir = win->area[0].xact;
1653 pixel_width = rk_fb_pixel_width(ext_win->format);
1654 vir_width_bit = pixel_width * ext_win->area[0].xvir;
1655 y_stride = ALIGN_N_TIMES(vir_width_bit, 32) / 8;
1656 ext_win->area[0].y_vir_stride = y_stride >> 2;
1658 #ifdef CONFIG_ROCKCHIP_IOMMU
1659 if (ext_dev_drv->iommu_enabled) {
1660 ret = ion_map_iommu(ext_dev_drv->dev,
1662 win->area[0].ion_hdl,
1663 (unsigned long *)&phy_addr,
1664 (unsigned long *)&len);
1666 dev_err(ext_dev_drv->dev, "ion map to get phy addr failed\n");
1667 ion_free(rk_fb->ion_client, win->area[0].ion_hdl);
1670 ext_win->area[0].smem_start = phy_addr;
1672 ext_win->area[0].smem_start = win->area[0].smem_start;
1675 ext_win->area[0].smem_start = win->area[0].smem_start;
1677 ext_win->area[0].y_offset = win->area[0].y_offset;
1678 ext_win->area[0].xact = win->area[0].xact;
1679 ext_win->area[0].yact = win->area[0].yact;
1680 ext_win->area[0].xvir = win->area[0].xvir;
1681 ext_win->area[0].yvir = win->area[0].yvir;
1682 ext_win->area[0].y_vir_stride = win->area[0].y_vir_stride;
1685 if (win->area[0].xpos != 0 || win->area[0].ypos != 0) {
1686 if (ext_dev_drv->rotate_mode == ROTATE_270) {
1687 int xbom_pos = 0, ybom_pos = 0;
1688 int xtop_pos = 0, ytop_pos = 0;
1690 ext_win->area[0].xsize = hdmi_xsize *
1691 win->area[0].ysize /
1693 ext_win->area[0].ysize = hdmi_ysize *
1694 win->area[0].xsize /
1696 xbom_pos = hdmi_xsize * win->area[0].ypos / screen->mode.yres;
1697 ybom_pos = hdmi_ysize * win->area[0].xpos / screen->mode.xres;
1698 xtop_pos = hdmi_xsize - ext_win->area[0].xsize - xbom_pos;
1699 ytop_pos = hdmi_ysize - ext_win->area[0].ysize - ybom_pos;
1700 ext_win->area[0].xpos =
1701 ((ext_screen->mode.xres - hdmi_xsize) >> 1) + xtop_pos;
1702 ext_win->area[0].ypos =
1703 ((ext_screen->mode.yres - hdmi_ysize) >> 1) + ytop_pos;
1704 } else if (ext_dev_drv->rotate_mode == ROTATE_90) {
1705 ext_win->area[0].xsize = hdmi_xsize *
1706 win->area[0].ysize /
1708 ext_win->area[0].ysize = hdmi_ysize *
1709 win->area[0].xsize /
1711 ext_win->area[0].xpos =
1712 ((ext_screen->mode.xres - hdmi_xsize) >> 1) +
1713 hdmi_xsize * win->area[0].ypos / screen->mode.yres;
1714 ext_win->area[0].ypos =
1715 ((ext_screen->mode.yres - hdmi_ysize) >> 1) +
1716 hdmi_ysize * win->area[0].xpos / screen->mode.xres;
1718 ext_win->area[0].xsize = hdmi_xsize *
1719 win->area[0].xsize /
1721 ext_win->area[0].ysize = hdmi_ysize *
1722 win->area[0].ysize /
1724 ext_win->area[0].xpos =
1725 ((ext_screen->mode.xres - hdmi_xsize) >> 1) +
1726 hdmi_xsize * win->area[0].xpos / screen->mode.xres;
1727 ext_win->area[0].ypos =
1728 ((ext_screen->mode.yres - hdmi_ysize) >> 1) +
1729 hdmi_ysize * win->area[0].ypos / screen->mode.yres;
1732 ext_win->area[0].xsize = hdmi_xsize;
1733 ext_win->area[0].ysize = hdmi_ysize;
1734 ext_win->area[0].xpos =
1735 (ext_screen->mode.xres - hdmi_xsize) >> 1;
1736 ext_win->area[0].ypos =
1737 (ext_screen->mode.yres - hdmi_ysize) >> 1;
1741 ext_win->area[0].uv_vir_stride = 0;
1742 ext_win->area[0].cbr_start = 0;
1743 ext_win->area[0].c_offset = 0;
1747 if (ext_dev_drv->rotate_mode == ROTATE_90 ||
1748 ext_dev_drv->rotate_mode == ROTATE_270) {
1749 ext_win->area[0].uv_vir_stride = ext_win->area[0].y_vir_stride;
1750 ext_win->area[0].cbr_start = ext_win->area[0].smem_start +
1751 ext_win->area[0].y_offset +
1752 ext_win->area[0].xvir * ext_win->area[0].yvir;
1753 ext_win->area[0].c_offset = win->area[0].c_offset;
1755 ext_win->area[0].uv_vir_stride = win->area[0].uv_vir_stride;
1756 #ifdef CONFIG_ROCKCHIP_IOMMU
1757 if (ext_dev_drv->iommu_enabled)
1758 ext_win->area[0].cbr_start =
1759 ext_win->area[0].smem_start +
1760 ext_win->area[0].y_offset +
1761 ext_win->area[0].xvir * ext_win->area[0].yvir;
1763 ext_win->area[0].cbr_start = win->area[0].cbr_start;
1765 ext_win->area[0].cbr_start = win->area[0].cbr_start;
1767 ext_win->area[0].c_offset = win->area[0].c_offset;
1773 static struct rk_fb_reg_win_data *rk_fb_get_win_data(struct rk_fb_reg_data
1777 struct rk_fb_reg_win_data *win_data = NULL;
1778 for (i = 0; i < regs->win_num; i++) {
1779 if (regs->reg_win_data[i].win_id == win_id) {
1780 win_data = &(regs->reg_win_data[i]);
1788 static void rk_fb_update_reg(struct rk_lcdc_driver *dev_drv,
1789 struct rk_fb_reg_data *regs)
1792 struct rk_lcdc_win *win;
1793 ktime_t timestamp = dev_drv->vsync_info.timestamp;
1794 struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
1795 struct rk_lcdc_driver *ext_dev_drv;
1796 struct rk_lcdc_win *ext_win;
1797 struct rk_fb_reg_win_data *win_data;
1798 bool wait_for_vsync;
1800 unsigned int dsp_addr[4];
1803 /* acq_fence wait */
1804 for (i = 0; i < regs->win_num; i++) {
1805 win_data = ®s->reg_win_data[i];
1806 for (j = 0; j < RK_WIN_MAX_AREA; j++) {
1807 if (win_data->reg_area_data[j].acq_fence) {
1808 /* printk("acq_fence wait!!!!!\n"); */
1809 rk_fd_fence_wait(dev_drv, win_data->reg_area_data[j].acq_fence);
1814 for (i = 0; i < dev_drv->lcdc_win_num; i++) {
1815 win = dev_drv->win[i];
1816 win_data = rk_fb_get_win_data(regs, i);
1818 rk_fb_update_driver(dev_drv, win, win_data);
1820 dev_drv->ops->set_par(dev_drv, i);
1821 dev_drv->ops->pan_display(dev_drv, i);
1822 #if defined(CONFIG_ROCKCHIP_IOMMU)
1823 if (dev_drv->iommu_enabled) {
1824 g_last_addr[i] = win_data->reg_area_data[0].smem_start +
1825 win_data->reg_area_data[0].y_offset;
1833 dev_drv->ops->ovl_mgr(dev_drv, 0, 1);
1835 if ((rk_fb->disp_mode == DUAL)
1836 && (hdmi_get_hotplug() == HDMI_HPD_ACTIVED)
1837 && (hdmi_switch_complete == 0xff)) {
1838 ext_dev_drv = rk_get_extend_lcdc_drv();
1840 printk(KERN_ERR "hdmi lcdc driver not found!\n");
1844 /* win0 and win1 have only one area and support scale
1845 * but win2 and win3 don't support scale
1846 * so hdmi only use win0 or win1
1848 for (i = 0; i < 2; i++) {
1849 win = dev_drv->win[i];
1850 ext_win = ext_dev_drv->win[i];
1851 ext_win->state = win->state;
1852 if (!ext_win->state)
1855 rk_fb_update_hdmi_win(ext_win, win);
1857 if (ext_dev_drv->rotate_mode > X_Y_MIRROR)
1858 rk_fb_win_rotate(ext_win, win,
1859 ext_dev_drv->rotate_mode,
1860 ext_dev_drv->iommu_enabled);
1862 ext_dev_drv->ops->set_par(ext_dev_drv, i);
1863 ext_dev_drv->ops->pan_display(ext_dev_drv, i);
1866 ext_dev_drv->ops->cfg_done(ext_dev_drv);
1869 dev_drv->ops->cfg_done(dev_drv);
1872 timestamp = dev_drv->vsync_info.timestamp;
1873 timeout = wait_event_interruptible_timeout(dev_drv->vsync_info.wait,
1874 ktime_compare(dev_drv->vsync_info.timestamp, timestamp) > 0,
1875 msecs_to_jiffies(25));
1876 dev_drv->ops->get_dsp_addr(dev_drv, dsp_addr);
1877 wait_for_vsync = false;
1878 for (i = 0; i < dev_drv->lcdc_win_num; i++) {
1879 if (dev_drv->win[i]->state == 1) {
1881 dev_drv->win[i]->area[0].smem_start +
1882 dev_drv->win[i]->area[0].y_offset;
1883 u32 reg_start = dsp_addr[i];
1885 if (unlikely(new_start != reg_start)) {
1886 wait_for_vsync = true;
1887 dev_info(dev_drv->dev,
1888 "win%d:new_addr:0x%08x cur_addr:0x%08x--%d\n",
1889 i, new_start, reg_start, 101 - count);
1894 } while (wait_for_vsync && count--);
1896 sw_sync_timeline_inc(dev_drv->timeline, 1);
1899 #if defined(CONFIG_ROCKCHIP_IOMMU)
1900 if (dev_drv->iommu_enabled) {
1902 g_last_timeout = timeout;
1905 for (i = 0; i < g_last_win_num; i++)
1906 rk_fb_free_dma_buf(dev_drv, &g_reg_win_data[i]);
1908 #if defined(CONFIG_ROCKCHIP_IOMMU)
1909 if (dev_drv->iommu_enabled)
1910 freed_addr[freed_index] = 0xfefefefe;
1913 for (i = 0; i < regs->win_num; i++) {
1914 memcpy(&g_reg_win_data[i], &(regs->reg_win_data[i]),
1915 sizeof(struct rk_fb_reg_win_data));
1917 g_last_win_num = regs->win_num;
1921 static void rk_fb_update_regs_handler(struct kthread_work *work)
1923 struct rk_lcdc_driver *dev_drv =
1924 container_of(work, struct rk_lcdc_driver, update_regs_work);
1925 struct rk_fb_reg_data *data, *next;
1926 /* struct list_head saved_list; */
1928 mutex_lock(&dev_drv->update_regs_list_lock);
1929 saved_list = dev_drv->update_regs_list;
1930 list_replace_init(&dev_drv->update_regs_list, &saved_list);
1931 mutex_unlock(&dev_drv->update_regs_list_lock);
1933 list_for_each_entry_safe(data, next, &saved_list, list) {
1934 rk_fb_update_reg(dev_drv, data);
1935 list_del(&data->list);
1939 if (dev_drv->wait_fs && list_empty(&dev_drv->update_regs_list))
1940 wake_up(&dev_drv->update_regs_wait);
1943 static int rk_fb_check_config_var(struct rk_fb_area_par *area_par,
1944 struct rk_screen *screen)
1946 if ((area_par->x_offset + area_par->xact > area_par->xvir) ||
1947 (area_par->xact <= 0) || (area_par->yact <= 0) ||
1948 (area_par->xvir <= 0) || (area_par->yvir <= 0)) {
1949 pr_err("check config var fail 0:\n"
1950 "x_offset=%d,xact=%d,xvir=%d\n",
1951 area_par->x_offset, area_par->xact, area_par->xvir);
1955 if ((area_par->xpos + area_par->xsize > screen->mode.xres) ||
1956 (area_par->ypos + area_par->ysize > screen->mode.yres) ||
1957 (area_par->xsize <= 0) || (area_par->ysize <= 0)) {
1958 pr_err("check config var fail 1:\n"
1959 "xpos=%d,xsize=%d,xres=%d\n"
1960 "ypos=%d,ysize=%d,yres=%d\n",
1961 area_par->xpos, area_par->xsize, screen->mode.xres,
1962 area_par->ypos, area_par->ysize, screen->mode.yres);
1968 static int rk_fb_set_win_buffer(struct fb_info *info,
1969 struct rk_fb_win_par *win_par,
1970 struct rk_fb_reg_win_data *reg_win_data)
1972 struct rk_fb *rk_fb = dev_get_drvdata(info->device);
1973 struct fb_fix_screeninfo *fix = &info->fix;
1974 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
1975 struct rk_screen *screen = dev_drv->cur_screen;
1976 struct rk_screen primary_screen;
1977 struct fb_info *fbi = rk_fb->fb[0];
1978 int i, ion_fd, acq_fence_fd;
1980 u32 xoffset, yoffset;
1982 struct ion_handle *hdl;
1988 u32 stride, uv_stride;
1991 u16 uv_x_off, uv_y_off, uv_y_act;
1993 u8 ppixel_a = 0, global_a = 0;
1994 ion_phys_addr_t phy_addr;
1997 reg_win_data->reg_area_data[0].smem_start = -1;
1998 reg_win_data->area_num = 0;
1999 fbi = rk_fb->fb[reg_win_data->win_id];
2000 if (win_par->area_par[0].phy_addr == 0) {
2001 for (i = 0; i < RK_WIN_MAX_AREA; i++) {
2002 ion_fd = win_par->area_par[i].ion_fd;
2005 ion_import_dma_buf(rk_fb->ion_client,
2009 ("%s: Could not import handle: %d\n",
2010 __func__, (int)hdl);
2011 /*return -EINVAL; */
2015 ion_map_kernel(rk_fb->ion_client, hdl);
2016 reg_win_data->area_num++;
2017 reg_win_data->reg_area_data[i].ion_handle = hdl;
2018 #ifndef CONFIG_ROCKCHIP_IOMMU
2019 ret = ion_phys(rk_fb->ion_client, hdl, &phy_addr,
2022 if (dev_drv->iommu_enabled)
2023 ret = ion_map_iommu(dev_drv->dev,
2026 (unsigned long *)&phy_addr,
2027 (unsigned long *)&len);
2029 ret = ion_phys(rk_fb->ion_client, hdl,
2033 dev_err(fbi->dev, "ion map to get phy addr failed\n");
2034 ion_free(rk_fb->ion_client, hdl);
2037 reg_win_data->reg_area_data[i].smem_start = phy_addr;
2038 reg_win_data->area_buf_num++;
2039 reg_win_data->reg_area_data[i].index_buf = 1;
2043 reg_win_data->reg_area_data[0].smem_start =
2044 win_par->area_par[0].phy_addr;
2045 reg_win_data->area_num = 1;
2046 fbi->screen_base = phys_to_virt(win_par->area_par[0].phy_addr);
2049 if (reg_win_data->area_num == 0)
2052 for (i = 0; i < reg_win_data->area_num; i++) {
2053 acq_fence_fd = win_par->area_par[i].acq_fence_fd;
2054 index_buf = reg_win_data->reg_area_data[i].index_buf;
2055 if ((acq_fence_fd > 0) && (index_buf == 1)) {
2056 reg_win_data->reg_area_data[i].acq_fence =
2057 sync_fence_fdget(win_par->area_par[i].acq_fence_fd);
2060 fb_data_fmt = rk_fb_data_fmt(win_par->data_format, 0);
2061 reg_win_data->data_format = fb_data_fmt;
2062 pixel_width = rk_fb_pixel_width(fb_data_fmt);
2064 ppixel_a = ((fb_data_fmt == ARGB888) ||
2065 (fb_data_fmt == ABGR888)) ? 1 : 0;
2066 global_a = (win_par->g_alpha_val == 0) ? 0 : 1;
2067 reg_win_data->alpha_en = ppixel_a | global_a;
2068 reg_win_data->g_alpha_val = win_par->g_alpha_val;
2069 reg_win_data->alpha_mode = win_par->alpha_mode;
2070 if (reg_win_data->reg_area_data[0].smem_start > 0) {
2071 reg_win_data->z_order = win_par->z_order;
2072 reg_win_data->win_id = win_par->win_id;
2074 reg_win_data->z_order = -1;
2075 reg_win_data->win_id = -1;
2078 rk_fb_get_prmry_screen(&primary_screen);
2079 for (i = 0; i < reg_win_data->area_num; i++) {
2080 rk_fb_check_config_var(&win_par->area_par[i], &primary_screen);
2081 /* visiable pos in panel */
2082 reg_win_data->reg_area_data[i].xpos = win_par->area_par[i].xpos;
2083 reg_win_data->reg_area_data[i].ypos = win_par->area_par[i].ypos;
2085 /* realy size in panel */
2086 reg_win_data->reg_area_data[i].xsize = win_par->area_par[i].xsize;
2087 reg_win_data->reg_area_data[i].ysize = win_par->area_par[i].ysize;
2089 /* realy size in panel */
2090 reg_win_data->reg_area_data[i].xact = win_par->area_par[i].xact;
2091 reg_win_data->reg_area_data[i].yact = win_par->area_par[i].yact;
2093 xoffset = win_par->area_par[i].x_offset; /* buf offset */
2094 yoffset = win_par->area_par[i].y_offset;
2095 xvir = win_par->area_par[i].xvir;
2096 reg_win_data->reg_area_data[i].xvir = xvir;
2097 yvir = win_par->area_par[i].yvir;
2098 reg_win_data->reg_area_data[i].yvir = yvir;
2100 vir_width_bit = pixel_width * xvir;
2101 /* pixel_width = byte_num*8 */
2102 stride_32bit_1 = ((vir_width_bit + 31) & (~31)) / 8;
2103 stride_32bit_2 = ((vir_width_bit * 2 + 31) & (~31)) / 8;
2105 stride = stride_32bit_1; /* default rgb */
2106 fix->line_length = stride;
2107 reg_win_data->reg_area_data[i].y_vir_stride = stride >> 2;
2109 /* x y mirror ,jump line
2110 * reg_win_data->reg_area_data[i].y_offset =
2111 * yoffset*stride+xoffset*pixel_width/8;
2113 if (screen->y_mirror == 1) {
2114 if (screen->interlace == 1) {
2115 reg_win_data->reg_area_data[i].y_offset =
2116 yoffset * stride * 2 +
2117 ((reg_win_data->reg_area_data[i].yact - 1) * 2 + 1) * stride +
2118 xoffset * pixel_width / 8;
2120 reg_win_data->reg_area_data[i].y_offset =
2122 (reg_win_data->reg_area_data[i].yact - 1) * stride +
2123 xoffset * pixel_width / 8;
2126 if (screen->interlace == 1) {
2127 reg_win_data->reg_area_data[i].y_offset =
2128 yoffset * stride * 2 +
2129 xoffset * pixel_width / 8;
2131 reg_win_data->reg_area_data[i].y_offset =
2133 xoffset * pixel_width / 8;
2137 switch (fb_data_fmt) {
2141 stride = stride_32bit_1;
2142 uv_stride = stride_32bit_1 >> 1;
2143 uv_x_off = xoffset >> 1;
2145 fix->line_length = stride;
2146 uv_y_act = win_par->area_par[0].yact >> 1;
2148 case YUV420: /* 420sp */
2151 stride = stride_32bit_1;
2152 uv_stride = stride_32bit_1;
2154 uv_y_off = yoffset >> 1;
2155 fix->line_length = stride;
2156 uv_y_act = win_par->area_par[0].yact >> 1;
2161 stride = stride_32bit_1;
2162 uv_stride = stride_32bit_2;
2163 uv_x_off = xoffset * 2;
2165 fix->line_length = stride << 2;
2166 uv_y_act = win_par->area_par[0].yact;
2171 if (is_pic_yuv == 1) {
2172 reg_win_data->reg_area_data[0].cbr_start =
2173 reg_win_data->reg_area_data[0].smem_start + xvir * yvir;
2174 reg_win_data->reg_area_data[0].uv_vir_stride = uv_stride >> 2;
2175 if (screen->y_mirror == 1) {
2176 if (screen->interlace == 1) {
2177 reg_win_data->reg_area_data[0].c_offset =
2178 uv_y_off * uv_stride * 2 +
2179 ((uv_y_act - 1) * 2 + 1) * uv_stride +
2180 uv_x_off * pixel_width / 8;
2182 reg_win_data->reg_area_data[0].c_offset =
2183 uv_y_off * uv_stride +
2184 (uv_y_act - 1) * uv_stride +
2185 uv_x_off * pixel_width / 8;
2188 if (screen->interlace == 1) {
2189 reg_win_data->reg_area_data[0].c_offset =
2190 uv_y_off * uv_stride * 2 +
2191 uv_x_off * pixel_width / 8;
2193 reg_win_data->reg_area_data[0].c_offset =
2194 uv_y_off * uv_stride +
2195 uv_x_off * pixel_width / 8;
2202 static int rk_fb_set_win_config(struct fb_info *info,
2203 struct rk_fb_win_cfg_data *win_data)
2205 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2206 struct rk_fb_reg_data *regs;
2208 struct sync_fence *release_fence[RK_MAX_BUF_NUM];
2209 struct sync_fence *retire_fence;
2210 struct sync_pt *release_sync_pt[RK_MAX_BUF_NUM];
2211 struct sync_pt *retire_sync_pt;
2212 char fence_name[20];
2214 int ret = 0, i, j = 0;
2215 int list_is_empty = 0;
2217 regs = kzalloc(sizeof(struct rk_fb_reg_data), GFP_KERNEL);
2219 printk(KERN_INFO "could not allocate rk_fb_reg_data\n");
2225 regs->post_cfg.xpos = win_data->post_cfg.xpos;
2226 regs->post_cfg.ypos = win_data->post_cfg.ypos;
2227 regs->post_cfg.xsize = win_data->post_cfg.xsize;
2228 regs->post_cfg.ysize = win_data->post_cfg.xsize;
2231 for (i = 0; i < dev_drv->lcdc_win_num; i++) {
2232 if (win_data->win_par[i].win_id < dev_drv->lcdc_win_num) {
2233 if (rk_fb_set_win_buffer(info, &win_data->win_par[i],
2234 ®s->reg_win_data[j]))
2236 if (regs->reg_win_data[j].area_num > 0) {
2239 regs->reg_win_data[j].area_buf_num;
2243 printk(KERN_INFO "error:win_id bigger than lcdc_win_num\n");
2244 printk(KERN_INFO "i=%d,win_id=%d\n", i,
2245 win_data->win_par[i].win_id);
2249 mutex_lock(&dev_drv->output_lock);
2250 if (!(dev_drv->suspend_flag == 0)) {
2251 rk_fb_update_reg(dev_drv, regs);
2253 printk(KERN_INFO "suspend_flag = 1\n");
2257 dev_drv->timeline_max++;
2259 for (i = 0; i < RK_MAX_BUF_NUM; i++) {
2260 if (i < regs->buf_num) {
2261 sprintf(fence_name, "fence%d", i);
2262 win_data->rel_fence_fd[i] = get_unused_fd();
2263 if (win_data->rel_fence_fd[i] < 0) {
2264 printk(KERN_INFO "rel_fence_fd=%d\n",
2265 win_data->rel_fence_fd[i]);
2269 release_sync_pt[i] =
2270 sw_sync_pt_create(dev_drv->timeline,
2271 dev_drv->timeline_max);
2273 sync_fence_create(fence_name, release_sync_pt[i]);
2274 sync_fence_install(release_fence[i],
2275 win_data->rel_fence_fd[i]);
2277 win_data->rel_fence_fd[i] = -1;
2281 win_data->ret_fence_fd = get_unused_fd();
2282 if (win_data->ret_fence_fd < 0) {
2283 printk("ret_fence_fd=%d\n", win_data->ret_fence_fd);
2288 sw_sync_pt_create(dev_drv->timeline, dev_drv->timeline_max);
2289 retire_fence = sync_fence_create("ret_fence", retire_sync_pt);
2290 sync_fence_install(retire_fence, win_data->ret_fence_fd);
2292 for (i = 0; i < RK_MAX_BUF_NUM; i++)
2293 win_data->rel_fence_fd[i] = -1;
2295 win_data->ret_fence_fd = -1;
2297 if (dev_drv->wait_fs == 0) {
2298 mutex_lock(&dev_drv->update_regs_list_lock);
2299 list_add_tail(®s->list, &dev_drv->update_regs_list);
2300 mutex_unlock(&dev_drv->update_regs_list_lock);
2301 queue_kthread_work(&dev_drv->update_regs_worker,
2302 &dev_drv->update_regs_work);
2304 mutex_lock(&dev_drv->update_regs_list_lock);
2305 list_is_empty = list_empty(&dev_drv->update_regs_list) &&
2306 list_empty(&saved_list);
2307 mutex_unlock(&dev_drv->update_regs_list_lock);
2308 if (!list_is_empty) {
2309 ret = wait_event_timeout(dev_drv->update_regs_wait,
2310 list_empty(&dev_drv->update_regs_list) && list_empty(&saved_list),
2311 msecs_to_jiffies(60));
2313 rk_fb_update_reg(dev_drv, regs);
2315 printk("%s: wait update_regs_wait timeout\n", __func__);
2316 } else if (ret == 0) {
2317 rk_fb_update_reg(dev_drv, regs);
2323 mutex_unlock(&dev_drv->output_lock);
2328 static int cfgdone_distlist[10] = { 0 };
2330 static int cfgdone_index;
2331 static int cfgdone_lasttime;
2333 int rk_get_real_fps(int before)
2335 struct timespec now;
2341 int index = cfgdone_index;
2350 getnstimeofday(&now);
2351 dist_curr = (now.tv_sec * 1000000 + now.tv_nsec / 1000) -
2357 for (i = 0; i < 10; i++) {
2360 total += cfgdone_distlist[index];
2362 dist_first = cfgdone_distlist[index];
2363 if (total < (before * 1000)) {
2365 printk("[%d:%d] ", dist_count, cfgdone_distlist[index]);
2367 dist_total += cfgdone_distlist[index];
2375 printk("total %d, count %d, curr %d, ", dist_total, dist_count, dist_curr);
2377 dist_curr = (dist_curr > dist_first) ? dist_curr : dist_first;
2378 dist_total += dist_curr;
2382 fps = (1000000 * dist_count) / dist_total;
2387 printk("curr2 %d, fps=%d\n", dist_curr, fps);
2391 EXPORT_SYMBOL(rk_get_real_fps);
2394 #ifdef CONFIG_ROCKCHIP_IOMMU
2396 static struct ion_handle *ion_hanle[ION_MAX];
2398 static int rk_fb_ioctl(struct fb_info *info, unsigned int cmd,
2401 struct rk_fb *rk_fb = dev_get_drvdata(info->device);
2402 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2403 struct fb_fix_screeninfo *fix = &info->fix;
2404 int fb_id = 0, extend_win_id = 0;
2405 struct fb_info *extend_info = NULL;
2406 struct rk_lcdc_driver *extend_dev_drv = NULL;
2407 struct rk_lcdc_win *extend_win = NULL;
2408 struct rk_lcdc_win *win;
2409 int enable; /* enable fb:1 enable;0 disable */
2410 int ovl; /* overlay:0 win1 on the top of win0;1,win0 on the top of win1 */
2411 int num_buf; /* buffer_number */
2413 struct rk_fb_win_cfg_data win_data;
2414 unsigned int dsp_addr[4];
2417 int win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
2419 void __user *argp = (void __user *)arg;
2420 win = dev_drv->win[win_id];
2421 if (rk_fb->disp_mode == DUAL) {
2422 fb_id = get_extend_fb_id(info);
2423 extend_info = rk_fb->fb[(rk_fb->num_fb >> 1) + fb_id];
2424 extend_dev_drv = (struct rk_lcdc_driver *)extend_info->par;
2425 extend_win_id = dev_drv->ops->fb_get_win_id(extend_dev_drv,
2426 extend_info->fix.id);
2427 extend_win = extend_dev_drv->win[extend_win_id];
2431 case RK_FBIOSET_YUV_ADDR:
2435 if (copy_from_user(yuv_phy, argp, 8))
2437 #ifdef CONFIG_ROCKCHIP_IOMMU
2438 if (!dev_drv->iommu_enabled || !strcmp(info->fix.id, "fb0")) {
2440 fix->smem_start = yuv_phy[0];
2441 fix->mmio_start = yuv_phy[1];
2442 #ifdef CONFIG_ROCKCHIP_IOMMU
2444 int usr_fd, offset, tmp;
2445 struct ion_handle *hdl;
2446 ion_phys_addr_t phy_addr;
2449 usr_fd = yuv_phy[0];
2450 offset = yuv_phy[1] - yuv_phy[0];
2453 fix->smem_start = 0;
2454 fix->mmio_start = 0;
2458 if (ion_hanle[ION_MAX - 1] != 0) {
2459 /*ion_unmap_kernel(rk_fb->ion_client, ion_hanle[ION_MAX - 1]);*/
2460 /*ion_unmap_iommu(dev_drv->dev, rk_fb->ion_client, ion_hanle[ION_MAX - 1]);*/
2461 ion_free(rk_fb->ion_client, ion_hanle[ION_MAX - 1]);
2462 ion_hanle[ION_MAX - 1] = 0;
2465 hdl = ion_import_dma_buf(rk_fb->ion_client, usr_fd);
2467 dev_err(info->dev, "failed to get ion handle:%ld\n",
2472 ret = ion_map_iommu(dev_drv->dev, rk_fb->ion_client, hdl,
2473 (unsigned long *)&phy_addr,
2474 (unsigned long *)&len);
2476 dev_err(info->dev, "ion map to get phy addr failed");
2477 ion_free(rk_fb->ion_client, hdl);
2480 fix->smem_start = phy_addr;
2481 fix->mmio_start = phy_addr + offset;
2482 fix->smem_len = len;
2483 /*info->screen_base = ion_map_kernel(rk_fb->ion_client, hdl);*/
2486 for (tmp = ION_MAX - 1; tmp > 0; tmp--)
2487 ion_hanle[tmp] = ion_hanle[tmp - 1];
2493 case RK_FBIOSET_ENABLE:
2494 if (copy_from_user(&enable, argp, sizeof(enable)))
2496 dev_drv->ops->open(dev_drv, win_id, enable);
2498 case RK_FBIOGET_ENABLE:
2499 enable = dev_drv->ops->get_win_state(dev_drv, win_id);
2500 if (copy_to_user(argp, &enable, sizeof(enable)))
2503 case RK_FBIOSET_OVERLAY_STA:
2504 if (copy_from_user(&ovl, argp, sizeof(ovl)))
2506 dev_drv->ops->ovl_mgr(dev_drv, ovl, 1);
2508 case RK_FBIOGET_OVERLAY_STA:
2509 ovl = dev_drv->ops->ovl_mgr(dev_drv, 0, 0);
2510 if (copy_to_user(argp, &ovl, sizeof(ovl)))
2513 case RK_FBIOPUT_NUM_BUFFERS:
2514 if (copy_from_user(&num_buf, argp, sizeof(num_buf)))
2516 dev_drv->num_buf = num_buf;
2518 case RK_FBIOSET_VSYNC_ENABLE:
2519 if (copy_from_user(&enable, argp, sizeof(enable)))
2521 dev_drv->vsync_info.active = enable;
2524 case RK_FBIOGET_DSP_ADDR:
2525 dev_drv->ops->get_dsp_addr(dev_drv, dsp_addr);
2526 if (copy_to_user(argp, &dsp_addr, sizeof(dsp_addr)))
2529 case RK_FBIOGET_LIST_STA:
2530 list_stat = rk_fb_get_list_stat(dev_drv);
2531 if (copy_to_user(argp, &list_stat, sizeof(list_stat)))
2535 case RK_FBIOGET_IOMMU_STA:
2536 if (copy_to_user(argp, &dev_drv->iommu_enabled,
2537 sizeof(dev_drv->iommu_enabled)))
2540 #if defined(CONFIG_ION_ROCKCHIP)
2541 case RK_FBIOSET_DMABUF_FD:
2544 struct ion_handle *hdl;
2545 ion_phys_addr_t phy_addr;
2547 if (copy_from_user(&usr_fd, argp, sizeof(usr_fd)))
2549 hdl = ion_import_dma_buf(rk_fb->ion_client, usr_fd);
2550 ion_phys(rk_fb->ion_client, hdl, &phy_addr, &len);
2551 fix->smem_start = phy_addr;
2554 case RK_FBIOGET_DMABUF_FD:
2557 ion_share_dma_buf_fd(rk_fb->ion_client,
2558 win->area[0].ion_hdl);
2561 "ion_share_dma_buf_fd failed\n");
2564 if (copy_to_user(argp, &fd, sizeof(fd)))
2569 case RK_FBIOSET_CLEAR_FB:
2570 memset(info->screen_base, 0, get_fb_size());
2572 case RK_FBIOSET_CONFIG_DONE:
2575 struct timespec now;
2577 getnstimeofday(&now);
2578 curr = now.tv_sec * 1000000 + now.tv_nsec / 1000;
2579 cfgdone_distlist[cfgdone_index++] =
2580 curr - cfgdone_lasttime;
2582 printk("%d ", curr - cfgdone_lasttime);
2584 cfgdone_lasttime = curr;
2585 if (cfgdone_index >= 10)
2588 if (copy_from_user(&win_data,
2589 (struct rk_fb_win_cfg_data __user *)argp,
2590 sizeof(win_data))) {
2595 dev_drv->wait_fs = win_data.wait_fs;
2596 rk_fb_set_win_config(info, &win_data);
2598 if (copy_to_user((struct rk_fb_win_cfg_data __user *)arg,
2599 &win_data, sizeof(win_data))) {
2603 memset(&win_data, 0, sizeof(struct rk_fb_win_cfg_data));
2606 dev_drv->ops->ioctl(dev_drv, cmd, arg, win_id);
2613 static int rk_fb_blank(int blank_mode, struct fb_info *info)
2615 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2616 struct fb_fix_screeninfo *fix = &info->fix;
2618 #if defined(CONFIG_RK_HDMI)
2619 struct rk_fb *rk_fb = dev_get_drvdata(info->device);
2622 win_id = dev_drv->ops->fb_get_win_id(dev_drv, fix->id);
2625 #if defined(CONFIG_RK_HDMI)
2626 if ((rk_fb->disp_mode == ONE_DUAL) &&
2627 (hdmi_get_hotplug() == HDMI_HPD_ACTIVED)) {
2628 printk(KERN_INFO "hdmi is connect , not blank lcdc\n");
2632 dev_drv->ops->blank(dev_drv, win_id, blank_mode);
2637 static int rk_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
2639 if ((0 == var->xres_virtual) || (0 == var->yres_virtual) ||
2640 (0 == var->xres) || (0 == var->yres) || (var->xres < 16) ||
2641 ((16 != var->bits_per_pixel) &&
2642 (32 != var->bits_per_pixel) &&
2643 (24 != var->bits_per_pixel))) {
2644 dev_err(info->dev, "%s check var fail 1:\n"
2645 "xres_vir:%d>>yres_vir:%d\n"
2646 "xres:%d>>yres:%d\n"
2647 "bits_per_pixel:%d\n",
2651 var->xres, var->yres, var->bits_per_pixel);
2655 if (((var->xoffset + var->xres) > var->xres_virtual) ||
2656 ((var->yoffset + var->yres) > (var->yres_virtual))) {
2657 dev_err(info->dev, "%s check_var fail 2:\n"
2658 "xoffset:%d>>xres:%d>>xres_vir:%d\n"
2659 "yoffset:%d>>yres:%d>>yres_vir:%d\n",
2664 var->yoffset, var->yres, var->yres_virtual);
2671 static ssize_t rk_fb_read(struct fb_info *info, char __user *buf,
2672 size_t count, loff_t *ppos)
2674 unsigned long p = *ppos;
2677 int c, cnt = 0, err = 0;
2678 unsigned long total_size;
2679 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2680 struct rk_lcdc_win *win = NULL;
2683 win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
2687 win = dev_drv->win[win_id];
2689 /* only read the current frame buffer */
2690 if (win->format == RGB565) {
2691 total_size = win->area[0].y_vir_stride * win->area[0].yact << 1;
2692 } else if (win->format == YUV420) {
2694 (win->area[0].y_vir_stride * win->area[0].yact * 6);
2696 total_size = win->area[0].y_vir_stride * win->area[0].yact << 2;
2698 if (p >= total_size)
2701 if (count >= total_size)
2704 if (count + p > total_size)
2705 count = total_size - p;
2707 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, GFP_KERNEL);
2711 src = (u8 __iomem *)(info->screen_base + p + win->area[0].y_offset);
2714 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
2716 fb_memcpy_fromfb(dst, src, c);
2720 if (copy_to_user(buf, buffer, c)) {
2732 return (err) ? err : cnt;
2735 static ssize_t rk_fb_write(struct fb_info *info, const char __user *buf,
2736 size_t count, loff_t *ppos)
2738 unsigned long p = *ppos;
2741 int c, cnt = 0, err = 0;
2742 unsigned long total_size;
2743 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2744 struct rk_lcdc_win *win = NULL;
2747 win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
2751 win = dev_drv->win[win_id];
2753 /* write the current frame buffer */
2754 if (win->format == RGB565)
2755 total_size = win->area[0].xact * win->area[0].yact << 1;
2757 total_size = win->area[0].xact * win->area[0].yact << 2;
2762 if (count > total_size) {
2767 if (count + p > total_size) {
2771 count = total_size - p;
2774 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, GFP_KERNEL);
2778 dst = (u8 __iomem *)(info->screen_base + p + win->area[0].y_offset);
2781 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
2784 if (copy_from_user(src, buf, c)) {
2789 fb_memcpy_tofb(dst, src, c);
2800 return (cnt) ? cnt : err;
2803 static int rk_fb_set_par(struct fb_info *info)
2805 struct fb_var_screeninfo *var = &info->var;
2806 struct fb_fix_screeninfo *fix = &info->fix;
2807 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
2808 struct rk_fb *rk_fb = dev_get_drvdata(info->device);
2809 int fb_id, extend_win_id = 0;
2810 struct fb_info *extend_info = NULL;
2811 struct rk_lcdc_driver *extend_dev_drv = NULL;
2812 struct rk_lcdc_win *extend_win = NULL;
2813 struct rk_lcdc_win *win = NULL;
2814 struct rk_screen *screen = dev_drv->cur_screen;
2816 u32 cblen = 0, crlen = 0;
2817 u16 xsize = 0, ysize = 0; /* winx display window height/width --->LCDC_WINx_DSP_INFO */
2818 u32 xoffset = var->xoffset; /* offset from virtual to visible */
2819 u32 yoffset = var->yoffset;
2820 u16 xpos = (var->nonstd >> 8) & 0xfff; /*visiable pos in panel */
2821 u16 ypos = (var->nonstd >> 20) & 0xfff;
2822 u32 xvir = var->xres_virtual;
2823 u32 yvir = var->yres_virtual;
2824 u8 data_format = var->nonstd & 0xff;
2828 u32 stride, uv_stride;
2831 u16 uv_x_off, uv_y_off, uv_y_act;
2834 var->pixclock = dev_drv->pixclock;
2835 win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
2839 win = dev_drv->win[win_id];
2840 if (rk_fb->disp_mode == DUAL) {
2841 fb_id = get_extend_fb_id(info);
2842 extend_info = rk_fb->fb[(rk_fb->num_fb >> 1) + fb_id];
2843 extend_dev_drv = (struct rk_lcdc_driver *)extend_info->par;
2844 extend_win_id = dev_drv->ops->fb_get_win_id(extend_dev_drv,
2847 extend_win = extend_dev_drv->win[extend_win_id];
2849 /* if the application has specific the horizontal and vertical display size */
2850 if (var->grayscale >> 8) {
2851 xsize = (var->grayscale >> 8) & 0xfff;
2852 ysize = (var->grayscale >> 20) & 0xfff;
2853 if (xsize > screen->mode.xres)
2854 xsize = screen->mode.xres;
2855 if (ysize > screen->mode.yres)
2856 ysize = screen->mode.yres;
2857 } else { /*ohterwise full screen display */
2858 xsize = screen->mode.xres;
2859 ysize = screen->mode.yres;
2862 fb_data_fmt = rk_fb_data_fmt(data_format, var->bits_per_pixel);
2863 pixel_width = rk_fb_pixel_width(fb_data_fmt);
2864 vir_width_bit = pixel_width * xvir;
2865 /* pixel_width = byte_num * 8 */
2866 stride_32bit_1 = ALIGN_N_TIMES(vir_width_bit, 32) / 8;
2867 stride_32bit_2 = ALIGN_N_TIMES(vir_width_bit * 2, 32) / 8;
2869 switch (fb_data_fmt) {
2873 stride = stride_32bit_1;
2874 uv_stride = stride_32bit_1 >> 1;
2875 uv_x_off = xoffset >> 1;
2877 fix->line_length = stride;
2878 cblen = crlen = (xvir * yvir) >> 1;
2879 uv_y_act = win->area[0].yact >> 1;
2881 case YUV420: /* 420sp */
2884 stride = stride_32bit_1;
2885 uv_stride = stride_32bit_1;
2887 uv_y_off = yoffset >> 1;
2888 fix->line_length = stride;
2889 cblen = crlen = (xvir * yvir) >> 2;
2890 uv_y_act = win->area[0].yact >> 1;
2895 stride = stride_32bit_1;
2896 uv_stride = stride_32bit_2;
2897 uv_x_off = xoffset * 2;
2899 fix->line_length = stride << 2;
2900 cblen = crlen = (xvir * yvir);
2901 uv_y_act = win->area[0].yact;
2904 stride = stride_32bit_1; /* default rgb */
2905 fix->line_length = stride;
2909 /* x y mirror ,jump line */
2910 if (screen->y_mirror == 1) {
2911 if (screen->interlace == 1) {
2912 win->area[0].y_offset = yoffset * stride * 2 +
2913 ((win->area[0].yact - 1) * 2 + 1) * stride +
2914 xoffset * pixel_width / 8;
2916 win->area[0].y_offset = yoffset * stride +
2917 (win->area[0].yact - 1) * stride +
2918 xoffset * pixel_width / 8;
2921 if (screen->interlace == 1) {
2922 win->area[0].y_offset =
2923 yoffset * stride * 2 + xoffset * pixel_width / 8;
2925 win->area[0].y_offset =
2926 yoffset * stride + xoffset * pixel_width / 8;
2929 if (is_pic_yuv == 1) {
2930 if (screen->y_mirror == 1) {
2931 if (screen->interlace == 1) {
2932 win->area[0].c_offset =
2933 uv_y_off * uv_stride * 2 +
2934 ((uv_y_act - 1) * 2 + 1) * uv_stride +
2935 uv_x_off * pixel_width / 8;
2937 win->area[0].c_offset = uv_y_off * uv_stride +
2938 (uv_y_act - 1) * uv_stride +
2939 uv_x_off * pixel_width / 8;
2942 if (screen->interlace == 1) {
2943 win->area[0].c_offset =
2944 uv_y_off * uv_stride * 2 +
2945 uv_x_off * pixel_width / 8;
2947 win->area[0].c_offset =
2948 uv_y_off * uv_stride +
2949 uv_x_off * pixel_width / 8;
2954 win->format = fb_data_fmt;
2955 win->area[0].y_vir_stride = stride >> 2;
2956 win->area[0].uv_vir_stride = uv_stride >> 2;
2957 win->area[0].xpos = xpos;
2958 win->area[0].ypos = ypos;
2959 win->area[0].xsize = xsize;
2960 win->area[0].ysize = ysize;
2961 win->area[0].xact = var->xres; /* winx active window height,is a wint of vir */
2962 win->area[0].yact = var->yres;
2963 win->area[0].xvir = var->xres_virtual; /* virtual resolution stride --->LCDC_WINx_VIR */
2964 win->area[0].yvir = var->yres_virtual;
2967 win->alpha_mode = 4; /* AB_SRC_OVER; */
2968 win->alpha_en = ((win->format == ARGB888) ||
2969 (win->format == ABGR888)) ? 1 : 0;
2970 win->g_alpha_val = 0;
2972 if (rk_fb->disp_mode == DUAL) {
2973 if (extend_win->state && (hdmi_switch_complete == 0xff)) {
2974 if (info != extend_info) {
2975 if (win->area[0].xact < win->area[0].yact) {
2976 extend_win->area[0].xact =
2978 extend_win->area[0].yact =
2980 extend_win->area[0].xvir =
2982 extend_info->var.xres = var->yres;
2983 extend_info->var.yres = var->xres;
2984 extend_info->var.xres_virtual =
2987 extend_win->area[0].xact =
2989 extend_win->area[0].yact =
2991 extend_win->area[0].xvir =
2993 extend_info->var.xres = var->xres;
2994 extend_info->var.yres = var->yres;
2995 extend_info->var.xres_virtual =
2998 extend_win->area[0].y_vir_stride =
2999 win->area[0].y_vir_stride;
3000 extend_win->area[0].uv_vir_stride =
3001 win->area[0].uv_vir_stride;
3002 if (win->area[0].xpos != 0 ||
3003 win->area[0].ypos != 0) {
3004 extend_win->area[0].xsize =
3005 (extend_dev_drv->cur_screen->xsize * win->area[0].xsize) / screen->mode.xres;
3006 extend_win->area[0].ysize =
3007 (extend_dev_drv->cur_screen->ysize * win->area[0].ysize) / screen->mode.yres;
3008 extend_win->area[0].xpos =
3009 ((extend_dev_drv->cur_screen->mode.xres - extend_dev_drv->cur_screen->xsize) >> 1) +
3010 extend_dev_drv->cur_screen->xsize * win->area[0].xpos / screen->mode.xres;
3011 extend_win->area[0].ypos =
3012 ((extend_dev_drv->cur_screen->mode.yres - extend_dev_drv->cur_screen->ysize) >> 1) +
3013 extend_dev_drv->cur_screen->ysize * win->area[0].ypos / screen->mode.yres;
3014 } else { /* the display image of the primary screen is full screen size */
3015 extend_win->area[0].xpos =
3016 (extend_dev_drv->cur_screen->mode.xres - extend_dev_drv->cur_screen->xsize) >> 1;
3017 extend_win->area[0].ypos =
3018 (extend_dev_drv->cur_screen->mode.yres - extend_dev_drv->cur_screen->ysize) >> 1;
3019 extend_win->area[0].xsize =
3020 extend_dev_drv->cur_screen->xsize;
3021 extend_win->area[0].ysize =
3022 extend_dev_drv->cur_screen->ysize;
3025 extend_win->area[0].state = 1;
3026 extend_win->area_num = 1;
3027 extend_win->alpha_en = 0;
3028 extend_win->format = win->format;
3029 extend_info->var.nonstd &= 0xffffff00;
3030 extend_info->var.nonstd |= data_format;
3031 extend_dev_drv->ops->set_par(extend_dev_drv,
3036 dev_drv->ops->set_par(dev_drv, win_id);
3041 static inline unsigned int chan_to_field(unsigned int chan,
3042 struct fb_bitfield *bf)
3045 chan >>= 16 - bf->length;
3046 return chan << bf->offset;
3049 static int fb_setcolreg(unsigned regno,
3050 unsigned red, unsigned green, unsigned blue,
3051 unsigned transp, struct fb_info *info)
3055 switch (info->fix.visual) {
3056 case FB_VISUAL_TRUECOLOR:
3057 /* true-colour, use pseudo-palette */
3059 u32 *pal = info->pseudo_palette;
3060 val = chan_to_field(red, &info->var.red);
3061 val |= chan_to_field(green, &info->var.green);
3062 val |= chan_to_field(blue, &info->var.blue);
3067 return -1; /* unknown type */
3073 static int rk_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
3075 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)info->par;
3076 int win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
3077 struct rk_lcdc_win *win;
3078 win = dev_drv->win[win_id];
3080 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
3082 return dma_buf_mmap(win->area[0].dma_buf, vma, 0);
3085 static struct fb_ops fb_ops = {
3086 .owner = THIS_MODULE,
3087 .fb_open = rk_fb_open,
3088 .fb_release = rk_fb_close,
3089 .fb_check_var = rk_fb_check_var,
3090 .fb_set_par = rk_fb_set_par,
3091 .fb_blank = rk_fb_blank,
3092 .fb_ioctl = rk_fb_ioctl,
3093 .fb_pan_display = rk_fb_pan_display,
3094 .fb_read = rk_fb_read,
3095 .fb_write = rk_fb_write,
3096 .fb_setcolreg = fb_setcolreg,
3097 .fb_fillrect = cfb_fillrect,
3098 .fb_copyarea = cfb_copyarea,
3099 .fb_imageblit = cfb_imageblit,
3102 static struct fb_var_screeninfo def_var = {
3103 #if defined(CONFIG_LOGO_LINUX_BMP)
3107 .transp = {0, 0, 0},
3108 .nonstd = HAL_PIXEL_FORMAT_BGRA_8888,
3113 .transp = {0, 0, 0},
3114 .nonstd = HAL_PIXEL_FORMAT_RGB_565, /* (ypos<<20+xpos<<8+format) format */
3116 .grayscale = 0, /* (ysize<<20+xsize<<8) */
3117 .activate = FB_ACTIVATE_NOW,
3119 .vmode = FB_VMODE_NONINTERLACED,
3122 static struct fb_fix_screeninfo def_fix = {
3123 .type = FB_TYPE_PACKED_PIXELS,
3128 .accel = FB_ACCEL_NONE,
3129 .visual = FB_VISUAL_TRUECOLOR,
3133 static int rk_fb_wait_for_vsync_thread(void *data)
3135 struct rk_lcdc_driver *dev_drv = data;
3136 struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3137 struct fb_info *fbi = rk_fb->fb[0];
3139 while (!kthread_should_stop()) {
3140 ktime_t timestamp = dev_drv->vsync_info.timestamp;
3141 int ret = wait_event_interruptible(dev_drv->vsync_info.wait,
3142 !ktime_equal(timestamp, dev_drv->vsync_info.timestamp) &&
3143 (dev_drv->vsync_info.active || dev_drv->vsync_info.irq_stop));
3146 sysfs_notify(&fbi->dev->kobj, NULL, "vsync");
3152 static ssize_t rk_fb_vsync_show(struct device *dev,
3153 struct device_attribute *attr, char *buf)
3155 struct fb_info *fbi = dev_get_drvdata(dev);
3156 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)fbi->par;
3157 return scnprintf(buf, PAGE_SIZE, "%llu\n",
3158 ktime_to_ns(dev_drv->vsync_info.timestamp));
3161 static DEVICE_ATTR(vsync, S_IRUGO, rk_fb_vsync_show, NULL);
3164 * this two function is for other module that in the kernel which
3165 * need show image directly through fb
3166 * fb_id:we have 4 fb here,default we use fb0 for ui display
3168 struct fb_info *rk_get_fb(int fb_id)
3170 struct rk_fb *inf = platform_get_drvdata(fb_pdev);
3171 struct fb_info *fb = inf->fb[fb_id];
3174 EXPORT_SYMBOL(rk_get_fb);
3176 void rk_direct_fb_show(struct fb_info *fbi)
3179 rk_fb_pan_display(&fbi->var, fbi);
3181 EXPORT_SYMBOL(rk_direct_fb_show);
3183 int rk_fb_dpi_open(bool open)
3185 struct rk_lcdc_driver *dev_drv = NULL;
3186 dev_drv = rk_get_prmry_lcdc_drv();
3188 if (dev_drv->ops->dpi_open)
3189 dev_drv->ops->dpi_open(dev_drv, open);
3193 int rk_fb_dpi_win_sel(int win_id)
3195 struct rk_lcdc_driver *dev_drv = NULL;
3196 dev_drv = rk_get_prmry_lcdc_drv();
3198 if (dev_drv->ops->dpi_win_sel)
3199 dev_drv->ops->dpi_win_sel(dev_drv, win_id);
3203 int rk_fb_dpi_status(void)
3206 struct rk_lcdc_driver *dev_drv = NULL;
3208 dev_drv = rk_get_prmry_lcdc_drv();
3209 if (dev_drv->ops->dpi_status)
3210 ret = dev_drv->ops->dpi_status(dev_drv);
3216 * function: this function will be called to modify hdmi info according to
3217 * pmy info for DUAL disp_mode when HDMI is used as EXTEND
3218 * @pmy_info: primary screen fb_info
3219 * @hdmi_info: hdmi screen fb_info
3220 * @screen: the hdmi screen info
3222 static int rk_fb_hdmi_info_align(struct fb_info *pmy_info,
3223 struct fb_info *hdmi_info,
3224 struct rk_screen *screen)
3226 struct rk_fb *inf = platform_get_drvdata(fb_pdev);
3227 struct fb_var_screeninfo *hdmi_var = NULL;
3228 struct fb_var_screeninfo *pmy_var = NULL;
3230 if(unlikely(!pmy_info) || unlikely(!hdmi_info))
3233 if (inf->disp_mode != DUAL || pmy_info == hdmi_info)
3236 pmy_var = &pmy_info->var;
3237 hdmi_var = &hdmi_info->var;
3238 if (pmy_var->xres < pmy_var->yres) { /* vertical lcd screen */
3239 hdmi_var->xres = pmy_var->yres;
3240 hdmi_var->yres = pmy_var->xres;
3241 hdmi_var->xres_virtual = pmy_var->yres;
3243 hdmi_var->xres = pmy_var->xres;
3244 hdmi_var->yres = pmy_var->yres;
3245 hdmi_var->xres_virtual = pmy_var->xres_virtual;
3248 /* use the same format as primary screen */
3249 hdmi_var->nonstd &= 0xffffff00;
3250 hdmi_var->nonstd |= (pmy_var->nonstd & 0xff);
3253 hdmi_var->grayscale &= 0xff;
3254 hdmi_var->grayscale |= (screen->mode.xres << 8) +
3255 (screen->mode.yres << 20);
3262 * function: this function will be called by display device, enable/disable lcdc
3263 * @screen: screen timing to be set to lcdc
3264 * @enable: 0 disable lcdc; 1 enable change lcdc timing; 2 just enable dclk
3265 * @lcdc_id: the lcdc id the display device attached ,0 or 1
3267 int rk_fb_switch_screen(struct rk_screen *screen, int enable, int lcdc_id)
3269 struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3270 struct fb_info *info = NULL;
3271 struct fb_info *pmy_info = NULL;
3272 struct rk_lcdc_driver *dev_drv = NULL;
3273 struct rk_lcdc_driver *pmy_dev_drv = rk_get_prmry_lcdc_drv();
3275 int i, win_id, load_screen = 0;
3277 if (unlikely(!rk_fb) || unlikely(!pmy_dev_drv) || unlikely(!screen))
3280 /* get lcdc driver */
3281 sprintf(name, "lcdc%d", lcdc_id);
3282 if (rk_fb->disp_mode != DUAL)
3283 dev_drv = rk_fb->lcdc_dev_drv[0];
3285 dev_drv = rk_get_lcdc_drv(name);
3287 if (dev_drv == NULL) {
3288 printk(KERN_ERR "%s driver not found!", name);
3291 if (screen->type == SCREEN_HDMI)
3292 printk("hdmi %s lcdc%d\n", enable ? "connect to" : "remove from",
3294 else if (screen->type == SCREEN_TVOUT)
3295 printk("cvbs %s lcdc%d\n", enable ? "connect to" : "remove from",
3298 if (enable == 2 /*&& dev_drv->enable*/)
3301 if (rk_fb->disp_mode == ONE_DUAL) {
3302 if (dev_drv->trsm_ops && dev_drv->trsm_ops->disable)
3303 dev_drv->trsm_ops->disable();
3304 if (dev_drv->ops->set_screen_scaler)
3305 dev_drv->ops->set_screen_scaler(dev_drv, dev_drv->screen0, 0);
3309 /* if screen type is different, we do not disable lcdc. */
3310 if (dev_drv->cur_screen->type != screen->type)
3313 /* if used one lcdc to dual disp, no need to close win */
3314 if (rk_fb->disp_mode == ONE_DUAL) {
3315 dev_drv->cur_screen = dev_drv->screen0;
3316 dev_drv->ops->load_screen(dev_drv, 1);
3317 if (dev_drv->trsm_ops && dev_drv->trsm_ops->enable)
3318 dev_drv->trsm_ops->enable();
3319 } else if (rk_fb->num_lcdc > 1) {
3320 /* If there is more than one lcdc device, we disable
3321 the layer which attached to this device */
3322 for (i = 0; i < dev_drv->lcdc_win_num; i++) {
3323 if (dev_drv->win[i] && dev_drv->win[i]->state)
3324 dev_drv->ops->open(dev_drv, i, 0);
3328 hdmi_switch_complete = 0;
3331 if (dev_drv->screen1)
3332 dev_drv->cur_screen = dev_drv->screen1;
3333 memcpy(dev_drv->cur_screen, screen, sizeof(struct rk_screen));
3334 dev_drv->cur_screen->x_mirror = dev_drv->rotate_mode & X_MIRROR;
3335 dev_drv->cur_screen->y_mirror = dev_drv->rotate_mode & Y_MIRROR;
3338 for (i = 0; i < dev_drv->lcdc_win_num; i++) {
3339 info = rk_fb->fb[dev_drv->fb_index_base + i];
3340 if (rk_fb->disp_mode == DUAL &&
3341 dev_drv != pmy_dev_drv) {
3342 pmy_info = rk_fb->fb[pmy_dev_drv->fb_index_base + i];
3343 rk_fb_hdmi_info_align(pmy_info, info,
3344 dev_drv->cur_screen);
3347 win_id = dev_drv->ops->fb_get_win_id(dev_drv, info->fix.id);
3348 if (dev_drv->win[win_id]) {
3349 if (rk_fb->disp_mode == DUAL) {
3350 if (dev_drv != pmy_dev_drv &&
3351 pmy_dev_drv->win[win_id]) {
3352 dev_drv->win[win_id]->logicalstate =
3353 pmy_dev_drv->win[win_id]->logicalstate;
3356 if (dev_drv->win[win_id]->logicalstate) {
3357 if (!dev_drv->win[win_id]->state)
3358 dev_drv->ops->open(dev_drv, win_id, 1);
3360 dev_drv->ops->load_screen(dev_drv, 1);
3363 info->var.activate |= FB_ACTIVATE_FORCE;
3364 info->fbops->fb_set_par(info);
3365 hdmi_switch_complete |= (1 << win_id);
3366 if (rk_fb->disp_mode == DUAL)
3367 pmy_info->fbops->fb_pan_display(&pmy_info->var,
3370 info->fbops->fb_pan_display(&info->var, info);
3375 hdmi_switch_complete = 0xff;
3376 if (rk_fb->disp_mode == ONE_DUAL) {
3377 if (dev_drv->ops->set_screen_scaler)
3378 dev_drv->ops->set_screen_scaler(dev_drv, dev_drv->screen0, 1);
3379 if (dev_drv->trsm_ops && dev_drv->trsm_ops->enable)
3380 dev_drv->trsm_ops->enable();
3386 * function:this function current only called by hdmi for
3388 * scale_x: scale rate of x resolution
3389 * scale_y: scale rate of y resolution
3390 * lcdc_id: the lcdc id the hdmi attached ,0 or 1
3392 int rk_fb_disp_scale(u8 scale_x, u8 scale_y, u8 lcdc_id)
3394 struct rk_fb *inf = platform_get_drvdata(fb_pdev);
3395 struct fb_info *info = NULL;
3396 struct fb_var_screeninfo *var = NULL;
3397 struct rk_lcdc_driver *dev_drv = NULL;
3398 u16 screen_x, screen_y;
3401 struct rk_screen primary_screen;
3402 rk_fb_get_prmry_screen(&primary_screen);
3403 if (primary_screen.type == SCREEN_HDMI) {
3407 sprintf(name, "lcdc%d", lcdc_id);
3409 if (inf->disp_mode == DUAL) {
3410 dev_drv = rk_get_lcdc_drv(name);
3411 if (dev_drv == NULL) {
3412 printk(KERN_ERR "%s driver not found!", name);
3416 dev_drv = inf->lcdc_dev_drv[0];
3419 if (inf->num_lcdc == 1)
3421 else if (inf->num_lcdc == 2)
3422 info = inf->fb[dev_drv->lcdc_win_num];
3425 screen_x = dev_drv->cur_screen->mode.xres;
3426 screen_y = dev_drv->cur_screen->mode.yres;
3428 if (inf->disp_mode != DUAL && dev_drv->screen1) {
3429 dev_drv->cur_screen->xpos =
3430 (screen_x - screen_x * scale_x / 100) >> 1;
3431 dev_drv->cur_screen->ypos =
3432 (screen_y - screen_y * scale_y / 100) >> 1;
3433 dev_drv->cur_screen->xsize = screen_x * scale_x / 100;
3434 dev_drv->cur_screen->ysize = screen_y * scale_y / 100;
3436 xpos = (screen_x - screen_x * scale_x / 100) >> 1;
3437 ypos = (screen_y - screen_y * scale_y / 100) >> 1;
3438 dev_drv->cur_screen->xsize = screen_x * scale_x / 100;
3439 dev_drv->cur_screen->ysize = screen_y * scale_y / 100;
3440 var->nonstd &= 0xff;
3441 var->nonstd |= (xpos << 8) + (ypos << 20);
3442 var->grayscale &= 0xff;
3444 (dev_drv->cur_screen->xsize << 8) + (dev_drv->cur_screen->ysize << 20);
3447 info->fbops->fb_set_par(info);
3448 /* info->fbops->fb_ioctl(info, RK_FBIOSET_CONFIG_DONE, 0); */
3449 dev_drv->ops->cfg_done(dev_drv);
3453 #if defined(CONFIG_ION_ROCKCHIP)
3454 static int rk_fb_alloc_buffer_by_ion(struct fb_info *fbi,
3455 struct rk_lcdc_win *win,
3456 unsigned long fb_mem_size)
3458 struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3459 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)fbi->par;
3460 struct ion_handle *handle;
3461 ion_phys_addr_t phy_addr;
3465 if (dev_drv->iommu_enabled)
3466 handle = ion_alloc(rk_fb->ion_client, (size_t) fb_mem_size, 0,
3467 ION_HEAP(ION_VMALLOC_HEAP_ID), 0);
3469 handle = ion_alloc(rk_fb->ion_client, (size_t) fb_mem_size, 0,
3470 ION_HEAP(ION_CMA_HEAP_ID), 0);
3471 if (IS_ERR(handle)) {
3472 dev_err(fbi->device, "failed to ion_alloc:%ld\n",
3476 win->area[0].dma_buf = ion_share_dma_buf(rk_fb->ion_client, handle);
3477 if (IS_ERR_OR_NULL(win->area[0].dma_buf)) {
3478 printk("ion_share_dma_buf() failed\n");
3479 goto err_share_dma_buf;
3481 win->area[0].ion_hdl = handle;
3482 if (dev_drv->prop == PRMRY)
3483 fbi->screen_base = ion_map_kernel(rk_fb->ion_client, handle);
3484 #ifdef CONFIG_ROCKCHIP_IOMMU
3485 if (dev_drv->iommu_enabled)
3486 ret = ion_map_iommu(dev_drv->dev, rk_fb->ion_client, handle,
3487 (unsigned long *)&phy_addr,
3488 (unsigned long *)&len);
3490 ret = ion_phys(rk_fb->ion_client, handle, &phy_addr, &len);
3492 ret = ion_phys(rk_fb->ion_client, handle, &phy_addr, &len);
3495 dev_err(fbi->dev, "ion map to get phy addr failed\n");
3496 goto err_share_dma_buf;
3498 fbi->fix.smem_start = phy_addr;
3499 fbi->fix.smem_len = len;
3500 printk(KERN_INFO "alloc_buffer:ion_phy_addr=0x%lx\n", phy_addr);
3504 ion_free(rk_fb->ion_client, handle);
3509 static int rk_fb_alloc_buffer(struct fb_info *fbi, int fb_id)
3511 struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3512 struct rk_lcdc_driver *dev_drv = (struct rk_lcdc_driver *)fbi->par;
3513 struct rk_lcdc_win *win = NULL;
3516 unsigned long fb_mem_size;
3517 #if !defined(CONFIG_ION_ROCKCHIP)
3518 dma_addr_t fb_mem_phys;
3522 win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
3526 win = dev_drv->win[win_id];
3528 if (!strcmp(fbi->fix.id, "fb0")) {
3529 fb_mem_size = get_fb_size();
3530 #if defined(CONFIG_ION_ROCKCHIP)
3531 if (rk_fb_alloc_buffer_by_ion(fbi, win, fb_mem_size) < 0)
3534 fb_mem_virt = dma_alloc_writecombine(fbi->dev, fb_mem_size,
3535 &fb_mem_phys, GFP_KERNEL);
3537 pr_err("%s: Failed to allocate framebuffer\n",
3541 fbi->fix.smem_len = fb_mem_size;
3542 fbi->fix.smem_start = fb_mem_phys;
3543 fbi->screen_base = fb_mem_virt;
3545 memset(fbi->screen_base, 0, fbi->fix.smem_len);
3546 printk(KERN_INFO "fb%d:phy:%lx>>vir:%p>>len:0x%x\n", fb_id,
3547 fbi->fix.smem_start, fbi->screen_base,
3550 if (dev_drv->rotate_mode > X_Y_MIRROR) {
3551 fb_mem_size = get_rotate_fb_size();
3552 #if defined(CONFIG_ION_ROCKCHIP)
3553 if (rk_fb_alloc_buffer_by_ion(fbi, win, fb_mem_size) < 0)
3557 dma_alloc_writecombine(fbi->dev,
3562 pr_err("%s: Failed to allocate framebuffer\n",
3566 fbi->fix.smem_len = fb_mem_size;
3567 fbi->fix.smem_start = fb_mem_phys;
3568 fbi->screen_base = fb_mem_virt;
3571 fbi->fix.smem_start = rk_fb->fb[0]->fix.smem_start;
3572 fbi->fix.smem_len = rk_fb->fb[0]->fix.smem_len;
3573 fbi->screen_base = rk_fb->fb[0]->screen_base;
3576 printk(KERN_INFO "fb%d:phy:%lx>>vir:%p>>len:0x%x\n", fb_id,
3577 fbi->fix.smem_start, fbi->screen_base,
3581 fbi->screen_size = fbi->fix.smem_len;
3582 win_id = dev_drv->ops->fb_get_win_id(dev_drv, fbi->fix.id);
3584 win = dev_drv->win[win_id];
3585 win->reserved = fbi->fix.smem_start;
3592 static int rk_release_fb_buffer(struct fb_info *fbi)
3594 /* buffer for fb1 and fb3 are alloc by android */
3595 if (!strcmp(fbi->fix.id, "fb1") || !strcmp(fbi->fix.id, "fb3"))
3597 iounmap(fbi->screen_base);
3598 release_mem_region(fbi->fix.smem_start, fbi->fix.smem_len);
3603 static int init_lcdc_win(struct rk_lcdc_driver *dev_drv,
3604 struct rk_lcdc_win *def_win)
3607 int lcdc_win_num = dev_drv->lcdc_win_num;
3609 for (i = 0; i < lcdc_win_num; i++) {
3610 struct rk_lcdc_win *win = NULL;
3611 win = kzalloc(sizeof(struct rk_lcdc_win), GFP_KERNEL);
3613 dev_err(dev_drv->dev, "kzmalloc for win fail!");
3617 strcpy(win->name, def_win[i].name);
3618 win->id = def_win[i].id;
3619 win->support_3d = def_win[i].support_3d;
3620 dev_drv->win[i] = win;
3626 static int init_lcdc_device_driver(struct rk_fb *rk_fb,
3627 struct rk_lcdc_win *def_win, int index)
3629 struct rk_lcdc_driver *dev_drv = rk_fb->lcdc_dev_drv[index];
3630 struct rk_screen *screen = devm_kzalloc(dev_drv->dev,
3631 sizeof(struct rk_screen),
3635 dev_err(dev_drv->dev, "malloc screen for lcdc%d fail!",
3640 screen->screen_id = 0;
3641 screen->lcdc_id = dev_drv->id;
3642 screen->overscan.left = 100;
3643 screen->overscan.top = 100;
3644 screen->overscan.right = 100;
3645 screen->overscan.bottom = 100;
3647 screen->x_mirror = dev_drv->rotate_mode & X_MIRROR;
3648 screen->y_mirror = dev_drv->rotate_mode & Y_MIRROR;
3650 dev_drv->screen0 = screen;
3651 dev_drv->cur_screen = screen;
3652 /* devie use one lcdc + rk61x scaler for dual display */
3653 if (rk_fb->disp_mode == ONE_DUAL) {
3654 struct rk_screen *screen1 =
3655 devm_kzalloc(dev_drv->dev,
3656 sizeof(struct rk_screen),
3659 dev_err(dev_drv->dev, "malloc screen1 for lcdc%d fail!",
3663 screen1->screen_id = 1;
3664 screen1->lcdc_id = 1;
3665 dev_drv->screen1 = screen1;
3667 sprintf(dev_drv->name, "lcdc%d", dev_drv->id);
3668 init_lcdc_win(dev_drv, def_win);
3669 init_completion(&dev_drv->frame_done);
3670 spin_lock_init(&dev_drv->cpl_lock);
3671 mutex_init(&dev_drv->fb_win_id_mutex);
3672 dev_drv->ops->fb_win_remap(dev_drv, dev_drv->fb_win_map);
3673 dev_drv->first_frame = 1;
3674 dev_drv->overscan.left = 100;
3675 dev_drv->overscan.top = 100;
3676 dev_drv->overscan.right = 100;
3677 dev_drv->overscan.bottom = 100;
3678 rk_disp_pwr_ctr_parse_dt(dev_drv);
3679 if (dev_drv->prop == PRMRY) {
3680 if (dev_drv->ops->set_dsp_cabc)
3681 dev_drv->ops->set_dsp_cabc(dev_drv, dev_drv->cabc_mode);
3682 rk_fb_set_prmry_screen(screen);
3683 rk_fb_get_prmry_screen(screen);
3685 dev_drv->trsm_ops = rk_fb_trsm_ops_get(screen->type);
3686 if (dev_drv->prop != PRMRY)
3687 rk_fb_get_prmry_screen(screen);
3692 #ifdef CONFIG_LOGO_LINUX_BMP
3693 static struct linux_logo *bmp_logo;
3694 static int fb_prewine_bmp_logo(struct fb_info *info, int rotate)
3696 bmp_logo = fb_find_logo(24);
3697 if (bmp_logo == NULL) {
3698 printk(KERN_INFO "%s error\n", __func__);
3704 static void fb_show_bmp_logo(struct fb_info *info, int rotate)
3706 unsigned char *src = bmp_logo->data;
3707 unsigned char *dst = info->screen_base;
3709 unsigned int Needwidth = (*(src - 24) << 8) | (*(src - 23));
3710 unsigned int Needheight = (*(src - 22) << 8) | (*(src - 21));
3712 for (i = 0; i < Needheight; i++)
3713 memcpy(dst + info->var.xres * i * 4,
3714 src + bmp_logo->width * i * 4, Needwidth * 4);
3719 * check if the primary lcdc has registerd,
3720 * the primary lcdc mas register first
3722 bool is_prmry_rk_lcdc_registered(void)
3724 struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3726 if (rk_fb->lcdc_dev_drv[0])
3732 int rk_fb_register(struct rk_lcdc_driver *dev_drv,
3733 struct rk_lcdc_win *win, int id)
3735 struct rk_fb *rk_fb = platform_get_drvdata(fb_pdev);
3736 struct fb_info *fbi;
3737 int i = 0, ret = 0, index = 0;
3739 #if defined(CONFIG_ROCKCHIP_IOMMU)
3740 struct device *mmu_dev = NULL;
3743 if (rk_fb->num_lcdc == RK30_MAX_LCDC_SUPPORT)
3746 for (i = 0; i < RK30_MAX_LCDC_SUPPORT; i++) {
3747 if (!rk_fb->lcdc_dev_drv[i]) {
3748 rk_fb->lcdc_dev_drv[i] = dev_drv;
3749 rk_fb->lcdc_dev_drv[i]->id = id;
3756 init_lcdc_device_driver(rk_fb, win, index);
3757 dev_drv->fb_index_base = rk_fb->num_fb;
3758 for (i = 0; i < dev_drv->lcdc_win_num; i++) {
3759 fbi = framebuffer_alloc(0, &fb_pdev->dev);
3761 dev_err(&fb_pdev->dev, "fb framebuffer_alloc fail!");
3767 sprintf(fbi->fix.id, "fb%d", rk_fb->num_fb);
3768 fb_videomode_to_var(&fbi->var, &dev_drv->cur_screen->mode);
3769 fbi->var.grayscale |=
3770 (fbi->var.xres << 8) + (fbi->var.yres << 20);
3771 #if defined(CONFIG_LOGO_LINUX_BMP)
3772 fbi->var.bits_per_pixel = 32;
3774 fbi->var.bits_per_pixel = 16;
3776 fbi->fix.line_length =
3777 (fbi->var.xres_virtual) * (fbi->var.bits_per_pixel >> 3);
3778 fbi->var.width = dev_drv->cur_screen->width;
3779 fbi->var.height = dev_drv->cur_screen->height;
3780 fbi->var.pixclock = dev_drv->pixclock;
3781 if (dev_drv->iommu_enabled)
3782 fb_ops.fb_mmap = rk_fb_mmap;
3783 fbi->fbops = &fb_ops;
3784 fbi->flags = FBINFO_FLAG_DEFAULT;
3785 fbi->pseudo_palette = dev_drv->win[i]->pseudo_pal;
3786 ret = register_framebuffer(fbi);
3788 dev_err(&fb_pdev->dev,
3789 "%s fb%d register_framebuffer fail!\n",
3790 __func__, rk_fb->num_fb);
3793 rkfb_create_sysfs(fbi);
3794 rk_fb->fb[rk_fb->num_fb] = fbi;
3795 dev_info(fbi->dev, "rockchip framebuffer registerd:%s\n",
3800 init_waitqueue_head(&dev_drv->vsync_info.wait);
3801 init_waitqueue_head(&dev_drv->update_regs_wait);
3802 ret = device_create_file(fbi->dev, &dev_attr_vsync);
3805 "failed to create vsync file\n");
3806 dev_drv->vsync_info.thread =
3807 kthread_run(rk_fb_wait_for_vsync_thread, dev_drv,
3809 if (dev_drv->vsync_info.thread == ERR_PTR(-ENOMEM)) {
3811 "failed to run vsync thread\n");
3812 dev_drv->vsync_info.thread = NULL;
3814 dev_drv->vsync_info.active = 1;
3816 mutex_init(&dev_drv->output_lock);
3818 INIT_LIST_HEAD(&dev_drv->update_regs_list);
3819 mutex_init(&dev_drv->update_regs_list_lock);
3820 init_kthread_worker(&dev_drv->update_regs_worker);
3822 dev_drv->update_regs_thread =
3823 kthread_run(kthread_worker_fn,
3824 &dev_drv->update_regs_worker, "rk-fb");
3825 if (IS_ERR(dev_drv->update_regs_thread)) {
3826 int err = PTR_ERR(dev_drv->update_regs_thread);
3827 dev_drv->update_regs_thread = NULL;
3829 printk("failed to run update_regs thread\n");
3832 init_kthread_work(&dev_drv->update_regs_work,
3833 rk_fb_update_regs_handler);
3836 sw_sync_timeline_create("fb-timeline");
3837 dev_drv->timeline_max = 1;
3841 /* show logo for primary display device */
3842 #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
3843 if (dev_drv->prop == PRMRY) {
3844 struct fb_info *main_fbi = rk_fb->fb[0];
3845 main_fbi->fbops->fb_open(main_fbi, 1);
3847 #if defined(CONFIG_ROCKCHIP_IOMMU)
3848 if (dev_drv->iommu_enabled) {
3849 if (dev_drv->mmu_dev)
3850 rockchip_iovmm_set_fault_handler(dev_drv->dev,
3851 rk_fb_sysmmu_fault_handler);
3855 rk_fb_alloc_buffer(main_fbi, 0); /* only alloc memory for main fb */
3856 if (support_uboot_display()) {
3857 if (dev_drv->iommu_enabled) {
3858 rk_fb_copy_from_loader(main_fbi);
3859 dev_drv->ops->direct_set_addr(dev_drv, 0,
3860 main_fbi->fix.smem_start);
3864 main_fbi->fbops->fb_set_par(main_fbi);
3865 #if defined(CONFIG_LOGO_LINUX_BMP)
3866 if (fb_prewine_bmp_logo(main_fbi, FB_ROTATE_UR)) {
3867 fb_set_cmap(&main_fbi->cmap, main_fbi);
3868 fb_show_bmp_logo(main_fbi, FB_ROTATE_UR);
3871 if (fb_prepare_logo(main_fbi, FB_ROTATE_UR)) {
3872 fb_set_cmap(&main_fbi->cmap, main_fbi);
3873 fb_show_logo(main_fbi, FB_ROTATE_UR);
3876 main_fbi->fbops->fb_pan_display(&main_fbi->var, main_fbi);
3878 #if !defined(CONFIG_ROCKCHIP_IOMMU)
3879 struct fb_info *extend_fbi = rk_fb->fb[rk_fb->num_fb >> 1];
3880 int extend_fb_id = get_extend_fb_id(extend_fbi);
3882 rk_fb_alloc_buffer(extend_fbi, extend_fb_id);
3889 int rk_fb_unregister(struct rk_lcdc_driver *dev_drv)
3891 struct rk_fb *fb_inf = platform_get_drvdata(fb_pdev);
3892 struct fb_info *fbi;
3893 int fb_index_base = dev_drv->fb_index_base;
3894 int fb_num = dev_drv->lcdc_win_num;
3897 if (fb_inf->lcdc_dev_drv[i]->vsync_info.thread) {
3898 fb_inf->lcdc_dev_drv[i]->vsync_info.irq_stop = 1;
3899 kthread_stop(fb_inf->lcdc_dev_drv[i]->vsync_info.thread);
3902 for (i = 0; i < fb_num; i++)
3903 kfree(dev_drv->win[i]);
3905 for (i = fb_index_base; i < (fb_index_base + fb_num); i++) {
3906 fbi = fb_inf->fb[i];
3907 unregister_framebuffer(fbi);
3908 /* rk_release_fb_buffer(fbi); */
3909 framebuffer_release(fbi);
3911 fb_inf->lcdc_dev_drv[dev_drv->id] = NULL;
3917 static int rk_fb_probe(struct platform_device *pdev)
3919 struct rk_fb *rk_fb = NULL;
3920 struct device_node *np = pdev->dev.of_node;
3924 dev_err(&pdev->dev, "Missing device tree node.\n");
3928 rk_fb = devm_kzalloc(&pdev->dev, sizeof(struct rk_fb), GFP_KERNEL);
3930 dev_err(&pdev->dev, "kmalloc for rk fb fail!");
3933 platform_set_drvdata(pdev, rk_fb);
3935 if (!of_property_read_u32(np, "rockchip,disp-mode", &mode)) {
3936 rk_fb->disp_mode = mode;
3939 dev_err(&pdev->dev, "no disp-mode node found!");
3943 if (!of_property_read_u32(np, "rockchip,uboot-logo-on", &uboot_logo_on))
3944 printk(KERN_DEBUG "uboot-logo-on:%d\n", uboot_logo_on);
3946 dev_set_name(&pdev->dev, "rockchip-fb");
3947 #if defined(CONFIG_ION_ROCKCHIP)
3948 rk_fb->ion_client = rockchip_ion_client_create("rk_fb");
3949 if (IS_ERR(rk_fb->ion_client)) {
3950 dev_err(&pdev->dev, "failed to create ion client for rk fb");
3951 return PTR_ERR(rk_fb->ion_client);
3953 dev_info(&pdev->dev, "rk fb ion client create success!\n");
3958 dev_info(&pdev->dev, "rockchip framebuffer driver probe\n");
3962 static int rk_fb_remove(struct platform_device *pdev)
3964 struct rk_fb *rk_fb = platform_get_drvdata(pdev);
3967 platform_set_drvdata(pdev, NULL);
3971 static void rk_fb_shutdown(struct platform_device *pdev)
3973 struct rk_fb *rk_fb = platform_get_drvdata(pdev);
3976 for (i = 0; i < rk_fb->num_lcdc; i++) {
3977 if (!rk_fb->lcdc_dev_drv[i])
3982 static const struct of_device_id rkfb_dt_ids[] = {
3983 {.compatible = "rockchip,rk-fb",},
3987 static struct platform_driver rk_fb_driver = {
3988 .probe = rk_fb_probe,
3989 .remove = rk_fb_remove,
3992 .owner = THIS_MODULE,
3993 .of_match_table = of_match_ptr(rkfb_dt_ids),
3995 .shutdown = rk_fb_shutdown,
3998 static int __init rk_fb_init(void)
4000 return platform_driver_register(&rk_fb_driver);
4003 static void __exit rk_fb_exit(void)
4005 platform_driver_unregister(&rk_fb_driver);
4008 fs_initcall(rk_fb_init);
4009 module_exit(rk_fb_exit);