Skip to content

Commit e2042d2

Browse files
Olivier Moysanjic23
authored andcommitted
iio: adc: stm32-adc: fix sleep in atomic context
This commit fixes the following error: "BUG: sleeping function called from invalid context at kernel/irq/chip.c" In DMA mode suppress the trigger irq handler, and make the buffer transfers directly in DMA callback, instead. Fixes: 2763ea0 ("iio: adc: stm32: add optional dma support") Signed-off-by: Olivier Moysan <[email protected]> Acked-by: Fabrice Gasnier <[email protected]> Cc: <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 2853587 commit e2042d2

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

drivers/iio/adc/stm32-adc.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,8 +1418,30 @@ static unsigned int stm32_adc_dma_residue(struct stm32_adc *adc)
14181418
static void stm32_adc_dma_buffer_done(void *data)
14191419
{
14201420
struct iio_dev *indio_dev = data;
1421+
struct stm32_adc *adc = iio_priv(indio_dev);
1422+
int residue = stm32_adc_dma_residue(adc);
1423+
1424+
/*
1425+
* In DMA mode the trigger services of IIO are not used
1426+
* (e.g. no call to iio_trigger_poll).
1427+
* Calling irq handler associated to the hardware trigger is not
1428+
* relevant as the conversions have already been done. Data
1429+
* transfers are performed directly in DMA callback instead.
1430+
* This implementation avoids to call trigger irq handler that
1431+
* may sleep, in an atomic context (DMA irq handler context).
1432+
*/
1433+
dev_dbg(&indio_dev->dev, "%s bufi=%d\n", __func__, adc->bufi);
14211434

1422-
iio_trigger_poll_chained(indio_dev->trig);
1435+
while (residue >= indio_dev->scan_bytes) {
1436+
u16 *buffer = (u16 *)&adc->rx_buf[adc->bufi];
1437+
1438+
iio_push_to_buffers(indio_dev, buffer);
1439+
1440+
residue -= indio_dev->scan_bytes;
1441+
adc->bufi += indio_dev->scan_bytes;
1442+
if (adc->bufi >= adc->rx_buf_sz)
1443+
adc->bufi = 0;
1444+
}
14231445
}
14241446

14251447
static int stm32_adc_dma_start(struct iio_dev *indio_dev)
@@ -1845,6 +1867,7 @@ static int stm32_adc_probe(struct platform_device *pdev)
18451867
{
18461868
struct iio_dev *indio_dev;
18471869
struct device *dev = &pdev->dev;
1870+
irqreturn_t (*handler)(int irq, void *p) = NULL;
18481871
struct stm32_adc *adc;
18491872
int ret;
18501873

@@ -1911,9 +1934,11 @@ static int stm32_adc_probe(struct platform_device *pdev)
19111934
if (ret < 0)
19121935
return ret;
19131936

1937+
if (!adc->dma_chan)
1938+
handler = &stm32_adc_trigger_handler;
1939+
19141940
ret = iio_triggered_buffer_setup(indio_dev,
1915-
&iio_pollfunc_store_time,
1916-
&stm32_adc_trigger_handler,
1941+
&iio_pollfunc_store_time, handler,
19171942
&stm32_adc_buffer_setup_ops);
19181943
if (ret) {
19191944
dev_err(&pdev->dev, "buffer setup failed\n");

0 commit comments

Comments
 (0)