Devicetree
 help / color / mirror / Atom feed
From: Markus Stockhausen <markus.stockhausen@gmx.de>
To: 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 2/2] i2c: shared-gpio: Add driver for gpio based busses with shared SCL
Date: Thu,  7 May 2026 20:17:11 +0200	[thread overview]
Message-ID: <20260507181711.2696783-3-markus.stockhausen@gmx.de> (raw)
In-Reply-To: <20260507181711.2696783-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. devices like the D-Link DGS-1250-28X can realize
4 I2C SFP busses with 5 gpios this way.

Provide a i2c-gpio-shared driver (derived from i2c-gpio) that
handles such hardware designs.

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 | 177 +++++++++++++++++++++++++++
 3 files changed, 188 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..f3ab68beeb7f 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 an arbitrary number of N shared
+	  busses implemented with N+1 gpios.
+
 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..de082888860c
--- /dev/null
+++ b/drivers/i2c/busses/i2c-gpio-shared.c
@@ -0,0 +1,177 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Bitbanging driver for multiple I2C busses with shared SCL pin using the GPIO API
+ * Copyright (c) 2025 Markus Stockhausen <markus.stockhausen at gmx.de>
+ */
+
+#include <linux/gpio/consumer.h>
+#include <linux/i2c-algo-bit.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+
+struct gpio_shared_ctx;
+
+struct gpio_shared_bus {
+	struct gpio_desc *sda;
+	struct i2c_adapter adap;
+	struct i2c_algo_bit_data bit_data;
+	struct gpio_shared_ctx *ctx;
+};
+
+struct gpio_shared_ctx {
+	struct device *dev;
+	struct gpio_desc *scl;
+	struct mutex lock;
+	struct gpio_shared_bus bus[];
+};
+
+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;
+	struct gpio_shared_ctx *ctx = bus->ctx;
+
+	gpiod_set_value_cansleep(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;
+	struct gpio_shared_ctx *ctx = bus->ctx;
+
+	return gpiod_get_value_cansleep(ctx->scl);
+}
+
+static int gpio_shared_pre_xfer(struct i2c_adapter *adap)
+{
+	struct gpio_shared_bus *bus = container_of(adap, struct gpio_shared_bus, adap);
+	struct gpio_shared_ctx *ctx = bus->ctx;
+
+	return mutex_lock_interruptible(&ctx->lock);
+}
+
+static void gpio_shared_post_xfer(struct i2c_adapter *adap)
+{
+	struct gpio_shared_bus *bus = container_of(adap, typeof(*bus), adap);
+	struct gpio_shared_ctx *ctx = bus->ctx;
+
+	mutex_unlock(&ctx->lock);
+}
+
+static void gpio_shared_del_adapter(void *data)
+{
+	i2c_del_adapter(data);
+}
+
+static int gpio_shared_probe(struct platform_device *pdev)
+{
+	int bus_count, msecs, ret, bus_num = 0;
+	struct device *dev = &pdev->dev;
+	struct gpio_shared_ctx *ctx;
+
+	bus_count = device_get_child_node_count(dev);
+	if (!bus_count)
+		return dev_err_probe(dev, -EINVAL, "no busses defined\n");
+
+	ctx = devm_kzalloc(dev, struct_size(ctx, bus, bus_count), GFP_KERNEL);
+	if (!ctx)
+		return dev_err_probe(dev, -ENOMEM, "memory allocation failed\n");
+
+	ctx->dev = dev;
+	mutex_init(&ctx->lock);
+
+	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 node not found\n");
+
+	device_for_each_child_node_scoped(dev, child) {
+		struct gpio_shared_bus *bus = &ctx->bus[bus_num];
+		struct i2c_adapter *adap = &bus->adap;
+		struct i2c_algo_bit_data *bit_data = &bus->bit_data;
+
+		bus->sda = devm_fwnode_gpiod_get(dev, child, "sda", GPIOD_OUT_HIGH_OPEN_DRAIN,
+						 fwnode_get_name(child));
+		if (IS_ERR(bus->sda))
+			return dev_err_probe(dev, PTR_ERR(bus->sda),
+					     "SDA node for bus %d not found\n", bus_num);
+		bus->ctx = ctx;
+		bit_data->data = bus;
+		bit_data->setsda = gpio_shared_setsda;
+		bit_data->setscl = gpio_shared_setscl;
+		bit_data->pre_xfer = gpio_shared_pre_xfer;
+		bit_data->post_xfer = gpio_shared_post_xfer;
+
+		if (fwnode_property_read_u32(child, "i2c-gpio-shared,delay-us", &bit_data->udelay))
+			bit_data->udelay = 5;
+		if (!fwnode_property_read_bool(child, "i2c-gpio-shared,sda-output-only"))
+			bit_data->getsda = gpio_shared_getsda;
+
+		if (!device_property_read_bool(dev, "i2c-gpio-shared,scl-output-only"))
+			bit_data->getscl = gpio_shared_getscl;
+		if (!device_property_read_u32(dev, "i2c-gpio-shared,timeout-ms", &msecs))
+			bit_data->timeout = msecs_to_jiffies(msecs);
+		else
+			bit_data->timeout = HZ / 10; /* 100ms */
+
+		if (gpiod_cansleep(bus->sda) || gpiod_cansleep(ctx->scl))
+			dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing\n");
+
+		adap->dev.parent = dev;
+		adap->owner = THIS_MODULE;
+		adap->algo_data = &bus->bit_data;
+		device_set_node(&adap->dev, child);
+		snprintf(adap->name, sizeof(adap->name),
+			 "i2c-gpio-shared:%s", fwnode_get_name(child));
+
+		ret = i2c_bit_add_bus(adap);
+		if (ret)
+			return dev_err_probe(dev, ret, "failed to register bus %d\n", bus_num);
+
+		ret = devm_add_action_or_reset(dev, gpio_shared_del_adapter, adap);
+		if (ret)
+			return dev_err_probe(dev, ret,
+					     "bus %d cleanup registration failed\n", bus_num);
+
+		dev_info(dev, "shared I2C bus %u using lines %u (SDA) and %u (SCL) delay=%d\n",
+			 bus_num, desc_to_gpio(bus->sda), desc_to_gpio(ctx->scl),
+			 bit_data->udelay);
+
+		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 at gmx.de>");
+MODULE_DESCRIPTION("bitbanging multi I2C driver for shared SCL");
-- 
2.54.0


      parent reply	other threads:[~2026-05-07 18:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-07 18:17 [PATCH 0/2] i2c: Add i2c-shared-gpio driver Markus Stockhausen
2026-05-07 18:17 ` [PATCH 1/2] dt-bindings: i2c: Add i2c-shared-gpio Markus Stockhausen
2026-05-07 19:30   ` Rob Herring (Arm)
2026-05-08 13:18   ` Rob Herring
2026-05-09 11:16     ` AW: " markus.stockhausen
2026-05-07 18:17 ` Markus Stockhausen [this message]

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=20260507181711.2696783-3-markus.stockhausen@gmx.de \
    --to=markus.stockhausen@gmx.de \
    --cc=andi.shyti@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 \
    /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