* [PATCH v2 1/5] i2c: mt7621: rework cmd/wait OPs to support atomic afer variant
2025-07-21 18:05 [PATCH v2 0/5] i2c: mt7621: improve support for Airoha Christian Marangi
@ 2025-07-21 18:05 ` Christian Marangi
2025-07-21 18:05 ` [PATCH v2 2/5] i2c: mt7621: clear pending interrupt on i2c reset Christian Marangi
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Christian Marangi @ 2025-07-21 18:05 UTC (permalink / raw)
To: Stefan Roese, Andi Shyti, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Matthias Brugger, AngeloGioacchino Del Regno,
linux-i2c, devicetree, linux-kernel, linux-arm-kernel,
linux-mediatek
Cc: Christian Marangi
It was reported the need for atomic operation on some Airoha SoC that
makes use of I2C bus. Rework the cmd/wait OPs to suppor the xfer_atomic
variant. To support this it's mainlin needed to do the readl poll in
atomic context.
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
drivers/i2c/busses/i2c-mt7621.c | 59 ++++++++++++++++++++++-----------
1 file changed, 40 insertions(+), 19 deletions(-)
diff --git a/drivers/i2c/busses/i2c-mt7621.c b/drivers/i2c/busses/i2c-mt7621.c
index 2103f21f9ddd..c0e13f9af3e5 100644
--- a/drivers/i2c/busses/i2c-mt7621.c
+++ b/drivers/i2c/busses/i2c-mt7621.c
@@ -67,14 +67,19 @@ struct mtk_i2c {
struct clk *clk;
};
-static int mtk_i2c_wait_idle(struct mtk_i2c *i2c)
+static int mtk_i2c_wait_idle(struct mtk_i2c *i2c, bool atomic)
{
int ret;
u32 val;
- ret = readl_relaxed_poll_timeout(i2c->base + REG_SM0CTL1_REG,
- val, !(val & SM0CTL1_TRI),
- 10, TIMEOUT_MS * 1000);
+ if (atomic)
+ ret = readl_relaxed_poll_timeout_atomic(i2c->base + REG_SM0CTL1_REG,
+ val, !(val & SM0CTL1_TRI),
+ 10, TIMEOUT_MS * 1000);
+ else
+ ret = readl_relaxed_poll_timeout(i2c->base + REG_SM0CTL1_REG,
+ val, !(val & SM0CTL1_TRI),
+ 10, TIMEOUT_MS * 1000);
if (ret)
dev_dbg(i2c->dev, "idle err(%d)\n", ret);
@@ -117,27 +122,28 @@ static int mtk_i2c_check_ack(struct mtk_i2c *i2c, u32 expected)
return ((ack & ack_expected) == ack_expected) ? 0 : -ENXIO;
}
-static int mtk_i2c_start(struct mtk_i2c *i2c)
+static int mtk_i2c_start(struct mtk_i2c *i2c, bool atomic)
{
iowrite32(SM0CTL1_START | SM0CTL1_TRI, i2c->base + REG_SM0CTL1_REG);
- return mtk_i2c_wait_idle(i2c);
+ return mtk_i2c_wait_idle(i2c, atomic);
}
-static int mtk_i2c_stop(struct mtk_i2c *i2c)
+static int mtk_i2c_stop(struct mtk_i2c *i2c, bool atomic)
{
iowrite32(SM0CTL1_STOP | SM0CTL1_TRI, i2c->base + REG_SM0CTL1_REG);
- return mtk_i2c_wait_idle(i2c);
+ return mtk_i2c_wait_idle(i2c, atomic);
}
-static int mtk_i2c_cmd(struct mtk_i2c *i2c, u32 cmd, int page_len)
+static int mtk_i2c_cmd(struct mtk_i2c *i2c, u32 cmd, int page_len,
+ bool atomic)
{
iowrite32(cmd | SM0CTL1_TRI | SM0CTL1_PGLEN(page_len),
i2c->base + REG_SM0CTL1_REG);
- return mtk_i2c_wait_idle(i2c);
+ return mtk_i2c_wait_idle(i2c, atomic);
}
-static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
- int num)
+static int mtk_i2c_xfer_common(struct i2c_adapter *adap, struct i2c_msg *msgs,
+ int num, bool atomic)
{
struct mtk_i2c *i2c;
struct i2c_msg *pmsg;
@@ -152,12 +158,12 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
pmsg = &msgs[i];
/* wait hardware idle */
- ret = mtk_i2c_wait_idle(i2c);
+ ret = mtk_i2c_wait_idle(i2c, atomic);
if (ret)
goto err_timeout;
/* start sequence */
- ret = mtk_i2c_start(i2c);
+ ret = mtk_i2c_start(i2c, atomic);
if (ret)
goto err_timeout;
@@ -169,14 +175,16 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
if (pmsg->flags & I2C_M_RD)
addr |= 1;
iowrite32(addr, i2c->base + REG_SM0D0_REG);
- ret = mtk_i2c_cmd(i2c, SM0CTL1_WRITE, 2);
+ ret = mtk_i2c_cmd(i2c, SM0CTL1_WRITE, 2,
+ atomic);
if (ret)
goto err_timeout;
} else {
/* 7 bits address */
addr = i2c_8bit_addr_from_msg(pmsg);
iowrite32(addr, i2c->base + REG_SM0D0_REG);
- ret = mtk_i2c_cmd(i2c, SM0CTL1_WRITE, 1);
+ ret = mtk_i2c_cmd(i2c, SM0CTL1_WRITE, 1,
+ atomic);
if (ret)
goto err_timeout;
}
@@ -202,7 +210,7 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
cmd = SM0CTL1_WRITE;
}
- ret = mtk_i2c_cmd(i2c, cmd, page_len);
+ ret = mtk_i2c_cmd(i2c, cmd, page_len, atomic);
if (ret)
goto err_timeout;
@@ -222,7 +230,7 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
}
}
- ret = mtk_i2c_stop(i2c);
+ ret = mtk_i2c_stop(i2c, atomic);
if (ret)
goto err_timeout;
@@ -230,7 +238,7 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
return i;
err_ack:
- ret = mtk_i2c_stop(i2c);
+ ret = mtk_i2c_stop(i2c, atomic);
if (ret)
goto err_timeout;
return -ENXIO;
@@ -241,6 +249,18 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
return ret;
}
+static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
+ int num)
+{
+ return mtk_i2c_xfer_common(adap, msgs, num, false);
+}
+
+static int mtk_i2c_xfer_atomic(struct i2c_adapter *adap,
+ struct i2c_msg *msgs, int num)
+{
+ return mtk_i2c_xfer_common(adap, msgs, num, true);
+}
+
static u32 mtk_i2c_func(struct i2c_adapter *a)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
@@ -248,6 +268,7 @@ static u32 mtk_i2c_func(struct i2c_adapter *a)
static const struct i2c_algorithm mtk_i2c_algo = {
.xfer = mtk_i2c_xfer,
+ .xfer_atomic = mtk_i2c_xfer_atomic,
.functionality = mtk_i2c_func,
};
--
2.50.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 3/5] dt-bindings: i2c: mt7621: Document an7581 compatible
2025-07-21 18:05 [PATCH v2 0/5] i2c: mt7621: improve support for Airoha Christian Marangi
2025-07-21 18:05 ` [PATCH v2 1/5] i2c: mt7621: rework cmd/wait OPs to support atomic afer variant Christian Marangi
2025-07-21 18:05 ` [PATCH v2 2/5] i2c: mt7621: clear pending interrupt on i2c reset Christian Marangi
@ 2025-07-21 18:05 ` Christian Marangi
2025-07-21 20:17 ` Rob Herring (Arm)
2025-07-21 18:05 ` [PATCH v2 4/5] i2c: mt7621: limit SCL_STRETCH only to Mediatek SoC Christian Marangi
2025-07-21 18:05 ` [PATCH v2 5/5] i2c: mt7621: make device reset optional Christian Marangi
4 siblings, 1 reply; 7+ messages in thread
From: Christian Marangi @ 2025-07-21 18:05 UTC (permalink / raw)
To: Stefan Roese, Andi Shyti, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Matthias Brugger, AngeloGioacchino Del Regno,
linux-i2c, devicetree, linux-kernel, linux-arm-kernel,
linux-mediatek
Cc: Christian Marangi
Airoha SoC implement the same Mediatek logic for I2C bus with the only
difference of not having a dedicated reset line to reset it.
Add a dedicated compatible for the Airoha AN7581 SoC and reject the
unsupported property.
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
.../bindings/i2c/mediatek,mt7621-i2c.yaml | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/Documentation/devicetree/bindings/i2c/mediatek,mt7621-i2c.yaml b/Documentation/devicetree/bindings/i2c/mediatek,mt7621-i2c.yaml
index 118ec00fc190..8223fbc74f14 100644
--- a/Documentation/devicetree/bindings/i2c/mediatek,mt7621-i2c.yaml
+++ b/Documentation/devicetree/bindings/i2c/mediatek,mt7621-i2c.yaml
@@ -14,7 +14,9 @@ allOf:
properties:
compatible:
- const: mediatek,mt7621-i2c
+ enum:
+ - airoha,an7581-i2c
+ - mediatek,mt7621-i2c
reg:
maxItems: 1
@@ -38,6 +40,16 @@ required:
- "#address-cells"
- "#size-cells"
+if:
+ properties:
+ compatible:
+ contains:
+ const: airoha,an7581-i2c
+then:
+ properties:
+ resets: false
+ reset-names: false
+
unevaluatedProperties: false
examples:
--
2.50.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 4/5] i2c: mt7621: limit SCL_STRETCH only to Mediatek SoC
2025-07-21 18:05 [PATCH v2 0/5] i2c: mt7621: improve support for Airoha Christian Marangi
` (2 preceding siblings ...)
2025-07-21 18:05 ` [PATCH v2 3/5] dt-bindings: i2c: mt7621: Document an7581 compatible Christian Marangi
@ 2025-07-21 18:05 ` Christian Marangi
2025-07-21 18:05 ` [PATCH v2 5/5] i2c: mt7621: make device reset optional Christian Marangi
4 siblings, 0 replies; 7+ messages in thread
From: Christian Marangi @ 2025-07-21 18:05 UTC (permalink / raw)
To: Stefan Roese, Andi Shyti, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Matthias Brugger, AngeloGioacchino Del Regno,
linux-i2c, devicetree, linux-kernel, linux-arm-kernel,
linux-mediatek
Cc: Christian Marangi
The same I2C driver is also used for Airoha SoC with the only difference
that the i2c_reset should not enable SCL_STRETCH for Airoha SoC.
Introduce a new compatible for Airoha and limit the SCL_STRETCH only to
mediatek SoC.
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
drivers/i2c/busses/i2c-mt7621.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/i2c/busses/i2c-mt7621.c b/drivers/i2c/busses/i2c-mt7621.c
index 58c3da92cea3..b4dc1fb269a6 100644
--- a/drivers/i2c/busses/i2c-mt7621.c
+++ b/drivers/i2c/busses/i2c-mt7621.c
@@ -88,6 +88,7 @@ static int mtk_i2c_wait_idle(struct mtk_i2c *i2c, bool atomic)
static void mtk_i2c_reset(struct mtk_i2c *i2c)
{
+ u32 reg;
int ret;
ret = device_reset(i2c->adap.dev.parent);
@@ -98,8 +99,12 @@ static void mtk_i2c_reset(struct mtk_i2c *i2c)
* Don't set SM0CTL0_ODRAIN as its bit meaning is inverted. To
* configure open-drain mode, this bit needs to be cleared.
*/
- iowrite32(((i2c->clk_div << 16) & SM0CTL0_CLK_DIV_MASK) | SM0CTL0_EN |
- SM0CTL0_SCL_STRETCH, i2c->base + REG_SM0CTL0_REG);
+ reg = ((i2c->clk_div << 16) & SM0CTL0_CLK_DIV_MASK) | SM0CTL0_EN;
+ /* Set SCL_STRETCH only for Mediatek SoC */
+ if (device_is_compatible(i2c->dev, "mediatek,mt7621-i2c"))
+ reg |= SM0CTL0_SCL_STRETCH;
+
+ iowrite32(reg, i2c->base + REG_SM0CTL0_REG);
iowrite32(0, i2c->base + REG_SM0CFG2_REG);
/* Clear any pending interrupt */
iowrite32(1, i2c->base + REG_PINTEN_REG);
@@ -276,6 +281,7 @@ static const struct i2c_algorithm mtk_i2c_algo = {
static const struct of_device_id i2c_mtk_dt_ids[] = {
{ .compatible = "mediatek,mt7621-i2c" },
+ { .compatible = "airoha,an7581-i2c" },
{ /* sentinel */ }
};
--
2.50.0
^ permalink raw reply related [flat|nested] 7+ messages in thread