嵌入式Linux之我行——LCD背光驱动在2440上的实例开发
一、开发环境
二、实现步骤
1. 硬件原理图分析。由原理图得知LCD的背光是由2440的GPG4口控制的
#include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/input.h> #include <linux/init.h> #include <linux/serio.h> #include <linux/delay.h> #include <linux/clk.h> #include <linux/miscdevice.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/uaccess.h> #include <mach/regs-clock.h> #include <plat/regs-timer.h> #include <mach/regs-gpio.h> #include <linux/cdev.h> #define DEVICE_NAME "backlight" //设备名称 #define DEVICE_MINOR 5 //次设备号,这里我们将设备注册为misc设备,这种设备的主设备号都为10 static int my2440_backlight_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { switch(cmd) { case 0: //当接收的命令为0时,就将GPG4引脚设为低电平,关闭背光 s3c2410_gpio_setpin(S3C2410_GPG4, 0); printk(DEVICE_NAME " turn off!/n"); return 0; case 1: //当接收的命令为1时,就将GPG4引脚设为高电平,开启背光 s3c2410_gpio_setpin(S3C2410_GPG4, 1); printk(DEVICE_NAME " turn on!/n"); return 0; default: return -EINVAL; } } static struct file_operations dev_fops = { .owner = THIS_MODULE, .ioctl = my2440_backlight_ioctl, //这里只使用控制IO口的方式来控制背光 }; static struct miscdevice misc = { .minor = DEVICE_MINOR, .name = DEVICE_NAME, .fops = &dev_fops, }; static int __init dev_init(void) { int ret; ret = misc_register(&misc); //注册成misc设备 if(ret < 0) { printk("Register misc device fiald!"); return ret; } //将GPG4口配置成输出口 s3c2410_gpio_cfgpin(S3C2410_GPG4, S3C2410_GPG4_OUTP); return ret; } static void __exit dev_exit(void) { misc_deregister(&misc); //注销该misc设备 } module_init(dev_init); module_exit(dev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Huang Gang"); MODULE_DESCRIPTION("Backlight control for my2440");
将my2440_backlight.c复制到内核drivers/video/backlight/目录下
#cp -f my2440_backlight.c linux-2.6.30.4/drivers/video/backlight/修改backlight/目录下的Kconfig文件,添加背光选项
#gedit linux-2.6.30.4/drivers/video/backlight/Kconfig config MY2440_LCD_BACKLIGHT tristate "My2440 LCD Backlight Controls" depends on BACKLIGHT_LCD_SUPPORT default y
修改backlight/目录下的Makefile文件,在最后一行添加
#gedit linux-2.6.30.4/drivers/video/backlight/Makefile obj-$(CONFIG_MY2440_LCD_BACKLIGHT) += my2440_backlight.o
4. 配置内核选项,选中上面添加的配置项
Device Drivers ---> Graphics support ---> [*] Backlight & LCD device support ---> --- Backlight & LCD device support <*> My2440 LCD Backlight Controls (NEW) < > Lowlevel LCD controls < > Lowlevel Backlight controls
5. 重新编译内核并下载到开发板上,在开发板/dev/目录下建立设备的节点。设备名为backlight,主设备号为10,次设备号为5,在驱动源码里有注释
#mknod /dev/backlight c 10 5
6. 编写应用程序测试背光驱动,文件名为backlight_test.c
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/ioctl.h> int main(int argc, char **argv) { int turn; int fd; //检测命令后面带的参数 if(argc == 1 || argc > 2) { printf("Usage: backlight on|off!/n"); exit(1); } //打开背光设备 fd = open("/dev/backlight", O_RDWR); if(fd < 0) { printf("Open Backlight Device Faild!/n"); exit(1); } //判断输入的参数 if(strcmp(argv[1], "on") == 0) { turn = 1; } else if(strcmp(argv[1], "off") == 0) { turn = 0; } else { printf("Usage: backlight on|off!/n"); exit(1); } //进行IO控制 ioctl(fd, turn); //关闭背光设备 close(fd); return 0; }
7. 交叉编译应用程序,并下载到开发板的/usr/sbin/目录下
#arm-linux-gcc -o backlight_test backlight_test.c
8. 运行效果。执行应用程序时内核都会打印出背光状态信息,同时观察LCD的背光也得到了控制