diff mbox series

iio: adc: stm32-adc: enable timestamping for non-DMA usage

Message ID 20210121180228.30621-1-a.fatoum@pengutronix.de (mailing list archive)
State New, archived
Headers show
Series iio: adc: stm32-adc: enable timestamping for non-DMA usage | expand

Commit Message

Ahmad Fatoum Jan. 21, 2021, 6:02 p.m. UTC
For non-DMA usage, we have an easy way to associate a timestamp with a
sample: iio_pollfunc_store_time stores a timestamp in the primary
trigger IRQ handler and stm32_adc_trigger_handler runs in the IRQ thread
to push out the buffer along with the timestamp.

For this to work, the driver needs to register an IIO_TIMESTAMP channel.
Do this.

For DMA, it's not as easy, because we don't push the buffers out of
stm32_adc_trigger, but out of stm32_adc_dma_buffer_done, which runs in
a tasklet scheduled after a DMA completion.

Preferably, the DMA controller would copy us the timestamp into that buffer
as well. Until this is implemented, restrict timestamping support to
only PIO. For low-frequency sampling, PIO is probably good enough.

Cc: Holger Assmann <has@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/iio/adc/stm32-adc.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)

Comments

Fabrice Gasnier Jan. 22, 2021, 10:23 a.m. UTC | #1
On 1/21/21 7:02 PM, Ahmad Fatoum wrote:
> For non-DMA usage, we have an easy way to associate a timestamp with a
> sample: iio_pollfunc_store_time stores a timestamp in the primary
> trigger IRQ handler and stm32_adc_trigger_handler runs in the IRQ thread
> to push out the buffer along with the timestamp.
>
> For this to work, the driver needs to register an IIO_TIMESTAMP channel.
> Do this.
>
> For DMA, it's not as easy, because we don't push the buffers out of
> stm32_adc_trigger, but out of stm32_adc_dma_buffer_done, which runs in
> a tasklet scheduled after a DMA completion.
>
> Preferably, the DMA controller would copy us the timestamp into that buffer
> as well. Until this is implemented, restrict timestamping support to
> only PIO. For low-frequency sampling, PIO is probably good enough.
>
> Cc: Holger Assmann <has@pengutronix.de>
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  drivers/iio/adc/stm32-adc.c | 31 +++++++++++++++++++++++++------
>  1 file changed, 25 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
> index c067c994dae2..91d9483e1f5f 100644
> --- a/drivers/iio/adc/stm32-adc.c
> +++ b/drivers/iio/adc/stm32-adc.c
> @@ -1718,7 +1718,7 @@ static void stm32_adc_chan_init_one(struct iio_dev *indio_dev,
>  	}
>  }
>  
> -static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
> +static int stm32_adc_chan_of_init(struct iio_dev *indio_dev, bool timestamping)
>  {
>  	struct device_node *node = indio_dev->dev.of_node;
>  	struct stm32_adc *adc = iio_priv(indio_dev);
> @@ -1766,6 +1766,9 @@ static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
>  		return -EINVAL;
>  	}
>  
> +	if (timestamping)
> +		num_channels++;
> +
>  	channels = devm_kcalloc(&indio_dev->dev, num_channels,
>  				sizeof(struct iio_chan_spec), GFP_KERNEL);
>  	if (!channels)
> @@ -1816,6 +1819,19 @@ static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
>  		stm32_adc_smpr_init(adc, channels[i].channel, smp);
>  	}
>  
> +	if (timestamping) {
> +		struct iio_chan_spec *timestamp = &channels[scan_index];
> +
> +		timestamp->type = IIO_TIMESTAMP;
> +		timestamp->channel = -1;
> +		timestamp->scan_index = scan_index;
> +		timestamp->scan_type.sign = 's';
> +		timestamp->scan_type.realbits = 64;
> +		timestamp->scan_type.storagebits = 64;
> +
> +		scan_index++;
> +	}
> +
>  	indio_dev->num_channels = scan_index;
>  	indio_dev->channels = channels;
>  
> @@ -1875,6 +1891,7 @@ static int stm32_adc_probe(struct platform_device *pdev)
>  	struct device *dev = &pdev->dev;
>  	irqreturn_t (*handler)(int irq, void *p) = NULL;
>  	struct stm32_adc *adc;
> +	bool timestamping = false;
>  	int ret;
>  
>  	if (!pdev->dev.of_node)
> @@ -1931,16 +1948,18 @@ static int stm32_adc_probe(struct platform_device *pdev)
>  	if (ret < 0)
>  		return ret;
>  
> -	ret = stm32_adc_chan_of_init(indio_dev);
> -	if (ret < 0)
> -		return ret;
> -
>  	ret = stm32_adc_dma_request(dev, indio_dev);
>  	if (ret < 0)
>  		return ret;
>  
> -	if (!adc->dma_chan)
> +	if (!adc->dma_chan) {
>  		handler = &stm32_adc_trigger_handler;

Hi Ahmad,

Just suggesting: maybe add a quick comment to indicate that timestamping
is supported in PIO mode (only), as DMA doesn't fill it into the buffer
(well described in the commit message).

> +		timestamping = true;
> +	}
> +
> +	ret = stm32_adc_chan_of_init(indio_dev, timestamping);
> +	if (ret < 0)
> +		return ret;


DMA resources need to be freed, instead of returning directly here, in
case of error:

     goto err_dma_disable;

With that fixed, you can add my :

Acked-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>

Thanks,

Fabrice


>  
>  	ret = iio_triggered_buffer_setup(indio_dev,
>  					 &iio_pollfunc_store_time, handler,
Ahmad Fatoum Jan. 22, 2021, 11:02 a.m. UTC | #2
Hi,

On 22.01.21 11:23, Fabrice Gasnier wrote:
> On 1/21/21 7:02 PM, Ahmad Fatoum wrote:
>> For non-DMA usage, we have an easy way to associate a timestamp with a
>> sample: iio_pollfunc_store_time stores a timestamp in the primary
>> trigger IRQ handler and stm32_adc_trigger_handler runs in the IRQ thread
>> to push out the buffer along with the timestamp.
>>
>> For this to work, the driver needs to register an IIO_TIMESTAMP channel.
>> Do this.
>>
>> For DMA, it's not as easy, because we don't push the buffers out of
>> stm32_adc_trigger, but out of stm32_adc_dma_buffer_done, which runs in
>> a tasklet scheduled after a DMA completion.
>>
>> Preferably, the DMA controller would copy us the timestamp into that buffer
>> as well. Until this is implemented, restrict timestamping support to
>> only PIO. For low-frequency sampling, PIO is probably good enough.
>>
>> Cc: Holger Assmann <has@pengutronix.de>
>> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
>> ---
>>  drivers/iio/adc/stm32-adc.c | 31 +++++++++++++++++++++++++------
>>  1 file changed, 25 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
>> index c067c994dae2..91d9483e1f5f 100644
>> --- a/drivers/iio/adc/stm32-adc.c
>> +++ b/drivers/iio/adc/stm32-adc.c
>> @@ -1718,7 +1718,7 @@ static void stm32_adc_chan_init_one(struct iio_dev *indio_dev,
>>  	}
>>  }
>>  
>> -static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
>> +static int stm32_adc_chan_of_init(struct iio_dev *indio_dev, bool timestamping)
>>  {
>>  	struct device_node *node = indio_dev->dev.of_node;
>>  	struct stm32_adc *adc = iio_priv(indio_dev);
>> @@ -1766,6 +1766,9 @@ static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
>>  		return -EINVAL;
>>  	}
>>  
>> +	if (timestamping)
>> +		num_channels++;
>> +
>>  	channels = devm_kcalloc(&indio_dev->dev, num_channels,
>>  				sizeof(struct iio_chan_spec), GFP_KERNEL);
>>  	if (!channels)
>> @@ -1816,6 +1819,19 @@ static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
>>  		stm32_adc_smpr_init(adc, channels[i].channel, smp);
>>  	}
>>  
>> +	if (timestamping) {
>> +		struct iio_chan_spec *timestamp = &channels[scan_index];
>> +
>> +		timestamp->type = IIO_TIMESTAMP;
>> +		timestamp->channel = -1;
>> +		timestamp->scan_index = scan_index;
>> +		timestamp->scan_type.sign = 's';
>> +		timestamp->scan_type.realbits = 64;
>> +		timestamp->scan_type.storagebits = 64;
>> +
>> +		scan_index++;
>> +	}
>> +
>>  	indio_dev->num_channels = scan_index;
>>  	indio_dev->channels = channels;
>>  
>> @@ -1875,6 +1891,7 @@ static int stm32_adc_probe(struct platform_device *pdev)
>>  	struct device *dev = &pdev->dev;
>>  	irqreturn_t (*handler)(int irq, void *p) = NULL;
>>  	struct stm32_adc *adc;
>> +	bool timestamping = false;
>>  	int ret;
>>  
>>  	if (!pdev->dev.of_node)
>> @@ -1931,16 +1948,18 @@ static int stm32_adc_probe(struct platform_device *pdev)
>>  	if (ret < 0)
>>  		return ret;
>>  
>> -	ret = stm32_adc_chan_of_init(indio_dev);
>> -	if (ret < 0)
>> -		return ret;
>> -
>>  	ret = stm32_adc_dma_request(dev, indio_dev);
>>  	if (ret < 0)
>>  		return ret;
>>  
>> -	if (!adc->dma_chan)
>> +	if (!adc->dma_chan) {
>>  		handler = &stm32_adc_trigger_handler;
> 
> Hi Ahmad,
> 
> Just suggesting: maybe add a quick comment to indicate that timestamping
> is supported in PIO mode (only), as DMA doesn't fill it into the buffer
> (well described in the commit message).

Will do.

> 
>> +		timestamping = true;
>> +	}
>> +
>> +	ret = stm32_adc_chan_of_init(indio_dev, timestamping);
>> +	if (ret < 0)
>> +		return ret;
> 
> 
> DMA resources need to be freed, instead of returning directly here, in
> case of error:
> 
>      goto err_dma_disable;

Ouch, thanks for spotting. Will respin.

> 
> With that fixed, you can add my :
> 
> Acked-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>

Thanks,
Ahmad

> 
> Thanks,
> 
> Fabrice
> 
> 
>>  
>>  	ret = iio_triggered_buffer_setup(indio_dev,
>>  					 &iio_pollfunc_store_time, handler,
>
diff mbox series

Patch

diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
index c067c994dae2..91d9483e1f5f 100644
--- a/drivers/iio/adc/stm32-adc.c
+++ b/drivers/iio/adc/stm32-adc.c
@@ -1718,7 +1718,7 @@  static void stm32_adc_chan_init_one(struct iio_dev *indio_dev,
 	}
 }
 
-static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
+static int stm32_adc_chan_of_init(struct iio_dev *indio_dev, bool timestamping)
 {
 	struct device_node *node = indio_dev->dev.of_node;
 	struct stm32_adc *adc = iio_priv(indio_dev);
@@ -1766,6 +1766,9 @@  static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
 		return -EINVAL;
 	}
 
+	if (timestamping)
+		num_channels++;
+
 	channels = devm_kcalloc(&indio_dev->dev, num_channels,
 				sizeof(struct iio_chan_spec), GFP_KERNEL);
 	if (!channels)
@@ -1816,6 +1819,19 @@  static int stm32_adc_chan_of_init(struct iio_dev *indio_dev)
 		stm32_adc_smpr_init(adc, channels[i].channel, smp);
 	}
 
+	if (timestamping) {
+		struct iio_chan_spec *timestamp = &channels[scan_index];
+
+		timestamp->type = IIO_TIMESTAMP;
+		timestamp->channel = -1;
+		timestamp->scan_index = scan_index;
+		timestamp->scan_type.sign = 's';
+		timestamp->scan_type.realbits = 64;
+		timestamp->scan_type.storagebits = 64;
+
+		scan_index++;
+	}
+
 	indio_dev->num_channels = scan_index;
 	indio_dev->channels = channels;
 
@@ -1875,6 +1891,7 @@  static int stm32_adc_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	irqreturn_t (*handler)(int irq, void *p) = NULL;
 	struct stm32_adc *adc;
+	bool timestamping = false;
 	int ret;
 
 	if (!pdev->dev.of_node)
@@ -1931,16 +1948,18 @@  static int stm32_adc_probe(struct platform_device *pdev)
 	if (ret < 0)
 		return ret;
 
-	ret = stm32_adc_chan_of_init(indio_dev);
-	if (ret < 0)
-		return ret;
-
 	ret = stm32_adc_dma_request(dev, indio_dev);
 	if (ret < 0)
 		return ret;
 
-	if (!adc->dma_chan)
+	if (!adc->dma_chan) {
 		handler = &stm32_adc_trigger_handler;
+		timestamping = true;
+	}
+
+	ret = stm32_adc_chan_of_init(indio_dev, timestamping);
+	if (ret < 0)
+		return ret;
 
 	ret = iio_triggered_buffer_setup(indio_dev,
 					 &iio_pollfunc_store_time, handler,