This is an automated email from Gerrit. "Laszlo Sitzer <dlsit...@gmail.com>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/6713
-- gerrit commit b91192fc382d1da04b378b3fac10b44ee8e90591 Author: Laszlo Sitzer <dlsit...@gmail.com> Date: Thu Nov 18 13:10:39 2021 +0100 linuxgpiod: Allow using multiple GPIO chips. Signed-off-by: Laszlo Sitzer <dlsit...@gmail.com> Change-Id: I2903792ad6719eab81328e37e146eb20ab8dfe78 diff --git a/src/jtag/drivers/linuxgpiod.c b/src/jtag/drivers/linuxgpiod.c index 75f6152be..940e42797 100644 --- a/src/jtag/drivers/linuxgpiod.c +++ b/src/jtag/drivers/linuxgpiod.c @@ -28,9 +28,27 @@ static int srst_gpio = -1; static int swclk_gpio = -1; static int swdio_gpio = -1; static int led_gpio = -1; -static int gpiochip = -1; -static struct gpiod_chip *gpiod_chip; +static int tck_gpiochip = -1; +static int tms_gpiochip = -1; +static int tdi_gpiochip = -1; +static int tdo_gpiochip = -1; +static int trst_gpiochip = -1; +static int srst_gpiochip = -1; +static int swclk_gpiochip = -1; +static int swdio_gpiochip = -1; +static int led_gpiochip = -1; + +static struct gpiod_chip *gpiod_chip_tck; +static struct gpiod_chip *gpiod_chip_tms; +static struct gpiod_chip *gpiod_chip_tdi; +static struct gpiod_chip *gpiod_chip_tdo; +static struct gpiod_chip *gpiod_chip_trst; +static struct gpiod_chip *gpiod_chip_srst; +static struct gpiod_chip *gpiod_chip_swclk; +static struct gpiod_chip *gpiod_chip_swdio; +static struct gpiod_chip *gpiod_chip_led; + static struct gpiod_line *gpiod_tck; static struct gpiod_line *gpiod_tms; static struct gpiod_line *gpiod_tdi; @@ -273,12 +291,20 @@ static int linuxgpiod_quit(void) helper_release(gpiod_tdi); helper_release(gpiod_tdo); - gpiod_chip_close(gpiod_chip); + gpiod_chip_close(gpiod_chip_led); + gpiod_chip_close(gpiod_chip_srst); + gpiod_chip_close(gpiod_chip_swdio); + gpiod_chip_close(gpiod_chip_swclk); + gpiod_chip_close(gpiod_chip_trst); + gpiod_chip_close(gpiod_chip_tms); + gpiod_chip_close(gpiod_chip_tck); + gpiod_chip_close(gpiod_chip_tdi); + gpiod_chip_close(gpiod_chip_tdo); return ERROR_OK; } -static struct gpiod_line *helper_get_line(const char *label, unsigned int offset, int val, int dir, int flags) +static struct gpiod_line *helper_get_line(const char *label, struct gpiod_chip *gpiod_chip, unsigned int offset, int val, int dir, int flags) { struct gpiod_line *line; int retval; @@ -304,19 +330,19 @@ static struct gpiod_line *helper_get_line(const char *label, unsigned int offset return line; } -static struct gpiod_line *helper_get_input_line(const char *label, unsigned int offset) +static struct gpiod_line *helper_get_input_line(const char *label, struct gpiod_chip *gpiod_chip, unsigned int offset) { - return helper_get_line(label, offset, 0, GPIOD_LINE_REQUEST_DIRECTION_INPUT, 0); + return helper_get_line(label, gpiod_chip, offset, 0, GPIOD_LINE_REQUEST_DIRECTION_INPUT, 0); } -static struct gpiod_line *helper_get_output_line(const char *label, unsigned int offset, int val) +static struct gpiod_line *helper_get_output_line(const char *label, struct gpiod_chip *gpiod_chip, unsigned int offset, int val) { - return helper_get_line(label, offset, val, GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, 0); + return helper_get_line(label, gpiod_chip, offset, val, GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, 0); } -static struct gpiod_line *helper_get_open_drain_output_line(const char *label, unsigned int offset, int val) +static struct gpiod_line *helper_get_open_drain_output_line(const char *label, struct gpiod_chip *gpiod_chip, unsigned int offset, int val) { - return helper_get_line(label, offset, val, GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN); + return helper_get_line(label, gpiod_chip, offset, val, GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN); } static int linuxgpiod_init(void) @@ -325,12 +351,6 @@ static int linuxgpiod_init(void) bitbang_interface = &linuxgpiod_bitbang; - gpiod_chip = gpiod_chip_open_by_number(gpiochip); - if (!gpiod_chip) { - LOG_ERROR("Cannot open LinuxGPIOD gpiochip %d", gpiochip); - return ERROR_JTAG_INIT_FAILED; - } - /* * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high. @@ -343,27 +363,54 @@ static int linuxgpiod_init(void) goto out_error; } - gpiod_tdo = helper_get_input_line("tdo", tdo_gpio); + gpiod_tdo = helper_get_input_line("tdo", gpiod_chip_tdo, tdo_gpio); if (!gpiod_tdo) goto out_error; - gpiod_tdi = helper_get_output_line("tdi", tdi_gpio, 0); + gpiod_tdi = helper_get_output_line("tdi", gpiod_chip_tdi, tdi_gpio, 0); if (!gpiod_tdi) goto out_error; - gpiod_tck = helper_get_output_line("tck", tck_gpio, 0); + gpiod_tck = helper_get_output_line("tck", gpiod_chip_tck, tck_gpio, 0); if (!gpiod_tck) goto out_error; - gpiod_tms = helper_get_output_line("tms", tms_gpio, 1); + gpiod_tms = helper_get_output_line("tms", gpiod_chip_tms, tms_gpio, 1); if (!gpiod_tms) goto out_error; + gpiod_chip_tdo = gpiod_chip_open_by_number(tdo_gpiochip); + if (!gpiod_chip_tdo) { + LOG_ERROR("Cannot open LinuxGPIOD tdo_gpiochip %d", tdo_gpiochip); + goto out_error; + } + gpiod_chip_tdi = gpiod_chip_open_by_number(tdi_gpiochip); + if (!gpiod_chip_tdi) { + LOG_ERROR("Cannot open LinuxGPIOD tdi_gpiochip %d", tdi_gpiochip); + goto out_error; + } + gpiod_chip_tck = gpiod_chip_open_by_number(tck_gpiochip); + if (!gpiod_chip_tck) { + LOG_ERROR("Cannot open LinuxGPIOD tck_gpiochip %d", tck_gpiochip); + goto out_error; + } + gpiod_chip_tms = gpiod_chip_open_by_number(tms_gpiochip); + if (!gpiod_chip_tms) { + LOG_ERROR("Cannot open LinuxGPIOD tms_gpiochip %d", tms_gpiochip); + goto out_error; + } + if (is_gpio_valid(trst_gpio)) { + gpiod_chip_trst = gpiod_chip_open_by_number(trst_gpiochip); + if (!gpiod_chip_trst) { + LOG_ERROR("Cannot open LinuxGPIOD trst_gpiochip %d", trst_gpiochip); + goto out_error; + } + if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN) - gpiod_trst = helper_get_open_drain_output_line("trst", trst_gpio, 1); + gpiod_trst = helper_get_open_drain_output_line("trst", gpiod_chip_trst, trst_gpio, 1); else - gpiod_trst = helper_get_output_line("trst", trst_gpio, 1); + gpiod_trst = helper_get_output_line("trst", gpiod_chip_trst, trst_gpio, 1); if (!gpiod_trst) goto out_error; @@ -376,27 +423,49 @@ static int linuxgpiod_init(void) goto out_error; } - gpiod_swclk = helper_get_output_line("swclk", swclk_gpio, 1); + gpiod_chip_swclk = gpiod_chip_open_by_number(swclk_gpiochip); + if (!gpiod_chip_swclk) { + LOG_ERROR("Cannot open LinuxGPIOD swclk_gpiochip %d", swclk_gpiochip); + goto out_error; + } + gpiod_chip_swdio = gpiod_chip_open_by_number(swdio_gpiochip); + if (!gpiod_chip_swdio) { + LOG_ERROR("Cannot open LinuxGPIOD swdio_gpiochip %d", swdio_gpiochip); + goto out_error; + + gpiod_swclk = helper_get_output_line("swclk", gpiod_chip_swclk, swclk_gpio, 1); if (!gpiod_swclk) goto out_error; - gpiod_swdio = helper_get_output_line("swdio", swdio_gpio, 1); + gpiod_swdio = helper_get_output_line("swdio", gpiod_chip_swdio, swdio_gpio, 1); if (!gpiod_swdio) goto out_error; } if (is_gpio_valid(srst_gpio)) { + gpiod_chip_srst = gpiod_chip_open_by_number(srst_gpiochip); + if (!gpiod_chip_srst) { + LOG_ERROR("Cannot open LinuxGPIOD srst_gpiochip %d", srst_gpiochip); + goto out_error; + } + if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL) - gpiod_srst = helper_get_output_line("srst", srst_gpio, 1); + gpiod_srst = helper_get_output_line("srst", gpiod_chip_srst, srst_gpio, 1); else - gpiod_srst = helper_get_open_drain_output_line("srst", srst_gpio, 1); + gpiod_srst = helper_get_open_drain_output_line("srst", gpiod_chip_srst, srst_gpio, 1); if (!gpiod_srst) goto out_error; } if (is_gpio_valid(led_gpio)) { - gpiod_led = helper_get_output_line("led", led_gpio, 0); + gpiod_chip_led = gpiod_chip_open_by_number(led_gpiochip); + if (!gpiod_chip_led) { + LOG_ERROR("Cannot open LinuxGPIOD led_gpiochip %d", led_gpiochip); + goto out_error; + } + + gpiod_led = helper_get_output_line("led", gpiod_chip_led, led_gpio, 0); if (!gpiod_led) goto out_error; } @@ -524,12 +593,84 @@ COMMAND_HANDLER(linuxgpiod_handle_gpionum_led) return ERROR_OK; } -COMMAND_HANDLER(linuxgpiod_handle_gpiochip) +COMMAND_HANDLER(linuxgpiod_handle_tck_gpiochip) +{ + if (CMD_ARGC == 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpiochip); + + command_print(CMD, "LinuxGPIOD tck_gpiochip = %d", tck_gpiochip); + return ERROR_OK; +} + +COMMAND_HANDLER(linuxgpiod_handle_tms_gpiochip) +{ + if (CMD_ARGC == 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpiochip); + + command_print(CMD, "LinuxGPIOD tms_gpiochip = %d", tms_gpiochip); + return ERROR_OK; +} + +COMMAND_HANDLER(linuxgpiod_handle_tdi_gpiochip) +{ + if (CMD_ARGC == 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpiochip); + + command_print(CMD, "LinuxGPIOD tdi_gpiochip = %d", tdi_gpiochip); + return ERROR_OK; +} + +COMMAND_HANDLER(linuxgpiod_handle_tdo_gpiochip) +{ + if (CMD_ARGC == 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpiochip); + + command_print(CMD, "LinuxGPIOD tdo_gpiochip = %d", tdo_gpiochip); + return ERROR_OK; +} + +COMMAND_HANDLER(linuxgpiod_handle_trst_gpiochip) { if (CMD_ARGC == 1) - COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip); + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpiochip); - command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip); + command_print(CMD, "LinuxGPIOD trst_gpiochip = %d", trst_gpiochip); + return ERROR_OK; +} + +COMMAND_HANDLER(linuxgpiod_handle_srst_gpiochip) +{ + if (CMD_ARGC == 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpiochip); + + command_print(CMD, "LinuxGPIOD srst_gpiochip = %d", srst_gpiochip); + return ERROR_OK; +} + +COMMAND_HANDLER(linuxgpiod_handle_swclk_gpiochip) +{ + if (CMD_ARGC == 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpiochip); + + command_print(CMD, "LinuxGPIOD swclk_gpiochip = %d", swclk_gpiochip); + return ERROR_OK; +} + +COMMAND_HANDLER(linuxgpiod_handle_swdio_gpiochip) +{ + if (CMD_ARGC == 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpiochip); + + command_print(CMD, "LinuxGPIOD swdio_gpiochip = %d", swdio_gpiochip); + return ERROR_OK; +} + +COMMAND_HANDLER(linuxgpiod_handle_led_gpiochip) +{ + if (CMD_ARGC == 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], led_gpiochip); + + command_print(CMD, "LinuxGPIOD led_gpiochip = %d", led_gpiochip); return ERROR_OK; } @@ -612,11 +753,67 @@ static const struct command_registration linuxgpiod_subcommand_handlers[] = { .usage = "led", }, { - .name = "gpiochip", - .handler = linuxgpiod_handle_gpiochip, + .name = "tck_gpiochip", + .handler = linuxgpiod_handle_tck_gpiochip, + .mode = COMMAND_CONFIG, + .help = "number of the tck gpiochip.", + .usage = "tck_gpiochip", + }, + { + .name = "tms_gpiochip", + .handler = linuxgpiod_handle_tms_gpiochip, + .mode = COMMAND_CONFIG, + .help = "number of the tms_gpiochip.", + .usage = "tms_gpiochip", + }, + { + .name = "tdi_gpiochip", + .handler = linuxgpiod_handle_tdi_gpiochip, + .mode = COMMAND_CONFIG, + .help = "number of the tdi_gpiochip.", + .usage = "tdi_gpiochip", + }, + { + .name = "tdo_gpiochip", + .handler = linuxgpiod_handle_tdo_gpiochip, + .mode = COMMAND_CONFIG, + .help = "number of the tdo_gpiochip.", + .usage = "tdo_gpiochip", + }, + { + .name = "trst_gpiochip", + .handler = linuxgpiod_handle_trst_gpiochip, + .mode = COMMAND_CONFIG, + .help = "number of the trst_gpiochip.", + .usage = "trst_gpiochip", + }, + { + .name = "srst_gpiochip", + .handler = linuxgpiod_handle_srst_gpiochip, + .mode = COMMAND_CONFIG, + .help = "number of the srst_gpiochip.", + .usage = "srst_gpiochip", + }, + { + .name = "swclk_gpiochip", + .handler = linuxgpiod_handle_swclk_gpiochip, + .mode = COMMAND_CONFIG, + .help = "number of the swclk_gpiochip.", + .usage = "swclk_gpiochip", + }, + { + .name = "swdio_gpiochip", + .handler = linuxgpiod_handle_swdio_gpiochip, + .mode = COMMAND_CONFIG, + .help = "number of the swdio_gpiochip.", + .usage = "swdio_gpiochip", + }, + { + .name = "led_gpiochip", + .handler = linuxgpiod_handle_led_gpiochip, .mode = COMMAND_CONFIG, - .help = "number of the gpiochip.", - .usage = "gpiochip", + .help = "number of the led_gpiochip.", + .usage = "led_gpiochip", }, COMMAND_REGISTRATION_DONE }; --