bmi323_spi.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SPI driver for Bosch BMI323 6-Axis IMU.
  4. *
  5. * Copyright (C) 2023, Jagath Jog J <jagathjog1996@gmail.com>
  6. */
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/module.h>
  9. #include <linux/regmap.h>
  10. #include <linux/spi/spi.h>
  11. #include "bmi323.h"
  12. /*
  13. * From BMI323 datasheet section 4: Notes on the Serial Interface Support.
  14. * Each SPI register read operation requires to read one dummy byte before
  15. * the actual payload.
  16. */
  17. static int bmi323_regmap_spi_read(void *context, const void *reg_buf,
  18. size_t reg_size, void *val_buf,
  19. size_t val_size)
  20. {
  21. struct spi_device *spi = context;
  22. return spi_write_then_read(spi, reg_buf, reg_size, val_buf, val_size);
  23. }
  24. static int bmi323_regmap_spi_write(void *context, const void *data,
  25. size_t count)
  26. {
  27. struct spi_device *spi = context;
  28. u8 *data_buff = (u8 *)data;
  29. data_buff[1] = data_buff[0];
  30. return spi_write(spi, data_buff + 1, count - 1);
  31. }
  32. static const struct regmap_bus bmi323_regmap_bus = {
  33. .read = bmi323_regmap_spi_read,
  34. .write = bmi323_regmap_spi_write,
  35. };
  36. static const struct regmap_config bmi323_spi_regmap_config = {
  37. .reg_bits = 8,
  38. .val_bits = 16,
  39. .pad_bits = 8,
  40. .read_flag_mask = BIT(7),
  41. .max_register = BMI323_CFG_RES_REG,
  42. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  43. };
  44. static int bmi323_spi_probe(struct spi_device *spi)
  45. {
  46. struct device *dev = &spi->dev;
  47. struct regmap *regmap;
  48. regmap = devm_regmap_init(dev, &bmi323_regmap_bus, dev,
  49. &bmi323_spi_regmap_config);
  50. if (IS_ERR(regmap))
  51. return dev_err_probe(dev, PTR_ERR(regmap),
  52. "Failed to initialize SPI Regmap\n");
  53. return bmi323_core_probe(dev);
  54. }
  55. static const struct spi_device_id bmi323_spi_ids[] = {
  56. { "bmi323" },
  57. { }
  58. };
  59. MODULE_DEVICE_TABLE(spi, bmi323_spi_ids);
  60. static const struct of_device_id bmi323_of_spi_match[] = {
  61. { .compatible = "bosch,bmi323" },
  62. { }
  63. };
  64. MODULE_DEVICE_TABLE(of, bmi323_of_spi_match);
  65. static struct spi_driver bmi323_spi_driver = {
  66. .driver = {
  67. .name = "bmi323",
  68. .pm = pm_ptr(&bmi323_core_pm_ops),
  69. .of_match_table = bmi323_of_spi_match,
  70. },
  71. .probe = bmi323_spi_probe,
  72. .id_table = bmi323_spi_ids,
  73. };
  74. module_spi_driver(bmi323_spi_driver);
  75. MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
  76. MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
  77. MODULE_LICENSE("GPL");
  78. MODULE_IMPORT_NS(IIO_BMI323);