bme680_spi.c 4.1 KB

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