Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * SPI IIO driver for Bosch BMA400 triaxial acceleration sensor.
0004  *
0005  * Copyright 2020 Dan Robertson <dan@dlrobertson.com>
0006  *
0007  */
0008 #include <linux/bits.h>
0009 #include <linux/init.h>
0010 #include <linux/mod_devicetable.h>
0011 #include <linux/module.h>
0012 #include <linux/regmap.h>
0013 #include <linux/spi/spi.h>
0014 
0015 #include "bma400.h"
0016 
0017 #define BMA400_MAX_SPI_READ 2
0018 #define BMA400_SPI_READ_BUFFER_SIZE (BMA400_MAX_SPI_READ + 1)
0019 
0020 static int bma400_regmap_spi_read(void *context,
0021                   const void *reg, size_t reg_size,
0022                   void *val, size_t val_size)
0023 {
0024     struct device *dev = context;
0025     struct spi_device *spi = to_spi_device(dev);
0026     u8 result[BMA400_SPI_READ_BUFFER_SIZE];
0027     ssize_t status;
0028 
0029     if (val_size > BMA400_MAX_SPI_READ)
0030         return -EINVAL;
0031 
0032     status = spi_write_then_read(spi, reg, 1, result, val_size + 1);
0033     if (status)
0034         return status;
0035 
0036     /*
0037      * From the BMA400 datasheet:
0038      *
0039      * > For a basic read operation two bytes have to be read and the first
0040      * > has to be dropped and the second byte must be interpreted.
0041      */
0042     memcpy(val, result + 1, val_size);
0043 
0044     return 0;
0045 }
0046 
0047 static int bma400_regmap_spi_write(void *context, const void *data,
0048                    size_t count)
0049 {
0050     struct device *dev = context;
0051     struct spi_device *spi = to_spi_device(dev);
0052 
0053     return spi_write(spi, data, count);
0054 }
0055 
0056 static struct regmap_bus bma400_regmap_bus = {
0057     .read = bma400_regmap_spi_read,
0058     .write = bma400_regmap_spi_write,
0059     .read_flag_mask = BIT(7),
0060     .max_raw_read = BMA400_MAX_SPI_READ,
0061 };
0062 
0063 static int bma400_spi_probe(struct spi_device *spi)
0064 {
0065     const struct spi_device_id *id = spi_get_device_id(spi);
0066     struct regmap *regmap;
0067     unsigned int val;
0068     int ret;
0069 
0070     regmap = devm_regmap_init(&spi->dev, &bma400_regmap_bus,
0071                   &spi->dev, &bma400_regmap_config);
0072     if (IS_ERR(regmap)) {
0073         dev_err(&spi->dev, "failed to create regmap\n");
0074         return PTR_ERR(regmap);
0075     }
0076 
0077     /*
0078      * Per the bma400 datasheet, the first SPI read may
0079      * return garbage. As the datasheet recommends, the
0080      * chip ID register will be read here and checked
0081      * again in the following probe.
0082      */
0083     ret = regmap_read(regmap, BMA400_CHIP_ID_REG, &val);
0084     if (ret)
0085         dev_err(&spi->dev, "Failed to read chip id register\n");
0086 
0087     return bma400_probe(&spi->dev, regmap, spi->irq, id->name);
0088 }
0089 
0090 static const struct spi_device_id bma400_spi_ids[] = {
0091     { "bma400", 0 },
0092     { }
0093 };
0094 MODULE_DEVICE_TABLE(spi, bma400_spi_ids);
0095 
0096 static const struct of_device_id bma400_of_spi_match[] = {
0097     { .compatible = "bosch,bma400" },
0098     { }
0099 };
0100 MODULE_DEVICE_TABLE(of, bma400_of_spi_match);
0101 
0102 static struct spi_driver bma400_spi_driver = {
0103     .driver = {
0104         .name = "bma400",
0105         .of_match_table = bma400_of_spi_match,
0106     },
0107     .probe    = bma400_spi_probe,
0108     .id_table = bma400_spi_ids,
0109 };
0110 
0111 module_spi_driver(bma400_spi_driver);
0112 MODULE_AUTHOR("Dan Robertson <dan@dlrobertson.com>");
0113 MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor (SPI)");
0114 MODULE_LICENSE("GPL");
0115 MODULE_IMPORT_NS(IIO_BMA400);