[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240929155437.60115014@jic23-huawei>
Date: Sun, 29 Sep 2024 15:54:37 +0100
From: Jonathan Cameron <jic23@...nel.org>
To: Alex Lanzano <lanzano.alex@...il.com>
Cc: Lars-Peter Clausen <lars@...afoo.de>,
linux-kernel-mentees@...ts.linuxfoundation.org, skhan@...uxfoundation.org,
linux-kernel@...r.kernel.org, linux-iio@...r.kernel.org
Subject: Re: [PATCH] iio: imu: bmi270: Add spi driver for bmi270 imu
On Fri, 27 Sep 2024 14:37:10 -0400
Alex Lanzano <lanzano.alex@...il.com> wrote:
> Implement SPI driver for the Bosch BMI270 6-axis IMU. Provide raw read
> write access to acceleration and angle velocity measurements via the SPI
> interface on the device.
>
> Signed-off-by: Alex Lanzano <lanzano.alex@...il.com>
A few minor things inline but looks good in general.
Jonathan
> diff --git a/drivers/iio/imu/bmi270/bmi270.h b/drivers/iio/imu/bmi270/bmi270.h
> index 608b29ea58a3..8950e6234203 100644
> --- a/drivers/iio/imu/bmi270/bmi270.h
> +++ b/drivers/iio/imu/bmi270/bmi270.h
> @@ -4,11 +4,13 @@
> #define BMI270_H_
>
> #include <linux/regmap.h>
> +#include <linux/iio/iio.h>
>
> struct device;
> struct bmi270_data {
> struct device *dev;
> struct regmap *regmap;
> + __le16 sample __aligned(IIO_DMA_MINALIGN);
For the read path you are bouncing anyway, so the DMA_MINALIGN is only needed
for anything the write direction. Make the suggested change below and that
will bounce as well so that you don't need this.
> };
>
> extern const struct regmap_config bmi270_regmap_config;
> diff --git a/drivers/iio/imu/bmi270/bmi270_core.c b/drivers/iio/imu/bmi270/bmi270_core.c
> index 8e45343d6472..4decdad791d9 100644
> --- a/drivers/iio/imu/bmi270/bmi270_core.c
> +++ b/drivers/iio/imu/bmi270/bmi270_core.c
> @@ -66,16 +66,9 @@ enum bmi270_scan {
> BMI270_SCAN_GYRO_Z,
> };
>
> -const struct regmap_config bmi270_regmap_config = {
> - .reg_bits = 8,
> - .val_bits = 8,
> -};
> -EXPORT_SYMBOL_NS_GPL(bmi270_regmap_config, IIO_BMI270);
> -
> static int bmi270_get_data(struct bmi270_data *bmi270_device,
> int chan_type, int axis, int *val)
> {
> - __le16 sample;
> int reg;
> int ret;
>
> @@ -90,11 +83,13 @@ static int bmi270_get_data(struct bmi270_data *bmi270_device,
> return -EINVAL;
> }
>
> - ret = regmap_bulk_read(bmi270_device->regmap, reg, &sample, sizeof(sample));
> + ret = regmap_bulk_read(bmi270_device->regmap, reg,
> + &bmi270_device->sample,
> + sizeof(bmi270_device->sample));
> if (ret)
> return ret;
>
> - *val = sign_extend32(le16_to_cpu(sample), 15);
> + *val = sign_extend32(le16_to_cpu(bmi270_device->sample), 15);
>
> return 0;
> }
> diff --git a/drivers/iio/imu/bmi270/bmi270_i2c.c b/drivers/iio/imu/bmi270/bmi270_i2c.c
> index f70dee2d8a64..ce8279ae90cd 100644
> --- a/drivers/iio/imu/bmi270/bmi270_i2c.c
> +++ b/drivers/iio/imu/bmi270/bmi270_i2c.c
> @@ -9,12 +9,17 @@
>
> #include "bmi270.h"
>
> +const struct regmap_config bmi270_i2c_regmap_config = {
static const
(same for spi one)
> + .reg_bits = 8,
> + .val_bits = 8,
> +};
> +
> static int bmi270_i2c_probe(struct i2c_client *client)
> {
> struct regmap *regmap;
> struct device *dev = &client->dev;
>
> - regmap = devm_regmap_init_i2c(client, &bmi270_regmap_config);
> + regmap = devm_regmap_init_i2c(client, &bmi270_i2c_regmap_config);
> if (IS_ERR(regmap))
> return dev_err_probe(dev, PTR_ERR(regmap),
> "Failed to init i2c regmap");
> diff --git a/drivers/iio/imu/bmi270/bmi270_spi.c b/drivers/iio/imu/bmi270/bmi270_spi.c
> new file mode 100644
> index 000000000000..906b9b852a09
> --- /dev/null
> +++ b/drivers/iio/imu/bmi270/bmi270_spi.c
> @@ -0,0 +1,89 @@
> +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +
> +#include <linux/module.h>
> +#include <linux/spi/spi.h>
> +#include <linux/iio/iio.h>
> +#include <linux/module.h>
> +#include <linux/mod_devicetable.h>
> +#include <linux/regmap.h>
Alphabetical order preferred.
> +
> +#include "bmi270.h"
> +
> +/*
> + * The following two functions are taken from the BMI323 spi driver code.
> + * In section 6.4 of the BMI270 data it specifies that after a read
> + * operation the first data byte from the device is a dummy byte
> + */
> +static int bmi270_regmap_spi_read(void *context, const void *reg_buf,
> + size_t reg_size, void *val_buf,
> + size_t val_size)
> +{
> + struct spi_device *spi = context;
I'd be tempted to rename the input parameter context to spi and then
parse it directly to the spi_write_then_read()
> +
> + return spi_write_then_read(spi, reg_buf, reg_size, val_buf, val_size);
> +}
> +
> +static int bmi270_regmap_spi_write(void *context, const void *data,
> + size_t count)
> +{
> + struct spi_device *spi = context;
> + u8 *data_buff = (u8 *)data;
> +
> + /*
> + * Remove the extra pad byte since its only needed for the read
> + * operation
> + */
> + data_buff[1] = data_buff[0];
> + return spi_write(spi, data_buff + 1, count - 1);
That needs a DMA safe buffer (unlike write_then_read which always
bounces). I'd avoid that complexity by using spi_write_then_read
here as well but set the read to 0 length and pass NULL for the buffer.
That function is intended to be used like this as it special cases 0
length for either write or read buffers.
> +}
Powered by blists - more mailing lists