1 设备树如下:

my_platform_device004 {
		status = "okay";
		compatible ="my_platform_device_004";
		led-gpios = <&gpio1 7 GPIO_ACTIVE_HIGH>;
	};

我们的开发板是RK3399,使用的是第一组的 GPIO 里面的第7个 。

2 主要接口:

struct gpio_desc *red = devm_gpiod_get(&pdev->dev, "led", 0); 

这个led参数就是对应的 led-gpios 的led,

设置方向为输出,并且是低电平。

gpiod_direction_output(red,0);

设置方向为输出,并且是高电平。

gpiod_direction_output(red,1);

led接上面的gpio1 7 , 高电平点亮Led,低电平关闭Led 。

模块代码就很简单了,实现的一个timer不断的打开和关闭led 灯 。

#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/bug.h>			/* For BUG_ON.  */
#include <linux/cpu.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/delay.h>
#include <linux/smp.h>
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/percpu-defs.h>
#include <linux/wait.h>
#include <linux/gpio/driver.h>
#include <linux/atomic.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/kfifo.h>
#include <linux/timer.h>
#include <linux/gpio/consumer.h>

/**
 *
 *
my_platform_device004 {
		status = "okay";
		compatible ="my_platform_device_004";
		led-gpios = <&gpio1 7 GPIO_ACTIVE_HIGH>;
	};

 RK3399 has 5 groups of GPIO banks: GPIO0~GPIO4, and each group is distinguished by numbers A0~A7, B0~B7, C0~C7, D0~D7.


 */

#define MY_MAX_MINORS  1
#define   DEVICE_NAME  "device name 500"

struct gpio_desc *red;

DEFINE_KFIFO(myfifo, char, 1024);

struct my_device_data {
	struct cdev cdev;
};

DECLARE_WAIT_QUEUE_HEAD(wq);

static struct my_device_data devs[MY_MAX_MINORS];

static void my_timer_func(unsigned long);

static DEFINE_TIMER(my_timer, my_timer_func, 0, 0);

int a = 0;

static void my_timer_func(unsigned long unused) {
	pr_info("timer expires , now  do sth\n");
	if (a == 0)
		a = 1;
	else
		a = 0;
	gpiod_set_value(red, a);
	if (!timer_pending(&my_timer)) {
		mod_timer(&my_timer, jiffies + 2 * HZ);
	}
}

static int my_open(struct inode *inode, struct file *file) {
	pr_info("a3 my_open \n");
	return 0;
}

static ssize_t my_write(struct file *filp, const char __user *user_buffer,
		size_t size, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("write");
	ret = kfifo_from_user(&myfifo, user_buffer, size, &len);
	if (ret != 0) {
		pr_err("kfifo_from_user error");
		return 0;
	}
	if (len <= 0)
		return 0;
	*offset += len;
	wake_up(&wq);
	return len;
}

static ssize_t my_read(struct file *filp, char __user *user_buffer,
		size_t count, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("read");
	ret = kfifo_to_user(&myfifo, user_buffer, count, &len);
	if (len <= 0)
		return 0;
	*offset += len;
	return len;
}

unsigned int my_poll(struct file *flip, struct poll_table_struct *table) {
	int mask = 0;
	pr_info("my_poll \n");
	poll_wait(flip, &wq, table);
	if (kfifo_is_empty(&myfifo)) {

	} else {
		mask |= POLLIN | POLLRDNORM;
	}
	return mask;
}

int my_close(struct inode *inode, struct file *flip) {
	pr_info("my_close \n");
	return 0;
}

struct fasync_struct *fasync_struct;

int my_fasync(int fd, struct file *flip, int on) {
	return fasync_helper(fd, flip, on, &fasync_struct);
}

static const struct file_operations my_fops = { .owner = THIS_MODULE, .open =
		my_open, .read = my_read, .write = my_write, .poll = my_poll, .release =
		my_close, .fasync = my_fasync, };

static dev_t mydev;

int myprobe(struct platform_device *pdev) {
	int i;
	int err;

	pr_info("myplatformdriver myprobe \n");

	err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
	if (err != 0) {
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MAJOR(mydev), i), 1);
	}
	pr_err("before gpiod_get_index \n");
	red = gpiod_get(&pdev->dev, "led", 0);
	if (IS_ERR(red)) {
		pr_err("error \n");
			return -1;
		}

	gpiod_direction_output(red,1);

	pr_err("after gpiod_get_index \n");
	mod_timer(&my_timer, jiffies + 3 * HZ);
	return 0;
}

int myremove(struct platform_device *pdev) {
	int i;
	del_timer(&my_timer);
	pr_info("myplatformdriver myremove \n");
	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* release devs[i] fields */
		cdev_del(&devs[i].cdev);
	}
	unregister_chrdev_region(mydev, MY_MAX_MINORS);
	return 0;
}

int mysuspend(struct device *pdev, pm_message_t state) {
	pr_info("myplatformdriver mysuspend \n");
	return 0;
}

int myresume(struct device *pdev) {
	pr_info("myplatformdriver myresume \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_004", };

struct platform_driver my_platform_driver = { .driver = { .of_match_table =
		&my_of_match_table, .name = "my-platform-driver", .owner = THIS_MODULE,
		.suspend = mysuspend, .resume = myresume, }, .probe = myprobe, .remove =
		myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");


参考 : https://www.kernel.org/doc/Documentation/gpio/consumer.txt

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐