Re: [PATCH 7/9] iio: pressure: bmp280: add SPI interface driver

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2016-06-20 3:34 GMT+09:00 Linus Walleij <linus.walleij@xxxxxxxxxx>:
> This patch mimics the SPI functionality found in the misc driver in
> drivers/misc/bh085-spi.c to make it possible to reuse the existing
> BMP280/BMP180/BMP085 driver with all clients of the other driver.
> The adoption is straight-forward since like the other driver, it is
> a simple matter of using regmap.
>
> This driver is also so obviously inspired/copied from the old misc
> driver in drivers/misc/bmp085.c that I just took the liberty to
> add in the authors of the other drivers + self in the core driver
> file.
>
> Signed-off-by: Linus Walleij <linus.walleij@xxxxxxxxxx>

...

> +static int bmp280_spi_probe(struct spi_device *spi)
> +{
> +       const struct spi_device_id *id = spi_get_device_id(spi);
> +       struct regmap *regmap;
> +       const struct regmap_config *regmap_config;
> +       int ret;
> +
> +       spi->bits_per_word = 8;
> +       ret = spi_setup(spi);
> +       if (ret < 0) {
> +               dev_err(&spi->dev, "spi_setup failed!\n");
> +               return ret;
> +       }
> +
> +       switch (id->driver_data) {
> +       case BMP180_CHIP_ID:
> +               regmap_config = &bmp180_regmap_config;
> +               break;
> +       case BMP280_CHIP_ID:
> +               regmap_config = &bmp280_regmap_config;
> +               break;
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       regmap = devm_regmap_init_spi(spi, regmap_config);

I was also trying to support SPI interface for this driver and found
that we can't simply use regmap_init_spi().

Quote from 5.3 SPI interface in BMP280 datasheet:

        In SPI mode, only 7 bits of the register addresses are used; the MSB of
        register address is not used and replaced by a read/write bit (RW = ‘0’
        for write and RW = ‘1’ for read).  Example: address 0xF7 is accessed by
        using SPI register address 0x77. For write access, the byte 0x77 is
        transferred, for read access, the byte 0xF7 is transferred.

The write operation used by regmap_init_spi() doesn't replace the RW bit.
So we need custom regmap_bus like below.

static int bmp280_regmap_spi_write(void *context, const void *data,
                                   size_t count)
{
        struct device *dev = context;
        struct spi_device *spi = to_spi_device(dev);
        u8 buf[2];

        memcpy(buf, data, 2);

        /*
         * The SPI register address (= full register address without bit 7) and
         * the write command (bit7 = RW = '0')
         */
        buf[0] &= ~0x80;

        return spi_write_then_read(spi, buf, 2, NULL, 0);
}

static int bmp280_regmap_spi_read(void *context, const void *reg,
                                  size_t reg_size, void *val, size_t val_size)
{
        struct device *dev = context;
        struct spi_device *spi = to_spi_device(dev);

        return spi_write_then_read(spi, reg, reg_size, val, val_size);
}

static struct regmap_bus bmp280_regmap_spi = {
        .write = bmp280_regmap_spi_write,
        .read = bmp280_regmap_spi_read,
};
--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux