xref: /openbmc/linux/drivers/iio/accel/bma400_spi.c (revision 0a94608f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SPI IIO driver for Bosch BMA400 triaxial acceleration sensor.
4  *
5  * Copyright 2020 Dan Robertson <dan@dlrobertson.com>
6  *
7  */
8 #include <linux/bits.h>
9 #include <linux/init.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/spi/spi.h>
14 
15 #include "bma400.h"
16 
17 #define BMA400_MAX_SPI_READ 2
18 #define BMA400_SPI_READ_BUFFER_SIZE (BMA400_MAX_SPI_READ + 1)
19 
20 static int bma400_regmap_spi_read(void *context,
21 				  const void *reg, size_t reg_size,
22 				  void *val, size_t val_size)
23 {
24 	struct device *dev = context;
25 	struct spi_device *spi = to_spi_device(dev);
26 	u8 result[BMA400_SPI_READ_BUFFER_SIZE];
27 	ssize_t status;
28 
29 	if (val_size > BMA400_MAX_SPI_READ)
30 		return -EINVAL;
31 
32 	status = spi_write_then_read(spi, reg, 1, result, val_size + 1);
33 	if (status)
34 		return status;
35 
36 	/*
37 	 * From the BMA400 datasheet:
38 	 *
39 	 * > For a basic read operation two bytes have to be read and the first
40 	 * > has to be dropped and the second byte must be interpreted.
41 	 */
42 	memcpy(val, result + 1, val_size);
43 
44 	return 0;
45 }
46 
47 static int bma400_regmap_spi_write(void *context, const void *data,
48 				   size_t count)
49 {
50 	struct device *dev = context;
51 	struct spi_device *spi = to_spi_device(dev);
52 
53 	return spi_write(spi, data, count);
54 }
55 
56 static struct regmap_bus bma400_regmap_bus = {
57 	.read = bma400_regmap_spi_read,
58 	.write = bma400_regmap_spi_write,
59 	.read_flag_mask = BIT(7),
60 	.max_raw_read = BMA400_MAX_SPI_READ,
61 };
62 
63 static int bma400_spi_probe(struct spi_device *spi)
64 {
65 	const struct spi_device_id *id = spi_get_device_id(spi);
66 	struct regmap *regmap;
67 	unsigned int val;
68 	int ret;
69 
70 	regmap = devm_regmap_init(&spi->dev, &bma400_regmap_bus,
71 				  &spi->dev, &bma400_regmap_config);
72 	if (IS_ERR(regmap)) {
73 		dev_err(&spi->dev, "failed to create regmap\n");
74 		return PTR_ERR(regmap);
75 	}
76 
77 	/*
78 	 * Per the bma400 datasheet, the first SPI read may
79 	 * return garbage. As the datasheet recommends, the
80 	 * chip ID register will be read here and checked
81 	 * again in the following probe.
82 	 */
83 	ret = regmap_read(regmap, BMA400_CHIP_ID_REG, &val);
84 	if (ret)
85 		dev_err(&spi->dev, "Failed to read chip id register\n");
86 
87 	return bma400_probe(&spi->dev, regmap, id->name);
88 }
89 
90 static void bma400_spi_remove(struct spi_device *spi)
91 {
92 	bma400_remove(&spi->dev);
93 }
94 
95 static const struct spi_device_id bma400_spi_ids[] = {
96 	{ "bma400", 0 },
97 	{ }
98 };
99 MODULE_DEVICE_TABLE(spi, bma400_spi_ids);
100 
101 static const struct of_device_id bma400_of_spi_match[] = {
102 	{ .compatible = "bosch,bma400" },
103 	{ }
104 };
105 MODULE_DEVICE_TABLE(of, bma400_of_spi_match);
106 
107 static struct spi_driver bma400_spi_driver = {
108 	.driver = {
109 		.name = "bma400",
110 		.of_match_table = bma400_of_spi_match,
111 	},
112 	.probe    = bma400_spi_probe,
113 	.remove   = bma400_spi_remove,
114 	.id_table = bma400_spi_ids,
115 };
116 
117 module_spi_driver(bma400_spi_driver);
118 MODULE_AUTHOR("Dan Robertson <dan@dlrobertson.com>");
119 MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor (SPI)");
120 MODULE_LICENSE("GPL");
121 MODULE_IMPORT_NS(IIO_BMA400);
122