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, id->name,
56 				       true);
57 }
58 
59 static int bmi088_accel_remove(struct spi_device *spi)
60 {
61 	return bmi088_accel_core_remove(&spi->dev);
62 }
63 
64 static const struct spi_device_id bmi088_accel_id[] = {
65 	{"bmi088-accel", },
66 	{}
67 };
68 MODULE_DEVICE_TABLE(spi, bmi088_accel_id);
69 
70 static struct spi_driver bmi088_accel_driver = {
71 	.driver = {
72 		.name	= "bmi088_accel_spi",
73 		.pm	= &bmi088_accel_pm_ops,
74 	},
75 	.probe		= bmi088_accel_probe,
76 	.remove		= bmi088_accel_remove,
77 	.id_table	= bmi088_accel_id,
78 };
79 module_spi_driver(bmi088_accel_driver);
80 
81 MODULE_AUTHOR("Niek van Agt <niek.van.agt@topicproducts.com>");
82 MODULE_LICENSE("GPL v2");
83 MODULE_DESCRIPTION("BMI088 accelerometer driver (SPI)");
84