Skip to contentSkip to conversation

driver: adc: ltc2497: return directly after reading the adc conversion value

PATCH In review [email protected] 3 participants 9 messages Jun 1, 2021 – Jun 4, 2021

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

PATCH 1/1 driver: adc: ltc2497: return directly after reading the adc conversion value

From: Meng Li <[email protected]>

When read adc conversion value with below command:
cat /sys/.../iio:device0/in_voltage0-voltage1_raw
There is an error reported as below:
ltc2497 0-0014: i2c transfer failed: -EREMOTEIO
This i2c transfer issue is introduced by commit 69548b7c2c4f ("iio:
adc: ltc2497: split protocol independent part in a separate module"). When extract the common code into ltc2497-core.c, it change the code logic of function ltc2497core_read(). With wrong reading sequence, the action of enable adc channel is sent to chip again during adc channel is in conversion status. In this way, there is no ack from chip, and then cause i2c transfer failed.
In order to keep the code logic is the same with original ideal, it is need to return direct after reading the adc conversion value.

v2:
According to ltc2497 datasheet, the max value of conversion time is 149.9 ms. So, add 20% to the 150msecs so that there is enough time for data conversion.

Fixes 69548b7c2c4f ("iio: adc: ltc2497: split protocol independent part in a separate module ")
---
 drivers/iio/adc/ltc2497.c | 2 ++
 drivers/iio/adc/ltc2497.h | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/iio/adc/ltc2497.c +2 -0
index 1adddf5a88a9..fd5a66860a47 100644
@@ -41,6 +41,8 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
 		}
 
 		*val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+
+		return ret;
 	}
 
 	ret = i2c_smbus_write_byte(st->client,
drivers/iio/adc/ltc2497.h +1 -1
index d0b42dd6b8ad..e451930837d8 100644
@@ -2,7 +2,7 @@
 
 #define LTC2497_ENABLE			0xA0
 #define LTC2497_CONFIG_DEFAULT		LTC2497_ENABLE
-#define LTC2497_CONVERSION_TIME_MS	150ULL
+#define LTC2497_CONVERSION_TIME_MS	180ULL
 
 struct ltc2497core_driverdata {
 	struct regulator *ref;
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 16 lines · On Tue, 1 Jun 2021 17:28:05 +0800 [email protected] wrote:

From: Meng Li <[email protected]>

When read adc conversion value with below command:
cat /sys/.../iio:device0/in_voltage0-voltage1_raw
There is an error reported as below:
ltc2497 0-0014: i2c transfer failed: -EREMOTEIO
This i2c transfer issue is introduced by commit 69548b7c2c4f ("iio:
adc: ltc2497: split protocol independent part in a separate module"). When extract the common code into ltc2497-core.c, it change the code logic of function ltc2497core_read(). With wrong reading sequence, the action of enable adc channel is sent to chip again during adc channel is in conversion status. In this way, there is no ack from chip, and then cause i2c transfer failed.
In order to keep the code logic is the same with original ideal, it is need to return direct after reading the adc conversion value.

Quoted 4 lines

v2:
According to ltc2497 datasheet, the max value of conversion time is 149.9 ms. So, add 20% to the 150msecs so that there is enough time for data conversion.

Version change logs go below the --- as we don't want to preserve them forever in the git history.

I may have lost track of the discussion, but I thought the idea was that perhaps the longer time period would remove the need for the early return?

Thanks,

Jonathan

Quoted 35 lines
Fixes 69548b7c2c4f ("iio: adc: ltc2497: split protocol independent part in a separate module ")
---
 drivers/iio/adc/ltc2497.c | 2 ++
 drivers/iio/adc/ltc2497.h | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/iio/adc/ltc2497.c +2 -0
index 1adddf5a88a9..fd5a66860a47 100644
@@ -41,6 +41,8 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
 		}
 
 		*val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+
+		return ret;
 	}
 
 	ret = i2c_smbus_write_byte(st->client,
drivers/iio/adc/ltc2497.h +1 -1
index d0b42dd6b8ad..e451930837d8 100644
@@ -2,7 +2,7 @@
 
 #define LTC2497_ENABLE			0xA0
 #define LTC2497_CONFIG_DEFAULT		LTC2497_ENABLE
-#define LTC2497_CONVERSION_TIME_MS	150ULL
+#define LTC2497_CONVERSION_TIME_MS	180ULL
 
 struct ltc2497core_driverdata {
 	struct regulator *ref;
author
#2 lore raw
Quoted 38 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Friday, June 4, 2021 12:20 AM
To: Li, Meng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 16 lines · On Tue, 1 Jun 2021 17:28:05 +0800 [email protected] wrote:

From: Meng Li <[email protected]>

When read adc conversion value with below command:
cat /sys/.../iio:device0/in_voltage0-voltage1_raw
There is an error reported as below:
ltc2497 0-0014: i2c transfer failed: -EREMOTEIO This i2c transfer issue is introduced by commit 69548b7c2c4f ("iio:
adc: ltc2497: split protocol independent part in a separate module"). When extract the common code into ltc2497-core.c, it change the code logic of function ltc2497core_read(). With wrong reading sequence, the action of enable adc channel is sent to chip again during adc channel is in conversion status. In this way, there is no ack from chip, and then cause i2c transfer failed.
In order to keep the code logic is the same with original ideal, it is need to return direct after reading the adc conversion value.

Quoted 4 lines

v2:
According to ltc2497 datasheet, the max value of conversion time is 149.9 ms. So, add 20% to the 150msecs so that there is enough time for data conversion.

Quoted 6 lines

Version change logs go below the --- as we don't want to preserve them forever in the git history.

I may have lost track of the discussion, but I thought the idea was that perhaps the longer time period would remove the need for the early return?

No!
I think the ret is essential.
Because when call i2c_master_recv() to receive data from chip, there is a stop bit sent to chip at the last step. When the chip receive the stop bit, it will start the next conversion operation.
So, if we send any command on I2C bus when the chip is in conversion status, there will be a i2c error occurred.

Thanks,
Limeng

Quoted 40 lines

Thanks,

Jonathan

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct

Quoted 15 lines
Fixes 69548b7c2c4f ("iio: adc: ltc2497: split protocol independent

part in a separate module ")

---
 drivers/iio/adc/ltc2497.c | 2 ++
 drivers/iio/adc/ltc2497.h | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/iio/adc/ltc2497.c +0 -0
index 1adddf5a88a9..fd5a66860a47 100644
@@ -41,6 +41,8 @@ static int ltc2497_result_and_measure(struct

ltc2497core_driverdata *ddata,

Quoted 21 lines
drivers/iio/adc/ltc2497.c +2 -0
              }
 
              *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+
+             return ret;
      }
 
      ret = i2c_smbus_write_byte(st->client, diff --git

a/drivers/iio/adc/ltc2497.h b/drivers/iio/adc/ltc2497.h index d0b42dd6b8ad..e451930837d8 100644

drivers/iio/adc/ltc2497.h +1 -1
@@ -2,7 +2,7 @@
 
 #define LTC2497_ENABLE                       0xA0
 #define LTC2497_CONFIG_DEFAULT               LTC2497_ENABLE
-#define LTC2497_CONVERSION_TIME_MS   150ULL
+#define LTC2497_CONVERSION_TIME_MS   180ULL
 
 struct ltc2497core_driverdata {
      struct regulator *ref;
#3 lore raw

Hello,

Quoted 19 lines · Meng
Quoted 19 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Friday, June 4, 2021 12:20 AM
To: Li, Meng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 2 lines · On Tue, 1 Jun 2021 17:28:05 +0800 [email protected] wrote:

From: Meng Li <[email protected]>

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines

When read adc conversion value with below command:
cat /sys/.../iio:device0/in_voltage0-voltage1_raw
There is an error reported as below:
ltc2497 0-0014: i2c transfer failed: -EREMOTEIO This i2c transfer issue is introduced by commit 69548b7c2c4f ("iio:

Quoted 8 lines
Quoted 8 lines

adc: ltc2497: split protocol independent part in a separate module"). When extract the common code into ltc2497-core.c, it change the code logic of function ltc2497core_read(). With wrong reading sequence, the action of enable adc channel is sent to chip again during adc channel is in conversion status. In this way, there is no ack from chip, and then cause i2c transfer failed.
In order to keep the code logic is the same with original ideal, it is need to return direct after reading the adc conversion value.

As background about the choice of the .result_and_measure callback:
A difference between the ltc2497 (i2c) and ltc2496 (spi) is that for the latter reading the result of the last conversion and starting a new is a single bus operation and the one cannot be done without the other.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

v2:
According to ltc2497 datasheet, the max value of conversion time is 149.9 ms. So, add 20% to the 150msecs so that there is enough time for data conversion.

Version change logs go below the --- as we don't want to preserve them forever in the git history.

Quoted 6 lines
Quoted 3 lines

I may have lost track of the discussion, but I thought the idea was that perhaps the longer time period would remove the need for the early return?

No!
I think the ret is essential.

I'd like to understand why. Currently ltc2497core_read() looks as follows (simplified by dropping error handling, and unrolling the result_and_measure callback for the i2c case):

	ltc2497core_wait_conv()

	// result_and_measure(address, NULL)
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

	msleep_interruptible(LTC2497_CONVERSION_TIME_MS)

	// result_and_measure(address, val);
	i2c_master_recv(client, &buf, 3);
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

With the early return you suggest to introduce with your patch you save the last i2c_smbus_write_byte call. The data sheet indeed claims to start a new conversion at the stop condition.

So either the reading of the conversion result and programming of the (maybe) new address has to be done in a single i2c transfer, or we have to do something like your patch.

What I don't like about your approach is that it changes the semantic of the callback to result_*or*_measure which is something the spi variant cannot implement. With the current use of the function this is fine, however if at some time in the future we implement a bulk conversion shortcut this hurts.

So I suggest to do:

---->8----
From: Uwe Kleine-König <[email protected]>
Date: Fri, 4 Jun 2021 08:02:44 +0200
Subject: [PATCH] iio: ltc2497: Fix reading conversion results

After the result of the previous conversion is read the chip automatically starts a new conversion and doesn't accept new i2c transfers until this conversion is completed which makes the function return failure.

So add an early return iff the programming of the new address isn't needed. Note this will not fix the problem in general, but all cases that are currently used. Once this changes we get the failure back, but this can be addressed when the need arises.

Fixes 69548b7c2c4f ("iio: adc: ltc2497: split protocol independent part in a separate module ")
---
 drivers/iio/adc/ltc2497.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/adc/ltc2497.c +13 -0
@@ -41,6 +41,19 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
 		}
 
 		*val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+
+		/*
+		 * The part started a new conversion at the end of the above i2c
+		 * transfer, so if the address didn't change since the last call
+		 * everything is fine and we can return early.
+		 * If not (which should only happen when some sort of bulk
+		 * conversion is implemented) we have to program the new
+		 * address. Note that this probably fails as the conversion that
+		 * was triggered above is like not complete yet and the two
+		 * operations have to be done in a single transfer.
+		 */
+		if (ddata->addr_prev == address)
+			return 0;
 	}
 
 	ret = i2c_smbus_write_byte(st->client,

Compared to Meng Li's patch this doesn't introduce reporting of bogus conversion results once we implement bulk conversion.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#4 lore raw
Quoted 150 lines

-----Original Message-----
From: Uwe Kleine-König <[email protected]>
Sent: Friday, June 4, 2021 2:13 PM
To: Li, Meng <[email protected]>
Cc: Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

Hello,

Quoted 19 lines · Meng
Quoted 19 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Friday, June 4, 2021 12:20 AM
To: Li, Meng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 2 lines · On Tue, 1 Jun 2021 17:28:05 +0800 [email protected] wrote:

From: Meng Li <[email protected]>

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines

When read adc conversion value with below command:
cat /sys/.../iio:device0/in_voltage0-voltage1_raw
There is an error reported as below:
ltc2497 0-0014: i2c transfer failed: -EREMOTEIO This i2c transfer issue is introduced by commit 69548b7c2c4f ("iio:

Quoted 8 lines
Quoted 8 lines

adc: ltc2497: split protocol independent part in a separate module"). When extract the common code into ltc2497-core.c, it change the code logic of function ltc2497core_read(). With wrong reading sequence, the action of enable adc channel is sent to chip again during adc channel is in conversion status. In this way, there is no ack from chip, and then cause i2c transfer failed.
In order to keep the code logic is the same with original ideal, it is need to return direct after reading the adc conversion value.

As background about the choice of the .result_and_measure callback:
A difference between the ltc2497 (i2c) and ltc2496 (spi) is that for the latter reading the result of the last conversion and starting a new is a single bus operation and the one cannot be done without the other.

Quoted 11 lines
Quoted 11 lines
Quoted 5 lines

v2:
According to ltc2497 datasheet, the max value of conversion time is
149.9 ms. So, add 20% to the 150msecs so that there is enough time for data conversion.

Version change logs go below the --- as we don't want to preserve them forever in the git history.

I may have lost track of the discussion, but I thought the idea was that perhaps the longer time period would remove the need for the early

return?

Quoted 4 lines
Quoted 1 line

No!
I think the ret is essential.

I'd like to understand why. Currently ltc2497core_read() looks as follows (simplified by dropping error handling, and unrolling the result_and_measure callback for the i2c case):

	ltc2497core_wait_conv()

	// result_and_measure(address, NULL)
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

	msleep_interruptible(LTC2497_CONVERSION_TIME_MS)

	// result_and_measure(address, val);
	i2c_master_recv(client, &buf, 3);
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

With the early return you suggest to introduce with your patch you save the last i2c_smbus_write_byte call. The data sheet indeed claims to start a new conversion at the stop condition.

So either the reading of the conversion result and programming of the (maybe) new address has to be done in a single i2c transfer, or we have to do something like your patch.

What I don't like about your approach is that it changes the semantic of the callback to result_*or*_measure which is something the spi variant cannot implement. With the current use of the function this is fine, however if at some time in the future we implement a bulk conversion shortcut this hurts.

So I suggest to do:

---->8----
From: Uwe Kleine-König <[email protected]>
Date: Fri, 4 Jun 2021 08:02:44 +0200
Subject: [PATCH] iio: ltc2497: Fix reading conversion results

After the result of the previous conversion is read the chip automatically starts a new conversion and doesn't accept new i2c transfers until this conversion is completed which makes the function return failure.

So add an early return iff the programming of the new address isn't needed. Note this will not fix the problem in general, but all cases that are currently used. Once this changes we get the failure back, but this can be addressed when the need arises.

Fixes 69548b7c2c4f ("iio: adc: ltc2497: split protocol independent part in a

separate module ")

---
 drivers/iio/adc/ltc2497.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/adc/ltc2497.c +3 -0
@@ -41,6 +41,19 @@ static int ltc2497_result_and_measure(struct
 ltc2497core_driverdata *ddata,
 		}
 
 		*val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+
+		/*
+		 * The part started a new conversion at the end of the above

i2c

drivers/iio/adc/ltc2497.c +5 -0
+		 * transfer, so if the address didn't change since the last call
+		 * everything is fine and we can return early.
+		 * If not (which should only happen when some sort of bulk
+		 * conversion is implemented) we have to program the new
+		 * address. Note that this probably fails as the conversion

that

drivers/iio/adc/ltc2497.c +5 -0
+		 * was triggered above is like not complete yet and the two
+		 * operations have to be done in a single transfer.
+		 */
+		if (ddata->addr_prev == address)
+			return 0;
 	}
 
 	ret = i2c_smbus_write_byte(st->client,
Quoted 3 lines

Compared to Meng Li's patch this doesn't introduce reporting of bogus conversion results once we implement bulk conversion.

Ok!
Understand.
I agree you to push patch to upstream and it is more reasonable that the original author to fix this issue.

Thanks,
Limeng

Quoted 6 lines

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | https://www.pengutronix.de/ |

maintainer
#5 lore raw
Quoted 128 lines · Meng
Quoted 128 lines

-----Original Message-----
From: Uwe Kleine-König <[email protected]>
Sent: Friday, June 4, 2021 2:13 PM
To: Li, Meng <[email protected]>
Cc: Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

Hello,

Quoted 19 lines · Meng
Quoted 19 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Friday, June 4, 2021 12:20 AM
To: Li, Meng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 2 lines · On Tue, 1 Jun 2021 17:28:05 +0800 [email protected] wrote:

From: Meng Li <[email protected]>

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines

When read adc conversion value with below command:
cat /sys/.../iio:device0/in_voltage0-voltage1_raw
There is an error reported as below:
ltc2497 0-0014: i2c transfer failed: -EREMOTEIO This i2c transfer issue is introduced by commit 69548b7c2c4f ("iio:

Quoted 8 lines
Quoted 8 lines

adc: ltc2497: split protocol independent part in a separate module"). When extract the common code into ltc2497-core.c, it change the code logic of function ltc2497core_read(). With wrong reading sequence, the action of enable adc channel is sent to chip again during adc channel is in conversion status. In this way, there is no ack from chip, and then cause i2c transfer failed.
In order to keep the code logic is the same with original ideal, it is need to return direct after reading the adc conversion value.

As background about the choice of the .result_and_measure callback:
A difference between the ltc2497 (i2c) and ltc2496 (spi) is that for the latter reading the result of the last conversion and starting a new is a single bus operation and the one cannot be done without the other.

Quoted 11 lines
Quoted 11 lines
Quoted 5 lines

v2:
According to ltc2497 datasheet, the max value of conversion time is
149.9 ms. So, add 20% to the 150msecs so that there is enough time for data conversion.

Version change logs go below the --- as we don't want to preserve them forever in the git history.

I may have lost track of the discussion, but I thought the idea was that perhaps the longer time period would remove the need for the early

return?

Quoted 4 lines
Quoted 1 line

No!
I think the ret is essential.

I'd like to understand why. Currently ltc2497core_read() looks as follows (simplified by dropping error handling, and unrolling the result_and_measure callback for the i2c case):

	ltc2497core_wait_conv()

	// result_and_measure(address, NULL)
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

	msleep_interruptible(LTC2497_CONVERSION_TIME_MS)

	// result_and_measure(address, val);
	i2c_master_recv(client, &buf, 3);
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

With the early return you suggest to introduce with your patch you save the last i2c_smbus_write_byte call. The data sheet indeed claims to start a new conversion at the stop condition.

So either the reading of the conversion result and programming of the (maybe) new address has to be done in a single i2c transfer, or we have to do something like your patch.

What I don't like about your approach is that it changes the semantic of the callback to result_*or*_measure which is something the spi variant cannot implement. With the current use of the function this is fine, however if at some time in the future we implement a bulk conversion shortcut this hurts.

So I suggest to do:

---->8----
From: Uwe Kleine-König <[email protected]>
Date: Fri, 4 Jun 2021 08:02:44 +0200
Subject: [PATCH] iio: ltc2497: Fix reading conversion results

After the result of the previous conversion is read the chip automatically starts a new conversion and doesn't accept new i2c transfers until this conversion is completed which makes the function return failure.

So add an early return iff the programming of the new address isn't needed. Note this will not fix the problem in general, but all cases that are currently used. Once this changes we get the failure back, but this can be addressed when the need arises.

Fixes 69548b7c2c4f ("iio: adc: ltc2497: split protocol independent part in a

separate module ")

---
 drivers/iio/adc/ltc2497.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/adc/ltc2497.c +0 -0
@@ -41,6 +41,19 @@ static int ltc2497_result_and_measure(struct
 ltc2497core_driverdata *ddata,
 		}
 
Quoted 14 lines
Quoted 11 lines
drivers/iio/adc/ltc2497.c +3 -0
 		*val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+
+		/*
+		 * The part started a new conversion at the end of the above

i2c

drivers/iio/adc/ltc2497.c +5 -0
+		 * transfer, so if the address didn't change since the last call
+		 * everything is fine and we can return early.
+		 * If not (which should only happen when some sort of bulk
+		 * conversion is implemented) we have to program the new
+		 * address. Note that this probably fails as the conversion

that

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 54

Quoted 3 lines
drivers/iio/adc/ltc2497.c +3 -0
+		 * was triggered above is like not complete yet and the two
+		 * operations have to be done in a single transfer.
+		 */

I'm a little confused by this comment. It seems to say it will fail if we ever do have a different address? That doesn't seem very helpful...

J

Quoted 23 lines
Quoted 9 lines
drivers/iio/adc/ltc2497.c +2 -0
+		if (ddata->addr_prev == address)
+			return 0;
 	}
 
 	ret = i2c_smbus_write_byte(st->client,

Compared to Meng Li's patch this doesn't introduce reporting of bogus conversion results once we implement bulk conversion.

Ok!
Understand.
I agree you to push patch to upstream and it is more reasonable that the original author to fix this issue.

Thanks,
Limeng

Quoted 6 lines

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | https://www.pengutronix.de/ |

#6 lore raw
Quoted 131 lines · Jonathan Cameron
Quoted 128 lines · Meng
Quoted 128 lines

-----Original Message-----
From: Uwe Kleine-König <[email protected]>
Sent: Friday, June 4, 2021 2:13 PM
To: Li, Meng <[email protected]>
Cc: Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

Hello,

Quoted 19 lines · Meng
Quoted 19 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Friday, June 4, 2021 12:20 AM
To: Li, Meng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

[Please note: This e-mail is from an EXTERNAL e-mail address]

Quoted 2 lines · On Tue, 1 Jun 2021 17:28:05 +0800 [email protected] wrote:

From: Meng Li <[email protected]>

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines

When read adc conversion value with below command:
cat /sys/.../iio:device0/in_voltage0-voltage1_raw
There is an error reported as below:
ltc2497 0-0014: i2c transfer failed: -EREMOTEIO This i2c transfer issue is introduced by commit 69548b7c2c4f ("iio:

Quoted 8 lines
Quoted 8 lines

adc: ltc2497: split protocol independent part in a separate module"). When extract the common code into ltc2497-core.c, it change the code logic of function ltc2497core_read(). With wrong reading sequence, the action of enable adc channel is sent to chip again during adc channel is in conversion status. In this way, there is no ack from chip, and then cause i2c transfer failed.
In order to keep the code logic is the same with original ideal, it is need to return direct after reading the adc conversion value.

As background about the choice of the .result_and_measure callback:
A difference between the ltc2497 (i2c) and ltc2496 (spi) is that for the latter reading the result of the last conversion and starting a new is a single bus operation and the one cannot be done without the other.

Quoted 11 lines
Quoted 11 lines
Quoted 5 lines

v2:
According to ltc2497 datasheet, the max value of conversion time is
149.9 ms. So, add 20% to the 150msecs so that there is enough time for data conversion.

Version change logs go below the --- as we don't want to preserve them forever in the git history.

I may have lost track of the discussion, but I thought the idea was that perhaps the longer time period would remove the need for the early

return?

Quoted 4 lines
Quoted 1 line

No!
I think the ret is essential.

I'd like to understand why. Currently ltc2497core_read() looks as follows (simplified by dropping error handling, and unrolling the result_and_measure callback for the i2c case):

	ltc2497core_wait_conv()

	// result_and_measure(address, NULL)
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

	msleep_interruptible(LTC2497_CONVERSION_TIME_MS)

	// result_and_measure(address, val);
	i2c_master_recv(client, &buf, 3);
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

With the early return you suggest to introduce with your patch you save the last i2c_smbus_write_byte call. The data sheet indeed claims to start a new conversion at the stop condition.

So either the reading of the conversion result and programming of the (maybe) new address has to be done in a single i2c transfer, or we have to do something like your patch.

What I don't like about your approach is that it changes the semantic of the callback to result_*or*_measure which is something the spi variant cannot implement. With the current use of the function this is fine, however if at some time in the future we implement a bulk conversion shortcut this hurts.

So I suggest to do:

---->8----
From: Uwe Kleine-König <[email protected]>
Date: Fri, 4 Jun 2021 08:02:44 +0200
Subject: [PATCH] iio: ltc2497: Fix reading conversion results

After the result of the previous conversion is read the chip automatically starts a new conversion and doesn't accept new i2c transfers until this conversion is completed which makes the function return failure.

So add an early return iff the programming of the new address isn't needed. Note this will not fix the problem in general, but all cases that are currently used. Once this changes we get the failure back, but this can be addressed when the need arises.

Fixes 69548b7c2c4f ("iio: adc: ltc2497: split protocol independent part in a

separate module ")

---
 drivers/iio/adc/ltc2497.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/adc/ltc2497.c +0 -0
@@ -41,6 +41,19 @@ static int ltc2497_result_and_measure(struct
 ltc2497core_driverdata *ddata,
 		}
 
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/adc/ltc2497.c +3 -0
 		*val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+
+		/*
+		 * The part started a new conversion at the end of the above i2c
Quoted 8 lines

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 54

Quoted 8 lines
drivers/iio/adc/ltc2497.c +8 -0
+		 * transfer, so if the address didn't change since the last call
+		 * everything is fine and we can return early.
+		 * If not (which should only happen when some sort of bulk
+		 * conversion is implemented) we have to program the new
+		 * address. Note that this probably fails as the conversion that
+		 * was triggered above is like not complete yet and the two
+		 * operations have to be done in a single transfer.
+		 */

I'm a little confused by this comment. It seems to say it will fail if we ever do have a different address? That doesn't seem very helpful...

It's not a real problem in the sense that it triggers today. If you want to read out (say) the channels 1, 5, 6 and 7, you could do:

start conversion for channel 1
wait for the conversion to complete
read out conversion for channel 1 and start channel 5 wait for the conversion to complete
read out conversion for channel 5 and start channel 6 wait for the conversion to complete
read out conversion for channel 6 and start channel 7 wait for the conversion to complete
read out conversion for channel 7

With this procedure the step "read out conversion for channel X and start channel Y" has to (and can) be done in a single transfer. But the status quo is, that when these channels are to be read the following happens:

start conversion for channel 1
wait for the conversion to complete
read out conversion for channel 1 and (implicitly) start another conversion for channel 1

wait for the conversion to complete

start conversion for channel 5
wait for the conversion to complete
read out conversion for channel 5 and (implicitly) start another conversion for channel 5

wait for the conversion to complete

...

and ltc2497_result_and_measure is well suited to handle this.

So maybe reword the comment to:

The part started a new conversion at the end of the above i2c transfer. With the current implementation of how reading is implemented in ltc2497core it never happens that this new conversion should be done for a different channel which would require writing a new channel address. (Actually writing such a new address requires more effort, either another delay must be added or the now two transfers must be combined into a single one.)

So check the assumption that the channel really didn't change and then return early which does the right thing today.

?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#7 lore raw
Quoted 174 lines

-----Original Message-----
From: Uwe Kleine-König <[email protected]>
Sent: Friday, June 4, 2021 4:53 PM
To: Jonathan Cameron <[email protected]>
Cc: Li, Meng <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

Quoted 33 lines · Jonathan Cameron
Quoted 30 lines · Meng
Quoted 30 lines

-----Original Message-----
From: Uwe Kleine-König <[email protected]>
Sent: Friday, June 4, 2021 2:13 PM
To: Li, Meng <[email protected]>
Cc: Jonathan Cameron <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

Hello,

Quoted 17 lines · Meng
Quoted 17 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Friday, June 4, 2021 12:20 AM
To: Li, Meng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] driver: adc: ltc2497: return directly after reading the adc conversion value

[Please note: This e-mail is from an EXTERNAL e-mail address]

On Tue, 1 Jun 2021 17:28:05 +0800 [email protected] wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

From: Meng Li <[email protected]>

When read adc conversion value with below command:
cat /sys/.../iio:device0/in_voltage0-voltage1_raw There is an error reported as below:
ltc2497 0-0014: i2c transfer failed: -EREMOTEIO This i2c transfer issue is introduced by commit 69548b7c2c4f ("iio:
adc: ltc2497: split protocol independent part in a separate

module").

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

When extract the common code into ltc2497-core.c, it change the code logic of function ltc2497core_read(). With wrong reading sequence, the action of enable adc channel is sent to chip again during adc channel is in conversion status. In this way, there is no ack from chip, and then cause i2c transfer

failed.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In order to keep the code logic is the same with original ideal, it is need to return direct after reading the adc conversion

value.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

As background about the choice of the .result_and_measure callback:
A difference between the ltc2497 (i2c) and ltc2496 (spi) is that for the latter reading the result of the last conversion and starting a new is a single bus operation and the one cannot be done

without the other.

Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

v2:
According to ltc2497 datasheet, the max value of conversion time is
149.9 ms. So, add 20% to the 150msecs so that there is enough time for data conversion.

Quoted 6 lines
Quoted 6 lines

Version change logs go below the --- as we don't want to preserve them forever in the git history.

I may have lost track of the discussion, but I thought the idea was that perhaps the longer time period would remove the need for the early

return?

Quoted 4 lines
Quoted 1 line

No!
I think the ret is essential.

I'd like to understand why. Currently ltc2497core_read() looks as follows (simplified by dropping error handling, and unrolling the result_and_measure callback for the i2c case):

	ltc2497core_wait_conv()

	// result_and_measure(address, NULL)
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

	msleep_interruptible(LTC2497_CONVERSION_TIME_MS)

	// result_and_measure(address, val);
	i2c_master_recv(client, &buf, 3);
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

With the early return you suggest to introduce with your patch you save the last i2c_smbus_write_byte call. The data sheet indeed claims to start a new conversion at the stop condition.

So either the reading of the conversion result and programming of the
(maybe) new address has to be done in a single i2c transfer, or we have to do something like your patch.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

What I don't like about your approach is that it changes the semantic of the callback to result_*or*_measure which is something the spi variant cannot implement. With the current use of the function this is fine, however if at some time in the future we

implement a bulk conversion shortcut this hurts.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

So I suggest to do:

---->8----
From: Uwe Kleine-König <[email protected]>
Date: Fri, 4 Jun 2021 08:02:44 +0200
Subject: [PATCH] iio: ltc2497: Fix reading conversion results

After the result of the previous conversion is read the chip automatically starts a new conversion and doesn't accept new i2c transfers until this conversion is completed which makes the function

return failure.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

So add an early return iff the programming of the new address isn't

needed.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

Note this will not fix the problem in general, but all cases that are currently used. Once this changes we get the failure back, but this can be addressed when the need arises.

Fixes 69548b7c2c4f ("iio: adc: ltc2497: split protocol

independent part in a separate module ")

---
 drivers/iio/adc/ltc2497.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/adc/ltc2497.c +0 -0
@@ -41,6 +41,19 @@ static int ltc2497_result_and_measure(struct
 ltc2497core_driverdata *ddata,
 		}
 
Quoted 4 lines
Quoted 4 lines

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,

Quoted 4 lines
drivers/iio/adc/ltc2497.c +3 -0
 		*val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+
+		/*
+		 * The part started a new conversion at the end of the above

i2c

Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,

Quoted 5 lines
drivers/iio/adc/ltc2497.c +5 -0
+		 * transfer, so if the address didn't change since the last call
+		 * everything is fine and we can return early.
+		 * If not (which should only happen when some sort of bulk
+		 * conversion is implemented) we have to program the new
+		 * address. Note that this probably fails as the conversion

that

Quoted 6 lines
Quoted 3 lines

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 54

Quoted 3 lines
drivers/iio/adc/ltc2497.c +3 -0
+		 * was triggered above is like not complete yet and the two
+		 * operations have to be done in a single transfer.
+		 */

I'm a little confused by this comment. It seems to say it will fail if we ever do have a different address? That doesn't seem very helpful...

It's not a real problem in the sense that it triggers today. If you want to read out (say) the channels 1, 5, 6 and 7, you could do:

Quoted 10 lines

start conversion for channel 1
wait for the conversion to complete
read out conversion for channel 1 and start channel 5 wait for the conversion to complete
read out conversion for channel 5 and start channel 6 wait for the conversion to complete
read out conversion for channel 6 and start channel 7 wait for the conversion to complete
read out conversion for channel 7

Have you tested above case on real hardware? Or only a inference based on data sheet?

Thanks.
Limeng

Quoted 45 lines

With this procedure the step "read out conversion for channel X and start channel Y" has to (and can) be done in a single transfer. But the status quo is, that when these channels are to be read the following
happens:

start conversion for channel 1
wait for the conversion to complete
read out conversion for channel 1 and (implicitly) start another
conversion for channel 1

wait for the conversion to complete

start conversion for channel 5
wait for the conversion to complete
read out conversion for channel 5 and (implicitly) start another
conversion for channel 5

	wait for the conversion to complete

	...

and ltc2497_result_and_measure is well suited to handle this.

So maybe reword the comment to:

The part started a new conversion at the end of the above i2c transfer. With the current implementation of how reading is implemented in ltc2497core it never happens that this new conversion should be done for a different channel which would require writing a new channel address. (Actually writing such a new address requires more effort, either another delay must be added or the now two transfers must be combined into a single one.)

So check the assumption that the channel really didn't change and then return early which does the right thing today.

?

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | https://www.pengutronix.de/ |

#8 lore raw
Quoted 97 lines · Meng
Quoted 87 lines · Jonathan Cameron
Quoted 67 lines
Quoted 65 lines · Meng
Quoted 65 lines · Meng
Quoted 1 line

I think the ret is essential.

I'd like to understand why. Currently ltc2497core_read() looks as follows (simplified by dropping error handling, and unrolling the result_and_measure callback for the i2c case):

	ltc2497core_wait_conv()

	// result_and_measure(address, NULL)
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

	msleep_interruptible(LTC2497_CONVERSION_TIME_MS)

	// result_and_measure(address, val);
	i2c_master_recv(client, &buf, 3);
	i2c_smbus_write_byte(client, LTC2497_ENABLE | address);

With the early return you suggest to introduce with your patch you save the last i2c_smbus_write_byte call. The data sheet indeed claims to start a new conversion at the stop condition.

So either the reading of the conversion result and programming of the
(maybe) new address has to be done in a single i2c transfer, or we have to do something like your patch.

What I don't like about your approach is that it changes the semantic of the callback to result_*or*_measure which is something the spi variant cannot implement. With the current use of the function this is fine, however if at some time in the future we implement a bulk conversion shortcut this hurts.

So I suggest to do:

---->8----
From: Uwe Kleine-König <[email protected]>
Date: Fri, 4 Jun 2021 08:02:44 +0200
Subject: [PATCH] iio: ltc2497: Fix reading conversion results

After the result of the previous conversion is read the chip automatically starts a new conversion and doesn't accept new i2c transfers until this conversion is completed which makes the function return failure.

So add an early return iff the programming of the new address isn't needed. Note this will not fix the problem in general, but all cases that are currently used. Once this changes we get the failure back, but this can be addressed when the need arises.

Fixes 69548b7c2c4f ("iio: adc: ltc2497: split protocol

independent part in a separate module ")

---
 drivers/iio/adc/ltc2497.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/adc/ltc2497.c +0 -0
@@ -41,6 +41,19 @@ static int ltc2497_result_and_measure(struct
 ltc2497core_driverdata *ddata,
 		}
 
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/adc/ltc2497.c +3 -0
 		*val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+
+		/*
+		 * The part started a new conversion at the end of the above i2c
Quoted 8 lines

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 54

Quoted 8 lines
drivers/iio/adc/ltc2497.c +8 -0
+		 * transfer, so if the address didn't change since the last call
+		 * everything is fine and we can return early.
+		 * If not (which should only happen when some sort of bulk
+		 * conversion is implemented) we have to program the new
+		 * address. Note that this probably fails as the conversion that
+		 * was triggered above is like not complete yet and the two
+		 * operations have to be done in a single transfer.
+		 */

I'm a little confused by this comment. It seems to say it will fail if we ever do have a different address? That doesn't seem very helpful...

It's not a real problem in the sense that it triggers today. If you want to read out (say) the channels 1, 5, 6 and 7, you could do:

Quoted 9 lines

start conversion for channel 1
wait for the conversion to complete
read out conversion for channel 1 and start channel 5 wait for the conversion to complete
read out conversion for channel 5 and start channel 6 wait for the conversion to complete
read out conversion for channel 6 and start channel 7 wait for the conversion to complete
read out conversion for channel 7

Quoted 1 line

Have you tested above case on real hardware? Or only a inference based on data sheet?

I never had the ltc2497 in my hands, but the customer I created my patches for uses the ltc2496 (i.e. its spi variant) and did the above from userspace using spidev.

And I'm confident enough to claim that if your patch fixes your problem, then mine does it, too.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |