Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / iio / chemical / bme680_spi.c
blob3916a51ba68e1712d79ed245432b8b40c80e0a00
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/mod_devicetable.h>
8 #include <linux/module.h>
9 #include <linux/regmap.h>
10 #include <linux/spi/spi.h>
12 #include "bme680.h"
14 struct bme680_spi_bus_context {
15 struct spi_device *spi;
16 u8 current_page;
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;
28 int ret;
29 u8 buf[2];
30 u8 page = (reg & 0x80) ? 0 : 1; /* Page "1" is low range */
32 if (page == ctx->current_page)
33 return 0;
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);
41 if (ret < 0) {
42 dev_err(&spi->dev, "failed to set page %u\n", page);
43 return ret;
46 buf[0] = BME680_REG_STATUS;
47 if (page)
48 buf[1] |= BME680_SPI_MEM_PAGE_BIT;
49 else
50 buf[1] &= ~BME680_SPI_MEM_PAGE_BIT;
52 ret = spi_write(spi, buf, 2);
53 if (ret < 0) {
54 dev_err(&spi->dev, "failed to set page %u\n", page);
55 return ret;
58 ctx->current_page = page;
60 return 0;
63 static int bme680_regmap_spi_write(void *context, const void *data,
64 size_t count)
66 struct bme680_spi_bus_context *ctx = context;
67 struct spi_device *spi = ctx->spi;
68 int ret;
69 u8 buf[2];
71 memcpy(buf, data, 2);
73 ret = bme680_regmap_spi_select_page(ctx, buf[0]);
74 if (ret)
75 return ret;
78 * The SPI register address (= full register address without bit 7)
79 * and the write command (bit7 = RW = '0')
81 buf[0] &= ~0x80;
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;
91 int ret;
92 u8 addr = *(const u8 *)reg;
94 ret = bme680_regmap_spi_select_page(ctx, addr);
95 if (ret)
96 return ret;
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;
115 int ret;
117 spi->bits_per_word = 8;
118 ret = spi_setup(spi);
119 if (ret < 0) {
120 dev_err(&spi->dev, "spi_setup failed!\n");
121 return ret;
124 bus_context = devm_kzalloc(&spi->dev, sizeof(*bus_context), GFP_KERNEL);
125 if (!bus_context)
126 return -ENOMEM;
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[] = {
142 {"bme680", 0},
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 = {
154 .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");