adxl367_i2c.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Analog Devices, Inc.
  4. * Author: Cosmin Tanislav <cosmin.tanislav@analog.com>
  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 0x18
  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. {
  36. struct adxl367_i2c_state *st;
  37. struct regmap *regmap;
  38. st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
  39. if (!st)
  40. return -ENOMEM;
  41. regmap = devm_regmap_init_i2c(client, &adxl367_i2c_regmap_config);
  42. if (IS_ERR(regmap))
  43. return PTR_ERR(regmap);
  44. st->regmap = regmap;
  45. return adxl367_probe(&client->dev, &adxl367_i2c_ops, st, regmap,
  46. client->irq);
  47. }
  48. static const struct i2c_device_id adxl367_i2c_id[] = {
  49. { "adxl367" },
  50. { }
  51. };
  52. MODULE_DEVICE_TABLE(i2c, adxl367_i2c_id);
  53. static const struct of_device_id adxl367_of_match[] = {
  54. { .compatible = "adi,adxl367" },
  55. { },
  56. };
  57. MODULE_DEVICE_TABLE(of, adxl367_of_match);
  58. static struct i2c_driver adxl367_i2c_driver = {
  59. .driver = {
  60. .name = "adxl367_i2c",
  61. .of_match_table = adxl367_of_match,
  62. },
  63. .probe = adxl367_i2c_probe,
  64. .id_table = adxl367_i2c_id,
  65. };
  66. module_i2c_driver(adxl367_i2c_driver);
  67. MODULE_IMPORT_NS(IIO_ADXL367);
  68. MODULE_AUTHOR("Cosmin Tanislav <cosmin.tanislav@analog.com>");
  69. MODULE_DESCRIPTION("Analog Devices ADXL367 3-axis accelerometer I2C driver");
  70. MODULE_LICENSE("GPL");