From: Markus Stockhausen <markus.stockhausen@gmx.de>
To: brgl@kernel.org, wsa+renesas@sang-engineering.com,
andi.shyti@kernel.org, robh@kernel.org, krzk+dt@kernel.org,
conor+dt@kernel.org, linux-i2c@vger.kernel.org,
devicetree@vger.kernel.org
Cc: Markus Stockhausen <markus.stockhausen@gmx.de>
Subject: [PATCH v2 2/2] i2c: Add driver for gpio based busses with shared SCL
Date: Mon, 11 May 2026 18:25:28 +0200 [thread overview]
Message-ID: <20260511162528.84508-3-markus.stockhausen@gmx.de> (raw)
In-Reply-To: <20260511162528.84508-1-markus.stockhausen@gmx.de>
Some lower end hardware (especially Realtek based switches) are
designed with multiple I2C busses that share a single clock
(SCL) line. E.g. the D-Link DGS-1250-28X realizes 4 I2C SFP busses
with 5 gpios.
Provide a i2c-gpio-shared driver that handles such hardware designs.
It manages up to 32 buses that share a single SCL line. All data
transfers (including recovery) are synchronized with a central mutex.
Signed-off-by: Markus Stockhausen <markus.stockhausen@gmx.de>
---
drivers/i2c/busses/Kconfig | 10 ++
drivers/i2c/busses/Makefile | 1 +
drivers/i2c/busses/i2c-gpio-shared.c | 257 +++++++++++++++++++++++++++
3 files changed, 268 insertions(+)
create mode 100644 drivers/i2c/busses/i2c-gpio-shared.c
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index 8c935f867a37..fa05db759b15 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -691,6 +691,16 @@ config I2C_GPIO
This is a very simple bitbanging I2C driver utilizing the
arch-neutral GPIO API to control the SCL and SDA lines.
+config I2C_GPIO_SHARED
+ tristate "multiple GPIO-based bitbanging I2C with shared SCL"
+ depends on GPIOLIB || COMPILE_TEST
+ select I2C_ALGOBIT
+ help
+ This is an alternative of the I2C GPIO driver for devices with only
+ few GPIO pins where multiple busses with dedicated SDA lines share
+ a single SCL line. It can handle up to 32 buses, where N shared I2C
+ buses are implemented with N+1 GPIO.
+
config I2C_GPIO_FAULT_INJECTOR
bool "GPIO-based fault injector"
depends on I2C_GPIO
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index 547123ab351f..724b09e613cb 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -66,6 +66,7 @@ obj-$(CONFIG_I2C_EG20T) += i2c-eg20t.o
obj-$(CONFIG_I2C_EMEV2) += i2c-emev2.o
obj-$(CONFIG_I2C_EXYNOS5) += i2c-exynos5.o
obj-$(CONFIG_I2C_GPIO) += i2c-gpio.o
+obj-$(CONFIG_I2C_GPIO_SHARED) += i2c-gpio-shared.o
obj-$(CONFIG_I2C_HIGHLANDER) += i2c-highlander.o
obj-$(CONFIG_I2C_HISI) += i2c-hisi.o
obj-$(CONFIG_I2C_HIX5HD2) += i2c-hix5hd2.o
diff --git a/drivers/i2c/busses/i2c-gpio-shared.c b/drivers/i2c/busses/i2c-gpio-shared.c
new file mode 100644
index 000000000000..4c2e4011fcbf
--- /dev/null
+++ b/drivers/i2c/busses/i2c-gpio-shared.c
@@ -0,0 +1,257 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Bitbanging driver for up to 32 I2C buses that share a single SCL pin */
+
+#include <linux/gpio/consumer.h>
+#include <linux/i2c-algo-bit.h>
+#include <linux/i2c.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+
+#define MAX_BUSES 32
+
+struct gpio_shared_ctx;
+
+struct gpio_shared_bus {
+ struct gpio_desc *sda;
+ struct i2c_adapter adap;
+ struct i2c_algo_bit_data bit_data;
+ struct i2c_bus_recovery_info recovery_info;
+ struct gpio_shared_ctx *ctx;
+};
+
+struct gpio_shared_ctx {
+ struct gpio_desc *scl;
+ struct mutex lock;
+ struct gpio_shared_bus bus[];
+};
+
+static struct gpio_shared_bus *adap_to_bus(struct i2c_adapter *adap)
+{
+ return container_of(adap, struct gpio_shared_bus, adap);
+}
+
+static struct gpio_shared_ctx *adap_to_ctx(struct i2c_adapter *adap)
+{
+ return adap_to_bus(adap)->ctx;
+}
+
+static void gpio_shared_setsda(void *data, int state)
+{
+ struct gpio_shared_bus *bus = data;
+
+ gpiod_set_value_cansleep(bus->sda, state);
+}
+
+static void gpio_shared_setscl(void *data, int state)
+{
+ struct gpio_shared_bus *bus = data;
+
+ gpiod_set_value_cansleep(bus->ctx->scl, state);
+}
+
+static int gpio_shared_getsda(void *data)
+{
+ struct gpio_shared_bus *bus = data;
+
+ return gpiod_get_value_cansleep(bus->sda);
+}
+
+static int gpio_shared_getscl(void *data)
+{
+ struct gpio_shared_bus *bus = data;
+
+ return gpiod_get_value_cansleep(bus->ctx->scl);
+}
+
+static int gpio_shared_pre_xfer(struct i2c_adapter *adap)
+{
+ return mutex_lock_interruptible(&adap_to_ctx(adap)->lock);
+}
+
+static void gpio_shared_recovery_set_scl(struct i2c_adapter *adap, int val)
+{
+ gpiod_set_value_cansleep(adap_to_ctx(adap)->scl, val);
+}
+
+static int gpio_shared_recovery_get_scl(struct i2c_adapter *adap)
+{
+ return gpiod_get_value_cansleep(adap_to_ctx(adap)->scl);
+}
+
+static void gpio_shared_recovery_set_sda(struct i2c_adapter *adap, int val)
+{
+ gpiod_set_value_cansleep(adap_to_bus(adap)->sda, val);
+}
+
+static int gpio_shared_recovery_get_sda(struct i2c_adapter *adap)
+{
+ return gpiod_get_value_cansleep(adap_to_bus(adap)->sda);
+}
+
+static void gpio_shared_prepare_recovery(struct i2c_adapter *adap)
+{
+ mutex_lock(&adap_to_ctx(adap)->lock);
+}
+
+static void gpio_shared_unlock(struct i2c_adapter *adap)
+{
+ mutex_unlock(&adap_to_ctx(adap)->lock);
+}
+
+static void gpio_shared_del_adapter(void *data)
+{
+ i2c_del_adapter(data);
+}
+
+static int gpio_shared_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ int bus_count, sda_count, ret;
+ struct gpio_shared_ctx *ctx;
+ u32 used_buses = 0;
+
+ bus_count = device_get_child_node_count(dev);
+ if (!bus_count)
+ return dev_err_probe(dev, -EINVAL, "no buses defined\n");
+
+ if (bus_count > MAX_BUSES)
+ return dev_err_probe(dev, -EINVAL, "maximum of %d buses allowed\n", MAX_BUSES);
+
+ sda_count = gpiod_count(dev, "sda");
+ if (sda_count < 0)
+ return dev_err_probe(dev, sda_count, "sda-gpios missing\n");
+
+ if (sda_count != bus_count)
+ return dev_err_probe(dev, -EINVAL,
+ "sda-gpios count (%d) must match child node count (%d)\n",
+ sda_count, bus_count);
+
+ ctx = devm_kzalloc(dev, struct_size(ctx, bus, bus_count), GFP_KERNEL);
+ if (!ctx)
+ return dev_err_probe(dev, -ENOMEM, "memory allocation failed\n");
+
+ ret = devm_mutex_init(dev, &ctx->lock);
+ if (ret)
+ return dev_err_probe(dev, ret, "mutex initialization failed\n");
+
+ platform_set_drvdata(pdev, ctx);
+
+ ctx->scl = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
+ if (IS_ERR(ctx->scl))
+ return dev_err_probe(dev, PTR_ERR(ctx->scl), "shared SCL GPIO not found\n");
+
+ device_for_each_child_node_scoped(dev, child) {
+ struct i2c_algo_bit_data *bit_data;
+ struct i2c_bus_recovery_info *ri;
+ struct gpio_shared_bus *bus;
+ struct i2c_adapter *adap;
+ u32 bus_num, clock_freq;
+
+ /* Use the child's reg value as the index into the sda-gpios array. */
+ ret = fwnode_property_read_u32(child, "reg", &bus_num);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "missing reg property in child node\n");
+
+ if (bus_num >= sda_count)
+ return dev_err_probe(dev, -EINVAL,
+ "reg value %u out of range (max %d)\n",
+ bus_num, sda_count - 1);
+
+ if (used_buses & BIT(bus_num))
+ return dev_err_probe(dev, -EINVAL,
+ "duplicate definition of bus %d\n", bus_num);
+
+ bus = &ctx->bus[bus_num];
+ bit_data = &bus->bit_data;
+ ri = &bus->recovery_info;
+ adap = &bus->adap;
+
+ bus->sda = devm_gpiod_get_index(dev, "sda", bus_num,
+ GPIOD_OUT_HIGH_OPEN_DRAIN);
+ if (IS_ERR(bus->sda))
+ return dev_err_probe(dev, PTR_ERR(bus->sda),
+ "SDA GPIO for bus %u not found\n", bus_num);
+ /*
+ * Data transfer synchronization between multiple busses is realized by mutex
+ * locking/unlocking within pre_xfer and post_xfer helpers.
+ */
+ bus->ctx = ctx;
+ bit_data->data = bus;
+ bit_data->setsda = gpio_shared_setsda;
+ bit_data->setscl = gpio_shared_setscl;
+ bit_data->getsda = gpio_shared_getsda;
+ bit_data->getscl = gpio_shared_getscl;
+ bit_data->pre_xfer = gpio_shared_pre_xfer;
+ bit_data->post_xfer = gpio_shared_unlock;
+ bit_data->timeout = msecs_to_jiffies(100);
+ /*
+ * clock-frequency specifies the I2C bus frequency. Convert to the half-period
+ * delay in microseconds that i2c-algo-bit expects. Default to 5 us (~100 kHz)
+ * if not specified. This is usually lower than the configured frequency,
+ * especially near the 400 kHz limit.
+ */
+ if (!fwnode_property_read_u32(child, "clock-frequency", &clock_freq) &&
+ clock_freq > 0)
+ bit_data->udelay = max(1u, DIV_ROUND_UP(1000000, 2 * clock_freq));
+ else
+ bit_data->udelay = 5;
+ /*
+ * i2c_recover_bus() is called by the I2C core without going through pre_xfer
+ * and post_xfer. So the shared SCL mutex is NOT held at that point. Provide
+ * prepare_recovery/unprepare_recovery to explicitly prevent concurrent SCL
+ * access from another bus.
+ */
+ ri->recover_bus = i2c_generic_scl_recovery;
+ ri->set_scl = gpio_shared_recovery_set_scl;
+ ri->get_scl = gpio_shared_recovery_get_scl;
+ ri->set_sda = gpio_shared_recovery_set_sda;
+ ri->get_sda = gpio_shared_recovery_get_sda;
+ ri->prepare_recovery = gpio_shared_prepare_recovery;
+ ri->unprepare_recovery = gpio_shared_unlock;
+
+ adap->dev.parent = dev;
+ adap->owner = THIS_MODULE;
+ adap->algo_data = bit_data;
+ adap->bus_recovery_info = ri;
+ device_set_node(&adap->dev, child);
+ snprintf(adap->name, sizeof(adap->name), "i2c-gpio-shared:%u", bus_num);
+
+ ret = devm_add_action_or_reset(dev, gpio_shared_del_adapter, adap);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "bus %u cleanup registration failed\n", bus_num);
+
+ ret = i2c_bit_add_bus(adap);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to register bus %u\n", bus_num);
+
+ dev_info(dev, "registered I2C bus %u with shared SCL (udelay %d us)\n",
+ bus_num, bit_data->udelay);
+
+ used_buses |= BIT(bus_num);
+ }
+
+ return 0;
+}
+
+static const struct of_device_id gpio_shared_of_match[] = {
+ { .compatible = "i2c-gpio-shared" },
+ {}
+};
+MODULE_DEVICE_TABLE(of, gpio_shared_of_match);
+
+static struct platform_driver gpio_shared_driver = {
+ .probe = gpio_shared_probe,
+ .driver = {
+ .name = "i2c-gpio-shared",
+ .of_match_table = gpio_shared_of_match,
+ },
+};
+
+module_platform_driver(gpio_shared_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Markus Stockhausen <markus.stockhausen@gmx.de>");
+MODULE_DESCRIPTION("bitbanging multi I2C driver for shared SCL");
--
2.54.0
next prev parent reply other threads:[~2026-05-11 16:25 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-11 16:25 [PATCH v2 0/2] i2c: Add i2c-shared-gpio driver Markus Stockhausen
2026-05-11 16:25 ` [PATCH v2 1/2] dt-bindings: i2c: Add i2c-shared-gpio Markus Stockhausen
2026-05-11 17:36 ` Rob Herring (Arm)
2026-05-12 20:59 ` sashiko-bot
2026-05-11 16:25 ` Markus Stockhausen [this message]
2026-05-12 21:44 ` [PATCH v2 2/2] i2c: Add driver for gpio based busses with shared SCL sashiko-bot
2026-05-12 11:00 ` [PATCH v2 0/2] i2c: Add i2c-shared-gpio driver Bartosz Golaszewski
2026-05-13 5:33 ` AW: " markus.stockhausen
2026-05-13 7:12 ` Wolfram Sang
2026-05-13 7:26 ` Bartosz Golaszewski
2026-05-13 7:47 ` Wolfram Sang
2026-05-13 7:50 ` Bartosz Golaszewski
2026-05-13 12:11 ` Rob Herring
2026-05-13 12:25 ` AW: " markus.stockhausen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260511162528.84508-3-markus.stockhausen@gmx.de \
--to=markus.stockhausen@gmx.de \
--cc=andi.shyti@kernel.org \
--cc=brgl@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-i2c@vger.kernel.org \
--cc=robh@kernel.org \
--cc=wsa+renesas@sang-engineering.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox