linux——LCD驅動
阿新 • • 發佈:2019-01-12
轉載fbmem分析
實現步驟
-
定義、分配fb_info*
static struct fb_info *s3c_lcd;
s3c_lcd = framebuffer_alloc(0, NULL); -
設定s3c_lcd*
-
硬體相關的操作*
配置GPIO用於LCD ;
3.2 根據LCD手冊設定LCD控制器,LCDCON、LCDADDR;
在初始化函式中,配置引腳開啟背光。 -
註冊*
register_framebuffer(s3c_lcd);
unregister_framebuffer(s3c_lcd);
AT043TN24引數推薦值
實驗平臺
zj2440v3
linux-3.4.2
編入核心:修改linux-3.4.2/drivers/video/Makefile,替換為lcd_4.3.o
驅動程式
#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>
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];
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;
};
/* 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 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 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 = 240*320*16/8;
s3c_lcd->fix.type = FB_TYPE_PACKED_PIXELS;//0
s3c_lcd->fix.visual = FB_VISUAL_TRUECOLOR; /* TFT真彩色 */
s3c_lcd->fix.line_length = 240*2;
/* 2.2 設定可變的引數 */
s3c_lcd->var.xres = 240;
s3c_lcd->var.yres = 320;
s3c_lcd->var.xres_virtual = 240;
s3c_lcd->var.yres_virtual = 320;
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 = 240*324*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], 畫素時鐘
* 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=Tvb-1=3
* bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319
* bit[13:6] : VFPD, 發出最後一行資料之後,再過多長時間才發出VSYNC
* LCD手冊T2-T5=322-320=2, 所以VFPD=Tvf-1=1
* bit[5:0] : VSPW, VSYNC訊號的脈衝寬度, LCD手冊T1=1, 所以VSPW=Tvp-1=0
*/
lcd_regs->lcdcon2 = (3<<24) | (319<<14) | (1<<6) | (0<<0);
/* 水平方向的時間引數
* bit[25:19]: HBPD, VSYNC之後再過多長時間才能發出第1行資料
* LCD手冊 T6-T7-T8=17
* HBPD=Thb-1=17
* bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239
* bit[7:0] : HFPD, 發出最後一行裡最後一個象素資料之後,再過多長時間才發出HSYNC
* LCD手冊T8-T11=251-240=11, 所以HFPD=Thf-1=10
*/
lcd_regs->lcdcon3 = (16<<19) | (239<<8) | (10<<0);
/* 水平方向的同步訊號
* bit[7:0] : HSPW, HSYNC訊號的脈衝寬度, LCD手冊T7=5, 所以HSPW=Thp-1=4
*/
lcd_regs->lcdcon4 = 4;
#else//320x240-lcd
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 = (240*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");