Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * BME680 - SPI Driver
0004  *
0005  * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com>
0006  */
0007 #include <linux/mod_devicetable.h>
0008 #include <linux/module.h>
0009 #include <linux/regmap.h>
0010 #include <linux/spi/spi.h>
0011 
0012 #include "bme680.h"
0013 
0014 struct bme680_spi_bus_context {
0015     struct spi_device *spi;
0016     u8 current_page;
0017 };
0018 
0019 /*
0020  * In SPI mode there are only 7 address bits, a "page" register determines
0021  * which part of the 8-bit range is active. This function looks at the address
0022  * and writes the page selection bit if needed
0023  */
0024 static int bme680_regmap_spi_select_page(
0025     struct bme680_spi_bus_context *ctx, u8 reg)
0026 {
0027     struct spi_device *spi = ctx->spi;
0028     int ret;
0029     u8 buf[2];
0030     u8 page = (reg & 0x80) ? 0 : 1; /* Page "1" is low range */
0031 
0032     if (page == ctx->current_page)
0033         return 0;
0034 
0035     /*
0036      * Data sheet claims we're only allowed to change bit 4, so we must do
0037      * a read-modify-write on each and every page select
0038      */
0039     buf[0] = BME680_REG_STATUS;
0040     ret = spi_write_then_read(spi, buf, 1, buf + 1, 1);
0041     if (ret < 0) {
0042         dev_err(&spi->dev, "failed to set page %u\n", page);
0043         return ret;
0044     }
0045 
0046     buf[0] = BME680_REG_STATUS;
0047     if (page)
0048         buf[1] |= BME680_SPI_MEM_PAGE_BIT;
0049     else
0050         buf[1] &= ~BME680_SPI_MEM_PAGE_BIT;
0051 
0052     ret = spi_write(spi, buf, 2);
0053     if (ret < 0) {
0054         dev_err(&spi->dev, "failed to set page %u\n", page);
0055         return ret;
0056     }
0057 
0058     ctx->current_page = page;
0059 
0060     return 0;
0061 }
0062 
0063 static int bme680_regmap_spi_write(void *context, const void *data,
0064                    size_t count)
0065 {
0066     struct bme680_spi_bus_context *ctx = context;
0067     struct spi_device *spi = ctx->spi;
0068     int ret;
0069     u8 buf[2];
0070 
0071     memcpy(buf, data, 2);
0072 
0073     ret = bme680_regmap_spi_select_page(ctx, buf[0]);
0074     if (ret)
0075         return ret;
0076 
0077     /*
0078      * The SPI register address (= full register address without bit 7)
0079      * and the write command (bit7 = RW = '0')
0080      */
0081     buf[0] &= ~0x80;
0082 
0083     return spi_write(spi, buf, 2);
0084 }
0085 
0086 static int bme680_regmap_spi_read(void *context, const void *reg,
0087                   size_t reg_size, void *val, size_t val_size)
0088 {
0089     struct bme680_spi_bus_context *ctx = context;
0090     struct spi_device *spi = ctx->spi;
0091     int ret;
0092     u8 addr = *(const u8 *)reg;
0093 
0094     ret = bme680_regmap_spi_select_page(ctx, addr);
0095     if (ret)
0096         return ret;
0097 
0098     addr |= 0x80; /* bit7 = RW = '1' */
0099 
0100     return spi_write_then_read(spi, &addr, 1, val, val_size);
0101 }
0102 
0103 static struct regmap_bus bme680_regmap_bus = {
0104     .write = bme680_regmap_spi_write,
0105     .read = bme680_regmap_spi_read,
0106     .reg_format_endian_default = REGMAP_ENDIAN_BIG,
0107     .val_format_endian_default = REGMAP_ENDIAN_BIG,
0108 };
0109 
0110 static int bme680_spi_probe(struct spi_device *spi)
0111 {
0112     const struct spi_device_id *id = spi_get_device_id(spi);
0113     struct bme680_spi_bus_context *bus_context;
0114     struct regmap *regmap;
0115     int ret;
0116 
0117     spi->bits_per_word = 8;
0118     ret = spi_setup(spi);
0119     if (ret < 0) {
0120         dev_err(&spi->dev, "spi_setup failed!\n");
0121         return ret;
0122     }
0123 
0124     bus_context = devm_kzalloc(&spi->dev, sizeof(*bus_context), GFP_KERNEL);
0125     if (!bus_context)
0126         return -ENOMEM;
0127 
0128     bus_context->spi = spi;
0129     bus_context->current_page = 0xff; /* Undefined on warm boot */
0130 
0131     regmap = devm_regmap_init(&spi->dev, &bme680_regmap_bus,
0132                   bus_context, &bme680_regmap_config);
0133     if (IS_ERR(regmap)) {
0134         dev_err(&spi->dev, "Failed to register spi regmap %ld\n", PTR_ERR(regmap));
0135         return PTR_ERR(regmap);
0136     }
0137 
0138     return bme680_core_probe(&spi->dev, regmap, id->name);
0139 }
0140 
0141 static const struct spi_device_id bme680_spi_id[] = {
0142     {"bme680", 0},
0143     {},
0144 };
0145 MODULE_DEVICE_TABLE(spi, bme680_spi_id);
0146 
0147 static const struct of_device_id bme680_of_spi_match[] = {
0148     { .compatible = "bosch,bme680", },
0149     {},
0150 };
0151 MODULE_DEVICE_TABLE(of, bme680_of_spi_match);
0152 
0153 static struct spi_driver bme680_spi_driver = {
0154     .driver = {
0155         .name           = "bme680_spi",
0156         .of_match_table     = bme680_of_spi_match,
0157     },
0158     .probe = bme680_spi_probe,
0159     .id_table = bme680_spi_id,
0160 };
0161 module_spi_driver(bme680_spi_driver);
0162 
0163 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
0164 MODULE_DESCRIPTION("Bosch BME680 SPI driver");
0165 MODULE_LICENSE("GPL v2");
0166 MODULE_IMPORT_NS(IIO_BME680);