1 // SPDX-License-Identifier: GPL-2.0
3 * SPI driver for Bosch BMI323 6-Axis IMU.
5 * Copyright (C) 2023, Jagath Jog J <jagathjog1996@gmail.com>
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/regmap.h>
11 #include <linux/spi/spi.h>
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
20 static int bmi323_regmap_spi_read(void *context
, const void *reg_buf
,
21 size_t reg_size
, void *val_buf
,
24 struct spi_device
*spi
= context
;
26 return spi_write_then_read(spi
, reg_buf
, reg_size
, val_buf
, val_size
);
29 static int bmi323_regmap_spi_write(void *context
, const void *data
,
32 struct spi_device
*spi
= context
;
33 u8
*data_buff
= (u8
*)data
;
35 data_buff
[1] = data_buff
[0];
36 return spi_write(spi
, data_buff
+ 1, count
- 1);
39 static const struct regmap_bus bmi323_regmap_bus
= {
40 .read
= bmi323_regmap_spi_read
,
41 .write
= bmi323_regmap_spi_write
,
44 static const struct regmap_config bmi323_spi_regmap_config
= {
48 .read_flag_mask
= BIT(7),
49 .max_register
= BMI323_CFG_RES_REG
,
50 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
53 static int bmi323_spi_probe(struct spi_device
*spi
)
55 struct device
*dev
= &spi
->dev
;
56 struct regmap
*regmap
;
58 regmap
= devm_regmap_init(dev
, &bmi323_regmap_bus
, dev
,
59 &bmi323_spi_regmap_config
);
61 return dev_err_probe(dev
, PTR_ERR(regmap
),
62 "Failed to initialize SPI Regmap\n");
64 return bmi323_core_probe(dev
);
67 static const struct spi_device_id bmi323_spi_ids
[] = {
71 MODULE_DEVICE_TABLE(spi
, bmi323_spi_ids
);
73 static const struct of_device_id bmi323_of_spi_match
[] = {
74 { .compatible
= "bosch,bmi323" },
77 MODULE_DEVICE_TABLE(of
, bmi323_of_spi_match
);
79 static struct spi_driver bmi323_spi_driver
= {
82 .pm
= pm_ptr(&bmi323_core_pm_ops
),
83 .of_match_table
= bmi323_of_spi_match
,
85 .probe
= bmi323_spi_probe
,
86 .id_table
= bmi323_spi_ids
,
88 module_spi_driver(bmi323_spi_driver
);
90 MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
91 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
92 MODULE_LICENSE("GPL");
93 MODULE_IMPORT_NS("IIO_BMI323");