转自:http://blog.csdn.net/cnsecs/article/details/6689182
系列函数的定义在arch/arm/mach-s3c2410/gpio.c,相关的宏定义在include/asm-arm/arch-s3c2410/regs-gpio.h
(1)void s3c2410_gpio_setpin(unsigned int pin, unsigned int to);
设置相应GPIO口的输出值,例如:
pin=S3C2410_GPG2,to=0,则设置S3C2410_GPG2的输出值为0;
pin=S3C2410_GPG2,to=1,则设置S3C2410_GPG2的输出值为1。
函数定义:
- void s3c2410_gpio_setpin(unsigned int pin, unsigned int to)
- {
- void __iomem *base = S3C2410_GPIO_BASE(pin);
- unsigned long offs = S3C2410_GPIO_OFFSET(pin);
- unsigned long flags;
- unsigned long dat;
-
- local_irq_save(flags);
-
- dat = __raw_readl(base + 0x04);
- dat &= ~(1 << offs);
- dat |= to << offs;
- __raw_writel(dat, base + 0x04);
-
- local_irq_restore(flags);
- }
- EXPORT_SYMBOL(s3c2410_gpio_setpin);
(2)unsigned int s3c2410_gpio_getpin(unsigned int pin);
获取相应GPIO口的值。
函数定义:
- unsigned int s3c2410_gpio_getpin(unsigned int pin)
- {
- void __iomem *base = S3C2410_GPIO_BASE(pin);
- unsigned long offs = S3C2410_GPIO_OFFSET(pin);
-
- return __raw_readl(base + 0x04) & (1<< offs);
- }
-
- EXPORT_SYMBOL(s3c2410_gpio_getpin);
(3)void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function);
设置相应GPIO口的工作模式,输入、输出、中断等。
函数定义:
- void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function)
- {
- void __iomem *base = S3C2410_GPIO_BASE(pin);
- unsigned long mask;
- unsigned long con;
- unsigned long flags;
-
- if (pin < S3C2410_GPIO_BANKB) {
- mask = 1 << S3C2410_GPIO_OFFSET(pin);
- } else {
- mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
- }
-
- local_irq_save(flags);
-
- con = __raw_readl(base + 0x00);
- con &= ~mask;
- con |= function;
-
- __raw_writel(con, base + 0x00);
-
- local_irq_restore(flags);
- }
-
- EXPORT_SYMBOL(s3c2410_gpio_cfgpin);
(4)unsigned int s3c2410_gpio_getcfg(unsigned int pin);
获取相应GPIO口的工作模式,输入、输出、中断等。
函数定义:
- unsigned int s3c2410_gpio_getcfg(unsigned int pin)
- {
- void __iomem *base = S3C2410_GPIO_BASE(pin);
- unsigned long mask;
-
- if (pin < S3C2410_GPIO_BANKB) {
- mask = 1 << S3C2410_GPIO_OFFSET(pin);
- } else {
- mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
- }
-
- return __raw_readl(base) & mask;
- }
-
- EXPORT_SYMBOL(s3c2410_gpio_getcfg);
(5)void s3c2410_gpio_pullup(unsigned int pin, unsigned int to);
设置相应的GPIO口的电平,例如:
pin=S3C2410_GPG2,to=0,则拉低S3C2410_GPG2,也就是设置S3C2410_GPG2的值为0;
pin=S3C2410_GPG2,to=1,则拉高S3C2410_GPG2,也就是设置S3C2410_GPG2的值为1。
函数定义:
- void s3c2410_gpio_pullup(unsigned int pin, unsigned int to)
- {
- void __iomem *base = S3C2410_GPIO_BASE(pin);
- unsigned long offs = S3C2410_GPIO_OFFSET(pin);
- unsigned long flags;
- unsigned long up;
-
- if (pin < S3C2410_GPIO_BANKB)
- return;
-
- local_irq_save(flags);
-
- up = __raw_readl(base + 0x08);
- up &= ~(1L << offs);
- up |= to << offs;
- __raw_writel(up, base + 0x08);
-
- local_irq_restore(flags);
- }
-
- EXPORT_SYMBOL(s3c2410_gpio_pullup);
(6)unsigned int s3c2410_modify_misccr(unsigned int clear, unsigned int change);
杂项设置,设置寄存器MISCCR,具体看函数定义。
函数定义:
- unsigned int s3c2410_modify_misccr(unsigned int clear, unsigned int change)
- {
- unsigned long flags;
- unsigned long misccr;
-
- local_irq_save(flags);
- misccr = __raw_readl(S3C2410_MISCCR);
- misccr &= ~clear;
- misccr ^= change;
- __raw_writel(misccr, S3C2410_MISCCR);
- local_irq_restore(flags);
-
- return misccr;
- }
-
- EXPORT_SYMBOL(s3c2410_modify_misccr);
(7)int s3c2410_gpio_getirq(unsigned int pin);
获取相应的GPIO口所对应的中断号。
函数定义:
- int s3c2410_gpio_getirq(unsigned int pin)
- {
- if (pin < S3C2410_GPF0 || pin > S3C2410_GPG15_EINT23)
- return -1;
-
- if (pin < S3C2410_GPG0 && pin > S3C2410_GPF7)
- return -1;
-
- if (pin < S3C2410_GPF4)
- return (pin - S3C2410_GPF0) + IRQ_EINT0;
-
- if (pin < S3C2410_GPG0)
- return (pin - S3C2410_GPF4) + IRQ_EINT4;
-
- return (pin - S3C2410_GPG0) + IRQ_EINT8;
- }
-
- EXPORT_SYMBOL(s3c2410_gpio_getirq);
(8)int s3c2410_gpio_irqfilter(unsigned int pin, unsigned int on, unsigned int config);
中断过滤配置,具体看函数定义。
函数定义:
int s3c2410_gpio_irqfilter(unsigned int pin, unsigned int on,
unsigned int config)
{
void __iomem *reg = S3C2410_EINFLT0;
unsigned long flags;
unsigned long val;
if (pin < S3C2410_GPG8 || pin > S3C2410_GPG15)
return -1;
config &= 0xff;
pin -= S3C2410_GPG8_EINT16;
reg += pin & ~3;
local_irq_save(flags);
/* update filter width and clock source */
val = __raw_readl(reg);
val &= ~(0xff << ((pin & 3) * 8));
val |= config << ((pin & 3) * 8);
__raw_writel(val, reg);
/* update filter enable */
val = __raw_readl(S3C2410_EXTINT2);
val &= ~(1 << ((pin * 4) + 3));
val |= on << ((pin * 4) + 3);
__raw_writel(val, S3C2410_EXTINT2);
local_irq_restore(flags);
return 0;
}
EXPORT_SYMBOL(s3c2410_gpio_irqfilter);
***********************************
这里对这些函数的用法进行简要解读:
1.void s3c2410_gpio_cfgpin(unsigned int pin,unsigned int function)
第一个参数pin 是对应的io引脚(这里用宏S3C2410_GPB(5),5不是固定的,看你需要引用的引脚而定)
第二个引脚是设置该引脚的功能的(由S3C2410_GPIO_INPUT,S3C2410_GPIO_OUTPUT,S3C2410_GPIO_SFN2,S3C2410_GPIO_SFN3这4个宏进行定义)
例如:s3c2410_gpio_cfgpin(S3C2410_GPB(5),S3C2410_GPIO_INPUT)设置GPB5引脚为输入。
2.unsigned int s3c2410_gpio_gecfg(unsigned int pin)
作用:返回对应的GPIO的配置情况例如:pin=s3c2410_GPB5返回GPB5的配置情况
3.void s3c2410_gpio_pullup(unsigned int pin,unsigned int to)
作用:设置相应的的GPIO的上拉电阻。
第一个参数:相应的引脚,和1里面的用法一致。
第二个参数:设置为1或者0,1表示上拉,0表示不上拉。
4.void s3c2410_gpio_setpin(unsigned int pin,unsigned int to)
作用:将相应的引脚输出为1或者0。
第一个参数:相应的引脚宏
第二个参数:1或者0
例子:s3c2410_gpio_setpin(S3C2410_GPB(5),1) 将引脚GPB5输出为1
5.unsigned int s3c2410_gpin_getpin(unsigned int pin)
功能:获取输入值
参数:相应的引脚
6.unsigned int s3c2410_modify_misccr(unsigned int clear ,unsigned int change)
功能:杂项设置,设置寄存器MISCCR,具体看函数定义
7.int s3c2410_gpio_getirq(unsigned pin)
功能:获取相应的GPIO口所对应的中断号