设备树中的GPIO

 

涉及到的GPIO驱动基于linux-4.14,soc是全志H3,平台是nanopi-m1,

linux中关于GPIO控制是通过GPIOLIB实现的,相关主要API如下:

#include <linux/gpio.h>  //GPIO操作相关接口
int gpio_direction_input(unsigned gpio);//把管脚设置成输入
int gpio_direction_output(unsigned gpio, int value);//把管脚设置成输出
int gpio_get_value(unsigned gpio);//获取管脚电平的值
void gpio_set_value(unsigned gpio, int value);//设置管脚电平的值
int gpio_to_irq(unsigned gpio);//获取对应GPIO的逻辑中断号

 

在没有使用设备树来描述GPIO管脚的时候,内核中使用一个数值代表GPIO_num,该值由SOC产商在板级信息中定义好,由于在新的内核中省去了这些板级信息,而改用在设备树中去描述这些硬件相关资源,比如GPIO序号,中断控制器中的中断号,寄存器地址。

全志H3有6个GPIO bank,PA6和PA7上接入两个LED,通过编写一个GPIO的驱动程序来控制LED的亮灭,在设备树文件sun8i-h3-nanopi-m1.dts的根节点增加一个myleds子节点 ,该节点的描述如下所示:

myleds {       
	compatible = "usr,myleds";         
	led-gpios = <&pio 0  7  GPIO_ACTIVE_HIGH>,<&pio 0  6  GPIO_ACTIVE_HIGH>;/* 0表示PA组的io口, 因h3里第0组的io口就是以PA开始命名的 */          
};

 

节点中的led-gpios属性,引用了pinctrl信息,sunxi-h3-h5.dtsi中有关于该控制器的描述,

        pio: pinctrl@01c20800 {
            /* compatible is in per SoC .dtsi file */
            reg = <0x01c20800 0x400>;
            interrupts = <GIC_SPI 11 IRQ_TYPE_LEVEL_HIGH>,
                     <GIC_SPI 17 IRQ_TYPE_LEVEL_HIGH>;
            clocks = <&ccu CLK_BUS_PIO>, <&osc24M>, <&osc32k>;
            clock-names = "apb", "hosc", "losc";
            gpio-controller;
            #gpio-cells = <3>;
            interrupt-controller;
            #interrupt-cells = <3>;
            ......
    };

 

myleds子节点描述了两个GPIO信息,具有多个gpio口信息的属性值: <&gpio控制器节点名 具体gpio口的标识符>,<&gpio控制器节点名 具体gpio口的标识符> …; 具体gpio口的标识符是由多个数字组成, 数字的个数由所用的gpio控制器节点里的#gpio-cells属性值指定.。全志H3 共有七组GPIO ,也即七个bank,分别为PA (PA0-21),PC(PC0-16) PD (PD0-17), PE(PE0-15),PF(PF0-6) PG(PG0-13) ,PL(PL0-PL11),在内核中PA-PG是一个pinctrl控制器,而PL是另一个pinctrl控制器 。

注意在新版的linux内核中GPIO子系统和pinctrl子系统密切,彼此之间存在密切的耦合

 

由#gpio-cells = <3>可以推出GPIO属性需要用3个u32的数据描述,第一个参数代表该GPIO位于哪个bank,第二个参数代表该bank下的序号,第三个参数代表默认电平,myleds节点下的GPIO_ACTIVE_HIGH这个宏就定义于include/dt-bindings/gpio/gpio.h,从这里可以猜出设备树文件是可以包含c语言中的头文件的。

编译设备树,把编译好的dtb文件烧写到启动卡的第一个分区。

驱动程序写法

  • 构造一个platform_driver,其中的of_match_table字段需要与my_leds节点的compatible属性值一致,当匹配时则调用platform_driver的probe函数。
static const struct of_device_id of_match_leds[] = {
	{ .compatible = "usr,myleds" },
	{ /* sentinel */ }
};
struct platform_driver led_drv = {
	.probe		= led_probe,
	.remove		= led_remove,
	.driver		= {
		.name	= "myled",
		.of_match_table = of_match_leds, /* 能支持哪些来自于dts的platform_device */
	}
};
static int  led_init(void)
{
	platform_driver_register(&led_drv);
	return 0;
}

 

  • 在probe函数中调用of_gpio.h里的相关API解析my_leds节点里的GPIO属性,获得GPIO_num,并分配设置和注册一个字符设备
static int led_probe(struct platform_device *pdev)
{
	struct device_node *node=pdev->dev.of_node;
	int flags;
	int i;
	printk("led_probe,found led\n");
		//注册字符设备		自动分配
	major=register_chrdev(0,DEVICE_NAME, &led_fops);
	cls=class_create(THIS_MODULE, "led");
	//根据udev生成设备节点 /dev/led
	device_create(cls, 0, MKDEV(major, 0), 0, "led");
	for(i=0;i<nums;i++)
		{
	       #if 1
			gpio[i]=of_get_named_gpio_flags(node,"led-gpios",i,&flags);
	      #endif
			 if (!gpio_is_valid(gpio[i]))
                 printk("gpio isn't valid\n");
			printk("gpio num=%d",gpio[i]);
	}
	return 0;
}

 

  • 填充字符设备中的file_operations结构体 ,在驱动的open函数中,通过获取的GPIO_num,申请GPIO资源 ,在驱动的write函数中,根据应用程序传过来的数值,设置GPIO高低电平
static int led_open(struct inode *inode, struct file *filp)
{

	int ret,i;
	printk("led_open\n");
	for(i=0;i<nums;i++)
		{
		//gpio_request(LED_GPIO, "led");
		ret= gpio_request(gpio[i], "led-gpios");
		if(ret!=0)
		printk("can not request gpio\n");
		gpio_direction_output(gpio[i], 0);
	}
	return 0;

}
static int  led_close(struct inode *inode, struct file *filp)
{
	//gpio_free(gpio);
	int i;
	for(i=0;i<nums;i++)
	  gpio_free(gpio[i]);
	return 0;
}
ssize_t led_write(struct file *filp, const char __user *buf, size_t count,
                loff_t *f_pos)
{
	char data[2];
    int i;
	copy_from_user(data, buf, count);

	//gpio_direction_output(gpio, data[0]);
	for (i = 0; i < nums; i++)
        gpio_direction_output(gpio[i], data[0]);

	return count;
}

 

  • 在platform_driver的remove函数中,注销该字符设备
static int led_remove(struct platform_device *pdev)
{
	printk("led_remove,remove led");

	device_destroy(cls, MKDEV(major, 0));
	class_destroy(cls);
	unregister_chrdev(major, "my_led");
	return 0;
}

 

编写led测试程序

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    int fd;
    unsigned char val = 1;
    fd = open("/dev/led", O_RDWR);
    if (fd < 0)
    {
        printf("can't open!\n");
    }
    if (argc != 2)
    {
        printf("Usage :\n");
        printf("%s <on|off>\n", argv[0]);
        return 0;
    }

    if (strcmp(argv[1], "on") == 0)
    {
        val  = 1;
    }
    else
    {
        val = 0;
    }

    write(fd, &val, 1);
    return 0;
}

 

编译上面的驱动程序,交叉编译该测试程序,在nanopi-m1上挂载nfs,就可以使用该测试程序。

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值