之前几个led驱动本质都是配置 LED 灯所使用的 GPIO 寄存器,驱动开发方式和裸机基本没啥区别。Linux 内核提供了 pinctrl 和 gpio 子系统用于GPIO 驱动。本实验我们就来学习一下如何借助 pinctrl 和 gpio 子系统来简化 GPIO 驱动开发。
1 pinctrl 子系统
1.1 pinctrl 子系统简介
我们使用者只需要在设备树里面设置好某个 pin 的相关属性即可,其他的初始化工作均由 pinctrl 子系统来完成,pinctrl 子系统源码目录为 drivers/pinctrl。
1.2 I.MX6ULL 的 pinctrl 子系统驱动
内容较多,参考正点原子《I.MX6U 嵌入式 Linux 驱动开发指南 V1.6》第45.1.2章节
1.3 设备树中添加 pinctrl 节点模板
我们虚拟一个名为“test”的设备,test 使用了 GPIO1_IO00 这个 PIN 的 GPIO 功能,pinctrl 节点添加过程如下:
pinctrl_test: testgrp {
/* 具体的 PIN 信息 */
};
pinctrl_test: testgrp {
fsl,pins = <
/* 设备所使用的 PIN 配置信息 */
>;
};
pinctrl_test: testgrp {
fsl,pins = <
MX6UL_PAD_GPIO1_IO00__GPIO1_IO00 config /*config 是具体设置值*/
>;
};
2.3 gpio 子系统 API 函数
常用的API函数如下所示
1、int gpio_request(unsigned gpio, const char *label)
2、void gpio_free(unsigned gpio)
3、int gpio_direction_input(unsigned gpio)
4、int gpio_direction_output(unsigned gpio, int value)
5、#define gpio_get_value __gpio_get_value
int __gpio_get_value(unsigned gpio)
6、#define gpio_set_value __gpio_set_value
void __gpio_set_value(unsigned gpio, int value)
test {
/* 节点内容 */
};
2、添加 pinctrl 信息
test {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_test>;
/* 其他节点内容 */
};
test {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_test>;
gpio = <&gpio1 0 GPIO_ACTIVE_LOW>;
};
本次实验我们通过设备树向 dtsled.c 文件传递相应的寄存器物理地址,然后在驱动文件中配置寄存器。本章实验我们使用 pinctrl 和 gpio 子系统来完成 LED 灯驱动。
4.1 修改设备树文件
打开 imx6ull-alientek-emmc.dts。
1、添加 pinctrl 节点
在 iomuxc 节点的 imx6ul-evk 子节点下创建一个名为“pinctrl_gpioled”的子节点
pinctrl_gpioled: ledprp{
fsl,pins = <
MX6UL_PAD_GPIO1_IO03__GPIO1_IO03 0x10b0
>;
};
第 3 行,将 GPIO1_IO03 这个 PIN 复用为 GPIO1_IO03,电气属性值为 0X10B0。
2、添加 LED 设备节点
gpioled{
compatible = "alientek,gpioled";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_gpioled>;
led-gpios = <&gpio1 3 GPIO_ACTIVE_LOW>;
status= "okay";
};
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define GPIOLED_CNT 1
#define GPIOLED_NAME "gpioled"
#define LEDOFF 0
#define LEDON 1
/*gpioled 设备结构体*/
struct gpioled_dev
{
dev_t devid;
int major;
int minor;
struct cdev cdev;
struct class *class;
struct device *device;
struct device_node *nd;
int led_gpio;
};
struct gpioled_dev gpioled; /*LED*/
static int led_open(struct inode *inode, struct file *filp)
{
filp->private_data = &gpioled;
return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
return 0;
}
static ssize_t led_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
int ret;
unsigned char databuf[1];
struct gpioled_dev *dev = filp->private_data;
ret = copy_from_user(databuf, buf, count);
if (ret < 0)
{
return -EINVAL;
}
if (databuf[0] == LEDON)
{
gpio_set_value(dev->led_gpio, 0);
}
else if (databuf[0] == LEDOFF)
{
gpio_set_value(dev->led_gpio, 1);
}
return 0;
}
/*操作集*/
static const struct file_operations led_fops = {
.owner = THIS_MODULE,
.write = led_write,
.open = led_open,
.release = led_release,
};
/*驱动入口函数*/
static int __init led_init(void)
{
int ret = 0;
/*1,注册字符设备驱动*/
gpioled.major = 0;
if (gpioled.major)
{ /*给定主设备号*/
gpioled.devid = MKDEV(gpioled.major, 0);
register_chrdev_region(gpioled.devid, GPIOLED_CNT, "GPIOLED_NAME");
}
else
{
alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, "GPIOLED_NAME");
gpioled.major = MAJOR(gpioled.devid);
gpioled.minor = MINOR(gpioled.devid);
}
printk("gpioled major =%d, minor =%d \r\n", gpioled.major, gpioled.minor);
/*2,初始化cdev*/
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev, &led_fops);
/*3,添加cdev*/
cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT);
/*4,创建类*/
gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME);
if (IS_ERR(gpioled.class))
{
return PTR_ERR(gpioled.class);
}
/*5,创建设备*/
gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
if (IS_ERR(gpioled.device))
{
return PTR_ERR(gpioled.device);
}
/***************本次实验重点添加内容**********************/
/*1,获取设备节点*/
gpioled.nd = of_find_node_by_path("/gpioled");
if (gpioled.nd == NULL)
{
ret = -EINVAL;
goto fail_findnode;
}
/*2,获取LED设备所对应的GPIO编号*/
gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpios", 0);
if (gpioled.led_gpio < 0)
{
printk("can't find led gpio\r\n");
ret = -EINVAL;
goto fail_findnode;
}
printk("led gpio num =%d\r\n", gpioled.led_gpio);
/*3,申请GPIO引脚*/
ret = gpio_request(gpioled.led_gpio, "led-gpio");
if (ret)
{
printk("Failed to request the led gpio\r\n");
ret = -EINVAL;
goto fail_findnode;
}
/*4,设置 GPIO1_IO03 为输出,并且输出高电平,默认关闭 LED 灯*/
ret = gpio_direction_output(gpioled.led_gpio, 1);
if (ret)
{
goto fail_setoutput;
}
/*5,输出低电平,点亮led灯*/
gpio_set_value(gpioled.led_gpio, 0);
return 0;
fail_setoutput:
gpio_free(gpioled.led_gpio);
fail_findnode:
return ret;
}
/*驱动出口函数*/
static void __exit led_exit(void)
{
/*关灯*/
gpio_set_value(gpioled.led_gpio, 1);
/*注销字符设备驱动*/
cdev_del(&gpioled.cdev);
unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
device_destroy(gpioled.class, gpioled.devid);
class_destroy(gpioled.class);
/*释放IO*/
gpio_free(gpioled.led_gpio);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("supersmart");
4.3 编写测试 APP
同 《新字符设备驱动实验》测试APP一样:
#include
#include
#include
#include
#include
#include
#include
/*
*argc:应用程序参数个数
* argv[]:具体的参数内容,字符串形式
* ./ledAPP <0:1> 0 关灯,1 开灯
* ./ledAPP /dev/dtsled 0 关灯
* ./ledAPP /dev/dtsled 1 开灯
*/
#define LEDOFF 0
#define LEDON 1
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
unsigned char databuf[1];
if (argc != 3)
{
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if (fd < 0)
{
printf("file %s open failed!\r\n", filename);
return -1;
}
databuf[0] = atoi(argv[2]); /*将字符转化为数字*/
retvalue = write(fd, databuf, sizeof(databuf));
if (retvalue < 0)
{
printf("LED Control Failed ! \r\n");
close(fd);
return -1;
}
close(fd);
return 0;
}
5 运行测试
5.1 编译驱动程序和测试 APP
1、编译驱动程序
编写 Makefile 文件
KERNELDIR := /home/znn/linux/IMX6ULL/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga
CURRENT_PAHT := $(shell pwd)
obj-m := gpioled.o
build :kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) clean
编译驱动模块
重启开发板,进入到目录 lib/modules/4.1.15 中,加载驱动模块
打开led
关闭led