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 static int bme680_regmap_spi_write(void *context, const void *data,
16 				   size_t count)
17 {
18 	struct spi_device *spi = context;
19 	u8 buf[2];
20 
21 	memcpy(buf, data, 2);
22 	/*
23 	 * The SPI register address (= full register address without bit 7)
24 	 * and the write command (bit7 = RW = '0')
25 	 */
26 	buf[0] &= ~0x80;
27 
28 	return spi_write_then_read(spi, buf, 2, NULL, 0);
29 }
30 
31 static int bme680_regmap_spi_read(void *context, const void *reg,
32 				  size_t reg_size, void *val, size_t val_size)
33 {
34 	struct spi_device *spi = context;
35 
36 	return spi_write_then_read(spi, reg, reg_size, val, val_size);
37 }
38 
39 static struct regmap_bus bme680_regmap_bus = {
40 	.write = bme680_regmap_spi_write,
41 	.read = bme680_regmap_spi_read,
42 	.reg_format_endian_default = REGMAP_ENDIAN_BIG,
43 	.val_format_endian_default = REGMAP_ENDIAN_BIG,
44 };
45 
46 static int bme680_spi_probe(struct spi_device *spi)
47 {
48 	const struct spi_device_id *id = spi_get_device_id(spi);
49 	struct regmap *regmap;
50 	unsigned int val;
51 	int ret;
52 
53 	spi->bits_per_word = 8;
54 	ret = spi_setup(spi);
55 	if (ret < 0) {
56 		dev_err(&spi->dev, "spi_setup failed!\n");
57 		return ret;
58 	}
59 
60 	regmap = devm_regmap_init(&spi->dev, &bme680_regmap_bus,
61 				  &spi->dev, &bme680_regmap_config);
62 	if (IS_ERR(regmap)) {
63 		dev_err(&spi->dev, "Failed to register spi regmap %d\n",
64 				(int)PTR_ERR(regmap));
65 		return PTR_ERR(regmap);
66 	}
67 
68 	ret = regmap_write(regmap, BME680_REG_SOFT_RESET_SPI,
69 			   BME680_CMD_SOFTRESET);
70 	if (ret < 0) {
71 		dev_err(&spi->dev, "Failed to reset chip\n");
72 		return ret;
73 	}
74 
75 	/* after power-on reset, Page 0(0x80-0xFF) of spi_mem_page is active */
76 	ret = regmap_read(regmap, BME680_REG_CHIP_SPI_ID, &val);
77 	if (ret < 0) {
78 		dev_err(&spi->dev, "Error reading SPI chip ID\n");
79 		return ret;
80 	}
81 
82 	if (val != BME680_CHIP_ID_VAL) {
83 		dev_err(&spi->dev, "Wrong chip ID, got %x expected %x\n",
84 				val, BME680_CHIP_ID_VAL);
85 		return -ENODEV;
86 	}
87 	/*
88 	 * select Page 1 of spi_mem_page to enable access to
89 	 * to registers from address 0x00 to 0x7F.
90 	 */
91 	ret = regmap_write_bits(regmap, BME680_REG_STATUS,
92 				BME680_SPI_MEM_PAGE_BIT,
93 				BME680_SPI_MEM_PAGE_1_VAL);
94 	if (ret < 0) {
95 		dev_err(&spi->dev, "failed to set page 1 of spi_mem_page\n");
96 		return ret;
97 	}
98 
99 	return bme680_core_probe(&spi->dev, regmap, id->name);
100 }
101 
102 static const struct spi_device_id bme680_spi_id[] = {
103 	{"bme680", 0},
104 	{},
105 };
106 MODULE_DEVICE_TABLE(spi, bme680_spi_id);
107 
108 static const struct acpi_device_id bme680_acpi_match[] = {
109 	{"BME0680", 0},
110 	{},
111 };
112 MODULE_DEVICE_TABLE(acpi, bme680_acpi_match);
113 
114 static const struct of_device_id bme680_of_spi_match[] = {
115 	{ .compatible = "bosch,bme680", },
116 	{},
117 };
118 MODULE_DEVICE_TABLE(of, bme680_of_spi_match);
119 
120 static struct spi_driver bme680_spi_driver = {
121 	.driver = {
122 		.name			= "bme680_spi",
123 		.acpi_match_table	= ACPI_PTR(bme680_acpi_match),
124 		.of_match_table		= bme680_of_spi_match,
125 	},
126 	.probe = bme680_spi_probe,
127 	.id_table = bme680_spi_id,
128 };
129 module_spi_driver(bme680_spi_driver);
130 
131 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
132 MODULE_DESCRIPTION("Bosch BME680 SPI driver");
133 MODULE_LICENSE("GPL v2");
134