bmp280-spi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include "bmp280.h"
  12. static int bmp280_regmap_spi_write(void *context, const void *data,
  13. size_t count)
  14. {
  15. struct device *dev = context;
  16. struct spi_device *spi = to_spi_device(dev);
  17. u8 buf[2];
  18. memcpy(buf, data, 2);
  19. /*
  20. * The SPI register address (= full register address without bit 7) and
  21. * the write command (bit7 = RW = '0')
  22. */
  23. buf[0] &= ~0x80;
  24. return spi_write_then_read(spi, buf, 2, NULL, 0);
  25. }
  26. static int bmp280_regmap_spi_read(void *context, const void *reg,
  27. size_t reg_size, void *val, size_t val_size)
  28. {
  29. struct device *dev = context;
  30. struct spi_device *spi = to_spi_device(dev);
  31. return spi_write_then_read(spi, reg, reg_size, val, val_size);
  32. }
  33. static struct regmap_bus bmp280_regmap_bus = {
  34. .write = bmp280_regmap_spi_write,
  35. .read = bmp280_regmap_spi_read,
  36. .reg_format_endian_default = REGMAP_ENDIAN_BIG,
  37. .val_format_endian_default = REGMAP_ENDIAN_BIG,
  38. };
  39. static int bmp280_spi_probe(struct spi_device *spi)
  40. {
  41. const struct spi_device_id *id = spi_get_device_id(spi);
  42. struct regmap *regmap;
  43. const struct regmap_config *regmap_config;
  44. int ret;
  45. spi->bits_per_word = 8;
  46. ret = spi_setup(spi);
  47. if (ret < 0) {
  48. dev_err(&spi->dev, "spi_setup failed!\n");
  49. return ret;
  50. }
  51. switch (id->driver_data) {
  52. case BMP180_CHIP_ID:
  53. regmap_config = &bmp180_regmap_config;
  54. break;
  55. case BMP280_CHIP_ID:
  56. case BME280_CHIP_ID:
  57. regmap_config = &bmp280_regmap_config;
  58. break;
  59. case BMP380_CHIP_ID:
  60. regmap_config = &bmp380_regmap_config;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. regmap = devm_regmap_init(&spi->dev,
  66. &bmp280_regmap_bus,
  67. &spi->dev,
  68. 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. return bmp280_common_probe(&spi->dev,
  74. regmap,
  75. id->driver_data,
  76. id->name,
  77. spi->irq);
  78. }
  79. static const struct of_device_id bmp280_of_spi_match[] = {
  80. { .compatible = "bosch,bmp085", },
  81. { .compatible = "bosch,bmp180", },
  82. { .compatible = "bosch,bmp181", },
  83. { .compatible = "bosch,bmp280", },
  84. { .compatible = "bosch,bme280", },
  85. { .compatible = "bosch,bmp380", },
  86. { },
  87. };
  88. MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
  89. static const struct spi_device_id bmp280_spi_id[] = {
  90. { "bmp180", BMP180_CHIP_ID },
  91. { "bmp181", BMP180_CHIP_ID },
  92. { "bmp280", BMP280_CHIP_ID },
  93. { "bme280", BME280_CHIP_ID },
  94. { "bmp380", BMP380_CHIP_ID },
  95. { }
  96. };
  97. MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
  98. static struct spi_driver bmp280_spi_driver = {
  99. .driver = {
  100. .name = "bmp280",
  101. .of_match_table = bmp280_of_spi_match,
  102. .pm = pm_ptr(&bmp280_dev_pm_ops),
  103. },
  104. .id_table = bmp280_spi_id,
  105. .probe = bmp280_spi_probe,
  106. };
  107. module_spi_driver(bmp280_spi_driver);
  108. MODULE_DESCRIPTION("BMP280 SPI bus driver");
  109. MODULE_LICENSE("GPL");
  110. MODULE_IMPORT_NS(IIO_BMP280);