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/+/6739
-- gerrit commit d4102a7f9dc359189ae4768da0c3f1a29eab8bed Author: Laszlo Sitzer <dlsit...@gmail.com> Date: Thu Nov 18 13:10:39 2021 +0100 linuxgpiod: Allow using multiple GPIO chips. Allow passing optional gpiochip number after gpio number. If no optional chip number is passed, the one from the 'gpiochip' configuration directive is used. Signed-off-by: Laszlo Sitzer <dlsit...@gmail.com> Change-Id: I23dfa9eedc5bb72a7331da79b75971162d6d8344 diff --git a/src/jtag/drivers/linuxgpiod.c b/src/jtag/drivers/linuxgpiod.c index 75f6152be..b60ef4dca 100644 --- a/src/jtag/drivers/linuxgpiod.c +++ b/src/jtag/drivers/linuxgpiod.c @@ -29,8 +29,26 @@ static int swclk_gpio = -1; static int swdio_gpio = -1; static int led_gpio = -1; static int gpiochip = -1; +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_chip *gpiod_chip; 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,50 @@ 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; } @@ -429,55 +499,67 @@ COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums) COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck) { - if (CMD_ARGC == 1) + if (CMD_ARGC > 0) COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio); + if (CMD_ARGC > 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tck_gpiochip); - command_print(CMD, "LinuxGPIOD num: tck = %d", tck_gpio); + command_print(CMD, "LinuxGPIOD num: tck = %d, chip = %d", tck_gpio, tck_gpiochip); return ERROR_OK; } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms) { - if (CMD_ARGC == 1) + if (CMD_ARGC > 0) COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tms_gpio); + if (CMD_ARGC > 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpiochip); - command_print(CMD, "LinuxGPIOD num: tms = %d", tms_gpio); + command_print(CMD, "LinuxGPIOD num: tms = %d, chip = %d", tms_gpio, tms_gpiochip); return ERROR_OK; } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo) { - if (CMD_ARGC == 1) + if (CMD_ARGC > 0) COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdo_gpio); + if (CMD_ARGC > 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tdo_gpiochip); - command_print(CMD, "LinuxGPIOD num: tdo = %d", tdo_gpio); + command_print(CMD, "LinuxGPIOD num: tdo = %d, chip = %d", tdo_gpio, tdo_gpiochip); return ERROR_OK; } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi) { - if (CMD_ARGC == 1) + if (CMD_ARGC > 0) COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tdi_gpio); + if (CMD_ARGC > 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tdi_gpiochip); - command_print(CMD, "LinuxGPIOD num: tdi = %d", tdi_gpio); + command_print(CMD, "LinuxGPIOD num: tdi = %d, chip = %d", tdi_gpio, tdi_gpiochip); return ERROR_OK; } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst) { - if (CMD_ARGC == 1) + if (CMD_ARGC > 0) COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], srst_gpio); + if (CMD_ARGC > 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], srst_gpiochip); - command_print(CMD, "LinuxGPIOD num: srst = %d", srst_gpio); + command_print(CMD, "LinuxGPIOD num: srst = %d, chip = %d", srst_gpio, srst_gpiochip); return ERROR_OK; } COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst) { - if (CMD_ARGC == 1) + if (CMD_ARGC > 0) COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], trst_gpio); + if (CMD_ARGC > 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], trst_gpiochip); - command_print(CMD, "LinuxGPIOD num: trst = %d", trst_gpio); + command_print(CMD, "LinuxGPIOD num: trst = %d, chip = %d", trst_gpio, trst_gpiochip); return ERROR_OK; } @@ -499,35 +581,51 @@ COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums) COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk) { - if (CMD_ARGC == 1) + if (CMD_ARGC > 0) COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio); + if (CMD_ARGC > 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swclk_gpiochip); - command_print(CMD, "LinuxGPIOD num: swclk = %d", swclk_gpio); + command_print(CMD, "LinuxGPIOD num: swclk = %d, chip = %d", swclk_gpio, swclk_gpiochip); return ERROR_OK; } COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio) { - if (CMD_ARGC == 1) + if (CMD_ARGC > 0) COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swdio_gpio); + if (CMD_ARGC > 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpiochip); - command_print(CMD, "LinuxGPIOD num: swdio = %d", swdio_gpio); + command_print(CMD, "LinuxGPIOD num: swdio = %d, chip = %d", swdio_gpio, swdio_gpiochip); return ERROR_OK; } COMMAND_HANDLER(linuxgpiod_handle_gpionum_led) { - if (CMD_ARGC == 1) + if (CMD_ARGC > 0) COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], led_gpio); + if (CMD_ARGC > 1) + COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], led_gpiochip); - command_print(CMD, "LinuxGPIOD num: led = %d", led_gpio); + command_print(CMD, "LinuxGPIOD num: led = %d, chip = %d", led_gpio, led_gpiochip); return ERROR_OK; } COMMAND_HANDLER(linuxgpiod_handle_gpiochip) { - if (CMD_ARGC == 1) + if (CMD_ARGC == 1) { COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip); + tck_gpiochip = gpiochip; + tms_gpiochip = gpiochip; + tdi_gpiochip = gpiochip; + tdo_gpiochip = gpiochip; + trst_gpiochip = gpiochip; + srst_gpiochip = gpiochip; + swclk_gpiochip = gpiochip; + swdio_gpiochip = gpiochip; + led_gpiochip = gpiochip; + } command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip); return ERROR_OK; @@ -545,42 +643,42 @@ static const struct command_registration linuxgpiod_subcommand_handlers[] = { .name = "tck_num", .handler = linuxgpiod_handle_jtag_gpionum_tck, .mode = COMMAND_CONFIG, - .help = "gpio number for tck.", + .help = "gpio number for tck (optionally followed by gpio chip number).", .usage = "tck", }, { .name = "tms_num", .handler = linuxgpiod_handle_jtag_gpionum_tms, .mode = COMMAND_CONFIG, - .help = "gpio number for tms.", + .help = "gpio number for tms (optionally followed by gpio chip number).", .usage = "tms", }, { .name = "tdo_num", .handler = linuxgpiod_handle_jtag_gpionum_tdo, .mode = COMMAND_CONFIG, - .help = "gpio number for tdo.", + .help = "gpio number for tdo (optionally followed by gpio chip number).", .usage = "tdo", }, { .name = "tdi_num", .handler = linuxgpiod_handle_jtag_gpionum_tdi, .mode = COMMAND_CONFIG, - .help = "gpio number for tdi.", + .help = "gpio number for tdi (optionally followed by gpio chip number).", .usage = "tdi", }, { .name = "srst_num", .handler = linuxgpiod_handle_jtag_gpionum_srst, .mode = COMMAND_CONFIG, - .help = "gpio number for srst.", + .help = "gpio number for srst (optionally followed by gpio chip number).", .usage = "srst", }, { .name = "trst_num", .handler = linuxgpiod_handle_jtag_gpionum_trst, .mode = COMMAND_CONFIG, - .help = "gpio number for trst.", + .help = "gpio number for trst (optionally followed by gpio chip number).", .usage = "trst", }, { @@ -594,21 +692,21 @@ static const struct command_registration linuxgpiod_subcommand_handlers[] = { .name = "swclk_num", .handler = linuxgpiod_handle_swd_gpionum_swclk, .mode = COMMAND_CONFIG, - .help = "gpio number for swclk.", + .help = "gpio number for swclk (optionally followed by gpio chip number).", .usage = "swclk", }, { .name = "swdio_num", .handler = linuxgpiod_handle_swd_gpionum_swdio, .mode = COMMAND_CONFIG, - .help = "gpio number for swdio.", + .help = "gpio number for swdio (optionally followed by gpio chip number).", .usage = "swdio", }, { .name = "led_num", .handler = linuxgpiod_handle_gpionum_led, .mode = COMMAND_CONFIG, - .help = "gpio number for LED.", + .help = "gpio number for LED (optionally followed by gpio chip number).", .usage = "led", }, { --