您的位置:首页 > 其它

总线设备模型-LED驱动

2014-01-30 22:16 423 查看
#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");

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: