GPIO control driver

/*
 * Control LED for G7 project
 *
 * module name: gpio_led
 *
 * Author:	sunsea
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/fs.h>

#include "../../../arch/arm/mach-tegra/gpio-names.h"
#include <linux/gpio.h>

#define DRIVER_VERSION		"v1.0"
#define DRIVER_AUTHOR		"[email protected]"
#define DRIVER_DESC		"led gpio control"
#define DRIVER_LICENSE		"GPL"

#define DEV_NAME		"gpio_led_control"

static int gpio_led_major;
static struct class *gpio_led_class;
static struct device *gpio_led_dev;

static int gpio_led_open(struct inode *inode, struct file *file)
{
	return 0;
}

static ssize_t gpio_led_write(struct file *file, const char __user *buf, size_t count,
		loff_t *ppos)
{
	char cmd[64];
	char led_name[10] = {0};
	char led_state[10] = {0};
	int i = 0, j = 0;

	memset(cmd, 0x0, strlen(cmd));
	memcpy(cmd, buf, count);

	printk(KERN_INFO "cmd: %s\n", cmd);

	while (cmd[i] != '-') {
		led_name[i] = cmd[i];
		i++;
	}
	i++;

	while (cmd[i] != '\0')
		led_state[j++] = cmd[i++];

	if (!strcmp("red", led_name)) {
		if (!strcmp("on", led_state))
			gpio_set_value(TEGRA_GPIO_PS4, 1);
		else
			gpio_set_value(TEGRA_GPIO_PS4, 0);
	}

	if (!strcmp("green", led_name)) {
		if (!strcmp("on", led_state))
			gpio_set_value(TEGRA_GPIO_PS5, 1);
		else
			gpio_set_value(TEGRA_GPIO_PS5, 0);
	}

	if (!strcmp("blue", led_name)) {
		if (!strcmp("on", led_state))
			gpio_set_value(TEGRA_GPIO_PS6, 1);
		else
			gpio_set_value(TEGRA_GPIO_PS6, 0);
	}
	return 0;
}

static ssize_t gpio_led_read(struct file *file, char __user *buf, size_t count,
		loff_t *ppos)
{
	return 0;
}

static ssize_t gpio_led_release(struct inode *inode, struct file *file)
{
	return 0;
}

static struct file_operations gpio_led_fops = {
	.owner		= THIS_MODULE,
	.open		= gpio_led_open,
	.write		= gpio_led_write,
	.read		= gpio_led_read,
	.release	= gpio_led_release,
};

static int __init gpio_led_init(void)
{
	int error;

	gpio_led_major = register_chrdev(0, DEV_NAME, &gpio_led_fops);
	if (gpio_led_major < 0) {
		printk(KERN_ERR "%s:register gpio_led_control failed\n", __FILE__);
		goto out_unreg_chrdev;
	}

	gpio_led_class = class_create(THIS_MODULE, "gpio_led_control");
	if (IS_ERR(gpio_led_class)) {
		printk(KERN_ERR "%s:class create failed\n", __FILE__);
		goto out_unreg_class;
	}


	gpio_led_dev = device_create(gpio_led_class, NULL, MKDEV(gpio_led_major, 0), NULL, "gpio_led_control");
	if (IS_ERR(gpio_led_dev)) {
		printk(KERN_ERR "%s:device create failed\n", __FILE__);
		goto out_unreg_device;
	}

	printk(KERN_INFO "gpio_led_control load successfully.\n");

	return 0;

out_unreg_device:
	error = PTR_ERR(gpio_led_dev);
	device_destroy(gpio_led_class, MKDEV(gpio_led_major, 0));
	class_destroy(gpio_led_class);
	unregister_chrdev(gpio_led_major, DEV_NAME);
	return error;

out_unreg_class:
	error = PTR_ERR(gpio_led_class);
	class_destroy(gpio_led_class);
	unregister_chrdev(gpio_led_major, DEV_NAME);
	return error;

out_unreg_chrdev:
	unregister_chrdev(gpio_led_major, DEV_NAME);
	return gpio_led_major;
}

static void __exit gpio_led_exit(void)
{
	device_destroy(gpio_led_class, MKDEV(gpio_led_major, 0));
	class_destroy(gpio_led_class);
	unregister_chrdev(gpio_led_major, DEV_NAME);

	printk(KERN_INFO "gpio_led_control unload successfully.\n");
}

module_init(gpio_led_init);
module_exit(gpio_led_exit);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE(DRIVER_LICENSE);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章