xref: /linux/drivers/iio/imu/bmi270/bmi270_spi.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 
3 #include <linux/iio/iio.h>
4 #include <linux/mod_devicetable.h>
5 #include <linux/module.h>
6 #include <linux/regmap.h>
7 #include <linux/spi/spi.h>
8 
9 #include "bmi270.h"
10 
11 /*
12  * The following two functions are taken from the BMI323 spi driver code.
13  * In section 6.4 of the BMI270 data it specifies that after a read
14  * operation the first data byte from the device is a dummy byte
15  */
16 static int bmi270_regmap_spi_read(void *spi, const void *reg_buf,
17 				  size_t reg_size, void *val_buf,
18 				  size_t val_size)
19 {
20 	return spi_write_then_read(spi, reg_buf, reg_size, val_buf, val_size);
21 }
22 
23 static int bmi270_regmap_spi_write(void *spi, const void *data,
24 				   size_t count)
25 {
26 	u8 *data_buff = (u8 *)data;
27 
28 	/*
29 	 * Remove the extra pad byte since its only needed for the read
30 	 * operation
31 	 */
32 	data_buff[1] = data_buff[0];
33 	return spi_write_then_read(spi, data_buff + 1, count - 1, NULL, 0);
34 }
35 
36 static const struct regmap_bus bmi270_regmap_bus = {
37 	.read = bmi270_regmap_spi_read,
38 	.write = bmi270_regmap_spi_write,
39 };
40 
41 static const struct regmap_config bmi270_spi_regmap_config = {
42 	.reg_bits = 8,
43 	.val_bits = 8,
44 	.pad_bits = 8,
45 	.read_flag_mask = BIT(7),
46 };
47 
48 static int bmi270_spi_probe(struct spi_device *spi)
49 {
50 	struct regmap *regmap;
51 	struct device *dev = &spi->dev;
52 	const struct bmi270_chip_info *chip_info;
53 
54 	chip_info = spi_get_device_match_data(spi);
55 	if (!chip_info)
56 		return -ENODEV;
57 
58 	regmap = devm_regmap_init(dev, &bmi270_regmap_bus, dev,
59 				  &bmi270_spi_regmap_config);
60 	if (IS_ERR(regmap))
61 		return dev_err_probe(dev, PTR_ERR(regmap),
62 				     "Failed to init i2c regmap");
63 
64 	return bmi270_core_probe(dev, regmap, chip_info);
65 }
66 
67 static const struct spi_device_id bmi270_spi_id[] = {
68 	{ "bmi260", (kernel_ulong_t)&bmi260_chip_info },
69 	{ "bmi270", (kernel_ulong_t)&bmi270_chip_info },
70 	{ }
71 };
72 
73 static const struct of_device_id bmi270_of_match[] = {
74 	{ .compatible = "bosch,bmi260", .data = &bmi260_chip_info },
75 	{ .compatible = "bosch,bmi270", .data = &bmi270_chip_info },
76 	{ }
77 };
78 
79 static struct spi_driver bmi270_spi_driver = {
80 	.driver = {
81 		.name = "bmi270",
82 		.of_match_table = bmi270_of_match,
83 	},
84 	.probe = bmi270_spi_probe,
85 	.id_table = bmi270_spi_id,
86 };
87 module_spi_driver(bmi270_spi_driver);
88 
89 MODULE_AUTHOR("Alex Lanzano");
90 MODULE_DESCRIPTION("BMI270 driver");
91 MODULE_LICENSE("GPL");
92 MODULE_IMPORT_NS(IIO_BMI270);
93