Skip to content

Commit 18215da

Browse files
l1kgregkh
authored andcommitted
iio: adc: mcp320x: Fix readout of negative voltages
commit e6f4794371ee7cce1339e7ca9542f1e703c5f84a upstream. Commit f686a36 ("iio: adc: mcp320x: Add support for mcp3301") returns a signed voltage from mcp320x_adc_conversion() but neglects that the caller interprets a negative return value as failure. Only mcp3301 (and the upcoming mcp3550/1/3) is affected as the other chips are incapable of measuring negative voltages. Fix and while at it, add mcp3301 to the list of supported chips at the top of the file. Fixes: f686a36 ("iio: adc: mcp320x: Add support for mcp3301") Cc: Andrea Galbusera <gizero@gmail.com> Signed-off-by: Lukas Wunner <lukas@wunner.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f2f68ec commit 18215da

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

drivers/iio/adc/mcp320x.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* MCP3204
1818
* MCP3208
1919
* ------------
20+
* 13 bit converter
21+
* MCP3301
2022
*
2123
* Datasheet can be found here:
2224
* http://ww1.microchip.com/downloads/en/DeviceDoc/21293C.pdf mcp3001
@@ -96,7 +98,7 @@ static int mcp320x_channel_to_tx_data(int device_index,
9698
}
9799

98100
static int mcp320x_adc_conversion(struct mcp320x *adc, u8 channel,
99-
bool differential, int device_index)
101+
bool differential, int device_index, int *val)
100102
{
101103
int ret;
102104

@@ -117,19 +119,25 @@ static int mcp320x_adc_conversion(struct mcp320x *adc, u8 channel,
117119

118120
switch (device_index) {
119121
case mcp3001:
120-
return (adc->rx_buf[0] << 5 | adc->rx_buf[1] >> 3);
122+
*val = (adc->rx_buf[0] << 5 | adc->rx_buf[1] >> 3);
123+
return 0;
121124
case mcp3002:
122125
case mcp3004:
123126
case mcp3008:
124-
return (adc->rx_buf[0] << 2 | adc->rx_buf[1] >> 6);
127+
*val = (adc->rx_buf[0] << 2 | adc->rx_buf[1] >> 6);
128+
return 0;
125129
case mcp3201:
126-
return (adc->rx_buf[0] << 7 | adc->rx_buf[1] >> 1);
130+
*val = (adc->rx_buf[0] << 7 | adc->rx_buf[1] >> 1);
131+
return 0;
127132
case mcp3202:
128133
case mcp3204:
129134
case mcp3208:
130-
return (adc->rx_buf[0] << 4 | adc->rx_buf[1] >> 4);
135+
*val = (adc->rx_buf[0] << 4 | adc->rx_buf[1] >> 4);
136+
return 0;
131137
case mcp3301:
132-
return sign_extend32((adc->rx_buf[0] & 0x1f) << 8 | adc->rx_buf[1], 12);
138+
*val = sign_extend32((adc->rx_buf[0] & 0x1f) << 8
139+
| adc->rx_buf[1], 12);
140+
return 0;
133141
default:
134142
return -EINVAL;
135143
}
@@ -150,12 +158,10 @@ static int mcp320x_read_raw(struct iio_dev *indio_dev,
150158
switch (mask) {
151159
case IIO_CHAN_INFO_RAW:
152160
ret = mcp320x_adc_conversion(adc, channel->address,
153-
channel->differential, device_index);
154-
161+
channel->differential, device_index, val);
155162
if (ret < 0)
156163
goto out;
157164

158-
*val = ret;
159165
ret = IIO_VAL_INT;
160166
break;
161167

0 commit comments

Comments
 (0)