adxl345_spi.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * ADXL345 3-Axis Digital Accelerometer SPI driver
  3. *
  4. * Copyright (c) 2017 Eva Rachel Retuya <eraretuya@gmail.com>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/regmap.h>
  12. #include <linux/spi/spi.h>
  13. #include "adxl345.h"
  14. #define ADXL345_MAX_SPI_FREQ_HZ 5000000
  15. static const struct regmap_config adxl345_spi_regmap_config = {
  16. .reg_bits = 8,
  17. .val_bits = 8,
  18. /* Setting bits 7 and 6 enables multiple-byte read */
  19. .read_flag_mask = BIT(7) | BIT(6),
  20. };
  21. static int adxl345_spi_probe(struct spi_device *spi)
  22. {
  23. const struct spi_device_id *id = spi_get_device_id(spi);
  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. dev_err(&spi->dev, "SPI CLK, %d Hz exceeds 5 MHz\n",
  28. spi->max_speed_hz);
  29. return -EINVAL;
  30. }
  31. regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
  32. if (IS_ERR(regmap)) {
  33. dev_err(&spi->dev, "Error initializing spi regmap: %ld\n",
  34. PTR_ERR(regmap));
  35. return PTR_ERR(regmap);
  36. }
  37. return adxl345_core_probe(&spi->dev, regmap, id->driver_data, id->name);
  38. }
  39. static int adxl345_spi_remove(struct spi_device *spi)
  40. {
  41. return adxl345_core_remove(&spi->dev);
  42. }
  43. static const struct spi_device_id adxl345_spi_id[] = {
  44. { "adxl345", ADXL345 },
  45. { "adxl375", ADXL375 },
  46. { }
  47. };
  48. MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
  49. static const struct of_device_id adxl345_of_match[] = {
  50. { .compatible = "adi,adxl345" },
  51. { .compatible = "adi,adxl375" },
  52. { },
  53. };
  54. MODULE_DEVICE_TABLE(of, adxl345_of_match);
  55. static struct spi_driver adxl345_spi_driver = {
  56. .driver = {
  57. .name = "adxl345_spi",
  58. .of_match_table = adxl345_of_match,
  59. },
  60. .probe = adxl345_spi_probe,
  61. .remove = adxl345_spi_remove,
  62. .id_table = adxl345_spi_id,
  63. };
  64. module_spi_driver(adxl345_spi_driver);
  65. MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
  66. MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver");
  67. MODULE_LICENSE("GPL v2");