Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / iio / chemical / bme680_spi.c
blob3b838068a7e48d7378597aeec7eef7da5953cd58
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * BME680 - SPI Driver
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>
13 #include "bme680.h"
15 struct bme680_spi_bus_context {
16 struct spi_device *spi;
17 u8 current_page;
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;
29 int ret;
30 u8 buf[2];
31 u8 page = (reg & 0x80) ? 0 : 1; /* Page "1" is low range */
33 if (page == ctx->current_page)
34 return 0;
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);
42 if (ret < 0) {
43 dev_err(&spi->dev, "failed to set page %u\n", page);
44 return ret;
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;
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;
59 ctx->current_page = page;
61 return 0;
64 static int bme680_regmap_spi_write(void *context, const void *data,
65 size_t count)
67 struct bme680_spi_bus_context *ctx = context;
68 struct spi_device *spi = ctx->spi;
69 int ret;
70 u8 buf[2];
72 memcpy(buf, data, 2);
74 ret = bme680_regmap_spi_select_page(ctx, buf[0]);
75 if (ret)
76 return ret;
79 * The SPI register address (= full register address without bit 7)
80 * and the write command (bit7 = RW = '0')
82 buf[0] &= ~0x80;
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;
92 int ret;
93 u8 addr = *(const u8 *)reg;
95 ret = bme680_regmap_spi_select_page(ctx, addr);
96 if (ret)
97 return ret;
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;
116 int ret;
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;
125 bus_context = devm_kzalloc(&spi->dev, sizeof(*bus_context), GFP_KERNEL);
126 if (!bus_context)
127 return -ENOMEM;
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[] = {
144 {"bme680", 0},
147 MODULE_DEVICE_TABLE(spi, bme680_spi_id);
149 static const struct acpi_device_id bme680_acpi_match[] = {
150 {"BME0680", 0},
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 = {
162 .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");