海王  

http://blog.chinaunix.net/u3/101649/showart_2081192.html

 

1.编写背光驱动。文件名为my2440_backlight.c

(1)

代码
#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");

 

(2)制作Makefile,内容为:obj-m := my2440_backlight.o
(3)make -C /root/2410kernels/linux-2.6.25.20_v1.0.4/ M=$(pwd) modules

2.背光应用程序:

 

代码
#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;
}

 

编译:arm-linux-gcc -o backlight_test backlight_test.c

 

 

 

 

 

 

posted on 2010-08-23 12:20  海王  阅读(618)  评论(0编辑  收藏  举报