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
16static 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
23static 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(dev: &spi->dev, err: -EINVAL, fmt: "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(ptr: regmap))
34 return dev_err_probe(dev: &spi->dev, err: PTR_ERR(ptr: regmap), fmt: "Error initializing regmap\n");
35
36 return adxl345_core_probe(dev: &spi->dev, regmap);
37}
38
39static const struct adxl345_chip_info adxl345_spi_info = {
40 .name = "adxl345",
41 .uscale = ADXL345_USCALE,
42};
43
44static const struct adxl345_chip_info adxl375_spi_info = {
45 .name = "adxl375",
46 .uscale = ADXL375_USCALE,
47};
48
49static const struct spi_device_id adxl345_spi_id[] = {
50 { "adxl345", (kernel_ulong_t)&adxl345_spi_info },
51 { "adxl375", (kernel_ulong_t)&adxl375_spi_info },
52 { }
53};
54MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
55
56static const struct of_device_id adxl345_of_match[] = {
57 { .compatible = "adi,adxl345", .data = &adxl345_spi_info },
58 { .compatible = "adi,adxl375", .data = &adxl375_spi_info },
59 { }
60};
61MODULE_DEVICE_TABLE(of, adxl345_of_match);
62
63static const struct acpi_device_id adxl345_acpi_match[] = {
64 { "ADS0345", (kernel_ulong_t)&adxl345_spi_info },
65 { }
66};
67MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
68
69static struct spi_driver adxl345_spi_driver = {
70 .driver = {
71 .name = "adxl345_spi",
72 .of_match_table = adxl345_of_match,
73 .acpi_match_table = adxl345_acpi_match,
74 },
75 .probe = adxl345_spi_probe,
76 .id_table = adxl345_spi_id,
77};
78module_spi_driver(adxl345_spi_driver);
79
80MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
81MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer SPI driver");
82MODULE_LICENSE("GPL v2");
83MODULE_IMPORT_NS(IIO_ADXL345);
84

source code of linux/drivers/iio/accel/adxl345_spi.c