1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com>
7 #include <linux/acpi.h>
8 #include <linux/module.h>
10 #include <linux/regmap.h>
11 #include <linux/spi/spi.h>
15 static int bme680_regmap_spi_write(void *context, const void *data,
18 struct spi_device *spi = context;
23 * The SPI register address (= full register address without bit 7)
24 * and the write command (bit7 = RW = '0')
28 return spi_write_then_read(spi, buf, 2, NULL, 0);
31 static int bme680_regmap_spi_read(void *context, const void *reg,
32 size_t reg_size, void *val, size_t val_size)
34 struct spi_device *spi = context;
36 return spi_write_then_read(spi, reg, reg_size, val, val_size);
39 static struct regmap_bus bme680_regmap_bus = {
40 .write = bme680_regmap_spi_write,
41 .read = bme680_regmap_spi_read,
42 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
43 .val_format_endian_default = REGMAP_ENDIAN_BIG,
46 static int bme680_spi_probe(struct spi_device *spi)
48 const struct spi_device_id *id = spi_get_device_id(spi);
49 struct regmap *regmap;
53 spi->bits_per_word = 8;
56 dev_err(&spi->dev, "spi_setup failed!\n");
60 regmap = devm_regmap_init(&spi->dev, &bme680_regmap_bus,
61 &spi->dev, &bme680_regmap_config);
63 dev_err(&spi->dev, "Failed to register spi regmap %d\n",
64 (int)PTR_ERR(regmap));
65 return PTR_ERR(regmap);
68 ret = regmap_write(regmap, BME680_REG_SOFT_RESET_SPI,
69 BME680_CMD_SOFTRESET);
71 dev_err(&spi->dev, "Failed to reset chip\n");
75 /* after power-on reset, Page 0(0x80-0xFF) of spi_mem_page is active */
76 ret = regmap_read(regmap, BME680_REG_CHIP_SPI_ID, &val);
78 dev_err(&spi->dev, "Error reading SPI chip ID\n");
82 if (val != BME680_CHIP_ID_VAL) {
83 dev_err(&spi->dev, "Wrong chip ID, got %x expected %x\n",
84 val, BME680_CHIP_ID_VAL);
88 * select Page 1 of spi_mem_page to enable access to
89 * to registers from address 0x00 to 0x7F.
91 ret = regmap_write_bits(regmap, BME680_REG_STATUS,
92 BME680_SPI_MEM_PAGE_BIT,
93 BME680_SPI_MEM_PAGE_1_VAL);
95 dev_err(&spi->dev, "failed to set page 1 of spi_mem_page\n");
99 return bme680_core_probe(&spi->dev, regmap, id->name);
102 static const struct spi_device_id bme680_spi_id[] = {
106 MODULE_DEVICE_TABLE(spi, bme680_spi_id);
108 static const struct acpi_device_id bme680_acpi_match[] = {
112 MODULE_DEVICE_TABLE(acpi, bme680_acpi_match);
114 static const struct of_device_id bme680_of_spi_match[] = {
115 { .compatible = "bosch,bme680", },
118 MODULE_DEVICE_TABLE(of, bme680_of_spi_match);
120 static struct spi_driver bme680_spi_driver = {
122 .name = "bme680_spi",
123 .acpi_match_table = ACPI_PTR(bme680_acpi_match),
124 .of_match_table = bme680_of_spi_match,
126 .probe = bme680_spi_probe,
127 .id_table = bme680_spi_id,
129 module_spi_driver(bme680_spi_driver);
131 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
132 MODULE_DESCRIPTION("Bosch BME680 SPI driver");
133 MODULE_LICENSE("GPL v2");