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_setup(struct device
*dev
, struct regmap
*regmap
)
25 return regmap_write(regmap
, ADXL345_REG_DATA_FORMAT
, ADXL345_DATA_FORMAT_SPI_3WIRE
);
28 static int adxl345_spi_probe(struct spi_device
*spi
)
30 struct regmap
*regmap
;
32 /* Bail out if max_speed_hz exceeds 5 MHz */
33 if (spi
->max_speed_hz
> ADXL345_MAX_SPI_FREQ_HZ
)
34 return dev_err_probe(&spi
->dev
, -EINVAL
, "SPI CLK, %d Hz exceeds 5 MHz\n",
37 regmap
= devm_regmap_init_spi(spi
, &adxl345_spi_regmap_config
);
39 return dev_err_probe(&spi
->dev
, PTR_ERR(regmap
), "Error initializing regmap\n");
41 if (spi
->mode
& SPI_3WIRE
)
42 return adxl345_core_probe(&spi
->dev
, regmap
, adxl345_spi_setup
);
44 return adxl345_core_probe(&spi
->dev
, regmap
, NULL
);
47 static const struct adxl345_chip_info adxl345_spi_info
= {
49 .uscale
= ADXL345_USCALE
,
52 static const struct adxl345_chip_info adxl375_spi_info
= {
54 .uscale
= ADXL375_USCALE
,
57 static const struct spi_device_id adxl345_spi_id
[] = {
58 { "adxl345", (kernel_ulong_t
)&adxl345_spi_info
},
59 { "adxl375", (kernel_ulong_t
)&adxl375_spi_info
},
62 MODULE_DEVICE_TABLE(spi
, adxl345_spi_id
);
64 static const struct of_device_id adxl345_of_match
[] = {
65 { .compatible
= "adi,adxl345", .data
= &adxl345_spi_info
},
66 { .compatible
= "adi,adxl375", .data
= &adxl375_spi_info
},
69 MODULE_DEVICE_TABLE(of
, adxl345_of_match
);
71 static const struct acpi_device_id adxl345_acpi_match
[] = {
72 { "ADS0345", (kernel_ulong_t
)&adxl345_spi_info
},
75 MODULE_DEVICE_TABLE(acpi
, adxl345_acpi_match
);
77 static struct spi_driver adxl345_spi_driver
= {
79 .name
= "adxl345_spi",
80 .of_match_table
= adxl345_of_match
,
81 .acpi_match_table
= adxl345_acpi_match
,
83 .probe
= adxl345_spi_probe
,
84 .id_table
= adxl345_spi_id
,
86 module_spi_driver(adxl345_spi_driver
);
88 MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
89 MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver");
90 MODULE_LICENSE("GPL v2");
91 MODULE_IMPORT_NS(IIO_ADXL345
);