Skip to content

Commit 3f1093d

Browse files
angelocjic23
authored andcommitted
iio: adc: mcp3422: fix locking scope
Locking should be held for the entire reading sequence involving setting the channel, waiting for the channel switch and reading from the channel. If not, reading from a channel can result mixing with the reading from another channel. Fixes: 07914c8 ("iio: adc: Add driver for Microchip MCP3422/3/4 high resolution ADC") Signed-off-by: Angelo Compagnucci <angelo.compagnucci@gmail.com> Link: https://lore.kernel.org/r/20200819075525.1395248-1-angelo.compagnucci@gmail.com Cc: <Stable@vger.kernel.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent d53bfdf commit 3f1093d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

drivers/iio/adc/mcp3422.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,12 @@ static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig)
9696
{
9797
int ret;
9898

99-
mutex_lock(&adc->lock);
100-
10199
ret = i2c_master_send(adc->i2c, &newconfig, 1);
102100
if (ret > 0) {
103101
adc->config = newconfig;
104102
ret = 0;
105103
}
106104

107-
mutex_unlock(&adc->lock);
108-
109105
return ret;
110106
}
111107

@@ -138,6 +134,8 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
138134
u8 config;
139135
u8 req_channel = channel->channel;
140136

137+
mutex_lock(&adc->lock);
138+
141139
if (req_channel != MCP3422_CHANNEL(adc->config)) {
142140
config = adc->config;
143141
config &= ~MCP3422_CHANNEL_MASK;
@@ -150,7 +148,11 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
150148
msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]);
151149
}
152150

153-
return mcp3422_read(adc, value, &config);
151+
ret = mcp3422_read(adc, value, &config);
152+
153+
mutex_unlock(&adc->lock);
154+
155+
return ret;
154156
}
155157

156158
static int mcp3422_read_raw(struct iio_dev *iio,

0 commit comments

Comments
 (0)