Skip to contentSkip to conversation

iio: add driver for Bosch BMA400 accelerometer

PATCH Superseded Dan Robertson 6 participants 13 messages Oct 18, 2019 – Nov 24, 2019

Subsystems (3)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Dan Robertson

author maintainer
#0 lore raw

v3 cover iio: add driver for Bosch BMA400 accelerometer

This patchset adds a IIO driver for the Bosch BMA400 3-axes ultra low-power accelerometer. The initial implementation of the driver adds read support for the acceleration and temperature data registers. The driver also has support for reading and writing to the output data rate, oversampling ratio, and scale configuration registers.

I've ran some basic tests with libiio and I've run the device tree checks. Are there other tests that are recommended to be run for a new device?

Thanks again for the reviews!

Cheers,

- Dan

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

 .../devicetree/bindings/iio/accel/bma400.yaml |  39 +
 drivers/iio/accel/Kconfig                     |  18 +
 drivers/iio/accel/Makefile                    |   2 +
 drivers/iio/accel/bma400.h                    |  85 ++
 drivers/iio/accel/bma400_core.c               | 796 ++++++++++++++++++
 drivers/iio/accel/bma400_i2c.c                |  60 ++
 6 files changed, 1000 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/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 v4 1/2 dt-bindings: iio: accel: bma400: add bindings

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

---
 .../devicetree/bindings/iio/accel/bma400.yaml | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/bma400.yaml
Documentation/devicetree/bindings/iio/accel/bma400.yaml new file +39 -0
new file mode 100644
index 000000000000..e0a85dc7bf34
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/accel/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:
+  - |
+    i2c0 {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      bma400@14 {
+        compatible = "bosch,bma400";
+        reg = <0x14>;
+      };
+    };
maintainer
#2 lore raw

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

Quoted 8 lines · Dan Robertson

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

---
 .../devicetree/bindings/iio/accel/bma400.yaml | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/bma400.yaml

bosch,bma400.yaml

Quoted 29 lines
Documentation/devicetree/bindings/iio/accel/bma400.yaml new file +22 -0
new file mode 100644
index 000000000000..e0a85dc7bf34
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/accel/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
+

Documentation/devicetree/bindings/iio/accel/bma400.yaml line 32

Quoted 10 lines
Documentation/devicetree/bindings/iio/accel/bma400.yaml new file +10 -0
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+examples:
+  - |
+    i2c0 {

i2c {

(will enable i2c bus schema)

Documentation/devicetree/bindings/iio/accel/bma400.yaml line 35

Quoted 3 lines
Documentation/devicetree/bindings/iio/accel/bma400.yaml new file +3 -0
+      #address-cells = <1>;
+      #size-cells = <0>;
+      bma400@14 {

accelerometer@14

Quoted 6 lines
Documentation/devicetree/bindings/iio/accel/bma400.yaml new file +4 -0
+        compatible = "bosch,bma400";
+        reg = <0x14>;
+      };
+    };
author maintainer
#3 lore raw

PATCH v4 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       |  18 +
 drivers/iio/accel/Makefile      |   2 +
 drivers/iio/accel/bma400.h      |  85 ++++
 drivers/iio/accel/bma400_core.c | 796 ++++++++++++++++++++++++++++++++
 drivers/iio/accel/bma400_i2c.c  |  60 +++
 5 files changed, 961 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 +18 -0
index 9b9656ce37e6..a1081b902d16 100644
@@ -112,6 +112,24 @@ 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"
+	depends on I2C
+	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 for I2C.
+
+config BMA400_I2C
+	tristate
+	depends on BMA400
+	select REGMAP_I2C
+
 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 +85 -0

85 lines not on this page · raw message

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

796 lines not on this page · raw message

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

60 lines not on this page · raw message

#4 lore raw

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

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.

Hi,
I'm repeating some comments from my V2 review.

Quoted 16 lines
---
 drivers/iio/accel/Kconfig       |  18 +
 drivers/iio/accel/Makefile      |   2 +
 drivers/iio/accel/bma400.h      |  85 ++++
 drivers/iio/accel/bma400_core.c | 796 ++++++++++++++++++++++++++++++++
 drivers/iio/accel/bma400_i2c.c  |  60 +++
 5 files changed, 961 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 +0 -0
index 9b9656ce37e6..a1081b902d16 100644

drivers/iio/accel/Kconfig config BMA220 · line 119

Quoted 8 lines
drivers/iio/accel/Kconfig +5 -0
@@ -112,6 +112,24 @@ 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"
+	depends on I2C
+	select REGMAP
+	select BMA400_I2C if (I2C)

Since BMA400 already depends on I2C, the "if (I2C)" above is redundant so it's not needed.

Quoted 77 lines
drivers/iio/accel/Kconfig +13 -0
+	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 for I2C.
+
+config BMA400_I2C
+	tristate
+	depends on BMA400
+	select REGMAP_I2C
+
 config BMC150_ACCEL
 	tristate "Bosch BMC150 Accelerometer Driver"
 	select IIO_BUFFER
drivers/iio/accel/bma400_core.c new file +53 -0
new file mode 100644
index 000000000000..80f1ee6713fa
@@ -0,0 +1,796 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * bma400_core.c - Core IIO driver for Bosch BMA400 triaxial acceleration
+ *                 sensor. Used by bma400-i2c.
+ *
+ * Copyright 2019 Dan Robertson <[email protected]>
+ *
+ * TODO:
+ *  - Support for power management
+ *  - Support events and interrupts
+ *  - Create channel the step count
+ *  - Create channel for sensor time
+ */
+
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/bitops.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#include "bma400.h"
+
+/*
+ * The G-range selection may be one of 2g, 4g, 8, or 16g. The scale may
+ * be selected with the acc_range bits of the ACC_CONFIG1 register.
+ */
+static const int bma400_scale_table[] = {
+	0, 38344,
+	0, 76590,
+	0, 153277,
+	0, 306457
+};
+
+static const int bma400_osr_table[] = { 0, 1, 3 };
+
+/* See the ACC_CONFIG1 section of the datasheet */
+static const int bma400_sample_freqs[] = {
+	12,  500000,
+	25,  0,
+	50,  0,
+	100, 0,
+	200, 0,
+	400, 0,
+	800, 0,
+};
+
+/* See the ACC_CONFIG0 section of the datasheet */
+enum bma400_power_mode {
+	POWER_MODE_SLEEP   = 0x00,
+	POWER_MODE_LOW     = 0x01,
+	POWER_MODE_NORMAL  = 0x02,
+	POWER_MODE_INVALID = 0x03,

drivers/iio/accel/bma400_core.c line 63

Quoted 10 lines
drivers/iio/accel/bma400_core.c new file +10 -0
+};
+
+struct bma400_sample_freq {
+	int hz;
+	int uhz;
+};
+
+struct bma400_data {
+	struct device *dev;
+	struct mutex mutex; /* data register lock */

needs #include <linux/mutex.h>

Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +8 -0
+	struct iio_mount_matrix orientation;
+	struct regmap *regmap;
+	enum bma400_power_mode power_mode;
+	struct bma400_sample_freq sample_freq;
+	int oversampling_ratio;
+	int scale;
+};
+

[snip]

drivers/iio/accel/bma400_core.c line 456

Quoted 5 lines
drivers/iio/accel/bma400_core.c new file +5 -0
+static int bma400_get_accel_scale_idx(struct bma400_data *data, int val)
+{
+	int i;
+
+	for (i = 1; i < ARRAY_SIZE(bma400_scale_table); i += 2) {

needs #include <linux/kernel.h>
for ARRAY_SIZE()

drivers/iio/accel/bma400_core.c line 461

Quoted 5 lines
drivers/iio/accel/bma400_core.c new file +5 -0
+		if (bma400_scale_table[i] == val)
+			return i - 1;
+	}
+	return -EINVAL;
+}

[snip]

Thanks.

Signature · 2 lines
~Randy
See Documentation/process/submit-checklist.rst, especially Rule #1.
author maintainer
#5 lore raw

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

Quoted 9 lines · Randy Dunlap

drivers/iio/accel/Kconfig config BMA220 · line 119

Quoted 5 lines · Dan Robertson
drivers/iio/accel/Kconfig +5 -0
+config BMA400
+	tristate "Bosch BMA400 3-Axis Accelerometer Driver"
+	depends on I2C
+	select REGMAP
+	select BMA400_I2C if (I2C)

Since BMA400 already depends on I2C, the "if (I2C)" above is redundant so it's not needed.

You are correct. I'll remove the depends on I2C.

Quoted 5 lines

drivers/iio/accel/bma400_core.c line 63

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+struct bma400_data {
+	struct device *dev;
+	struct mutex mutex; /* data register lock */

needs #include <linux/mutex.h>

Good point.

Quoted 8 lines

drivers/iio/accel/bma400_core.c line 456

Quoted 5 lines
drivers/iio/accel/bma400_core.c new file +5 -0
+static int bma400_get_accel_scale_idx(struct bma400_data *data, int val)
+{
+	int i;
+
+	for (i = 1; i < ARRAY_SIZE(bma400_scale_table); i += 2) {

needs #include <linux/kernel.h>
for ARRAY_SIZE()

Thanks

Cheers,

- Dan

#6 lore raw

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

In reply to Dan Robertson
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.

Thanks for an update, my comments below.

drivers/iio/accel/bma400.h line 4

Quoted 4 lines
drivers/iio/accel/bma400.h new file +2 -0
@@ -0,0 +1,85 @@
 
+ * bma400.h - Register constants and other forward declarations
+ *            needed by the bma400 sources.

Including file name in the file is not the best practice. Imagine if by some reason we will need to rename it (to support more sensors, for example, and reflect it by replacing 00 -> 0x).
So, please, remove here and everywhere else.

drivers/iio/accel/bma400.h line 71

Quoted 9 lines

+ *
+ * Copyright 2019 Dan Robertson <[email protected]>
+ */

drivers/iio/accel/bma400.h new file +5 -0
+#define BMA400_TWO_BITS_MASK    0x03
+#define BMA400_LP_OSR_MASK      0x60
+#define BMA400_NP_OSR_MASK      0x30
+#define BMA400_ACC_ODR_MASK     0x0f
+#define BMA400_ACC_SCALE_MASK   0xc0

GENMASK()
(Don't forget to include bits.h for it)

Quoted 1 line

+extern const struct regmap_config bma400_regmap_config;

I'm not sure, why you need this exposed.

Quoted 2 lines

+ * bma400_core.c - Core IIO driver for Bosch BMA400 triaxial acceleration
+ * sensor. Used by bma400-i2c.

No name.

drivers/iio/accel/bma400_core.c line 32

Quoted 10 lines
drivers/iio/accel/bma400_core.c new file +9 -0
+/*
+ * The G-range selection may be one of 2g, 4g, 8, or 16g. The scale may
+ * be selected with the acc_range bits of the ACC_CONFIG1 register.
+ */
+static const int bma400_scale_table[] = {
+       0, 38344,
+       0, 76590,
+       0, 153277,
 
+       0, 306457

Better to leave comma here. It doesn't matter for this device, but make of use the better practices.

Quoted 1 line

+};

Also, I'm wondering why values are not exactly multiply by 2. Is in DS of the chip any explanation for this?

drivers/iio/accel/bma400_core.c line 46

Quoted 12 lines
drivers/iio/accel/bma400_core.c new file +11 -0
+static const int bma400_osr_table[] = { 0, 1, 3 };
 
+/* See the ACC_CONFIG1 section of the datasheet */
+static const int bma400_sample_freqs[] = {
+       12,  500000,
+       25,  0,
+       50,  0,
+       100, 0,
+       200, 0,
+       400, 0,
+       800, 0,
+};

This can be replaced by a formula(s).

drivers/iio/accel/bma400_core.c line 59

Quoted 4 lines
drivers/iio/accel/bma400_core.c new file +4 -0
+struct bma400_sample_freq {
+       int hz;
+       int uhz;
+};

I'm wondering why above table is not using this struct.

drivers/iio/accel/bma400_core.c line 145

Quoted 10 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);

Why? And why it's not _GPL?

drivers/iio/accel/bma400_core.c line 190

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+       int ret;
+       int host_temp;
+       unsigned int raw_temp;

Better reversed xmas tree order.

drivers/iio/accel/bma400_core.c line 196

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +2 -0
+       ret = regmap_read(data->regmap, BMA400_TEMP_DATA_REG, &raw_temp);
 
+

Redundant blank line..

drivers/iio/accel/bma400_core.c line 271

Quoted 4 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+       if (ret < 0)
+               return ret;
 
+               odr = (val & BMA400_ACC_ODR_MASK);

Too many parentheses.

drivers/iio/accel/bma400_core.c line 279

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +2 -0
+               idx = (odr - BMA400_ACC_ODR_MIN) * 2;
 
+

Redundant.

Quoted 1 line

+ if (idx + 1 >= ARRAY_SIZE(bma400_sample_freqs)) {

Why do you need this churn with +1 and = ?

drivers/iio/accel/bma400_core.c line 311

Quoted 7 lines
drivers/iio/accel/bma400_core.c new file +5 -0
+                       dev_err(data->dev, "sample freq index is too high");
+                       ret = -EINVAL;
+                       goto error;
+               }
 
 
+       for (i = 0; i + 1 < ARRAY_SIZE(bma400_sample_freqs); i += 2) {

Using defined struct will guarantee you to have always 2x members in the array. So, drop this arithmetic churn.

Quoted 1 line

+ if (i + 1 >= ARRAY_SIZE(bma400_sample_freqs))

As a bit above.

drivers/iio/accel/bma400_core.c line 270

Quoted 5 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+               return -EINVAL;
 
+       ret = regmap_read(data->regmap, BMA400_ACC_CONFIG1_REG, &val);
 
+

Redundant.

drivers/iio/accel/bma400_core.c line 443

Quoted 4 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+       if (ret < 0)
+               return ret;
 
+       idx = (((val & BMA400_ACC_SCALE_MASK) >> BMA400_SCALE_SHIFT) * 2) + 1;

Too many parentheses.

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (idx >= ARRAY_SIZE(bma400_scale_table))
+               return -EINVAL;

Why + 1 above and = here?
You may do + 1 below.

drivers/iio/accel/bma400_core.c line 474

Quoted 6 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+       data->scale = bma400_scale_table[idx];
 
 
+       idx = bma400_get_accel_scale_idx(data, val);
 
+

Redundant

drivers/iio/accel/bma400_core.c line 523

Quoted 9 lines
drivers/iio/accel/bma400_core.c new file +6 -0
+       if (idx < 0)
+               return idx;
 
 
+       /* Preserve the low-power oversample ratio etc */
+       ret = regmap_write(data->regmap, BMA400_ACC_CONFIG0_REG,
+                          mode | (val & ~BMA400_TWO_BITS_MASK));
 
+

Redundant

Quoted 5 lines
drivers/iio/accel/bma400_core.c new file +4 -0
+       if (ret < 0) {
+               dev_err(data->dev, "Failed to write to power-mode");
+               return ret;
+       }
 

drivers/iio/accel/bma400_core.c line 550

Quoted 10 lines
drivers/iio/accel/bma400_core.c new file +10 -0
+static int bma400_init(struct bma400_data *data)
+{
+       int ret;
+       unsigned int val;
+
+       /* Try to read chip_id register. It must return 0x90. */
+       ret = regmap_read(data->regmap, BMA400_CHIP_ID_REG, &val);
+
+       if (ret < 0) {
+               dev_err(data->dev, "Failed to read chip id register: %x!", ret);

%x for returned error code is too hackerish.

drivers/iio/accel/bma400_core.c line 552

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +2 -0
+               return ret;
 
+       } else if (val != BMA400_ID_REG_VAL) {

Redundant 'else'

Quoted 1 line

+ dev_err(data->dev, "CHIP ID MISMATCH: %x!", ret);

Hacker detected!

drivers/iio/accel/bma400_core.c line 573

Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +7 -0
+               return -ENODEV;
+       }
 
+               /*
+                * TODO: The datasheet waits 1500us here in the example, but
+                * lists 2/ODR as the wakeup time.
+                */
+               usleep_range(1500, 20000);

These range values are too sparse. Usually the second one is less than first one * 2.
Fix it now.

Quoted 1 line

+EXPORT_SYMBOL(bma400_probe);

Why is not GPL?

Quoted 1 line

+EXPORT_SYMBOL(bma400_remove);

Ditto.

P.S. I probably missed some places with the same mistake as commented above. Please address all places in the code where my comments are applicable.

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

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

Quoted 8 lines · Andy Shevchenko
Quoted 2 lines · Dan Robertson

+ * bma400.h - Register constants and other forward declarations
+ * needed by the bma400 sources.

Including file name in the file is not the best practice. Imagine if by some reason we will need to rename it (to support more sensors, for example, and reflect it by replacing 00 -> 0x).
So, please, remove here and everywhere else.

That makes sense.

Quoted 8 lines

drivers/iio/accel/bma400.h line 71

Quoted 5 lines
drivers/iio/accel/bma400.h new file +5 -0
+#define BMA400_TWO_BITS_MASK    0x03
+#define BMA400_LP_OSR_MASK      0x60
+#define BMA400_NP_OSR_MASK      0x30
+#define BMA400_ACC_ODR_MASK     0x0f
+#define BMA400_ACC_SCALE_MASK   0xc0

GENMASK()
(Don't forget to include bits.h for it)

Thanks.

Quoted 5 lines
Quoted 4 lines
drivers/iio/accel/bma400_core.c new file +4 -0
+static const int bma400_scale_table[] = {
+       0, 38344,
+       0, 76590,
+       0, 153277,
Quoted 8 lines
Quoted 1 line

+ 0, 306457

Better to leave comma here. It doesn't matter for this device, but make of use the better practices.

Quoted 1 line

+};

Also, I'm wondering why values are not exactly multiply by 2. Is in DS of the chip any explanation for this?

It would be a multiply by 2. I tried to follow the bma180 driver here, but I'm starting to think that may be the wrong approach.

Quoted 14 lines

drivers/iio/accel/bma400_core.c line 46

Quoted 12 lines
drivers/iio/accel/bma400_core.c new file +11 -0
+static const int bma400_osr_table[] = { 0, 1, 3 };
 
+/* See the ACC_CONFIG1 section of the datasheet */
+static const int bma400_sample_freqs[] = {
+       12,  500000,
+       25,  0,
+       50,  0,
+       100, 0,
+       200, 0,
+       400, 0,
+       800, 0,
+};

This can be replaced by a formula(s).

Yeah I think I can implement the get, set, and read functions for sample_freq with a formula, but the scale and sample frequency tables are needed by the implementation of read_avail. A implementation of read_avail with a range and a step would be ideal, but I couldn't find any documentation on implementing read_avail where the step value of the range is a multiple. Please correct me if I've missed something.

Note that this applies to the scale table as well.

Quoted 6 lines

drivers/iio/accel/bma400_core.c line 59

Quoted 4 lines
drivers/iio/accel/bma400_core.c new file +4 -0
+struct bma400_sample_freq {
+       int hz;
+       int uhz;
+};

I'm wondering why above table is not using this struct.

Originally it did, but I changed this in the second version when I added support for iio_info read_avail to try to be a little closer to other implementations of iio_read avail.

Quoted 9 lines
Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +8 -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,
+};
Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(bma400_regmap_config);

Why? And why it's not _GPL?

This is used by the bma400_i2c module.

Quoted 5 lines

drivers/iio/accel/bma400_core.c line 190

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+       int ret;
+       int host_temp;
+       unsigned int raw_temp;

Better reversed xmas tree order.

Sounds good.

Quoted 4 lines
Quoted 2 lines

+ if (idx + 1 >= ARRAY_SIZE(bma400_sample_freqs)) {

Why do you need this churn with +1 and = ?

Since we've "flattened" the array of sample frequency we need to ensure that the Hz (bma400_sample_freqs[idx]) and uHz (bma400_sample_freqs[idx + 1]) are both valid. This will be negated in the next version as I'll switch to a formula. Instead I'll ensure the returned ODR value is not above 0x0b.

Quoted 10 lines

drivers/iio/accel/bma400_core.c line 311

Quoted 7 lines
drivers/iio/accel/bma400_core.c new file +5 -0
+                       dev_err(data->dev, "sample freq index is too high");
+                       ret = -EINVAL;
+                       goto error;
+               }
 
 
+       for (i = 0; i + 1 < ARRAY_SIZE(bma400_sample_freqs); i += 2) {

Using defined struct will guarantee you to have always 2x members in the array. So, drop this arithmetic churn.

I should be able to figure out how to use a formula here, but I see where you're coming from and I agree.

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (ret < 0) {
+               dev_err(data->dev, "Failed to read chip id register: %x!", ret);

%x for returned error code is too hackerish.

Makes sense. I'll change this in the update.

Quoted 9 lines

drivers/iio/accel/bma400_core.c line 552

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +2 -0
+               return ret;
 
+       } else if (val != BMA400_ID_REG_VAL) {

Redundant 'else'

Quoted 1 line

+ dev_err(data->dev, "CHIP ID MISMATCH: %x!", ret);

Hacker detected!

:)

Quoted 9 lines

drivers/iio/accel/bma400_core.c line 573

Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +7 -0
+               return -ENODEV;
+       }
 
+               /*
+                * TODO: The datasheet waits 1500us here in the example, but
+                * lists 2/ODR as the wakeup time.
+                */
+               usleep_range(1500, 20000);
Quoted 3 lines

These range values are too sparse. Usually the second one is less than first one * 2.
Fix it now.

Good to know. I'll fix this in the update.

Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(bma400_probe);

Why is not GPL?

Ah, saw in the docs "GPL" means GPL-2.0.

Quoted 4 lines
Quoted 2 lines

+EXPORT_SYMBOL(bma400_remove);

Ditto.

This symbol is used in bma400_i2c.

Quoted 4 lines

P.S. I probably missed some places with the same mistake as commented above. Please address all places in the code where my comments are applicable.

Noted. Thanks for the feedback!

Cheers,

- Dan

maintainer
#8 lore raw

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

Quoted 32 lines · On Sat, 19 Oct 2019 02:43:51 +0000 Dan Robertson <[email protected]> wrote:
Quoted 8 lines · Andy Shevchenko
Quoted 2 lines · Dan Robertson

+ * bma400.h - Register constants and other forward declarations
+ * needed by the bma400 sources.

Including file name in the file is not the best practice. Imagine if by some reason we will need to rename it (to support more sensors, for example, and reflect it by replacing 00 -> 0x).
So, please, remove here and everywhere else.

That makes sense.

Quoted 8 lines

drivers/iio/accel/bma400.h line 71

Quoted 5 lines
drivers/iio/accel/bma400.h new file +5 -0
+#define BMA400_TWO_BITS_MASK    0x03
+#define BMA400_LP_OSR_MASK      0x60
+#define BMA400_NP_OSR_MASK      0x30
+#define BMA400_ACC_ODR_MASK     0x0f
+#define BMA400_ACC_SCALE_MASK   0xc0  

GENMASK()
(Don't forget to include bits.h for it)

Thanks.

Quoted 9 lines

drivers/iio/accel/bma400_core.c line 32

Quoted 6 lines
drivers/iio/accel/bma400_core.c new file +5 -0
+static const int bma400_scale_table[] = {
+       0, 38344,
+       0, 76590,
+       0, 153277,  
 
+       0, 306457  

Better to leave comma here. It doesn't matter for this device, but make of use the better practices.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+};

Also, I'm wondering why values are not exactly multiply by 2. Is in DS of the chip any explanation for this?

It would be a multiply by 2. I tried to follow the bma180 driver here, but I'm starting to think that may be the wrong approach.

I'll guess that, like so many accelerometers this one has documentation in terms of 'g'. Which is bonkers at g isn't a constant... g_0 is but no datasheet ever seems to note that distinction. Oh well. So these are rounded to nearest value in m/s^2... To be honest, the sensor noise levels are usual such that this level of precision really doesn't matter.

Quoted 16 lines
Quoted 15 lines

drivers/iio/accel/bma400_core.c line 46

Quoted 13 lines
drivers/iio/accel/bma400_core.c new file +11 -0
+static const int bma400_osr_table[] = { 0, 1, 3 };  
 
+/* See the ACC_CONFIG1 section of the datasheet */
+static const int bma400_sample_freqs[] = {
+       12,  500000,
+       25,  0,
+       50,  0,
+       100, 0,
+       200, 0,
+       400, 0,
+       800, 0,
+};  

This can be replaced by a formula(s).

Quoted 6 lines

Yeah I think I can implement the get, set, and read functions for sample_freq with a formula, but the scale and sample frequency tables are needed by the implementation of read_avail. A implementation of read_avail with a range and a step would be ideal, but I couldn't find any documentation on implementing read_avail where the step value of the range is a multiple. Please correct me if I've missed something.

Indeed. We've only defined it as being fixed intervals. I'm not keen to expand the options for the userspace interface any further.

You could compute the values at startup and store it in your state structure I think (or compute them on demand, but you'd need to have the space somewhere non volatile).

Quoted 23 lines

Note that this applies to the scale table as well.

Quoted 6 lines

drivers/iio/accel/bma400_core.c line 59

Quoted 4 lines
drivers/iio/accel/bma400_core.c new file +4 -0
+struct bma400_sample_freq {
+       int hz;
+       int uhz;
+};  

I'm wondering why above table is not using this struct.

Originally it did, but I changed this in the second version when I added support for iio_info read_avail to try to be a little closer to other implementations of iio_read avail.

Quoted 9 lines
Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +8 -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,
+};  
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(bma400_regmap_config);

Why? And why it's not _GPL?

This is used by the bma400_i2c module.

Which is licensed as GPL v2, so I don't follow why this isn't _GPL either.

Quoted 65 lines
Quoted 6 lines

drivers/iio/accel/bma400_core.c line 190

Quoted 4 lines
drivers/iio/accel/bma400_core.c new file +3 -0
+       int ret;
+       int host_temp;
+       unsigned int raw_temp;  

Better reversed xmas tree order.

Sounds good.

Quoted 4 lines
Quoted 2 lines

+ if (idx + 1 >= ARRAY_SIZE(bma400_sample_freqs)) {

Why do you need this churn with +1 and = ?

Since we've "flattened" the array of sample frequency we need to ensure that the Hz (bma400_sample_freqs[idx]) and uHz (bma400_sample_freqs[idx + 1]) are both valid. This will be negated in the next version as I'll switch to a formula. Instead I'll ensure the returned ODR value is not above 0x0b.

Quoted 10 lines

drivers/iio/accel/bma400_core.c line 311

Quoted 7 lines
drivers/iio/accel/bma400_core.c new file +5 -0
+                       dev_err(data->dev, "sample freq index is too high");
+                       ret = -EINVAL;
+                       goto error;
+               }  
 
 
+       for (i = 0; i + 1 < ARRAY_SIZE(bma400_sample_freqs); i += 2) {  

Using defined struct will guarantee you to have always 2x members in the array. So, drop this arithmetic churn.

I should be able to figure out how to use a formula here, but I see where you're coming from and I agree.

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (ret < 0) {
+               dev_err(data->dev, "Failed to read chip id register: %x!", ret);  

%x for returned error code is too hackerish.

Makes sense. I'll change this in the update.

Quoted 9 lines

drivers/iio/accel/bma400_core.c line 552

Quoted 3 lines
drivers/iio/accel/bma400_core.c new file +2 -0
+               return ret;  
 
+       } else if (val != BMA400_ID_REG_VAL) {  

Redundant 'else'

Quoted 1 line

+ dev_err(data->dev, "CHIP ID MISMATCH: %x!", ret);

Hacker detected!

:)

Quoted 9 lines

drivers/iio/accel/bma400_core.c line 573

Quoted 8 lines
drivers/iio/accel/bma400_core.c new file +7 -0
+               return -ENODEV;
+       }  
 
+               /*
+                * TODO: The datasheet waits 1500us here in the example, but
+                * lists 2/ODR as the wakeup time.
+                */
+               usleep_range(1500, 20000);  
Quoted 3 lines

These range values are too sparse. Usually the second one is less than first one * 2.
Fix it now.

Quoted 7 lines

Good to know. I'll fix this in the update.

Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(bma400_probe);

Why is not GPL?

Ah, saw in the docs "GPL" means GPL-2.0.

So guess you are fine with making these all GPL exports then :)

Quoted 13 lines
Quoted 5 lines
Quoted 3 lines

+EXPORT_SYMBOL(bma400_remove);

Ditto.

This symbol is used in bma400_i2c.

Quoted 4 lines

P.S. I probably missed some places with the same mistake as commented above. Please address all places in the code where my comments are applicable.

Quoted 5 lines

Noted. Thanks for the feedback!

Cheers,

- Dan

Thanks,

Jonathan.

author maintainer
#9 lore raw

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

Sorry for the incredibly late reply. Before I submit the next patchset version, I have a question from the last set of reviews.

Quoted 26 lines · Jonathan Cameron
Quoted 17 lines · On Sat, 19 Oct 2019 02:43:51 +0000 Dan Robertson <[email protected]> wrote:
Quoted 15 lines · Andy Shevchenko

drivers/iio/accel/bma400_core.c line 46

Quoted 12 lines · Dan Robertson
drivers/iio/accel/bma400_core.c new file +11 -0
+static const int bma400_osr_table[] = { 0, 1, 3 };  
 
+/* See the ACC_CONFIG1 section of the datasheet */
+static const int bma400_sample_freqs[] = {
+       12,  500000,
+       25,  0,
+       50,  0,
+       100, 0,
+       200, 0,
+       400, 0,
+       800, 0,
+};  

This can be replaced by a formula(s).

Quoted 6 lines

Yeah I think I can implement the get, set, and read functions for sample_freq with a formula, but the scale and sample frequency tables are needed by the implementation of read_avail. A implementation of read_avail with a range and a step would be ideal, but I couldn't find any documentation on implementing read_avail where the step value of the range is a multiple. Please correct me if I've missed something.

Quoted 8 lines

Indeed. We've only defined it as being fixed intervals. I'm not keen to expand the options for the userspace interface any further.

You could compute the values at startup and store it in your state structure I think (or compute them on demand, but you'd need to have the space somewhere non volatile).

I ended up writing an implementation that uses a formula for the get/set functions of the sample frequency and scale, but uses a table for the implementation of the read_avail function. While it does work, I worry that this makes the driver less maintainable and would make it harder to add support for a new hypothetical future BMA4xx device. Also, the majority of drivers seem to use a table for the raw value to user input conversion, so a move from this might make the code less "familiar".

If we do stick with the translation table, would it be better to have two tables (a translation table and a read_avail table) so that we do not have a step distance of two? This would mean we would need to maintain two tables, but would simplify the code.

Random workflow question:

The sampling ratio, frequency, etc code seems to be the most complicated part of the driver. Is it typically recommended to upstream a more minimal driver that might assume the defaults?

Cheers,

- Dan

maintainer
#10 lore raw

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

Quoted 47 lines · On Mon, 18 Nov 2019 00:25:04 +0000 Dan Robertson <[email protected]> wrote:

Sorry for the incredibly late reply. Before I submit the next patchset version, I have a question from the last set of reviews.

Quoted 26 lines · Jonathan Cameron
Quoted 17 lines · On Sat, 19 Oct 2019 02:43:51 +0000 Dan Robertson <[email protected]> wrote:
Quoted 15 lines · Andy Shevchenko

drivers/iio/accel/bma400_core.c line 46

Quoted 12 lines · Dan Robertson
drivers/iio/accel/bma400_core.c new file +11 -0
+static const int bma400_osr_table[] = { 0, 1, 3 };    
 
+/* See the ACC_CONFIG1 section of the datasheet */
+static const int bma400_sample_freqs[] = {
+       12,  500000,
+       25,  0,
+       50,  0,
+       100, 0,
+       200, 0,
+       400, 0,
+       800, 0,
+};    

This can be replaced by a formula(s).

Quoted 6 lines

Yeah I think I can implement the get, set, and read functions for sample_freq with a formula, but the scale and sample frequency tables are needed by the implementation of read_avail. A implementation of read_avail with a range and a step would be ideal, but I couldn't find any documentation on implementing read_avail where the step value of the range is a multiple. Please correct me if I've missed something.

Quoted 8 lines

Indeed. We've only defined it as being fixed intervals. I'm not keen to expand the options for the userspace interface any further.

You could compute the values at startup and store it in your state structure I think (or compute them on demand, but you'd need to have the space somewhere non volatile).

I ended up writing an implementation that uses a formula for the get/set functions of the sample frequency and scale, but uses a table for the implementation of the read_avail function. While it does work, I worry that this makes the driver less maintainable and would make it harder to add support for a new hypothetical future BMA4xx device. Also, the majority of drivers seem to use a table for the raw value to user input conversion, so a move from this might make the code less "familiar".

Quoted 4 lines

If we do stick with the translation table, would it be better to have two tables (a translation table and a read_avail table) so that we do not have a step distance of two? This would mean we would need to maintain two tables, but would simplify the code.

If a function is your preferred route you could also just use it to compute the values for the available table at startup?

Otherwise, its fine to just use a table for both.

Quoted 6 lines

Random workflow question:

The sampling ratio, frequency, etc code seems to be the most complicated part of the driver. Is it typically recommended to upstream a more minimal driver that might assume the defaults?

Often people upstream a first version that just uses defaults, then follow up (if they care) with later series adding the more fiddly elements.

Sometimes those more fiddly bits never come as a particular author never needed them. That's absolutely fine. It's a rare driver that supports all the features on a non trivial device!

Thanks,

Jonathan

Quoted 5 lines

Cheers,

- Dan

author maintainer
#11 lore raw

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

Quoted 2 lines · Jonathan Cameron

If a function is your preferred route you could also just use it to compute the values for the available table at startup?

Yeah that makes sense. I'll add that in the next patchset version.

Quoted 10 lines
Quoted 3 lines

The sampling ratio, frequency, etc code seems to be the most complicated part of the driver. Is it typically recommended to upstream a more minimal driver that might assume the defaults?

Often people upstream a first version that just uses defaults, then follow up (if they care) with later series adding the more fiddly elements.

Sometimes those more fiddly bits never come as a particular author never needed them. That's absolutely fine. It's a rare driver that supports all the features on a non trivial device!

Makes sense. I'll likely add some extra bits in a follow-up patchset, so I can learn a bit more.

Cheers,

- Dan

#12 lore raw

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

In reply to Dan Robertson
Quoted 4 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.

trivial logging note:

drivers/iio/accel/bma400_core.c

Quoted 1 line
drivers/iio/accel/bma400_core.c +0 -0

[]

drivers/iio/accel/bma400_core.c line 213

Quoted 4 lines
drivers/iio/accel/bma400_core.c +4 -0
+static int bma400_get_accel_reg(struct bma400_data *data,
+				const struct iio_chan_spec *chan,
+				int *val)
+{

[]

drivers/iio/accel/bma400_core.c line 232

Quoted 1 line
drivers/iio/accel/bma400_core.c +1 -0
+		dev_err(data->dev, "invalid axis channel modifier");

All the logging should use \n terminations

		dev_err(data->dev, "invalid axis channel modifier\n");

[]

drivers/iio/accel/bma400_core.c

Quoted 2 lines
drivers/iio/accel/bma400_core.c +2 -0
+static int bma400_get_accel_output_data_rate(struct bma400_data *data)
+{

[]

drivers/iio/accel/bma400_core.c line 273

Quoted 1 line
drivers/iio/accel/bma400_core.c +1 -0
+			dev_err(data->dev, "invalid ODR=%x", odr);

should be:

			dev_err(data->dev, "invalid ODR=%x\n", odr);

etc...