学习笔记 --- LINUX LCD显示原理与驱动分析

 

在分析驱动之前,先来分析下显示原理,这里以S3C2440为例,看下这个芯片的LCD控制器时序图:

VSYNC :帧数据脉冲,脉冲换屏,表示一屏数据开始

HSYNC :行数据脉冲,脉冲换行,表示一行数据开始

LEND :行结束脉冲,脉冲表示一行结束

VDEN :数据使能,表示VD可以发数据

VCLK  :基准时钟,脉冲送往数据线送一次数据

VD :数据

这些都是硬件管脚信号线,从图中可以看出一行中有效数据是HOZVAL+1个像素,(HSPW+1)+(HBPD+1)是屏幕左边黑框的像素,HFPD+1是屏幕右边黑框的像素,一般设置为左边等于右边,上边等于下边,iphone手机有很明显的黑框。那么上边就是(VSPW+1)+(VBPD+1)行,下边就是VFPD+1行,中间有效数据是LINEVAL+1行。假设LCD为240X320那么时序对应的显示图像就是(截图自韦东山老师):

外面的大框表示LCD黑框,里面表示真正显示的有效数据240X320。接下来说下显示16bpp图像的原理:

16bpp图像意思就是一个像素要用16个位来表示,那么就有2的16次方种颜色,一个像素16位,占2个字节,一屏240X320像素,那么一屏数据占240X320X2个字节,所以显存至少要这么大的空间。一个像素由红绿蓝三原色构成,所以这两个字节包含了红绿蓝三种颜色的信息,他们的占用bit数的比率为5:6:5,也可以是5:5:5,先只说5:6:5格式的,5+6+5=16bit,高位到低位分别表示5位红色,6位绿色,5位蓝色组成了一个两字节数据放在显存区里面,然后LCD控制器将其搬到LCD上显示出来,他们在显存里面的存放有两种方式:

一般选择下面这种方式,先放低16位,再放高16位,S3C2440默认是小端存储,所以这样低位对应低地址,刚好可以对应起来;那么从显存传输到LCD时数据线[0-23]传输的格式如何?

______________________________________________________________________________________________________

分析了原理,下面看驱动,LCD的驱动核心在内核fbmem.c里面,分析驱动从入口开始:

 
  1. static int __init fbmem_init(void)

  2. {

  3. proc_create("fb", 0, NULL, &fb_proc_fops);

  4.  
  5. if (register_chrdev(FB_MAJOR,"fb",&fb_fops)) //注册为字符设备驱动,主设备号为FB_MAJOR

  6. printk("unable to get major %d for fb devs\n", FB_MAJOR);

  7.  
  8. fb_class = class_create(THIS_MODULE, "graphics"); //创建类

  9. if (IS_ERR(fb_class)) {

  10. printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class));

  11. fb_class = NULL;

  12. }

  13. return 0;

  14. }

可以知道LCD驱动也就是一个字符设备驱动而已,这个框架已经熟悉了,再看下fops部分:

 
  1. static const struct file_operations fb_fops = {

  2. .owner = THIS_MODULE,

  3. .read = fb_read,

  4. .write = fb_write,

  5. .unlocked_ioctl = fb_ioctl,

  6. #ifdef CONFIG_COMPAT

  7. .compat_ioctl = fb_compat_ioctl,

  8. #endif

  9. .mmap = fb_mmap,

  10. .open = fb_open,

  11. .release = fb_release,

  12. #ifdef HAVE_ARCH_FB_UNMAPPED_AREA

  13. .get_unmapped_area = get_fb_unmapped_area,

  14. #endif

  15. #ifdef CONFIG_FB_DEFERRED_IO

  16. .fsync = fb_deferred_io_fsync,

  17. #endif

  18. };

这里有读写控制,打开等操作,我们首先肯定是打开,先看打开:

 
  1. static int

  2. fb_open(struct inode *inode, struct file *file)

  3. __acquires(&info->lock)

  4. __releases(&info->lock)

  5. {

  6. int fbidx = iminor(inode);

  7. struct fb_info *info;

  8. int res = 0;

  9.  
  10. if (fbidx >= FB_MAX)

  11. return -ENODEV;

  12. info = registered_fb[fbidx]; //这里从数组传入一个info

  13. if (!info)

  14. request_module("fb%d", fbidx);

  15. info = registered_fb[fbidx];

  16. if (!info)

  17. return -ENODEV;

  18. mutex_lock(&info->lock);

  19. if (!try_module_get(info->fbops->owner)) {

  20. res = -ENODEV;

  21. goto out;

  22. }

  23. file->private_data = info;

  24. if (info->fbops->fb_open) {

  25. res = info->fbops->fb_open(info,1); //调用info的打开函数

  26. if (res)

  27. module_put(info->fbops->owner);

  28. }

  29. #ifdef CONFIG_FB_DEFERRED_IO

  30. if (info->fbdefio)

  31. fb_deferred_io_open(info, inode, file);

  32. #endif

  33. out:

  34. mutex_unlock(&info->lock);

  35. return res;

  36. }

可以知道他首先从registered_fb获取一个info,再调用info的open,再看read:

 
  1. static ssize_t

  2. fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)

  3. {

  4. unsigned long p = *ppos;

  5. struct inode *inode = file->f_path.dentry->d_inode;

  6. int fbidx = iminor(inode);

  7. struct fb_info *info = registered_fb[fbidx];//熟悉吧,我们又看到这个数组了

  8. u32 *buffer, *dst;

  9. u32 __iomem *src;

  10. int c, i, cnt = 0, err = 0;

  11. unsigned long total_size;

  12.  
  13. if (!info || ! info->screen_base)//跟那个数组有关

  14. return -ENODEV;

  15.  
  16. if (info->state != FBINFO_STATE_RUNNING)//跟那个数组有关

  17. return -EPERM;

  18.  
  19. if (info->fbops->fb_read)//如果操作函数集里定义了read函数,就调用,否则就算了

  20. return info->fbops->fb_read(info, buf, count, ppos);

  21.  
  22. total_size = info->screen_size;//跟那个数组有关

  23.  
  24. if (total_size == 0)

  25. total_size = info->fix.smem_len;//跟那个数组有关

  26.  
  27. if (p >= total_size)

  28. return 0;

  29.  
  30. if (count >= total_size)

  31. count = total_size;

  32.  
  33. if (count + p > total_size)

  34. count = total_size - p;

  35.  
  36. buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,//开辟一个buffer

  37. GFP_KERNEL);

  38. if (!buffer)

  39. return -ENOMEM;

  40.  
  41. src = (u32 __iomem *) (info->screen_base + p);//显存基地址也在里面

  42.  
  43. if (info->fbops->fb_sync)

  44. info->fbops->fb_sync(info);

  45.  
  46. while (count) {

  47. c = (count > PAGE_SIZE) ? PAGE_SIZE : count;

  48. dst = buffer;//目的指针指向一个buffer(我们在上面开辟的)

  49. for (i = c >> 2; i--; )

  50. *dst++ = fb_readl(src++);//从基地址读取数据放进buffer中

  51. if (c & 3) {

  52. u8 *dst8 = (u8 *) dst;

  53. u8 __iomem *src8 = (u8 __iomem *) src;

  54.  
  55. for (i = c & 3; i--;)

  56. *dst8++ = fb_readb(src8++);

  57.  
  58. src = (u32 __iomem *) src8;

  59. }

  60.  
  61. if (copy_to_user(buf, buffer, c))//将buffer中的数据拷贝到用户空间,这样在用户空间调用read函数时就把显存内容读出来了

  62. {

  63. err = -EFAULT;

  64. break;

  65. }

  66. *ppos += c;

  67. buf += c;

  68. cnt += c;

  69. count -= c;

  70. }

  71.  
  72. kfree(buffer);

  73.  
  74. return (err) ? err : cnt;

  75. }

读函数就是读info里面显存存放的数据,可以知道这个info很重要,那么registered_fb里面放的info,怎么来的?

 
  1. int

  2. register_framebuffer(struct fb_info *fb_info)

  3. {

  4. int i;

  5. struct fb_event event;

  6. struct fb_videomode mode;

  7.  
  8. if (num_registered_fb == FB_MAX)

  9. return -ENXIO;

  10.  
  11. if (fb_check_foreignness(fb_info))

  12. return -ENOSYS;

  13.  
  14. /* check all firmware fbs and kick off if the base addr overlaps */

  15. for (i = 0 ; i < FB_MAX; i++) {

  16. if (!registered_fb[i])

  17. continue;

  18.  
  19. if (registered_fb[i]->flags & FBINFO_MISC_FIRMWARE) {

  20. if (fb_do_apertures_overlap(registered_fb[i], fb_info)) {

  21. printk(KERN_ERR "fb: conflicting fb hw usage "

  22. "%s vs %s - removing generic driver\n",

  23. fb_info->fix.id,

  24. registered_fb[i]->fix.id);

  25. unregister_framebuffer(registered_fb[i]);

  26. break;

  27. }

  28. }

  29. }

  30.  
  31. num_registered_fb++;

  32. for (i = 0 ; i < FB_MAX; i++)

  33. if (!registered_fb[i])

  34. break;

  35. fb_info->node = i;

  36. mutex_init(&fb_info->lock);

  37. mutex_init(&fb_info->mm_lock);

  38.  
  39. fb_info->dev = device_create(fb_class, fb_info->device,

  40. MKDEV(FB_MAJOR, i), NULL, "fb%d", i);

  41. if (IS_ERR(fb_info->dev)) {

  42. /* Not fatal */

  43. printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));

  44. fb_info->dev = NULL;

  45. } else

  46. fb_init_device(fb_info);

  47.  
  48. if (fb_info->pixmap.addr == NULL) {

  49. fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);

  50. if (fb_info->pixmap.addr) {

  51. fb_info->pixmap.size = FBPIXMAPSIZE;

  52. fb_info->pixmap.buf_align = 1;

  53. fb_info->pixmap.scan_align = 1;

  54. fb_info->pixmap.access_align = 32;

  55. fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;

  56. }

  57. }

  58. fb_info->pixmap.offset = 0;

  59.  
  60. if (!fb_info->pixmap.blit_x)

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

  62.  
  63. if (!fb_info->pixmap.blit_y)

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

  65.  
  66. if (!fb_info->modelist.prev || !fb_info->modelist.next)

  67. INIT_LIST_HEAD(&fb_info->modelist);

  68.  
  69. fb_var_to_videomode(&mode, &fb_info->var);

  70. fb_add_videomode(&mode, &fb_info->modelist);

  71. registered_fb[i] = fb_info; //这里放入info到registered_fb

  72.  
  73. event.info = fb_info;

  74. if (!lock_fb_info(fb_info))

  75. return -ENODEV;

  76. fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);

  77. unlock_fb_info(fb_info);

  78. return 0;

  79. }

可以知道是通过register_framebuffer这个函数放进去的,register_framebuffer又是谁调用的?搜索代码可以知道很多文件都有调用,看S3c2410fb.c可以看到这个在s3c24xxfb_probe里面被调用的,而s3c24xxfb_probe就是s3c24xx这种CPU的LCD驱动程序,这里挂到虚拟总线上了:

 
  1. static struct platform_driver s3c2410fb_driver = {

  2. .probe = s3c2410fb_probe,

  3. .remove = s3c2410fb_remove,

  4. .suspend = s3c2410fb_suspend,

  5. .resume = s3c2410fb_resume,

  6. .driver = {

  7. .name = "s3c2410-lcd",

  8. .owner = THIS_MODULE,

  9. },

  10. };

  11.  
  12. int __init s3c2410fb_init(void)

  13. {

  14. int ret = platform_driver_register(&s3c2410fb_driver);

  15.  
  16. if (ret == 0)

  17. ret = platform_driver_register(&s3c2412fb_driver);

  18.  
  19. return ret;

  20. }

  21.  
  22. static void __exit s3c2410fb_cleanup(void)

  23. {

  24. platform_driver_unregister(&s3c2410fb_driver);

  25. platform_driver_unregister(&s3c2412fb_driver);

  26. }

  27.  
  28. module_init(s3c2410fb_init);

  29. module_exit(s3c2410fb_cleanup);

  30.  
  31. MODULE_AUTHOR("Arnaud Patard <[email protected]>, "

  32. "Ben Dooks <[email protected]>");

  33. MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");

  34. MODULE_LICENSE("GPL");

  35. MODULE_ALIAS("platform:s3c2410-lcd");

  36. MODULE_ALIAS("platform:s3c2412-lcd");

所以可以知道最开始看的那个fbmem.c就是LCD驱动的driver端(叫做FrameBuffer驱动),而这边跟平台有关的S3c2410fb.c等就是LCD驱动的divice端,这个跟之前分析的输入子系统很类似,也是分离的思想,把成熟的软件框架与硬件平台分开,软件框架(driver端)已经帮我们实现了,我们只需要编写divece端,最后把device注册到driver端就可以了。下面看看如何编写device端:

从S3c2410fb.c可以知道我们的LCD驱动就是按要求设置好这个info,然后注册到LCD驱动核心层。这里为了分析思路的清晰,先不考虑虚拟总线,我们自己写一个驱动照着填好这个info,然后设置好跟LCD有关的寄存器就差不多了,然后注册到LCD驱动核心。

1 先把框架写好:

 
  1. #include <linux/module.h>

  2. #include <linux/kernel.h>

  3. #include <linux/errno.h>

  4. #include <linux/string.h>

  5. #include <linux/mm.h>

  6. #include <linux/slab.h>

  7. #include <linux/delay.h>

  8. #include <linux/fb.h>

  9. #include <linux/init.h>

  10. #include <linux/dma-mapping.h>

  11. #include <linux/interrupt.h>

  12. #include <linux/workqueue.h>

  13. #include <linux/wait.h>

  14. #include <linux/platform_device.h>

  15. #include <linux/clk.h>

  16.  
  17. #include <asm/io.h>

  18. #include <asm/uaccess.h>

  19. #include <asm/div64.h>

  20.  
  21. #include <asm/mach/map.h>

  22. #include <asm/arch/regs-lcd.h>

  23. #include <asm/arch/regs-gpio.h>

  24. #include <asm/arch/fb.h>

  25.  
  26.  
  27. static struct fb_info *s3c_lcd;

  28.  
  29. static int lcd_init(void)

  30. {

  31. /* 1. 分配一个fb_info */

  32. s3c_lcd = framebuffer_alloc(0, NULL);

  33.  
  34. /* 2. 设置 */

  35. /* 2.1 设置固定的参数 fix */

  36. /* 2.2 设置可变的参数 var */

  37. /* 2.3 设置操作函数 fbops*/

  38. /* 2.4 其他的设置 */

  39.  
  40. /* 3. 硬件相关的操作 */

  41. /* 3.1 配置GPIO用于LCD */

  42. /* 3.2 根据LCD手册设置LCD控制器, 比如VCLK的频率等 */

  43. /* 3.3 分配显存(framebuffer), 并把地址告诉LCD控制器 */

  44.  
  45. /* 4. 注册 */

  46. register_framebuffer(s3c_lcd);

  47.  
  48. return 0;

  49. }

  50.  
  51. static void lcd_exit(void)

  52. {

  53. }

  54.  
  55. module_init(lcd_init);

  56. module_exit(lcd_exit);

  57.  
  58. MODULE_LICENSE("GPL");

  59.  

看下这个info哪些有必要设置:

 
  1. struct fb_info {

  2. int node;

  3. int flags;

  4. struct mutex lock; /* Lock for open/release/ioctl funcs */

  5. struct mutex mm_lock; /* Lock for fb_mmap and smem_* fields */

  6. struct fb_var_screeninfo var; /* Current var */

  7. struct fb_fix_screeninfo fix; /* Current fix */

  8. struct fb_monspecs monspecs; /* Current Monitor specs */

  9. struct work_struct queue; /* Framebuffer event queue */

  10. struct fb_pixmap pixmap; /* Image hardware mapper */

  11. struct fb_pixmap sprite; /* Cursor hardware mapper */

  12. struct fb_cmap cmap; /* Current cmap */

  13. struct list_head modelist; /* mode list */

  14. struct fb_videomode *mode; /* current mode */

  15.  
  16. #ifdef CONFIG_FB_BACKLIGHT

  17. /* assigned backlight device */

  18. /* set before framebuffer registration,

  19. remove after unregister */

  20. struct backlight_device *bl_dev;

  21.  
  22. /* Backlight level curve */

  23. struct mutex bl_curve_mutex;

  24. u8 bl_curve[FB_BACKLIGHT_LEVELS];

  25. #endif

  26. #ifdef CONFIG_FB_DEFERRED_IO

  27. struct delayed_work deferred_work;

  28. struct fb_deferred_io *fbdefio;

  29. #endif

  30.  
  31. struct fb_ops *fbops;

  32. struct device *device; /* This is the parent */

  33. struct device *dev; /* This is this fb device */

  34. int class_flag; /* private sysfs flags */

  35. #ifdef CONFIG_FB_TILEBLITTING

  36. struct fb_tile_ops *tileops; /* Tile Blitting */

  37. #endif

  38. char __iomem *screen_base; /* Virtual address */

  39. unsigned long screen_size; /* Amount of ioremapped VRAM or 0 */

  40. void *pseudo_palette; /* Fake palette of 16 colors */

  41. #define FBINFO_STATE_RUNNING 0

  42. #define FBINFO_STATE_SUSPENDED 1

  43. u32 state; /* Hardware state i.e suspend */

  44. void *fbcon_par; /* fbcon use-only private area */

  45. /* From here on everything is device dependent */

  46. void *par;

  47. /* we need the PCI or similiar aperture base/size not

  48. smem_start/size as smem_start may just be an object

  49. allocated inside the aperture so may not actually overlap */

  50. resource_size_t aperture_base;

  51. resource_size_t aperture_size;

  52. };

2 然后来看下info的设置:

 
  1. #include <linux/module.h>

  2. #include <linux/kernel.h>

  3. #include <linux/errno.h>

  4. #include <linux/string.h>

  5. #include <linux/mm.h>

  6. #include <linux/slab.h>

  7. #include <linux/delay.h>

  8. #include <linux/fb.h>

  9. #include <linux/init.h>

  10. #include <linux/dma-mapping.h>

  11. #include <linux/interrupt.h>

  12. #include <linux/workqueue.h>

  13. #include <linux/wait.h>

  14. #include <linux/platform_device.h>

  15. #include <linux/clk.h>

  16.  
  17. #include <asm/io.h>

  18. #include <asm/uaccess.h>

  19. #include <asm/div64.h>

  20.  
  21. #include <asm/mach/map.h>

  22. #include <asm/arch/regs-lcd.h>

  23. #include <asm/arch/regs-gpio.h>

  24. #include <asm/arch/fb.h>

  25.  
  26. //对显存的一些操作函数:

  27. static struct fb_ops s3c_lcdfb_ops = {

  28. .owner = THIS_MODULE,

  29. // .fb_setcolreg = atmel_lcdfb_setcolreg,

  30. .fb_fillrect = cfb_fillrect, //这三个照着写,以后再分析

  31. .fb_copyarea = cfb_copyarea,

  32. .fb_imageblit = cfb_imageblit,

  33. };

  34.  
  35.  
  36. static struct fb_info *s3c_lcd;

  37.  
  38. static int lcd_init(void)

  39. {

  40. /* 1. 分配一个fb_info */

  41. s3c_lcd = framebuffer_alloc(0, NULL);

  42.  
  43. /* 2. 设置 */

  44. /* 2.1 设置固定的参数 */

  45. strcpy(s3c_lcd->fix.id, "mylcd"); //名称

  46. s3c_lcd->fix.smem_len = 240*320*16/8; //显存大小,16bpp所以16位表示一个像素点,240x320的屏幕有240x320个点,再x16就是总位数,再/8就是总字节数

  47. s3c_lcd->fix.type = FB_TYPE_PACKED_PIXELS;

  48. s3c_lcd->fix.visual = FB_VISUAL_TRUECOLOR; /* TFT 真彩色*/

  49. s3c_lcd->fix.line_length = 240*2; //一行的长度,一行240个点,每个点两个字节(16bit),所以*2

  50.  
  51. /* 2.2 设置可变的参数 */

  52. s3c_lcd->var.xres = 240; //行

  53. s3c_lcd->var.yres = 320; //列

  54. s3c_lcd->var.xres_virtual = 240; //这里设置虚拟屏,我们设置虚拟屏为一样大小

  55. s3c_lcd->var.yres_virtual = 320;

  56. s3c_lcd->var.bits_per_pixel = 16; //每个像素16位

  57.  
  58. /* RGB:565 */

  59. s3c_lcd->var.red.offset = 11; //16位表示一个像素点的格式是R:G:B分别占5:6:5位

  60. s3c_lcd->var.red.length = 5;

  61.  
  62. s3c_lcd->var.green.offset = 5;

  63. s3c_lcd->var.green.length = 6;

  64.  
  65. s3c_lcd->var.blue.offset = 0;

  66. s3c_lcd->var.blue.length = 5;

  67.  
  68. s3c_lcd->var.activate = FB_ACTIVATE_NOW;

  69.  
  70.  
  71. /* 2.3 设置操作函数 */

  72. s3c_lcd->fbops = &s3c_lcdfb_ops; //对显存的操作函数,透明处理等操作

  73.  
  74. /* 2.4 其他的设置 */

  75. //s3c_lcd->pseudo_palette =; //

  76. //s3c_lcd->screen_base = ; /* 显存的虚拟地址 */

  77. s3c_lcd->screen_size = 240*324*16/8; //和显存大小一样设置

  78.  
  79. /* 3. 硬件相关的操作 */

  80. /* 3.1 配置GPIO用于LCD */

  81. /* 3.2 根据LCD手册设置LCD控制器, 比如VCLK的频率等 */

  82. /* 3.3 分配显存(framebuffer), 并把地址告诉LCD控制器 */

  83. //s3c_lcd->fix.smem_start = xxx; /* 显存的物理地址 */

  84.  
  85. /* 4. 注册 */

  86. register_framebuffer(s3c_lcd);

  87.  
  88. return 0;

  89. }

  90.  
  91. static void lcd_exit(void)

  92. {

  93. }

  94.  
  95. module_init(lcd_init);

  96. module_exit(lcd_exit);

  97.  
  98. MODULE_LICENSE("GPL");

  99.  

3  info一般设置这些东西,再看些硬件方面的设置:

 
  1. #include <linux/module.h>

  2. #include <linux/kernel.h>

  3. #include <linux/errno.h>

  4. #include <linux/string.h>

  5. #include <linux/mm.h>

  6. #include <linux/slab.h>

  7. #include <linux/delay.h>

  8. #include <linux/fb.h>

  9. #include <linux/init.h>

  10. #include <linux/dma-mapping.h>

  11. #include <linux/interrupt.h>

  12. #include <linux/workqueue.h>

  13. #include <linux/wait.h>

  14. #include <linux/platform_device.h>

  15. #include <linux/clk.h>

  16.  
  17. #include <asm/io.h>

  18. #include <asm/uaccess.h>

  19. #include <asm/div64.h>

  20.  
  21. #include <asm/mach/map.h>

  22. #include <asm/arch/regs-lcd.h>

  23. #include <asm/arch/regs-gpio.h>

  24. #include <asm/arch/fb.h>

  25.  
  26. static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,

  27. unsigned int green, unsigned int blue,

  28. unsigned int transp, struct fb_info *info);

  29.  
  30.  
  31. struct lcd_regs {

  32. unsigned long lcdcon1;

  33. unsigned long lcdcon2;

  34. unsigned long lcdcon3;

  35. unsigned long lcdcon4;

  36. unsigned long lcdcon5;

  37. unsigned long lcdsaddr1;

  38. unsigned long lcdsaddr2;

  39. unsigned long lcdsaddr3;

  40. unsigned long redlut;

  41. unsigned long greenlut;

  42. unsigned long bluelut;

  43. unsigned long reserved[9];

  44. unsigned long dithmode;

  45. unsigned long tpal;

  46. unsigned long lcdintpnd;

  47. unsigned long lcdsrcpnd;

  48. unsigned long lcdintmsk;

  49. unsigned long lpcsel;

  50. };

  51.  
  52. static struct fb_ops s3c_lcdfb_ops = {

  53. .owner = THIS_MODULE,

  54. .fb_setcolreg = s3c_lcdfb_setcolreg,

  55. .fb_fillrect = cfb_fillrect,

  56. .fb_copyarea = cfb_copyarea,

  57. .fb_imageblit = cfb_imageblit,

  58. };

  59.  
  60.  
  61. static struct fb_info *s3c_lcd;

  62. static volatile unsigned long *gpbcon;

  63. static volatile unsigned long *gpbdat;

  64. static volatile unsigned long *gpccon;

  65. static volatile unsigned long *gpdcon;

  66. static volatile unsigned long *gpgcon;

  67. static volatile struct lcd_regs* lcd_regs;

  68. static u32 pseudo_palette[16];

  69.  
  70.  
  71. /* from pxafb.c */

  72. static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)

  73. {

  74. chan &= 0xffff;

  75. chan >>= 16 - bf->length;

  76. return chan << bf->offset;

  77. }

  78.  
  79.  
  80. static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,

  81. unsigned int green, unsigned int blue,

  82. unsigned int transp, struct fb_info *info)

  83. {

  84. unsigned int val;

  85.  
  86. if (regno > 16)

  87. return 1;

  88.  
  89. /* 用red,green,blue三原色构造出val */

  90. val = chan_to_field(red, &info->var.red);

  91. val |= chan_to_field(green, &info->var.green);

  92. val |= chan_to_field(blue, &info->var.blue);

  93.  
  94. //((u32 *)(info->pseudo_palette))[regno] = val;

  95. pseudo_palette[regno] = val;

  96. return 0;

  97. }

  98.  
  99. static int lcd_init(void)

  100. {

  101. /* 1. 分配一个fb_info */

  102. s3c_lcd = framebuffer_alloc(0, NULL);

  103.  
  104. /* 2. 设置 */

  105. /* 2.1 设置固定的参数 */

  106. strcpy(s3c_lcd->fix.id, "mylcd");

  107. s3c_lcd->fix.smem_len = 240*320*16/8;

  108. s3c_lcd->fix.type = FB_TYPE_PACKED_PIXELS;

  109. s3c_lcd->fix.visual = FB_VISUAL_TRUECOLOR; /* TFT */

  110. s3c_lcd->fix.line_length = 240*2;

  111.  
  112. /* 2.2 设置可变的参数 */

  113. s3c_lcd->var.xres = 240;

  114. s3c_lcd->var.yres = 320;

  115. s3c_lcd->var.xres_virtual = 240;

  116. s3c_lcd->var.yres_virtual = 320;

  117. s3c_lcd->var.bits_per_pixel = 16;

  118.  
  119. /* RGB:565 */

  120. s3c_lcd->var.red.offset = 11;

  121. s3c_lcd->var.red.length = 5;

  122.  
  123. s3c_lcd->var.green.offset = 5;

  124. s3c_lcd->var.green.length = 6;

  125.  
  126. s3c_lcd->var.blue.offset = 0;

  127. s3c_lcd->var.blue.length = 5;

  128.  
  129. s3c_lcd->var.activate = FB_ACTIVATE_NOW;

  130.  
  131.  
  132. /* 2.3 设置操作函数 */

  133. s3c_lcd->fbops = &s3c_lcdfb_ops;

  134.  
  135. /* 2.4 其他的设置 */

  136. s3c_lcd->pseudo_palette = pseudo_palette;

  137. //s3c_lcd->screen_base = ; /* 显存的虚拟地址 */

  138. s3c_lcd->screen_size = 240*324*16/8;

  139.  
  140. /* 3. 硬件相关的操作 */

  141. /* 3.1 配置GPIO用于LCD */

  142. gpbcon = ioremap(0x56000010, 8);

  143. gpbdat = gpbcon+1;

  144. gpccon = ioremap(0x56000020, 4);

  145. gpdcon = ioremap(0x56000030, 4);

  146. gpgcon = ioremap(0x56000060, 4);

  147.  
  148. *gpccon = 0xaaaaaaaa; /* GPIO管脚用于VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND */

  149. *gpdcon = 0xaaaaaaaa; /* GPIO管脚用于VD[23:8] */

  150.  
  151. *gpbcon &= ~(3); /* GPB0设置为输出引脚 */

  152. *gpbcon |= 1;

  153. *gpbdat &= ~1; /* 输出低电平 */

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

  156.  
  157. /* 3.2 根据LCD手册设置LCD控制器, 比如VCLK的频率等 */

  158. lcd_regs = ioremap(0x4D000000, sizeof(struct lcd_regs));

  159.  
  160. /* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手册P14

  161. * 10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2]

  162. * CLKVAL = 4

  163. * bit[6:5]: 0b11, TFT LCD

  164. * bit[4:1]: 0b1100, 16 bpp for TFT

  165. * bit[0] : 0 = Disable the video output and the LCD control signal.

  166. */

  167. lcd_regs->lcdcon1 = (4<<8) | (3<<5) | (0x0c<<1);

  168.  
  169.  
  170. /* 垂直方向的时间参数

  171. * bit[31:24]: VBPD, VSYNC之后再过多长时间才能发出第1行数据

  172. * LCD手册 T0-T2-T1=4

  173. * VBPD=3

  174. * bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319

  175. * bit[13:6] : VFPD, 发出最后一行数据之后,再过多长时间才发出VSYNC

  176. * LCD手册T2-T5=322-320=2, 所以VFPD=2-1=1

  177. * bit[5:0] : VSPW, VSYNC信号的脉冲宽度, LCD手册T1=1, 所以VSPW=1-1=0

  178. */

  179. lcd_regs->lcdcon2 = (3<<24) | (319<<14) | (1<<6) | (0<<0);

  180.  
  181.  
  182. /* 水平方向的时间参数

  183. * bit[25:19]: HBPD, VSYNC之后再过多长时间才能发出第1行数据

  184. * LCD手册 T6-T7-T8=17

  185. * HBPD=16

  186. * bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239

  187. * bit[7:0] : HFPD, 发出最后一行里最后一个象素数据之后,再过多长时间才发出HSYNC

  188. * LCD手册T8-T11=251-240=11, 所以HFPD=11-1=10

  189. */

  190. lcd_regs->lcdcon3 = (16<<19) | (239<<8) | (10<<0);

  191.  
  192. /* 水平方向的同步信号

  193. * bit[7:0] : HSPW, HSYNC信号的脉冲宽度, LCD手册T7=5, 所以HSPW=5-1=4

  194. */

  195. lcd_regs->lcdcon4 = 4;

  196.  
  197.  
  198. /* 信号的极性

  199. * bit[11]: 1=565 format

  200. * bit[10]: 0 = The video data is fetched at VCLK falling edge

  201. * bit[9] : 1 = HSYNC信号要反转,即低电平有效

  202. * bit[8] : 1 = VSYNC信号要反转,即低电平有效

  203. * bit[6] : 0 = VDEN不用反转

  204. * bit[3] : 0 = PWREN输出0

  205. * bit[1] : 0 = BSWP

  206. * bit[0] : 1 = HWSWP 2440手册P413

  207. */

  208. lcd_regs->lcdcon5 = (1<<11) | (0<<10) | (1<<9) | (1<<8) | (1<<0);

  209.  
  210. /* 3.3 分配显存(framebuffer), 并把地址告诉LCD控制器 */

  211. s3c_lcd->screen_base = dma_alloc_writecombine(NULL, s3c_lcd->fix.smem_len, &s3c_lcd->fix.smem_start, GFP_KERNEL);

  212.  
  213. lcd_regs->lcdsaddr1 = (s3c_lcd->fix.smem_start >> 1) & ~(3<<30);

  214. lcd_regs->lcdsaddr2 = ((s3c_lcd->fix.smem_start + s3c_lcd->fix.smem_len) >> 1) & 0x1fffff;

  215. lcd_regs->lcdsaddr3 = (240*16/16); /* 一行的长度(单位: 2字节) */

  216.  
  217. //s3c_lcd->fix.smem_start = xxx; /* 显存的物理地址 */

  218. /* 启动LCD */

  219. lcd_regs->lcdcon1 |= (1<<0); /* 使能LCD控制器 */

  220. lcd_regs->lcdcon5 |= (1<<3); /* 使能LCD本身 */

  221. *gpbdat |= 1; /* 输出高电平, 使能背光 */

  222.  
  223. /* 4. 注册 */

  224. register_framebuffer(s3c_lcd);

  225.  
  226. return 0;

  227. }

  228.  
  229. static void lcd_exit(void)

  230. {

  231. unregister_framebuffer(s3c_lcd);

  232. lcd_regs->lcdcon1 &= ~(1<<0); /* 关闭LCD本身 */

  233. *gpbdat &= ~1; /* 关闭背光 */

  234. dma_free_writecombine(NULL, s3c_lcd->fix.smem_len, s3c_lcd->screen_base, s3c_lcd->fix.smem_start);

  235. iounmap(lcd_regs);

  236. iounmap(gpbcon);

  237. iounmap(gpccon);

  238. iounmap(gpdcon);

  239. iounmap(gpgcon);

  240. framebuffer_release(s3c_lcd);

  241. }

  242.  
  243. module_init(lcd_init);

  244. module_exit(lcd_exit);

  245.  
  246. MODULE_LICENSE("GPL");

猜你喜欢

转载自blog.csdn.net/wlf_go/article/details/82258522