linux/drivers/iio/pressure/bmp280-spi.c
<<
>>
Prefs
   1/*
   2 * SPI interface for the BMP280 driver
   3 *
   4 * Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c
   5 */
   6#include <linux/module.h>
   7#include <linux/spi/spi.h>
   8#include <linux/err.h>
   9#include <linux/regmap.h>
  10
  11#include "bmp280.h"
  12
  13static int bmp280_regmap_spi_write(void *context, const void *data,
  14                                   size_t count)
  15{
  16        struct device *dev = context;
  17        struct spi_device *spi = to_spi_device(dev);
  18        u8 buf[2];
  19
  20        memcpy(buf, data, 2);
  21        /*
  22         * The SPI register address (= full register address without bit 7) and
  23         * the write command (bit7 = RW = '0')
  24         */
  25        buf[0] &= ~0x80;
  26
  27        return spi_write_then_read(spi, buf, 2, NULL, 0);
  28}
  29
  30static int bmp280_regmap_spi_read(void *context, const void *reg,
  31                                  size_t reg_size, void *val, size_t val_size)
  32{
  33        struct device *dev = context;
  34        struct spi_device *spi = to_spi_device(dev);
  35
  36        return spi_write_then_read(spi, reg, reg_size, val, val_size);
  37}
  38
  39static struct regmap_bus bmp280_regmap_bus = {
  40        .write = bmp280_regmap_spi_write,
  41        .read = bmp280_regmap_spi_read,
  42        .reg_format_endian_default = REGMAP_ENDIAN_BIG,
  43        .val_format_endian_default = REGMAP_ENDIAN_BIG,
  44};
  45
  46static int bmp280_spi_probe(struct spi_device *spi)
  47{
  48        const struct spi_device_id *id = spi_get_device_id(spi);
  49        struct regmap *regmap;
  50        const struct regmap_config *regmap_config;
  51        int ret;
  52
  53        spi->bits_per_word = 8;
  54        ret = spi_setup(spi);
  55        if (ret < 0) {
  56                dev_err(&spi->dev, "spi_setup failed!\n");
  57                return ret;
  58        }
  59
  60        switch (id->driver_data) {
  61        case BMP180_CHIP_ID:
  62                regmap_config = &bmp180_regmap_config;
  63                break;
  64        case BMP280_CHIP_ID:
  65        case BME280_CHIP_ID:
  66                regmap_config = &bmp280_regmap_config;
  67                break;
  68        default:
  69                return -EINVAL;
  70        }
  71
  72        regmap = devm_regmap_init(&spi->dev,
  73                                  &bmp280_regmap_bus,
  74                                  &spi->dev,
  75                                  regmap_config);
  76        if (IS_ERR(regmap)) {
  77                dev_err(&spi->dev, "failed to allocate register map\n");
  78                return PTR_ERR(regmap);
  79        }
  80
  81        return bmp280_common_probe(&spi->dev,
  82                                   regmap,
  83                                   id->driver_data,
  84                                   id->name,
  85                                   spi->irq);
  86}
  87
  88static int bmp280_spi_remove(struct spi_device *spi)
  89{
  90        return bmp280_common_remove(&spi->dev);
  91}
  92
  93static const struct of_device_id bmp280_of_spi_match[] = {
  94        { .compatible = "bosch,bmp085", },
  95        { .compatible = "bosch,bmp180", },
  96        { .compatible = "bosch,bmp181", },
  97        { .compatible = "bosch,bmp280", },
  98        { .compatible = "bosch,bme280", },
  99        { },
 100};
 101MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
 102
 103static const struct spi_device_id bmp280_spi_id[] = {
 104        { "bmp180", BMP180_CHIP_ID },
 105        { "bmp181", BMP180_CHIP_ID },
 106        { "bmp280", BMP280_CHIP_ID },
 107        { "bme280", BME280_CHIP_ID },
 108        { }
 109};
 110MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
 111
 112static struct spi_driver bmp280_spi_driver = {
 113        .driver = {
 114                .name = "bmp280",
 115                .of_match_table = bmp280_of_spi_match,
 116                .pm = &bmp280_dev_pm_ops,
 117        },
 118        .id_table = bmp280_spi_id,
 119        .probe = bmp280_spi_probe,
 120        .remove = bmp280_spi_remove,
 121};
 122module_spi_driver(bmp280_spi_driver);
 123
 124MODULE_DESCRIPTION("BMP280 SPI bus driver");
 125MODULE_LICENSE("GPL");
 126