----------------------------------------------------------------------------------------------------------------------------
内核版本:linux 5.2.8
根文件系统:busybox 1.25.0
u-boot:2016.05
----------------------------------------------------------------------------------------------------------------------------
这一节我们来分析Mini2440 GPIO控制器驱动的源码,由于GPIO控制器驱动比较简单,所以这一节内容相对来说也是比较少的。
GPIO控制器驱动编写主要包含两个步骤:
- 为SoC的每个Bank分配一个gpio_chip,并进行初始化;
- 调用gpiochip_add注册每一个gpio_chip;
一、S3C2440 GPIO
根据平台的不同。GPIO控制器的数量也不同,GPIO控制器的寄存器端口映射方式和映射地址都不同,这些都是与平台先关的,需要平台去适配。
当然,不同的GPIO控制器的寄存器数量和功能也不尽相同,一般情况下,GPIO控制器都提供两个寄存器:一个是配置(控制)寄存器,另一个是数据寄存器。
S3C2440提供了9个GPIO控制器,分别为:
- 端口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控制器至少提供了2个32位寄存器:
- 端口配置寄存器:GPACON~GPJCON,配置寄存器的作用是,用来配置该芯片的每根引脚的输入输出状态或者特殊功能等;
- 端口数据寄存器:GPADAT~GPJDAT,如果端口配置为输出端口,可以写入数据到 PnDAT 的相应位。如果端口配置为输入端口,可以从 PnDAT 的相应位读取数据;
- 端口上拉寄存器:GPBUP~GPJUP,端口上拉寄存器控制每个端口组的使能/禁止上拉电阻。当相应位为0时使能引脚的上拉电阻,当为 1 时禁止上拉电阻。如果使能了上拉电阻,那么上拉电阻与引脚的功能设置无关(输入、输出、DATAn、EINTn 等等);
GPIOA的配置寄存器GPACON不同于其他GPIO的配置寄存器:
- 使用1bit控制GPIO的相应引脚状态,32bit寄存器使用低23bit控制23个引脚;
- 当控制位值为0的时候,表示该引脚是通用输出状态;
- 控制位为1的时候,引脚是特殊功能状态,用于控制其他外接器件(此时,数据寄存器GPADAT无效)。
因而,可以看出,GPIOA芯片不能实现通用输入功能。一般来说,GPIOA控制寄存器配置成1,用于控制其他外接器件。
GPIOB~GPIOJ的配置寄存器GPBCON~GPJCON,使用2bit控制芯片的相应引脚状态(bit0,1控制引脚0,bit2,3控制引脚1,以此类推),每个芯片提供的引脚个数不尽相同,但最多只能提供32/2=16个引脚;
- 控制位为00的时候,表示相应引脚处于通用输入状态;
- 控制位为01时,表示相应引脚为通用输出状态;
- 控制位为10时,表示相应引脚为特殊功能状态;
- 控制位为11时,表示相应引脚为特殊功能状态,或者是预留;
在介绍之前,我们先了解一下三星为了GPIO定义的一些数据结构、
1.1 samsung_gpio_cfg
samsung_gpio_cfg这个结构体用来描述三星芯片GPIO的配置,类型定义在arch/arm/plat-samsung/include/plat/gpio-cfg.h:
/** * struct samsung_gpio_cfg GPIO configuration * @cfg_eint: Configuration setting when used for external interrupt source * @get_pull: Read the current pull configuration for the GPIO * @set_pull: Set the current pull configuration for the GPIO * @set_config: Set the current configuration for the GPIO * @get_config: Read the current configuration for the GPIO * * Each chip can have more than one type of GPIO bank available and some * have different capabilites even when they have the same control register * layouts. Provide an point to vector control routine and provide any * per-bank configuration information that other systems such as the * external interrupt code will need. * * @sa samsung_gpio_cfgpin * @sa s3c_gpio_getcfg * @sa s3c_gpio_setpull * @sa s3c_gpio_getpull */ struct samsung_gpio_cfg { unsigned int cfg_eint; samsung_gpio_pull_t (*get_pull)(struct samsung_gpio_chip *chip, unsigned offs); int (*set_pull)(struct samsung_gpio_chip *chip, unsigned offs, samsung_gpio_pull_t pull); unsigned (*get_config)(struct samsung_gpio_chip *chip, unsigned offs); int (*set_config)(struct samsung_gpio_chip *chip, unsigned offs, unsigned config); };
其成员如下:
- cfg_eint:用于外部中断源时的配置;
- get_pull:读取当前GPIO的上拉配置;
- set_pull:设置当前GPIO上拉配置;
- set_config:设置GPIO配置;
- get_config:获取GPIO配置;
1.2 samsung_gpio_chip
三星定义的samsung_gpio_chip结构对gpio_chip又包装了一层,用来表示三星厂家生产的SoC的GPIO控制器,定义在arch/arm/plat-samsung/include/plat/gpio-core.h:
/** * struct samsung_gpio_chip - wrapper for specific implementation of gpio * @chip: The chip structure to be exported via gpiolib. * @base: The base pointer to the gpio configuration registers. * @group: The group register number for gpio interrupt support. * @irq_base: The base irq number. * @config: special function and pull-resistor control information. * @lock: Lock for exclusive access to this gpio bank. * @pm_save: Save information for suspend/resume support. * @bitmap_gpio_int: Bitmap for representing GPIO interrupt or not. * * This wrapper provides the necessary information for the Samsung * specific gpios being registered with gpiolib. * * The lock protects each gpio bank from multiple access of the shared * configuration registers, or from reading of data whilst another thread * is writing to the register set. * * Each chip has its own lock to avoid any contention between different * CPU cores trying to get one lock for different GPIO banks, where each * bank of GPIO has its own register space and configuration registers. */ struct samsung_gpio_chip { struct gpio_chip chip; struct samsung_gpio_cfg *config; struct samsung_gpio_pm *pm; void __iomem *base; int irq_base; int group; spinlock_t lock; #ifdef CONFIG_PM u32 pm_save[4]; #endif u32 bitmap_gpio_int; };
其成员如下:
- chip:gpiolib核心数据结构gpio_chip;
- base:指向GPIO控制器寄存器基地址;
- group:GPIO中断支持的组寄存器编号;
- irq_base:中断base编号;
- config:数据结构samsung_gpio_cfg,用于配置;
- lock:自旋锁;
- bitmap_gpio_int:位映射,用于描述GPIO中断信息;
二、分配gpio_chip
2.1 s3c24xx_gpios
我们定位到arch/arm/plat-samsung/gpio-samsung.c文件:
struct samsung_gpio_chip s3c24xx_gpios[] = { #ifdef CONFIG_PLAT_S3C24XX { .config = &s3c24xx_gpiocfg_banka, .chip = { .base = S3C2410_GPA(0), .owner = THIS_MODULE, .label = "GPIOA", .ngpio = 27, .direction_input = s3c24xx_gpiolib_banka_input, .direction_output = s3c24xx_gpiolib_banka_output, }, }, { .chip = { .base = S3C2410_GPB(0), .owner = THIS_MODULE, .label = "GPIOB", .ngpio = 11, }, }, { .chip = { .base = S3C2410_GPC(0), .owner = THIS_MODULE, .label = "GPIOC", .ngpio = 16, }, }, { .chip = { .base = S3C2410_GPD(0), .owner = THIS_MODULE, .label = "GPIOD", .ngpio = 16, }, }, { .chip = { .base = S3C2410_GPE(0), .label = "GPIOE", .owner = THIS_MODULE, .ngpio = 16, }, }, { .chip = { .base = S3C2410_GPF(0), .owner = THIS_MODULE, .label = "GPIOF", .ngpio = 8, .to_irq = s3c24xx_gpiolib_fbank_to_irq, }, }, { .irq_base = IRQ_EINT8, .chip = { .base = S3C2410_GPG(0), .owner = THIS_MODULE, .label = "GPIOG", .ngpio = 16, .to_irq = samsung_gpiolib_to_irq, }, }, { .chip = { .base = S3C2410_GPH(0), .owner = THIS_MODULE, .label = "GPIOH", .ngpio = 15, }, }, /* GPIOS for the S3C2443 and later devices. */ { .base = S3C2440_GPJCON, .chip = { .base = S3C2410_GPJ(0), .owner = THIS_MODULE, .label = "GPIOJ", .ngpio = 16, }, }, { .base = S3C2443_GPKCON, .chip = { .base = S3C2410_GPK(0), .owner = THIS_MODULE, .label = "GPIOK", .ngpio = 16, }, }, { .base = S3C2443_GPLCON, .chip = { .base = S3C2410_GPL(0), .owner = THIS_MODULE, .label = "GPIOL", .ngpio = 15, }, }, { .base = S3C2443_GPMCON, .chip = { .base = S3C2410_GPM(0), .owner = THIS_MODULE, .label = "GPIOM", .ngpio = 2, }, }, #endif };
定义好了各个Bank 的基地址和 GPIO 的个数。以及支持中断的情况。
2.2 s3c24xx_gpiocfg_banka
由于GPIOA比较特殊,所以s3c24xx_gpios对于GPIOA对config单独配置,s3c24xx_gpiocfg_banka:
static struct samsung_gpio_cfg s3c24xx_gpiocfg_banka = { .set_config = s3c24xx_gpio_setcfg_abank, .get_config = s3c24xx_gpio_getcfg_abank, };
三、模块入口
在arch/arm/plat-samsung/gpio-samsung.c文件,定位到模块入口函数:
/* TODO: cleanup soc_is_* */ static __init int samsung_gpiolib_init(void) { /* * Currently there are two drivers that can provide GPIO support for * Samsung SoCs. For device tree enabled platforms, the new * pinctrl-samsung driver is used, providing both GPIO and pin control * interfaces. For legacy (non-DT) platforms this driver is used. */ if (of_have_populated_dt()) return 0; if (soc_is_s3c24xx()) { // 这行这里 samsung_gpiolib_set_cfg(samsung_gpio_cfgs, ARRAY_SIZE(samsung_gpio_cfgs)); s3c24xx_gpiolib_add_chips(s3c24xx_gpios, ARRAY_SIZE(s3c24xx_gpios), S3C24XX_VA_GPIO); } else if (soc_is_s3c64xx()) { samsung_gpiolib_set_cfg(samsung_gpio_cfgs, ARRAY_SIZE(samsung_gpio_cfgs)); samsung_gpiolib_add_2bit_chips(s3c64xx_gpios_2bit, ARRAY_SIZE(s3c64xx_gpios_2bit), S3C64XX_VA_GPIO + 0xE0, 0x20); samsung_gpiolib_add_4bit_chips(s3c64xx_gpios_4bit, ARRAY_SIZE(s3c64xx_gpios_4bit), S3C64XX_VA_GPIO); samsung_gpiolib_add_4bit2_chips(s3c64xx_gpios_4bit2, ARRAY_SIZE(s3c64xx_gpios_4bit2)); } return 0; } core_initcall(samsung_gpiolib_init);
首先调用samsung_gpiolib_set_cfg初始化samsung_gpio_cfgs数组成员。
调用了 xxx_gpiolib_add_xxxx 的函数,在这一些列的调用中,就将gpio_chip结构体相关的成员进行了赋值,并最终调用到了gpiochip_add_data函数,将其注册到了内核的gpiolib 子系统。
3.1 samsung_gpiolib_set_cfg
samsung_gpiolib_set_cfg函数用来遍历samsung_gpio_chip数组,并依次配置:
- set_config:默认配置为samsung_gpio_setcfg_4bit;
- get_config:默认配置为samsung_gpio_getcfg_4bit;
- set_pull:默认配置为samsung_gpio_setpull_updown;
- get_pull:默认配置为samsung_gpio_getpull_updown;
static void __init samsung_gpiolib_set_cfg(struct samsung_gpio_cfg *chipcfg, int nr_chips) { for (; nr_chips > 0; nr_chips--, chipcfg++) { if (!chipcfg->set_config) chipcfg->set_config = samsung_gpio_setcfg_4bit; if (!chipcfg->get_config) chipcfg->get_config = samsung_gpio_getcfg_4bit; if (!chipcfg->set_pull) chipcfg->set_pull = samsung_gpio_setpull_updown; if (!chipcfg->get_pull) chipcfg->get_pull = samsung_gpio_getpull_updown; } }
samsung_gpio_cfgs数组定义如下,数组长度为8,依次对应S3C2440 GPA~GPH的配置。
static struct samsung_gpio_cfg samsung_gpio_cfgs[] = { [0] = { .cfg_eint = 0x0, }, [1] = { .cfg_eint = 0x3, }, [2] = { .cfg_eint = 0x7, }, [3] = { .cfg_eint = 0xF, }, [4] = { .cfg_eint = 0x0, .set_config = samsung_gpio_setcfg_2bit, .get_config = samsung_gpio_getcfg_2bit, }, [5] = { .cfg_eint = 0x2, .set_config = samsung_gpio_setcfg_2bit, .get_config = samsung_gpio_getcfg_2bit, }, [6] = { .cfg_eint = 0x3, .set_config = samsung_gpio_setcfg_2bit, .get_config = samsung_gpio_getcfg_2bit, }, [7] = { .set_config = samsung_gpio_setcfg_2bit, .get_config = samsung_gpio_getcfg_2bit, }, };
3.2 s3c24xx_gpiolib_add_chips
samsung_gpiolib_set_cfg函数用来遍历samsung_gpio_chip数组,并依次初始化配置config、
static void __init s3c24xx_gpiolib_add_chips(struct samsung_gpio_chip *chip, int nr_chips, void __iomem *base) { int i; struct gpio_chip *gc = &chip->chip; for (i = 0 ; i < nr_chips; i++, chip++) { /* skip banks not present on SoC */ if (chip->chip.base >= S3C_GPIO_END) // #define S3C_GPIO_END (S3C2410_GPJ(0) + 32) 这里跳过了GPJ以及以及后面的接口 continue; if (!chip->config) chip->config = &s3c24xx_gpiocfg_default; // 如果没有设置config,默认是2bit配置操作。对GPIOA,已经显示配置成s3c24xx_gpiocfg_banka if (!chip->pm) chip->pm = __gpio_pm(&samsung_gpio_pm_2bit); // if ((base != NULL) && (chip->base == NULL)) chip->base = base + ((i) * 0x10); // GPIO控制器寄存器基地址 虚拟地址 if (!gc->direction_input) gc->direction_input = samsung_gpiolib_2bit_input; // 配置为输入 平台相关配置函数,默认2bit配置操作 if (!gc->direction_output) gc->direction_output = samsung_gpiolib_2bit_output; // 配置为输出 平台相关配置函数,默认2bit配置操作 samsung_gpiolib_add(chip); // 注册chip } }
执行完上面代码,我们就可以知道:
- GPIOA chip->config设置为了s3c24xx_gpiocfg_banka;
- GPIOB~GPIOJ chip->config设置为了s3c24xx_gpiocfg_default;
GPIOA寄存器基地址设置为:
#define S3C24XX_VA_GPIO ((S3C24XX_PA_GPIO - S3C24XX_PA_UART) + S3C24XX_VA_UART)
GPIOB~GPIO寄存器基地址依次增加0x10。
3.3 samsung_gpiolib_add
/* * samsung_gpiolib_add() - add the Samsung gpio_chip. * @chip: The chip to register * * This is a wrapper to gpiochip_add() that takes our specific gpio chip * information and makes the necessary alterations for the platform and * notes the information for use with the configuration systems and any * other parts of the system. */ static void __init samsung_gpiolib_add(struct samsung_gpio_chip *chip) { struct gpio_chip *gc = &chip->chip; int ret; BUG_ON(!chip->base); // 检查必须参数 BUG_ON(!gc->label); BUG_ON(!gc->ngpio); spin_lock_init(&chip->lock); // 初始化自旋锁 if (!gc->direction_input) gc->direction_input = samsung_gpiolib_2bit_input; // 配置为输入 if (!gc->direction_output) gc->direction_output = samsung_gpiolib_2bit_output; // 配置为输出 if (!gc->set) gc->set = samsung_gpiolib_set; // 设置输出电平 if (!gc->get) gc->get = samsung_gpiolib_get; // 获取输出电平 #ifdef CONFIG_PM if (chip->pm != NULL) { if (!chip->pm->save || !chip->pm->resume) pr_err("gpio: %s has missing PM functions\n", gc->label); } else pr_err("gpio: %s has no PM function\n", gc->label); #endif /* gpiochip_add() prints own failure message on error. */ ret = gpiochip_add_data(gc, chip); // 注册 if (ret >= 0) s3c_gpiolib_track(chip); }
四、
标签:samsung,chip,linux,gpiolib,GPIO,gpio,控制器驱动,config From: https://www.cnblogs.com/zyly/p/17170424.html