1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADXL313 3-Axis Digital Accelerometer
5 * Copyright (c) 2021 Lucas Stankus <lucas.p.stankus@gmail.com>
7 * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL313.pdf
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/spi/spi.h>
14 #include <linux/property.h>
18 static const struct regmap_config adxl31x_spi_regmap_config
[] = {
22 .rd_table
= &adxl312_readable_regs_table
,
23 .wr_table
= &adxl312_writable_regs_table
,
25 /* Setting bits 7 and 6 enables multiple-byte read */
26 .read_flag_mask
= BIT(7) | BIT(6),
31 .rd_table
= &adxl313_readable_regs_table
,
32 .wr_table
= &adxl313_writable_regs_table
,
34 /* Setting bits 7 and 6 enables multiple-byte read */
35 .read_flag_mask
= BIT(7) | BIT(6),
40 .rd_table
= &adxl314_readable_regs_table
,
41 .wr_table
= &adxl314_writable_regs_table
,
43 /* Setting bits 7 and 6 enables multiple-byte read */
44 .read_flag_mask
= BIT(7) | BIT(6),
48 static int adxl313_spi_setup(struct device
*dev
, struct regmap
*regmap
)
50 struct spi_device
*spi
= container_of(dev
, struct spi_device
, dev
);
53 if (spi
->mode
& SPI_3WIRE
) {
54 ret
= regmap_write(regmap
, ADXL313_REG_DATA_FORMAT
,
60 return regmap_update_bits(regmap
, ADXL313_REG_POWER_CTL
,
61 ADXL313_I2C_DISABLE
, ADXL313_I2C_DISABLE
);
64 static int adxl313_spi_probe(struct spi_device
*spi
)
66 const struct adxl313_chip_info
*chip_data
;
67 struct regmap
*regmap
;
70 spi
->mode
|= SPI_MODE_3
;
75 chip_data
= spi_get_device_match_data(spi
);
77 regmap
= devm_regmap_init_spi(spi
,
78 &adxl31x_spi_regmap_config
[chip_data
->type
]);
81 dev_err(&spi
->dev
, "Error initializing spi regmap: %ld\n",
83 return PTR_ERR(regmap
);
86 return adxl313_core_probe(&spi
->dev
, regmap
,
87 chip_data
, &adxl313_spi_setup
);
90 static const struct spi_device_id adxl313_spi_id
[] = {
91 { .name
= "adxl312", .driver_data
= (kernel_ulong_t
)&adxl31x_chip_info
[ADXL312
] },
92 { .name
= "adxl313", .driver_data
= (kernel_ulong_t
)&adxl31x_chip_info
[ADXL313
] },
93 { .name
= "adxl314", .driver_data
= (kernel_ulong_t
)&adxl31x_chip_info
[ADXL314
] },
97 MODULE_DEVICE_TABLE(spi
, adxl313_spi_id
);
99 static const struct of_device_id adxl313_of_match
[] = {
100 { .compatible
= "adi,adxl312", .data
= &adxl31x_chip_info
[ADXL312
] },
101 { .compatible
= "adi,adxl313", .data
= &adxl31x_chip_info
[ADXL313
] },
102 { .compatible
= "adi,adxl314", .data
= &adxl31x_chip_info
[ADXL314
] },
106 MODULE_DEVICE_TABLE(of
, adxl313_of_match
);
108 static struct spi_driver adxl313_spi_driver
= {
110 .name
= "adxl313_spi",
111 .of_match_table
= adxl313_of_match
,
113 .probe
= adxl313_spi_probe
,
114 .id_table
= adxl313_spi_id
,
117 module_spi_driver(adxl313_spi_driver
);
119 MODULE_AUTHOR("Lucas Stankus <lucas.p.stankus@gmail.com>");
120 MODULE_DESCRIPTION("ADXL313 3-Axis Digital Accelerometer SPI driver");
121 MODULE_LICENSE("GPL v2");
122 MODULE_IMPORT_NS(IIO_ADXL313
);