LCD驱动移植之背光控制
程序员文章站
2022-03-22 14:44:34
...
移植环境
BootLoader:u-boot-1.1.6
kernel:linux-2.6.30.4
CPU:s3c2440
开发板:TQ2440
移植步骤
控制 LCD 背光的开关对于TQ2440 开发板来讲,就是控制S3C2440 的 LCD 控制的LCD_PWREN 脚,根据 S3C2440 的datasheet,可以知道在 LCDCON5 寄存器的 PWREN 位是控制 LCD 是否输出的,当为 0 时 LCD 不输出,此时 LCD_PWREN 脚为低,就会关闭 LCD 的背光;当为 1 时 LCD 输出,此时 LCD_PWREN 脚为高,开启 LCD 的背光。
1、编写LCD背光驱动
对于 Linux-2.6.30.4 的内核来讲,需要重新制作LCD 背光驱动,命名为:le2440_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 <mach/fb.h>
#include <linux/cdev.h>
#define DEVICE_NAME "backlight"
#define S3C2440_LCD_BASE 0x59000000
#define S3C2440_LCDCON1 (S3C2440_LCD_BASE + 0x00)
volatile int *lcdcon1 = NULL;
static int tq2440_backlight_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd)
{
case 0:
s3c2410_gpio_setpin(S3C2410_GPG4, 0);
//*lcdcon1 = *lcdcon1 & (~(0x1<<0));
printk("backlight Turn Off!\n");
return 0;
case 1:
s3c2410_gpio_setpin(S3C2410_GPG4, 1);
//*lcdcon1 |= (0x1<<0);
printk("backlight Turn On!\n");
return 0;
default:
return -EINVAL;
}
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.ioctl = tq2440_backlight_ioctl
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
lcdcon1 = (int *)ioremap(S3C2440_LCDCON1, 4);
ret = misc_register(&misc);
printk (DEVICE_NAME" initialized\n");
s3c2410_gpio_cfgpin(S3C2410_GPG4, S3C2410_GPG4_OUTP);
return ret;
}
static void __exit dev_exit(void)
{
misc_deregister(&misc);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
编写Makefile如下:
#Makefile
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /home/linux/sky/ker/linux-2.6.30.4/
#KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD)
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*
.PHONY: modules modules_install clean
else
obj-m := le2440_backlight.o
endif
然后make,生成 le2440_backlight.ko 文件,拷贝到NFS文件系统中
2、编写应用程序控制LCD背光
编写应用程序backlight.c,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fb.h>
int main(int argc, char **argv)
{
int turn;
int fd;
if ( argc == 1 )
{
fprintf(stderr, "\nUsage: backlight on|off !\n\n");
exit(1);
}
fd = open("/dev/backlight", O_RDWR);
if (fd < 0) {
perror("open LCD device !");
exit(1);
}
if ( strcmp(argv[1], "on" ) == 0)
turn = 1;
else if ( strcmp(argv[1], "off" ) == 0)
turn = 0;
else
{
fprintf(stderr, "\nUsage: backlight on|off !\n\n");
exit(1);
}
ioctl(fd, turn);
close(fd);
return 0;
}
编写Makefile,如下:
CROSS=arm-linux-
all: backlight
backlight:backlight.c
$(CROSS)gcc -o backlight backlight.c
$(CROSS)strip backlight
clean:
@rm -vf backlight *.o *~
make生成可执行文件backlight,拷贝到NFS文件系统中
3、测试LCD背光
可看到LCD屏亮灭
推荐阅读