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/of.h>
10 #include <linux/regmap.h>
11 #include <linux/spi/spi.h>
12 
13 #include "bme680.h"
14 
15 struct bme680_spi_bus_context {
16 	struct spi_device *spi;
17 	u8 current_page;
18 };
19 
20 /*
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
24  */
25 static int bme680_regmap_spi_select_page(
26 	struct bme680_spi_bus_context *ctx, u8 reg)
27 {
28 	struct spi_device *spi = ctx->spi;
29 	int ret;
30 	u8 buf[2];
31 	u8 page = (reg & 0x80) ? 0 : 1; /* Page "1" is low range */
32 
33 	if (page == ctx->current_page)
34 		return 0;
35 
36 	/*
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
39 	 */
40 	buf[0] = BME680_REG_STATUS;
41 	ret = spi_write_then_read(spi, buf, 1, buf + 1, 1);
42 	if (ret < 0) {
43 		dev_err(&spi->dev, "failed to set page %u\n", page);
44 		return ret;
45 	}
46 
47 	buf[0] = BME680_REG_STATUS;
48 	if (page)
49 		buf[1] |= BME680_SPI_MEM_PAGE_BIT;
50 	else
51 		buf[1] &= ~BME680_SPI_MEM_PAGE_BIT;
52 
53 	ret = spi_write(spi, buf, 2);
54 	if (ret < 0) {
55 		dev_err(&spi->dev, "failed to set page %u\n", page);
56 		return ret;
57 	}
58 
59 	ctx->current_page = page;
60 
61 	return 0;
62 }
63 
64 static int bme680_regmap_spi_write(void *context, const void *data,
65 				   size_t count)
66 {
67 	struct bme680_spi_bus_context *ctx = context;
68 	struct spi_device *spi = ctx->spi;
69 	int ret;
70 	u8 buf[2];
71 
72 	memcpy(buf, data, 2);
73 
74 	ret = bme680_regmap_spi_select_page(ctx, buf[0]);
75 	if (ret)
76 		return ret;
77 
78 	/*
79 	 * The SPI register address (= full register address without bit 7)
80 	 * and the write command (bit7 = RW = '0')
81 	 */
82 	buf[0] &= ~0x80;
83 
84 	return spi_write(spi, buf, 2);
85 }
86 
87 static int bme680_regmap_spi_read(void *context, const void *reg,
88 				  size_t reg_size, void *val, size_t val_size)
89 {
90 	struct bme680_spi_bus_context *ctx = context;
91 	struct spi_device *spi = ctx->spi;
92 	int ret;
93 	u8 addr = *(const u8 *)reg;
94 
95 	ret = bme680_regmap_spi_select_page(ctx, addr);
96 	if (ret)
97 		return ret;
98 
99 	addr |= 0x80; /* bit7 = RW = '1' */
100 
101 	return spi_write_then_read(spi, &addr, 1, val, val_size);
102 }
103 
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,
109 };
110 
111 static int bme680_spi_probe(struct spi_device *spi)
112 {
113 	const struct spi_device_id *id = spi_get_device_id(spi);
114 	struct bme680_spi_bus_context *bus_context;
115 	struct regmap *regmap;
116 	int ret;
117 
118 	spi->bits_per_word = 8;
119 	ret = spi_setup(spi);
120 	if (ret < 0) {
121 		dev_err(&spi->dev, "spi_setup failed!\n");
122 		return ret;
123 	}
124 
125 	bus_context = devm_kzalloc(&spi->dev, sizeof(*bus_context), GFP_KERNEL);
126 	if (!bus_context)
127 		return -ENOMEM;
128 
129 	bus_context->spi = spi;
130 	bus_context->current_page = 0xff; /* Undefined on warm boot */
131 
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);
138 	}
139 
140 	return bme680_core_probe(&spi->dev, regmap, id->name);
141 }
142 
143 static const struct spi_device_id bme680_spi_id[] = {
144 	{"bme680", 0},
145 	{},
146 };
147 MODULE_DEVICE_TABLE(spi, bme680_spi_id);
148 
149 static const struct acpi_device_id bme680_acpi_match[] = {
150 	{"BME0680", 0},
151 	{},
152 };
153 MODULE_DEVICE_TABLE(acpi, bme680_acpi_match);
154 
155 static const struct of_device_id bme680_of_spi_match[] = {
156 	{ .compatible = "bosch,bme680", },
157 	{},
158 };
159 MODULE_DEVICE_TABLE(of, bme680_of_spi_match);
160 
161 static struct spi_driver bme680_spi_driver = {
162 	.driver = {
163 		.name			= "bme680_spi",
164 		.acpi_match_table	= ACPI_PTR(bme680_acpi_match),
165 		.of_match_table		= bme680_of_spi_match,
166 	},
167 	.probe = bme680_spi_probe,
168 	.id_table = bme680_spi_id,
169 };
170 module_spi_driver(bme680_spi_driver);
171 
172 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
173 MODULE_DESCRIPTION("Bosch BME680 SPI driver");
174 MODULE_LICENSE("GPL v2");
175