Skip to content

Commit 30fd0e2

Browse files
marcusfolkessongregkh
authored andcommitted
iio: adc: mcp3911: use correct formula for AD conversion
commit 9e2238e upstream. The ADC conversion is actually not rail-to-rail but with a factor 1.5. Make use of this factor when calculating actual voltage. Fixes: 3a89b28 ("iio: adc: add support for mcp3911") Signed-off-by: Marcus Folkesson <marcus.folkesson@gmail.com> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Link: https://lore.kernel.org/r/20220722130726.7627-4-marcus.folkesson@gmail.com Cc: <Stable@vger.kernel.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 89aa443 commit 30fd0e2

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

drivers/iio/adc/mcp3911.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
#define MCP3911_CHANNEL(x) (MCP3911_REG_CHANNEL0 + x * 3)
3939
#define MCP3911_OFFCAL(x) (MCP3911_REG_OFFCAL_CH0 + x * 6)
4040

41-
/* Internal voltage reference in uV */
42-
#define MCP3911_INT_VREF_UV 1200000
41+
/* Internal voltage reference in mV */
42+
#define MCP3911_INT_VREF_MV 1200
4343

4444
#define MCP3911_REG_READ(reg, id) ((((reg) << 1) | ((id) << 5) | (1 << 0)) & 0xff)
4545
#define MCP3911_REG_WRITE(reg, id) ((((reg) << 1) | ((id) << 5) | (0 << 0)) & 0xff)
@@ -137,11 +137,18 @@ static int mcp3911_read_raw(struct iio_dev *indio_dev,
137137

138138
*val = ret / 1000;
139139
} else {
140-
*val = MCP3911_INT_VREF_UV;
140+
*val = MCP3911_INT_VREF_MV;
141141
}
142142

143-
*val2 = 24;
144-
ret = IIO_VAL_FRACTIONAL_LOG2;
143+
/*
144+
* For 24bit Conversion
145+
* Raw = ((Voltage)/(Vref) * 2^23 * Gain * 1.5
146+
* Voltage = Raw * (Vref)/(2^23 * Gain * 1.5)
147+
*/
148+
149+
/* val2 = (2^23 * 1.5) */
150+
*val2 = 12582912;
151+
ret = IIO_VAL_FRACTIONAL;
145152
break;
146153
}
147154

0 commit comments

Comments
 (0)