lib: utils/gpio: Use fdt_driver for initialization

FDT gpio drivers have an extra .xlate operation, so they need to embed
the `struct fdt_driver` inside the subsystem-specific type. The gpio
subsystem always initializes the driver for a specific DT node.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
Samuel Holland
2024-11-11 14:02:49 -08:00
committed by Anup Patel
parent 10df2d6fb5
commit bef8f9b806
7 changed files with 28 additions and 40 deletions

View File

@@ -10,18 +10,17 @@
#ifndef __FDT_GPIO_H__ #ifndef __FDT_GPIO_H__
#define __FDT_GPIO_H__ #define __FDT_GPIO_H__
#include <sbi_utils/fdt/fdt_driver.h>
#include <sbi_utils/gpio/gpio.h> #include <sbi_utils/gpio/gpio.h>
struct fdt_phandle_args; struct fdt_phandle_args;
/** FDT based GPIO driver */ /** FDT based GPIO driver */
struct fdt_gpio { struct fdt_gpio {
const struct fdt_match *match_table; struct fdt_driver driver;
int (*xlate)(struct gpio_chip *chip, int (*xlate)(struct gpio_chip *chip,
const struct fdt_phandle_args *pargs, const struct fdt_phandle_args *pargs,
struct gpio_pin *out_pin); struct gpio_pin *out_pin);
int (*init)(const void *fdt, int nodeoff,
const struct fdt_match *match);
}; };
/** Get a GPIO pin using "gpios" DT property of client DT node */ /** Get a GPIO pin using "gpios" DT property of client DT node */

View File

@@ -40,7 +40,7 @@ struct gpio_pin {
/** Representation of a GPIO chip */ /** Representation of a GPIO chip */
struct gpio_chip { struct gpio_chip {
/** Pointer to GPIO driver owning this GPIO chip */ /** Pointer to GPIO driver owning this GPIO chip */
void *driver; const void *driver;
/** Uniquie ID of the GPIO chip assigned by the driver */ /** Uniquie ID of the GPIO chip assigned by the driver */
unsigned int id; unsigned int id;
/** Number of GPIOs supported by the GPIO chip */ /** Number of GPIOs supported by the GPIO chip */

View File

@@ -13,34 +13,15 @@
#include <sbi_utils/gpio/fdt_gpio.h> #include <sbi_utils/gpio/fdt_gpio.h>
/* List of FDT gpio drivers generated at compile time */ /* List of FDT gpio drivers generated at compile time */
extern struct fdt_gpio *const fdt_gpio_drivers[]; extern const struct fdt_driver *const fdt_gpio_drivers[];
static int fdt_gpio_init(const void *fdt, int nodeoff) static int fdt_gpio_init(const void *fdt, int nodeoff)
{ {
int pos, rc;
struct fdt_gpio *drv;
const struct fdt_match *match;
/* Check "gpio-controller" property */ /* Check "gpio-controller" property */
if (!fdt_getprop(fdt, nodeoff, "gpio-controller", &rc)) if (!fdt_getprop(fdt, nodeoff, "gpio-controller", NULL))
return SBI_EINVAL; return SBI_EINVAL;
/* Try all GPIO drivers one-by-one */ return fdt_driver_init_by_offset(fdt, nodeoff, fdt_gpio_drivers);
for (pos = 0; fdt_gpio_drivers[pos]; pos++) {
drv = fdt_gpio_drivers[pos];
match = fdt_match_node(fdt, nodeoff, drv->match_table);
if (match && drv->init) {
rc = drv->init(fdt, nodeoff, match);
if (rc == SBI_ENODEV)
continue;
if (rc)
return rc;
return 0;
}
}
return SBI_ENOSYS;
} }
static int fdt_gpio_chip_find(const void *fdt, int nodeoff, static int fdt_gpio_chip_find(const void *fdt, int nodeoff,
@@ -71,7 +52,7 @@ int fdt_gpio_pin_get(const void *fdt, int nodeoff, int index,
struct gpio_pin *out_pin) struct gpio_pin *out_pin)
{ {
int rc; int rc;
struct fdt_gpio *drv; const struct fdt_gpio *drv;
struct gpio_chip *chip = NULL; struct gpio_chip *chip = NULL;
struct fdt_phandle_args pargs; struct fdt_phandle_args pargs;

View File

@@ -30,7 +30,7 @@ struct dw_gpio_chip {
struct gpio_chip chip; struct gpio_chip chip;
}; };
extern struct fdt_gpio fdt_gpio_designware; const struct fdt_gpio fdt_gpio_designware;
#define pin_to_chip(__p) container_of((__p)->chip, struct dw_gpio_chip, chip); #define pin_to_chip(__p) container_of((__p)->chip, struct dw_gpio_chip, chip);
@@ -132,8 +132,10 @@ static const struct fdt_match dw_gpio_match[] = {
{ }, { },
}; };
struct fdt_gpio fdt_gpio_designware = { const struct fdt_gpio fdt_gpio_designware = {
.match_table = dw_gpio_match, .driver = {
.match_table = dw_gpio_match,
.init = dw_gpio_init_bank,
},
.xlate = fdt_gpio_simple_xlate, .xlate = fdt_gpio_simple_xlate,
.init = dw_gpio_init_bank,
}; };

View File

@@ -1,3 +1,5 @@
HEADER: sbi_utils/gpio/fdt_gpio.h HEADER: sbi_utils/gpio/fdt_gpio.h
TYPE: struct fdt_gpio TYPE: const struct fdt_gpio
NAME: fdt_gpio_drivers NAME: fdt_gpio_drivers
MEMBER-NAME: driver
MEMBER-TYPE: const struct fdt_driver

View File

@@ -60,7 +60,7 @@ static void sifive_gpio_set(struct gpio_pin *gp, int value)
writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL)); writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
} }
extern struct fdt_gpio fdt_gpio_sifive; const struct fdt_gpio fdt_gpio_sifive;
static int sifive_gpio_init(const void *fdt, int nodeoff, static int sifive_gpio_init(const void *fdt, int nodeoff,
const struct fdt_match *match) const struct fdt_match *match)
@@ -99,8 +99,10 @@ static const struct fdt_match sifive_gpio_match[] = {
{ }, { },
}; };
struct fdt_gpio fdt_gpio_sifive = { const struct fdt_gpio fdt_gpio_sifive = {
.match_table = sifive_gpio_match, .driver = {
.match_table = sifive_gpio_match,
.init = sifive_gpio_init,
},
.xlate = fdt_gpio_simple_xlate, .xlate = fdt_gpio_simple_xlate,
.init = sifive_gpio_init,
}; };

View File

@@ -69,7 +69,7 @@ static void starfive_gpio_set(struct gpio_pin *gp, int value)
writel(val, (void *)(reg_addr + STARFIVE_GPIO_OUTVAL)); writel(val, (void *)(reg_addr + STARFIVE_GPIO_OUTVAL));
} }
extern struct fdt_gpio fdt_gpio_starfive; const struct fdt_gpio fdt_gpio_starfive;
static int starfive_gpio_init(const void *fdt, int nodeoff, static int starfive_gpio_init(const void *fdt, int nodeoff,
const struct fdt_match *match) const struct fdt_match *match)
@@ -109,8 +109,10 @@ static const struct fdt_match starfive_gpio_match[] = {
{ }, { },
}; };
struct fdt_gpio fdt_gpio_starfive = { const struct fdt_gpio fdt_gpio_starfive = {
.match_table = starfive_gpio_match, .driver = {
.match_table = starfive_gpio_match,
.init = starfive_gpio_init,
},
.xlate = fdt_gpio_simple_xlate, .xlate = fdt_gpio_simple_xlate,
.init = starfive_gpio_init,
}; };