Skip to contentSkip to conversation

iio: accel: add support for ADXL355

PATCH Superseded Puranjay Mohan 2 participants 6 messages Aug 9, 2021 – Aug 10, 2021

Subsystems (3)

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v9 cover iio: accel: add support for ADXL355

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

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):
iio: accel: Add driver support for ADXL355
iio: accel: adxl355: Add triggered buffer support

 MAINTAINERS                      |  10 +
 drivers/iio/accel/Kconfig        |  33 ++
 drivers/iio/accel/Makefile       |   3 +
 drivers/iio/accel/adxl355.h      |  19 +
 drivers/iio/accel/adxl355_core.c | 676 +++++++++++++++++++++++++++++++
 drivers/iio/accel/adxl355_i2c.c  |  65 +++
 drivers/iio/accel/adxl355_spi.c  |  67 +++
 7 files changed, 873 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
Signature · 1 line
2.30.1
#1 lore raw

PATCH v9 1/2 iio: accel: Add driver support for ADXL355

ADXL355 is 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      |  19 +
 drivers/iio/accel/adxl355_core.c | 578 +++++++++++++++++++++++++++++++
 drivers/iio/accel/adxl355_i2c.c  |  64 ++++
 drivers/iio/accel/adxl355_spi.c  |  67 ++++
 7 files changed, 770 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..704e70b86 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:	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
+F:	Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml
+
 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 +19 -0
new file mode 100644
index 000000000..322b0abb8
@@ -0,0 +1,19 @@
+/* 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>
+
+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 +578 -0

578 lines not on this page · raw message

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

64 lines not on this page · raw message

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

67 lines not on this page · raw message

Signature · 1 line
2.30.1
#2 lore raw

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

Quoted 2 lines · Puranjay Mohan

ADXL355 is 3-axis MEMS Accelerometer. It offers low noise density,

is a 3-axis

Quoted 2 lines

low 0g offset drift, low power with selectable measurement ranges. It also features programmable high-pass and low-pass filters.

...

MAINTAINERS W: http://ez.analog.com/community/linux-device-drivers · line 597

Quoted 5 lines
MAINTAINERS +5 -0
+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
+F:     Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml

Have you run checkpatch?

...

drivers/iio/accel/adxl355.h line 11

Quoted 4 lines
drivers/iio/accel/adxl355.h new file +4 -0
+#ifndef _ADXL355_H_
+#define _ADXL355_H_
+
+#include <linux/regmap.h>

Missed declaration for struct device.

Review

Quoted 2 lines
(quoted diff) +2 -0
+extern const struct regmap_access_table adxl355_readable_regs_tbl;
+

I think you may drop this blank line.

drivers/iio/accel/adxl355.h line 18

Quoted 4 lines
drivers/iio/accel/adxl355.h new file +4 -0
+extern const struct regmap_access_table adxl355_writeable_regs_tbl;
+
+int adxl355_core_probe(struct device *dev, struct regmap *regmap,
+                      const char *name);

+ blank line?

Quoted 1 line

+#endif /* _ADXL355_H_ */

...

Quoted 1 line

+#include <asm/unaligned.h>

asm/* is less generic, can you move it after linux/*?

Quoted 1 line

+#include <linux/bitfield.h>

Does this imply bits.h? If not, add the latter.

Quoted 6 lines
drivers/iio/accel/adxl355_core.c new file +6 -0
+#include <linux/iio/iio.h>
+#include <linux/limits.h>
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/regmap.h>

...

drivers/iio/accel/adxl355_core.c line 58

Quoted 3 lines
drivers/iio/accel/adxl355_core.c new file +3 -0
+static const struct regmap_range adxl355_read_reg_range[] = {
+       regmap_reg_range(ADXL355_DEVID_AD_REG, ADXL355_FIFO_DATA_REG),
+       regmap_reg_range(ADXL355_OFFSET_X_H_REG, ADXL355_SELF_TEST_REG)

Leave commas in non-terminator lines.

Quoted 1 line

+};

...

drivers/iio/accel/adxl355_core.c line 68

Quoted 2 lines
drivers/iio/accel/adxl355_core.c new file +2 -0
+static const struct regmap_range adxl355_write_reg_range[] = {
+       regmap_reg_range(ADXL355_OFFSET_X_H_REG, ADXL355_RESET_REG)

Ditto.

Quoted 1 line

+};

...

drivers/iio/accel/adxl355_core.c line 80

Quoted 3 lines
drivers/iio/accel/adxl355_core.c new file +2 -0
+enum adxl355_op_mode {
 
+       ADXL355_TEMP_OFF

Ditto.

drivers/iio/accel/adxl355_core.c line 94

Quoted 5 lines
drivers/iio/accel/adxl355_core.c new file +4 -0
+};
+
+enum adxl355_odr {
 
+       ADXL355_ODR_3_906HZ

Ditto.

drivers/iio/accel/adxl355_core.c line 104

Quoted 5 lines
drivers/iio/accel/adxl355_core.c new file +4 -0
+};
+
+enum adxl355_hpf_3db {
 
+       ADXL355_HPF_0_0238

Ditto.

drivers/iio/accel/adxl355_core.c line 118

Quoted 5 lines
drivers/iio/accel/adxl355_core.c new file +4 -0
+};
+
+static const int adxl355_odr_table[][2] = {
 
+       [10] = {3, 906000}

Ditto.

drivers/iio/accel/adxl355_core.c line 128

Quoted 5 lines
drivers/iio/accel/adxl355_core.c new file +4 -0
+};
+
+static const int adxl355_hpf_3db_multipliers[] = {
 
+       238

Ditto.

drivers/iio/accel/adxl355_core.c line 132

Quoted 4 lines
drivers/iio/accel/adxl355_core.c new file +4 -0
+};
+
+enum adxl355_chans {
+       chan_x, chan_y, chan_z

Ditto.

drivers/iio/accel/adxl355_core.c line 152

Quoted 8 lines
drivers/iio/accel/adxl355_core.c new file +6 -0
+};
 
+static const struct adxl355_chan_info adxl355_chans[] = {
 
+       [chan_z] = {
+               .data_reg = ADXL355_ZDATA3_REG,
+               .offset_reg = ADXL355_OFFSET_Z_H_REG
+       }

Ditto.

Quoted 1 line

+};

To avoid adding extra entries, consider using static_assert():s.

...

drivers/iio/accel/adxl355_core.c line 190

Quoted 6 lines
drivers/iio/accel/adxl355_core.c new file +6 -0
+static void adxl355_fill_3db_frequency_table(struct adxl355_data *data)
+{
+       int i;
+       u64 rem;
+       u64 div;
+       u32 multiplier;

Reversed xmas tree order?

drivers/iio/accel/adxl355_core.c line 192

Quoted 2 lines
drivers/iio/accel/adxl355_core.c new file +2 -0
+       u64 odr = mul_u64_u32_shr(adxl355_odr_table[data->odr][0], 1000000, 0) +
+                                 adxl355_odr_table[data->odr][1];

Split definition and assignment.

drivers/iio/accel/adxl355_core.c line 201

Quoted 8 lines
drivers/iio/accel/adxl355_core.c new file +8 -0
+       for (i = 0; i < ARRAY_SIZE(adxl355_hpf_3db_multipliers); i++) {
+               multiplier = adxl355_hpf_3db_multipliers[i];
+               div = div64_u64_rem(mul_u64_u32_shr(odr, multiplier, 0),
+                                   100000000000000UL, &rem);
+
+               data->adxl355_hpf_3db_table[i][0] = div;
+               data->adxl355_hpf_3db_table[i][1] = div_u64(rem, 100000000);
+       }

Do all those power-of-ten constants have a meaning? Shouldn't it be better to use named definitions?

Quoted 1 line

+}

...

drivers/iio/accel/adxl355_core.c line 238

Quoted 3 lines
drivers/iio/accel/adxl355_core.c new file +3 -0
+       /*
+        * Perform a software reset to make sure the device is in a consistent
+        * state after start up.

start-up

Quoted 1 line

+ */

...

Quoted 1 line

+ ret = regmap_bulk_read(data->regmap, addr, data->transf_buf, 3);

ARRAY_SIZE() ?

Quoted 2 lines
(quoted diff) +2 -0
+       if (ret < 0)
+               return ret;

...

drivers/iio/accel/adxl355_core.c line 399

Quoted 8 lines
drivers/iio/accel/adxl355_core.c new file +8 -0
+               /*
+                * The datasheet defines an intercept of 1885 LSB at 25 degC
+                * and a slope of -9.05 LSB/C. The following formula can be used
+                * to find the temperature:
+                * Temp = ((RAW - 1885)/(-9.05)) + 25 but this doesn't follow
+                * the format of the IIO which is Temp = (RAW + OFFSET) * SCALE.
+                * Hence using some rearranging we get the scale as -110.497238
+                * and offset as -2111.25

Missed period.

Quoted 1 line

+ */

...

drivers/iio/accel/adxl355_core.c line 407

Quoted 3 lines
drivers/iio/accel/adxl355_core.c new file +3 -0
+               /*
+                * At +/- 2g with 20-bit resolution, scale is given in datasheet
+                * as 3.9ug/LSB = 0.0000039 * 9.80665 = 0.00003824593 m/s^2

Ditto for all multi-line comments.

Quoted 1 line

+ */

...

drivers/iio/accel/adxl355_i2c.c line 20

Quoted 6 lines
drivers/iio/accel/adxl355_i2c.c new file +6 -0
+static const struct regmap_config adxl355_i2c_regmap_config = {
+       .reg_bits = 8,
+       .val_bits = 8,
+       .max_register = 0x2F,
+       .rd_table = &adxl355_readable_regs_tbl,
+       .wr_table = &adxl355_writeable_regs_tbl

+ comma

Quoted 1 line

+};

...

drivers/iio/accel/adxl355_i2c.c line 30

Quoted 4 lines
drivers/iio/accel/adxl355_i2c.c new file +4 -0
+       regmap = devm_regmap_init_i2c(client, &adxl355_i2c_regmap_config);
+       if (IS_ERR(regmap)) {
+               dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
+                       PTR_ERR(regmap));

One line?

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

...

drivers/iio/accel/adxl355_i2c.c line 41

Quoted 6 lines
drivers/iio/accel/adxl355_i2c.c new file +5 -0
+static const struct i2c_device_id adxl355_i2c_id[] = {
+       { "adxl355", 0 },
+       { }
+};
 
+

Redundant blank line.

drivers/iio/accel/adxl355_i2c.c line 48

Quoted 8 lines
drivers/iio/accel/adxl355_i2c.c new file +7 -0
+MODULE_DEVICE_TABLE(i2c, adxl355_i2c_id);
+
+static const struct of_device_id adxl355_of_match[] = {
+       { .compatible = "adi,adxl355" },
+       { }
+};
 
+

Ditto.

drivers/iio/accel/adxl355_i2c.c line 59

Quoted 12 lines
drivers/iio/accel/adxl355_i2c.c new file +11 -0
+MODULE_DEVICE_TABLE(of, adxl355_of_match);
+
+static struct i2c_driver adxl355_i2c_driver = {
+       .driver = {
+               .name   = "adxl355_i2c",
+               .of_match_table = adxl355_of_match,
+       },
+       .probe_new      = adxl355_i2c_probe,
+       .id_table       = adxl355_i2c_id,
+};
 
+

Ditto.

Quoted 1 line

+module_i2c_driver(adxl355_i2c_driver);

For SPI part the very same bunch of comments.

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

PATCH v9 2/2 iio: accel: adxl355: Add triggered buffer support

In reply to Puranjay Mohan

Provide a way for continuous data capture by setting up buffer support. The data ready signal exposed at the DRDY pin of the ADXL355 is exploited as a hardware interrupt which triggers to fill the buffer.

---
 drivers/iio/accel/Kconfig        |   4 ++
 drivers/iio/accel/adxl355.h      |   2 +-
 drivers/iio/accel/adxl355_core.c | 102 ++++++++++++++++++++++++++++++-
 drivers/iio/accel/adxl355_i2c.c  |   3 +-
 drivers/iio/accel/adxl355_spi.c  |   2 +-
 5 files changed, 108 insertions(+), 5 deletions(-)
drivers/iio/accel/Kconfig +4 -0
index d0c45c809..9c16c1841 100644
@@ -69,6 +69,8 @@ config ADXL355_I2C
 	depends on I2C
 	select ADXL355
 	select REGMAP_I2C
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
 	help
 	  Say Y here if you want to build i2c support for the Analog Devices
 	  ADXL355 3-axis digital accelerometer.
@@ -82,6 +84,8 @@ config ADXL355_SPI
 	depends on SPI
 	select ADXL355
 	select REGMAP_SPI
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
 	help
 	  Say Y here if you want to build spi support for the Analog Devices
 	  ADXL355 3-axis digital accelerometer.
drivers/iio/accel/adxl355.h +1 -1
index 322b0abb8..f0a376e6d 100644
@@ -15,5 +15,5 @@ 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);
+		       const char *name, int irq);
 #endif /* _ADXL355_H_ */
drivers/iio/accel/adxl355_core.c +100 -2

171 lines not on this page · raw message

drivers/iio/accel/adxl355_i2c.c +2 -1
index e3070ee81..c18521819 100644
@@ -31,7 +31,8 @@ static int adxl355_i2c_probe(struct i2c_client *client)
 		return PTR_ERR(regmap);
 	}
 
-	return adxl355_core_probe(&client->dev, regmap, client->name);
+	return adxl355_core_probe(&client->dev, regmap, client->name,
+				  client->irq);
 }
 
 static const struct i2c_device_id adxl355_i2c_id[] = {
drivers/iio/accel/adxl355_spi.c +1 -1
index a16bd1407..f9ba153f6 100644
@@ -34,7 +34,7 @@ static int adxl355_spi_probe(struct spi_device *spi)
 		return PTR_ERR(regmap);
 	}
 
-	return adxl355_core_probe(&spi->dev, regmap, id->name);
+	return adxl355_core_probe(&spi->dev, regmap, id->name, spi->irq);
 }
 
 static const struct spi_device_id adxl355_spi_id[] = {
Signature · 1 line
2.30.1
#4 lore raw

Re: [PATCH v9 2/2] iio: accel: adxl355: Add triggered buffer support

Please ignore this patch.
It was sent by mistake, it is not supposed to be a part of this series.

Quoted 261 lines · Puranjay Mohan

Provide a way for continuous data capture by setting up buffer support. The data ready signal exposed at the DRDY pin of the ADXL355 is exploited as a hardware interrupt which triggers to fill the buffer.

---
 drivers/iio/accel/Kconfig        |   4 ++
 drivers/iio/accel/adxl355.h      |   2 +-
 drivers/iio/accel/adxl355_core.c | 102 ++++++++++++++++++++++++++++++-
 drivers/iio/accel/adxl355_i2c.c  |   3 +-
 drivers/iio/accel/adxl355_spi.c  |   2 +-
 5 files changed, 108 insertions(+), 5 deletions(-)
drivers/iio/accel/Kconfig +4 -0
index d0c45c809..9c16c1841 100644
@@ -69,6 +69,8 @@ config ADXL355_I2C
        depends on I2C
        select ADXL355
        select REGMAP_I2C
+       select IIO_BUFFER
+       select IIO_TRIGGERED_BUFFER
        help
          Say Y here if you want to build i2c support for the Analog Devices
          ADXL355 3-axis digital accelerometer.
@@ -82,6 +84,8 @@ config ADXL355_SPI
        depends on SPI
        select ADXL355
        select REGMAP_SPI
+       select IIO_BUFFER
+       select IIO_TRIGGERED_BUFFER
        help
          Say Y here if you want to build spi support for the Analog Devices
          ADXL355 3-axis digital accelerometer.
drivers/iio/accel/adxl355.h +1 -1
index 322b0abb8..f0a376e6d 100644
@@ -15,5 +15,5 @@ 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);
+                      const char *name, int irq);
 #endif /* _ADXL355_H_ */
drivers/iio/accel/adxl355_core.c +100 -2
index d1163cde1..45397dcce 100644
@@ -9,6 +9,10 @@
 
 #include <asm/unaligned.h>
 #include <linux/bitfield.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/iio/trigger_consumer.h>
 #include <linux/iio/iio.h>
 #include <linux/limits.h>
 #include <linux/math64.h>
@@ -153,6 +157,7 @@ static const struct adxl355_chan_info adxl355_chans[] = {
 };
 
 struct adxl355_data {
+       int irq;
        struct regmap *regmap;
        struct device *dev;
        struct mutex lock; /* lock to protect op_mode */
@@ -162,6 +167,12 @@ struct adxl355_data {
        int calibbias[3];
        int adxl355_hpf_3db_table[7][2];
        u8 transf_buf[3] ____cacheline_aligned;
+       struct iio_trigger      *dready_trig;
+       /* Ensure correct alignment of timestamp when present */
+       struct {
+               __be32 channels[3];
+               s64 ts;
+       } buffer ____cacheline_aligned;
 };
 
 static int adxl355_set_op_mode(struct adxl355_data *data,
@@ -493,12 +504,46 @@ static int adxl355_read_avail(struct iio_dev *indio_dev,
        }
 }
 
+static const unsigned long adxl355_avail_scan_masks[] = {
+       GENMASK(3, 0),
+       0
+};
+
 static const struct iio_info adxl355_info = {
        .read_raw       = adxl355_read_raw,
        .write_raw      = adxl355_write_raw,
        .read_avail     = &adxl355_read_avail
 };
 
+static const struct iio_trigger_ops adxl355_trigger_ops = {
+       .validate_device = &iio_trigger_validate_own_device,
+};
+
+static irqreturn_t adxl355_trigger_handler(int irq, void *p)
+{
+       struct iio_poll_func *pf = p;
+       struct iio_dev *indio_dev = pf->indio_dev;
+       struct adxl355_data *data = iio_priv(indio_dev);
+       int ret;
+
+       mutex_lock(&data->lock);
+
+       ret = regmap_bulk_read(data->regmap, ADXL355_XDATA3_REG,
+                              data->buffer.channels,
+                              9);
+       if (ret)
+               goto out_unlock_notify;
+
+       iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
+                                          pf->timestamp);
+
+out_unlock_notify:
+       mutex_unlock(&data->lock);
+       iio_trigger_notify_done(indio_dev->trig);
+
+       return IRQ_HANDLED;
+}
+
 #define ADXL355_ACCEL_CHANNEL(index, reg, axis) {                      \
        .type = IIO_ACCEL,                                              \
        .address = reg,                                                 \
@@ -512,6 +557,7 @@ static const struct iio_info adxl355_info = {
        .info_mask_shared_by_type_available =                           \
                BIT(IIO_CHAN_INFO_SAMP_FREQ) |                          \
                BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY),      \
+       .scan_index = index,                                            \
        .scan_type = {                                                  \
                .sign = 's',                                            \
                .realbits = 20,                                         \
@@ -531,17 +577,56 @@ static const struct iio_chan_spec adxl355_channels[] = {
                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
                                      BIT(IIO_CHAN_INFO_SCALE) |
                                      BIT(IIO_CHAN_INFO_OFFSET),
+               .scan_index = 3,
                .scan_type = {
                        .sign = 's',
                        .realbits = 12,
                        .storagebits = 16,
                        .endianness = IIO_BE,
                },
-       }
+       },
+       IIO_CHAN_SOFT_TIMESTAMP(4)
 };
 
+static int adxl355_probe_trigger(struct iio_dev *indio_dev)
+{
+       struct adxl355_data *data = iio_priv(indio_dev);
+       int ret;
+
+       if (!data->irq) {
+               dev_info(data->dev, "no irq, using polling\n");
+               return 0;
+       }
+
+       data->dready_trig = devm_iio_trigger_alloc(data->dev, "%s-dev%d",
+                                                  indio_dev->name,
+                                                  indio_dev->id);
+       if (!data->dready_trig)
+               return -ENOMEM;
+
+       data->dready_trig->ops = &adxl355_trigger_ops;
+       iio_trigger_set_drvdata(data->dready_trig, indio_dev);
+
+       ret = devm_request_irq(data->dev, data->irq,
+                              &iio_trigger_generic_data_rdy_poll,
+                              IRQF_ONESHOT, "adxl355_irq", data->dready_trig);
+       if (ret < 0)
+               return dev_err_probe(data->dev, ret, "request irq %d failed\n",
+                                    data->irq);
+
+       ret = devm_iio_trigger_register(data->dev, data->dready_trig);
+       if (ret) {
+               dev_err(data->dev, "iio trigger register failed\n");
+               return ret;
+       }
+
+       indio_dev->trig = iio_trigger_get(data->dready_trig);
+
+       return 0;
+}
+
 int adxl355_core_probe(struct device *dev, struct regmap *regmap,
-                      const char *name)
+                      const char *name, int irq)
 {
        struct adxl355_data *data;
        struct iio_dev *indio_dev;
@@ -554,6 +639,7 @@ int adxl355_core_probe(struct device *dev, struct regmap *regmap,
        data = iio_priv(indio_dev);
        data->regmap = regmap;
        data->dev = dev;
+       data->irq = irq;
        data->op_mode = ADXL355_STANDBY;
        mutex_init(&data->lock);
 
@@ -562,6 +648,7 @@ int adxl355_core_probe(struct device *dev, struct regmap *regmap,
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->channels = adxl355_channels;
        indio_dev->num_channels = ARRAY_SIZE(adxl355_channels);
+       indio_dev->available_scan_masks = adxl355_avail_scan_masks;
 
        ret = adxl355_setup(data);
        if (ret < 0) {
@@ -569,6 +656,17 @@ int adxl355_core_probe(struct device *dev, struct regmap *regmap,
                return ret;
        }
 
+       ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
+                                             &iio_pollfunc_store_time,
+                                             &adxl355_trigger_handler, NULL);
+       if (ret < 0)
+               return dev_err_probe(dev, ret,
+                                    "iio triggered buffer setup failed\n");
+
+       ret = adxl355_probe_trigger(indio_dev);
+       if (ret < 0)
+               return ret;
+
        return devm_iio_device_register(dev, indio_dev);
 }
 EXPORT_SYMBOL_GPL(adxl355_core_probe);
drivers/iio/accel/adxl355_i2c.c +2 -1
index e3070ee81..c18521819 100644
@@ -31,7 +31,8 @@ static int adxl355_i2c_probe(struct i2c_client *client)
                return PTR_ERR(regmap);
        }
 
-       return adxl355_core_probe(&client->dev, regmap, client->name);
+       return adxl355_core_probe(&client->dev, regmap, client->name,
+                                 client->irq);
 }
 
 static const struct i2c_device_id adxl355_i2c_id[] = {
drivers/iio/accel/adxl355_spi.c +1 -2
index a16bd1407..f9ba153f6 100644
@@ -34,7 +34,7 @@ static int adxl355_spi_probe(struct spi_device *spi)
                return PTR_ERR(regmap);
        }
 
-       return adxl355_core_probe(&spi->dev, regmap, id->name);
+       return adxl355_core_probe(&spi->dev, regmap, id->name, spi->irq);
 }
 
 static const struct spi_device_id adxl355_spi_id[] = {
--

2.30.1

Signature · 5 lines
Thanks and Regards

Yours Truly,

Puranjay Mohan
#5 lore raw
In reply to Puranjay Mohan

Please Ignore this I will resend this properly.
I made some mistakes while sending the patches.
I am really sorry for the noise.

Quoted 71 lines · Puranjay Mohan

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

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):
iio: accel: Add driver support for ADXL355
iio: accel: adxl355: Add triggered buffer support

 MAINTAINERS                      |  10 +
 drivers/iio/accel/Kconfig        |  33 ++
 drivers/iio/accel/Makefile       |   3 +
 drivers/iio/accel/adxl355.h      |  19 +
 drivers/iio/accel/adxl355_core.c | 676 +++++++++++++++++++++++++++++++
 drivers/iio/accel/adxl355_i2c.c  |  65 +++
 drivers/iio/accel/adxl355_spi.c  |  67 +++
 7 files changed, 873 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

--
2.30.1

Signature · 5 lines
Thanks and Regards

Yours Truly,

Puranjay Mohan