Skip to content

Commit

Permalink
gpio: chardev: Add function to retrieve sysfs base for a gpiochip
Browse files Browse the repository at this point in the history
This allows to full mux structures which still need the sysfs GPIO
numbers. In combination with mraa_find_gpio_line_by_name, this allows
for platform setup that is independent of the gpiochip probing order.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
  • Loading branch information
jan-kiszka authored and tingleby committed Aug 5, 2022
1 parent 56a1136 commit 0c44a72
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/gpio/gpio_chardev.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mraa_gpiod_chip_info* mraa_get_chip_info_by_path(const char* path);
mraa_gpiod_chip_info* mraa_get_chip_info_by_name(const char* name);
mraa_gpiod_chip_info* mraa_get_chip_info_by_label(const char* label);
mraa_gpiod_chip_info* mraa_get_chip_info_by_number(unsigned number);
int mraa_get_chip_base_by_number(unsigned number);

mraa_gpiod_line_info* mraa_get_line_info_from_descriptor(int chip_fd, unsigned line_number);
mraa_gpiod_line_info* mraa_get_line_info_by_chip_number(unsigned chip_number, unsigned line_number);
Expand Down
37 changes: 37 additions & 0 deletions src/gpio/gpio_chardev.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,43 @@ mraa_get_chip_info_by_number(unsigned number)
return cinfo;
}

int
mraa_get_chip_base_by_number(unsigned number)
{
#if defined(PERIPHERALMAN)
return -1;
#else
char* glob;
char* path;
FILE* fh;
int res;

glob = malloc(PATH_MAX);
if (!glob) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: malloc() fail");
return -1;
}

snprintf(glob, PATH_MAX, "/sys/bus/gpio/devices/gpiochip%d/../gpio/*/base", number);

path = mraa_file_unglob(glob);
free(glob);
if (!path) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: invalid chip number");
res = -1;
} else {
fh = fopen(path, "r");
if (!fh || fscanf(fh, "%d", &res) != 1) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: could not read from %s", path);
res = -1;
}
}

free(path);
return res;
#endif
}

mraa_gpiod_line_info*
mraa_get_line_info_from_descriptor(int chip_fd, unsigned line_number)
{
Expand Down

0 comments on commit 0c44a72

Please sign in to comment.