S3C2440上LCD驅(qū)動(dòng)(FrameBuffer)實(shí)例開(kāi)發(fā)講解(二)
/*最后,注冊(cè)這個(gè)幀緩沖設(shè)備fb_info到系統(tǒng)中, register_framebuffer定義在fb.h中在fbmem.c中實(shí)現(xiàn)*/
本文引用地址:http://butianyuan.cn/article/201608/295509.htmret = register_framebuffer(fbinfo);
if (ret < 0)
{
dev_err(&pdev->dev, "failed to register framebuffer device: %dn", ret);
goto err_video_nomem;
}
/*對(duì)設(shè)備文件系統(tǒng)的支持(對(duì)設(shè)備文件系統(tǒng)的理解請(qǐng)參閱:嵌入式Linux之我行——設(shè)備文件系統(tǒng)剖析與使用)
創(chuàng)建frambuffer設(shè)備文件,device_create_file定義在linux/device.h中*/
ret = device_create_file(&pdev->dev, &dev_attr_debug);
if (ret)
{
dev_err(&pdev->dev, "failed to add debug attributen");
}
return 0;
/*以下是上面錯(cuò)誤處理的跳轉(zhuǎn)點(diǎn)*/
err_nomem:
release_resource(fbvar->lcd_mem);
kfree(fbvar->lcd_mem);
err_nomap:
iounmap(fbvar->lcd_base);
err_noclk:
clk_disable(fbvar->lcd_clock);
clk_put(fbvar->lcd_clock);
err_noirq:
free_irq(fbvar->lcd_irq_no, fbvar);
err_nofb:
platform_set_drvdata(pdev, NULL);
framebuffer_release(fbinfo);
err_video_nomem:
my2440fb_unmap_video_memory(fbinfo);
return ret;
}
/*LCD中斷服務(wù)程序*/
static irqreturn_t lcd_fb_irq(int irq, void *dev_id)
{
struct my2440fb_var *fbvar = dev_id;
void __iomem *lcd_irq_base;
unsigned long lcdirq;
/*LCD中斷掛起寄存器基地址*/
lcd_irq_base = fbvar->lcd_base + S3C2410_LCDINTBASE;
/*讀取LCD中斷掛起寄存器的值*/
lcdirq = readl(lcd_irq_base + S3C24XX_LCDINTPND);
/*判斷是否為中斷掛起狀態(tài)*/
if(lcdirq & S3C2410_LCDINT_FRSYNC)
{
/*填充調(diào)色板*/
if (fbvar->palette_ready)
{
my2440fb_write_palette(fbvar);
}
/*設(shè)置幀已插入中斷請(qǐng)求*/
writel(S3C2410_LCDINT_FRSYNC, lcd_irq_base + S3C24XX_LCDINTPND);
writel(S3C2410_LCDINT_FRSYNC, lcd_irq_base + S3C24XX_LCDSRCPND);
}
return IRQ_HANDLED;
}
/*填充調(diào)色板*/
static void my2440fb_write_palette(struct my2440fb_var *fbvar)
{
unsigned int i;
void __iomem *regs = fbvar->lcd_base;
fbvar->palette_ready = 0;
for (i = 0; i < 256; i++)
{
unsigned long ent = fbvar->palette_buffer[i];
if (ent == PALETTE_BUFF_CLEAR)
{
continue;
}
writel(ent, regs + S3C2410_TFTPAL(i));
if (readw(regs + S3C2410_TFTPAL(i)) == ent)
{
fbvar->palette_buffer[i] = PALETTE_BUFF_CLEAR;
}
else
{
fbvar->palette_ready = 1;
}
}
}
/*LCD各寄存器進(jìn)行初始化*/
static int my2440fb_init_registers(struct fb_info *fbinfo)
{
unsigned long flags;
void __iomem *tpal;
void __iomem *lpcsel;
/*從lcd_fb_probe探測(cè)函數(shù)設(shè)置的私有變量結(jié)構(gòu)體中再獲得LCD相關(guān)信息的數(shù)據(jù)*/
struct my2440fb_var *fbvar = fbinfo->par;
struct s3c2410fb_mach_info *mach_info = fbvar->dev->platform_data;
/*獲得臨時(shí)調(diào)色板寄存器基地址,S3C2410_TPAL宏定義在mach-s3c2410/include/mach/regs-lcd.h中。
注意對(duì)于lpcsel這是一個(gè)針對(duì)三星TFT屏的一個(gè)專用寄存器,如果用的不是三星的TFT屏應(yīng)該不用管它。*/
tpal = fbvar->lcd_base + S3C2410_TPAL;
lpcsel = fbvar->lcd_base + S3C2410_LPCSEL;
/*在修改下面寄存器值之前先屏蔽中斷,將中斷狀態(tài)保存到flags中*/
local_irq_save(flags);
/*這里就是在上一篇章中講到的把IO端口C和D配置成LCD模式*/
modify_gpio(S3C2410_GPCUP, mach_info->gpcup, mach_info->gpcup_mask);
modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);
modify_gpio(S3C2410_GPDUP, mach_info->gpdup, mach_info->gpdup_mask);
modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);
/*恢復(fù)被屏蔽的中斷*/
local_irq_restore(flags);
writel(0x00, tpal);/*臨時(shí)調(diào)色板寄存器使能禁止*/
writel(mach_info->lpcsel, lpcsel);/*在上一篇中講到過(guò),它是三星TFT屏的一個(gè)寄存器,這里可以不管*/
return 0;
}
/*該函數(shù)實(shí)現(xiàn)修改GPIO端口的值,注意第三個(gè)參數(shù)mask的作用是將要設(shè)置的寄存器值先清零*/
static inline void modify_gpio(void __iomem *reg, unsigned long set, unsigned long mask)
{
unsigned long tmp;
tmp = readl(reg) & ~mask;
writel(tmp | set, reg);
}
/*檢查fb_info中的可變參數(shù)*/
static int my2440fb_check_var(struct fb_info *fbinfo)
{
unsigned i;
/*從lcd_fb_probe探測(cè)函數(shù)設(shè)置的平臺(tái)數(shù)據(jù)中再獲得LCD相關(guān)信息的數(shù)據(jù)*/
struct fb_var_screeninfo *var = &fbinfo->var;/*fb_info中的可變參數(shù)*/
struct my2440fb_var *fbvar = fbinfo->par;/*在lcd_fb_probe探測(cè)函數(shù)中設(shè)置的私有結(jié)構(gòu)體數(shù)據(jù)*/
struct s3c2410fb_mach_info *mach_info = fbvar->dev->platform_data;/*LCD的配置結(jié)構(gòu)體數(shù)據(jù),這個(gè)配置結(jié)構(gòu)體的賦值在上一篇章的"3. 幀緩沖設(shè)備作為平臺(tái)設(shè)備"中*/
struct s3c2410fb_display *display = NULL;
struct s3c2410fb_display *default_display = mach_info->displays + mach_info->default_display;
int type = default_display->type;/*LCD的類型,看上一篇章的"3. 幀緩沖設(shè)備作為平臺(tái)設(shè)備"中的type賦值是TFT類型*/
/*驗(yàn)證X/Y解析度*/
if (var->yres == default_display->yres &&
var->xres == default_display->xres &&
var->bits_per_pixel == default_display->bpp)
{
display = default_display;
}
else
{
for (i = 0; i < mach_info->num_displays; i++)
{
if (type == mach_info->displays[i].type &&
var->yres == mach_info->displays[i].yres &&
var->xres == mach_info->displays[i].xres &&
var->bits_per_pixel == mach_info->displays[i].bpp)
{
display = mach_info->displays + i;
break;
}
}
}
if (!display)
{
return -EINVAL;
}
/*配置LCD配置寄存器1中的5-6位(配置成TFT類型)和配置LCD配置寄存器5*/
fbvar->regs.lcdcon1 = display->type;
fbvar->regs.lcdcon5 = display->lcdcon5;
/* 設(shè)置屏幕的虛擬解析像素和高度寬度 */
var->xres_virtual = display->xres;
var->yres_virtual = display->yres;
var->height = display->height;
var->width = display->width;
/* 設(shè)置時(shí)鐘像素,行、幀切換值,水平同步、垂直同步長(zhǎng)度值 */
var->pixclock = display->pixclock;
var->left_margin = display->left_margin;
var->right_margin = display->right_margin;
var->upper_margin = display->upper_margin;
var->lower_margin = display->lower_margin;
var->vsync_len = display->vsync_len;
var->hsync_len = display->hsync_len;
/*設(shè)置透明度*/
var->transp.offset = 0;
var->transp.length = 0;
/*根據(jù)色位模式(BPP)來(lái)設(shè)置可變參數(shù)中R、G、B的顏色位域。對(duì)于這些參數(shù)值的設(shè)置請(qǐng)參考CPU數(shù)據(jù)
手冊(cè)中"顯示緩沖區(qū)與顯示點(diǎn)對(duì)應(yīng)關(guān)系圖",例如在上一篇章中我就畫出了8BPP和16BPP時(shí)的對(duì)應(yīng)關(guān)系圖*/
switch (var->bits_per_pixel)
{
case 1:
case 2:
case 4:
var->red.offset = 0;
var->red.length = var->bits_per_pixel;
var->green = var->red;
var->blue = var->red;
break;
case 8:/* 8 bpp 332 */
if (display->type != S3C2410_LCDCON1_TFT)
{
var->red.length = 3;
var->red.offset = 5;
var->green.length = 3;
var->green.offset = 2;
var->blue.length = 2;
var->blue.offset = 0;
}else{
var->red.offset = 0;
var->red.length = 8;
var->green = var->red;
var->blue = var->red;
}
break;
case 12:/* 12 bpp 444 */
var->red.length = 4;
var->red.offset = 8;
var->green.length = 4;
var->green.offset = 4;
var->blue.length = 4;
var->blue.offset = 0;
break;
case 16:/* 16 bpp */
if (display->lcdcon5 & S3C2410_LCDCON5_FRM565)
{
/* 565 format */
var->red.offset = 11;
var->green.offset = 5;
var->blue.offset = 0;
var->red.length = 5;
var->green.length = 6;
var->blue.length = 5;
} else {
/* 5551 format */
var->red.offset = 11;
var->green.offset = 6;
var->blue.offset = 1;
var->red.length = 5;
var->green.length = 5;
var->blue.length = 5;
}
break;
case 32:/* 24 bpp 888 and 8 dummy */
var->red.length = 8;
var->red.offset = 16;
var->green.length = 8;
var->green.offset = 8;
var->blue.length = 8;
var->blue.offset = 0;
break;
}
return 0;
}
/*申請(qǐng)幀緩沖設(shè)備fb_info的顯示緩沖區(qū)空間*/
static int __init my2440fb_map_video_memory(struct fb_info *fbinfo)
{
dma_addr_t map_dma;/*用于保存DMA緩沖區(qū)總線地址*/
struct my2440fb_var *fbvar = fbinfo->par;/*獲得在lcd_fb_probe探測(cè)函數(shù)中設(shè)置的私有結(jié)構(gòu)體數(shù)據(jù)*/
unsigned map_size = PAGE_ALIGN(fbinfo->fix.smem_len);/*獲得FrameBuffer緩存的大小, PAGE_ALIGN定義在mm.h中*/
/*將分配的一個(gè)寫合并DMA緩存區(qū)設(shè)置為L(zhǎng)CD屏幕的虛擬地址(對(duì)于DMA請(qǐng)參考DMA相關(guān)知識(shí))
dma_alloc_writecombine定義在arch/arm/mm/dma-mapping.c中*/
fbinfo->screen_base = dma_alloc_writecombine(fbvar->dev, map_size, &map_dma, GFP_KERNEL);
if (fbinfo->screen_base)
{
/*設(shè)置這片DMA緩存區(qū)的內(nèi)容為空*/
memset(fbinfo->screen_base, 0x00, map_size);
/*將DMA緩沖區(qū)總線地址設(shè)成fb_info不可變參數(shù)中framebuffer緩存的開(kāi)始位置*/
fbinfo->fix.smem_start = map_dma;
}
return fbinfo->screen_base ? 0 : -ENOMEM;
}
/*釋放幀緩沖設(shè)備fb_info的顯示緩沖區(qū)空間*/
static inline void my2440fb_unmap_video_memory(struct fb_info *fbinfo)
{
struct my2440fb_var *fbvar = fbinfo->par;
unsigned map_size = PAGE_ALIGN(fbinfo->fix.smem_len);
/*跟申請(qǐng)DMA的地方想對(duì)應(yīng)*/
dma_free_writecombine(fbvar->dev, map_size, fbinfo->screen_base, fbinfo->fix.smem_start);
}
/*LCD FrameBuffer設(shè)備移除的實(shí)現(xiàn),注意這里使用一個(gè)__devexit宏,和lcd_fb_probe接口函數(shù)相對(duì)應(yīng)。
在Linux內(nèi)核中,使用了大量不同的宏來(lái)標(biāo)記具有不同作用的函數(shù)和數(shù)據(jù)結(jié)構(gòu),這些宏在include/linux/init.h
頭文件中定義,編譯器通過(guò)這些宏可以把代碼優(yōu)化放到合適的內(nèi)存位置,以減少內(nèi)存占用和提高內(nèi)核效率。
__devinit、__devexit就是這些宏之一,在probe()和remove()函數(shù)中應(yīng)該使用__devinit和__devexit宏。
又當(dāng)remove()函數(shù)使用了__devexit宏時(shí),則在驅(qū)動(dòng)結(jié)構(gòu)體中一定要使用__devexit_p宏來(lái)引用remove(),
所以在第①步中就用__devexit_p來(lái)引用lcd_fb_remove接口函數(shù)。*/
static int __devexit lcd_fb_remove(struct platform_device *pdev)
{
struct fb_info *fbinfo = platform_get_drvdata(pdev);
struct my2440fb_var *fbvar = fbinfo->par;
/*從系統(tǒng)中注銷幀緩沖設(shè)備*/
unregister_framebuffer(fbinfo);
/*停止LCD控制器的工作*/
my2440fb_lcd_enable(fbvar, 0);
/*延遲一段時(shí)間,因?yàn)橥V筁CD控制器需要一點(diǎn)時(shí)間 */
msleep(1);
/*釋放幀緩沖設(shè)備fb_info的顯示緩沖區(qū)空間*/
my2440fb_unmap_video_memory(fbinfo);
/*將LCD平臺(tái)數(shù)據(jù)清空和釋放fb_info空間資源*/
platform_set_drvdata(pdev, NULL);
framebuffer_release(fbinfo);
/*釋放中斷資源*/
free_irq(fbvar->lcd_irq_no, fbvar);
/*釋放時(shí)鐘資源*/
if (fbvar->lcd_clock)
{
clk_disable(fbvar->lcd_clock);
clk_put(fbvar->lcd_clock);
fbvar->lcd_clock = NULL;
}
/*釋放LCD IO空間映射的虛擬內(nèi)存空間*/
iounmap(fbvar->lcd_base);
/*釋放申請(qǐng)的LCD IO端口所占用的IO空間*/
release_resource(fbvar->lcd_mem);
kfree(fbvar->lcd_mem);
return 0;
}
/*停止LCD控制器的工作*/
static void my2440fb_lcd_enable(struct my2440fb_var *fbvar, int enable)
{
unsigned long flags;
/*在修改下面寄存器值之前先屏蔽中斷,將中斷狀態(tài)保存到flags中*/
local_irq_save(flags);
if (enable)
{
fbvar->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID;
}
else
{
fbvar->regs.lcdcon1 &= ~S3C2410_LCDCON1_ENVID;
}
writel(fbvar->regs.lcdcon1, fbvar->lcd_base + S3C2410_LCDCON1);
/*恢復(fù)被屏蔽的中斷*/
local_irq_restore(flags);
}
/*對(duì)LCD FrameBuffer平臺(tái)設(shè)備驅(qū)動(dòng)電源管理的支持,CONFIG_PM這個(gè)宏定義在內(nèi)核中*/
#ifdef CONFIG_PM
/*當(dāng)配置內(nèi)核時(shí)選上電源管理,則平臺(tái)設(shè)備的驅(qū)動(dòng)就支持掛起和恢復(fù)功能*/
static int lcd_fb_suspend(struct platform_device *pdev, pm_message_t state)
{
/*掛起LCD設(shè)備,注意這里掛起LCD時(shí)并沒(méi)有保存LCD控制器的各種狀態(tài),所以在恢復(fù)后LCD不會(huì)繼續(xù)顯示掛起前的內(nèi)容
若要繼續(xù)顯示掛起前的內(nèi)容,則要在這里保存LCD控制器的各種狀態(tài),這里就不講這個(gè)了,以后講到電源管理再講*/
struct fb_info *fbinfo = platform_get_drvdata(pdev);
struct my2440fb_var *fbvar = fbinfo->par;
/*停止LCD控制器的工作*/
my2440fb_lcd_enable(fbvar, 0);
msleep(1);
/*停止時(shí)鐘*/
clk_disable(fbvar->lcd_clock);
return 0;
}
staticint lcd_fb_resume(struct platform_device *pdev)
{
/*恢復(fù)掛起的LCD設(shè)備*/
struct fb_info *fbinfo = platform_get_drvdata(pdev);
struct my2440fb_var *fbvar = fbinfo->par;
/*開(kāi)啟時(shí)鐘*/
clk_enable(fbvar->lcd_clock);
/*初始化LCD控制器之前要延遲一段時(shí)間*/
msleep(1);
/*恢復(fù)時(shí)重新初始化LCD各寄存器*/
my2440fb_init_registers(fbinfo);
/*重新激活fb_info中所有的參數(shù)配置,該函數(shù)定義在第③步中再講*/
my2440fb_activate_var(fbinfo);
/*正與掛起時(shí)講到的那樣,因?yàn)闆](méi)保存掛起時(shí)LCD控制器的各種狀態(tài),
所以恢復(fù)后就讓LCD顯示空白,該函數(shù)定義也在第③步中再講*/
my2440fb_blank(FB_BLANK_UNBLANK, fbinfo);
return 0;
}
#else
/*如果配置內(nèi)核時(shí)沒(méi)選上電源管理,則平臺(tái)設(shè)備的驅(qū)動(dòng)就不支持掛起和恢復(fù)功能,這兩個(gè)函數(shù)也就無(wú)需實(shí)現(xiàn)了*/
#define lcd_fb_suspend NULL
#define lcd_fb_resume NULL
#endif
?、邸彌_設(shè)備驅(qū)動(dòng)對(duì)底層硬件操作的函數(shù)接口實(shí)現(xiàn)(即:my2440fb_ops的實(shí)現(xiàn)):
/*Framebuffer底層硬件操作各接口函數(shù)*/
static struct fb_ops my2440fb_ops =
{
.owner = THIS_MODULE,
.fb_check_var = my2440fb_check_var,/*第②步中已實(shí)現(xiàn)*/
.fb_set_par = my2440fb_set_par,/*設(shè)置fb_info中的參數(shù),主要是LCD的顯示模式*/
.fb_blank = my2440fb_blank,/*顯示空白(即:LCD開(kāi)關(guān)控制)*/
.fb_setcolreg = my2440fb_setcolreg,/*設(shè)置顏色表*/
/*以下三個(gè)函數(shù)是可選的,主要是提供fb_console的支持,在內(nèi)核中已經(jīng)實(shí)現(xiàn),這里直接調(diào)用即可*/
.fb_fillrect = cfb_fillrect,/*定義在drivers/video/cfbfillrect.c中*/
.fb_copyarea = cfb_copyarea,/*定義在drivers/video/cfbcopyarea.c中*/
.fb_imageblit = cfb_imageblit,/*定義在drivers/video/cfbimgblt.c中*/
};
/*設(shè)置fb_info中的參數(shù),這里根據(jù)用戶設(shè)置的可變參數(shù)var調(diào)整固定參數(shù)fix*/
static int my2440fb_set_par(struct fb_info *fbinfo)
{
/*獲得fb_info中的可變參數(shù)*/
struct fb_var_screeninfo *var = &fbinfo->var;
/*判斷可變參數(shù)中的色位模式,根據(jù)色位模式來(lái)設(shè)置色彩模式*/
switch (var->bits_per_pixel)
{
case 32:
case 16:
case 12:/*12BPP時(shí),設(shè)置為真彩色(分成紅、綠、藍(lán)三基色)*/
fbinfo->fix.visual = FB_VISUAL_TRUECOLOR;
break;
case 1:/*1BPP時(shí),設(shè)置為黑白色(分黑、白兩種色,F(xiàn)B_VISUAL_MONO01代表黑,F(xiàn)B_VISUAL_MONO10代表白)*/
fbinfo->fix.visual = FB_VISUAL_MONO01;
break;
default:/*默認(rèn)設(shè)置為偽彩色,采用索引顏色顯示*/
fbinfo->fix.visual = FB_VISUAL_PSEUDOCOLOR;
break;
}
/*設(shè)置fb_info中固定參數(shù)中一行的字節(jié)數(shù),公式:1行字節(jié)數(shù)=(1行像素個(gè)數(shù)*每像素位數(shù)BPP)/8 */
fbinfo->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
/*修改以上參數(shù)后,重新激活fb_info中的參數(shù)配置(即:使修改后的參數(shù)在硬件上生效)*/
my2440fb_activate_var(fbinfo);
return 0;
}
/*重新激活fb_info中的參數(shù)配置*/
static void my2440fb_activate_var(struct fb_info *fbinfo)
{
/*獲得結(jié)構(gòu)體變量*/
struct my2440fb_var *fbvar = fbinfo->par;
void __iomem *regs = fbvar->lcd_base;
/*獲得fb_info可變參數(shù)*/
struct fb_var_screeninfo *var = &fbinfo->var;
/*計(jì)算LCD控制寄存器1中的CLKVAL值, 根據(jù)數(shù)據(jù)手冊(cè)中該寄存器的描述,計(jì)算公式如下:
* STN屏:VCLK = HCLK / (CLKVAL * 2), CLKVAL要求>= 2
* TFT屏:VCLK = HCLK / [(CLKVAL + 1) * 2], CLKVAL要求>= 0*/
int clkdiv = my2440fb_calc_pixclk(fbvar, var->pixclock) / 2;
/*獲得屏幕的類型*/
int type = fbvar->regs.lcdcon1 & S3C2410_LCDCON1_TFT;
if (type == S3C2410_LCDCON1_TFT)
{
/*根據(jù)數(shù)據(jù)手冊(cè)按照TFT屏的要求配置LCD控制寄存器1-5*/
my2440fb_config_tft_lcd_regs(fbinfo, &fbvar->regs);
--clkdiv;
if (clkdiv < 0)
{
clkdiv = 0;
}
}
else
{
/*根據(jù)數(shù)據(jù)手冊(cè)按照STN屏的要求配置LCD控制寄存器1-5*/
my2440fb_config_stn_lcd_regs(fbinfo, &fbvar->regs);
if (clkdiv < 2)
{
clkdiv = 2;
}
}
/*設(shè)置計(jì)算的LCD控制寄存器1中的CLKVAL值*/
fbvar->regs.lcdcon1 |= S3C2410_LCDCON1_CLKVAL(clkdiv);
/*將各參數(shù)值寫入LCD控制寄存器1-5中*/
writel(fbvar->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID, regs + S3C2410_LCDCON1);
writel(fbvar->regs.lcdcon2, regs + S3C2410_LCDCON2);
writel(fbvar->regs.lcdcon3, regs + S3C2410_LCDCON3);
writel(fbvar->regs.lcdcon4, regs + S3C2410_LCDCON4);
writel(fbvar->regs.lcdcon5, regs + S3C2410_LCDCON5);
/*配置幀緩沖起始地址寄存器1-3*/
my2440fb_set_lcdaddr(fbinfo);
fbvar->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID,
writel(fbvar->regs.lcdcon1, regs + S3C2410_LCDCON1);
}
/*計(jì)算LCD控制寄存器1中的CLKVAL值*/
static unsigned int my2440fb_calc_pixclk(struct my2440fb_var *fbvar, unsigned long pixclk)
{
/*獲得LCD的時(shí)鐘*/
unsigned long clk = clk_get_rate(fbvar->lcd_clock);
/* 像素時(shí)鐘單位是皮秒,而時(shí)鐘的單位是赫茲,所以計(jì)算公式為:
* Hz -> picoseconds is / 10^-12
*/
unsigned long long div = (unsigned long long)clk * pixclk;
div >>= 12;/* div / 2^12 */
do_div(div, 625 * 625UL * 625); /* div / 5^12, do_div宏定義在asm/div64.h中*/
return div;
}
/*根據(jù)數(shù)據(jù)手冊(cè)按照TFT屏的要求配置LCD控制寄存器1-5*/
static void my2440fb_config_tft_lcd_regs(const struct fb_info *fbinfo, struct s3c2410fb_hw *regs)
{
const struct my2440fb_var *fbvar = fbinfo->par;
const struct fb_var_screeninfo *var = &fbinfo->var;
/*根據(jù)色位模式設(shè)置LCD控制寄存器1和5,參考數(shù)據(jù)手冊(cè)*/
switch (var->bits_per_pixel)
{
case 1:/*1BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
break;
case 2:/*2BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
break;
case 4:/*4BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
break;
case 8:/*8BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
regs->lcdcon5 |= S3C2410_LCDCON5_BSWP | S3C2410_LCDCON5_FRM565;
regs->lcdcon5 &= ~S3C2410_LCDCON5_HWSWP;
break;
case 16:/*16BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
regs->lcdcon5 &= ~S3C2410_LCDCON5_BSWP;
regs->lcdcon5 |= S3C2410_LCDCON5_HWSWP;
break;
case 32:/*32BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_TFT24BPP;
regs->lcdcon5 &= ~(S3C2410_LCDCON5_BSWP | S3C2410_LCDCON5_HWSWP | S3C2410_LCDCON5_BPP24BL);
break;
default:/*無(wú)效的BPP*/
dev_err(fbvar->dev, "invalid bpp %dn", var->bits_per_pixel);
}
/*設(shè)置LCD配置寄存器2、3、4*/
regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1) |
S3C2410_LCDCON2_VBPD(var->upper_margin - 1) |
S3C2410_LCDCON2_VFPD(var->lower_margin - 1) |
S3C2410_LCDCON2_VSPW(var->vsync_len - 1);
regs->lcdcon3 = S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
S3C2410_LCDCON3_HFPD(var->left_margin - 1) |
S3C2410_LCDCON3_HOZVAL(var->xres - 1);
regs->lcdcon4 = S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
}
/*根據(jù)數(shù)據(jù)手冊(cè)按照STN屏的要求配置LCD控制寄存器1-5*/
static void my2440fb_config_stn_lcd_regs(const struct fb_info *fbinfo, struct s3c2410fb_hw *regs)
{
const struct my2440fb_var *fbvar = fbinfo->par;
const struct fb_var_screeninfo *var = &fbinfo->var;
int type = regs->lcdcon1 & ~S3C2410_LCDCON1_TFT;
int hs = var->xres >> 2;
unsigned wdly = (var->left_margin >> 4) - 1;
unsigned wlh = (var->hsync_len >> 4) - 1;
if (type != S3C2410_LCDCON1_STN4)
{
hs >>= 1;
}
/*根據(jù)色位模式設(shè)置LCD控制寄存器1,參考數(shù)據(jù)手冊(cè)*/
switch (var->bits_per_pixel)
{
case 1:/*1BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_STN1BPP;
break;
case 2:/*2BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_STN2GREY;
break;
case 4:/*4BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_STN4GREY;
break;
case 8:/*8BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_STN8BPP;
hs *= 3;
break;
case 12:/*12BPP*/
regs->lcdcon1 |= S3C2410_LCDCON1_STN12BPP;
hs *= 3;
break;
default:/*無(wú)效的BPP*/
dev_err(fbvar->dev, "invalid bpp %dn", var->bits_per_pixel);
}
/*設(shè)置LCD配置寄存器2、3、4, 參考數(shù)據(jù)手冊(cè)*/
if (wdly > 3) wdly = 3;
if (wlh > 3) wlh = 3;
regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1);
regs->lcdcon3 = S3C2410_LCDCON3_WDLY(wdly) |
S3C2410_LCDCON3_LINEBLANK(var->right_margin / 8) |
S3C2410_LCDCON3_HOZVAL(hs - 1);
regs->lcdcon4 = S3C2410_LCDCON4_WLH(wlh);
}
/*配置幀緩沖起始地址寄存器1-3,參考數(shù)據(jù)手冊(cè)*/
static void my2440fb_set_lcdaddr(struct fb_info *fbinfo)
{
unsigned long saddr1, saddr2, saddr3;
struct my2440fb_var *fbvar = fbinfo->par;
void __iomem *regs = fbvar->lcd_base;
saddr1 = fbinfo->fix.smem_start >> 1;
saddr2 = fbinfo->fix.smem_start;
saddr2 += fbinfo->fix.line_length * fbinfo->var.yres;
saddr2 >>= 1;
saddr3 = S3C2410_OFFSIZE(0) | S3C2410_PAGEWIDTH((fbinfo->fix.line_length / 2) & 0x3ff);
writel(saddr1, regs + S3C2410_LCDSADDR1);
writel(saddr2, regs + S3C2410_LCDSADDR2);
writel(saddr3, regs + S3C2410_LCDSADDR3);
}
/*顯示空白,blank mode有5種模式,定義在fb.h中,是一個(gè)枚舉*/
static int my2440fb_blank(int blank_mode, struct fb_info *fbinfo)
{
struct my2440fb_var *fbvar = fbinfo->par;
void __iomem *regs = fbvar->lcd_base;
/*根據(jù)顯示空白的模式來(lái)設(shè)置LCD是開(kāi)啟還是停止*/
if (blank_mode == FB_BLANK_POWERDOWN)
{
my2440fb_lcd_enable(fbvar, 0);/*在第②步中定義*/
}
else
{
my2440fb_lcd_enable(fbvar, 1);/*在第②步中定義*/
}
/*根據(jù)顯示空白的模式來(lái)控制臨時(shí)調(diào)色板寄存器*/
if (blank_mode == FB_BLANK_UNBLANK)
{
/*臨時(shí)調(diào)色板寄存器無(wú)效*/
writel(0x0, regs + S3C2410_TPAL);
}
else
{
/*臨時(shí)調(diào)色板寄存器有效*/
writel(S3C2410_TPAL_EN, regs + S3C2410_TPAL);
}
return 0;
}
/*設(shè)置顏色表*/
static int my2440fb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info *fbinfo)
{
unsigned int val;
struct my2440fb_var *fbvar = fbinfo->par;
void __iomem *regs = fbvar->lcd_base;
switch (fbinfo->fix.visual)
{
case FB_VISUAL_TRUECOLOR:
/*真彩色*/
if (regno < 16)
{
u32 *pal = fbinfo->pseudo_palette;
val = chan_to_field(red, &fbinfo->var.red);
val |= chan_to_field(green, &fbinfo->var.green);
val |= chan_to_field(blue, &fbinfo->var.blue);
pal[regno] = val;
}
break;
case FB_VISUAL_PSEUDOCOLOR:
/*偽彩色*/
if (regno < 256)
{
val = (red >> 0) & 0xf800;
val |= (green >> 5) & 0x07e0;
val |= (blue >> 11) & 0x001f;
writel(val, regs + S3C2410_TFTPAL(regno));
/*修改調(diào)色板*/
schedule_palette_update(fbvar, regno, val);
}
break;
default:
return 1;
}
return 0;
}
static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
{
chan &= 0xffff;
chan >>= 16 - bf->length;
return chan << bf->offset;
}
/*修改調(diào)色板*/
static void schedule_palette_update(struct my2440fb_var *fbvar, unsigned int regno, unsigned int val)
{
unsigned long flags;
unsigned long irqen;
/*LCD中斷掛起寄存器基地址*/
void __iomem *lcd_irq_base = fbvar->lcd_base + S3C2410_LCDINTBASE;
/*在修改中斷寄存器值之前先屏蔽中斷,將中斷狀態(tài)保存到flags中*/
local_irq_save(flags);
fbvar->palette_buffer[regno] = val;
/*判斷調(diào)色板是否準(zhǔn)備就像*/
if (!fbvar->palette_ready)
{
fbvar->palette_ready = 1;
/*使能中斷屏蔽寄存器*/
irqen = readl(lcd_irq_base + S3C24XX_LCDINTMSK);
irqen &= ~S3C2410_LCDINT_FRSYNC;
writel(irqen, lcd_irq_base + S3C24XX_LCDINTMSK);
}
/*恢復(fù)被屏蔽的中斷*/
local_irq_restore(flags);
}
五、從整體上再描述一下FrameBuffer設(shè)備驅(qū)動(dòng)實(shí)例代碼的結(jié)構(gòu):
1、在第①部分代碼中主要做的事情有:
a.將LCD設(shè)備注冊(cè)到系統(tǒng)平臺(tái)設(shè)備中;
b.定義LCD平臺(tái)設(shè)備結(jié)構(gòu)體lcd_fb_driver。
2、在第②部分代碼中主要做的事情有:
a.獲取和設(shè)置LCD平臺(tái)設(shè)備的各種資源;
b.分配fb_info結(jié)構(gòu)體空間;
c.初始化fb_info結(jié)構(gòu)體中的各參數(shù);
d.初始化LCD控制器;
e.檢查fb_info中可變參數(shù);
f.申請(qǐng)幀緩沖設(shè)備的顯示緩沖區(qū)空間;
g.注冊(cè)fb_info。
a.實(shí)現(xiàn)對(duì)fb_info相關(guān)參數(shù)進(jìn)行檢查的硬件接口函數(shù);
b.實(shí)現(xiàn)對(duì)LCD顯示模式進(jìn)行設(shè)定的硬件接口函數(shù);
c.實(shí)現(xiàn)對(duì)LCD顯示開(kāi)關(guān)(空白)的硬件接口函數(shù)等。
評(píng)論