bma220_i2c.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Bosch triaxial acceleration sensor
  4. *
  5. * Copyright (c) 2025 Petre Rodan <petre.rodan@subdimension.ro>
  6. *
  7. * Datasheet: https://media.digikey.com/pdf/Data%20Sheets/Bosch/BMA220.pdf
  8. * I2C address is either 0x0b or 0x0a depending on CSB (pin 10)
  9. */
  10. #include <linux/bitfield.h>
  11. #include <linux/i2c.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/regmap.h>
  15. #include <linux/types.h>
  16. #include "bma220.h"
  17. static int bma220_set_wdt(struct regmap *regmap, const u8 val)
  18. {
  19. return regmap_update_bits(regmap, BMA220_REG_WDT, BMA220_WDT_MASK,
  20. FIELD_PREP(BMA220_WDT_MASK, val));
  21. }
  22. static int bma220_i2c_probe(struct i2c_client *client)
  23. {
  24. struct regmap *regmap;
  25. int ret;
  26. regmap = devm_regmap_init_i2c(client, &bma220_i2c_regmap_config);
  27. if (IS_ERR(regmap))
  28. return dev_err_probe(&client->dev, PTR_ERR(regmap),
  29. "failed to create regmap\n");
  30. ret = bma220_common_probe(&client->dev, regmap, client->irq);
  31. if (ret)
  32. return ret;
  33. return bma220_set_wdt(regmap, BMA220_WDT_1MS);
  34. }
  35. static const struct of_device_id bma220_i2c_match[] = {
  36. { .compatible = "bosch,bma220" },
  37. { }
  38. };
  39. MODULE_DEVICE_TABLE(of, bma220_i2c_match);
  40. static const struct i2c_device_id bma220_i2c_id[] = {
  41. { "bma220" },
  42. { }
  43. };
  44. MODULE_DEVICE_TABLE(i2c, bma220_i2c_id);
  45. static struct i2c_driver bma220_i2c_driver = {
  46. .driver = {
  47. .name = "bma220_i2c",
  48. .pm = pm_sleep_ptr(&bma220_pm_ops),
  49. .of_match_table = bma220_i2c_match,
  50. },
  51. .probe = bma220_i2c_probe,
  52. .id_table = bma220_i2c_id,
  53. };
  54. module_i2c_driver(bma220_i2c_driver);
  55. MODULE_AUTHOR("Petre Rodan <petre.rodan@subdimension.ro>");
  56. MODULE_DESCRIPTION("Bosch triaxial acceleration sensor i2c driver");
  57. MODULE_LICENSE("GPL");
  58. MODULE_IMPORT_NS("IIO_BOSCH_BMA220");