1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * SPI driver for Bosch BMI323 6-Axis IMU. 4 * 5 * Copyright (C) 2023, Jagath Jog J <jagathjog1996@gmail.com> 6 */ 7 8 #include <linux/mod_devicetable.h> 9 #include <linux/module.h> 10 #include <linux/regmap.h> 11 #include <linux/spi/spi.h> 12 13 #include "bmi323.h" 14 15 /* 16 * From BMI323 datasheet section 4: Notes on the Serial Interface Support. 17 * Each SPI register read operation requires to read one dummy byte before 18 * the actual payload. 19 */ 20 static int bmi323_regmap_spi_read(void *context, const void *reg_buf, 21 size_t reg_size, void *val_buf, 22 size_t val_size) 23 { 24 struct spi_device *spi = context; 25 26 return spi_write_then_read(spi, reg_buf, reg_size, val_buf, val_size); 27 } 28 29 static int bmi323_regmap_spi_write(void *context, const void *data, 30 size_t count) 31 { 32 struct spi_device *spi = context; 33 u8 *data_buff = (u8 *)data; 34 35 data_buff[1] = data_buff[0]; 36 return spi_write(spi, data_buff + 1, count - 1); 37 } 38 39 static struct regmap_bus bmi323_regmap_bus = { 40 .read = bmi323_regmap_spi_read, 41 .write = bmi323_regmap_spi_write, 42 }; 43 44 static const struct regmap_config bmi323_spi_regmap_config = { 45 .reg_bits = 8, 46 .val_bits = 16, 47 .pad_bits = 8, 48 .read_flag_mask = BIT(7), 49 .max_register = BMI323_CFG_RES_REG, 50 .val_format_endian = REGMAP_ENDIAN_LITTLE, 51 }; 52 53 static int bmi323_spi_probe(struct spi_device *spi) 54 { 55 struct device *dev = &spi->dev; 56 struct regmap *regmap; 57 58 regmap = devm_regmap_init(dev, &bmi323_regmap_bus, dev, 59 &bmi323_spi_regmap_config); 60 if (IS_ERR(regmap)) 61 return dev_err_probe(dev, PTR_ERR(regmap), 62 "Failed to initialize SPI Regmap\n"); 63 64 return bmi323_core_probe(dev); 65 } 66 67 static const struct spi_device_id bmi323_spi_ids[] = { 68 { "bmi323" }, 69 { } 70 }; 71 MODULE_DEVICE_TABLE(spi, bmi323_spi_ids); 72 73 static const struct of_device_id bmi323_of_spi_match[] = { 74 { .compatible = "bosch,bmi323" }, 75 { } 76 }; 77 MODULE_DEVICE_TABLE(of, bmi323_of_spi_match); 78 79 static struct spi_driver bmi323_spi_driver = { 80 .driver = { 81 .name = "bmi323", 82 .of_match_table = bmi323_of_spi_match, 83 }, 84 .probe = bmi323_spi_probe, 85 .id_table = bmi323_spi_ids, 86 }; 87 module_spi_driver(bmi323_spi_driver); 88 89 MODULE_DESCRIPTION("Bosch BMI323 IMU driver"); 90 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>"); 91 MODULE_LICENSE("GPL"); 92 MODULE_IMPORT_NS(IIO_BMI323); 93