adxl367_i2c.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Analog Devices, Inc.
  4. * Author: Cosmin Tanislav <[email protected]>
  5. */
  6. #include <linux/i2c.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/module.h>
  9. #include <linux/regmap.h>
  10. #include "adxl367.h"
  11. #define ADXL367_I2C_FIFO_DATA 0x42
  12. struct adxl367_i2c_state {
  13. struct regmap *regmap;
  14. };
  15. static bool adxl367_readable_noinc_reg(struct device *dev, unsigned int reg)
  16. {
  17. return reg == ADXL367_I2C_FIFO_DATA;
  18. }
  19. static int adxl367_i2c_read_fifo(void *context, __be16 *fifo_buf,
  20. unsigned int fifo_entries)
  21. {
  22. struct adxl367_i2c_state *st = context;
  23. return regmap_noinc_read(st->regmap, ADXL367_I2C_FIFO_DATA, fifo_buf,
  24. fifo_entries * sizeof(*fifo_buf));
  25. }
  26. static const struct regmap_config adxl367_i2c_regmap_config = {
  27. .reg_bits = 8,
  28. .val_bits = 8,
  29. .readable_noinc_reg = adxl367_readable_noinc_reg,
  30. };
  31. static const struct adxl367_ops adxl367_i2c_ops = {
  32. .read_fifo = adxl367_i2c_read_fifo,
  33. };
  34. static int adxl367_i2c_probe(struct i2c_client *client,
  35. const struct i2c_device_id *id)
  36. {
  37. struct adxl367_i2c_state *st;
  38. struct regmap *regmap;
  39. st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
  40. if (!st)
  41. return -ENOMEM;
  42. regmap = devm_regmap_init_i2c(client, &adxl367_i2c_regmap_config);
  43. if (IS_ERR(regmap))
  44. return PTR_ERR(regmap);
  45. st->regmap = regmap;
  46. return adxl367_probe(&client->dev, &adxl367_i2c_ops, st, regmap,
  47. client->irq);
  48. }
  49. static const struct i2c_device_id adxl367_i2c_id[] = {
  50. { "adxl367", 0 },
  51. { },
  52. };
  53. MODULE_DEVICE_TABLE(i2c, adxl367_i2c_id);
  54. static const struct of_device_id adxl367_of_match[] = {
  55. { .compatible = "adi,adxl367" },
  56. { },
  57. };
  58. MODULE_DEVICE_TABLE(of, adxl367_of_match);
  59. static struct i2c_driver adxl367_i2c_driver = {
  60. .driver = {
  61. .name = "adxl367_i2c",
  62. .of_match_table = adxl367_of_match,
  63. },
  64. .probe = adxl367_i2c_probe,
  65. .id_table = adxl367_i2c_id,
  66. };
  67. module_i2c_driver(adxl367_i2c_driver);
  68. MODULE_IMPORT_NS(IIO_ADXL367);
  69. MODULE_AUTHOR("Cosmin Tanislav <[email protected]>");
  70. MODULE_DESCRIPTION("Analog Devices ADXL367 3-axis accelerometer I2C driver");
  71. MODULE_LICENSE("GPL");