bmp280-spi.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. chip_info = spi_get_device_match_data(spi);
  71. if (chip_info->spi_read_extra_byte)
  72. bmp_regmap_bus = &bmp380_regmap_bus;
  73. else
  74. bmp_regmap_bus = &bmp280_regmap_bus;
  75. regmap = devm_regmap_init(&spi->dev,
  76. bmp_regmap_bus,
  77. &spi->dev,
  78. chip_info->regmap_config);
  79. if (IS_ERR(regmap)) {
  80. dev_err(&spi->dev, "failed to allocate register map\n");
  81. return PTR_ERR(regmap);
  82. }
  83. return bmp280_common_probe(&spi->dev,
  84. regmap,
  85. chip_info,
  86. id->name,
  87. spi->irq);
  88. }
  89. static const struct of_device_id bmp280_of_spi_match[] = {
  90. { .compatible = "bosch,bmp085", .data = &bmp085_chip_info },
  91. { .compatible = "bosch,bmp180", .data = &bmp180_chip_info },
  92. { .compatible = "bosch,bmp181", .data = &bmp180_chip_info },
  93. { .compatible = "bosch,bmp280", .data = &bmp280_chip_info },
  94. { .compatible = "bosch,bme280", .data = &bme280_chip_info },
  95. { .compatible = "bosch,bmp380", .data = &bmp380_chip_info },
  96. { .compatible = "bosch,bmp580", .data = &bmp580_chip_info },
  97. { }
  98. };
  99. MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
  100. static const struct spi_device_id bmp280_spi_id[] = {
  101. { "bmp085", (kernel_ulong_t)&bmp085_chip_info },
  102. { "bmp180", (kernel_ulong_t)&bmp180_chip_info },
  103. { "bmp181", (kernel_ulong_t)&bmp180_chip_info },
  104. { "bmp280", (kernel_ulong_t)&bmp280_chip_info },
  105. { "bme280", (kernel_ulong_t)&bme280_chip_info },
  106. { "bmp380", (kernel_ulong_t)&bmp380_chip_info },
  107. { "bmp580", (kernel_ulong_t)&bmp580_chip_info },
  108. { }
  109. };
  110. MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
  111. static struct spi_driver bmp280_spi_driver = {
  112. .driver = {
  113. .name = "bmp280",
  114. .of_match_table = bmp280_of_spi_match,
  115. .pm = pm_ptr(&bmp280_dev_pm_ops),
  116. },
  117. .id_table = bmp280_spi_id,
  118. .probe = bmp280_spi_probe,
  119. };
  120. module_spi_driver(bmp280_spi_driver);
  121. MODULE_DESCRIPTION("BMP280 SPI bus driver");
  122. MODULE_LICENSE("GPL");
  123. MODULE_IMPORT_NS("IIO_BMP280");