1、前言
在嵌入式Linux开发中,对嵌入式SoC中的GPIO进行控制非常重要,Linux内核中提供了GPIO子系统,驱动开发者在驱动代码中使用GPIO子系统提供的API函数,便可以达到对GPIO控制的效果,例如将IO口的方向设置为输入或输出,当IO口的方向为输入时,可以通过调用API函数获取相应的IO口电平,当IO口设置为输出方向时,可以调用相关的API函数去设置IO口电平,本文将简单描述如何去使用Linux内核中GPIO子系统的API接口。
下图是Linux内核中GPIO子系统的软件驱动分层图:
2、常用API接口
当我们在驱动代码中要使用内核中提供的GPIO子系统,需要在驱动代码中包含<linux/gpio.h>头文件,另外,关于API接口函数的实现在内核源码drivers/gpio/gpiolib.c文件中,关于GPIO子系统的使用说明文档为Documentation/gpio.txt,该文档具有更详细的使用说明,接下来,将简单介绍一下常用的API接口。
/*
* "valid" GPIO numbers are nonnegative and may be passed to
* setup routines like gpio_request(). only some valid numbers
* can successfully be requested and used.
*
* Invalid GPIO numbers are useful for indicating no-such-GPIO in
* platform data and other tables.
*/
static inline bool gpio_is_valid(int number)
{
return number >= 0 && number < ARCH_NR_GPIOS;
}
函数gpio_is_valid()用来判断获取到的gpio号是否是有效的,只有有效的gpio号,才能向内核中进行申请使用,因此,当我们从设备树的设备节点获取到gpio号,可以使用该函数进行判断是否有效。
/* Always use the library code for GPIO management calls,
* or when sleeping may be involved.
*/
extern int gpio_request(unsigned gpio, const char *label);
extern void gpio_free(unsigned gpio);
上面这两个函数用来向系统中申请GPIO和释放已经申请的GPIO,在函数gpio_request()中传入的形参中,gpio为IO号,label为向系统中申请GPIO使用的标签,类似于GPIO的名称。
/**
* struct gpio - a structure describing a GPIO with configuration
* @gpio: the GPIO number
* @flags: GPIO configuration as specified by GPIOF_*
* @label: a literal description string of this GPIO
*/
struct gpio {
unsigned gpio;
unsigned long flags;
const char *label;
};
结构体struct gpio用来描述一个需要配置的GPIO。
extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
extern int gpio_request_array(const struct gpio *array, size_t num);
extern void gpio_free_array(const struct gpio *array, size_t num);
上面的3个函数也是用来向系统申请或者释放GPIO资源,函数gpio_request_one()用来申请单个GPIO,但是在申请的时候可以设置flag标志,例如,该函数在申请GPIO资源的同时,直接将GPIO的方向设置为输入或者输出,函数gpio_request_array()和gpio_free_array()用来向系统中申请或者释放多个GPIO资源。
/* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
struct device;
int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
int devm_gpio_request_one(struct device *dev, unsigned gpio,
unsigned long flags, const char *label);
void devm_gpio_free(struct device *dev, unsigned int gpio);
上面的3个函数也是用来向系统申请或者释放GPIO资源,但是函数带有devm_前缀,也就是说,这是带设备资源管理版本的函数,因此在使用上面的函数时,需要指定设备的struct device指针。
static inline int gpio_direction_input(unsigned gpio)
{
return gpiod_direction_input(gpio_to_desc