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