adxl313_i2c.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ADXL313 3-Axis Digital Accelerometer
  4. *
  5. * Copyright (c) 2021 Lucas Stankus <[email protected]>
  6. *
  7. * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL313.pdf
  8. */
  9. #include <linux/i2c.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include "adxl313.h"
  14. static const struct regmap_config adxl31x_i2c_regmap_config[] = {
  15. [ADXL312] = {
  16. .reg_bits = 8,
  17. .val_bits = 8,
  18. .rd_table = &adxl312_readable_regs_table,
  19. .wr_table = &adxl312_writable_regs_table,
  20. .max_register = 0x39,
  21. },
  22. [ADXL313] = {
  23. .reg_bits = 8,
  24. .val_bits = 8,
  25. .rd_table = &adxl313_readable_regs_table,
  26. .wr_table = &adxl313_writable_regs_table,
  27. .max_register = 0x39,
  28. },
  29. [ADXL314] = {
  30. .reg_bits = 8,
  31. .val_bits = 8,
  32. .rd_table = &adxl314_readable_regs_table,
  33. .wr_table = &adxl314_writable_regs_table,
  34. .max_register = 0x39,
  35. },
  36. };
  37. static const struct i2c_device_id adxl313_i2c_id[] = {
  38. { .name = "adxl312", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL312] },
  39. { .name = "adxl313", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL313] },
  40. { .name = "adxl314", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL314] },
  41. { }
  42. };
  43. MODULE_DEVICE_TABLE(i2c, adxl313_i2c_id);
  44. static const struct of_device_id adxl313_of_match[] = {
  45. { .compatible = "adi,adxl312", .data = &adxl31x_chip_info[ADXL312] },
  46. { .compatible = "adi,adxl313", .data = &adxl31x_chip_info[ADXL313] },
  47. { .compatible = "adi,adxl314", .data = &adxl31x_chip_info[ADXL314] },
  48. { }
  49. };
  50. MODULE_DEVICE_TABLE(of, adxl313_of_match);
  51. static int adxl313_i2c_probe(struct i2c_client *client)
  52. {
  53. const struct adxl313_chip_info *chip_data;
  54. struct regmap *regmap;
  55. /*
  56. * Retrieves device specific data as a pointer to a
  57. * adxl313_chip_info structure
  58. */
  59. chip_data = device_get_match_data(&client->dev);
  60. if (!chip_data)
  61. chip_data = (const struct adxl313_chip_info *)i2c_match_id(adxl313_i2c_id, client)->driver_data;
  62. regmap = devm_regmap_init_i2c(client,
  63. &adxl31x_i2c_regmap_config[chip_data->type]);
  64. if (IS_ERR(regmap)) {
  65. dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
  66. PTR_ERR(regmap));
  67. return PTR_ERR(regmap);
  68. }
  69. return adxl313_core_probe(&client->dev, regmap, chip_data, NULL);
  70. }
  71. static struct i2c_driver adxl313_i2c_driver = {
  72. .driver = {
  73. .name = "adxl313_i2c",
  74. .of_match_table = adxl313_of_match,
  75. },
  76. .probe_new = adxl313_i2c_probe,
  77. .id_table = adxl313_i2c_id,
  78. };
  79. module_i2c_driver(adxl313_i2c_driver);
  80. MODULE_AUTHOR("Lucas Stankus <[email protected]>");
  81. MODULE_DESCRIPTION("ADXL313 3-Axis Digital Accelerometer I2C driver");
  82. MODULE_LICENSE("GPL v2");
  83. MODULE_IMPORT_NS(IIO_ADXL313);