Skip to content

Commit

Permalink
gpio: chardev: Add helper to retrieve gpiochip and line offset by lin…
Browse files Browse the repository at this point in the history
…e 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>
  • Loading branch information
jan-kiszka authored and tingleby committed Aug 5, 2022
1 parent fdab669 commit 56a1136
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/gpio/gpio_chardev.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ mraa_boolean_t mraa_is_gpio_line_open_source(mraa_gpiod_line_info *linfo);
int mraa_get_number_of_gpio_chips();
int mraa_get_chip_infos(mraa_gpiod_chip_info*** cinfos);

int mraa_find_gpio_line_by_name(const char *name, unsigned *chip_number, unsigned *line_number);

/* Multiple gpio support. */
typedef struct _gpio_group* mraa_gpiod_group_t;

Expand Down
44 changes: 44 additions & 0 deletions src/gpio/gpio_chardev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 56a1136

Please sign in to comment.