1、LCD 背光控制原理
在 mini2440/micro2440 开发板中,LCD 背光是通过CPU 的LCD_PWR 引脚来控制的,从原理图中可以看出,它对应于GPG4,如图
当LCD_PWR 输出为高电平“1”时,将打开背光;当输出为低电平“0”时,将关闭背光(注意:这里只是打开和关闭背光,而并没有背光亮度的调节作用)。
2、在内核中添加背光驱动程序
增加一个简单的背光驱动,以便能够通过软件便可简单的控制背光的开关,在命令终端通过向背光设备发送“0”可关闭背光,发送“1”可打开背光,为了实现这点,我们在linux-2.6.32.2/drivers/video/backlight 目录增加一个mini2440_backlight.c文件,内容如下:
#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>
//定义背光驱动的名称为 backligh,将会出现在/dev/backlight
#define DEVICE_NAME "backlight"
extern void s3c2410_gpio_setpin(unsigned int pin, unsigned int to);
extern void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function);
static int mini2440_backlight_ioctl(struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd)
{
case 0:
//当接收的命令为0时,就将GPG4引脚设为低电平,关闭背光
s3c2410_gpio_setpin(S3C2410_GPG(4), 0);
printk(DEVICE_NAME " turn off!\n");
return 0;
case 1:
//当接收的命令为1时,就将GPG4引脚设为高电平,开启背光
s3c2410_gpio_setpin(S3C2410_GPG(4), 1);
printk(DEVICE_NAME " turn on!\n");
return 0;
default:
return -EINVAL;
}
}
//设备操作集
static struct file_operations dev_fops =
{
.owner = THIS_MODULE,
.ioctl = mini2440_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_GPG(4), S3C2410_GPIO_OUTPUT);
s3c2410_gpio_setpin(S3C2410_GPG(4), 1); //启动内核时打开背光
}
static void __exit dev_exit(void)
{
misc_deregister(&misc); //注销该misc设备
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("DreamCatcher");
MODULE_DESCRIPTION("Backlight control for mini2440");
3、把背光配置选项加入内核配置菜单
(1)、打开linux-2.6.32.2/drivers/video/backlight/Kconfig,定位到12行附近,加入如下代码
clx@think:/work/armlinux/linux-2.6.32.2/drivers/video/backlight$ gedit Kconfig
#
# Backlight & LCD drivers configuration
#
menuconfig BACKLIGHT_LCD_SUPPORT
bool "Backlight & LCD device support"
help
Enable this to be able to choose the drivers for controlling the
backlight and the LCD panel on some platforms, for example on PDAs.
config BACKLIGHT_MINI2440
tristate "Backlight support for mini2440 from FriendlyARM"
depends on BACKLIGHT_LCD_SUPPORT
default y
help
backlight driver for MINI2440 from FriendlyARM
#
# LCD
#
(2)、打开 linux-2.6.32.2/drivers/video/backlight/Makefile,定位到文件末尾,根据配置定义加入驱动目标文件,
obj-$(CONFIG_BACKLIGHT_ADP5520) += adp5520_bl.o
obj-$(CONFIG_BACKLIGHT_MINI2440) += mini2440_backlight.o
(3)、在内核配置菜单中确认 "Backlight support for mini2440 from FriendlyARM",被选中,默认是选中的。
Device Drivers --->
Graphics support --->
[*] Backlight & LCD device support --->
4、测试LCD背光
(1)、编写测试程序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_test 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_test on|off!\n");
exit(1);
}
//进行IO控制
ioctl(fd, turn);
//关闭背光设备
close(fd);
return 0;
}
在主机终端交叉编译,clx@think:/work/armlinux/codetest$ arm-linux-gcc -o backlight_test backlight_test.c
在跟文件系统中建立opt目录用来存放引用程序,将backlight_test拷贝到此目录下,用NFS挂载toorfs启动内核,运行此应用程序
root@MINI2440:/# cd opt
root@MINI2440:/opt# ls
backlight_test
root@MINI2440:/opt# ./backlight_test
Usage: backlight_test on|off!
root@MINI2440:/opt# ./backlight_test on
backlight turn on!
root@MINI2440:/opt# ./backlight_test off
backlight turn off!
root@MINI2440:/opt#