iio: imu: bmi270: Add spi driver for bmi270 imu
Subsystems (2)
Alex Lanzano
maintainerPATCH 1/1 iio: imu: bmi270: Add spi driver for bmi270 imu
Implement SPI driver for the Bosch BMI270 6-axis IMU. Provide raw read write access to acceleration and angle velocity measurements via the SPI interface on the device.
--- drivers/iio/imu/bmi270/Kconfig | 12 ++++ drivers/iio/imu/bmi270/Makefile | 1 + drivers/iio/imu/bmi270/bmi270.h | 2 + drivers/iio/imu/bmi270/bmi270_core.c | 13 ++-- drivers/iio/imu/bmi270/bmi270_i2c.c | 7 ++- drivers/iio/imu/bmi270/bmi270_spi.c | 89 ++++++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 drivers/iio/imu/bmi270/bmi270_spi.c
drivers/iio/imu/bmi270/Kconfig +12 -0
@@ -18,3 +18,15 @@ config BMI270_I2C This driver can also be built as a module. If so, the module will be called bmi270_i2c. + +config BMI270_SPI + tristate "Bosch BMI270 SPI driver" + depends on SPI + select BMI270 + select REGMAP_SPI + help + Enable support for the Bosch BMI270 6-Axis IMU connected to SPI + interface. + + This driver can also be built as a module. If so, the module will be + called bmi270_spi.
drivers/iio/imu/bmi270/Makefile +1 -0
@@ -4,3 +4,4 @@ # obj-$(CONFIG_BMI270) += bmi270_core.o obj-$(CONFIG_BMI270_I2C) += bmi270_i2c.o +obj-$(CONFIG_BMI270_SPI) += bmi270_spi.o
drivers/iio/imu/bmi270/bmi270.h +2 -0
@@ -4,11 +4,13 @@#define BMI270_H_ #include <linux/regmap.h> +#include <linux/iio/iio.h> struct device ;struct bmi270_data {struct device *dev;struct regmap *regmap; +__le16 sample__aligned(IIO_DMA_MINALIGN) ; };extern const struct regmap_config bmi270_regmap_config;
drivers/iio/imu/bmi270/bmi270_core.c +4 -9
@@ -66,16 +66,9 @@ enum bmi270_scan { BMI270_SCAN_GYRO_Z, }; -const struct regmap_config bmi270_regmap_config = { - .reg_bits =8 , - .val_bits =8 , -}; -EXPORT_SYMBOL_NS_GPL (bmi270_regmap_config,IIO_BMI270 ); -static int bmi270_get_data (struct bmi270_data *bmi270_device,int chan_type,int axis,int *val) { -__le16 sample;int reg;int ret;
@@ -90,11 +83,13 @@ static int bmi270_get_data(struct bmi270_data *bmi270_device,return -EINVAL ; } - ret =regmap_bulk_read (bmi270_device->regmap, reg, &sample,sizeof (sample)); + ret =regmap_bulk_read (bmi270_device->regmap, reg, + &bmi270_device->sample, +sizeof (bmi270_device->sample));if (ret)return ret; - *val =sign_extend32 (le16_to_cpu (sample),15 ); + *val =sign_extend32 (le16_to_cpu (bmi270_device->sample),15 );return 0 ; }
drivers/iio/imu/bmi270/bmi270_i2c.c +6 -1
@@ -9,12 +9,17 @@#include "bmi270.h" +const struct regmap_config bmi270_i2c_regmap_config = { + .reg_bits =8 , + .val_bits =8 , +}; +static int bmi270_i2c_probe (struct i2c_client *client) {struct regmap *regmap;struct device *dev = &client->dev; - regmap =devm_regmap_init_i2c (client, &bmi270_regmap_config); + regmap =devm_regmap_init_i2c (client, &bmi270_i2c_regmap_config);if (IS_ERR (regmap))return dev_err_probe (dev,PTR_ERR (regmap),"Failed to init i2c regmap" );
drivers/iio/imu/bmi270/bmi270_spi.c new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
2.46.2
Jonathan Cameron
maintainerQuoted 5 lines · On Fri, 27 Sep 2024 14:37:10 -0400 Alex Lanzano <[email protected]> wrote:
Implement SPI driver for the Bosch BMI270 6-axis IMU. Provide raw read write access to acceleration and angle velocity measurements via the SPI interface on the device.
Signed-off-by Alex Lanzano <[email protected]>
A few minor things inline but looks good in general.
Jonathan
Quoted 5 lines
drivers/iio/imu/bmi270/bmi270.h +0
-0
drivers/iio/imu/bmi270/bmi270.h line 13
Quoted 10 lines
drivers/iio/imu/bmi270/bmi270.h +2
-0@@ -4,11 +4,13 @@#define BMI270_H_ #include <linux/regmap.h> +#include <linux/iio/iio.h> struct device ;struct bmi270_data {struct device *dev;struct regmap *regmap; +__le16 sample__aligned(IIO_DMA_MINALIGN) ;
For the read path you are bouncing anyway, so the DMA_MINALIGN is only needed for anything the write direction. Make the suggested change below and that will bounce as well so that you don't need this.
Quoted 45 lines
drivers/iio/imu/bmi270/bmi270.h +0
-0};extern const struct regmap_config bmi270_regmap_config;drivers/iio/imu/bmi270/bmi270_core.c +4
-9@@ -66,16 +66,9 @@ enum bmi270_scan { BMI270_SCAN_GYRO_Z, }; -const struct regmap_config bmi270_regmap_config = { - .reg_bits =8 , - .val_bits =8 , -}; -EXPORT_SYMBOL_NS_GPL (bmi270_regmap_config,IIO_BMI270 ); -static int bmi270_get_data (struct bmi270_data *bmi270_device,int chan_type,int axis,int *val) { -__le16 sample;int reg;int ret;@@ -90,11 +83,13 @@ static int bmi270_get_data(struct bmi270_data *bmi270_device,return -EINVAL ; } - ret =regmap_bulk_read (bmi270_device->regmap, reg, &sample,sizeof (sample)); + ret =regmap_bulk_read (bmi270_device->regmap, reg, + &bmi270_device->sample, +sizeof (bmi270_device->sample));if (ret)return ret; - *val =sign_extend32 (le16_to_cpu (sample),15 ); + *val =sign_extend32 (le16_to_cpu (bmi270_device->sample),15 );return 0 ; }drivers/iio/imu/bmi270/bmi270_i2c.c +0
-0
drivers/iio/imu/bmi270/bmi270_i2c.c line 12
Quoted 4 lines
drivers/iio/imu/bmi270/bmi270_i2c.c +1
-0@@ -9,12 +9,17 @@#include "bmi270.h" +const struct regmap_config bmi270_i2c_regmap_config = {
static const
(same for spi one)
Quoted 20 lines
drivers/iio/imu/bmi270/bmi270_i2c.c +5
-1+ .reg_bits = 8, + .val_bits = 8, +}; +static int bmi270_i2c_probe (struct i2c_client *client) {struct regmap *regmap;struct device *dev = &client->dev; - regmap =devm_regmap_init_i2c (client, &bmi270_regmap_config); + regmap =devm_regmap_init_i2c (client, &bmi270_i2c_regmap_config);if (IS_ERR (regmap))return dev_err_probe (dev,PTR_ERR (regmap),"Failed to init i2c regmap" );drivers/iio/imu/bmi270/bmi270_spi.c new file +0
-0
drivers/iio/imu/bmi270/bmi270_spi.c line 8
Quoted 8 lines
drivers/iio/imu/bmi270/bmi270_spi.c new file +8
-0@@ -0,0 +1,89 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) + +#include <linux/module.h> +#include <linux/spi/spi.h> +#include <linux/iio/iio.h> +#include <linux/module.h> +#include <linux/mod_devicetable.h> +#include <linux/regmap.h>
Alphabetical order preferred.
drivers/iio/imu/bmi270/bmi270_spi.c line 21
Quoted 13 lines
drivers/iio/imu/bmi270/bmi270_spi.c new file +13
-0+ +#include "bmi270.h" + +/* +* The following two functions are taken from the BMI323 spi driver code. +* In section 6.4 of the BMI270 data it specifies that after a read +* operation the first data byte from the device is a dummy byte +*/ +static int bmi270_regmap_spi_read (void *context,const void *reg_buf, +size_t reg_size,void *val_buf, +size_t val_size) +{ +struct spi_device *spi = context;
I'd be tempted to rename the input parameter context to spi and then parse it directly to the spi_write_then_read()
Quoted 6 lines
drivers/iio/imu/bmi270/bmi270_spi.c new file +6
-0+ +return spi_write_then_read (spi, reg_buf, reg_size, val_buf, val_size); +} + +static int bmi270_regmap_spi_write (void *context,const void *data, +size_t count)
drivers/iio/imu/bmi270/bmi270_spi.c line 37
Quoted 10 lines
drivers/iio/imu/bmi270/bmi270_spi.c new file +10
-0+{ +struct spi_device *spi = context; +u8 *data_buff = (u8 *)data; + +/* +* Remove the extra pad byte since its only needed for the read +* operation +*/ + data_buff[1 ] = data_buff[0 ]; +return spi_write (spi, data_buff +1 , count -1 );
That needs a DMA safe buffer (unlike write_then_read which always bounces). I'd avoid that complexity by using spi_write_then_read here as well but set the read to 0 length and pass NULL for the buffer. That function is intended to be used like this as it special cases 0 length for either write or read buffers.
Quoted 1 line
drivers/iio/imu/bmi270/bmi270_spi.c new file +1
-0+}
Alex Lanzano
maintainerThanks for the review!
Quoted 23 lines · Jonathan Cameron
Quoted 5 lines · On Fri, 27 Sep 2024 14:37:10 -0400 Alex Lanzano <[email protected]> wrote:
Implement SPI driver for the Bosch BMI270 6-axis IMU. Provide raw read write access to acceleration and angle velocity measurements via the SPI interface on the device.
Signed-off-by Alex Lanzano <[email protected]>A few minor things inline but looks good in general.
Jonathan
Quoted 10 lines
drivers/iio/imu/bmi270/bmi270.h +1
-0@@ -4,11 +4,13 @@#define BMI270_H_ #include <linux/regmap.h> +#include <linux/iio/iio.h>
Quoted 10 lines
drivers/iio/imu/bmi270/bmi270.h line 13
Quoted 5 lines
drivers/iio/imu/bmi270/bmi270.h +1
-0struct device ;struct bmi270_data {struct device *dev;struct regmap *regmap; +__le16 sample__aligned(IIO_DMA_MINALIGN) ;For the read path you are bouncing anyway, so the
DMA_MINALIGNis only needed for anything the write direction. Make the suggested change below and that will bounce as well so that you don't need this.
Understood! Will remove in v2 and use spi_write_then_read
Quoted 46 lines
Quoted 46 lines
drivers/iio/imu/bmi270/bmi270.h +0
-0};extern const struct regmap_config bmi270_regmap_config;drivers/iio/imu/bmi270/bmi270_core.c +4
-9@@ -66,16 +66,9 @@ enum bmi270_scan { BMI270_SCAN_GYRO_Z, }; -const struct regmap_config bmi270_regmap_config = { - .reg_bits =8 , - .val_bits =8 , -}; -EXPORT_SYMBOL_NS_GPL (bmi270_regmap_config,IIO_BMI270 ); -static int bmi270_get_data (struct bmi270_data *bmi270_device,int chan_type,int axis,int *val) { -__le16 sample;int reg;int ret;@@ -90,11 +83,13 @@ static int bmi270_get_data(struct bmi270_data *bmi270_device,return -EINVAL ; } - ret =regmap_bulk_read (bmi270_device->regmap, reg, &sample,sizeof (sample)); + ret =regmap_bulk_read (bmi270_device->regmap, reg, + &bmi270_device->sample, +sizeof (bmi270_device->sample));if (ret)return ret; - *val =sign_extend32 (le16_to_cpu (sample),15 ); + *val =sign_extend32 (le16_to_cpu (bmi270_device->sample),15 );return 0 ; }drivers/iio/imu/bmi270/bmi270_i2c.c +0
-0@@ -9,12 +9,17 @@
Quoted 7 lines
drivers/iio/imu/bmi270/bmi270_i2c.c line 12
Quoted 3 lines
drivers/iio/imu/bmi270/bmi270_i2c.c +1
-0#include "bmi270.h" +const struct regmap_config bmi270_i2c_regmap_config = {static const
(same for spi one)
Will fix in v2!
Quoted 21 lines
Quoted 21 lines
drivers/iio/imu/bmi270/bmi270_i2c.c +5
-1+ .reg_bits = 8, + .val_bits = 8, +}; +static int bmi270_i2c_probe (struct i2c_client *client) {struct regmap *regmap;struct device *dev = &client->dev; - regmap =devm_regmap_init_i2c (client, &bmi270_regmap_config); + regmap =devm_regmap_init_i2c (client, &bmi270_i2c_regmap_config);if (IS_ERR (regmap))return dev_err_probe (dev,PTR_ERR (regmap),"Failed to init i2c regmap" );drivers/iio/imu/bmi270/bmi270_spi.c new file +1
-0@@ -0,0 +1,89 @@ +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
Quoted 9 lines
drivers/iio/imu/bmi270/bmi270_spi.c line 8
Quoted 7 lines
drivers/iio/imu/bmi270/bmi270_spi.c new file +7
-0+ +#include <linux/module.h> +#include <linux/spi/spi.h> +#include <linux/iio/iio.h> +#include <linux/module.h> +#include <linux/mod_devicetable.h> +#include <linux/regmap.h> Alphabetical order preferred.
Will fix in v2
Quoted 8 lines
Quoted 8 lines
drivers/iio/imu/bmi270/bmi270_spi.c new file +8
-0+ +#include "bmi270.h" + +/* +* The following two functions are taken from the BMI323 spi driver code. +* In section 6.4 of the BMI270 data it specifies that after a read +* operation the first data byte from the device is a dummy byte +*/
Quoted 9 lines
drivers/iio/imu/bmi270/bmi270_spi.c line 21
Quoted 5 lines
drivers/iio/imu/bmi270/bmi270_spi.c new file +5
-0+static int bmi270_regmap_spi_read (void *context,const void *reg_buf, +size_t reg_size,void *val_buf, +size_t val_size) +{ +struct spi_device *spi = context;I'd be tempted to rename the input parameter context to spi and then parse it directly to the
spi_write_then_read()
Will do! I named it context since that's what the function pointer uses
Quoted 13 lines
Quoted 13 lines
drivers/iio/imu/bmi270/bmi270_spi.c new file +13
-0+ +return spi_write_then_read (spi, reg_buf, reg_size, val_buf, val_size); +} + +static int bmi270_regmap_spi_write (void *context,const void *data, +size_t count) +{ +struct spi_device *spi = context; +u8 *data_buff = (u8 *)data; + +/* +* Remove the extra pad byte since its only needed for the read +* operation
Quoted 9 lines
drivers/iio/imu/bmi270/bmi270_spi.c line 37
Quoted 3 lines
drivers/iio/imu/bmi270/bmi270_spi.c new file +3
-0+*/ + data_buff[1 ] = data_buff[0 ]; +return spi_write (spi, data_buff +1 , count -1 );That needs a DMA safe buffer (unlike
write_then_readwhich always bounces). I'd avoid that complexity by usingspi_write_then_readhere as well but set the read to 0 length and pass NULL for the buffer. That function is intended to be used like this as it special cases 0 length for either write or read buffers.
Understood! Will fix in v2.
Quoted 2 lines
Quoted 1 line
drivers/iio/imu/bmi270/bmi270_spi.c new file +1
-0+}