[U-Boot] [PATCH v2 77/78] i2c: ihs_i2c: Factor out send_buffer method

Mario Six mario.six at gdsys.cc
Wed Dec 13 07:19:45 UTC 2017


Simplify the driver logic by extracting a common send_buffer method.

Signed-off-by: Mario Six <mario.six at gdsys.cc>
---

v1 -> v2:
None

---
 drivers/i2c/ihs_i2c.c | 50 ++++++++++++++++++++++++++------------------------
 1 file changed, 26 insertions(+), 24 deletions(-)

diff --git a/drivers/i2c/ihs_i2c.c b/drivers/i2c/ihs_i2c.c
index c3fddb55f9..0432ca0994 100644
--- a/drivers/i2c/ihs_i2c.c
+++ b/drivers/i2c/ihs_i2c.c
@@ -203,32 +203,48 @@ static int ihs_i2c_transfer(uchar chip, uchar *buffer, int len, bool read,
 }

 #ifdef CONFIG_DM_I2C
-static int ihs_i2c_address(struct udevice *dev, uchar chip, u8 *addr, int alen, bool hold_bus)
+static int ihs_i2c_send_buffer(struct udevice *dev, uchar chip, u8 *data, int len, bool hold_bus, int read)
 #else
-static int ihs_i2c_address(uchar chip, u8 *addr, int alen, bool hold_bus)
+static int ihs_i2c_send_buffer(uchar chip, u8 *data, int len, bool hold_bus,
+			       int read)
 #endif
 {
-	while (alen) {
-		int transfer = min(alen, 2);
-		bool is_last = alen <= transfer;
+	while (len) {
+		int transfer = min(len, 2);
+		bool is_last = len <= transfer;

 #ifdef CONFIG_DM_I2C
-		if (ihs_i2c_transfer(dev, chip, addr, transfer, I2COP_WRITE,
+		if (ihs_i2c_transfer(dev, chip, data, transfer, read,
 				     hold_bus ? false : is_last))
 			return 1;
 #else
-		if (ihs_i2c_transfer(chip, addr, transfer, I2COP_WRITE,
+		if (ihs_i2c_transfer(chip, data, transfer, read,
 				     hold_bus ? false : is_last))
 			return 1;
 #endif

-		alen -= transfer;
+		data += transfer;
+		len -= transfer;
 	}

 	return 0;
 }

 #ifdef CONFIG_DM_I2C
+static int ihs_i2c_address(struct udevice *dev, uchar chip, u8 *addr, int alen,
+			   bool hold_bus)
+#else
+static int ihs_i2c_address(uchar chip, u8 *addr, int alen, bool hold_bus)
+#endif
+{
+#ifdef CONFIG_DM_I2C
+	return ihs_i2c_send_buffer(dev, chip, addr, alen, hold_bus, I2COP_WRITE);
+#else
+	return ihs_i2c_send_buffer(chip, addr, alen, hold_bus, I2COP_WRITE);
+#endif
+}
+
+#ifdef CONFIG_DM_I2C
 static int ihs_i2c_access(struct udevice *dev, uchar chip, u8 *addr,
 			  int alen, uchar *buffer, int len, int read)
 #else
@@ -245,25 +261,11 @@ static int ihs_i2c_access(struct i2c_adapter *adap, uchar chip, u8 *addr,
 		return 1;
 #endif

-	while (len) {
-		int transfer = min(len, 2);
-		bool is_last = len <= transfer;
-
 #ifdef CONFIG_DM_I2C
-		if (ihs_i2c_transfer(dev, chip, buffer, transfer, read,
-				     is_last))
-			return 2;
+	return ihs_i2c_send_buffer(dev, chip, buffer, len, false, read);
 #else
-		if (ihs_i2c_transfer(chip, buffer, transfer, read,
-				     is_last))
-			return 2;
+	return ihs_i2c_send_buffer(chip, buffer, len, false, read);
 #endif
-
-		buffer += transfer;
-		len -= transfer;
-	}
-
-	return 0;
 }

 #ifdef CONFIG_DM_I2C
--
2.13.6



More information about the U-Boot mailing list