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 
8 #include <linux/module.h>
9 #include <linux/regmap.h>
10 #include <linux/spi/spi.h>
11 
12 #include "adxl345.h"
13 
14 #define ADXL345_MAX_SPI_FREQ_HZ		5000000
15 
16 static const struct regmap_config adxl345_spi_regmap_config = {
17 	.reg_bits = 8,
18 	.val_bits = 8,
19 	 /* Setting bits 7 and 6 enables multiple-byte read */
20 	.read_flag_mask = BIT(7) | BIT(6),
21 };
22 
adxl345_spi_probe(struct spi_device * spi)23 static int adxl345_spi_probe(struct spi_device *spi)
24 {
25 	struct regmap *regmap;
26 
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",
30 				     spi->max_speed_hz);
31 
32 	regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
33 	if (IS_ERR(regmap))
34 		return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
35 
36 	return adxl345_core_probe(&spi->dev, regmap);
37 }
38 
39 static const struct spi_device_id adxl345_spi_id[] = {
40 	{ "adxl345", ADXL345 },
41 	{ "adxl375", ADXL375 },
42 	{ }
43 };
44 MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
45 
46 static const struct of_device_id adxl345_of_match[] = {
47 	{ .compatible = "adi,adxl345", .data = (const void *)ADXL345 },
48 	{ .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
49 	{ }
50 };
51 MODULE_DEVICE_TABLE(of, adxl345_of_match);
52 
53 static const struct acpi_device_id adxl345_acpi_match[] = {
54 	{ "ADS0345", ADXL345 },
55 	{ }
56 };
57 MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
58 
59 static struct spi_driver adxl345_spi_driver = {
60 	.driver = {
61 		.name	= "adxl345_spi",
62 		.of_match_table = adxl345_of_match,
63 		.acpi_match_table = adxl345_acpi_match,
64 	},
65 	.probe		= adxl345_spi_probe,
66 	.id_table	= adxl345_spi_id,
67 };
68 module_spi_driver(adxl345_spi_driver);
69 
70 MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
71 MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver");
72 MODULE_LICENSE("GPL v2");
73 MODULE_IMPORT_NS(IIO_ADXL345);
74