18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * BME680 - SPI Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/acpi.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/of.h>
108c2ecf20Sopenharmony_ci#include <linux/regmap.h>
118c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "bme680.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct bme680_spi_bus_context {
168c2ecf20Sopenharmony_ci	struct spi_device *spi;
178c2ecf20Sopenharmony_ci	u8 current_page;
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * In SPI mode there are only 7 address bits, a "page" register determines
228c2ecf20Sopenharmony_ci * which part of the 8-bit range is active. This function looks at the address
238c2ecf20Sopenharmony_ci * and writes the page selection bit if needed
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_cistatic int bme680_regmap_spi_select_page(
268c2ecf20Sopenharmony_ci	struct bme680_spi_bus_context *ctx, u8 reg)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	struct spi_device *spi = ctx->spi;
298c2ecf20Sopenharmony_ci	int ret;
308c2ecf20Sopenharmony_ci	u8 buf[2];
318c2ecf20Sopenharmony_ci	u8 page = (reg & 0x80) ? 0 : 1; /* Page "1" is low range */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	if (page == ctx->current_page)
348c2ecf20Sopenharmony_ci		return 0;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	/*
378c2ecf20Sopenharmony_ci	 * Data sheet claims we're only allowed to change bit 4, so we must do
388c2ecf20Sopenharmony_ci	 * a read-modify-write on each and every page select
398c2ecf20Sopenharmony_ci	 */
408c2ecf20Sopenharmony_ci	buf[0] = BME680_REG_STATUS;
418c2ecf20Sopenharmony_ci	ret = spi_write_then_read(spi, buf, 1, buf + 1, 1);
428c2ecf20Sopenharmony_ci	if (ret < 0) {
438c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "failed to set page %u\n", page);
448c2ecf20Sopenharmony_ci		return ret;
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	buf[0] = BME680_REG_STATUS;
488c2ecf20Sopenharmony_ci	if (page)
498c2ecf20Sopenharmony_ci		buf[1] |= BME680_SPI_MEM_PAGE_BIT;
508c2ecf20Sopenharmony_ci	else
518c2ecf20Sopenharmony_ci		buf[1] &= ~BME680_SPI_MEM_PAGE_BIT;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	ret = spi_write(spi, buf, 2);
548c2ecf20Sopenharmony_ci	if (ret < 0) {
558c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "failed to set page %u\n", page);
568c2ecf20Sopenharmony_ci		return ret;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	ctx->current_page = page;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	return 0;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic int bme680_regmap_spi_write(void *context, const void *data,
658c2ecf20Sopenharmony_ci				   size_t count)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	struct bme680_spi_bus_context *ctx = context;
688c2ecf20Sopenharmony_ci	struct spi_device *spi = ctx->spi;
698c2ecf20Sopenharmony_ci	int ret;
708c2ecf20Sopenharmony_ci	u8 buf[2];
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	memcpy(buf, data, 2);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	ret = bme680_regmap_spi_select_page(ctx, buf[0]);
758c2ecf20Sopenharmony_ci	if (ret)
768c2ecf20Sopenharmony_ci		return ret;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/*
798c2ecf20Sopenharmony_ci	 * The SPI register address (= full register address without bit 7)
808c2ecf20Sopenharmony_ci	 * and the write command (bit7 = RW = '0')
818c2ecf20Sopenharmony_ci	 */
828c2ecf20Sopenharmony_ci	buf[0] &= ~0x80;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return spi_write(spi, buf, 2);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int bme680_regmap_spi_read(void *context, const void *reg,
888c2ecf20Sopenharmony_ci				  size_t reg_size, void *val, size_t val_size)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct bme680_spi_bus_context *ctx = context;
918c2ecf20Sopenharmony_ci	struct spi_device *spi = ctx->spi;
928c2ecf20Sopenharmony_ci	int ret;
938c2ecf20Sopenharmony_ci	u8 addr = *(const u8 *)reg;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	ret = bme680_regmap_spi_select_page(ctx, addr);
968c2ecf20Sopenharmony_ci	if (ret)
978c2ecf20Sopenharmony_ci		return ret;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	addr |= 0x80; /* bit7 = RW = '1' */
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return spi_write_then_read(spi, &addr, 1, val, val_size);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic struct regmap_bus bme680_regmap_bus = {
1058c2ecf20Sopenharmony_ci	.write = bme680_regmap_spi_write,
1068c2ecf20Sopenharmony_ci	.read = bme680_regmap_spi_read,
1078c2ecf20Sopenharmony_ci	.reg_format_endian_default = REGMAP_ENDIAN_BIG,
1088c2ecf20Sopenharmony_ci	.val_format_endian_default = REGMAP_ENDIAN_BIG,
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int bme680_spi_probe(struct spi_device *spi)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	const struct spi_device_id *id = spi_get_device_id(spi);
1148c2ecf20Sopenharmony_ci	struct bme680_spi_bus_context *bus_context;
1158c2ecf20Sopenharmony_ci	struct regmap *regmap;
1168c2ecf20Sopenharmony_ci	int ret;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	spi->bits_per_word = 8;
1198c2ecf20Sopenharmony_ci	ret = spi_setup(spi);
1208c2ecf20Sopenharmony_ci	if (ret < 0) {
1218c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "spi_setup failed!\n");
1228c2ecf20Sopenharmony_ci		return ret;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	bus_context = devm_kzalloc(&spi->dev, sizeof(*bus_context), GFP_KERNEL);
1268c2ecf20Sopenharmony_ci	if (!bus_context)
1278c2ecf20Sopenharmony_ci		return -ENOMEM;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	bus_context->spi = spi;
1308c2ecf20Sopenharmony_ci	bus_context->current_page = 0xff; /* Undefined on warm boot */
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	regmap = devm_regmap_init(&spi->dev, &bme680_regmap_bus,
1338c2ecf20Sopenharmony_ci				  bus_context, &bme680_regmap_config);
1348c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
1358c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "Failed to register spi regmap %d\n",
1368c2ecf20Sopenharmony_ci				(int)PTR_ERR(regmap));
1378c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	return bme680_core_probe(&spi->dev, regmap, id->name);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic const struct spi_device_id bme680_spi_id[] = {
1448c2ecf20Sopenharmony_ci	{"bme680", 0},
1458c2ecf20Sopenharmony_ci	{},
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, bme680_spi_id);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic const struct acpi_device_id bme680_acpi_match[] = {
1508c2ecf20Sopenharmony_ci	{"BME0680", 0},
1518c2ecf20Sopenharmony_ci	{},
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, bme680_acpi_match);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic const struct of_device_id bme680_of_spi_match[] = {
1568c2ecf20Sopenharmony_ci	{ .compatible = "bosch,bme680", },
1578c2ecf20Sopenharmony_ci	{},
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, bme680_of_spi_match);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic struct spi_driver bme680_spi_driver = {
1628c2ecf20Sopenharmony_ci	.driver = {
1638c2ecf20Sopenharmony_ci		.name			= "bme680_spi",
1648c2ecf20Sopenharmony_ci		.acpi_match_table	= ACPI_PTR(bme680_acpi_match),
1658c2ecf20Sopenharmony_ci		.of_match_table		= bme680_of_spi_match,
1668c2ecf20Sopenharmony_ci	},
1678c2ecf20Sopenharmony_ci	.probe = bme680_spi_probe,
1688c2ecf20Sopenharmony_ci	.id_table = bme680_spi_id,
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_cimodule_spi_driver(bme680_spi_driver);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ciMODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
1738c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Bosch BME680 SPI driver");
1748c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
175