S3C6410 GPIO 驱动(二) --- LED驱动

//			wzk_led.h



//led header
#define LED1_ON 10
#define LED1_OFF 11
#define LED2_ON 20
#define LED2_OFF 21
#define LED3_ON 30
#define LED3_OFF 31
#define LED4_ON 40
#define LED4_OFF 41
 
 
//			wzk_led.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <asm/irq.h>
#include <mach/gpio.h>
#include <mach/regs-gpio.h>
#include <mach/gpio-bank-k.h>
#include <mach/map.h>			//S3C64XX_VA_GPIO
#include <plat/gpio-cfg.h>
#include <mach/hardware.h>
#include <linux/io.h>
#include <asm/io.h>

#include "wzk_led.h"

#define DEVICE_NAME	"leds"
#define LED_MAJOR 231


static unsigned long led_table [] = {
       S3C64XX_GPK(4),
       S3C64XX_GPK(5),
       S3C64XX_GPK(6),
       S3C64XX_GPK(7),
};

int s3c6410_leds_open(struct inode *inode,struct file *file)
{
	return 0;
}
static long s3c6410_leds_ioctl(
	struct file *file, 
	unsigned int cmd, 
	unsigned long arg)
{
	unsigned int tmp;
	printk("%u\n",cmd);

	switch(cmd){

	case LED1_ON:
		tmp = readl(S3C64XX_GPKDAT);
		tmp = tmp & 0xffef;
		writel(tmp,S3C64XX_GPKDAT);
		break;
		
	case LED1_OFF:
                tmp = readl(S3C64XX_GPKDAT);
                tmp = tmp | 0x0010;
                writel(tmp,S3C64XX_GPKDAT);
                break;

	case LED2_ON:
		tmp = readl(S3C64XX_GPKDAT);
		tmp = tmp & 0xffdf;
		writel(tmp,S3C64XX_GPKDAT);
		break;
		
	case LED2_OFF:
                tmp = readl(S3C64XX_GPKDAT);
                tmp = tmp | 0x0020;
                writel(tmp,S3C64XX_GPKDAT);
                break;
	case LED3_ON:
		tmp = readl(S3C64XX_GPKDAT);
		tmp = tmp & 0xffbf;
		writel(tmp,S3C64XX_GPKDAT);
		break;
		
	case LED3_OFF:
                tmp = readl(S3C64XX_GPKDAT);
                tmp = tmp | 0x0040;
                writel(tmp,S3C64XX_GPKDAT);
                break;

	case LED4_ON:
		tmp = readl(S3C64XX_GPKDAT);
		tmp = tmp & 0xff7f;
		writel(tmp,S3C64XX_GPKDAT);
		break;
		
	case LED4_OFF:
                tmp = readl(S3C64XX_GPKDAT);
                tmp = tmp | 0x0080;
                writel(tmp,S3C64XX_GPKDAT);
                break;

 	default:
                return -1;
		
	}

	return 0;
}

void init_dev(void)
{
	unsigned int tmp;
	s3c_gpio_cfgpin(led_table[0],S3C_GPIO_OUTPUT);
	s3c_gpio_cfgpin(led_table[1],S3C_GPIO_OUTPUT);
	s3c_gpio_cfgpin(led_table[2],S3C_GPIO_OUTPUT);
	s3c_gpio_cfgpin(led_table[3],S3C_GPIO_OUTPUT);

//	gpio_set_value(led_table[0],0);
//	gpio_set_value(led_table[1],0);
//	gpio_set_value(led_table[2],0);
//	gpio_set_value(led_table[3],0);
	
//	unsigned int tmp;
	tmp = readl(S3C64XX_GPKDAT);
	tmp = tmp | 0x0000f0;
	writel(tmp,S3C64XX_GPKDAT);
		
}
static struct file_operations s3c6410_leds_fops = {
	.owner	= THIS_MODULE,
	.open = s3c6410_leds_open,
	.unlocked_ioctl	= s3c6410_leds_ioctl,
};

static struct cdev cdev_leds;
struct class * my_class;

static int __init s3c6410_leds_init(void)
{
	int ret;
	dev_t devno;
	printk(KERN_NOTICE "enter s3c6410_leds_init\n");


	devno = MKDEV(LED_MAJOR,0);

	ret = register_chrdev_region(devno,1,DEVICE_NAME);
	if(ret<0)
	{
		printk(KERN_NOTICE "can not register led device");
		return ret;
	}
	
	cdev_init(&cdev_leds,&s3c6410_leds_fops);
	cdev_leds.owner = THIS_MODULE;

	ret =cdev_add(&cdev_leds,devno,1);
	if(ret)
	{
		printk(KERN_NOTICE "can not add leds device");
		return ret;
	}

	my_class = class_create(THIS_MODULE,"my_class");
	if(IS_ERR(my_class))
	{
		printk("Err: Failed in creating class\n");
		return -1;	
	}

	device_create(my_class,NULL,MKDEV(LED_MAJOR,0),NULL,DEVICE_NAME);

	init_dev();

	printk(DEVICE_NAME " initialized\n");

	return 0;
}

static void __exit s3c6410_leds_exit(void)
{

	
	device_destroy(my_class,MKDEV(LED_MAJOR,0));
	class_destroy(my_class);

	cdev_del(&cdev_leds);

	unregister_chrdev_region(MKDEV(LED_MAJOR,0),1);	

	printk(KERN_NOTICE "s3c2440_leds_exit\n");
}


module_init(s3c6410_leds_init);
module_exit(s3c6410_leds_exit);

MODULE_LICENSE("GPL");














   

你可能感兴趣的:(S3C6410 GPIO 驱动(二) --- LED驱动)