Linux IIO development
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: "Leonard Göhrs" <l.goehrs@pengutronix.de>,
	kernel@pengutronix.de, "Jonathan Cameron" <jic23@kernel.org>
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1] iio: adc: add buffering support to the TI LMP92064 ADC driver
Date: Wed, 14 Jun 2023 04:59:04 -0700	[thread overview]
Message-ID: <dd0dd53c-2ac5-a3c0-69f3-a95994b6f486@metafoo.de> (raw)
In-Reply-To: <20230614075537.3525194-1-l.goehrs@pengutronix.de>

On 6/14/23 00:55, Leonard Göhrs wrote:
> Enable buffered reading of samples from the LMP92064 ADC.
> The main benefit of this change is being able to read out current and
> voltage measurements in a single transfer, allowing instantaneous power
> measurements.
>
> Reads into the buffer can be triggered by any software triggers, e.g.
> the iio-trig-hrtimer:
>
>      $ mkdir /sys/kernel/config/iio/triggers/hrtimer/my-trigger
>      $ cat /sys/bus/iio/devices/iio\:device3/name
>      lmp92064
>      $ iio_readdev -t my-trigger -b 16 iio:device3 | hexdump
>      WARNING: High-speed mode not enabled
>      0000000 0000 0176 0101 0001 5507 abd5 7645 1768
>      0000010 0000 016d 0101 0001 ee1e ac6b 7645 1768
>      ...
>
> Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>

Patch looks good. A small comment for an improvement, but not a most have.

> ---
>   drivers/iio/adc/ti-lmp92064.c | 54 +++++++++++++++++++++++++++++++++++
>   1 file changed, 54 insertions(+)
>
> diff --git a/drivers/iio/adc/ti-lmp92064.c b/drivers/iio/adc/ti-lmp92064.c
> index c30ed824924f3..03765c4057dda 100644
> --- a/drivers/iio/adc/ti-lmp92064.c
> +++ b/drivers/iio/adc/ti-lmp92064.c
> @@ -16,7 +16,10 @@
>   #include <linux/spi/spi.h>
>   
>   #include <linux/iio/iio.h>
> +#include <linux/iio/buffer.h>
>   #include <linux/iio/driver.h>
> +#include <linux/iio/triggered_buffer.h>
> +#include <linux/iio/trigger_consumer.h>
>   
>   #define TI_LMP92064_REG_CONFIG_A 0x0000
>   #define TI_LMP92064_REG_CONFIG_B 0x0001
> @@ -91,6 +94,13 @@ static const struct iio_chan_spec lmp92064_adc_channels[] = {
>   		.address = TI_LMP92064_CHAN_INC,
>   		.info_mask_separate =
>   			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
> +		.scan_index = 0,
> +		.scan_type = {
> +			.sign = 'u',
> +			.realbits = 12,
> +			.storagebits = 16,
> +			.shift = 0,
> +		},
>   		.datasheet_name = "INC",
>   	},
>   	{
> @@ -98,8 +108,16 @@ static const struct iio_chan_spec lmp92064_adc_channels[] = {
>   		.address = TI_LMP92064_CHAN_INV,
>   		.info_mask_separate =
>   			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
> +		.scan_index = 1,
> +		.scan_type = {
> +			.sign = 'u',
> +			.realbits = 12,
> +			.storagebits = 16,
> +			.shift = 0,
> +		},
>   		.datasheet_name = "INV",
>   	},
> +	IIO_CHAN_SOFT_TIMESTAMP(2),
>   };
>   
>   static int lmp92064_read_meas(struct lmp92064_adc_priv *priv, u16 *res)
> @@ -171,6 +189,37 @@ static int lmp92064_read_raw(struct iio_dev *indio_dev,
>   	}
>   }
>   
> +static irqreturn_t lmp92064_trigger_handler(int irq, void *p)
> +{
> +	struct iio_poll_func *pf = p;
> +	struct iio_dev *indio_dev = pf->indio_dev;
> +	struct lmp92064_adc_priv *priv = iio_priv(indio_dev);
> +	int i = 0, j, ret;
> +	u16 raw[2];
> +	u16 *data;
> +
> +	ret = lmp92064_read_meas(priv, raw);
> +	if (ret < 0)
> +		goto done;
> +
> +	data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
> +	if (!data)
> +		goto done;
If you want to avoid allocating the buffer each time a sample-set is 
captured you can register the `update_scan_mode` callback and allocate 
the buffer in there. Or if you know the upper limit of your buffer size, 
base on number of channels, and its small you can also use a one time 
allocation directly embedded in the priv struct.
> +
> +	for_each_set_bit(j, indio_dev->active_scan_mask, indio_dev->masklength)
> +		data[i++] = raw[j];
> +
> +	iio_push_to_buffers_with_timestamp(indio_dev, data,
> +					   iio_get_time_ns(indio_dev));
> +
> +	kfree(data);
> +
> +done:
> +	iio_trigger_notify_done(indio_dev->trig);
> +
> +	return IRQ_HANDLED;
> +}
> +
]...\

  reply	other threads:[~2023-06-14 11:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-14  7:55 [PATCH v1] iio: adc: add buffering support to the TI LMP92064 ADC driver Leonard Göhrs
2023-06-14 11:59 ` Lars-Peter Clausen [this message]
2023-06-17 18:27 ` Jonathan Cameron
2023-06-22  7:05   ` Leonard Göhrs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dd0dd53c-2ac5-a3c0-69f3-a95994b6f486@metafoo.de \
    --to=lars@metafoo.de \
    --cc=jic23@kernel.org \
    --cc=kernel@pengutronix.de \
    --cc=l.goehrs@pengutronix.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox