18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * SPI interface for the BMP280 driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
98c2ecf20Sopenharmony_ci#include <linux/err.h>
108c2ecf20Sopenharmony_ci#include <linux/regmap.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "bmp280.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic int bmp280_regmap_spi_write(void *context, const void *data,
158c2ecf20Sopenharmony_ci                                   size_t count)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	struct device *dev = context;
188c2ecf20Sopenharmony_ci	struct spi_device *spi = to_spi_device(dev);
198c2ecf20Sopenharmony_ci	u8 buf[2];
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	memcpy(buf, data, 2);
228c2ecf20Sopenharmony_ci	/*
238c2ecf20Sopenharmony_ci	 * The SPI register address (= full register address without bit 7) and
248c2ecf20Sopenharmony_ci	 * the write command (bit7 = RW = '0')
258c2ecf20Sopenharmony_ci	 */
268c2ecf20Sopenharmony_ci	buf[0] &= ~0x80;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	return spi_write_then_read(spi, buf, 2, NULL, 0);
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic int bmp280_regmap_spi_read(void *context, const void *reg,
328c2ecf20Sopenharmony_ci                                  size_t reg_size, void *val, size_t val_size)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct device *dev = context;
358c2ecf20Sopenharmony_ci	struct spi_device *spi = to_spi_device(dev);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	return spi_write_then_read(spi, reg, reg_size, val, val_size);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic struct regmap_bus bmp280_regmap_bus = {
418c2ecf20Sopenharmony_ci	.write = bmp280_regmap_spi_write,
428c2ecf20Sopenharmony_ci	.read = bmp280_regmap_spi_read,
438c2ecf20Sopenharmony_ci	.reg_format_endian_default = REGMAP_ENDIAN_BIG,
448c2ecf20Sopenharmony_ci	.val_format_endian_default = REGMAP_ENDIAN_BIG,
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int bmp280_spi_probe(struct spi_device *spi)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	const struct spi_device_id *id = spi_get_device_id(spi);
508c2ecf20Sopenharmony_ci	struct regmap *regmap;
518c2ecf20Sopenharmony_ci	const struct regmap_config *regmap_config;
528c2ecf20Sopenharmony_ci	int ret;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	spi->bits_per_word = 8;
558c2ecf20Sopenharmony_ci	ret = spi_setup(spi);
568c2ecf20Sopenharmony_ci	if (ret < 0) {
578c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "spi_setup failed!\n");
588c2ecf20Sopenharmony_ci		return ret;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	switch (id->driver_data) {
628c2ecf20Sopenharmony_ci	case BMP180_CHIP_ID:
638c2ecf20Sopenharmony_ci		regmap_config = &bmp180_regmap_config;
648c2ecf20Sopenharmony_ci		break;
658c2ecf20Sopenharmony_ci	case BMP280_CHIP_ID:
668c2ecf20Sopenharmony_ci	case BME280_CHIP_ID:
678c2ecf20Sopenharmony_ci		regmap_config = &bmp280_regmap_config;
688c2ecf20Sopenharmony_ci		break;
698c2ecf20Sopenharmony_ci	default:
708c2ecf20Sopenharmony_ci		return -EINVAL;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	regmap = devm_regmap_init(&spi->dev,
748c2ecf20Sopenharmony_ci				  &bmp280_regmap_bus,
758c2ecf20Sopenharmony_ci				  &spi->dev,
768c2ecf20Sopenharmony_ci				  regmap_config);
778c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
788c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "failed to allocate register map\n");
798c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	return bmp280_common_probe(&spi->dev,
838c2ecf20Sopenharmony_ci				   regmap,
848c2ecf20Sopenharmony_ci				   id->driver_data,
858c2ecf20Sopenharmony_ci				   id->name,
868c2ecf20Sopenharmony_ci				   spi->irq);
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic const struct of_device_id bmp280_of_spi_match[] = {
908c2ecf20Sopenharmony_ci	{ .compatible = "bosch,bmp085", },
918c2ecf20Sopenharmony_ci	{ .compatible = "bosch,bmp180", },
928c2ecf20Sopenharmony_ci	{ .compatible = "bosch,bmp181", },
938c2ecf20Sopenharmony_ci	{ .compatible = "bosch,bmp280", },
948c2ecf20Sopenharmony_ci	{ .compatible = "bosch,bme280", },
958c2ecf20Sopenharmony_ci	{ },
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic const struct spi_device_id bmp280_spi_id[] = {
1008c2ecf20Sopenharmony_ci	{ "bmp180", BMP180_CHIP_ID },
1018c2ecf20Sopenharmony_ci	{ "bmp181", BMP180_CHIP_ID },
1028c2ecf20Sopenharmony_ci	{ "bmp280", BMP280_CHIP_ID },
1038c2ecf20Sopenharmony_ci	{ "bme280", BME280_CHIP_ID },
1048c2ecf20Sopenharmony_ci	{ }
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, bmp280_spi_id);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic struct spi_driver bmp280_spi_driver = {
1098c2ecf20Sopenharmony_ci	.driver = {
1108c2ecf20Sopenharmony_ci		.name = "bmp280",
1118c2ecf20Sopenharmony_ci		.of_match_table = bmp280_of_spi_match,
1128c2ecf20Sopenharmony_ci		.pm = &bmp280_dev_pm_ops,
1138c2ecf20Sopenharmony_ci	},
1148c2ecf20Sopenharmony_ci	.id_table = bmp280_spi_id,
1158c2ecf20Sopenharmony_ci	.probe = bmp280_spi_probe,
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_cimodule_spi_driver(bmp280_spi_driver);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("BMP280 SPI bus driver");
1208c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
121