xref: /openbmc/linux/drivers/iio/accel/adxl345_spi.c (revision f3d7c2cd)
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 const struct spi_device_id adxl345_spi_id[] = {
46 	{ "adxl345", ADXL345 },
47 	{ "adxl375", ADXL375 },
48 	{ }
49 };
50 
51 MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
52 
53 static const struct of_device_id adxl345_of_match[] = {
54 	{ .compatible = "adi,adxl345" },
55 	{ .compatible = "adi,adxl375" },
56 	{ },
57 };
58 
59 MODULE_DEVICE_TABLE(of, adxl345_of_match);
60 
61 static struct spi_driver adxl345_spi_driver = {
62 	.driver = {
63 		.name	= "adxl345_spi",
64 		.of_match_table = adxl345_of_match,
65 	},
66 	.probe		= adxl345_spi_probe,
67 	.id_table	= adxl345_spi_id,
68 };
69 
70 module_spi_driver(adxl345_spi_driver);
71 
72 MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
73 MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver");
74 MODULE_LICENSE("GPL v2");
75