bme680_spi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * BME680 - SPI Driver
  4. *
  5. * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com>
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/module.h>
  9. #include <linux/regmap.h>
  10. #include <linux/spi/spi.h>
  11. #include "bme680.h"
  12. struct bme680_spi_bus_context {
  13. struct spi_device *spi;
  14. u8 current_page;
  15. };
  16. /*
  17. * In SPI mode there are only 7 address bits, a "page" register determines
  18. * which part of the 8-bit range is active. This function looks at the address
  19. * and writes the page selection bit if needed
  20. */
  21. static int bme680_regmap_spi_select_page(
  22. struct bme680_spi_bus_context *ctx, u8 reg)
  23. {
  24. struct spi_device *spi = ctx->spi;
  25. int ret;
  26. u8 buf[2];
  27. u8 page = (reg & 0x80) ? 0 : 1; /* Page "1" is low range */
  28. if (page == ctx->current_page)
  29. return 0;
  30. /*
  31. * Data sheet claims we're only allowed to change bit 4, so we must do
  32. * a read-modify-write on each and every page select
  33. */
  34. buf[0] = BME680_REG_STATUS;
  35. ret = spi_write_then_read(spi, buf, 1, buf + 1, 1);
  36. if (ret < 0) {
  37. dev_err(&spi->dev, "failed to set page %u\n", page);
  38. return ret;
  39. }
  40. buf[0] = BME680_REG_STATUS;
  41. if (page)
  42. buf[1] |= BME680_SPI_MEM_PAGE_BIT;
  43. else
  44. buf[1] &= ~BME680_SPI_MEM_PAGE_BIT;
  45. ret = spi_write(spi, buf, 2);
  46. if (ret < 0) {
  47. dev_err(&spi->dev, "failed to set page %u\n", page);
  48. return ret;
  49. }
  50. ctx->current_page = page;
  51. return 0;
  52. }
  53. static int bme680_regmap_spi_write(void *context, const void *data,
  54. size_t count)
  55. {
  56. struct bme680_spi_bus_context *ctx = context;
  57. struct spi_device *spi = ctx->spi;
  58. int ret;
  59. u8 buf[2];
  60. memcpy(buf, data, 2);
  61. ret = bme680_regmap_spi_select_page(ctx, buf[0]);
  62. if (ret)
  63. return ret;
  64. /*
  65. * The SPI register address (= full register address without bit 7)
  66. * and the write command (bit7 = RW = '0')
  67. */
  68. buf[0] &= ~0x80;
  69. return spi_write(spi, buf, 2);
  70. }
  71. static int bme680_regmap_spi_read(void *context, const void *reg,
  72. size_t reg_size, void *val, size_t val_size)
  73. {
  74. struct bme680_spi_bus_context *ctx = context;
  75. struct spi_device *spi = ctx->spi;
  76. int ret;
  77. u8 addr = *(const u8 *)reg;
  78. ret = bme680_regmap_spi_select_page(ctx, addr);
  79. if (ret)
  80. return ret;
  81. addr |= 0x80; /* bit7 = RW = '1' */
  82. return spi_write_then_read(spi, &addr, 1, val, val_size);
  83. }
  84. static struct regmap_bus bme680_regmap_bus = {
  85. .write = bme680_regmap_spi_write,
  86. .read = bme680_regmap_spi_read,
  87. .reg_format_endian_default = REGMAP_ENDIAN_BIG,
  88. .val_format_endian_default = REGMAP_ENDIAN_BIG,
  89. };
  90. static int bme680_spi_probe(struct spi_device *spi)
  91. {
  92. const struct spi_device_id *id = spi_get_device_id(spi);
  93. struct bme680_spi_bus_context *bus_context;
  94. struct regmap *regmap;
  95. int ret;
  96. spi->bits_per_word = 8;
  97. ret = spi_setup(spi);
  98. if (ret < 0) {
  99. dev_err(&spi->dev, "spi_setup failed!\n");
  100. return ret;
  101. }
  102. bus_context = devm_kzalloc(&spi->dev, sizeof(*bus_context), GFP_KERNEL);
  103. if (!bus_context)
  104. return -ENOMEM;
  105. bus_context->spi = spi;
  106. bus_context->current_page = 0xff; /* Undefined on warm boot */
  107. regmap = devm_regmap_init(&spi->dev, &bme680_regmap_bus,
  108. bus_context, &bme680_regmap_config);
  109. if (IS_ERR(regmap)) {
  110. dev_err(&spi->dev, "Failed to register spi regmap %d\n",
  111. (int)PTR_ERR(regmap));
  112. return PTR_ERR(regmap);
  113. }
  114. return bme680_core_probe(&spi->dev, regmap, id->name);
  115. }
  116. static const struct spi_device_id bme680_spi_id[] = {
  117. {"bme680", 0},
  118. {},
  119. };
  120. MODULE_DEVICE_TABLE(spi, bme680_spi_id);
  121. static const struct acpi_device_id bme680_acpi_match[] = {
  122. {"BME0680", 0},
  123. {},
  124. };
  125. MODULE_DEVICE_TABLE(acpi, bme680_acpi_match);
  126. static struct spi_driver bme680_spi_driver = {
  127. .driver = {
  128. .name = "bme680_spi",
  129. .acpi_match_table = ACPI_PTR(bme680_acpi_match),
  130. },
  131. .probe = bme680_spi_probe,
  132. .id_table = bme680_spi_id,
  133. };
  134. module_spi_driver(bme680_spi_driver);
  135. MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
  136. MODULE_DESCRIPTION("Bosch BME680 SPI driver");
  137. MODULE_LICENSE("GPL v2");