LCD驱动程序——基础知识

1、简介
lcd是liquid crystal display的简称,液晶显示器按驱动方式分为静态驱动、简单矩阵驱动及主动矩阵驱动。其中简单矩阵驱动又可细分为扭转向列型(TN)和超转向列型(STN)两种,而主动矩阵驱动则以TFT为主。下面具体来说一下:
TN型驱动液晶,是LCD中最基本的,其它LCD都以TN型改进而得。它只能将入射光旋转90度,视角只有30度,色彩单一,对比度低,用于电子表和电子计算器。
STN型驱动液晶,可将入射光旋转180度至270度,也改善了视角。它通过搭配色彩滤光片,将单色矩阵的任意像素分成3个子像素成红绿蓝,再经由三色按比例调和,显示出逼近全彩模式的色彩。
TN和STN都场电压驱动方式,如果显示尺寸加大,中心部分对电极变化的反应时间就会变长,显示器的速度跟不上, 为了解决这个问题,主动矩阵驱动TFT被提出。它通过晶体管显示信号开启或关闭液晶分子的电压,从而避免了显示器对电场效应的依靠。
2、显示原理
一块lcd屏显示图像不但需要lcd驱动器,还需要lcd控制器。很多芯片cpu集成了lcd控制器。所有lcd显示图像的原理都是 从上到下,从左到右的。一副图像可以看作是一个矩形,由许多排列整齐的点一行一行组成,这些店成为像素。依据显示原理: 作为帧同步信号的vsync,每发出一个脉冲,都意味着新的一副图像数据开始发送。而作为行同步信号的hsync,每发出一个脉冲,都表明新的一行图像资料开始发送。我们可以看一下时序图:
我们来解释一下各信号的含义:
VSYNC:帧同步信号
每发出一个脉冲,表示新的一屏图像数据开始传送。
HSYNC:行同步信号
每发出一个脉冲,表示新的一行图像数据开始传送。
VCLK:像素时钟信号
每发出一个脉冲,表示新的一个点图像数据开始传送。
LEND:行结束信号
VBPD:表示在一帧图像开始时,帧同步信号以后的无效的行数,对应驱动中的upper_margin;
VFBD:表示在一帧图像结束后,帧同步信号以前的无效的行数,对应驱动中的lower_margin;
VSPW:表示垂直同步脉冲的宽度,用行数计算,对应驱动中的nvync_len;
HBPD:表示从水平同步信号开始到一行的有效数据开始之间的vclk的个数,对应驱动中的left_margin;
HFPD:表示一行的有效数据结束到下一个水平同步信号开始之间的vclk的个数,对应驱动中的right_margin;
HSPW:表示水平同步信号的宽度,用vclk计算,对应驱动中的hsync_nc;
3、帧缓冲
(1)这是一个十分重要的概念,framebuffer从本质来说是图形设备的硬件抽象。对开发者而言,framebuffer是一块显示缓存,往显示缓存里写入特定格式的数据就意味着向屏幕输出内容。通过不断的向framebuffer中写入数据,显示控制器就 自动的从framebuffer中取数据并显示出来。
(2)帧缓冲设备对应的设备文件为/dev/fb*,如果系统有多个显示卡,linux下还可以支持多个帧缓冲设备,最多可达32个,分别为/dev/fb0到/dev/fb31,而/dev/fb则为当前缺省的帧缓冲设备,通常指向/dev/fb0。帧缓冲设备为标准的字符型设备,主设备号为29,次设备号为0到31。
我们可以通过一下命令来演示一下:
清除lcd:dd if=/dev/zero of=/dev/fb0 bs=240 count=320
运行应用程序,画图:./LCD
清除lcd:dd if=/dev/zero of=/dev/fb0 bs=240 count=320
显示图片:cat 7.bmp>/dev/fb0
4、系统构架
我们要完成的工作只有两个,一是配置这个fb_info结构体,二是注册这个结构体
 
5、一些结构体与函数
(1)设备描述
linux内核用struct fb_info来描述帧缓冲设备
struct fb_info {
int node;
int flags;
struct mutex lock; /* Lock for open/release/ioctl funcs */
struct fb_var_screeninfo var; /* Current var见注释1*/
struct fb_fix_screeninfo fix; /* Current fix固定参数 */
struct fb_monspecs monspecs; /* Current Monitor specs */
struct work_struct queue; /* Framebuffer event queue */
struct fb_pixmap pixmap; /* Image hardware mapper */
struct fb_pixmap sprite; /* Cursor hardware mapper */
struct fb_cmap cmap; /* Current cmap */
struct list_head modelist;      /* mode list */
struct fb_videomode *mode; /* current mode */

#ifdef CONFIG_FB_BACKLIGHT
/* assigned backlight device */
/* set before framebuffer registration, 
   remove after unregister */
struct backlight_device *bl_dev;

/* Backlight level curve */
struct mutex bl_curve_mutex;
u8 bl_curve[FB_BACKLIGHT_LEVELS];
#endif
#ifdef CONFIG_FB_DEFERRED_IO
struct delayed_work deferred_work;
struct fb_deferred_io *fbdefio;
#endif

struct fb_ops *fbops;//帧缓冲操作函数
struct device *device; /* This is the parent */
struct device *dev; /* This is this fb device */
int class_flag;                    /* private sysfs flags */
#ifdef CONFIG_FB_TILEBLITTING
struct fb_tile_ops *tileops;    /* Tile Blitting */
#endif
char __iomem *screen_base; /* Virtual address */
unsigned long screen_size; /* Amount of ioremapped VRAM or 0 */ 
void *pseudo_palette; /* Fake palette of 16 colors */ 
#define FBINFO_STATE_RUNNING 0
#define FBINFO_STATE_SUSPENDED 1
u32 state; /* Hardware state i.e suspend */
void *fbcon_par;                /* fbcon use-only private area */
/* From here on everything is device dependent */
void *par;
};
注释1:struct  fb_var_screeninfo记录用户可修改的显示控制器参数,包括屏幕的分辨率和每个像素点的比特数,具体内容如下:
struct fb_var_screeninfo {
__u32 xres; /* visible resolution */
__u32 yres;
__u32 xres_virtual; /* virtual resolution */
__u32 yres_virtual;
__u32 xoffset; /* offset from virtual to visible */
__u32 yoffset; /* resolution */

__u32 bits_per_pixel; /* guess what */
__u32 grayscale; /* != 0 Graylevels instead of colors */

struct fb_bitfield red; /* bitfield in fb mem if true color, */
struct fb_bitfield green; /* else only length is significant */
struct fb_bitfield blue;
struct fb_bitfield transp; /* transparency */

__u32 nonstd; /* != 0 Non standard pixel format */

__u32 activate; /* see FB_ACTIVATE_* */

__u32 height; /* height of picture in mm    */
__u32 width; /* width of picture in mm     */

__u32 accel_flags; /* (OBSOLETE) see fb_info.flags */

/* Timing: All values in pixclocks, except pixclock (of course) */
__u32 pixclock; /* pixel clock in ps (pico seconds) */
__u32 left_margin; /* time from sync to picture */
__u32 right_margin; /* time from picture to sync */
__u32 upper_margin; /* time from sync to picture */
__u32 lower_margin;
__u32 hsync_len; /* length of horizontal sync */
__u32 vsync_len; /* length of vertical sync */
__u32 sync; /* see FB_SYNC_* */
__u32 vmode; /* see FB_VMODE_* */
__u32 rotate; /* angle we rotate counter clockwise */
__u32 reserved[5]; /* Reserved for future compatibility */
}
注释2: struct fb_fix_screeninfo fix,记录了用户不能修改的显示控制器参数,如显示缓存的物理地址, 具体代码如下:
struct fb_fix_screeninfo {
char id[16]; /* identification string eg "TT Builtin" */
unsigned long smem_start; /* Start of frame buffer mem */
/* (physical address) */
__u32 smem_len; /* Length of frame buffer mem */
__u32 type; /* see FB_TYPE_* */
__u32 type_aux; /* Interleave for interleaved Planes */
__u32 visual; /* see FB_VISUAL_* */ 
__u16 xpanstep; /* zero if no hardware panning  */
__u16 ypanstep; /* zero if no hardware panning  */
__u16 ywrapstep; /* zero if no hardware ywrap    */
__u32 line_length; /* length of a line in bytes    */
unsigned long mmio_start; /* Start of Memory Mapped I/O   */
/* (physical address) */
__u32 mmio_len; /* Length of Memory Mapped I/O  */
__u32 accel; /* Indicate to driver which */
/*  specific chip/card we have */
__u16 reserved[3]; /* Reserved for future compatibility */
};
注释3:这是一个类似于file_operations的可实现文件设备操作的数据结构
struct fb_ops {
/* open/release and usage marking */
struct module *owner;
int (*fb_open)(struct fb_info *info, int user);
int (*fb_release)(struct fb_info *info, int user);

/* For framebuffers with strange non linear layouts or that do not
 * work with normal memory mapped access
 */
ssize_t (*fb_read)(struct fb_info *info, char __user *buf,
   size_t count, loff_t *ppos);
ssize_t (*fb_write)(struct fb_info *info, const char __user *buf,
    size_t count, loff_t *ppos);

/* checks var and eventually tweaks it to something supported,
 * DO NOT MODIFY PAR */
int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);

/* set the video mode according to info->var */
int (*fb_set_par)(struct fb_info *info);

/* set color register */
int (*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,
    unsigned blue, unsigned transp, struct fb_info *info);

/* set color registers in batch */
int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);

/* blank display */
int (*fb_blank)(int blank, struct fb_info *info);

/* pan display */
int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);

/* Draws a rectangle */
void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);
/* Copy data from area to another */
void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);
/* Draws a image to the display */
void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);

/* Draws cursor */
int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);

/* Rotates the display */
void (*fb_rotate)(struct fb_info *info, int angle);

/* wait for blit idle, optional */
int (*fb_sync)(struct fb_info *info);

/* perform fb specific ioctl (optional) */
int (*fb_ioctl)(struct fb_info *info, unsigned int cmd,
unsigned long arg);

/* Handle 32bit compat ioctl (optional) */
int (*fb_compat_ioctl)(struct fb_info *info, unsigned cmd,
unsigned long arg);

/* perform fb specific mmap */
int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);

/* save current hardware state */
void (*fb_save_state)(struct fb_info *info);

/* restore saved state */
void (*fb_restore_state)(struct fb_info *info);

/* get capability given var */
void (*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,
    struct fb_var_screeninfo *var);
};
(2)fb_bitfield结构体:描述每一像素显示缓冲区的组织方式,包含位域偏移、位域长度和MSB指示:
struct fb_bitfield {
__u32 offset; /* beginning of bitfield */
__u32 length; /* length of bitfield */
__u32 msb_right; /* != 0 : Most significant bit is */ 
/* right */ 
};
(3)注册函数:register_framebuffer(struct fb_info *fb_info)
         注销函数:un register_framebuffer(struct fb_info *fb_info)

你可能感兴趣的:(linux—driver)