JZ2440_V3版 4.3寸LCD驱动 - 2(编写测试驱动)

本代码来自于韦东山老师嵌入式二期驱动视频所讲解,仅供学习参考。如有侵权等行为,可当即撤销本文章。更多信息请关注www.100ask.com!!!

s3c2440_V3_4.3 lcd.c
  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. #include <asm/io.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/div64.h>
  19. #include <asm/mach/map.h>
  20. #include <asm/arch/regs-lcd.h>
  21. #include <asm/arch/regs-gpio.h>
  22. #include <asm/arch/fb.h>
  23. static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
  24. unsigned int green, unsigned int blue,
  25. unsigned int transp, struct fb_info *info);
  26. struct lcd_regs {
  27. unsigned long lcdcon1;
  28. unsigned long lcdcon2;
  29. unsigned long lcdcon3;
  30. unsigned long lcdcon4;
  31. unsigned long lcdcon5;
  32. unsigned long lcdsaddr1;
  33. unsigned long lcdsaddr2;
  34. unsigned long lcdsaddr3;
  35. unsigned long redlut;
  36. unsigned long greenlut;
  37. unsigned long bluelut;
  38. unsigned long reserved[9];
  39. unsigned long dithmode;
  40. unsigned long tpal;
  41. unsigned long lcdintpnd;
  42. unsigned long lcdsrcpnd;
  43. unsigned long lcdintmsk;
  44. unsigned long lpcsel;
  45. };
  46. static struct fb_ops s3c_lcdfb_ops = {
  47. .owner = THIS_MODULE,
  48. .fb_setcolreg = s3c_lcdfb_setcolreg,
  49. .fb_fillrect = cfb_fillrect,
  50. .fb_copyarea = cfb_copyarea,
  51. .fb_imageblit = cfb_imageblit,
  52. };
  53. static struct fb_info *s3c_lcd;
  54. static volatile unsigned long *gpbcon;
  55. static volatile unsigned long *gpbdat;
  56. static volatile unsigned long *gpccon;
  57. static volatile unsigned long *gpdcon;
  58. static volatile unsigned long *gpgcon;
  59. static volatile struct lcd_regs* lcd_regs;
  60. static u32 pseudo_palette[16];
  61. /* from pxafb.c */
  62. static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
  63. {
  64. chan &= 0xffff;
  65. chan >>= 16 - bf->length;
  66. return chan << bf->offset;
  67. }
  68. static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
  69. unsigned int green, unsigned int blue,
  70. unsigned int transp, struct fb_info *info)
  71. {
  72. unsigned int val;
  73. if (regno > 16)
  74. return 1;
  75. /* 用red,green,blue三原色构造出val */
  76. val = chan_to_field(red, &info->var.red);
  77. val |= chan_to_field(green, &info->var.green);
  78. val |= chan_to_field(blue, &info->var.blue);
  79. //((u32 *)(info->pseudo_palette))[regno] = val;
  80. pseudo_palette[regno] = val;
  81. return 0;
  82. }
  83. static int lcd_init(void)
  84. {
  85. /* 1. 分配一个fb_info */
  86. s3c_lcd = framebuffer_alloc(0, NULL);
  87. /* 2. 设置 */
  88. /* 2.1 设置固定的参数 */
  89. strcpy(s3c_lcd->fix.id, "mylcd");
  90. s3c_lcd->fix.smem_len = 480*272*16/8;
  91. s3c_lcd->fix.type = FB_TYPE_PACKED_PIXELS;
  92. s3c_lcd->fix.visual = FB_VISUAL_TRUECOLOR; /* TFT */
  93. s3c_lcd->fix.line_length = 480*2;
  94. /* 2.2 设置可变的参数 */
  95. s3c_lcd->var.xres = 480;
  96. s3c_lcd->var.yres = 272;
  97. s3c_lcd->var.xres_virtual = 480;
  98. s3c_lcd->var.yres_virtual = 272;
  99. s3c_lcd->var.bits_per_pixel = 16;
  100. /* RGB:565 */
  101. s3c_lcd->var.red.offset = 11;
  102. s3c_lcd->var.red.length = 5;
  103. s3c_lcd->var.green.offset = 5;
  104. s3c_lcd->var.green.length = 6;
  105. s3c_lcd->var.blue.offset = 0;
  106. s3c_lcd->var.blue.length = 5;
  107. s3c_lcd->var.activate = FB_ACTIVATE_NOW;
  108. /* 2.3 设置操作函数 */
  109. s3c_lcd->fbops = &s3c_lcdfb_ops;
  110. /* 2.4 其他的设置 */
  111. s3c_lcd->pseudo_palette = pseudo_palette;
  112. //s3c_lcd->screen_base = ; /* 显存的虚拟地址 */
  113. s3c_lcd->screen_size = 480*272*16/8;
  114. /* 3. 硬件相关的操作 */
  115. /* 3.1 配置GPIO用于LCD */
  116. gpbcon = ioremap(0x56000010, 8);
  117. gpbdat = gpbcon+1;
  118. gpccon = ioremap(0x56000020, 4);
  119. gpdcon = ioremap(0x56000030, 4);
  120. gpgcon = ioremap(0x56000060, 4);
  121. *gpccon = 0xaaaaaaaa; /* GPIO管脚用于VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND */
  122. *gpdcon = 0xaaaaaaaa; /* GPIO管脚用于VD[23:8] */
  123. *gpbcon &= ~(3); /* GPB0设置为输出引脚 */
  124. *gpbcon |= 1;
  125. *gpbdat &= ~1; /* 输出低电平 */
  126. *gpgcon |= (3<<8); /* GPG4用作LCD_PWREN */
  127. /* 3.2 根据LCD手册设置LCD控制器, 比如VCLK的频率等 */
  128. lcd_regs = ioremap(0x4D000000, sizeof(struct lcd_regs));
  129. /* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手册P14
  130. * 10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2]
  131. * CLKVAL = 4
  132. * bit[6:5]: 0b11, TFT LCD
  133. * bit[4:1]: 0b1100, 16 bpp for TFT
  134. * bit[0] : 0 = Disable the video output and the LCD control signal.
  135. */
  136. lcd_regs->lcdcon1 = (4<<8) | (3<<5) | (0x0c<<1);
  137. #if 1
  138. /* 垂直方向的时间参数
  139. * bit[31:24]: VBPD, VSYNC之后再过多长时间才能发出第1行数据
  140. * LCD手册 T0-T2-T1=4
  141. * VBPD=3
  142. * bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319
  143. * bit[13:6] : VFPD, 发出最后一行数据之后,再过多长时间才发出VSYNC
  144. * LCD手册T2-T5=322-320=2, 所以VFPD=2-1=1
  145. * bit[5:0] : VSPW, VSYNC信号的脉冲宽度, LCD手册T1=1, 所以VSPW=1-1=0
  146. */
  147. lcd_regs->lcdcon2 = (1<<24) | (271<<14) | (1<<6) | (9);
  148. /* 水平方向的时间参数
  149. * bit[25:19]: HBPD, VSYNC之后再过多长时间才能发出第1行数据
  150. * LCD手册 T6-T7-T8=17
  151. * HBPD=16
  152. * bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239
  153. * bit[7:0] : HFPD, 发出最后一行里最后一个象素数据之后,再过多长时间才发出HSYNC
  154. * LCD手册T8-T11=251-240=11, 所以HFPD=11-1=10
  155. */
  156. lcd_regs->lcdcon3 = (1<<19) | (479<<8) | (1);
  157. /* 水平方向的同步信号
  158. * bit[7:0] : HSPW, HSYNC信号的脉冲宽度, LCD手册T7=5, 所以HSPW=5-1=4
  159. */
  160. lcd_regs->lcdcon4 = 40;
  161. #else
  162. lcd_regs->lcdcon2 = S3C2410_LCDCON2_VBPD(5) | \
  163. S3C2410_LCDCON2_LINEVAL(319) | \
  164. S3C2410_LCDCON2_VFPD(3) | \
  165. S3C2410_LCDCON2_VSPW(1);
  166. lcd_regs->lcdcon3 = S3C2410_LCDCON3_HBPD(10) | \
  167. S3C2410_LCDCON3_HOZVAL(239) | \
  168. S3C2410_LCDCON3_HFPD(1);
  169. lcd_regs->lcdcon4 = S3C2410_LCDCON4_MVAL(13) | \
  170. S3C2410_LCDCON4_HSPW(0);
  171. #endif
  172. /* 信号的极性
  173. * bit[11]: 1=565 format
  174. * bit[10]: 0 = The video data is fetched at VCLK falling edge
  175. * bit[9] : 1 = HSYNC信号要反转,即低电平有效
  176. * bit[8] : 1 = VSYNC信号要反转,即低电平有效
  177. * bit[6] : 0 = VDEN不用反转
  178. * bit[3] : 0 = PWREN输出0
  179. * bit[1] : 0 = BSWP
  180. * bit[0] : 1 = HWSWP 2440手册P413
  181. */
  182. lcd_regs->lcdcon5 = (1<<11) | (0<<10) | (1<<9) | (1<<8) | (1<<0);
  183. /* 3.3 分配显存(framebuffer), 并把地址告诉LCD控制器 */
  184. s3c_lcd->screen_base = dma_alloc_writecombine(NULL, s3c_lcd->fix.smem_len, &s3c_lcd->fix.smem_start, GFP_KERNEL);
  185. lcd_regs->lcdsaddr1 = (s3c_lcd->fix.smem_start >> 1) & ~(3<<30);
  186. lcd_regs->lcdsaddr2 = ((s3c_lcd->fix.smem_start + s3c_lcd->fix.smem_len) >> 1) & 0x1fffff;
  187. lcd_regs->lcdsaddr3 = (480*16/16); /* 一行的长度(单位: 2字节) */
  188. //s3c_lcd->fix.smem_start = xxx; /* 显存的物理地址 */
  189. /* 启动LCD */
  190. lcd_regs->lcdcon1 |= (1<<0); /* 使能LCD控制器 */
  191. lcd_regs->lcdcon5 |= (1<<3); /* 使能LCD本身 */
  192. *gpbdat |= 1; /* 输出高电平, 使能背光 */
  193. /* 4. 注册 */
  194. register_framebuffer(s3c_lcd);
  195. return 0;
  196. }
  197. static void lcd_exit(void)
  198. {
  199. unregister_framebuffer(s3c_lcd);
  200. lcd_regs->lcdcon1 &= ~(1<<0); /* 关闭LCD本身 */
  201. *gpbdat &= ~1; /* 关闭背光 */
  202. dma_free_writecombine(NULL, s3c_lcd->fix.smem_len, s3c_lcd->screen_base, s3c_lcd->fix.smem_start);
  203. iounmap(lcd_regs);
  204. iounmap(gpbcon);
  205. iounmap(gpccon);
  206. iounmap(gpdcon);
  207. iounmap(gpgcon);
  208. framebuffer_release(s3c_lcd);
  209. }
  210. module_init(lcd_init);
  211. module_exit(lcd_exit);
  212. MODULE_LICENSE("GPL");
  1. 1. cd /work/system/linux-2.6.22.6,执行 make menuconfig命令.
  2. 2. 选择 -> Device Drivers
  3. 选择 -> Graphics support
  4. <M> S3C2410 LCD framebuffer support
  5. /*上面“<*> S3C2410 LCD framebuffer support”是之前自带的驱动程序,这里去掉后以<M>模块方式编译进内核.*/
  6. 3. 执行 config_ok .config
  7. 执行 make menuconfig (生成uImage文件)
  8. 执行 make modules (cfbcopyarea.ko,cfbfillrect.ko,cfbimgblt.ko)
  9. 4. cp arch/arm/boot/uImage /work/nfs_root/uImage_nolcd
  10. cp drivers/video cfb*.ko /work/nfs_root/first_fs
  11. 5. cd /work/driver_and _test/4th_4.3
  12. make(生成lcd.ko)
  13. cp lcd.ko /work/nfs_root/first_fs
  14. 6. insmod cfbcopyarea.ko
  15. insmod cfbfillrect.ko
  16. insmod cfbimgblt.ko
  17. insmod lcd.c


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值