[PATCH 2/3] i2c: ambarella: add IDC master for CV75

From: Long Zhao via B4 Relay

Date: Tue Dec 01 2026 - 05:47:03 EST


From: Long Zhao <longzhao@xxxxxxxxxxxxx>

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@xxxxxxxxxxxxx>
---
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@xxxxxxxxxxxxx>");
+MODULE_LICENSE("GPL");

--
2.34.1