Skip to contentSkip to conversation

i2c: at91: Fixes and updates

PATCH RESEND In review Codrin Ciubotariu 4 participants 8 messages Jun 14, 2022 – Mar 20, 2025

Subsystems (3)
lore
Share & tools
lore

Codrin Ciubotariu

author maintainer
#0 lore raw

cover i2c: at91: Fixes and updates

Resend due to the fact that the i2c list no longer accepts replys on the old patch-set. Comments not addressed yet.

The first patch is mostly a prerequisite for the second one. It only moves the i2c_recover_bus() out of the actual transfer function. This helps the second patch disable the controller before using GPIO recovery. The second patch will keep the controller enabled when a transfer occurs. Before using GPIO recovery, the controller must be disabled, to ignore potential glitches. However, the controller must be enabled for HW recovery (bus CLEAR command). The third and last patch adds advanced digital filtering support for SAMA5D4. The TWI IP found in SAMA5D4 supports advanced digital filtering, even if, at the moment of this patch, the SAMA5D4 datasheet does not mention it.

Codrin Ciubotariu (3):
i2c: at91: move i2c_recover_bus() outside of at91_do_twi_transfer()
i2c: at91: keep the controller disabled when it is not used
i2c: at91: add advanced digital filtering support for SAMA5D4

 drivers/i2c/busses/i2c-at91-core.c   |  1 +
 drivers/i2c/busses/i2c-at91-master.c | 53 ++++++++++++++++++++++------
 2 files changed, 44 insertions(+), 10 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH RESEND 1/3 i2c: at91: move i2c_recover_bus() outside of at91_do_twi_transfer()

This patch doesn't add a functional change, it just separates the recovery from the transfer itself.

---

Removed 'Fixes' tag

 drivers/i2c/busses/i2c-at91-master.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/i2c/busses/i2c-at91-master.c +11 -8
index c0c35785a0dc..34df1614bc37 100644
@@ -639,13 +639,6 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
 			       AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
 	}
 
-	/*
-	 * some faulty I2C slave devices might hold SDA down;
-	 * we can send a bus clear command, hoping that the pins will be
-	 * released
-	 */
-	i2c_recover_bus(&dev->adapter);
-
 	return ret;
 }
 
@@ -716,7 +709,17 @@ static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
 	ret = at91_do_twi_transfer(dev);
 	i2c_put_dma_safe_msg_buf(dma_buf, m_start, !ret);
 
-	ret = (ret < 0) ? ret : num;
+	if (ret < 0) {
+		/*
+		 * some faulty I2C slave devices might hold SDA down;
+		 * we can send a bus clear command, hoping that the pins will be
+		 * released
+		 */
+		i2c_recover_bus(&dev->adapter);
+	} else {
+		ret = num;
+	}
+
 out:
 	pm_runtime_mark_last_busy(dev->dev);
 	pm_runtime_put_autosuspend(dev->dev);
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH RESEND 2/3 i2c: at91: keep the controller disabled when it is not used

In reply to Codrin Ciubotariu

The controller (master mode) should be disabled when it is not used, to avoid potential glitches on the bus. The controller must be disabled only when the controller is not running.
This fixes an issue on Microchip's SAMA5D4 platform when pinctrl changes the I2C's pin states between GPIO and I2C, done when GPIO recovery is used.

Fixes 813e30e9ab91 ("i2c: New Atmel AT91 bus driver")
---
 drivers/i2c/busses/i2c-at91-master.c | 34 ++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-at91-master.c +32 -2
index 34df1614bc37..14c2a4856978 100644
@@ -38,7 +38,7 @@ void at91_init_twi_bus_master(struct at91_twi_dev *dev)
 	/* FIFO should be enabled immediately after the software reset */
 	if (dev->fifo_size)
 		at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
-	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
+	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSDIS);
 	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
 	at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
 
@@ -593,7 +593,6 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
 	if (time_left == 0) {
 		dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
 		dev_err(dev->dev, "controller timed out\n");
-		at91_init_twi_bus(dev);
 		ret = -ETIMEDOUT;
 		goto error;
 	}
@@ -642,6 +641,26 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
 	return ret;
 }
 
+static void at91_twi_disable(struct at91_twi_dev *dev)
+{
+	/* return if previous operation is completed */
+	if (!(at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_TXCOMP)) {
+		/* wait for previous command to complete before disabling the controller */
+		dev_dbg(dev->dev, "wait for command to complete...\n");
+		reinit_completion(&dev->cmd_complete);
+		dev->transfer_status = 0;
+		at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
+		wait_for_completion_timeout(&dev->cmd_complete, dev->adapter.timeout);
+		if (!(dev->transfer_status & AT91_TWI_TXCOMP)) {
+			dev_dbg(dev->dev, "IP still busy, resetting the controller...\n");
+			at91_init_twi_bus(dev);
+			return;
+		}
+	}
+
+	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSDIS);
+}
+
 static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
 {
 	struct at91_twi_dev *dev = i2c_get_adapdata(adap);
@@ -657,6 +676,8 @@ static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
 	if (ret < 0)
 		goto out;
 
+	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
+
 	if (num == 2) {
 		int internal_address = 0;
 		int i;
@@ -710,13 +731,22 @@ static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
 	i2c_put_dma_safe_msg_buf(dma_buf, m_start, !ret);
 
 	if (ret < 0) {
+		/* disable controller before using GPIO recovery */
+		if (!dev->pdata->has_clear_cmd)
+			at91_twi_disable(dev);
+
 		/*
 		 * some faulty I2C slave devices might hold SDA down;
 		 * we can send a bus clear command, hoping that the pins will be
 		 * released
 		 */
 		i2c_recover_bus(&dev->adapter);
+
+		/* disable controller if not disabled before */
+		if (dev->pdata->has_clear_cmd)
+			at91_twi_disable(dev);
 	} else {
+		at91_twi_disable(dev);
 		ret = num;
 	}
 
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH RESEND 3/3 i2c: at91: add advanced digital filtering support for SAMA5D4

In reply to Codrin Ciubotariu

I2C/TWI IP variant found in SAMA5D4 supports advanced digital filtering, even though, at the time of this patch, it is not present in Datasheet.

---
 drivers/i2c/busses/i2c-at91-core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/i2c/busses/i2c-at91-core.c +1 -0
index 2df9df585131..7549a75a98ef 100644
@@ -120,6 +120,7 @@ static struct at91_twi_pdata sama5d4_config = {
 	.clk_offset = 4,
 	.has_hold_field = true,
 	.has_dig_filtr = true,
+	.has_adv_dig_filtr = true,
 };
 
 static struct at91_twi_pdata sama5d2_config = {
Signature · 1 line
2.34.1
#4 lore raw
In reply to Codrin Ciubotariu
Quoted 13 lines · Codrin Ciubotariu

Resend due to the fact that the i2c list no longer accepts replys on the old patch-set. Comments not addressed yet.

The first patch is mostly a prerequisite for the second one. It only moves the i2c_recover_bus() out of the actual transfer function. This helps the second patch disable the controller before using GPIO recovery. The second patch will keep the controller enabled when a transfer occurs. Before using GPIO recovery, the controller must be disabled, to ignore potential glitches. However, the controller must be enabled for HW recovery (bus CLEAR command). The third and last patch adds advanced digital filtering support for SAMA5D4. The TWI IP found in SAMA5D4 supports advanced digital filtering, even if, at the moment of this patch, the SAMA5D4 datasheet does not mention it.

So, can you guys read this mail? It should be possible somehow that I can read your remarks to my remarks, no?

maintainer
#5 lore raw

Hi Wolfram,

Quoted 15 lines · Wolfram Sang
Quoted 13 lines · Codrin Ciubotariu

Resend due to the fact that the i2c list no longer accepts replys on the old patch-set. Comments not addressed yet.

The first patch is mostly a prerequisite for the second one. It only moves the i2c_recover_bus() out of the actual transfer function. This helps the second patch disable the controller before using GPIO recovery. The second patch will keep the controller enabled when a transfer occurs. Before using GPIO recovery, the controller must be disabled, to ignore potential glitches. However, the controller must be enabled for HW recovery (bus CLEAR command). The third and last patch adds advanced digital filtering support for SAMA5D4. The TWI IP found in SAMA5D4 supports advanced digital filtering, even if, at the moment of this patch, the SAMA5D4 datasheet does not mention it.

Quoted 2 lines

So, can you guys read this mail? It should be possible somehow that I can read your remarks to my remarks, no?

Codrin is not with Microchip anymore and we have to re-distribute the maintenance of this driver internally. His email address will probably "bounce" soon.

So, forgive us if there's a lack of response for some time ;-)

Best regards,
Nicolas

Signature · 1 line
Nicolas Ferre
#6 lore raw

Hi Nicolas,

Quoted 3 lines

Codrin is not with Microchip anymore and we have to re-distribute the maintenance of this driver internally. His email address will probably "bounce" soon.

Ah, very unfortunate that we couldn't move this series forward because of the mysterious mail problems.

All the best,

Wolfram

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#7 lore raw
In reply to Codrin Ciubotariu

Hi Codrin,

Quoted 4 lines

Codrin Ciubotariu (3):
i2c: at91: move i2c_recover_bus() outside of at91_do_twi_transfer()
i2c: at91: keep the controller disabled when it is not used
i2c: at91: add advanced digital filtering support for SAMA5D4

if you still have interest in applying this patch, may I please ask you to resend it?

Thanks,
Andi