Skip to contentSkip to conversation

iio: accel: add support for ADXL355

PATCH In review Puranjay Mohan 4 participants 11 messages Aug 11, 2021 – Aug 19, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v12 cover iio: accel: add support for ADXL355

Add the dt-bindings and the driver for ADXL355 3-axis MEMS Accelerometer.

Changes since v11:
1. Add forward declaration of struct device in header.
2. Use defines for power-of-10s.

Changes since v10:
1. Use macros for powers-of-10 in fill_3db_frequency_tbl().
2. Change names of gotos.
3. add commas to non-terminal lines.

Changes since v9:
1. Fix white space issues, remove extra lines.
2. Include bits.h and device.h
3. Add commas to non-terminal lines in structs etc.

Changes since v8:
1. Make scale and offset defines inline and remove them.
2. Change dt-binding doc to state interrupt polarity only for DRDY pin.
3. Remove triggered buffer support from this patch series.

Changes since v7:
1. Update MAINTAINERS to show all driver files.
2. Set CONFIGS for buffered support in Kconfig.

Changes since v6:
1. Use interrupt-names property in device tree document.
2. Add triggered buffer support.
3. Use a static table for offset and data registers.
4. Fix coding style issues.
5. move defines from header to c file.

Changes since v5:
1. Used get_unaligned_be24() and get_unaligned_be16() to parse acceleration and temperature data. This solves sparse errors and also make the code more understandable.

Changes since v4:
1. Fix errors reported by sparse.

Changes since v3:
1. Fix errors in yaml DT doc.
2. Change SPDX-License-Identifier to GPL-2.0-only OR BSD-2-Clause

Changes since v2:
1. Add separate DT binding doc in yaml.
2. Use ____cacheline_aligned buffer for regmap_bulk_read/write calls.
3. Make code consistent by using same style in switch case.
4. Use FIELD_PREP in place of custom macros.
5. Make Kconfig description more informative.

Changes since v1:
1. Remove the declarations for static regmap structures from adxl355.h. This was missed in the v1 and caused errors.
2. Make switch case statements consistent by directly returning from each case rather than saving the return in a variable.
3. Some coding style changes.

Changes since v0:
1. Move adxl355_hpf_3db_table to adxl355_data structure. This is done to make sure that each device gets its own table.
2. Make local regmap definitions private to adxl355_core.c.
3. Other minor coding style changes.

Puranjay Mohan (2):
dt-bindings: iio: accel: Add DT binding doc for ADXL355
iio: accel: Add driver support for ADXL355

 .../bindings/iio/accel/adi,adxl355.yaml       |  88 +++
 MAINTAINERS                                   |  10 +
 drivers/iio/accel/Kconfig                     |  29 +
 drivers/iio/accel/Makefile                    |   3 +
 drivers/iio/accel/adxl355.h                   |  21 +
 drivers/iio/accel/adxl355_core.c              | 584 ++++++++++++++++++
 drivers/iio/accel/adxl355_i2c.c               |  62 ++
 drivers/iio/accel/adxl355_spi.c               |  65 ++
 8 files changed, 862 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml
 create mode 100644 drivers/iio/accel/adxl355.h
 create mode 100644 drivers/iio/accel/adxl355_core.c
 create mode 100644 drivers/iio/accel/adxl355_i2c.c
 create mode 100644 drivers/iio/accel/adxl355_spi.c
Signature · 1 line
2.30.1
#1 lore raw

PATCH v12 1/2 dt-bindings: iio: accel: Add DT binding doc for ADXL355

Add devicetree binding document for ADXL355, a 3-Axis MEMS Accelerometer.

---
 .../bindings/iio/accel/adi,adxl355.yaml       | 88 +++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml
Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml new file +88 -0
new file mode 100644
index 000000000..ba54d6998
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/accel/adi,adxl355.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices ADXL355 3-Axis, Low noise MEMS Accelerometer
+
+maintainers:
+  - Puranjay Mohan <[email protected]>
+
+description: |
+  Analog Devices ADXL355 3-Axis, Low noise MEMS Accelerometer that supports
+  both I2C & SPI interfaces
+    https://www.analog.com/en/products/adxl355.html
+
+properties:
+  compatible:
+    enum:
+      - adi,adxl355
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    minItems: 1
+    maxItems: 3
+    description: |
+      Type for DRDY should be IRQ_TYPE_EDGE_RISING.
+      Three configurable interrupt lines exist.
+
+  interrupt-names:
+    description: Specify which interrupt line is in use.
+    items:
+      enum:
+        - INT1
+        - INT2
+        - DRDY
+    minItems: 1
+    maxItems: 3
+
+  vdd-supply:
+    description: Regulator that provides power to the sensor
+
+  vddio-supply:
+    description: Regulator that provides power to the bus
+
+  spi-max-frequency: true
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+        #include <dt-bindings/gpio/gpio.h>
+        #include <dt-bindings/interrupt-controller/irq.h>
+        i2c {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                /* Example for a I2C device node */
+                accelerometer@1d {
+                        compatible = "adi,adxl355";
+                        reg = <0x1d>;
+                        interrupt-parent = <&gpio>;
+                        interrupts = <25 IRQ_TYPE_EDGE_RISING>;
+                        interrupt-names = "DRDY";
+                };
+        };
+  - |
+        #include <dt-bindings/gpio/gpio.h>
+        #include <dt-bindings/interrupt-controller/irq.h>
+        spi {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                accelerometer@0 {
+                        compatible = "adi,adxl355";
+                        reg = <0>;
+                        spi-max-frequency = <1000000>;
+                        interrupt-parent = <&gpio>;
+                        interrupts = <25 IRQ_TYPE_EDGE_RISING>;
+                        interrupt-names = "DRDY";
+                };
+        };
Signature · 1 line
2.30.1
#2 lore raw

Re: [PATCH v12 1/2] dt-bindings: iio: accel: Add DT binding doc for ADXL355

+cc Rob Herring <[email protected]>

Quoted 106 lines · Puranjay Mohan

Add devicetree binding document for ADXL355, a 3-Axis MEMS Accelerometer.

---
 .../bindings/iio/accel/adi,adxl355.yaml       | 88 +++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml
Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml new file +88 -1
new file mode 100644
index 000000000..ba54d6998
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/accel/adi,adxl355.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices ADXL355 3-Axis, Low noise MEMS Accelerometer
+
+maintainers:
+  - Puranjay Mohan <[email protected]>
+
+description: |
+  Analog Devices ADXL355 3-Axis, Low noise MEMS Accelerometer that supports
+  both I2C & SPI interfaces
+    https://www.analog.com/en/products/adxl355.html
+
+properties:
+  compatible:
+    enum:
+      - adi,adxl355
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    minItems: 1
+    maxItems: 3
+    description: |
+      Type for DRDY should be IRQ_TYPE_EDGE_RISING.
+      Three configurable interrupt lines exist.
+
+  interrupt-names:
+    description: Specify which interrupt line is in use.
+    items:
+      enum:
+        - INT1
+        - INT2
+        - DRDY
+    minItems: 1
+    maxItems: 3
+
+  vdd-supply:
+    description: Regulator that provides power to the sensor
+
+  vddio-supply:
+    description: Regulator that provides power to the bus
+
+  spi-max-frequency: true
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+        #include <dt-bindings/gpio/gpio.h>
+        #include <dt-bindings/interrupt-controller/irq.h>
+        i2c {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                /* Example for a I2C device node */
+                accelerometer@1d {
+                        compatible = "adi,adxl355";
+                        reg = <0x1d>;
+                        interrupt-parent = <&gpio>;
+                        interrupts = <25 IRQ_TYPE_EDGE_RISING>;
+                        interrupt-names = "DRDY";
+                };
+        };
+  - |
+        #include <dt-bindings/gpio/gpio.h>
+        #include <dt-bindings/interrupt-controller/irq.h>
+        spi {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                accelerometer@0 {
+                        compatible = "adi,adxl355";
+                        reg = <0>;
+                        spi-max-frequency = <1000000>;
+                        interrupt-parent = <&gpio>;
+                        interrupts = <25 IRQ_TYPE_EDGE_RISING>;
+                        interrupt-names = "DRDY";
+                };
+        };
--

2.30.1

Signature · 5 lines
Thanks and Regards

Yours Truly,

Puranjay Mohan
maintainer Reviewed-by on v12 1/2
#3 lore raw

Re: [PATCH v12 1/2] dt-bindings: iio: accel: Add DT binding doc for ADXL355

In reply to Puranjay Mohan
Quoted 8 lines · Puranjay Mohan

Add devicetree binding document for ADXL355, a 3-Axis MEMS Accelerometer.

---
 .../bindings/iio/accel/adi,adxl355.yaml       | 88 +++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml
#4 lore raw

PATCH v12 2/2 iio: accel: Add driver support for ADXL355

In reply to Puranjay Mohan

ADXL355 is a 3-axis MEMS Accelerometer. It offers low noise density, low 0g offset drift, low power with selectable measurement ranges. It also features programmable high-pass and low-pass filters.

Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/adxl354_adxl355.pdf

---
 MAINTAINERS                      |  10 +
 drivers/iio/accel/Kconfig        |  29 ++
 drivers/iio/accel/Makefile       |   3 +
 drivers/iio/accel/adxl355.h      |  21 ++
 drivers/iio/accel/adxl355_core.c | 584 +++++++++++++++++++++++++++++++
 drivers/iio/accel/adxl355_i2c.c  |  62 ++++
 drivers/iio/accel/adxl355_spi.c  |  65 ++++
 7 files changed, 774 insertions(+)
 create mode 100644 drivers/iio/accel/adxl355.h
 create mode 100644 drivers/iio/accel/adxl355_core.c
 create mode 100644 drivers/iio/accel/adxl355_i2c.c
 create mode 100644 drivers/iio/accel/adxl355_spi.c
MAINTAINERS +10 -0
index bd7aff0c1..104d22c83 100644
@@ -586,6 +586,16 @@ W:	http://ez.analog.com/community/linux-device-drivers
 F:	Documentation/devicetree/bindings/iio/accel/adi,adxl345.yaml
 F:	drivers/input/misc/adxl34x.c
 
+ADXL355 THREE-AXIS DIGITAL ACCELEROMETER DRIVER
+M:	Puranjay Mohan <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml
+F:	drivers/iio/accel/adxl355.h
+F:	drivers/iio/accel/adxl355_core.c
+F:	drivers/iio/accel/adxl355_i2c.c
+F:	drivers/iio/accel/adxl355_spi.c
+
 ADXL372 THREE-AXIS DIGITAL ACCELEROMETER DRIVER
 M:	Michael Hennerich <[email protected]>
 S:	Supported
drivers/iio/accel/Kconfig +29 -0
index cceda3cec..d0c45c809 100644
@@ -61,6 +61,35 @@ config ADXL345_SPI
 	  will be called adxl345_spi and you will also get adxl345_core
 	  for the core module.
 
+config ADXL355
+	tristate
+
+config ADXL355_I2C
+	tristate "Analog Devices ADXL355 3-Axis Digital Accelerometer I2C Driver"
+	depends on I2C
+	select ADXL355
+	select REGMAP_I2C
+	help
+	  Say Y here if you want to build i2c support for the Analog Devices
+	  ADXL355 3-axis digital accelerometer.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called adxl355_i2c and you will also get adxl355_core
+	  for the core module.
+
+config ADXL355_SPI
+	tristate "Analog Devices ADXL355 3-Axis Digital Accelerometer SPI Driver"
+	depends on SPI
+	select ADXL355
+	select REGMAP_SPI
+	help
+	  Say Y here if you want to build spi support for the Analog Devices
+	  ADXL355 3-axis digital accelerometer.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called adxl355_spi and you will also get adxl355_core
+	  for the core module.
+
 config ADXL372
 	tristate
 	select IIO_BUFFER
drivers/iio/accel/Makefile +3 -0
index 32cd1342a..0e4721d2d 100644
@@ -9,6 +9,9 @@ obj-$(CONFIG_ADIS16209) += adis16209.o
 obj-$(CONFIG_ADXL345) += adxl345_core.o
 obj-$(CONFIG_ADXL345_I2C) += adxl345_i2c.o
 obj-$(CONFIG_ADXL345_SPI) += adxl345_spi.o
+obj-$(CONFIG_ADXL355) += adxl355_core.o
+obj-$(CONFIG_ADXL355_I2C) += adxl355_i2c.o
+obj-$(CONFIG_ADXL355_SPI) += adxl355_spi.o
 obj-$(CONFIG_ADXL372) += adxl372.o
 obj-$(CONFIG_ADXL372_I2C) += adxl372_i2c.o
 obj-$(CONFIG_ADXL372_SPI) += adxl372_spi.o
drivers/iio/accel/adxl355.h new file +21 -0
new file mode 100644
index 000000000..6dd49b13e
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * ADXL355 3-Axis Digital Accelerometer
+ *
+ * Copyright (c) 2021 Puranjay Mohan <[email protected]>
+ */
+
+#ifndef _ADXL355_H_
+#define _ADXL355_H_
+
+#include <linux/regmap.h>
+
+struct device;
+
+extern const struct regmap_access_table adxl355_readable_regs_tbl;
+extern const struct regmap_access_table adxl355_writeable_regs_tbl;
+
+int adxl355_core_probe(struct device *dev, struct regmap *regmap,
+		       const char *name);
+
+#endif /* _ADXL355_H_ */
drivers/iio/accel/adxl355_core.c new file +584 -0

584 lines not on this page · raw message

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

62 lines not on this page · raw message

drivers/iio/accel/adxl355_spi.c new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.30.1
maintainer
#5 lore raw

Re: [PATCH v12 2/2] iio: accel: Add driver support for ADXL355

Quoted 8 lines · On Wed, 11 Aug 2021 13:00:27 +0530 Puranjay Mohan <[email protected]> wrote:

ADXL355 is a 3-axis MEMS Accelerometer. It offers low noise density, low 0g offset drift, low power with selectable measurement ranges. It also features programmable high-pass and low-pass filters.

Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/adxl354_adxl355.pdf

Hi Puranjay,

I took one last look at this so I can apply it without looking again assuming the dt review is fine. Noticed one issue with error handling, but I can tidy that up whilst applying assuming you aren't doing a v13 for some other reason. If you are please incorporate these changes as well.

Thanks,

Jonathan

...

Quoted 16 lines
drivers/iio/accel/adxl355_core.c new file +16 -0
+
+static int adxl355_set_odr(struct adxl355_data *data,
+			   enum adxl355_odr odr)
+{
+	int ret = 0;
+
+	mutex_lock(&data->lock);
+
+	if (data->odr == odr)
+		goto out_unlock;
+
+	ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
+	if (ret < 0)
+		goto out_unlock;
+
+	ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,

drivers/iio/accel/adxl355_core.c line 308

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+				 ADXL355_FILTER_ODR_MSK,
+				 FIELD_PREP(ADXL355_FILTER_ODR_MSK, odr));
+	if (ret < 0)
+		goto out_unlock;
+
+	data->odr = odr;
+	adxl355_fill_3db_frequency_table(data);
+
+out_unlock:
+	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);

As below, we should do this because it risks returning success when a failure actually occured. Again, unless you are respinning for some other reason I'll add the logic whilst applying.

Quoted 16 lines
drivers/iio/accel/adxl355_core.c new file +16 -0
+	mutex_unlock(&data->lock);
+	return ret;
+}
+
+static int adxl355_set_hpf_3db(struct adxl355_data *data,
+			       enum adxl355_hpf_3db hpf)
+{
+	int ret = 0;
+
+	mutex_lock(&data->lock);
+
+	if (data->hpf_3db == hpf)
+		goto unlock;
+
+	ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
+	if (ret < 0)

drivers/iio/accel/adxl355_core.c line 334

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+		goto set_opmode_unlock;
+
+	ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
+				 ADXL355_FILTER_HPF_MSK,
+				 FIELD_PREP(ADXL355_FILTER_HPF_MSK, hpf));
+	if (!ret)
+		data->hpf_3db = hpf;
+
+set_opmode_unlock:
+	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);

We can't do this as it might potentially eat an error that meant the regmap update didn't occur. To avoid that a little dance is needed using a second return value and we only set ret = ret2 if ret == 0

Alternatively we just have a separate error handling path which doesn't set ret for the adxl355_set_op_mode(). I'll probably go with that as it's more code but easier to read.

Quoted 5 lines
drivers/iio/accel/adxl355_core.c new file +5 -0
+unlock:
+	mutex_unlock(&data->lock);
+	return ret;
+}
+

...

Quoted 9 lines
drivers/iio/accel/adxl355_core.c new file +9 -0
+static int adxl355_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2, long mask)
+{
+	struct adxl355_data *data = iio_priv(indio_dev);
+	int odr_idx, hpf_idx, calibbias;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:

drivers/iio/accel/adxl355_core.c line 460

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+		odr_idx = adxl355_find_match(adxl355_odr_table,
+					     ARRAY_SIZE(adxl355_odr_table),
+					     val, val2);
+		if (odr_idx < 0)
+			return odr_idx;
+
+		return adxl355_set_odr(data, odr_idx);
+	case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
+		hpf_idx = adxl355_find_match(data->adxl355_hpf_3db_table,
+					ARRAY_SIZE(data->adxl355_hpf_3db_table),

Mixing different indentation styles isn't very nice for readability. I'll tweak this whilst applying.

Quoted 13 lines
drivers/iio/accel/adxl355_core.c new file +13 -0
+					     val, val2);
+		if (hpf_idx < 0)
+			return hpf_idx;
+
+		return adxl355_set_hpf_3db(data, hpf_idx);
+	case IIO_CHAN_INFO_CALIBBIAS:
+		calibbias = clamp_t(int, val, S16_MIN, S16_MAX);
+
+		return adxl355_set_calibbias(data, chan->address, calibbias);
+	default:
+		return -EINVAL;
+	}
+}

...

#6 lore raw

Re: [PATCH v12 2/2] iio: accel: Add driver support for ADXL355

Quoted 12 lines · Jonathan Cameron
Quoted 8 lines · On Wed, 11 Aug 2021 13:00:27 +0530 Puranjay Mohan <[email protected]> wrote:

ADXL355 is a 3-axis MEMS Accelerometer. It offers low noise density, low 0g offset drift, low power with selectable measurement ranges. It also features programmable high-pass and low-pass filters.

Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/adxl354_adxl355.pdf

Quoted 7 lines

Hi Puranjay,

I took one last look at this so I can apply it without looking again assuming the dt review is fine. Noticed one issue with error handling, but I can tidy that up whilst applying assuming you aren't doing a v13 for some other reason. If you are please incorporate these changes as well.

Hi Jonathan, It would be great if you could make these changes while applying. I am not doing a v13 as all comments have been covered earlier.
I shall be thankful to you.

Quoted 120 lines

Thanks,

Jonathan

...

Quoted 16 lines
drivers/iio/accel/adxl355_core.c new file +16 -0
+
+static int adxl355_set_odr(struct adxl355_data *data,
+                        enum adxl355_odr odr)
+{
+     int ret = 0;
+
+     mutex_lock(&data->lock);
+
+     if (data->odr == odr)
+             goto out_unlock;
+
+     ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
+     if (ret < 0)
+             goto out_unlock;
+
+     ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,

drivers/iio/accel/adxl355_core.c line 308

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+                              ADXL355_FILTER_ODR_MSK,
+                              FIELD_PREP(ADXL355_FILTER_ODR_MSK, odr));
+     if (ret < 0)
+             goto out_unlock;
+
+     data->odr = odr;
+     adxl355_fill_3db_frequency_table(data);
+
+out_unlock:
+     ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);

As below, we should do this because it risks returning success when a failure actually occured. Again, unless you are respinning for some other reason I'll add the logic whilst applying.

Quoted 16 lines
drivers/iio/accel/adxl355_core.c new file +16 -0
+     mutex_unlock(&data->lock);
+     return ret;
+}
+
+static int adxl355_set_hpf_3db(struct adxl355_data *data,
+                            enum adxl355_hpf_3db hpf)
+{
+     int ret = 0;
+
+     mutex_lock(&data->lock);
+
+     if (data->hpf_3db == hpf)
+             goto unlock;
+
+     ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
+     if (ret < 0)

drivers/iio/accel/adxl355_core.c line 334

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+             goto set_opmode_unlock;
+
+     ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
+                              ADXL355_FILTER_HPF_MSK,
+                              FIELD_PREP(ADXL355_FILTER_HPF_MSK, hpf));
+     if (!ret)
+             data->hpf_3db = hpf;
+
+set_opmode_unlock:
+     ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);

We can't do this as it might potentially eat an error that meant the regmap update didn't occur. To avoid that a little dance is needed using a second return value and we only set ret = ret2 if ret == 0

Alternatively we just have a separate error handling path which doesn't set ret for the adxl355_set_op_mode(). I'll probably go with that as it's more code but easier to read.

Quoted 5 lines
drivers/iio/accel/adxl355_core.c new file +5 -0
+unlock:
+     mutex_unlock(&data->lock);
+     return ret;
+}
+

...

Quoted 9 lines
drivers/iio/accel/adxl355_core.c new file +9 -0
+static int adxl355_write_raw(struct iio_dev *indio_dev,
+                          struct iio_chan_spec const *chan,
+                          int val, int val2, long mask)
+{
+     struct adxl355_data *data = iio_priv(indio_dev);
+     int odr_idx, hpf_idx, calibbias;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_SAMP_FREQ:

drivers/iio/accel/adxl355_core.c line 460

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+             odr_idx = adxl355_find_match(adxl355_odr_table,
+                                          ARRAY_SIZE(adxl355_odr_table),
+                                          val, val2);
+             if (odr_idx < 0)
+                     return odr_idx;
+
+             return adxl355_set_odr(data, odr_idx);
+     case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
+             hpf_idx = adxl355_find_match(data->adxl355_hpf_3db_table,
+                                     ARRAY_SIZE(data->adxl355_hpf_3db_table),

Mixing different indentation styles isn't very nice for readability. I'll tweak this whilst applying.

Quoted 13 lines
drivers/iio/accel/adxl355_core.c new file +13 -0
+                                          val, val2);
+             if (hpf_idx < 0)
+                     return hpf_idx;
+
+             return adxl355_set_hpf_3db(data, hpf_idx);
+     case IIO_CHAN_INFO_CALIBBIAS:
+             calibbias = clamp_t(int, val, S16_MIN, S16_MAX);
+
+             return adxl355_set_calibbias(data, chan->address, calibbias);
+     default:
+             return -EINVAL;
+     }
+}

...

Signature · 5 lines
Thanks and Regards

Yours Truly,

Puranjay Mohan
maintainer
#7 lore raw

Re: [PATCH v12 2/2] iio: accel: Add driver support for ADXL355

Quoted 21 lines · On Sun, 15 Aug 2021 21:59:42 +0530 Puranjay Mohan <[email protected]> wrote:
Quoted 12 lines · Jonathan Cameron
Quoted 8 lines · On Wed, 11 Aug 2021 13:00:27 +0530 Puranjay Mohan <[email protected]> wrote:

ADXL355 is a 3-axis MEMS Accelerometer. It offers low noise density, low 0g offset drift, low power with selectable measurement ranges. It also features programmable high-pass and low-pass filters.

Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/adxl354_adxl355.pdf

Quoted 7 lines

Hi Puranjay,

I took one last look at this so I can apply it without looking again assuming the dt review is fine. Noticed one issue with error handling, but I can tidy that up whilst applying assuming you aren't doing a v13 for some other reason. If you are please incorporate these changes as well.

Quoted 4 lines

Hi Jonathan, It would be great if you could make these changes while applying. I am not doing a v13 as all comments have been covered earlier.
I shall be thankful to you.

I ended up tweaking those 3 functions a little more than I was originally planning so please check it all looks good to you.

Series applied to the togreg branch of iio.git and pushed out as testing for 0-day to poke at. Note these won't hit mainline until 5.16 now.

Thanks,

Jonathan

Quoted 124 lines
Quoted 121 lines

Thanks,

Jonathan

...

Quoted 16 lines
drivers/iio/accel/adxl355_core.c new file +16 -0
+
+static int adxl355_set_odr(struct adxl355_data *data,
+                        enum adxl355_odr odr)
+{
+     int ret = 0;
+
+     mutex_lock(&data->lock);
+
+     if (data->odr == odr)
+             goto out_unlock;
+
+     ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
+     if (ret < 0)
+             goto out_unlock;
+
+     ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,

drivers/iio/accel/adxl355_core.c line 308

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+                              ADXL355_FILTER_ODR_MSK,
+                              FIELD_PREP(ADXL355_FILTER_ODR_MSK, odr));
+     if (ret < 0)
+             goto out_unlock;
+
+     data->odr = odr;
+     adxl355_fill_3db_frequency_table(data);
+
+out_unlock:
+     ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);  

As below, we should do this because it risks returning success when a failure actually occured. Again, unless you are respinning for some other reason I'll add the logic whilst applying.

Quoted 16 lines
drivers/iio/accel/adxl355_core.c new file +16 -0
+     mutex_unlock(&data->lock);
+     return ret;
+}
+
+static int adxl355_set_hpf_3db(struct adxl355_data *data,
+                            enum adxl355_hpf_3db hpf)
+{
+     int ret = 0;
+
+     mutex_lock(&data->lock);
+
+     if (data->hpf_3db == hpf)
+             goto unlock;
+
+     ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
+     if (ret < 0)

drivers/iio/accel/adxl355_core.c line 334

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+             goto set_opmode_unlock;
+
+     ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
+                              ADXL355_FILTER_HPF_MSK,
+                              FIELD_PREP(ADXL355_FILTER_HPF_MSK, hpf));
+     if (!ret)
+             data->hpf_3db = hpf;
+
+set_opmode_unlock:
+     ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);  

We can't do this as it might potentially eat an error that meant the regmap update didn't occur. To avoid that a little dance is needed using a second return value and we only set ret = ret2 if ret == 0

Alternatively we just have a separate error handling path which doesn't set ret for the adxl355_set_op_mode(). I'll probably go with that as it's more code but easier to read.

Quoted 5 lines
drivers/iio/accel/adxl355_core.c new file +5 -0
+unlock:
+     mutex_unlock(&data->lock);
+     return ret;
+}
+  

...

Quoted 9 lines
drivers/iio/accel/adxl355_core.c new file +9 -0
+static int adxl355_write_raw(struct iio_dev *indio_dev,
+                          struct iio_chan_spec const *chan,
+                          int val, int val2, long mask)
+{
+     struct adxl355_data *data = iio_priv(indio_dev);
+     int odr_idx, hpf_idx, calibbias;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_SAMP_FREQ:

drivers/iio/accel/adxl355_core.c line 460

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+             odr_idx = adxl355_find_match(adxl355_odr_table,
+                                          ARRAY_SIZE(adxl355_odr_table),
+                                          val, val2);
+             if (odr_idx < 0)
+                     return odr_idx;
+
+             return adxl355_set_odr(data, odr_idx);
+     case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
+             hpf_idx = adxl355_find_match(data->adxl355_hpf_3db_table,
+                                     ARRAY_SIZE(data->adxl355_hpf_3db_table),  

Mixing different indentation styles isn't very nice for readability. I'll tweak this whilst applying.

Quoted 13 lines
drivers/iio/accel/adxl355_core.c new file +13 -0
+                                          val, val2);
+             if (hpf_idx < 0)
+                     return hpf_idx;
+
+             return adxl355_set_hpf_3db(data, hpf_idx);
+     case IIO_CHAN_INFO_CALIBBIAS:
+             calibbias = clamp_t(int, val, S16_MIN, S16_MAX);
+
+             return adxl355_set_calibbias(data, chan->address, calibbias);
+     default:
+             return -EINVAL;
+     }
+}  

...

#8 lore raw

Re: [PATCH v12 2/2] iio: accel: Add driver support for ADXL355

Quoted 32 lines · Jonathan Cameron
Quoted 21 lines · On Sun, 15 Aug 2021 21:59:42 +0530 Puranjay Mohan <[email protected]> wrote:
Quoted 12 lines · Jonathan Cameron
Quoted 8 lines · On Wed, 11 Aug 2021 13:00:27 +0530 Puranjay Mohan <[email protected]> wrote:

ADXL355 is a 3-axis MEMS Accelerometer. It offers low noise density, low 0g offset drift, low power with selectable measurement ranges. It also features programmable high-pass and low-pass filters.

Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/adxl354_adxl355.pdf

Quoted 7 lines

Hi Puranjay,

I took one last look at this so I can apply it without looking again assuming the dt review is fine. Noticed one issue with error handling, but I can tidy that up whilst applying assuming you aren't doing a v13 for some other reason. If you are please incorporate these changes as well.

Quoted 4 lines

Hi Jonathan, It would be great if you could make these changes while applying. I am not doing a v13 as all comments have been covered earlier.
I shall be thankful to you.

I ended up tweaking those 3 functions a little more than I was originally planning so please check it all looks good to you.

Quoted 7 lines

Series applied to the togreg branch of iio.git and pushed out as testing for 0-day to poke at. Note these won't hit mainline until 5.16 now.

Thanks,

Jonathan

Hi Jonathan,
the changes you made look good to me.

Thanks,

Quoted 125 lines
Quoted 124 lines
Quoted 121 lines

Thanks,

Jonathan

...

Quoted 16 lines
drivers/iio/accel/adxl355_core.c new file +16 -0
+
+static int adxl355_set_odr(struct adxl355_data *data,
+                        enum adxl355_odr odr)
+{
+     int ret = 0;
+
+     mutex_lock(&data->lock);
+
+     if (data->odr == odr)
+             goto out_unlock;
+
+     ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
+     if (ret < 0)
+             goto out_unlock;
+
+     ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,

drivers/iio/accel/adxl355_core.c line 308

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+                              ADXL355_FILTER_ODR_MSK,
+                              FIELD_PREP(ADXL355_FILTER_ODR_MSK, odr));
+     if (ret < 0)
+             goto out_unlock;
+
+     data->odr = odr;
+     adxl355_fill_3db_frequency_table(data);
+
+out_unlock:
+     ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);

As below, we should do this because it risks returning success when a failure actually occured. Again, unless you are respinning for some other reason I'll add the logic whilst applying.

Quoted 16 lines
drivers/iio/accel/adxl355_core.c new file +16 -0
+     mutex_unlock(&data->lock);
+     return ret;
+}
+
+static int adxl355_set_hpf_3db(struct adxl355_data *data,
+                            enum adxl355_hpf_3db hpf)
+{
+     int ret = 0;
+
+     mutex_lock(&data->lock);
+
+     if (data->hpf_3db == hpf)
+             goto unlock;
+
+     ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
+     if (ret < 0)

drivers/iio/accel/adxl355_core.c line 334

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+             goto set_opmode_unlock;
+
+     ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
+                              ADXL355_FILTER_HPF_MSK,
+                              FIELD_PREP(ADXL355_FILTER_HPF_MSK, hpf));
+     if (!ret)
+             data->hpf_3db = hpf;
+
+set_opmode_unlock:
+     ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);

We can't do this as it might potentially eat an error that meant the regmap update didn't occur. To avoid that a little dance is needed using a second return value and we only set ret = ret2 if ret == 0

Alternatively we just have a separate error handling path which doesn't set ret for the adxl355_set_op_mode(). I'll probably go with that as it's more code but easier to read.

Quoted 5 lines
drivers/iio/accel/adxl355_core.c new file +5 -0
+unlock:
+     mutex_unlock(&data->lock);
+     return ret;
+}
+

...

Quoted 9 lines
drivers/iio/accel/adxl355_core.c new file +9 -0
+static int adxl355_write_raw(struct iio_dev *indio_dev,
+                          struct iio_chan_spec const *chan,
+                          int val, int val2, long mask)
+{
+     struct adxl355_data *data = iio_priv(indio_dev);
+     int odr_idx, hpf_idx, calibbias;
+
+     switch (mask) {
+     case IIO_CHAN_INFO_SAMP_FREQ:

drivers/iio/accel/adxl355_core.c line 460

Quoted 10 lines
drivers/iio/accel/adxl355_core.c new file +10 -0
+             odr_idx = adxl355_find_match(adxl355_odr_table,
+                                          ARRAY_SIZE(adxl355_odr_table),
+                                          val, val2);
+             if (odr_idx < 0)
+                     return odr_idx;
+
+             return adxl355_set_odr(data, odr_idx);
+     case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
+             hpf_idx = adxl355_find_match(data->adxl355_hpf_3db_table,
+                                     ARRAY_SIZE(data->adxl355_hpf_3db_table),

Mixing different indentation styles isn't very nice for readability. I'll tweak this whilst applying.

Quoted 13 lines
drivers/iio/accel/adxl355_core.c new file +13 -0
+                                          val, val2);
+             if (hpf_idx < 0)
+                     return hpf_idx;
+
+             return adxl355_set_hpf_3db(data, hpf_idx);
+     case IIO_CHAN_INFO_CALIBBIAS:
+             calibbias = clamp_t(int, val, S16_MIN, S16_MAX);
+
+             return adxl355_set_calibbias(data, chan->address, calibbias);
+     default:
+             return -EINVAL;
+     }
+}

...

Signature · 5 lines
Thanks and Regards

Yours Truly,

Puranjay Mohan
#9 lore raw
In reply to Puranjay Mohan
Quoted 7 lines · Puranjay Mohan

Add the dt-bindings and the driver for ADXL355 3-axis MEMS Accelerometer.

Changes since v11:
1. Add forward declaration of struct device in header.

2. Use defines for power-of-10s.

TWIMC, Wolfram applied patch against units.h to add the SI prefixes:
https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git/commit/?id=26471d4a6cf8d5d0bd0fb55c7169de7d67cc703a

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Puranjay Mohan

Add the dt-bindings and the driver for ADXL355 3-axis MEMS Accelerometer.

Quoted 8 lines
Quoted 4 lines

Changes since v11:
1. Add forward declaration of struct device in header.

2. Use defines for power-of-10s.

TWIMC, Wolfram applied patch against units.h to add the SI prefixes:
https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git/commit/?id=26471d4a6cf8d5d0bd0fb55c7169de7d67cc703a

I will change to this once this patch gets merged.

Quoted 3 lines

--
With Best Regards,
Andy Shevchenko

Signature · 5 lines
Thanks and Regards

Yours Truly,

Puranjay Mohan