xref: /linux/drivers/iio/imu/bmi270/bmi270_spi.c (revision cdd30ebb1b9f36159d66f088b61aee264e649d7a)
192cc50a0SAlex Lanzano // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
292cc50a0SAlex Lanzano 
392cc50a0SAlex Lanzano #include <linux/iio/iio.h>
492cc50a0SAlex Lanzano #include <linux/mod_devicetable.h>
592cc50a0SAlex Lanzano #include <linux/module.h>
692cc50a0SAlex Lanzano #include <linux/regmap.h>
792cc50a0SAlex Lanzano #include <linux/spi/spi.h>
892cc50a0SAlex Lanzano 
992cc50a0SAlex Lanzano #include "bmi270.h"
1092cc50a0SAlex Lanzano 
1192cc50a0SAlex Lanzano /*
1292cc50a0SAlex Lanzano  * The following two functions are taken from the BMI323 spi driver code.
1392cc50a0SAlex Lanzano  * In section 6.4 of the BMI270 data it specifies that after a read
1492cc50a0SAlex Lanzano  * operation the first data byte from the device is a dummy byte
1592cc50a0SAlex Lanzano  */
bmi270_regmap_spi_read(void * spi,const void * reg_buf,size_t reg_size,void * val_buf,size_t val_size)1692cc50a0SAlex Lanzano static int bmi270_regmap_spi_read(void *spi, const void *reg_buf,
1792cc50a0SAlex Lanzano 				  size_t reg_size, void *val_buf,
1892cc50a0SAlex Lanzano 				  size_t val_size)
1992cc50a0SAlex Lanzano {
2092cc50a0SAlex Lanzano 	return spi_write_then_read(spi, reg_buf, reg_size, val_buf, val_size);
2192cc50a0SAlex Lanzano }
2292cc50a0SAlex Lanzano 
bmi270_regmap_spi_write(void * spi,const void * data,size_t count)2392cc50a0SAlex Lanzano static int bmi270_regmap_spi_write(void *spi, const void *data,
2492cc50a0SAlex Lanzano 				   size_t count)
2592cc50a0SAlex Lanzano {
2692cc50a0SAlex Lanzano 	u8 *data_buff = (u8 *)data;
2792cc50a0SAlex Lanzano 
2892cc50a0SAlex Lanzano 	/*
2992cc50a0SAlex Lanzano 	 * Remove the extra pad byte since its only needed for the read
3092cc50a0SAlex Lanzano 	 * operation
3192cc50a0SAlex Lanzano 	 */
3292cc50a0SAlex Lanzano 	data_buff[1] = data_buff[0];
3392cc50a0SAlex Lanzano 	return spi_write_then_read(spi, data_buff + 1, count - 1, NULL, 0);
3492cc50a0SAlex Lanzano }
3592cc50a0SAlex Lanzano 
3692cc50a0SAlex Lanzano static const struct regmap_bus bmi270_regmap_bus = {
3792cc50a0SAlex Lanzano 	.read = bmi270_regmap_spi_read,
3892cc50a0SAlex Lanzano 	.write = bmi270_regmap_spi_write,
3992cc50a0SAlex Lanzano };
4092cc50a0SAlex Lanzano 
4192cc50a0SAlex Lanzano static const struct regmap_config bmi270_spi_regmap_config = {
4292cc50a0SAlex Lanzano 	.reg_bits = 8,
4392cc50a0SAlex Lanzano 	.val_bits = 8,
4492cc50a0SAlex Lanzano 	.pad_bits = 8,
4592cc50a0SAlex Lanzano 	.read_flag_mask = BIT(7),
4692cc50a0SAlex Lanzano };
4792cc50a0SAlex Lanzano 
bmi270_spi_probe(struct spi_device * spi)4892cc50a0SAlex Lanzano static int bmi270_spi_probe(struct spi_device *spi)
4992cc50a0SAlex Lanzano {
5092cc50a0SAlex Lanzano 	struct regmap *regmap;
5192cc50a0SAlex Lanzano 	struct device *dev = &spi->dev;
52bb372ac2SJustin Weiss 	const struct bmi270_chip_info *chip_info;
53bb372ac2SJustin Weiss 
54bb372ac2SJustin Weiss 	chip_info = spi_get_device_match_data(spi);
55bb372ac2SJustin Weiss 	if (!chip_info)
56bb372ac2SJustin Weiss 		return -ENODEV;
5792cc50a0SAlex Lanzano 
5892cc50a0SAlex Lanzano 	regmap = devm_regmap_init(dev, &bmi270_regmap_bus, dev,
5992cc50a0SAlex Lanzano 				  &bmi270_spi_regmap_config);
6092cc50a0SAlex Lanzano 	if (IS_ERR(regmap))
6192cc50a0SAlex Lanzano 		return dev_err_probe(dev, PTR_ERR(regmap),
6292cc50a0SAlex Lanzano 				     "Failed to init i2c regmap");
6392cc50a0SAlex Lanzano 
64bb372ac2SJustin Weiss 	return bmi270_core_probe(dev, regmap, chip_info);
6592cc50a0SAlex Lanzano }
6692cc50a0SAlex Lanzano 
6792cc50a0SAlex Lanzano static const struct spi_device_id bmi270_spi_id[] = {
68f35f3c83SJustin Weiss 	{ "bmi260", (kernel_ulong_t)&bmi260_chip_info },
69bb372ac2SJustin Weiss 	{ "bmi270", (kernel_ulong_t)&bmi270_chip_info },
7092cc50a0SAlex Lanzano 	{ }
7192cc50a0SAlex Lanzano };
7292cc50a0SAlex Lanzano 
7392cc50a0SAlex Lanzano static const struct of_device_id bmi270_of_match[] = {
74f35f3c83SJustin Weiss 	{ .compatible = "bosch,bmi260", .data = &bmi260_chip_info },
75bb372ac2SJustin Weiss 	{ .compatible = "bosch,bmi270", .data = &bmi270_chip_info },
7692cc50a0SAlex Lanzano 	{ }
7792cc50a0SAlex Lanzano };
7892cc50a0SAlex Lanzano 
7992cc50a0SAlex Lanzano static struct spi_driver bmi270_spi_driver = {
8092cc50a0SAlex Lanzano 	.driver = {
8192cc50a0SAlex Lanzano 		.name = "bmi270",
8292cc50a0SAlex Lanzano 		.of_match_table = bmi270_of_match,
8392cc50a0SAlex Lanzano 	},
8492cc50a0SAlex Lanzano 	.probe = bmi270_spi_probe,
8592cc50a0SAlex Lanzano 	.id_table = bmi270_spi_id,
8692cc50a0SAlex Lanzano };
8792cc50a0SAlex Lanzano module_spi_driver(bmi270_spi_driver);
8892cc50a0SAlex Lanzano 
8992cc50a0SAlex Lanzano MODULE_AUTHOR("Alex Lanzano");
9092cc50a0SAlex Lanzano MODULE_DESCRIPTION("BMI270 driver");
9192cc50a0SAlex Lanzano MODULE_LICENSE("GPL");
92*cdd30ebbSPeter Zijlstra MODULE_IMPORT_NS("IIO_BMI270");
93