mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Long Zhao via B4 Relay <devnull+longzhao.ambarella.com@kernel.org>
To: Andi Shyti <andi.shyti@kernel.org>, linux-i2c@vger.kernel.org
Cc: Long Zhao <zl020895@163.com>,
	linux-kernel@vger.kernel.org,  Long Zhao <longzhao@ambarella.com>
Subject: [PATCH 2/3] i2c: ambarella: add IDC master for CV75
Date: Tue, 01 Dec 2026 18:46:39 +0800	[thread overview]
Message-ID: <20261201-cv75-i2c-v1-2-729176f61836@ambarella.com> (raw)
In-Reply-To: <20261201-cv75-i2c-v1-0-729176f61836@ambarella.com>

From: Long Zhao <longzhao@ambarella.com>

Add a platform driver for the Ambarella I2C master. The transfer
path is an IRQ state machine with wait_for_completion. The APB clock
stays enabled for the lifetime of the device. Bus frequency comes
from clock-frequency and is clamped to 1 kHz .. 1 MHz.

Signed-off-by: Long Zhao <longzhao@ambarella.com>
---
 drivers/i2c/busses/Kconfig         |  11 +
 drivers/i2c/busses/Makefile        |   1 +
 drivers/i2c/busses/i2c-ambarella.c | 469 +++++++++++++++++++++++++++++++++++++
 3 files changed, 481 insertions(+)

diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index d7b89508311f..11dfa070b530 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -405,6 +405,17 @@ config I2C_ALTERA
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-altera.
 
+config I2C_AMBARELLA
+	tristate "Ambarella I2C controller"
+	depends on ARCH_AMBARELLA || COMPILE_TEST
+	depends on OF
+	help
+	  Say yes here to support the Ambarella I2C master
+	  controller found on Ambarella media SoCs such as the CV75.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called i2c-ambarella.
+
 config I2C_ASPEED
 	tristate "Aspeed I2C Controller"
 	depends on ARCH_ASPEED || COMPILE_TEST
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index 3755c54b3d82..a5b418008cce 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_I2C_POWERMAC)	+= i2c-powermac.o
 
 # Embedded system I2C/SMBus host controller drivers
 obj-$(CONFIG_I2C_ALTERA)	+= i2c-altera.o
+obj-$(CONFIG_I2C_AMBARELLA)	+= i2c-ambarella.o
 obj-$(CONFIG_I2C_AMD_MP2)	+= i2c-amd-mp2-pci.o i2c-amd-mp2-plat.o
 obj-$(CONFIG_I2C_AMD_ASF)	+= i2c-amd-asf-plat.o
 obj-$(CONFIG_I2C_ASPEED)	+= i2c-aspeed.o
diff --git a/drivers/i2c/busses/i2c-ambarella.c b/drivers/i2c/busses/i2c-ambarella.c
new file mode 100644
index 000000000000..99fed762c3ff
--- /dev/null
+++ b/drivers/i2c/busses/i2c-ambarella.c
@@ -0,0 +1,469 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/clk.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+#define IDC_ENR				0x00
+#define IDC_CTRL				0x04
+#define IDC_DATA			0x08
+#define IDC_STS				0x0c
+#define IDC_PSLL				0x10
+#define IDC_PSLH				0x14
+#define IDC_DUTYCYCLE		0x24
+#define IDC_STRETCHSCL		0x28
+
+#define IDC_ENR_ENABLE		0x01
+#define IDC_ENR_DISABLE		0x00
+
+#define IDC_CTRL_STOP		0x08
+#define IDC_CTRL_START		0x04
+#define IDC_CTRL_ACK		0x01
+#define IDC_CTRL_CLS		0x00
+
+#define IDC_STS_FIFO_EMP	0x04
+
+#define AMBARELLA_I2C_RETRIES		3
+
+#define AMBARELLA_I2C_STOP_WAIT_US	10
+#define AMBARELLA_I2C_STOP_TIMEOUT_US	500
+
+enum ambarella_i2c_state {
+	AMBA_I2C_STATE_IDLE,
+	AMBA_I2C_STATE_START,
+	AMBA_I2C_STATE_START_TEN,
+	AMBA_I2C_STATE_START_NEW,
+	AMBA_I2C_STATE_READ,
+	AMBA_I2C_STATE_READ_STOP,
+	AMBA_I2C_STATE_WRITE,
+	AMBA_I2C_STATE_WRITE_WAIT_ACK,
+	AMBA_I2C_STATE_NO_ACK,
+	AMBA_I2C_STATE_ERROR,
+};
+
+#define IDC_STS_HW_IDLE		0
+#define IDC_STS_HW_SHIFT		4
+#define IDC_STS_HW_MASK		0xf
+
+struct ambarella_i2c {
+	struct device		*dev;
+	void __iomem		*base;
+	struct clk		*clk;
+
+	struct i2c_adapter	adap;
+
+	struct i2c_msg		*msgs;
+	struct completion	complete;
+
+	spinlock_t		lock;	/* protects state and msg cursor */
+	enum ambarella_i2c_state state;
+	unsigned int		num_msgs;
+	unsigned int		msg_idx;
+	unsigned int		bus_freq;
+	u16			addr_rw;
+};
+
+static bool ambarella_i2c_hw_idle(struct ambarella_i2c *i2c)
+{
+	u32 status = readl_relaxed(i2c->base + IDC_STS);
+
+	return ((status >> IDC_STS_HW_SHIFT) & IDC_STS_HW_MASK) ==
+	       IDC_STS_HW_IDLE;
+}
+
+static void ambarella_i2c_disable(struct ambarella_i2c *i2c)
+{
+	writeb_relaxed(IDC_ENR_DISABLE, i2c->base + IDC_ENR);
+}
+
+static void ambarella_i2c_enable(struct ambarella_i2c *i2c)
+{
+	writeb_relaxed(IDC_ENR_ENABLE, i2c->base + IDC_ENR);
+}
+
+static void ambarella_i2c_set_freq(struct ambarella_i2c *i2c)
+{
+	unsigned long apb = clk_get_rate(i2c->clk);
+	u32 prescale = 0;
+	u8 duty;
+
+	ambarella_i2c_disable(i2c);
+
+	if (i2c->bus_freq <= I2C_MAX_STANDARD_MODE_FREQ)
+		duty = 0;
+	else
+		duty = 2;
+
+	if (apb >= (6UL + duty) * i2c->bus_freq)
+		prescale = ((apb / i2c->bus_freq) - 2) / (4 + duty) - 1;
+	else
+		dev_warn(i2c->dev,
+			 "APB %lu Hz too low for %u Hz bus, using prescale 0\n",
+			 apb, i2c->bus_freq);
+
+	writeb_relaxed(prescale & 0xff, i2c->base + IDC_PSLL);
+	writeb_relaxed((prescale >> 8) & 0xff, i2c->base + IDC_PSLH);
+	writeb_relaxed(duty, i2c->base + IDC_DUTYCYCLE);
+	writeb_relaxed(1, i2c->base + IDC_STRETCHSCL);
+
+	ambarella_i2c_enable(i2c);
+}
+
+static void ambarella_i2c_hw_init(struct ambarella_i2c *i2c)
+{
+	ambarella_i2c_set_freq(i2c);
+
+	scoped_guard(spinlock_irqsave, &i2c->lock) {
+		i2c->msgs = NULL;
+		i2c->num_msgs = 0;
+		i2c->state = AMBA_I2C_STATE_IDLE;
+	}
+}
+
+static void ambarella_i2c_stop(struct ambarella_i2c *i2c,
+			       enum ambarella_i2c_state state, u32 *ctrl)
+{
+	if (state != AMBA_I2C_STATE_IDLE)
+		*ctrl |= IDC_CTRL_ACK;
+
+	i2c->state = state;
+	i2c->msgs = NULL;
+	i2c->num_msgs = 0;
+
+	*ctrl |= IDC_CTRL_STOP;
+
+	if (state == AMBA_I2C_STATE_IDLE || state == AMBA_I2C_STATE_NO_ACK)
+		complete(&i2c->complete);
+}
+
+static bool ambarella_i2c_check_ack(struct ambarella_i2c *i2c, u32 *ctrl,
+				    unsigned int retries)
+{
+	if (!(*ctrl & IDC_CTRL_ACK))
+		return true;
+
+	if (i2c->msgs->flags & I2C_M_IGNORE_NAK)
+		return true;
+
+	if ((i2c->msgs->flags & I2C_M_RD) &&
+	    (i2c->msgs->flags & I2C_M_NO_RD_ACK))
+		return true;
+
+	while ((*ctrl & IDC_CTRL_ACK) && retries--) {
+		udelay(100);
+		*ctrl = readl_relaxed(i2c->base + IDC_CTRL);
+	}
+
+	if (*ctrl & IDC_CTRL_ACK) {
+		*ctrl = IDC_CTRL_CLS;
+		ambarella_i2c_stop(i2c, AMBA_I2C_STATE_NO_ACK, ctrl);
+		return false;
+	}
+
+	return true;
+}
+
+static void ambarella_i2c_start_msg(struct ambarella_i2c *i2c)
+{
+	struct i2c_msg *msg = i2c->msgs;
+	u8 addr;
+
+	i2c->msg_idx = 0;
+	i2c->addr_rw = i2c_8bit_addr_from_msg(msg);
+	if (msg->flags & I2C_M_REV_DIR_ADDR)
+		i2c->addr_rw ^= 1;
+
+	if (msg->flags & I2C_M_TEN) {
+		i2c->state = AMBA_I2C_STATE_START_TEN;
+		addr = i2c_10bit_addr_hi_from_msg(msg);
+		if (msg->flags & I2C_M_REV_DIR_ADDR)
+			addr ^= I2C_M_RD;
+		writeb_relaxed(addr, i2c->base + IDC_DATA);
+	} else {
+		i2c->state = AMBA_I2C_STATE_START;
+		writeb_relaxed(i2c->addr_rw, i2c->base + IDC_DATA);
+	}
+
+	writel_relaxed(IDC_CTRL_START, i2c->base + IDC_CTRL);
+}
+
+static irqreturn_t ambarella_i2c_irq(int irq, void *dev_id)
+{
+	struct ambarella_i2c *i2c = dev_id;
+	u32 ack = IDC_CTRL_CLS;
+	u32 status;
+	u32 ctrl;
+
+	guard(spinlock)(&i2c->lock);
+
+	status = readl_relaxed(i2c->base + IDC_STS);
+	ctrl = readl_relaxed(i2c->base + IDC_CTRL);
+
+	switch (i2c->state) {
+	case AMBA_I2C_STATE_START:
+		if (!ambarella_i2c_check_ack(i2c, &ctrl, 1)) {
+			ack = ctrl;
+			break;
+		}
+
+		if (!i2c->msgs->len) {
+			ambarella_i2c_stop(i2c, AMBA_I2C_STATE_IDLE, &ack);
+			break;
+		}
+		if (i2c->msgs->flags & I2C_M_RD) {
+			if (i2c->msgs->len == 1)
+				ack |= IDC_CTRL_ACK;
+			i2c->state = AMBA_I2C_STATE_READ;
+		} else {
+			i2c->state = AMBA_I2C_STATE_WRITE;
+			writeb_relaxed(i2c->msgs->buf[i2c->msg_idx],
+				       i2c->base + IDC_DATA);
+			i2c->state = AMBA_I2C_STATE_WRITE_WAIT_ACK;
+		}
+		break;
+
+	case AMBA_I2C_STATE_START_TEN:
+		i2c->state = AMBA_I2C_STATE_START;
+		writeb_relaxed(i2c_10bit_addr_lo_from_msg(i2c->msgs),
+			       i2c->base + IDC_DATA);
+		break;
+
+	case AMBA_I2C_STATE_START_NEW:
+		ambarella_i2c_start_msg(i2c);
+		return IRQ_HANDLED;
+
+	case AMBA_I2C_STATE_READ:
+		i2c->msgs->buf[i2c->msg_idx] =
+			readb_relaxed(i2c->base + IDC_DATA);
+		i2c->msg_idx++;
+
+		if (i2c->msg_idx >= i2c->msgs->len - 1) {
+			if (i2c->num_msgs > 1) {
+				i2c->msgs++;
+				i2c->num_msgs--;
+				i2c->state = AMBA_I2C_STATE_START_NEW;
+			} else if (i2c->msg_idx > i2c->msgs->len - 1) {
+				ambarella_i2c_stop(i2c, AMBA_I2C_STATE_IDLE, &ack);
+			} else {
+				i2c->state = AMBA_I2C_STATE_READ_STOP;
+				ack |= IDC_CTRL_ACK;
+			}
+		}
+		break;
+
+	case AMBA_I2C_STATE_READ_STOP:
+		i2c->msgs->buf[i2c->msg_idx] =
+			readb_relaxed(i2c->base + IDC_DATA);
+		i2c->msg_idx++;
+		ambarella_i2c_stop(i2c, AMBA_I2C_STATE_IDLE, &ack);
+		break;
+
+	case AMBA_I2C_STATE_WRITE_WAIT_ACK:
+		if (!ambarella_i2c_check_ack(i2c, &ctrl, 1)) {
+			ack = ctrl;
+			break;
+		}
+		i2c->state = AMBA_I2C_STATE_WRITE;
+		i2c->msg_idx++;
+
+		if (i2c->msg_idx >= i2c->msgs->len) {
+			if (i2c->num_msgs > 1) {
+				i2c->msgs++;
+				i2c->num_msgs--;
+				i2c->state = AMBA_I2C_STATE_START_NEW;
+				ambarella_i2c_start_msg(i2c);
+				return IRQ_HANDLED;
+			}
+
+			ambarella_i2c_stop(i2c, AMBA_I2C_STATE_IDLE, &ack);
+		} else {
+			writeb_relaxed(i2c->msgs->buf[i2c->msg_idx],
+				       i2c->base + IDC_DATA);
+			i2c->state = AMBA_I2C_STATE_WRITE_WAIT_ACK;
+		}
+		break;
+
+	default:
+		if ((status & IDC_STS_FIFO_EMP) &&
+		    i2c->state == AMBA_I2C_STATE_IDLE)
+			return IRQ_HANDLED;
+
+		if ((status & IDC_STS_FIFO_EMP) &&
+		    i2c->state == AMBA_I2C_STATE_NO_ACK) {
+			ack = IDC_CTRL_STOP | IDC_CTRL_ACK;
+			break;
+		}
+
+		dev_err(i2c->dev,
+			"irq in wrong state 0x%x, status 0x%x, control 0x%x\n",
+			i2c->state, status, ctrl);
+		ack = IDC_CTRL_STOP | IDC_CTRL_ACK;
+		i2c->state = AMBA_I2C_STATE_ERROR;
+		break;
+	}
+
+	writel_relaxed(ack, i2c->base + IDC_CTRL);
+	return IRQ_HANDLED;
+}
+
+static int ambarella_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
+			      int num)
+{
+	struct ambarella_i2c *i2c = i2c_get_adapdata(adap);
+	enum ambarella_i2c_state state;
+	struct device *dev = i2c->dev;
+	unsigned long time_left;
+	bool hw_idle;
+	int wait;
+	int ret;
+
+	scoped_guard(spinlock_irqsave, &i2c->lock) {
+		reinit_completion(&i2c->complete);
+		i2c->msgs = msgs;
+		i2c->num_msgs = num;
+		i2c->state = AMBA_I2C_STATE_IDLE;
+		ambarella_i2c_start_msg(i2c);
+	}
+
+	time_left = wait_for_completion_timeout(&i2c->complete, adap->timeout);
+	if (!time_left) {
+		/*
+		 * Abort the hung transfer and bounce the controller so the
+		 * next xfer does not start from a stuck state machine.
+		 */
+		scoped_guard(spinlock_irqsave, &i2c->lock) {
+			i2c->msgs = NULL;
+			i2c->num_msgs = 0;
+			ambarella_i2c_disable(i2c);
+			ambarella_i2c_enable(i2c);
+			i2c->state = AMBA_I2C_STATE_IDLE;
+		}
+	}
+
+	/*
+	 * Wait for the controller to return to idle so a STOP is
+	 * really on the bus before a mux switches the lines.
+	 */
+	wait = read_poll_timeout(ambarella_i2c_hw_idle, hw_idle, hw_idle,
+				 AMBARELLA_I2C_STOP_WAIT_US,
+				 AMBARELLA_I2C_STOP_TIMEOUT_US, false, i2c);
+	if (wait)
+		dev_dbg(dev, "controller still busy after %u us\n",
+			AMBARELLA_I2C_STOP_TIMEOUT_US);
+
+	scoped_guard(spinlock_irqsave, &i2c->lock)
+		state = i2c->state;
+
+	if (!time_left)
+		ret = -ETIMEDOUT;
+	else if (state == AMBA_I2C_STATE_IDLE)
+		ret = num;
+	else if (state == AMBA_I2C_STATE_NO_ACK)
+		ret = -ENXIO;
+	else
+		ret = -ETIMEDOUT;
+
+	if (ret == -ENXIO)
+		dev_dbg(dev, "no ACK from address 0x%x\n", i2c->addr_rw >> 1);
+	else if (ret == -ETIMEDOUT)
+		dev_err(dev, "transfer timed out at address 0x%x\n", i2c->addr_rw >> 1);
+
+	return ret;
+}
+
+static u32 ambarella_i2c_func(struct i2c_adapter *adap)
+{
+	return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL |
+	       I2C_FUNC_PROTOCOL_MANGLING;
+}
+
+static const struct i2c_algorithm ambarella_i2c_algo = {
+	.xfer		= ambarella_i2c_xfer,
+	.functionality	= ambarella_i2c_func,
+};
+
+static int ambarella_i2c_probe(struct platform_device *pdev)
+{
+	u32 freq = I2C_MAX_STANDARD_MODE_FREQ;
+	struct device *dev = &pdev->dev;
+	struct ambarella_i2c *i2c;
+	struct i2c_adapter *adap;
+	int ret;
+
+	i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
+	if (!i2c)
+		return -ENOMEM;
+
+	i2c->dev = dev;
+
+	i2c->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(i2c->base))
+		return PTR_ERR(i2c->base);
+
+	i2c->clk = devm_clk_get_enabled(dev, NULL);
+	if (IS_ERR(i2c->clk))
+		return dev_err_probe(dev, PTR_ERR(i2c->clk),
+				     "failed to enable clock\n");
+
+	of_property_read_u32(dev->of_node, "clock-frequency", &freq);
+	i2c->bus_freq = clamp(freq, 1000u, I2C_MAX_FAST_MODE_PLUS_FREQ);
+
+	spin_lock_init(&i2c->lock);
+	init_completion(&i2c->complete);
+
+	ambarella_i2c_hw_init(i2c);
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0)
+		return ret;
+
+	ret = devm_request_irq(dev, ret, ambarella_i2c_irq, 0,
+			       dev_name(dev), i2c);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to request irq\n");
+
+	adap = &i2c->adap;
+	i2c_set_adapdata(adap, i2c);
+	adap->owner = THIS_MODULE;
+	adap->dev.parent = dev;
+	adap->dev.of_node = dev->of_node;
+	strscpy(adap->name, "Ambarella I2C adapter", sizeof(adap->name));
+	adap->algo = &ambarella_i2c_algo;
+	adap->retries = AMBARELLA_I2C_RETRIES;
+
+	ret = devm_i2c_add_adapter(dev, adap);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to add i2c adapter\n");
+
+	return 0;
+}
+
+static const struct of_device_id ambarella_i2c_of_match[] = {
+	{ .compatible = "ambarella,cv75-i2c" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, ambarella_i2c_of_match);
+
+static struct platform_driver ambarella_i2c_driver = {
+	.probe	= ambarella_i2c_probe,
+	.driver	= {
+		.name		= "ambarella-i2c",
+		.of_match_table	= ambarella_i2c_of_match,
+	},
+};
+module_platform_driver(ambarella_i2c_driver);
+
+MODULE_DESCRIPTION("Ambarella I2C master controller driver");
+MODULE_AUTHOR("Long Zhao <longzhao@ambarella.com>");
+MODULE_LICENSE("GPL");

-- 
2.34.1



  parent reply	other threads:[~2026-12-01 10:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 10:46 [PATCH 0/3] i2c: add Ambarella " Long Zhao via B4 Relay
2026-12-01 10:46 ` [PATCH 1/3] dt-bindings: i2c: add Ambarella IDC controller Long Zhao via B4 Relay
2026-12-02 11:28   ` Krzysztof Kozlowski
2026-12-02 11:50     ` zl020895
2026-12-02 12:57       ` Krzysztof Kozlowski
2026-12-01 10:46 ` Long Zhao via B4 Relay [this message]
2026-12-01 10:46 ` [PATCH 3/3] MAINTAINERS: add Ambarella I2C controller Long Zhao via B4 Relay

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=20261201-cv75-i2c-v1-2-729176f61836@ambarella.com \
    --to=devnull+longzhao.ambarella.com@kernel.org \
    --cc=andi.shyti@kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longzhao@ambarella.com \
    --cc=zl020895@163.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

all inboxes | Powered by JetHome®