adxl345_spi.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <eraretuya@gmail.com>
  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_setup(struct device *dev, struct regmap *regmap)
  19. {
  20. return regmap_write(regmap, ADXL345_REG_DATA_FORMAT, ADXL345_DATA_FORMAT_SPI_3WIRE);
  21. }
  22. static int adxl345_spi_probe(struct spi_device *spi)
  23. {
  24. struct regmap *regmap;
  25. /* Bail out if max_speed_hz exceeds 5 MHz */
  26. if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ)
  27. return dev_err_probe(&spi->dev, -EINVAL, "SPI CLK, %d Hz exceeds 5 MHz\n",
  28. spi->max_speed_hz);
  29. regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
  30. if (IS_ERR(regmap))
  31. return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
  32. if (spi->mode & SPI_3WIRE)
  33. return adxl345_core_probe(&spi->dev, regmap, adxl345_spi_setup);
  34. else
  35. return adxl345_core_probe(&spi->dev, regmap, NULL);
  36. }
  37. static const struct adxl345_chip_info adxl345_spi_info = {
  38. .name = "adxl345",
  39. .uscale = ADXL345_USCALE,
  40. };
  41. static const struct adxl345_chip_info adxl375_spi_info = {
  42. .name = "adxl375",
  43. .uscale = ADXL375_USCALE,
  44. };
  45. static const struct spi_device_id adxl345_spi_id[] = {
  46. { "adxl345", (kernel_ulong_t)&adxl345_spi_info },
  47. { "adxl375", (kernel_ulong_t)&adxl375_spi_info },
  48. { }
  49. };
  50. MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
  51. static const struct of_device_id adxl345_of_match[] = {
  52. { .compatible = "adi,adxl345", .data = &adxl345_spi_info },
  53. { .compatible = "adi,adxl375", .data = &adxl375_spi_info },
  54. { }
  55. };
  56. MODULE_DEVICE_TABLE(of, adxl345_of_match);
  57. static const struct acpi_device_id adxl345_acpi_match[] = {
  58. { "ADS0345", (kernel_ulong_t)&adxl345_spi_info },
  59. { }
  60. };
  61. MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
  62. static struct spi_driver adxl345_spi_driver = {
  63. .driver = {
  64. .name = "adxl345_spi",
  65. .of_match_table = adxl345_of_match,
  66. .acpi_match_table = adxl345_acpi_match,
  67. },
  68. .probe = adxl345_spi_probe,
  69. .id_table = adxl345_spi_id,
  70. };
  71. module_spi_driver(adxl345_spi_driver);
  72. MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
  73. MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver");
  74. MODULE_LICENSE("GPL v2");
  75. MODULE_IMPORT_NS(IIO_ADXL345);