xref: /linux/drivers/iio/accel/adxl345_spi.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
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 
23 static int adxl345_spi_probe(struct spi_device *spi)
24 {
25 	const struct spi_device_id *id = spi_get_device_id(spi);
26 	struct regmap *regmap;
27 
28 	/* Bail out if max_speed_hz exceeds 5 MHz */
29 	if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ) {
30 		dev_err(&spi->dev, "SPI CLK, %d Hz exceeds 5 MHz\n",
31 			spi->max_speed_hz);
32 		return -EINVAL;
33 	}
34 
35 	regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
36 	if (IS_ERR(regmap)) {
37 		dev_err(&spi->dev, "Error initializing spi regmap: %ld\n",
38 			PTR_ERR(regmap));
39 		return PTR_ERR(regmap);
40 	}
41 
42 	return adxl345_core_probe(&spi->dev, regmap, id->driver_data, id->name);
43 }
44 
45 static int adxl345_spi_remove(struct spi_device *spi)
46 {
47 	return adxl345_core_remove(&spi->dev);
48 }
49 
50 static const struct spi_device_id adxl345_spi_id[] = {
51 	{ "adxl345", ADXL345 },
52 	{ "adxl375", ADXL375 },
53 	{ }
54 };
55 
56 MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
57 
58 static const struct of_device_id adxl345_of_match[] = {
59 	{ .compatible = "adi,adxl345" },
60 	{ .compatible = "adi,adxl375" },
61 	{ },
62 };
63 
64 MODULE_DEVICE_TABLE(of, adxl345_of_match);
65 
66 static struct spi_driver adxl345_spi_driver = {
67 	.driver = {
68 		.name	= "adxl345_spi",
69 		.of_match_table = adxl345_of_match,
70 	},
71 	.probe		= adxl345_spi_probe,
72 	.remove		= adxl345_spi_remove,
73 	.id_table	= adxl345_spi_id,
74 };
75 
76 module_spi_driver(adxl345_spi_driver);
77 
78 MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
79 MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver");
80 MODULE_LICENSE("GPL v2");
81