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>
9 #include <linux/regmap.h>
10 #include <linux/spi/spi.h>
14 static int bme680_regmap_spi_write(void *context
, const void *data
,
17 struct spi_device
*spi
= context
;
22 * The SPI register address (= full register address without bit 7)
23 * and the write command (bit7 = RW = '0')
27 return spi_write_then_read(spi
, buf
, 2, NULL
, 0);
30 static int bme680_regmap_spi_read(void *context
, const void *reg
,
31 size_t reg_size
, void *val
, size_t val_size
)
33 struct spi_device
*spi
= context
;
35 return spi_write_then_read(spi
, reg
, reg_size
, val
, val_size
);
38 static struct regmap_bus bme680_regmap_bus
= {
39 .write
= bme680_regmap_spi_write
,
40 .read
= bme680_regmap_spi_read
,
41 .reg_format_endian_default
= REGMAP_ENDIAN_BIG
,
42 .val_format_endian_default
= REGMAP_ENDIAN_BIG
,
45 static int bme680_spi_probe(struct spi_device
*spi
)
47 const struct spi_device_id
*id
= spi_get_device_id(spi
);
48 struct regmap
*regmap
;
52 spi
->bits_per_word
= 8;
55 dev_err(&spi
->dev
, "spi_setup failed!\n");
59 regmap
= devm_regmap_init(&spi
->dev
, &bme680_regmap_bus
,
60 &spi
->dev
, &bme680_regmap_config
);
62 dev_err(&spi
->dev
, "Failed to register spi regmap %d\n",
63 (int)PTR_ERR(regmap
));
64 return PTR_ERR(regmap
);
67 ret
= regmap_write(regmap
, BME680_REG_SOFT_RESET_SPI
,
68 BME680_CMD_SOFTRESET
);
70 dev_err(&spi
->dev
, "Failed to reset chip\n");
74 /* after power-on reset, Page 0(0x80-0xFF) of spi_mem_page is active */
75 ret
= regmap_read(regmap
, BME680_REG_CHIP_SPI_ID
, &val
);
77 dev_err(&spi
->dev
, "Error reading SPI chip ID\n");
81 if (val
!= BME680_CHIP_ID_VAL
) {
82 dev_err(&spi
->dev
, "Wrong chip ID, got %x expected %x\n",
83 val
, BME680_CHIP_ID_VAL
);
87 * select Page 1 of spi_mem_page to enable access to
88 * to registers from address 0x00 to 0x7F.
90 ret
= regmap_write_bits(regmap
, BME680_REG_STATUS
,
91 BME680_SPI_MEM_PAGE_BIT
,
92 BME680_SPI_MEM_PAGE_1_VAL
);
94 dev_err(&spi
->dev
, "failed to set page 1 of spi_mem_page\n");
98 return bme680_core_probe(&spi
->dev
, regmap
, id
->name
);
101 static const struct spi_device_id bme680_spi_id
[] = {
105 MODULE_DEVICE_TABLE(spi
, bme680_spi_id
);
107 static const struct acpi_device_id bme680_acpi_match
[] = {
111 MODULE_DEVICE_TABLE(acpi
, bme680_acpi_match
);
113 static struct spi_driver bme680_spi_driver
= {
115 .name
= "bme680_spi",
116 .acpi_match_table
= ACPI_PTR(bme680_acpi_match
),
118 .probe
= bme680_spi_probe
,
119 .id_table
= bme680_spi_id
,
121 module_spi_driver(bme680_spi_driver
);
123 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
124 MODULE_DESCRIPTION("Bosch BME680 SPI driver");
125 MODULE_LICENSE("GPL v2");