Skip to contentSkip to conversation

Add I2C driver for Bosch BMI270 IMU

PATCH Superseded Alex Lanzano 3 participants 8 messages Sep 6, 2024 – Sep 8, 2024

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Alex Lanzano

author maintainer
#0 lore raw

v2 cover Add I2C driver for Bosch BMI270 IMU

Add basic I2C support for the Bosch BMI270 IMU.

References:
https://www.bosch-sensortec.com/products/motion-sensors/imus/bmi270/

---
Changes in v2:
- Remove spi example in binding documentation
- Add more properties to i2c example in binding documentation
---

Alex Lanzano (2):
dt-bindings: iio: imu: add bmi270 bindings
iio: imu: Add i2c driver for bmi270 imu

 .../bindings/iio/imu/bosch,bmi270.yaml        |  77 +++++
 MAINTAINERS                                   |   7 +
 drivers/iio/imu/Kconfig                       |   1 +
 drivers/iio/imu/Makefile                      |   1 +
 drivers/iio/imu/bmi270/Kconfig                |  22 ++
 drivers/iio/imu/bmi270/Makefile               |   6 +
 drivers/iio/imu/bmi270/bmi270.h               |  18 +
 drivers/iio/imu/bmi270/bmi270_core.c          | 322 ++++++++++++++++++
 drivers/iio/imu/bmi270/bmi270_i2c.c           |  56 +++
 9 files changed, 510 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml
 create mode 100644 drivers/iio/imu/bmi270/Kconfig
 create mode 100644 drivers/iio/imu/bmi270/Makefile
 create mode 100644 drivers/iio/imu/bmi270/bmi270.h
 create mode 100644 drivers/iio/imu/bmi270/bmi270_core.c
 create mode 100644 drivers/iio/imu/bmi270/bmi270_i2c.c
Signature · 1 line
2.46.0
author maintainer
#1 lore raw

PATCH v2 1/2 dt-bindings: iio: imu: add bmi270 bindings

Add device tree bindings for the bmi270 IMU

---
 .../bindings/iio/imu/bosch,bmi270.yaml        | 77 +++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml
Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml new file +77 -0
new file mode 100644
index 000000000000..7de35b9bfa2e
@@ -0,0 +1,77 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/bosch,bmi270.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMI270 6-Axis IMU
+
+maintainers:
+  - Alex Lanzano <[email protected]>
+
+description: |
+  BMI270 is a 6-axis inertial measurement unit that can measure acceleration and
+  angular velocity. The sensor also supports configurable interrupt events such
+  as motion, step counter, and wrist motion gestures. The sensor can communicate
+  I2C or SPI.
+  https://www.bosch-sensortec.com/products/motion-sensors/imus/bmi270/
+
+properties:
+  compatible:
+    const: bosch,bmi270
+
+  reg:
+    maxItems: 1
+
+  vdd-supply: true
+  vddio-supply: true
+
+  interrupts:
+    minItems: 1
+    maxItems: 2
+
+  interrupt-names:
+    minItems: 1
+    maxItems: 2
+    items:
+      enum:
+        - INT1
+        - INT2
+
+  drive-open-drain:
+    description:
+      set if the specified interrupt pin should be configured as
+      open drain. If not set, defaults to push-pull.
+
+  mount-matrix:
+    description:
+      an optional 3x3 mounting rotation matrix.
+
+required:
+  - compatible
+  - reg
+  - vdd-supply
+  - vddio-supply
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        imu@68 {
+            compatible = "bosch,bmi270";
+            reg = <0x68>;
+            vdd-supply = <&vdd>;
+            vddio-supply = <&vddio>;
+            interrupt-parent = <&gpio1>;
+            interrupts = <16 IRQ_TYPE_EDGE_RISING>;
+            interrupt-names = "INT1";
+        };
+    };
Signature · 1 line
2.46.0
maintainer
#3 lore raw

Re: [PATCH v2 1/2] dt-bindings: iio: imu: add bmi270 bindings

In reply to Alex Lanzano
Quoted 3 lines · On Fri, 6 Sep 2024 12:52:50 -0400 Alex Lanzano <[email protected]> wrote:

Add device tree bindings for the bmi270 IMU

Hi Alex

One totally trivial thing inline. If nothing comes up in driver I'll tidy this up whilst applying

Jonathan

Quoted 44 lines
---
 .../bindings/iio/imu/bosch,bmi270.yaml        | 77 +++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml
Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml new file +33 -0
new file mode 100644
index 000000000000..7de35b9bfa2e
@@ -0,0 +1,77 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/bosch,bmi270.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMI270 6-Axis IMU
+
+maintainers:
+  - Alex Lanzano <[email protected]>
+
+description: |
+  BMI270 is a 6-axis inertial measurement unit that can measure acceleration and
+  angular velocity. The sensor also supports configurable interrupt events such
+  as motion, step counter, and wrist motion gestures. The sensor can communicate
+  I2C or SPI.
+  https://www.bosch-sensortec.com/products/motion-sensors/imus/bmi270/
+
+properties:
+  compatible:
+    const: bosch,bmi270
+
+  reg:
+    maxItems: 1
+
+  vdd-supply: true
+  vddio-supply: true
+
+  interrupts:
+    minItems: 1
+    maxItems: 2
+
+  interrupt-names:

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

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml new file +10 -0
+    minItems: 1
+    maxItems: 2
+    items:
+      enum:
+        - INT1
+        - INT2
+
+  drive-open-drain:
+    description:
+      set if the specified interrupt pin should be configured as

pins

Even if they are configurable separately we've previously argued that it is very unlikely a board designer would want one open drain and the other push-pull. So simply making the description plural should be enough.

Quoted 34 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml new file +34 -0
+      open drain. If not set, defaults to push-pull.
+
+  mount-matrix:
+    description:
+      an optional 3x3 mounting rotation matrix.
+
+required:
+  - compatible
+  - reg
+  - vdd-supply
+  - vddio-supply
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        imu@68 {
+            compatible = "bosch,bmi270";
+            reg = <0x68>;
+            vdd-supply = <&vdd>;
+            vddio-supply = <&vddio>;
+            interrupt-parent = <&gpio1>;
+            interrupts = <16 IRQ_TYPE_EDGE_RISING>;
+            interrupt-names = "INT1";
+        };
+    };
author maintainer
#4 lore raw

Re: [PATCH v2 1/2] dt-bindings: iio: imu: add bmi270 bindings

Quoted 5 lines · Jonathan Cameron
Quoted 2 lines · On Fri, 6 Sep 2024 12:52:50 -0400 Alex Lanzano <[email protected]> wrote:

Add device tree bindings for the bmi270 IMU

Quoted 8 lines
Quoted 1 line

Hi Alex

One totally trivial thing inline. If nothing comes up in driver I'll tidy this up whilst applying

Jonathan

Will apply in v3!

Quoted 57 lines
Quoted 44 lines
---
 .../bindings/iio/imu/bosch,bmi270.yaml        | 77 +++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml
Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml new file +33 -0
new file mode 100644
index 000000000000..7de35b9bfa2e
@@ -0,0 +1,77 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/bosch,bmi270.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMI270 6-Axis IMU
+
+maintainers:
+  - Alex Lanzano <[email protected]>
+
+description: |
+  BMI270 is a 6-axis inertial measurement unit that can measure acceleration and
+  angular velocity. The sensor also supports configurable interrupt events such
+  as motion, step counter, and wrist motion gestures. The sensor can communicate
+  I2C or SPI.
+  https://www.bosch-sensortec.com/products/motion-sensors/imus/bmi270/
+
+properties:
+  compatible:
+    const: bosch,bmi270
+
+  reg:
+    maxItems: 1
+
+  vdd-supply: true
+  vddio-supply: true
+
+  interrupts:
+    minItems: 1
+    maxItems: 2
+
+  interrupt-names:

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

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml new file +10 -0
+    minItems: 1
+    maxItems: 2
+    items:
+      enum:
+        - INT1
+        - INT2
+
+  drive-open-drain:
+    description:
+      set if the specified interrupt pin should be configured as

pins

Quoted 5 lines

Even if they are configurable separately we've previously argued that it is very unlikely a board designer would want one open drain and the other push-pull. So simply making the description plural should be enough.

Will Fix in v3!

Quoted 26 lines
Quoted 26 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml new file +26 -0
+      open drain. If not set, defaults to push-pull.
+
+  mount-matrix:
+    description:
+      an optional 3x3 mounting rotation matrix.
+
+required:
+  - compatible
+  - reg
+  - vdd-supply
+  - vddio-supply
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        imu@68 {
+            compatible = "bosch,bmi270";
Quoted 8 lines

Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml line 77

Quoted 8 lines
Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml new file +8 -0
+            reg = <0x68>;
+            vdd-supply = <&vdd>;
+            vddio-supply = <&vddio>;
+            interrupt-parent = <&gpio1>;
+            interrupts = <16 IRQ_TYPE_EDGE_RISING>;
+            interrupt-names = "INT1";
+        };
+    };

Thanks so much for the review!

Best regards,
Alex

author maintainer
#5 lore raw

PATCH v2 2/2 iio: imu: Add i2c driver for bmi270 imu

In reply to Alex Lanzano

Add initial i2c support for the Bosch BMI270 6-axis IMU. Provides raw read access to acceleration and angle velocity measurements via iio channels. Device configuration requires firmware provided by Bosch and is requested and load from userspace.

---
 MAINTAINERS                          |   7 +
 drivers/iio/imu/Kconfig              |   1 +
 drivers/iio/imu/Makefile             |   1 +
 drivers/iio/imu/bmi270/Kconfig       |  22 ++
 drivers/iio/imu/bmi270/Makefile      |   6 +
 drivers/iio/imu/bmi270/bmi270.h      |  18 ++
 drivers/iio/imu/bmi270/bmi270_core.c | 322 +++++++++++++++++++++++++++
 drivers/iio/imu/bmi270/bmi270_i2c.c  |  56 +++++
 8 files changed, 433 insertions(+)
 create mode 100644 drivers/iio/imu/bmi270/Kconfig
 create mode 100644 drivers/iio/imu/bmi270/Makefile
 create mode 100644 drivers/iio/imu/bmi270/bmi270.h
 create mode 100644 drivers/iio/imu/bmi270/bmi270_core.c
 create mode 100644 drivers/iio/imu/bmi270/bmi270_i2c.c
MAINTAINERS +7 -0
index a2184637a5d9..6612d27525b5 100644
@@ -3928,6 +3928,13 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml
 F:	drivers/iio/accel/bma400*
 
+BOSCH SENSORTEC BMI270 IMU IIO DRIVER
+M:	Alex Lanzano <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/imu/bosch,bmi270.yaml
+F:	drivers/iio/imu/bmi270/
+
 BOSCH SENSORTEC BMI323 IMU IIO DRIVER
 M:	Jagath Jog J <[email protected]>
 L:	[email protected]
drivers/iio/imu/Kconfig +1 -0
index 782fb80e44c2..489dd898830b 100644
@@ -53,6 +53,7 @@ config ADIS16480
 	  ADIS16485, ADIS16488 inertial sensors.
 
 source "drivers/iio/imu/bmi160/Kconfig"
+source "drivers/iio/imu/bmi270/Kconfig"
 source "drivers/iio/imu/bmi323/Kconfig"
 source "drivers/iio/imu/bno055/Kconfig"
 
drivers/iio/imu/Makefile +1 -0
index 7e2d7d5c3b7b..79f83ea6f644 100644
@@ -15,6 +15,7 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o
 obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
 
 obj-y += bmi160/
+obj-y += bmi270/
 obj-y += bmi323/
 obj-y += bno055/
 
drivers/iio/imu/bmi270/Kconfig new file +22 -0
new file mode 100644
index 000000000000..05e13c67db57
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# BMI270 IMU driver
+#
+
+config BMI270
+	tristate
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+
+config BMI270_I2C
+	tristate "Bosch BMI270 I2C driver"
+	depends on I2C
+	select BMI270
+	select REGMAP_I2C
+	help
+	  Enable support for the Bosch BMI270 6-Axis IMU connected to I2C
+	  interface.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called bmi270_i2c.
+
drivers/iio/imu/bmi270/Makefile new file +6 -0
new file mode 100644
index 000000000000..ab4acaaee6d2
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for Bosch BMI270 IMU
+#
+obj-$(CONFIG_BMI270) += bmi270_core.o
+obj-$(CONFIG_BMI270_I2C) += bmi270_i2c.o
drivers/iio/imu/bmi270/bmi270.h new file +18 -0
new file mode 100644
index 000000000000..52e806529748
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef BMI270_H_
+#define BMI270_H_
+
+#include <linux/iio/iio.h>
+
+struct bmi270_data {
+	struct device *dev;
+	struct regmap *regmap;
+};
+
+extern const struct regmap_config bmi270_regmap_config;
+
+int bmi270_core_probe(struct device *dev, struct regmap *regmap,
+		      const char *name, bool use_spi);
+
+#endif  /* BMI270_H_ */
drivers/iio/imu/bmi270/bmi270_core.c new file +322 -0

322 lines not on this page · raw message

drivers/iio/imu/bmi270/bmi270_i2c.c new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.46.0
maintainer
#6 lore raw

Re: [PATCH v2 2/2] iio: imu: Add i2c driver for bmi270 imu

Quoted 6 lines · On Fri, 6 Sep 2024 12:52:51 -0400 Alex Lanzano <[email protected]> wrote:

Add initial i2c support for the Bosch BMI270 6-axis IMU. Provides raw read access to acceleration and angle velocity measurements via iio channels. Device configuration requires firmware provided by Bosch and is requested and load from userspace.

Hi Alex,

Various comments inline. Just to check, have you confirmed these have a substantially different interface to the other bosch IMU devices?

(I'm too lazy / busy to datasheet dive today!)

Jonathan

Quoted 6 lines
drivers/iio/imu/bmi270/Kconfig new file +0 -0
new file mode 100644
index 000000000000..05e13c67db57

drivers/iio/imu/bmi270/Kconfig line 9

Quoted 9 lines
drivers/iio/imu/bmi270/Kconfig new file +9 -0
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# BMI270 IMU driver
+#
+
+config BMI270
+	tristate
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER

So far the driver isn't using the buffer / triggered buffer so drop this until it does.

Quoted 24 lines
drivers/iio/imu/bmi270/Kconfig new file +13 -0
+
+config BMI270_I2C
+	tristate "Bosch BMI270 I2C driver"
+	depends on I2C
+	select BMI270
+	select REGMAP_I2C
+	help
+	  Enable support for the Bosch BMI270 6-Axis IMU connected to I2C
+	  interface.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called bmi270_i2c.
+
drivers/iio/imu/bmi270/bmi270_core.c new file +4 -0
new file mode 100644
index 000000000000..f8c53e8e35a2
@@ -0,0 +1,322 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+
+#include <linux/firmware.h>
+#include <linux/i2c.h>

drivers/iio/imu/bmi270/bmi270_core.c line 14

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +10 -0
+#include <linux/iio/iio.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+
+#include "bmi270.h"
+
+#define BMI270_CHIP_ID 0x24
+#define BMI270_INIT_DATA_FILE "bmi270-init-data.fw"
+
+enum bmi270_registers {

Unless you use the type somewhere, ti's usually better to just use defines for register addresses.
That lets you group them with the field definitions for the elements that make up each register.

Quoted 9 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +9 -0
+	BMI270_REG_CHIP_ID = 0x00,
+	BMI270_REG_INTERNAL_STATUS = 0x21,
+	BMI270_REG_ACC_CONF = 0x40,
+	BMI270_REG_GYR_CONF = 0x42,
+	BMI270_REG_INIT_CTRL = 0x59,
+	BMI270_REG_INIT_DATA = 0x5e,
+	BMI270_REG_PWR_CONF = 0x7c,
+	BMI270_REG_PWR_CTRL = 0x7d,
+};

drivers/iio/imu/bmi270/bmi270_core.c line 48

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +9 -0
 
+static int bmi270_get_data(struct bmi270_data *bmi270_device,
+			   int chan_type, int axis, int *val)
+{
+	__le16 sample;
+	int reg;
+
+	switch (chan_type) {
+	case IIO_ACCEL:
+		reg = 0xc + (axis - IIO_MOD_X) * sizeof(sample);

0xc and 0x12 are magic values, give them names via defines. I assume they are the x access data registers.

drivers/iio/imu/bmi270/bmi270_core.c line 57

Quoted 9 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +9 -0
+		break;
+	case IIO_ANGL_VEL:
+		reg = 0x12 + (axis - IIO_MOD_X) * sizeof(sample);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	regmap_bulk_read(bmi270_device->regmap, reg, &sample, sizeof(sample));

check for an error return.

It's fine with i2c to use a buffer on the stack (as it bounce buffers everything) but keep in mind that regmap in general doesn't guarantee that (even it happens to be the case today) so when you add SPI this will need to be a DMA safe buffer. Either allocate one on the heap, or embed one marked __aligned(IIO_DMA_MINALIGN) at the end of your iio_priv() structure.

Note you only need to do this once you add spi support.

Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +4 -0
+	*val = sign_extend32(le16_to_cpu(sample), 15);
+
+	return 0;
+}

drivers/iio/imu/bmi270/bmi270_core.c line 71

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +10 -0
+
+static int bmi270_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int *val, int *val2, long mask)
+{
+	struct bmi270_data *bmi270_device = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		bmi270_get_data(bmi270_device, chan->type, chan->channel2, val);

Check for error return and pass it on if there is one.

drivers/iio/imu/bmi270/bmi270_core.c line 77

Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +6 -0
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;

unreachable code. Drop this last return.

Quoted 44 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +44 -0
+}
+
+static const struct iio_info bmi270_info = {
+	.read_raw = bmi270_read_raw,
+};
+
+static const struct iio_chan_spec bmi270_channels[] = {
+	{
+		.type = IIO_ACCEL,
+		.modified = 1,
+		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_ACCEL_X,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+	{
+		.type = IIO_ACCEL,
+		.modified = 1,
+		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_ACCEL_Y,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+	{
+		.type = IIO_ACCEL,
+		.modified = 1,
+		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),

drivers/iio/imu/bmi270/bmi270_core.c line 131

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +10 -0
+		.scan_index = BMI270_SCAN_ACCEL_Z,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+	{
+		.type = IIO_ANGL_VEL,

Perhaps a macro given 3 instances that only differ in _X _Y _Z. And another one for the acceleration channels.

Quoted 32 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +32 -0
+		.modified = 1,
+		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_GYRO_X,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+	{
+		.type = IIO_ANGL_VEL,
+		.modified = 1,
+		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_GYRO_Y,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+
+	},
+	{
+		.type = IIO_ANGL_VEL,
+		.modified = 1,

drivers/iio/imu/bmi270/bmi270_core.c line 173

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +10 -0
+		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_GYRO_Z,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,

Until you add support for the buffer, scan_index and scan_type should be at least mostly irrelevant. If you aren't using them for something else, don't set them until the patch where you add buffer support.

Quoted 12 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +12 -0
+		},
+	},
+};
+
+static int bmi270_validate_chip_id(struct bmi270_data *bmi270_device)
+{
+	int chip_id;
+	int ret;
+	struct device *dev = bmi270_device->dev;
+	struct regmap *regmap = bmi270_device->regmap;
+
+	ret = regmap_read(regmap, BMI270_REG_CHIP_ID, &chip_id);

drivers/iio/imu/bmi270/bmi270_core.c line 195

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +10 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to read chip id");
+
+	if (chip_id != BMI270_CHIP_ID)
+		return dev_err_probe(dev, -ENODEV, "Invalid chip id");
+
+	return 0;
+}
+
+static int bmi270_write_init_data(struct bmi270_data *bmi270_device)

Consider renaming. Perhaps bmi270_write_calibration_data() or something like that?

drivers/iio/imu/bmi270/bmi270_core.c line 208

Quoted 13 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +13 -0
+{
+	int pwr_conf = 0;
+	int ret;
+	int status = 0;
+	const struct firmware *init_data;
+	struct device *dev = bmi270_device->dev;
+	struct regmap *regmap = bmi270_device->regmap;
+
+	ret = regmap_read(regmap, BMI270_REG_PWR_CONF, &pwr_conf);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to read power configuration");
+
+	pwr_conf &=  0xfffffffe;

Probably define that as ~BIT(0) plus give it a name as that's not obvious. regmap_clear_bits() would be cleaner for clearing just one bit.

Quoted 11 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +11 -0
+	ret = regmap_write(regmap, BMI270_REG_PWR_CONF, pwr_conf);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to write power configuration");
+
+	usleep_range(450, 1000);
+
+	ret = regmap_write(regmap, BMI270_REG_INIT_CTRL, 0x0);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to prepare device to load init data");
+
+	ret = request_firmware(&init_data, BMI270_INIT_DATA_FILE, dev);

drivers/iio/imu/bmi270/bmi270_core.c line 229

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +10 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to load init data file");
+
+	ret = regmap_bulk_write(regmap, BMI270_REG_INIT_DATA,
+				init_data->data, init_data->size);
+	release_firmware(init_data);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to write init data");
+
+	ret = regmap_write(regmap, BMI270_REG_INIT_CTRL, 0x1);

Give that bit a define even if it's the only one in the register.

drivers/iio/imu/bmi270/bmi270_core.c line 239

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +10 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to stop device initialization");
+
+	usleep_range(20000, 55000);
+
+	ret = regmap_read(regmap, BMI270_REG_INTERNAL_STATUS, &status);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to read internal status");
+
+	if (status != 1)

Define even for that 1. It must mean something?

drivers/iio/imu/bmi270/bmi270_core.c line 251

Quoted 12 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +12 -0
+		return dev_err_probe(dev, -ENODEV, "Device failed to initialize");
+
+	return 0;
+}
+
+static int bmi270_configure_imu(struct bmi270_data *bmi270_device)
+{
+	int ret;
+	struct device *dev = bmi270_device->dev;
+	struct regmap *regmap = bmi270_device->regmap;
+
+	ret = regmap_write(regmap, BMI270_REG_PWR_CTRL, 0x0e);

Magic register values. Assuming you know what these break down into please add the defines for each field so see can see what is being controlled by each write.

Quoted 25 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +25 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable accelerometer and gyroscope");
+
+	ret = regmap_write(regmap, BMI270_REG_ACC_CONF, 0xa8);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to configure accelerometer");
+
+	ret = regmap_write(regmap, BMI270_REG_GYR_CONF, 0xa9);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to configure gyroscope");
+
+	ret = regmap_write(regmap, BMI270_REG_PWR_CONF, 0x02);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to set power configuration");
+
+	return 0;
+}
+
+static int bmi270_chip_init(struct bmi270_data *bmi270_device)
+{
+	int ret;
+
+	ret = bmi270_validate_chip_id(bmi270_device);
+	if (ret)
+		return ret;

drivers/iio/imu/bmi270/bmi270_core.c line 286

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +10 -0
+
+	ret = bmi270_write_init_data(bmi270_device);
+	if (ret)
+		return ret;
+
+	ret = bmi270_configure_imu(bmi270_device);
+	if (ret)
+		return ret;
+
+	return 0;

return bmi270_configure_imu()
saves a few lines for no significant loss of readability.

drivers/iio/imu/bmi270/bmi270_core.c line 290

Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +4 -0
+}
+
+int bmi270_core_probe(struct device *dev, struct regmap *regmap,
+		      const char *name, bool use_spi)

Drop the use_spi parameter. That isn't relevant yet (and may never be!)

Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +4 -0
+{
+	int ret;
+	struct bmi270_data *bmi270_device;
+	struct iio_dev *indio_dev;

drivers/iio/imu/bmi270/bmi270_core.c line 304

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +10 -0
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(struct bmi270_data *));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	bmi270_device = iio_priv(indio_dev);
+	bmi270_device->dev = dev;
+	bmi270_device->regmap = regmap;
+
+	dev_set_drvdata(dev, indio_dev);

Is this ever used? If not, don't set it.

Quoted 24 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +18 -0
+
+	ret = bmi270_chip_init(bmi270_device);
+	if (ret)
+		return ret;
+
+	indio_dev->channels = bmi270_channels;
+	indio_dev->num_channels = ARRAY_SIZE(bmi270_channels);
+	indio_dev->name = name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->info = &bmi270_info;
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+EXPORT_SYMBOL_NS_GPL(bmi270_core_probe, IIO_BMI270);
+
+MODULE_AUTHOR("Alex Lanzano");
+MODULE_DESCRIPTION("BMI270 driver");
+MODULE_LICENSE("GPL");
drivers/iio/imu/bmi270/bmi270_i2c.c new file +0 -0
new file mode 100644
index 000000000000..2a18c3af92d2

drivers/iio/imu/bmi270/bmi270_i2c.c line 6

Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +6 -0
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/module.h>

mod_devicetable.h

Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +6 -0
+#include <linux/regmap.h>
+
+#include "bmi270.h"
+
+static int bmi270_i2c_probe(struct i2c_client *client)
+{

drivers/iio/imu/bmi270/bmi270_i2c.c line 22

Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +10 -0
+	const char *name;
+	struct regmap *regmap;
+	struct device *dev = &client->dev;
+	const struct i2c_device_id *id;
+
+	regmap = devm_regmap_init_i2c(client, &bmi270_regmap_config);
+	if (IS_ERR(regmap)) {
+		return dev_err_probe(dev, PTR_ERR(regmap),
+				     "Failed to init i2c regmap");
+	}

No {} needed around a single statement like this.

drivers/iio/imu/bmi270/bmi270_i2c.c line 24

Quoted 2 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +2 -0
+
+	id = i2c_client_get_device_id(client);

For modern drivers, should only need to get the associated data and i2c_get_match_data() deals with various firmware types. You only need that once multiple chips are supported. For now there should be no reason to query this.

drivers/iio/imu/bmi270/bmi270_i2c.c line 28

Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +4 -0
+	if (id)
+		name = id->name;
+	else
+		name = dev_name(dev);

Until the driver supports multiple devices, hardcode this in the core code as "bmi270"

When multiple parts are supported, use a chip type specific structure with a const char * in it.
naming via id->name or dev_name and similar tends to give unstable results that aren't always the part number of the device.

drivers/iio/imu/bmi270/bmi270_i2c.c line 40

Quoted 12 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +12 -0
+
+	return bmi270_core_probe(dev, regmap, name, false);
+}
+
+static const struct i2c_device_id bmi270_i2c_id[] = {
+	{"bmi270", 0},
+	{}
+};
+
+static const struct of_device_id bmi270_of_match[] = {
+	{.compatible = "bosch,bmi270"},
+	{},

Preferred style (I'm slowly tidying this up across IIO) is

	{ .compatible = "bosch,bmi270" },
	{ }

So spaces and no comma after terminator as we never want to add anything after that.
Applies to other similar cases.

Quoted 1 line
drivers/iio/imu/bmi270/bmi270_i2c.c new file +1 -0
+};
author maintainer
#7 lore raw

Re: [PATCH v2 2/2] iio: imu: Add i2c driver for bmi270 imu

Quoted 10 lines · Jonathan Cameron
Quoted 6 lines · On Fri, 6 Sep 2024 12:52:51 -0400 Alex Lanzano <[email protected]> wrote:

Add initial i2c support for the Bosch BMI270 6-axis IMU. Provides raw read access to acceleration and angle velocity measurements via iio channels. Device configuration requires firmware provided by Bosch and is requested and load from userspace.

Quoted 9 lines

Hi Alex,

Various comments inline. Just to check, have you confirmed these have a substantially different interface to the other bosch IMU devices?

(I'm too lazy / busy to datasheet dive today!)

Jonathan

Yes, the BMI270 has a different register layout and some features not included in the existing BMI160 and BMI323 such as the wrist worn gesture detection.

Quoted 9 lines
Quoted 9 lines
drivers/iio/imu/bmi270/Kconfig new file +3 -0
new file mode 100644
index 000000000000..05e13c67db57
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# BMI270 IMU driver
Quoted 9 lines

drivers/iio/imu/bmi270/Kconfig line 9

Quoted 6 lines
drivers/iio/imu/bmi270/Kconfig new file +6 -0
+#
+
+config BMI270
+	tristate
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER

So far the driver isn't using the buffer / triggered buffer so drop this until it does.

Wiil do in v3!

Quoted 31 lines
Quoted 31 lines
drivers/iio/imu/bmi270/Kconfig new file +13 -0
+
+config BMI270_I2C
+	tristate "Bosch BMI270 I2C driver"
+	depends on I2C
+	select BMI270
+	select REGMAP_I2C
+	help
+	  Enable support for the Bosch BMI270 6-Axis IMU connected to I2C
+	  interface.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called bmi270_i2c.
+
drivers/iio/imu/bmi270/bmi270_core.c new file +11 -0
new file mode 100644
index 000000000000..f8c53e8e35a2
@@ -0,0 +1,322 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+
+#include <linux/firmware.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+
+#include "bmi270.h"
+
+#define BMI270_CHIP_ID 0x24
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 14

Quoted 3 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +3 -0
+#define BMI270_INIT_DATA_FILE "bmi270-init-data.fw"
+
+enum bmi270_registers {

Unless you use the type somewhere, ti's usually better to just use defines for register addresses.
That lets you group them with the field definitions for the elements that make up each register.

Will fix in v3!

Quoted 10 lines
Quoted 9 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +9 -0
+	BMI270_REG_CHIP_ID = 0x00,
+	BMI270_REG_INTERNAL_STATUS = 0x21,
+	BMI270_REG_ACC_CONF = 0x40,
+	BMI270_REG_GYR_CONF = 0x42,
+	BMI270_REG_INIT_CTRL = 0x59,
+	BMI270_REG_INIT_DATA = 0x5e,
+	BMI270_REG_PWR_CONF = 0x7c,
+	BMI270_REG_PWR_CTRL = 0x7d,
+};
Quoted 13 lines

drivers/iio/imu/bmi270/bmi270_core.c line 48

Quoted 9 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +9 -0
+static int bmi270_get_data(struct bmi270_data *bmi270_device,
+			   int chan_type, int axis, int *val)
+{
+	__le16 sample;
+	int reg;
+
+	switch (chan_type) {
+	case IIO_ACCEL:
+		reg = 0xc + (axis - IIO_MOD_X) * sizeof(sample);

0xc and 0x12 are magic values, give them names via defines. I assume they are the x access data registers.

Will fix in v3!

Quoted 11 lines

drivers/iio/imu/bmi270/bmi270_core.c line 57

Quoted 9 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +9 -0
+		break;
+	case IIO_ANGL_VEL:
+		reg = 0x12 + (axis - IIO_MOD_X) * sizeof(sample);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	regmap_bulk_read(bmi270_device->regmap, reg, &sample, sizeof(sample));

check for an error return.

Will fix in v3!

Quoted 8 lines

It's fine with i2c to use a buffer on the stack (as it bounce buffers everything) but keep in mind that regmap in general doesn't guarantee that (even it happens to be the case today) so when you add SPI this will need to be a DMA safe buffer. Either allocate one on the heap, or embed one marked __aligned(IIO_DMA_MINALIGN) at the end of your iio_priv() structure.

Note you only need to do this once you add spi support.

Will make note of this for upcoming SPI support.

Quoted 8 lines
Quoted 8 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +8 -0
+	*val = sign_extend32(le16_to_cpu(sample), 15);
+
+	return 0;
+}
+
+static int bmi270_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int *val, int *val2, long mask)
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 71

Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +6 -0
+{
+	struct bmi270_data *bmi270_device = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		bmi270_get_data(bmi270_device, chan->type, chan->channel2, val);

Check for error return and pass it on if there is one.

Will fix in v3.

Quoted 8 lines

drivers/iio/imu/bmi270/bmi270_core.c line 77

Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +6 -0
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;

unreachable code. Drop this last return.

Will fix in v3.

Quoted 49 lines
Quoted 49 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +49 -0
+}
+
+static const struct iio_info bmi270_info = {
+	.read_raw = bmi270_read_raw,
+};
+
+static const struct iio_chan_spec bmi270_channels[] = {
+	{
+		.type = IIO_ACCEL,
+		.modified = 1,
+		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_ACCEL_X,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+	{
+		.type = IIO_ACCEL,
+		.modified = 1,
+		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_ACCEL_Y,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+	{
+		.type = IIO_ACCEL,
+		.modified = 1,
+		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_ACCEL_Z,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
Quoted 10 lines

drivers/iio/imu/bmi270/bmi270_core.c line 131

Quoted 5 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +5 -0
+			.endianness = IIO_LE,
+		},
+	},
+	{
+		.type = IIO_ANGL_VEL,

Perhaps a macro given 3 instances that only differ in _X _Y _Z. And another one for the acceleration channels.

Will create macro in v3.

Quoted 38 lines
Quoted 38 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +38 -0
+		.modified = 1,
+		.channel2 = IIO_MOD_X,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_GYRO_X,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+	},
+	{
+		.type = IIO_ANGL_VEL,
+		.modified = 1,
+		.channel2 = IIO_MOD_Y,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_GYRO_Y,
+		.scan_type = {
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,
+		},
+
+	},
+	{
+		.type = IIO_ANGL_VEL,
+		.modified = 1,
+		.channel2 = IIO_MOD_Z,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+			BIT(IIO_CHAN_INFO_FREQUENCY),
+		.scan_index = BMI270_SCAN_GYRO_Z,
+		.scan_type = {
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 97

Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +4 -0
+			.sign = 's',
+			.realbits = 16,
+			.storagebits = 16,
+			.endianness = IIO_LE,

Until you add support for the buffer, scan_index and scan_type should be at least mostly irrelevant. If you aren't using them for something else, don't set them until the patch where you add buffer support.

Will drop in v3.

Quoted 16 lines
Quoted 16 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +16 -0
+		},
+	},
+};
+
+static int bmi270_validate_chip_id(struct bmi270_data *bmi270_device)
+{
+	int chip_id;
+	int ret;
+	struct device *dev = bmi270_device->dev;
+	struct regmap *regmap = bmi270_device->regmap;
+
+	ret = regmap_read(regmap, BMI270_REG_CHIP_ID, &chip_id);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to read chip id");
+
+	if (chip_id != BMI270_CHIP_ID)
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 195

Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +6 -0
+		return dev_err_probe(dev, -ENODEV, "Invalid chip id");
+
+	return 0;
+}
+
+static int bmi270_write_init_data(struct bmi270_data *bmi270_device)

Consider renaming. Perhaps bmi270_write_calibration_data() or something like that?

Will rename in v3.

Quoted 8 lines
Quoted 8 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +8 -0
+{
+	int pwr_conf = 0;
+	int ret;
+	int status = 0;
+	const struct firmware *init_data;
+	struct device *dev = bmi270_device->dev;
+	struct regmap *regmap = bmi270_device->regmap;
+
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 208

Quoted 5 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +5 -0
+	ret = regmap_read(regmap, BMI270_REG_PWR_CONF, &pwr_conf);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to read power configuration");
+
+	pwr_conf &=  0xfffffffe;

Probably define that as ~BIT(0) plus give it a name as that's not obvious. regmap_clear_bits() would be cleaner for clearing just one bit.

Will use regmap_clear_bits() in v3.

Quoted 14 lines
Quoted 14 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +14 -0
+	ret = regmap_write(regmap, BMI270_REG_PWR_CONF, pwr_conf);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to write power configuration");
+
+	usleep_range(450, 1000);
+
+	ret = regmap_write(regmap, BMI270_REG_INIT_CTRL, 0x0);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to prepare device to load init data");
+
+	ret = request_firmware(&init_data, BMI270_INIT_DATA_FILE, dev);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to load init data file");
+
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 229

Quoted 7 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +7 -0
+	ret = regmap_bulk_write(regmap, BMI270_REG_INIT_DATA,
+				init_data->data, init_data->size);
+	release_firmware(init_data);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to write init data");
+
+	ret = regmap_write(regmap, BMI270_REG_INIT_CTRL, 0x1);

Give that bit a define even if it's the only one in the register.

Will do in v3.

Quoted 4 lines
Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +4 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to stop device initialization");
+
+	usleep_range(20000, 55000);
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 239

Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +6 -0
+
+	ret = regmap_read(regmap, BMI270_REG_INTERNAL_STATUS, &status);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to read internal status");
+
+	if (status != 1)

Define even for that 1. It must mean something?

Will define in v3.

Quoted 8 lines
Quoted 8 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +8 -0
+		return dev_err_probe(dev, -ENODEV, "Device failed to initialize");
+
+	return 0;
+}
+
+static int bmi270_configure_imu(struct bmi270_data *bmi270_device)
+{
+	int ret;
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 251

Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +4 -0
+	struct device *dev = bmi270_device->dev;
+	struct regmap *regmap = bmi270_device->regmap;
+
+	ret = regmap_write(regmap, BMI270_REG_PWR_CTRL, 0x0e);

Magic register values. Assuming you know what these break down into please add the defines for each field so see can see what is being controlled by each write.

Will define in v3.

Quoted 30 lines
Quoted 30 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +30 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable accelerometer and gyroscope");
+
+	ret = regmap_write(regmap, BMI270_REG_ACC_CONF, 0xa8);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to configure accelerometer");
+
+	ret = regmap_write(regmap, BMI270_REG_GYR_CONF, 0xa9);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to configure gyroscope");
+
+	ret = regmap_write(regmap, BMI270_REG_PWR_CONF, 0x02);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to set power configuration");
+
+	return 0;
+}
+
+static int bmi270_chip_init(struct bmi270_data *bmi270_device)
+{
+	int ret;
+
+	ret = bmi270_validate_chip_id(bmi270_device);
+	if (ret)
+		return ret;
+
+	ret = bmi270_write_init_data(bmi270_device);
+	if (ret)
+		return ret;
+
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 286

Quoted 5 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +5 -0
+	ret = bmi270_configure_imu(bmi270_device);
+	if (ret)
+		return ret;
+
+	return 0;

return bmi270_configure_imu()
saves a few lines for no significant loss of readability.

Will do in v3.

Quoted 7 lines

drivers/iio/imu/bmi270/bmi270_core.c line 290

Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +4 -0
+}
+
+int bmi270_core_probe(struct device *dev, struct regmap *regmap,
+		      const char *name, bool use_spi)

Drop the use_spi parameter. That isn't relevant yet (and may never be!)

Will do in v3.

Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +7 -0
+{
+	int ret;
+	struct bmi270_data *bmi270_device;
+	struct iio_dev *indio_dev;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(struct bmi270_data *));
+	if (!indio_dev)
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_core.c line 304

Quoted 7 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +7 -0
+		return -ENOMEM;
+
+	bmi270_device = iio_priv(indio_dev);
+	bmi270_device->dev = dev;
+	bmi270_device->regmap = regmap;
+
+	dev_set_drvdata(dev, indio_dev);

Is this ever used? If not, don't set it.

Will drop in v3.

Quoted 24 lines
Quoted 24 lines
drivers/iio/imu/bmi270/bmi270_core.c new file +18 -0
+
+	ret = bmi270_chip_init(bmi270_device);
+	if (ret)
+		return ret;
+
+	indio_dev->channels = bmi270_channels;
+	indio_dev->num_channels = ARRAY_SIZE(bmi270_channels);
+	indio_dev->name = name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->info = &bmi270_info;
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+EXPORT_SYMBOL_NS_GPL(bmi270_core_probe, IIO_BMI270);
+
+MODULE_AUTHOR("Alex Lanzano");
+MODULE_DESCRIPTION("BMI270 driver");
+MODULE_LICENSE("GPL");
drivers/iio/imu/bmi270/bmi270_i2c.c new file +0 -0
new file mode 100644
index 000000000000..2a18c3af92d2
@@ -0,0 +1,56 @@
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_i2c.c line 6

Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +6 -0
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/module.h>

mod_devicetable.h

Will add in v3.

Quoted 10 lines
Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +10 -0
+#include <linux/regmap.h>
+
+#include "bmi270.h"
+
+static int bmi270_i2c_probe(struct i2c_client *client)
+{
+	const char *name;
+	struct regmap *regmap;
+	struct device *dev = &client->dev;
+	const struct i2c_device_id *id;
Quoted 9 lines

drivers/iio/imu/bmi270/bmi270_i2c.c line 22

Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +6 -0
+
+	regmap = devm_regmap_init_i2c(client, &bmi270_regmap_config);
+	if (IS_ERR(regmap)) {
+		return dev_err_probe(dev, PTR_ERR(regmap),
+				     "Failed to init i2c regmap");
+	}

No {} needed around a single statement like this.

Will fix in v3.

Quoted 8 lines

drivers/iio/imu/bmi270/bmi270_i2c.c line 24

Quoted 2 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +2 -0
+
+	id = i2c_client_get_device_id(client);

For modern drivers, should only need to get the associated data and i2c_get_match_data() deals with various firmware types. You only need that once multiple chips are supported. For now there should be no reason to query this.

Will drop in v3.

Quoted 8 lines

drivers/iio/imu/bmi270/bmi270_i2c.c line 28

Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +4 -0
+	if (id)
+		name = id->name;
+	else
+		name = dev_name(dev);

Until the driver supports multiple devices, hardcode this in the core code as "bmi270"

Quoted 6 lines

When multiple parts are supported, use a chip type specific structure with a const char * in it.
naming via id->name or dev_name and similar tends to give unstable results that aren't always the part number of the device.

Will hardcode this in v3.

Quoted 13 lines

drivers/iio/imu/bmi270/bmi270_i2c.c line 40

Quoted 12 lines
drivers/iio/imu/bmi270/bmi270_i2c.c new file +12 -0
+
+	return bmi270_core_probe(dev, regmap, name, false);
+}
+
+static const struct i2c_device_id bmi270_i2c_id[] = {
+	{"bmi270", 0},
+	{}
+};
+
+static const struct of_device_id bmi270_of_match[] = {
+	{.compatible = "bosch,bmi270"},
+	{},

Preferred style (I'm slowly tidying this up across IIO) is

Quoted 7 lines
	{ .compatible = "bosch,bmi270" },
	{ }

So spaces and no comma after terminator as we never want to add anything after that.
Applies to other similar cases.

Will fix in v3!

Quoted 3 lines

drivers/iio/imu/bmi270/bmi270_i2c.c line 41

Quoted 1 line
drivers/iio/imu/bmi270/bmi270_i2c.c new file +1 -0
+};

Thanks for the review! Will fix this up and resend!

Best regards,
Alex