这次要搞的是一个imx6q的HDMI输出,外接一个HDMI转LVDS转接板(由单片机bypass转图像信号),然后接一个1920x720分辨率的LVDS屏幕,这个分辨率在imx的HDMI驱动里面没有支持,只有我们自己添加了。
IMX6的HDMI分辨率的输出在驱动中有一个表mxc_cea_mode,规定了能输出哪些分辨率:
const struct fb_videomode mxc_cea_mode[64] = {/* #1: 640x480p@59.94/60Hz 4:3 */[1] = {NULL, 60, 640, 480, 39683, 48, 16, 33, 10, 96, 2, 0,FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_4_3, 0,},/* #2: 720x480p@59.94/60Hz 4:3 */[2] = {NULL, 60, 720, 480, 37037, 60, 16, 30, 9, 62, 6, 0,FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_4_3, 0,},/* #3: 720x480p@59.94/60Hz 16:9 */[3] = {NULL, 60, 720, 480, 37037, 60, 16, 30, 9, 62, 6, 0,FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,},/* #4: 1280x720p@59.94/60Hz 16:9 */[4] = {NULL, 60, 1280, 720, 13468, 220, 110, 20, 5, 40, 5,FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0},/* #5: 1920x1080i@59.94/60Hz 16:9 */[5] = {NULL, 60, 1920, 1080, 13763, 148, 88, 15, 2, 44, 5,FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,FB_VMODE_INTERLACED | FB_VMODE_ASPECT_16_9, 0,},........};
就是上面这种,只贴了一部分代码,如果我们需要输出我们自己想要的分辨率,就需要在这个表中添加要适配的屏幕的参数;
接下来我们开始调试,先交代几个关键步骤:
1> 在驱动中增加cea mode用以支持1920x720@60的分辨率,这个修改在drivers/video/fbdev/mxc/mxc_edid.c中
2> 在驱动读到EDID后要重建modelist,将我们加入的新模式添加到链表头,这样设置即可生效,这个修改在drivers/video/fbdev/mxc/mxc_hdmi.c中
3> 解决手填的cea mode时序不正确的问题
有了以上三个步骤我们就可以操作了:
增加分辨率
我们看到mxc_cea_mode结构体数组是下面这样的,和LCD驱动的结构内容一样,我们将注释补全一下:
struct fb_videomode {const char *name; /* optional */u32 refresh; /* optional */ /*刷新率*/u32 xres; /*水平有效像素*/u32 yres; /*垂直有效像素*/u32 pixclock; /*像素时钟*/u32 left_margin; /*HBP horizontal back porch*/u32 right_margin; /*HFP horizontal front porch*/u32 upper_margin; /*VBP vertical back porch*/u32 lower_margin; /*VFP vertical front porch*/u32 hsync_len; /*HSPW horizontal sync pulse width*/u32 vsync_len; /*VSPW vertical sync pulse width*/u32 sync;u32 vmode;u32 flag;
};
我们看到其他的mxc_cea_mode成员的格式,发现只用填写几个关键时序参数,其他的延用就行了,主要有以下几个参数要填写:
refresh; xres; yres; pixclock; left_margin; right_margin; upper_margin; lower_margin; hsync_len; vsync_len
这些参数相信调过屏幕的兄弟都认识,这里不做介绍了,每个屏幕的数据手册中都有这部分的屏参内容,下面以我自己的屏幕为例:
根据我们LVDS屏幕的屏参,我们看一下设备树,那里面有算好的一部分屏参,我们试试拿过来能不能用:
lvds-channel@1 {fsl,data-mapping = "jeida";fsl,data-width = <24>;crtc = "ipu2-di1";status = "okay";display-timings {native-mode = <&timing_disp1>;timing_disp1: timing_disp1 {/*clock-frequency = <89000000>;*/clock-frequency = <88200000>;hactive = <1920>;vactive = <720>;hback-porch = <32>;hfront-porch = <50>;vback-porch = <5>;vfront-porch = <7>;hsync-len = <2>;vsync-len = <1>;};};
然后我们算出像素时钟:
refresh=60; xres=1920; yres=720; HBP=32; HFP=50; HSPW=2; VBP=5; VFP=7; VSPW=1;
pixclock=(1012)/(refresh*(xres+HBP+HFP+HSPW)*(yres+VBP+VFP+VSPW))=(1012)/(60*(1920+32+50+2)*(720+5+7+1))=11346
算出来之后我们就可以填充cea mode这个数组了:
[48] = { NULL, 60, 1920, 720, 11346, 32, 50, 5, 7, 2, 1, FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,},
这是根据自己的判断计算出的参数值,这里先交代一下,这里没有成功,但这篇文章的目的是记录自己的调试过程。
修改驱动
在填好这些时序之后,还需要对驱动做一点修改,以让我们的屏参能够生效,下面是我的patch:
diff --git a/drivers/video/fbdev/mxc/mxc_hdmi.c b/drivers/video/fbdev/mxc/mxc_hdmi.c
index 9e844805c..acd635a2b 100644
--- a/drivers/video/fbdev/mxc/mxc_hdmi.c
+++ b/drivers/video/fbdev/mxc/mxc_hdmi.c
@@ -64,7 +64,8 @@#define HDMI_EDID_NO_MODES -3#define NUM_CEA_VIDEO_MODES 64
-#define DEFAULT_VIDEO_MODE 16 /* 1080P */
+//#define DEFAULT_VIDEO_MODE 16 /* 1080P */
+#define DEFAULT_VIDEO_MODE 48 /* 1920x720P */#define RGB 0#define YCBCR444 1
@@ -1791,8 +1792,9 @@ static void mxc_hdmi_edid_rebuild_modelist(struct mxc_hdmi *hdmi)console_lock();fb_destroy_modelist(&hdmi->fbi->modelist);
- fb_add_videomode(&vga_mode, &hdmi->fbi->modelist);+ //fb_add_videomode(&vga_mode, &hdmi->fbi->modelist);
+ fb_add_videomode(mxc_cea_mode+DEFAULT_VIDEO_MODE, &hdmi->fbi->modelist);// add sunsufor (i = 0; i < hdmi->fbi->monspecs.modedb_len; i++) {/** We might check here if mode is supported by HDMI.
@@ -2582,7 +2584,7 @@ static int mxc_hdmi_disp_init(struct mxc_dispdrv_handle *disp,spin_lock_init(&hdmi->irq_lock);/* Set the default mode and modelist when disp init. */
- fb_find_mode(&hdmi->fbi->var, hdmi->fbi,
+ ret = fb_find_mode(&hdmi->fbi->var, hdmi->fbi,hdmi->dft_mode_str, NULL, 0, NULL,hdmi->default_bpp);@@ -2604,6 +2606,7 @@ static int mxc_hdmi_disp_init(struct mxc_dispdrv_handle *disp,hdmi->dft_mode_set = false;mode = fb_find_nearest_mode(&m, &hdmi->fbi->modelist);
+ dump_fb_videomode(&m);// &m is from cmdlineif (!mode) {pr_err("%s: could not find mode in modelist\n", __func__);return -1;
--
2.17.1
修改到这里,发现屏幕没有点亮。根据自己添加的一些打印,知道增加的时序是添加进去了生效了的,只是时序不对。
调试屏参
我们知道在nxp的代码中,hdmi的分辨率是由uboot的cmdline传到kernel中用的。然后在函数mxc_hdmi_disp_init中发现,下面的代码片段会将cmdline中传入的HDMI分辨率参数到这里来处理:
/* HDMI Initialization Step A */
static int mxc_hdmi_disp_init(struct mxc_dispdrv_handle *disp,struct mxc_dispdrv_setting *setting)
{......../* Set the default mode and modelist when disp init. */ret = fb_find_mode(&hdmi->fbi->var, hdmi->fbi,hdmi->dft_mode_str, NULL, 0, NULL,hdmi->default_bpp); //dft_mode_str is cmdline parameter;console_lock();fb_destroy_modelist(&hdmi->fbi->modelist);/*Add all no interlaced CEA mode to default modelist */for (i = 0; i < ARRAY_SIZE(mxc_cea_mode); i++) {mode = &mxc_cea_mode[i];if (!(mode->vmode & FB_VMODE_INTERLACED) && (mode->xres != 0))fb_add_videomode(mode, &hdmi->fbi->modelist);}console_unlock();/* Find a nearest mode in default modelist */fb_var_to_videomode(&m, &hdmi->fbi->var);hdmi->dft_mode_set = false;mode = fb_find_nearest_mode(&m, &hdmi->fbi->modelist);dump_fb_videomode(&m);// add: &m is from cmdlineif (!mode) {pr_err("%s: could not find mode in modelist\n", __func__);return -1;}
代码片段中 hdmi->fbi->var将转化为&m, 可以将m结构体中的值打印出来得到计算出的上面我们需要的参数;
打印出来是这样的:
refresh;xres;yres;pixclock;left_margin;right_margin;upper_margin;lower_margin;hsync_len;vsync_len = 60, 1920, 720, 8948, 288, 96, 15, 3, 192, 10
相当不错,代码自己给我算出来了,直接使用看看,然后我们添加的cea mode变成了这样:
[48] = {/*name;refresh;xres;yres;pixclock;left_margin;right_margin;upper_margin;lower_margin;hsync_len;vsync_len;sync;vmode;flag;*//* NULL, 60, 1920, 720, 11346, 32, 50, 5, 7, 2, 1, */NULL, 60, 1920, 720, 8948, 288, 96, 15, 3, 192, 10,FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,},
这下好了,可以显示了,但正常显示大概5s后,屏幕右半部分就出现闪烁的横杠条纹,随后直接黑屏。
看现象像是时钟问题,然后我将8948这个pixclock换成前面计算出的11346,终于,终于可以正常显示了。不容易阿。
备注
至于驱动中是如何计算出关键时序参数的,下方是计算代码:
/** fb_find_mode_cvt - calculate mode using VESA(TM) CVT* @mode: pointer to fb_videomode; xres, yres, refresh and vmode must be* pre-filled with the desired values* @margins: add margin to calculation (1.8% of xres and yres)* @rb: compute with reduced blanking (for flatpanels)** RETURNS:* 0 for success* @mode is filled with computed values. If interlaced, the refresh field* will be filled with the field rate (2x the frame rate)** DESCRIPTION:* Computes video timings using VESA(TM) Coordinated Video Timings*/
int fb_find_mode_cvt(struct fb_videomode *mode, int margins, int rb)
{struct fb_cvt_data cvt;memset(&cvt, 0, sizeof(cvt));if (margins)cvt.flags |= FB_CVT_FLAG_MARGINS;if (rb)cvt.flags |= FB_CVT_FLAG_REDUCED_BLANK;if (mode->vmode & FB_VMODE_INTERLACED)cvt.flags |= FB_CVT_FLAG_INTERLACED;cvt.xres = mode->xres;cvt.yres = mode->yres;cvt.refresh = mode->refresh;cvt.f_refresh = cvt.refresh;cvt.interlace = 1;if (!cvt.xres || !cvt.yres || !cvt.refresh) {printk(KERN_INFO "fbcvt: Invalid input parameters\n");return 1;}if (!(cvt.refresh == 50 || cvt.refresh == 60 || cvt.refresh == 70 ||cvt.refresh == 85)) {printk(KERN_INFO "fbcvt: Refresh rate not CVT ""standard\n");cvt.status = 1;}cvt.xres &= ~(FB_CVT_CELLSIZE - 1);if (cvt.flags & FB_CVT_FLAG_INTERLACED) {cvt.interlace = 2;cvt.f_refresh *= 2;}if (cvt.flags & FB_CVT_FLAG_REDUCED_BLANK) {if (cvt.refresh != 60) {printk(KERN_INFO "fbcvt: 60Hz refresh rate ""advised for reduced blanking\n");cvt.status = 1;}}if (cvt.flags & FB_CVT_FLAG_MARGINS) {cvt.h_margin = (cvt.xres * 18)/1000;cvt.h_margin &= ~(FB_CVT_CELLSIZE - 1);cvt.v_margin = ((cvt.yres/cvt.interlace)* 18)/1000;}cvt.aspect_ratio = fb_cvt_aspect_ratio(&cvt);cvt.active_pixels = cvt.xres + 2 * cvt.h_margin;cvt.hperiod = fb_cvt_hperiod(&cvt);cvt.vsync = fb_cvt_vbi_tab[cvt.aspect_ratio];cvt.vtotal = fb_cvt_vtotal(&cvt);cvt.hblank = fb_cvt_hblank(&cvt);cvt.htotal = cvt.active_pixels + cvt.hblank;cvt.hsync = fb_cvt_hsync(&cvt);cvt.pixclock = fb_cvt_pixclock(&cvt);cvt.hfreq = cvt.pixclock/cvt.htotal;cvt.h_back_porch = cvt.hblank/2 + cvt.h_margin;cvt.h_front_porch = cvt.hblank - cvt.hsync - cvt.h_back_porch +2 * cvt.h_margin;cvt.v_front_porch = 3 + cvt.v_margin;cvt.v_back_porch = cvt.vtotal - cvt.yres/cvt.interlace -cvt.v_front_porch - cvt.vsync;fb_cvt_print_name(&cvt);fb_cvt_convert_to_mode(&cvt, mode);return 0;
}
日后再分析
祝大家明日无BUG