1. 程式人生 > >基於JZ2440自己編寫4.3寸LCD驅動

基於JZ2440自己編寫4.3寸LCD驅動

簡介

linux核心自帶LCD驅動,這裡我們自己從頭寫一個LCD驅動程式,編寫APP的人只知道使用open、read、write······,並不清楚原理圖或者暫存器,這介面函式最終呼叫的是我們自己在驅動中實現的驅動層open、read、write,這些程式碼與硬體息息相關。一般寫驅動基本步驟就是:

  • 定義主裝置號
  • 寫一個檔案操作集合
  • register_chidev進行設備註冊(建立裝置類、類下建立裝置)
  • 修飾入口出口函式
    其實別人寫驅動也不外乎這幾個步驟,但是對於我們寫LCD驅動程式還是毫無頭緒,外事開頭難,我們可以參考核心螢幕驅動的寫法,參考檔案為fbmem.c

分析fbmem.c(linux2.6版本)

入口函式

fbmem_init(void)
{
    proc_create("fb", 0, NULL, &fb_proc_fops);
    //字元設備註冊  已經實現檔案操作結合
    if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
        printk("unable to get major %d for fb devs\n", FB_MAJOR);
    //建立裝置類 在這個函式中沒有建立裝置,估計像其它子系統驅動一樣是分層來寫的
    fb_class
= class_create(THIS_MODULE, "graphics");
if (IS_ERR(fb_class)) { printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class)); fb_class = NULL; } return 0; }

在初始化函式中,我們可以看到該檔案實現了檔案操作集合,我們看一下他實現了那些功能:

static const struct file_operations fb_fops = {
    .owner
= THIS_MODULE, .read = fb_read, .write = fb_write, .unlocked_ioctl = fb_ioctl, #ifdef CONFIG_COMPAT .compat_ioctl = fb_compat_ioctl, #endif .mmap = fb_mmap, .open = fb_open, .release = fb_release, #ifdef HAVE_ARCH_FB_UNMAPPED_AREA .get_unmapped_area = get_fb_unmapped_area, #endif #ifdef CONFIG_FB_DEFERRED_IO .fsync = fb_deferred_io_fsync, #endif .llseek = default_llseek, };

我們可以看到該檔案實現了一些檔案操作,那麼這些函式可不可以直接拿過來就能使用呢?

在APP中呼叫open函式
kernel中呼叫proc_fb_open,我們看下這個函式做了些什麼:

fb_open(struct inode *inode, struct file *file)
__acquires(&info->lock)
__releases(&info->lock)
{
    int fbidx = iminor(inode);  //取出裝置次裝置號
    struct fb_info *info;       //定義了一個fb_info結構
    int res = 0;

    info = get_fb_info(fbidx); //在次裝置裡面得到fb_info結構資訊賦值給info
    //下面是一系列錯誤判斷
    if (!info) {
        request_module("fb%d", fbidx);
        info = get_fb_info(fbidx);
        if (!info)
            return -ENODEV;
    }
    if (IS_ERR(info))
        return PTR_ERR(info);

    mutex_lock(&info->lock);
    if (!try_module_get(info->fbops->owner)) {
        res = -ENODEV;
        goto out;
    }
    file->private_data = info;
    //如果這個裝置info裡面有open函式,就執行open
    if (info->fbops->fb_open) {
        res = info->fbops->fb_open(info,1);
        if (res)
            module_put(info->fbops->owner);
    }
#ifdef CONFIG_FB_DEFERRED_IO
    if (info->fbdefio)
        fb_deferred_io_open(info, inode, file);
#endif
out:
    mutex_unlock(&info->lock);
    if (res)
        put_fb_info(info);
    return res;
}

我們從原始碼中可以看出,由於fb裝置(幀緩衝裝置)主裝置號固定,不同裝置以次裝置號進行區分,執行該裝置對應open函式時,通過取出該裝置對應的fb_info結構裡面是否實現open函式,如果有就開啟,沒有就開啟失敗,類似輸入子系統或者平臺裝置那樣要實現相應結構填充才行,所以該open函式最終指向的是對應裝置的open函式,該open函式不能直接進行open操作。

open函式是這樣,那麼其餘函式究竟是不是這樣呢?下面再看看read函式:

fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
    unsigned long p = *ppos; 
    struct fb_info *info = file_fb_info(file);  //從開啟的file檔案中取出fb_info結構賦值給info
    u8 *buffer, *dst;
    u8 __iomem *src;
    int c, cnt = 0, err = 0;
    unsigned long total_size;
    //下面是一系列錯誤判斷
    if (!info || ! info->screen_base)
        return -ENODEV;

    if (info->state != FBINFO_STATE_RUNNING)
        return -EPERM;
   /* 判斷該info結構中是否存在read函式,如果有就執行 */
    if (info->fbops->fb_read)
        return info->fbops->fb_read(info, buf, count, ppos);

    total_size = info->screen_size;

    if (total_size == 0)
        total_size = info->fix.smem_len;

    if (p >= total_size)
        return 0;

    if (count >= total_size)
        count = total_size;

    if (count + p > total_size)
        count = total_size - p;

    buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
             GFP_KERNEL);
    if (!buffer)
        return -ENOMEM;

    src = (u8 __iomem *) (info->screen_base + p);

    if (info->fbops->fb_sync)
        info->fbops->fb_sync(info);

    while (count) {
        c  = (count > PAGE_SIZE) ? PAGE_SIZE : count;
        dst = buffer;
        fb_memcpy_fromfb(dst, src, c);
        dst += c;
        src += c;

        if (copy_to_user(buf, buffer, c)) {
            err = -EFAULT;
            break;
        }
        *ppos += c;
        buf += c;
        cnt += c;
        count -= c;
    }

    kfree(buffer);

    return (err) ? err : cnt;
}

我們看到read函式中同樣不是直接可以讀取裝置,最終執行read操作的是開啟裝置的open函式,這個open函式只是指向具體裝置的open。

從兩個函式基本可以分析出來,該檔案提供的檔案操作集合,並不能直接操作裝置,執行它們時他們將具體的檔案操作指向具體裝置對應的操作函式。本身沒有直接操作底層的能力。完全屬於軟體層,在具體的驅動層面之上。如果我們要實現LCD驅動,只需要驅動層滿足他們的條件即可,就可以掛接在核心LCD驅動框架下,作為標準裝置。
在該檔案的操作結合裡面,每個函式都從fb_info結構中獲得想要的資訊,fb_info從哪來的呢?從open函式中可以看到info = registered_fb[fbidx]info結構來自registered_fb這個陣列中的fbidx位置,fbidx向上可以看到這個值是開啟檔案的次裝置號
那麼registered_fb這個陣列在哪裡進行操作使用了呢?查詢一下發現在下面的函式中進行了呼叫register_framebuffer

register_framebuffer(struct fb_info *fb_info)
{
    int i;
    struct fb_event event;
    struct fb_videomode mode;

    if (num_registered_fb == FB_MAX)
        return -ENXIO;
    num_registered_fb++;
    for (i = 0 ; i < FB_MAX; i++)
        if (!registered_fb[i])
            break;
    fb_info->node = i;

    fb_info->dev = device_create(fb_class, fb_info->device,
                     MKDEV(FB_MAJOR, i), "fb%d", i);
    if (IS_ERR(fb_info->dev)) {
        /* Not fatal */
        printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
        fb_info->dev = NULL;
    } else
        fb_init_device(fb_info);

    if (fb_info->pixmap.addr == NULL) {
        fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
        if (fb_info->pixmap.addr) {
            fb_info->pixmap.size = FBPIXMAPSIZE;
            fb_info->pixmap.buf_align = 1;
            fb_info->pixmap.scan_align = 1;
            fb_info->pixmap.access_align = 32;
            fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
        }
    }   
    fb_info->pixmap.offset = 0;

    if (!fb_info->pixmap.blit_x)
        fb_info->pixmap.blit_x = ~(u32)0;

    if (!fb_info->pixmap.blit_y)
        fb_info->pixmap.blit_y = ~(u32)0;

    if (!fb_info->modelist.prev || !fb_info->modelist.next)
        INIT_LIST_HEAD(&fb_info->modelist);

    fb_var_to_videomode(&mode, &fb_info->var);
    fb_add_videomode(&mode, &fb_info->modelist);
    registered_fb[i] = fb_info;

    event.info = fb_info;
    fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
    return 0;
}

fbmem.c基本框架就出來了

雖然在fbmem.c檔案中,實現了主裝置號、裝置類的註冊,也實現了相應的fops,但是經過仔細閱讀後發現,該檔案並沒有實質性的建立具體裝置,檔案操作集合中的函式只是連結到具體裝置具體操作的介面,從而可以認定這個檔案只是響輸入子系統input.c那個級別的檔案一樣不涉及硬體操作,屬於在硬體驅動之上的核心層,那麼他是怎樣實現與LCD硬體層的連線呢?通過register_framebuffer(struct fb_info *fb_info)這個函式,這個函式主要是將具體LCD對應的fb_info進行註冊。一個具體的裝置對應具體的一個fb_info結構,裡面包含了該LCD非常詳細的硬體資訊。

檢視register_framebuffer都被哪些檔案使用,找出一個具體分析

查詢結果顯示使用這個函式的都是各種各樣的LCD,我們找出與我們平臺相近的檔案 s3c2410fb.c ,看看具體是怎樣使用和實現的。
在初始化程式碼中可以看到,這個驅動是基於平臺裝置模型書寫的

static struct platform_driver s3c2410fb_driver = {
    .probe      = s3c2410fb_probe,
    .remove     = s3c2410fb_remove,
    .suspend    = s3c2410fb_suspend,
    .resume     = s3c2410fb_resume,
    .driver     = {
        .name   = "s3c2410-lcd",
        .owner  = THIS_MODULE,
    },
};
int __devinit s3c2410fb_init(void)
{
    return platform_driver_register(&s3c2410fb_driver);
}
module_init(s3c2410fb_init);

臺裝置模型,主要看的是probe函式,這個函式是在裝置與驅動匹配一致後,進行設備註冊以及一些硬體操作。實現步驟無非就是以下幾點:

  • 分配fb_info,framebuffer_alloc
  • 設定初始化
  • 註冊 register_framebuffer
  • 硬體相關的操作
static int __init s3c2410fb_probe(struct platform_device *pdev)
{
    ..........

    ret = register_framebuffer(fbinfo);
    ..........
    return ret;
}

如果想要檢視LCD解析度等資訊,怎麼操作呢?
在fbmem.c檔案的操作函式中有fb_ioctrl函式,可以看到,這個函式就是通過此裝置號,找到對應裝置的fb_info結構,通過解析各種命令,返回這個結構的相關資訊。

fb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
     unsigned long arg)
{
    int fbidx = iminor(inode);
    struct fb_info *info = registered_fb[fbidx];
    .......
    if (!fb)
        return -ENODEV;
    switch (cmd) {
    case FBIOGET_VSCREENINFO:
        return copy_to_user(argp, &info->var,
                    sizeof(var)) ? -EFAULT : 0;
    case FBIOPUT_VSCREENINFO:
        if (copy_from_user(&var, argp, sizeof(var)))
            return -EFAULT;
        acquire_console_sem();
        info->flags |= FBINFO_MISC_USEREVENT;
        i = fb_set_var(info, &var);
        info->flags &= ~FBINFO_MISC_USEREVENT;
        release_console_sem();
        if (i) return i;
        if (copy_to_user(argp, &var, sizeof(var)))
            return -EFAULT;
        return 0;
    case FBIOGET_FSCREENINFO:
    .........
        return 0;
    case FBIO_CURSOR:

fb_info結構裡面儲存著關於該LCD的一切資訊

硬體操作

  • 根據LCD手冊設定2440LCD控制器,例如時鐘頻率等等
  • 分配視訊記憶體,把地址告訴LCD控制器
  • 配置相關引腳用於 LCD

fb_info結構

struct fb_info {
    int node;
    int flags;
    struct fb_var_screeninfo var;   /* 可變引數 */
    struct fb_fix_screeninfo 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;  
};
//fix固定資訊結構體
struct fb_fix_screeninfo {
    char id[16];            /* 識別符號 */
    unsigned long smem_start;   /* 視訊記憶體起始地址 */
                    /* (實體地址) */
    __u32 smem_len;         /* 視訊記憶體長度 */
    __u32 type;         /* 裝置型別
#define FB_TYPE_PACKED_PIXELS       0   /* Packed Pixels    */
#define FB_TYPE_PLANES          1   /* Non interleaved planes */
#define FB_TYPE_INTERLEAVED_PLANES  2   /* Interleaved planes   */
#define FB_TYPE_TEXT            3   /* Text/attributes  */
#define FB_TYPE_VGA_PLANES      4   /* EGA/VGA planes   */
    */
    __u32 type_aux;         /* 附加的東西用於平板類  普通不用管 */
    __u32 visual;           /* see FB_VISUAL_*      */ 
    /*
#define FB_VISUAL_MONO01        0   /* 單色屏Monochr. 1=Black 0=White */
#define FB_VISUAL_MONO10        1   /* 單色屏Monochr. 1=White 0=Black */
#define FB_VISUAL_TRUECOLOR     2   /* 真彩色True color    */
#define FB_VISUAL_PSEUDOCOLOR       3   /* Pseudo color (like atari) */
#define FB_VISUAL_DIRECTCOLOR       4   /* Direct color */
#define FB_VISUAL_STATIC_PSEUDOCOLOR    5   /* Pseudo color readonly */
    */
    __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 LCD IO引腳起始實體地址  */
    __u32 mmio_len;         /* Length of Memory Mapped I/O IO對映長度 */
    /*上面兩項如果APP不用訪問引腳狀態可以不用設定*/
    __u32 accel;            /* Indicate to driver which */
                    /*  specific chip/card we have  */
    __u16 reserved[3];      /* Reserved for future compatibility */
    /*以上兩項不用管*/
};
//var可變資訊結構體
struct fb_var_screeninfo {
    __u32 xres;         /* visible resolution   XY方向解析度 */
    __u32 yres;
    __u32 xres_virtual;     /* virtual resolution   XY方向虛擬解析度 */
    __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;     /* 紅、綠、藍、透明顏色結構 */
    struct fb_bitfield green;   /* else only length is significant */
    struct fb_bitfield blue;
    struct fb_bitfield transp;  /* transparency         */  

    struct fb_bitfield {    //紅綠藍顏色結構展開
    __u32 offset;           /* 位元組中該顏色偏移量    */
    __u32 length;           /* 所佔位寬     */
    __u32 msb_right;        /* != 0 : Most significant bit is */ 
                    /* right 控制顏色資料左右對齊*/ 
};

    __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 過時的標誌位不管 */

    /* 下面是LCD相關控制引數,時鐘頻率,黑邊框長度,時序訊號等等,這些引數不會設定到硬體,只供軟體查詢使用可以不填 */
    __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 */
};

自己寫的4.3寸LCD原始碼

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <linux/wait.h>
#include <linux/platform_device.h>
#include <linux/clk.h>

#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/div64.h>

#include <asm/mach/map.h>
#include <asm/arch/regs-lcd.h>
#include <asm/arch/regs-gpio.h>
#include <asm/arch/fb.h>

static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
                 unsigned int green, unsigned int blue,
                 unsigned int transp, struct fb_info *info);


struct lcd_regs {
    unsigned long   lcdcon1;
    unsigned long   lcdcon2;
    unsigned long   lcdcon3;
    unsigned long   lcdcon4;
    unsigned long   lcdcon5;
    unsigned long   lcdsaddr1;
    unsigned long   lcdsaddr2;
    unsigned long   lcdsaddr3;
    unsigned long   redlut;
    unsigned long   greenlut;
    unsigned long   bluelut;
    unsigned long   reserved[9];
    unsigned long   dithmode;
    unsigned long   tpal;
    unsigned long   lcdintpnd;
    unsigned long   lcdsrcpnd;
    unsigned long   lcdintmsk;
    unsigned long   lpcsel;
};

static struct fb_ops s3c_lcdfb_ops = {
    .owner      = THIS_MODULE,
    .fb_setcolreg   = s3c_lcdfb_setcolreg,
    .fb_fillrect    = cfb_fillrect,
    .fb_copyarea    = cfb_copyarea,
    .fb_imageblit   = cfb_imageblit,
};


static struct fb_info *s3c_lcd;
static volatile unsigned long *gpbcon;
static volatile unsigned long *gpbdat;
static volatile unsigned long *gpccon;
static volatile unsigned long *gpdcon;
static volatile unsigned long *gpgcon;
static volatile struct lcd_regs* lcd_regs;
static u32 pseudo_palette[16];


/* from pxafb.c */
static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
{
    chan &= 0xffff;
    chan >>= 16 - bf->length;
    return chan << bf->offset;
}


static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
                 unsigned int green, unsigned int blue,
                 unsigned int transp, struct fb_info *info)
{
    unsigned int val;

    if (regno > 16)
        return 1;

    /* 用red,green,blue三原色構造出val */
    val  = chan_to_field(red,   &info->var.red);
    val |= chan_to_field(green, &info->var.green);
    val |= chan_to_field(blue,  &info->var.blue);

    //((u32 *)(info->pseudo_palette))[regno] = val;
    pseudo_palette[regno] = val;
    return 0;
}

static int lcd_init(void)
{
    /* 1. 分配一個fb_info */
    s3c_lcd = framebuffer_alloc(0, NULL);

    /* 2. 設定 */
    /* 2.1 設定固定的引數 */
    strcpy(s3c_lcd->fix.id, "mylcd");
    s3c_lcd->fix.smem_len = 480*272*16/8;
    s3c_lcd->fix.type     = FB_TYPE_PACKED_PIXELS;
    s3c_lcd->fix.visual   = FB_VISUAL_TRUECOLOR; /* TFT */
    s3c_lcd->fix.line_length = 480*2;

    /* 2.2 設定可變的引數 */
    s3c_lcd->var.xres           = 480;
    s3c_lcd->var.yres           = 272;
    s3c_lcd->var.xres_virtual   = 480;
    s3c_lcd->var.yres_virtual   = 272;
    s3c_lcd->var.bits_per_pixel = 16;

    /* RGB:565 */
    s3c_lcd->var.red.offset     = 11;
    s3c_lcd->var.red.length     = 5;

    s3c_lcd->var.green.offset   = 5;
    s3c_lcd->var.green.length   = 6;

    s3c_lcd->var.blue.offset    = 0;
    s3c_lcd->var.blue.length    = 5;

    s3c_lcd->var.activate       = FB_ACTIVATE_NOW;


    /* 2.3 設定操作函式 */
    s3c_lcd->fbops              = &s3c_lcdfb_ops;

    /* 2.4 其他的設定 */
    s3c_lcd->pseudo_palette = pseudo_palette;
    //s3c_lcd->screen_base  = ;  /* 視訊記憶體的虛擬地址 */ 
    s3c_lcd->screen_size   = 480*272*16/8;

    /* 3. 硬體相關的操作 */
    /* 3.1 配置GPIO用於LCD */
    gpbcon = ioremap(0x56000010, 8);
    gpbdat = gpbcon+1;
    gpccon = ioremap(0x56000020, 4);
    gpdcon = ioremap(0x56000030, 4);
    gpgcon = ioremap(0x56000060, 4);

    *gpccon  = 0xaaaaaaaa;   /* GPIO管腳用於VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND */
    *gpdcon  = 0xaaaaaaaa;   /* GPIO管腳用於VD[23:8] */

    *gpbcon &= ~(3);  /* GPB0設定為輸出引腳 */
    *gpbcon |= 1;
    *gpbdat &= ~1;     /* 輸出低電平 */

    *gpgcon |= (3<<8); /* GPG4用作LCD_PWREN */

    /* 3.2 根據LCD手冊設定LCD控制器, 比如VCLK的頻率等 */
    lcd_regs = ioremap(0x4D000000, sizeof(struct lcd_regs));

    /* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手冊P14
     *            10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2]
     *            CLKVAL = 4
     * bit[6:5]: 0b11, TFT LCD
     * bit[4:1]: 0b1100, 16 bpp for TFT
     * bit[0]  : 0 = Disable the video output and the LCD control signal.
     */
    lcd_regs->lcdcon1  = (4<<8) | (3<<5) | (0x0c<<1);

#if 1
    /* 垂直方向的時間引數
     * bit[31:24]: VBPD, VSYNC之後再過多長時間才能發出第1行資料
     *             LCD手冊 T0-T2-T1=4
     *             VBPD=3
     * bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319
     * bit[13:6] : VFPD, 發出最後一行資料之後,再過多長時間才發出VSYNC
     *             LCD手冊T2-T5=322-320=2, 所以VFPD=2-1=1
     * bit[5:0]  : VSPW, VSYNC訊號的脈衝寬度, LCD手冊T1=1, 所以VSPW=1-1=0
     */
    lcd_regs->lcdcon2  = (1<<24) | (271<<14) | (1<<6) | (9);


    /* 水平方向的時間引數
     * bit[25:19]: HBPD, VSYNC之後再過多長時間才能發出第1行資料
     *             LCD手冊 T6-T7-T8=17
     *             HBPD=16
     * bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239
     * bit[7:0] : HFPD, 發出最後一行裡最後一個象素資料之後,再過多長時間才發出HSYNC
     *             LCD手冊T8-T11=251-240=11, 所以HFPD=11-1=10
     */
    lcd_regs->lcdcon3 = (1<<19) | (479<<8) | (1);

    /* 水平方向的同步訊號
     * bit[7:0] : HSPW, HSYNC訊號的脈衝寬度, LCD手冊T7=5, 所以HSPW=5-1=4
     */ 
    lcd_regs->lcdcon4 = 40;

#else
lcd_regs->lcdcon2 = S3C2410_LCDCON2_VBPD(5) | \
        S3C2410_LCDCON2_LINEVAL(319) | \
        S3C2410_LCDCON2_VFPD(3) | \
        S3C2410_LCDCON2_VSPW(1);

lcd_regs->lcdcon3 = S3C2410_LCDCON3_HBPD(10) | \
        S3C2410_LCDCON3_HOZVAL(239) | \
        S3C2410_LCDCON3_HFPD(1);

lcd_regs->lcdcon4 = S3C2410_LCDCON4_MVAL(13) | \
        S3C2410_LCDCON4_HSPW(0);

#endif
    /* 訊號的極性 
     * bit[11]: 1=565 format
     * bit[10]: 0 = The video data is fetched at VCLK falling edge
     * bit[9] : 1 = HSYNC訊號要反轉,即低電平有效 
     * bit[8] : 1 = VSYNC訊號要反轉,即低電平有效 
     * bit[6] : 0 = VDEN不用反轉
     * bit[3] : 0 = PWREN輸出0
     * bit[1] : 0 = BSWP
     * bit[0] : 1 = HWSWP 2440手冊P413
     */
    lcd_regs->lcdcon5 = (1<<11) | (0<<10) | (1<<9) | (1<<8) | (1<<0);

    /* 3.3 分配視訊記憶體(framebuffer), 並把地址告訴LCD控制器 */
    s3c_lcd->screen_base = dma_alloc_writecombine(NULL, s3c_lcd->fix.smem_len, &s3c_lcd->fix.smem_start, GFP_KERNEL);

    lcd_regs->lcdsaddr1  = (s3c_lcd->fix.smem_start >> 1) & ~(3<<30);
    lcd_regs->lcdsaddr2  = ((s3c_lcd->fix.smem_start + s3c_lcd->fix.smem_len) >> 1) & 0x1fffff;
    lcd_regs->lcdsaddr3  = (480*16/16);  /* 一行的長度(單位: 2位元組) */   

    //s3c_lcd->fix.smem_start = xxx;  /* 視訊記憶體的實體地址 */
    /* 啟動LCD */
    lcd_regs->lcdcon1 |= (1<<0); /* 使能LCD控制器 */
    lcd_regs->lcdcon5 |= (1<<3); /* 使能LCD本身 */
    *gpbdat |= 1;     /* 輸出高電平, 使能背光 */     

    /* 4. 註冊 */
    register_framebuffer(s3c_lcd);

    return 0;
}

static void lcd_exit(void)
{
    unregister_framebuffer(s3c_lcd);
    lcd_regs->lcdcon1 &= ~(1<<0); /* 關閉LCD本身 */
    *gpbdat &= ~1;     /* 關閉背光 */
    dma_free_writecombine(NULL, s3c_lcd->fix.smem_len, s3c_lcd->screen_base, s3c_lcd->fix.smem_start);
    iounmap(lcd_regs);
    iounmap(gpbcon);
    iounmap(gpccon);
    iounmap(gpdcon);
    iounmap(gpgcon);
    framebuffer_release(s3c_lcd);
}

module_init(lcd_init);
module_exit(lcd_exit);

MODULE_LICENSE("GPL");

若想開發板開機直接支援該LCD,需要修改linux核心LCD驅動部分與硬體相關程式碼。

linux-2.6.22.6\arch\arm\mach\mach-smdk2440.c 修改完成直接編譯核心即可

static struct s3c2410fb_mach_info smdk2440_lcd_cfg __initdata = {
    .regs   = {

        .lcdcon1    = S3C2410_LCDCON1_TFT16BPP |
                  S3C2410_LCDCON1_TFT |
                  S3C2410_LCDCON1_CLKVAL(0x04),

        .lcdcon2    = S3C2410_LCDCON2_VBPD(7) |
                  S3C2410_LCDCON2_LINEVAL(319) |
                  S3C2410_LCDCON2_VFPD(6) |
                  S3C2410_LCDCON2_VSPW(3),

        .lcdcon3    = S3C2410_LCDCON3_HBPD(19) |
                  S3C2410_LCDCON3_HOZVAL(239) |
                  S3C2410_LCDCON3_HFPD(7),

        .lcdcon4    = S3C2410_LCDCON4_MVAL(0) |
                  S3C2410_LCDCON4_HSPW(40),

        .lcdcon5    = S3C2410_LCDCON5_FRM565 |
                  S3C2410_LCDCON5_INVVLINE |
                  S3C2410_LCDCON5_INVVFRAME |
                  S3C2410_LCDCON5_PWREN |
                  S3C2410_LCDCON5_HWSWP,
    },


    /* 暫存器管腳註意要修改*/
    .gpccon     = 0xaa940659,
    .gpccon_mask    = 0xffffffff,
    .gpcup      = 0x0000ffff,
    .gpcup_mask = 0xffffffff,
    .gpdcon     = 0xaa84aaa0,
    .gpdcon_mask    = 0xffffffff,
    .gpdup      = 0x0000faff,
    .gpdup_mask = 0xffffffff,


    .lpcsel     = ((0xCE6) & ~7) | 1<<4,
    .type       = S3C2410_LCDCON1_TFT16BPP,

    .width      = 480,
    .height     = 272,

    .xres       = {
        .min    = 480,
        .max    = 480,
        .defval = 480,
    },

    .yres       = {
        .min    = 272,
        .max    = 272,
        .defval = 272,
    },

    .bpp        = {
        .min    = 16,
        .max    = 16,
        .defval = 16,
    },
};