Skip to contentSkip to conversation

dm: i2c: Add a flag that need generate stop bit

PATCH New Chuanhua Han 4 participants 9 messages Jun 17, 2019 – Jul 7, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 dm: i2c: Add a flag that need generate stop bit

Usually the i2c bus needs to write the address of the register before reading the internal register data of the device (ignoring the transmission of the slave address).

Generally, the stop signal is not needed before the register is read, but there is a special chip that needs this stop signal (such as pcf2127). However, in the current i2c general code, the dm_i2c_read api encapsulates two messages, the first time is to set the register address message, the second time is a message to read the register data, so that no stop signal is generated.

This patch uses the DM_I2C_CHIP_ADDR_STOP flag for specific i2c chips, so if the i2c slave requires a stop signal, chips driver can set this flag, then call the dm_i2c_write to set the register address (a stop signal is generated after this API call), then call dm_i2c_read to read the register data.

---
Changes in v4:
- Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with
DM_I2C_CHIP_ADDR_STOP.

Changes in v3:
- Use the new flag DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET

Changes in v2:
- Split the original patch into 3 patches
- Add detailed description information for each patch

 drivers/i2c/i2c-uclass.c | 6 ++++--
 include/i2c.h            | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +4 -2
index e47abf1833..9804b5e8c7 100644
@@ -135,8 +135,10 @@ int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
 	if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
 		return i2c_read_bytewise(dev, offset, buffer, len);
 	ptr = msg;
-	if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
-		ptr++;
+	if (!(chip->flags & DM_I2C_CHIP_ADDR_STOP)) {
+		if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
+			ptr++;
+	}
 
 	if (len) {
 		ptr->addr = chip->chip_addr;
include/i2c.h +1 -0
index a5c760c711..3123cbf280 100644
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit addressing */
 	DM_I2C_CHIP_RD_ADDRESS	= 1 << 1, /* Send address for each read byte */
 	DM_I2C_CHIP_WR_ADDRESS	= 1 << 2, /* Send address for each write byte */
+	DM_I2C_CHIP_ADDR_STOP  = 1 << 3, /* Need generate stop bit */
 };
 
 struct udevice;
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 rtc: pcf2127: Fixed bug with rtc settings and getting error time

The previous pcf2127 RTC chip could not read and set the correct time. When reading the data of internal registers, the read address was the value of register plus 1. This is because this chip requires the host to send a stop signal after setting the register address and before reading the register data.

This patch sets the DM_I2C_CHIP_ADDR_STOP flag in order to generate a stop signal and fixes the bug of the original read and write time.

---
Changes in v4:
- Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with
DM_I2C_CHIP_ADDR_STOP.

Changes in v3:
- When the rtc time is obtained, the address of the set register
is separated from the read data.

Changes in v2:
- Split the original patch into 3 patches
- Add detailed description information for each patch

 drivers/rtc/pcf2127.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/rtc/pcf2127.c +14 -6
index dcf0340b4d..65794b98e6 100644
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm)
 {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm)
 	buf[i++] = bin2bcd(tm->tm_year % 100);
 
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
 	return ret;
 }
@@ -54,9 +51,12 @@ static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm)
 	int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
+	/* Set the address of the start register to be read */
+	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, NULL, 0);
 	if (ret < 0)
 		return ret;
+
+	/* Read register's data */
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
 	if (ret < 0)
 		return ret;
@@ -90,6 +90,13 @@ static int pcf2127_rtc_reset(struct udevice *dev)
 	return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev)
+{
+	i2c_set_chip_flags(dev, DM_I2C_CHIP_ADDR_STOP);
+
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,
@@ -104,6 +111,7 @@ static const struct udevice_id pcf2127_rtc_ids[] = {
 U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/2 rtc: pcf2127: Fixed bug with rtc settings and getting error time

Hi Chuanhua,

Quoted 9 lines

The previous pcf2127 RTC chip could not read and set the correct time. When reading the data of internal registers, the read address was the value of register plus 1. This is because this chip requires the host to send a stop signal after setting the register address and before reading the register data.

This patch sets the DM_I2C_CHIP_ADDR_STOP flag in order to generate a stop signal and fixes the bug of the original read and write time.

Thank you for the patch. It now looks far more better than the previous versions.

It is not using generic flag and can be reused by other ICs with similar issues.

Let's wait for Heiko's opinion.

Quoted 75 lines

---
Changes in v4:
- Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with
DM_I2C_CHIP_ADDR_STOP.

Changes in v3:
- When the rtc time is obtained, the address of the set
register is separated from the read data.

Changes in v2:
- Split the original patch into 3 patches
- Add detailed description information for each patch

 drivers/rtc/pcf2127.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/rtc/pcf2127.c +13 -6
index dcf0340b4d..65794b98e6 100644
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct
 rtc_time *tm) {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice *dev,
 const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);
 
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
 	return ret;
 }
@@ -54,9 +51,12 @@ static int pcf2127_rtc_get(struct udevice *dev,
 struct rtc_time *tm) int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
+	/* Set the address of the start register to be read */
+	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, NULL, 0);
 	if (ret < 0)
 		return ret;
+
+	/* Read register's data */
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
 	if (ret < 0)
 		return ret;
@@ -90,6 +90,13 @@ static int pcf2127_rtc_reset(struct udevice *dev)
 	return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev)
+{
+	i2c_set_chip_flags(dev, DM_I2C_CHIP_ADDR_STOP);
+
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,

drivers/rtc/pcf2127.c static const struct udevice_id pcf2127_rtc_ids[] · line 117

Quoted 7 lines
drivers/rtc/pcf2127.c +1 -0
@@ -104,6 +111,7 @@ static const struct udevice_id pcf2127_rtc_ids[]
 = { U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190618/fdb3ff34/attachment.sig>

#3 lore raw

PATCH 2/2 rtc: pcf2127: Fixed bug with rtc settings and getting error time

Hello Lukasz,

Am 18.06.2019 um 10:06 schrieb Lukasz Majewski:

Quoted 8 lines

Hi Chuanhua,

Quoted 6 lines

The previous pcf2127 RTC chip could not read and set the correct time. When reading the data of internal registers, the read address was the value of register plus 1. This is because this chip requires the host to send a stop signal after setting the register address and before reading the register data.

Quoted 6 lines
Quoted 3 lines

This patch sets the DM_I2C_CHIP_ADDR_STOP flag in order to generate a stop signal and fixes the bug of the original read and write time.

Thank you for the patch. It now looks far more better than the previous versions.

Yep!

Quoted 4 lines

It is not using generic flag and can be reused by other ICs with similar issues.

Let's wait for Heiko's opinion.

I already started a travis build, see:

https://travis-ci.org/hsdenx/u-boot-i2c/builds/547057368

Thanks for your comments!

bye,
Heiko

Quoted 99 lines
Quoted 75 lines

---
Changes in v4:
- Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with
DM_I2C_CHIP_ADDR_STOP.

Changes in v3:
- When the rtc time is obtained, the address of the set
register is separated from the read data.

Changes in v2:
- Split the original patch into 3 patches
- Add detailed description information for each patch

  drivers/rtc/pcf2127.c | 20 ++++++++++++++------
  1 file changed, 14 insertions(+), 6 deletions(-)
drivers/rtc/pcf2127.c +13 -6
index dcf0340b4d..65794b98e6 100644
@@ -24,12 +24,9 @@
 
  static int pcf2127_rtc_set(struct udevice *dev, const struct
 rtc_time *tm) {
-	uchar buf[8];
+	uchar buf[7] = {0};
  	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
  	/* hours, minutes and seconds */
  	buf[i++] = bin2bcd(tm->tm_sec);
  	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice *dev,
 const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);
 
  	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
  	return ret;
  }
@@ -54,9 +51,12 @@ static int pcf2127_rtc_get(struct udevice *dev,
 struct rtc_time *tm) int ret = 0;
  	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
+	/* Set the address of the start register to be read */
+	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, NULL, 0);
  	if (ret < 0)
  		return ret;
+
+	/* Read register's data */
  	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
  	if (ret < 0)
  		return ret;
@@ -90,6 +90,13 @@ static int pcf2127_rtc_reset(struct udevice *dev)
  	return 0;
  }
 
+static int pcf2127_probe(struct udevice *dev)
+{
+	i2c_set_chip_flags(dev, DM_I2C_CHIP_ADDR_STOP);
+
+	return 0;
+}
+
  static const struct rtc_ops pcf2127_rtc_ops = {
  	.get = pcf2127_rtc_get,
  	.set = pcf2127_rtc_set,

drivers/rtc/pcf2127.c static const struct udevice_id pcf2127_rtc_ids[] · line 117

Quoted 7 lines
drivers/rtc/pcf2127.c +1 -0
@@ -104,6 +111,7 @@ static const struct udevice_id pcf2127_rtc_ids[]
 = { U_BOOT_DRIVER(rtc_pcf2127) = {
  	.name	= "rtc-pcf2127",
  	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
  	.of_match = pcf2127_rtc_ids,
  	.ops	= &pcf2127_rtc_ops,
  };

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: hs at denx.de
#4 lore raw

[U-Boot] [EXT] Re: [PATCH 2/2] rtc: pcf2127: Fixed bug with rtc settings and getting error time

In reply to Lukasz Majewski

+ Simon Glass

Quoted 123 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年6月18日 16:06
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; sjg at chromium.org; Biwen Li <[email protected]>; u-boot at lists.denx.de
Subject: [EXT] Re: [PATCH 2/2] rtc: pcf2127: Fixed bug with rtc settings and getting error time

Hi Chuanhua,

Quoted 8 lines

The previous pcf2127 RTC chip could not read and set the correct time. When reading the data of internal registers, the read address was the value of register plus 1. This is because this chip requires the host to send a stop signal after setting the register address and before reading the register data.

This patch sets the DM_I2C_CHIP_ADDR_STOP flag in order to generate a stop signal and fixes the bug of the original read and write time.

Thank you for the patch. It now looks far more better than the previous versions.

It is not using generic flag and can be reused by other ICs with similar issues.

Let's wait for Heiko's opinion.

Quoted 74 lines

---
Changes in v4:
- Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with
DM_I2C_CHIP_ADDR_STOP.

Changes in v3:
- When the rtc time is obtained, the address of the set register is
separated from the read data.

Changes in v2:
- Split the original patch into 3 patches
- Add detailed description information for each patch

 drivers/rtc/pcf2127.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/rtc/pcf2127.c index renamed from drivers/rtc/pcf2127.c +0 -0

dcf0340b4d..65794b98e6 100644

drivers/rtc/pcf2127.c +12 -6
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time
 *tm) {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice *dev,
 const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);
 
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
 	return ret;
 }
@@ -54,9 +51,12 @@ static int pcf2127_rtc_get(struct udevice *dev,
 struct rtc_time *tm) int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
+	/* Set the address of the start register to be read */
+	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, NULL, 0);
 	if (ret < 0)
 		return ret;
+
+	/* Read register's data */
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
 	if (ret < 0)
 		return ret;
@@ -90,6 +90,13 @@ static int pcf2127_rtc_reset(struct udevice *dev)
 	return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev) {
+	i2c_set_chip_flags(dev, DM_I2C_CHIP_ADDR_STOP);
+
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,

drivers/rtc/pcf2127.c static const struct udevice_id pcf2127_rtc_ids[] = · line 117

Quoted 7 lines
drivers/rtc/pcf2127.c +1 -0
@@ -104,6 +111,7 @@ static const struct udevice_id pcf2127_rtc_ids[] =
 { U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de

#5 lore raw

PATCH 1/2 dm: i2c: Add a flag that need generate stop bit

In reply to Chuanhua Han
Quoted 11 lines · On Mon, 17 Jun 2019 21:12:40 +0800 Chuanhua Han <[email protected]> wrote:

Usually the i2c bus needs to write the address of the register before reading the internal register data of the device (ignoring the transmission of the slave address).

Generally, the stop signal is not needed before the register is read, but there is a special chip that needs this stop signal (such as pcf2127). However, in the current i2c general code, the dm_i2c_read api encapsulates two messages, the first time is to set the register address message, the second time is a message to read the register data, so that no stop signal is generated.

Quoted 7 lines

This patch uses the DM_I2C_CHIP_ADDR_STOP flag for specific i2c chips, so if the i2c slave requires a stop signal, chips driver can set this flag, then call the dm_i2c_write to set the register address (a stop signal is generated after this API call), then call dm_i2c_read to read the register data.

Quoted 42 lines

---
Changes in v4:
- Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with
DM_I2C_CHIP_ADDR_STOP.

Changes in v3:
- Use the new flag DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET

Changes in v2:
- Split the original patch into 3 patches
- Add detailed description information for each patch

 drivers/i2c/i2c-uclass.c | 6 ++++--
 include/i2c.h            | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c +0 -0
index e47abf1833..9804b5e8c7 100644
@@ -135,8 +135,10 @@ int dm_i2c_read(struct udevice *dev, uint

offset, uint8_t *buffer, int len) if (chip->flags & DM_I2C_CHIP_RD_ADDRESS) return i2c_read_bytewise(dev, offset, buffer, len); ptr = msg;

drivers/i2c/i2c-uclass.c +4 -2
-	if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
-		ptr++;
+	if (!(chip->flags & DM_I2C_CHIP_ADDR_STOP)) {
+		if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
+			ptr++;
+	}
 
 	if (len) {
 		ptr->addr = chip->chip_addr;
include/i2c.h +0 -0
index a5c760c711..3123cbf280 100644
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit addressing
 */ DM_I2C_CHIP_RD_ADDRESS	= 1 << 1, /* Send address for each

read byte */ DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address
for each write byte */

include/i2c.h enum dm_i2c_chip_flags { · line 34

Quoted 4 lines
include/i2c.h +1 -0
+	DM_I2C_CHIP_ADDR_STOP  = 1 << 3, /* Need generate stop bit */
 };
 
 struct udevice;

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190618/5b8b95c3/attachment.sig>

#6 lore raw

+ Simon Glass

Quoted 91 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年6月18日 16:07
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; sjg at chromium.org; Biwen Li <[email protected]>; u-boot at lists.denx.de
Subject: [EXT] Re: [PATCH 1/2] dm: i2c: Add a flag that need generate stop bit

Quoted 11 lines · On Mon, 17 Jun 2019 21:12:40 +0800 Chuanhua Han <[email protected]> wrote:

Usually the i2c bus needs to write the address of the register before reading the internal register data of the device (ignoring the transmission of the slave address).

Generally, the stop signal is not needed before the register is read, but there is a special chip that needs this stop signal (such as pcf2127). However, in the current i2c general code, the dm_i2c_read api encapsulates two messages, the first time is to set the register address message, the second time is a message to read the register data, so that no stop signal is generated.

Quoted 7 lines

This patch uses the DM_I2C_CHIP_ADDR_STOP flag for specific i2c chips, so if the i2c slave requires a stop signal, chips driver can set this flag, then call the dm_i2c_write to set the register address (a stop signal is generated after this API call), then call dm_i2c_read to read the register data.

Quoted 42 lines

---
Changes in v4:
- Replace DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET with
DM_I2C_CHIP_ADDR_STOP.

Changes in v3:
- Use the new flag DM_I2C_CHIP_RD_NO_I2C_SETUP_OFFSET

Changes in v2:
- Split the original patch into 3 patches
- Add detailed description information for each patch

 drivers/i2c/i2c-uclass.c | 6 ++++--
 include/i2c.h            | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/i2c/i2c-uclass.c index renamed from drivers/i2c/i2c-uclass.c +0 -0

e47abf1833..9804b5e8c7 100644

drivers/i2c/i2c-uclass.c +0 -0
@@ -135,8 +135,10 @@ int dm_i2c_read(struct udevice *dev, uint offset,

uint8_t *buffer, int len) if (chip->flags & DM_I2C_CHIP_RD_ADDRESS) return i2c_read_bytewise(dev, offset, buffer, len); ptr = msg;

drivers/i2c/i2c-uclass.c +4 -2
-	if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
-		ptr++;
+	if (!(chip->flags & DM_I2C_CHIP_ADDR_STOP)) {
+		if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
+			ptr++;
+	}
 
 	if (len) {
 		ptr->addr = chip->chip_addr;
include/i2c.h index renamed from include/i2c.h +0 -0

a5c760c711..3123cbf280 100644

include/i2c.h +0 -0
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit addressing
 */ DM_I2C_CHIP_RD_ADDRESS	= 1 << 1, /* Send address for each

read byte */ DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address
for each write byte */

include/i2c.h enum dm_i2c_chip_flags { · line 34

Quoted 4 lines
include/i2c.h +1 -0
+	DM_I2C_CHIP_ADDR_STOP  = 1 << 3, /* Need generate stop bit */
 };
 
 struct udevice;

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de

#7 lore raw

Hi Chuanhua,

Quoted 34 lines · Chuanhua Han

+ Simon Glass

Quoted 31 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年6月18日 16:07
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; sjg at chromium.org; Biwen Li <[email protected]>; u-boot at lists.denx.de
Subject: [EXT] Re: [PATCH 1/2] dm: i2c: Add a flag that need generate stop bit

Quoted 11 lines · On Mon, 17 Jun 2019 21:12:40 +0800 Chuanhua Han <[email protected]> wrote:

Usually the i2c bus needs to write the address of the register before reading the internal register data of the device (ignoring the transmission of the slave address).

Generally, the stop signal is not needed before the register is read, but there is a special chip that needs this stop signal (such as pcf2127). However, in the current i2c general code, the dm_i2c_read api encapsulates two messages, the first time is to set the register address message, the second time is a message to read the register data, so that no stop signal is generated.

Quoted 7 lines

This patch uses the DM_I2C_CHIP_ADDR_STOP flag for specific i2c chips, so if the i2c slave requires a stop signal, chips driver can set this flag, then call the dm_i2c_write to set the register address (a stop signal is generated after this API call), then call dm_i2c_read to read the register data.

I already asked why you cannot use dm_i2c_xfer() to do what you want, but I did not see your response. Can you please send your response again here on this thread? The dm_i2c_xfer() function is designed to handle any sorts of quirk that are needed. I'd like to avoid quirks in the core code if possible.

If you really want to add a quirk, then I would like to put it behind a CONFIG_I2C_QUIRKS option, and use:

  if (IS_ENABLED(CONFIG_I2C_QUIRKS) || !(chip->flags & DM_I2C_CHIP_ADDR_STOP)) {
           if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
                   ptr++;

so we don't affect code size on other platforms. After all this feature is just for one broken chip and should not negatively affect everyone else that follows the spec correctly.

I am worried about all the SPL-affecting changing that add a few bytes here and there, but really add up.

But my first preference would be to use i2c_xfer() if possible.

Regards,
Simon

#8 lore raw
Quoted 66 lines

-----Original Message-----
From: Simon Glass <[email protected]>
Sent: 2019年7月7日 1:17
To: Chuanhua Han <[email protected]>
Cc: Lukasz Majewski <[email protected]>; hs at denx.de; Biwen Li <[email protected]>; u-boot at lists.denx.de
Subject: Re: [EXT] Re: [PATCH 1/2] dm: i2c: Add a flag that need generate stop bit

Caution: EXT Email

Hi Chuanhua,

Quoted 35 lines · Chuanhua Han

+ Simon Glass

Quoted 32 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年6月18日 16:07
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; sjg at chromium.org; Biwen Li <[email protected]>; u-boot at lists.denx.de
Subject: [EXT] Re: [PATCH 1/2] dm: i2c: Add a flag that need generate stop bit

Quoted 11 lines · On Mon, 17 Jun 2019 21:12:40 +0800 Chuanhua Han <[email protected]> wrote:

Usually the i2c bus needs to write the address of the register before reading the internal register data of the device (ignoring the transmission of the slave address).

Generally, the stop signal is not needed before the register is read, but there is a special chip that needs this stop signal (such as pcf2127). However, in the current i2c general code, the dm_i2c_read api encapsulates two messages, the first time is to set the register address message, the second time is a message to read the register data, so that no stop signal is generated.

Quoted 7 lines

This patch uses the DM_I2C_CHIP_ADDR_STOP flag for specific i2c chips, so if the i2c slave requires a stop signal, chips driver can set this flag, then call the dm_i2c_write to set the register address (a stop signal is generated after this API call), then call dm_i2c_read to read the register data.

I already asked why you cannot use dm_i2c_xfer() to do what you want, but I did not see your response. Can you please send your response again here on this thread? The dm_i2c_xfer() function is designed to handle any sorts of quirk that are needed. I'd like to avoid quirks in the core code if possible.

If you really want to add a quirk, then I would like to put it behind a CONFIG_I2C_QUIRKS option, and use:

if (IS_ENABLED(CONFIG_I2C_QUIRKS) || !(chip->flags &
DM_I2C_CHIP_ADDR_STOP)) {
if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
ptr++;

Quoted 8 lines

so we don't affect code size on other platforms. After all this feature is just for one broken chip and should not negatively affect everyone else that follows the spec correctly.

I am worried about all the SPL-affecting changing that add a few bytes here and there, but really add up.

But my first preference would be to use i2c_xfer() if possible.

OK,thanks,I will send next version!

Quoted 3 lines

Regards,
Simon