Gpio control driver

Source: Internet
Author: User
/* * 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"sunsea1026@gmail.com"#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);elsegpio_set_value(TEGRA_GPIO_PS4, 0);}if (!strcmp("green", led_name)) {if (!strcmp("on", led_state))gpio_set_value(TEGRA_GPIO_PS5, 1);elsegpio_set_value(TEGRA_GPIO_PS5, 0);}if (!strcmp("blue", led_name)) {if (!strcmp("on", led_state))gpio_set_value(TEGRA_GPIO_PS6, 1);elsegpio_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);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.