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