1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * ADXL355 3-Axis Digital Accelerometer SPI driver
4 *
5 * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com>
6 */
7
8#include <linux/module.h>
9#include <linux/mod_devicetable.h>
10#include <linux/regmap.h>
11#include <linux/spi/spi.h>
12#include <linux/property.h>
13
14#include "adxl355.h"
15
16static const struct regmap_config adxl355_spi_regmap_config = {
17 .reg_bits = 7,
18 .pad_bits = 1,
19 .val_bits = 8,
20 .read_flag_mask = BIT(0),
21 .max_register = 0x2F,
22 .rd_table = &adxl355_readable_regs_tbl,
23 .wr_table = &adxl355_writeable_regs_tbl,
24};
25
26static int adxl355_spi_probe(struct spi_device *spi)
27{
28 const struct adxl355_chip_info *chip_data;
29 struct regmap *regmap;
30
31 chip_data = device_get_match_data(dev: &spi->dev);
32 if (!chip_data) {
33 chip_data = (void *)spi_get_device_id(sdev: spi)->driver_data;
34
35 if (!chip_data)
36 return -EINVAL;
37 }
38
39 regmap = devm_regmap_init_spi(spi, &adxl355_spi_regmap_config);
40 if (IS_ERR(ptr: regmap)) {
41 dev_err(&spi->dev, "Error initializing spi regmap: %ld\n",
42 PTR_ERR(regmap));
43
44 return PTR_ERR(ptr: regmap);
45 }
46
47 return adxl355_core_probe(dev: &spi->dev, regmap, chip_info: chip_data);
48}
49
50static const struct spi_device_id adxl355_spi_id[] = {
51 { "adxl355", (kernel_ulong_t)&adxl35x_chip_info[ADXL355] },
52 { "adxl359", (kernel_ulong_t)&adxl35x_chip_info[ADXL359] },
53 { }
54};
55MODULE_DEVICE_TABLE(spi, adxl355_spi_id);
56
57static const struct of_device_id adxl355_of_match[] = {
58 { .compatible = "adi,adxl355", .data = &adxl35x_chip_info[ADXL355] },
59 { .compatible = "adi,adxl359", .data = &adxl35x_chip_info[ADXL359] },
60 { }
61};
62MODULE_DEVICE_TABLE(of, adxl355_of_match);
63
64static struct spi_driver adxl355_spi_driver = {
65 .driver = {
66 .name = "adxl355_spi",
67 .of_match_table = adxl355_of_match,
68 },
69 .probe = adxl355_spi_probe,
70 .id_table = adxl355_spi_id,
71};
72module_spi_driver(adxl355_spi_driver);
73
74MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>");
75MODULE_DESCRIPTION("ADXL355 3-Axis Digital Accelerometer SPI driver");
76MODULE_LICENSE("GPL v2");
77MODULE_IMPORT_NS(IIO_ADXL355);
78

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