总线设备模型-LED驱动

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>

#include <linux/platform_device.h>

#define LED_DEVICE_NAME ("LED_DEV")

static struct resource s3c_led_resource[] = {
    [0] = {
        .start = 0x56000050,
        .end = 0x56000050 + 8 - 1,
        .flags = IORESOURCE_MEM,
    },
    [1] = {
        .start = 4,
        .end = 4,
        .flags = IORESOURCE_IRQ,
    }
};

void	s3c_led_release(struct device * dev)
{

}

struct platform_device s3c_device_led = {
    .name         = LED_DEVICE_NAME,
    .id         = -1,
    .num_resources     = ARRAY_SIZE(s3c_led_resource),
    .resource     = s3c_led_resource,
    .dev = {
        .release = s3c_led_release,
    }
};

static int __init led_init(void)
{	
	return platform_device_register(&s3c_device_led);
}

static void __exit led_exit(void)
{	
	return platform_device_unregister(&s3c_device_led);
}

module_init(led_init);
module_exit(led_exit);

MODULE_AUTHOR("Itdog");
MODULE_DESCRIPTION("S3c2410 LED");
MODULE_LICENSE("GPL");



#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/uaccess.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/platform_device.h>
#include <asm/arch/regs-gpio.h>
#include <linux/ioport.h>
#include <asm/hardware.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/poll.h>



#define LED_DEVICE_NAME ("LED_DEV")
#define LED_MASK (0x3)
#define LED_EN (0x01)

static volatile unsigned long * volatile led_con_addr = 0;
static volatile unsigned long * volatile led_dat_addr = 0;
static unsigned int led_pin  = 0;
static unsigned long major = 0;
static struct class * led_clz = 0 ;


ssize_t led_write (struct file * flip, const char __user * buf, size_t cnt, loff_t * pos)
{
	unsigned int led_status = 0;
	copy_from_user(&led_status, buf, sizeof(int));		

	printk("STATE : %p\n", led_status);

	if(led_status)
	{
		// 开灯
		(*led_dat_addr) &= ~(0x01 << led_pin);
	}
	else
	{
		// 熄灭
		(*led_dat_addr) |= (0x01 << led_pin);
	}
	return 0;
}

static struct file_operations f_ops = {
	.owner = THIS_MODULE,
	.write = led_write, 
};

int led_probe(struct platform_device * pdev)
{
	struct resource * res = 0;		


	/* 获取资源 */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);	
	printk("io base:%p\n", res->start);

	/* 将物理地址映射至虚拟地址空间 */
	led_con_addr = ioremap(res->start, res->end - res->start + 1);
	led_dat_addr = led_con_addr + 1;
	printk("viraddr: %p\n", led_con_addr);
	

	/* 将指定引脚配置为输出状态*/
	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	led_pin = res->start;
	(*led_con_addr) &= ~(LED_MASK << (res->start * 2));	
	(*led_con_addr) |= (LED_EN << (res->start * 2));
	
	/* 注册字符设备驱动 */		
	major = register_chrdev(0, LED_DEVICE_NAME, &f_ops);

	/* 创建设备节点 */
	led_clz = class_create(THIS_MODULE, LED_DEVICE_NAME);
	device_create(led_clz, NULL, MKDEV(major, 0), LED_DEVICE_NAME);		

	return 0;
}

int led_remove(struct platform_device * pdev)
{
	class_destroy(led_clz);
	device_destroy(led_clz, MKDEV(major, 0));
	unregister_chrdev(MKDEV(major, 0), LED_DEVICE_NAME);
	iounmap(led_con_addr);			

	return 0;	
}

	
static struct platform_driver led_driver = {
	.probe = led_probe,
	.remove = led_remove,
	.driver		= {
		.name	= LED_DEVICE_NAME,
	},
};

static int __init led_init(void)
{
	if (platform_driver_register(&led_driver) == 0)
	{
		printk("Driver installed succeed.\n");
	}
	else
	{
		printk("Driver installed failed.\n");
	}

	return 0;
		
}

static void __exit led_exit(void)
{
	platform_driver_unregister(&led_driver);
}



module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值