1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 3-axis accelerometer driver supporting following Bosch-Sensortec chips:
4  *  - BMI088
5  *
6  * Copyright (c) 2018-2020, Topic Embedded Products
7  */
8 
9 #include <linux/module.h>
10 #include <linux/regmap.h>
11 #include <linux/slab.h>
12 #include <linux/spi/spi.h>
13 
14 #include "bmi088-accel.h"
15 
16 static int bmi088_regmap_spi_write(void *context, const void *data, size_t count)
17 {
18 	struct spi_device *spi = context;
19 
20 	/* Write register is same as generic SPI */
21 	return spi_write(spi, data, count);
22 }
23 
24 static int bmi088_regmap_spi_read(void *context, const void *reg,
25 				size_t reg_size, void *val, size_t val_size)
26 {
27 	struct spi_device *spi = context;
28 	u8 addr[2];
29 
30 	addr[0] = *(u8 *)reg;
31 	addr[0] |= BIT(7); /* Set RW = '1' */
32 	addr[1] = 0; /* Read requires a dummy byte transfer */
33 
34 	return spi_write_then_read(spi, addr, sizeof(addr), val, val_size);
35 }
36 
37 static struct regmap_bus bmi088_regmap_bus = {
38 	.write = bmi088_regmap_spi_write,
39 	.read = bmi088_regmap_spi_read,
40 };
41 
42 static int bmi088_accel_probe(struct spi_device *spi)
43 {
44 	struct regmap *regmap;
45 	const struct spi_device_id *id = spi_get_device_id(spi);
46 
47 	regmap = devm_regmap_init(&spi->dev, &bmi088_regmap_bus,
48 			spi, &bmi088_regmap_conf);
49 
50 	if (IS_ERR(regmap)) {
51 		dev_err(&spi->dev, "Failed to initialize spi regmap\n");
52 		return PTR_ERR(regmap);
53 	}
54 
55 	return bmi088_accel_core_probe(&spi->dev, regmap, spi->irq,
56 					id->driver_data);
57 }
58 
59 static void bmi088_accel_remove(struct spi_device *spi)
60 {
61 	bmi088_accel_core_remove(&spi->dev);
62 }
63 
64 static const struct of_device_id bmi088_of_match[] = {
65 	{ .compatible = "bosch,bmi085-accel" },
66 	{ .compatible = "bosch,bmi088-accel" },
67 	{ .compatible = "bosch,bmi090l-accel" },
68 	{}
69 };
70 MODULE_DEVICE_TABLE(of, bmi088_of_match);
71 
72 static const struct spi_device_id bmi088_accel_id[] = {
73 	{"bmi085-accel",  BOSCH_BMI085},
74 	{"bmi088-accel",  BOSCH_BMI088},
75 	{"bmi090l-accel", BOSCH_BMI090L},
76 	{}
77 };
78 MODULE_DEVICE_TABLE(spi, bmi088_accel_id);
79 
80 static struct spi_driver bmi088_accel_driver = {
81 	.driver = {
82 		.name	= "bmi088_accel_spi",
83 		.pm	= &bmi088_accel_pm_ops,
84 		.of_match_table = bmi088_of_match,
85 	},
86 	.probe		= bmi088_accel_probe,
87 	.remove		= bmi088_accel_remove,
88 	.id_table	= bmi088_accel_id,
89 };
90 module_spi_driver(bmi088_accel_driver);
91 
92 MODULE_AUTHOR("Niek van Agt <niek.van.agt@topicproducts.com>");
93 MODULE_LICENSE("GPL v2");
94 MODULE_DESCRIPTION("BMI088 accelerometer driver (SPI)");
95 MODULE_IMPORT_NS(IIO_BMI088);
96