diff mbox series

[v4,1/8] iio: add read scale and offset services to iio backend framework

Message ID 20240704155338.2387858-2-olivier.moysan@foss.st.com (mailing list archive)
State Changes Requested
Headers show
Series iio: adc: dfsdm: add scaling support | expand

Commit Message

Olivier MOYSAN July 4, 2024, 3:53 p.m. UTC
Add iio_backend_read_scale() and iio_backend_read_offset() services
to read channel scale and offset from an IIO backbend device.

Also add a read_raw callback which replicates the read_raw callback of
the IIO framework, and is intended to request miscellaneous channel
attributes from the backend device.
Both scale and offset helpers use this callback.

Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
Reviewed-by: Nuno Sa <nuno.sa@analog.com>
---
 drivers/iio/industrialio-backend.c | 34 ++++++++++++++++++++++++++++++
 include/linux/iio/backend.h        |  9 +++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

Comments

Nuno Sá July 5, 2024, 10:18 a.m. UTC | #1
On Thu, 2024-07-04 at 17:53 +0200, Olivier Moysan wrote:
> Add iio_backend_read_scale() and iio_backend_read_offset() services
> to read channel scale and offset from an IIO backbend device.
> 
> Also add a read_raw callback which replicates the read_raw callback of
> the IIO framework, and is intended to request miscellaneous channel
> attributes from the backend device.
> Both scale and offset helpers use this callback.
> 
> Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
> Reviewed-by: Nuno Sa <nuno.sa@analog.com>
> ---
>  drivers/iio/industrialio-backend.c | 34 ++++++++++++++++++++++++++++++
>  include/linux/iio/backend.h        |  9 +++++++-
>  2 files changed, 42 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/industrialio-backend.c b/drivers/iio/industrialio-
> backend.c
> index efe05be284b6..4e0ff6e6e9d4 100644
> --- a/drivers/iio/industrialio-backend.c
> +++ b/drivers/iio/industrialio-backend.c
> @@ -357,6 +357,40 @@ int devm_iio_backend_request_buffer(struct device *dev,
>  }
>  EXPORT_SYMBOL_NS_GPL(devm_iio_backend_request_buffer, IIO_BACKEND);
>  
> +/**
> + * iio_backend_read_scale - Request channel scale from the IIO backend.
> + * @back:	Backend device
> + * @chan:	IIO channel reference
> + * @scale:	returned scale value
> + *
> + * RETURNS:
> + * 0 on success, negative error number on failure.
> + */
> +int iio_backend_read_scale(struct iio_backend *back,
> +			   struct iio_chan_spec const *chan, int *scale)
> +{
> +	return iio_backend_op_call(back, read_raw, chan, scale, NULL,
> +				   IIO_CHAN_INFO_SCALE);
> +}
> +EXPORT_SYMBOL_NS_GPL(iio_backend_read_scale, IIO_BACKEND);
> +
> +/**
> + * iio_backend_read_offset - Request channel offset from the IIO backend.
> + * @back:	Backend device
> + * @chan:	IIO channel reference
> + * @offset:	returned offset value
> + *
> + * RETURNS:
> + * 0 on success, negative error number on failure.
> + */
> +int iio_backend_read_offset(struct iio_backend *back,
> +			    struct iio_chan_spec const *chan, int *offset)
> +{
> +	return iio_backend_op_call(back, read_raw, chan, offset, NULL,
> +				   IIO_CHAN_INFO_OFFSET);
> +}
> +EXPORT_SYMBOL_NS_GPL(iio_backend_read_offset, IIO_BACKEND);
> +

Hi Olivier,

Not exactly what I had in mind :). My thinking was to have:

int iio_backend_read_raw(struct iio_backend *back,
			 struct iio_chan_spec const *chan, int *val, int *val2,
                         long mask)
{
	return iio_backend_op_call(back, read_raw, chan, val, val2, mask);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_read_raw, IIO_BACKEND);

Then, on backend.h

static inline int iio_backend_read_scale(struct iio_backend *back, struct
iio_chan_spec const *chan, int *val, int val2)
{
	return iio_backend_read_raw(..., IIO_CHAN_INFO_SCALE);
}

Advantage is that we only need to export one symbol from the framework. But the most
important piece I don't really agree in the patch is assuming NULL for val2 (even
more in scale where often we use both vals). So I think it already makes sense to
expose the API with two int's even if you don't need them for your usecase.

- Nuno Sá
diff mbox series

Patch

diff --git a/drivers/iio/industrialio-backend.c b/drivers/iio/industrialio-backend.c
index efe05be284b6..4e0ff6e6e9d4 100644
--- a/drivers/iio/industrialio-backend.c
+++ b/drivers/iio/industrialio-backend.c
@@ -357,6 +357,40 @@  int devm_iio_backend_request_buffer(struct device *dev,
 }
 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_request_buffer, IIO_BACKEND);
 
+/**
+ * iio_backend_read_scale - Request channel scale from the IIO backend.
+ * @back:	Backend device
+ * @chan:	IIO channel reference
+ * @scale:	returned scale value
+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */
+int iio_backend_read_scale(struct iio_backend *back,
+			   struct iio_chan_spec const *chan, int *scale)
+{
+	return iio_backend_op_call(back, read_raw, chan, scale, NULL,
+				   IIO_CHAN_INFO_SCALE);
+}
+EXPORT_SYMBOL_NS_GPL(iio_backend_read_scale, IIO_BACKEND);
+
+/**
+ * iio_backend_read_offset - Request channel offset from the IIO backend.
+ * @back:	Backend device
+ * @chan:	IIO channel reference
+ * @offset:	returned offset value
+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */
+int iio_backend_read_offset(struct iio_backend *back,
+			    struct iio_chan_spec const *chan, int *offset)
+{
+	return iio_backend_op_call(back, read_raw, chan, offset, NULL,
+				   IIO_CHAN_INFO_OFFSET);
+}
+EXPORT_SYMBOL_NS_GPL(iio_backend_read_offset, IIO_BACKEND);
+
 static struct iio_backend *iio_backend_from_indio_dev_parent(const struct device *dev)
 {
 	struct iio_backend *back = ERR_PTR(-ENODEV), *iter;
diff --git a/include/linux/iio/backend.h b/include/linux/iio/backend.h
index 8099759d7242..2d80e3fa58ce 100644
--- a/include/linux/iio/backend.h
+++ b/include/linux/iio/backend.h
@@ -81,6 +81,7 @@  enum iio_backend_sample_trigger {
  * @extend_chan_spec: Extend an IIO channel.
  * @ext_info_set: Extended info setter.
  * @ext_info_get: Extended info getter.
+ * @read_raw: Read a channel attribute from a backend device
  **/
 struct iio_backend_ops {
 	int (*enable)(struct iio_backend *back);
@@ -113,6 +114,9 @@  struct iio_backend_ops {
 			    const char *buf, size_t len);
 	int (*ext_info_get)(struct iio_backend *back, uintptr_t private,
 			    const struct iio_chan_spec *chan, char *buf);
+	int (*read_raw)(struct iio_backend *back,
+			struct iio_chan_spec const *chan, int *val, int *val2,
+			long mask);
 };
 
 int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan);
@@ -141,7 +145,10 @@  ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private,
 				 const char *buf, size_t len);
 ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private,
 				 const struct iio_chan_spec *chan, char *buf);
-
+int iio_backend_read_scale(struct iio_backend *back,
+			   struct iio_chan_spec const *chan, int *scale);
+int iio_backend_read_offset(struct iio_backend *back,
+			    struct iio_chan_spec const *chan, int *offset);
 int iio_backend_extend_chan_spec(struct iio_dev *indio_dev,
 				 struct iio_backend *back,
 				 struct iio_chan_spec *chan);