bmp280-spi.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/bits.h>
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/regmap.h>
  11. #include <linux/spi/spi.h>
  12. #include "bmp280.h"
  13. static int bmp280_regmap_spi_write(void *context, const void *data,
  14. size_t count)
  15. {
  16. struct spi_device *spi = to_spi_device(context);
  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 spi_device *spi = to_spi_device(context);
  30. return spi_write_then_read(spi, reg, reg_size, val, val_size);
  31. }
  32. static int bmp380_regmap_spi_read(void *context, const void *reg,
  33. size_t reg_size, void *val, size_t val_size)
  34. {
  35. struct spi_device *spi = to_spi_device(context);
  36. u8 rx_buf[BME280_BURST_READ_BYTES + 1];
  37. ssize_t status;
  38. if (val_size > BME280_BURST_READ_BYTES)
  39. return -EINVAL;
  40. /*
  41. * According to the BMP3xx datasheets, for a basic SPI read opertion,
  42. * the first byte needs to be dropped and the rest are the requested
  43. * data.
  44. */
  45. status = spi_write_then_read(spi, reg, 1, rx_buf, val_size + 1);
  46. if (status)
  47. return status;
  48. memcpy(val, rx_buf + 1, val_size);
  49. return 0;
  50. }
  51. static const struct regmap_bus bmp280_regmap_bus = {
  52. .write = bmp280_regmap_spi_write,
  53. .read = bmp280_regmap_spi_read,
  54. .reg_format_endian_default = REGMAP_ENDIAN_BIG,
  55. .val_format_endian_default = REGMAP_ENDIAN_BIG,
  56. };
  57. static const struct regmap_bus bmp380_regmap_bus = {
  58. .write = bmp280_regmap_spi_write,
  59. .read = bmp380_regmap_spi_read,
  60. .read_flag_mask = BIT(7),
  61. .reg_format_endian_default = REGMAP_ENDIAN_BIG,
  62. .val_format_endian_default = REGMAP_ENDIAN_BIG,
  63. };
  64. static int bmp280_spi_probe(struct spi_device *spi)
  65. {
  66. const struct spi_device_id *id = spi_get_device_id(spi);
  67. const struct bmp280_chip_info *chip_info;
  68. struct regmap_bus const *bmp_regmap_bus;
  69. struct regmap *regmap;
  70. int ret;
  71. spi->bits_per_word = 8;
  72. ret = spi_setup(spi);
  73. if (ret < 0) {
  74. dev_err(&spi->dev, "spi_setup failed!\n");
  75. return ret;
  76. }
  77. chip_info = spi_get_device_match_data(spi);
  78. if (chip_info->spi_read_extra_byte)
  79. bmp_regmap_bus = &bmp380_regmap_bus;
  80. else
  81. bmp_regmap_bus = &bmp280_regmap_bus;
  82. regmap = devm_regmap_init(&spi->dev,
  83. bmp_regmap_bus,
  84. &spi->dev,
  85. chip_info->regmap_config);
  86. if (IS_ERR(regmap)) {
  87. dev_err(&spi->dev, "failed to allocate register map\n");
  88. return PTR_ERR(regmap);
  89. }
  90. return bmp280_common_probe(&spi->dev,
  91. regmap,
  92. chip_info,
  93. id->name,
  94. spi->irq);
  95. }
  96. static const struct of_device_id bmp280_of_spi_match[] = {
  97. { .compatible = "bosch,bmp085", .data = &bmp180_chip_info },
  98. { .compatible = "bosch,bmp180", .data = &bmp180_chip_info },
  99. { .compatible = "bosch,bmp181", .data = &bmp180_chip_info },
  100. { .compatible = "bosch,bmp280", .data = &bmp280_chip_info },
  101. { .compatible = "bosch,bme280", .data = &bme280_chip_info },
  102. { .compatible = "bosch,bmp380", .data = &bmp380_chip_info },
  103. { .compatible = "bosch,bmp580", .data = &bmp580_chip_info },
  104. { },
  105. };
  106. MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
  107. static const struct spi_device_id bmp280_spi_id[] = {
  108. { "bmp085", (kernel_ulong_t)&bmp180_chip_info },
  109. { "bmp180", (kernel_ulong_t)&bmp180_chip_info },
  110. { "bmp181", (kernel_ulong_t)&bmp180_chip_info },
  111. { "bmp280", (kernel_ulong_t)&bmp280_chip_info },
  112. { "bme280", (kernel_ulong_t)&bme280_chip_info },
  113. { "bmp380", (kernel_ulong_t)&bmp380_chip_info },
  114. { "bmp580", (kernel_ulong_t)&bmp580_chip_info },
  115. { }
  116. };
  117. MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
  118. static struct spi_driver bmp280_spi_driver = {
  119. .driver = {
  120. .name = "bmp280",
  121. .of_match_table = bmp280_of_spi_match,
  122. .pm = pm_ptr(&bmp280_dev_pm_ops),
  123. },
  124. .id_table = bmp280_spi_id,
  125. .probe = bmp280_spi_probe,
  126. };
  127. module_spi_driver(bmp280_spi_driver);
  128. MODULE_DESCRIPTION("BMP280 SPI bus driver");
  129. MODULE_LICENSE("GPL");
  130. MODULE_IMPORT_NS(IIO_BMP280);