1 // SPDX-License-Identifier: GPL-2.0-only
3 * SPI IIO driver for Bosch BMA400 triaxial acceleration sensor.
8 #include <linux/bits.h>
9 #include <linux/init.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/spi/spi.h>
17 #define BMA400_MAX_SPI_READ 2
18 #define BMA400_SPI_READ_BUFFER_SIZE (BMA400_MAX_SPI_READ + 1)
20 static int bma400_regmap_spi_read(void *context,
21 const void *reg, size_t reg_size,
22 void *val, size_t val_size)
24 struct device *dev = context;
25 struct spi_device *spi = to_spi_device(dev);
26 u8 result[BMA400_SPI_READ_BUFFER_SIZE];
29 if (val_size > BMA400_MAX_SPI_READ)
32 status = spi_write_then_read(spi, reg, 1, result, val_size + 1);
37 * From the BMA400 datasheet:
39 * > For a basic read operation two bytes have to be read and the first
40 * > has to be dropped and the second byte must be interpreted.
42 memcpy(val, result + 1, val_size);
47 static int bma400_regmap_spi_write(void *context, const void *data,
50 struct device *dev = context;
51 struct spi_device *spi = to_spi_device(dev);
53 return spi_write(spi, data, count);
56 static const struct regmap_bus bma400_regmap_bus = {
57 .read = bma400_regmap_spi_read,
58 .write = bma400_regmap_spi_write,
59 .read_flag_mask = BIT(7),
60 .max_raw_read = BMA400_MAX_SPI_READ,
63 static int bma400_spi_probe(struct spi_device *spi)
65 const struct spi_device_id *id = spi_get_device_id(spi);
66 struct regmap *regmap;
70 regmap = devm_regmap_init(&spi->dev, &bma400_regmap_bus,
71 &spi->dev, &bma400_regmap_config);
73 dev_err(&spi->dev, "failed to create regmap\n");
74 return PTR_ERR(regmap);
78 * Per the bma400 datasheet, the first SPI read may
79 * return garbage. As the datasheet recommends, the
80 * chip ID register will be read here and checked
81 * again in the following probe.
83 ret = regmap_read(regmap, BMA400_CHIP_ID_REG, &val);
85 dev_err(&spi->dev, "Failed to read chip id register\n");
87 return bma400_probe(&spi->dev, regmap, spi->irq, id->name);
90 static const struct spi_device_id bma400_spi_ids[] = {
94 MODULE_DEVICE_TABLE(spi, bma400_spi_ids);
96 static const struct of_device_id bma400_of_spi_match[] = {
97 { .compatible = "bosch,bma400" },
100 MODULE_DEVICE_TABLE(of, bma400_of_spi_match);
102 static struct spi_driver bma400_spi_driver = {
105 .of_match_table = bma400_of_spi_match,
107 .probe = bma400_spi_probe,
108 .id_table = bma400_spi_ids,
111 module_spi_driver(bma400_spi_driver);
113 MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor (SPI)");
114 MODULE_LICENSE("GPL");
115 MODULE_IMPORT_NS("IIO_BMA400");