送给从STM32库或者其他单片机爬过来的熊孩纸
操作GPIO还是这个顺序:
1:开时钟(linux不用管)
2:设置属性
3:设置方向
4:看是否需要设置上下拉
我这个版本是exynos5260平台的,常规IO操作方式如下
通常在配置一个GPIO口时,首先通过gpio_request函数申请GPIO口,再通过s3c_gpio_cfgpin、s3c_gpio_setpull、gpio_set_value函数配置GPIO口,在不用时通过gpio_free函数释放GPIO口。
相关源码路径:
/*
kernel/drivers/gpio/gpiolib.c
kernel/arm/alpha/include/asm/gpio.h(和gpiolib的内容直接关联,由于三星源码版本,所以路径可能有差异)
kernel/drivers/gpio/gpio-samsung.c(所有API的source都在这里)
kernel/arch/arm/mach-exynos/include/mach/gpio.h
kernel/arch/arm/plat-samsung/include/plat/gpio-cfg.h
(老版本是linux/arch/arm/plat-s3c/include/plat/gpio-cfg.h,所有要用的API都集中在这个文件,还带参数宏,有故事的同学直接戳这个文件即可)
*/
内核中已经将GPIO的相关操作做好了,我们只需要调用即可。这里只给出几种常用的操作函数的使用方法,具体实现过程可以琢磨源码(kernel/drivers/gpio/gpio-samsung.c)。
一:设置GPIO上下拉属性的函数
// kernel/drivers/gpio/gpio-samsung.c
int s3c_gpio_setpull(unsigned int pin, samsung_gpio_pull_t pull)
{
struct samsung_gpio_chip *chip = samsung_gpiolib_getchip(pin);
int offset, ret;
if (!chip)
return -EINVAL;
offset = pin - chip->chip.base;
ret = samsung_gpio_do_setpull(chip, offset, pull);
return ret;
}
EXPORT_SYMBOL(s3c_gpio_setpull);
第一个传入参数表示需要设置的管脚编号,第二个传入参数表示上下拉的属性。每一个GPIO的管脚编号都可以通过宏来指定,这些宏都在kernel/arch/arm/mach-exynos/include/mach/gpio.h中定义。按照5260datasheet的寄存器介绍,GPIO可以配置成none,上拉或下拉三种状态,对应宏定义如下:
//kernel/arch/arm/plat-samsung/include/plat/gpio-cfg.h
/* Define values for the pull-{up,down} available for each gpio pin.
*
* These values control the state of the weak pull-{up,down} resistors
* available on most pins on the S3C series. Not all chips support both
* up or down settings, and it may be dependent on the chip that is being
* used to whether the particular mode is available.
*/
#define S3C_GPIO_PULL_NONE ((__force samsung_gpio_pull_t)0x00)
#define S3C_GPIO_PULL_DOWN ((__force samsung_gpio_pull_t)0x01)
#define S3C_GPIO_PULL_UP ((__force samsung_gpio_pull_t)0x03)
比如设置GPX0-1为上拉:
s3c_gpio_setpull(EXYNOS5260_GPX0(1), S3C_GPIO_PULL_UP);
二:设置GPIO功能属性的函数
// kernel/drivers/gpio/gpio-samsung.c
int s3c_gpio_cfgpin(unsigned int pin, unsigned int config)
{
struct samsung_gpio_chip *chip = samsung_gpiolib_getchip(pin);
int offset;
int ret;
if (!chip)
return -EINVAL;
offset = pin - chip->chip.base;
ret = samsung_gpio_do_setcfg(chip, offset, config);
return ret;
}
EXPORT_SYMBOL(s3c_gpio_cfgpin);
第一个传入参数表示管脚编号,第二个传入参数表示配置信息,如下宏可以表述管脚的配置状态:
#define S3C_GPIO_SPECIAL_MARK (0xfffffff0)
#define S3C_GPIO_SPECIAL(x) (S3C_GPIO_SPECIAL_MARK | (x))
/* Defines for generic pin configurations */
#define S3C_GPIO_INPUT (S3C_GPIO_SPECIAL(0))
#define S3C_GPIO_OUTPUT (S3C_GPIO_SPECIAL(1))
#define S3C_GPIO_SFN(x) (S3C_GPIO_SPECIAL(x))
#define samsung_gpio_is_cfg_special(_cfg) \
(((_cfg) & S3C_GPIO_SPECIAL_MARK) == S3C_GPIO_SPECIAL_MARK)
S3C_GPIO_SFN(0)或S3C_GPIO_INPUT表示将管脚设置为输入,S3C_GPIO_SFN(1)或S3C_GPIO_OUTPUT表示将管脚设置为输出,将管脚设置为其他复合的功能时,需参考5260芯片用户手册的寄存器参数表,结合S3C_GPIO_SFN宏使用。
以下程序表示将GPX0(1)管脚设置为输出:
s3c_gpio_cfgpin(EXYNOS5260_GPX0(1), S3C_GPIO_SFN(1));
当GPIO被设置为输出时,设置其电平状态属性的函数清单如下:
//kernel/arm/alpha/include/asm/gpio.h
static inline void gpio_set_value(unsigned int gpio, int value)
{
__gpio_set_value(gpio, value);
}
//static inline int gpio_to_irq(unsigned int gpio)也在这个文件中
//kernel/drivers/gpio/gpiolib.c
/** * __gpio_set_value() - assign a gpio's value * @gpio: gpio whose value will be assigned * @value: value to assign * Context: any * * This is used directly or indirectly to implement gpio_set_value(). * It invokes the associated gpio_chip.set() method. */
void __gpio_set_value(unsigned gpio, int value)
{
struct gpio_chip *chip;
chip = gpio_to_chip(gpio);
/* Should be using gpio_set_value_cansleep() */
WARN_ON(chip->can_sleep);
trace_gpio_value(gpio, 0, value);
if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
_gpio_set_open_drain_value(gpio, chip, value);
else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
_gpio_set_open_source_value(gpio, chip, value);
else
chip->set(chip, gpio - chip->base, value);
}
EXPORT_SYMBOL_GPL(__gpio_set_value);
gpio表示管脚编号,value表示电平状态,0表示低电平,1表示高电平。如下程序表示将GPD0(1)管脚设置为低电平:
gpio_set_value(EXYNOS5260_GPX0(1), 0);
gpio_request和gpio_free函数分别用于申请GPIO和释放GPIO,强烈建议在使用GPIO时,首先申请GPIO,用完后释放,避免IO口冲突。申请GPD0(1)作为GPIO口的程序清单如下:
gpio_request(EXYNOS5260_GPX0(1), "GPX0(1)");
这里第一个传入参数为IO口的管脚编号,第二个为IO口的名称,可自定义。释放GPX0(1)的程序清单如下:
gpio_free(EXYNOS5260_GPX0(1));
通常在配置一个GPIO口时,首先通过gpio_request函数申请GPIO口,再通过s3c_gpio_cfgpin、s3c_gpio_setpull、gpio_set_value函数配置GPIO口,在不用时通过gpio_free函数释放GPIO口。
最后直接贴完整的三星源码方便参考:
/* linux/arch/arm/plat-s3c/include/plat/gpio-cfg.h * * Copyright 2008 Openmoko, Inc. * Copyright 2008 Simtec Electronics * http://armlinux.simtec.co.uk/ * Ben Dooks <[email protected]> * * S3C Platform - GPIO pin configuration * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */
/* This file contains the necessary definitions to get the basic gpio * pin configuration done such as setting a pin to input or output or * changing the pull-{up,down} configurations. */
/* Note, this interface is being added to the s3c64xx arch first and will * be added to the s3c24xx systems later. */
#ifndef __PLAT_GPIO_CFG_H
#define __PLAT_GPIO_CFG_H __FILE__
#include<linux/types.h>
typedef unsigned int __bitwise__ samsung_gpio_pull_t;
typedef unsigned int __bitwise__ s5p_gpio_drvstr_t;
typedef unsigned int __bitwise__ s5p_gpio_pd_cfg_t;
typedef unsigned int __bitwise__ s5p_gpio_data_t;
typedef unsigned int __bitwise__ s5p_gpio_pd_pull_t;
/* forward declaration if gpio-core.h hasn't been included */
struct samsung_gpio_chip;
/** * 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 configuraiton 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);
};
#define S3C_GPIO_SPECIAL_MARK (0xfffffff0)
#define S3C_GPIO_SPECIAL(x) (S3C_GPIO_SPECIAL_MARK | (x))
/* Defines for generic pin configurations */
#define S3C_GPIO_INPUT (S3C_GPIO_SPECIAL(0))
#define S3C_GPIO_OUTPUT (S3C_GPIO_SPECIAL(1))
#define S3C_GPIO_SFN(x) (S3C_GPIO_SPECIAL(x))
#define samsung_gpio_is_cfg_special(_cfg) \
(((_cfg) & S3C_GPIO_SPECIAL_MARK) == S3C_GPIO_SPECIAL_MARK)
/** * s3c_gpio_cfgpin() - Change the GPIO function of a pin. * @pin pin The pin number to configure. * @to to The configuration for the pin's function. * * Configure which function is actually connected to the external * pin, such as an gpio input, output or some form of special function * connected to an internal peripheral block. * * The @to parameter can be one of the generic S3C_GPIO_INPUT, S3C_GPIO_OUTPUT * or S3C_GPIO_SFN() to indicate one of the possible values that the helper * will then generate the correct bit mask and shift for the configuration. * * If a bank of GPIOs all needs to be set to special-function 2, then * the following code will work: * * for (gpio = start; gpio < end; gpio++) * s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN(2)); * * The @to parameter can also be a specific value already shifted to the * correct position in the control register, although these are discouraged * in newer kernels and are only being kept for compatibility. */
extern int s3c_gpio_cfgpin(unsigned int pin, unsigned int to);
/** * s3c_gpio_getcfg - Read the current function for a GPIO pin * @pin: The pin to read the configuration value for. * * Read the configuration state of the given @pin, returning a value that * could be passed back to s3c_gpio_cfgpin(). * * @sa s3c_gpio_cfgpin */
extern unsigned s3c_gpio_getcfg(unsigned int pin);
/** * s3c_gpio_cfgpin_range() - Change the GPIO function for configuring pin range * @start: The pin number to start at * @nr: The number of pins to configure from @start. * @cfg: The configuration for the pin's function * * Call s3c_gpio_cfgpin() for the @nr pins starting at @start. * * @sa s3c_gpio_cfgpin. */
extern int s3c_gpio_cfgpin_range(unsigned int start, unsigned int nr,
unsigned int cfg);
/* Define values for the pull-{up,down} available for each gpio pin. * * These values control the state of the weak pull-{up,down} resistors * available on most pins on the S3C series. Not all chips support both * up or down settings, and it may be dependent on the chip that is being * used to whether the particular mode is available. */
#define S3C_GPIO_PULL_NONE ((__force samsung_gpio_pull_t)0x00)
#define S3C_GPIO_PULL_DOWN ((__force samsung_gpio_pull_t)0x01)
#define S3C_GPIO_PULL_UP ((__force samsung_gpio_pull_t)0x03)
/** * s3c_gpio_setpull() - set the state of a gpio pin pull resistor * @pin: The pin number to configure the pull resistor. * @pull: The configuration for the pull resistor. * * This function sets the state of the pull-{up,down} resistor for the * specified pin. It will return 0 if successful, or a negative error * code if the pin cannot support the requested pull setting. * * @pull is one of S3C_GPIO_PULL_NONE, S3C_GPIO_PULL_DOWN or S3C_GPIO_PULL_UP. */
extern int s3c_gpio_setpull(unsigned int pin, samsung_gpio_pull_t pull);
/** * s3c_gpio_getpull() - get the pull resistor state of a gpio pin * @pin: The pin number to get the settings for * * Read the pull resistor value for the specified pin. */
extern samsung_gpio_pull_t s3c_gpio_getpull(unsigned int pin);
/* configure `all` aspects of an gpio */
/** * s3c_gpio_cfgall_range() - configure range of gpio functtion and pull. * @start: The gpio number to start at. * @nr: The number of gpio to configure from @start. * @cfg: The configuration to use * @pull: The pull setting to use. * * Run s3c_gpio_cfgpin() and s3c_gpio_setpull() over the gpio range starting * @gpio and running for @size. * * @sa s3c_gpio_cfgpin * @sa s3c_gpio_setpull * @sa s3c_gpio_cfgpin_range */
extern int s3c_gpio_cfgall_range(unsigned int start, unsigned int nr,
unsigned int cfg, samsung_gpio_pull_t pull);
static inline int s3c_gpio_cfgrange_nopull(unsigned int pin, unsigned int size,
unsigned int cfg)
{
return s3c_gpio_cfgall_range(pin, size, cfg, S3C_GPIO_PULL_NONE);
}
/* Define values for the drvstr available for each gpio pin. * * These values control the value of the output signal driver strength, * configurable on most pins on the S5P series. */
#define S5P_GPIO_DRVSTR_LV1 ((__force s5p_gpio_drvstr_t)0x0)
#ifdef CONFIG_SOC_EXYNOS5260
#define S5P_GPIO_DRVSTR_LV2 ((__force s5p_gpio_drvstr_t)0x1)
#define S5P_GPIO_DRVSTR_LV3 ((__force s5p_gpio_drvstr_t)0x2)
#else
#define S5P_GPIO_DRVSTR_LV2 ((__force s5p_gpio_drvstr_t)0x2)
#define S5P_GPIO_DRVSTR_LV3 ((__force s5p_gpio_drvstr_t)0x1)
#endif
#define S5P_GPIO_DRVSTR_LV4 ((__force s5p_gpio_drvstr_t)0x3)
/** * s5c_gpio_get_drvstr() - get the driver streght value of a gpio pin * @pin: The pin number to get the settings for * * Read the driver streght value for the specified pin. */
extern s5p_gpio_drvstr_t s5p_gpio_get_drvstr(unsigned int pin);
/** * s3c_gpio_set_drvstr() - set the driver streght value of a gpio pin * @pin: The pin number to configure the driver streght value * @drvstr: The new value of the driver strength * * This function sets the driver strength value for the specified pin. * It will return 0 if successful, or a negative error code if the pin * cannot support the requested setting. */
extern int s5p_gpio_set_drvstr(unsigned int pin, s5p_gpio_drvstr_t drvstr);
/* Define values for the power down configuration available for each gpio pin. * * These values control the state of the power down configuration resistors * available on most pins on the S5P series. */
#define S5P_GPIO_PD_OUTPUT0 ((__force s5p_gpio_pd_cfg_t)0x00)
#define S5P_GPIO_PD_OUTPUT1 ((__force s5p_gpio_pd_cfg_t)0x01)
#define S5P_GPIO_PD_INPUT ((__force s5p_gpio_pd_cfg_t)0x02)
#define S5P_GPIO_PD_PREV_STATE ((__force s5p_gpio_pd_cfg_t)0x03)
extern s5p_gpio_data_t s5p_gpio_get_data(unsigned int pin);
extern int s5p_gpio_set_data(unsigned int pin, s5p_gpio_data_t drvstr);
/* Define values for the power down configuration available for each gpio pin. * * These values control the state of the power down configuration resistors * available on most pins on the S5P series. */
#define S5P_GPIO_DATA0 ((__force s5p_gpio_data_t)0x0)
#define S5P_GPIO_DATA1 ((__force s5p_gpio_data_t)0x1)
/** * s5p_gpio_set_pd_cfg() - set the configuration of a gpio power down mode * @pin: The pin number to configure the pull resistor. * @pd_cfg: The configuration for the pwer down mode configuration register. * * This function sets the configuration of the power down mode resistor for the * specified pin. It will return 0 if successful, or a negative error * code if the pin cannot support the requested power down mode. * */
extern int s5p_gpio_set_pd_cfg(unsigned int pin, s5p_gpio_pd_cfg_t pd_cfg);
/** * s5p_gpio_get_pd_cfg() - get the power down mode configuration of a gpio pin * @pin: The pin number to get the settings for * * Read the power down mode resistor value for the specified pin. */
extern s5p_gpio_pd_cfg_t s5p_gpio_get_pd_cfg(unsigned int pin);
/* Define values for the power down pull-{up,down} available for each gpio pin. * * These values control the state of the power down mode pull-{up,down} * resistors available on most pins on the S5P series. */
#define S5P_GPIO_PD_UPDOWN_DISABLE ((__force s5p_gpio_pd_pull_t)0x00)
#define S5P_GPIO_PD_DOWN_ENABLE ((__force s5p_gpio_pd_pull_t)0x01)
#define S5P_GPIO_PD_UP_ENABLE ((__force s5p_gpio_pd_pull_t)0x03)
/** * s5p_gpio_set_pd_pull() - set the pull-{up,down} of a gpio pin power down mode * @pin: The pin number to configure the pull resistor. * @pd_pull: The configuration for the power down mode pull resistor. * * This function sets the configuration of the pull-{up,down} resistor for the * specified pin. It will return 0 if successful, or a negative error * code if the pin cannot support the requested pull setting. * */
extern int s5p_gpio_set_pd_pull(unsigned int pin, s5p_gpio_pd_pull_t pd_pull);
/** * s5p_gpio_get_pd_pull() - get the power down pull resistor config of gpio pin * @pin: The pin number to get the settings for * * Read the power mode pull resistor value for the specified pin. */
extern s5p_gpio_pd_pull_t s5p_gpio_get_pd_pull(unsigned int pin);
/** * s5p_register_gpio_interrupt() - register interrupt support for a gpio group * @pin: The pin number from the group to be registered * * This function registers gpio interrupt support for the group that the * specified pin belongs to. * * The total number of gpio pins is quite large ob s5p series. Registering * irq support for all of them would be a resource waste. Because of that the * interrupt support for standard gpio pins is registered dynamically. * * It will return the irq number of the interrupt that has been registered * or -ENOMEM if no more gpio interrupts can be registered. It is allowed * to call this function more than once for the same gpio group (the group * will be registered only once). */
extern int s5p_register_gpio_interrupt(int pin);
/** s5p_register_gpioint_bank() - add gpio bank for further gpio interrupt * registration (see s5p_register_gpio_interrupt function) * @chain_irq: chained irq number for the gpio int handler for this bank * @start: start gpio group number of this bank * @nr_groups: number of gpio groups handled by this bank * * This functions registers initial information about gpio banks that * can be later used by the s5p_register_gpio_interrupt() function to * enable support for gpio interrupt for particular gpio group. */
#ifdef CONFIG_S5P_GPIO_INT
extern int s5p_register_gpioint_bank(int chain_irq, int start, int nr_groups);
#else
#define s5p_register_gpioint_bank(chain_irq, start, nr_groups) do { } while (0)
#endif
/** * exynos_set_lpa_pdn() - Help to set GPIO power down register before * entering LPA(Low Power Audio) mode. This mode is implemented as a * kind of cpuidle state(C-state). * In this mode, Normal GPIO cannot sustain configuration and need to * configure power down register. */
extern void exynos_set_lpa_pdn(unsigned int gpio_end);
#endif /* __PLAT_GPIO_CFG_H */
/* * Generic GPIO API implementation for Alpha. * * A stright copy of that for PowerPC which was: * * Copyright (c) 2007-2008 MontaVista Software, Inc. * * Author: Anton Vorontsov <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */
#ifndef _ASM_ALPHA_GPIO_H
#define _ASM_ALPHA_GPIO_H
#include <linux/errno.h>
#include <asm-generic/gpio.h>
#ifdef CONFIG_GPIOLIB
/* * We don't (yet) implement inlined/rapid versions for on-chip gpios. * Just call gpiolib. */
static inline int gpio_get_value(unsigned int gpio)
{
return __gpio_get_value(gpio);
}
static inline void gpio_set_value(unsigned int gpio, int value)
{
__gpio_set_value(gpio, value);
}
static inline int gpio_cansleep(unsigned int gpio)
{
return __gpio_cansleep(gpio);
}
static inline int gpio_to_irq(unsigned int gpio)
{
return __gpio_to_irq(gpio);
}
static inline int irq_to_gpio(unsigned int irq)
{
return -EINVAL;
}
#endif /* CONFIG_GPIOLIB */
#endif /* _ASM_ALPHA_GPIO_H */