Skip to contentSkip to conversation

iio: add driver for Bosch BMA400 accelerometer

PATCH Superseded Dan Robertson 5 participants 12 messages Dec 11, 2019 – Dec 18, 2019

Subsystems (3)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Dan Robertson

author maintainer
#0 lore raw

v6 cover iio: add driver for Bosch BMA400 accelerometer

Only update from the last patchset are the changes to the Kconfig options.

Cheers,

- Dan

Changes in v6:

* Improve readability Kconfig options

Changes in v5:

* Move to using a function instead of lookup tables for scale and frequency conversions.
* Rename DT bindings to bosch,bma400.yaml
* Fixed other errors and improvements found by reviewers

Changes in v4:

* Fix error in DT bindings
* Fix typo when setting the OSR
* Simplified the cached sample frequency
* Fix the MODULE_LICENSE

Changes in v3:

* Use yaml format for DT bindings
* Remove strict dependency on OF
* Tidy Kconfig dependencies
* Stylistic changes
* Do not soft-reset device on remove

Changes in v2:

* Implemented iio_info -> read_avail
* Stylistic changes
* Implemented devicetree bindings

Dan Robertson (2):
dt-bindings: iio: accel: bma400: add bindings
iio: (bma400) add driver for the BMA400

 .../bindings/iio/accel/bosch,bma400.yaml      |  39 +
 drivers/iio/accel/Kconfig                     |  16 +
 drivers/iio/accel/Makefile                    |   2 +
 drivers/iio/accel/bma400.h                    |  95 ++
 drivers/iio/accel/bma400_core.c               | 823 ++++++++++++++++++
 drivers/iio/accel/bma400_i2c.c                |  62 ++
 6 files changed, 1037 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml
 create mode 100644 drivers/iio/accel/bma400.h
 create mode 100644 drivers/iio/accel/bma400_core.c
 create mode 100644 drivers/iio/accel/bma400_i2c.c
author maintainer
#1 lore raw

PATCH v6 1/2 dt-bindings: iio: accel: bma400: add bindings

Add devicetree binding for the Bosch BMA400 3-axes ultra-low power accelerometer sensor.

---
 .../bindings/iio/accel/bosch,bma400.yaml      | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml
Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml new file +39 -0
new file mode 100644
index 000000000000..03b7ad345523
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/accel/bosch,bma400.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMA400 triaxial acceleration sensor
+
+maintainers:
+  - Dan Robertson <[email protected]>
+
+description: |
+  Acceleration and temerature iio sensors with an i2c interface
+
+  Specifications about the sensor can be found at:
+    https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMA400-DS000.pdf
+
+properties:
+  compatible:
+    enum:
+      - bosch,bma400
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+examples:
+  - |
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      accelerometer@14 {
+        compatible = "bosch,bma400";
+        reg = <0x14>;
+      };
+    };
#2 lore raw

Re: [PATCH v6 1/2] dt-bindings: iio: accel: bma400: add bindings

Hi Dan,

thanks for your patch!

Quoted 4 lines · Dan Robertson

Add devicetree binding for the Bosch BMA400 3-axes ultra-low power accelerometer sensor.

(...)

Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml line 16

Quoted 2 lines
Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml new file +2 -0
+  Specifications about the sensor can be found at:
+    https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMA400-DS000.pdf

As can be seen in page 113 in the manual this component has VDD and VDDIO supplies and sooner or later someone is going to have to model that so I'd say add vdd-supply and vddio-supply regulator phandles as optional to the component already from start.

Thanks!
Linus Walleij

maintainer
#3 lore raw

Re: [PATCH v6 1/2] dt-bindings: iio: accel: bma400: add bindings

Quoted 12 lines · Linus Walleij

Hi Dan,

thanks for your patch!

Quoted 4 lines · Dan Robertson

Add devicetree binding for the Bosch BMA400 3-axes ultra-low power accelerometer sensor.

(...)

Quoted 8 lines

Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml line 16

Quoted 2 lines
Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml new file +2 -0
+  Specifications about the sensor can be found at:
+    https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMA400-DS000.pdf

As can be seen in page 113 in the manual this component has VDD and VDDIO supplies and sooner or later someone is going to have to model that so I'd say add vdd-supply and vddio-supply regulator phandles as optional to the component already from start.

And interrupts too. Doesn't matter what the driver currently uses.

Rob

author maintainer
#4 lore raw

Re: [PATCH v6 1/2] dt-bindings: iio: accel: bma400: add bindings

Quoted 16 lines · Rob Herring
Quoted 15 lines · Linus Walleij

Hi Dan,

thanks for your patch!

Quoted 4 lines · Dan Robertson

Add devicetree binding for the Bosch BMA400 3-axes ultra-low power accelerometer sensor.

(...)

Quoted 2 lines
Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml new file +2 -0
+  Specifications about the sensor can be found at:
+    https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMA400-DS000.pdf
Quoted 7 lines
Quoted 5 lines

As can be seen in page 113 in the manual this component has VDD and VDDIO supplies and sooner or later someone is going to have to model that so I'd say add vdd-supply and vddio-supply regulator phandles as optional to the component already from start.

And interrupts too. Doesn't matter what the driver currently uses.

Will do. I plan to work on adding support for interrupts in a follow-up patchset, but I'll add this to the devicetree documentation.

Cheers,

- Dan

maintainer
#5 lore raw

Re: [PATCH v6 1/2] dt-bindings: iio: accel: bma400: add bindings

In reply to Dan Robertson
Quoted 15 lines · Dan Robertson

Add devicetree binding for the Bosch BMA400 3-axes ultra-low power accelerometer sensor.

---
 .../bindings/iio/accel/bosch,bma400.yaml      | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml
Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml new file +0 -0
new file mode 100644
index 000000000000..03b7ad345523

Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml line 1

Quoted 1 line
Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml new file +1 -0
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: GPL-2.0

Dual license new bindings please:

(GPL-2.0-only OR BSD-2-Clause)

Quoted 40 lines
Documentation/devicetree/bindings/iio/accel/bosch,bma400.yaml new file +38 -0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/accel/bosch,bma400.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Bosch BMA400 triaxial acceleration sensor
+
+maintainers:
+  - Dan Robertson <[email protected]>
+
+description: |
+  Acceleration and temerature iio sensors with an i2c interface
+
+  Specifications about the sensor can be found at:
+    https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMA400-DS000.pdf
+
+properties:
+  compatible:
+    enum:
+      - bosch,bma400
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+examples:
+  - |
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      accelerometer@14 {
+        compatible = "bosch,bma400";
+        reg = <0x14>;
+      };
+    };
author maintainer
#6 lore raw

PATCH v6 2/2 iio: (bma400) add driver for the BMA400

In reply to Dan Robertson

Add a IIO driver for the Bosch BMA400 3-axes ultra-low power accelerometer. The driver supports reading from the acceleration and temperature registers. The driver also supports reading and configuring the output data rate, oversampling ratio, and scale.

---
 drivers/iio/accel/Kconfig       |  16 +
 drivers/iio/accel/Makefile      |   2 +
 drivers/iio/accel/bma400.h      |  95 ++++
 drivers/iio/accel/bma400_core.c | 823 ++++++++++++++++++++++++++++++++
 drivers/iio/accel/bma400_i2c.c  |  62 +++
 5 files changed, 998 insertions(+)
 create mode 100644 drivers/iio/accel/bma400.h
 create mode 100644 drivers/iio/accel/bma400_core.c
 create mode 100644 drivers/iio/accel/bma400_i2c.c
drivers/iio/accel/Kconfig +16 -0
index d4ef35aeb579..670e60568033 100644
@@ -112,6 +112,22 @@ config BMA220
 	  To compile this driver as a module, choose M here: the
 	  module will be called bma220_spi.
 
+config BMA400
+	tristate "Bosch BMA400 3-Axis Accelerometer Driver"
+	select REGMAP
+	select BMA400_I2C if I2C
+	help
+	  Say Y here if you want to build a driver for the Bosch BMA400
+	  triaxial acceleration sensor.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called bma400_core and you will also get
+	  bma400_i2c if I2C is enabled.
+
+config BMA400_I2C
+	tristate
+	depends on BMA400
+
 config BMC150_ACCEL
 	tristate "Bosch BMC150 Accelerometer Driver"
 	select IIO_BUFFER
drivers/iio/accel/Makefile +2 -0
index 56bd0215e0d4..3a051cf37f40 100644
@@ -14,6 +14,8 @@ obj-$(CONFIG_ADXL372_I2C) += adxl372_i2c.o
 obj-$(CONFIG_ADXL372_SPI) += adxl372_spi.o
 obj-$(CONFIG_BMA180) += bma180.o
 obj-$(CONFIG_BMA220) += bma220_spi.o
+obj-$(CONFIG_BMA400) += bma400_core.o
+obj-$(CONFIG_BMA400_I2C) += bma400_i2c.o
 obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
 obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
 obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
drivers/iio/accel/bma400.h new file +95 -0

95 lines not on this page · raw message

drivers/iio/accel/bma400_core.c new file +823 -0

823 lines not on this page · raw message

drivers/iio/accel/bma400_i2c.c new file +62 -0

62 lines not on this page · raw message

#7 lore raw

Re: [PATCH v6 2/2] iio: (bma400) add driver for the BMA400

drivers/iio/accel/bma400.h line 70

Quoted 9 lines · Dan Robertson

Add a IIO driver for the Bosch BMA400 3-axes ultra-low power accelerometer. The driver supports reading from the acceleration and temperature registers. The driver also supports reading and configuring the output data rate, oversampling ratio, and scale.

drivers/iio/accel/bma400.h new file +3 -0
+#define BMA400_LP_OSR_SHIFT         0x05
+#define BMA400_NP_OSR_SHIFT         0x04
+#define BMA400_SCALE_SHIFT          0x06

I'm not sure why this is being defined as hex number instead of plain decimal...

drivers/iio/accel/bma400.h line 76

Quoted 5 lines
drivers/iio/accel/bma400.h new file +5 -0
+#define BMA400_TWO_BITS_MASK        GENMASK(1, 0)
+#define BMA400_LP_OSR_MASK          GENMASK(6, BMA400_LP_OSR_SHIFT)
+#define BMA400_NP_OSR_MASK          GENMASK(5, BMA400_NP_OSR_SHIFT)
+#define BMA400_ACC_ODR_MASK         GENMASK(3, 0)
+#define BMA400_ACC_SCALE_MASK       GENMASK(7, BMA400_SCALE_SHIFT)

And here simple better to put same numbers. It will help to read.

...

drivers/iio/accel/bma400_core.c line 137

Quoted 11 lines
drivers/iio/accel/bma400_core.c new file +10 -0
+extern const struct regmap_config bma400_regmap_config;
 
+const struct regmap_config bma400_regmap_config = {
+       .reg_bits = 8,
+       .val_bits = 8,
+       .max_register = BMA400_CMD_REG,
+       .cache_type = REGCACHE_RBTREE,
+       .writeable_reg = bma400_is_writable_reg,
+       .volatile_reg = bma400_is_volatile_reg,
+};
+EXPORT_SYMBOL(bma400_regmap_config);

I'm not sure I got the idea why this one is being exported.

...

drivers/iio/accel/bma400_core.c line 274

Quoted 2 lines
drivers/iio/accel/bma400_core.c new file +2 -0
+               if (odr < BMA400_ACC_ODR_MIN_RAW ||
+                   odr > BMA400_ACC_ODR_MAX_RAW) {

One line?

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+                       ret = -EINVAL;
+                       goto error;
+               }

...

drivers/iio/accel/bma400_core.c line 311

Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +7 -0
+               if (uhz || hz % BMA400_ACC_ODR_MIN_WHOLE_HZ)
+                       return -EINVAL;
+
+               val = hz / BMA400_ACC_ODR_MIN_WHOLE_HZ;
+               idx = __ffs(val);
+
 
+               if (val ^ BIT(idx))

Seems like funny way of checking is_power_of_2(). But it's up to maintainers. And your variant may even be better here (in code generation perspective)...

However, the whole idea here is, IIUC, to have something like

hz = 2^idx * BMA400_ACC_ODR_MIN_WHOLE_HZ

I think you may do it without divisions, i.e. call __ffs() first and then do
idx = __ffs(...);
val = hz >> idx;
if (val != BMA400_ACC_ODR_MIN_WHOLE_HZ)

    return -EINVAL;

or something like above.

Quoted 1 line

+ return -EINVAL;

...

Quoted 1 line

+ odr = (~BMA400_ACC_ODR_MASK & val) | idx;

I'm wondering why Yoda style is being used here.

...

drivers/iio/accel/bma400_core.c line 435

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+static void bma400_accel_scale_from_raw(int raw, unsigned int *val)
+{
+       *val = BMA400_SCALE_MIN * (1 << raw);

Isn't it the same as

    *val = BMA400_SCALE_MIN << raw;

?

Quoted 1 line

+}

...

Quoted 1 line

+ if (val % BMA400_SCALE_MIN || scale ^ BIT(raw))

Similar comment as above about divisions, is_power_of_2(), etc.

Quoted 1 line

+ return -EINVAL;

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       ret = regmap_read(data->regmap, BMA400_ACC_CONFIG0_REG, &val);
+       if (ret < 0)

I'm wondering if in all of these regmap_read()...

drivers/iio/accel/bma400_core.c line 531

Quoted 5 lines
drivers/iio/accel/bma400_core.c new file +4 -0
+               return ret;
 
+       ret = regmap_write(data->regmap, BMA400_ACC_CONFIG0_REG,
+                          mode | (val & ~BMA400_TWO_BITS_MASK));
+       if (ret < 0) {

...and regmap_write() calls you ever can get a positive returned code.

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+               dev_err(data->dev, "Failed to write to power-mode\n");
+               return ret;
+       }

...

drivers/iio/accel/bma400_i2c.c line 22

Quoted 3 lines
drivers/iio/accel/bma400_i2c.c new file +2 -0
+       regmap = devm_regmap_init_i2c(client, &bma400_regmap_config);
 
+

Redundant blank line.

Quoted 4 lines
drivers/iio/accel/bma400_i2c.c new file +4 -0
+       if (IS_ERR(regmap)) {
+               dev_err(&client->dev, "failed to create regmap\n");
+               return PTR_ERR(regmap);
+       }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#8 lore raw

Re: [PATCH v6 2/2] iio: (bma400) add driver for the BMA400

Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Dan Robertson

Add a IIO driver for the Bosch BMA400 3-axes ultra-low power accelerometer. The driver supports reading from the acceleration and temperature registers. The driver also supports reading and configuring the output data rate, oversampling ratio, and scale.

Quoted 5 lines

drivers/iio/accel/bma400.h line 70

Quoted 3 lines
drivers/iio/accel/bma400.h new file +3 -0
+#define BMA400_LP_OSR_SHIFT         0x05
+#define BMA400_NP_OSR_SHIFT         0x04
+#define BMA400_SCALE_SHIFT          0x06

I'm not sure why this is being defined as hex number instead of plain decimal...

Sounds good.

Quoted 7 lines

drivers/iio/accel/bma400.h line 76

Quoted 5 lines
drivers/iio/accel/bma400.h new file +5 -0
+#define BMA400_TWO_BITS_MASK        GENMASK(1, 0)
+#define BMA400_LP_OSR_MASK          GENMASK(6, BMA400_LP_OSR_SHIFT)
+#define BMA400_NP_OSR_MASK          GENMASK(5, BMA400_NP_OSR_SHIFT)
+#define BMA400_ACC_ODR_MASK         GENMASK(3, 0)
+#define BMA400_ACC_SCALE_MASK       GENMASK(7, BMA400_SCALE_SHIFT)

And here simple better to put same numbers. It will help to read.

Do you mean for the shift or for the mask?

Quoted 11 lines

drivers/iio/accel/bma400_core.c line 137

Quoted 9 lines
drivers/iio/accel/bma400_core.c new file +9 -0
+const struct regmap_config bma400_regmap_config = {
+       .reg_bits = 8,
+       .val_bits = 8,
+       .max_register = BMA400_CMD_REG,
+       .cache_type = REGCACHE_RBTREE,
+       .writeable_reg = bma400_is_writable_reg,
+       .volatile_reg = bma400_is_volatile_reg,
+};
+EXPORT_SYMBOL(bma400_regmap_config);

I'm not sure I got the idea why this one is being exported.

It needs to be exported so that it can be used in the bma400_i2c module and the future bma400_spi module. In theory, if we _really_ do not want to export this, then we can define separate regmap configs in each of the bma400_i2c and (future) bma400_spi modules, but then we would have to export the is_volitile_reg and is_writable_reg functions. As a result, I do not see any benefits to that method over exporting the config, but I could be convinced otherwise.

Quoted 4 lines

drivers/iio/accel/bma400_core.c line 274

Quoted 2 lines
drivers/iio/accel/bma400_core.c new file +2 -0
+               if (odr < BMA400_ACC_ODR_MIN_RAW ||
+                   odr > BMA400_ACC_ODR_MAX_RAW) {

One line?

It is too long if I simplify to one line.

Quoted 16 lines

drivers/iio/accel/bma400_core.c line 311

Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +7 -0
+               if (uhz || hz % BMA400_ACC_ODR_MIN_WHOLE_HZ)
+                       return -EINVAL;
+
+               val = hz / BMA400_ACC_ODR_MIN_WHOLE_HZ;
+               idx = __ffs(val);
+
 
+               if (val ^ BIT(idx))

Seems like funny way of checking is_power_of_2(). But it's up to maintainers. And your variant may even be better here (in code generation perspective)...

However, the whole idea here is, IIUC, to have something like

hz = 2^idx * BMA400_ACC_ODR_MIN_WHOLE_HZ

Quoted 7 lines

I think you may do it without divisions, i.e. call __ffs() first and then do

   idx = __ffs(...);
   val = hz >> idx;
   if (val != BMA400_ACC_ODR_MIN_WHOLE_HZ)
    return -EINVAL;

or something like above.

It would be more obvious what is being done here with is_power_of_two. I'll revisit this function with your suggestions. If I can make it simpler, I'll go this route.

Quoted 8 lines
Quoted 2 lines

+ return -EINVAL;

...

Quoted 1 line

+ odr = (~BMA400_ACC_ODR_MASK & val) | idx;

I'm wondering why Yoda style is being used here.

I guess I think like Yoda :) I can update this. I typically do prefer new_mask | old_mask, but I do not feel too strongly about it.

Quoted 7 lines

drivers/iio/accel/bma400_core.c line 435

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+static void bma400_accel_scale_from_raw(int raw, unsigned int *val)
+{
+       *val = BMA400_SCALE_MIN * (1 << raw);

Isn't it the same as

    *val = BMA400_SCALE_MIN << raw;

?

Yes. Good catch. Not sure what I was thinking :)

Quoted 10 lines
Quoted 2 lines

+ return -EINVAL;

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       ret = regmap_read(data->regmap, BMA400_ACC_CONFIG0_REG, &val);
+       if (ret < 0)

I'm wondering if in all of these regmap_read()...

Quoted 7 lines

drivers/iio/accel/bma400_core.c line 531

Quoted 5 lines
drivers/iio/accel/bma400_core.c new file +4 -0
+               return ret;
 
+       ret = regmap_write(data->regmap, BMA400_ACC_CONFIG0_REG,
+                          mode | (val & ~BMA400_TWO_BITS_MASK));
+       if (ret < 0) {

...and regmap_write() calls you ever can get a positive returned code.

From the regmap_read/regmap_write docs:

Quoted 2 lines

* A value of zero will be returned on success, a negative errno will
* be returned in error cases.

So I assume ret <= 0

Cheers,

- Dan

#9 lore raw

Re: [PATCH v6 2/2] iio: (bma400) add driver for the BMA400

Quoted 19 lines · Dan Robertson
Quoted 7 lines · Andy Shevchenko

drivers/iio/accel/bma400.h line 70

Quoted 3 lines · Dan Robertson
drivers/iio/accel/bma400.h new file +3 -0
+#define BMA400_LP_OSR_SHIFT         0x05
+#define BMA400_NP_OSR_SHIFT         0x04
+#define BMA400_SCALE_SHIFT          0x06

I'm not sure why this is being defined as hex number instead of plain decimal...

Sounds good.

Quoted 7 lines

drivers/iio/accel/bma400.h line 76

Quoted 5 lines
drivers/iio/accel/bma400.h new file +5 -0
+#define BMA400_TWO_BITS_MASK        GENMASK(1, 0)
+#define BMA400_LP_OSR_MASK          GENMASK(6, BMA400_LP_OSR_SHIFT)
+#define BMA400_NP_OSR_MASK          GENMASK(5, BMA400_NP_OSR_SHIFT)
+#define BMA400_ACC_ODR_MASK         GENMASK(3, 0)
+#define BMA400_ACC_SCALE_MASK       GENMASK(7, BMA400_SCALE_SHIFT)

And here simple better to put same numbers. It will help to read.

Quoted 1 line

Do you mean for the shift or for the mask?

SHIFTs -> plain decimals

Quoted 10 lines
Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(bma400_regmap_config);

I'm not sure I got the idea why this one is being exported.

It needs to be exported so that it can be used in the bma400_i2c module and the future bma400_spi module. In theory, if we _really_ do not want to export this, then we can define separate regmap configs in each of the bma400_i2c and (future) bma400_spi modules, but then we would have to export the is_volitile_reg and is_writable_reg functions. As a result, I do not see any benefits to that method over exporting the config, but I could be convinced otherwise.

I think there might be better way to do this.
But I leave it to you and maintainer to agree on (I will be fine with any solution you will come to).

Quoted 22 lines
Quoted 22 lines

drivers/iio/accel/bma400_core.c line 311

Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +7 -0
+               if (uhz || hz % BMA400_ACC_ODR_MIN_WHOLE_HZ)
+                       return -EINVAL;
+
+               val = hz / BMA400_ACC_ODR_MIN_WHOLE_HZ;
+               idx = __ffs(val);
+
 
+               if (val ^ BIT(idx))

Seems like funny way of checking is_power_of_2(). But it's up to maintainers. And your variant may even be better here (in code generation perspective)...

However, the whole idea here is, IIUC, to have something like

hz = 2^idx * BMA400_ACC_ODR_MIN_WHOLE_HZ

I think you may do it without divisions, i.e. call __ffs() first and then do

   idx = __ffs(...);
   val = hz >> idx;
   if (val != BMA400_ACC_ODR_MIN_WHOLE_HZ)
    return -EINVAL;
Quoted 5 lines
Quoted 1 line

or something like above.

It would be more obvious what is being done here with is_power_of_two. I'll revisit this function with your suggestions. If I can make it simpler, I'll go this route.

The main point here to get rid of divisions. Is it achievable?

Quoted 15 lines
Quoted 15 lines
Quoted 1 line

+ return -EINVAL;

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       ret = regmap_read(data->regmap, BMA400_ACC_CONFIG0_REG, &val);
+       if (ret < 0)

I'm wondering if in all of these regmap_read()...

Quoted 5 lines
drivers/iio/accel/bma400_core.c new file +4 -0
+               return ret;
 
+       ret = regmap_write(data->regmap, BMA400_ACC_CONFIG0_REG,
+                          mode | (val & ~BMA400_TWO_BITS_MASK));
+       if (ret < 0) {
Quoted 8 lines
Quoted 1 line

...and regmap_write() calls you ever can get a positive returned code.

From the regmap_read/regmap_write docs:

Quoted 2 lines

* A value of zero will be returned on success, a negative errno will
* be returned in error cases.

So I assume ret <= 0

There is no positive codes mentioned at all. And you assume right. But why we care about positive codes if they never can be returned?

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

Re: [PATCH v6 2/2] iio: (bma400) add driver for the BMA400

Quoted 35 lines · On Thu, 12 Dec 2019 11:41:45 +0200 Andy Shevchenko <[email protected]>…
Quoted 19 lines · Dan Robertson
Quoted 7 lines · Andy Shevchenko

drivers/iio/accel/bma400.h line 70

Quoted 3 lines · Dan Robertson
drivers/iio/accel/bma400.h new file +3 -0
+#define BMA400_LP_OSR_SHIFT         0x05
+#define BMA400_NP_OSR_SHIFT         0x04
+#define BMA400_SCALE_SHIFT          0x06  

I'm not sure why this is being defined as hex number instead of plain decimal...

Sounds good.

Quoted 7 lines

drivers/iio/accel/bma400.h line 76

Quoted 5 lines
drivers/iio/accel/bma400.h new file +5 -0
+#define BMA400_TWO_BITS_MASK        GENMASK(1, 0)
+#define BMA400_LP_OSR_MASK          GENMASK(6, BMA400_LP_OSR_SHIFT)
+#define BMA400_NP_OSR_MASK          GENMASK(5, BMA400_NP_OSR_SHIFT)
+#define BMA400_ACC_ODR_MASK         GENMASK(3, 0)
+#define BMA400_ACC_SCALE_MASK       GENMASK(7, BMA400_SCALE_SHIFT)  

And here simple better to put same numbers. It will help to read.

Quoted 1 line

Do you mean for the shift or for the mask?

SHIFTs -> plain decimals

Quoted 10 lines
Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(bma400_regmap_config);

I'm not sure I got the idea why this one is being exported.

It needs to be exported so that it can be used in the bma400_i2c module and the future bma400_spi module. In theory, if we _really_ do not want to export this, then we can define separate regmap configs in each of the bma400_i2c and (future) bma400_spi modules, but then we would have to export the is_volitile_reg and is_writable_reg functions. As a result, I do not see any benefits to that method over exporting the config, but I could be convinced otherwise.

Quoted 3 lines

I think there might be better way to do this.
But I leave it to you and maintainer to agree on (I will be fine with any solution you will come to).

This does always feel a bit silly. We have plenty of cases of both the suggested options (replicate vs export). I don't really care either way.

Quoted 50 lines
Quoted 23 lines
Quoted 23 lines

drivers/iio/accel/bma400_core.c line 311

Quoted 9 lines
drivers/iio/accel/bma400_core.c new file +7 -0
+               if (uhz || hz % BMA400_ACC_ODR_MIN_WHOLE_HZ)
+                       return -EINVAL;
+
+               val = hz / BMA400_ACC_ODR_MIN_WHOLE_HZ;
+               idx = __ffs(val);
+  
 
+               if (val ^ BIT(idx))  

Seems like funny way of checking is_power_of_2(). But it's up to maintainers. And your variant may even be better here (in code generation perspective)...

However, the whole idea here is, IIUC, to have something like

hz = 2^idx * BMA400_ACC_ODR_MIN_WHOLE_HZ

I think you may do it without divisions, i.e. call __ffs() first and then do

   idx = __ffs(...);
   val = hz >> idx;
   if (val != BMA400_ACC_ODR_MIN_WHOLE_HZ)
    return -EINVAL;
Quoted 5 lines
Quoted 1 line

or something like above.

It would be more obvious what is being done here with is_power_of_two. I'll revisit this function with your suggestions. If I can make it simpler, I'll go this route.

The main point here to get rid of divisions. Is it achievable?

Quoted 19 lines
Quoted 9 lines
Quoted 1 line

+ return -EINVAL;

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       ret = regmap_read(data->regmap, BMA400_ACC_CONFIG0_REG, &val);
+       if (ret < 0)  

I'm wondering if in all of these regmap_read()...

Quoted 7 lines

drivers/iio/accel/bma400_core.c line 531

Quoted 5 lines
drivers/iio/accel/bma400_core.c new file +4 -0
+               return ret;  
 
+       ret = regmap_write(data->regmap, BMA400_ACC_CONFIG0_REG,
+                          mode | (val & ~BMA400_TWO_BITS_MASK));
+       if (ret < 0) {  

...and regmap_write() calls you ever can get a positive returned code.

From the regmap_read/regmap_write docs:

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

* A value of zero will be returned on success, a negative errno will
* be returned in error cases.

So I assume ret <= 0

There is no positive codes mentioned at all. And you assume right. But why we care about positive codes if they never can be returned?

Agreed, for regmap calls, definitely prefer the driver to check with if (ret)

	...
Quoted 1 line
#11 lore raw

Re: [PATCH v6 2/2] iio: (bma400) add driver for the BMA400

In reply to Dan Robertson

Hi Dan,

thanks for your patch!

Inspired by my recent work to revive the BMA180/BMA250 driver for BMA254 I have some comments:

Quoted 6 lines · Dan Robertson

Add a IIO driver for the Bosch BMA400 3-axes ultra-low power accelerometer. The driver supports reading from the acceleration and temperature registers. The driver also supports reading and configuring the output data rate, oversampling ratio, and scale.

(...)

drivers/iio/accel/bma400_core.c line 21

Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +8 -0
+#include <linux/bitops.h>
+#include <linux/device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/regmap.h>

#include <linux/regulator/consumer.h>

Quoted 1 line

+int bma400_probe(struct device *dev, struct regmap *regmap, const char *name)

Even if the component is always powered on in your design please add regulator handling already from start so that we don't have to go in and add it later. I've added regulator support to sooo many IIO drivers by now.

You can pretty much just copy/paste my code for BMA180/BMA250:
https://lore.kernel.org/linux-iio/[email protected]/T/#u

Yours,
Linus Walleij