| 1 |
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (c) 2025 Robert Bosch GmbH. |
| 4 |
*/ |
| 5 |
#include <linux/mod_devicetable.h> |
| 6 |
#include <linux/module.h> |
| 7 |
#include <linux/regmap.h> |
| 8 |
#include <linux/spi/spi.h> |
| 9 |
|
| 10 |
#include "smi330.h" |
| 11 |
|
| 12 |
static int smi330_regmap_spi_read(void *context, const void *reg_buf, |
| 13 |
size_t reg_size, void *val_buf, |
| 14 |
size_t val_size) |
| 15 |
{ |
| 16 |
struct spi_device *spi = context; |
| 17 |
|
| 18 |
/* Insert pad byte for reading */ |
| 19 |
u8 reg[] = { *(u8 *)reg_buf, 0 }; |
| 20 |
|
| 21 |
if (reg_size + 1 != ARRAY_SIZE(reg)) { |
| 22 |
dev_err(&spi->dev, "Invalid register size %zu\n", reg_size); |
| 23 |
return -EINVAL; |
| 24 |
} |
| 25 |
|
| 26 |
return spi_write_then_read(spi, reg, ARRAY_SIZE(reg), val_buf, |
| 27 |
val_size); |
| 28 |
} |
| 29 |
|
| 30 |
static int smi330_regmap_spi_write(void *context, const void *data, |
| 31 |
size_t count) |
| 32 |
{ |
| 33 |
struct spi_device *spi = context; |
| 34 |
|
| 35 |
return spi_write(spi, data, count); |
| 36 |
} |
| 37 |
|
| 38 |
static const struct regmap_bus smi330_regmap_bus = { |
| 39 |
.read = smi330_regmap_spi_read, |
| 40 |
.write = smi330_regmap_spi_write, |
| 41 |
.read_flag_mask = 0x80, |
| 42 |
}; |
| 43 |
|
| 44 |
static int smi330_spi_probe(struct spi_device *spi) |
| 45 |
{ |
| 46 |
struct regmap *regmap; |
| 47 |
|
| 48 |
regmap = devm_regmap_init(&spi->dev, &smi330_regmap_bus, &spi->dev, |
| 49 |
&smi330_regmap_config); |
| 50 |
if (IS_ERR(regmap)) |
| 51 |
return dev_err_probe(&spi->dev, PTR_ERR(regmap), |
| 52 |
"Failed to initialize SPI Regmap\n"); |
| 53 |
|
| 54 |
return smi330_core_probe(&spi->dev, regmap); |
| 55 |
} |
| 56 |
|
| 57 |
static const struct spi_device_id smi330_spi_device_id[] = { |
| 58 |
{ .name = "smi330" }, |
| 59 |
{ } |
| 60 |
}; |
| 61 |
MODULE_DEVICE_TABLE(spi, smi330_spi_device_id); |
| 62 |
|
| 63 |
static const struct of_device_id smi330_of_match[] = { |
| 64 |
{ .compatible = "bosch,smi330" }, |
| 65 |
{ } |
| 66 |
}; |
| 67 |
MODULE_DEVICE_TABLE(of, smi330_of_match); |
| 68 |
|
| 69 |
static struct spi_driver smi330_spi_driver = { |
| 70 |
.probe = smi330_spi_probe, |
| 71 |
.id_table = smi330_spi_device_id, |
| 72 |
.driver = { |
| 73 |
.of_match_table = smi330_of_match, |
| 74 |
.name = "smi330_spi", |
| 75 |
}, |
| 76 |
}; |
| 77 |
module_spi_driver(smi330_spi_driver); |
| 78 |
|
| 79 |
MODULE_AUTHOR("Stefan Gutmann <[email protected]>"); |
| 80 |
MODULE_AUTHOR("Roman Huber <[email protected]>"); |
| 81 |
MODULE_AUTHOR("Filip Andrei <[email protected]>"); |
| 82 |
MODULE_AUTHOR("Drimbarean Avram Andrei <[email protected]>"); |
| 83 |
MODULE_DESCRIPTION("Bosch SMI330 SPI driver"); |
| 84 |
MODULE_LICENSE("Dual BSD/GPL"); |
| 85 |
MODULE_IMPORT_NS("IIO_SMI330"); |