1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADXL345 3-Axis Digital Accelerometer SPI driver
5 * Copyright (c) 2017 Eva Rachel Retuya <eraretuya@gmail.com>
8 #include <linux/module.h>
9 #include <linux/regmap.h>
10 #include <linux/spi/spi.h>
14 #define ADXL345_MAX_SPI_FREQ_HZ 5000000
16 static const struct regmap_config adxl345_spi_regmap_config = {
19 /* Setting bits 7 and 6 enables multiple-byte read */
20 .read_flag_mask = BIT(7) | BIT(6),
23 static int adxl345_spi_probe(struct spi_device *spi)
25 struct regmap *regmap;
27 /* Bail out if max_speed_hz exceeds 5 MHz */
28 if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ)
29 return dev_err_probe(&spi->dev, -EINVAL, "SPI CLK, %d Hz exceeds 5 MHz\n",
32 regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
34 return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
36 return adxl345_core_probe(&spi->dev, regmap);
39 static const struct adxl345_chip_info adxl345_spi_info = {
41 .uscale = ADXL345_USCALE,
44 static const struct adxl345_chip_info adxl375_spi_info = {
46 .uscale = ADXL375_USCALE,
49 static const struct spi_device_id adxl345_spi_id[] = {
50 { "adxl345", (kernel_ulong_t)&adxl345_spi_info },
51 { "adxl375", (kernel_ulong_t)&adxl375_spi_info },
54 MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
56 static const struct of_device_id adxl345_of_match[] = {
57 { .compatible = "adi,adxl345", .data = &adxl345_spi_info },
58 { .compatible = "adi,adxl375", .data = &adxl375_spi_info },
61 MODULE_DEVICE_TABLE(of, adxl345_of_match);
63 static const struct acpi_device_id adxl345_acpi_match[] = {
64 { "ADS0345", (kernel_ulong_t)&adxl345_spi_info },
67 MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
69 static struct spi_driver adxl345_spi_driver = {
71 .name = "adxl345_spi",
72 .of_match_table = adxl345_of_match,
73 .acpi_match_table = adxl345_acpi_match,
75 .probe = adxl345_spi_probe,
76 .id_table = adxl345_spi_id,
78 module_spi_driver(adxl345_spi_driver);
80 MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
81 MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver");
82 MODULE_LICENSE("GPL v2");
83 MODULE_IMPORT_NS(IIO_ADXL345);