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 */
20static 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, txbuf: reg_buf, n_tx: reg_size, rxbuf: val_buf, n_rx: val_size);
27}
28
29static 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, buf: data_buff + 1, len: count - 1);
37}
38
39static const struct regmap_bus bmi323_regmap_bus = {
40 .read = bmi323_regmap_spi_read,
41 .write = bmi323_regmap_spi_write,
42};
43
44static 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
53static 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(ptr: regmap))
61 return dev_err_probe(dev, err: PTR_ERR(ptr: regmap),
62 fmt: "Failed to initialize SPI Regmap\n");
63
64 return bmi323_core_probe(dev);
65}
66
67static const struct spi_device_id bmi323_spi_ids[] = {
68 { "bmi323" },
69 { }
70};
71MODULE_DEVICE_TABLE(spi, bmi323_spi_ids);
72
73static const struct of_device_id bmi323_of_spi_match[] = {
74 { .compatible = "bosch,bmi323" },
75 { }
76};
77MODULE_DEVICE_TABLE(of, bmi323_of_spi_match);
78
79static 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,
84 },
85 .probe = bmi323_spi_probe,
86 .id_table = bmi323_spi_ids,
87};
88module_spi_driver(bmi323_spi_driver);
89
90MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
91MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
92MODULE_LICENSE("GPL");
93MODULE_IMPORT_NS("IIO_BMI323");
94

source code of linux/drivers/iio/imu/bmi323/bmi323_spi.c