xref: /linux/drivers/iio/accel/adxl345_spi.c (revision cdd30ebb1b9f36159d66f088b61aee264e649d7a)
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_setup(struct device * dev,struct regmap * regmap)23 static int adxl345_spi_setup(struct device *dev, struct regmap *regmap)
24 {
25 	return regmap_write(regmap, ADXL345_REG_DATA_FORMAT, ADXL345_DATA_FORMAT_SPI_3WIRE);
26 }
27 
adxl345_spi_probe(struct spi_device * spi)28 static int adxl345_spi_probe(struct spi_device *spi)
29 {
30 	struct regmap *regmap;
31 
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",
35 				     spi->max_speed_hz);
36 
37 	regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
38 	if (IS_ERR(regmap))
39 		return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
40 
41 	if (spi->mode & SPI_3WIRE)
42 		return adxl345_core_probe(&spi->dev, regmap, adxl345_spi_setup);
43 	else
44 		return adxl345_core_probe(&spi->dev, regmap, NULL);
45 }
46 
47 static const struct adxl345_chip_info adxl345_spi_info = {
48 	.name = "adxl345",
49 	.uscale = ADXL345_USCALE,
50 };
51 
52 static const struct adxl345_chip_info adxl375_spi_info = {
53 	.name = "adxl375",
54 	.uscale = ADXL375_USCALE,
55 };
56 
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 },
60 	{ }
61 };
62 MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
63 
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 },
67 	{ }
68 };
69 MODULE_DEVICE_TABLE(of, adxl345_of_match);
70 
71 static const struct acpi_device_id adxl345_acpi_match[] = {
72 	{ "ADS0345", (kernel_ulong_t)&adxl345_spi_info },
73 	{ }
74 };
75 MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
76 
77 static struct spi_driver adxl345_spi_driver = {
78 	.driver = {
79 		.name	= "adxl345_spi",
80 		.of_match_table = adxl345_of_match,
81 		.acpi_match_table = adxl345_acpi_match,
82 	},
83 	.probe		= adxl345_spi_probe,
84 	.id_table	= adxl345_spi_id,
85 };
86 module_spi_driver(adxl345_spi_driver);
87 
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");
92