lib: utils/gpio: Use heap in SiFive and StartFive GPIO drivers

Let's use heap allocation in SiFive and Starfive GPIO drivers
instead of using a fixed size global array.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
This commit is contained in:
Anup Patel 2023-04-19 16:49:19 +05:30 committed by Anup Patel
parent 66daafe3ba
commit fa5ad2e6f9
2 changed files with 20 additions and 21 deletions

View file

@ -9,11 +9,10 @@
#include <sbi/riscv_io.h>
#include <sbi/sbi_error.h>
#include <sbi/sbi_heap.h>
#include <sbi_utils/fdt/fdt_helper.h>
#include <sbi_utils/gpio/fdt_gpio.h>
#define SIFIVE_GPIO_CHIP_MAX 2
#define SIFIVE_GPIO_PINS_MIN 1
#define SIFIVE_GPIO_PINS_MAX 32
#define SIFIVE_GPIO_PINS_DEF 16
@ -27,9 +26,6 @@ struct sifive_gpio_chip {
struct gpio_chip chip;
};
static unsigned int sifive_gpio_chip_count;
static struct sifive_gpio_chip sifive_gpio_chip_array[SIFIVE_GPIO_CHIP_MAX];
static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
{
unsigned int v;
@ -73,13 +69,15 @@ static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
struct sifive_gpio_chip *chip;
uint64_t addr;
if (SIFIVE_GPIO_CHIP_MAX <= sifive_gpio_chip_count)
return SBI_ENOSPC;
chip = &sifive_gpio_chip_array[sifive_gpio_chip_count];
chip = sbi_zalloc(sizeof(*chip));
if (!chip)
return SBI_ENOMEM;
rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
if (rc)
if (rc) {
sbi_free(chip);
return rc;
}
chip->addr = addr;
chip->chip.driver = &fdt_gpio_sifive;
@ -88,10 +86,11 @@ static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
chip->chip.direction_output = sifive_gpio_direction_output;
chip->chip.set = sifive_gpio_set;
rc = gpio_chip_add(&chip->chip);
if (rc)
if (rc) {
sbi_free(chip);
return rc;
}
sifive_gpio_chip_count++;
return 0;
}

View file

@ -9,11 +9,11 @@
#include <sbi/riscv_io.h>
#include <sbi/sbi_error.h>
#include <sbi/sbi_heap.h>
#include <sbi/sbi_console.h>
#include <sbi_utils/fdt/fdt_helper.h>
#include <sbi_utils/gpio/fdt_gpio.h>
#define STARFIVE_GPIO_CHIP_MAX 2
#define STARFIVE_GPIO_PINS_DEF 64
#define STARFIVE_GPIO_OUTVAL 0x40
#define STARFIVE_GPIO_MASK 0xff
@ -25,9 +25,6 @@ struct starfive_gpio_chip {
struct gpio_chip chip;
};
static unsigned int starfive_gpio_chip_count;
static struct starfive_gpio_chip starfive_gpio_chip_array[STARFIVE_GPIO_CHIP_MAX];
static int starfive_gpio_direction_output(struct gpio_pin *gp, int value)
{
u32 val;
@ -82,13 +79,15 @@ static int starfive_gpio_init(void *fdt, int nodeoff, u32 phandle,
struct starfive_gpio_chip *chip;
u64 addr;
if (starfive_gpio_chip_count >= STARFIVE_GPIO_CHIP_MAX)
return SBI_ENOSPC;
chip = &starfive_gpio_chip_array[starfive_gpio_chip_count];
chip = sbi_zalloc(sizeof(*chip));
if (!chip)
return SBI_ENOMEM;
rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
if (rc)
if (rc) {
sbi_free(chip);
return rc;
}
chip->addr = addr;
chip->chip.driver = &fdt_gpio_starfive;
@ -97,10 +96,11 @@ static int starfive_gpio_init(void *fdt, int nodeoff, u32 phandle,
chip->chip.direction_output = starfive_gpio_direction_output;
chip->chip.set = starfive_gpio_set;
rc = gpio_chip_add(&chip->chip);
if (rc)
if (rc) {
sbi_free(chip);
return rc;
}
starfive_gpio_chip_count++;
return 0;
}