Skip to content

Commit df1d80a

Browse files
larsclausenJonathan Cameron
authored andcommitted
iio: ad_sigma_delta: Properly handle SPI bus locking vs CS assertion
For devices from the SigmaDelta family we need to keep CS low when doing a conversion, since the device will use the MISO line as a interrupt to indicate that the conversion is complete. This is why the driver locks the SPI bus and when the SPI bus is locked keeps as long as a conversion is going on. The current implementation gets one small detail wrong though. CS is only de-asserted after the SPI bus is unlocked. This means it is possible for a different SPI device on the same bus to send a message which would be wrongfully be addressed to the SigmaDelta device as well. Make sure that the last SPI transfer that is done while holding the SPI bus lock de-asserts the CS signal. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Alexandru Ardelean <Alexandru.Ardelean@analog.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 6ec417d commit df1d80a

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎drivers/iio/adc/ad_sigma_delta.c‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
6262
struct spi_transfer t = {
6363
.tx_buf = data,
6464
.len = size + 1,
65-
.cs_change = sigma_delta->bus_locked,
65+
.cs_change = sigma_delta->keep_cs_asserted,
6666
};
6767
struct spi_message m;
6868
int ret;
@@ -217,6 +217,7 @@ static int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
217217

218218
spi_bus_lock(sigma_delta->spi->master);
219219
sigma_delta->bus_locked = true;
220+
sigma_delta->keep_cs_asserted = true;
220221
reinit_completion(&sigma_delta->completion);
221222

222223
ret = ad_sigma_delta_set_mode(sigma_delta, mode);
@@ -234,9 +235,10 @@ static int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
234235
ret = 0;
235236
}
236237
out:
238+
sigma_delta->keep_cs_asserted = false;
239+
ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
237240
sigma_delta->bus_locked = false;
238241
spi_bus_unlock(sigma_delta->spi->master);
239-
ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
240242

241243
return ret;
242244
}
@@ -289,6 +291,7 @@ int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
289291

290292
spi_bus_lock(sigma_delta->spi->master);
291293
sigma_delta->bus_locked = true;
294+
sigma_delta->keep_cs_asserted = true;
292295
reinit_completion(&sigma_delta->completion);
293296

294297
ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
@@ -298,9 +301,6 @@ int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
298301
ret = wait_for_completion_interruptible_timeout(
299302
&sigma_delta->completion, HZ);
300303

301-
sigma_delta->bus_locked = false;
302-
spi_bus_unlock(sigma_delta->spi->master);
303-
304304
if (ret == 0)
305305
ret = -EIO;
306306
if (ret < 0)
@@ -321,7 +321,10 @@ int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
321321
sigma_delta->irq_dis = true;
322322
}
323323

324+
sigma_delta->keep_cs_asserted = false;
324325
ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
326+
sigma_delta->bus_locked = false;
327+
spi_bus_unlock(sigma_delta->spi->master);
325328
mutex_unlock(&indio_dev->mlock);
326329

327330
if (ret)
@@ -358,6 +361,8 @@ static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
358361

359362
spi_bus_lock(sigma_delta->spi->master);
360363
sigma_delta->bus_locked = true;
364+
sigma_delta->keep_cs_asserted = true;
365+
361366
ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
362367
if (ret)
363368
goto err_unlock;
@@ -386,6 +391,7 @@ static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
386391
sigma_delta->irq_dis = true;
387392
}
388393

394+
sigma_delta->keep_cs_asserted = false;
389395
ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
390396

391397
sigma_delta->bus_locked = false;

‎include/linux/iio/adc/ad_sigma_delta.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct ad_sigma_delta {
6969
bool irq_dis;
7070

7171
bool bus_locked;
72+
bool keep_cs_asserted;
7273

7374
uint8_t comm;
7475

0 commit comments

Comments
 (0)