xref: /linux/drivers/iio/accel/adxl313_spi.c (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ADXL313 3-Axis Digital Accelerometer
4  *
5  * Copyright (c) 2021 Lucas Stankus <lucas.p.stankus@gmail.com>
6  *
7  * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL313.pdf
8  */
9 
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/spi/spi.h>
14 
15 #include "adxl313.h"
16 
17 static const struct regmap_config adxl313_spi_regmap_config = {
18 	.reg_bits	= 8,
19 	.val_bits	= 8,
20 	.rd_table	= &adxl313_readable_regs_table,
21 	.wr_table	= &adxl313_writable_regs_table,
22 	.max_register	= 0x39,
23 	 /* Setting bits 7 and 6 enables multiple-byte read */
24 	.read_flag_mask	= BIT(7) | BIT(6),
25 };
26 
27 static int adxl313_spi_setup(struct device *dev, struct regmap *regmap)
28 {
29 	struct spi_device *spi = container_of(dev, struct spi_device, dev);
30 	int ret;
31 
32 	if (spi->mode & SPI_3WIRE) {
33 		ret = regmap_write(regmap, ADXL313_REG_DATA_FORMAT,
34 				   ADXL313_SPI_3WIRE);
35 		if (ret)
36 			return ret;
37 	}
38 
39 	return regmap_update_bits(regmap, ADXL313_REG_POWER_CTL,
40 				  ADXL313_I2C_DISABLE, ADXL313_I2C_DISABLE);
41 }
42 
43 static int adxl313_spi_probe(struct spi_device *spi)
44 {
45 	const struct spi_device_id *id = spi_get_device_id(spi);
46 	struct regmap *regmap;
47 	int ret;
48 
49 	spi->mode |= SPI_MODE_3;
50 	ret = spi_setup(spi);
51 	if (ret)
52 		return ret;
53 
54 	regmap = devm_regmap_init_spi(spi, &adxl313_spi_regmap_config);
55 	if (IS_ERR(regmap)) {
56 		dev_err(&spi->dev, "Error initializing spi regmap: %ld\n",
57 			PTR_ERR(regmap));
58 		return PTR_ERR(regmap);
59 	}
60 
61 	return adxl313_core_probe(&spi->dev, regmap, id->name,
62 				  &adxl313_spi_setup);
63 }
64 
65 static const struct spi_device_id adxl313_spi_id[] = {
66 	{ "adxl313" },
67 	{ }
68 };
69 
70 MODULE_DEVICE_TABLE(spi, adxl313_spi_id);
71 
72 static const struct of_device_id adxl313_of_match[] = {
73 	{ .compatible = "adi,adxl313" },
74 	{ }
75 };
76 
77 MODULE_DEVICE_TABLE(of, adxl313_of_match);
78 
79 static struct spi_driver adxl313_spi_driver = {
80 	.driver = {
81 		.name	= "adxl313_spi",
82 		.of_match_table = adxl313_of_match,
83 	},
84 	.probe		= adxl313_spi_probe,
85 	.id_table	= adxl313_spi_id,
86 };
87 
88 module_spi_driver(adxl313_spi_driver);
89 
90 MODULE_AUTHOR("Lucas Stankus <lucas.p.stankus@gmail.com>");
91 MODULE_DESCRIPTION("ADXL313 3-Axis Digital Accelerometer SPI driver");
92 MODULE_LICENSE("GPL v2");
93 MODULE_IMPORT_NS(IIO_ADXL313);
94