xref: /openbmc/linux/drivers/iio/accel/bma400_spi.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
19bea1064SDan Robertson // SPDX-License-Identifier: GPL-2.0-only
29bea1064SDan Robertson /*
39bea1064SDan Robertson  * SPI IIO driver for Bosch BMA400 triaxial acceleration sensor.
49bea1064SDan Robertson  *
59bea1064SDan Robertson  * Copyright 2020 Dan Robertson <dan@dlrobertson.com>
69bea1064SDan Robertson  *
79bea1064SDan Robertson  */
89bea1064SDan Robertson #include <linux/bits.h>
99bea1064SDan Robertson #include <linux/init.h>
109bea1064SDan Robertson #include <linux/mod_devicetable.h>
119bea1064SDan Robertson #include <linux/module.h>
129bea1064SDan Robertson #include <linux/regmap.h>
139bea1064SDan Robertson #include <linux/spi/spi.h>
149bea1064SDan Robertson 
159bea1064SDan Robertson #include "bma400.h"
169bea1064SDan Robertson 
179bea1064SDan Robertson #define BMA400_MAX_SPI_READ 2
189bea1064SDan Robertson #define BMA400_SPI_READ_BUFFER_SIZE (BMA400_MAX_SPI_READ + 1)
199bea1064SDan Robertson 
bma400_regmap_spi_read(void * context,const void * reg,size_t reg_size,void * val,size_t val_size)209bea1064SDan Robertson static int bma400_regmap_spi_read(void *context,
219bea1064SDan Robertson 				  const void *reg, size_t reg_size,
229bea1064SDan Robertson 				  void *val, size_t val_size)
239bea1064SDan Robertson {
249bea1064SDan Robertson 	struct device *dev = context;
259bea1064SDan Robertson 	struct spi_device *spi = to_spi_device(dev);
269bea1064SDan Robertson 	u8 result[BMA400_SPI_READ_BUFFER_SIZE];
279bea1064SDan Robertson 	ssize_t status;
289bea1064SDan Robertson 
299bea1064SDan Robertson 	if (val_size > BMA400_MAX_SPI_READ)
309bea1064SDan Robertson 		return -EINVAL;
319bea1064SDan Robertson 
329bea1064SDan Robertson 	status = spi_write_then_read(spi, reg, 1, result, val_size + 1);
339bea1064SDan Robertson 	if (status)
349bea1064SDan Robertson 		return status;
359bea1064SDan Robertson 
369bea1064SDan Robertson 	/*
379bea1064SDan Robertson 	 * From the BMA400 datasheet:
389bea1064SDan Robertson 	 *
399bea1064SDan Robertson 	 * > For a basic read operation two bytes have to be read and the first
409bea1064SDan Robertson 	 * > has to be dropped and the second byte must be interpreted.
419bea1064SDan Robertson 	 */
429bea1064SDan Robertson 	memcpy(val, result + 1, val_size);
439bea1064SDan Robertson 
449bea1064SDan Robertson 	return 0;
459bea1064SDan Robertson }
469bea1064SDan Robertson 
bma400_regmap_spi_write(void * context,const void * data,size_t count)479bea1064SDan Robertson static int bma400_regmap_spi_write(void *context, const void *data,
489bea1064SDan Robertson 				   size_t count)
499bea1064SDan Robertson {
509bea1064SDan Robertson 	struct device *dev = context;
519bea1064SDan Robertson 	struct spi_device *spi = to_spi_device(dev);
529bea1064SDan Robertson 
539bea1064SDan Robertson 	return spi_write(spi, data, count);
549bea1064SDan Robertson }
559bea1064SDan Robertson 
569bea1064SDan Robertson static struct regmap_bus bma400_regmap_bus = {
579bea1064SDan Robertson 	.read = bma400_regmap_spi_read,
589bea1064SDan Robertson 	.write = bma400_regmap_spi_write,
599bea1064SDan Robertson 	.read_flag_mask = BIT(7),
609bea1064SDan Robertson 	.max_raw_read = BMA400_MAX_SPI_READ,
619bea1064SDan Robertson };
629bea1064SDan Robertson 
bma400_spi_probe(struct spi_device * spi)639bea1064SDan Robertson static int bma400_spi_probe(struct spi_device *spi)
649bea1064SDan Robertson {
659bea1064SDan Robertson 	const struct spi_device_id *id = spi_get_device_id(spi);
669bea1064SDan Robertson 	struct regmap *regmap;
679bea1064SDan Robertson 	unsigned int val;
689bea1064SDan Robertson 	int ret;
699bea1064SDan Robertson 
709bea1064SDan Robertson 	regmap = devm_regmap_init(&spi->dev, &bma400_regmap_bus,
719bea1064SDan Robertson 				  &spi->dev, &bma400_regmap_config);
729bea1064SDan Robertson 	if (IS_ERR(regmap)) {
739bea1064SDan Robertson 		dev_err(&spi->dev, "failed to create regmap\n");
749bea1064SDan Robertson 		return PTR_ERR(regmap);
759bea1064SDan Robertson 	}
769bea1064SDan Robertson 
779bea1064SDan Robertson 	/*
789bea1064SDan Robertson 	 * Per the bma400 datasheet, the first SPI read may
799bea1064SDan Robertson 	 * return garbage. As the datasheet recommends, the
809bea1064SDan Robertson 	 * chip ID register will be read here and checked
819bea1064SDan Robertson 	 * again in the following probe.
829bea1064SDan Robertson 	 */
839bea1064SDan Robertson 	ret = regmap_read(regmap, BMA400_CHIP_ID_REG, &val);
849bea1064SDan Robertson 	if (ret)
859bea1064SDan Robertson 		dev_err(&spi->dev, "Failed to read chip id register\n");
869bea1064SDan Robertson 
87*ffe0ab6aSJagath Jog J 	return bma400_probe(&spi->dev, regmap, spi->irq, id->name);
889bea1064SDan Robertson }
899bea1064SDan Robertson 
909bea1064SDan Robertson static const struct spi_device_id bma400_spi_ids[] = {
919bea1064SDan Robertson 	{ "bma400", 0 },
929bea1064SDan Robertson 	{ }
939bea1064SDan Robertson };
949bea1064SDan Robertson MODULE_DEVICE_TABLE(spi, bma400_spi_ids);
959bea1064SDan Robertson 
969bea1064SDan Robertson static const struct of_device_id bma400_of_spi_match[] = {
979bea1064SDan Robertson 	{ .compatible = "bosch,bma400" },
989bea1064SDan Robertson 	{ }
999bea1064SDan Robertson };
1009bea1064SDan Robertson MODULE_DEVICE_TABLE(of, bma400_of_spi_match);
1019bea1064SDan Robertson 
1029bea1064SDan Robertson static struct spi_driver bma400_spi_driver = {
1039bea1064SDan Robertson 	.driver = {
1049bea1064SDan Robertson 		.name = "bma400",
1059bea1064SDan Robertson 		.of_match_table = bma400_of_spi_match,
1069bea1064SDan Robertson 	},
1079bea1064SDan Robertson 	.probe    = bma400_spi_probe,
1089bea1064SDan Robertson 	.id_table = bma400_spi_ids,
1099bea1064SDan Robertson };
1109bea1064SDan Robertson 
1119bea1064SDan Robertson module_spi_driver(bma400_spi_driver);
1129bea1064SDan Robertson MODULE_AUTHOR("Dan Robertson <dan@dlrobertson.com>");
1139bea1064SDan Robertson MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor (SPI)");
1149bea1064SDan Robertson MODULE_LICENSE("GPL");
115f9c02c94SJonathan Cameron MODULE_IMPORT_NS(IIO_BMA400);
116