diff --git a/src/gpio.c b/src/gpio.c index 318e013..995f6a3 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -103,36 +103,54 @@ int gpio_sequence_sleep(struct EG25Manager *manager) return 0; } -struct gpiod_line *gpio_get_output_line(struct EG25Manager *manager, int chip, int line) -{ - struct gpiod_line *gpio_line; +struct gpiod_line_request *gpio_request_line(struct EG25Manager *manager, int chip, unsigned int line, enum gpiod_line_direction direction) { + struct gpiod_line_request *request = NULL; + struct gpiod_line_settings *settings; + struct gpiod_line_config *line_cfg; + struct gpiod_request_config *req_cfg; + int ret; - gpio_line = gpiod_chip_get_line(manager->gpiochip[chip], line); - if (!gpio_line) + settings = gpiod_line_settings_new(); + if (!settings) return NULL; - if (gpiod_line_request_output(gpio_line, "eg25manager", 0) < 0) { - gpiod_line_release(gpio_line); - return NULL; - } + gpiod_line_settings_set_direction(settings, direction); - return gpio_line; + line_cfg = gpiod_line_config_new(); + if (!line_cfg) + goto free_settings; + + ret = gpiod_line_config_add_line_settings(line_cfg, &line, 1, settings); + if (ret) + goto free_line_config; + + req_cfg = gpiod_request_config_new(); + if (!req_cfg) + goto free_line_config; + + gpiod_request_config_set_consumer(req_cfg, "eg25-manager"); + + request = gpiod_chip_request_lines(manager->gpiochip[chip], req_cfg, line_cfg); + + gpiod_request_config_free(req_cfg); + +free_line_config: + gpiod_line_config_free(line_cfg); + +free_settings: + gpiod_line_settings_free(settings); + + return request; } -struct gpiod_line *gpio_get_input_line(struct EG25Manager *manager, int chip, int line) +struct gpiod_line_request *gpio_request_output_line(struct EG25Manager *manager, int chip, unsigned int line) { - struct gpiod_line *gpio_line; + return gpio_request_line(manager, chip, line, GPIOD_LINE_DIRECTION_OUTPUT); +} - gpio_line = gpiod_chip_get_line(manager->gpiochip[chip], line); - if (!gpio_line) - return NULL; - - if (gpiod_line_request_input(gpio_line, "eg25manager") < 0) { - gpiod_line_release(gpio_line); - return NULL; - } - - return gpio_line; +struct gpiod_line_request *gpio_request_input_line(struct EG25Manager *manager, int chip, unsigned int line) +{ + return gpio_request_line(manager, chip, line, GPIOD_LINE_DIRECTION_INPUT); } static int gpio_chip_dir_filter(const struct dirent *entry) @@ -274,7 +292,7 @@ int gpio_init(struct EG25Manager *manager, toml_table_t *config[]) if (!line.ok || line.u.i < 0 || line.u.i > gpiod_chip_num_lines(manager->gpiochip[chip.u.i])) g_error("Wrong line ID for output GPIO '%s'", gpio_out_names[i]); - manager->gpio_out[i] = gpio_get_output_line(manager, chip.u.i, line.u.i); + manager->gpio_out[i] = gpio_request_output_line(manager, chip.u.i, line.u.i); if (!manager->gpio_out[i]) g_error("Unable to get output GPIO %d", i); } @@ -298,7 +316,7 @@ int gpio_init(struct EG25Manager *manager, toml_table_t *config[]) if (!line.ok || line.u.i < 0 || line.u.i > gpiod_chip_num_lines(manager->gpiochip[chip.u.i])) g_error("Wrong line ID for input GPIO '%s'", gpio_in_names[i]); - manager->gpio_in[i] = gpio_get_input_line(manager, chip.u.i, line.u.i); + manager->gpio_in[i] = gpio_request_input_line(manager, chip.u.i, line.u.i); if (!manager->gpio_in[i]) g_error("Unable to get input GPIO %d", i); } @@ -326,7 +344,7 @@ int gpio_init(struct EG25Manager *manager, toml_table_t *config[]) chipidx = 1; } - manager->gpio_out[i] = gpio_get_input_line(manager, chipidx, offset); + manager->gpio_out[i] = gpio_request_input_line(manager, chipidx, offset); if (!manager->gpio_out[i]) g_error("Unable to get output GPIO %d", i); } @@ -343,7 +361,7 @@ int gpio_init(struct EG25Manager *manager, toml_table_t *config[]) chipidx = 1; } - manager->gpio_in[i] = gpio_get_input_line(manager, chipidx, offset); + manager->gpio_in[i] = gpio_request_input_line(manager, chipidx, offset); if (!manager->gpio_in[i]) g_error("Unable to get input GPIO %d", i); } @@ -374,12 +392,12 @@ void gpio_destroy(struct EG25Manager *manager) for (i = 0; i < GPIO_OUT_COUNT; i++) { if (manager->gpio_out[i]) - gpiod_line_release(manager->gpio_out[i]); + gpiod_line_request_release(manager->gpio_out[i]); } for (i = 0; i < GPIO_IN_COUNT; i++) { if (manager->gpio_in[i]) - gpiod_line_release(manager->gpio_in[i]); + gpiod_line_request_release(manager->gpio_in[i]); } if (manager->gpiochip[0]) diff --git a/src/manager.h b/src/manager.h index 43d44bd..3c25ecc 100644 --- a/src/manager.h +++ b/src/manager.h @@ -116,8 +116,8 @@ struct EG25Manager { GUdevClient *udev; struct gpiod_chip *gpiochip[2]; - struct gpiod_line *gpio_out[5]; - struct gpiod_line *gpio_in[2]; + struct gpiod_line_request *gpio_out[5]; + struct gpiod_line_request *gpio_in[2]; }; void modem_configure(struct EG25Manager *data);