----------------------------------------------------------------------------------------------------------------------------
内核版本:linux 5.2.8
根文件系统:busybox 1.25.0
u-boot:2016.05
----------------------------------------------------------------------------------------------------------------------------
一、GPIO驱动框架
1.1 GPIO概述
GPIO(General purpose input/putput)也就是通用输入/输出口,片上系统 (SoC) 处理器对 GPIO 有很大的依赖。在某些情况下,每个非专用引脚都可配置为 GPIO,且大多数芯片都最少有一些 GPIO。可编程逻辑器件(类似 FPGA) 可以方便地提供 GPIO。像电源管理和音频编解码器这样的多功能芯片经常留有一些这样的引脚来帮助那些引脚匮乏的 SoC。同时还有通过I2C或SPI串行总线连接的“GPIO扩展器”芯片。
GPIO的实际功能因系统而异。通常用法有:
- 输出值可写 (高电平=1,低电平=0)。一些芯片也有如何驱动这些值的选项,例如只允许输出一个值、支持“线与”及其他取值类似的模式(值得注意的是“开漏”信号);
- 输入值可读(1、0)。一些芯片支持引脚在配置为“输出”时回读,这对于类似“线与”的情况(以支持双向信号)是非常有用的。GPIO 控制器可能有输入去毛刺/消抖逻辑,这有时需要软件控制;
- 输入通常可作为 IRQ 信号,一般是沿触发,但有时是电平触发。这样的 IRQ可能配置为系统唤醒事件,以将系统从低功耗状态下唤醒;
- 通常一个GPIO根据不同产品电路板的需求,可以配置为输入或输出,也有仅支持单向的;
- 大部分GPIO可以在持有自旋锁时访问,但是通常由串行总线扩展的GPIO不允许持有自旋锁。但某些系统也支持这种类型;
比如我们所使用的的S3C2440这款SoC就有130个多功能输入/输出引脚。具体如下:
- 端口A(GPA):25位输入/输出端口 ;
- 端口B(GPB):11 位输入/输出端口 ;
- 端口C(GPC):16 位输入/输出端口;
- 端口D(GPD):16 位输入/输出端口;
- 端口E(GPE):16 位输入/输出端口 ;
- 端口F(GPF):8 位输入/输出端口;
- 端口G(GPG):16 位输入/输出端口;
- 端口H(GPH):9 位输入/输出端口;
- 端口J(GPJ):13 位输入/输出端口;
对于给定的电路板,每个GPIO都用于某个特定的目的,如监控 MMC/SD 卡的插入/移除、检测卡的写保护状态、驱动 LED、配置收发器、模拟串行总线、复位硬件看门狗、感知开关状态等等。
1.2 GPIO框架
在linux内核中,要想使用GPIO口,首先就要了解gpiolib,linux内核对GPIO资源进行了抽象,抽象出来的概念就是gpiolib。
中间层是gpiolib,用于管理系统中的GPIO。gpiolib汇总了GPIO的通用操作,根据GPIO的特性:
- 对上gpiolib提供的一套统一通用的操作GPIO的软件接口,屏蔽了不同芯片的具体实现;
- 对下gpiolib 提供了针对不同芯片操作的一套framework,针对不同芯片,只需要实现gpio controller driver ,然后使用 gpiolib 提供的注册函数,将其挂接到gpiolib 上,这样就完成了这一套东西;
对于其它驱动来说,比如在linux驱动移植-SPI驱动移植(OLED SSD1306),就用到通用的gpiolib的函数来进行I/O口的操作。
1.3 目录结构
linux内核将SPI驱动相关的代码放在drivers/gpio目录下,这下面的文件还是比较多的,我们大概了解一下即可。
GPIO核心功能文件gpiolib.c,提供的一套统一通用的操作GPIO的软件接口。
1.4 标识GPIO
在linux内核,每个GPIO是通过无符号整型来标识的,范围是0到 MAX_INT。保留“负”数用于其他目的,例如标识信号在这个板子上不可用”或指示错误。
平台会定义这些整数的用法,且通常使用 #define来定义GPIO,这样板级特定的启动代码可以直接关联相应的原理图。
例如:一个平台使用编号32-159来标识GPIO;而在另一个平台使用编号0-63标识一组 GPIO控制器,64-79标识另一类 GPIO 控制器;且在一个含有FPGA 的特定板子上使用 80-95来标识GPIO;编号不一定要连续,那些平台中,也可以使用编号2000-2063来标识一个 I2C接口的GPIO扩展器中的GPIO。
二、SPI核心数据结构
学习GPIO驱动,首先要了解驱动框架涉及到的数据结构,知道每个数据结构以及成员的含义之后,再去看源码就容易了。
2.1 struct gpio_chip
看到struct gpio_chip这个结构体,不由而然的想起了中断子系统中我们介绍过的struct irq_chip;内核其使用struct irq_chip对中断控制器的接口抽象;其中的成员大多用于操作底层硬件,比如设置寄存器以屏蔽中断,使能中断,清除中断等。
那么我们不难猜到struct gpio_chip对GPIO控制器的接口抽象,是用于适配不同芯片的一个通用结构;其中的成员用于操作底层的硬件,比如设置为输出/输入模式等。它的定义在 include/linux/gpio/driver.h 文件,如下:
/** * struct gpio_chip - abstract a GPIO controller * @label: a functional name for the GPIO device, such as a part * number or the name of the SoC IP-block implementing it. * @gpiodev: the internal state holder, opaque struct * @parent: optional parent device providing the GPIOs * @owner: helps prevent removal of modules exporting active GPIOs * @request: optional hook for chip-specific activation, such as * enabling module power and clock; may sleep * @free: optional hook for chip-specific deactivation, such as * disabling module power and clock; may sleep * @get_direction: returns direction for signal "offset", 0=out, 1=in, * (same as GPIOF_DIR_XXX), or negative error. * It is recommended to always implement this function, even on * input-only or output-only gpio chips. * @direction_input: configures signal "offset" as input, or returns error * This can be omitted on input-only or output-only gpio chips. * @direction_output: configures signal "offset" as output, or returns error * This can be omitted on input-only or output-only gpio chips. * @get: returns value for signal "offset", 0=low, 1=high, or negative error * @get_multiple: reads values for multiple signals defined by "mask" and * stores them in "bits", returns 0 on success or negative error * @set: assigns output value for signal "offset" * @set_multiple: assigns output values for multiple signals defined by "mask" * @set_config: optional hook for all kinds of settings. Uses the same * packed config format as generic pinconf. * @to_irq: optional hook supporting non-static gpio_to_irq() mappings; * implementation may not sleep * @dbg_show: optional routine to show contents in debugfs; default code * will be used when this is omitted, but custom code can show extra * state (such as pullup/pulldown configuration). * @base: identifies the first GPIO number handled by this chip; * or, if negative during registration, requests dynamic ID allocation. * DEPRECATION: providing anything non-negative and nailing the base * offset of GPIO chips is deprecated. Please pass -1 as base to * let gpiolib select the chip base in all possible cases. We want to * get rid of the static GPIO number space in the long run. * @ngpio: the number of GPIOs handled by this controller; the last GPIO * handled is (base + ngpio - 1). * @names: if set, must be an array of strings to use as alternative * names for the GPIOs in this chip. Any entry in the array * may be NULL if there is no alias for the GPIO, however the * array must be @ngpio entries long. A name can include a single printk * format specifier for an unsigned int. It is substituted by the actual * number of the gpio. * @can_sleep: flag must be set iff get()/set() methods sleep, as they * must while accessing GPIO expander chips over I2C or SPI. This * implies that if the chip supports IRQs, these IRQs need to be threaded * as the chip access may sleep when e.g. reading out the IRQ status * registers. * @read_reg: reader function for generic GPIO * @write_reg: writer function for generic GPIO * @be_bits: if the generic GPIO has big endian bit order (bit 31 is representing * line 0, bit 30 is line 1 ... bit 0 is line 31) this is set to true by the * generic GPIO core. It is for internal housekeeping only. * @reg_dat: data (in) register for generic GPIO * @reg_set: output set register (out=high) for generic GPIO * @reg_clr: output clear register (out=low) for generic GPIO * @reg_dir_out: direction out setting register for generic GPIO * @reg_dir_in: direction in setting register for generic GPIO * @bgpio_dir_unreadable: indicates that the direction register(s) cannot * be read and we need to rely on out internal state tracking. * @bgpio_bits: number of register bits used for a generic GPIO i.e. * <register width> * 8 * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep * shadowed and real data registers writes together. * @bgpio_data: shadowed data register for generic GPIO to clear/set bits * safely. * @bgpio_dir: shadowed direction register for generic GPIO to clear/set * direction safely. A "1" in this word means the line is set as * output. * * A gpio_chip can help platforms abstract various sources of GPIOs so * they can all be accessed through a common programing interface. * Example sources would be SOC controllers, FPGAs, multifunction * chips, dedicated GPIO expanders, and so on. * * Each chip controls a number of signals, identified in method calls * by "offset" values in the range 0..(@ngpio - 1). When those signals * are referenced through calls like gpio_get_value(gpio), the offset * is calculated by subtracting @base from the gpio number. */ struct gpio_chip { const char *label; struct gpio_device *gpiodev; struct device *parent; struct module *owner; int (*request)(struct gpio_chip *chip, unsigned offset); void (*free)(struct gpio_chip *chip, unsigned offset); int (*get_direction)(struct gpio_chip *chip, unsigned offset); int (*direction_input)(struct gpio_chip *chip, unsigned offset); int (*direction_output)(struct gpio_chip *chip, unsigned offset, int value); int (*get)(struct gpio_chip *chip, unsigned offset); int (*get_multiple)(struct gpio_chip *chip, unsigned long *mask, unsigned long *bits); void (*set)(struct gpio_chip *chip, unsigned offset, int value); void (*set_multiple)(struct gpio_chip *chip, unsigned long *mask, unsigned long *bits); int (*set_config)(struct gpio_chip *chip, unsigned offset, unsigned long config); int (*to_irq)(struct gpio_chip *chip, unsigned offset); void (*dbg_show)(struct seq_file *s, struct gpio_chip *chip); int (*init_valid_mask)(struct gpio_chip *chip); int base; u16 ngpio; const char *const *names; bool can_sleep; #if IS_ENABLED(CONFIG_GPIO_GENERIC) unsigned long (*read_reg)(void __iomem *reg); void (*write_reg)(void __iomem *reg, unsigned long data); bool be_bits; void __iomem *reg_dat; void __iomem *reg_set; void __iomem *reg_clr; void __iomem *reg_dir_out; void __iomem *reg_dir_in; bool bgpio_dir_unreadable; int bgpio_bits; spinlock_t bgpio_lock; unsigned long bgpio_data; unsigned long bgpio_dir; #endif #ifdef CONFIG_gpiolib_IRQCHIP /* * With CONFIG_gpiolib_IRQCHIP we get an irqchip inside the gpiolib * to handle IRQs for most practical cases. */ /** * @irq: * * Integrates interrupt chip functionality with the GPIO chip. Can be * used to handle IRQs for most practical cases. */ struct gpio_irq_chip irq; #endif /** * @need_valid_mask: * * If set core allocates @valid_mask with all its values initialized * with init_valid_mask() or set to one if init_valid_mask() is not * defined */ bool need_valid_mask; /** * @valid_mask: * * If not %NULL holds bitmask of GPIOs which are valid to be used * from the chip. */ unsigned long *valid_mask; #if defined(CONFIG_OF_GPIO) /* * If CONFIG_OF is enabled, then all GPIO controllers described in the * device tree automatically may have an OF translation */ /** * @of_node: * * Pointer to a device tree node representing this GPIO controller. */ struct device_node *of_node; /** * @of_gpio_n_cells: * * Number of cells used to form the GPIO specifier. */ unsigned int of_gpio_n_cells; /** * @of_xlate: * * Callback to translate a device tree GPIO specifier into a chip- * relative GPIO number and flags. */ int (*of_xlate)(struct gpio_chip *gc, const struct of_phandle_args *gpiospec, u32 *flags); #endif };
其中部分参数含义如下:
- label:标签,比如GPA、GPB;
- parent:提供GPIO的可父设备;
- request :用于SoC激活的可选回调函数,比如启用模块电源或时钟。如果提供了,在调用gpio_request()或gpiod_get()时,它会在分配GPIO之前执行;
- free: 是一个可选的回调函数,用于特定芯片的释放,比如禁用模块电源或时钟。如果提供了,那么在调用gpiod_put()或gpio_free()时,它会在GPIO被释放之前执行;
- get_direction :用于判断偏移为offset的GPIO是输入还是输出模式。返回值应为0表示out,,1表示in,或负错误;
- direction_input :将偏移为offsert的GPIO配置为输入;
- direction_output :将偏移为offset的GPIO配置为输出;
- get:获取偏移为offset的GPIO的的输出电平,0低电平,1电平;
- set:设置偏移为offset的GPIO的的输出电平,0低电平,1电平;
- base:GPIO控制器的第一个GPIO编号,如果省略,它将动态分配;
- ngpio:GPIO控制器包含的GPIO数量;
- to_irq:将偏移为offset的GPIO映射到IRQ并返回相关的编号;
一般而言,一款芯片的所有GPIO引脚都是支持配置的,默认情况下一般为输入。除了作为输入/输出,可能还提供引脚复用的功能,要使用GPIO,我们首先需要配置为输入/输出。
针对GPIO,有一些通用的特性,比如设置GPIO的方向,读取GPIO的电平,设置GPIO输出的电平,将GPIO作为外部中断输入等。
struct gpio_chip的抽象,实际上就是对GPIO一组Bank的抽象,通常在硬件上,一个SoC对IO口来说,分为了很多个Bank,每个Bank分为n组GPIO。
比如:1个SoC将I/O分为了4 个Bank:
Bank1 | GPIOA~GPIOB |
Bank2 | GPIOC~GPIOD |
Bank3 | GPIOE~GPIOF |
Bank4 | GPIOG~GPIOH |
每个Bank都有n个寄存器来控制GPIO的操作,比如,Bank1针对GPIOA:
GPACON | 配置端口A的引脚:输入、输出、功能复用 |
GPADAT | 配置端口A的数据寄存器 |
GPAUP | 端口A的上拉使能寄存器 |
当然Bank1针对GPIOB,也有n个寄存器来控制GPIO的操作:
GPBCON | 配置端口B的引脚:输入、输出、功能复用 |
GPBDAT | 配置端口B的数据寄存器 |
GPBUP | 端口B的上拉使能寄存器 |
所以整体结构是如下所示(这里只是打个比方,有的芯片 Bank 很多,寄存器也很多):
linux内核使用struct gpio_chip对一组Bank描述,当一个SoC存在多组Bank时,就可是使用数组来描述。
2.2 struct gpio_desc
struct gpio_desc同中断子系统中的struct irq_desc类似,在中断子系统中使用struct irq_desc来描述一个IRQ,那这里就是通过struct gpio_desc去描述某一个G具体的PIO实体。它的定义在drivers/gpio/gpiolib.h文件,如下:
struct gpio_desc { struct gpio_device *gdev; unsigned long flags; /* flag symbols are bit numbers */ #define FLAG_REQUESTED 0 #define FLAG_IS_OUT 1 #define FLAG_EXPORT 2 /* protected by sysfs_lock */ #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ #define FLAG_ACTIVE_LOW 6 /* value has active low */ #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */ #define FLAG_IS_HOGGED 11 /* GPIO is hogged */ #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */ #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */ #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ /* Connection label */ const char *label; /* Name of the GPIO */ const char *name; };
其中部分参数含义如下:
- gdev:指向了一个gpio_device;内核是通过这个指针将gpio_chip和gpio_desc连接起来的,这个也和中断子系统中的irq_data数据结构类似;
- flags:GPIO属性标志,比如上拉使能,开漏输出等;
- lable:标签;
- name:名称;
2.3 struct gpio_device
struct gpio_device这个结构是一个比较重要的数据结构,它真正实现了对一个Bank的GPIO管理,其定义在drivers/gpio/gpiolib.h文件,如下:
/** * struct gpio_device - internal state container for GPIO devices * @id: numerical ID number for the GPIO chip * @dev: the GPIO device struct * @chrdev: character device for the GPIO device * @mockdev: class device used by the deprecated sysfs interface (may be * NULL) * @owner: helps prevent removal of modules exporting active GPIOs * @chip: pointer to the corresponding gpiochip, holding static * data for this device * @descs: array of ngpio descriptors. * @ngpio: the number of GPIO lines on this GPIO device, equal to the size * of the @descs array. * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned * at device creation time. * @label: a descriptive name for the GPIO device, such as the part number * or name of the IP component in a System on Chip. * @data: per-instance data assigned by the driver * @list: links gpio_device:s together for traversal * * This state container holds most of the runtime variable data * for a GPIO device and can hold references and live on after the * GPIO chip has been removed, if it is still being used from * userspace. */ struct gpio_device { int id; struct device dev; struct cdev chrdev; struct device *mockdev; struct module *owner; struct gpio_chip *chip; struct gpio_desc *descs; int base; u16 ngpio; const char *label; void *data; struct list_head list; #ifdef CONFIG_PINCTRL /* * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally * describe the actual pin range which they serve in an SoC. This * information would be used by pinctrl subsystem to configure * corresponding pins for gpio usage. */ struct list_head pin_ranges; #endif };
其中部分参数含义如下:
- id:标识是第几个GPIO控制器;
- dev:设备模型使用;把gpio_device看做是device的子类;
- chrdev:GPIO设备的字符设备;
- chip:指向一个gpio_chip,描述所属芯片GPIO的操作集;
- descs:指向一个gpio_desc数组,每一个元素都描述了一个GPIO;
- base:在全局GPIO编号空间中的编号基址;
- ngpio:支持多少个GPIO,等于descs数组的大小;
- label:GPIO设备的描述性名称
- data:驱动程序分配的每个实例数据;
- list:用于构建双向链表,将gpio_device链接在一起;
这个结构贯穿了整个 gpiolib,因为gpio_device代表的是一个Bank,一个SoC有多个 Bank,所以linux内核中,将gpio_device链接成一个双向链表,在 drivers/gpio/gpiolib.c定义:
LIST_HEAD(gpio_devices);
2.4 数据结构关系图
下图展示了gpio_chip、gpio_desc、gepio_device之间的关系:
实际上同一Bank1下的用来描述每个GPIO实例的gpio_desc还需要指向同一个gpio_device,不过上图没有画出来。
三、GPIO框架API
GPIO框架对外提供的API几乎都可以在include/linux/gpio.h中找,其实现代码一般都位于drivers/gpio/gpiolib.c文件,主要都是围着上面数据结构进行的。
3.1 注册GPIO资源
通过上面对GPIO相关数据结构的分析,我们大概可以看出来,编写GPIO控制器驱动的话,实际上就是要构建gpio_chip,根据所使用的SoC寄存器信息去编写通用的操作函数。
构建完gpio_chip之后,就需要调用gpiochip_add函数将gpio_chip注册到gpiolib子系统。gpiochip_add函数定义在include/linux/gpio/driver.h文件中:
static inline int gpiochip_add(struct gpio_chip *chip) { return gpiochip_add_data(chip, NULL); }
3.1.1 gpiochip_add_data
gpiochip_add其内部调用了gpiochip_add_data,同样是在该文件中定义:
/** * gpiochip_add_data() - register a gpio_chip * @chip: the chip to register, with chip->base initialized * @data: driver-private data associated with this chip * * Context: potentially before irqs will work * * When gpiochip_add_data() is called very early during boot, so that GPIOs * can be freely used, the chip->parent device must be registered before * the gpio framework's arch_initcall(). Otherwise sysfs initialization * for GPIOs will fail rudely. * * gpiochip_add_data() must only be called after gpiolib initialization, * ie after core_initcall(). * * If chip->base is negative, this requests dynamic assignment of * a range of valid GPIOs. * * Returns: * A negative errno if the chip can't be registered, such as because the * chip->base is invalid or already associated with a different chip. * Otherwise it returns zero as a success code. */ #ifdef CONFIG_LOCKDEP #define gpiochip_add_data(chip, data) ({ \ static struct lock_class_key lock_key; \ static struct lock_class_key request_key; \ gpiochip_add_data_with_key(chip, data, &lock_key, \ &request_key); \ }) #else #define gpiochip_add_data(chip, data) gpiochip_add_data_with_key(chip, data, NULL, NULL) #endif
3.1.2 gpiochip_add_data_with_key
gpiochip_add_data函数又调用了gpiochip_add_data_with_key,其定义在drivers/gpio/gpiolib.c文件
int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, struct lock_class_key *lock_key, struct lock_class_key *request_key) { unsigned long flags; int status = 0; unsigned i; int base = chip->base; struct gpio_device *gdev; /* * First: allocate and populate the internal stat container, and * set up the struct device. */ gdev = kzalloc(sizeof(*gdev), GFP_KERNEL); if (!gdev) return -ENOMEM; gdev->dev.bus = &gpio_bus_type; gdev->chip = chip; chip->gpiodev = gdev; if (chip->parent) { gdev->dev.parent = chip->parent; gdev->dev.of_node = chip->parent->of_node; } #ifdef CONFIG_OF_GPIO /* If the gpiochip has an assigned OF node this takes precedence */ if (chip->of_node) gdev->dev.of_node = chip->of_node; else chip->of_node = gdev->dev.of_node; #endif gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL); if (gdev->id < 0) { status = gdev->id; goto err_free_gdev; } dev_set_name(&gdev->dev, "gpiochip%d", gdev->id); device_initialize(&gdev->dev); dev_set_drvdata(&gdev->dev, gdev); if (chip->parent && chip->parent->driver) gdev->owner = chip->parent->driver->owner; else if (chip->owner) /* TODO: remove chip->owner */ gdev->owner = chip->owner; else gdev->owner = THIS_MODULE; gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); if (!gdev->descs) { status = -ENOMEM; goto err_free_ida; } if (chip->ngpio == 0) { chip_err(chip, "tried to insert a GPIO chip with zero lines\n"); status = -EINVAL; goto err_free_descs; } if (chip->ngpio > FASTPATH_NGPIO) chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n", chip->ngpio, FASTPATH_NGPIO); gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL); if (!gdev->label) { status = -ENOMEM; goto err_free_descs; } gdev->ngpio = chip->ngpio; gdev->data = data; spin_lock_irqsave(&gpio_lock, flags); /* * TODO: this allocates a Linux GPIO number base in the global * GPIO numberspace for this chip. In the long run we want to * get *rid* of this numberspace and use only descriptors, but * it may be a pipe dream. It will not happen before we get rid * of the sysfs interface anyways. */ if (base < 0) { base = gpiochip_find_base(chip->ngpio); if (base < 0) { status = base; spin_unlock_irqrestore(&gpio_lock, flags); goto err_free_label; } /* * TODO: it should not be necessary to reflect the assigned * base outside of the GPIO subsystem. Go over drivers and * see if anyone makes use of this, else drop this and assign * a poison instead. */ chip->base = base; } gdev->base = base; status = gpiodev_add_to_list(gdev); if (status) { spin_unlock_irqrestore(&gpio_lock, flags); goto err_free_label; } spin_unlock_irqrestore(&gpio_lock, flags); for (i = 0; i < chip->ngpio; i++) gdev->descs[i].gdev = gdev; #ifdef CONFIG_PINCTRL INIT_LIST_HEAD(&gdev->pin_ranges); #endif status = gpiochip_set_desc_names(chip); if (status) goto err_remove_from_list; status = gpiochip_irqchip_init_valid_mask(chip); if (status) goto err_remove_from_list; status = gpiochip_alloc_valid_mask(chip); if (status) goto err_remove_irqchip_mask; status = gpiochip_add_irqchip(chip, lock_key, request_key); if (status) goto err_free_gpiochip_mask; status = of_gpiochip_add(chip); if (status) goto err_remove_chip; status = gpiochip_init_valid_mask(chip); if (status) goto err_remove_of_chip; for (i = 0; i < chip->ngpio; i++) { struct gpio_desc *desc = &gdev->descs[i]; if (chip->get_direction && gpiochip_line_is_valid(chip, i)) { if (!chip->get_direction(chip, i)) set_bit(FLAG_IS_OUT, &desc->flags); else clear_bit(FLAG_IS_OUT, &desc->flags); } else { if (!chip->direction_input) set_bit(FLAG_IS_OUT, &desc->flags); else clear_bit(FLAG_IS_OUT, &desc->flags); } } acpi_gpiochip_add(chip); machine_gpiochip_add(chip); /* * By first adding the chardev, and then adding the device, * we get a device node entry in sysfs under * /sys/bus/gpio/devices/gpiochipN/dev that can be used for * coldplug of device nodes and other udev business. * We can do this only if gpiolib has been initialized. * Otherwise, defer until later. */ if (gpiolib_initialized) { status = gpiochip_setup_dev(gdev); if (status) goto err_remove_acpi_chip; } return 0; err_remove_acpi_chip: acpi_gpiochip_remove(chip); err_remove_of_chip: gpiochip_free_hogs(chip); of_gpiochip_remove(chip); err_remove_chip: gpiochip_irqchip_remove(chip); err_free_gpiochip_mask: gpiochip_free_valid_mask(chip); err_remove_irqchip_mask: gpiochip_irqchip_free_valid_mask(chip); err_remove_from_list: spin_lock_irqsave(&gpio_lock, flags); list_del(&gdev->list); spin_unlock_irqrestore(&gpio_lock, flags); err_free_label: kfree_const(gdev->label); err_free_descs: kfree(gdev->descs); err_free_ida: ida_simple_remove(&gpio_ida, gdev->id); err_free_gdev: /* failures here can mean systems won't boot... */ pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__, gdev->base, gdev->base + gdev->ngpio - 1, chip->label ? : "generic", status); kfree(gdev); return status; }
参考文章
[1] Linux GPIO 驱动 (gpiolib)(部分转载)
[3]Documentation/translations/zh_CN/gpio.txt
标签:struct,chip,linux,gdev,GPIO,device,gpio,子系统 From: https://www.cnblogs.com/zyly/p/17161900.html