bmi323_i2c.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * I2C driver for Bosch BMI323 6-Axis IMU.
  4. *
  5. * Copyright (C) 2023, Jagath Jog J <jagathjog1996@gmail.com>
  6. */
  7. #include <linux/i2c.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/regmap.h>
  11. #include "bmi323.h"
  12. struct bmi323_i2c_priv {
  13. struct i2c_client *i2c;
  14. u8 i2c_rx_buffer[BMI323_FIFO_LENGTH_IN_BYTES + BMI323_I2C_DUMMY];
  15. };
  16. /*
  17. * From BMI323 datasheet section 4: Notes on the Serial Interface Support.
  18. * Each I2C register read operation requires to read two dummy bytes before
  19. * the actual payload.
  20. */
  21. static int bmi323_regmap_i2c_read(void *context, const void *reg_buf,
  22. size_t reg_size, void *val_buf,
  23. size_t val_size)
  24. {
  25. struct bmi323_i2c_priv *priv = context;
  26. struct i2c_msg msgs[2];
  27. int ret;
  28. msgs[0].addr = priv->i2c->addr;
  29. msgs[0].flags = priv->i2c->flags;
  30. msgs[0].len = reg_size;
  31. msgs[0].buf = (u8 *)reg_buf;
  32. msgs[1].addr = priv->i2c->addr;
  33. msgs[1].len = val_size + BMI323_I2C_DUMMY;
  34. msgs[1].buf = priv->i2c_rx_buffer;
  35. msgs[1].flags = priv->i2c->flags | I2C_M_RD;
  36. ret = i2c_transfer(priv->i2c->adapter, msgs, ARRAY_SIZE(msgs));
  37. if (ret < 0)
  38. return -EIO;
  39. memcpy(val_buf, priv->i2c_rx_buffer + BMI323_I2C_DUMMY, val_size);
  40. return 0;
  41. }
  42. static int bmi323_regmap_i2c_write(void *context, const void *data,
  43. size_t count)
  44. {
  45. struct bmi323_i2c_priv *priv = context;
  46. u8 reg;
  47. reg = *(u8 *)data;
  48. return i2c_smbus_write_i2c_block_data(priv->i2c, reg,
  49. count - sizeof(u8),
  50. data + sizeof(u8));
  51. }
  52. static const struct regmap_bus bmi323_regmap_bus = {
  53. .read = bmi323_regmap_i2c_read,
  54. .write = bmi323_regmap_i2c_write,
  55. };
  56. static const struct regmap_config bmi323_i2c_regmap_config = {
  57. .reg_bits = 8,
  58. .val_bits = 16,
  59. .max_register = BMI323_CFG_RES_REG,
  60. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  61. };
  62. static int bmi323_i2c_probe(struct i2c_client *i2c)
  63. {
  64. struct device *dev = &i2c->dev;
  65. struct bmi323_i2c_priv *priv;
  66. struct regmap *regmap;
  67. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  68. if (!priv)
  69. return -ENOMEM;
  70. priv->i2c = i2c;
  71. regmap = devm_regmap_init(dev, &bmi323_regmap_bus, priv,
  72. &bmi323_i2c_regmap_config);
  73. if (IS_ERR(regmap))
  74. return dev_err_probe(dev, PTR_ERR(regmap),
  75. "Failed to initialize I2C Regmap\n");
  76. return bmi323_core_probe(dev);
  77. }
  78. static const struct acpi_device_id bmi323_acpi_match[] = {
  79. /*
  80. * The "BOSC0200" identifier used here is not unique to bmi323 devices.
  81. * The same "BOSC0200" identifier is found in the ACPI tables of devices
  82. * using the bmc150 chip. This creates a conflict with duplicate ACPI
  83. * identifiers which multiple drivers want to use. If a non-bmi323
  84. * device starts to load with this "BOSC0200" ACPI match here, then the
  85. * chip ID check portion should fail because the chip IDs received (via
  86. * i2c) are unique between bmc150 and bmi323 and the driver should
  87. * relinquish the device. If and when a different driver (such as
  88. * bmc150) starts to load with the "BOSC0200" ACPI match, a short reset
  89. * should ensure that the device is not in a bad state during that
  90. * driver initialization. This device reset does occur in both the
  91. * bmi323 and bmc150 init sequences.
  92. */
  93. { "BOSC0200" },
  94. { }
  95. };
  96. MODULE_DEVICE_TABLE(acpi, bmi323_acpi_match);
  97. static const struct i2c_device_id bmi323_i2c_ids[] = {
  98. { "bmi323" },
  99. { }
  100. };
  101. MODULE_DEVICE_TABLE(i2c, bmi323_i2c_ids);
  102. static const struct of_device_id bmi323_of_i2c_match[] = {
  103. { .compatible = "bosch,bmi323" },
  104. { }
  105. };
  106. MODULE_DEVICE_TABLE(of, bmi323_of_i2c_match);
  107. static struct i2c_driver bmi323_i2c_driver = {
  108. .driver = {
  109. .name = "bmi323",
  110. .pm = pm_ptr(&bmi323_core_pm_ops),
  111. .of_match_table = bmi323_of_i2c_match,
  112. .acpi_match_table = bmi323_acpi_match,
  113. },
  114. .probe = bmi323_i2c_probe,
  115. .id_table = bmi323_i2c_ids,
  116. };
  117. module_i2c_driver(bmi323_i2c_driver);
  118. MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
  119. MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
  120. MODULE_LICENSE("GPL");
  121. MODULE_IMPORT_NS(IIO_BMI323);