Skip to contentSkip to conversation

Bugs in dps310 Linux driver

DISCUSSION Andres Heinloo 2 participants 9 messages Mar 3, 2023 – Mar 19, 2023 IIO

lore
Share & tools
lore
#0 lore raw

Hello,

I've been struggling with the dps310 driver, which gives incorrect pressure values and in particular different values than manufacturers code (https://github.com/Infineon/RaspberryPi_DPS).

I think I've found where the problem is. Firstly, there is a mistake in bit numbering at https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L51

According to datasheet, correct is:

#define  DPS310_INT_HL          BIT(7)
#define  DPS310_TMP_SHIFT_EN    BIT(3)
#define  DPS310_PRS_SHIFT_EN    BIT(2)
#define  DPS310_FIFO_EN         BIT(1)
#define  DPS310_SPI_EN          BIT(0)

Eg., the current code is using wrong bit (4) for DPS310_PRS_SHIFT_EN, which means that pressure shift is never enabled.

Secondly, there is a problem with overflows starting at https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L654

Since p is a 24-bit value,

nums[3] = p * p * p * (s64)data->c30;

can and does overflow.

Second overflow problem is at https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L684

In fact, I don't understand why 1000000000LL is needed. Since only 7 values are summed, using 10LL should give the same precision.

Best regards,
Andres

maintainer
#1 lore raw
Quoted 12 lines · On Fri, 03 Mar 2023 12:10:00 +0100 "Andres Heinloo" <[email protected]>…

Hello,

I've been struggling with the dps310 driver, which gives incorrect pressure values and in particular different values than manufacturers code (https://github.com/Infineon/RaspberryPi_DPS).

I think I've found where the problem is. Firstly, there is a mistake in bit numbering at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L51

According to datasheet, correct is:

Quoted 8 lines
#define  DPS310_INT_HL          BIT(7)
#define  DPS310_TMP_SHIFT_EN    BIT(3)
#define  DPS310_PRS_SHIFT_EN    BIT(2)
#define  DPS310_FIFO_EN         BIT(1)
#define  DPS310_SPI_EN          BIT(0)

Eg., the current code is using wrong bit (4) for DPS310_PRS_SHIFT_EN, which means that pressure shift is never enabled.

Checking the datasheet, seems like you are right.
https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
Section 7:

Though that's not the only bit that is wrong. Looks like FIFO enable is as well. So any fix should deal with that as well.

The differences between the register map and the datasheet I'm looking at make me think that perhaps the driver was developed against a prototype part. The registers are in a different order for starters with the B0, B1 and B2 sets in reverse order. Any fix patch should tidy that up as well.

Quoted 9 lines

Secondly, there is a problem with overflows starting at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L654

Since p is a 24-bit value,

nums[3] = p * p * p * (s64)data->c30;

can and does overflow.

Makes sense, though I can't immediately see a good solution as we need to maintain the remainder part.

Quoted 6 lines

Second overflow problem is at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L684

In fact, I don't understand why 1000000000LL is needed. Since only 7 values are summed, using 10LL should give the same precision.

Whilst the existing value seems large - I'm not great with precision calcs so could you lay out why 10LL is sufficient?

Quoted 3 lines

Best regards,
Andres

#2 lore raw
Quoted 17 lines · On Sat, 4 Mar 2023 17:06:20 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 14 lines · On Fri, 03 Mar 2023 12:10:00 +0100 "Andres Heinloo" <[email protected]>…

Hello,

I've been struggling with the dps310 driver, which gives incorrect pressure values and in particular different values than manufacturers code (https://github.com/Infineon/RaspberryPi_DPS).

I think I've found where the problem is. Firstly, there is a mistake in bit numbering at https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L51

According to datasheet, correct is:

#define  DPS310_INT_HL          BIT(7)
#define  DPS310_TMP_SHIFT_EN    BIT(3)
#define  DPS310_PRS_SHIFT_EN    BIT(2)
#define  DPS310_FIFO_EN         BIT(1)
#define  DPS310_SPI_EN          BIT(0)
Quoted 8 lines
Quoted 1 line

Eg., the current code is using wrong bit (4) for DPS310_PRS_SHIFT_EN, which means that pressure shift is never enabled.

Checking the datasheet, seems like you are right.
https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
Section 7:

Though that's not the only bit that is wrong. Looks like FIFO enable is as well. So any fix should deal with that as well.

Yes, DPS310_PRS_SHIFT_EN, DPS310_FIFO_EN, DPS310_SPI_EN are all wrong, but the latter 2 are not used by the driver.

Quoted 4 lines

The differences between the register map and the datasheet I'm looking at make me think that perhaps the driver was developed against a prototype part. The registers are in a different order for starters with the B0, B1 and B2 sets in reverse order. Any fix patch should tidy that up as well.

Yes, but that's just different naming. MSB is called B2 in the datasheet and B0 in the driver.

Quoted 10 lines
Quoted 7 lines

Secondly, there is a problem with overflows starting at https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L654

Since p is a 24-bit value,

nums[3] = p * p * p * (s64)data->c30;

can and does overflow.

Makes sense, though I can't immediately see a good solution as we need to maintain the remainder part.

I don't have a good solution either, but there must be other IIO sensors that have something similar that could be possibly reused.

Quoted 5 lines
Quoted 3 lines

Second overflow problem is at https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L684

In fact, I don't understand why 1000000000LL is needed. Since only 7 values are summed, using 10LL should give the same precision.

Whilst the existing value seems large - I'm not great with precision calcs so could you lay out why 10LL is sufficient?

Unless I overlooked something, the error of integer division (eg., discarding fractional part) is <1. In this case, the results of 7 integer divisions are summed, so the error is <7. When multiplying numerators by 10LL, the error would be <0.7. Which is OK, since we are interested only in the integer part.

Andres

#3 lore raw
Quoted 51 lines · On Sun, 05 Mar 2023 03:05:01 +0100 "Andres Heinloo" <[email protected]>…
Quoted 17 lines · On Sat, 4 Mar 2023 17:06:20 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 14 lines · On Fri, 03 Mar 2023 12:10:00 +0100 "Andres Heinloo" <[email protected]>…

Hello,

I've been struggling with the dps310 driver, which gives incorrect pressure values and in particular different values than manufacturers code (https://github.com/Infineon/RaspberryPi_DPS).

I think I've found where the problem is. Firstly, there is a mistake in bit numbering at https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L51

According to datasheet, correct is:

#define  DPS310_INT_HL          BIT(7)
#define  DPS310_TMP_SHIFT_EN    BIT(3)
#define  DPS310_PRS_SHIFT_EN    BIT(2)
#define  DPS310_FIFO_EN         BIT(1)
#define  DPS310_SPI_EN          BIT(0)
Quoted 6 lines
Quoted 1 line

Eg., the current code is using wrong bit (4) for DPS310_PRS_SHIFT_EN, which means that pressure shift is never enabled.

Checking the datasheet, seems like you are right.
https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
Section 7: Though that's not the only bit that is wrong. Looks like FIFO enable is as well. So any fix should deal with that as well.

Yes, DPS310_PRS_SHIFT_EN, DPS310_FIFO_EN, DPS310_SPI_EN are all wrong, but the latter 2 are not used by the driver.

Quoted 4 lines

The differences between the register map and the datasheet I'm looking at make me think that perhaps the driver was developed against a prototype part. The registers are in a different order for starters with the B0, B1 and B2 sets in reverse order. Any fix patch should tidy that up as well.

Yes, but that's just different naming. MSB is called B2 in the datasheet and B0 in the driver.

Quoted 10 lines
Quoted 7 lines

Secondly, there is a problem with overflows starting at https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L654

Since p is a 24-bit value,

nums[3] = p * p * p * (s64)data->c30;

can and does overflow.

Makes sense, though I can't immediately see a good solution as we need to maintain the remainder part.

I don't have a good solution either, but there must be other IIO sensors that have something similar that could be possibly reused.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Second overflow problem is at https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L684

In fact, I don't understand why 1000000000LL is needed. Since only 7 values are summed, using 10LL should give the same precision.

Whilst the existing value seems large - I'm not great with precision calcs so could you lay out why 10LL is sufficient?

Unless I overlooked something, the error of integer division (eg., discarding fractional part) is <1. In this case, the results of 7 integer divisions are summed, so the error is <7. When multiplying numerators by 10LL, the error would be <0.7. Which is OK, since we are interested only in the integer part.

Unfortunately it seems that there may be more problems with the driver. I've noticed that my device has lost sample rate and precision settings few times. When looking at the code, it seems the settings are not restored when dps310_reset_reinit() is called at https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L438

Apparently I've also ended up with data->timeout_recovery_failed == true at https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L443, but the device worked fine after just reloading the kernel module. This is difficult to reproduce, though.

Andres

maintainer
#4 lore raw
Quoted 84 lines · On Mon, 06 Mar 2023 22:15:15 +0100 "Andres Heinloo" <[email protected]>…
Quoted 76 lines · On Sun, 05 Mar 2023 03:05:01 +0100 "Andres Heinloo" <[email protected]>…
Quoted 25 lines · On Sat, 4 Mar 2023 17:06:20 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 18 lines · On Fri, 03 Mar 2023 12:10:00 +0100 "Andres Heinloo" <[email protected]>…

Hello,

I've been struggling with the dps310 driver, which gives incorrect pressure values and in particular different values than manufacturers code (https://github.com/Infineon/RaspberryPi_DPS).

I think I've found where the problem is. Firstly, there is a mistake in bit numbering at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L51

According to datasheet, correct is:

#define  DPS310_INT_HL          BIT(7)
#define  DPS310_TMP_SHIFT_EN    BIT(3)
#define  DPS310_PRS_SHIFT_EN    BIT(2)
#define  DPS310_FIFO_EN         BIT(1)
#define  DPS310_SPI_EN          BIT(0)
Quoted 3 lines

Eg., the current code is using wrong bit (4) for DPS310_PRS_SHIFT_EN, which means that pressure shift is never enabled.

Quoted 6 lines

Checking the datasheet, seems like you are right.
https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
Section 7:
Though that's not the only bit that is wrong. Looks like FIFO enable is as well.
So any fix should deal with that as well.

Yes, DPS310_PRS_SHIFT_EN, DPS310_FIFO_EN, DPS310_SPI_EN are all wrong, but the latter 2 are not used by the driver.

Quoted 7 lines

The differences between the register map and the datasheet I'm looking at make
me think that perhaps the driver was developed against a prototype part.
The registers are in a different order for starters with the B0, B1 and B2
sets in reverse order. Any fix patch should tidy that up as well.

Yes, but that's just different naming. MSB is called B2 in the datasheet and B0 in the driver.

Quoted 5 lines
Quoted 5 lines

Secondly, there is a problem with overflows starting at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L654

Since p is a 24-bit value,

Quoted 7 lines
Quoted 3 lines

nums[3] = p * p * p * (s64)data->c30;

can and does overflow.

Makes sense, though I can't immediately see a good solution as we need
to maintain the remainder part.

I don't have a good solution either, but there must be other IIO sensors that have something similar that could be possibly reused.

Quoted 8 lines
Quoted 5 lines

Second overflow problem is at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L684

In fact, I don't understand why 1000000000LL is needed. Since only 7 values are summed, using 10LL should give the same precision.

Whilst the existing value seems large - I'm not great with precision calcs so could
you lay out why 10LL is sufficient?

Quoted 5 lines

Unless I overlooked something, the error of integer division (eg., discarding fractional part) is <1. In this case, the results of 7 integer divisions are summed, so the error is <7. When multiplying numerators by 10LL, the error would be <0.7. Which is OK, since we are interested only in the integer part.

Quoted 6 lines

Unfortunately it seems that there may be more problems with the driver. I've noticed that my device has lost sample rate and precision settings few times. When looking at the code, it seems the settings are not restored when dps310_reset_reinit() is called at
https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L438

Agreed. Looks like that stuff is missing. This reset path is pretty horrible in general, so I'm not surprised a few things got missed. Should be easy enough to add a cache of those and and set them again if you want to try that.

Quoted 5 lines

Apparently I've also ended up with data->timeout_recovery_failed == true at
https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L443,
but the device worked fine after just reloading the kernel module. This is difficult to reproduce, though.

Hmm. There are a few things that could cause that. Maybe something running slow, or an intermittent write failure (noisy environment or bad wire / track maybe?)

Quoted 1 line

Andres

#5 lore raw
Quoted 94 lines · Jonathan Cameron
Quoted 84 lines · On Mon, 06 Mar 2023 22:15:15 +0100 "Andres Heinloo" <[email protected]>…
Quoted 76 lines · On Sun, 05 Mar 2023 03:05:01 +0100 "Andres Heinloo" <[email protected]>…
Quoted 25 lines · On Sat, 4 Mar 2023 17:06:20 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 18 lines · On Fri, 03 Mar 2023 12:10:00 +0100 "Andres Heinloo" <[email protected]>…

Hello,

I've been struggling with the dps310 driver, which gives incorrect pressure values and in particular different values than manufacturers code (https://github.com/Infineon/RaspberryPi_DPS).

I think I've found where the problem is. Firstly, there is a mistake in bit numbering at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L51

According to datasheet, correct is:

#define  DPS310_INT_HL          BIT(7)
#define  DPS310_TMP_SHIFT_EN    BIT(3)
#define  DPS310_PRS_SHIFT_EN    BIT(2)
#define  DPS310_FIFO_EN         BIT(1)
#define  DPS310_SPI_EN          BIT(0)
Quoted 3 lines

Eg., the current code is using wrong bit (4) for DPS310_PRS_SHIFT_EN, which means that pressure shift is never enabled.

Quoted 6 lines

Checking the datasheet, seems like you are right.
https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
Section 7:
Though that's not the only bit that is wrong. Looks like FIFO enable is as well.
So any fix should deal with that as well.

Yes, DPS310_PRS_SHIFT_EN, DPS310_FIFO_EN, DPS310_SPI_EN are all wrong, but the latter 2 are not used by the driver.

Quoted 7 lines

The differences between the register map and the datasheet I'm looking at make
me think that perhaps the driver was developed against a prototype part.
The registers are in a different order for starters with the B0, B1 and B2
sets in reverse order. Any fix patch should tidy that up as well.

Yes, but that's just different naming. MSB is called B2 in the datasheet and B0 in the driver.

Quoted 5 lines
Quoted 5 lines

Secondly, there is a problem with overflows starting at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L654

Since p is a 24-bit value,

Quoted 7 lines
Quoted 3 lines

nums[3] = p * p * p * (s64)data->c30;

can and does overflow.

Makes sense, though I can't immediately see a good solution as we need
to maintain the remainder part.

I don't have a good solution either, but there must be other IIO sensors that have something similar that could be possibly reused.

Quoted 8 lines
Quoted 5 lines

Second overflow problem is at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L684

In fact, I don't understand why 1000000000LL is needed. Since only 7 values are summed, using 10LL should give the same precision.

Whilst the existing value seems large - I'm not great with precision calcs so could
you lay out why 10LL is sufficient?

Quoted 5 lines

Unless I overlooked something, the error of integer division (eg., discarding fractional part) is <1. In this case, the results of 7 integer divisions are summed, so the error is <7. When multiplying numerators by 10LL, the error would be <0.7. Which is OK, since we are interested only in the integer part.

Quoted 6 lines

Unfortunately it seems that there may be more problems with the driver. I've noticed that my device has lost sample rate and precision settings few times. When looking at the code, it seems the settings are not restored when dps310_reset_reinit() is called at
https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L438

Quoted 3 lines

Agreed. Looks like that stuff is missing. This reset path is pretty horrible in general, so I'm not surprised a few things got missed. Should be easy enough to add a cache of those and and set them again if you want to try that.

One option would be adding sample_rate and oversampling_ratio to dps310_data struct, but some questions remain.

I think doing a silent reset is not good, because it can cause bad samples. When looking at data, you don't know if there was an actual pressure spike or if it was caused by a reset. I think a better solution would be immediately returning an error (-EIO?) to inform user space about an intermittent failure.

(I suspect that in case of some errors, the IIO subsystem retries internally and hides the error.)

The purpose of timeout_recovery_failed seems questionable, because if you end up with timeout_recovery_failed == true, the only way to recover from timeout is reloading the kernel module.

I'm afraid I'm not able to provide a patch to fix the problems soon.

Quoted 8 lines
Quoted 5 lines

Apparently I've also ended up with data->timeout_recovery_failed == true at
https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L443,
but the device worked fine after just reloading the kernel module. This is difficult to reproduce, though.

Hmm. There are a few things that could cause that. Maybe something running slow, or an intermittent write failure (noisy environment or bad wire / track maybe?)

(Off-topic) Write failures is a different problem that is very annoying. I'm not sure if this could be the case with dps310, but I have an ADXL355 device that is connected via SPI and often register writes fail silently. I have a short cable that is well connected and SPI should be running at max 1Mhz according to .../of_node/spi-max-frequency. I ended up wrapping regmap functions to check register value after each write. I haven't noticed errors when reading registers. (I'm using Raspberry Pi.)

Andres

maintainer
#6 lore raw
Quoted 95 lines · On Mon, 13 Mar 2023 17:36:00 +0100 Andres Heinloo <[email protected]> wrote:
Quoted 94 lines · Jonathan Cameron
Quoted 90 lines · On Mon, 06 Mar 2023 22:15:15 +0100 "Andres Heinloo" <[email protected]>…
Quoted 76 lines · On Sun, 05 Mar 2023 03:05:01 +0100 "Andres Heinloo" <[email protected]>…
Quoted 25 lines · On Sat, 4 Mar 2023 17:06:20 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 18 lines · On Fri, 03 Mar 2023 12:10:00 +0100 "Andres Heinloo" <[email protected]>…

Hello,

I've been struggling with the dps310 driver, which gives incorrect pressure values and in particular different values than manufacturers code (https://github.com/Infineon/RaspberryPi_DPS).

I think I've found where the problem is. Firstly, there is a mistake in bit numbering at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L51

According to datasheet, correct is:

#define  DPS310_INT_HL          BIT(7)
#define  DPS310_TMP_SHIFT_EN    BIT(3)
#define  DPS310_PRS_SHIFT_EN    BIT(2)
#define  DPS310_FIFO_EN         BIT(1)
#define  DPS310_SPI_EN          BIT(0)
Quoted 3 lines

Eg., the current code is using wrong bit (4) for DPS310_PRS_SHIFT_EN, which means that pressure shift is never enabled.

Quoted 6 lines

Checking the datasheet, seems like you are right.
https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
Section 7:
Though that's not the only bit that is wrong. Looks like FIFO enable is as well.
So any fix should deal with that as well.

Yes, DPS310_PRS_SHIFT_EN, DPS310_FIFO_EN, DPS310_SPI_EN are all wrong, but the latter 2 are not used by the driver.

Quoted 7 lines

The differences between the register map and the datasheet I'm looking at make
me think that perhaps the driver was developed against a prototype part.
The registers are in a different order for starters with the B0, B1 and B2
sets in reverse order. Any fix patch should tidy that up as well.

Yes, but that's just different naming. MSB is called B2 in the datasheet and B0 in the driver.

Quoted 5 lines
Quoted 5 lines

Secondly, there is a problem with overflows starting at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L654

Since p is a 24-bit value,

Quoted 7 lines
Quoted 3 lines

nums[3] = p * p * p * (s64)data->c30;

can and does overflow.

Makes sense, though I can't immediately see a good solution as we need
to maintain the remainder part.

I don't have a good solution either, but there must be other IIO sensors that have something similar that could be possibly reused.

Quoted 8 lines
Quoted 5 lines

Second overflow problem is at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L684

In fact, I don't understand why 1000000000LL is needed. Since only 7 values are summed, using 10LL should give the same precision.

Whilst the existing value seems large - I'm not great with precision calcs so could
you lay out why 10LL is sufficient?

Quoted 5 lines

Unless I overlooked something, the error of integer division (eg., discarding fractional part) is <1. In this case, the results of 7 integer divisions are summed, so the error is <7. When multiplying numerators by 10LL, the error would be <0.7. Which is OK, since we are interested only in the integer part.

Unfortunately it seems that there may be more problems with the driver. I've noticed that my device has lost sample rate and precision settings few times. When looking at the code, it seems the settings are not restored when dps310_reset_reinit() is called at
https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L438

Quoted 6 lines
Quoted 3 lines

Agreed. Looks like that stuff is missing. This reset path is pretty horrible in general, so I'm not surprised a few things got missed. Should be easy enough to add a cache of those and and set them again if you want to try that.

One option would be adding sample_rate and oversampling_ratio to dps310_data struct, but some questions remain.

That's what I'd do here.

Quoted 6 lines

I think doing a silent reset is not good, because it can cause bad samples. When looking at data, you don't know if there was an actual pressure spike or if it was caused by a reset. I think a better solution would be immediately returning an error (-EIO?) to inform user space about an intermittent failure.

Userspace may well not retry because for vast majority of devices a failure like this indicates something went very wrong (or bad hardware). So it might well just log the error somewhere and quit. Here we have a device that as I understand it has a reasonably frequent lock up condition so we kind of need to paper over that in the kernel to present what userspace expects.

A rate limited print to the kernel log might be a good idea though so at least there is some record of it happening.

Quoted 3 lines

(I suspect that in case of some errors, the IIO subsystem retries internally and hides the error.)

I can't immediately think of a path were the subsystem does retries. Tend to just pass them up to userspace.

Quoted 4 lines

The purpose of timeout_recovery_failed seems questionable, because if you end up with timeout_recovery_failed == true, the only way to recover from timeout is reloading the kernel module.

In theory if that happens we've concluded the device is dead. So expected fix is reset the board. The fact it recovers after some other sequence means the work around isn't working.

Quoted 2 lines

I'm afraid I'm not able to provide a patch to fix the problems soon.

Understood, we may have to wait on someone with hardware who has the time. The wrong bit definitions are easier so maybe we can fix those up.

Quoted 11 lines
Quoted 10 lines
Quoted 7 lines

Apparently I've also ended up with data->timeout_recovery_failed == true at
https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L443,
but the device worked fine after just reloading the kernel module. This is difficult to reproduce, though.

Hmm. There are a few things that could cause that. Maybe something running slow, or an intermittent write failure (noisy environment or bad wire / track maybe?)

Quoted 7 lines

(Off-topic) Write failures is a different problem that is very annoying. I'm not sure if this could be the case with dps310, but I have an ADXL355 device that is connected via SPI and often register writes fail silently. I have a short cable that is well connected and SPI should be running at max 1Mhz according to .../of_node/spi-max-frequency. I ended up wrapping regmap functions to check register value after each write. I haven't noticed errors when reading registers. (I'm using Raspberry Pi.)

Some of these sensors are very sensitive. I have one or two that basically don't work over cables (including one that only works if I attach an oscilloscope with a long test lead...)

You could perhaps propose an addition to regmap to verify a write so that at least we know it failed.

Jonathan

Quoted 2 lines

Andres

#7 lore raw
Quoted 131 lines · On Sat, 18 Mar 2023 17:09:17 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 116 lines · On Mon, 13 Mar 2023 17:36:00 +0100 Andres Heinloo <[email protected]> wrote:
Quoted 5 lines · Jonathan Cameron
Quoted 2 lines · On Mon, 06 Mar 2023 22:15:15 +0100 "Andres Heinloo" <[email protected]>…

On Sun, 05 Mar 2023 03:05:01 +0100
"Andres Heinloo" <[email protected]> wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines · On Sat, 4 Mar 2023 17:06:20 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 3 lines · On Fri, 03 Mar 2023 12:10:00 +0100 "Andres Heinloo" <[email protected]>…

Hello,

I've been struggling with the dps310 driver, which gives

incorrect

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

pressure values and in particular different values than

manufacturers

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

code (https://github.com/Infineon/RaspberryPi_DPS).

I think I've found where the problem is. Firstly, there is a

mistake

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

in bit numbering at

https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L51

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

According to datasheet, correct is:

#define  DPS310_INT_HL          BIT(7)
#define  DPS310_TMP_SHIFT_EN    BIT(3)
#define  DPS310_PRS_SHIFT_EN    BIT(2)
#define  DPS310_FIFO_EN         BIT(1)
#define  DPS310_SPI_EN          BIT(0)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Eg., the current code is using wrong bit (4) for DPS310_PRS_SHIFT_EN, which means that pressure shift is never enabled.

Checking the datasheet, seems like you are right.

https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Section 7:
Though that's not the only bit that is wrong. Looks like FIFO enable is as well.
So any fix should deal with that as well.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Yes, DPS310_PRS_SHIFT_EN, DPS310_FIFO_EN, DPS310_SPI_EN are all wrong, but the latter 2 are not used by the driver.

Quoted 3 lines

The differences between the register map and the datasheet I'm looking at make
me think that perhaps the driver was developed against a

prototype

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

part.
The registers are in a different order for starters with the

B0, B1

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

and B2
sets in reverse order. Any fix patch should tidy that up as

well.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Yes, but that's just different naming. MSB is called B2 in the datasheet and B0 in the driver.

Quoted 2 lines
Quoted 2 lines

Secondly, there is a problem with overflows starting at

https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L654

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Since p is a 24-bit value,

nums[3] = p * p * p * (s64)data->c30;

can and does overflow.

Makes sense, though I can't immediately see a good solution as

we

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

need
to maintain the remainder part.

I don't have a good solution either, but there must be other IIO sensors that have something similar that could be possibly

reused.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Second overflow problem is at

https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L684

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

In fact, I don't understand why 1000000000LL is needed. Since

only 7

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

values are summed, using 10LL should give the same precision.

Whilst the existing value seems large - I'm not great with precision calcs so could
you lay out why 10LL is sufficient?

Unless I overlooked something, the error of integer division

(eg.,

Quoted 1 line
Quoted 1 line
Quoted 1 line

discarding fractional part) is <1. In this case, the results of

7

Quoted 1 line
Quoted 1 line
Quoted 1 line

integer divisions are summed, so the error is <7. When

multiplying

Quoted 1 line
Quoted 1 line
Quoted 1 line

numerators by 10LL, the error would be <0.7. Which is OK, since

we

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

are interested only in the integer part.

Unfortunately it seems that there may be more problems with the driver. I've noticed that my device has lost sample rate and

precision

Quoted 1 line
Quoted 1 line

settings few times. When looking at the code, it seems the

settings

Quoted 2 lines
Quoted 2 lines

are not restored when dps310_reset_reinit() is called at

https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L438

Quoted 2 lines
Quoted 1 line
Quoted 8 lines
Quoted 1 line

Agreed. Looks like that stuff is missing. This reset path is

pretty horrible

Quoted 1 line

in general, so I'm not surprised a few things got missed. Should

be easy enough

Quoted 1 line

to add a cache of those and and set them again if you want to try

that.

One option would be adding sample_rate and oversampling_ratio to dps310_data struct, but some questions remain.

That's what I'd do here.

Quoted 1 line
Quoted 7 lines
Quoted 1 line

I think doing a silent reset is not good, because it can cause bad samples. When looking at data, you don't know if there was an actual pressure spike or if it was caused by a reset. I think a better solution would be immediately returning an error (-EIO?) to inform user space about an intermittent failure.

Userspace may well not retry because for vast majority of devices a failure like this indicates something went very wrong (or bad hardware). So it might well just log the error somewhere and quit. Here we have a device that as I understand it has a reasonably frequent lock up condition so we kind of need to paper over that in the kernel to present what userspace expects.

I don't have experience with many IIO devices, but for example dht11 has very frequent errors due to unreliable bitbanging protocol and if taking a reading fails, it returns I/O error to userspace and the userspace just retries.

Quoted 8 lines

A rate limited print to the kernel log might be a good idea though so at least there is some record of it happening.

Quoted 2 lines

(I suspect that in case of some errors, the IIO subsystem retries internally and hides the error.)

I can't immediately think of a path were the subsystem does retries. Tend to just pass them up to userspace.

I'm not 100% sure, but when I debugged dps310, I noticed that when -ERANGE is returned at https://github.com/torvalds/linux/blob/a3671bd86a9770e34969522d29bb30a1b66fd88a/drivers/iio/pressure/dps310.c#L688, dps310_calculate_pressure() seems to be executed 2 times internally before -ERANGE is returned to userspace.

Quoted 28 lines
Quoted 1 line

The purpose of timeout_recovery_failed seems questionable, because if you end up with timeout_recovery_failed == true, the only way to recover from timeout is reloading the kernel module.

In theory if that happens we've concluded the device is dead. So expected fix is reset the board. The fact it recovers after some other sequence means the work around isn't working.

Quoted 2 lines

I'm afraid I'm not able to provide a patch to fix the problems soon.

Understood, we may have to wait on someone with hardware who has the time. The wrong bit definitions are easier so maybe we can fix those up.

Quoted 16 lines
Quoted 3 lines
Quoted 3 lines

Apparently I've also ended up with data->timeout_recovery_failed

==

Quoted 2 lines
Quoted 2 lines

true at

https://github.com/torvalds/linux/blob/8ca09d5fa3549d142c2080a72a4c70ce389163cd/drivers/iio/pressure/dps310.c#L443,

Quoted 1 line
Quoted 1 line

but the device worked fine after just reloading the kernel

module.

Quoted 3 lines
Quoted 1 line

This is difficult to reproduce, though.

Hmm. There are a few things that could cause that. Maybe

something running slow, or

Quoted 1 line

an intermittent write failure (noisy environment or bad wire /

track maybe?)

Quoted 8 lines
Quoted 1 line

(Off-topic) Write failures is a different problem that is very annoying. I'm not sure if this could be the case with dps310, but I have an ADXL355 device that is connected via SPI and often register writes fail silently. I have a short cable that is well connected and SPI should be running at max 1Mhz according to .../of_node/spi-max-frequency. I ended up wrapping regmap functions to check register value after each write. I haven't noticed errors when reading registers. (I'm using Raspberry Pi.)

Some of these sensors are very sensitive. I have one or two that basically don't work over cables (including one that only works if I attach an oscilloscope with a long test lead...)

You could perhaps propose an addition to regmap to verify a write so that at least we know it failed.

Actually my wrapper does not only verify, but also retries up to 10 times, because those write failures are really frequent (haven't counted exactly, but I think up to 10% of the writes).

Andres

maintainer
#8 lore raw
In reply to Andres Heinloo
Quoted 48 lines · On Sun, 05 Mar 2023 03:05:01 +0100 "Andres Heinloo" <[email protected]>…
Quoted 26 lines · On Sat, 4 Mar 2023 17:06:20 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 19 lines · On Fri, 03 Mar 2023 12:10:00 +0100 "Andres Heinloo" <[email protected]>…

Hello,

I've been struggling with the dps310 driver, which gives incorrect pressure values and in particular different values than manufacturers
code (https://github.com/Infineon/RaspberryPi_DPS).

I think I've found where the problem is. Firstly, there is a mistake in bit numbering at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L51

According to datasheet, correct is:

#define  DPS310_INT_HL          BIT(7)
#define  DPS310_TMP_SHIFT_EN    BIT(3)
#define  DPS310_PRS_SHIFT_EN    BIT(2)
#define  DPS310_FIFO_EN         BIT(1)
#define  DPS310_SPI_EN          BIT(0)
Quoted 3 lines

Eg., the current code is using wrong bit (4) for
DPS310_PRS_SHIFT_EN,
which means that pressure shift is never enabled.

Quoted 7 lines

Checking the datasheet, seems like you are right.
https://www.infineon.com/dgdl/Infineon-DPS310-DataSheet-v01_02-EN.pdf?fileId=5546d462576f34750157750826c42242
Section 7:

Though that's not the only bit that is wrong. Looks like FIFO enable is as well.
So any fix should deal with that as well.

Yes, DPS310_PRS_SHIFT_EN, DPS310_FIFO_EN, DPS310_SPI_EN are all wrong, but the latter 2 are not used by the driver.

Quoted 7 lines

The differences between the register map and the datasheet I'm looking at make
me think that perhaps the driver was developed against a prototype part.
The registers are in a different order for starters with the B0, B1 and B2
sets in reverse order. Any fix patch should tidy that up as well.

Quoted 2 lines

Yes, but that's just different naming. MSB is called B2 in the datasheet and B0 in the driver.

That would be good to fix though as it will only cause more confusion in the future. Sure that 'fix' should be at the end of any patch set and might wait for the next kernel merge window rather than going in quickly.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Secondly, there is a problem with overflows starting at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L654

Since p is a 24-bit value,

nums[3] = p * p * p * (s64)data->c30;

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

can and does overflow.

Makes sense, though I can't immediately see a good solution as we need
to maintain the remainder part.

I don't have a good solution either, but there must be other IIO sensors that have something similar that could be possibly reused.

I'm not sure I've seen a power of 3 in many calculations.
There may be a similar case in another driver, but I can't immediately point to one.

Quoted 11 lines
Quoted 10 lines
Quoted 7 lines

Second overflow problem is at
https://github.com/torvalds/linux/blob/857f1268a591147f7be7509f249dbb3aba6fc65c/drivers/iio/pressure/dps310.c#L684

In fact, I don't understand why 1000000000LL is needed. Since only 7 values are summed, using 10LL should give the same precision.

Whilst the existing value seems large - I'm not great with precision calcs so could
you lay out why 10LL is sufficient?

Quoted 5 lines

Unless I overlooked something, the error of integer division (eg., discarding fractional part) is <1. In this case, the results of 7 integer divisions are summed, so the error is <7. When multiplying numerators by 10LL, the error would be <0.7. Which is OK, since we are interested only in the integer part.

That works for me as an explanation. Thanks

Jonathan

Quoted 2 lines

Andres