Skip to contentSkip to conversation

iio: add new backend framework

PATCH Superseded Nuno Sa 7 participants 27 messages Dec 20, 2023 – Jan 11, 2024

Subsystems (7)

v4 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Nuno Sa

author maintainer
#0 lore raw

v4 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]/

Changes in v4:
- Patch 1:
* Don't make io-backends mandatory.
- Patch 5:
* Add helper __devm_iio_backend_get();
* Add new devm_iio_backend_get_optional();
* Add new devm_iio_backend_get_from_fwnode_lookup();
* Convert all dev_ logs to plain pr_ logs so we actually use pr_fmt.
- Patch 6:
* Implement the fallback backend lookput in case

    devm_iio_backend_get_optional() fails;

So the main difference in this series is to still handle 'adi,adc-dev' in case 'io-backends' is not given. Hence we don't need to make it mandatory and break dt ABI for the ad9467 bindings. Rob, it would be nice if you can take a quick a look on it and let me know if there's a more efficient way of doing things.

Rob also already expressed that he is ok with the dt ABI breakage as long as it is properly justified in the commit message. Jonathan, we should see if we keep things as in this version or rollback. Personally I'm not that "happy" with that devm_iio_backend_get_from_fwnode_lookup() given the fact that is not really supposed to be used anymore. OTOH, it's small and simple... So, let me know your preference and I'll go with that :).

This is likely the last version of this series for this year. If things go well, I plan to start working on the second user of the framework right in the beginning of the next year...

Keeping the block diagram in v3's cover 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: deprecate 'adi,adc-dev'
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   |   4 +-
 MAINTAINERS                                        |   8 +
 drivers/base/core.c                                |  14 +-
 drivers/iio/Kconfig                                |   5 +
 drivers/iio/Makefile                               |   1 +
 drivers/iio/adc/Kconfig                            |   4 +-
 drivers/iio/adc/ad9467.c                           | 282 ++++++++-----
 drivers/iio/adc/adi-axi-adc.c                      | 381 +++++------------
 drivers/iio/buffer/industrialio-buffer-dmaengine.c |   8 +-
 drivers/iio/industrialio-backend.c                 | 456 +++++++++++++++++++++
 drivers/of/property.c                              |   2 +
 include/linux/iio/adc/adi-axi-adc.h                |  68 ---
 include/linux/iio/backend.h                        |  75 ++++
 include/linux/iio/buffer-dmaengine.h               |   3 +
 15 files changed, 845 insertions(+), 470 deletions(-)

---
base-commit: 2dfef50589aef3b9a2fa2190ae95b328fb664f89
change-id: 20231219-iio-backend-a3dc1a6a7a58 --

Thanks!
- Nuno Sá

author maintainer
#1 lore raw

PATCH v4 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
maintainer
#2 lore raw

Re: [PATCH v4 1/8] dt-bindings: adc: ad9467: add new io-backend property

Quoted 10 lines · Nuno Sa

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(+)

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:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/iio/adc/adi,ad9467.yaml: io-backends: missing type definition

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.

maintainer
#3 lore raw

Re: [PATCH v4 1/8] dt-bindings: adc: ad9467: add new io-backend property

Quoted 12 lines · On Wed, 20 Dec 2023 10:56:24 -0600 Rob Herring <[email protected]> wrote:
Quoted 10 lines · Nuno Sa

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(+)
Quoted 7 lines

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:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/iio/adc/adi,ad9467.yaml: io-backends: missing type definition

For reference this is expected as if this goes ahead that will be a dtschema addition.

Quoted 18 lines

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.

maintainer Reviewed-by on v4 1/8
#4 lore raw

Re: [PATCH v4 1/8] dt-bindings: adc: ad9467: add new io-backend property

In reply to Nuno Sa
Quoted 10 lines · Nuno Sa

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(+)
author maintainer
#5 lore raw

PATCH v4 2/8 dt-bindings: adc: axi-adc: deprecate 'adi,adc-dev'

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.

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

Re: [PATCH v4 2/8] dt-bindings: adc: axi-adc: deprecate 'adi,adc-dev'

Quoted 2 lines · On Wed, 20 Dec 2023 16:34:05 +0100 Nuno Sa <[email protected]> wrote:

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

With my 'specifications language' brain engaged (also know as pedantic) I think this is a 'should' not a 'must' case. You aren't breaking backwards compatibility just advising moving to the newer / better interface.

Quoted 20 lines

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.

---
 Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml +0 -0
index 9996dd93f84b..835b40063343 100644

Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml properties: · line 47

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml +1 -1
@@ -39,12 +39,12 @@ properties:
     $ref: /schemas/types.yaml#/definitions/phandle
     description:
       A reference to a the actual ADC to which this FPGA ADC interfaces to.
+    deprecated: true
 
 required:
   - compatible
   - dmas
   - reg
-  - adi,adc-dev

Dropping it from required is fine, but do we have a new condition where one or the other should be required? If so good to add the dt-binding magic to enforce that. Look for a oneOf combined with required. There are a few IIO examples of this either or type required. You may want to then enforce that both are not provided though I guess we perhaps don't care - the driver will just prioritise one approach over the other.

Jonathan

Quoted 12 lines
Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml +0 -2
 additionalProperties: false
 
@@ -55,7 +55,5 @@ examples:
         reg = <0x44a00000 0x10000>;
         dmas = <&rx_dma 0>;
         dma-names = "rx";
-
-        adi,adc-dev = <&spi_adc>;
     };
...
#7 lore raw

Re: [PATCH v4 2/8] dt-bindings: adc: axi-adc: deprecate 'adi,adc-dev'

Quoted 10 lines · Jonathan Cameron
Quoted 2 lines · On Wed, 20 Dec 2023 16:34:05 +0100 Nuno Sa <[email protected]> wrote:

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

With my 'specifications language' brain engaged (also know as pedantic) I think this is a 'should' not a 'must' case. You aren't breaking backwards compatibility just advising moving to the newer / better interface.

Well, you surely know better than me as a native speaker :)

Quoted 33 lines
Quoted 24 lines

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.

---
 Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml | 4 +---

 1 file changed, 1 insertion(+), 3 deletions(-)
Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml +0 -0
index 9996dd93f84b..835b40063343 100644
@@ -39,12 +39,12 @@ properties:

     $ref: /schemas/types.yaml#/definitions/phandle description:

Quoted 8 lines

       A reference to a the actual ADC to which this FPGA ADC interfaces to. +    deprecated: true

 required:
   - compatible
   - dmas
   - reg
-  - adi,adc-dev

Quoted 8 lines

Dropping it from required is fine, but do we have a new condition where one or the other
should be required?  If so good to add the dt-binding magic to enforce that. Look for a oneOf combined with required. There are a few IIO examples of this either or type required. You may want to then enforce that both are not provided though I guess we perhaps don't care - the driver will just prioritise one approach over the other.

Hmm, the thing is that io-backends is applied in the frontend device (so other binding) and in here we should only have the adi,adc-dev which is now deprecated so I'm not sure how that would look like?

I think new users of the deprecated property are very unlikely unless they choose to ignore the deprecated warning. As for old users (if they add the new one and don't remove this one, the new one will have priority). But I'm still confident there are no users of this out there :)

- Nuno Sá

maintainer
#8 lore raw

Re: [PATCH v4 2/8] dt-bindings: adc: axi-adc: deprecate 'adi,adc-dev'

Quoted 56 lines · On Fri, 22 Dec 2023 10:07:34 +0100 Nuno Sá <[email protected]> wrote:
Quoted 10 lines · Jonathan Cameron
Quoted 2 lines · On Wed, 20 Dec 2023 16:34:05 +0100 Nuno Sa <[email protected]> wrote:

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

With my 'specifications language' brain engaged (also know as pedantic) I think this is a 'should' not a 'must' case. You aren't breaking backwards compatibility just advising moving to the newer / better interface.

Well, you surely know better than me as a native speaker :)

Quoted 33 lines
Quoted 24 lines

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.

---
 Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml | 4 +---

 1 file changed, 1 insertion(+), 3 deletions(-)
Documentation/devicetree/bindings/iio/adc/adi,axi-adc.yaml +0 -0
index 9996dd93f84b..835b40063343 100644
@@ -39,12 +39,12 @@ properties:

     $ref: /schemas/types.yaml#/definitions/phandle description:

Quoted 8 lines

       A reference to a the actual ADC to which this FPGA ADC interfaces to. +    deprecated: true

 required:
   - compatible
   - dmas
   - reg
-  - adi,adc-dev

Quoted 8 lines

Dropping it from required is fine, but do we have a new condition where one or the other
should be required?  If so good to add the dt-binding magic to enforce that. Look for a oneOf combined with required. There are a few IIO examples of this either or type required. You may want to then enforce that both are not provided though I guess we perhaps don't care - the driver will just prioritise one approach over the other.

Quoted 3 lines

Hmm, the thing is that io-backends is applied in the frontend device (so other binding) and in here we should only have the adi,adc-dev which is now deprecated so I'm not sure how that would look like?

Ah. I'd somehow failed to register the property is now in the other device. Not much we can do then :(

Quoted 5 lines

I think new users of the deprecated property are very unlikely unless they choose to ignore the deprecated warning. As for old users (if they add the new one and don't remove this one, the new one will have priority). But I'm still confident there are no users of this out there :(

Sometimes it's easier to cater for non existent users than get into too much debate on whether they exist :)

Jonathan

Quoted 5 lines

- Nuno Sá

author maintainer
#9 lore raw

PATCH v4 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
#10 lore raw

PATCH v4 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..a4835447759f 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", NULL)
 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
reviewer
#12 lore raw

Re: [PATCH v4 4/8] of: property: add device link support for io-backends

In reply to Nuno Sa
Quoted 16 lines · 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 +0 -0
index afdaefbd03f6..a4835447759f 100644

drivers/of/property.c DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells") · line 1247

Quoted 4 lines
drivers/of/property.c +1 -0
@@ -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", NULL)

In v3 it was agreed that adding #io-backend-cells right way seemed like a good idea. Do we need to include that here?

Quoted 16 lines
drivers/of/property.c +1 -1
 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, },
 
--

2.43.0

#13 lore raw

Re: [PATCH v4 4/8] of: property: add device link support for io-backends

Quoted 12 lines · David Lechner
Quoted 11 lines · 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(+)
Quoted 14 lines

drivers/of/property.c DEFINE_SIMPLE_PROP(interconnects, "interconnects", · line 1248

Quoted 10 lines
drivers/of/property.c +1 -0
index afdaefbd03f6..a4835447759f 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", NULL)

In v3 it was agreed that adding #io-backend-cells right way seemed like a good idea. Do we need to include that here?

Yeah that's something I forgot and should do in v5. Should also update the core code and the backend bindings (axi-adc) to have it (one of Rob's points in having the #cells was to easily identify backends - providers).

Thanks for spotting this!
- Nuno Sá

Quoted 1 line
author maintainer
#14 lore raw

PATCH v4 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
#15 lore raw

PATCH v4 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                |   5 +
 drivers/iio/Makefile               |   1 +
 drivers/iio/industrialio-backend.c | 456 +++++++++++++++++++++++++++++++++++++
 include/linux/iio/backend.h        |  75 ++++++
 5 files changed, 545 insertions(+)
MAINTAINERS +8 -0
index 3029841e92a8..df5f5b988926 100644
@@ -10334,6 +10334,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 +5 -0
index 52eb46ef84c1..451671112f73 100644
@@ -71,6 +71,11 @@ 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.
+
 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 +456 -0

456 lines not on this page · raw message

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

75 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#16 lore raw

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

Quoted 8 lines · On Wed, 20 Dec 2023 16:34:09 +0100 Nuno Sa <[email protected]> wrote:

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, but seems good to me otherwise.

Jonathan

Quoted 32 lines
---
 MAINTAINERS                        |   8 +
 drivers/iio/Kconfig                |   5 +
 drivers/iio/Makefile               |   1 +
 drivers/iio/industrialio-backend.c | 456 +++++++++++++++++++++++++++++++++++++
 include/linux/iio/backend.h        |  75 ++++++
 5 files changed, 545 insertions(+)
MAINTAINERS +8 -0
index 3029841e92a8..df5f5b988926 100644
@@ -10334,6 +10334,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 +0 -0
index 52eb46ef84c1..451671112f73 100644

drivers/iio/Kconfig config IIO_TRIGGERED_EVENT · line 77

Quoted 7 lines
drivers/iio/Kconfig +4 -0
@@ -71,6 +71,11 @@ 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.

Add some more description here. Not sure the current text will help anyone understand it :)

Quoted 76 lines
drivers/iio/Kconfig +1 -0
+
 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 +54 -0
new file mode 100644
index 000000000000..75a0a66003e1
@@ -0,0 +1,456 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * 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 framework interface is pretty simple:
+ *   - Backends should register themselves with @devm_iio_backend_register()
+ *   - Frontend devices should get backends with @devm_iio_backend_get()
+ *
+ * Also to note that the primary target for this framework are converters like
+ * ADC/DACs so @iio_backend_ops will have some operations typical of converter
+ * devices. On top of that, this is "generic" for all IIO which means any kind
+ * of device can make use of the framework. That said, If the @iio_backend_ops
+ * struct begins to grow out of control, we can always refactor things so that
+ * the industrialio-backend.c is only left with the really generic stuff. Then,
+ * we can build on top of it depending on the needs.
+ *
+ * Copyright (C) 2023 Analog Devices Inc.
+ */
+#define pr_fmt(fmt) "iio-backend: " fmt
+
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/list.h>
+#include <linux/lockdep.h>
+#include <linux/kref.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+
+#include <linux/iio/backend.h>
+
+struct iio_backend {
+	struct list_head entry;
+	const struct iio_backend_ops *ops;
+	struct device *dev;
+	struct module *owner;
+	void *priv;
+	/*
+	 * mutex used to synchronize backend callback access with concurrent
+	 * calls to @iio_backend_unregister. The lock makes sure a device is
+	 * not unregistered while a callback is being run.
+	 */
+	struct mutex lock;
+	struct kref ref;
+};
+

...

Quoted 7 lines
drivers/iio/industrialio-backend.c new file +7 -0
+
+static void iio_backend_release(void *arg)
+{
+	struct iio_backend *back = arg;
+
+	module_put(back->owner);
+	kref_put(&back->ref, iio_backend_free);

drivers/iio/industrialio-backend.c line 266

Quoted 10 lines
drivers/iio/industrialio-backend.c new file +10 -0
+}
+
+static int __devm_iio_backend_get(struct device *dev, struct iio_backend *back)
+{
+	struct device_link *link;
+	int ret;
+
+	kref_get(&back->ref);
+	if (!try_module_get(back->owner)) {
+		pr_err("%s: Cannot get module reference\n", dev_name(dev));

Why do you need the reference? Good to add a comment on that here.

drivers/iio/industrialio-backend.c line 277

Quoted 11 lines
drivers/iio/industrialio-backend.c new file +11 -0
+		return -ENODEV;
+	}
+
+	ret = devm_add_action_or_reset(dev, iio_backend_release, back);
+	if (ret)
+		return ret;
+
+	link = device_link_add(dev, back->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
+	if (!link)
+		pr_warn("%s: Could not link to supplier(%s)\n", dev_name(dev),
+			dev_name(back->dev));

Why is that not an error and we try to carry on?

Quoted 40 lines
drivers/iio/industrialio-backend.c new file +40 -0
+
+	pr_debug("%s: Found backend(%s) device\n", dev_name(dev),
+		 dev_name(back->dev));
+	return 0;
+}
+
+/**
+ * devm_iio_backend_get - Get a backend device
+ * @dev:	Device where to look for the backend.
+ * @name:	Backend name.
+ *
+ * Get's the backend associated with @dev.
+ *
+ * RETURNS:
+ * A backend pointer, negative error pointer otherwise.
+ */
+struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name)
+{
+	struct fwnode_handle *fwnode;
+	struct iio_backend *back;
+	int index = 0, ret;
+
+	if (name) {
+		index = device_property_match_string(dev, "io-backends-names",
+						     name);
+		if (index < 0)
+			return ERR_PTR(index);
+	}
+
+	fwnode = fwnode_find_reference(dev_fwnode(dev), "io-backends", index);
+	if (IS_ERR(fwnode)) {
+		/* not an error if optional */
+		pr_debug("%s: Cannot get Firmware reference\n", dev_name(dev));
+		return ERR_CAST(fwnode);
+	}
+
+	guard(mutex)(&iio_back_lock);
+	list_for_each_entry(back, &iio_back_list, entry) {
+		if (!device_match_fwnode(back->dev, fwnode))
+			continue;

drivers/iio/industrialio-backend.c line 327

Quoted 10 lines
drivers/iio/industrialio-backend.c new file +10 -0
+
+		fwnode_handle_put(fwnode);
+		ret = __devm_iio_backend_get(dev, back);
+		if (ret)
+			return ERR_PTR(ret);
+
+		return back;
+	}
+
+	fwnode_handle_put(fwnode);

FYI. I have a series doing auto cleanup of fwnode_handles in progress. Should get some time over the weekend to snd that out. Aim is to avoid need to dance around manually freeing them (similar to the DT __free(device_node) series I posted the other day).

Quoted 16 lines
drivers/iio/industrialio-backend.c new file +16 -0
+	return ERR_PTR(-EPROBE_DEFER);
+}
+EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get, IIO_BACKEND);
+
+/**
+ * devm_iio_backend_get_optional - Get optional backend device
+ * @dev:	Device where to look for the backend.
+ * @name:	Backend name.
+ *
+ * Same as @devm_iio_backend_get() but return NULL if backend not found.
+ *
+ * RETURNS:
+ * A backend pointer, negative error pointer otherwise or NULL if not found.
+ */
+struct iio_backend *devm_iio_backend_get_optional(struct device *dev,
+						  const char *name)

drivers/iio/industrialio-backend.c line 353

Quoted 10 lines
drivers/iio/industrialio-backend.c new file +10 -0
+{
+	struct iio_backend *back;
+
+	back = devm_iio_backend_get(dev, name);
+	if (IS_ERR(back) && PTR_ERR(back) == -ENOENT)
+		return NULL;
+
+	return back;
+}
+EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get_optional, IIO_BACKEND);

I'm not convinced the optional variant is worth while. Could just choose a particular return value to mean that e.g. ERR_PTR(-ENOENT) and document it for the normal get. Then have special handling in the drivers where you need backwards compatibility with a previous approach.

I'd rather pay the complexity price in a couple of drivers than have to explain backends aren't typically optional for years to come.

drivers/iio/industrialio-backend.c line 360

Quoted 7 lines
drivers/iio/industrialio-backend.c new file +7 -0
+
+/**
+ * devm_iio_backend_get_from_fwnode_lookup
+ * @dev:	Device where to bind the backend lifetime.
+ * @fwnode:	Firmware node of the backend device.
+ *
+ * It directly looks the backend device list for a device matching @fwnode.

I would word this:
Search the backend list for a device matchign &fwnode.

Quoted 2 lines

+ * 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.

You could stick a __ in front of the name to hopefully scare people away :)
+ highlight something odd is going on to reviewers seeing this called in some future driver.
Also I can we might convert other drivers that are doing similar things (dfsdm for example) and maybe this will be useful
so __devm_iio_backend_get_from_fwnode_lookup() and
"preventing breakage of old DT bindings".

Quoted 26 lines

+ *
+ * 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);
#17 lore raw

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

Quoted 55 lines · Jonathan Cameron
Quoted 8 lines · On Wed, 20 Dec 2023 16:34:09 +0100 Nuno Sa <[email protected]> wrote:

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, but seems good to me otherwise.

Jonathan

Quoted 34 lines

---
 MAINTAINERS                        |   8 +
 drivers/iio/Kconfig                |   5 +
 drivers/iio/Makefile               |   1 + drivers/iio/industrialio-backend.c | 456 +++++++++++++++++++++++++++++++++++++ include/linux/iio/backend.h        |  75 ++++++

 5 files changed, 545 insertions(+)
MAINTAINERS +0 -0
index 3029841e92a8..df5f5b988926 100644
@@ -10334,6 +10334,14 @@ L:     [email protected]

 S:     Maintained
 F:     drivers/media/rc/iguanair.c

MAINTAINERS +8 -0
+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 +0 -0
index 52eb46ef84c1..451671112f73 100644
@@ -71,6 +71,11 @@ config IIO_TRIGGERED_EVENT

        help
          Provides helper functions for setting up triggered events.

Quoted 9 lines
Quoted 5 lines


+config IIO_BACKEND
+       tristate
+       help
+         Framework to handle complex IIO aggregate devices.

Add some more description here. Not sure the current text will help anyone understand it :)

Alright...

Quoted 91 lines
Quoted 77 lines

+
 source "drivers/iio/accel/Kconfig"
 source "drivers/iio/adc/Kconfig"
 source "drivers/iio/addac/Kconfig"

drivers/iio/Makefile +0 -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- renamed from drivers/iio/industrialio-backend.c +0 -0

backend.c
new file mode 100644
index 000000000000..75a0a66003e1

drivers/iio/industrialio-backend.c new file +54 -0
@@ -0,0 +1,456 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * 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 framework interface is pretty simple:
+ *   - Backends should register themselves with @devm_iio_backend_register()
+ *   - Frontend devices should get backends with @devm_iio_backend_get()
+ *
+ * Also to note that the primary target for this framework are converters like
+ * ADC/DACs so @iio_backend_ops will have some operations typical of converter
+ * devices. On top of that, this is "generic" for all IIO which means any kind
+ * of device can make use of the framework. That said, If the @iio_backend_ops
+ * struct begins to grow out of control, we can always refactor things so that
+ * the industrialio-backend.c is only left with the really generic stuff. Then,
+ * we can build on top of it depending on the needs.
+ *
+ * Copyright (C) 2023 Analog Devices Inc.
+ */
+#define pr_fmt(fmt) "iio-backend: " fmt
+
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/list.h>
+#include <linux/lockdep.h>
+#include <linux/kref.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+
+#include <linux/iio/backend.h>
+
+struct iio_backend {
+       struct list_head entry;
+       const struct iio_backend_ops *ops;
+       struct device *dev;
+       struct module *owner;
+       void *priv;
+       /*
+        * mutex used to synchronize backend callback access with concurrent
+        * calls to @iio_backend_unregister. The lock makes sure a device is
+        * not unregistered while a callback is being run.
+        */
+       struct mutex lock;
+       struct kref ref;
+};
+

...

Quoted 11 lines
drivers/iio/industrialio-backend.c new file +11 -0
+
+static void iio_backend_release(void *arg)
+{
+       struct iio_backend *back = arg;
+
+       module_put(back->owner);
+       kref_put(&back->ref, iio_backend_free);
+}
+
+static int __devm_iio_backend_get(struct device *dev, struct iio_backend *back)
+{
Quoted 8 lines
Quoted 6 lines

+       struct device_link *link;
+       int ret;
+
+       kref_get(&back->ref);
+       if (!try_module_get(back->owner)) {
+               pr_err("%s: Cannot get module reference\n", dev_name(dev));

Why do you need the reference?  Good to add a comment on that here.

Yeah, typical stuff when it's a module and we don't want for it to go away. Even though we handle that case with pointer stuff. I'll comment it.

Quoted 14 lines

drivers/iio/industrialio-backend.c line 277

Quoted 12 lines

+               return -ENODEV;
+       }

drivers/iio/industrialio-backend.c new file +9 -0
+
+       ret = devm_add_action_or_reset(dev, iio_backend_release, back);
+       if (ret)
+               return ret;
+
+       link = device_link_add(dev, back->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
+       if (!link)
+               pr_warn("%s: Could not link to supplier(%s)\n", dev_name(dev),
+                       dev_name(back->dev));

Why is that not an error and we try to carry on?

I guess having the links are not really mandatory for the whole thing to work (more like a nice to have). That's also how this is handled in another subsystems so I figured it would be fine.

But since you are speaking about this... After you pointing me to Bartosz's talk and sawing it (as stuff like this is mentioned), I started to question this. The goal with the above comment is that if you remove the backend, all the consumers are automatically removed (unbound). Just not sure if that's what we always want (and we are already handling the situation where a backend goes away with -ENODEV) as some frontends could still be useful without the backend (I guess that could be plausible). I think for now we don't really have such usecases so the links can make sense (and we can figure something like optionally creating these links if we ever need too) but having your inputs on this will definitely be valuable.

Quoted 47 lines
Quoted 47 lines
drivers/iio/industrialio-backend.c new file +47 -0
+
+       pr_debug("%s: Found backend(%s) device\n", dev_name(dev),
+                dev_name(back->dev));
+       return 0;
+}
+
+/**
+ * devm_iio_backend_get - Get a backend device
+ * @dev:       Device where to look for the backend.
+ * @name:      Backend name.
+ *
+ * Get's the backend associated with @dev.
+ *
+ * RETURNS:
+ * A backend pointer, negative error pointer otherwise.
+ */
+struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name)
+{
+       struct fwnode_handle *fwnode;
+       struct iio_backend *back;
+       int index = 0, ret;
+
+       if (name) {
+               index = device_property_match_string(dev, "io-backends-names",
+                                                    name);
+               if (index < 0)
+                       return ERR_PTR(index);
+       }
+
+       fwnode = fwnode_find_reference(dev_fwnode(dev), "io-backends", index);
+       if (IS_ERR(fwnode)) {
+               /* not an error if optional */
+               pr_debug("%s: Cannot get Firmware reference\n", dev_name(dev));
+               return ERR_CAST(fwnode);
+       }
+
+       guard(mutex)(&iio_back_lock);
+       list_for_each_entry(back, &iio_back_list, entry) {
+               if (!device_match_fwnode(back->dev, fwnode))
+                       continue;
+
+               fwnode_handle_put(fwnode);
+               ret = __devm_iio_backend_get(dev, back);
+               if (ret)
+                       return ERR_PTR(ret);
+
+               return back;
Quoted 8 lines
Quoted 3 lines

+       }
+
+       fwnode_handle_put(fwnode);

FYI. I have a series doing auto cleanup of fwnode_handles in progress. Should get some time over the weekend to snd that out.  Aim is to avoid need to dance around manually freeing them (similar to the DT __free(device_node) series I posted the other day).

Cool! This will surely be an user of that.

Quoted 33 lines
Quoted 17 lines

+       return ERR_PTR(-EPROBE_DEFER);

drivers/iio/industrialio-backend.c new file +15 -0
+}
+EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get, IIO_BACKEND);
+
+/**
+ * devm_iio_backend_get_optional - Get optional backend device
+ * @dev:       Device where to look for the backend.
+ * @name:      Backend name.
+ *
+ * Same as @devm_iio_backend_get() but return NULL if backend not found.
+ *
+ * RETURNS:
+ * A backend pointer, negative error pointer otherwise or NULL if not found.
+ */
+struct iio_backend *devm_iio_backend_get_optional(struct device *dev,
+                                                 const char *name)

drivers/iio/industrialio-backend.c line 353

Quoted 10 lines
drivers/iio/industrialio-backend.c new file +10 -0
+{
+       struct iio_backend *back;
+
+       back = devm_iio_backend_get(dev, name);
+       if (IS_ERR(back) && PTR_ERR(back) == -ENOENT)
+               return NULL;
+
+       return back;
+}
+EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get_optional, IIO_BACKEND);

I'm not convinced the optional variant is worth while.  Could just choose a particular return value to mean that e.g. ERR_PTR(-ENOENT) and document it for the normal get.  Then have special handling in the drivers where you need backwards compatibility with a previous approach.

Quoted 2 lines

I'd rather pay the complexity price in a couple of drivers than have to explain backends aren't typically optional for years to come.

Alright. For now I'm not seeing any usecase where backends are optional. For the case we need the backward compatibility I'll do as you suggest. If we ever have a real 'optional' usecase we can easily add this one again.

Quoted 12 lines

drivers/iio/industrialio-backend.c line 360

Quoted 9 lines
drivers/iio/industrialio-backend.c new file +7 -0
+
+/**
+ * devm_iio_backend_get_from_fwnode_lookup
+ * @dev:       Device where to bind the backend lifetime.
+ * @fwnode:    Firmware node of the backend device.
+ *
+ * It directly looks the backend device list for a device matching @fwnode.

I would word this:
Search the backend list for a device matchign &fwnode.

ack

Quoted 11 lines
Quoted 3 lines

+ * 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.

You could stick a __ in front of the name to hopefully scare people away :)
+ highlight something odd is going on to reviewers seeing this called in some future driver.
Also I can we might convert other drivers that are doing similar things (dfsdm for example) and maybe this will be useful
so __devm_iio_backend_get_from_fwnode_lookup() and
"preventing breakage of old DT bindings".

Will do!

Thanks for the inputs!
- Nuno Sá

maintainer
#18 lore raw

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

Quoted 17 lines
Quoted 12 lines

drivers/iio/industrialio-backend.c line 277

Quoted 10 lines
drivers/iio/industrialio-backend.c new file +9 -0
+
+       ret = devm_add_action_or_reset(dev, iio_backend_release, back);
+       if (ret)
+               return ret;
+
+       link = device_link_add(dev, back->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
+       if (!link)
+               pr_warn("%s: Could not link to supplier(%s)\n", dev_name(dev),
+                       dev_name(back->dev));  

Why is that not an error and we try to carry on?

I guess having the links are not really mandatory for the whole thing to work (more like a nice to have). That's also how this is handled in another subsystems so I figured it would be fine.

Quoted 9 lines

But since you are speaking about this... After you pointing me to Bartosz's talk and sawing it (as stuff like this is mentioned), I started to question this. The goal with the above comment is that if you remove the backend, all the consumers are automatically removed (unbound). Just not sure if that's what we always want (and we are already handling the situation where a backend goes away with -ENODEV) as some frontends could still be useful without the backend (I guess that could be plausible). I think for now we don't really have such usecases so the links can make sense (and we can figure something like optionally creating these links if we ever need too) but having your inputs on this will definitely be valuable.

I'm not keen on both trying to make everything tear down cleanly AND making sure it all works even if we don't. That just adds two code paths to test when either should be sufficient on its own. I don't really mind which. Bartosz's stuff is nice, but it may not be the right solution here.

Quoted 7 lines
Quoted 7 lines

drivers/iio/industrialio-backend.c line 283

Quoted 7 lines
drivers/iio/industrialio-backend.c new file +6 -0
+
+       pr_debug("%s: Found backend(%s) device\n", dev_name(dev),
+                dev_name(back->dev));
+       return 0;
+}
+

Jonathan

#19 lore raw

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

Quoted 38 lines · Jonathan Cameron
Quoted 21 lines
Quoted 14 lines
Quoted 7 lines
drivers/iio/industrialio-backend.c new file +6 -0
+
+       ret = devm_add_action_or_reset(dev, iio_backend_release, back);
+       if (ret)
+               return ret;
+
+       link = device_link_add(dev, back->dev,
Quoted 5 lines

DL_FLAG_AUTOREMOVE_CONSUMER);
+       if (!link)
+               pr_warn("%s: Could not link to supplier(%s)\n", dev_name(dev),
+                       dev_name(back->dev));

Why is that not an error and we try to carry on?

I guess having the links are not really mandatory for the whole thing to work (more
like a nice to have). That's also how this is handled in another subsystems so I
figured it would be fine.

Quoted 16 lines

But since you are speaking about this... After you pointing me to Bartosz's talk and
sawing it (as stuff like this is mentioned), I started to question this. The goal
with the above comment is that if you remove the backend, all the consumers are
automatically removed (unbound). Just not sure if that's what we always want (and we
are already handling the situation where a backend goes away with -ENODEV) as some
frontends could still be useful without the backend (I guess that could be plausible). I think for now we don't really have such usecases so the links can make
sense (and we can figure something like optionally creating these links if we ever
need too) but having your inputs on this will definitely be valuable.

Quoted 5 lines

I'm not keen on both trying to make everything tear down cleanly AND making sure
it all works even if we don't. That just adds two code paths to test when either
should be sufficient on its own.  I don't really mind which.  Bartosz's stuff

Agreed...

Quoted 1 line

is nice, but it may not be the right solution here.

There's pros and cons on both options...

For the device links the cons I see is that it depends on patch 3 for it to work (or some other approach if the one in that patch is not good) - not really a real con though :). The biggest concern is (possible) future uses where we end up with cases where removing a backend is not really a "deal breaker". I could think of frontends that have multiple backends (one per data path) and removing one backend would not tear the whole thing down (we would just have one non functional data paths/port where the others are still ok).

Olivier, for STM usecases, do we always need the backend? I mean, does it make sense to always remove/unbind the frontend in case the backend is unbound?

Maybe some of your usecases already "forces" us with a decision.

The biggest pro I see is code simplicity. If we can assume the frontend can never exist in case one of the backends is gone, we can:

* get rid of the sync mutex;
* get rid of the kref and bind the backend object lifetime to the backend
device (using devm_kzalloc() instead of kzalloc + refcount.

Basically, we would not need to care about syncing the backend existence with accessing it...
To sum up, the device_links approach tends to be similar (not identical) to the previous approach using the component API.

The biggest pro I see in Bartosz's stuff is flexibility. So it should just work in whatever future usecases we might have. I fear that going the device_links road we might end up needing this stuff anyways.

Obviously, the biggest con is code complexity (not that bad though) as we always need to properly sync any backend callback.

- Nuno Sá

Quoted 1 line
#20 lore raw

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

Quoted 51 lines · On Tue, 09 Jan 2024 13:15:54 +0100 Nuno Sá <[email protected]> wrote:
Quoted 38 lines · Jonathan Cameron
Quoted 21 lines
Quoted 14 lines
Quoted 7 lines
drivers/iio/industrialio-backend.c new file +6 -0
+
+       ret = devm_add_action_or_reset(dev, iio_backend_release, back);
+       if (ret)
+               return ret;
+
+       link = device_link_add(dev, back->dev,
Quoted 5 lines

DL_FLAG_AUTOREMOVE_CONSUMER);
+       if (!link)
+               pr_warn("%s: Could not link to supplier(%s)\n", dev_name(dev),
+                       dev_name(back->dev));

Why is that not an error and we try to carry on?

I guess having the links are not really mandatory for the whole thing to work (more
like a nice to have). That's also how this is handled in another subsystems so I
figured it would be fine.

Quoted 16 lines

But since you are speaking about this... After you pointing me to Bartosz's talk and
sawing it (as stuff like this is mentioned), I started to question this. The goal
with the above comment is that if you remove the backend, all the consumers are
automatically removed (unbound). Just not sure if that's what we always want (and we
are already handling the situation where a backend goes away with -ENODEV) as some
frontends could still be useful without the backend (I guess that could be plausible). I think for now we don't really have such usecases so the links can make
sense (and we can figure something like optionally creating these links if we ever
need too) but having your inputs on this will definitely be valuable.

Quoted 5 lines

I'm not keen on both trying to make everything tear down cleanly AND making sure
it all works even if we don't. That just adds two code paths to test when either
should be sufficient on its own.  I don't really mind which.  Bartosz's stuff

Agreed...

Quoted 1 line

is nice, but it may not be the right solution here.

There's pros and cons on both options...

Quoted 7 lines

For the device links the cons I see is that it depends on patch 3 for it to work (or some other approach if the one in that patch is not good) - not really a real con though :). The biggest concern is (possible) future uses where we end up with cases where removing a backend is not really a "deal breaker". I could think of frontends that have multiple backends (one per data path) and removing one backend would not tear the whole thing down (we would just have one non functional data paths/port where the others are still ok).

I wouldn't waste time catering to such set ups. If the whole thing gets torn down because one element went away that should be fine. To do anything else I'd want to see a real world use case.

Quoted 13 lines

Olivier, for STM usecases, do we always need the backend? I mean, does it make sense to always remove/unbind the frontend in case the backend is unbound?

Maybe some of your usecases already "forces" us with a decision.

The biggest pro I see is code simplicity. If we can assume the frontend can never exist in case one of the backends is gone, we can:

* get rid of the sync mutex;
* get rid of the kref and bind the backend object lifetime to the backend
device (using devm_kzalloc() instead of kzalloc + refcount.

Quoted 8 lines

Basically, we would not need to care about syncing the backend existence with accessing it...
To sum up, the device_links approach tends to be similar (not identical) to the previous approach using the component API.

The biggest pro I see in Bartosz's stuff is flexibility. So it should just work in whatever future usecases we might have. I fear that going the device_links road we might end up needing this stuff anyways.

I'm keen on it if it simplifies code or becomes the dominant paradigm for such things in the kernel (so becomes what people expect). That isn't true yet and I doubt it will be particularly soon. If things head that way we can revisit as it would enable things that currently we don't support - nothing should break.

Jonathan

Quoted 7 lines

Obviously, the biggest con is code complexity (not that bad though) as we always need to properly sync any backend callback.

- Nuno Sá

Quoted 1 line
#21 lore raw

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

Quoted 78 lines · Jonathan Cameron
Quoted 63 lines · On Tue, 09 Jan 2024 13:15:54 +0100 Nuno Sá <[email protected]> wrote:
Quoted 48 lines · Jonathan Cameron

Quoted 38 lines
Quoted 6 lines
Quoted 6 lines

+
+       ret = devm_add_action_or_reset(dev, iio_backend_release, back);
+       if (ret)
+               return ret;
+

Quoted 8 lines
Quoted 6 lines

+       link = device_link_add(dev, back->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
+       if (!link)
+               pr_warn("%s: Could not link to supplier(%s)\n", dev_name(dev),
+                       dev_name(back->dev));

Why is that not an error and we try to carry on?

I guess having the links are not really mandatory for the whole thing to work (more
like a nice to have). That's also how this is handled in another subsystems
so I
figured it would be fine.

But since you are speaking about this... After you pointing me to Bartosz's
talk and
sawing it (as stuff like this is mentioned), I started to question this. The
goal
with the above comment is that if you remove the backend, all the consumers
are
automatically removed (unbound). Just not sure if that's what we always want
(and we
are already handling the situation where a backend goes away with - ENODEV)
as some
frontends could still be useful without the backend (I guess that could

Quoted 8 lines

be
plausible). I think for now we don't really have such usecases so the links
can make
sense (and we can figure something like optionally creating these links if
we ever
need too) but having your inputs on this will definitely be valuable.

Quoted 7 lines

I'm not keen on both trying to make everything tear down cleanly AND making
sure
it all works even if we don't. That just adds two code paths to test when either
should be sufficient on its own.  I don't really mind which.  Bartosz's stuff

Agreed...

Quoted 1 line

is nice, but it may not be the right solution here.

There's pros and cons on both options...

Quoted 11 lines

For the device links the cons I see is that it depends on patch 3 for it to work
(or some other approach if the one in that patch is not good) - not really a real con though :). The biggest concern is (possible) future uses where we end
up with cases where removing a backend is not really a "deal breaker". I could
think of frontends that have multiple backends (one per data path) and removing
one backend would not tear the whole thing down (we would just have one non functional data paths/port where the others are still ok).

Quoted 3 lines

I wouldn't waste time catering to such set ups.  If the whole thing gets torn down because one element went away that should be fine. To do anything else I'd want to see a real world use case.

Fair enough...

Quoted 28 lines
Quoted 22 lines

Olivier, for STM usecases, do we always need the backend? I mean, does it make
sense to always remove/unbind the frontend in case the backend is unbound?

Maybe some of your usecases already "forces" us with a decision.

The biggest pro I see is code simplicity. If we can assume the frontend can never exist in case one of the backends is gone, we can:

 * get rid of the sync mutex;
 * get rid of the kref and bind the backend object lifetime to the backend device (using devm_kzalloc() instead of kzalloc + refcount.

Basically, we would not need to care about syncing the backend existence with
accessing it...
To sum up, the device_links approach tends to be similar (not identical) to the
previous approach using the component API.

Quoted 5 lines

The biggest pro I see in Bartosz's stuff is flexibility. So it should just work
in whatever future usecases we might have. I fear that going the device_links
road we might end up needing this stuff anyways.

Quoted 6 lines

I'm keen on it if it simplifies code or becomes the dominant paradigm for such things in the kernel (so becomes what people expect).  That isn't true yet and I doubt it will be particularly soon.  If things head that way we can revisit as it would enable things that currently we don't support - nothing should break.

Well, If I'm not missing anything, simpler code would be with device_links so I guess that's your preferred approach for now :). Also fine by me as this is an in kernel interface so we easily revisit it.

I'll just wait a bit more (before sending v5) to see if Olivier has any foreseeable usecase where device_links would be an issue.

- Nuno Sá

#22 lore raw

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

In reply to Nuno Sá

Hi Nuno,

Quoted 59 lines · Nuno Sá
Quoted 38 lines · Jonathan Cameron
Quoted 21 lines
Quoted 14 lines
Quoted 7 lines
drivers/iio/industrialio-backend.c new file +6 -0
+
+       ret = devm_add_action_or_reset(dev, iio_backend_release, back);
+       if (ret)
+               return ret;
+
+       link = device_link_add(dev, back->dev,
Quoted 5 lines

DL_FLAG_AUTOREMOVE_CONSUMER);
+       if (!link)
+               pr_warn("%s: Could not link to supplier(%s)\n", dev_name(dev),
+                       dev_name(back->dev));

Why is that not an error and we try to carry on?

I guess having the links are not really mandatory for the whole thing to work (more
like a nice to have). That's also how this is handled in another subsystems so I
figured it would be fine.

Quoted 16 lines

But since you are speaking about this... After you pointing me to Bartosz's talk and
sawing it (as stuff like this is mentioned), I started to question this. The goal
with the above comment is that if you remove the backend, all the consumers are
automatically removed (unbound). Just not sure if that's what we always want (and we
are already handling the situation where a backend goes away with -ENODEV) as some
frontends could still be useful without the backend (I guess that could be plausible). I think for now we don't really have such usecases so the links can make
sense (and we can figure something like optionally creating these links if we ever
need too) but having your inputs on this will definitely be valuable.

Quoted 5 lines

I'm not keen on both trying to make everything tear down cleanly AND making sure
it all works even if we don't. That just adds two code paths to test when either
should be sufficient on its own.  I don't really mind which.  Bartosz's stuff

Agreed...

Quoted 1 line

is nice, but it may not be the right solution here.

There's pros and cons on both options...

For the device links the cons I see is that it depends on patch 3 for it to work (or some other approach if the one in that patch is not good) - not really a real con though :). The biggest concern is (possible) future uses where we end up with cases where removing a backend is not really a "deal breaker". I could think of frontends that have multiple backends (one per data path) and removing one backend would not tear the whole thing down (we would just have one non functional data paths/port where the others are still ok).

Quoted 3 lines

Olivier, for STM usecases, do we always need the backend? I mean, does it make sense to always remove/unbind the frontend in case the backend is unbound?

In STM usecases we may have severals backends linked to a frontend. But I cannot find a usecase where it may be necessary to remove a backend while keeping the others alive. So from my side it is acceptable to tear everythings down if a backend if removed.

Olivier

Quoted 23 lines

Maybe some of your usecases already "forces" us with a decision.

The biggest pro I see is code simplicity. If we can assume the frontend can never exist in case one of the backends is gone, we can:

* get rid of the sync mutex;
* get rid of the kref and bind the backend object lifetime to the backend
device (using devm_kzalloc() instead of kzalloc + refcount.

Basically, we would not need to care about syncing the backend existence with accessing it...
To sum up, the device_links approach tends to be similar (not identical) to the previous approach using the component API.

The biggest pro I see in Bartosz's stuff is flexibility. So it should just work in whatever future usecases we might have. I fear that going the device_links road we might end up needing this stuff anyways.

Obviously, the biggest con is code complexity (not that bad though) as we always need to properly sync any backend callback.

- Nuno Sá

Quoted 1 line
author maintainer
#23 lore raw

PATCH v4 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 | 282 ++++++++++++++++++++++++++++++-----------------
 2 files changed, 182 insertions(+), 102 deletions(-)
drivers/iio/adc/Kconfig +1 -1
index 4eebd5161419..10e0e340cdae 100644
@@ -275,7 +275,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 +181 -101

473 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#24 lore raw

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

Quoted 9 lines · On Wed, 20 Dec 2023 16:34:10 +0100 Nuno Sa <[email protected]> wrote:

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.

A few minor comments. Looks good to me.

Jonathan

Quoted 23 lines
drivers/iio/adc/ad9467.c +19 -0
+static int ad9467_update_scan_mode(struct iio_dev *indio_dev,
+				   const unsigned long *scan_mask)
+{
+	struct ad9467_state *st = iio_priv(indio_dev);
+	unsigned int c;
+	int ret;
+
+	for (c = 0; c < st->info->num_channels; c++) {
+		if (test_bit(c, scan_mask))
+			ret = iio_backend_chan_enable(st->back, c);
+		else
+			ret = iio_backend_chan_disable(st->back, c);
+
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
 
 static int ad9467_reset(struct device *dev)
@@ -443,25 +475,63 @@ static int ad9467_reset(struct device *dev)
 	return 0;

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

Quoted 10 lines
drivers/iio/adc/ad9467.c +8 -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_optional(&st->spi->dev, NULL);
+	if (IS_ERR(st->back))
+		return PTR_ERR(st->back);

As per the comment on previous patch I'd just get it using the normal function and handle PTR_ERR(-ENOENT) here as meaning we need to try the old way.

Quoted 15 lines
drivers/iio/adc/ad9467.c +15 -0
+	if (st->back)
+		return 0;
+	/*
+	 * 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;

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

Quoted 10 lines
drivers/iio/adc/ad9467.c +10 -0
+
+		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);

If it lands first the patch
RFC PATCH 1/4] of: Add cleanup.h based autorelease via __free(device_node) markings. will get rid of this manual handling for you for both the continue and return. This will make a very nice example for that :)

Quoted 5 lines
drivers/iio/adc/ad9467.c +5 -0
+		return PTR_ERR_OR_ZERO(st->back);
+	}
+
+	return -ENODEV;
+}
#25 lore raw

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

Quoted 46 lines · Jonathan Cameron
Quoted 9 lines · On Wed, 20 Dec 2023 16:34:10 +0100 Nuno Sa <[email protected]> wrote:

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.

A few minor comments. Looks good to me.

Jonathan

Quoted 29 lines
drivers/iio/adc/ad9467.c +19 -0
+static int ad9467_update_scan_mode(struct iio_dev *indio_dev,
+                                  const unsigned long *scan_mask)
+{
+       struct ad9467_state *st = iio_priv(indio_dev);
+       unsigned int c;
+       int ret;
+
+       for (c = 0; c < st->info->num_channels; c++) {
+               if (test_bit(c, scan_mask))
+                       ret = iio_backend_chan_enable(st->back, c);
+               else
+                       ret = iio_backend_chan_disable(st->back, c);
+
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}

 static int ad9467_reset(struct device *dev)

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

drivers/iio/adc/ad9467.c +4 -0
+static int ad9467_iio_backend_get(struct ad9467_state *st)
+{
+       struct device *dev = &st->spi->dev;
+       struct device_node *__back;
Quoted 8 lines
Quoted 4 lines

+
+       st->back = devm_iio_backend_get_optional(&st->spi->dev, NULL); +       if (IS_ERR(st->back))
+               return PTR_ERR(st->back);

As per the comment on previous patch I'd just get it using the normal function and handle PTR_ERR(-ENOENT) here as meaning we need to try the old way.

That makes sense. So we can add a devm_iio_backend_get_optional() API when/if we really need one and not just for handling legacy code.

Quoted 25 lines
Quoted 25 lines

+       if (st->back)
+               return 0;
+       /*
+        * 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;

drivers/iio/adc/ad9467.c +13 -0
+
+               __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,
+                                                                 
Quoted 8 lines
Quoted 2 lines

of_fwnode_handle(__back));
+               of_node_put(__back);

If it lands first the patch
RFC PATCH 1/4] of: Add cleanup.h based autorelease via __free(device_node) markings.
will get rid of this manual handling for you for both the continue and return. This will make a very nice example for that :)

Yeah, and I'll happily rebase on that...

- Nuno Sá

Quoted 1 line
author maintainer
#26 lore raw

PATCH v4 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 10e0e340cdae..b64e0d442a19 100644
@@ -293,7 +293,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