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