Add libgpiod v2 compatible bitbang master

The API was completely re-invented with libgpiod 2.0. Hence we add a
new driver as we would otherwise only share boilerplate with the old
driver. The new API makes it easier to re-configure GPIO pins on-the-
fly. Which we will use to implement dual- and quad-i/o modes in the
future.

One downside of the new API is that there is no function to open a
chip by number anymore. So we have to emulate that behavior now for
backwards compatibility.

Change-Id: If6a02d98f15def51b61fe0c3f09092cf286a37eb
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/83
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/Makefile b/Makefile
index e51fa25..1a9fb47 100644
--- a/Makefile
+++ b/Makefile
@@ -766,7 +766,11 @@
 
 ifeq ($(CONFIG_LINUX_GPIO_SPI), yes)
 FEATURE_FLAGS += -D'CONFIG_LINUX_GPIO_SPI=1'
+ifneq ($(filter 1.%,$(CONFIG_LIBGPIOD_VERSION)), )
 PROGRAMMER_OBJS += linux_gpio_spi.o
+else
+PROGRAMMER_OBJS += linux_gpio2_spi.o
+endif
 endif
 
 ifeq ($(CONFIG_MSTARDDC_SPI), yes)
diff --git a/linux_gpio2_spi.c b/linux_gpio2_spi.c
new file mode 100644
index 0000000..fd3cdf1
--- /dev/null
+++ b/linux_gpio2_spi.c
@@ -0,0 +1,229 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2024 Nico Huber <nico.h@gmx.de>
+ *
+ * based on linux_gpio_spi.c
+ * Copyright (C) 2023 Steve Markgraf <steve@steve-m.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <errno.h>
+#include <gpiod.h>
+#include "programmer.h"
+#include "spi.h"
+#include "flash.h"
+
+#define CONSUMER "flashprog"
+
+enum {
+	CS	= 0,
+	SCK	= 1,
+	MOSI	= 2,
+	MISO	= 3,
+	MAX_LINES
+};
+
+struct linux_gpio_spi {
+	struct gpiod_chip *chip;
+	struct gpiod_line_request *lines;
+	unsigned int offsets[MAX_LINES];
+};
+
+static void linux_gpio_spi_bitbang_set_cs(int val, void *data)
+{
+	const struct linux_gpio_spi *const gpio_spi = data;
+
+	if (gpiod_line_request_set_value(gpio_spi->lines, gpio_spi->offsets[CS], val) < 0)
+		msg_perr("Setting cs line failed: %s\n", strerror(errno));
+}
+
+static void linux_gpio_spi_bitbang_set_sck(int val, void *data)
+{
+	const struct linux_gpio_spi *const gpio_spi = data;
+
+	if (gpiod_line_request_set_value(gpio_spi->lines, gpio_spi->offsets[SCK], val) < 0)
+		msg_perr("Setting sck line failed: %s\n", strerror(errno));
+}
+
+static void linux_gpio_spi_bitbang_set_mosi(int val, void *data)
+{
+	struct linux_gpio_spi *const gpio_spi = data;
+
+	if (gpiod_line_request_set_value(gpio_spi->lines, gpio_spi->offsets[MOSI], val) < 0)
+		msg_perr("Setting mosi line failed: %s\n", strerror(errno));
+}
+
+static int linux_gpio_spi_bitbang_get_miso(void *data)
+{
+	struct linux_gpio_spi *const gpio_spi = data;
+
+	const enum gpiod_line_value ret =
+		gpiod_line_request_get_value(gpio_spi->lines, gpio_spi->offsets[MISO]);
+	if (ret < 0)
+		msg_perr("Getting miso line failed: %s\n", strerror(errno));
+	return ret;
+}
+
+static void linux_gpio_spi_bitbang_set_sck_set_mosi(int sck, int mosi, void *data)
+{
+	struct linux_gpio_spi *const gpio_spi = data;
+
+	enum gpiod_line_value vals[] = { sck, mosi };
+	if (gpiod_line_request_set_values_subset(gpio_spi->lines, 2, &gpio_spi->offsets[SCK], vals) < 0)
+		msg_perr("Setting sck/mosi lines failed: %s\n", strerror(errno));
+}
+
+static struct bitbang_spi_master bitbang_spi_master_gpiod = {
+	.set_cs			= linux_gpio_spi_bitbang_set_cs,
+	.set_sck		= linux_gpio_spi_bitbang_set_sck,
+	.set_mosi		= linux_gpio_spi_bitbang_set_mosi,
+	.get_miso		= linux_gpio_spi_bitbang_get_miso,
+	.set_sck_set_mosi	= linux_gpio_spi_bitbang_set_sck_set_mosi,
+};
+
+static int linux_gpio_spi_shutdown(void *data)
+{
+	struct linux_gpio_spi *gpio_spi = data;
+
+	if (gpio_spi->lines)
+		gpiod_line_request_release(gpio_spi->lines);
+	if (gpio_spi->chip)
+		gpiod_chip_close(gpio_spi->chip);
+
+	free(data);
+
+	return 0;
+}
+
+static int linux_gpio_spi_init(void)
+{
+	struct linux_gpio_spi *gpio_spi = NULL;
+	const char *param_str[] = { "cs", "sck", "mosi", "miso", "gpiochip" };
+	const bool param_required[] = { true, true, true, true, false };
+	unsigned int param_int[ARRAY_SIZE(param_str)];
+	unsigned int i;
+	int r;
+
+	gpio_spi = calloc(1, sizeof(*gpio_spi));
+	if (!gpio_spi) {
+		msg_perr("Unable to allocate space for SPI master data\n");
+		return SPI_GENERIC_ERROR;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(param_str); i++) {
+		char *param = extract_programmer_param(param_str[i]);
+		char *endptr;
+		r = 1;
+
+		if (param) {
+			errno = 0;
+			param_int[i] = strtoul(param, &endptr, 10);
+			r = (*endptr != '\0') || (errno != 0);
+			free(param);
+		} else {
+			param_int[i] = UINT_MAX;
+		}
+
+		if ((param_required[i] || param) && r) {
+			msg_perr("Missing or invalid required programmer "
+				 "parameter %s=<n>\n", param_str[i]);
+			goto err_exit;
+		}
+	}
+
+	char *const dev = extract_programmer_param("dev");
+	const unsigned int gpiochip = param_int[MAX_LINES];
+	if (!dev && gpiochip == UINT_MAX) {
+		msg_perr("Either a `dev' or `gpiochip' parameter must be specified.\n"
+			 "e.g. `-p linux_gpio2_spi:dev=/dev/gpiochip0'.\n");
+		goto err_exit;
+	}
+	if (dev && gpiochip != UINT_MAX) {
+		msg_perr("Only one of `dev' or `gpiochip' parameters can be specified.\n");
+		goto free_dev_exit;
+	}
+
+	const char *devpath;
+	char devpath_template[] = "/dev/gpiochipX";
+	if (dev) {
+		devpath = dev;
+	} else if (gpiochip > 9) {
+		msg_perr("Maximum `gpiochip' number supported is 9.\n");
+		goto err_exit;
+	} else {
+		devpath_template[13] = '0' + gpiochip;
+		devpath = devpath_template;
+	}
+
+	gpio_spi->chip = gpiod_chip_open(devpath);
+	if (!gpio_spi->chip) {
+		msg_perr("Failed to open gpiochip `%s': %s\n", devpath, strerror(errno));
+		goto free_dev_exit;
+	}
+	free(dev);
+
+	struct gpiod_line_settings *const in = gpiod_line_settings_new();
+	struct gpiod_line_settings *const out = gpiod_line_settings_new();
+	struct gpiod_line_config *const cfg = gpiod_line_config_new();
+	if (!in || !out || !cfg) {
+		msg_perr("Unable to allocate space for GPIO line config\n");
+		if (cfg)
+			gpiod_line_config_free(cfg);
+		if (out)
+			gpiod_line_settings_free(out);
+		if (in)
+			gpiod_line_settings_free(in);
+		goto err_exit;
+	}
+
+	gpiod_line_settings_set_direction(in, GPIOD_LINE_DIRECTION_INPUT);
+	gpiod_line_settings_set_direction(out, GPIOD_LINE_DIRECTION_OUTPUT);
+
+	gpiod_line_config_add_line_settings(cfg, &param_int[CS], 3, out);
+	gpiod_line_config_add_line_settings(cfg, &param_int[MISO], 1, in);
+
+	gpiod_line_settings_free(out);
+	gpiod_line_settings_free(in);
+
+	gpio_spi->lines = gpiod_chip_request_lines(gpio_spi->chip, NULL, cfg);
+	if (!gpio_spi->lines) {
+		msg_perr("Failed to acquire GPIO lines\n");
+		goto err_exit;
+	}
+
+	gpiod_line_config_free(cfg);
+
+	memcpy(gpio_spi->offsets, param_int, sizeof(gpio_spi->offsets));
+
+	if (register_shutdown(linux_gpio_spi_shutdown, gpio_spi))
+		goto err_exit;
+
+	/* shutdown function does the cleanup */
+	return register_spi_bitbang_master(&bitbang_spi_master_gpiod, gpio_spi);
+
+free_dev_exit:
+	free(dev);
+err_exit:
+	linux_gpio_spi_shutdown(gpio_spi);
+	return SPI_GENERIC_ERROR;
+}
+
+const struct programmer_entry programmer_linux_gpio_spi = {
+	.name		= "linux_gpio_spi",
+	.type		= OTHER,
+	.devs.note	= "Device file /dev/gpiochip<n>\n",
+	.init		= linux_gpio_spi_init,
+};
diff --git a/meson.build b/meson.build
index c14ab4b..940f113 100644
--- a/meson.build
+++ b/meson.build
@@ -298,7 +298,9 @@
     'systems' : [ 'linux' ],
     'deps'    : [ libgpiod ],
     'groups'  : [ group_gpiod, group_external ],
-    'srcs'    : files('linux_gpio_spi.c'),
+    'srcs'    : libgpiod.version() < '2.0.0'
+                  ? files('linux_gpio_spi.c')
+                  : files('linux_gpio2_spi.c'),
     'flags'   : [ '-DCONFIG_LINUX_GPIO_SPI=1' ],
   },
   'linux_mtd' : {