iio: adc: mcp320x: Fix scale of the MCP3301
PATCH 1/1 iio: adc: mcp320x: Fix scale of the MCP3301
mcp320x_read_raw() reports the scale as Vref / 2^resolution: it returns Vref in mV and chip_info->resolution as an IIO_VAL_FRACTIONAL_LOG2 pair, and the IIO core shifts right by the resolution.
The sign bit of the MCP3301 should not be counted toward resolution, so set it to 12.
---
Notes:
This is based on the datasheet (DS21700E) and the driver only. I do not have an MCP3301, so it is untested on hardware. Could someone with one measure a known input against in_voltage_raw * in_voltage_scale?
The scale reported to userspace for the MCP3301 doubles with this patch. I think that is right because the old value contradicts the datasheet (Section 5.3: 610 uV per LSB at 2.5 V, the driver gave 305 uV). I searched lore for mcp3301 and found no earlier discussion of the scale, and the commit that added the part does not say why 13 was chosen.
I did not add Cc: stable; please add it if you think it is warranted.
AI assistance: I used an AI assistant (Claude) to help read the datasheet and the driver and to draft this message. I checked the quoted datasheet lines and the code paths myself. Build-tested (W=1, C=1); not tested on hardware.
drivers/iio/adc/mcp320x.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/mcp320x.c +1 -1
@@ -341,7 +341,7 @@ static const struct mcp320x_chip_info mcp320x_chip_infos[] = { [mcp3301] = { .channels = mcp3201_channels, .num_channels = ARRAY_SIZE(mcp3201_channels), - .resolution =13 + .resolution =12 }, [mcp3550_50] = { .channels = mcp3201_channels,
base-commit: fba506008a5b548ad2708bc2765a039cb00a47ab
Signature · 1 line
2.47.3
Quoted 9 lines · Jakkapong Saksrisuwan
mcp320x_read_raw()reports the scale as Vref / 2^resolution: it returns Vref in mV andchip_info->resolutionas anIIO_VAL_FRACTIONAL_LOG2pair, and the IIO core shifts right by the resolution.The sign bit of the MCP3301 should not be counted toward resolution, so set it to 12.
Fixes f686a36b4b79 ("iio: adc: mcp320x: Add support for mcp3301")
Use Datasheet: tag instead.
...
drivers/iio/adc/mcp320x.c static const struct mcp320x_chip_info mcp320x_chip_infos[] = { · line 344
Quoted 5 lines
drivers/iio/adc/mcp320x.c +1
-1[mcp3301] = { .channels = mcp3201_channels, .num_channels = ARRAY_SIZE(mcp3201_channels), - .resolution =13 + .resolution =12
While at it, add trailing comma. Or may be add a patch that adds it to all chip_info entries.
Quoted 1 line
},
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Jakkapong Saksrisuwan
The sign bit of the MCP3301 should not be counted toward resolution, so set it to 12.