驱动代码:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <plat/gpio-cfg.h>
#include <mach/gpio.h>
#include <mach/gpio-exynos4.h>
MODULE_LICENSE("GPL");
//设备节点
#define NODE_NAME "leds_node"
static int leds_open(struct inode *node, struct file *fp)
{
printk("leds open.\n");
return 0;
}
static int leds_release(struct inode *node, struct file *fp)
{
printk("leds release.\n");
return 0;
}
static int leds_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
{
printk("leds ioctl, cmd: %d, arg: %d\n", cmd, arg);
switch (cmd)
{
case 0:
gpio_direction_output(EXYNOS4_GPL2(0), arg);
break;
default:
break;
}
return 0;
}
struct file_operations leds_ops = {
.owner = THIS_MODULE,
.open = leds_open,
.release = leds_release,
.unlocked_ioctl = leds_ioctl,
};
struct miscdevice leds_misc_dev = {
.minor = MISC_DYNAMIC_MINOR, //自动分配次设备号
.name = NODE_NAME,
.fops = &leds_ops,
};
static int leds_probe(struct platform_device *dev)
{
printk("leds probe, device name: %s\n", dev->name);
int ret = gpio_request(EXYNOS4_GPL2(0), "LEDS");
if (ret)
{
printk("gpio request gpl0(2) failed.\n");
return -1;
}
//gpio_direction_output(EXYNOS4_GPL2(0), 1);
s3c_gpio_cfgpin(EXYNOS4_GPL2(0), GPIO_OUTPUT);
misc_register(&leds_misc_dev);
return 0;
}
static int leds_remove(struct platform_device *dev)
{
printk("leds remove, device name: %s\n", dev->name);
gpio_free(EXYNOS4_GPL2(0));
misc_deregister(&leds_misc_dev);
return 0;
}
struct platform_driver leds_driver = {
.probe = leds_probe,
.remove = leds_remove,
.driver = {
.name = "leds_ctl",
.owner = THIS_MODULE,
}
};
struct platform_device leds_dev = {
.name = "leds_ctl",
.id = -1,
};
static int leds_init()
{
printk(KERN_EMERG "leds init.\n");
platform_device_register(&leds_dev);
platform_driver_register(&leds_driver);
return 0;
}
static void leds_exit()
{
printk(KERN_EMERG "leds exit.\n");
platform_driver_unregister(&leds_driver);
platform_device_unregister(&leds_dev);
}
module_init(leds_init);
module_exit(leds_exit);
应用代码:
#include <stdio.h> //printf()
#include <stdlib.h> //exit()
#include <string.h> //strlen(), bzero();
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(int argc, char *argv[])
{
int fd = open("/dev/leds_node", O_RDWR | O_NDELAY);
while (1)
{
ioctl(fd, 0, 1);
sleep(1);
ioctl(fd, 0, 0);
sleep(1);
}
close(fd);
return 0;
}