Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / iio / imu / bmi323 / bmi323_spi.c
blobfd56ab6207500d01af6c10a03ab26a296cb49ff1
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SPI driver for Bosch BMI323 6-Axis IMU.
5 * Copyright (C) 2023, Jagath Jog J <jagathjog1996@gmail.com>
6 */
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/regmap.h>
11 #include <linux/spi/spi.h>
13 #include "bmi323.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
18 * the actual payload.
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)
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,
30 size_t count)
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 = {
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,
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);
60 if (IS_ERR(regmap))
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[] = {
68 { "bmi323" },
69 { }
71 MODULE_DEVICE_TABLE(spi, bmi323_spi_ids);
73 static const struct of_device_id bmi323_of_spi_match[] = {
74 { .compatible = "bosch,bmi323" },
75 { }
77 MODULE_DEVICE_TABLE(of, bmi323_of_spi_match);
79 static struct spi_driver bmi323_spi_driver = {
80 .driver = {
81 .name = "bmi323",
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");