adxl345_spi.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ADXL345 3-Axis Digital Accelerometer SPI driver
  4. *
  5. * Copyright (c) 2017 Eva Rachel Retuya <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/regmap.h>
  9. #include <linux/spi/spi.h>
  10. #include "adxl345.h"
  11. #define ADXL345_MAX_SPI_FREQ_HZ 5000000
  12. static const struct regmap_config adxl345_spi_regmap_config = {
  13. .reg_bits = 8,
  14. .val_bits = 8,
  15. /* Setting bits 7 and 6 enables multiple-byte read */
  16. .read_flag_mask = BIT(7) | BIT(6),
  17. };
  18. static int adxl345_spi_probe(struct spi_device *spi)
  19. {
  20. struct regmap *regmap;
  21. /* Bail out if max_speed_hz exceeds 5 MHz */
  22. if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ)
  23. return dev_err_probe(&spi->dev, -EINVAL, "SPI CLK, %d Hz exceeds 5 MHz\n",
  24. spi->max_speed_hz);
  25. regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
  26. if (IS_ERR(regmap))
  27. return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
  28. return adxl345_core_probe(&spi->dev, regmap);
  29. }
  30. static const struct spi_device_id adxl345_spi_id[] = {
  31. { "adxl345", ADXL345 },
  32. { "adxl375", ADXL375 },
  33. { }
  34. };
  35. MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
  36. static const struct of_device_id adxl345_of_match[] = {
  37. { .compatible = "adi,adxl345", .data = (const void *)ADXL345 },
  38. { .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
  39. { }
  40. };
  41. MODULE_DEVICE_TABLE(of, adxl345_of_match);
  42. static const struct acpi_device_id adxl345_acpi_match[] = {
  43. { "ADS0345", ADXL345 },
  44. { }
  45. };
  46. MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
  47. static struct spi_driver adxl345_spi_driver = {
  48. .driver = {
  49. .name = "adxl345_spi",
  50. .of_match_table = adxl345_of_match,
  51. .acpi_match_table = adxl345_acpi_match,
  52. },
  53. .probe = adxl345_spi_probe,
  54. .id_table = adxl345_spi_id,
  55. };
  56. module_spi_driver(adxl345_spi_driver);
  57. MODULE_AUTHOR("Eva Rachel Retuya <[email protected]>");
  58. MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver");
  59. MODULE_LICENSE("GPL v2");
  60. MODULE_IMPORT_NS(IIO_ADXL345);