1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * BME680 - SPI Driver 4 * 5 * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com> 6 */ 7 #include <linux/acpi.h> 8 #include <linux/module.h> 9 #include <linux/regmap.h> 10 #include <linux/spi/spi.h> 11 12 #include "bme680.h" 13 14 static int bme680_regmap_spi_write(void *context, const void *data, 15 size_t count) 16 { 17 struct spi_device *spi = context; 18 u8 buf[2]; 19 20 memcpy(buf, data, 2); 21 /* 22 * The SPI register address (= full register address without bit 7) 23 * and 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 30 static int bme680_regmap_spi_read(void *context, const void *reg, 31 size_t reg_size, void *val, size_t val_size) 32 { 33 struct spi_device *spi = context; 34 35 return spi_write_then_read(spi, reg, reg_size, val, val_size); 36 } 37 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, 43 }; 44 45 static int bme680_spi_probe(struct spi_device *spi) 46 { 47 const struct spi_device_id *id = spi_get_device_id(spi); 48 struct regmap *regmap; 49 unsigned int val; 50 int ret; 51 52 spi->bits_per_word = 8; 53 ret = spi_setup(spi); 54 if (ret < 0) { 55 dev_err(&spi->dev, "spi_setup failed!\n"); 56 return ret; 57 } 58 59 regmap = devm_regmap_init(&spi->dev, &bme680_regmap_bus, 60 &spi->dev, &bme680_regmap_config); 61 if (IS_ERR(regmap)) { 62 dev_err(&spi->dev, "Failed to register spi regmap %d\n", 63 (int)PTR_ERR(regmap)); 64 return PTR_ERR(regmap); 65 } 66 67 ret = regmap_write(regmap, BME680_REG_SOFT_RESET_SPI, 68 BME680_CMD_SOFTRESET); 69 if (ret < 0) { 70 dev_err(&spi->dev, "Failed to reset chip\n"); 71 return ret; 72 } 73 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); 76 if (ret < 0) { 77 dev_err(&spi->dev, "Error reading SPI chip ID\n"); 78 return ret; 79 } 80 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); 84 return -ENODEV; 85 } 86 /* 87 * select Page 1 of spi_mem_page to enable access to 88 * to registers from address 0x00 to 0x7F. 89 */ 90 ret = regmap_write_bits(regmap, BME680_REG_STATUS, 91 BME680_SPI_MEM_PAGE_BIT, 92 BME680_SPI_MEM_PAGE_1_VAL); 93 if (ret < 0) { 94 dev_err(&spi->dev, "failed to set page 1 of spi_mem_page\n"); 95 return ret; 96 } 97 98 return bme680_core_probe(&spi->dev, regmap, id->name); 99 } 100 101 static const struct spi_device_id bme680_spi_id[] = { 102 {"bme680", 0}, 103 {}, 104 }; 105 MODULE_DEVICE_TABLE(spi, bme680_spi_id); 106 107 static const struct acpi_device_id bme680_acpi_match[] = { 108 {"BME0680", 0}, 109 {}, 110 }; 111 MODULE_DEVICE_TABLE(acpi, bme680_acpi_match); 112 113 static struct spi_driver bme680_spi_driver = { 114 .driver = { 115 .name = "bme680_spi", 116 .acpi_match_table = ACPI_PTR(bme680_acpi_match), 117 }, 118 .probe = bme680_spi_probe, 119 .id_table = bme680_spi_id, 120 }; 121 module_spi_driver(bme680_spi_driver); 122 123 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>"); 124 MODULE_DESCRIPTION("Bosch BME680 SPI driver"); 125 MODULE_LICENSE("GPL v2"); 126