blob: 3b838068a7e48d7378597aeec7eef7da5953cd58 [file] [log] [blame]
Himanshu Jha1b3bd852018-07-26 17:05:10 +05301// 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>
Sebastien Bourdelinf7da8842019-01-14 15:19:13 -05009#include <linux/of.h>
Himanshu Jha1b3bd852018-07-26 17:05:10 +053010#include <linux/regmap.h>
11#include <linux/spi/spi.h>
12
13#include "bme680.h"
14
Mike Looijmans73f3bc62019-03-06 08:31:48 +010015struct 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 */
25static 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
Himanshu Jha1b3bd852018-07-26 17:05:10 +053064static int bme680_regmap_spi_write(void *context, const void *data,
65 size_t count)
66{
Mike Looijmans73f3bc62019-03-06 08:31:48 +010067 struct bme680_spi_bus_context *ctx = context;
68 struct spi_device *spi = ctx->spi;
69 int ret;
Himanshu Jha1b3bd852018-07-26 17:05:10 +053070 u8 buf[2];
71
72 memcpy(buf, data, 2);
Mike Looijmans73f3bc62019-03-06 08:31:48 +010073
74 ret = bme680_regmap_spi_select_page(ctx, buf[0]);
75 if (ret)
76 return ret;
77
Himanshu Jha1b3bd852018-07-26 17:05:10 +053078 /*
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
Mike Looijmans73f3bc62019-03-06 08:31:48 +010084 return spi_write(spi, buf, 2);
Himanshu Jha1b3bd852018-07-26 17:05:10 +053085}
86
87static int bme680_regmap_spi_read(void *context, const void *reg,
88 size_t reg_size, void *val, size_t val_size)
89{
Mike Looijmans73f3bc62019-03-06 08:31:48 +010090 struct bme680_spi_bus_context *ctx = context;
91 struct spi_device *spi = ctx->spi;
92 int ret;
93 u8 addr = *(const u8 *)reg;
Himanshu Jha1b3bd852018-07-26 17:05:10 +053094
Mike Looijmans73f3bc62019-03-06 08:31:48 +010095 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);
Himanshu Jha1b3bd852018-07-26 17:05:10 +0530102}
103
104static 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
111static int bme680_spi_probe(struct spi_device *spi)
112{
113 const struct spi_device_id *id = spi_get_device_id(spi);
Mike Looijmans73f3bc62019-03-06 08:31:48 +0100114 struct bme680_spi_bus_context *bus_context;
Himanshu Jha1b3bd852018-07-26 17:05:10 +0530115 struct regmap *regmap;
Himanshu Jha1b3bd852018-07-26 17:05:10 +0530116 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
Mike Looijmans73f3bc62019-03-06 08:31:48 +0100125 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
Himanshu Jha1b3bd852018-07-26 17:05:10 +0530132 regmap = devm_regmap_init(&spi->dev, &bme680_regmap_bus,
Mike Looijmans73f3bc62019-03-06 08:31:48 +0100133 bus_context, &bme680_regmap_config);
Himanshu Jha1b3bd852018-07-26 17:05:10 +0530134 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
Himanshu Jha1b3bd852018-07-26 17:05:10 +0530140 return bme680_core_probe(&spi->dev, regmap, id->name);
141}
142
143static const struct spi_device_id bme680_spi_id[] = {
144 {"bme680", 0},
145 {},
146};
147MODULE_DEVICE_TABLE(spi, bme680_spi_id);
148
149static const struct acpi_device_id bme680_acpi_match[] = {
150 {"BME0680", 0},
151 {},
152};
153MODULE_DEVICE_TABLE(acpi, bme680_acpi_match);
154
Sebastien Bourdelinf7da8842019-01-14 15:19:13 -0500155static const struct of_device_id bme680_of_spi_match[] = {
156 { .compatible = "bosch,bme680", },
157 {},
158};
159MODULE_DEVICE_TABLE(of, bme680_of_spi_match);
160
Himanshu Jha1b3bd852018-07-26 17:05:10 +0530161static struct spi_driver bme680_spi_driver = {
162 .driver = {
163 .name = "bme680_spi",
164 .acpi_match_table = ACPI_PTR(bme680_acpi_match),
Sebastien Bourdelinf7da8842019-01-14 15:19:13 -0500165 .of_match_table = bme680_of_spi_match,
Himanshu Jha1b3bd852018-07-26 17:05:10 +0530166 },
167 .probe = bme680_spi_probe,
168 .id_table = bme680_spi_id,
169};
170module_spi_driver(bme680_spi_driver);
171
172MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
173MODULE_DESCRIPTION("Bosch BME680 SPI driver");
174MODULE_LICENSE("GPL v2");