misc类型的gpio驱动模版-linu


     还是闲着没事,丢个模版就跑惊讶


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/delay.h>
 
#include <linux/gpio.h>
#include <mach/gpio.h>
#include <plat/gpio-cfg.h>


#define DEVICE_NAME "av5260-leds"
#define LED_DEFAULT 0

static int led_gpios[] = {
	EXYNOS5260_GPX3(1), //r
	EXYNOS5260_GPX3(2), //g
	EXYNOS5260_GPX1(3), //b
};

#define LED_NUM		ARRAY_SIZE(led_gpios)


static long av5260_leds_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	switch(cmd) {
		case 0:
		case 1:
			if (arg > LED_NUM) {
				return -EINVAL;
			}

			gpio_set_value(led_gpios[arg], cmd);
			//printk(DEVICE_NAME": %d %d\n", arg, cmd);
			break;

		default:
			return -EINVAL;
	}

	return 0;
}

static struct file_operations av5260_led_dev_fops = {
	.owner			= THIS_MODULE,
	.unlocked_ioctl	= av5260_leds_ioctl,
};

static struct miscdevice av5260_led_dev = {
	.minor			= MISC_DYNAMIC_MINOR,
	.name			= DEVICE_NAME,
	.fops			= &av5260_led_dev_fops,
};

static int __init av5260_led_dev_init(void) {
	int ret;
	int i;

	for (i = 0; i < LED_NUM; i++) {
		ret = gpio_request(led_gpios[i], "LED");
		if (ret) {
			printk("%s: request GPIO %d for LED failed, ret = %d\n", DEVICE_NAME, led_gpios[i], ret);
			return ret;
		}

		s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT);
		gpio_set_value(led_gpios[i], LED_DEFAULT);
	}

	ret = misc_register(&av5260_led_dev);

	printk(DEVICE_NAME"\tinitialized.\n");

	return ret;
}

static void __exit av5260_led_dev_exit(void) {
	int i;

	for (i = 0; i < LED_NUM; i++) {
		gpio_free(led_gpios[i]);
	}

	misc_deregister(&av5260_led_dev);
}

module_init(av5260_led_dev_init);
module_exit(av5260_led_dev_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Arashi Vision Inc.");


/*
Makefile:

obj-$(CONFIG_AV5260_LEDS)   += av5260_led.o

Kconfig:

config AV5260_LEDS
	tristate "LED Support for ArashiVision AV5260 GPIO LEDs"
	#depends on MACH_AV5260
	default y
	help
	  This option enables support for LEDs connected to GPIO lines
	  on AV5260 boards.

*/


阅读更多
个人分类: 嵌入式linux
上一篇misc设备驱动模板-linux
下一篇misc类型 按键驱动及应用- Linux
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭