您的位置:首页 > 运维架构 > Linux

Linux下s3c6410的GPIO操作(4)

2012-05-13 16:14 323 查看
1、前面几篇中,有一篇层调用过一个这样的函数,如下:

static __init void s3c64xx_gpiolib_add_4bit(struct s3c_gpio_chip *chip)

{

chip->chip.direction_input = s3c64xx_gpiolib_4bit_input;

chip->chip.direction_output = s3c64xx_gpiolib_4bit_output;

}

此函数在arch/arm/plat-s3c64xx/gpiolib.c中定义,

/* The s3c64xx_gpiolib_4bit routines are to control the gpio banks where

* the gpio configuration register (GPxCON) has 4 bits per GPIO, as the

* following example:

*

* base + 0x00: Control register, 4 bits per gpio

* gpio n: 4 bits starting at (4*n)

* 0000 = input, 0001 = output, others mean special-function

* base + 0x04: Data register, 1 bit per gpio

* bit n: data bit n

*

* Note, since the data register is one bit per gpio and is at base + 0x4

* we can use s3c_gpiolib_get and s3c_gpiolib_set to change the state of

* the output.

*/

此函数直接定义GPIO端口如A的某一个如A1为输入

static int s3c64xx_gpiolib_4bit_input(struct gpio_chip *chip, unsigned offset)

{

struct s3c_gpio_chip *ourchip =
to_s3c_gpio(chip);

to_s3c_gpio(chip)函数源码如下:这个大家应该很熟悉

static inline struct s3c_gpio_chip *to_s3c_gpio(struct gpio_chip *gpc)

{

return container_of(gpc, struct s3c_gpio_chip, chip);

}

void __iomem *base = ourchip->base;基地址

unsigned long con;

con = __raw_readl(base + OFF_GPCON);

con &= ~(0xf <<
con_4bit_shift(offset));

其中 #define con_4bit_shift(__off) ((__off) * 4)

#define OFF_GPCON (0x00)

#define OFF_GPDAT (0x04)

__raw_writel(con, base + OFF_GPCON);

gpio_dbg("%s: %p: CON now %08lx\n", __func__, base, con);

return 0;

}

static int s3c64xx_gpiolib_4bit_output(struct gpio_chip *chip,

unsigned offset, int value)

{

struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);

void __iomem *base = ourchip->base;

unsigned long con;

unsigned long dat;

con = __raw_readl(base + OFF_GPCON);读控制寄存器

con &= ~(0xf << con_4bit_shift(offset));

con |= 0x1 << con_4bit_shift(offset);设置数值,为写做准备

dat = __raw_readl(base + OFF_GPDAT); 读数据寄存器

if (value)

dat |= 1 << offset;

else

dat &= ~(1 << offset);

__raw_writel(dat, base + OFF_GPDAT);写数据寄存器

__raw_writel(con, base + OFF_GPCON);写控制寄存器

__raw_writel(dat, base + OFF_GPDAT);写数据寄存器

这里有个疑问,为何要写两次数据寄存器?

gpio_dbg("%s: %p: CON %08lx, DAT %08lx\n", __func__, base, con, dat);

return 0;

}

2、同样还有

/* The next set of routines are for the case where the GPIO configuration

* registers are 4 bits per GPIO but there is more than one register (the

* bank has more than 8 GPIOs.

*当某个GPIO端口的IO个数如H大于8个的时候,有两个控制寄存器

* This case is the similar to the 4 bit case, but the registers are as

* follows:

*

* base + 0x00: Control register, 4 bits per gpio (lower 8 GPIOs)

* gpio n: 4 bits starting at (4*n)

* 0000 = input, 0001 = output, others mean special-function

* base + 0x04: Control register, 4 bits per gpio (up to 8 additions GPIOs)

* gpio n: 4 bits starting at (4*n)

* 0000 = input, 0001 = output, others mean special-function

* base + 0x08: Data register, 1 bit per gpio

* bit n: data bit n

*

* To allow us to use the s3c_gpiolib_get and s3c_gpiolib_set routines we

* store the 'base + 0x4' address so that these routines see the data

* register at ourchip->base + 0x04.

*/

static int s3c64xx_gpiolib_4bit2_input(struct gpio_chip *chip, unsigned offset)

{

struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);

void __iomem *base = ourchip->base;

void __iomem *regcon = base;

unsigned long con;

if (offset > 7)

offset -= 8;

else

regcon -= 4;

对大于8个的处理

con = __raw_readl(regcon);

con &= ~(0xf << con_4bit_shift(offset));

__raw_writel(con, regcon);

gpio_dbg("%s: %p: CON %08lx\n", __func__, base, con);

return 0;

}

static int s3c64xx_gpiolib_4bit2_output(struct gpio_chip *chip,

unsigned offset, int value)

{

struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);

void __iomem *base = ourchip->base;

void __iomem *regcon = base;

unsigned long con;

unsigned long dat;

if (offset > 7)

offset -= 8;

else

regcon -= 4;同样

con = __raw_readl(regcon);

con &= ~(0xf << con_4bit_shift(offset));

con |= 0x1 << con_4bit_shift(offset);

dat = __raw_readl(base + OFF_GPDAT);

if (value)

dat |= 1 << offset;

else

dat &= ~(1 << offset);

__raw_writel(dat, base + OFF_GPDAT);

__raw_writel(con, regcon);

__raw_writel(dat, base + OFF_GPDAT);

gpio_dbg("%s: %p: CON %08lx, DAT %08lx\n", __func__, base, con, dat);

return 0;

}

这两个函数和上面的差不多,很简单。

到目前为止,整个arch/arm/plat-s3c64xx/gpiolib.c文件中的源码都被我们看完了,是不是挺简单的。

Linux下s3c6410的GPIO操作(5)的链接

Linux下s3c6410的GPIO操作(1)的链接

Linux下s3c6410的GPIO操作(2)的链接

Linux下s3c6410的GPIO操作(3)的链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: