Private
Public Access
2
0

gpio: chardev: Add helper to retrieve gpiochip and line offset by line name

This makes the retrieval robust against chips being reordered during
boot. The results can be used to fill out mraa_pin_t while initializing
a board.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Jan Kiszka
2021-01-18 07:11:20 +01:00
committed by Tom Ingleby
parent fdab66958e
commit 56a11363de
2 changed files with 46 additions and 0 deletions

View File

@@ -444,3 +444,47 @@ mraa_get_chip_infos(mraa_gpiod_chip_info*** cinfos)
return num_chips;
}
#ifndef GPIO_MAX_NAME_SIZE
#define GPIO_MAX_NAME_SIZE 32
#endif
int
mraa_find_gpio_line_by_name(const char *name, unsigned *chip_number, unsigned *line_number)
{
mraa_gpiod_chip_info **cinfos;
mraa_gpiod_chip_info *cinfo;
mraa_gpiod_line_info *linfo;
int num_chips, i;
num_chips = mraa_get_chip_infos(&cinfos);
if (num_chips < 0) {
return -1;
}
for_each_gpio_chip(cinfo, cinfos, num_chips) {
for (i = 0; i < cinfo->chip_info.lines; i++) {
linfo = mraa_get_line_info_by_chip_name(cinfo->chip_info.name, i);
if (!strncmp(linfo->name, name, GPIO_MAX_NAME_SIZE)) {
if (chip_number) {
/* idx is coming from `for_each_gpio_chip` definition */
*chip_number = idx;
}
if (line_number) {
*line_number = linfo->line_offset;
}
free(linfo);
free(cinfos);
return 0;
}
free(linfo);
}
}
free(cinfos);
return -1;
}