Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

Signed-off-by: Alex Lanzano <lanzano.alex@gmail.com>
Link: https://patch.msgid.link/20241002033628.681812-1-lanzano.alex@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Alex Lanzano and committed by
Jonathan Cameron
92cc50a0 ccc26bd7

+105 -7
+12
drivers/iio/imu/bmi270/Kconfig
··· 18 18 19 19 This driver can also be built as a module. If so, the module will be 20 20 called bmi270_i2c. 21 + 22 + config BMI270_SPI 23 + tristate "Bosch BMI270 SPI driver" 24 + depends on SPI 25 + select BMI270 26 + select REGMAP_SPI 27 + help 28 + Enable support for the Bosch BMI270 6-Axis IMU connected to SPI 29 + interface. 30 + 31 + This driver can also be built as a module. If so, the module will be 32 + called bmi270_spi.
+1
drivers/iio/imu/bmi270/Makefile
··· 4 4 # 5 5 obj-$(CONFIG_BMI270) += bmi270_core.o 6 6 obj-$(CONFIG_BMI270_I2C) += bmi270_i2c.o 7 + obj-$(CONFIG_BMI270_SPI) += bmi270_spi.o
+1
drivers/iio/imu/bmi270/bmi270.h
··· 4 4 #define BMI270_H_ 5 5 6 6 #include <linux/regmap.h> 7 + #include <linux/iio/iio.h> 7 8 8 9 struct device; 9 10 struct bmi270_data {
-6
drivers/iio/imu/bmi270/bmi270_core.c
··· 66 66 BMI270_SCAN_GYRO_Z, 67 67 }; 68 68 69 - const struct regmap_config bmi270_regmap_config = { 70 - .reg_bits = 8, 71 - .val_bits = 8, 72 - }; 73 - EXPORT_SYMBOL_NS_GPL(bmi270_regmap_config, IIO_BMI270); 74 - 75 69 static int bmi270_get_data(struct bmi270_data *bmi270_device, 76 70 int chan_type, int axis, int *val) 77 71 {
+6 -1
drivers/iio/imu/bmi270/bmi270_i2c.c
··· 9 9 10 10 #include "bmi270.h" 11 11 12 + static const struct regmap_config bmi270_i2c_regmap_config = { 13 + .reg_bits = 8, 14 + .val_bits = 8, 15 + }; 16 + 12 17 static int bmi270_i2c_probe(struct i2c_client *client) 13 18 { 14 19 struct regmap *regmap; 15 20 struct device *dev = &client->dev; 16 21 17 - regmap = devm_regmap_init_i2c(client, &bmi270_regmap_config); 22 + regmap = devm_regmap_init_i2c(client, &bmi270_i2c_regmap_config); 18 23 if (IS_ERR(regmap)) 19 24 return dev_err_probe(dev, PTR_ERR(regmap), 20 25 "Failed to init i2c regmap");
+85
drivers/iio/imu/bmi270/bmi270_spi.c
··· 1 + // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + 3 + #include <linux/iio/iio.h> 4 + #include <linux/mod_devicetable.h> 5 + #include <linux/module.h> 6 + #include <linux/regmap.h> 7 + #include <linux/spi/spi.h> 8 + 9 + #include "bmi270.h" 10 + 11 + /* 12 + * The following two functions are taken from the BMI323 spi driver code. 13 + * In section 6.4 of the BMI270 data it specifies that after a read 14 + * operation the first data byte from the device is a dummy byte 15 + */ 16 + static int bmi270_regmap_spi_read(void *spi, const void *reg_buf, 17 + size_t reg_size, void *val_buf, 18 + size_t val_size) 19 + { 20 + return spi_write_then_read(spi, reg_buf, reg_size, val_buf, val_size); 21 + } 22 + 23 + static int bmi270_regmap_spi_write(void *spi, const void *data, 24 + size_t count) 25 + { 26 + u8 *data_buff = (u8 *)data; 27 + 28 + /* 29 + * Remove the extra pad byte since its only needed for the read 30 + * operation 31 + */ 32 + data_buff[1] = data_buff[0]; 33 + return spi_write_then_read(spi, data_buff + 1, count - 1, NULL, 0); 34 + } 35 + 36 + static const struct regmap_bus bmi270_regmap_bus = { 37 + .read = bmi270_regmap_spi_read, 38 + .write = bmi270_regmap_spi_write, 39 + }; 40 + 41 + static const struct regmap_config bmi270_spi_regmap_config = { 42 + .reg_bits = 8, 43 + .val_bits = 8, 44 + .pad_bits = 8, 45 + .read_flag_mask = BIT(7), 46 + }; 47 + 48 + static int bmi270_spi_probe(struct spi_device *spi) 49 + { 50 + struct regmap *regmap; 51 + struct device *dev = &spi->dev; 52 + 53 + regmap = devm_regmap_init(dev, &bmi270_regmap_bus, dev, 54 + &bmi270_spi_regmap_config); 55 + if (IS_ERR(regmap)) 56 + return dev_err_probe(dev, PTR_ERR(regmap), 57 + "Failed to init i2c regmap"); 58 + 59 + return bmi270_core_probe(dev, regmap); 60 + } 61 + 62 + static const struct spi_device_id bmi270_spi_id[] = { 63 + { "bmi270" }, 64 + { } 65 + }; 66 + 67 + static const struct of_device_id bmi270_of_match[] = { 68 + { .compatible = "bosch,bmi270" }, 69 + { } 70 + }; 71 + 72 + static struct spi_driver bmi270_spi_driver = { 73 + .driver = { 74 + .name = "bmi270", 75 + .of_match_table = bmi270_of_match, 76 + }, 77 + .probe = bmi270_spi_probe, 78 + .id_table = bmi270_spi_id, 79 + }; 80 + module_spi_driver(bmi270_spi_driver); 81 + 82 + MODULE_AUTHOR("Alex Lanzano"); 83 + MODULE_DESCRIPTION("BMI270 driver"); 84 + MODULE_LICENSE("GPL"); 85 + MODULE_IMPORT_NS(IIO_BMI270);