Commit 3f1093d8 authored by Angelo Compagnucci's avatar Angelo Compagnucci Committed by Jonathan Cameron

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: 07914c84 ("iio: adc: Add driver for Microchip MCP3422/3/4 high resolution ADC")
Signed-off-by: default avatarAngelo 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: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent d53bfdf2
...@@ -96,16 +96,12 @@ static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig) ...@@ -96,16 +96,12 @@ static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig)
{ {
int ret; int ret;
mutex_lock(&adc->lock);
ret = i2c_master_send(adc->i2c, &newconfig, 1); ret = i2c_master_send(adc->i2c, &newconfig, 1);
if (ret > 0) { if (ret > 0) {
adc->config = newconfig; adc->config = newconfig;
ret = 0; ret = 0;
} }
mutex_unlock(&adc->lock);
return ret; return ret;
} }
...@@ -138,6 +134,8 @@ static int mcp3422_read_channel(struct mcp3422 *adc, ...@@ -138,6 +134,8 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
u8 config; u8 config;
u8 req_channel = channel->channel; u8 req_channel = channel->channel;
mutex_lock(&adc->lock);
if (req_channel != MCP3422_CHANNEL(adc->config)) { if (req_channel != MCP3422_CHANNEL(adc->config)) {
config = adc->config; config = adc->config;
config &= ~MCP3422_CHANNEL_MASK; config &= ~MCP3422_CHANNEL_MASK;
...@@ -150,7 +148,11 @@ static int mcp3422_read_channel(struct mcp3422 *adc, ...@@ -150,7 +148,11 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]); msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]);
} }
return mcp3422_read(adc, value, &config); ret = mcp3422_read(adc, value, &config);
mutex_unlock(&adc->lock);
return ret;
} }
static int mcp3422_read_raw(struct iio_dev *iio, static int mcp3422_read_raw(struct iio_dev *iio,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment