bmp280-spi.c 2.9 KB

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