^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * SPI IIO driver for Bosch BMA400 triaxial acceleration sensor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2020 Dan Robertson <dan@dlrobertson.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/bits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "bma400.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define BMA400_MAX_SPI_READ 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define BMA400_SPI_READ_BUFFER_SIZE (BMA400_MAX_SPI_READ + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int bma400_regmap_spi_read(void *context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) const void *reg, size_t reg_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) void *val, size_t val_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct device *dev = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct spi_device *spi = to_spi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u8 result[BMA400_SPI_READ_BUFFER_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) ssize_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (val_size > BMA400_MAX_SPI_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) status = spi_write_then_read(spi, reg, 1, result, val_size + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * From the BMA400 datasheet:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * > For a basic read operation two bytes have to be read and the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * > has to be dropped and the second byte must be interpreted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) memcpy(val, result + 1, val_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int bma400_regmap_spi_write(void *context, const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct device *dev = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct spi_device *spi = to_spi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return spi_write(spi, data, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static struct regmap_bus bma400_regmap_bus = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .read = bma400_regmap_spi_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .write = bma400_regmap_spi_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .read_flag_mask = BIT(7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .max_raw_read = BMA400_MAX_SPI_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int bma400_spi_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) const struct spi_device_id *id = spi_get_device_id(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) regmap = devm_regmap_init(&spi->dev, &bma400_regmap_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) &spi->dev, &bma400_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (IS_ERR(regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) dev_err(&spi->dev, "failed to create regmap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return PTR_ERR(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * Per the bma400 datasheet, the first SPI read may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * return garbage. As the datasheet recommends, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * chip ID register will be read here and checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * again in the following probe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ret = regmap_read(regmap, BMA400_CHIP_ID_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) dev_err(&spi->dev, "Failed to read chip id register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return bma400_probe(&spi->dev, regmap, id->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static int bma400_spi_remove(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return bma400_remove(&spi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static const struct spi_device_id bma400_spi_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) { "bma400", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) MODULE_DEVICE_TABLE(spi, bma400_spi_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static const struct of_device_id bma400_of_spi_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) { .compatible = "bosch,bma400" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) MODULE_DEVICE_TABLE(of, bma400_of_spi_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static struct spi_driver bma400_spi_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .name = "bma400",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .of_match_table = bma400_of_spi_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .probe = bma400_spi_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .remove = bma400_spi_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .id_table = bma400_spi_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) module_spi_driver(bma400_spi_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) MODULE_AUTHOR("Dan Robertson <dan@dlrobertson.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor (SPI)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) MODULE_LICENSE("GPL");