Skip to contentSkip to conversation

lib: utils/i2c: update i2c interface

PATCH New Xiang W 1 participant 1 message Jan 10, 2022 SiFive drivers

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH v3 1/1 lib: utils/i2c: update i2c interface

1. Remove i2c register related operations
2. Simplify the low-level interface
3. Add 10bit address support
4. Add combination operation
5. Update fdt_i2c_sifive.c
6. Update sifive_fu740.c

---
Changes since v2:

In the 10-bit address mode, the addresses sent by repeated START and START are different, so a new flag I2C_M_RSTART is introduced. And change the old I2C_M_NOSTART to I2C_M_START.

Not all adapters can implement 8bit read and write operations, so add interfaces to allow the lower layer to implement i2c transmission.

Add support for I2C_M_RECV_LEN, which needs to be used when smbus block read

 include/sbi_utils/i2c/i2c.h     |  99 +++++++++++++++--------
 lib/utils/i2c/fdt_i2c_sifive.c  | 137 +++++++-------------------------
 lib/utils/i2c/i2c.c             | 106 +++++++++++++++++++++---
 platform/generic/sifive_fu740.c |  74 +++++++++++++++--
 4 files changed, 252 insertions(+), 164 deletions(-)
include/sbi_utils/i2c/i2c.h +64 -35
index 5a70364..5edd0d1 100644
@@ -13,6 +13,36 @@
 #include <sbi/sbi_types.h>
 #include <sbi/sbi_list.h>
 
+struct i2c_msg {
+	uint16_t addr;
+	uint16_t flags;
+
+/* standard i2c flags, the adapter needs to support all, I2C_M_TEN can choose to support */
+#define I2C_M_RD		0x0001 /* set this flag to read, otherwise write */
+#define I2C_M_START		0x0002 /* START before this message */
+#define I2C_M_RSTART		0x0004 /* send repeated START befor message */
+#define I2C_M_STOP		0x0008 /* send stop after this message */
+#define I2C_M_TEN		0x0010 /* 10 bit address */
+/* non-standard i2c flags, adapter does not necessarily support  */
+#define I2C_M_RECV_LEN		0x0400 /* length is first received byte */
+#define I2C_M_NO_RD_ACK		0x0800 /* skip the Ack bit on reads */
+#define I2C_M_IGNORE_NAK	0x1000 /* continue after NAK */
+#define I2C_M_REV_DIR_ADDR	0x2000 /* invert polarity of R/W bit */
+
+#define I2C_SIMPLE_SUPPORTED_FLAGS \
+	(I2C_M_RD \
+	| I2C_M_START \
+	| I2C_M_RSTART \
+	| I2C_M_STOP \
+	| I2C_M_TEN \
+	| I2C_M_RECV_LEN)
+
+	uint16_t len;
+	uint8_t *buf;
+};
+
+
+
 /** Representation of a I2C adapter */
 struct i2c_adapter {
 	/** Pointer to I2C driver owning this I2C adapter */
@@ -22,20 +52,41 @@ struct i2c_adapter {
 	int id;
 
 	/**
-	 * Send buffer to given address, register
+	 * The flags supported by the adapter
 	 *
-	 * @return 0 on success and negative error code on failure
-	 */
-	int (*write)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		    uint8_t *buffer, int len);
+	 * if the device supports write_byte/read_byte will support
+	 * I2C_SIMPLE_SUPPORTED_FLAGS
+	 * */
+	uint16_t supported_flags;
+
+	/**
+	 * Send 8 bits
+	 * can send a START condition before
+	 * can send a STOP condition at the end
+	 *
+	 * can be used to send addresses and data
+	 *
+	 * Not all adapters can implement this method
+	 * */
+	int (*write_byte)(struct i2c_adapter *ia, uint8_t byte,
+			bool send_start, bool send_stop);
+
+	/**
+	 * receive 8 bits
+	 * can respond with ack or nack after receiving
+	 * A stop can be sent after ack or nack
+	 * */
+	int (*read_byte)(struct i2c_adapter *ia, uint8_t *byte,
+			bool nak, bool send_stop);
 
 	/**
-	 * Read buffer from given address, register
+	 * i2c transfer
 	 *
-	 * @return 0 on success and negative error code on failure
-	 */
-	int (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		    uint8_t *buffer, int len);
+	 * If need to support flags other than I2C_SIMPLE_SUPPORTED_FLAGS,
+	 * need to implement this method
+	*/
+	int (*transfer)(struct i2c_adapter *ia,
+			struct i2c_msg *msgs, int msg_num);
 
 	/** List */
 	struct sbi_dlist node;
@@ -55,31 +106,9 @@ int i2c_adapter_add(struct i2c_adapter *ia);
 /** Un-register I2C adapter */
 void i2c_adapter_remove(struct i2c_adapter *ia);
 
-/** Send to device on I2C adapter bus */
-int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		      uint8_t *buffer, int len);
-
-/** Read from device on I2C adapter bus */
-int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		     uint8_t *buffer, int len);
-
-static inline int i2c_adapter_reg_write(struct i2c_adapter *ia, uint8_t addr,
-				 uint8_t reg, uint8_t val)
-{
-	return i2c_adapter_write(ia, addr, reg, &val, 1);
-}
+/** i2c transfer, transfer multiple i2c_msg */
+int i2c_adapter_transfer(struct i2c_adapter *ia,
+		struct i2c_msg *msgs, int msg_num);
 
-static inline int i2c_adapter_reg_read(struct i2c_adapter *ia, uint8_t addr,
-				       uint8_t reg, uint8_t *val)
-{
-	uint8_t buf;
-	int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
-
-	if (ret)
-		return ret;
-
-	*val = buf;
-	return 0;
-}
 
 #endif
lib/utils/i2c/fdt_i2c_sifive.c +29 -108

176 lines not on this page · raw message

lib/utils/i2c/i2c.c +93 -13

124 lines not on this page · raw message

platform/generic/sifive_fu740.c +66 -8

143 lines not on this page · raw message

Signature · 1 line
2.30.2