Skip to contentSkip to conversation

iio: imu: Add driver and dt-bindings for BMI323

RFC Superseded Jagath Jog J 8 participants 36 messages Sep 18, 2023 – Oct 13, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jagath Jog J

author maintainer
#0 lore raw

cover iio: imu: Add driver and dt-bindings for BMI323

Add dt-bindings and IIO driver for Bosch BMI323 a 6 axis IMU.

Jagath Jog J (2):
dt-bindings: iio: imu: Add DT binding doc for BMI323
iio: imu: Add driver for BMI323 IMU

 .../bindings/iio/imu/bosch,bmi323.yaml        |   81 +
 MAINTAINERS                                   |    7 +
 drivers/iio/imu/Kconfig                       |    1 +
 drivers/iio/imu/Makefile                      |    1 +
 drivers/iio/imu/bmi323/Kconfig                |   33 +
 drivers/iio/imu/bmi323/Makefile               |    7 +
 drivers/iio/imu/bmi323/bmi323.h               |  198 ++
 drivers/iio/imu/bmi323/bmi323_core.c          | 2260 +++++++++++++++++
 drivers/iio/imu/bmi323/bmi323_i2c.c           |  115 +
 drivers/iio/imu/bmi323/bmi323_spi.c           |  106 +
 10 files changed, 2809 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml
 create mode 100644 drivers/iio/imu/bmi323/Kconfig
 create mode 100644 drivers/iio/imu/bmi323/Makefile
 create mode 100644 drivers/iio/imu/bmi323/bmi323.h
 create mode 100644 drivers/iio/imu/bmi323/bmi323_core.c
 create mode 100644 drivers/iio/imu/bmi323/bmi323_i2c.c
 create mode 100644 drivers/iio/imu/bmi323/bmi323_spi.c
Signature · 1 line
2.20.1
author maintainer
#1 lore raw

RFC 1/2 dt-bindings: iio: imu: Add DT binding doc for BMI323

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

---
 .../bindings/iio/imu/bosch,bmi323.yaml        | 81 +++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +81 -0
new file mode 100644
index 000000000000..9c08988103c5
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/bosch,bmi323.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMI323 6-Axis IMU
+
+maintainers:
+  - Jagath Jog J <[email protected]>
+
+description:
+  BMI323 is a 6-axis inertial measurement unit that supports acceleration and
+  gyroscopic measurements with hardware fifo buffering. Sensor also provides
+  events information such as motion, steps, orientation, single and double
+  tap detection.
+
+properties:
+  compatible:
+    const: bosch,bmi323
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    enum:
+      - INT1
+      - INT2
+    description: |
+      set to "INT1" if INT1 pin should be used as interrupt input, set
+      to "INT2" if INT2 pin should be used instead
+
+  drive-open-drain:
+    description: |
+      set if the specified interrupt pin should be configured as
+      open drain. If not set, defaults to push-pull.
+
+required:
+  - compatible
+  - reg
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    // Example for I2C
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        bmi323@68 {
+            compatible = "bosch,bmi323";
+            reg = <0x68>;
+            interrupt-parent = <&gpio1>;
+            interrupts = <29 IRQ_TYPE_EDGE_RISING>;
+            interrupt-names = "INT1";
+        };
+    };
+  - |
+    // Example for SPI
+    #include <dt-bindings/interrupt-controller/irq.h>
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        bmi323@0 {
+            compatible = "bosch,bmi323";
+            reg = <0>;
+            spi-max-frequency = <10000000>;
+            interrupt-parent = <&gpio2>;
+            interrupts = <12 IRQ_TYPE_EDGE_FALLING>;
+            interrupt-names = "INT2";
+        };
+    };
Signature · 1 line
2.20.1
#2 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Quoted 1 line · Jagath Jog J

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

I don't know why this is RFC and cover letter does not explain it. Shall I just ignore it? Patch is no ready? Recently at least two times someone was disappointed that his code marked as RFC received my review.

A nit, subject: drop second/last, redundant "DT binding doc for". The "dt-bindings" prefix is already stating that these are bindings. Four words entirely redundant and duplicating what prefix is saying...

Quoted 35 lines
---
 .../bindings/iio/imu/bosch,bmi323.yaml        | 81 +++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +22 -0
new file mode 100644
index 000000000000..9c08988103c5
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/bosch,bmi323.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMI323 6-Axis IMU
+
+maintainers:
+  - Jagath Jog J <[email protected]>
+
+description:
+  BMI323 is a 6-axis inertial measurement unit that supports acceleration and
+  gyroscopic measurements with hardware fifo buffering. Sensor also provides
+  events information such as motion, steps, orientation, single and double
+  tap detection.
+
+properties:
+  compatible:
+    const: bosch,bmi323
+
+  reg:

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 32

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +10 -0
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    enum:
+      - INT1
+      - INT2
+    description: |

Do not need '|' unless you need to preserve formatting.

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 34

Quoted 2 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +2 -0
+      set to "INT1" if INT1 pin should be used as interrupt input, set
+      to "INT2" if INT2 pin should be used instead

And what happens with other INT pin? Remains floating?

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 37

Quoted 3 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +3 -0
+
+  drive-open-drain:
+    description: |

Do not need '|' unless you need to preserve formatting.

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 39

Quoted 2 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +2 -0
+      set if the specified interrupt pin should be configured as
+      open drain. If not set, defaults to push-pull.

Missing supplies. Are you sure device does not use any electric energy?

Quoted 8 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +8 -0
+
+required:
+  - compatible
+  - reg
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +10 -0
+unevaluatedProperties: false
+
+examples:
+  - |
+    // Example for I2C
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+> +        bmi323@68 {

Node names should be generic. See also an explanation and list of examples (not exhaustive) in DT specification:
https://devicetree-specification.readthedocs.io/en/latest/chapter2-devicetree-basics.html#generic-names-recommendation

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 69

Quoted 11 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +11 -0
+            compatible = "bosch,bmi323";
+            reg = <0x68>;
+            interrupt-parent = <&gpio1>;
+            interrupts = <29 IRQ_TYPE_EDGE_RISING>;
+            interrupt-names = "INT1";
+        };
+    };
+  - |
+    // Example for SPI
+    #include <dt-bindings/interrupt-controller/irq.h>
+    spi {

It's the same as other example. No difference. Drop.

Best regards,
Krzysztof

author maintainer
#3 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Hi Krzysztof,

Quoted 7 lines · Krzysztof Kozlowski
Quoted 1 line · Jagath Jog J

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

I don't know why this is RFC and cover letter does not explain it. Shall I just ignore it? Patch is no ready? Recently at least two times someone was disappointed that his code marked as RFC received my review.

Thank you for reviewing. This was the sensor's first patch series, so I initially submitted it as an RFC. I will mark it as "Patch" in the next series.

Quoted 4 lines

A nit, subject: drop second/last, redundant "DT binding doc for". The "dt-bindings" prefix is already stating that these are bindings. Four words entirely redundant and duplicating what prefix is saying...

Sure I will remove redundant words from the subject line.

Quoted 50 lines
Quoted 37 lines
---
 .../bindings/iio/imu/bosch,bmi323.yaml        | 81 +++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +22 -0
new file mode 100644
index 000000000000..9c08988103c5
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/bosch,bmi323.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMI323 6-Axis IMU
+
+maintainers:
+  - Jagath Jog J <[email protected]>
+
+description:
+  BMI323 is a 6-axis inertial measurement unit that supports acceleration and
+  gyroscopic measurements with hardware fifo buffering. Sensor also provides
+  events information such as motion, steps, orientation, single and double
+  tap detection.
+
+properties:
+  compatible:
+    const: bosch,bmi323
+
+  reg:

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 32

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +10 -0
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    enum:
+      - INT1
+      - INT2
+    description: |

Do not need '|' unless you need to preserve formatting.

Quoted 4 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 34

Quoted 2 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +2 -0
+      set to "INT1" if INT1 pin should be used as interrupt input, set
+      to "INT2" if INT2 pin should be used instead

And what happens with other INT pin? Remains floating?`

Yes, the other pin is unconnected. The driver provides support for either INT1, INT2, or no interrupt configuration. I should have added minItems, maxItems, and const, I will add these in the next series.

Quoted 6 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 37

Quoted 4 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +3 -0
+
+  drive-open-drain:
+    description: |

Do not need '|' unless you need to preserve formatting.

Sure I will remove this.

Quoted 5 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 39

Quoted 3 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +2 -0
+      set if the specified interrupt pin should be configured as
+      open drain. If not set, defaults to push-pull.

Missing supplies. Are you sure device does not use any electric energy?

Sorry, I missed adding supply, I will add it in the next series.

Quoted 15 lines
Quoted 15 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +14 -0
+
+required:
+  - compatible
+  - reg
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    // Example for I2C
+    #include <dt-bindings/interrupt-controller/irq.h>
Quoted 8 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml

Quoted 4 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +4 -0
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+> +        bmi323@68 {

Node names should be generic. See also an explanation and list of examples (not exhaustive) in DT specification:
https://devicetree-specification.readthedocs.io/en/latest/chapter2-devicetree-basics.html#generic-names-recommendation

I intend to utilize the generic term 'imu' (representing the accelerometer and gyrometer)
even though it's not listed in the generic-names recommendation. Would this be acceptable?

Quoted 14 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 69

Quoted 12 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +11 -0
+            compatible = "bosch,bmi323";
+            reg = <0x68>;
+            interrupt-parent = <&gpio1>;
+            interrupts = <29 IRQ_TYPE_EDGE_RISING>;
+            interrupt-names = "INT1";
+        };
+    };
+  - |
+    // Example for SPI
+    #include <dt-bindings/interrupt-controller/irq.h>
+    spi {
Quoted 1 line

It's the same as other example. No difference. Drop.

Sure I will keep only one example.

Quoted 4 lines

Best regards,
Krzysztof

Regards,
Jagath

maintainer
#4 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Quoted 8 lines · On Tue, 19 Sep 2023 22:14:40 +0530 Jagath Jog J <[email protected]> wrote:

Hi Krzysztof,

Quoted 4 lines · Krzysztof Kozlowski
Quoted 1 line · Jagath Jog J

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

Quoted 7 lines
Quoted 3 lines

I don't know why this is RFC and cover letter does not explain it. Shall I just ignore it? Patch is no ready? Recently at least two times someone was disappointed that his code marked as RFC received my review.

Thank you for reviewing. This was the sensor's first patch series, so I initially submitted it as an RFC. I will mark it as "Patch" in the next series.

Have more confidence! RFCs need to have clearly stated questions. If you don't have any then you are putting forwards driver for review in ordering to get it merged upstream - so PATCH is appropriate. As you'll see many IIO drivers go through a 'few' revisions once they are posted (hopefully not too many!)

Krzysztof is great at reviewing whatever shows up, but in many cases reviewers won't look at an RFC (unless a big discussion starts) because they aren't interested in open questions, just code that the author considers ready.

Jonathan

author maintainer
#5 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Hi Jonathan

Quoted 26 lines · Jonathan Cameron
Quoted 8 lines · On Tue, 19 Sep 2023 22:14:40 +0530 Jagath Jog J <[email protected]> wrote:

Hi Krzysztof,

Quoted 4 lines · Krzysztof Kozlowski
Quoted 1 line · Jagath Jog J

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

Quoted 7 lines
Quoted 3 lines

I don't know why this is RFC and cover letter does not explain it. Shall I just ignore it? Patch is no ready? Recently at least two times someone was disappointed that his code marked as RFC received my review.

Thank you for reviewing. This was the sensor's first patch series, so I initially submitted it as an RFC. I will mark it as "Patch" in the next series.

Have more confidence! RFCs need to have clearly stated questions. If you don't have any then you are putting forwards driver for review in ordering to get it merged upstream - so PATCH is appropriate. As you'll see many IIO drivers go through a 'few' revisions once they are posted (hopefully not too many!)

Quoted 4 lines

Krzysztof is great at reviewing whatever shows up, but in many cases reviewers won't look at an RFC (unless a big discussion starts) because they aren't interested in open questions, just code that the author considers ready.

Thank you for your feedback and guidance. I appreciate the insight into the RFC process and the importance of clearly stated questions. I'll keep this in mind when preparing future submissions.

Regards
Jagath

Quoted 2 lines

Jonathan

maintainer
#6 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

In reply to Jagath Jog J
Quoted 38 lines · On Mon, 18 Sep 2023 13:33:13 +0530 Jagath Jog J <[email protected]> wrote:

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

---
 .../bindings/iio/imu/bosch,bmi323.yaml        | 81 +++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +24 -0
new file mode 100644
index 000000000000..9c08988103c5
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/bosch,bmi323.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMI323 6-Axis IMU
+
+maintainers:
+  - Jagath Jog J <[email protected]>
+
+description:
+  BMI323 is a 6-axis inertial measurement unit that supports acceleration and
+  gyroscopic measurements with hardware fifo buffering. Sensor also provides
+  events information such as motion, steps, orientation, single and double
+  tap detection.
+
+properties:
+  compatible:
+    const: bosch,bmi323
+
+  reg:
+    maxItems: 1
+

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 34

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +10 -0
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    enum:
+      - INT1
+      - INT2
+    description: |
+      set to "INT1" if INT1 pin should be used as interrupt input, set
+      to "INT2" if INT2 pin should be used instead

Why not both? Sure driver might elect to use only one, but the binding describes the hardware not the driver and both might be wired.

Lots of different sources of interrupts so might be advantageous
to split them up across two wires. A simple case being to route
errors to one and everything 'good' to the other. No obligation to support that in the Linux driver though if you don't need to.

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 39

Quoted 5 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +5 -0
+
+  drive-open-drain:
+    description: |
+      set if the specified interrupt pin should be configured as
+      open drain. If not set, defaults to push-pull.

Two pins. Might be different so you need two controls.

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 43

Quoted 4 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +4 -0
+
+required:
+  - compatible
+  - reg

As mentioned, need power supplies specified and marked as required (though they may be provided via always on regulators and rely on stubs being created by the regulator subsystem on a given board). Looks like there are at least 2 supplies.

Quoted 38 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +38 -0
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    // Example for I2C
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        bmi323@68 {
+            compatible = "bosch,bmi323";
+            reg = <0x68>;
+            interrupt-parent = <&gpio1>;
+            interrupts = <29 IRQ_TYPE_EDGE_RISING>;
+            interrupt-names = "INT1";
+        };
+    };
+  - |
+    // Example for SPI
+    #include <dt-bindings/interrupt-controller/irq.h>
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        bmi323@0 {
+            compatible = "bosch,bmi323";
+            reg = <0>;
+            spi-max-frequency = <10000000>;
+            interrupt-parent = <&gpio2>;
+            interrupts = <12 IRQ_TYPE_EDGE_FALLING>;
+            interrupt-names = "INT2";
+        };
+    };
author maintainer
#7 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

HI Jonathan,

Quoted 47 lines · Jonathan Cameron
Quoted 43 lines · On Mon, 18 Sep 2023 13:33:13 +0530 Jagath Jog J <[email protected]> wrote:

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

---
 .../bindings/iio/imu/bosch,bmi323.yaml        | 81 +++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +29 -0
new file mode 100644
index 000000000000..9c08988103c5
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/bosch,bmi323.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMI323 6-Axis IMU
+
+maintainers:
+  - Jagath Jog J <[email protected]>
+
+description:
+  BMI323 is a 6-axis inertial measurement unit that supports acceleration and
+  gyroscopic measurements with hardware fifo buffering. Sensor also provides
+  events information such as motion, steps, orientation, single and double
+  tap detection.
+
+properties:
+  compatible:
+    const: bosch,bmi323
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    enum:
Quoted 8 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 34

Quoted 5 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +5 -0
+      - INT1
+      - INT2
+    description: |
+      set to "INT1" if INT1 pin should be used as interrupt input, set
+      to "INT2" if INT2 pin should be used instead

Why not both? Sure driver might elect to use only one, but the binding describes the hardware not the driver and both might be wired.

If both interrupt pins are wired, should the DTS file need to define both of the pins?

Quoted 5 lines

Lots of different sources of interrupts so might be advantageous
to split them up across two wires. A simple case being to route
errors to one and everything 'good' to the other. No obligation to support that in the Linux driver though if you don't need to.

Sure I will split into two different wires in bindings.

Quoted 8 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 39

Quoted 6 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +5 -0
+
+  drive-open-drain:
+    description: |
+      set if the specified interrupt pin should be configured as
+      open drain. If not set, defaults to push-pull.

Two pins. Might be different so you need two controls.

Sure, In bindings I will add two different drive controls. If both interrupt pins are wired with different drive options should the DTS file still define both of the pins?

Quoted 10 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 43

Quoted 5 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +4 -0
+
+required:
+  - compatible
+  - reg

As mentioned, need power supplies specified and marked as required (though they may be provided via always on regulators and rely on stubs being created by the regulator subsystem on a given board). Looks like there are at least 2 supplies.

Sure, I will add 2 power supplies in the next series.

Regards
Jagath

maintainer
#8 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Quoted 59 lines · On Thu, 28 Sep 2023 03:07:22 +0530 Jagath Jog J <[email protected]> wrote:

HI Jonathan,

Quoted 47 lines · Jonathan Cameron
Quoted 43 lines · On Mon, 18 Sep 2023 13:33:13 +0530 Jagath Jog J <[email protected]> wrote:

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

---
 .../bindings/iio/imu/bosch,bmi323.yaml        | 81 +++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +29 -0
new file mode 100644
index 000000000000..9c08988103c5
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/bosch,bmi323.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMI323 6-Axis IMU
+
+maintainers:
+  - Jagath Jog J <[email protected]>
+
+description:
+  BMI323 is a 6-axis inertial measurement unit that supports acceleration and
+  gyroscopic measurements with hardware fifo buffering. Sensor also provides
+  events information such as motion, steps, orientation, single and double
+  tap detection.
+
+properties:
+  compatible:
+    const: bosch,bmi323
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    enum:
Quoted 8 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 34

Quoted 5 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +5 -0
+      - INT1
+      - INT2
+    description: |
+      set to "INT1" if INT1 pin should be used as interrupt input, set
+      to "INT2" if INT2 pin should be used instead  

Why not both? Sure driver might elect to use only one, but the binding describes the hardware not the driver and both might be wired.

Quoted 2 lines

If both interrupt pins are wired, should the DTS file need to define both of the pins?

Yes it should. + we need the names to know which is which.
You could rely on order, but it's more flexible to not do so, particularly when you also need to support case where only one is wired.

Jonathan

author maintainer
#9 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Hi Jonathan,

Few more questions before sending the next series.

Quoted 29 lines · Jonathan Cameron
Quoted 25 lines · On Thu, 28 Sep 2023 03:07:22 +0530 Jagath Jog J <[email protected]> wrote:

HI Jonathan,

Quoted 21 lines · Jonathan Cameron
Quoted 4 lines · On Mon, 18 Sep 2023 13:33:13 +0530 Jagath Jog J <[email protected]> wrote:

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 34

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +10 -0
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    enum:
+      - INT1
+      - INT2
+    description: |
+      set to "INT1" if INT1 pin should be used as interrupt input, set
+      to "INT2" if INT2 pin should be used instead

Why not both? Sure driver might elect to use only one, but the binding describes the hardware not the driver and both might be wired.

Quoted 6 lines
Quoted 2 lines

If both interrupt pins are wired, should the DTS file need to define both of the pins?

Yes it should. + we need the names to know which is which.
You could rely on order, but it's more flexible to not do so, particularly when you also need to support case where only one is wired.

In the driver, I currently prioritize INT1 over INT2 when checking (bmi323_trigger_probe(..)) based on the interrupt-names defined in the device tree. However, I'm open to suggestions on the best way to ensure that the order doesn't affect the selection process when both interrupts are defined in the device tree.

Each feature, such as data-ready, watermark, tap, and others, supports either INT1 or INT2. Based on the interrupt pin defined in the device tree, I configure the all the features accordingly.

Regarding your earlier suggestion to have two different controls for drive-open-drain, do I need to define sensor-specific drive controls in bindings for both interrupt pins?
for ex: bosch,irq{1,2}-open-drain

Regards
Jagath

Quoted 5 lines

Jonathan

maintainer
#10 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Quoted 41 lines · On Sun, 8 Oct 2023 11:54:39 +0530 Jagath Jog J <[email protected]> wrote:

Hi Jonathan,

Few more questions before sending the next series.

Quoted 29 lines · Jonathan Cameron
Quoted 25 lines · On Thu, 28 Sep 2023 03:07:22 +0530 Jagath Jog J <[email protected]> wrote:

HI Jonathan,

Quoted 21 lines · Jonathan Cameron
Quoted 4 lines · On Mon, 18 Sep 2023 13:33:13 +0530 Jagath Jog J <[email protected]> wrote:

Add devicetree description document for Bosch BMI323, a 6-Axis IMU.

Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml line 34

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi323.yaml new file +10 -0
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    enum:
+      - INT1
+      - INT2
+    description: |
+      set to "INT1" if INT1 pin should be used as interrupt input, set
+      to "INT2" if INT2 pin should be used instead  

Why not both? Sure driver might elect to use only one, but the binding describes the hardware not the driver and both might be wired.

Quoted 6 lines
Quoted 2 lines

If both interrupt pins are wired, should the DTS file need to define both of the pins?

Yes it should. + we need the names to know which is which.
You could rely on order, but it's more flexible to not do so, particularly when you also need to support case where only one is wired.

Quoted 5 lines

In the driver, I currently prioritize INT1 over INT2 when checking (bmi323_trigger_probe(..)) based on the interrupt-names defined in the device tree. However, I'm open to suggestions on the best way to ensure that the order doesn't affect the selection process when both interrupts are defined in the device tree.

If they are both present it is absolutely fine to pick one in preference to the other.

Quoted 4 lines

Each feature, such as data-ready, watermark, tap, and others, supports either INT1 or INT2. Based on the interrupt pin defined in the device tree, I configure the all the features accordingly.

That's an implementation choice to do them all based on one interrupt pin so absolutely fine to do that in the Linux driver, but the dt binding should allow for other choices as there are sometimes efficiency gains in doing so.

Quoted 5 lines

Regarding your earlier suggestion to have two different controls for drive-open-drain, do I need to define sensor-specific drive controls in bindings for both interrupt pins?
for ex: bosch,irq{1,2}-open-drain

Hmm. We do have precedence for a single control e.g. nxp,fxls8962af.yaml as drive-open-drain. So perhaps just go with that and if anyone is needs different values we can figure it out later. pin control (which is where that binding item comes from) seems to have examples doing much the same. Sets of pins with a single drive-open-drain entry.

Linus, any comments on this as you've dealt with far more similar cases than me!

Jonathan

Quoted 8 lines

Regards
Jagath

Quoted 5 lines

Jonathan

#11 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Quoted 14 lines · Jonathan Cameron
Quoted 4 lines · Jagath Jog J

Regarding your earlier suggestion to have two different controls for drive-open-drain, do I need to define sensor-specific drive controls in bindings for both interrupt pins?
for ex: bosch,irq{1,2}-open-drain

Hmm. We do have precedence for a single control e.g. nxp,fxls8962af.yaml as drive-open-drain. So perhaps just go with that and if anyone is needs different values we can figure it out later. pin control (which is where that binding item comes from) seems to have examples doing much the same. Sets of pins with a single drive-open-drain entry.

Quoted 2 lines

Linus, any comments on this as you've dealt with far more similar cases than me!

Also st,st-sensors use drive-open-drain.

And that in turn is used because the pin control subsystem use that exact property. (See Documentation/devicetree/bindings/pinctrl/pincfg-node.yaml)

So use that.

(I'm so happy to be able to provide a definitive answer for once!)

Yours,
Linus Walleij

maintainer
#12 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Quoted 20 lines · On Tue, 10 Oct 2023 11:06:18 +0200 Linus Walleij <[email protected]>…
Quoted 14 lines · Jonathan Cameron
Quoted 4 lines · Jagath Jog J

Regarding your earlier suggestion to have two different controls for drive-open-drain, do I need to define sensor-specific drive controls in bindings for both interrupt pins?
for ex: bosch,irq{1,2}-open-drain

Hmm. We do have precedence for a single control e.g. nxp,fxls8962af.yaml as drive-open-drain. So perhaps just go with that and if anyone is needs different values we can figure it out later. pin control (which is where that binding item comes from) seems to have examples doing much the same. Sets of pins with a single drive-open-drain entry.

Quoted 2 lines

Linus, any comments on this as you've dealt with far more similar cases than me!

Also st,st-sensors use drive-open-drain.

Quoted 7 lines

And that in turn is used because the pin control subsystem use that exact property. (See Documentation/devicetree/bindings/pinctrl/pincfg-node.yaml)

So use that.

(I'm so happy to be able to provide a definitive answer for once!)

We kind of lost the question along the way. Wasn't so much about whether there was a generic binding but more about whether it is worth providing separate controls for the two IRQ pins? Or just assume no one is crazy enough to play that level of mix and match.

J

Quoted 3 lines

Yours,
Linus Walleij

#13 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Quoted 4 lines · Jonathan Cameron

We kind of lost the question along the way. Wasn't so much about whether there was a generic binding but more about whether it is worth providing separate controls for the two IRQ pins? Or just assume no one is crazy enough to play that level of mix and match.

Ugh no, that's upfront design for a nonexistent use case.

- First, to even consider open drain the designer need to be really short of IRQ lines/rails, and, despite knowing it's a bad idea, decide to share this line between several peripherals, even though it will require I2C traffic to just determine which one even fired the IRQ.

- Second, be interested in using two IRQs to distinguish between different events? When we just faced the situation that we had too few IRQ lines so we need to start sharing them with open drain...?

It's not gonna happen.

Stay with just drive-open-drain; and configure them all as that if that property is set.

Yours,
Linus Walleij

#14 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Quoted 19 lines · On Tue, 10 Oct 2023 21:51:17 +0200 Linus Walleij <[email protected]>…
Quoted 4 lines · Jonathan Cameron

We kind of lost the question along the way. Wasn't so much about whether there was a generic binding but more about whether it is worth providing separate controls for the two IRQ pins? Or just assume no one is crazy enough to play that level of mix and match.

Ugh no, that's upfront design for a nonexistent use case.

- First, to even consider open drain the designer need to be really short of IRQ lines/rails, and, despite knowing it's a bad idea, decide to share this line between several peripherals, even though it will require I2C traffic to just determine which one even fired the IRQ.

- Second, be interested in using two IRQs to distinguish between different events? When we just faced the situation that we had too few IRQ lines so we need to start sharing them with open drain...?

Quoted 4 lines

It's not gonna happen.

Stay with just drive-open-drain; and configure them all as that if that property is set.

Good insights, I'd not really thought about the wider reasons for using this :) Not done any circuit design or embedded board bring up in a long while.

Thanks!

Quoted 4 lines

Yours,
Linus Walleij

author maintainer
#15 lore raw

Re: [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc for BMI323

Hi,

Quoted 25 lines · Jonathan Cameron
Quoted 21 lines · On Tue, 10 Oct 2023 21:51:17 +0200 Linus Walleij <[email protected]>…
Quoted 4 lines · Jonathan Cameron

We kind of lost the question along the way. Wasn't so much about whether there was a generic binding but more about whether it is worth providing separate controls for the two IRQ pins? Or just assume no one is crazy enough to play that level of mix and match.

Ugh no, that's upfront design for a nonexistent use case.

- First, to even consider open drain the designer need to be really short of IRQ lines/rails, and, despite knowing it's a bad idea, decide to share this line between several peripherals, even though it will require I2C traffic to just determine which one even fired the IRQ.

- Second, be interested in using two IRQs to distinguish between different events? When we just faced the situation that we had too few IRQ lines so we need to start sharing them with open drain...?

It's not gonna happen.

Quoted 8 lines
Quoted 2 lines

Stay with just drive-open-drain; and configure them all as that if that property is set.

Good insights, I'd not really thought about the wider reasons for using this :) Not done any circuit design or embedded board bring up in a long while.

Thanks!

Thank you for the explanation and suggestion.

Regards
Jagath.

Quoted 6 lines
Quoted 5 lines

Yours,
Linus Walleij

author maintainer
#16 lore raw

RFC 2/2 iio: imu: Add driver for BMI323 IMU

In reply to Jagath Jog J

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

The driver supports various functionalities, including data ready, FIFO data handling, and events such as tap detection, step counting, and activity interrupts

Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf

---
 MAINTAINERS                          |    7 +
 drivers/iio/imu/Kconfig              |    1 +
 drivers/iio/imu/Makefile             |    1 +
 drivers/iio/imu/bmi323/Kconfig       |   33 +
 drivers/iio/imu/bmi323/Makefile      |    7 +
 drivers/iio/imu/bmi323/bmi323.h      |  198 +++
 drivers/iio/imu/bmi323/bmi323_core.c | 2260 ++++++++++++++++++++++++++
 drivers/iio/imu/bmi323/bmi323_i2c.c  |  115 ++
 drivers/iio/imu/bmi323/bmi323_spi.c  |  106 ++
 9 files changed, 2728 insertions(+)
 create mode 100644 drivers/iio/imu/bmi323/Kconfig
 create mode 100644 drivers/iio/imu/bmi323/Makefile
 create mode 100644 drivers/iio/imu/bmi323/bmi323.h
 create mode 100644 drivers/iio/imu/bmi323/bmi323_core.c
 create mode 100644 drivers/iio/imu/bmi323/bmi323_i2c.c
 create mode 100644 drivers/iio/imu/bmi323/bmi323_spi.c
MAINTAINERS +7 -0
index 4fe86f4ec383..7479e187270a 100644
@@ -3605,6 +3605,13 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml
 F:	drivers/iio/accel/bma400*
 
+BOSCH SENSORTEC BMI323 IMU IIO DRIVER
+M:	Jagath Jog J <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/imu/bosch,bma400.yaml
+F:	drivers/iio/imu/bmi323/
+
 BPF JIT for ARM
 M:	Shubham Bansal <[email protected]>
 L:	[email protected]
drivers/iio/imu/Kconfig +1 -0
index c2f97629e9cd..6c9a85294bc1 100644
@@ -54,6 +54,7 @@ config ADIS16480
 
 source "drivers/iio/imu/bmi160/Kconfig"
 source "drivers/iio/imu/bno055/Kconfig"
+source "drivers/iio/imu/bmi323/Kconfig"
 
 config FXOS8700
 	tristate
drivers/iio/imu/Makefile +1 -0
index 6eb612034722..627406476357 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
 
 obj-y += bmi160/
 obj-y += bno055/
+obj-y += bmi323/
 
 obj-$(CONFIG_FXOS8700) += fxos8700_core.o
 obj-$(CONFIG_FXOS8700_I2C) += fxos8700_i2c.o
drivers/iio/imu/bmi323/Kconfig new file +33 -0
new file mode 100644
index 000000000000..ab37b285393c
@@ -0,0 +1,33 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# BMI323 IMU driver
+#
+
+config BMI323
+	tristate
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+
+config BMI323_I2C
+	tristate "Bosch BMI323 I2C driver"
+	depends on I2C
+	select BMI323
+	select REGMAP_I2C
+	help
+	  Enable support for the Bosch BMI323 6-Axis IMU connected to I2C
+	  interface.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called bmi323_i2c.
+
+config BMI323_SPI
+	tristate "Bosch BMI323 SPI driver"
+	depends on SPI
+	select BMI323
+	select REGMAP_SPI
+	help
+	  Enable support for the Bosch BMI323 6-Axis IMU connected to SPI
+	  interface.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called bmi323_spi.
drivers/iio/imu/bmi323/Makefile new file +7 -0
new file mode 100644
index 000000000000..a6a6dc0207c9
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for Bosch BMI323 IMU
+#
+obj-$(CONFIG_BMI323) += bmi323_core.o
+obj-$(CONFIG_BMI323_I2C) += bmi323_i2c.o
+obj-$(CONFIG_BMI323_SPI) += bmi323_spi.o
drivers/iio/imu/bmi323/bmi323.h new file +198 -0

198 lines not on this page · raw message

drivers/iio/imu/bmi323/bmi323_core.c new file +2260 -0

2,260 lines not on this page · raw message

drivers/iio/imu/bmi323/bmi323_i2c.c new file +115 -0

115 lines not on this page · raw message

drivers/iio/imu/bmi323/bmi323_spi.c new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.20.1
#17 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Quoted 8 lines · Jagath Jog J

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

The driver supports various functionalities, including data ready, FIFO data handling, and events such as tap detection, step counting, and activity interrupts

Missing period.

...

drivers/iio/imu/bmi323/bmi323.h line 12

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323.h new file +2 -0
+#include <linux/regmap.h>
+#include <linux/bits.h>

Ordered?

Missing units.h.

...

drivers/iio/imu/bmi323/bmi323.h line 190

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323.h new file +2 -0
+#define BMI323_INT_MICRO_TO_RAW(val, val2, scale) (((val) * (scale)) + \
+						  (((val2) * (scale)) / MEGA))

Better to split:

#define BMI323_INT_MICRO_TO_RAW(val, val2, scale)
	((val) * (scale) + ((val2) * (scale)) / MEGA)

Also note dropped redundant parentheses.

...

Quoted 1 line

+#define BMI323_RAW_TO_MICRO(raw, scale) ((((raw) % (scale)) * MEGA) / scale)

...

drivers/iio/imu/bmi323/bmi323_core.c line 142

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+struct bmi323_data {
 
+	u32 odrns[2];
+	u32 odrhz[2];
 
 
+	__le16 steps_count[2];
+};

I'm wondering if these 2:s anyhow semantically the same? Shouldn't a definition be used instead of magic number?

...

drivers/iio/imu/bmi323/bmi323_core.c line 299

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +8 -0
+	320 * MEGA,
+	160 * MEGA,
+	80 * MEGA,
+	40 * MEGA,
+	20 * MEGA,
+	10 * MEGA,
+	5 * MEGA,
+	2500000,
	2500 * KILO,

for the sake of consistency?

drivers/iio/imu/bmi323/bmi323_core.c line 300

Quoted 1 line
drivers/iio/imu/bmi323/bmi323_core.c new file +1 -0
+	1250000,
	1250 * KILO,
Quoted 1 line

+};

...

drivers/iio/imu/bmi323/bmi323_core.c line 335

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +6 -0
+static int bmi323_write_ext_reg(struct bmi323_data *data, unsigned int ext_addr,
+				unsigned int ext_data)
+{
+	int ret, feature_status;
+
+	mutex_lock(&data->mutex);

You can start using cleanup.h, it will reduce your code by a few percents! But the point is it makes it less error prone and less verbose.

Ditto for the entire code base.

Quoted 20 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +20 -0
+	ret = regmap_read(data->regmap, BMI323_FEAT_DATA_STATUS,
+			  &feature_status);
+	if (ret)
+		goto unlock_out;
+
+	if (!FIELD_GET(BMI323_FEAT_DATA_TX_RDY_MSK, feature_status)) {
+		ret = -EBUSY;
+		goto unlock_out;
+	}
+
+	ret = regmap_write(data->regmap, BMI323_FEAT_DATA_ADDR, ext_addr);
+	if (ret)
+		goto unlock_out;
+
+	ret = regmap_write(data->regmap, BMI323_FEAT_DATA_TX, ext_data);
+
+unlock_out:
+	mutex_unlock(&data->mutex);
+	return ret;
+}

...

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +8 -0
+static int bmi323_update_ext_reg(struct bmi323_data *data,
+				 unsigned int ext_addr,
+				 unsigned int mask, unsigned int ext_data)
+{
+	unsigned int value;
+	int ret;
+
+	ret = bmi323_read_ext_reg(data, ext_addr, &value);

drivers/iio/imu/bmi323/bmi323_core.c line 406

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +9 -0
+	if (ret)
+		return ret;
+
+	set_mask_bits(&value, mask, ext_data);
 
+	ret = bmi323_write_ext_reg(data, ext_addr, value);
+	if (ret)
+		return ret;
+
+	return 0;
	return _write_ext_reg(...);
Quoted 1 line

+}

...

	unsigned int state_value = GENMASK();

drivers/iio/imu/bmi323/bmi323_core.c line 519

Quoted 11 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +11 -0
+	switch (dir) {
+	case IIO_EV_DIR_RISING:
+		msk = BMI323_FEAT_IO0_XYZ_MOTION_MSK;
+		raw = 512;
+		config = BMI323_ANYMO1_REG;
+		irq_msk = BMI323_MOTION_MSK;
+		set_mask_bits(&irq_field_val, BMI323_MOTION_MSK,
+			      FIELD_PREP(BMI323_MOTION_MSK, motion_irq));
+		set_mask_bits(&field_value, BMI323_FEAT_IO0_XYZ_MOTION_MSK,
+			      FIELD_PREP(BMI323_FEAT_IO0_XYZ_MOTION_MSK,
+					 state ? 7 : 0));

state_value

drivers/iio/imu/bmi323/bmi323_core.c line 530

Quoted 11 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +11 -0
+		break;
+	case IIO_EV_DIR_FALLING:
+		msk = BMI323_FEAT_IO0_XYZ_NOMOTION_MSK;
+		raw = 0;
+		config = BMI323_NOMO1_REG;
+		irq_msk = BMI323_NOMOTION_MSK;
+		set_mask_bits(&irq_field_val, BMI323_NOMOTION_MSK,
+			      FIELD_PREP(BMI323_NOMOTION_MSK, motion_irq));
+		set_mask_bits(&field_value, BMI323_FEAT_IO0_XYZ_NOMOTION_MSK,
+			      FIELD_PREP(BMI323_FEAT_IO0_XYZ_NOMOTION_MSK,
+					 state ? 7 : 0));

Ditto.

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+		break;
+	default:
+		return -EINVAL;
+	}

...

drivers/iio/imu/bmi323/bmi323_core.c line 659

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +8 -0
+static ssize_t in_accel_gesture_tap_max_dur_show(struct device *dev,
+						 struct device_attribute *attr,
+						 char *buf)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct bmi323_data *data = iio_priv(indio_dev);
+	unsigned int reg_value, raw;
+	int ret, val[2];

Why val is int (i.e. not unsigned)?

drivers/iio/imu/bmi323/bmi323_core.c line 669

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +8 -0
+	ret = bmi323_read_ext_reg(data, BMI323_TAP2_REG, &reg_value);
+	if (ret)
+		return ret;
+
+	raw = FIELD_GET(BMI323_TAP2_MAX_DUR_MSK, reg_value);
 
+	val[0] = raw / BMI323_MAX_GES_DUR_SCALE;
+	val[1] = BMI323_RAW_TO_MICRO(raw, BMI323_MAX_GES_DUR_SCALE);
 
+	return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, val);

ARRAY_SIZE()

Quoted 1 line

+}

...

drivers/iio/imu/bmi323/bmi323_core.c line 731

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +9 -0
+static ssize_t in_accel_gesture_tap_timeout_store(struct device *dev,
+						  struct device_attribute *attr,
+						  const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct bmi323_data *data = iio_priv(indio_dev);
+	int ret, val;
 
+	if (kstrtoint(buf, 10, &val))
+		return -EINVAL;

Don't shadow the error code.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!(val == 0 || val == 1))
+		return -EINVAL;

So, effectively you should use kstrtobool().

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +8 -0
+	ret = bmi323_update_ext_reg(data, BMI323_TAP1_REG,
+				    BMI323_TAP1_TIMOUT_MSK,
+				    FIELD_PREP(BMI323_TAP1_TIMOUT_MSK, val));
+	if (ret)
+		return ret;
+
+	return len;
+}

...

drivers/iio/imu/bmi323/bmi323_core.c line 780

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+static const struct attribute_group bmi323_event_attribute_group = {
+	.attrs = bmi323_event_attributes,
+};

ATTRIBUTE_GROUPS() ?

...

drivers/iio/imu/bmi323/bmi323_core.c line 711

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+{
+	struct bmi323_data *data = iio_priv(indio_dev);
 
+	int ret;

Unneeded variable, return directly.

Quoted 14 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +14 -0
+
+	switch (type) {
+	case IIO_EV_TYPE_MAG:
+		ret = bmi323_motion_event_en(data, dir, state);
+		return ret;
+	case IIO_EV_TYPE_GESTURE:
+		return bmi323_tap_event_en(data, dir, state);
+	case IIO_EV_TYPE_CHANGE:
+		ret = bmi323_step_wtrmrk_en(data, state);
+		return ret;
+	default:
+		return -EINVAL;
+	}
+}

...

drivers/iio/imu/bmi323/bmi323_core.c line 880

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+		case IIO_EV_INFO_RESET_TIMEOUT:
+			if (val != 0 || val2 < 40000 || val2 > 600000)
+				return -EINVAL;
			if (val || ...

Use is_range() from minmax.h.

Quoted 7 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +7 -0
+
+			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
+						      BMI323_QUITE_TIM_GES_SCALE);
+
+			return bmi323_update_ext_reg(data, BMI323_TAP3_REG,
+						     BMI323_TAP3_QT_AFT_GES_MSK,
+						     FIELD_PREP(BMI323_TAP3_QT_AFT_GES_MSK,

...

drivers/iio/imu/bmi323/bmi323_core.c line 890

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+		case IIO_EV_INFO_TAP2_MIN_DELAY:
+			if (val != 0 || val2 < 5000 || val2 > 75000)

Ditto.

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +9 -0
+				return -EINVAL;
+
+			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
+						      BMI323_DUR_BW_TAP_SCALE);
+
+			return bmi323_update_ext_reg(data, BMI323_TAP3_REG,
+						     BMI323_TAP3_QT_BW_TAP_MSK,
+						     FIELD_PREP(BMI323_TAP3_QT_BW_TAP_MSK,
+								raw));

...

drivers/iio/imu/bmi323/bmi323_core.c line 911

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+		case IIO_EV_INFO_VALUE:
+			if (val < 0 || val > 7)
+				return -EINVAL;

Ditto.

drivers/iio/imu/bmi323/bmi323_core.c line 921

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +9 -0
+			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
+						      BMI323_MOTION_THRES_SCALE);
+
+			return bmi323_update_ext_reg(data, reg,
+						     BMI323_MO1_SLOPE_TH_MSK,
+						     FIELD_PREP(BMI323_MO1_SLOPE_TH_MSK,
+								raw));
+		case IIO_EV_INFO_PERIOD:
+			if (val < 0 || val > 162)

Ditto.

drivers/iio/imu/bmi323/bmi323_core.c line 933

Quoted 12 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +12 -0
+				return -EINVAL;
+
+			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
+						      BMI323_MOTION_DURAT_SCALE);
+
+			return bmi323_update_ext_reg(data,
+						     reg + BMI323_MO3_OFFSET,
+						     BMI323_MO3_DURA_MSK,
+						     FIELD_PREP(BMI323_MO3_DURA_MSK,
+								raw));
+		case IIO_EV_INFO_HYSTERESIS:
+			if (!(val == 0 || val == 1))

Ditto.

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+				return -EINVAL;
+
+			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
+						      BMI323_MOTION_HYSTR_SCALE);
+
+			return bmi323_update_ext_reg(data,
+						     reg + BMI323_MO2_OFFSET,
+						     BMI323_MO2_HYSTR_MSK,
+						     FIELD_PREP(BMI323_MO2_HYSTR_MSK,
+								raw));

...

drivers/iio/imu/bmi323/bmi323_core.c line 948

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+	case IIO_EV_TYPE_CHANGE:
+		if (val < 0 || val > 20460)

Ditto.

Quoted 7 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +7 -0
+			return -EINVAL;
+
+		raw = val / 20;
+		return bmi323_update_ext_reg(data, BMI323_STEP_SC1_REG,
+					     BMI323_STEP_SC1_WTRMRK_MSK,
+					     FIELD_PREP(BMI323_STEP_SC1_WTRMRK_MSK,
+							raw));

...

drivers/iio/imu/bmi323/bmi323_core.c line 1138

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+	if (val > BMI323_FIFO_FULL_IN_FRAMES)
+		val = BMI323_FIFO_FULL_IN_FRAMES;

min()

...

drivers/iio/imu/bmi323/bmi323_core.c line 1197

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+	ret = regmap_update_bits(data->regmap, BMI323_FIFO_CONF_REG,
+				 BMI323_FIFO_CONF_ACC_GYR_EN_MSK,
+				 FIELD_PREP(BMI323_FIFO_CONF_ACC_GYR_EN_MSK,
+					    3));

GENMASK() ?

Quoted 2 lines
(quoted diff) +2 -0
+	if (ret)
+		goto unlock_out;

...

drivers/iio/imu/bmi323/bmi323_core.c line 1279

Quoted 1 line
drivers/iio/imu/bmi323/bmi323_core.c new file +1 -0
+	state = data->state == BMI323_BUFFER_FIFO ? true : false;

== already results in boolean type.

...

drivers/iio/imu/bmi323/bmi323_core.c line 1286

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+static IIO_DEVICE_ATTR_RO(hwfifo_watermark, 0);
+static IIO_DEVICE_ATTR_RO(hwfifo_enabled, 0);

Place them closer to the respective callbacks.

...

drivers/iio/imu/bmi323/bmi323_core.c line 1320

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+	if (!status || FIELD_GET(BMI323_STATUS_ERROR_MSK, status)) {
+		dev_err(data->dev, "status error = 0x%x\n", status);

If it's not your interrupt you may flood the logs here.

Quoted 2 lines
(quoted diff) +2 -0
+		return IRQ_NONE;
+	}

...

drivers/iio/imu/bmi323/bmi323_core.c line 1464

Quoted 1 line
drivers/iio/imu/bmi323/bmi323_core.c new file +1 -0
+	int ret, raw;

Why raw is signed?

drivers/iio/imu/bmi323/bmi323_core.c line 1470

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+	for (raw = 0; raw < ARRAY_SIZE(bmi323_accel_gyro_avrg); raw++)
+		if (avg == bmi323_accel_gyro_avrg[raw])
+			break;
 
+	if (raw >= ARRAY_SIZE(bmi323_accel_gyro_avrg))

When is the > part true?

Quoted 1 line
(quoted diff) +1 -0
+		return -EINVAL;

...

drivers/iio/imu/bmi323/bmi323_core.c line 1549

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+static const struct attribute_group bmi323_attrs_group = {
+	.attrs = bmi323_attributes,
+};

ATTRIBUTE_GROUPS() ?

...

drivers/iio/imu/bmi323/bmi323_core.c line 1561

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+	ret = bmi323_feature_engine_events(data, BMI323_FEAT_IO0_STP_CNT_MSK,
+					   val ? 1 : 0);

Ternary here...

drivers/iio/imu/bmi323/bmi323_core.c line 1566

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+	if (ret)
+		return ret;
+
+	set_mask_bits(&data->feature_events, BMI323_FEAT_IO0_STP_CNT_MSK,
+		      FIELD_PREP(BMI323_FEAT_IO0_STP_CNT_MSK, val ? 1 : 0));

...and here are dups.

Review

Quoted 1 line
(quoted diff) +1 -0
+	return ret;

Can it be not 0 here?

...

drivers/iio/imu/bmi323/bmi323_core.c line 1616

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+static int bmi323_get_temp_data(struct bmi323_data *data, int *val)
+{
+	unsigned int value;

Why it's not defined as __le16 to begin with?

drivers/iio/imu/bmi323/bmi323_core.c line 1627

Quoted 11 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +11 -0
+	int ret;
+
+	ret = bmi323_get_error_status(data);
+	if (ret)
+		return -EINVAL;
+
+	ret = regmap_read(data->regmap, BMI323_TEMP_REG, &value);
+	if (ret)
+		return ret;
+
+	*val = sign_extend32(le16_to_cpup((const __le16 *)&value), 15);

No, simply no castings here.

Quoted 2 lines
(quoted diff) +2 -0
+	return IIO_VAL_INT;
+}

...

drivers/iio/imu/bmi323/bmi323_core.c line 1657

Quoted 1 line
drivers/iio/imu/bmi323/bmi323_core.c new file +1 -0
+	if (bmi323_acc_gyro_odr[odr_index][0] <= 25)

Why not positive check: if (... > 25) ... else ...?

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+		mode = ACC_GYRO_MODE_DUTYCYCLE;
+	else
+		mode = ACC_GYRO_MODE_CONTINOUS;

...

drivers/iio/imu/bmi323/bmi323_core.c line 1668

Quoted 1 line
drivers/iio/imu/bmi323/bmi323_core.c new file +1 -0
+	int odr_raw, ret;

Why odr_raw is signed?

drivers/iio/imu/bmi323/bmi323_core.c line 1677

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +9 -0
+
+	odr_raw = ARRAY_SIZE(bmi323_acc_gyro_odr);
+
+	while (odr_raw--)
+		if (odr == bmi323_acc_gyro_odr[odr_raw][0] &&
+		    uodr == bmi323_acc_gyro_odr[odr_raw][1])
+			break;
+	if (odr_raw < 0)
+		return -EINVAL;

In one case in the code you used for-loop, here is while-loop. Maybe a bit of consistency?

...

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +6 -0
+static int bmi323_set_scale(struct bmi323_data *data,
+			    enum bmi323_sensor_type sensor, int val, int val2)
+{
+	int scale_raw;
+
+	if (data->state != BMI323_IDLE)

drivers/iio/imu/bmi323/bmi323_core.c line 1727

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+		return -EBUSY;
+
+	scale_raw = bmi323_hw[sensor].scale_table_len;
+
+	while (scale_raw--)
+		if (val == bmi323_hw[sensor].scale_table[scale_raw][0] &&
+		    val2 == bmi323_hw[sensor].scale_table[scale_raw][1])
+			break;
+	if (scale_raw < 0)
+		return -EINVAL;

Note, here is a different case and hence fine to be while-loop.

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+	return regmap_update_bits(data->regmap, bmi323_hw[sensor].config,
+				  BMI323_ACC_GYRO_CONF_SCL_MSK,
+				  FIELD_PREP(BMI323_ACC_GYRO_CONF_SCL_MSK,
+					     scale_raw));
+}

...

drivers/iio/imu/bmi323/bmi323_core.c line 1983

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+	fwnode = dev_fwnode(data->dev);
+	if (!fwnode)
+		return -ENODEV;
+
+	irq = fwnode_irq_get_byname(fwnode, "INT1");
+	if (irq > 0) {
+		irq_pin = BMI323_IRQ_INT1;
+	} else {
+		irq = fwnode_irq_get_byname(fwnode, "INT2");
+		if (irq <= 0)

When can it be == 0?

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+			return 0;
+
+		irq_pin = BMI323_IRQ_INT2;
+	}

...

drivers/iio/imu/bmi323/bmi323_core.c line 1996

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+	irq_type = irqd_get_trigger_type(desc);
 
+

Redundant blank line.

Quoted 11 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +11 -0
+	switch (irq_type) {
+	case IRQF_TRIGGER_RISING:
+		latch = false;
+		active_high = true;
+		break;
+	case IRQF_TRIGGER_HIGH:
+		latch = true;
+		active_high = true;
+		break;
+	case IRQF_TRIGGER_FALLING:
+		latch = false;

drivers/iio/imu/bmi323/bmi323_core.c line 2017

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+		active_high = false;
+		break;
+	case IRQF_TRIGGER_LOW:
+		latch = true;
+		active_high = false;
+		break;
+	default:
+		dev_err(data->dev, "Invalid interrupt type 0x%x specified\n",
+			irq_type);
+		return -EINVAL;

Here and above, why not dev_err_probe() as you used it already below?

Quoted 1 line
(quoted diff) +1 -0
+	}

...

Quoted 17 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +15 -0
+	if (en) {
 
+		ret = regmap_write(data->regmap, BMI323_FEAT_IO2_REG,
+				   0x012c);
+		if (ret)
+			return ret;
+
+		ret = regmap_write(data->regmap, BMI323_FEAT_IO_STATUS_REG,
+				   BMI323_FEAT_IO_STATUS_MSK);
+		if (ret)
+			return ret;
+
+		ret = regmap_write(data->regmap, BMI323_FEAT_CTRL_REG,
+				   BMI323_FEAT_ENG_EN_MSK);
+		if (ret)
+			return ret;
 

drivers/iio/imu/bmi323/bmi323_core.c line 2083

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+		i = 5;
+		do {
+			ret = regmap_read(data->regmap, BMI323_FEAT_IO1_REG,
+					  &feature_status);
+			if (ret)
+				return ret;
+
+			i--;
+			mdelay(2);
+		} while (feature_status != 0x01 && i);

NIH regmap_read_poll_timeout().

drivers/iio/imu/bmi323/bmi323_core.c line 2091

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +7 -0
+		if (feature_status != 0x01) {
+			dev_err(data->dev, "Failed to enable feature engine\n");
+			return -EINVAL;
+		}
+
+		return ret;
 
+	} else {

Redundant. But here it's okay to leave (I can understand the justification).

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+		return regmap_write(data->regmap, BMI323_FEAT_CTRL_REG, 0);
+	}

...

drivers/iio/imu/bmi323/bmi323_core.c line 2137

Quoted 1 line
drivers/iio/imu/bmi323/bmi323_core.c new file +1 -0
+	if ((val & 0xFF) != BMI323_CHIP_ID_VAL) {

GENMASK() ? (BIT(x) - 1) ? A defined constant?

drivers/iio/imu/bmi323/bmi323_core.c line 2139

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+		dev_err(data->dev, "Chip ID mismatch\n");
+		return -EINVAL;

Why not dev_err_probe() in this entire function?

Quoted 1 line
(quoted diff) +1 -0
+	}

...

drivers/iio/imu/bmi323/bmi323_core.c line 2188

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+	ret = devm_add_action_or_reset(data->dev, bmi323_disable, data);
+	if (ret)
+		return ret;
+
+	return 0;
	return devm_...

...

drivers/iio/imu/bmi323/bmi323_core.c line 2202

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+	regmap = dev_get_regmap(dev, NULL);
+	if (!regmap) {
+		dev_err(dev, "No regmap\n");
+		return -EINVAL;

Why not dev_err_probe()?

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +5 -0
+	}
 
+#include <linux/i2c.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/regmap.h>

...

drivers/iio/imu/bmi323/bmi323_i2c.c line 27

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +8 -0
+static int bmi323_regmap_i2c_read(void *context, const void *reg_buf,
+				  size_t reg_size, void *val_buf,
+				  size_t val_size)
+{
+	struct device *dev = context;
+	struct i2c_client *i2c = to_i2c_client(dev);
+	struct i2c_msg msgs[2];
 
+	u8 *buff = NULL;

Redundant assignment.

drivers/iio/imu/bmi323/bmi323_i2c.c line 30

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +3 -0
+	int ret;
+
+	buff = kmalloc(val_size + BMI323_I2C_DUMMY, GFP_KERNEL);

size_add() and include overflow.h for it.

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +6 -0
+	if (!buff)
+		return -ENOMEM;
+
+	msgs[0].addr = i2c->addr;
+	msgs[0].flags = i2c->flags;
+	msgs[0].len = reg_size;

drivers/iio/imu/bmi323/bmi323_i2c.c line 46

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +10 -0
+	msgs[0].buf = (u8 *)reg_buf;
+
+	msgs[1].addr = i2c->addr;
+	msgs[1].len = val_size + BMI323_I2C_DUMMY;
+	msgs[1].buf = (u8 *)buff;
+	msgs[1].flags = i2c->flags | I2C_M_RD;
+
+	ret = i2c_transfer(i2c->adapter, msgs, ARRAY_SIZE(msgs));
+	if (ret < 0) {
+		kfree(buff);

With cleanup.h this will look better.

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +8 -0
+		return -EIO;
+	}
+
+	memcpy(val_buf, buff + BMI323_I2C_DUMMY, val_size);
+	kfree(buff);
+
+	return 0;
+}

...

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +4 -0
+static int bmi323_regmap_i2c_write(void *context, const void *data,
+				   size_t count)
+{
+	struct device *dev = context;

drivers/iio/imu/bmi323/bmi323_i2c.c line 69

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +10 -0
+	struct i2c_client *i2c = to_i2c_client(dev);
+	int ret;
+	u8 reg;
+
+	reg = *(u8 *)data;
+	ret = i2c_smbus_write_i2c_block_data(i2c, reg, count - 1,
+					     data + sizeof(u8));
+
+	return ret;
+}

Hmm... Don't we have a better approach for these? regmap doesn't provide SMBus accessors?

...

drivers/iio/imu/bmi323/bmi323_i2c.c line 91

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +2 -0
+static const struct i2c_device_id bmi323_i2c_ids[] = {
+	{ "bmi323", 0 },

', 0' is redundant

Quoted 2 lines
(quoted diff) +2 -0
+	{ }
+};

...

drivers/iio/imu/bmi323/bmi323_spi.c line 28

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +9 -0
+static int bmi323_regmap_spi_read(void *context, const void *reg_buf,
+				  size_t reg_size, void *val_buf,
+				  size_t val_size)
+{
+	struct spi_device *spi = context;
+	u8 reg, *buff = NULL;
+	int ret;
+
+	buff = kmalloc(val_size + BMI323_SPI_DUMMY, GFP_KERNEL);

As per i2c part.

drivers/iio/imu/bmi323/bmi323_spi.c line 32

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +4 -0
+	if (!buff)
+		return -ENOMEM;
+
+	reg = *(u8 *)reg_buf | 0x80;

Doesn't regmap configuration provide a way to set this?

Quoted 12 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +12 -0
+	ret = spi_write_then_read(spi, &reg, sizeof(reg), buff,
+				  val_size + BMI323_SPI_DUMMY);
+	if (ret) {
+		kfree(buff);
+		return ret;
+	}
+
+	memcpy(val_buf, buff + BMI323_SPI_DUMMY, val_size);
+	kfree(buff);
+
+	return ret;
+}

...

drivers/iio/imu/bmi323/bmi323_i2c.c line 86

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +3 -0
+	regmap = devm_regmap_init(dev, &bmi323_regmap_bus, dev,
+				  &bmi323_regmap_config);
 
+

Redundant blank line.

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +4 -0
+	if (IS_ERR(regmap))
+		return dev_err_probe(dev, PTR_ERR(regmap),
+				     "Failed to initialize SPI Regmap\n");
+

...

drivers/iio/imu/bmi323/bmi323_spi.c line 82

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +2 -0
+static const struct spi_device_id bmi323_spi_ids[] = {
+	{ "bmi323", 0 },

As per above.

Quoted 2 lines
(quoted diff) +2 -0
+	{ }
+};
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#18 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Hi Andy,

Thank you for reviewing.

Quoted 13 lines · Andy Shevchenko
Quoted 8 lines · Jagath Jog J

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

The driver supports various functionalities, including data ready, FIFO data handling, and events such as tap detection, step counting, and activity interrupts

Missing period.

Quoted 8 lines

...

drivers/iio/imu/bmi323/bmi323.h line 12

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323.h new file +2 -0
+#include <linux/regmap.h>
+#include <linux/bits.h>

Ordered?

Missing units.h.

Sure I will correct these in the next series. Please note that I omitted certain portions of your reviews while responding, and I fully agree with the comments that I didn't address. I intend to make the necessary corrections in the next series.
....

Quoted 11 lines

drivers/iio/imu/bmi323/bmi323_core.c line 142

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+struct bmi323_data {
 
+     u32 odrns[2];
+     u32 odrhz[2];
 
 
+     __le16 steps_count[2];
+};

I'm wondering if these 2:s anyhow semantically the same? Shouldn't a definition be used instead of magic number?

The arrays odrns[] and odrhz[] are used to store the ODR in nanoseconds and frequency for both the accelerometer and gyro. Instead of the magic number 2, I will define an enum. For steps_count[] array is of size 2 words and I will define
a separate macro.

Quoted 12 lines

...

drivers/iio/imu/bmi323/bmi323_core.c line 335

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +6 -0
+static int bmi323_write_ext_reg(struct bmi323_data *data, unsigned int ext_addr,
+                             unsigned int ext_data)
+{
+     int ret, feature_status;
+
+     mutex_lock(&data->mutex);

You can start using cleanup.h, it will reduce your code by a few percents! But the point is it makes it less error prone and less verbose.

Quoted 1 line

Ditto for the entire code base.

Sure, thanks for pointing this I will go through cleanup.h. If required I will get back with some questions.

Quoted 37 lines
Quoted 21 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +20 -0
+     ret = regmap_read(data->regmap, BMI323_FEAT_DATA_STATUS,
+                       &feature_status);
+     if (ret)
+             goto unlock_out;
+
+     if (!FIELD_GET(BMI323_FEAT_DATA_TX_RDY_MSK, feature_status)) {
+             ret = -EBUSY;
+             goto unlock_out;
+     }
+
+     ret = regmap_write(data->regmap, BMI323_FEAT_DATA_ADDR, ext_addr);
+     if (ret)
+             goto unlock_out;
+
+     ret = regmap_write(data->regmap, BMI323_FEAT_DATA_TX, ext_data);
+
+unlock_out:
+     mutex_unlock(&data->mutex);
+     return ret;
+}

...

        unsigned int state_value = GENMASK();

drivers/iio/imu/bmi323/bmi323_core.c line 519

Quoted 11 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +11 -0
+     switch (dir) {
+     case IIO_EV_DIR_RISING:
+             msk = BMI323_FEAT_IO0_XYZ_MOTION_MSK;
+             raw = 512;
+             config = BMI323_ANYMO1_REG;
+             irq_msk = BMI323_MOTION_MSK;
+             set_mask_bits(&irq_field_val, BMI323_MOTION_MSK,
+                           FIELD_PREP(BMI323_MOTION_MSK, motion_irq));
+             set_mask_bits(&field_value, BMI323_FEAT_IO0_XYZ_MOTION_MSK,
+                           FIELD_PREP(BMI323_FEAT_IO0_XYZ_MOTION_MSK,
+                                      state ? 7 : 0));
Quoted 1 line

state_value

Sorry I didn't get this, I am updating field_value based on state value. What is the purpose of state_value?

Quoted 22 lines

drivers/iio/imu/bmi323/bmi323_core.c line 530

Quoted 12 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +11 -0
+             break;
+     case IIO_EV_DIR_FALLING:
+             msk = BMI323_FEAT_IO0_XYZ_NOMOTION_MSK;
+             raw = 0;
+             config = BMI323_NOMO1_REG;
+             irq_msk = BMI323_NOMOTION_MSK;
+             set_mask_bits(&irq_field_val, BMI323_NOMOTION_MSK,
+                           FIELD_PREP(BMI323_NOMOTION_MSK, motion_irq));
+             set_mask_bits(&field_value, BMI323_FEAT_IO0_XYZ_NOMOTION_MSK,
+                           FIELD_PREP(BMI323_FEAT_IO0_XYZ_NOMOTION_MSK,
+                                      state ? 7 : 0));

Ditto.

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+             break;
+     default:
+             return -EINVAL;
+     }

...

Quoted 10 lines

drivers/iio/imu/bmi323/bmi323_core.c line 659

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +8 -0
+static ssize_t in_accel_gesture_tap_max_dur_show(struct device *dev,
+                                              struct device_attribute *attr,
+                                              char *buf)
+{
+     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+     struct bmi323_data *data = iio_priv(indio_dev);
+     unsigned int reg_value, raw;
+     int ret, val[2];

Why val is int (i.e. not unsigned)?

iio_format_value() expects int* so I used int.

Quoted 7 lines
Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+     ret = bmi323_read_ext_reg(data, BMI323_TAP2_REG, &reg_value);
+     if (ret)
+             return ret;
+
+     raw = FIELD_GET(BMI323_TAP2_MAX_DUR_MSK, reg_value);
Quoted 6 lines

drivers/iio/imu/bmi323/bmi323_core.c line 669

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+     val[0] = raw / BMI323_MAX_GES_DUR_SCALE;
+     val[1] = BMI323_RAW_TO_MICRO(raw, BMI323_MAX_GES_DUR_SCALE);
 
+     return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, val);

ARRAY_SIZE()

Okay, I will use ARRAY_SIZE() instead of number.

Quoted 6 lines

drivers/iio/imu/bmi323/bmi323_core.c line 780

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+static const struct attribute_group bmi323_event_attribute_group = {
+     .attrs = bmi323_event_attributes,
+};

ATTRIBUTE_GROUPS() ?

Okay, I will use ATTRIBUTE_GROUPS.

Quoted 6 lines

...

Quoted 1 line

+ state = data->state == BMI323_BUFFER_FIFO ? true : false;

== already results in boolean type.

Sure I will directly assign the result of comparison. state = (data->state == BMI323_BUFFER_FIFO);

Quoted 5 lines

...

Quoted 1 line

+ int ret, raw;

Why raw is signed?

I don't have a specific reason for this; however, since it stores register value, it should be unsigned. I will make this correction in the next series

Quoted 11 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1470

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+     for (raw = 0; raw < ARRAY_SIZE(bmi323_accel_gyro_avrg); raw++)
+             if (avg == bmi323_accel_gyro_avrg[raw])
+                     break;
 
+     if (raw >= ARRAY_SIZE(bmi323_accel_gyro_avrg))

When is the > part true?

Quoted 1 line

+ return -EINVAL;

I missed this, > is not possible, I should have used while() here also, I will correct this in the next series.

Quoted 5 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1561

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+     ret = bmi323_feature_engine_events(data, BMI323_FEAT_IO0_STP_CNT_MSK,
+                                        val ? 1 : 0);

Ternary here...

Quoted 7 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1566

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+     if (ret)
+             return ret;
+
+     set_mask_bits(&data->feature_events, BMI323_FEAT_IO0_STP_CNT_MSK,
+                   FIELD_PREP(BMI323_FEAT_IO0_STP_CNT_MSK, val ? 1 : 0));

...and here are dups.

Is the ternary operator not permitted to use?

Quoted 5 lines
Quoted 2 lines

+ return ret;

Can it be not 0 here?

Quoted 7 lines

...

drivers/iio/imu/bmi323/bmi323_core.c line 1616

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+static int bmi323_get_temp_data(struct bmi323_data *data, int *val)
+{
+     unsigned int value;

Why it's not defined as __le16 to begin with?

To match the regmap_read() val parameter I used unsigned int*.

Note: each sensor register values are 16 bit wide
and regmap is configured with .val_bits = 16.

Quoted 13 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1627

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+
+     ret = bmi323_get_error_status(data);
+     if (ret)
+             return -EINVAL;
+
+     ret = regmap_read(data->regmap, BMI323_TEMP_REG, &value);
+     if (ret)
+             return ret;
+
+     *val = sign_extend32(le16_to_cpup((const __le16 *)&value), 15);

No, simply no castings here.

Quoted 12 lines
Quoted 2 lines
(quoted diff) +2 -0
+     return IIO_VAL_INT;
+}

...

Quoted 1 line

+ if (bmi323_acc_gyro_odr[odr_index][0] <= 25)

Why not positive check: if (... > 25) ... else ...?

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+             mode = ACC_GYRO_MODE_DUTYCYCLE;
+     else
+             mode = ACC_GYRO_MODE_CONTINOUS;

Sure, this can also be used. I will update this

Quoted 6 lines

...

Quoted 1 line

+ int odr_raw, ret;

Why odr_raw is signed?

In the below conditions, I am checking for -ve value so odr_raw is signed.

Quoted 13 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1677

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +9 -0
+
+     odr_raw = ARRAY_SIZE(bmi323_acc_gyro_odr);
+
+     while (odr_raw--)
+             if (odr == bmi323_acc_gyro_odr[odr_raw][0] &&
+                 uodr == bmi323_acc_gyro_odr[odr_raw][1])
+                     break;
+     if (odr_raw < 0)
+             return -EINVAL;

In one case in the code you used for-loop, here is while-loop. Maybe a bit of consistency?

Sure, for other case, I will use a while loop instead of a for loop.

Quoted 12 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1983

Quoted 11 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+     fwnode = dev_fwnode(data->dev);
+     if (!fwnode)
+             return -ENODEV;
+
+     irq = fwnode_irq_get_byname(fwnode, "INT1");
+     if (irq > 0) {
+             irq_pin = BMI323_IRQ_INT1;
+     } else {
+             irq = fwnode_irq_get_byname(fwnode, "INT2");
+             if (irq <= 0)
Quoted 1 line

When can it be == 0?

Right, fwnode_irq_get_byname won't return 0, I will correct this in the next series.

Quoted 29 lines
Quoted 18 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +15 -0
+     if (en) {
 
+             ret = regmap_write(data->regmap, BMI323_FEAT_IO2_REG,
+                                0x012c);
+             if (ret)
+                     return ret;
+
+             ret = regmap_write(data->regmap, BMI323_FEAT_IO_STATUS_REG,
+                                BMI323_FEAT_IO_STATUS_MSK);
+             if (ret)
+                     return ret;
+
+             ret = regmap_write(data->regmap, BMI323_FEAT_CTRL_REG,
+                                BMI323_FEAT_ENG_EN_MSK);
+             if (ret)
+                     return ret;
 

drivers/iio/imu/bmi323/bmi323_core.c line 2083

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+             i = 5;
+             do {
+                     ret = regmap_read(data->regmap, BMI323_FEAT_IO1_REG,
+                                       &feature_status);
+                     if (ret)
+                             return ret;
+
+                     i--;
+                     mdelay(2);
+             } while (feature_status != 0x01 && i);
Quoted 1 line

NIH regmap_read_poll_timeout().

Okay.

Quoted 17 lines

drivers/iio/imu/bmi323/bmi323_core.c line 2091

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +7 -0
+             if (feature_status != 0x01) {
+                     dev_err(data->dev, "Failed to enable feature engine\n");
+                     return -EINVAL;
+             }
+
+             return ret;
 
+     } else {

Redundant. But here it's okay to leave (I can understand the justification).

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+             return regmap_write(data->regmap, BMI323_FEAT_CTRL_REG, 0);
+     }

...

Quoted 8 lines
Quoted 1 line

+ if ((val & 0xFF) != BMI323_CHIP_ID_VAL) {

GENMASK() ? (BIT(x) - 1) ? A defined constant?

drivers/iio/imu/bmi323/bmi323_core.c line 2139

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+             dev_err(data->dev, "Chip ID mismatch\n");
+             return -EINVAL;

Why not dev_err_probe() in this entire function?

Okay I will make use of dev_err_probe() here and in all probe paths.

Quoted 9 lines

drivers/iio/imu/bmi323/bmi323_core.c line 2188

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+     ret = devm_add_action_or_reset(data->dev, bmi323_disable, data);
+     if (ret)
+             return ret;
+
+     return 0;
        return devm_...
Quoted 8 lines

...

drivers/iio/imu/bmi323/bmi323_core.c line 2202

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+     regmap = dev_get_regmap(dev, NULL);
+     if (!regmap) {
+             dev_err(dev, "No regmap\n");
+             return -EINVAL;

Why not dev_err_probe()?

There was no int return value from dev_get_regmap(), I think I can use -EINVAL for err in dev_err_probe as well.

Quoted 13 lines
Quoted 13 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +10 -0
+     }
 
 
+static int bmi323_regmap_i2c_write(void *context, const void *data,
+                                size_t count)
+{
+     struct device *dev = context;
+     struct i2c_client *i2c = to_i2c_client(dev);
+     int ret;
+     u8 reg;
+
+     reg = *(u8 *)data;
Quoted 8 lines

drivers/iio/imu/bmi323/bmi323_i2c.c line 69

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +5 -0
+     ret = i2c_smbus_write_i2c_block_data(i2c, reg, count - 1,
+                                          data + sizeof(u8));
+
+     return ret;
+}

Hmm... Don't we have a better approach for these? regmap doesn't provide SMBus accessors?

Custom implementation is required for the 'read' operation, while 'write' can utilize the regmap SMBus accessors. Is it okay to have only custom read while write uses the SMBus accessor?

Quoted 13 lines

drivers/iio/imu/bmi323/bmi323_spi.c line 28

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +9 -0
+static int bmi323_regmap_spi_read(void *context, const void *reg_buf,
+                               size_t reg_size, void *val_buf,
+                               size_t val_size)
+{
+     struct spi_device *spi = context;
+     u8 reg, *buff = NULL;
+     int ret;
+
+     buff = kmalloc(val_size + BMI323_SPI_DUMMY, GFP_KERNEL);

As per i2c part.

Quoted 6 lines

drivers/iio/imu/bmi323/bmi323_spi.c line 32

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +4 -0
+     if (!buff)
+             return -ENOMEM;
+
+     reg = *(u8 *)reg_buf | 0x80;

Doesn't regmap configuration provide a way to set this?

Okay, I will make use of regmap .read_flag_mask member. I will update this in the next series.

Quoted 19 lines

drivers/iio/imu/bmi323/bmi323_spi.c line 44

Quoted 13 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +12 -0
+     ret = spi_write_then_read(spi, &reg, sizeof(reg), buff,
+                               val_size + BMI323_SPI_DUMMY);
+     if (ret) {
+             kfree(buff);
+             return ret;
+     }
+
+     memcpy(val_buf, buff + BMI323_SPI_DUMMY, val_size);
+     kfree(buff);
+
+     return ret;
+}

--
With Best Regards,
Andy Shevchenko

Thank you
Jagath

#19 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Quoted 3 lines · Jagath Jog J
Quoted 1 line · Andy Shevchenko

On Mon, Sep 18, 2023 at 01:33:14PM +0530, Jagath Jog J wrote:

...

Quoted 14 lines
Quoted 14 lines
        unsigned int state_value = GENMASK();
Quoted 11 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +11 -0
+     switch (dir) {
+     case IIO_EV_DIR_RISING:
+             msk = BMI323_FEAT_IO0_XYZ_MOTION_MSK;
+             raw = 512;
+             config = BMI323_ANYMO1_REG;
+             irq_msk = BMI323_MOTION_MSK;
+             set_mask_bits(&irq_field_val, BMI323_MOTION_MSK,
+                           FIELD_PREP(BMI323_MOTION_MSK, motion_irq));
+             set_mask_bits(&field_value, BMI323_FEAT_IO0_XYZ_MOTION_MSK,
+                           FIELD_PREP(BMI323_FEAT_IO0_XYZ_MOTION_MSK,
+                                      state ? 7 : 0));
Quoted 4 lines
Quoted 1 line

state_value

Sorry I didn't get this, I am updating field_value based on state value. What is the purpose of state_value?

Something like this I meant:

	unsigned int state_value = state ? GENMASK(2, 0) : 0;
	...
	switch (dir) {
	case IIO_EV_DIR_RISING:
		...
		set_mask_bits(&field_value, BMI323_FEAT_IO0_XYZ_MOTION_MSK,
			      FIELD_PREP(BMI323_FEAT_IO0_XYZ_MOTION_MSK, state_value));
Quoted 11 lines
Quoted 11 lines

drivers/iio/imu/bmi323/bmi323_core.c line 530

Quoted 11 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +11 -0
+             break;
+     case IIO_EV_DIR_FALLING:
+             msk = BMI323_FEAT_IO0_XYZ_NOMOTION_MSK;
+             raw = 0;
+             config = BMI323_NOMO1_REG;
+             irq_msk = BMI323_NOMOTION_MSK;
+             set_mask_bits(&irq_field_val, BMI323_NOMOTION_MSK,
+                           FIELD_PREP(BMI323_NOMOTION_MSK, motion_irq));
+             set_mask_bits(&field_value, BMI323_FEAT_IO0_XYZ_NOMOTION_MSK,
+                           FIELD_PREP(BMI323_FEAT_IO0_XYZ_NOMOTION_MSK,
+                                      state ? 7 : 0));

Ditto.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+             break;
+     default:
+             return -EINVAL;
+     }

...

Quoted 11 lines
Quoted 11 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1561

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+     ret = bmi323_feature_engine_events(data, BMI323_FEAT_IO0_STP_CNT_MSK,
+                                        val ? 1 : 0);

Ternary here...

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+     if (ret)
+             return ret;
+
+     set_mask_bits(&data->feature_events, BMI323_FEAT_IO0_STP_CNT_MSK,
+                   FIELD_PREP(BMI323_FEAT_IO0_STP_CNT_MSK, val ? 1 : 0));
Quoted 3 lines
Quoted 1 line

...and here are dups.

Is the ternary operator not permitted to use?

Yes, it's permitted. My point that you can calculate once the value

	unsigned int value = val ? 1 : 0;

and use it everywhere where it makes sense.

...

Quoted 6 lines
Quoted 5 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1616

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+static int bmi323_get_temp_data(struct bmi323_data *data, int *val)
+{
+     unsigned int value;

Why it's not defined as __le16 to begin with?

Quoted 12 lines

To match the regmap_read() val parameter I used unsigned int*.

Note: each sensor register values are 16 bit wide
and regmap is configured with .val_bits = 16.

Quoted 7 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1627

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+     ret = regmap_read(data->regmap, BMI323_TEMP_REG, &value);
+     if (ret)
+             return ret;
+
+     *val = sign_extend32(le16_to_cpup((const __le16 *)&value), 15);

No, simply no castings here.

This is an interesting case.

Your regmap configuration tells about endianess of the accessors. Default IIRC is defined either by bus or by driver itself.

That said, since you are not using _raw variants of the accessors, the above will give you a wrong result on BE.

Hence

	*val = sign_extend32(&value), 15);

should be enough.

(Note, the _raw variants take void pointer on purpose.)

...

Quoted 9 lines
Quoted 6 lines

drivers/iio/imu/bmi323/bmi323_core.c line 2202

Quoted 4 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +4 -0
+     regmap = dev_get_regmap(dev, NULL);
+     if (!regmap) {
+             dev_err(dev, "No regmap\n");
+             return -EINVAL;

Why not dev_err_probe()?

There was no int return value from dev_get_regmap(), I think I can use -EINVAL for err in dev_err_probe as well.

Yes, it's fine.

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ }

...

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +9 -0
+static int bmi323_regmap_i2c_write(void *context, const void *data,
+                                size_t count)
+{
+     struct device *dev = context;
+     struct i2c_client *i2c = to_i2c_client(dev);
+     int ret;
+     u8 reg;
+
+     reg = *(u8 *)data;
Quoted 8 lines

drivers/iio/imu/bmi323/bmi323_i2c.c line 69

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +5 -0
+     ret = i2c_smbus_write_i2c_block_data(i2c, reg, count - 1,
+                                          data + sizeof(u8));
+
+     return ret;
+}

Hmm... Don't we have a better approach for these? regmap doesn't provide SMBus accessors?

Quoted 3 lines

Custom implementation is required for the 'read' operation, while 'write' can utilize the regmap SMBus accessors. Is it okay to have only custom read while write uses the SMBus accessor?

Yes, why not, but I don't know if regmap API allows this.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#20 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

In reply to Jagath Jog J

Hi Andy,

Quoted 10 lines · Jagath Jog J
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Jagath Jog J

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

...

Quoted 7 lines
Quoted 5 lines

drivers/iio/imu/bmi323/bmi323_core.c line 780

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+static const struct attribute_group bmi323_event_attribute_group = {
+     .attrs = bmi323_event_attributes,
+};

ATTRIBUTE_GROUPS() ?

Okay, I will use ATTRIBUTE_GROUPS.

The ATTRIBUTE_GROUP(bmi323_event) macro will define two variables, bmi323_event_groups and bmi323_event_group. The event_attrs member of iio_info is of type struct attribute_group*, which means that bmi323_event_groups will remain unused. Since I am using a single event group, Can I keep it the same way?

Regards
Jagath

Jagath

maintainer
#21 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Quoted 23 lines · On Sun, 8 Oct 2023 11:55:32 +0530 Jagath Jog J <[email protected]> wrote:

Hi Andy,

Quoted 10 lines · Jagath Jog J
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Jagath Jog J

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

...

Quoted 6 lines
Quoted 5 lines

drivers/iio/imu/bmi323/bmi323_core.c line 780

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+static const struct attribute_group bmi323_event_attribute_group = {
+     .attrs = bmi323_event_attributes,
+};  

ATTRIBUTE_GROUPS() ?

Quoted 7 lines
Quoted 1 line

Okay, I will use ATTRIBUTE_GROUPS.

The ATTRIBUTE_GROUP(bmi323_event) macro will define two variables, bmi323_event_groups and bmi323_event_group. The event_attrs member of iio_info is of type struct attribute_group*, which means that bmi323_event_groups will remain unused. Since I am using a single event group, Can I keep it the same way?

Yes, don't use that macro as not appropriate in this case.

Quoted 6 lines

Regards
Jagath

Jagath

maintainer
#22 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

In reply to Jagath Jog J
Quoted 11 lines · On Mon, 18 Sep 2023 13:33:14 +0530 Jagath Jog J <[email protected]> wrote:

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

The driver supports various functionalities, including data ready, FIFO data handling, and events such as tap detection, step counting, and activity interrupts

Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf

Given Andy has done a thorough review (as he always does!) and there is quite a bit in my review queue, I'll just scan through quickly and call out a few things. Will take a closer look at next version.

One thing that is useful for a complex driver like this is to include (typically in the cover letter) a full listing of what shows up in sysfs. That gives an easy way to check the ABI looks right without having to figure out what all the generated file names are from the code.

Thanks,

Jonathan

Quoted 40 lines
---
 MAINTAINERS                          |    7 +
 drivers/iio/imu/Kconfig              |    1 +
 drivers/iio/imu/Makefile             |    1 +
 drivers/iio/imu/bmi323/Kconfig       |   33 +
 drivers/iio/imu/bmi323/Makefile      |    7 +
 drivers/iio/imu/bmi323/bmi323.h      |  198 +++
 drivers/iio/imu/bmi323/bmi323_core.c | 2260 ++++++++++++++++++++++++++
 drivers/iio/imu/bmi323/bmi323_i2c.c  |  115 ++
 drivers/iio/imu/bmi323/bmi323_spi.c  |  106 ++
 9 files changed, 2728 insertions(+)
 create mode 100644 drivers/iio/imu/bmi323/Kconfig
 create mode 100644 drivers/iio/imu/bmi323/Makefile
 create mode 100644 drivers/iio/imu/bmi323/bmi323.h
 create mode 100644 drivers/iio/imu/bmi323/bmi323_core.c
 create mode 100644 drivers/iio/imu/bmi323/bmi323_i2c.c
 create mode 100644 drivers/iio/imu/bmi323/bmi323_spi.c
drivers/iio/imu/bmi323/bmi323_core.c new file +14 -0
new file mode 100644
index 000000000000..adbcfbc6b97d
@@ -0,0 +1,2260 @@
 
+struct bmi323_data {
+	struct device *dev;
+	struct regmap *regmap;
+	struct iio_mount_matrix orientation;
+	enum bmi323_irq_pin irq_pin;
+	struct iio_trigger *trig;
+	bool drdy_trigger_enabled;
+	enum bmi323_state state;
+	s64 fifo_tstamp, old_fifo_tstamp;
+	u32 odrns[2];
+	u32 odrhz[2];
+	unsigned int feature_events;
+
+	/*

drivers/iio/imu/bmi323/bmi323_core.c line 139

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+	 * Lock to protect the members of device's private data from concurrent
+	 * access and also to serialize the access of extended registers.
+	 * See bmi323_write_ext_reg(..) for more info.
+	 */
+	struct mutex mutex;
+	int watermark;
+	__le16 fifo_buff[BMI323_FIFO_FULL_IN_WORDS] __aligned(IIO_DMA_MINALIGN);
+	struct {
+		__le16 channels[6];
+		s64 ts __aligned(8);

Hopefully Andy's aligned_s64 set will land soon and we can tidy this up. I'm a bit unsure of this, but can you overlap some of these buffers or are they used concurrently? (if they are then we have problems with DMA safety.)

Perhaps an anonymous union is appropriate?

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +3 -0
+	} buffer;
+	__le16 steps_count[2];
+};
 
 
 
 
 

drivers/iio/imu/bmi323/bmi323_core.c line 1233

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+static int bmi323_buffer_preenable(struct iio_dev *indio_dev)
+{
+	struct bmi323_data *data = iio_priv(indio_dev);
+	int ret = 0;
+
+	mutex_lock(&data->mutex);
+	/*
+	 * When the ODR of the accelerometer and gyroscope do not match, the
+	 * maximum ODR value between the accelerometer and gyroscope is used
+	 * for FIFO and the signal with lower ODR will insert dummy frame.

yuk.

drivers/iio/imu/bmi323/bmi323_core.c line 1235

Quoted 2 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +2 -0
+	 * So allow buffer read only when ODR's of accelero and gyro are equal.
+	 * See datasheet section 5.7 "FIFO Data Buffering".

Good :)

Quoted 25 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +24 -0
+	 */
+	if (data->odrns[BMI323_ACCEL] != data->odrns[BMI323_GYRO]) {
+		dev_err(data->dev, "Accelero and Gyro ODR doesn't match\n");
+		ret = -EINVAL;
+	}
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}
 
+
+static int bmi323_set_drdy_irq(struct bmi323_data *data,
+			       enum bmi323_irq_pin irq_pin)
+{
+	int ret;
+
+	ret = regmap_update_bits(data->regmap, BMI323_INT_MAP2_REG,
+				 BMI323_GYR_DRDY_MSK,
+				 FIELD_PREP(BMI323_GYR_DRDY_MSK, irq_pin));
+	if (ret)
+		return ret;
+
+	return regmap_update_bits(data->regmap, BMI323_INT_MAP2_REG,
+				  BMI323_ACC_DRDY_MSK,
+				  FIELD_PREP(BMI323_ACC_DRDY_MSK, irq_pin));

drivers/iio/imu/bmi323/bmi323_core.c line 1408

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+}
+
+static int bmi323_data_rdy_trigger_set_state(struct iio_trigger *trig,
+					     bool state)
+{
+	struct bmi323_data *data = iio_trigger_get_drvdata(trig);
+	enum bmi323_irq_pin irq_pin;
+	int ret;
+
+	mutex_lock(&data->mutex);

guard(mutex)(&data->mutex);

Quoted 25 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +25 -0
+
+	if (data->state == BMI323_BUFFER_FIFO) {
+		dev_warn(data->dev, "Can't set trigger when FIFO enabled\n");
+		ret = -EBUSY;
+		goto unlock_out;
+	}
+
+	if (state) {
+		data->state = BMI323_BUFFER_DRDY_TRIGGERED;
+		irq_pin = data->irq_pin;
+	} else {
+		data->state = BMI323_IDLE;
+		irq_pin = BMI323_IRQ_DISABLED;
+	}
+
+	ret = bmi323_set_drdy_irq(data, irq_pin);
+
+unlock_out:
+	mutex_unlock(&data->mutex);
+	return ret;
+}
+
+static const struct iio_trigger_ops bmi323_trigger_ops = {
+	.set_trigger_state = &bmi323_data_rdy_trigger_set_state,
+};

drivers/iio/imu/bmi323/bmi323_core.c line 1443

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+
+static irqreturn_t bmi323_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct bmi323_data *data = iio_priv(indio_dev);
+	int ret;
+
+	/* Lock to protect the data->buffer */
+	mutex_lock(&data->mutex);

scoped_guard(mutex)(&data->mutex); might work well here though it will push up the indent.

Quoted 16 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +16 -0
+	ret = regmap_bulk_read(data->regmap, BMI323_ACCEL_X_REG,
+			       &data->buffer.channels,
+			       ARRAY_SIZE(data->buffer.channels));
+	if (ret)
+		goto unlock_out;
+
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
+					   iio_get_time_ns(indio_dev));
+	mutex_unlock(&data->mutex);
+	iio_trigger_notify_done(indio_dev->trig);
+	return IRQ_HANDLED;
+
+unlock_out:
+	mutex_unlock(&data->mutex);
+	return IRQ_NONE;
+}

drivers/iio/imu/bmi323/bmi323_core.c

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +7 -0
+
 
 
 
+static IIO_DEVICE_ATTR_RW(in_accel_gyro_averaging, 0);
+static IIO_CONST_ATTR(in_accel_gyro_averaging_available, "2 4 8 16 32 64");
+
+static struct attribute *bmi323_attributes[] = {
+	&iio_dev_attr_in_accel_gyro_averaging.dev_attr.attr,
+	&iio_const_attr_in_accel_gyro_averaging_available.dev_attr.attr,

So averaging often maps directly to oversampling. Kind of different names for the same thing. Perhaps that standard ABI can be used?
It tends to make sampling frequency reporting need to take it into account though as that drops as divided by oversampling ratio.

Quoted 56 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +55 -0
+	NULL
+};
+
 
+}
+
+static int bmi323_write_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan, int val,
+			    int val2, long mask)
+{
+	struct bmi323_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		ret = iio_device_claim_direct_mode(indio_dev);
+		if (ret)
+			return ret;
+
+		mutex_lock(&data->mutex);
+		ret = bmi323_set_odr(data, bmi323_iio_to_sensor(chan->type),
+				     val, val2);
+		mutex_unlock(&data->mutex);
+		iio_device_release_direct_mode(indio_dev);
+		return ret;
+	case IIO_CHAN_INFO_SCALE:
+		ret = iio_device_claim_direct_mode(indio_dev);
+		if (ret)
+			return ret;
+
+		mutex_lock(&data->mutex);
+		ret = bmi323_set_scale(data, bmi323_iio_to_sensor(chan->type),
+				       val, val2);
+		mutex_unlock(&data->mutex);
+		iio_device_release_direct_mode(indio_dev);
+		return ret;
+	case IIO_CHAN_INFO_ENABLE:
+		mutex_lock(&data->mutex);
+		ret = bmi323_enable_steps(data, val);
+		mutex_unlock(&data->mutex);
+		return ret;
+	case IIO_CHAN_INFO_PROCESSED:
+		if (val || !FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK,
+				      data->feature_events))
+			return -EINVAL;
+
+		/* Clear step counter value */
+		return bmi323_update_ext_reg(data, BMI323_STEP_SC1_REG,
+					     BMI323_STEP_SC1_RST_CNT_MSK,
+					     FIELD_PREP(BMI323_STEP_SC1_RST_CNT_MSK,
+							1));
+	default:
+		return -EINVAL;
+	}
+}
+

drivers/iio/imu/bmi323/bmi323_core.c line 1818

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+static int bmi323_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan, int *val,
+			   int *val2, long mask)
+{
+	struct bmi323_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&data->mutex);

scoped_guard() might be mice for some of these cases
or push the guards down into the function being called perhaps.

Quoted 77 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +77 -0
+		ret = bmi323_read_steps(data, val);
+		mutex_unlock(&data->mutex);
+		return ret;
+	case IIO_CHAN_INFO_RAW:
+		switch (chan->type) {
+		case IIO_ACCEL:
+		case IIO_ANGL_VEL:
+			ret = iio_device_claim_direct_mode(indio_dev);
+			if (ret)
+				return ret;
+
+			mutex_lock(&data->mutex);
+			ret = bmi323_read_axis(data, chan, val);
+			mutex_unlock(&data->mutex);
+
+			iio_device_release_direct_mode(indio_dev);
+			return ret;
+		case IIO_TEMP:
+			mutex_lock(&data->mutex);
+			ret = bmi323_get_temp_data(data, val);
+			mutex_unlock(&data->mutex);
+			return ret;
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		mutex_lock(&data->mutex);
+		ret = bmi323_get_odr(data, chan->type, val, val2);
+		mutex_unlock(&data->mutex);
+		return ret;
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_ACCEL:
+		case IIO_ANGL_VEL:
+			*val = 0;
+			mutex_lock(&data->mutex);
+			ret = bmi323_get_scale(data, chan->type, val2);
+			mutex_unlock(&data->mutex);
+			return ret;
+		case IIO_TEMP:
+			*val = BMI323_TEMP_SCALE / MEGA;
+			*val2 = BMI323_TEMP_SCALE % MEGA;
+			return IIO_VAL_INT_PLUS_MICRO;
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_OFFSET:
+		switch (chan->type) {
+		case IIO_TEMP:
+			*val = BMI323_TEMP_OFFSET;
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_ENABLE:
+		mutex_lock(&data->mutex);
+		*val = FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK,
+				 data->feature_events);
+		mutex_unlock(&data->mutex);
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info bmi323_info = {
+	.read_raw = bmi323_read_raw,
+	.write_raw = bmi323_write_raw,
+	.read_avail = bmi323_read_avail,
+	.attrs = &bmi323_attrs_group,
+	.hwfifo_set_watermark = bmi323_set_watermark,
+	.write_event_config = bmi323_write_event_config,
+	.read_event_config = bmi323_read_event_config,
+	.write_event_value = bmi323_write_event_value,
+	.read_event_value = bmi323_read_event_value,
+	.event_attrs = &bmi323_event_attribute_group,
+};

drivers/iio/imu/bmi323/bmi323_core.c line 1905

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+
+#define BMI323_SCAN_MASK_ACCEL_3AXIS		\
+	(BIT(BMI323_ACCEL_X) | BIT(BMI323_ACCEL_Y) | BIT(BMI323_ACCEL_Z))
+
+#define BMI323_SCAN_MASK_GYRO_3AXIS		\
+	(BIT(BMI323_GYRO_X) | BIT(BMI323_GYRO_Y) | BIT(BMI323_GYRO_Z))
+
+static const unsigned long bmi323_avail_scan_masks[] = {
+	/* 3-axis accel + 3-axis gyro */
+	BMI323_SCAN_MASK_ACCEL_3AXIS | BMI323_SCAN_MASK_GYRO_3AXIS,

Can you poke this an see if you get what you expect which is the minimum sufficient set of channels. Matti pointed out earlier that the search logic isn't well documented in iio_scan_mask_match() but it looks to match against first case where the requested values are a subset. So this would need to be in the opposite order or you will always get everything turned on.

Chances are we have this wrong in other drivers as well :( Won't break things, but may mean that we over read in some configurations.

Quoted 16 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +16 -0
+	/* 3-axis accel */
+	BMI323_SCAN_MASK_ACCEL_3AXIS,
+	/* 3-axis gyro */
+	BMI323_SCAN_MASK_GYRO_3AXIS,
+	0
+};
+
+static int bmi323_int_pin_config(struct bmi323_data *data,
+				 enum bmi323_irq_pin irq_pin,
+				 bool active_high, bool open_drain, bool latch)
+{
+	unsigned int mask, field_value = 0;
+	int ret;
+
+	switch (irq_pin) {
+	case BMI323_IRQ_INT1:

drivers/iio/imu/bmi323/bmi323_core.c line 1931

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+		mask = BMI323_IO_INT1_LVL_OD_OP_MSK;
+
+		set_mask_bits(&field_value, BMI323_IO_INT1_LVL_MSK,
+			      FIELD_PREP(BMI323_IO_INT1_LVL_MSK, active_high));
+
+		set_mask_bits(&field_value, BMI323_IO_INT1_OD_MSK,
+			      FIELD_PREP(BMI323_IO_INT1_OD_MSK, open_drain));
+
+		set_mask_bits(&field_value, BMI323_IO_INT1_OP_EN_MSK,
+			      FIELD_PREP(BMI323_IO_INT1_OP_EN_MSK, 1));

Given you are filling in a value that starts as zero and don't overwrite values you set earlier this is much simpler as

		mask = ...
		field_value = FIELD_PREP(...) |
			      FIELD_PREP(...) |
			      FIELD_PREP(...);

Also move working this out to just before it is used. Right now it is hard to spot where that is.

Quoted 97 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +97 -0
+		break;
+	case BMI323_IRQ_INT2:
+		mask = BMI323_IO_INT2_LVL_OD_OP_MSK;
+
+		set_mask_bits(&field_value, BMI323_IO_INT2_LVL_MSK,
+			      FIELD_PREP(BMI323_IO_INT2_LVL_MSK, active_high));
+
+		set_mask_bits(&field_value, BMI323_IO_INT2_OD_MSK,
+			      FIELD_PREP(BMI323_IO_INT2_OD_MSK, open_drain));
+
+		set_mask_bits(&field_value, BMI323_IO_INT2_OP_EN_MSK,
+			      FIELD_PREP(BMI323_IO_INT2_OP_EN_MSK, 1));
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	ret = regmap_update_bits(data->regmap, BMI323_IO_INT_CONF_REG,
+				 BMI323_IO_INT_LTCH_MSK,
+				 FIELD_PREP(BMI323_IO_INT_LTCH_MSK, latch));
+	if (ret)
+		return ret;
+
+	ret = bmi323_update_ext_reg(data, BMI323_GEN_SET1_REG,
+				    BMI323_GEN_HOLD_DUR_MSK,
+				    FIELD_PREP(BMI323_GEN_HOLD_DUR_MSK, 0));
+	if (ret)
+		return ret;
+
+	return regmap_update_bits(data->regmap, BMI323_IO_INT_CTR_REG, mask,
+				  field_value);
+}
+
+static int bmi323_trigger_probe(struct bmi323_data *data,
+				struct iio_dev *indio_dev)
+{
+	bool open_drain, active_high, latch;
+	struct fwnode_handle *fwnode;
+	enum bmi323_irq_pin irq_pin;
+	int ret, irq, irq_type;
+	struct irq_data *desc;
+
+	fwnode = dev_fwnode(data->dev);
+	if (!fwnode)
+		return -ENODEV;
+
+	irq = fwnode_irq_get_byname(fwnode, "INT1");
+	if (irq > 0) {
+		irq_pin = BMI323_IRQ_INT1;
+	} else {
+		irq = fwnode_irq_get_byname(fwnode, "INT2");
+		if (irq <= 0)
+			return 0;
+
+		irq_pin = BMI323_IRQ_INT2;
+	}
+
+	desc = irq_get_irq_data(irq);
+	if (!desc) {
+		dev_err(data->dev, "Could not find IRQ %d\n", irq);
+		return -EINVAL;
+	}
+
+	irq_type = irqd_get_trigger_type(desc);
+
+	switch (irq_type) {
+	case IRQF_TRIGGER_RISING:
+		latch = false;
+		active_high = true;
+		break;
+	case IRQF_TRIGGER_HIGH:
+		latch = true;
+		active_high = true;
+		break;
+	case IRQF_TRIGGER_FALLING:
+		latch = false;
+		active_high = false;
+		break;
+	case IRQF_TRIGGER_LOW:
+		latch = true;
+		active_high = false;
+		break;
+	default:
+		dev_err(data->dev, "Invalid interrupt type 0x%x specified\n",
+			irq_type);
+		return -EINVAL;
+	}
+
+	open_drain = fwnode_property_read_bool(fwnode, "drive-open-drain");
+
+	ret = bmi323_int_pin_config(data, irq_pin, active_high, open_drain,
+				    latch);
+	if (ret)
+		return dev_err_probe(data->dev, ret,
+				     "Failed to configure irq line\n");
+
+	data->trig = devm_iio_trigger_alloc(data->dev, "%s-trig-%d",

drivers/iio/imu/bmi323/bmi323_core.c line 2038

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+					    indio_dev->name, irq_pin);
+	if (!data->trig)
+		return -ENOMEM;
+
+	data->trig->ops = &bmi323_trigger_ops;
+	iio_trigger_set_drvdata(data->trig, data);
+
+	ret = devm_request_threaded_irq(data->dev, irq, NULL,
+					bmi323_irq_thread_handler,
+					irq_type | IRQF_ONESHOT,

I think if you leave the irq_type bit out, it will be set up to match what was specified in firmware anyway. Could be wrong on that though so check.

Quoted 26 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +26 -0
+					"bmi323-int", indio_dev);
+	if (ret)
+		return dev_err_probe(data->dev, ret, "Failed to request IRQ\n");
+
+	ret = devm_iio_trigger_register(data->dev, data->trig);
+	if (ret)
+		return dev_err_probe(data->dev, ret,
+				     "Trigger registration failed\n");
+
+	data->irq_pin = irq_pin;
+
+	return ret;
+}
+
+static int bmi323_feature_engine_enable(struct bmi323_data *data, bool en)
+{
+	unsigned int feature_status;
+	int ret, i;
+
+	if (en) {
+		ret = regmap_write(data->regmap, BMI323_FEAT_IO2_REG,
+				   0x012c);
+		if (ret)
+			return ret;
+
+		ret = regmap_write(data->regmap, BMI323_FEAT_IO_STATUS_REG,

drivers/iio/imu/bmi323/bmi323_core.c line 2074

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+				   BMI323_FEAT_IO_STATUS_MSK);
+		if (ret)
+			return ret;
+
+		ret = regmap_write(data->regmap, BMI323_FEAT_CTRL_REG,
+				   BMI323_FEAT_ENG_EN_MSK);
+		if (ret)
+			return ret;
+
+		i = 5;

Why 5?

drivers/iio/imu/bmi323/bmi323_core.c line 2083

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +9 -0
+		do {
+			ret = regmap_read(data->regmap, BMI323_FEAT_IO1_REG,
+					  &feature_status);
+			if (ret)
+				return ret;
+
+			i--;
+			mdelay(2);
+		} while (feature_status != 0x01 && i);

GET_FIELD() always rather than directly masking out values It means we have a clear name for what is being checked.

drivers/iio/imu/bmi323/bmi323_core.c line 2092

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +9 -0
+
+		if (feature_status != 0x01) {
+			dev_err(data->dev, "Failed to enable feature engine\n");
+			return -EINVAL;
+		}
+
+		return ret;
+	} else {
+		return regmap_write(data->regmap, BMI323_FEAT_CTRL_REG, 0);

Flip the logic for readability by reducing indent

	if (!en)
		return regmap_write()

	ret = regmap_write() etc
Quoted 19 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +18 -0
+	}
+}
 
+static int bmi323_init(struct bmi323_data *data)
+{
+	int ret, val;
+
+	/*
+	 * Perform soft reset to make sure the device is in a known state after
+	 * start up. A delay of 1.5 ms is required after reset.
+	 * See datasheet section 5.17 "Soft Reset".
+	 */
+	ret = regmap_write(data->regmap, BMI323_CMD_REG, BMI323_RST_VAL);
+	if (ret)
+		return ret;
+
+	usleep_range(1500, 2000);
+
+	/*

drivers/iio/imu/bmi323/bmi323_core.c line 2137

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+	 * Dummy read is required to enable SPI interface after reset.
+	 * See datasheet section 7.2.1 "Protocol Selection".
+	 */
+	regmap_read(data->regmap, BMI323_CHIP_ID_REG, &val);
+
+	ret = regmap_read(data->regmap, BMI323_CHIP_ID_REG, &val);
+	if (ret)
+		return ret;
+
+	if ((val & 0xFF) != BMI323_CHIP_ID_VAL) {

FIELD_GET() and appropriate mask given this is half of a 16 bit register.

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+		dev_err(data->dev, "Chip ID mismatch\n");
+		return -EINVAL;
+	}
+
+	ret = bmi323_feature_engine_enable(data, true);

drivers/iio/imu/bmi323/bmi323_core.c line 2152

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+	if (ret)
+		return ret;
+
+	ret = regmap_read(data->regmap, BMI323_ERR_REG, &val);
+	if (ret)
+		return ret;
+
+	if (val) {
+		dev_err(data->dev, "Sensor power error = 0x%x\n", val);
+		return -EINVAL;

I think this is only called in probe() so an use dev_err_probe() in here as well.

drivers/iio/imu/bmi323/bmi323_core.c line 2159

Quoted 7 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +7 -0
+	}
+
+	ret = regmap_read(data->regmap, BMI323_STATUS_REG, &val);
+	if (ret)
+		return ret;
+
+	if (val != 0x01) {

FIELD_GET() and appropriate definition.

Quoted 66 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +66 -0
+		dev_err(data->dev, "Sensor initialization error = 0x%x\n", val);
+		return -EINVAL;
+	}
+
+	/*
+	 * Set the Bandwidth coefficient which defines the 3 dB cutoff
+	 * frequency in relation to the ODR.
+	 */
+	ret = bmi323_set_bw(data, BMI323_ACCEL, BMI323_BW_ODR_BY_2);
+	if (ret)
+		return -EINVAL;
+
+	ret = bmi323_set_bw(data, BMI323_GYRO, BMI323_BW_ODR_BY_2);
+	if (ret)
+		return -EINVAL;
+
+	ret = bmi323_set_odr(data, BMI323_ACCEL, 25, 0);
+	if (ret)
+		return -EINVAL;
+
+	ret = bmi323_set_odr(data, BMI323_GYRO, 25, 0);
+	if (ret)
+		return -EINVAL;
+
+	ret = devm_add_action_or_reset(data->dev, bmi323_disable, data);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+int bmi323_core_probe(struct device *dev)
+{
+	static const char * const regulator_names[] = { "vdd", "vddio" };
+	struct iio_dev *indio_dev;
+	struct bmi323_data *data;
+	struct regmap *regmap;
+	int ret;
+
+	regmap = dev_get_regmap(dev, NULL);
+	if (!regmap) {
+		dev_err(dev, "No regmap\n");
+		return -EINVAL;
+	}
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulator_names),
+					     regulator_names);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable regulators\n");
+
+	data = iio_priv(indio_dev);
+	data->dev = dev;
+	data->regmap = regmap;
+	mutex_init(&data->mutex);
+
+	ret = bmi323_init(data);
+	if (ret)
+		return -EINVAL;
+
+	ret = iio_read_mount_matrix(dev, &data->orientation);
+	if (ret)
+		return ret;

drivers/iio/imu/bmi323/bmi323_core.c line 2235

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+
+	indio_dev->name = "bmi323-imu";
+	indio_dev->info = &bmi323_info;
+	indio_dev->channels = bmi323_channels;
+	indio_dev->num_channels = ARRAY_SIZE(bmi323_channels);
+	indio_dev->available_scan_masks = bmi323_avail_scan_masks;
+	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
+	dev_set_drvdata(data->dev, indio_dev);
+
+	ret = bmi323_trigger_probe(data, indio_dev);

What if interrupt isn't wired? Do we need it for basic read of channels? Would expect the interfaces provided to be more limited, but not that we would provide none at all.

Quoted 71 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +25 -0
+	if (ret)
+		return -EINVAL;
+
+	ret = devm_iio_triggered_buffer_setup_ext(data->dev, indio_dev,
+						  &iio_pollfunc_store_time,
+						  bmi323_trigger_handler,
+						  IIO_BUFFER_DIRECTION_IN,
+						  &bmi323_buffer_ops,
+						  bmi323_fifo_attributes);
+	if (ret)
+		return dev_err_probe(data->dev, ret,
+				     "Failed to setup trigger buffer\n");
+
+	ret = devm_iio_device_register(data->dev, indio_dev);
+	if (ret)
+		return dev_err_probe(data->dev, ret,
+				     "Unable to register iio device\n");
+
+	return ret;
+}
+EXPORT_SYMBOL_NS_GPL(bmi323_core_probe, IIO_BMI323);
+
+MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
+MODULE_AUTHOR("Jagath Jog J <[email protected]>");
+MODULE_LICENSE("GPL");
drivers/iio/imu/bmi323/bmi323_i2c.c new file +40 -0
new file mode 100644
index 000000000000..afbaa3d3c638
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * I2C driver for Bosch BMI323 6-Axis IMU.
+ *
+ * Copyright (C) 2023, Jagath Jog J <[email protected]>
+ */
+
+#include <linux/i2c.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+
+#include "bmi323.h"
+
+/*
+ * From BMI323 datasheet section 4: Notes on the Serial Interface Support.
+ * Each I2C register read operation requires to read two dummy bytes before
+ * the actual payload.
+ */
+static int bmi323_regmap_i2c_read(void *context, const void *reg_buf,
+				  size_t reg_size, void *val_buf,
+				  size_t val_size)
+{
+	struct device *dev = context;
+	struct i2c_client *i2c = to_i2c_client(dev);
+	struct i2c_msg msgs[2];
+	u8 *buff = NULL;
+	int ret;
+
+	buff = kmalloc(val_size + BMI323_I2C_DUMMY, GFP_KERNEL);
+	if (!buff)
+		return -ENOMEM;
+
+	msgs[0].addr = i2c->addr;
+	msgs[0].flags = i2c->flags;
+	msgs[0].len = reg_size;
+	msgs[0].buf = (u8 *)reg_buf;
+
+	msgs[1].addr = i2c->addr;
+	msgs[1].len = val_size + BMI323_I2C_DUMMY;

drivers/iio/imu/bmi323/bmi323_i2c.c line 50

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +10 -0
+	msgs[1].buf = (u8 *)buff;
+	msgs[1].flags = i2c->flags | I2C_M_RD;
+
+	ret = i2c_transfer(i2c->adapter, msgs, ARRAY_SIZE(msgs));
+	if (ret < 0) {
+		kfree(buff);
+		return -EIO;
+	}
+
+	memcpy(val_buf, buff + BMI323_I2C_DUMMY, val_size);

Annoyingly can't do same trick as I suggest for SPI as we need the address send vs when data turns up to be correct.

Whilst this code is 'generic' do you know the max size of the buffer that might be read? If it's small I'd just use an array on the stack. If large, then the cleanup.h stuff will help with code, but it's still annoying to have to do an allocation in here. You can probably put something in context alongside dev.

Quoted 7 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +7 -0
+	kfree(buff);
+
+	return 0;
+}
+
+static int bmi323_regmap_i2c_write(void *context, const void *data,
+				   size_t count)

drivers/iio/imu/bmi323/bmi323_i2c.c line 67

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +10 -0
+{
+	struct device *dev = context;
+	struct i2c_client *i2c = to_i2c_client(dev);
+	int ret;
+	u8 reg;
+
+	reg = *(u8 *)data;
+	ret = i2c_smbus_write_i2c_block_data(i2c, reg, count - 1,
+					     data + sizeof(u8));
+
	return i2c_smbus...
Quoted 72 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +48 -0
+	return ret;
+}
+
+static struct regmap_bus bmi323_regmap_bus = {
+	.read = bmi323_regmap_i2c_read,
+	.write = bmi323_regmap_i2c_write,
+};
+
+static int bmi323_i2c_probe(struct i2c_client *i2c)
+{
+	struct device *dev = &i2c->dev;
+	struct regmap *regmap;
+
+	regmap = devm_regmap_init(dev, &bmi323_regmap_bus, dev,
+				  &bmi323_regmap_config);
+	if (IS_ERR(regmap))
+		return dev_err_probe(dev, PTR_ERR(regmap),
+				     "Failed to initialize I2C Regmap\n");
+
+	return bmi323_core_probe(dev);
+}
+
+static const struct i2c_device_id bmi323_i2c_ids[] = {
+	{ "bmi323", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, bmi323_i2c_ids);
+
+static const struct of_device_id bmi323_of_i2c_match[] = {
+	{ .compatible = "bosch,bmi323" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, bmi323_of_i2c_match);
+
+static struct i2c_driver bmi323_i2c_driver = {
+	.driver = {
+		.name = "bmi323",
+		.of_match_table = bmi323_of_i2c_match,
+	},
+	.probe_new = bmi323_i2c_probe,
+	.id_table = bmi323_i2c_ids,
+};
+module_i2c_driver(bmi323_i2c_driver);
+
+MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
+MODULE_AUTHOR("Jagath Jog J <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS(IIO_BMI323);
drivers/iio/imu/bmi323/bmi323_spi.c new file +18 -0
new file mode 100644
index 000000000000..2b802a0c6d9d
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SPI driver for Bosch BMI323 6-Axis IMU.
+ *
+ * Copyright (C) 2023, Jagath Jog J <[email protected]>
+ */
+
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/spi/spi.h>
+
+#include "bmi323.h"
+
+/*
+ * From BMI323 datasheet section 4: Notes on the Serial Interface Support.
+ * Each SPI register read operation requires to read one dummy byte before
+ * the actual payload.

drivers/iio/imu/bmi323/bmi323_spi.c line 28

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +10 -0
+ */
+static int bmi323_regmap_spi_read(void *context, const void *reg_buf,
+				  size_t reg_size, void *val_buf,
+				  size_t val_size)
+{
+	struct spi_device *spi = context;
+	u8 reg, *buff = NULL;
+	int ret;
+
+	buff = kmalloc(val_size + BMI323_SPI_DUMMY, GFP_KERNEL);

Hmm. Regmap has pad_bits (which can be multiple bytes) but this case is unusual in that they only apply to reads.

I wonder if we can make this cheaper though rather than having to handle either some context or having dynamic allocations in here.

How about making the write bigger? Does that have any effect? Looks like don't care state in Figure 31. If that's the case, send some zeros on that as it's known fixed size (2 bytes including the padding) and then you can directly use the read buffer without yet another memcpy.

Quoted 29 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +29 -0
+	if (!buff)
+		return -ENOMEM;
+
+	reg = *(u8 *)reg_buf | 0x80;
+	ret = spi_write_then_read(spi, &reg, sizeof(reg), buff,
+				  val_size + BMI323_SPI_DUMMY);
+	if (ret) {
+		kfree(buff);
+		return ret;
+	}
+
+	memcpy(val_buf, buff + BMI323_SPI_DUMMY, val_size);
+	kfree(buff);
+
+	return ret;
+}
+
+static int bmi323_regmap_spi_write(void *context, const void *data,
+				   size_t count)
+{
+	struct spi_device *spi = context;
+
+	return spi_write(spi, data, count);
+}
+
+static struct regmap_bus bmi323_regmap_bus = {
+	.read = bmi323_regmap_spi_read,
+	.write = bmi323_regmap_spi_write,
+};
author maintainer
#23 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Hi Jonathan,

Thank you for the review.

Quoted 21 lines · Jonathan Cameron
Quoted 11 lines · On Mon, 18 Sep 2023 13:33:14 +0530 Jagath Jog J <[email protected]> wrote:

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

The driver supports various functionalities, including data ready, FIFO data handling, and events such as tap detection, step counting, and activity interrupts

Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf

Given Andy has done a thorough review (as he always does!) and there is quite a bit in my review queue, I'll just scan through quickly and call out a few things. Will take a closer look at next version.

Quoted 4 lines

One thing that is useful for a complex driver like this is to include (typically in the cover letter) a full listing of what shows up in sysfs. That gives an easy way to check the ABI looks right without having to figure out what all the generated file names are from the code.

Sure, I will add all ABI's in the cover letter.

Please note that I omitted certain portions of your reviews while responding, and I agree with the review comments that I didn't address. I intend to make the necessary corrections in the next series.

Quoted 35 lines

Thanks,

Jonathan

Quoted 14 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +14 -0
+struct bmi323_data {
+     struct device *dev;
+     struct regmap *regmap;
+     struct iio_mount_matrix orientation;
+     enum bmi323_irq_pin irq_pin;
+     struct iio_trigger *trig;
+     bool drdy_trigger_enabled;
+     enum bmi323_state state;
+     s64 fifo_tstamp, old_fifo_tstamp;
+     u32 odrns[2];
+     u32 odrhz[2];
+     unsigned int feature_events;
+
+     /*

drivers/iio/imu/bmi323/bmi323_core.c line 139

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+      * Lock to protect the members of device's private data from concurrent
+      * access and also to serialize the access of extended registers.
+      * See bmi323_write_ext_reg(..) for more info.
+      */
+     struct mutex mutex;
+     int watermark;
+     __le16 fifo_buff[BMI323_FIFO_FULL_IN_WORDS] __aligned(IIO_DMA_MINALIGN);
+     struct {
+             __le16 channels[6];
+             s64 ts __aligned(8);

Hopefully Andy's aligned_s64 set will land soon and we can tidy this up. I'm a bit unsure of this, but can you overlap some of these buffers or are they used concurrently? (if they are then we have problems with DMA safety.)

Quoted 1 line

Perhaps an anonymous union is appropriate?

Yes both buffers are used at the same time. In fifo_flush fifo_buff is used to store all fifo data, and buffer is used to push a single data frame to iio buffers, overlapping will corrupt the data, so I used separate buffers for both.

Quoted 9 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1408

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +8 -0
+static int bmi323_data_rdy_trigger_set_state(struct iio_trigger *trig,
+                                          bool state)
+{
+     struct bmi323_data *data = iio_trigger_get_drvdata(trig);
+     enum bmi323_irq_pin irq_pin;
+     int ret;
+
+     mutex_lock(&data->mutex);

guard(mutex)(&data->mutex);

Andy also suggested the same.
Sure I will use helpers from cleanup.h.

Quoted 11 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1543

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +6 -0
+static IIO_DEVICE_ATTR_RW(in_accel_gyro_averaging, 0);
+static IIO_CONST_ATTR(in_accel_gyro_averaging_available, "2 4 8 16 32 64");
+
+static struct attribute *bmi323_attributes[] = {
+     &iio_dev_attr_in_accel_gyro_averaging.dev_attr.attr,
+     &iio_const_attr_in_accel_gyro_averaging_available.dev_attr.attr,

So averaging often maps directly to oversampling. Kind of different names for the same thing. Perhaps that standard ABI can be used?
It tends to make sampling frequency reporting need to take it into account though as that drops as divided by oversampling ratio.

Yes, oversampling can be used, but changing the average value doesn't alter the sampling frequency. The sampling frequency is same even with the increase in averaging value.

Quoted 8 lines
Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +8 -0
+#define BMI323_SCAN_MASK_ACCEL_3AXIS         \
+     (BIT(BMI323_ACCEL_X) | BIT(BMI323_ACCEL_Y) | BIT(BMI323_ACCEL_Z))
+
+#define BMI323_SCAN_MASK_GYRO_3AXIS          \
+     (BIT(BMI323_GYRO_X) | BIT(BMI323_GYRO_Y) | BIT(BMI323_GYRO_Z))
+
+static const unsigned long bmi323_avail_scan_masks[] = {
+     /* 3-axis accel + 3-axis gyro */
Quoted 8 lines
Quoted 1 line

+ BMI323_SCAN_MASK_ACCEL_3AXIS | BMI323_SCAN_MASK_GYRO_3AXIS,

Can you poke this an see if you get what you expect which is the minimum sufficient set of channels. Matti pointed out earlier that the search logic isn't well documented in iio_scan_mask_match() but it looks to match against first case where the requested values are a subset. So this would need to be in the opposite order or you will always get everything turned on.

Sure, I will check and update you further on this.

Quoted 51 lines

Chances are we have this wrong in other drivers as well :( Won't break things, but may mean that we over read in some configurations.

Quoted 47 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +46 -0
+     /* 3-axis accel */
+     BMI323_SCAN_MASK_ACCEL_3AXIS,
+     /* 3-axis gyro */
+     BMI323_SCAN_MASK_GYRO_3AXIS,
+     0
+};
+
 
+     irq_type = irqd_get_trigger_type(desc);
+
+     switch (irq_type) {
+     case IRQF_TRIGGER_RISING:
+             latch = false;
+             active_high = true;
+             break;
+     case IRQF_TRIGGER_HIGH:
+             latch = true;
+             active_high = true;
+             break;
+     case IRQF_TRIGGER_FALLING:
+             latch = false;
+             active_high = false;
+             break;
+     case IRQF_TRIGGER_LOW:
+             latch = true;
+             active_high = false;
+             break;
+     default:
+             dev_err(data->dev, "Invalid interrupt type 0x%x specified\n",
+                     irq_type);
+             return -EINVAL;
+     }
+
+     open_drain = fwnode_property_read_bool(fwnode, "drive-open-drain");
+
+     ret = bmi323_int_pin_config(data, irq_pin, active_high, open_drain,
+                                 latch);
+     if (ret)
+             return dev_err_probe(data->dev, ret,
+                                  "Failed to configure irq line\n");
+
+     data->trig = devm_iio_trigger_alloc(data->dev, "%s-trig-%d",
+                                         indio_dev->name, irq_pin);
+     if (!data->trig)
+             return -ENOMEM;
+
+     data->trig->ops = &bmi323_trigger_ops;
Quoted 8 lines

drivers/iio/imu/bmi323/bmi323_core.c line 2038

Quoted 5 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +5 -0
+     iio_trigger_set_drvdata(data->trig, data);
+
+     ret = devm_request_threaded_irq(data->dev, irq, NULL,
+                                     bmi323_irq_thread_handler,
+                                     irq_type | IRQF_ONESHOT,

I think if you leave the irq_type bit out, it will be set up to match what was specified in firmware anyway. Could be wrong on that though so check.

Yes, irq_type is not required __setup_irq() is handling that. Thanks for pointing it out.

Quoted 17 lines
Quoted 17 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +17 -0
+
+static int bmi323_feature_engine_enable(struct bmi323_data *data, bool en)
+{
+     unsigned int feature_status;
+     int ret, i;
+
+     if (en) {
+             ret = regmap_write(data->regmap, BMI323_FEAT_IO2_REG,
+                                0x012c);
+             if (ret)
+                     return ret;
+
+             ret = regmap_write(data->regmap, BMI323_FEAT_IO_STATUS_REG,
+                                BMI323_FEAT_IO_STATUS_MSK);
+             if (ret)
+                     return ret;
+
Quoted 8 lines

drivers/iio/imu/bmi323/bmi323_core.c line 2074

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +6 -0
+             ret = regmap_write(data->regmap, BMI323_FEAT_CTRL_REG,
+                                BMI323_FEAT_ENG_EN_MSK);
+             if (ret)
+                     return ret;
+
+             i = 5;

Why 5?

No specific reason, during testing the feature engine was taking around 4 milliseconds, so I thought of checking every 2 milliseconds and max of 5 trials.

Quoted 11 lines
Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +9 -0
+             do {
+                     ret = regmap_read(data->regmap, BMI323_FEAT_IO1_REG,
+                                       &feature_status);
+                     if (ret)
+                             return ret;
+
+                     i--;
+                     mdelay(2);
+             } while (feature_status != 0x01 && i);
Quoted 14 lines

drivers/iio/imu/bmi323/bmi323_core.c line 2235

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+
+     indio_dev->name = "bmi323-imu";
+     indio_dev->info = &bmi323_info;
+     indio_dev->channels = bmi323_channels;
+     indio_dev->num_channels = ARRAY_SIZE(bmi323_channels);
+     indio_dev->available_scan_masks = bmi323_avail_scan_masks;
+     indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
+     dev_set_drvdata(data->dev, indio_dev);
+
+     ret = bmi323_trigger_probe(data, indio_dev);

What if interrupt isn't wired? Do we need it for basic read of channels? Would expect the interfaces provided to be more limited, but not that we would provide none at all.

Yes, the basic read of channels will be available even if none of the interrupts are wired and trigger buffer through sysfs or hrt timer is also available.

Quoted 53 lines
Quoted 39 lines
(quoted diff) +37 -0
+     if (ret)
+             return -EINVAL;
+
+     ret = devm_iio_triggered_buffer_setup_ext(data->dev, indio_dev,
+                                               &iio_pollfunc_store_time,
+                                               bmi323_trigger_handler,
+                                               IIO_BUFFER_DIRECTION_IN,
+                                               &bmi323_buffer_ops,
+                                               bmi323_fifo_attributes);
+     if (ret)
+             return dev_err_probe(data->dev, ret,
+                                  "Failed to setup trigger buffer\n");
 
+ * From BMI323 datasheet section 4: Notes on the Serial Interface Support.
+ * Each I2C register read operation requires to read two dummy bytes before
+ * the actual payload.
+ */
+static int bmi323_regmap_i2c_read(void *context, const void *reg_buf,
+                               size_t reg_size, void *val_buf,
+                               size_t val_size)
+{
+     struct device *dev = context;
+     struct i2c_client *i2c = to_i2c_client(dev);
+     struct i2c_msg msgs[2];
+     u8 *buff = NULL;
+     int ret;
+
+     buff = kmalloc(val_size + BMI323_I2C_DUMMY, GFP_KERNEL);
+     if (!buff)
+             return -ENOMEM;
+
+     msgs[0].addr = i2c->addr;
+     msgs[0].flags = i2c->flags;
+     msgs[0].len = reg_size;
+     msgs[0].buf = (u8 *)reg_buf;
+
+     msgs[1].addr = i2c->addr;
+     msgs[1].len = val_size + BMI323_I2C_DUMMY;

Review

Quoted 10 lines
(quoted diff) +10 -0
+     msgs[1].buf = (u8 *)buff;
+     msgs[1].flags = i2c->flags | I2C_M_RD;
+
+     ret = i2c_transfer(i2c->adapter, msgs, ARRAY_SIZE(msgs));
+     if (ret < 0) {
+             kfree(buff);
+             return -EIO;
+     }
+
+     memcpy(val_buf, buff + BMI323_I2C_DUMMY, val_size);

Annoyingly can't do same trick as I suggest for SPI as we need the address send vs when data turns up to be correct.

Quoted 6 lines

Whilst this code is 'generic' do you know the max size of the buffer that might be read? If it's small I'd just use an array on the stack. If large, then the cleanup.h stuff will help with code, but it's still annoying to have to do an allocation in here. You can probably put something in context alongside dev.

A buffer size of 2028 bytes is required when configuring the FIFO watermark for maximum capacity. Since the necessary buffer size is substantial, I am allocating it dynamically.

I will try to use an additional buffer from the device's private structure and pass it through the context. This approach will help reduce the memory allocation and deallocation on every device access.

Quoted 17 lines

drivers/iio/imu/bmi323/bmi323_spi.c line 28

Quoted 13 lines

+ * From BMI323 datasheet section 4: Notes on the Serial Interface Support.
+ * Each SPI register read operation requires to read one dummy byte before
+ * the actual payload.
+ */

drivers/iio/imu/bmi323/bmi323_spi.c new file +9 -0
+static int bmi323_regmap_spi_read(void *context, const void *reg_buf,
+                               size_t reg_size, void *val_buf,
+                               size_t val_size)
+{
+     struct spi_device *spi = context;
+     u8 reg, *buff = NULL;
+     int ret;
+
+     buff = kmalloc(val_size + BMI323_SPI_DUMMY, GFP_KERNEL);

Hmm. Regmap has pad_bits (which can be multiple bytes) but this case is unusual in that they only apply to reads.

Quoted 8 lines

I wonder if we can make this cheaper though rather than having to handle either some context or having dynamic allocations in here.

How about making the write bigger? Does that have any effect? Looks like don't care state in Figure 31. If that's the case, send some zeros on that as it's known fixed size (2 bytes including the padding) and then you can directly use the read buffer without yet another memcpy.

For spi with pad_bits=8 and without any custom read and write functions, regmap_read() works but regmap_write() does not. Write is also adding 8 bits of padding and the device is treating it as data.
(7.2.3 SPI Protocol Figure 30)

Quoted 17 lines
Quoted 7 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +5 -0
+     if (!buff)
+             return -ENOMEM;
+
+     reg = *(u8 *)reg_buf | 0x80;
+     ret = spi_write_then_read(spi, &reg, sizeof(reg), buff,

drivers/iio/imu/bmi323/bmi323_spi.c line 43

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +10 -0
+                               val_size + BMI323_SPI_DUMMY);
+     if (ret) {
+             kfree(buff);
+             return ret;
+     }
+
+     memcpy(val_buf, buff + BMI323_SPI_DUMMY, val_size);
+     kfree(buff);
+
+     return ret;

Regards
Jagath

#24 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Hello,

Some devices (as my asus rog ally) have an ACPI node describing a BOSC0200 sensor. The IC being used in those devices is a bmi323 but as a result of how the ACPI table reports that device, it is detected by the existing kernel module and we have no way of differentiating until after the chip ID probe.

The module loaded is bmc150-accel-i2c.c which currently doesn't support the bmi323 and the loading of the module just fails at chip check.

I have solved the problem by expanding the current bmc150-accel-i2c.c and bmc150-accel-core.c files to handle that IC in almost every part: gyroscope, accelerometer and temperature sensor.

What is the best way of organizing code to have this module mainlined? Is it correct leaving files called bmc150-accel-* even if it is managing another IC and and not just the accelerometer part anymore?

TIA for your time.

Best regards,
Denis Benato

author maintainer
#25 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Hi Denis,

Quoted 6 lines · Denis Benato

Hello,

Some devices (as my asus rog ally) have an ACPI node describing a BOSC0200 sensor. The IC being used in those devices is a bmi323 but as a result of how the ACPI table reports that device, it is detected by the existing kernel module and we have no way of differentiating until after the chip ID probe.

The module loaded is bmc150-accel-i2c.c which currently doesn't support the bmi323 and the loading of the module just fails at chip check.

bmc150 driver supports multiple accelerometer sensors such as bma222, bma280, bmi055 and all of them are having similar register map, but the bmi323 register map is completely different from bmc150.

Quoted 9 lines

I have solved the problem by expanding the current bmc150-accel-i2c.c and bmc150-accel-core.c files to handle that IC in almost every part: gyroscope, accelerometer and temperature sensor.

What is the best way of organizing code to have this module mainlined? Is it correct leaving files called bmc150-accel-* even if it is managing another IC and and not just the accelerometer part anymore?

TIA for your time.

Best regards,
Denis Benato

Regards

Jagath

maintainer
#26 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Quoted 6 lines · On Fri, 29 Sep 2023 13:29:13 +0530 Jagath Jog J <[email protected]> wrote:

Hi Denis,

Quoted 3 lines · Denis Benato

Hello,

Quoted 8 lines
Quoted 3 lines

Some devices (as my asus rog ally) have an ACPI node describing a BOSC0200 sensor. The IC being used in those devices is a bmi323 but as a result of how the ACPI table reports that device, it is detected by the existing kernel module and we have no way of differentiating until after the chip ID probe.

The module loaded is bmc150-accel-i2c.c which currently doesn't support the bmi323 and the loading of the module just fails at chip check.

bmc150 driver supports multiple accelerometer sensors such as bma222, bma280, bmi055 and all of them are having similar register map, but the bmi323 register map is completely different from bmc150.

Horrible bios hacks that depend on a particular driver stack are always a pain.

Hmm. Andy (handy ACPI expert), any suggestion?

We could maybe do a wrapper driver that does appropriate checks and wraps the probe + remove from the two drivers? Whilst we can obviously have a single driver that deals with radically different devices I'm not particularly keen on that as it tends to make things less maintainable.

Jonathan

Quoted 15 lines
Quoted 11 lines

I have solved the problem by expanding the current bmc150-accel-i2c.c and bmc150-accel-core.c files to handle that IC in almost every part: gyroscope, accelerometer and temperature sensor.

What is the best way of organizing code to have this module mainlined? Is it correct leaving files called bmc150-accel-* even if it is managing another IC and and not just the accelerometer part anymore?

TIA for your time.

Best regards,
Denis Benato

Regards

Jagath

#27 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

In reply to Jagath Jog J

Hello Jagath,

Quoted 6 lines · Jagath Jog J

Hi Denis,

Quoted 3 lines · Denis Benato

Hello,

Quoted 8 lines
Quoted 3 lines

Some devices (as my asus rog ally) have an ACPI node describing a BOSC0200 sensor. The IC being used in those devices is a bmi323 but as a result of how the ACPI table reports that device, it is detected by the existing kernel module and we have no way of differentiating until after the chip ID probe.

The module loaded is bmc150-accel-i2c.c which currently doesn't support the bmi323 and the loading of the module just fails at chip check.

bmc150 driver supports multiple accelerometer sensors such as bma222, bma280, bmi055 and all of them are having similar register map, but the bmi323 register map is completely different from bmc150.

I apologize for the confusion.

What I was trying to say is that to have the bmi323 working in those aforementioned devices bmc150 will need to be modified: that is the probe function that ends up being executed, fact that cannot be changed because it depends on the ACPI implementation shipped on those devices.

Therefore I was asking about the best way of handing control to the new driver and how that should be organized: in my implementation the new bmi323 code was written inside the bmc150-accel-core.c and only shares sleep/suspend, probe and removal functions in addition to checking for the new chip presence before checking for any bmc150 chip as that issues an i2c write, while the check for the bmi323 only requires an i2c read.

We also have done duplicate work as I have written a driver for that chip myself, but it's not as good as yours because my hardware didn't come with the IRQ pin connected and so I couldn't develop triggers and I only got the i2c interface working.

Quoted 7 lines
Quoted 7 lines

I have solved the problem by expanding the current bmc150-accel-i2c.c and bmc150-accel-core.c files to handle that IC in almost every part: gyroscope, accelerometer and temperature sensor.

What is the best way of organizing code to have this module mainlined? Is it correct leaving files called bmc150-accel-* even if it is managing another IC and and not just the accelerometer part anymore?

Quoted 8 lines
Quoted 4 lines

TIA for your time.

Best regards,
Denis Benato

Regards

Jagath

TIA for your time.

Best regards,
Denis Benato

author maintainer
#28 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Hi Denis, Jonathan

Quoted 19 lines · Denis Benato

Hello Jagath,

Quoted 6 lines · Jagath Jog J

Hi Denis,

Quoted 3 lines · Denis Benato

Hello,

Quoted 8 lines
Quoted 3 lines

Some devices (as my asus rog ally) have an ACPI node describing a BOSC0200 sensor. The IC being used in those devices is a bmi323 but as a result of how the ACPI table reports that device, it is detected by the existing kernel module and we have no way of differentiating until after the chip ID probe.

The module loaded is bmc150-accel-i2c.c which currently doesn't support the bmi323 and the loading of the module just fails at chip check.

bmc150 driver supports multiple accelerometer sensors such as bma222, bma280, bmi055 and all of them are having similar register map, but the bmi323 register map is completely different from bmc150.

Quoted 5 lines

I apologize for the confusion.

What I was trying to say is that to have the bmi323 working in those aforementioned devices bmc150 will need to be modified: that is the probe function that ends up being executed, fact that cannot be changed because it depends on the ACPI implementation shipped on those devices.

Therefore I was asking about the best way of handing control to the new driver and how that should be organized: in my implementation the new bmi323 code was written inside the bmc150-accel-core.c and only shares sleep/suspend, probe and removal functions in addition to checking for the new chip presence before checking for any bmc150 chip as that issues an i2c write, while the check for the bmi323 only requires an i2c read.

Means you want to handle control to the standalone driver from bmc150. Sorry, I didn't find any examples.

Important thing to handle is the bmi323 private structure and call required exported functions from another driver.

Jonathan: Can you suggest any example wrapper drivers which handle that way?

Quoted 15 lines

We also have done duplicate work as I have written a driver for that chip myself, but it's not as good as yours because my hardware didn't come with the IRQ pin connected and so I couldn't develop triggers and I only got the i2c interface working.

Quoted 12 lines
Quoted 11 lines

I have solved the problem by expanding the current bmc150-accel-i2c.c and bmc150-accel-core.c files to handle that IC in almost every part: gyroscope, accelerometer and temperature sensor.

What is the best way of organizing code to have this module mainlined? Is it correct leaving files called bmc150-accel-* even if it is managing another IC and and not just the accelerometer part anymore?

TIA for your time.

Best regards,
Denis Benato

Quoted 8 lines
Quoted 3 lines

Regards

Jagath

TIA for your time.

Best regards,
Denis Benato

Regards
Jagath

maintainer
#29 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

In reply to Jagath Jog J
Quoted 31 lines
Quoted 31 lines
Quoted 16 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +14 -0
+struct bmi323_data {
+     struct device *dev;
+     struct regmap *regmap;
+     struct iio_mount_matrix orientation;
+     enum bmi323_irq_pin irq_pin;
+     struct iio_trigger *trig;
+     bool drdy_trigger_enabled;
+     enum bmi323_state state;
+     s64 fifo_tstamp, old_fifo_tstamp;
+     u32 odrns[2];
+     u32 odrhz[2];
+     unsigned int feature_events;
+
+     /*

drivers/iio/imu/bmi323/bmi323_core.c line 139

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +10 -0
+      * Lock to protect the members of device's private data from concurrent
+      * access and also to serialize the access of extended registers.
+      * See bmi323_write_ext_reg(..) for more info.
+      */
+     struct mutex mutex;
+     int watermark;
+     __le16 fifo_buff[BMI323_FIFO_FULL_IN_WORDS] __aligned(IIO_DMA_MINALIGN);
+     struct {
+             __le16 channels[6];
+             s64 ts __aligned(8);  

Hopefully Andy's aligned_s64 set will land soon and we can tidy this up. I'm a bit unsure of this, but can you overlap some of these buffers or are they used concurrently? (if they are then we have problems with DMA safety.)

Quoted 6 lines
Quoted 1 line

Perhaps an anonymous union is appropriate?

Yes both buffers are used at the same time. In fifo_flush fifo_buff is used to store all fifo data, and buffer is used to push a single data frame to iio buffers, overlapping will corrupt the data, so I used separate buffers for both.

Ah. So the structure is used in 2 ways.

1. As a target for DMA, which means it should live in the cacheline we are saving for that purpsoe.
2. As a place to build up data.

In general we should be careful with doing 2 as that could race with DMA and end up with data corruption, however you only use it that way in flush_fifo where both the DMA and this usage under under the mutex. Hence I think you are fine.

Quoted 13 lines
Quoted 12 lines

drivers/iio/imu/bmi323/bmi323_core.c line 1543

Quoted 7 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +6 -0
+static IIO_DEVICE_ATTR_RW(in_accel_gyro_averaging, 0);
+static IIO_CONST_ATTR(in_accel_gyro_averaging_available, "2 4 8 16 32 64");
+
+static struct attribute *bmi323_attributes[] = {
+     &iio_dev_attr_in_accel_gyro_averaging.dev_attr.attr,
+     &iio_const_attr_in_accel_gyro_averaging_available.dev_attr.attr,  

So averaging often maps directly to oversampling. Kind of different names for the same thing. Perhaps that standard ABI can be used?
It tends to make sampling frequency reporting need to take it into account though as that drops as divided by oversampling ratio.

Quoted 3 lines

Yes, oversampling can be used, but changing the average value doesn't alter the sampling frequency. The sampling frequency is same even with the increase in averaging value.

Ok. That's unusual so good to know.

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +16 -0
+static int bmi323_feature_engine_enable(struct bmi323_data *data, bool en)
+{
+     unsigned int feature_status;
+     int ret, i;
+
+     if (en) {
+             ret = regmap_write(data->regmap, BMI323_FEAT_IO2_REG,
+                                0x012c);
+             if (ret)
+                     return ret;
+
+             ret = regmap_write(data->regmap, BMI323_FEAT_IO_STATUS_REG,
+                                BMI323_FEAT_IO_STATUS_MSK);
+             if (ret)
+                     return ret;
+
Quoted 8 lines

drivers/iio/imu/bmi323/bmi323_core.c line 2074

Quoted 6 lines
drivers/iio/imu/bmi323/bmi323_core.c new file +6 -0
+             ret = regmap_write(data->regmap, BMI323_FEAT_CTRL_REG,
+                                BMI323_FEAT_ENG_EN_MSK);
+             if (ret)
+                     return ret;
+
+             i = 5;  

Why 5?

Quoted 3 lines

No specific reason, during testing the feature engine was taking around 4 milliseconds, so I thought of checking every 2 milliseconds and max of 5 trials.

That's a good reason. Just add a comment to that say that.

Quoted 27 lines
Quoted 18 lines
Quoted 5 lines

+ * From BMI323 datasheet section 4: Notes on the Serial Interface Support.
+ * Each SPI register read operation requires to read one dummy byte before
+ * the actual payload.
+ */

drivers/iio/imu/bmi323/bmi323_spi.c line 28

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_spi.c new file +9 -0
+static int bmi323_regmap_spi_read(void *context, const void *reg_buf,
+                               size_t reg_size, void *val_buf,
+                               size_t val_size)
+{
+     struct spi_device *spi = context;
+     u8 reg, *buff = NULL;
+     int ret;
+
+     buff = kmalloc(val_size + BMI323_SPI_DUMMY, GFP_KERNEL);  

Hmm. Regmap has pad_bits (which can be multiple bytes) but this case is unusual in that they only apply to reads.

Quoted 8 lines

I wonder if we can make this cheaper though rather than having to handle either some context or having dynamic allocations in here.

How about making the write bigger? Does that have any effect? Looks like don't care state in Figure 31. If that's the case, send some zeros on that as it's known fixed size (2 bytes including the padding) and then you can directly use the read buffer without yet another memcpy.

Quoted 5 lines

For spi with pad_bits=8 and without any custom read and write functions, regmap_read() works but regmap_write() does not. Write is also adding 8 bits of padding and the device is treating it as data.
(7.2.3 SPI Protocol Figure 30)

Understood. I looked it up too before suggesting this local hack. You'll still need a custom regmap, but at least this trick would allow you to avoid allocating a buffer in the read function.

Jonathan

#30 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

In reply to Jagath Jog J

Hello,

Quoted 10 lines · Jagath Jog J

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

The driver supports various functionalities, including data ready, FIFO data handling, and events such as tap detection, step counting, and activity interrupts

Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf

Maybe put this link better in the driver.

drivers/iio/imu/bmi323/bmi323_i2c.c line 110

Quoted 9 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +9 -0
+static struct i2c_driver bmi323_i2c_driver = {
+	.driver = {
+		.name = "bmi323",
+		.of_match_table = bmi323_of_i2c_match,
+	},
+	.probe_new = bmi323_i2c_probe,
+	.id_table = bmi323_i2c_ids,
+};
+module_i2c_driver(bmi323_i2c_driver);

If you want to compile this driver after v6.6-rc2 (which includes commit 5eb1e6e459cf ("i2c: Drop legacy callback .probe_new()")) better use .probe here instead of .probe_new().

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#31 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Quoted 1 line · Uwe Kleine-König

On Mon, Sep 18, 2023 at 01:33:14PM +0530, Jagath Jog J wrote:

...

Quoted 3 lines
Quoted 1 line

Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf

Maybe put this link better in the driver.

Why? We have a handful commits with this and it's better to see the link to the datasheet without browsing the source code.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#32 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Quoted 11 lines · Andy Shevchenko
Quoted 1 line · Uwe Kleine-König

On Mon, Sep 18, 2023 at 01:33:14PM +0530, Jagath Jog J wrote:

...

Quoted 3 lines
Quoted 1 line

Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf

Maybe put this link better in the driver.

Why? We have a handful commits with this and it's better to see the link to the datasheet without browsing the source code.

But if you later work on a problem in the driver, it's better to see the link without browsing git history. :-)

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#33 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Quoted 3 lines · Uwe Kleine-König
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Uwe Kleine-König

On Mon, Sep 18, 2023 at 01:33:14PM +0530, Jagath Jog J wrote:

...

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf

Maybe put this link better in the driver.

Why? We have a handful commits with this and it's better to see the link to the datasheet without browsing the source code.

But if you later work on a problem in the driver, it's better to see the link without browsing git history. :-)

Both make sense.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#34 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

In reply to Uwe Kleine-König

Hi Uwe Kleine-König,

Quoted 9 lines · Uwe Kleine-König

Hello,

Quoted 5 lines · Jagath Jog J

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

Quoted 7 lines
Quoted 5 lines

The driver supports various functionalities, including data ready, FIFO data handling, and events such as tap detection, step counting, and activity interrupts

Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf

Maybe put this link better in the driver.

Yes, if there are multiple commits on the driver, the datasheet link will move further down with the initial commit. I will add datasheet link in the driver.

Quoted 14 lines

drivers/iio/imu/bmi323/bmi323_i2c.c line 110

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +9 -0
+static struct i2c_driver bmi323_i2c_driver = {
+     .driver = {
+             .name = "bmi323",
+             .of_match_table = bmi323_of_i2c_match,
+     },
+     .probe_new = bmi323_i2c_probe,
+     .id_table = bmi323_i2c_ids,
+};
+module_i2c_driver(bmi323_i2c_driver);

If you want to compile this driver after v6.6-rc2 (which includes commit 5eb1e6e459cf ("i2c: Drop legacy callback .probe_new()")) better use .probe here instead of .probe_new().

Thanks for pointing it out.
I switched to v6.6-rc3 and I will change to .probe.

Regards
Jagath

Jagath

Quoted 7 lines

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | https://www.pengutronix.de/ |

#35 lore raw

Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU

Quoted 40 lines · Jagath Jog J

Hi Uwe Kleine-König,

Quoted 9 lines · Uwe Kleine-König

Hello,

Quoted 5 lines · Jagath Jog J

The Bosch BMI323 is a 6-axis low-power IMU that provide measurements for acceleration, angular rate, and temperature. This sensor includes motion-triggered interrupt features, such as a step counter, tap detection, and activity/inactivity interrupt capabilities.

Quoted 7 lines
Quoted 5 lines

The driver supports various functionalities, including data ready, FIFO data handling, and events such as tap detection, step counting, and activity interrupts

Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf

Maybe put this link better in the driver.

Yes, if there are multiple commits on the driver, the datasheet link will move further down with the initial commit. I will add datasheet link in the driver.

Quoted 14 lines

drivers/iio/imu/bmi323/bmi323_i2c.c line 110

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_i2c.c new file +9 -0
+static struct i2c_driver bmi323_i2c_driver = {
+     .driver = {
+             .name = "bmi323",
+             .of_match_table = bmi323_of_i2c_match,
+     },
+     .probe_new = bmi323_i2c_probe,
+     .id_table = bmi323_i2c_ids,
+};
+module_i2c_driver(bmi323_i2c_driver);

If you want to compile this driver after v6.6-rc2 (which includes commit 5eb1e6e459cf ("i2c: Drop legacy callback .probe_new()")) better use .probe here instead of .probe_new().

Quoted 2 lines

Thanks for pointing it out.
I switched to v6.6-rc3 and I will change to .probe.

Note that you can use .probe (with that prototype) already since v6.3-rc2. So there is no hard requirement to go to v6.6-rc3.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |