1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * SPI interface for the BMP280 driver 4 * 5 * Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c 6 */ 7 #include <linux/module.h> 8 #include <linux/spi/spi.h> 9 #include <linux/err.h> 10 #include <linux/regmap.h> 11 12 #include "bmp280.h" 13 14 static int bmp280_regmap_spi_write(void *context, const void *data, 15 size_t count) 16 { 17 struct device *dev = context; 18 struct spi_device *spi = to_spi_device(dev); 19 u8 buf[2]; 20 21 memcpy(buf, data, 2); 22 /* 23 * The SPI register address (= full register address without bit 7) and 24 * 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 bmp280_regmap_spi_read(void *context, const void *reg, 32 size_t reg_size, void *val, size_t val_size) 33 { 34 struct device *dev = context; 35 struct spi_device *spi = to_spi_device(dev); 36 37 return spi_write_then_read(spi, reg, reg_size, val, val_size); 38 } 39 40 static struct regmap_bus bmp280_regmap_bus = { 41 .write = bmp280_regmap_spi_write, 42 .read = bmp280_regmap_spi_read, 43 .reg_format_endian_default = REGMAP_ENDIAN_BIG, 44 .val_format_endian_default = REGMAP_ENDIAN_BIG, 45 }; 46 47 static int bmp280_spi_probe(struct spi_device *spi) 48 { 49 const struct spi_device_id *id = spi_get_device_id(spi); 50 const struct bmp280_chip_info *chip_info; 51 struct regmap *regmap; 52 int ret; 53 54 spi->bits_per_word = 8; 55 ret = spi_setup(spi); 56 if (ret < 0) { 57 dev_err(&spi->dev, "spi_setup failed!\n"); 58 return ret; 59 } 60 61 chip_info = device_get_match_data(&spi->dev); 62 if (!chip_info) 63 chip_info = (const struct bmp280_chip_info *) id->driver_data; 64 65 regmap = devm_regmap_init(&spi->dev, 66 &bmp280_regmap_bus, 67 &spi->dev, 68 chip_info->regmap_config); 69 if (IS_ERR(regmap)) { 70 dev_err(&spi->dev, "failed to allocate register map\n"); 71 return PTR_ERR(regmap); 72 } 73 74 return bmp280_common_probe(&spi->dev, 75 regmap, 76 chip_info, 77 id->name, 78 spi->irq); 79 } 80 81 static const struct of_device_id bmp280_of_spi_match[] = { 82 { .compatible = "bosch,bmp085", .data = &bmp180_chip_info }, 83 { .compatible = "bosch,bmp180", .data = &bmp180_chip_info }, 84 { .compatible = "bosch,bmp181", .data = &bmp180_chip_info }, 85 { .compatible = "bosch,bmp280", .data = &bmp280_chip_info }, 86 { .compatible = "bosch,bme280", .data = &bmp280_chip_info }, 87 { .compatible = "bosch,bmp380", .data = &bmp380_chip_info }, 88 { .compatible = "bosch,bmp580", .data = &bmp580_chip_info }, 89 { }, 90 }; 91 MODULE_DEVICE_TABLE(of, bmp280_of_spi_match); 92 93 static const struct spi_device_id bmp280_spi_id[] = { 94 { "bmp085", (kernel_ulong_t)&bmp180_chip_info }, 95 { "bmp180", (kernel_ulong_t)&bmp180_chip_info }, 96 { "bmp181", (kernel_ulong_t)&bmp180_chip_info }, 97 { "bmp280", (kernel_ulong_t)&bmp280_chip_info }, 98 { "bme280", (kernel_ulong_t)&bmp280_chip_info }, 99 { "bmp380", (kernel_ulong_t)&bmp380_chip_info }, 100 { "bmp580", (kernel_ulong_t)&bmp580_chip_info }, 101 { } 102 }; 103 MODULE_DEVICE_TABLE(spi, bmp280_spi_id); 104 105 static struct spi_driver bmp280_spi_driver = { 106 .driver = { 107 .name = "bmp280", 108 .of_match_table = bmp280_of_spi_match, 109 .pm = pm_ptr(&bmp280_dev_pm_ops), 110 }, 111 .id_table = bmp280_spi_id, 112 .probe = bmp280_spi_probe, 113 }; 114 module_spi_driver(bmp280_spi_driver); 115 116 MODULE_DESCRIPTION("BMP280 SPI bus driver"); 117 MODULE_LICENSE("GPL"); 118 MODULE_IMPORT_NS(IIO_BMP280); 119