lilzz
lilzz

Reputation: 5413

What's this linux structure

#define GPIO_CHIP(_bank) { \
    .irq_base = IRQ_GPIO_BASE_ ## _bank, \
    .gpio_chip = { \
        .label = "Bank " # _bank, \
        .owner = THIS_MODULE, \
        .set = gpio_set_value, \
        .get = gpio_get_value, \
        .direction_output =gpio_direction_output, \
        .direction_input = gpio_direction_input, \
        .base = GPIO_BASE_ ## _bank, \
        .ngpio =GPIO_NUM_ ## _bank, \
    }, \
}

What's this define with .label and .set and others?

static void gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
{
    uint32_t __iomem *reg = CHIP_TO_REG(chip, REG_GPIO_DATA_SET);
    reg += !value;
    writel(BIT(gpio), reg);
}

This function with writel, __iomen, BIT() , where are they referenced from in Linux?

Upvotes: 1

Views: 243

Answers (1)

hcs
hcs

Reputation: 1534

GPIO_CHIP is used to initialize a struct for a bank of GPIO (General Purpose I/O). If you have

struct s {
   int a, b, c;
   char * d;
}

then you can initialize a variable like

struct s example = { .a = 1, .b = 2, .c = 3, .d = "Hello!" };

You could also do it like

struct s example = { 1, 2, 3, "Hello!" };

but in that case you need to keep track of the order of the members of the struct, it isn't obvious at a glance what 1, 2, etc. actually being used for, and it can get out of sync easily.

If you need to initialize a lot of variables like this you can use a #define which is just the initializer, like

#define S_INIT(num) { .a = num, .b = 2, .c = 3 }
struct s example = S_INIT(0);
struct s examples[] = { S_INIT(1), S_INIT(2), S_INIT(3) };

GPIO_CHIP sets the function pointer .gpio_chip.set in the structure to point to gpio_set_value, so any call to that function is likely through that function pointer.

Consult the kernel documentation for the GPIO driver interface for details.

Upvotes: 1

Related Questions