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 struct bme680_spi_bus_context
{
16 struct spi_device
*spi
;
21 * In SPI mode there are only 7 address bits, a "page" register determines
22 * which part of the 8-bit range is active. This function looks at the address
23 * and writes the page selection bit if needed
25 static int bme680_regmap_spi_select_page(
26 struct bme680_spi_bus_context
*ctx
, u8 reg
)
28 struct spi_device
*spi
= ctx
->spi
;
31 u8 page
= (reg
& 0x80) ? 0 : 1; /* Page "1" is low range */
33 if (page
== ctx
->current_page
)
37 * Data sheet claims we're only allowed to change bit 4, so we must do
38 * a read-modify-write on each and every page select
40 buf
[0] = BME680_REG_STATUS
;
41 ret
= spi_write_then_read(spi
, buf
, 1, buf
+ 1, 1);
43 dev_err(&spi
->dev
, "failed to set page %u\n", page
);
47 buf
[0] = BME680_REG_STATUS
;
49 buf
[1] |= BME680_SPI_MEM_PAGE_BIT
;
51 buf
[1] &= ~BME680_SPI_MEM_PAGE_BIT
;
53 ret
= spi_write(spi
, buf
, 2);
55 dev_err(&spi
->dev
, "failed to set page %u\n", page
);
59 ctx
->current_page
= page
;
64 static int bme680_regmap_spi_write(void *context
, const void *data
,
67 struct bme680_spi_bus_context
*ctx
= context
;
68 struct spi_device
*spi
= ctx
->spi
;
74 ret
= bme680_regmap_spi_select_page(ctx
, buf
[0]);
79 * The SPI register address (= full register address without bit 7)
80 * and the write command (bit7 = RW = '0')
84 return spi_write(spi
, buf
, 2);
87 static int bme680_regmap_spi_read(void *context
, const void *reg
,
88 size_t reg_size
, void *val
, size_t val_size
)
90 struct bme680_spi_bus_context
*ctx
= context
;
91 struct spi_device
*spi
= ctx
->spi
;
93 u8 addr
= *(const u8
*)reg
;
95 ret
= bme680_regmap_spi_select_page(ctx
, addr
);
99 addr
|= 0x80; /* bit7 = RW = '1' */
101 return spi_write_then_read(spi
, &addr
, 1, val
, val_size
);
104 static struct regmap_bus bme680_regmap_bus
= {
105 .write
= bme680_regmap_spi_write
,
106 .read
= bme680_regmap_spi_read
,
107 .reg_format_endian_default
= REGMAP_ENDIAN_BIG
,
108 .val_format_endian_default
= REGMAP_ENDIAN_BIG
,
111 static int bme680_spi_probe(struct spi_device
*spi
)
113 const struct spi_device_id
*id
= spi_get_device_id(spi
);
114 struct bme680_spi_bus_context
*bus_context
;
115 struct regmap
*regmap
;
118 spi
->bits_per_word
= 8;
119 ret
= spi_setup(spi
);
121 dev_err(&spi
->dev
, "spi_setup failed!\n");
125 bus_context
= devm_kzalloc(&spi
->dev
, sizeof(*bus_context
), GFP_KERNEL
);
129 bus_context
->spi
= spi
;
130 bus_context
->current_page
= 0xff; /* Undefined on warm boot */
132 regmap
= devm_regmap_init(&spi
->dev
, &bme680_regmap_bus
,
133 bus_context
, &bme680_regmap_config
);
134 if (IS_ERR(regmap
)) {
135 dev_err(&spi
->dev
, "Failed to register spi regmap %d\n",
136 (int)PTR_ERR(regmap
));
137 return PTR_ERR(regmap
);
140 return bme680_core_probe(&spi
->dev
, regmap
, id
->name
);
143 static const struct spi_device_id bme680_spi_id
[] = {
147 MODULE_DEVICE_TABLE(spi
, bme680_spi_id
);
149 static const struct acpi_device_id bme680_acpi_match
[] = {
153 MODULE_DEVICE_TABLE(acpi
, bme680_acpi_match
);
155 static const struct of_device_id bme680_of_spi_match
[] = {
156 { .compatible
= "bosch,bme680", },
159 MODULE_DEVICE_TABLE(of
, bme680_of_spi_match
);
161 static struct spi_driver bme680_spi_driver
= {
163 .name
= "bme680_spi",
164 .acpi_match_table
= ACPI_PTR(bme680_acpi_match
),
165 .of_match_table
= bme680_of_spi_match
,
167 .probe
= bme680_spi_probe
,
168 .id_table
= bme680_spi_id
,
170 module_spi_driver(bme680_spi_driver
);
172 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
173 MODULE_DESCRIPTION("Bosch BME680 SPI driver");
174 MODULE_LICENSE("GPL v2");