Skip to contentSkip to conversation

i2c: riic: Add support for I2C bus recovery, along with driver cleanup and improvements

PATCH Applied Prabhakar 3 participants 15 messages Jan 13, 2025 – Jan 14, 2025

Subsystems (2)
lore
Share & tools
lore

Prabhakar

author
#0 lore raw

v6 cover i2c: riic: Add support for I2C bus recovery, along with driver cleanup and improvements

From: Lad Prabhakar <[email protected]>

Hi All,

This patch series introduces support for I2C bus recovery in the RIIC driver, which is utilized in RZ series SoCs. The addition of bus recovery functionality enhances the reliability of the I2C interface by allowing it to recover from error conditions that might leave the bus in an unusable state.

Alongside the bus recovery implementation, the series includes several cleanup and improvement patches that simplify and modernize the driver code. These include replacing dev_err calls with dev_err_probe, consistent usage of the BIT and GENMASK macros, leveraging devres helpers for reset management, and improving code readability by marking static data as const.

v5->v6
- Swicthed to use linux/bits.h in patch 4/10
- Dropped linux/bits.h in patch 5/10
- Swicthed to use linux/time.h in patch 8/10
- Included RB/TB tags
- Included I2C bus recovery patch

v4->v5
- Only patch 1/10 was sent out to ML

v3->v4
-> Created new patch 1/9
-> Dropped RB/TB tags from patch 8/9
-> Dropped unsigned long cast and updated the format specifier while
printing bus frequency
-> Included required headers
-> Propogated the error

v1->v2
- Fixed review comments and collected RB tags from Geert

v1:
https://lore.kernel.org/all/[email protected]/

Cheers,
Prabhakar

Lad Prabhakar (10):
i2c: riic: Introduce a separate variable for IRQ
i2c: riic: Use dev_err_probe in probe and riic_init_hw functions
i2c: riic: Use local dev pointer in dev_err_probe()
i2c: riic: Use BIT macro consistently
i2c: riic: Use GENMASK() macro for bitmask definitions
i2c: riic: Make use of devres helper to request deasserted reset line
i2c: riic: Mark riic_irqs array as const
i2c: riic: Use predefined macro and simplify clock tick calculation
i2c: riic: Add riic_bus_barrier() to check bus availability
i2c: riic: Implement bus recovery

 drivers/i2c/busses/i2c-riic.c | 234 +++++++++++++++++++++++-----------
 1 file changed, 162 insertions(+), 72 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v6 1/10 i2c: riic: Introduce a separate variable for IRQ

From: Lad Prabhakar <[email protected]>

Refactor the IRQ handling in riic_i2c_probe by introducing a local variable irq to store IRQ numbers instead of assigning them to ret. This change improves code readability and clarity.

---
v5->v6
- Included RB tags

---
 drivers/i2c/busses/i2c-riic.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-riic.c +6 -4
index 9264adc97ca9..9809ac095710 100644
@@ -464,11 +464,13 @@ static int riic_i2c_probe(struct platform_device *pdev)
 		return ret;
 
 	for (i = 0; i < ARRAY_SIZE(riic_irqs); i++) {
-		ret = platform_get_irq(pdev, riic_irqs[i].res_num);
-		if (ret < 0)
-			return ret;
+		int irq;
+
+		irq = platform_get_irq(pdev, riic_irqs[i].res_num);
+		if (irq < 0)
+			return irq;
 
-		ret = devm_request_irq(dev, ret, riic_irqs[i].isr,
+		ret = devm_request_irq(dev, irq, riic_irqs[i].isr,
 				       0, riic_irqs[i].name, riic);
 		if (ret) {
 			dev_err(dev, "failed to request irq %s\n", riic_irqs[i].name);
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v6 2/10 i2c: riic: Use dev_err_probe in probe and riic_init_hw functions

In reply to Prabhakar

From: Lad Prabhakar <[email protected]>

Refactor error handling in the riic_i2c_probe() and riic_init_hw() functions by replacing multiple dev_err() calls with dev_err_probe().

Additionally, update the riic_init_hw() function to use a local dev pointer instead of riic->adapter.dev for dev_err_probe(), as the I2C adapter is not initialized at this stage. Drop the cast to (unsigned long) in the riic_init_hw() function when printing the bus frequency, and update the error message to display the frequency in Hz, improving clarity.

---
v4->v6
- No changes

v3->v4
- Dropped unsigned long cast and updated the format specifier while printing bus frequency
- Since the changes were small, I've kept the RB/TB tags.

v2->v3
- Squashed dev_err_probe() change from patch #2 into patch #1
- Updated commit message
- Collected RB and tested tags

v1->v2
- Collected RB tag from Geert

---
 drivers/i2c/busses/i2c-riic.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
drivers/i2c/busses/i2c-riic.c +9 -13
index 9809ac095710..4e2add343c9e 100644
@@ -356,11 +356,9 @@ static int riic_init_hw(struct riic_dev *riic)
 		rate /= 2;
 	}
 
-	if (brl > (0x1F + 3)) {
-		dev_err(&riic->adapter.dev, "invalid speed (%lu). Too slow.\n",
-			(unsigned long)t->bus_freq_hz);
-		return -EINVAL;
-	}
+	if (brl > (0x1F + 3))
+		return dev_err_probe(dev, -EINVAL, "invalid speed (%uHz). Too slow.\n",
+				     t->bus_freq_hz);
 
 	brh = total_ticks - brl;
 
@@ -445,10 +443,9 @@ static int riic_i2c_probe(struct platform_device *pdev)
 		return PTR_ERR(riic->base);
 
 	riic->clk = devm_clk_get(dev, NULL);
-	if (IS_ERR(riic->clk)) {
-		dev_err(dev, "missing controller clock");
-		return PTR_ERR(riic->clk);
-	}
+	if (IS_ERR(riic->clk))
+		return dev_err_probe(dev, PTR_ERR(riic->clk),
+				     "missing controller clock");
 
 	riic->rstc = devm_reset_control_get_optional_exclusive(dev, NULL);
 	if (IS_ERR(riic->rstc))
@@ -472,10 +469,9 @@ static int riic_i2c_probe(struct platform_device *pdev)
 
 		ret = devm_request_irq(dev, irq, riic_irqs[i].isr,
 				       0, riic_irqs[i].name, riic);
-		if (ret) {
-			dev_err(dev, "failed to request irq %s\n", riic_irqs[i].name);
-			return ret;
-		}
+		if (ret)
+			return dev_err_probe(dev, ret, "failed to request irq %s\n",
+					     riic_irqs[i].name);
 	}
 
 	riic->info = of_device_get_match_data(dev);
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v6 3/10 i2c: riic: Use local `dev` pointer in `dev_err_probe()`

In reply to Prabhakar

From: Lad Prabhakar <[email protected]>

Update the riic_init_hw() function to use the local dev pointer in calls to dev_err_probe(). Previously, riic_init_hw() used riic->adapter.dev in error reporting. Since this function is invoked during the probe phase, the I2C adapter is not yet initialized, leading to (null) ... being printed in error messages. This patch fixes the issue by consistently using the local dev pointer, which points to riic->adapter.dev.parent.

---
v4->v6
- No change

v3->v4
- No change

v2->v3
- Moved replacing dev_err -> dev_err_probe into patch#1
- Dropped fixes tags
- Updated commit message
- Collected RB and tested tags

v1->v2
- Collected RB tag from Geert

---
 drivers/i2c/busses/i2c-riic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-riic.c +1 -1
index 4e2add343c9e..c555b6220e66 100644
@@ -320,7 +320,7 @@ static int riic_init_hw(struct riic_dev *riic)
 				      : I2C_MAX_FAST_MODE_FREQ;
 
 	if (t->bus_freq_hz > max_freq)
-		return dev_err_probe(&riic->adapter.dev, -EINVAL,
+		return dev_err_probe(dev, -EINVAL,
 				     "unsupported bus speed %uHz (%u max)\n",
 				     t->bus_freq_hz, max_freq);
 
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH v6 4/10 i2c: riic: Use BIT macro consistently

In reply to Prabhakar

From: Lad Prabhakar <[email protected]>

Easier to read and ensures proper types.

---
v4->v6
- Used linux/bits.h
- Since the changes were small, I've kept the RB/TB tags.

v3->v4
- Included bits.h
- Since the changes were small, I've kept the RB/TB tags.

v2->v3
- Collected RB and tested tags

v1->v2
- Collected RB tag from Geert

---
 drivers/i2c/busses/i2c-riic.c | 37 ++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 18 deletions(-)
drivers/i2c/busses/i2c-riic.c +19 -18
index c555b6220e66..370cb83bf5ac 100644
@@ -34,6 +34,7 @@
  * Also check the comments in the interrupt routines for some gory details.
  */
 
+#include <linux/bits.h>
 #include <linux/clk.h>
 #include <linux/completion.h>
 #include <linux/err.h>
@@ -46,32 +47,32 @@
 #include <linux/pm_runtime.h>
 #include <linux/reset.h>
 
-#define ICCR1_ICE	0x80
-#define ICCR1_IICRST	0x40
-#define ICCR1_SOWP	0x10
+#define ICCR1_ICE	BIT(7)
+#define ICCR1_IICRST	BIT(6)
+#define ICCR1_SOWP	BIT(4)
 
-#define ICCR2_BBSY	0x80
-#define ICCR2_SP	0x08
-#define ICCR2_RS	0x04
-#define ICCR2_ST	0x02
+#define ICCR2_BBSY	BIT(7)
+#define ICCR2_SP	BIT(3)
+#define ICCR2_RS	BIT(2)
+#define ICCR2_ST	BIT(1)
 
 #define ICMR1_CKS_MASK	0x70
-#define ICMR1_BCWP	0x08
+#define ICMR1_BCWP	BIT(3)
 #define ICMR1_CKS(_x)	((((_x) << 4) & ICMR1_CKS_MASK) | ICMR1_BCWP)
 
-#define ICMR3_RDRFS	0x20
-#define ICMR3_ACKWP	0x10
-#define ICMR3_ACKBT	0x08
+#define ICMR3_RDRFS	BIT(5)
+#define ICMR3_ACKWP	BIT(4)
+#define ICMR3_ACKBT	BIT(3)
 
-#define ICFER_FMPE	0x80
+#define ICFER_FMPE	BIT(7)
 
-#define ICIER_TIE	0x80
-#define ICIER_TEIE	0x40
-#define ICIER_RIE	0x20
-#define ICIER_NAKIE	0x10
-#define ICIER_SPIE	0x08
+#define ICIER_TIE	BIT(7)
+#define ICIER_TEIE	BIT(6)
+#define ICIER_RIE	BIT(5)
+#define ICIER_NAKIE	BIT(4)
+#define ICIER_SPIE	BIT(3)
 
-#define ICSR2_NACKF	0x10
+#define ICSR2_NACKF	BIT(4)
 
 #define ICBR_RESERVED	0xe0 /* Should be 1 on writes */
 
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH v6 5/10 i2c: riic: Use GENMASK() macro for bitmask definitions

In reply to Prabhakar

From: Lad Prabhakar <[email protected]>

Replace raw bitmask values with the GENMASK() macro in the i2c-riic driver to improve readability and maintain consistency.

---
v4->v6
- Dropped bits.h, as patch 04/10 adds it.
- Since the changes were small, I've kept the RB/TB tags.

v3->v4
- Included bits.h
- Since the changes were small, I've kept the RB/TB tags.

v2->v3
- Collected RB and tested tags

v1->v2
- Collected RB tag from Geert

---
 drivers/i2c/busses/i2c-riic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-riic.c +2 -2
index 370cb83bf5ac..cf0b45f9e3d5 100644
@@ -56,7 +56,7 @@
 #define ICCR2_RS	BIT(2)
 #define ICCR2_ST	BIT(1)
 
-#define ICMR1_CKS_MASK	0x70
+#define ICMR1_CKS_MASK	GENMASK(6, 4)
 #define ICMR1_BCWP	BIT(3)
 #define ICMR1_CKS(_x)	((((_x) << 4) & ICMR1_CKS_MASK) | ICMR1_BCWP)
 
@@ -74,7 +74,7 @@
 
 #define ICSR2_NACKF	BIT(4)
 
-#define ICBR_RESERVED	0xe0 /* Should be 1 on writes */
+#define ICBR_RESERVED	GENMASK(7, 5) /* Should be 1 on writes */
 
 #define RIIC_INIT_MSG	-1
 
Signature · 1 line
2.43.0
author
#6 lore raw

PATCH v6 6/10 i2c: riic: Make use of devres helper to request deasserted reset line

In reply to Prabhakar

From: Lad Prabhakar <[email protected]>

Simplify the riic_i2c_probe() function by using the devm_reset_control_get_optional_exclusive_deasserted() API to request a deasserted reset line. This eliminates the need to manually deassert the reset control and the additional cleanup.

---
v4->v6
- No change

v3->v4
- No change

v2->v3
- Collected RB and tested tags

v1->v2
- Updated error message

---
 drivers/i2c/busses/i2c-riic.c | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)
drivers/i2c/busses/i2c-riic.c +2 -15
index cf0b45f9e3d5..577be1fb941e 100644
@@ -423,11 +423,6 @@ static struct riic_irq_desc riic_irqs[] = {
 	{ .res_num = 5, .isr = riic_tend_isr, .name = "riic-nack" },
 };
 
-static void riic_reset_control_assert(void *data)
-{
-	reset_control_assert(data);
-}
-
 static int riic_i2c_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -448,18 +443,10 @@ static int riic_i2c_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(riic->clk),
 				     "missing controller clock");
 
-	riic->rstc = devm_reset_control_get_optional_exclusive(dev, NULL);
+	riic->rstc = devm_reset_control_get_optional_exclusive_deasserted(dev, NULL);
 	if (IS_ERR(riic->rstc))
 		return dev_err_probe(dev, PTR_ERR(riic->rstc),
-				     "Error: missing reset ctrl\n");
-
-	ret = reset_control_deassert(riic->rstc);
-	if (ret)
-		return ret;
-
-	ret = devm_add_action_or_reset(dev, riic_reset_control_assert, riic->rstc);
-	if (ret)
-		return ret;
+				     "failed to acquire deasserted reset\n");
 
 	for (i = 0; i < ARRAY_SIZE(riic_irqs); i++) {
 		int irq;
Signature · 1 line
2.43.0
author
#7 lore raw

PATCH v6 7/10 i2c: riic: Mark riic_irqs array as const

In reply to Prabhakar

From: Lad Prabhakar <[email protected]>

The riic_irqs array describes the supported IRQs by the RIIC driver and does not change at runtime.

---
v4->v6
- Included RB tag from Wolfram

v3->v4
- No change

v2->v3
- Collected RB and tested tags

v1->v2
- Collected RB tag from Geert

---
 drivers/i2c/busses/i2c-riic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-riic.c +1 -1
index 577be1fb941e..d1768b38b12d 100644
@@ -415,7 +415,7 @@ static int riic_init_hw(struct riic_dev *riic)
 	return 0;
 }
 
-static struct riic_irq_desc riic_irqs[] = {
+static const struct riic_irq_desc riic_irqs[] = {
 	{ .res_num = 0, .isr = riic_tend_isr, .name = "riic-tend" },
 	{ .res_num = 1, .isr = riic_rdrf_isr, .name = "riic-rdrf" },
 	{ .res_num = 2, .isr = riic_tdre_isr, .name = "riic-tdre" },
Signature · 1 line
2.43.0
author
#8 lore raw

PATCH v6 8/10 i2c: riic: Use predefined macro and simplify clock tick calculation

In reply to Prabhakar

From: Lad Prabhakar <[email protected]>

Replace the hardcoded 1000000000 with the predefined NSEC_PER_SEC macro for clarity. Simplify the code by introducing a ns_per_tick variable to store NSEC_PER_SEC / rate, reducing redundancy and improving readability.

---
v4->v6
- Included linux/time.h
- Included RB/TB tested from Geert and Wolfram

v3->v4
- Switched to use NSEC_PER_SEC instead of NANO
- Updated the commit message
- Dropped the RB/TB tags

v2->v3
- Collected RB and tested tags

v1->v2
- Collected RB tag from Geert

---
 drivers/i2c/busses/i2c-riic.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-riic.c +6 -4
index d1768b38b12d..aa3e4f430b11 100644
@@ -46,6 +46,7 @@
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/reset.h>
+#include <linux/time.h>
 
 #define ICCR1_ICE	BIT(7)
 #define ICCR1_IICRST	BIT(6)
@@ -313,6 +314,7 @@ static int riic_init_hw(struct riic_dev *riic)
 {
 	int ret;
 	unsigned long rate;
+	unsigned long ns_per_tick;
 	int total_ticks, cks, brl, brh;
 	struct i2c_timings *t = &riic->i2c_t;
 	struct device *dev = riic->adapter.dev.parent;
@@ -376,8 +378,9 @@ static int riic_init_hw(struct riic_dev *riic)
 	 * Remove clock ticks for rise and fall times. Convert ns to clock
 	 * ticks.
 	 */
-	brl -= t->scl_fall_ns / (1000000000 / rate);
-	brh -= t->scl_rise_ns / (1000000000 / rate);
+	ns_per_tick = NSEC_PER_SEC / rate;
+	brl -= t->scl_fall_ns / ns_per_tick;
+	brh -= t->scl_rise_ns / ns_per_tick;
 
 	/* Adjust for min register values for when SCLE=1 and NFE=1 */
 	if (brl < 1)
@@ -387,8 +390,7 @@ static int riic_init_hw(struct riic_dev *riic)
 
 	pr_debug("i2c-riic: freq=%lu, duty=%d, fall=%lu, rise=%lu, cks=%d, brl=%d, brh=%d\n",
 		 rate / total_ticks, ((brl + 3) * 100) / (brl + brh + 6),
-		 t->scl_fall_ns / (1000000000 / rate),
-		 t->scl_rise_ns / (1000000000 / rate), cks, brl, brh);
+		 t->scl_fall_ns / ns_per_tick, t->scl_rise_ns / ns_per_tick, cks, brl, brh);
 
 	ret = pm_runtime_resume_and_get(dev);
 	if (ret)
Signature · 1 line
2.43.0
Reviewed-by on v6 8/10
#9 lore raw

Re: [PATCH v6 08/10] i2c: riic: Use predefined macro and simplify clock tick calculation

Quoted 10 lines · Prabhakar

From: Lad Prabhakar <[email protected]>

Replace the hardcoded 1000000000 with the predefined NSEC_PER_SEC macro for clarity. Simplify the code by introducing a ns_per_tick variable to store NSEC_PER_SEC / rate, reducing redundancy and improving readability.

author
#10 lore raw

PATCH v6 9/10 i2c: riic: Add `riic_bus_barrier()` to check bus availability

In reply to Prabhakar

From: Lad Prabhakar <[email protected]>

Introduce a new riic_bus_barrier() function to verify bus availability before initiating an I2C transfer. This function enhances the bus arbitration check by ensuring that the SDA and SCL lines are not held low, in addition to checking the BBSY flag using readb_poll_timeout().

Previously, only the BBSY flag was checked to determine bus availability. However, it is possible for the SDA line to remain low even when BBSY = 0. This new implementation performs an additional check on the SDA and SCL lines to avoid potential bus contention issues.

---
v4->v6
- No change

v3->v4
- No change

v3->v4
- Propogated error code when readb_poll_timeout() failed
- I've kept the RB/TB tags as the changes were minimal.

v2->v3
- Collected RB and tested tags

v1->v2
- Used single register read to check SDA/SCL lines

---
 drivers/i2c/busses/i2c-riic.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-riic.c +26 -4
index aa3e4f430b11..d7dddd6c296a 100644
@@ -41,6 +41,7 @@
 #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>
@@ -51,6 +52,8 @@
 #define ICCR1_ICE	BIT(7)
 #define ICCR1_IICRST	BIT(6)
 #define ICCR1_SOWP	BIT(4)
+#define ICCR1_SCLI	BIT(1)
+#define ICCR1_SDAI	BIT(0)
 
 #define ICCR2_BBSY	BIT(7)
 #define ICCR2_SP	BIT(3)
@@ -136,6 +139,27 @@ static inline void riic_clear_set_bit(struct riic_dev *riic, u8 clear, u8 set, u
 	riic_writeb(riic, (riic_readb(riic, reg) & ~clear) | set, reg);
 }
 
+static int riic_bus_barrier(struct riic_dev *riic)
+{
+	int ret;
+	u8 val;
+
+	/*
+	 * The SDA line can still be low even when BBSY = 0. Therefore, after checking
+	 * the BBSY flag, also verify that the SDA and SCL lines are not being held low.
+	 */
+	ret = readb_poll_timeout(riic->base + riic->info->regs[RIIC_ICCR2], val,
+				 !(val & ICCR2_BBSY), 10, riic->adapter.timeout);
+	if (ret)
+		return ret;
+
+	if ((riic_readb(riic, RIIC_ICCR1) & (ICCR1_SDAI | ICCR1_SCLI)) !=
+	     (ICCR1_SDAI | ICCR1_SCLI))
+		return -EBUSY;
+
+	return 0;
+}
+
 static int riic_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
 {
 	struct riic_dev *riic = i2c_get_adapdata(adap);
@@ -148,13 +172,11 @@ static int riic_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
 	if (ret)
 		return ret;
 
-	if (riic_readb(riic, RIIC_ICCR2) & ICCR2_BBSY) {
-		riic->err = -EBUSY;
+	riic->err = riic_bus_barrier(riic);
+	if (riic->err)
 		goto out;
-	}
 
 	reinit_completion(&riic->msg_done);
-	riic->err = 0;
 
 	riic_writeb(riic, 0, RIIC_ICSR2);
 
Signature · 1 line
2.43.0
author
#11 lore raw

PATCH v6 10/10 i2c: riic: Implement bus recovery

In reply to Prabhakar

From: Lad Prabhakar <[email protected]>

Implement bus recovery by reinitializing the hardware to reset the bus state and generating 9 clock cycles (and a stop condition) to release the SDA line.

---
Hi Wolfram,

I have inlcuded bus recovery patch as part of v6 as I am seeing issues while using generic I2C algorithm for bus recovery as mentioned in the below thread.

[0] https://lore.kernel.org/all/CA+V-a8s4-g9vxyfYMgnKMK=Oej9kDBwWsWehWLYTkxw-06w-2g@mail.gmail.com/

Cheers,
Prabhakar

v2->v6
- Included RB and TB from Claudiu.

v1->v2
- Used single register read to check SDA/SCL lines

---
 drivers/i2c/busses/i2c-riic.c | 100 ++++++++++++++++++++++++++++++----
 1 file changed, 90 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-riic.c +90 -10
index d7dddd6c296a..888825423d94 100644
@@ -51,6 +51,7 @@
 
 #define ICCR1_ICE	BIT(7)
 #define ICCR1_IICRST	BIT(6)
+#define ICCR1_CLO	BIT(5)
 #define ICCR1_SOWP	BIT(4)
 #define ICCR1_SCLI	BIT(1)
 #define ICCR1_SDAI	BIT(0)
@@ -69,6 +70,7 @@
 #define ICMR3_ACKBT	BIT(3)
 
 #define ICFER_FMPE	BIT(7)
+#define ICFER_MALE	BIT(1)
 
 #define ICIER_TIE	BIT(7)
 #define ICIER_TEIE	BIT(6)
@@ -82,6 +84,8 @@
 
 #define RIIC_INIT_MSG	-1
 
+#define RIIC_RECOVERY_CLK_CNT	9
+
 enum riic_reg_list {
 	RIIC_ICCR1 = 0,
 	RIIC_ICCR2,
@@ -151,13 +155,16 @@ static int riic_bus_barrier(struct riic_dev *riic)
 	ret = readb_poll_timeout(riic->base + riic->info->regs[RIIC_ICCR2], val,
 				 !(val & ICCR2_BBSY), 10, riic->adapter.timeout);
 	if (ret)
-		return ret;
+		goto i2c_recover;
 
 	if ((riic_readb(riic, RIIC_ICCR1) & (ICCR1_SDAI | ICCR1_SCLI)) !=
 	     (ICCR1_SDAI | ICCR1_SCLI))
-		return -EBUSY;
+		goto i2c_recover;
 
 	return 0;
+
+i2c_recover:
+	return i2c_recover_bus(&riic->adapter);
 }
 
 static int riic_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
@@ -332,7 +339,7 @@ static const struct i2c_algorithm riic_algo = {
 	.functionality = riic_func,
 };
 
-static int riic_init_hw(struct riic_dev *riic)
+static int riic_init_hw(struct riic_dev *riic, bool recover)
 {
 	int ret;
 	unsigned long rate;
@@ -414,9 +421,11 @@ static int riic_init_hw(struct riic_dev *riic)
 		 rate / total_ticks, ((brl + 3) * 100) / (brl + brh + 6),
 		 t->scl_fall_ns / ns_per_tick, t->scl_rise_ns / ns_per_tick, cks, brl, brh);
 
-	ret = pm_runtime_resume_and_get(dev);
-	if (ret)
-		return ret;
+	if (!recover) {
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret)
+			return ret;
+	}
 
 	/* Changing the order of accessing IICRST and ICE may break things! */
 	riic_writeb(riic, ICCR1_IICRST | ICCR1_SOWP, RIIC_ICCR1);
@@ -434,8 +443,74 @@ static int riic_init_hw(struct riic_dev *riic)
 
 	riic_clear_set_bit(riic, ICCR1_IICRST, 0, RIIC_ICCR1);
 
-	pm_runtime_mark_last_busy(dev);
-	pm_runtime_put_autosuspend(dev);
+	if (!recover) {
+		pm_runtime_mark_last_busy(dev);
+		pm_runtime_put_autosuspend(dev);
+	}
+	return 0;
+}
+
+static int riic_recover_bus(struct i2c_adapter *adap)
+{
+	struct riic_dev *riic = i2c_get_adapdata(adap);
+	struct device *dev = riic->adapter.dev.parent;
+	int ret;
+	u8 val;
+
+	ret = riic_init_hw(riic, true);
+	if (ret)
+		return ret;
+
+	/* output extra SCL clock cycles with master arbitration-lost detection disabled */
+	riic_clear_set_bit(riic, ICFER_MALE, 0, RIIC_ICFER);
+
+	for (unsigned int i = 0; i < RIIC_RECOVERY_CLK_CNT; i++) {
+		riic_clear_set_bit(riic, 0, ICCR1_CLO, RIIC_ICCR1);
+		ret = readb_poll_timeout(riic->base + riic->info->regs[RIIC_ICCR1], val,
+					 !(val & ICCR1_CLO), 0, 100);
+		if (ret) {
+			dev_err(dev, "SCL clock cycle timeout\n");
+			return ret;
+		}
+	}
+
+	/*
+	 * The last clock cycle may have driven the SDA line high, so add a
+	 * short delay to allow the line to stabilize before checking the status.
+	 */
+	udelay(5);
+
+	/*
+	 * If an incomplete byte write occurs, the SDA line may remain low
+	 * even after 9 clock pulses, indicating the bus is not released.
+	 * To resolve this, send an additional clock pulse to simulate a STOP
+	 * condition and ensure proper bus release.
+	 */
+	if ((riic_readb(riic, RIIC_ICCR1) & (ICCR1_SDAI | ICCR1_SCLI)) !=
+	    (ICCR1_SDAI | ICCR1_SCLI)) {
+		riic_clear_set_bit(riic, 0, ICCR1_CLO, RIIC_ICCR1);
+		ret = readb_poll_timeout(riic->base + riic->info->regs[RIIC_ICCR1], val,
+					 !(val & ICCR1_CLO), 0, 100);
+		if (ret) {
+			dev_err(dev, "SCL clock cycle timeout occurred while issuing the STOP condition\n");
+			return ret;
+		}
+		/* delay to make sure SDA line goes back HIGH again */
+		udelay(5);
+	}
+
+	/* clear any flags set */
+	riic_writeb(riic, 0, RIIC_ICSR2);
+	/* read back register to confirm writes */
+	riic_readb(riic, RIIC_ICSR2);
+
+	/* restore back ICFER_MALE */
+	riic_clear_set_bit(riic, 0, ICFER_MALE, RIIC_ICFER);
+
+	if ((riic_readb(riic, RIIC_ICCR1) & (ICCR1_SDAI | ICCR1_SCLI)) !=
+	    (ICCR1_SDAI | ICCR1_SCLI))
+		return -EINVAL;
+
 	return 0;
 }
 
@@ -447,6 +522,10 @@ static const struct riic_irq_desc riic_irqs[] = {
 	{ .res_num = 5, .isr = riic_tend_isr, .name = "riic-nack" },
 };
 
+static struct i2c_bus_recovery_info riic_bri = {
+	.recover_bus = riic_recover_bus,
+};
+
 static int riic_i2c_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -493,6 +572,7 @@ static int riic_i2c_probe(struct platform_device *pdev)
 	strscpy(adap->name, "Renesas RIIC adapter", sizeof(adap->name));
 	adap->owner = THIS_MODULE;
 	adap->algo = &riic_algo;
+	adap->bus_recovery_info = &riic_bri;
 	adap->dev.parent = dev;
 	adap->dev.of_node = dev->of_node;
 
@@ -505,7 +585,7 @@ static int riic_i2c_probe(struct platform_device *pdev)
 	pm_runtime_use_autosuspend(dev);
 	pm_runtime_enable(dev);
 
-	ret = riic_init_hw(riic);
+	ret = riic_init_hw(riic, false);
 	if (ret)
 		goto out;
 
@@ -613,7 +693,7 @@ static int riic_i2c_resume(struct device *dev)
 	if (ret)
 		return ret;
 
-	ret = riic_init_hw(riic);
+	ret = riic_init_hw(riic, false);
 	if (ret) {
 		/*
 		 * In case this happens there is no way to recover from this
Signature · 1 line
2.43.0
Reviewed-by on the v6 series
#12 lore raw
In reply to Prabhakar
Quoted 11 lines · Prabhakar

From: Lad Prabhakar <[email protected]>

Hi All,

This patch series introduces support for I2C bus recovery in the RIIC driver, which is utilized in RZ series SoCs. The addition of bus recovery functionality enhances the reliability of the I2C interface by allowing it to recover from error conditions that might leave the bus in an unusable state.

Quoted 6 lines

Alongside the bus recovery implementation, the series includes several cleanup and improvement patches that simplify and modernize the driver code. These include replacing dev_err calls with dev_err_probe, consistent usage of the BIT and GENMASK macros, leveraging devres helpers for reset management, and improving code readability by marking static data as const.

Looks very good to me now, thank you.
FWIW,

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Applied
#13 lore raw
In reply to Prabhakar
Quoted 7 lines

This patch series introduces support for I2C bus recovery in the RIIC driver, which is utilized in RZ series SoCs. The addition of bus recovery functionality enhances the reliability of the I2C interface by allowing it to recover from error conditions that might leave the bus in an unusable state.

Quoted 6 lines

Alongside the bus recovery implementation, the series includes several cleanup and improvement patches that simplify and modernize the driver code. These include replacing dev_err calls with dev_err_probe, consistent usage of the BIT and GENMASK macros, leveraging devres helpers for reset management, and improving code readability by marking static data as const.

Applied patches 1-9 to for-next, thank you! Patch 10 needs a separate look.

#14 lore raw

Hi Wolfram,

Quoted 15 lines · Wolfram Sang
Quoted 8 lines

This patch series introduces support for I2C bus recovery in the RIIC driver, which is utilized in RZ series SoCs. The addition of bus recovery functionality enhances the reliability of the I2C interface by allowing it to recover from error conditions that might leave the bus in an unusable state.

Quoted 6 lines

Alongside the bus recovery implementation, the series includes several cleanup and improvement patches that simplify and modernize the driver code. These include replacing dev_err calls with dev_err_probe, consistent usage of the BIT and GENMASK macros, leveraging devres helpers for reset management, and improving code readability by marking static data as const.

Quoted 3 lines

Applied patches 1-9 to for-next, thank you! Patch 10 needs a separate look.

Thank you. I'll resend patch 10/10 after the merge window.

Cheers,
Prabhakar