Skip to contentSkip to conversation

iio: add new backend framework

PATCH Superseded Nuno Sa 5 participants 17 messages Jan 12, 2024 – Jan 15, 2024

Subsystems (7)

v5 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Nuno Sa

author maintainer
#0 lore raw

v5 cover iio: add new backend framework

v1:
https://lore.kernel.org/linux-iio/20231204144925.4fe9922f@jic23-huawei/T/#m222f5175273b81dbfe40b7f0daffcdc67d6cb8ff

v2:
https://lore.kernel.org/r/[email protected]

v3:
https://lore.kernel.org/linux-iio/[email protected]/

v4:
https://lore.kernel.org/r/[email protected]

Changes in v5:
- Patch 2
* Update commit subject and message;
* Add '#io-backends-cells' property.
- Patch 4
* Also support '#io-backends-cells'.
- Patch 6
* Improve Kconfig help message;
* Only use device_links to control backends access. With this, we can remove all the mutex + sync logic as we are now guaranteed that a frontend cannot be up and running if one of the backends is gone.
* Error out if we can't create the device_link (so we can guarantee the above).
* Improve docs for __devm_iio_backend_get_from_fwnode_lookup();
* Add a __ prefix to devm_iio_backend_get_from_fwnode_lookup() so it's clear

    that API should not be used (or used with care);

* Drop devm_iio_backend_get_optional();
* remove unneeded headers.
- Patch 7
* Handle the optional backend in the driver.

The biggest difference is that the backend @ops pointer handling to reason about the backend existence is gone. Now, we rely on device_links as that makes sure all consumers (in the link) are unbound before the provider. Hence, the frontend is guaranteed to be unbound if any of the backends is gone. That simplifies things a lot...

Keeping the block diagram so we don't have to follow links to check one of the typical setups.

                                           -------------------------------------------------------
 ------------------                        | -----------         ------------      -------  FPGA |
 |     ADC        |------------------------| | AXI ADC |---------| DMA CORE |------| RAM |       |
 | (Frontend/IIO) | Serial Data (eg: LVDS) | |(backend)|---------|          |------|     |       |
 |                |------------------------| -----------         ------------      -------       |
 ------------------                        -------------------------------------------------------

---
Nuno Sa (7):
dt-bindings: adc: ad9467: add new io-backend property
dt-bindings: adc: axi-adc: update bindings for backend framework
driver: core: allow modifying device_links flags
iio: buffer-dmaengine: export buffer alloc and free functions
iio: add the IIO backend framework
iio: adc: ad9467: convert to backend framework
iio: adc: adi-axi-adc: move to backend framework

Olivier Moysan (1):
of: property: add device link support for io-backends

 .../devicetree/bindings/iio/adc/adi,ad9467.yaml    |   4 +
 .../devicetree/bindings/iio/adc/adi,axi-adc.yaml   |   7 +-
 MAINTAINERS                                        |   8 +
 drivers/base/core.c                                |  14 +-
 drivers/iio/Kconfig                                |   9 +
 drivers/iio/Makefile                               |   1 +
 drivers/iio/adc/Kconfig                            |   4 +-
 drivers/iio/adc/ad9467.c                           | 283 +++++++++-----
 drivers/iio/adc/adi-axi-adc.c                      | 381 +++++--------------
 drivers/iio/buffer/industrialio-buffer-dmaengine.c |   8 +-
 drivers/iio/industrialio-backend.c                 | 411 +++++++++++++++++++++
 drivers/of/property.c                              |   2 +
 include/linux/iio/adc/adi-axi-adc.h                |  68 ----
 include/linux/iio/backend.h                        |  73 ++++
 include/linux/iio/buffer-dmaengine.h               |   3 +
 15 files changed, 806 insertions(+), 470 deletions(-)

---
base-commit: 3f4525f924e21d4f532517b17a20ffa5df7c0db7
change-id: 20231219-iio-backend-a3dc1a6a7a58 --

Thanks!
- Nuno Sá

author maintainer
#1 lore raw

PATCH v5 1/8 dt-bindings: adc: ad9467: add new io-backend property

The ad9467 will make use of the new IIO backend framework which is a provider - consumer interface where IIO backends provide services to consumers. As such, and being this device a consumer, add the new generic io-backend property to the bindings.

---
 Documentation/devicetree/bindings/iio/adc/adi,ad9467.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/iio/adc/adi,ad9467.yaml +4 -0
index 7aa748d6b7a0..eecd5fbab695 100644
@@ -44,6 +44,9 @@ properties:
       Pin that controls the powerdown mode of the device.
     maxItems: 1
 
+  io-backends:
+    maxItems: 1
+
   reset-gpios:
     description:
       Reset pin for the device.
@@ -68,6 +71,7 @@ examples:
             reg = <0>;
             clocks = <&adc_clk>;
             clock-names = "adc-clk";
+            io-backends = <&iio_backend>;
         };
     };
 ...
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v5 2/8 dt-bindings: adc: axi-adc: update bindings for backend framework

In reply to Nuno Sa

'adi,adc-dev' is now deprecated and must not be used anymore. Hence, also remove it from being required.

The reason why it's being deprecated is because the axi-adc CORE is now an IIO service provider hardware (IIO backends) for consumers to make use of. Before, the logic with 'adi,adc-dev' was the opposite (it was kind of consumer referencing other nodes/devices) and that proved to be wrong and to not scale.

Now, IIO consumers of this hardware are expected to reference it using the io-backends property. Hence, the new '#io-backend-cells' is being added so the device is easily identified as a provider.

---
 Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml +4 -3
index 9996dd93f84b..dc7b14546afb 100644
@@ -39,12 +39,15 @@ properties:
     $ref: /schemas/types.yaml#/definitions/phandle
     description:
       A reference to a the actual ADC to which this FPGA ADC interfaces to.
+    deprecated: true
+
+  '#io-backends-cells'
+    const: 0
 
 required:
   - compatible
   - dmas
   - reg
-  - adi,adc-dev
 
 additionalProperties: false
 
@@ -55,7 +58,5 @@ examples:
         reg = <0x44a00000 0x10000>;
         dmas = <&rx_dma 0>;
         dma-names = "rx";
-
-        adi,adc-dev = <&spi_adc>;
     };
 ...
Signature · 1 line
2.43.0
maintainer
#3 lore raw

Re: [PATCH v5 2/8] dt-bindings: adc: axi-adc: update bindings for backend framework

Quoted 9 lines · Nuno Sa

'adi,adc-dev' is now deprecated and must not be used anymore. Hence, also remove it from being required.

The reason why it's being deprecated is because the axi-adc CORE is now an IIO service provider hardware (IIO backends) for consumers to make use of. Before, the logic with 'adi,adc-dev' was the opposite (it was kind of consumer referencing other nodes/devices) and that proved to be wrong and to not scale.

Quoted 9 lines

Now, IIO consumers of this hardware are expected to reference it using the io-backends property. Hence, the new '#io-backend-cells' is being added so the device is easily identified as a provider.

---
 Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:
./Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml:45:5: [error] syntax error: could not find expected ':' (syntax)

dtschema/dtc warnings/errors:
make[2]: *** Deleting file 'Documentation/devicetree/bindings/iio/adc/adi,axi-adc.example.dts' Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml:45:5: could not find expected ':'
make[2]: *** [Documentation/devicetree/bindings/Makefile:26: Documentation/devicetree/bindings/iio/adc/adi,axi-adc.example.dts] Error 1 make[2]: *** Waiting for unfinished jobs....
./Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml:45:5: could not find expected ':' /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml: ignoring, error parsing file make[1]: *** [/builds/robherring/dt-review-ci/linux/Makefile:1424: dt_binding_check] Error 2
make: *** [Makefile:234: __sub-make] Error 2

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

author maintainer
#4 lore raw

PATCH v5 3/8 driver: core: allow modifying device_links flags

In reply to Nuno Sa

If a device_link is previously created (eg: via fw_devlink_create_devlink()) before the supplier + consumer are both present and bound to their respective drivers, there's no way to set DL_FLAG_AUTOREMOVE_CONSUMER anymore while one can still set DL_FLAG_AUTOREMOVE_SUPPLIER. Hence, rework the flags checks to allow for DL_FLAG_AUTOREMOVE_CONSUMER in the same way DL_FLAG_AUTOREMOVE_SUPPLIER is done.

While at it, make sure that we are never left with DL_FLAG_AUTOPROBE_CONSUMER set together with one of DL_FLAG_AUTOREMOVE_CONSUMER or DL_FLAG_AUTOREMOVE_SUPPLIER.

---
 drivers/base/core.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/base/core.c +9 -5
index 67ba592afc77..fdbb5abc75d5 100644
@@ -807,11 +807,15 @@ struct device_link *device_link_add(struct device *consumer,
 		 * update the existing link to stay around longer.
 		 */
 		if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
-			if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
-				link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
-				link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
-			}
-		} else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
+			link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
+			link->flags &= ~DL_FLAG_AUTOPROBE_CONSUMER;
+			link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
+
+		} else if (flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
+			link->flags &= ~DL_FLAG_AUTOREMOVE_SUPPLIER;
+			link->flags &= ~DL_FLAG_AUTOPROBE_CONSUMER;
+			link->flags |= DL_FLAG_AUTOREMOVE_CONSUMER;
+		} else {
 			link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
 					 DL_FLAG_AUTOREMOVE_SUPPLIER);
 		}
Signature · 1 line
2.43.0
author maintainer
#5 lore raw

PATCH v5 4/8 of: property: add device link support for io-backends

In reply to Nuno Sa

From: Olivier Moysan <[email protected]>

Add support for creating device links out of more DT properties.

---
 drivers/of/property.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/of/property.c +2 -0
index afdaefbd03f6..c5909ffd93f2 100644
@@ -1244,6 +1244,7 @@ DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
 DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
+DEFINE_SIMPLE_PROP(io_backends, "io-backends", "#io-backends-cells")
 DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
@@ -1334,6 +1335,7 @@ static const struct supplier_bindings of_supplier_bindings[] = {
 	{ .parse_prop = parse_iommu_maps, .optional = true, },
 	{ .parse_prop = parse_mboxes, },
 	{ .parse_prop = parse_io_channels, },
+	{ .parse_prop = parse_io_backends, },
 	{ .parse_prop = parse_interrupt_parent, },
 	{ .parse_prop = parse_dmas, .optional = true, },
 	{ .parse_prop = parse_power_domains, },
Signature · 1 line
2.43.0
author maintainer
#6 lore raw

PATCH v5 5/8 iio: buffer-dmaengine: export buffer alloc and free functions

In reply to Nuno Sa

Export iio_dmaengine_buffer_free() and iio_dmaengine_buffer_alloc(). This is in preparation of introducing IIO backends support. This will allow us to allocate a buffer and control it's lifetime from a device different from the one holding the DMA firmware properties. Effectively, in this case the struct device holding the firmware information about the DMA channels is not the same as iio_dev->dev.parent (typical case).

While at it, namespace the buffer-dmaengine exports and update the current user of these buffers.

---
 drivers/iio/adc/adi-axi-adc.c                      | 1 +
 drivers/iio/buffer/industrialio-buffer-dmaengine.c | 8 +++++---
 include/linux/iio/buffer-dmaengine.h               | 3 +++
 3 files changed, 9 insertions(+), 3 deletions(-)
drivers/iio/adc/adi-axi-adc.c +1 -0
index c247ff1541d2..0f21d1d98b9f 100644
@@ -447,3 +447,4 @@ module_platform_driver(adi_axi_adc_driver);
 MODULE_AUTHOR("Michael Hennerich <[email protected]>");
 MODULE_DESCRIPTION("Analog Devices Generic AXI ADC IP core driver");
 MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(IIO_DMAENGINE_BUFFER);
drivers/iio/buffer/industrialio-buffer-dmaengine.c +5 -3
index 5f85ba38e6f6..0d53c0a07b0d 100644
@@ -159,7 +159,7 @@ static const struct iio_dev_attr *iio_dmaengine_buffer_attrs[] = {
  * Once done using the buffer iio_dmaengine_buffer_free() should be used to
  * release it.
  */
-static struct iio_buffer *iio_dmaengine_buffer_alloc(struct device *dev,
+struct iio_buffer *iio_dmaengine_buffer_alloc(struct device *dev,
 	const char *channel)
 {
 	struct dmaengine_buffer *dmaengine_buffer;
@@ -210,6 +210,7 @@ static struct iio_buffer *iio_dmaengine_buffer_alloc(struct device *dev,
 	kfree(dmaengine_buffer);
 	return ERR_PTR(ret);
 }
+EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_alloc, IIO_DMAENGINE_BUFFER);
 
 /**
  * iio_dmaengine_buffer_free() - Free dmaengine buffer
@@ -217,7 +218,7 @@ static struct iio_buffer *iio_dmaengine_buffer_alloc(struct device *dev,
  *
  * Frees a buffer previously allocated with iio_dmaengine_buffer_alloc().
  */
-static void iio_dmaengine_buffer_free(struct iio_buffer *buffer)
+void iio_dmaengine_buffer_free(struct iio_buffer *buffer)
 {
 	struct dmaengine_buffer *dmaengine_buffer =
 		iio_buffer_to_dmaengine_buffer(buffer);
@@ -227,6 +228,7 @@ static void iio_dmaengine_buffer_free(struct iio_buffer *buffer)
 
 	iio_buffer_put(buffer);
 }
+EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_free, IIO_DMAENGINE_BUFFER);
 
 static void __devm_iio_dmaengine_buffer_free(void *buffer)
 {
@@ -288,7 +290,7 @@ int devm_iio_dmaengine_buffer_setup(struct device *dev,
 
 	return iio_device_attach_buffer(indio_dev, buffer);
 }
-EXPORT_SYMBOL_GPL(devm_iio_dmaengine_buffer_setup);
+EXPORT_SYMBOL_NS_GPL(devm_iio_dmaengine_buffer_setup, IIO_DMAENGINE_BUFFER);
 
 MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>");
 MODULE_DESCRIPTION("DMA buffer for the IIO framework");
include/linux/iio/buffer-dmaengine.h +3 -0
index 5c355be89814..cbb8ba957fad 100644
@@ -10,6 +10,9 @@
 struct iio_dev;
 struct device;
 
+struct iio_buffer *iio_dmaengine_buffer_alloc(struct device *dev,
+					      const char *channel);
+void iio_dmaengine_buffer_free(struct iio_buffer *buffer);
 int devm_iio_dmaengine_buffer_setup(struct device *dev,
 				    struct iio_dev *indio_dev,
 				    const char *channel);
Signature · 1 line
2.43.0
author maintainer
#7 lore raw

PATCH v5 6/8 iio: add the IIO backend framework

In reply to Nuno Sa

This is a Framework to handle complex IIO aggregate devices.

The typical architecture is to have one device as the frontend device which can be "linked" against one or multiple backend devices. All the IIO and userspace interface is expected to be registers/managed by the frontend device which will callback into the backends when needed (to get/set some configuration that it does not directly control).

The basic framework interface is pretty simple:
- Backends should register themselves with @devm_iio_backend_register()
- Frontend devices should get backends with @devm_iio_backend_get()

---
 MAINTAINERS                        |   8 +
 drivers/iio/Kconfig                |   9 +
 drivers/iio/Makefile               |   1 +
 drivers/iio/industrialio-backend.c | 411 +++++++++++++++++++++++++++++++++++++
 include/linux/iio/backend.h        |  73 +++++++
 5 files changed, 502 insertions(+)
MAINTAINERS +8 -0
index 0764fafce958..100aef8adc26 100644
@@ -10302,6 +10302,14 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/media/rc/iguanair.c
 
+IIO BACKEND FRAMEWORK
+M:	Nuno Sa <[email protected]>
+R:	Olivier Moysan <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/iio/industrialio-backend.c
+F:	include/linux/iio/backend.h
+
 IIO DIGITAL POTENTIOMETER DAC
 M:	Peter Rosin <[email protected]>
 L:	[email protected]
drivers/iio/Kconfig +9 -0
index 52eb46ef84c1..9c351ffc7bed 100644
@@ -71,6 +71,15 @@ config IIO_TRIGGERED_EVENT
 	help
 	  Provides helper functions for setting up triggered events.
 
+config IIO_BACKEND
+	tristate
+	help
+	  Framework to handle complex IIO aggregate devices. The typical
+	  architecture that can make use of this framework is to have one
+	  device as the frontend device which can be "linked" against one or
+	  multiple backend devices. The framework then makes it easy to get
+	  and control such backend devices.
+
 source "drivers/iio/accel/Kconfig"
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/addac/Kconfig"
drivers/iio/Makefile +1 -0
index 9622347a1c1b..0ba0e1521ba4 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_IIO_GTS_HELPER) += industrialio-gts-helper.o
 obj-$(CONFIG_IIO_SW_DEVICE) += industrialio-sw-device.o
 obj-$(CONFIG_IIO_SW_TRIGGER) += industrialio-sw-trigger.o
 obj-$(CONFIG_IIO_TRIGGERED_EVENT) += industrialio-triggered-event.o
+obj-$(CONFIG_IIO_BACKEND) += industrialio-backend.o
 
 obj-y += accel/
 obj-y += adc/
drivers/iio/industrialio-backend.c new file +411 -0

411 lines not on this page · raw message

include/linux/iio/backend.h new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#8 lore raw

Re: [PATCH v5 6/8] iio: add the IIO backend framework

Quoted 10 lines · On Fri, 12 Jan 2024 17:40:20 +0100 Nuno Sa via B4 Relay…

From: Nuno Sa <[email protected]>

This is a Framework to handle complex IIO aggregate devices.

The typical architecture is to have one device as the frontend device which can be "linked" against one or multiple backend devices. All the IIO and userspace interface is expected to be registers/managed by the frontend device which will callback into the backends when needed (to get/set some configuration that it does not directly control).

Quoted 5 lines

The basic framework interface is pretty simple:
- Backends should register themselves with @devm_iio_backend_register()
- Frontend devices should get backends with @devm_iio_backend_get()

A few minor comments inline.

...

Quoted 6 lines
drivers/iio/industrialio-backend.c new file +0 -0
new file mode 100644
index 000000000000..994bc68c2679
@@ -0,0 +1,411 @@

...

drivers/iio/industrialio-backend.c line 48

Quoted 4 lines
drivers/iio/industrialio-backend.c new file +4 -0
+
+/*
+ * Helper struct for requesting buffers. Allows for multiple buffers per
+ * backend.

Only seems to be used to ensure we have all the data needed to free it... So comment seems less than obviously connected to that.

Quoted 15 lines

+ */

drivers/iio/industrialio-backend.c new file +13 -0
+struct iio_backend_buffer_pair {
+	struct iio_backend *back;
+	struct iio_buffer *buffer;
+};
+
 
+/**
+ * iio_backend_chan_enable - Enable a backend channel.
+ * @back:	Backend device.
+ * @chan:	Channel number.
+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */

drivers/iio/industrialio-backend.c line 122

Quoted 10 lines
drivers/iio/industrialio-backend.c new file +10 -0
+int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan)
+{
+	return iio_backend_op_call(back, chan_enable, chan);
+}
+EXPORT_SYMBOL_NS_GPL(iio_backend_chan_enable, IIO_BACKEND);
+
+/**
+ * iio_backend_chan_disable - Disable a backend channel.
+ * @back:	Backend device.
+ * @chan:	Channel number.

Would be good to be consistent on . or not.

Quoted 13 lines

+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */

drivers/iio/industrialio-backend.c new file +9 -0
+int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan)
+{
+	return iio_backend_op_call(back, chan_disable, chan);
+}
+EXPORT_SYMBOL_NS_GPL(iio_backend_chan_disable, IIO_BACKEND);
+
+/**
+ * iio_backend_chan_enable - Enable the backend.
+ * @back:	Backend device

...

Quoted 2 lines

+/**
+ * devm_iio_backend_get_from_fwnode_lookup

Not valid kernel doc + name is wrong. Make sure you run the kernel-doc script over this and fix any errors or warnings reported.

Quoted 32 lines

+ * @dev: Device where to bind the backend lifetime.
+ * @fwnode: Firmware node of the backend device.
+ *
+ * Search the backend list for a device matching @fwnode.
+ * This API should not be used and it's only present for preventing the first
+ * user of this framework to break it's DT ABI.
+ *
+ * RETURNS:
+ * A backend pointer, negative error pointer otherwise.
+ */

drivers/iio/industrialio-backend.c new file +22 -0
+struct iio_backend *
+__devm_iio_backend_get_from_fwnode_lookup(struct device *dev,
+					  struct fwnode_handle *fwnode)
+{
+	struct iio_backend *back;
+	int ret;
+
+	guard(mutex)(&iio_back_lock);
+	list_for_each_entry(back, &iio_back_list, entry) {
+		if (!device_match_fwnode(back->dev, fwnode))
+			continue;
+
+		ret = __devm_iio_backend_get(dev, back);
+		if (ret)
+			return ERR_PTR(ret);
+
+		return back;
+	}
+
+	return ERR_PTR(-EPROBE_DEFER);
+}
+EXPORT_SYMBOL_NS_GPL(__devm_iio_backend_get_from_fwnode_lookup, IIO_BACKEND);

drivers/iio/industrialio-backend.c line 371

Quoted 10 lines
drivers/iio/industrialio-backend.c new file +7 -0
 
 
 
+/**
+ * devm_iio_backend_register - Register a new backend device
+ * @dev:	Backend device being registered.
+ * @ops:	Backend ops
+ * @priv:	Device private data.
+ *
+ * @ops and @priv are both mandatory. Not providing them results in -EINVAL.

It's unusual to 'insist' on the private data.
Whilst it's highly likely it will always be there from a core point of view we don't mind it being NULL. This is different from the ops as we want to be able to call those without checking they are there.

Quoted 11 lines

+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */

drivers/iio/industrialio-backend.c new file +6 -0
+int devm_iio_backend_register(struct device *dev,
+			      const struct iio_backend_ops *ops, void *priv)
+{
+	struct iio_backend *back;
+
+	if (!ops || !priv) {
 

drivers/iio/industrialio-backend.c line 391

Quoted 10 lines
drivers/iio/industrialio-backend.c new file +10 -0
+		pr_err("%s: No backend ops or private data given\n",
+		       dev_name(dev));
+		return -EINVAL;
+	}
+
+	/*
+	 * Through device_links, we guarantee that a frontend device cannot be
+	 * bound/exist if the backend driver is not around. Hence, we can bind
+	 * the backend object lifetime with the device being passed since
+	 * removing it will torn the frontend down.

"will have torn" or "will tear the"

Quoted 20 lines
drivers/iio/industrialio-backend.c new file +20 -0
+	 */
+	back = devm_kzalloc(dev, sizeof(*back), GFP_KERNEL);
+	if (!back)
+		return -ENOMEM;
+
+	back->ops = ops;
+	back->owner = dev->driver->owner;
+	back->dev = dev;
+	back->priv = priv;
+	mutex_lock(&iio_back_lock);
+	list_add(&back->entry, &iio_back_list);
+	mutex_unlock(&iio_back_lock);
+
+	return devm_add_action_or_reset(dev, iio_backend_unregister, back);
+}
+EXPORT_SYMBOL_NS_GPL(devm_iio_backend_register, IIO_BACKEND);
+
+MODULE_AUTHOR("Nuno Sa <[email protected]>");
+MODULE_DESCRIPTION("Framework to handle complex IIO aggregate devices");
+MODULE_LICENSE("GPL");
#9 lore raw

Re: [PATCH v5 6/8] iio: add the IIO backend framework

Quoted 33 lines · Jonathan Cameron
Quoted 10 lines · On Fri, 12 Jan 2024 17:40:20 +0100 Nuno Sa via B4 Relay…

From: Nuno Sa <[email protected]>

This is a Framework to handle complex IIO aggregate devices.

The typical architecture is to have one device as the frontend device which can be "linked" against one or multiple backend devices. All the IIO and userspace interface is expected to be registers/managed by the frontend device which will callback into the backends when needed (to get/set some configuration that it does not directly control).

Quoted 5 lines

The basic framework interface is pretty simple:
 - Backends should register themselves with @devm_iio_backend_register()
 - Frontend devices should get backends with @devm_iio_backend_get()

A few minor comments inline.

...

Quoted 7 lines
drivers/iio/industrialio- renamed from drivers/iio/industrialio-backend.c +0 -0

backend.c
new file mode 100644
index 000000000000..994bc68c2679

drivers/iio/industrialio-backend.c new file +0 -0
@@ -0,0 +1,411 @@

...

Quoted 6 lines

drivers/iio/industrialio-backend.c line 48

Quoted 4 lines
drivers/iio/industrialio-backend.c new file +4 -0
+
+/*
+ * Helper struct for requesting buffers. Allows for multiple buffers per
+ * backend.

Only seems to be used to ensure we have all the data needed to free it... So comment seems less than obviously connected to that.

I'll update the comment...

Quoted 18 lines
Quoted 18 lines

+ */

drivers/iio/industrialio-backend.c new file +16 -0
+struct iio_backend_buffer_pair {
+	struct iio_backend *back;
+	struct iio_buffer *buffer;
+};
+
 
+/**
+ * iio_backend_chan_enable - Enable a backend channel.
+ * @back:	Backend device.
+ * @chan:	Channel number.
+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */
+int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan)
+{
+	return iio_backend_op_call(back, chan_enable, chan);
Quoted 8 lines

drivers/iio/industrialio-backend.c line 122

Quoted 7 lines
drivers/iio/industrialio-backend.c new file +7 -0
+}
+EXPORT_SYMBOL_NS_GPL(iio_backend_chan_enable, IIO_BACKEND);
+
+/**
+ * iio_backend_chan_disable - Disable a backend channel.
+ * @back:	Backend device.
+ * @chan:	Channel number.

Would be good to be consistent on . or not.

Agreed.

Quoted 19 lines
Quoted 13 lines

+ *
+ * RETURNS:
+ * 0 on success, negative error number on failure.
+ */

drivers/iio/industrialio-backend.c new file +9 -0
+int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan)
+{
+	return iio_backend_op_call(back, chan_disable, chan);
+}
+EXPORT_SYMBOL_NS_GPL(iio_backend_chan_disable, IIO_BACKEND);
+
+/**
+ * iio_backend_chan_enable - Enable the backend.
+ * @back:	Backend device

...

Quoted 6 lines
Quoted 2 lines

+/**
+ * devm_iio_backend_get_from_fwnode_lookup

Not valid kernel doc + name is wrong.  Make sure you run the kernel-doc script over this and fix any errors or warnings reported.

Noted.

Quoted 47 lines
Quoted 38 lines

+ * @dev: Device where to bind the backend lifetime.
+ * @fwnode: Firmware node of the backend device.
+ *
+ * Search the backend list for a device matching @fwnode.
+ * This API should not be used and it's only present for preventing the first
+ * user of this framework to break it's DT ABI.
+ *
+ * RETURNS:
+ * A backend pointer, negative error pointer otherwise.
+ */

drivers/iio/industrialio-backend.c new file +22 -0
+struct iio_backend *
+__devm_iio_backend_get_from_fwnode_lookup(struct device *dev,
+					  struct fwnode_handle *fwnode)
+{
+	struct iio_backend *back;
+	int ret;
+
+	guard(mutex)(&iio_back_lock);
+	list_for_each_entry(back, &iio_back_list, entry) {
+		if (!device_match_fwnode(back->dev, fwnode))
+			continue;
+
+		ret = __devm_iio_backend_get(dev, back);
+		if (ret)
+			return ERR_PTR(ret);
+
+		return back;
+	}
+
+	return ERR_PTR(-EPROBE_DEFER);
+}
+EXPORT_SYMBOL_NS_GPL(__devm_iio_backend_get_from_fwnode_lookup,

IIO_BACKEND);

Quoted 8 lines

+/**
+ * devm_iio_backend_register - Register a new backend device
+ * @dev: Backend device being registered.
+ * @ops: Backend ops
+ * @priv: Device private data.
+ *
+ * @ops and @priv are both mandatory. Not providing them results in - EINVAL.

Quoted 5 lines

It's unusual to 'insist' on the private data.
Whilst it's highly likely it will always be there from a core point of view we don't mind it being NULL.  This is different from the ops as we want to be able to call those without checking they are there.

Hmm, you're right. The private is for the callers to care as we don't really touch it.

- Nuno Sá

Quoted 1 line
author maintainer
#10 lore raw

PATCH v5 7/8 iio: adc: ad9467: convert to backend framework

In reply to Nuno Sa

Convert the driver to use the new IIO backend framework. The device functionality is expected to be the same (meaning no added or removed features).

Also note this patch effectively breaks ABI and that's needed so we can properly support this device and add needed features making use of the new IIO framework.

---
 drivers/iio/adc/Kconfig  |   2 +-
 drivers/iio/adc/ad9467.c | 283 ++++++++++++++++++++++++++++++-----------------
 2 files changed, 183 insertions(+), 102 deletions(-)
drivers/iio/adc/Kconfig +1 -1
index 3b73c509bd68..6793faacb4dc 100644
@@ -291,7 +291,7 @@ config AD799X
 config AD9467
 	tristate "Analog Devices AD9467 High Speed ADC driver"
 	depends on SPI
-	depends on ADI_AXI_ADC
+	select IIO_BACKEND
 	help
 	  Say yes here to build support for Analog Devices:
 	  * AD9467 16-Bit, 200 MSPS/250 MSPS Analog-to-Digital Converter
drivers/iio/adc/ad9467.c +182 -101

474 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v5 7/8] iio: adc: ad9467: convert to backend framework

Quoted 11 lines · On Fri, 12 Jan 2024 17:40:21 +0100 Nuno Sa via B4 Relay…

From: Nuno Sa <[email protected]>

Convert the driver to use the new IIO backend framework. The device functionality is expected to be the same (meaning no added or removed features).

Also note this patch effectively breaks ABI and that's needed so we can properly support this device and add needed features making use of the new IIO framework.

Hi Nuno,

Some trivial stuff in here (not I reviewed in reverse so might make more sense read that way). Mostly little changes that will reduce what appears to be going on in this patch to the minimum possible

drivers/iio/adc/ad9467.c static const struct iio_chan_spec ad9467_channels[] = { · line 275

Quoted 5 lines
drivers/iio/adc/ad9467.c +1 -2
-static int ad9467_get_scale(struct adi_axi_adc_conv *conv, int *val, int *val2)
+static int ad9467_get_scale(struct ad9467_state *st, int *val, int *val2)
 {
-	const struct adi_axi_adc_chip_info *info = conv->chip_info;

Keep an info variable around in here as well.

Quoted 24 lines
drivers/iio/adc/ad9467.c +4 -7
-	const struct ad9467_chip_info *info1 = to_ad9467_chip_info(info);
-	struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
 	unsigned int i, vref_val;
 	int ret;
@@ -282,25 +276,23 @@ static int ad9467_get_scale(struct adi_axi_adc_conv *conv, int *val, int *val2)
 	if (ret < 0)
 		return ret;
 
-	vref_val = ret & info1->vref_mask;
+	vref_val = ret & st->info->vref_mask;
 
-	for (i = 0; i < info->num_scales; i++) {
-		if (vref_val == info->scale_table[i][1])
+	for (i = 0; i < st->info->num_scales; i++) {
+		if (vref_val == st->info->scale_table[i][1])
 			break;
 	}
 
-	if (i == info->num_scales)
+	if (i == st->info->num_scales)
 		return -ERANGE;
 
-	__ad9467_get_scale(conv, i, val, val2);

drivers/iio/adc/ad9467.c static int ad9467_get_scale(struct adi_axi_adc_conv *conv, int *val, int *val2) · line 303

Quoted 10 lines
drivers/iio/adc/ad9467.c +2 -3
+	__ad9467_get_scale(st, i, val, val2);
 
 	return IIO_VAL_INT_PLUS_MICRO;
 }
 
-static int ad9467_set_scale(struct adi_axi_adc_conv *conv, int val, int val2)
+static int ad9467_set_scale(struct ad9467_state *st, int val, int val2)
 {
-	const struct adi_axi_adc_chip_info *info = conv->chip_info;
-	struct ad9467_state *st = adi_axi_adc_conv_priv(conv);

Definitely good to keep local info variable here.

Quoted 24 lines
drivers/iio/adc/ad9467.c +3 -3
 	unsigned int scale_val[2];
 	unsigned int i;
 	int ret;
@@ -308,14 +300,14 @@ static int ad9467_set_scale(struct adi_axi_adc_conv *conv, int val, int val2)
 	if (val != 0)
 		return -EINVAL;
 
-	for (i = 0; i < info->num_scales; i++) {
-		__ad9467_get_scale(conv, i, &scale_val[0], &scale_val[1]);
+	for (i = 0; i < st->info->num_scales; i++) {
+		__ad9467_get_scale(st, i, &scale_val[0], &scale_val[1]);
 		if (scale_val[0] != val || scale_val[1] != val2)
 			continue;
 
 		guard(mutex)(&st->lock);
 		ret = ad9467_spi_write(st->spi, AN877_ADC_REG_VREF,
-				       info->scale_table[i][1]);
+				       st->info->scale_table[i][1]);
 		if (ret < 0)
 			return ret;
 

}

drivers/iio/adc/ad9467.c static int ad9467_read_raw(struct adi_axi_adc_conv *conv, · line 343

Quoted 8 lines
drivers/iio/adc/ad9467.c +2 -3
-static int ad9467_write_raw(struct adi_axi_adc_conv *conv,
+static int ad9467_write_raw(struct iio_dev *indio_dev,
 			    struct iio_chan_spec const *chan,
 			    int val, int val2, long mask)
 {
-	const struct adi_axi_adc_chip_info *info = conv->chip_info;
-	struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
+	struct ad9467_state *st = iio_priv(indio_dev);

Even here I'd keep a local info variable as it's harmless and reduces what is changed in this patch.

Quoted 16 lines
drivers/iio/adc/ad9467.c +2 -2
 	long r_clk;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
-		return ad9467_set_scale(conv, val, val2);
+		return ad9467_set_scale(st, val, val2);
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		r_clk = clk_round_rate(st->clk, val);
-		if (r_clk < 0 || r_clk > info->max_rate) {
+		if (r_clk < 0 || r_clk > st->info->max_rate) {
 			dev_warn(&st->spi->dev,
 				 "Error setting ADC sample rate %ld", r_clk);
 			return -EINVAL;
@@ -369,26 +360,53 @@ static int ad9467_write_raw(struct adi_axi_adc_conv *conv,
 	}
 }

drivers/iio/adc/ad9467.c static int ad9467_write_raw(struct adi_axi_adc_conv *conv, · line 368

Quoted 10 lines
drivers/iio/adc/ad9467.c +2 -3
 
-static int ad9467_read_avail(struct adi_axi_adc_conv *conv,
+static int ad9467_read_avail(struct iio_dev *indio_dev,
 			     struct iio_chan_spec const *chan,
 			     const int **vals, int *type, int *length,
 			     long mask)
 {
-	const struct adi_axi_adc_chip_info *info = conv->chip_info;
-	struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
+	struct ad9467_state *st = iio_priv(indio_dev);

As below, I'd keep the local info variable to reduce scope of changes to the minimum.

Quoted 20 lines
drivers/iio/adc/ad9467.c +2 -1
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
 		*vals = (const int *)st->scales;
 		*type = IIO_VAL_INT_PLUS_MICRO;
 		/* Values are stored in a 2D matrix */
-		*length = info->num_scales * 2;
+		*length = st->info->num_scales * 2;
 		return IIO_AVAIL_LIST;
 	default:
 		return -EINVAL;
 	}
 }
 
 
+
 static int ad9467_outputmode_set(struct spi_device *spi, unsigned int mode)
 {
 	int ret;

drivers/iio/adc/ad9467.c static int ad9467_outputmode_set(struct spi_device *spi, unsigned int mode) · line 424

Quoted 9 lines
drivers/iio/adc/ad9467.c +1 -3
@@ -401,19 +419,17 @@ static int ad9467_outputmode_set(struct spi_device *spi, unsigned int mode)
 				AN877_ADC_TRANSFER_SYNC);
 }
 
-static int ad9467_scale_fill(struct adi_axi_adc_conv *conv)
+static int ad9467_scale_fill(struct ad9467_state *st)
 {
-	const struct adi_axi_adc_chip_info *info = conv->chip_info;
-	struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
 	unsigned int i, val1, val2;
	const struct adi_axi_adc_chip_info *info = st->info;

I think...

Makes this patch more minimal which is nice and it's not a bad change in it's own right..

Same with some other cases changed by this patch.

Quoted 20 lines
drivers/iio/adc/ad9467.c +4 -4
-	st->scales = devm_kmalloc_array(&st->spi->dev, info->num_scales,
+	st->scales = devm_kmalloc_array(&st->spi->dev, st->info->num_scales,
 					sizeof(*st->scales), GFP_KERNEL);
 	if (!st->scales)
 		return -ENOMEM;
 
-	for (i = 0; i < info->num_scales; i++) {
-		__ad9467_get_scale(conv, i, &val1, &val2);
+	for (i = 0; i < st->info->num_scales; i++) {
+		__ad9467_get_scale(st, i, &val1, &val2);
 		st->scales[i][0] = val1;
 		st->scales[i][1] = val2;
 	}
@@ -421,11 +437,27 @@ static int ad9467_scale_fill(struct adi_axi_adc_conv *conv)
 	return 0;
 }
 
-static int ad9467_preenable_setup(struct adi_axi_adc_conv *conv)
+static int ad9467_setup(struct ad9467_state *st)

drivers/iio/adc/ad9467.c static int ad9467_scale_fill(struct adi_axi_adc_conv *conv) · line 449

Quoted 10 lines
drivers/iio/adc/ad9467.c +8 -1
 {
-	struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
+	struct iio_backend_data_fmt data = {
+		.sign_extend = true,
+		.enable = true,
+	};
+	unsigned int c, mode;
+	int ret;
+
+	mode = st->info->default_output_mode | AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT;

Bit of a long line. Perhaps break it after the |

Quoted 20 lines
drivers/iio/adc/ad9467.c +11 -1
+	ret = ad9467_outputmode_set(st->spi, mode);
+	if (ret)
+		return ret;
 
-	return ad9467_outputmode_set(st->spi, st->output_mode);
+	for (c = 0; c < st->info->num_channels; c++) {
+		ret = iio_backend_data_format_set(st->back, c, &data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
 }
 
 static int ad9467_reset(struct device *dev)
@@ -443,25 +475,64 @@ static int ad9467_reset(struct device *dev)
 	return 0;
 }
 
+static int ad9467_iio_backend_get(struct ad9467_state *st)

drivers/iio/adc/ad9467.c static int ad9467_reset(struct device *dev) · line 488

Quoted 10 lines
drivers/iio/adc/ad9467.c +10 -0
+{
+	struct device *dev = &st->spi->dev;
+	struct device_node *__back;
+
+	st->back = devm_iio_backend_get(&st->spi->dev, NULL);
+	/* If not found, don't error out as we might have legacy DT property */
+	if (IS_ERR(st->back) && PTR_ERR(st->back) != -ENOENT)
+		return PTR_ERR(st->back);
+	if (!IS_ERR(st->back))
+		return 0;

Why not do this one first? I know I normally moan about having error handlers out of line, but in this case the good is out of line whatever.

	if (!IS_ERR(st->back)
		return 0;

	if (PTR_ERR(st->back) != ENOENT)
		return PTR_ERR(st->back);

	...
Quoted 38 lines
drivers/iio/adc/ad9467.c +31 -2
+	/*
+	 * if we don't get the backend using the normal API's, use the legacy
+	 * 'adi,adc-dev' property. So we get all nodes with that property, and
+	 * look for the one pointing at us. Then we directly lookup that fwnode
+	 * on the backend list of registered devices. This is done so we don't
+	 * make io-backends mandatory which would break DT ABI.
+	 */
+	for_each_node_with_property(__back, "adi,adc-dev") {
+		struct device_node *__me;
+
+		__me = of_parse_phandle(__back, "adi,adc-dev", 0);
+		if (!__me)
+			continue;
+
+		if (!device_match_of_node(dev, __me)) {
+			of_node_put(__me);
+			continue;
+		}
+
+		of_node_put(__me);
+		st->back = __devm_iio_backend_get_from_fwnode_lookup(dev,
+								     of_fwnode_handle(__back));
+		of_node_put(__back);
+		return PTR_ERR_OR_ZERO(st->back);
+	}
+
+	return -ENODEV;
+}
+
 static int ad9467_probe(struct spi_device *spi)
 {
 
 
 
-	st->output_mode = info->default_output_mode |
-			  AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT;
+	ret = ad9467_iio_backend_get(st);
+	if (ret)

drivers/iio/adc/ad9467.c static int ad9467_reset(struct device *dev)

Quoted 10 lines
drivers/iio/adc/ad9467.c +8 -1
+		return ret;
 
-	return 0;
+	ret = devm_iio_backend_request_buffer(&spi->dev, st->back, indio_dev);
+	if (ret)
+		return ret;
+
+	ret = iio_backend_enable(st->back);
+	if (ret)
+		return ret;

I'm curious there is no iio_backend_disable() to be done in the exit path?

Quoted 16 lines
drivers/iio/adc/ad9467.c +7 -1
+
+	ret = ad9467_setup(st);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(&spi->dev, indio_dev);
 }
 
 static const struct of_device_id ad9467_of_match[] = {
@@ -529,4 +610,4 @@ module_spi_driver(ad9467_driver);
 MODULE_AUTHOR("Michael Hennerich <[email protected]>");
 MODULE_DESCRIPTION("Analog Devices AD9467 ADC driver");
 MODULE_LICENSE("GPL v2");
-MODULE_IMPORT_NS(IIO_ADI_AXI);
+MODULE_IMPORT_NS(IIO_BACKEND);
#12 lore raw

Re: [PATCH v5 7/8] iio: adc: ad9467: convert to backend framework

Quoted 38 lines · Jonathan Cameron
Quoted 11 lines · On Fri, 12 Jan 2024 17:40:21 +0100 Nuno Sa via B4 Relay…

From: Nuno Sa <[email protected]>

Convert the driver to use the new IIO backend framework. The device functionality is expected to be the same (meaning no added or removed features).

Also note this patch effectively breaks ABI and that's needed so we can properly support this device and add needed features making use of the new IIO framework.

Hi Nuno,

Some trivial stuff in here (not I reviewed in reverse so might make more sense read that way).  Mostly little changes that will reduce what appears to be going on in this patch to the minimum possible

Quoted 14 lines

+
 static int ad9467_outputmode_set(struct spi_device *spi, unsigned int mode) {
  int ret;

drivers/iio/adc/ad9467.c +0 -0
@@ -401,19 +419,17 @@ static int ad9467_outputmode_set(struct spi_device
 *spi, unsigned int mode)
  				AN877_ADC_TRANSFER_SYNC);
  }

drivers/iio/adc/ad9467.c +1 -1
-static int ad9467_scale_fill(struct adi_axi_adc_conv *conv)
+static int ad9467_scale_fill(struct ad9467_state *st)

 {

(quoted diff) +0 -2
-	const struct adi_axi_adc_chip_info *info = conv->chip_info;
-	struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
Quoted 1 line

  unsigned int i, val1, val2;

	const struct adi_axi_adc_chip_info *info = st->info;
Quoted 6 lines

I think...

Makes this patch more minimal which is nice and it's not a bad change in it's own right..

Same with some other cases changed by this patch.

Alright, will do it in all the places you pointed out...

Quoted 28 lines
Quoted 28 lines

(quoted diff) +1 -1
-	st->scales = devm_kmalloc_array(&st->spi->dev, info->num_scales,
+	st->scales = devm_kmalloc_array(&st->spi->dev, st->info-
Quoted 1 line

num_scales,

  sizeof(*st->scales), GFP_KERNEL); if (!st->scales)
  return -ENOMEM;

drivers/iio/adc/ad9467.c +2 -2
-	for (i = 0; i < info->num_scales; i++) {
-		__ad9467_get_scale(conv, i, &val1, &val2);
+	for (i = 0; i < st->info->num_scales; i++) {
+		__ad9467_get_scale(st, i, &val1, &val2);

  st->scales[i][0] = val1;
  st->scales[i][1] = val2;
  }

drivers/iio/adc/ad9467.c +0 -0
@@ -421,11 +437,27 @@ static int ad9467_scale_fill(struct adi_axi_adc_conv
 *conv)
  	return 0;
  }

drivers/iio/adc/ad9467.c +1 -1
-static int ad9467_preenable_setup(struct adi_axi_adc_conv *conv)
+static int ad9467_setup(struct ad9467_state *st)

 {

drivers/iio/adc/ad9467.c +3 -1
-	struct ad9467_state *st = adi_axi_adc_conv_priv(conv);
+	struct iio_backend_data_fmt data = {
+		.sign_extend = true,
+		.enable = true,
Quoted 8 lines
Quoted 6 lines
drivers/iio/adc/ad9467.c +5 -0
+	};
+	unsigned int c, mode;
+	int ret;
+
+	mode = st->info->default_output_mode |

AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT;

Bit of a long line. Perhaps break it after the |

One of those cases where I think going beyond the 80 limit col helps readability. But I can break the line if that's your preference.

Quoted 35 lines
Quoted 28 lines
drivers/iio/adc/ad9467.c +3 -0
+	ret = ad9467_outputmode_set(st->spi, mode);
+	if (ret)
+		return ret;

drivers/iio/adc/ad9467.c +7 -1
-	return ad9467_outputmode_set(st->spi, st->output_mode);
+	for (c = 0; c < st->info->num_channels; c++) {
+		ret = iio_backend_data_format_set(st->back, c, &data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;

 }

 static int ad9467_reset(struct device *dev)

(quoted diff) +0 -0
@@ -443,25 +475,64 @@ static int ad9467_reset(struct device *dev)
  	return 0;
  }

drivers/iio/adc/ad9467.c +7 -0
+static int ad9467_iio_backend_get(struct ad9467_state *st)
+{
+	struct device *dev = &st->spi->dev;
+	struct device_node *__back;
+
+	st->back = devm_iio_backend_get(&st->spi->dev, NULL);
+	/* If not found, don't error out as we might have legacy DT

property */

drivers/iio/adc/ad9467.c static int ad9467_reset(struct device *dev) · line 488

Quoted 4 lines
drivers/iio/adc/ad9467.c +4 -0
+	if (IS_ERR(st->back) && PTR_ERR(st->back) != -ENOENT)
+		return PTR_ERR(st->back);
+	if (!IS_ERR(st->back))
+		return 0;

Why not do this one first? I know I normally moan about having error handlers out of line, but in this case the good is out of line whatever.

Quoted 7 lines
	if (!IS_ERR(st->back)
		return 0;

	if (PTR_ERR(st->back) != ENOENT)
		return PTR_ERR(st->back);

	...

Yeah, really because I wanted to have the error condition checked first. But I agree in this particular case it makes the code simpler by not doing it that way.

Quoted 50 lines
Quoted 50 lines
(quoted diff) +2 -0
+	/*
+	 * if we don't get the backend using the normal API's, use the

legacy

(quoted diff) +1 -0
+	 * 'adi,adc-dev' property. So we get all nodes with that property,

and

(quoted diff) +1 -0
+	 * look for the one pointing at us. Then we directly lookup that

fwnode

(quoted diff) +1 -0
+	 * on the backend list of registered devices. This is done so we

don't

drivers/iio/adc/ad9467.c +17 -0
+	 * make io-backends mandatory which would break DT ABI.
+	 */
+	for_each_node_with_property(__back, "adi,adc-dev") {
+		struct device_node *__me;
+
+		__me = of_parse_phandle(__back, "adi,adc-dev", 0);
+		if (!__me)
+			continue;
+
+		if (!device_match_of_node(dev, __me)) {
+			of_node_put(__me);
+			continue;
+		}
+
+		of_node_put(__me);
+		st->back = __devm_iio_backend_get_from_fwnode_lookup(dev,
+								    

of_fwnode_handle(__back));

drivers/iio/adc/ad9467.c +7 -0
+		of_node_put(__back);
+		return PTR_ERR_OR_ZERO(st->back);
+	}
+
+	return -ENODEV;
+}
+

 static int ad9467_probe(struct spi_device *spi)
 {

drivers/iio/adc/ad9467.c +3 -2
-	st->output_mode = info->default_output_mode |
-			  AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT;
+	ret = ad9467_iio_backend_get(st);
+	if (ret)
+		return ret;

(quoted diff) +1 -1
-	return 0;
+	ret = devm_iio_backend_request_buffer(&spi->dev, st->back,

indio_dev);

Quoted 9 lines

drivers/iio/adc/ad9467.c static int ad9467_probe(struct spi_device *spi) · line 575

Quoted 6 lines
drivers/iio/adc/ad9467.c +6 -0
+	if (ret)
+		return ret;
+
+	ret = iio_backend_enable(st->back);
+	if (ret)
+		return ret;

I'm curious there is no iio_backend_disable() to be done in the exit path?

Ehehe something I have in my mind, yes. I'm just not disabling the core because it was the same with the previous approach. My goal was to have (more or less) the same state before vs after introducing the backend. I was thinking in adding a devm_iio_backend_enable() as a follow up patch and use it in here (or actually use it for the first axi-dac/dds user as that one will be come from a "clean" state).

If you prefer I can already turn iio_backend_enable() -> devm_iio_backend_enable() and use it in this patch.

- Nuno Sá

Quoted 1 line
#13 lore raw

Re: [PATCH v5 7/8] iio: adc: ad9467: convert to backend framework

Quoted 16 lines
Quoted 8 lines

drivers/iio/adc/ad9467.c static int ad9467_probe(struct spi_device *spi) · line 575

Quoted 5 lines
drivers/iio/adc/ad9467.c +5 -0
+		return ret;
+
+	ret = iio_backend_enable(st->back);
+	if (ret)
+		return ret;  

I'm curious there is no iio_backend_disable() to be done in the exit path?

Ehehe something I have in my mind, yes. I'm just not disabling the core because it was the same with the previous approach. My goal was to have (more or less) the same state before vs after introducing the backend. I was thinking in adding a devm_iio_backend_enable() as a follow up patch and use it in here (or actually use it for the first axi-dac/dds user as that one will be come from a "clean" state).

Quoted 2 lines

If you prefer I can already turn iio_backend_enable() -> devm_iio_backend_enable() and use it in this patch.

Might be cleaner to do that. Or add a big fat comment here to say it'll get cleaned up in a follow up patch.

Quoted 3 lines

- Nuno Sá

Quoted 1 line
author maintainer
#14 lore raw

PATCH v5 8/8 iio: adc: adi-axi-adc: move to backend framework

In reply to Nuno Sa

Move to the IIO backend framework. Devices supported by adi-axi-adc now register themselves as backend devices.

---
 drivers/iio/adc/Kconfig             |   2 +-
 drivers/iio/adc/adi-axi-adc.c       | 380 +++++++++---------------------------
 include/linux/iio/adc/adi-axi-adc.h |  68 -------
 3 files changed, 93 insertions(+), 357 deletions(-)
drivers/iio/adc/Kconfig +1 -1
index 6793faacb4dc..02a55809cb6a 100644
@@ -309,7 +309,7 @@ config ADI_AXI_ADC
 	select IIO_BUFFER_HW_CONSUMER
 	select IIO_BUFFER_DMAENGINE
 	select REGMAP_MMIO
-	depends on OF
+	select IIO_BACKEND
 	help
 	  Say yes here to build support for Analog Devices Generic
 	  AXI ADC IP core. The IP core is used for interfacing with
drivers/iio/adc/adi-axi-adc.c +92 -288

494 lines not on this page · raw message

include/linux/iio/adc/adi-axi-adc.h deleted +0 -68

68 lines not on this page · raw message

Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH v5 8/8] iio: adc: adi-axi-adc: move to backend framework

Quoted 6 lines · On Fri, 12 Jan 2024 17:40:22 +0100 Nuno Sa via B4 Relay…

From: Nuno Sa <[email protected]>

Move to the IIO backend framework. Devices supported by adi-axi-adc now register themselves as backend devices.

A few quick drive by comments whist I wait for a build to finish...

Quoted 7 lines
drivers/iio/adc/adi-axi-adc.c +0 -0
index 0f21d1d98b9f..741b53c25bb1 100644
@@ -8,6 +8,7 @@
 >  static int adi_axi_adc_probe(struct platform_device *pdev)
 {

...

drivers/iio/adc/adi-axi-adc.c static int adi_axi_adc_probe(struct platform_device *pdev) · line 210

Quoted 8 lines
drivers/iio/adc/adi-axi-adc.c +1 -1
@@ -390,37 +205,23 @@ static int adi_axi_adc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	if (cl->info->version > ver) {
+	if (*expected_ver > ver) {
 		dev_err(&pdev->dev,
 			"IP core version is too old. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n",

Format doesn't match with later.

Quoted 23 lines
drivers/iio/adc/adi-axi-adc.c +4 -10
-			ADI_AXI_PCORE_VER_MAJOR(cl->info->version),
-			ADI_AXI_PCORE_VER_MINOR(cl->info->version),
-			ADI_AXI_PCORE_VER_PATCH(cl->info->version),
+			ADI_AXI_PCORE_VER_MAJOR(*expected_ver),
+			ADI_AXI_PCORE_VER_MINOR(*expected_ver),
+			ADI_AXI_PCORE_VER_PATCH(*expected_ver),
 			ADI_AXI_PCORE_VER_MAJOR(ver),
 			ADI_AXI_PCORE_VER_MINOR(ver),
 			ADI_AXI_PCORE_VER_PATCH(ver));
 		return -ENODEV;
 	}
 
-	indio_dev->info = &adi_axi_adc_info;
-	indio_dev->name = "adi-axi-adc";
-	indio_dev->modes = INDIO_DIRECT_MODE;
-	indio_dev->num_channels = conv->chip_info->num_channels;
-	indio_dev->channels = conv->chip_info->channels;
-
-	ret = adi_axi_adc_config_dma_buffer(&pdev->dev, indio_dev);
+	ret = devm_iio_backend_register(&pdev->dev, &adi_axi_adc_generic, st);
 	if (ret)
 		return ret;
 

drivers/iio/adc/adi-axi-adc.c static int adi_axi_adc_probe(struct platform_device *pdev) · line 224

Quoted 10 lines
drivers/iio/adc/adi-axi-adc.c +1 -9
-	ret = adi_axi_adc_setup_channels(&pdev->dev, st);
-	if (ret)
-		return ret;
-
-	ret = devm_iio_device_register(&pdev->dev, indio_dev);
-	if (ret)
-		return ret;
-
-	dev_info(&pdev->dev, "AXI ADC IP core (%d.%.2d.%c) probed\n",
+	dev_info(&pdev->dev, "AXI ADC IP core (%d.%.2d.%d) probed\n",

I'd rip this (I think) unrelated change out to reduce noise in here somewhat. I'm curious though as it's still %c above.

Quoted 6 lines
drivers/iio/adc/adi-axi-adc.c +0 -0
 		 ADI_AXI_PCORE_VER_MAJOR(ver),
 		 ADI_AXI_PCORE_VER_MINOR(ver),
 		 ADI_AXI_PCORE_VER_PATCH(ver));
@@ -428,6 +229,8 @@ static int adi_axi_adc_probe(struct platform_device *pdev)
 	return 0;
 }
#16 lore raw

Re: [PATCH v5 8/8] iio: adc: adi-axi-adc: move to backend framework

Quoted 62 lines · Jonathan Cameron
Quoted 6 lines · On Fri, 12 Jan 2024 17:40:22 +0100 Nuno Sa via B4 Relay…

From: Nuno Sa <[email protected]>

Move to the IIO backend framework. Devices supported by adi-axi-adc now register themselves as backend devices.

A few quick drive by comments whist I wait for a build to finish...

Quoted 7 lines
drivers/iio/adc/adi-axi-adc.c +0 -0
index 0f21d1d98b9f..741b53c25bb1 100644
@@ -8,6 +8,7 @@
 >  static int adi_axi_adc_probe(struct platform_device *pdev)
  {

...

Quoted 10 lines
drivers/iio/adc/adi-axi-adc.c +0 -0
@@ -390,37 +205,23 @@ static int adi_axi_adc_probe(struct platform_device
 *pdev)
  	if (ret)
  		return ret;

drivers/iio/adc/adi-axi-adc.c +1 -1
-	if (cl->info->version > ver) {
+	if (*expected_ver > ver) {

  dev_err(&pdev->dev,
  "IP core version is too old. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n",

Format doesn't match with later.

Quoted 28 lines
drivers/iio/adc/adi-axi-adc.c +3 -3
-			ADI_AXI_PCORE_VER_MAJOR(cl->info->version),
-			ADI_AXI_PCORE_VER_MINOR(cl->info->version),
-			ADI_AXI_PCORE_VER_PATCH(cl->info->version),
+			ADI_AXI_PCORE_VER_MAJOR(*expected_ver),
+			ADI_AXI_PCORE_VER_MINOR(*expected_ver),
+			ADI_AXI_PCORE_VER_PATCH(*expected_ver),

  ADI_AXI_PCORE_VER_MAJOR(ver), ADI_AXI_PCORE_VER_MINOR(ver), ADI_AXI_PCORE_VER_PATCH(ver)); return -ENODEV;
  }

drivers/iio/adc/adi-axi-adc.c +1 -7
-	indio_dev->info = &adi_axi_adc_info;
-	indio_dev->name = "adi-axi-adc";
-	indio_dev->modes = INDIO_DIRECT_MODE;
-	indio_dev->num_channels = conv->chip_info->num_channels;
-	indio_dev->channels = conv->chip_info->channels;
-
-	ret = adi_axi_adc_config_dma_buffer(&pdev->dev, indio_dev);
+	ret = devm_iio_backend_register(&pdev->dev, &adi_axi_adc_generic,

st);
  if (ret)
  return ret;

drivers/iio/adc/adi-axi-adc.c +0 -4
-	ret = adi_axi_adc_setup_channels(&pdev->dev, st);
-	if (ret)
-		return ret;
-
Quoted 8 lines

drivers/iio/adc/adi-axi-adc.c static int adi_axi_adc_probe(struct platform_device *pdev) · line 224

Quoted 6 lines
drivers/iio/adc/adi-axi-adc.c +1 -5
-	ret = devm_iio_device_register(&pdev->dev, indio_dev);
-	if (ret)
-		return ret;
-
-	dev_info(&pdev->dev, "AXI ADC IP core (%d.%.2d.%c) probed\n",
+	dev_info(&pdev->dev, "AXI ADC IP core (%d.%.2d.%d) probed\n",

I'd rip this (I think) unrelated change out to reduce noise in here somewhat. I'm curious though as it's still %c above.

Oh, just forgot about the above %c... Yeah, the %c format gives a crappy output. That's why I changed it :)

- Nuno Sá