1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com>
7 #include <linux/mod_devicetable.h>
8 #include <linux/module.h>
9 #include <linux/regmap.h>
10 #include <linux/spi/spi.h>
14 struct bme680_spi_bus_context
{
15 struct spi_device
*spi
;
20 * In SPI mode there are only 7 address bits, a "page" register determines
21 * which part of the 8-bit range is active. This function looks at the address
22 * and writes the page selection bit if needed
24 static int bme680_regmap_spi_select_page(
25 struct bme680_spi_bus_context
*ctx
, u8 reg
)
27 struct spi_device
*spi
= ctx
->spi
;
30 u8 page
= (reg
& 0x80) ? 0 : 1; /* Page "1" is low range */
32 if (page
== ctx
->current_page
)
36 * Data sheet claims we're only allowed to change bit 4, so we must do
37 * a read-modify-write on each and every page select
39 buf
[0] = BME680_REG_STATUS
;
40 ret
= spi_write_then_read(spi
, buf
, 1, buf
+ 1, 1);
42 dev_err(&spi
->dev
, "failed to set page %u\n", page
);
46 buf
[0] = BME680_REG_STATUS
;
48 buf
[1] |= BME680_SPI_MEM_PAGE_BIT
;
50 buf
[1] &= ~BME680_SPI_MEM_PAGE_BIT
;
52 ret
= spi_write(spi
, buf
, 2);
54 dev_err(&spi
->dev
, "failed to set page %u\n", page
);
58 ctx
->current_page
= page
;
63 static int bme680_regmap_spi_write(void *context
, const void *data
,
66 struct bme680_spi_bus_context
*ctx
= context
;
67 struct spi_device
*spi
= ctx
->spi
;
73 ret
= bme680_regmap_spi_select_page(ctx
, buf
[0]);
78 * The SPI register address (= full register address without bit 7)
79 * and the write command (bit7 = RW = '0')
83 return spi_write(spi
, buf
, 2);
86 static int bme680_regmap_spi_read(void *context
, const void *reg
,
87 size_t reg_size
, void *val
, size_t val_size
)
89 struct bme680_spi_bus_context
*ctx
= context
;
90 struct spi_device
*spi
= ctx
->spi
;
92 u8 addr
= *(const u8
*)reg
;
94 ret
= bme680_regmap_spi_select_page(ctx
, addr
);
98 addr
|= 0x80; /* bit7 = RW = '1' */
100 return spi_write_then_read(spi
, &addr
, 1, val
, val_size
);
103 static const struct regmap_bus bme680_regmap_bus
= {
104 .write
= bme680_regmap_spi_write
,
105 .read
= bme680_regmap_spi_read
,
106 .reg_format_endian_default
= REGMAP_ENDIAN_BIG
,
107 .val_format_endian_default
= REGMAP_ENDIAN_BIG
,
110 static int bme680_spi_probe(struct spi_device
*spi
)
112 const struct spi_device_id
*id
= spi_get_device_id(spi
);
113 struct bme680_spi_bus_context
*bus_context
;
114 struct regmap
*regmap
;
117 spi
->bits_per_word
= 8;
118 ret
= spi_setup(spi
);
120 dev_err(&spi
->dev
, "spi_setup failed!\n");
124 bus_context
= devm_kzalloc(&spi
->dev
, sizeof(*bus_context
), GFP_KERNEL
);
128 bus_context
->spi
= spi
;
129 bus_context
->current_page
= 0xff; /* Undefined on warm boot */
131 regmap
= devm_regmap_init(&spi
->dev
, &bme680_regmap_bus
,
132 bus_context
, &bme680_regmap_config
);
133 if (IS_ERR(regmap
)) {
134 dev_err(&spi
->dev
, "Failed to register spi regmap %ld\n", PTR_ERR(regmap
));
135 return PTR_ERR(regmap
);
138 return bme680_core_probe(&spi
->dev
, regmap
, id
->name
);
141 static const struct spi_device_id bme680_spi_id
[] = {
145 MODULE_DEVICE_TABLE(spi
, bme680_spi_id
);
147 static const struct of_device_id bme680_of_spi_match
[] = {
148 { .compatible
= "bosch,bme680", },
151 MODULE_DEVICE_TABLE(of
, bme680_of_spi_match
);
153 static struct spi_driver bme680_spi_driver
= {
155 .name
= "bme680_spi",
156 .of_match_table
= bme680_of_spi_match
,
158 .probe
= bme680_spi_probe
,
159 .id_table
= bme680_spi_id
,
161 module_spi_driver(bme680_spi_driver
);
163 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
164 MODULE_DESCRIPTION("Bosch BME680 SPI driver");
165 MODULE_LICENSE("GPL v2");
166 MODULE_IMPORT_NS("IIO_BME680");