lib: utils/gpio: use list for drivers

Convert static array to sbi_list.

This removes size limitation, makes add/remove more efficient and
saves space.

Signed-off-by: Nikita Shubin <n.shubin@yadro.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
This commit is contained in:
Nikita Shubin
2021-11-02 17:39:11 +03:00
committed by Anup Patel
parent 013ba4ef3d
commit 0979ffda12
2 changed files with 19 additions and 29 deletions

View File

@@ -11,6 +11,7 @@
#define __GPIO_H__ #define __GPIO_H__
#include <sbi/sbi_types.h> #include <sbi/sbi_types.h>
#include <sbi/sbi_list.h>
#define GPIO_LINE_DIRECTION_IN 1 #define GPIO_LINE_DIRECTION_IN 1
#define GPIO_LINE_DIRECTION_OUT 0 #define GPIO_LINE_DIRECTION_OUT 0
@@ -70,8 +71,15 @@ struct gpio_chip {
int (*get)(struct gpio_pin *gp); int (*get)(struct gpio_pin *gp);
/** Set output value for GPIO pin */ /** Set output value for GPIO pin */
void (*set)(struct gpio_pin *gp, int value); void (*set)(struct gpio_pin *gp, int value);
/** List */
struct sbi_dlist node;
}; };
static inline struct gpio_chip *to_gpio_chip(struct sbi_dlist *node)
{
return container_of(node, struct gpio_chip, node);
}
/** Find a registered GPIO chip */ /** Find a registered GPIO chip */
struct gpio_chip *gpio_chip_find(unsigned int id); struct gpio_chip *gpio_chip_find(unsigned int id);

View File

@@ -10,58 +10,40 @@
#include <sbi/sbi_error.h> #include <sbi/sbi_error.h>
#include <sbi_utils/gpio/gpio.h> #include <sbi_utils/gpio/gpio.h>
#define GPIO_CHIP_MAX 16 static SBI_LIST_HEAD(gpio_chip_list);
static struct gpio_chip *gc_array[GPIO_CHIP_MAX];
struct gpio_chip *gpio_chip_find(unsigned int id) struct gpio_chip *gpio_chip_find(unsigned int id)
{ {
unsigned int i; struct sbi_dlist *pos;
struct gpio_chip *ret = NULL;
for (i = 0; i < GPIO_CHIP_MAX; i++) { sbi_list_for_each(pos, &(gpio_chip_list)) {
if (gc_array[i] && gc_array[i]->id == id) { struct gpio_chip *chip = to_gpio_chip(pos);
ret = gc_array[i];
break; if (chip->id == id)
} return chip;
} }
return ret; return NULL;
} }
int gpio_chip_add(struct gpio_chip *gc) int gpio_chip_add(struct gpio_chip *gc)
{ {
int i, ret = SBI_ENOSPC;
if (!gc) if (!gc)
return SBI_EINVAL; return SBI_EINVAL;
if (gpio_chip_find(gc->id)) if (gpio_chip_find(gc->id))
return SBI_EALREADY; return SBI_EALREADY;
for (i = 0; i < GPIO_CHIP_MAX; i++) { sbi_list_add(&(gc->node), &(gpio_chip_list));
if (!gc_array[i]) {
gc_array[i] = gc;
ret = 0;
break;
}
}
return ret; return 0;
} }
void gpio_chip_remove(struct gpio_chip *gc) void gpio_chip_remove(struct gpio_chip *gc)
{ {
int i;
if (!gc) if (!gc)
return; return;
for (i = 0; i < GPIO_CHIP_MAX; i++) { sbi_list_del(&(gc->node));
if (gc_array[i] == gc) {
gc_array[i] = NULL;
break;
}
}
} }
int gpio_get_direction(struct gpio_pin *gp) int gpio_get_direction(struct gpio_pin *gp)