bma400_spi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SPI IIO driver for Bosch BMA400 triaxial acceleration sensor.
  4. *
  5. * Copyright 2020 Dan Robertson <[email protected]>
  6. *
  7. */
  8. #include <linux/bits.h>
  9. #include <linux/init.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include <linux/spi/spi.h>
  14. #include "bma400.h"
  15. #define BMA400_MAX_SPI_READ 2
  16. #define BMA400_SPI_READ_BUFFER_SIZE (BMA400_MAX_SPI_READ + 1)
  17. static int bma400_regmap_spi_read(void *context,
  18. const void *reg, size_t reg_size,
  19. void *val, size_t val_size)
  20. {
  21. struct device *dev = context;
  22. struct spi_device *spi = to_spi_device(dev);
  23. u8 result[BMA400_SPI_READ_BUFFER_SIZE];
  24. ssize_t status;
  25. if (val_size > BMA400_MAX_SPI_READ)
  26. return -EINVAL;
  27. status = spi_write_then_read(spi, reg, 1, result, val_size + 1);
  28. if (status)
  29. return status;
  30. /*
  31. * From the BMA400 datasheet:
  32. *
  33. * > For a basic read operation two bytes have to be read and the first
  34. * > has to be dropped and the second byte must be interpreted.
  35. */
  36. memcpy(val, result + 1, val_size);
  37. return 0;
  38. }
  39. static int bma400_regmap_spi_write(void *context, const void *data,
  40. size_t count)
  41. {
  42. struct device *dev = context;
  43. struct spi_device *spi = to_spi_device(dev);
  44. return spi_write(spi, data, count);
  45. }
  46. static struct regmap_bus bma400_regmap_bus = {
  47. .read = bma400_regmap_spi_read,
  48. .write = bma400_regmap_spi_write,
  49. .read_flag_mask = BIT(7),
  50. .max_raw_read = BMA400_MAX_SPI_READ,
  51. };
  52. static int bma400_spi_probe(struct spi_device *spi)
  53. {
  54. const struct spi_device_id *id = spi_get_device_id(spi);
  55. struct regmap *regmap;
  56. unsigned int val;
  57. int ret;
  58. regmap = devm_regmap_init(&spi->dev, &bma400_regmap_bus,
  59. &spi->dev, &bma400_regmap_config);
  60. if (IS_ERR(regmap)) {
  61. dev_err(&spi->dev, "failed to create regmap\n");
  62. return PTR_ERR(regmap);
  63. }
  64. /*
  65. * Per the bma400 datasheet, the first SPI read may
  66. * return garbage. As the datasheet recommends, the
  67. * chip ID register will be read here and checked
  68. * again in the following probe.
  69. */
  70. ret = regmap_read(regmap, BMA400_CHIP_ID_REG, &val);
  71. if (ret)
  72. dev_err(&spi->dev, "Failed to read chip id register\n");
  73. return bma400_probe(&spi->dev, regmap, spi->irq, id->name);
  74. }
  75. static const struct spi_device_id bma400_spi_ids[] = {
  76. { "bma400", 0 },
  77. { }
  78. };
  79. MODULE_DEVICE_TABLE(spi, bma400_spi_ids);
  80. static const struct of_device_id bma400_of_spi_match[] = {
  81. { .compatible = "bosch,bma400" },
  82. { }
  83. };
  84. MODULE_DEVICE_TABLE(of, bma400_of_spi_match);
  85. static struct spi_driver bma400_spi_driver = {
  86. .driver = {
  87. .name = "bma400",
  88. .of_match_table = bma400_of_spi_match,
  89. },
  90. .probe = bma400_spi_probe,
  91. .id_table = bma400_spi_ids,
  92. };
  93. module_spi_driver(bma400_spi_driver);
  94. MODULE_AUTHOR("Dan Robertson <[email protected]>");
  95. MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor (SPI)");
  96. MODULE_LICENSE("GPL");
  97. MODULE_IMPORT_NS(IIO_BMA400);